[pypy-commit] pypy default: test, fix - any str subtype should never have tp_as_a_number.* functions set

mattip pypy.commits at gmail.com
Sun Aug 27 13:22:20 EDT 2017


Author: Matti Picus <matti.picus at gmail.com>
Branch: 
Changeset: r92268:e45fdeb7813a
Date: 2017-08-26 20:02 +0300
http://bitbucket.org/pypy/pypy/changeset/e45fdeb7813a/

Log:	test, fix - any str subtype should never have tp_as_a_number.*
	functions set

diff --git a/pypy/module/cpyext/test/test_bytesobject.py b/pypy/module/cpyext/test/test_bytesobject.py
--- a/pypy/module/cpyext/test/test_bytesobject.py
+++ b/pypy/module/cpyext/test/test_bytesobject.py
@@ -367,6 +367,16 @@
              """
                 return PyLong_FromLong(PyObject_Size(args));
              """),
+            ('has_nb_add', "METH_O",
+             '''
+                if (args->ob_type->tp_as_number == NULL) {
+                    Py_RETURN_FALSE;
+                }
+                if (args->ob_type->tp_as_number->nb_add == NULL) {
+                    Py_RETURN_FALSE;
+                }
+                Py_RETURN_TRUE;
+             '''),
             ], prologue="""
                 #include <Python.h>
                 PyTypeObject PyStringArrType_Type = {
@@ -447,6 +457,8 @@
             ''')
 
         a = module.newsubstr('abc')
+        assert module.has_nb_add('a') is False
+        assert module.has_nb_add(a) is False
         assert type(a).__name__ == 'string_'
         assert a == 'abc'
         assert 3 == module.get_len(a)
diff --git a/pypy/module/cpyext/typeobject.py b/pypy/module/cpyext/typeobject.py
--- a/pypy/module/cpyext/typeobject.py
+++ b/pypy/module/cpyext/typeobject.py
@@ -309,13 +309,17 @@
                 setattr(pto, slot_names[0], slot_func_helper)
         elif ((w_type is space.w_list or w_type is space.w_tuple) and
               slot_names[0] == 'c_tp_as_number'):
-            # XXX hack - hwo can we generalize this? The problem is method
+            # XXX hack - how can we generalize this? The problem is method
             # names like __mul__ map to more than one slot, and we have no
             # convenient way to indicate which slots CPython have filled
             #
             # We need at least this special case since Numpy checks that
             # (list, tuple) do __not__ fill tp_as_number
             pass
+        elif (space.issubtype_w(w_type, space.w_basestring) and
+                slot_names[0] == 'c_tp_as_number'):
+            # like above but for any str type
+            pass
         else:
             assert len(slot_names) == 2
             struct = getattr(pto, slot_names[0])


More information about the pypy-commit mailing list