Author: guido.van.rossum
Date: Tue May  8 23:05:48 2007
New Revision: 55193

Modified:
   python/branches/py3k-struni/Lib/test/test_builtin.py
   python/branches/py3k-struni/Lib/test/test_bytes.py
   python/branches/py3k-struni/Python/bltinmodule.c
Log:
Given that ord() of a bytes object of length 1 is defined, it should
never return a negative number.


Modified: python/branches/py3k-struni/Lib/test/test_builtin.py
==============================================================================
--- python/branches/py3k-struni/Lib/test/test_builtin.py        (original)
+++ python/branches/py3k-struni/Lib/test/test_builtin.py        Tue May  8 
23:05:48 2007
@@ -1383,6 +1383,15 @@
         self.assertEqual(ord(' '), 32)
         self.assertEqual(ord('A'), 65)
         self.assertEqual(ord('a'), 97)
+        self.assertEqual(ord('\x80'), 128)
+        self.assertEqual(ord('\xff'), 255)
+
+        self.assertEqual(ord(b' '), 32)
+        self.assertEqual(ord(b'A'), 65)
+        self.assertEqual(ord(b'a'), 97)
+        self.assertEqual(ord(b'\x80'), 128)
+        self.assertEqual(ord(b'\xff'), 255)
+
         if have_unicode:
             self.assertEqual(ord(chr(sys.maxunicode)), sys.maxunicode)
         self.assertRaises(TypeError, ord, 42)

Modified: python/branches/py3k-struni/Lib/test/test_bytes.py
==============================================================================
--- python/branches/py3k-struni/Lib/test/test_bytes.py  (original)
+++ python/branches/py3k-struni/Lib/test/test_bytes.py  Tue May  8 23:05:48 2007
@@ -671,6 +671,11 @@
         self.assertEqual(b.rstrip(b'im'), b'mississipp')
         self.assertEqual(b.rstrip(b'pim'), b'mississ')
 
+    def test_ord(self):
+        b = b'\0A\x7f\x80\xff'
+        self.assertEqual([ord(b[i:i+1]) for i in range(len(b))],
+                         [0, 65, 127, 128, 255])
+
     # Optimizations:
     # __iter__? (optimization)
     # __reversed__? (optimization)

Modified: python/branches/py3k-struni/Python/bltinmodule.c
==============================================================================
--- python/branches/py3k-struni/Python/bltinmodule.c    (original)
+++ python/branches/py3k-struni/Python/bltinmodule.c    Tue May  8 23:05:48 2007
@@ -1482,7 +1482,7 @@
                /* XXX Hopefully this is temporary */
                size = PyBytes_GET_SIZE(obj);
                if (size == 1) {
-                       ord = (long)*PyBytes_AS_STRING(obj);
+                       ord = (long)((unsigned char)*PyBytes_AS_STRING(obj));
                        return PyInt_FromLong(ord);
                }
        }
_______________________________________________
Python-3000-checkins mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-3000-checkins

Reply via email to