summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2007-05-08 21:05:48 (GMT)
committerGuido van Rossum <guido@python.org>2007-05-08 21:05:48 (GMT)
commitf9e91c9c58de72afdf51f2a6ebfe50e98beeaa78 (patch)
treef2dd45d9585bc92e27c45e345fae3e906e3f2204
parent2b08b38deacc065b4fea8421528de1eed66d56b0 (diff)
downloadcpython-f9e91c9c58de72afdf51f2a6ebfe50e98beeaa78.zip
cpython-f9e91c9c58de72afdf51f2a6ebfe50e98beeaa78.tar.gz
cpython-f9e91c9c58de72afdf51f2a6ebfe50e98beeaa78.tar.bz2
Given that ord() of a bytes object of length 1 is defined, it should
never return a negative number.
-rw-r--r--Lib/test/test_builtin.py9
-rw-r--r--Lib/test/test_bytes.py5
-rw-r--r--Python/bltinmodule.c2
3 files changed, 15 insertions, 1 deletions
diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py
index 58cb29b..a11e40a 100644
--- a/Lib/test/test_builtin.py
+++ b/Lib/test/test_builtin.py
@@ -1383,6 +1383,15 @@ class BuiltinTest(unittest.TestCase):
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)
diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py
index ded0491..33a4b0d 100644
--- a/Lib/test/test_bytes.py
+++ b/Lib/test/test_bytes.py
@@ -671,6 +671,11 @@ class BytesTest(unittest.TestCase):
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)
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c
index 264470b..aa0d0df 100644
--- a/Python/bltinmodule.c
+++ b/Python/bltinmodule.c
@@ -1482,7 +1482,7 @@ builtin_ord(PyObject *self, PyObject* obj)
/* 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);
}
}