GNHLUG > PySIGArcRiley22Jan2009
GNHLUG webs: Main | TWiki | Sandbox   Log In or Register

Changes | Index | Search | Go
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

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 format
Texttxt r1 manage 1.2 K 2009-01-26 - 11:33 TedRoche Python source code for PyPI setup and to run the example extension
Edit | Attach | Watch | Print version | History: r1 | Backlinks | Raw View | Raw edit | More topic actions

All content is Copyright © 1999-2024 by, and the property of, the contributing authors.