[pypy-commit] pypy cpyext-ext: test and fix: slot_tp_str

arigo pypy.commits at gmail.com
Tue Mar 1 15:08:16 EST 2016


Author: Armin Rigo <arigo at tunes.org>
Branch: cpyext-ext
Changeset: r82644:b530a83f16e8
Date: 2016-03-01 20:53 +0100
http://bitbucket.org/pypy/pypy/changeset/b530a83f16e8/

Log:	test and fix: slot_tp_str

diff --git a/pypy/module/cpyext/slotdefs.py b/pypy/module/cpyext/slotdefs.py
--- a/pypy/module/cpyext/slotdefs.py
+++ b/pypy/module/cpyext/slotdefs.py
@@ -337,10 +337,6 @@
     return 0
 
 @cpython_api([PyObject], PyObject, header=None)
-def slot_tp_str(space, w_self):
-    return space.str(w_self)
-
- at cpython_api([PyObject], PyObject, header=None)
 def slot_nb_int(space, w_self):
     return space.int(w_self)
 
@@ -423,6 +419,17 @@
             return space.call_args(call_fn, args)
         api_func = slot_tp_call.api_func
 
+    elif name == 'tp_str':
+        str_fn = w_type.getdictvalue(space, '__str__')
+        if str_fn is None:
+            return
+
+        @cpython_api([PyObject], PyObject, header=header)
+        @func_renamer("cpyext_%s_%s" % (name.replace('.', '_'), typedef.name))
+        def slot_tp_str(space, w_self):
+            return space.call_function(str_fn, w_self)
+        api_func = slot_tp_str.api_func
+
     else:
         return
 
diff --git a/pypy/module/cpyext/test/test_typeobject.py b/pypy/module/cpyext/test/test_typeobject.py
--- a/pypy/module/cpyext/test/test_typeobject.py
+++ b/pypy/module/cpyext/test/test_typeobject.py
@@ -551,21 +551,27 @@
 
     def test_tp_str(self):
         module = self.import_extension('foo', [
-           ("tp_str", "METH_O",
+           ("tp_str", "METH_VARARGS",
             '''
-                 if (!args->ob_type->tp_str)
+                 PyTypeObject *type = (PyTypeObject *)PyTuple_GET_ITEM(args, 0);
+                 PyObject *obj = PyTuple_GET_ITEM(args, 1);
+                 if (!type->tp_str)
                  {
                      PyErr_SetNone(PyExc_ValueError);
                      return NULL;
                  }
-                 return args->ob_type->tp_str(args);
+                 return type->tp_str(obj);
              '''
              )
             ])
         class C:
             def __str__(self):
                 return "text"
-        assert module.tp_str(C()) == "text"
+        assert module.tp_str(type(C()), C()) == "text"
+        class D(int):
+            def __str__(self):
+                return "more text"
+        assert module.tp_str(int, D(42)) == "42"
 
     def test_mp_ass_subscript(self):
         module = self.import_extension('foo', [


More information about the pypy-commit mailing list