[pypy-commit] pypy default: 'cast_primitive' to a bool: it was not working on non-GNU systems where

arigo noreply at buildbot.pypy.org
Fri Jun 15 17:29:32 CEST 2012


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r55686:1ef5ed3cd321
Date: 2012-06-15 17:29 +0200
http://bitbucket.org/pypy/pypy/changeset/1ef5ed3cd321/

Log:	'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.

diff --git a/pypy/translator/c/funcgen.py b/pypy/translator/c/funcgen.py
--- a/pypy/translator/c/funcgen.py
+++ b/pypy/translator/c/funcgen.py
@@ -716,12 +716,14 @@
     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()
 
diff --git a/pypy/translator/c/test/test_typed.py b/pypy/translator/c/test/test_typed.py
--- a/pypy/translator/c/test/test_typed.py
+++ b/pypy/translator/c/test/test_typed.py
@@ -895,3 +895,12 @@
         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


More information about the pypy-commit mailing list