blob: 7c7ea04a4708820fe0dd82400f3b2b03014f59d7 [file] [log] [blame]
Benjamin Petersone9bbc8b2008-09-28 02:06:32 +00001.. highlightlang:: c
2
Guido van Rossum56076da2008-12-02 22:58:36 +00003.. _cporting-howto:
4
Benjamin Petersone9bbc8b2008-09-28 02:06:32 +00005********************************
6Porting Extension Modules to 3.0
7********************************
8
9:author: Benjamin Peterson
10
11
12.. topic:: Abstract
13
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.
19
20
21Conditional compilation
22=======================
23
24The easiest way to compile only some code for 3.0 is to check if
25:cmacro:`PY_MAJOR_VERSION` is greater than or equal to 3. ::
26
27 #if PY_MAJOR_VERSION >= 3
28 #define IS_PY3K
29 #endif
30
31API functions that are not present can be aliased to their equivalents within
32conditional blocks.
33
34
35Changes to Object APIs
36======================
37
38Python 3.0 merged together some types with similar functions while cleanly
39separating others.
40
41
42str/unicode Unification
43-----------------------
44
45
46Python 3.0's :func:`str` (``PyString_*`` functions in C) type is equivalent to
472.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,
49:file:`bytesobject.h`, mapping ``PyBytes`` names to ``PyString`` ones. For best
50compatibility with 3.0, :ctype:`PyUnicode` should be used for textual data and
51:ctype:`PyBytes` for binary data. It's also important to remember that
52:ctype:`PyBytes` and :ctype:`PyUnicode` in 3.0 are not interchangeable like
53:ctype:`PyString` and :ctype:`PyString` are in 2.x. The following example shows
54best practices with regards to :ctype:`PyUnicode`, :ctype:`PyString`, and
55:ctype:`PyBytes`. ::
56
57 #include "stdlib.h"
58 #include "Python.h"
59 #include "bytesobject.h"
60
61 /* text example */
62 static PyObject *
63 say_hello(PyObject *self, PyObject *args) {
64 PyObject *name, *result;
65
66 if (!PyArg_ParseTuple(args, "U:say_hello", &name))
67 return NULL;
68
69 result = PyUnicode_FromFormat("Hello, %S!", name);
70 return result;
71 }
72
73 /* just a forward */
74 static char * do_encode(PyObject *);
75
76 /* bytes example */
77 static PyObject *
78 encode_object(PyObject *self, PyObject *args) {
79 char *encoded;
80 PyObject *result, *myobj;
81
82 if (!PyArg_ParseTuple(args, "O:encode_object", &myobj))
83 return NULL;
84
85 encoded = do_encode(myobj);
86 if (encoded == NULL)
87 return NULL;
88 result = PyBytes_FromString(encoded);
89 free(encoded);
90 return result;
91 }
92
93
94long/int Unification
95--------------------
96
97In Python 3.0, there is only one integer type. It is called :func:`int` on the
98Python level, but actually corresponds to 2.x's :func:`long` type. In the
99C-API, ``PyInt_*`` functions are replaced by their ``PyLong_*`` neighbors. The
100best course of action here is using the ``PyInt_*`` functions aliased to
Benjamin Petersond23f8222009-04-05 19:13:16 +0000101``PyLong_*`` found in :file:`intobject.h`. The abstract ``PyNumber_*`` APIs
Benjamin Petersone9bbc8b2008-09-28 02:06:32 +0000102can also be used in some cases. ::
103
104 #include "Python.h"
105 #include "intobject.h"
106
107 static PyObject *
108 add_ints(PyObject *self, PyObject *args) {
109 int one, two;
110 PyObject *result;
111
112 if (!PyArg_ParseTuple(args, "ii:add_ints", &one, &two))
113 return NULL;
114
115 return PyInt_FromLong(one + two);
116 }
117
118
119
120Module initialization and state
121===============================
122
123Python 3.0 has a revamped extension module initialization system. (See PEP
124:pep:`3121`.) Instead of storing module state in globals, they should be stored
125in an interpreter specific structure. Creating modules that act correctly in
126both 2.x and 3.0 is tricky. The following simple example demonstrates how. ::
127
128 #include "Python.h"
129
130 struct module_state {
131 PyObject *error;
132 };
133
134 #if PY_MAJOR_VERSION >= 3
135 #define GETSTATE(m) ((struct module_state*)PyModule_GetState(m))
136 #else
137 #define GETSTATE(m) (&_state)
138 static struct module_state _state;
139 #endif
140
141 static PyObject *
142 error_out(PyObject *m) {
143 struct module_state *st = GETSTATE(m);
144 PyErr_SetString(st->error, "something bad happened");
145 return NULL;
146 }
147
148 static PyMethodDef myextension_methods[] = {
149 {"error_out", (PyCFunction)error_out, METH_NOARGS, NULL},
150 {NULL, NULL}
151 };
152
153 #if PY_MAJOR_VERSION >= 3
154
155 static int myextension_traverse(PyObject *m, visitproc visit, void *arg) {
156 Py_VISIT(GETSTATE(m)->error);
157 return 0;
158 }
159
160 static int myextension_clear(PyObject *m) {
161 Py_CLEAR(GETSTATE(m)->error);
162 return 0;
163 }
164
165
166 static struct PyModuleDef moduledef = {
167 PyModuleDef_HEAD_INIT,
168 "myextension",
169 NULL,
170 sizeof(struct module_state),
171 myextension_methods,
172 NULL,
173 myextension_traverse,
174 myextension_clear,
175 NULL
176 };
177
178 #define INITERROR return NULL
179
180 PyObject *
181 PyInit_myextension(void)
182
183 #else
184 #define INITERROR return
185
186 void
187 initmyextension(void)
188 #endif
189 {
190 #if PY_MAJOR_VERSION >= 3
191 PyObject *module = PyModule_Create(&moduledef);
192 #else
193 PyObject *module = Py_InitModule("myextension", myextension_methods);
194 #endif
195
196 if (module == NULL)
197 INITERROR;
198 struct module_state *st = GETSTATE(module);
199
200 st->error = PyErr_NewException("myextension.Error", NULL, NULL);
201 if (st->error == NULL) {
202 Py_DECREF(module);
203 INITERROR;
204 }
205
206 #if PY_MAJOR_VERSION >= 3
207 return module;
208 #endif
209 }
210
211
212Other options
213=============
214
215If you are writing a new extension module, you might consider `Cython
216<http://www.cython.org>`_. It translates a Python-like language to C. The
217extension modules it creates are compatible with Python 3.x and 2.x.
218