]> granicus.if.org Git - python/commitdiff
SF bug [#470040] ParseTuple t# vs subclasses.
authorTim Peters <tim.peters@gmail.com>
Fri, 12 Oct 2001 02:38:24 +0000 (02:38 +0000)
committerTim Peters <tim.peters@gmail.com>
Fri, 12 Oct 2001 02:38:24 +0000 (02:38 +0000)
inherit_slots():  tp_as_buffer was getting inherited as if it were a
method pointer, rather than a pointer to a vector of method pointers.  As
a result, inheriting from a type that implemented buffer methods was
ineffective, leaving all the tp_as_buffer slots NULL in the subclass.

Lib/test/test_descr.py
Objects/typeobject.c

index 1768a6d3d0a5453b608d70a427253a32d004f107..e5ee591466841ab6506fe4270704d14ae51205c8 100644 (file)
@@ -2298,7 +2298,37 @@ def subclasspropagation():
         pass
     else:
         raise TestFailed, "d.foo should be undefined now"
-    
+
+def buffer_inherit():
+    import binascii
+    # SF bug [#470040] ParseTuple t# vs subclasses.
+    if verbose:
+        print "Testing that buffer interface is inherited ..."
+
+    class MyStr(str):
+        pass
+    base = 'abc'
+    m = MyStr(base)
+    # b2a_hex uses the buffer interface to get its argument's value, via
+    # PyArg_ParseTuple 't#' code.
+    vereq(binascii.b2a_hex(m), binascii.b2a_hex(base))
+
+    # It's not clear that unicode will continue to support the character
+    # buffer interface, and this test will fail if that's taken away.
+    class MyUni(unicode):
+        pass
+    base = u'abc'
+    m = MyUni(base)
+    vereq(binascii.b2a_hex(m), binascii.b2a_hex(base))
+
+    class MyInt(int):
+        pass
+    m = MyInt(42)
+    try:
+        binascii.b2a_hex(m)
+        raise TestFailed('subclass of int should not have a buffer interface')
+    except TypeError:
+        pass
 
 def test_main():
     class_docstrings()
@@ -2347,6 +2377,7 @@ def test_main():
     copies()
     binopoverride()
     subclasspropagation()
+    buffer_inherit()
     if verbose: print "All OK"
 
 if __name__ == "__main__":
index ced80ec21f68ae292d0d99632f4b125711c0a9b4..a498f0f64bf4224090c7ed579d1e304a77775216 100644 (file)
@@ -1657,6 +1657,7 @@ inherit_slots(PyTypeObject *type, PyTypeObject *base)
 #define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT)
 #define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT)
 #define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT)
+#define COPYBUF(SLOT) COPYSLOT(tp_as_buffer->SLOT)
 
        /* This won't inherit indirect slots (from tp_as_number etc.)
           if type doesn't provide the space. */
@@ -1732,6 +1733,16 @@ inherit_slots(PyTypeObject *type, PyTypeObject *base)
                COPYMAP(mp_ass_subscript);
        }
 
+       if (type->tp_as_buffer != NULL && base->tp_as_buffer != NULL) {
+               basebase = base->tp_base;
+               if (basebase->tp_as_buffer == NULL)
+                       basebase = NULL;
+               COPYBUF(bf_getreadbuffer);
+               COPYBUF(bf_getwritebuffer);
+               COPYBUF(bf_getsegcount);
+               COPYBUF(bf_getcharbuffer);
+       }
+
        basebase = base->tp_base;
 
        COPYSLOT(tp_dealloc);
@@ -1749,7 +1760,6 @@ inherit_slots(PyTypeObject *type, PyTypeObject *base)
        /* tp_hash see tp_richcompare */
        COPYSLOT(tp_call);
        COPYSLOT(tp_str);
-       COPYSLOT(tp_as_buffer);
        if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE) {
                if (type->tp_compare == NULL &&
                    type->tp_richcompare == NULL &&