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 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 Link object struct members directly to object attributes.

argument and keyword parsing 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 Reference this table whenever you need to raise an builtin exception

glib reference manual 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

Attachment Action Size Date Who Comment
examplemodule.c manage 6.9 K 26 Jan 2009 - 11:31 TedRoche Example extension module in C, by Arc Riley (GPL)
LICENSE manage 34.2 K 26 Jan 2009 - 11:31 TedRoche GPL License manage 1.2 K 26 Jan 2009 - 11:33 TedRoche Python source code for PyPI? setup and to run the example extension
getting_started.odt manage 24.2 K 26 Jan 2009 - 11:34 TedRoche Notes and links from the presentation (also included above) in OpenOffice?.org format

Revision: r1.1 - 26 Jan 2009 - 11:34 - TedRoche
Www > PySIGArcRiley22Jan2009
All content is Copyright © 1999-2023 by, and the property of, the contributing authors.
Questions, comments, or concerns? Contact GNHLUG
Legal Notice (includes Terms of Service)