[pypy-svn] pypy bytearray: (mfoord) ord(bytearray)

mfoord commits-noreply at bitbucket.org
Fri Jan 21 12:47:00 CET 2011


Author: Michael Foord <michael at voidspace.org.uk>
Branch: bytearray
Changeset: r41122:daee56de2bf1
Date: 2011-01-21 12:27 +0100
http://bitbucket.org/pypy/pypy/changeset/daee56de2bf1/

Log:	(mfoord) ord(bytearray)

diff --git a/pypy/objspace/std/bytearrayobject.py b/pypy/objspace/std/bytearrayobject.py
--- a/pypy/objspace/std/bytearrayobject.py
+++ b/pypy/objspace/std/bytearrayobject.py
@@ -88,6 +88,13 @@
     result = len(w_bytearray.data)
     return wrapint(space, result)
 
+def ord__Bytearray(space, w_bytearray):
+    if len(w_bytearray.data) != 1:
+        raise OperationError(space.w_TypeError,
+                             space.wrap("expected a character, but string"
+                            "of length %s found" % len(w_bytearray.data)))
+    return space.wrap(ord(w_bytearray.data[0]))
+
 def getitem__Bytearray_ANY(space, w_bytearray, w_index):
     # getindex_w should get a second argument space.w_IndexError,
     # but that doesn't exist the first time this is called.

diff --git a/pypy/objspace/std/test/test_bytes.py b/pypy/objspace/std/test/test_bytes.py
--- a/pypy/objspace/std/test/test_bytes.py
+++ b/pypy/objspace/std/test/test_bytes.py
@@ -79,6 +79,13 @@
         assert bytearray('ll') in bytearray('hello')
         assert memoryview('ll') in bytearray('hello')
 
+    def test_ord(self):
+        b = bytearray('\0A\x7f\x80\xff')
+        assert ([ord(b[i:i+1]) for i in range(len(b))] ==
+                         [0, 65, 127, 128, 255])
+        raises(TypeError, ord, bytearray('ll'))
+        raises(TypeError, ord, bytearray())
+
     def test_translate(self):
         b = 'hello'
         ba = bytearray(b)


More information about the Pypy-commit mailing list