Commits

Armin Rigo committed 1ef5ed3

'cast_primitive' to a bool: it was not working on non-GNU systems where
bools are implemented with a "char". This cast should really result in
either 0 or 1.

Comments (0)

Files changed (2)

pypy/translator/c/funcgen.py

     def OP_CAST_PRIMITIVE(self, op):
         TYPE = self.lltypemap(op.result)
         val =  self.expr(op.args[0])
+        result = self.expr(op.result)
+        if TYPE == Bool:
+            return "%(result)s = !!%(val)s;" % locals()
         ORIG = self.lltypemap(op.args[0])
         if ORIG is Char:
             val = "(unsigned char)%s" % val
         elif ORIG is UniChar:
             val = "(unsigned long)%s" % val
-        result = self.expr(op.result)
         typename = cdecl(self.db.gettype(TYPE), '')        
         return "%(result)s = (%(typename)s)(%(val)s);" % locals()
 

pypy/translator/c/test/test_typed.py

         f = self.getcompiled(func, [int])
         res = f(-2000000000)
         assert res == -200000000000000
+
+    def test_bool_2(self):
+        from pypy.rpython.lltypesystem import lltype, rffi
+        def func(n):
+            x = rffi.cast(lltype.Bool, n)
+            return int(x)
+        f = self.getcompiled(func, [int])
+        res = f(2)
+        assert res == 1     # and not 2