[pypy-svn] r58718 - in pypy/branch/2.5-merge/pypy/lib: . test2

arigo at codespeak.net arigo at codespeak.net
Tue Oct 7 13:09:10 CEST 2008


Author: arigo
Date: Tue Oct  7 13:09:06 2008
New Revision: 58718

Modified:
   pypy/branch/2.5-merge/pypy/lib/array.py
   pypy/branch/2.5-merge/pypy/lib/test2/test_array.py
Log:
Test and fix: array.array can now be subclassed, with new keyword arguments
added in the subclass's __init__.  Requires ad-hoc hacks in the base
class's __new__, just like in CPython.


Modified: pypy/branch/2.5-merge/pypy/lib/array.py
==============================================================================
--- pypy/branch/2.5-merge/pypy/lib/array.py	(original)
+++ pypy/branch/2.5-merge/pypy/lib/array.py	Tue Oct  7 13:09:06 2008
@@ -91,8 +91,10 @@
     """
     __slots__ = ["typecode", "itemsize", "_data", "_descriptor", "__weakref__"]
 
-    def __new__(cls, typecode, initializer=[]):
+    def __new__(cls, typecode, initializer=[], **extrakwds):
         self = object.__new__(cls)
+        if cls is array and extrakwds:
+            raise TypeError("array() does not take keyword arguments")
         if not isinstance(typecode, str) or len(typecode) != 1:
             raise TypeError(
                      "array() argument 1 must be char, not %s" % type(typecode))

Modified: pypy/branch/2.5-merge/pypy/lib/test2/test_array.py
==============================================================================
--- pypy/branch/2.5-merge/pypy/lib/test2/test_array.py	(original)
+++ pypy/branch/2.5-merge/pypy/lib/test2/test_array.py	Tue Oct  7 13:09:06 2008
@@ -2,6 +2,7 @@
 
 import autopath
 import py
+from py.test import raises
 import struct
 from pypy.conftest import gettestobjspace
 
@@ -31,13 +32,44 @@
         assert a.tolist() == [unichr(9999)]
 
     def test_pickle(self):
+        import sys
+        if sys.version_info < (2, 5):
+            py.test.skip("array.array not picklable before python 2.5")
         import pickle
+
         for content in [[56, -12, 34], []]:
             a = self.array.array('i', content)
             a2 = pickle.loads(pickle.dumps(a))
             assert type(a2) is self.array.array
             assert list(a2) == content
 
+    def test_init_vs_new(self):
+        import sys
+        if sys.version_info < (2, 5):
+            py.test.skip("array.array constructor changed in 2.5")
+        class A(self.array.array):
+            def __init__(self, *args, **kwds):
+                self.args = args
+                self.kwds = kwds
+
+        a = A('c', foo='bar')
+        assert a.args == ('c',)
+        assert a.kwds == {'foo': 'bar'}
+        a = A('i', range(10), some=42)
+        assert a.args == ('i', range(10))
+        assert a.kwds == {'some': 42}
+        raises(TypeError, A)
+        raises(TypeError, A, 42)
+        raises(TypeError, A, 'i', [], [])
+        raises(TypeError, self.array.array, 'i', [], foo='bar')
+
+
+class TestCPythonsOwnArray(BaseArrayTests):
+
+    def setup_class(cls):
+        import array
+        cls.array = array
+
 
 class TestArrayOnTopOfCPython(BaseArrayTests):
 



More information about the Pypy-commit mailing list