Beginning Python - From Novice to Professional
Beginning Python - From Novice to Professional Beginning Python - From Novice to Professional
CHAPTER 17 ■ EXTENDING PYTHON 367 • If you create a new object and return it, you should transfer ownership to the caller. In other words, if you don’t already have a reference to the object, you should call Py_INCREF; if you do have a reference, you should simply avoid calling Py_DECREF. You then just leave it up to the receiver to call Py_DECREF at some later stage. (This also applies if you return a global constant, such as None.) If you simply extract a part of another object, such as a list, that object (for example, the list) may still have a reference to the object or element, so you may return a borrowed reference (in other words, not call Py_INCREF, or, if you already have a reference, call Py_DECREF, and require that the receiver won’t call Py_DECREF). Which you choose should be clearly documented. Note that this only applies if your function is called from other C functions. If it’s called from Python, it should always transfer ownership (that is, always give the receiver its own reference, either by avoiding calling Py_DECREF on a reference you already have, or, if you don’t have one, by calling Py_INCREF). Hopefully, this will all seem clearer when we get down to a concrete example in a little while. MORE GARBAGE COLLECTION Reference counting is a form of garbage collection, where the term “garbage” refers to objects that are no longer of use to the program. Python also uses a more sophisticated algorithm to detect cyclic garbage, that is, objects that only refer to each other (and thus have nonzero reference counts), but have no other objects referring to them. You can access the Python garbage collector in your Python programs, through the gc module. You can find more information about it in the Python Library Reference (http://python.org/doc/lib/ module-gc.html). A Framework for Extensions There’s quite a lot of cookie-cutter code needed to write a Python C extension, which is why tools such as SWIG, Pyrex, and modulator are so nice: Automating cookie-cutter code is the way to go. Doing it by hand can be a great learning experience, though. You do have quite some leeway in how you structure your code, really; I’ll just show you a way that works. The first thing to remember is that the Python.h header file must be included first, before other standard header files. That is because it may, on some platforms, perform some redefinitions that should be used by the other headers. So, for simplicity, just place #include as the first line of your code. Your function can be called anything you want; it should be static, return a pointer (an owned reference) to an object of the PyObject type, and take two arguments, both also pointers to PyObject. The objects are conventionally called self and args (with self being the self-object, or NULL, and args being a tuple of arguments). In other words, the function should look something like this:
368 CHAPTER 17 ■ EXTENDING PYTHON static PyObject *somename(PyObject *self, PyObject *args) { PyObject *result; /* Do something here, including allocating result. */ } Py_INCREF(result); /* Only if needed! */ return result; The self argument is only actually used in bound methods; in other functions, it will simply be a NULL pointer. Note that the call to Py_INCREF may not be needed. If the object is created in the function (for example, using a utility function such as Py_BuildValue), the function will already own a reference to it, and can simply return it. If, however, you wish to return None from your function, you should use the existing object Py_None. In this case, however, the function does not own a reference to Py_None, and so should call Py_INCREF(Py_None) before returning it. The args parameter contains all the arguments to your function (except, if present, the self argument). In order to extract the objects, you use the function PyArg_ParseTuple (for positional arguments) and PyArg_ParseTupleAndKeywords (for positional and keyword arguments). I’ll stick to positional arguments here. The function PyArg_ParseTuple has the signature int PyArg_ParseTuple(PyObject *args, char *format, ...); The format string describes what arguments you’re expecting, and then you supply the addresses of the variables you want populated at the end. The return value is a Boolean value: If it’s true, everything went well; otherwise, there was an error. If there was an error, the proper preparations for raising an exception will have been made (you can learn more about that in the documentation) and all you have to do is to return NULL to set it off. So, if we’re not expecting any arguments (an empty format string), the following is a useful way of handling arguments: if (!PyArg_ParseTuple(args, "")) { return NULL; } If the code proceeds beyond this statement, we know we have our arguments (in this case no arguments). Format strings can look like "s" for a string, "i" for an integer, "o" for a Python object (there are many more codes), with possible combinations such as "iis" for two integers and a string. A full reference of how to write format strings can be found in the Python/C API Reference (http://python.org/doc/api/arg-parsing.html). WHAT ABOUT CLASSES? AND OBJECTS? AND TYPES? You can create your own built-in types and classes in extension modules too. It’s not too hard, really, but still a rather involved subject, so I’ll skip it for now. If you mainly need to factor out some bottleneck code into C, using functions will probably be enough for most of your needs anyway. If you want to learn how to create types and classes, the Python documentation is a good source of information.
- Page 347 and 348: 316 CHAPTER 15 ■ PYTHON AND THE W
- Page 349 and 350: 318 CHAPTER 15 ■ PYTHON AND THE W
- Page 351 and 352: 320 CHAPTER 15 ■ PYTHON AND THE W
- Page 353 and 354: 322 CHAPTER 15 ■ PYTHON AND THE W
- Page 355 and 356: 324 CHAPTER 15 ■ PYTHON AND THE W
- Page 357 and 358: 326 CHAPTER 15 ■ PYTHON AND THE W
- Page 359 and 360: 328 CHAPTER 15 ■ PYTHON AND THE W
- Page 361 and 362: 330 CHAPTER 15 ■ PYTHON AND THE W
- Page 363 and 364: 332 CHAPTER 15 ■ PYTHON AND THE W
- Page 365 and 366: 334 CHAPTER 15 ■ PYTHON AND THE W
- Page 367 and 368: 336 CHAPTER 15 ■ PYTHON AND THE W
- Page 369 and 370: 338 CHAPTER 15 ■ PYTHON AND THE W
- Page 372 and 373: CHAPTER 16 ■ ■ ■ Testing, 1-2
- Page 374 and 375: CHAPTER 16 ■ TESTING, 1-2-3 343 W
- Page 376 and 377: CHAPTER 16 ■ TESTING, 1-2-3 345 d
- Page 378 and 379: CHAPTER 16 ■ TESTING, 1-2-3 347 u
- Page 380 and 381: CHAPTER 16 ■ TESTING, 1-2-3 349 F
- Page 382 and 383: CHAPTER 16 ■ TESTING, 1-2-3 351 P
- Page 384 and 385: CHAPTER 16 ■ TESTING, 1-2-3 353 "
- Page 386: CHAPTER 16 ■ TESTING, 1-2-3 355 N
- Page 389 and 390: 358 CHAPTER 17 ■ EXTENDING PYTHON
- Page 391 and 392: 360 CHAPTER 17 ■ EXTENDING PYTHON
- Page 393 and 394: 362 CHAPTER 17 ■ EXTENDING PYTHON
- Page 395 and 396: 364 CHAPTER 17 ■ EXTENDING PYTHON
- Page 397: 366 CHAPTER 17 ■ EXTENDING PYTHON
- Page 401 and 402: 370 CHAPTER 17 ■ EXTENDING PYTHON
- Page 404 and 405: CHAPTER 18 ■ ■ ■ Packaging Yo
- Page 406 and 407: CHAPTER 18 ■ PACKAGING YOUR PROGR
- Page 408 and 409: CHAPTER 18 ■ PACKAGING YOUR PROGR
- Page 410 and 411: CHAPTER 18 ■ PACKAGING YOUR PROGR
- Page 412 and 413: CHAPTER 19 ■ ■ ■ Playful Prog
- Page 414 and 415: CHAPTER 19 ■ PLAYFUL PROGRAMMING
- Page 416 and 417: CHAPTER 19 ■ PLAYFUL PROGRAMMING
- Page 418 and 419: CHAPTER 19 ■ PLAYFUL PROGRAMMING
- Page 420: CHAPTER 19 ■ PLAYFUL PROGRAMMING
- Page 423 and 424: 392 CHAPTER 20 ■ PROJECT 1: INSTA
- Page 425 and 426: 394 CHAPTER 20 ■ PROJECT 1: INSTA
- Page 427 and 428: 396 CHAPTER 20 ■ PROJECT 1: INSTA
- Page 429 and 430: 398 CHAPTER 20 ■ PROJECT 1: INSTA
- Page 431 and 432: 400 CHAPTER 20 ■ PROJECT 1: INSTA
- Page 433 and 434: 402 CHAPTER 20 ■ PROJECT 1: INSTA
- Page 435 and 436: 404 CHAPTER 20 ■ PROJECT 1: INSTA
- Page 437 and 438: 406 CHAPTER 20 ■ PROJECT 1: INSTA
- Page 439 and 440: 408 CHAPTER 20 ■ PROJECT 1: INSTA
- Page 442 and 443: CHAPTER 21 ■ ■ ■ Project 2: P
- Page 444 and 445: CHAPTER 21 ■ PROJECT 2: PAINTING
- Page 446 and 447: CHAPTER 21 ■ PROJECT 2: PAINTING
CHAPTER 17 ■ EXTENDING PYTHON 367<br />
• If you create a new object and return it, you should transfer ownership <strong>to</strong> the caller. In<br />
other words, if you don’t already have a reference <strong>to</strong> the object, you should call Py_INCREF; if<br />
you do have a reference, you should simply avoid calling Py_DECREF. You then just leave<br />
it up <strong>to</strong> the receiver <strong>to</strong> call Py_DECREF at some later stage. (This also applies if you return<br />
a global constant, such as None.) If you simply extract a part of another object, such as a<br />
list, that object (for example, the list) may still have a reference <strong>to</strong> the object or element,<br />
so you may return a borrowed reference (in other words, not call Py_INCREF, or, if you<br />
already have a reference, call Py_DECREF, and require that the receiver won’t call Py_DECREF).<br />
Which you choose should be clearly documented. Note that this only applies if your<br />
function is called from other C functions. If it’s called from <strong>Python</strong>, it should always<br />
transfer ownership (that is, always give the receiver its own reference, either by avoiding<br />
calling Py_DECREF on a reference you already have, or, if you don’t have one, by calling<br />
Py_INCREF).<br />
Hopefully, this will all seem clearer when we get down <strong>to</strong> a concrete example in a little while.<br />
MORE GARBAGE COLLECTION<br />
Reference counting is a form of garbage collection, where the term “garbage” refers <strong>to</strong> objects that are no<br />
longer of use <strong>to</strong> the program. <strong>Python</strong> also uses a more sophisticated algorithm <strong>to</strong> detect cyclic garbage, that<br />
is, objects that only refer <strong>to</strong> each other (and thus have nonzero reference counts), but have no other objects<br />
referring <strong>to</strong> them.<br />
You can access the <strong>Python</strong> garbage collec<strong>to</strong>r in your <strong>Python</strong> programs, through the gc module. You<br />
can find more information about it in the <strong>Python</strong> Library Reference (http://python.org/doc/lib/<br />
module-gc.html).<br />
A Framework for Extensions<br />
There’s quite a lot of cookie-cutter code needed <strong>to</strong> write a <strong>Python</strong> C extension, which is why<br />
<strong>to</strong>ols such as SWIG, Pyrex, and modula<strong>to</strong>r are so nice: Au<strong>to</strong>mating cookie-cutter code is the<br />
way <strong>to</strong> go. Doing it by hand can be a great learning experience, though. You do have quite some<br />
leeway in how you structure your code, really; I’ll just show you a way that works.<br />
The first thing <strong>to</strong> remember is that the <strong>Python</strong>.h header file must be included first, before<br />
other standard header files. That is because it may, on some platforms, perform some redefinitions<br />
that should be used by the other headers. So, for simplicity, just place<br />
#include <br />
as the first line of your code.<br />
Your function can be called anything you want; it should be static, return a pointer (an<br />
owned reference) <strong>to</strong> an object of the PyObject type, and take two arguments, both also pointers<br />
<strong>to</strong> PyObject. The objects are conventionally called self and args (with self being the self-object,<br />
or NULL, and args being a tuple of arguments). In other words, the function should look something<br />
like this: