Beginning Python - From Novice to Professional
Beginning Python - From Novice to Professional Beginning Python - From Novice to Professional
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.
- 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 and 398: 366 CHAPTER 17 ■ EXTENDING PYTHON
- Page 399 and 400: 368 CHAPTER 17 ■ EXTENDING PYTHON
- Page 401: 370 CHAPTER 17 ■ EXTENDING PYTHON
- Page 405 and 406: 374 CHAPTER 18 ■ PACKAGING YOUR P
- Page 407 and 408: 376 CHAPTER 18 ■ PACKAGING YOUR P
- Page 409 and 410: 378 CHAPTER 18 ■ PACKAGING YOUR P
- Page 411 and 412: 380 CHAPTER 18 ■ PACKAGING YOUR P
- Page 413 and 414: 382 CHAPTER 19 ■ PLAYFUL PROGRAMM
- Page 415 and 416: 384 CHAPTER 19 ■ PLAYFUL PROGRAMM
- Page 417 and 418: 386 CHAPTER 19 ■ PLAYFUL PROGRAMM
- Page 419 and 420: 388 CHAPTER 19 ■ PLAYFUL PROGRAMM
- Page 422 and 423: CHAPTER 20 ■ ■ ■ Project 1: I
- Page 424 and 425: CHAPTER 20 ■ PROJECT 1: INSTANT M
- Page 426 and 427: CHAPTER 20 ■ PROJECT 1: INSTANT M
- Page 428 and 429: CHAPTER 20 ■ PROJECT 1: INSTANT M
- Page 430 and 431: CHAPTER 20 ■ PROJECT 1: INSTANT M
- Page 432 and 433: CHAPTER 20 ■ PROJECT 1: INSTANT M
- Page 434 and 435: CHAPTER 20 ■ PROJECT 1: INSTANT M
- Page 436 and 437: CHAPTER 20 ■ PROJECT 1: INSTANT M
- Page 438 and 439: CHAPTER 20 ■ PROJECT 1: INSTANT M
- Page 440: CHAPTER 20 ■ PROJECT 1: INSTANT M
- Page 443 and 444: 412 CHAPTER 21 ■ PROJECT 2: PAINT
- Page 445 and 446: 414 CHAPTER 21 ■ PROJECT 2: PAINT
- Page 447 and 448: 416 CHAPTER 21 ■ PROJECT 2: PAINT
- Page 449 and 450: 418 CHAPTER 21 ■ PROJECT 2: PAINT
- Page 451 and 452: 420 CHAPTER 21 ■ PROJECT 2: PAINT
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.