[Python-checkins] [3.8] bpo-37250: put back tp_print for backwards compatibility (GH-14193)

Petr Viktorin webhook-mailer at python.org
Tue Jun 25 06:59:03 EDT 2019


https://github.com/python/cpython/commit/d917cfe4051d45b2b755c726c096ecfcc4869ceb
commit: d917cfe4051d45b2b755c726c096ecfcc4869ceb
branch: 3.8
author: Jeroen Demeyer <J.Demeyer at UGent.be>
committer: Petr Viktorin <encukou at gmail.com>
date: 2019-06-25T12:58:58+02:00
summary:

[3.8] bpo-37250: put back tp_print for backwards compatibility (GH-14193)

This is a 3.8-only compatibility measure for third-party Cython-based sdists.

https://bugs.python.org/issue37250

files:
A Misc/NEWS.d/next/C API/2019-06-12-11-45-36.bpo-37221.RhP1E7.rst
M Include/cpython/object.h
M Lib/test/test_sys.py
M Modules/_testcapimodule.c

diff --git a/Include/cpython/object.h b/Include/cpython/object.h
index a65aaf648215..5a0ac4afdae8 100644
--- a/Include/cpython/object.h
+++ b/Include/cpython/object.h
@@ -256,6 +256,9 @@ typedef struct _typeobject {
     destructor tp_finalize;
     vectorcallfunc tp_vectorcall;
 
+    /* bpo-37250: kept for backwards compatibility in CPython 3.8 only */
+    Py_DEPRECATED(3.8) int (*tp_print)(PyObject *, FILE *, int);
+
 #ifdef COUNT_ALLOCS
     /* these must be last and never explicitly initialized */
     Py_ssize_t tp_allocs;
diff --git a/Lib/test/test_sys.py b/Lib/test/test_sys.py
index 49f2722d9514..a7df7a2fc8c9 100644
--- a/Lib/test/test_sys.py
+++ b/Lib/test/test_sys.py
@@ -1275,7 +1275,7 @@ def delx(self): del self.__x
         check((1,2,3), vsize('') + 3*self.P)
         # type
         # static type: PyTypeObject
-        fmt = 'P2nPI13Pl4Pn9Pn11PIPP'
+        fmt = 'P2nPI13Pl4Pn9Pn11PIPPP'
         if hasattr(sys, 'getcounts'):
             fmt += '3n2P'
         s = vsize(fmt)
diff --git a/Misc/NEWS.d/next/C API/2019-06-12-11-45-36.bpo-37221.RhP1E7.rst b/Misc/NEWS.d/next/C API/2019-06-12-11-45-36.bpo-37221.RhP1E7.rst
new file mode 100644
index 000000000000..96b61dea0d9c
--- /dev/null
+++ b/Misc/NEWS.d/next/C API/2019-06-12-11-45-36.bpo-37221.RhP1E7.rst	
@@ -0,0 +1,4 @@
+``tp_print`` is put back at the end of the ``PyTypeObject`` structure
+to restore support for old code (in particular generated by Cython)
+setting ``tp_print = 0``.
+Note that ``tp_print`` will be removed entirely in Python 3.9.
diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c
index 07aadea3e983..1042ea701923 100644
--- a/Modules/_testcapimodule.c
+++ b/Modules/_testcapimodule.c
@@ -6008,6 +6008,10 @@ PyInit__testcapi(void)
     Py_INCREF(&MyList_Type);
     PyModule_AddObject(m, "MyList", (PyObject *)&MyList_Type);
 
+    /* bpo-37250: old Cython code sets tp_print to 0, we check that
+     * this doesn't break anything. */
+    MyList_Type.tp_print = 0;
+
     if (PyType_Ready(&MethodDescriptorBase_Type) < 0)
         return NULL;
     Py_INCREF(&MethodDescriptorBase_Type);



More information about the Python-checkins mailing list