The 22-January-2009 Python Special Interest Group (PySIG) hosted a meeting where ArcRiley presented on developing extensions in C for Python 3.0. Notes follow, and source are included in the attachments at the bottom of the page.


Building a Python 3.0 Extension the missing PyModule_* functions

These functions (PyModule_*) are not included in the Python 3.0 documentation but their use can be derived from Python's source code. See http://bugs.python.org/issue4614 which indicates this has been fixed and will be included in future releases.

PyObject* PyModule_Create(struct PyModuleDef*);

Allocates and returns a Python module object for the supplied PyModuleDef struct: static struct PyModuleDef example_Module = { PyModuleDef_HEAD_INIT, "example", /*m_name*/ "Example's PyDoc", /*m_doc*/ -1, /*m_size*/ example_Methods, /*m_methods*/ NULL, /*m_reload*/ NULL, /*m_traverse*/ NULL, /*m_clear*/ NULL /*m_free*/ };

NULL will be returned when an exception was raised (ie, out of memory).

int PyModule_AddObject(PyObject*, const char*, PyObject*);

First argument is the module object (from PyModule_Create), Second argument is the name of the new object in the module's namespace, Third argument is a pointer to a PyTypeObject (cast as a PyObject*) of the new object. Returns 0 on success, -1 on failure (result of initfunc).

int PyModule_AddIntConstant(PyObject*, const char*, long);

As PyModule_AddObject but the 3rd argument is the value of a new int object.

int PyModule_AddStringConstant(PyObject*, const char*, const char*);

As PyModule_AddObject but the 3rd argument is the value of a new str object. Building a Python 3.0 Extension bookmarks you will need

object members http://docs.python.org/3.0/c-api/structures.html#PyMemberDef Link object struct members directly to object attributes.

argument and keyword parsing http://docs.python.org/3.0/c-api/arg.html These are extremely convenient functions which parse arguments and/or keywords, test their type and value limits, and convert them into C types (char*, short, long, etc).

standard exceptions http://docs.python.org/3.0/c-api/exceptions.html#standard-exceptions Reference this table whenever you need to raise an builtin exception

glib reference manual http://library.gnome.org/devel/glib/unstable/ Not Python-specific, Glib is an invaluable cross-platform “swiss army knife” library you'll find yourself using in every C-based project. Especially useful for threaded extensions!

handy macros you'll use everywhere

Returning None This replaces “Py_INCREF(Py_None); return Py_None;” everywhere you need to return None. Py_RETURN_NONE

Returning True/False Same as above but for Py_True and Py_False Py_RETURN_TRUE Py_RETURN_FALSE

Returning Special Floats Same as above but for Py_NAN and Py_HUGE_VAL Py_RETURN_NAN Py_RETURN_INF(sign)

Release and Reacquire the GIL (Global Interpreter Lock) Working with the GIL can be a pain, but this macro pair makes it a little simpler: Py_BEGIN_ALLOW_THREADS (nogil code here) Py_END_ALLOW_THREADS

-- TedRoche - 26 Jan 2009

Topic attachments
I Attachment History Action Size Date Who Comment
Unknown file formatext LICENSE r1 manage 34.2 K 2009-01-26 - 11:31 TedRoche GPL License
C source code filec examplemodule.c r1 manage 6.9 K 2009-01-26 - 11:31 TedRoche Example extension module in C, by Arc Riley (GPL)
Unknown file formatodt getting_started.odt r1 manage 24.2 K 2009-01-26 - 11:34 TedRoche Notes and links from the presentation (also included above) in OpenOffice.org format
Texttxt setup.py.txt r1 manage 1.2 K 2009-01-26 - 11:33 TedRoche Python source code for PyPI setup and to run the example extension
Topic revision: r1 - 2009-01-26 - TedRoche
 

All content is Copyright © 1999-2024 by, and the property of, the contributing authors.
Questions, comments, or concerns? Contact GNHLUG.
All use of this site subject to our Legal Notice (includes Terms of Service).