Benjamin Peterson | f1c08f0 | 2008-09-26 02:58:36 +0000 | [diff] [blame] | 1 | .. highlightlang:: c |
| 2 | |
Éric Araujo | 52a5a03 | 2011-08-19 01:22:42 +0200 | [diff] [blame] | 3 | .. _cporting-howto: |
| 4 | |
Benjamin Peterson | f1c08f0 | 2008-09-26 02:58:36 +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 |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame^] | 25 | :c:macro:`PY_MAJOR_VERSION` is greater than or equal to 3. :: |
Benjamin Peterson | f1c08f0 | 2008-09-26 02:58:36 +0000 | [diff] [blame] | 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 |
Georg Brandl | da84d21 | 2008-09-26 21:15:21 +0000 | [diff] [blame] | 32 | conditional blocks. |
Benjamin Peterson | f1c08f0 | 2008-09-26 02:58:36 +0000 | [diff] [blame] | 33 | |
| 34 | |
| 35 | Changes to Object APIs |
| 36 | ====================== |
| 37 | |
Georg Brandl | da84d21 | 2008-09-26 21:15:21 +0000 | [diff] [blame] | 38 | Python 3.0 merged together some types with similar functions while cleanly |
Benjamin Peterson | f1c08f0 | 2008-09-26 02:58:36 +0000 | [diff] [blame] | 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 |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame^] | 50 | compatibility with 3.0, :c:type:`PyUnicode` should be used for textual data and |
| 51 | :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 |
| 54 | shows best practices with regards to :c:type:`PyUnicode`, :c:type:`PyString`, |
| 55 | and :c:type:`PyBytes`. :: |
Benjamin Peterson | f1c08f0 | 2008-09-26 02:58:36 +0000 | [diff] [blame] | 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 | |
Benjamin Peterson | 4008ef0 | 2008-09-27 23:28:43 +0000 | [diff] [blame] | 73 | /* just a forward */ |
Benjamin Peterson | f1c08f0 | 2008-09-26 02:58:36 +0000 | [diff] [blame] | 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 |
Benjamin Peterson | 4008ef0 | 2008-09-27 23:28:43 +0000 | [diff] [blame] | 100 | best course of action here is using the ``PyInt_*`` functions aliased to |
Andrew M. Kuchling | a178a69 | 2009-04-03 21:45:29 +0000 | [diff] [blame] | 101 | ``PyLong_*`` found in :file:`intobject.h`. The abstract ``PyNumber_*`` APIs |
Benjamin Peterson | 4008ef0 | 2008-09-27 23:28:43 +0000 | [diff] [blame] | 102 | can also be used in some cases. :: |
Benjamin Peterson | f1c08f0 | 2008-09-26 02:58:36 +0000 | [diff] [blame] | 103 | |
| 104 | #include "Python.h" |
Benjamin Peterson | 4008ef0 | 2008-09-27 23:28:43 +0000 | [diff] [blame] | 105 | #include "intobject.h" |
Benjamin Peterson | f1c08f0 | 2008-09-26 02:58:36 +0000 | [diff] [blame] | 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 | |
Victor Stinner | 8ded477 | 2010-05-14 14:20:07 +0000 | [diff] [blame] | 123 | Python 3.0 has a revamped extension module initialization system. (See |
Benjamin Peterson | f1c08f0 | 2008-09-26 02:58:36 +0000 | [diff] [blame] | 124 | :pep:`3121`.) Instead of storing module state in globals, they should be stored |
Georg Brandl | da84d21 | 2008-09-26 21:15:21 +0000 | [diff] [blame] | 125 | in an interpreter specific structure. Creating modules that act correctly in |
Benjamin Peterson | f1c08f0 | 2008-09-26 02:58:36 +0000 | [diff] [blame] | 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 | } |
Benjamin Peterson | 0eee7c6 | 2008-09-26 20:52:06 +0000 | [diff] [blame] | 210 | |
| 211 | |
Larry Hastings | fc45bba | 2011-10-09 13:03:44 +0100 | [diff] [blame] | 212 | CObject replaced with Capsule |
| 213 | ============================= |
| 214 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame^] | 215 | The :c:type:`Capsule` object was introduced in Python 3.1 and 2.7 to replace |
| 216 | :c:type:`CObject`. CObjects were useful, |
| 217 | but the :c:type:`CObject` API was problematic: it didn't permit distinguishing |
Larry Hastings | fc45bba | 2011-10-09 13:03:44 +0100 | [diff] [blame] | 218 | between valid CObjects, which allowed mismatched CObjects to crash the |
| 219 | interpreter, and some of its APIs relied on undefined behavior in C. |
| 220 | (For further reading on the rationale behind Capsules, please see :issue:`5630`.) |
| 221 | |
| 222 | If you're currently using CObjects, and you want to migrate to 3.1 or newer, |
| 223 | you'll need to switch to Capsules. |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame^] | 224 | :c:type:`CObject` was deprecated in 3.1 and 2.7 and completely removed in |
Larry Hastings | fc45bba | 2011-10-09 13:03:44 +0100 | [diff] [blame] | 225 | Python 3.2. If you only support 2.7, or 3.1 and above, you |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame^] | 226 | can simply switch to :c:type:`Capsule`. If you need to support 3.0 or |
Larry Hastings | fc45bba | 2011-10-09 13:03:44 +0100 | [diff] [blame] | 227 | versions of Python earlier than 2.7 you'll have to support both CObjects |
| 228 | and Capsules. |
| 229 | |
| 230 | The following example header file :file:`capsulethunk.h` may |
| 231 | solve the problem for you; |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame^] | 232 | simply write your code against the :c:type:`Capsule` API, include |
Larry Hastings | fc45bba | 2011-10-09 13:03:44 +0100 | [diff] [blame] | 233 | this header file after ``"Python.h"``, and you'll automatically use CObjects |
| 234 | in Python 3.0 or versions earlier than 2.7. |
| 235 | |
| 236 | :file:`capsulethunk.h` simulates Capsules using CObjects. However, |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame^] | 237 | :c:type:`CObject` provides no place to store the capsule's "name". As a |
| 238 | result the simulated :c:type:`Capsule` objects created by :file:`capsulethunk.h` |
Larry Hastings | fc45bba | 2011-10-09 13:03:44 +0100 | [diff] [blame] | 239 | behave slightly differently from real Capsules. Specifically: |
| 240 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame^] | 241 | * The name parameter passed in to :c:func:`PyCapsule_New` is ignored. |
Larry Hastings | fc45bba | 2011-10-09 13:03:44 +0100 | [diff] [blame] | 242 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame^] | 243 | * The name parameter passed in to :c:func:`PyCapsule_IsValid` and |
| 244 | :c:func:`PyCapsule_GetPointer` is ignored, and no error checking |
Larry Hastings | fc45bba | 2011-10-09 13:03:44 +0100 | [diff] [blame] | 245 | of the name is performed. |
| 246 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame^] | 247 | * :c:func:`PyCapsule_GetName` always returns NULL. |
Larry Hastings | fc45bba | 2011-10-09 13:03:44 +0100 | [diff] [blame] | 248 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame^] | 249 | * :c:func:`PyCapsule_SetName` always throws an exception and |
Larry Hastings | fc45bba | 2011-10-09 13:03:44 +0100 | [diff] [blame] | 250 | returns failure. (Since there's no way to store a name |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame^] | 251 | in a CObject, noisy failure of :c:func:`PyCapsule_SetName` |
Larry Hastings | fc45bba | 2011-10-09 13:03:44 +0100 | [diff] [blame] | 252 | was deemed preferable to silent failure here. If this is |
| 253 | inconveient, feel free to modify your local |
| 254 | copy as you see fit.) |
| 255 | |
| 256 | You can find :file:`capsulethunk.h` in the Python source distribution |
| 257 | in the :file:`Doc/includes` directory. We also include it here for |
| 258 | your reference; here is :file:`capsulethunk.h`: |
| 259 | |
| 260 | .. literalinclude:: ../includes/capsulethunk.h |
| 261 | |
| 262 | |
| 263 | |
Benjamin Peterson | 0eee7c6 | 2008-09-26 20:52:06 +0000 | [diff] [blame] | 264 | Other options |
| 265 | ============= |
| 266 | |
| 267 | If you are writing a new extension module, you might consider `Cython |
| 268 | <http://www.cython.org>`_. It translates a Python-like language to C. The |
| 269 | extension modules it creates are compatible with Python 3.x and 2.x. |
| 270 | |