blob: 46482abd62830268a543d48a378a96910335d9c1 [file] [log] [blame]
Benjamin Petersonf1c08f02008-09-26 02:58:36 +00001.. highlightlang:: c
2
Éric Araujo52a5a032011-08-19 01:22:42 +02003.. _cporting-howto:
4
Benjamin Petersonf1c08f02008-09-26 02:58:36 +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
Georg Brandlda84d212008-09-26 21:15:21 +000032conditional blocks.
Benjamin Petersonf1c08f02008-09-26 02:58:36 +000033
34
35Changes to Object APIs
36======================
37
Georg Brandlda84d212008-09-26 21:15:21 +000038Python 3.0 merged together some types with similar functions while cleanly
Benjamin Petersonf1c08f02008-09-26 02:58:36 +000039separating 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
Benjamin Peterson4008ef02008-09-27 23:28:43 +000050compatibility with 3.0, :ctype:`PyUnicode` should be used for textual data and
Benjamin Petersonf1c08f02008-09-26 02:58:36 +000051: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
Georg Brandl26946ec2010-11-26 07:42:15 +000053:ctype:`PyString` and :ctype:`PyUnicode` are in 2.x. The following example
54shows best practices with regards to :ctype:`PyUnicode`, :ctype:`PyString`,
55and :ctype:`PyBytes`. ::
Benjamin Petersonf1c08f02008-09-26 02:58:36 +000056
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
Benjamin Peterson4008ef02008-09-27 23:28:43 +000073 /* just a forward */
Benjamin Petersonf1c08f02008-09-26 02:58:36 +000074 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
Benjamin Peterson4008ef02008-09-27 23:28:43 +0000100best course of action here is using the ``PyInt_*`` functions aliased to
Andrew M. Kuchlinga178a692009-04-03 21:45:29 +0000101``PyLong_*`` found in :file:`intobject.h`. The abstract ``PyNumber_*`` APIs
Benjamin Peterson4008ef02008-09-27 23:28:43 +0000102can also be used in some cases. ::
Benjamin Petersonf1c08f02008-09-26 02:58:36 +0000103
104 #include "Python.h"
Benjamin Peterson4008ef02008-09-27 23:28:43 +0000105 #include "intobject.h"
Benjamin Petersonf1c08f02008-09-26 02:58:36 +0000106
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
Victor Stinner8ded4772010-05-14 14:20:07 +0000123Python 3.0 has a revamped extension module initialization system. (See
Benjamin Petersonf1c08f02008-09-26 02:58:36 +0000124:pep:`3121`.) Instead of storing module state in globals, they should be stored
Georg Brandlda84d212008-09-26 21:15:21 +0000125in an interpreter specific structure. Creating modules that act correctly in
Benjamin Petersonf1c08f02008-09-26 02:58:36 +0000126both 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 }
Benjamin Peterson0eee7c62008-09-26 20:52:06 +0000210
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