Benjamin Peterson | e9bbc8b | 2008-09-28 02:06:32 +0000 | [diff] [blame] | 1 | .. highlightlang:: c |
| 2 | |
Guido van Rossum | 56076da | 2008-12-02 22:58:36 +0000 | [diff] [blame] | 3 | .. _cporting-howto: |
| 4 | |
Benjamin Peterson | e9bbc8b | 2008-09-28 02:06:32 +0000 | [diff] [blame] | 5 | ******************************** |
| 6 | Porting 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 | |
| 21 | Conditional compilation |
| 22 | ======================= |
| 23 | |
| 24 | The 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 | |
| 31 | API functions that are not present can be aliased to their equivalents within |
| 32 | conditional blocks. |
| 33 | |
| 34 | |
| 35 | Changes to Object APIs |
| 36 | ====================== |
| 37 | |
| 38 | Python 3.0 merged together some types with similar functions while cleanly |
| 39 | separating others. |
| 40 | |
| 41 | |
| 42 | str/unicode Unification |
| 43 | ----------------------- |
| 44 | |
| 45 | |
| 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, |
| 49 | :file:`bytesobject.h`, mapping ``PyBytes`` names to ``PyString`` ones. For best |
| 50 | compatibility 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 |
| 54 | best 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 | |
| 94 | long/int Unification |
| 95 | -------------------- |
| 96 | |
| 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 |
| 101 | ``PyLong_*`` found in :file:`intobject.h`. The the abstract ``PyNumber_*`` APIs |
| 102 | can 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 | |
| 120 | Module initialization and state |
| 121 | =============================== |
| 122 | |
| 123 | Python 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 |
| 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. :: |
| 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 | |
| 212 | Other options |
| 213 | ============= |
| 214 | |
| 215 | If 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 |
| 217 | extension modules it creates are compatible with Python 3.x and 2.x. |
| 218 | |