[pypy-svn] pypy default: (bivab, arigo)

arigo commits-noreply at bitbucket.org
Thu Feb 24 15:23:06 CET 2011


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r42256:c4568a7b5237
Date: 2011-02-24 15:22 +0100
http://bitbucket.org/pypy/pypy/changeset/c4568a7b5237/

Log:	(bivab, arigo)

	Oups. A bug in the C translation backend. Only shows up when we
	have a switch on characters, whose cases are >= 128.

diff --git a/pypy/translator/c/test/test_backendoptimized.py b/pypy/translator/c/test/test_backendoptimized.py
--- a/pypy/translator/c/test/test_backendoptimized.py
+++ b/pypy/translator/c/test/test_backendoptimized.py
@@ -190,6 +190,22 @@
             y = ord(x)
             assert fn(y) == f(y)
 
+    def test_char_may_be_signed(self):
+        def f(n):
+            case = chr(n)
+            if case == '\xFF': return 1
+            if case == '\xFE': return 2
+            if case == '\xFD': return 3
+            if case == '\xFC': return 4
+            if case == '\xFB': return 5
+            if case == '\xFA': return 6
+            return 7
+        codegenerator = self.CodeGenerator()
+        fn = codegenerator.getcompiled(f, [int])
+        for input, expected in [(255, 1), (253, 3), (251, 5), (161, 7)]:
+            res = fn(input)
+            assert res == expected
+
     def test_unichr_switch(self):
         def f(y):
             x = unichr(y)

diff --git a/pypy/translator/c/primitive.py b/pypy/translator/c/primitive.py
--- a/pypy/translator/c/primitive.py
+++ b/pypy/translator/c/primitive.py
@@ -120,7 +120,7 @@
     if ' ' <= value < '\x7f':
         return "'%s'" % (value.replace("\\", r"\\").replace("'", r"\'"),)
     else:
-        return '%d' % ord(value)
+        return '((char)%d)' % ord(value)
 
 def name_bool(value, db):
     return '%d' % value


More information about the Pypy-commit mailing list