Differences between revisions 4 and 5
Revision 4 as of 2008-07-27 07:35:12
Size: 2544
Editor: 9
Comment:
Revision 5 as of 2008-07-30 13:26:01
Size: 2547
Editor: 121-73-93-163
Comment: english tweaks
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
This is a list of suggestion about the migration of Python C extension module to Python 3.0. Feel free to expand the list! This is a list of suggestions about the migration of Python C extension modules to Python 3.0. Feel free to expand the list!
Line 5: Line 5:
 * Now Python 3 uses Unicode as internal string representation, all the class name, method name, etc are Unicode string. So check all the occurrences of PyString_*, most of them should be replaced as PyUnicode_*. When you suffered segmentation fault, sometime it because you provided a PyString where the C-API expecting a PyUnicode. Also, for the returning value of your C extension function, you may use PyUnicode instead of PyString, otherwise you may broken your client's code.  * Now Python 3 uses Unicode as internal string representation, all the class name, method name, etc are Unicode strings. So check all the occurrences of PyString_*, most of them should be replaced with PyUnicode_*. When you suffer a segmentation fault, sometimes it's because you provided a PyString where the C-API expecting a PyUnicode. Also, for the return value of your C extension function, you may use PyUnicode instead of PyString, otherwise you may broken your client's code.
Line 7: Line 7:
 * PyInt_* removed, but you can explicitely ``#include <intobject.h>`` to define aliases that redirect most PyInt_* symbols to PyLong_*.  * PyInt_* removed, but you can explicitly ``#include <intobject.h>`` to define aliases that redirect most PyInt_* symbols to PyLong_*.
Line 42: Line 42:
 * In py3k dict.items() method returns a dict_items object, you can uses PySequence_Fast() to convert it to a sequence.  * In py3k dict.items() method returns a dict_items object, you can use PySequence_Fast() to convert it to a sequence.

This is a list of suggestions about the migration of Python C extension modules to Python 3.0. Feel free to expand the list!

  • Use conditional compilation to make your code able to compile under both Python 2.x and 3.0 C-API.
  • Now Python 3 uses Unicode as internal string representation, all the class name, method name, etc are Unicode strings. So check all the occurrences of PyString_*, most of them should be replaced with PyUnicode_*. When you suffer a segmentation fault, sometimes it's because you provided a PyString where the C-API expecting a PyUnicode. Also, for the return value of your C extension function, you may use PyUnicode instead of PyString, otherwise you may broken your client's code.

  • PyInt_* removed, but you can explicitly #include <intobject.h> to define aliases that redirect most PyInt_* symbols to PyLong_*.

  • PEP 3123: Making PyObject_HEAD conform to standard C (http://www.python.org/dev/peps/pep-3123/). When initializing type objects, replace

   1 PyObject_HEAD_INIT(NULL)
   2 0, /* ob_size */
  • with

   1 PyVarObject_HEAD_INIT(NULL, 0)
  • And you'd better use pre-defined macros Py_TYPE(o), Py_REFCNT(o), Py_SIZE(o) to access o->ob_type, o->ob_refcnt and o->ob_size separately. For more details please read the PEP.

  • Changes in PyNumberMethods: nb_divide, nb_coerce, nb_oct, nb_hex and nb_inplace_divide removed.

  • Read PEP 3121 Extension Module Initialization and Finalization (http://www.python.org/dev/peps/pep-3121/) and change your code to the new API. It solved many problems for module initialization and finalization.

  • Now PyObject_Compare() raises exception when comparing two objects of different type. (In case of Python 2.x, it compares their pointer.)

  • If your object has slicing interface (eg. obj[2:5]), you should notice that getitem, setitem and delitem now receive PySliceObject instead of a pair of integer value.

  • Unbound method removed. In Python 3 you can use PyInstanceMethod_New() C-API to generate an unbound method for your C function.

  • PyClass_Check() removed. Consider this as a workaround:

   1 #define PyClass_Check(obj) PyObject_IsInstance(obj, (PyObject *)&PyType_Type)
   2 
  • PyInstance_NewRaw() removed, try to use PyBaseObject_Type.tp_new() for replacement.

  • In py3k dict.items() method returns a dict_items object, you can use PySequence_Fast() to convert it to a sequence.

  • struct member flags: The RO shorthand for READONLY is gone.

Py3kExtensionModules (last edited 2019-10-27 00:06:49 by FrancesHocutt)

Unable to edit the page? See the FrontPage for instructions.