Beginning Python - From Novice to Professional

Beginning Python - From Novice to Professional Beginning Python - From Novice to Professional

16.01.2014 Views

CHAPTER 17 ■ EXTENDING PYTHON 371 SWIG. SWIG is a tool for automatically generating wrapper code for your C libraries. The wrapper code takes care of the Python C API so you don’t have to. It is one of the easiest and most popular ways of extending Python. Using the Python/C API. You can write C code yourself that can be imported directly into Python as shared libraries. To do this, you have to adhere to the Python/C API. Things you have to take care of for each function include reference counting, extracting arguments, and building return values. There is also a certain amount of code needed to make a C library work as a module, including listing the functions in the module and creating a module initialization function. New Functions in This Chapter Function Py_INCREF(obj) Py_DECREF(obj) PyArg_ParseTuple(args, fmt, ...) PyArg_ParseTupleAndKeywords(args, kws, fmt, kwlist) PyBuildValue(fmt, value) Description Increment reference count of obj Decrement reference count of obj Extract positional arguments Extract positional and keyword arguments Build a PyObject from a C value What Now? Now you ought to either have some really cool programs or at least some really cool program ideas. Once you’ve got something you want to share with the world (and you do want to share your code with the world, don’t you?), the next chapter can be your next step.

CHAPTER 17 ■ EXTENDING PYTHON 371<br />

SWIG. SWIG is a <strong>to</strong>ol for au<strong>to</strong>matically generating wrapper code for your C libraries. The<br />

wrapper code takes care of the <strong>Python</strong> C API so you don’t have <strong>to</strong>. It is one of the easiest<br />

and most popular ways of extending <strong>Python</strong>.<br />

Using the <strong>Python</strong>/C API. You can write C code yourself that can be imported directly in<strong>to</strong><br />

<strong>Python</strong> as shared libraries. To do this, you have <strong>to</strong> adhere <strong>to</strong> the <strong>Python</strong>/C API. Things you<br />

have <strong>to</strong> take care of for each function include reference counting, extracting arguments,<br />

and building return values. There is also a certain amount of code needed <strong>to</strong> make a C<br />

library work as a module, including listing the functions in the module and creating a<br />

module initialization function.<br />

New Functions in This Chapter<br />

Function<br />

Py_INCREF(obj)<br />

Py_DECREF(obj)<br />

PyArg_ParseTuple(args, fmt, ...)<br />

PyArg_ParseTupleAndKeywords(args, kws, fmt, kwlist)<br />

PyBuildValue(fmt, value)<br />

Description<br />

Increment reference count of obj<br />

Decrement reference count of obj<br />

Extract positional arguments<br />

Extract positional and keyword<br />

arguments<br />

Build a PyObject from a C value<br />

What Now?<br />

Now you ought <strong>to</strong> either have some really cool programs or at least some really cool program<br />

ideas. Once you’ve got something you want <strong>to</strong> share with the world (and you do want <strong>to</strong> share<br />

your code with the world, don’t you?), the next chapter can be your next step.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!