22
33.. _cporting-howto :
44
5- ********************************
6- Porting Extension Modules to 3.0
7- ********************************
5+ *************************************
6+ Porting Extension Modules to Python 3
7+ *************************************
88
99:author: Benjamin Peterson
1010
1111
1212.. topic :: Abstract
1313
14- Although changing the C-API was not one of Python 3.0's objectives, the many
15- Python level changes made leaving 2.x's API intact impossible. In fact, some
16- changes such as :func: `int ` and :func: `long ` unification are more obvious on
17- the C level. This document endeavors to document incompatibilities and how
18- they can be worked around.
14+ Although changing the C-API was not one of Python 3's objectives,
15+ the many Python-level changes made leaving Python 2's API intact
16+ impossible. In fact, some changes such as :func: `int ` and
17+ :func: `long ` unification are more obvious on the C level. This
18+ document endeavors to document incompatibilities and how they can
19+ be worked around.
1920
2021
2122Conditional compilation
2223=======================
2324
24- The easiest way to compile only some code for 3.0 is to check if
25- :c:macro: `PY_MAJOR_VERSION ` is greater than or equal to 3. ::
25+ The easiest way to compile only some code for Python 3 is to check
26+ if :c:macro: `PY_MAJOR_VERSION ` is greater than or equal to 3. ::
2627
2728 #if PY_MAJOR_VERSION >= 3
2829 #define IS_PY3K
@@ -35,22 +36,22 @@ conditional blocks.
3536Changes to Object APIs
3637======================
3738
38- Python 3.0 merged together some types with similar functions while cleanly
39+ Python 3 merged together some types with similar functions while cleanly
3940separating others.
4041
4142
4243str/unicode Unification
4344-----------------------
4445
4546
46- Python 3.0 's :func: `str ` (``PyString_* `` functions in C) type is equivalent to
47- 2.x 's :func: `unicode ` (``PyUnicode_* ``). The old 8-bit string type has become
48- :func: `bytes `. Python 2.6 and later provide a compatibility header,
47+ Python 3's :func: `str ` (``PyString_* `` functions in C) type is equivalent to
48+ Python 2 's :func: `unicode ` (``PyUnicode_* ``). The old 8-bit string type has
49+ become :func: `bytes `. Python 2.6 and later provide a compatibility header,
4950:file: `bytesobject.h `, mapping ``PyBytes `` names to ``PyString `` ones. For best
50- compatibility with 3.0 , :c:type: `PyUnicode ` should be used for textual data and
51+ compatibility with Python 3 , :c:type: `PyUnicode ` should be used for textual data and
5152:c:type: `PyBytes ` for binary data. It's also important to remember that
52- :c:type: `PyBytes ` and :c:type: `PyUnicode ` in 3.0 are not interchangeable like
53- :c:type: `PyString ` and :c:type: `PyUnicode ` are in 2.x . The following example
53+ :c:type: `PyBytes ` and :c:type: `PyUnicode ` in Python 3 are not interchangeable like
54+ :c:type: `PyString ` and :c:type: `PyUnicode ` are in Python 2 . The following example
5455shows best practices with regards to :c:type: `PyUnicode `, :c:type: `PyString `,
5556and :c:type: `PyBytes `. ::
5657
@@ -94,10 +95,12 @@ and :c:type:`PyBytes`. ::
9495long/int Unification
9596--------------------
9697
97- In Python 3.0, there is only one integer type. It is called :func: `int ` on the
98- Python level, but actually corresponds to 2.x's :func: `long ` type. In the
99- C-API, ``PyInt_* `` functions are replaced by their ``PyLong_* `` neighbors. The
100- best course of action here is using the ``PyInt_* `` functions aliased to
98+ Python 3 has only one integer type, :func: `int `. But it actually
99+ corresponds to Python 2's :func: `long ` type--the :func: `int ` type
100+ used in Python 2 was removed. In the C-API, ``PyInt_* `` functions
101+ are replaced by their ``PyLong_* `` equivalents.
102+
103+ The best course of action here is using the ``PyInt_* `` functions aliased to
101104``PyLong_* `` found in :file: `intobject.h `. The abstract ``PyNumber_* `` APIs
102105can also be used in some cases. ::
103106
@@ -120,10 +123,11 @@ can also be used in some cases. ::
120123Module initialization and state
121124===============================
122125
123- Python 3.0 has a revamped extension module initialization system. (See
124- :pep: `3121 `.) Instead of storing module state in globals, they should be stored
125- in an interpreter specific structure. Creating modules that act correctly in
126- both 2.x and 3.0 is tricky. The following simple example demonstrates how. ::
126+ Python 3 has a revamped extension module initialization system. (See
127+ :pep: `3121 `.) Instead of storing module state in globals, they should
128+ be stored in an interpreter specific structure. Creating modules that
129+ act correctly in both Python 2 and Python 3 is tricky. The following
130+ simple example demonstrates how. ::
127131
128132 #include "Python.h"
129133
@@ -209,10 +213,65 @@ both 2.x and 3.0 is tricky. The following simple example demonstrates how. ::
209213 }
210214
211215
216+ CObject replaced with Capsule
217+ =============================
218+
219+ The :c:type: `Capsule ` object was introduced in Python 3.1 and 2.7 to replace
220+ :c:type: `CObject `. CObjects were useful,
221+ but the :c:type: `CObject ` API was problematic: it didn't permit distinguishing
222+ between valid CObjects, which allowed mismatched CObjects to crash the
223+ interpreter, and some of its APIs relied on undefined behavior in C.
224+ (For further reading on the rationale behind Capsules, please see :issue: `5630 `.)
225+
226+ If you're currently using CObjects, and you want to migrate to 3.1 or newer,
227+ you'll need to switch to Capsules.
228+ :c:type: `CObject ` was deprecated in 3.1 and 2.7 and completely removed in
229+ Python 3.2. If you only support 2.7, or 3.1 and above, you
230+ can simply switch to :c:type: `Capsule `. If you need to support Python 3.0,
231+ or versions of Python earlier than 2.7,
232+ you'll have to support both CObjects and Capsules.
233+ (Note that Python 3.0 is no longer supported, and it is not recommended
234+ for production use.)
235+
236+ The following example header file :file: `capsulethunk.h ` may
237+ solve the problem for you. Simply write your code against the
238+ :c:type: `Capsule ` API and include this header file after
239+ :file: `Python.h `. Your code will automatically use Capsules
240+ in versions of Python with Capsules, and switch to CObjects
241+ when Capsules are unavailable.
242+
243+ :file: `capsulethunk.h ` simulates Capsules using CObjects. However,
244+ :c:type: `CObject ` provides no place to store the capsule's "name". As a
245+ result the simulated :c:type: `Capsule ` objects created by :file: `capsulethunk.h `
246+ behave slightly differently from real Capsules. Specifically:
247+
248+ * The name parameter passed in to :c:func: `PyCapsule_New ` is ignored.
249+
250+ * The name parameter passed in to :c:func: `PyCapsule_IsValid ` and
251+ :c:func: `PyCapsule_GetPointer ` is ignored, and no error checking
252+ of the name is performed.
253+
254+ * :c:func: `PyCapsule_GetName ` always returns NULL.
255+
256+ * :c:func: `PyCapsule_SetName ` always throws an exception and
257+ returns failure. (Since there's no way to store a name
258+ in a CObject, noisy failure of :c:func: `PyCapsule_SetName `
259+ was deemed preferable to silent failure here. If this is
260+ inconveient, feel free to modify your local
261+ copy as you see fit.)
262+
263+ You can find :file: `capsulethunk.h ` in the Python source distribution
264+ in the :file: `Doc/includes ` directory. We also include it here for
265+ your reference; here is :file: `capsulethunk.h `:
266+
267+ .. literalinclude :: ../includes/capsulethunk.h
268+
269+
270+
212271Other options
213272=============
214273
215274If you are writing a new extension module, you might consider `Cython
216275<http://www.cython.org> `_. It translates a Python-like language to C. The
217- extension modules it creates are compatible with Python 3.x and 2.x .
276+ extension modules it creates are compatible with Python 3 and Python 2 .
218277
0 commit comments