Cleanup – Dropping Support for Python 2¶
When users of your C extension are not using Python 2 any more, or you need to use one of Python 3’s irresistible new features, you can convert the project to use Python 3 only. As mentioned earlier, it is usually not a good idea to do this until you have full support for both Pythons.
With py3c, dropping Python 2 basically amounts to expanding all its compat
macros.
In other words, remove the py3c.h
header, and fix the compile errors.
Convert
PyStr_*
toPyUnicode_*
;PyInt_*
toPyLong_*
.Instead of
MODULE_INIT_FUNC(<name>)
, write:PyMODINIT_FUNC PyInit_<name>(void); PyMODINIT_FUNC PyInit_<name>(void)
Remove
Py_TPFLAGS_HAVE_WEAKREFS
andPy_TPFLAGS_HAVE_ITER
(py3c defines them as 0).Replace
return PY3C_RICHCMP
byPy_RETURN_RICHCOMPARE()
, as mentioned inPY3C_RICHCMP
documentation.Replace
Py_RETURN_RICHCOMPARE()
andPy_UNREACHABLE()
by its expansion, unless you either support Python 3.7+ only or keep thepy3c/comparison.h
header.Replace
Py_RETURN_NOTIMPLEMENTED()
by its expansion, unless you either support Python 3.3+ only or keep thepy3c/comparison.h
header.Drop
capsulethunk.h
, if you’re using it.Remove any code in
#if !IS_PY3
blocks, and the ifs around#if IS_PY3
ones.
You will want to check the code as you’re doing this.
For example, replacing PyLong can easily result in code like
if (PyInt_Check(o) || PyInt_Check(o))
.
Enjoy your Python 3-compatible extension!