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