Author: Armin Rigo <[email protected]>
Branch: py3.5
Changeset: r88470:91461751edba
Date: 2016-11-18 16:44 +0100
http://bitbucket.org/pypy/pypy/changeset/91461751edba/

Log:    bytearray.__alloc__()

diff --git a/lib-python/3/test/test_bytes.py b/lib-python/3/test/test_bytes.py
--- a/lib-python/3/test/test_bytes.py
+++ b/lib-python/3/test/test_bytes.py
@@ -1095,7 +1095,6 @@
         self.assertEqual(b, b1)
         self.assertTrue(b is b1)
 
-    @test.support.impl_detail("undocumented bytes.__alloc__()")
     def test_alloc(self):
         b = bytearray()
         alloc = b.__alloc__()
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
@@ -452,6 +452,9 @@
         assert isinstance(self, W_BytearrayObject)
         return self._getitem_result(space, index)
 
+    def descr_alloc(self, space):
+        return space.wrap(self._len() + 1)
+
 
 # ____________________________________________________________
 # helpers for slow paths, moved out because they contain loops
@@ -552,7 +555,9 @@
     def __alloc__():
         """B.__alloc__() -> int
 
-        Return the number of bytes actually allocated.
+        CPython compatibility: return len(B) + 1.
+        (The allocated size might be bigger, but getting it is
+        involved and may create pointless compatibility troubles.)
         """
 
     def __contains__():
@@ -967,6 +972,7 @@
 
     def hex():
         """B.hex() -> unicode
+
         Return a string object containing two hexadecimal digits
         for each byte in the instance B.
         """
@@ -1124,6 +1130,8 @@
                          doc=BytearrayDocstrings.copy.__doc__),
     hex = interp2app(W_BytearrayObject.descr_hex,
                            doc=BytearrayDocstrings.hex.__doc__),
+    __alloc__ = interp2app(W_BytearrayObject.descr_alloc,
+                           doc=BytearrayDocstrings.__alloc__.__doc__),
 )
 W_BytearrayObject.typedef.flag_sequence_bug_compat = True
 
diff --git a/pypy/objspace/std/test/test_bytearrayobject.py 
b/pypy/objspace/std/test/test_bytearrayobject.py
--- a/pypy/objspace/std/test/test_bytearrayobject.py
+++ b/pypy/objspace/std/test/test_bytearrayobject.py
@@ -548,3 +548,7 @@
 
     def test_format_bytes(self):
         assert bytearray(b'<%s>') % b'abc' == b'<abc>'
+
+    def test___alloc__(self):
+        # pypy: always returns len()+1; cpython: may be bigger
+        assert bytearray(b'123456').__alloc__() >= 7
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to