Changeset 388 for python/vendor/current/Doc/extending
- Timestamp:
- Mar 19, 2014, 11:11:30 AM (11 years ago)
- Location:
- python/vendor/current/Doc/extending
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
python/vendor/current/Doc/extending/building.rst
r2 r388 59 59 driver script. In the example above, the\ ``ext_modules`` argument to 60 60 :func:`setup` is a list of extension modules, each of which is an instance of 61 the :class:`Extension`. In the example, the instance defines an extension named 62 ``demo`` which is build by compiling a single source file, :file:`demo.c`. 61 the :class:`~distutils.extension.Extension`. In the example, the instance 62 defines an extension named ``demo`` which is build by compiling a single source 63 file, :file:`demo.c`. 63 64 64 65 In many cases, building an extension is more complex, since additional -
python/vendor/current/Doc/extending/embedding.rst
r2 r388 26 26 So if you are embedding Python, you are providing your own main program. One of 27 27 the things this main program has to do is initialize the Python interpreter. At 28 the very least, you have to call the function :c func:`Py_Initialize`. There are28 the very least, you have to call the function :c:func:`Py_Initialize`. There are 29 29 optional calls to pass command line arguments to Python. Then later you can 30 30 call the interpreter from any part of the application. 31 31 32 32 There are several different ways to call the interpreter: you can pass a string 33 containing Python statements to :c func:`PyRun_SimpleString`, or you can pass a33 containing Python statements to :c:func:`PyRun_SimpleString`, or you can pass a 34 34 stdio file pointer and a file name (for identification in error messages only) 35 to :c func:`PyRun_SimpleFile`. You can also call the lower-level operations35 to :c:func:`PyRun_SimpleFile`. You can also call the lower-level operations 36 36 described in the previous chapters to construct and use Python objects. 37 37 … … 62 62 main(int argc, char *argv[]) 63 63 { 64 Py_SetProgramName(argv[0]); /* optional but recommended */ 64 65 Py_Initialize(); 65 66 PyRun_SimpleString("from time import time,ctime\n" … … 69 70 } 70 71 71 The above code first initializes the Python interpreter with 72 :cfunc:`Py_Initialize`, followed by the execution of a hard-coded Python script 73 that print the date and time. Afterwards, the :cfunc:`Py_Finalize` call shuts 72 The :c:func:`Py_SetProgramName` function should be called before 73 :c:func:`Py_Initialize` to inform the interpreter about paths to Python run-time 74 libraries. Next, the Python interpreter is initialized with 75 :c:func:`Py_Initialize`, followed by the execution of a hard-coded Python script 76 that prints the date and time. Afterwards, the :c:func:`Py_Finalize` call shuts 74 77 the interpreter down, followed by the end of the program. In a real program, 75 78 you may want to get the Python script from another source, perhaps a text-editor 76 79 routine, a file, or a database. Getting the Python code from a file can better 77 be done by using the :c func:`PyRun_SimpleFile` function, which saves you the80 be done by using the :c:func:`PyRun_SimpleFile` function, which saves you the 78 81 trouble of allocating memory space and loading the file contents. 79 82 … … 138 141 in ``argv[2]``. Its integer arguments are the other values of the ``argv`` 139 142 array. If you compile and link this program (let's call the finished executable 140 :program:`call`), and use it to execute a Python script, such as:: 143 :program:`call`), and use it to execute a Python script, such as: 144 145 .. code-block:: python 141 146 142 147 def multiply(a,b): … … 163 168 164 169 After initializing the interpreter, the script is loaded using 165 :c func:`PyImport_Import`. This routine needs a Python string as its argument,166 which is constructed using the :c func:`PyString_FromString` data conversion170 :c:func:`PyImport_Import`. This routine needs a Python string as its argument, 171 which is constructed using the :c:func:`PyString_FromString` data conversion 167 172 routine. :: 168 173 … … 176 181 177 182 Once the script is loaded, the name we're looking for is retrieved using 178 :c func:`PyObject_GetAttrString`. If the name exists, and the object returned is183 :c:func:`PyObject_GetAttrString`. If the name exists, and the object returned is 179 184 callable, you can safely assume that it is a function. The program then 180 185 proceeds by constructing a tuple of arguments as normal. The call to the Python … … 219 224 }; 220 225 221 Insert the above code just above the :c func:`main` function. Also, insert the222 following two statements directly after :c func:`Py_Initialize`::226 Insert the above code just above the :c:func:`main` function. Also, insert the 227 following two statements directly after :c:func:`Py_Initialize`:: 223 228 224 229 numargs = argc; … … 227 232 These two lines initialize the ``numargs`` variable, and make the 228 233 :func:`emb.numargs` function accessible to the embedded Python interpreter. 229 With these extensions, the Python script can do things like :: 234 With these extensions, the Python script can do things like 235 236 .. code-block:: python 230 237 231 238 import emb … … 252 259 .. _link-reqs: 253 260 254 Linking Requirements 255 ==================== 256 257 While the :program:`configure` script shipped with the Python sources will 258 correctly build Python to export the symbols needed by dynamically linked 259 extensions, this is not automatically inherited by applications which embed the 260 Python library statically, at least on Unix. This is an issue when the 261 application is linked to the static runtime library (:file:`libpython.a`) and 262 needs to load dynamic extensions (implemented as :file:`.so` files). 263 264 The problem is that some entry points are defined by the Python runtime solely 265 for extension modules to use. If the embedding application does not use any of 266 these entry points, some linkers will not include those entries in the symbol 267 table of the finished executable. Some additional options are needed to inform 268 the linker not to remove these symbols. 269 270 Determining the right options to use for any given platform can be quite 271 difficult, but fortunately the Python configuration already has those values. 272 To retrieve them from an installed Python interpreter, start an interactive 273 interpreter and have a short session like this:: 274 275 >>> import distutils.sysconfig 276 >>> distutils.sysconfig.get_config_var('LINKFORSHARED') 261 Compiling and Linking under Unix-like systems 262 ============================================= 263 264 It is not necessarily trivial to find the right flags to pass to your 265 compiler (and linker) in order to embed the Python interpreter into your 266 application, particularly because Python needs to load library modules 267 implemented as C dynamic extensions (:file:`.so` files) linked against 268 it. 269 270 To find out the required compiler and linker flags, you can execute the 271 :file:`python{X.Y}-config` script which is generated as part of the 272 installation process (a :file:`python-config` script may also be 273 available). This script has several options, of which the following will 274 be directly useful to you: 275 276 * ``pythonX.Y-config --cflags`` will give you the recommended flags when 277 compiling:: 278 279 $ /opt/bin/python2.7-config --cflags 280 -I/opt/include/python2.7 -fno-strict-aliasing -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes 281 282 * ``pythonX.Y-config --ldflags`` will give you the recommended flags when 283 linking:: 284 285 $ /opt/bin/python2.7-config --ldflags 286 -L/opt/lib/python2.7/config -lpthread -ldl -lutil -lm -lpython2.7 -Xlinker -export-dynamic 287 288 .. note:: 289 To avoid confusion between several Python installations (and especially 290 between the system Python and your own compiled Python), it is recommended 291 that you use the absolute path to :file:`python{X.Y}-config`, as in the above 292 example. 293 294 If this procedure doesn't work for you (it is not guaranteed to work for 295 all Unix-like platforms; however, we welcome :ref:`bug reports <reporting-bugs>`) 296 you will have to read your system's documentation about dynamic linking and/or 297 examine Python's :file:`Makefile` (use :func:`sysconfig.get_makefile_filename` 298 to find its location) and compilation 299 options. In this case, the :mod:`sysconfig` module is a useful tool to 300 programmatically extract the configuration values that you will want to 301 combine together. For example: 302 303 .. code-block:: python 304 305 >>> import sysconfig 306 >>> sysconfig.get_config_var('LIBS') 307 '-lpthread -ldl -lutil' 308 >>> sysconfig.get_config_var('LINKFORSHARED') 277 309 '-Xlinker -export-dynamic' 278 310 279 .. index:: module: distutils.sysconfig 280 281 The contents of the string presented will be the options that should be used. 282 If the string is empty, there's no need to add any additional options. The 283 :const:`LINKFORSHARED` definition corresponds to the variable of the same name 284 in Python's top-level :file:`Makefile`. 285 311 312 .. XXX similar documentation for Windows missing -
python/vendor/current/Doc/extending/extending.rst
r2 r388 36 36 Let's create an extension module called ``spam`` (the favorite food of Monty 37 37 Python fans...) and let's say we want to create a Python interface to the C 38 library function :c func:`system`. [#]_ This function takes a null-terminated38 library function :c:func:`system`. [#]_ This function takes a null-terminated 39 39 character string as argument and returns an integer. We want this function to 40 40 be callable from Python as follows:: … … 66 66 includes a few standard header files: ``<stdio.h>``, ``<string.h>``, 67 67 ``<errno.h>``, and ``<stdlib.h>``. If the latter header file does not exist on 68 your system, it declares the functions :c func:`malloc`, :cfunc:`free` and69 :c func:`realloc` directly.68 your system, it declares the functions :c:func:`malloc`, :c:func:`free` and 69 :c:func:`realloc` directly. 70 70 71 71 The next thing we add to our module file is the C function that will be called … … 90 90 and *args*. 91 91 92 The *self* argument is only used when the C function implements a built-in 93 method, not a function. In the example, *self* will always be a *NULL* pointer, 94 since we are defining a function, not a method. (This is done so that the 95 interpreter doesn't have to understand two different types of C functions.) 92 The *self* argument points to the module object for module-level functions; 93 for a method it would point to the object instance. 96 94 97 95 The *args* argument will be a pointer to a Python tuple object containing the … … 99 97 argument list. The arguments are Python objects --- in order to do anything 100 98 with them in our C function we have to convert them to C values. The function 101 :c func:`PyArg_ParseTuple` in the Python API checks the argument types and99 :c:func:`PyArg_ParseTuple` in the Python API checks the argument types and 102 100 converts them to C values. It uses a template string to determine the required 103 101 types of the arguments as well as the types of the C variables into which to 104 102 store the converted values. More about this later. 105 103 106 :c func:`PyArg_ParseTuple` returns true (nonzero) if all arguments have the right104 :c:func:`PyArg_ParseTuple` returns true (nonzero) if all arguments have the right 107 105 type and its components have been stored in the variables whose addresses are 108 106 passed. It returns false (zero) if an invalid argument list was passed. In the … … 130 128 The Python API defines a number of functions to set various types of exceptions. 131 129 132 The most common one is :c func:`PyErr_SetString`. Its arguments are an exception130 The most common one is :c:func:`PyErr_SetString`. Its arguments are an exception 133 131 object and a C string. The exception object is usually a predefined object like 134 :c data:`PyExc_ZeroDivisionError`. The C string indicates the cause of the error132 :c:data:`PyExc_ZeroDivisionError`. The C string indicates the cause of the error 135 133 and is converted to a Python string object and stored as the "associated value" 136 134 of the exception. 137 135 138 Another useful function is :c func:`PyErr_SetFromErrno`, which only takes an136 Another useful function is :c:func:`PyErr_SetFromErrno`, which only takes an 139 137 exception argument and constructs the associated value by inspection of the 140 global variable :c data:`errno`. The most general function is141 :c func:`PyErr_SetObject`, which takes two object arguments, the exception and142 its associated value. You don't need to :c func:`Py_INCREF` the objects passed138 global variable :c:data:`errno`. The most general function is 139 :c:func:`PyErr_SetObject`, which takes two object arguments, the exception and 140 its associated value. You don't need to :c:func:`Py_INCREF` the objects passed 143 141 to any of these functions. 144 142 145 143 You can test non-destructively whether an exception has been set with 146 :c func:`PyErr_Occurred`. This returns the current exception object, or *NULL*144 :c:func:`PyErr_Occurred`. This returns the current exception object, or *NULL* 147 145 if no exception has occurred. You normally don't need to call 148 :c func:`PyErr_Occurred` to see whether an error occurred in a function call,146 :c:func:`PyErr_Occurred` to see whether an error occurred in a function call, 149 147 since you should be able to tell from the return value. 150 148 151 149 When a function *f* that calls another function *g* detects that the latter 152 150 fails, *f* should itself return an error value (usually *NULL* or ``-1``). It 153 should *not* call one of the :c func:`PyErr_\*` functions --- one has already151 should *not* call one of the :c:func:`PyErr_\*` functions --- one has already 154 152 been called by *g*. *f*'s caller is then supposed to also return an error 155 indication to *its* caller, again *without* calling :c func:`PyErr_\*`, and so on153 indication to *its* caller, again *without* calling :c:func:`PyErr_\*`, and so on 156 154 --- the most detailed cause of the error was already reported by the function 157 155 that first detected it. Once the error reaches the Python interpreter's main … … 160 158 161 159 (There are situations where a module can actually give a more detailed error 162 message by calling another :c func:`PyErr_\*` function, and in such cases it is160 message by calling another :c:func:`PyErr_\*` function, and in such cases it is 163 161 fine to do so. As a general rule, however, this is not necessary, and can cause 164 162 information about the cause of the error to be lost: most operations can fail … … 166 164 167 165 To ignore an exception set by a function call that failed, the exception 168 condition must be cleared explicitly by calling :c func:`PyErr_Clear`. The only169 time C code should call :c func:`PyErr_Clear` is if it doesn't want to pass the166 condition must be cleared explicitly by calling :c:func:`PyErr_Clear`. The only 167 time C code should call :c:func:`PyErr_Clear` is if it doesn't want to pass the 170 168 error on to the interpreter but wants to handle it completely by itself 171 169 (possibly by trying something else, or pretending nothing went wrong). 172 170 173 Every failing :c func:`malloc` call must be turned into an exception --- the174 direct caller of :c func:`malloc` (or :cfunc:`realloc`) must call175 :c func:`PyErr_NoMemory` and return a failure indicator itself. All the176 object-creating functions (for example, :c func:`PyInt_FromLong`) already do177 this, so this note is only relevant to those who call :c func:`malloc` directly.178 179 Also note that, with the important exception of :c func:`PyArg_ParseTuple` and171 Every failing :c:func:`malloc` call must be turned into an exception --- the 172 direct caller of :c:func:`malloc` (or :c:func:`realloc`) must call 173 :c:func:`PyErr_NoMemory` and return a failure indicator itself. All the 174 object-creating functions (for example, :c:func:`PyInt_FromLong`) already do 175 this, so this note is only relevant to those who call :c:func:`malloc` directly. 176 177 Also note that, with the important exception of :c:func:`PyArg_ParseTuple` and 180 178 friends, functions that return an integer status usually return a positive value 181 179 or zero for success and ``-1`` for failure, like Unix system calls. 182 180 183 Finally, be careful to clean up garbage (by making :c func:`Py_XDECREF` or184 :c func:`Py_DECREF` calls for objects you have already created) when you return181 Finally, be careful to clean up garbage (by making :c:func:`Py_XDECREF` or 182 :c:func:`Py_DECREF` calls for objects you have already created) when you return 185 183 an error indicator! 186 184 187 185 The choice of which exception to raise is entirely yours. There are predeclared 188 186 C objects corresponding to all built-in Python exceptions, such as 189 :c data:`PyExc_ZeroDivisionError`, which you can use directly. Of course, you190 should choose exceptions wisely --- don't use :c data:`PyExc_TypeError` to mean191 that a file couldn't be opened (that should probably be :c data:`PyExc_IOError`).192 If something's wrong with the argument list, the :c func:`PyArg_ParseTuple`193 function usually raises :c data:`PyExc_TypeError`. If you have an argument whose187 :c:data:`PyExc_ZeroDivisionError`, which you can use directly. Of course, you 188 should choose exceptions wisely --- don't use :c:data:`PyExc_TypeError` to mean 189 that a file couldn't be opened (that should probably be :c:data:`PyExc_IOError`). 190 If something's wrong with the argument list, the :c:func:`PyArg_ParseTuple` 191 function usually raises :c:data:`PyExc_TypeError`. If you have an argument whose 194 192 value must be in a particular range or must satisfy other conditions, 195 :c data:`PyExc_ValueError` is appropriate.193 :c:data:`PyExc_ValueError` is appropriate. 196 194 197 195 You can also define a new exception that is unique to your module. For this, you … … 200 198 static PyObject *SpamError; 201 199 202 and initialize it in your module's initialization function (:c func:`initspam`)200 and initialize it in your module's initialization function (:c:func:`initspam`) 203 201 with an exception object (leaving out the error checking for now):: 204 202 … … 218 216 219 217 Note that the Python name for the exception object is :exc:`spam.error`. The 220 :c func:`PyErr_NewException` function may create a class with the base class218 :c:func:`PyErr_NewException` function may create a class with the base class 221 219 being :exc:`Exception` (unless another class is passed in instead of *NULL*), 222 220 described in :ref:`bltin-exceptions`. 223 221 224 Note also that the :c data:`SpamError` variable retains a reference to the newly222 Note also that the :c:data:`SpamError` variable retains a reference to the newly 225 223 created exception class; this is intentional! Since the exception could be 226 224 removed from the module by external code, an owned reference to the class is 227 needed to ensure that it will not be discarded, causing :c data:`SpamError` to225 needed to ensure that it will not be discarded, causing :c:data:`SpamError` to 228 226 become a dangling pointer. Should it become a dangling pointer, C code which 229 227 raises the exception could cause a core dump or other unintended side effects. 230 228 231 We discuss the use of PyMODINIT_FUNCas a function return type later in this229 We discuss the use of ``PyMODINIT_FUNC`` as a function return type later in this 232 230 sample. 231 232 The :exc:`spam.error` exception can be raised in your extension module using a 233 call to :c:func:`PyErr_SetString` as shown below:: 234 235 static PyObject * 236 spam_system(PyObject *self, PyObject *args) 237 { 238 const char *command; 239 int sts; 240 241 if (!PyArg_ParseTuple(args, "s", &command)) 242 return NULL; 243 sts = system(command); 244 if (sts < 0) { 245 PyErr_SetString(SpamError, "System command failed"); 246 return NULL; 247 } 248 return PyLong_FromLong(sts); 249 } 233 250 234 251 … … 246 263 It returns *NULL* (the error indicator for functions returning object pointers) 247 264 if an error is detected in the argument list, relying on the exception set by 248 :c func:`PyArg_ParseTuple`. Otherwise the string value of the argument has been249 copied to the local variable :c data:`command`. This is a pointer assignment and265 :c:func:`PyArg_ParseTuple`. Otherwise the string value of the argument has been 266 copied to the local variable :c:data:`command`. This is a pointer assignment and 250 267 you are not supposed to modify the string to which it points (so in Standard C, 251 the variable :c data:`command` should properly be declared as ``const char268 the variable :c:data:`command` should properly be declared as ``const char 252 269 *command``). 253 270 254 The next statement is a call to the Unix function :c func:`system`, passing it255 the string we just got from :c func:`PyArg_ParseTuple`::271 The next statement is a call to the Unix function :c:func:`system`, passing it 272 the string we just got from :c:func:`PyArg_ParseTuple`:: 256 273 257 274 sts = system(command); 258 275 259 Our :func:`spam.system` function must return the value of :c data:`sts` as a260 Python object. This is done using the function :c func:`Py_BuildValue`, which is261 something like the inverse of :c func:`PyArg_ParseTuple`: it takes a format276 Our :func:`spam.system` function must return the value of :c:data:`sts` as a 277 Python object. This is done using the function :c:func:`Py_BuildValue`, which is 278 something like the inverse of :c:func:`PyArg_ParseTuple`: it takes a format 262 279 string and an arbitrary number of C values, and returns a new Python object. 263 More info on :c func:`Py_BuildValue` is given later. ::280 More info on :c:func:`Py_BuildValue` is given later. :: 264 281 265 282 return Py_BuildValue("i", sts); … … 269 286 270 287 If you have a C function that returns no useful argument (a function returning 271 :c type:`void`), the corresponding Python function must return ``None``. You272 need this idiom to do so (which is implemented by the :c macro:`Py_RETURN_NONE`288 :c:type:`void`), the corresponding Python function must return ``None``. You 289 need this idiom to do so (which is implemented by the :c:macro:`Py_RETURN_NONE` 273 290 macro):: 274 291 … … 276 293 return Py_None; 277 294 278 :c data:`Py_None` is the C name for the special Python object ``None``. It is a295 :c:data:`Py_None` is the C name for the special Python object ``None``. It is a 279 296 genuine Python object rather than a *NULL* pointer, which means "error" in most 280 297 contexts, as we have seen. … … 286 303 ===================================================== 287 304 288 I promised to show how :c func:`spam_system` is called from Python programs.305 I promised to show how :c:func:`spam_system` is called from Python programs. 289 306 First, we need to list its name and address in a "method table":: 290 307 … … 300 317 the calling convention to be used for the C function. It should normally always 301 318 be ``METH_VARARGS`` or ``METH_VARARGS | METH_KEYWORDS``; a value of ``0`` means 302 that an obsolete variant of :c func:`PyArg_ParseTuple` is used.319 that an obsolete variant of :c:func:`PyArg_ParseTuple` is used. 303 320 304 321 When using only ``METH_VARARGS``, the function should expect the Python-level 305 322 parameters to be passed in as a tuple acceptable for parsing via 306 :c func:`PyArg_ParseTuple`; more information on this function is provided below.323 :c:func:`PyArg_ParseTuple`; more information on this function is provided below. 307 324 308 325 The :const:`METH_KEYWORDS` bit may be set in the third field if keyword 309 326 arguments should be passed to the function. In this case, the C function should 310 327 accept a third ``PyObject *`` parameter which will be a dictionary of keywords. 311 Use :c func:`PyArg_ParseTupleAndKeywords` to parse the arguments to such a328 Use :c:func:`PyArg_ParseTupleAndKeywords` to parse the arguments to such a 312 329 function. 313 330 314 331 The method table must be passed to the interpreter in the module's 315 332 initialization function. The initialization function must be named 316 :c func:`initname`, where *name* is the name of the module, and should be the333 :c:func:`initname`, where *name* is the name of the module, and should be the 317 334 only non-\ ``static`` item defined in the module file:: 318 335 … … 328 345 329 346 When the Python program imports module :mod:`spam` for the first time, 330 :c func:`initspam` is called. (See below for comments about embedding Python.)331 It calls :c func:`Py_InitModule`, which creates a "module object" (which is347 :c:func:`initspam` is called. (See below for comments about embedding Python.) 348 It calls :c:func:`Py_InitModule`, which creates a "module object" (which is 332 349 inserted in the dictionary ``sys.modules`` under the key ``"spam"``), and 333 350 inserts built-in function objects into the newly created module based upon the 334 table (an array of :c type:`PyMethodDef` structures) that was passed as its335 second argument. :c func:`Py_InitModule` returns a pointer to the module object351 table (an array of :c:type:`PyMethodDef` structures) that was passed as its 352 second argument. :c:func:`Py_InitModule` returns a pointer to the module object 336 353 that it creates (which is unused here). It may abort with a fatal error for 337 354 certain errors, or return *NULL* if the module could not be initialized 338 355 satisfactorily. 339 356 340 When embedding Python, the :c func:`initspam` function is not called341 automatically unless there's an entry in the :c data:`_PyImport_Inittab` table.357 When embedding Python, the :c:func:`initspam` function is not called 358 automatically unless there's an entry in the :c:data:`_PyImport_Inittab` table. 342 359 The easiest way to handle this is to statically initialize your 343 statically-linked modules by directly calling :c func:`initspam` after the call344 to :c func:`Py_Initialize`::360 statically-linked modules by directly calling :c:func:`initspam` after the call 361 to :c:func:`Py_Initialize`:: 345 362 346 363 int … … 356 373 initspam(); 357 374 375 ... 376 358 377 An example may be found in the file :file:`Demo/embed/demo.c` in the Python 359 378 source distribution. … … 362 381 363 382 Removing entries from ``sys.modules`` or importing compiled modules into 364 multiple interpreters within a process (or following a :c func:`fork` without an365 intervening :c func:`exec`) can create problems for some extension modules.383 multiple interpreters within a process (or following a :c:func:`fork` without an 384 intervening :c:func:`exec`) can create problems for some extension modules. 366 385 Extension module authors should exercise caution when initializing internal data 367 386 structures. Note also that the :func:`reload` function can be used with 368 387 extension modules, and will call the module initialization function 369 (:c func:`initspam` in the example), but will not load the module again if it was388 (:c:func:`initspam` in the example), but will not load the module again if it was 370 389 loaded from a dynamically loadable object file (:file:`.so` on Unix, 371 390 :file:`.dll` on Windows). … … 373 392 A more substantial example module is included in the Python source distribution 374 393 as :file:`Modules/xxmodule.c`. This file may be used as a template or simply 375 read as an example. The :program:`modulator.py` script included in the source 376 distribution or Windows install provides a simple graphical user interface for 377 declaring the functions and objects which a module should implement, and can 378 generate a template which can be filled in. The script lives in the 379 :file:`Tools/modulator/` directory; see the :file:`README` file there for more 380 information. 394 read as an example. 381 395 382 396 … … 436 450 you the Python function object. You should provide a function (or some other 437 451 interface) to do this. When this function is called, save a pointer to the 438 Python function object (be careful to :c func:`Py_INCREF` it!) in a global452 Python function object (be careful to :c:func:`Py_INCREF` it!) in a global 439 453 variable --- or wherever you see fit. For example, the following function might 440 454 be part of a module definition:: … … 465 479 This function must be registered with the interpreter using the 466 480 :const:`METH_VARARGS` flag; this is described in section :ref:`methodtable`. The 467 :c func:`PyArg_ParseTuple` function and its arguments are documented in section481 :c:func:`PyArg_ParseTuple` function and its arguments are documented in section 468 482 :ref:`parsetuple`. 469 483 470 The macros :c func:`Py_XINCREF` and :cfunc:`Py_XDECREF` increment/decrement the484 The macros :c:func:`Py_XINCREF` and :c:func:`Py_XDECREF` increment/decrement the 471 485 reference count of an object and are safe in the presence of *NULL* pointers 472 486 (but note that *temp* will not be *NULL* in this context). More info on them … … 476 490 477 491 Later, when it is time to call the function, you call the C function 478 :c func:`PyObject_CallObject`. This function has two arguments, both pointers to492 :c:func:`PyObject_CallObject`. This function has two arguments, both pointers to 479 493 arbitrary Python objects: the Python function, and the argument list. The 480 494 argument list must always be a tuple object, whose length is the number of 481 495 arguments. To call the Python function with no arguments, pass in NULL, or 482 496 an empty tuple; to call it with one argument, pass a singleton tuple. 483 :c func:`Py_BuildValue` returns a tuple when its format string consists of zero497 :c:func:`Py_BuildValue` returns a tuple when its format string consists of zero 484 498 or more format codes between parentheses. For example:: 485 499 … … 495 509 Py_DECREF(arglist); 496 510 497 :c func:`PyObject_CallObject` returns a Python object pointer: this is the return498 value of the Python function. :c func:`PyObject_CallObject` is511 :c:func:`PyObject_CallObject` returns a Python object pointer: this is the return 512 value of the Python function. :c:func:`PyObject_CallObject` is 499 513 "reference-count-neutral" with respect to its arguments. In the example a new 500 tuple was created to serve as the argument list, which is :c func:`Py_DECREF`\501 -ed immediately after the call.502 503 The return value of :c func:`PyObject_CallObject` is "new": either it is a brand514 tuple was created to serve as the argument list, which is :c:func:`Py_DECREF`\ 515 -ed immediately after the :c:func:`PyObject_CallObject` call. 516 517 The return value of :c:func:`PyObject_CallObject` is "new": either it is a brand 504 518 new object, or it is an existing object whose reference count has been 505 519 incremented. So, unless you want to save it in a global variable, you should 506 somehow :c func:`Py_DECREF` the result, even (especially!) if you are not520 somehow :c:func:`Py_DECREF` the result, even (especially!) if you are not 507 521 interested in its value. 508 522 509 523 Before you do this, however, it is important to check that the return value 510 524 isn't *NULL*. If it is, the Python function terminated by raising an exception. 511 If the C code that called :c func:`PyObject_CallObject` is called from Python, it525 If the C code that called :c:func:`PyObject_CallObject` is called from Python, it 512 526 should now return an error indication to its Python caller, so the interpreter 513 527 can print a stack trace, or the calling Python code can handle the exception. 514 528 If this is not possible or desirable, the exception should be cleared by calling 515 :c func:`PyErr_Clear`. For example::529 :c:func:`PyErr_Clear`. For example:: 516 530 517 531 if (result == NULL) … … 521 535 522 536 Depending on the desired interface to the Python callback function, you may also 523 have to provide an argument list to :c func:`PyObject_CallObject`. In some cases537 have to provide an argument list to :c:func:`PyObject_CallObject`. In some cases 524 538 the argument list is also provided by the Python program, through the same 525 539 interface that specified the callback function. It can then be saved and used 526 540 in the same manner as the function object. In other cases, you may have to 527 541 construct a new tuple to pass as the argument list. The simplest way to do this 528 is to call :c func:`Py_BuildValue`. For example, if you want to pass an integral542 is to call :c:func:`Py_BuildValue`. For example, if you want to pass an integral 529 543 event code, you might use the following code:: 530 544 … … 541 555 Note the placement of ``Py_DECREF(arglist)`` immediately after the call, before 542 556 the error check! Also note that strictly speaking this code is not complete: 543 :c func:`Py_BuildValue` may run out of memory, and this should be checked.557 :c:func:`Py_BuildValue` may run out of memory, and this should be checked. 544 558 545 559 You may also call a function with keyword arguments by using 546 :c func:`PyObject_Call`, which supports arguments and keyword arguments. As in547 the above example, we use :c func:`Py_BuildValue` to construct the dictionary. ::560 :c:func:`PyObject_Call`, which supports arguments and keyword arguments. As in 561 the above example, we use :c:func:`Py_BuildValue` to construct the dictionary. :: 548 562 549 563 PyObject *dict; … … 565 579 .. index:: single: PyArg_ParseTuple() 566 580 567 The :c func:`PyArg_ParseTuple` function is declared as follows::581 The :c:func:`PyArg_ParseTuple` function is declared as follows:: 568 582 569 583 int PyArg_ParseTuple(PyObject *arg, char *format, ...); … … 575 589 determined by the format string. 576 590 577 Note that while :c func:`PyArg_ParseTuple` checks that the Python arguments have591 Note that while :c:func:`PyArg_ParseTuple` checks that the Python arguments have 578 592 the required types, it cannot check the validity of the addresses of C variables 579 593 passed to the call: if you make mistakes there, your code will probably crash or … … 652 666 .. index:: single: PyArg_ParseTupleAndKeywords() 653 667 654 The :c func:`PyArg_ParseTupleAndKeywords` function is declared as follows::668 The :c:func:`PyArg_ParseTupleAndKeywords` function is declared as follows:: 655 669 656 670 int PyArg_ParseTupleAndKeywords(PyObject *arg, PyObject *kwdict, … … 658 672 659 673 The *arg* and *format* parameters are identical to those of the 660 :c func:`PyArg_ParseTuple` function. The *kwdict* parameter is the dictionary of674 :c:func:`PyArg_ParseTuple` function. The *kwdict* parameter is the dictionary of 661 675 keywords received as the third parameter from the Python runtime. The *kwlist* 662 676 parameter is a *NULL*-terminated list of strings which identify the parameters; 663 677 the names are matched with the type information from *format* from left to 664 right. On success, :c func:`PyArg_ParseTupleAndKeywords` returns true, otherwise678 right. On success, :c:func:`PyArg_ParseTupleAndKeywords` returns true, otherwise 665 679 it returns false and raises an appropriate exception. 666 680 … … 726 740 ========================= 727 741 728 This function is the counterpart to :c func:`PyArg_ParseTuple`. It is declared742 This function is the counterpart to :c:func:`PyArg_ParseTuple`. It is declared 729 743 as follows:: 730 744 … … 732 746 733 747 It recognizes a set of format units similar to the ones recognized by 734 :c func:`PyArg_ParseTuple`, but the arguments (which are input to the function,748 :c:func:`PyArg_ParseTuple`, but the arguments (which are input to the function, 735 749 not output) must not be pointers, just values. It returns a new Python object, 736 750 suitable for returning from a C function called from Python. 737 751 738 One difference with :c func:`PyArg_ParseTuple`: while the latter requires its752 One difference with :c:func:`PyArg_ParseTuple`: while the latter requires its 739 753 first argument to be a tuple (since Python argument lists are always represented 740 as tuples internally), :c func:`Py_BuildValue` does not always build a tuple. It754 as tuples internally), :c:func:`Py_BuildValue` does not always build a tuple. It 741 755 builds a tuple only if its format string contains two or more format units. If 742 756 the format string is empty, it returns ``None``; if it contains exactly one … … 770 784 In languages like C or C++, the programmer is responsible for dynamic allocation 771 785 and deallocation of memory on the heap. In C, this is done using the functions 772 :c func:`malloc` and :cfunc:`free`. In C++, the operators ``new`` and786 :c:func:`malloc` and :c:func:`free`. In C++, the operators ``new`` and 773 787 ``delete`` are used with essentially the same meaning and we'll restrict 774 788 the following discussion to the C case. 775 789 776 Every block of memory allocated with :c func:`malloc` should eventually be777 returned to the pool of available memory by exactly one call to :c func:`free`.778 It is important to call :c func:`free` at the right time. If a block's address779 is forgotten but :c func:`free` is not called for it, the memory it occupies790 Every block of memory allocated with :c:func:`malloc` should eventually be 791 returned to the pool of available memory by exactly one call to :c:func:`free`. 792 It is important to call :c:func:`free` at the right time. If a block's address 793 is forgotten but :c:func:`free` is not called for it, the memory it occupies 780 794 cannot be reused until the program terminates. This is called a :dfn:`memory 781 leak`. On the other hand, if a program calls :c func:`free` for a block and then795 leak`. On the other hand, if a program calls :c:func:`free` for a block and then 782 796 continues to use the block, it creates a conflict with re-use of the block 783 through another :c func:`malloc` call. This is called :dfn:`using freed memory`.797 through another :c:func:`malloc` call. This is called :dfn:`using freed memory`. 784 798 It has the same bad consequences as referencing uninitialized data --- core 785 799 dumps, wrong results, mysterious crashes. … … 798 812 strategy that minimizes this kind of errors. 799 813 800 Since Python makes heavy use of :c func:`malloc` and :cfunc:`free`, it needs a814 Since Python makes heavy use of :c:func:`malloc` and :c:func:`free`, it needs a 801 815 strategy to avoid memory leaks as well as the use of freed memory. The chosen 802 816 method is called :dfn:`reference counting`. The principle is simple: every … … 810 824 strategy, hence my use of "automatic" to distinguish the two.) The big 811 825 advantage of automatic garbage collection is that the user doesn't need to call 812 :c func:`free` explicitly. (Another claimed advantage is an improvement in speed826 :c:func:`free` explicitly. (Another claimed advantage is an improvement in speed 813 827 or memory usage --- this is no hard fact however.) The disadvantage is that for 814 828 C, there is no truly portable automatic garbage collector, while reference 815 counting can be implemented portably (as long as the functions :c func:`malloc`816 and :c func:`free` are available --- which the C Standard guarantees). Maybe some829 counting can be implemented portably (as long as the functions :c:func:`malloc` 830 and :c:func:`free` are available --- which the C Standard guarantees). Maybe some 817 831 day a sufficiently portable automatic garbage collector will be available for C. 818 832 Until then, we'll have to live with reference counts. … … 832 846 as there are no finalizers implemented in Python (:meth:`__del__` methods). 833 847 When there are such finalizers, the detector exposes the cycles through the 834 :mod:`gc` module (specifically, the 835 ``garbage`` variable in that module). The :mod:`gc` module also exposes a way 836 to run the detector (the :func:`collect` function), as well as configuration848 :mod:`gc` module (specifically, the :attr:`~gc.garbage` variable in that module). 849 The :mod:`gc` module also exposes a way to run the detector (the 850 :func:`~gc.collect` function), as well as configuration 837 851 interfaces and the ability to disable the detector at runtime. The cycle 838 852 detector is considered an optional component; though it is included by default, … … 850 864 851 865 There are two macros, ``Py_INCREF(x)`` and ``Py_DECREF(x)``, which handle the 852 incrementing and decrementing of the reference count. :c func:`Py_DECREF` also866 incrementing and decrementing of the reference count. :c:func:`Py_DECREF` also 853 867 frees the object when the count reaches zero. For flexibility, it doesn't call 854 :c func:`free` directly --- rather, it makes a call through a function pointer in868 :c:func:`free` directly --- rather, it makes a call through a function pointer in 855 869 the object's :dfn:`type object`. For this purpose (and others), every object 856 870 also contains a pointer to its type object. … … 860 874 :dfn:`own a reference` to an object. An object's reference count is now defined 861 875 as the number of owned references to it. The owner of a reference is 862 responsible for calling :c func:`Py_DECREF` when the reference is no longer876 responsible for calling :c:func:`Py_DECREF` when the reference is no longer 863 877 needed. Ownership of a reference can be transferred. There are three ways to 864 dispose of an owned reference: pass it on, store it, or call :c func:`Py_DECREF`.878 dispose of an owned reference: pass it on, store it, or call :c:func:`Py_DECREF`. 865 879 Forgetting to dispose of an owned reference creates a memory leak. 866 880 867 881 It is also possible to :dfn:`borrow` [#]_ a reference to an object. The 868 borrower of a reference should not call :c func:`Py_DECREF`. The borrower must882 borrower of a reference should not call :c:func:`Py_DECREF`. The borrower must 869 883 not hold on to the object longer than the owner from which it was borrowed. 870 884 Using a borrowed reference after the owner has disposed of it risks using freed … … 880 894 881 895 A borrowed reference can be changed into an owned reference by calling 882 :c func:`Py_INCREF`. This does not affect the status of the owner from which the896 :c:func:`Py_INCREF`. This does not affect the status of the owner from which the 883 897 reference was borrowed --- it creates a new owned reference, and gives full 884 898 owner responsibilities (the new owner must dispose of the reference properly, as … … 897 911 Most functions that return a reference to an object pass on ownership with the 898 912 reference. In particular, all functions whose function it is to create a new 899 object, such as :c func:`PyInt_FromLong` and :cfunc:`Py_BuildValue`, pass913 object, such as :c:func:`PyInt_FromLong` and :c:func:`Py_BuildValue`, pass 900 914 ownership to the receiver. Even if the object is not actually new, you still 901 915 receive ownership of a new reference to that object. For instance, 902 :c func:`PyInt_FromLong` maintains a cache of popular values and can return a916 :c:func:`PyInt_FromLong` maintains a cache of popular values and can return a 903 917 reference to a cached item. 904 918 905 919 Many functions that extract objects from other objects also transfer ownership 906 with the reference, for instance :c func:`PyObject_GetAttrString`. The picture920 with the reference, for instance :c:func:`PyObject_GetAttrString`. The picture 907 921 is less clear, here, however, since a few common routines are exceptions: 908 :c func:`PyTuple_GetItem`, :cfunc:`PyList_GetItem`, :cfunc:`PyDict_GetItem`, and909 :c func:`PyDict_GetItemString` all return references that you borrow from the922 :c:func:`PyTuple_GetItem`, :c:func:`PyList_GetItem`, :c:func:`PyDict_GetItem`, and 923 :c:func:`PyDict_GetItemString` all return references that you borrow from the 910 924 tuple, list or dictionary. 911 925 912 The function :c func:`PyImport_AddModule` also returns a borrowed reference, even926 The function :c:func:`PyImport_AddModule` also returns a borrowed reference, even 913 927 though it may actually create the object it returns: this is possible because an 914 928 owned reference to the object is stored in ``sys.modules``. … … 916 930 When you pass an object reference into another function, in general, the 917 931 function borrows the reference from you --- if it needs to store it, it will use 918 :c func:`Py_INCREF` to become an independent owner. There are exactly two919 important exceptions to this rule: :c func:`PyTuple_SetItem` and920 :c func:`PyList_SetItem`. These functions take over ownership of the item passed921 to them --- even if they fail! (Note that :c func:`PyDict_SetItem` and friends932 :c:func:`Py_INCREF` to become an independent owner. There are exactly two 933 important exceptions to this rule: :c:func:`PyTuple_SetItem` and 934 :c:func:`PyList_SetItem`. These functions take over ownership of the item passed 935 to them --- even if they fail! (Note that :c:func:`PyDict_SetItem` and friends 922 936 don't take over ownership --- they are "normal.") 923 937 … … 926 940 reference's lifetime is guaranteed until the function returns. Only when such a 927 941 borrowed reference must be stored or passed on, it must be turned into an owned 928 reference by calling :c func:`Py_INCREF`.942 reference by calling :c:func:`Py_INCREF`. 929 943 930 944 The object reference returned from a C function that is called from Python must … … 942 956 interpreter, which can cause the owner of a reference to dispose of it. 943 957 944 The first and most important case to know about is using :c func:`Py_DECREF` on958 The first and most important case to know about is using :c:func:`Py_DECREF` on 945 959 an unrelated object while borrowing a reference to a list item. For instance:: 946 960 … … 958 972 Looks harmless, right? But it's not! 959 973 960 Let's follow the control flow into :c func:`PyList_SetItem`. The list owns974 Let's follow the control flow into :c:func:`PyList_SetItem`. The list owns 961 975 references to all its items, so when item 1 is replaced, it has to dispose of 962 976 the original item 1. Now let's suppose the original item 1 was an instance of a … … 967 981 Since it is written in Python, the :meth:`__del__` method can execute arbitrary 968 982 Python code. Could it perhaps do something to invalidate the reference to 969 ``item`` in :c func:`bug`? You bet! Assuming that the list passed into970 :c func:`bug` is accessible to the :meth:`__del__` method, it could execute a983 ``item`` in :c:func:`bug`? You bet! Assuming that the list passed into 984 :c:func:`bug` is accessible to the :meth:`__del__` method, it could execute a 971 985 statement to the effect of ``del list[0]``, and assuming this was the last 972 986 reference to that object, it would free the memory associated with it, thereby … … 995 1009 other's way, because there is a global lock protecting Python's entire object 996 1010 space. However, it is possible to temporarily release this lock using the macro 997 :c macro:`Py_BEGIN_ALLOW_THREADS`, and to re-acquire it using998 :c macro:`Py_END_ALLOW_THREADS`. This is common around blocking I/O calls, to1011 :c:macro:`Py_BEGIN_ALLOW_THREADS`, and to re-acquire it using 1012 :c:macro:`Py_END_ALLOW_THREADS`. This is common around blocking I/O calls, to 999 1013 let other threads use the processor while waiting for the I/O to complete. 1000 1014 Obviously, the following function has the same problem as the previous one:: … … 1025 1039 1026 1040 It is better to test for *NULL* only at the "source:" when a pointer that may be 1027 *NULL* is received, for example, from :c func:`malloc` or from a function that1041 *NULL* is received, for example, from :c:func:`malloc` or from a function that 1028 1042 may raise an exception. 1029 1043 1030 The macros :c func:`Py_INCREF` and :cfunc:`Py_DECREF` do not check for *NULL*1031 pointers --- however, their variants :c func:`Py_XINCREF` and :cfunc:`Py_XDECREF`1044 The macros :c:func:`Py_INCREF` and :c:func:`Py_DECREF` do not check for *NULL* 1045 pointers --- however, their variants :c:func:`Py_XINCREF` and :c:func:`Py_XDECREF` 1032 1046 do. 1033 1047 … … 1066 1080 1067 1081 1068 .. _using-c objects:1082 .. _using-capsules: 1069 1083 1070 1084 Providing a C API for an Extension Module … … 1102 1116 1103 1117 Python provides a special mechanism to pass C-level information (pointers) from 1104 one extension module to another one: C Objects. A CObjectis a Python data type1105 which stores a pointer (:c type:`void \*`). CObjects can only be created and1118 one extension module to another one: Capsules. A Capsule is a Python data type 1119 which stores a pointer (:c:type:`void \*`). Capsules can only be created and 1106 1120 accessed via their C API, but they can be passed around like any other Python 1107 1121 object. In particular, they can be assigned to a name in an extension module's 1108 1122 namespace. Other extension modules can then import this module, retrieve the 1109 value of this name, and then retrieve the pointer from the C Object.1110 1111 There are many ways in which C Objects can be used to export the C API of an1112 extension module. Each name could get its own CObject, or all C API pointers1113 could be stored in an array whose address is published in a C Object. And the1123 value of this name, and then retrieve the pointer from the Capsule. 1124 1125 There are many ways in which Capsules can be used to export the C API of an 1126 extension module. Each function could get its own Capsule, or all C API pointers 1127 could be stored in an array whose address is published in a Capsule. And the 1114 1128 various tasks of storing and retrieving the pointers can be distributed in 1115 1129 different ways between the module providing the code and the client modules. 1130 1131 Whichever method you choose, it's important to name your Capsules properly. 1132 The function :c:func:`PyCapsule_New` takes a name parameter 1133 (:c:type:`const char \*`); you're permitted to pass in a *NULL* name, but 1134 we strongly encourage you to specify a name. Properly named Capsules provide 1135 a degree of runtime type-safety; there is no feasible way to tell one unnamed 1136 Capsule from another. 1137 1138 In particular, Capsules used to expose C APIs should be given a name following 1139 this convention:: 1140 1141 modulename.attributename 1142 1143 The convenience function :c:func:`PyCapsule_Import` makes it easy to 1144 load a C API provided via a Capsule, but only if the Capsule's name 1145 matches this convention. This behavior gives C API users a high degree 1146 of certainty that the Capsule they load contains the correct C API. 1116 1147 1117 1148 The following example demonstrates an approach that puts most of the burden on 1118 1149 the writer of the exporting module, which is appropriate for commonly used 1119 1150 library modules. It stores all C API pointers (just one in the example!) in an 1120 array of :c type:`void` pointers which becomes the value of a CObject. The header1151 array of :c:type:`void` pointers which becomes the value of a Capsule. The header 1121 1152 file corresponding to the module provides a macro that takes care of importing 1122 1153 the module and retrieving its C API pointers; client modules only have to call … … 1125 1156 The exporting module is a modification of the :mod:`spam` module from section 1126 1157 :ref:`extending-simpleexample`. The function :func:`spam.system` does not call 1127 the C library function :c func:`system` directly, but a function1128 :c func:`PySpam_System`, which would of course do something more complicated in1158 the C library function :c:func:`system` directly, but a function 1159 :c:func:`PySpam_System`, which would of course do something more complicated in 1129 1160 reality (such as adding "spam" to every command). This function 1130 :c func:`PySpam_System` is also exported to other extension modules.1131 1132 The function :c func:`PySpam_System` is a plain C function, declared1161 :c:func:`PySpam_System` is also exported to other extension modules. 1162 1163 The function :c:func:`PySpam_System` is a plain C function, declared 1133 1164 ``static`` like everything else:: 1134 1165 … … 1139 1170 } 1140 1171 1141 The function :c func:`spam_system` is modified in a trivial way::1172 The function :c:func:`spam_system` is modified in a trivial way:: 1142 1173 1143 1174 static PyObject * … … 1180 1211 PySpam_API[PySpam_System_NUM] = (void *)PySpam_System; 1181 1212 1182 /* Create a C Objectcontaining the API pointer array's address */1183 c_api_object = PyC Object_FromVoidPtr((void *)PySpam_API, NULL);1213 /* Create a Capsule containing the API pointer array's address */ 1214 c_api_object = PyCapsule_New((void *)PySpam_API, "spam._C_API", NULL); 1184 1215 1185 1216 if (c_api_object != NULL) … … 1223 1254 (*(PySpam_System_RETURN (*)PySpam_System_PROTO) PySpam_API[PySpam_System_NUM]) 1224 1255 1225 /* Return -1 and set exception on error, 0 on success. */ 1256 /* Return -1 on error, 0 on success. 1257 * PyCapsule_Import will set an exception if there's an error. 1258 */ 1226 1259 static int 1227 1260 import_spam(void) 1228 1261 { 1229 PyObject *c_api_object; 1230 PyObject *module; 1231 1232 module = PyImport_ImportModule("spam"); 1233 if (module == NULL) 1234 return -1; 1235 1236 c_api_object = PyObject_GetAttrString(module, "_C_API"); 1237 if (c_api_object == NULL) { 1238 Py_DECREF(module); 1239 return -1; 1240 } 1241 if (PyCObject_Check(c_api_object)) 1242 PySpam_API = (void **)PyCObject_AsVoidPtr(c_api_object); 1243 1244 Py_DECREF(c_api_object); 1245 Py_DECREF(module); 1246 return 0; 1262 PySpam_API = (void **)PyCapsule_Import("spam._C_API", 0); 1263 return (PySpam_API != NULL) ? 0 : -1; 1247 1264 } 1248 1265 … … 1256 1273 1257 1274 All that a client module must do in order to have access to the function 1258 :c func:`PySpam_System` is to call the function (or rather macro)1259 :c func:`import_spam` in its initialization function::1275 :c:func:`PySpam_System` is to call the function (or rather macro) 1276 :c:func:`import_spam` in its initialization function:: 1260 1277 1261 1278 PyMODINIT_FUNC … … 1276 1293 that is exported, so it has to be learned only once. 1277 1294 1278 Finally it should be mentioned that C Objects offer additional functionality,1295 Finally it should be mentioned that Capsules offer additional functionality, 1279 1296 which is especially useful for memory allocation and deallocation of the pointer 1280 stored in a C Object. The details are described in the Python/C API Reference1281 Manual in the section :ref:`c objects` and in the implementation of CObjects (files1282 :file:`Include/ cobject.h` and :file:`Objects/cobject.c` in the Python source1297 stored in a Capsule. The details are described in the Python/C API Reference 1298 Manual in the section :ref:`capsules` and in the implementation of Capsules (files 1299 :file:`Include/pycapsule.h` and :file:`Objects/pycapsule.c` in the Python source 1283 1300 code distribution). 1284 1301 -
python/vendor/current/Doc/extending/index.rst
r2 r388 5 5 ################################################## 6 6 7 :Release: |version|8 :Date: |today|9 10 7 This document describes how to write modules in C or C++ to extend the Python 11 interpreter with new modules. Those modules can define new functions but also12 new object types and their methods. The document also describes how to embed 13 t he Python interpreter in another application, for use as an extension language.14 Finally, it shows how to compile and link extension modules so that they can be 15 loaded dynamically (at run time) into the interpreter, if the underlying 16 operating system supports this feature.8 interpreter with new modules. Those modules can not only define new functions 9 but also new object types and their methods. The document also describes how 10 to embed the Python interpreter in another application, for use as an extension 11 language. Finally, it shows how to compile and link extension modules so that 12 they can be loaded dynamically (at run time) into the interpreter, if the 13 underlying operating system supports this feature. 17 14 18 15 This document assumes basic knowledge about Python. For an informal -
python/vendor/current/Doc/extending/newtypes.rst
r2 r388 35 35 36 36 The Python runtime sees all Python objects as variables of type 37 :c type:`PyObject\*`. A :ctype:`PyObject` is not a very magnificent object - it37 :c:type:`PyObject\*`. A :c:type:`PyObject` is not a very magnificent object - it 38 38 just contains the refcount and a pointer to the object's "type object". This is 39 39 where the action is; the type object determines which (C) functions get called 40 40 when, for instance, an attribute gets looked up on an object or it is multiplied 41 by another object. These C functions are called "type methods" to distinguish 42 them from things like ``[].append`` (which we call "object methods"). 41 by another object. These C functions are called "type methods". 43 42 44 43 So, if you want to define a new object type, you need to create a new type … … 105 104 }; 106 105 107 Now if you go and look up the definition of :c type:`PyTypeObject` in106 Now if you go and look up the definition of :c:type:`PyTypeObject` in 108 107 :file:`object.h` you'll see that it has many more fields that the definition 109 108 above. The remaining fields will be filled with zeros by the C compiler, and … … 121 120 as the type of a type object is "type", but this isn't strictly conforming C and 122 121 some compilers complain. Fortunately, this member will be filled in for us by 123 :c func:`PyType_Ready`. ::122 :c:func:`PyType_Ready`. :: 124 123 125 124 0, /* ob_size */ … … 147 146 148 147 This is so that Python knows how much memory to allocate when you call 149 :c func:`PyObject_New`.148 :c:func:`PyObject_New`. 150 149 151 150 .. note:: 152 151 153 152 If you want your type to be subclassable from Python, and your type has the same 154 : attr:`tp_basicsize` as its base type, you may have problems with multiple153 :c:member:`~PyTypeObject.tp_basicsize` as its base type, you may have problems with multiple 155 154 inheritance. A Python subclass of your type will have to list your type first 156 in its :attr:` __bases__`, or else it will not be able to call your type's155 in its :attr:`~class.__bases__`, or else it will not be able to call your type's 157 156 :meth:`__new__` method without getting an error. You can avoid this problem by 158 ensuring that your type has a larger value for : attr:`tp_basicsize` than its157 ensuring that your type has a larger value for :c:member:`~PyTypeObject.tp_basicsize` than its 159 158 base type does. Most of the time, this will be true anyway, because either your 160 159 base type will be :class:`object`, or else you will be adding data members to … … 176 175 members defined by the current version of Python. 177 176 178 We provide a doc string for the type in : attr:`tp_doc`. ::177 We provide a doc string for the type in :c:member:`~PyTypeObject.tp_doc`. :: 179 178 180 179 "Noddy objects", /* tp_doc */ … … 185 184 186 185 For now, all we want to be able to do is to create new :class:`Noddy` objects. 187 To enable object creation, we have to provide a : attr:`tp_new` implementation.186 To enable object creation, we have to provide a :c:member:`~PyTypeObject.tp_new` implementation. 188 187 In this case, we can just use the default implementation provided by the API 189 function :c func:`PyType_GenericNew`. We'd like to just assign this to the190 : attr:`tp_new` slot, but we can't, for portability sake, On some platforms or188 function :c:func:`PyType_GenericNew`. We'd like to just assign this to the 189 :c:member:`~PyTypeObject.tp_new` slot, but we can't, for portability sake, On some platforms or 191 190 compilers, we can't statically initialize a structure member with a function 192 defined in another C module, so, instead, we'll assign the : attr:`tp_new` slot191 defined in another C module, so, instead, we'll assign the :c:member:`~PyTypeObject.tp_new` slot 193 192 in the module initialization function just before calling 194 :c func:`PyType_Ready`::193 :c:func:`PyType_Ready`:: 195 194 196 195 noddy_NoddyType.tp_new = PyType_GenericNew; … … 202 201 203 202 Everything else in the file should be familiar, except for some code in 204 :c func:`initnoddy`::203 :c:func:`initnoddy`:: 205 204 206 205 if (PyType_Ready(&noddy_NoddyType) < 0) … … 253 252 We've added an extra include:: 254 253 255 #include "structmember.h"254 #include <structmember.h> 256 255 257 256 This include provides declarations that we use to handle attributes, as … … 285 284 } 286 285 287 which is assigned to the : attr:`tp_dealloc` member::286 which is assigned to the :c:member:`~PyTypeObject.tp_dealloc` member:: 288 287 289 288 (destructor)Noddy_dealloc, /*tp_dealloc*/ 290 289 291 290 This method decrements the reference counts of the two Python attributes. We use 292 :c func:`Py_XDECREF` here because the :attr:`first` and :attr:`last` members293 could be *NULL*. It then calls the : attr:`tp_free` member of the object's type291 :c:func:`Py_XDECREF` here because the :attr:`first` and :attr:`last` members 292 could be *NULL*. It then calls the :c:member:`~PyTypeObject.tp_free` member of the object's type 294 293 to free the object's memory. Note that the object's type might not be 295 294 :class:`NoddyType`, because the object may be an instance of a subclass. … … 325 324 } 326 325 327 and install it in the : attr:`tp_new` member::326 and install it in the :c:member:`~PyTypeObject.tp_new` member:: 328 327 329 328 Noddy_new, /* tp_new */ … … 336 335 to make sure that the initial values of the members :attr:`first` and 337 336 :attr:`last` are not *NULL*. If we didn't care whether the initial values were 338 *NULL*, we could have used :c func:`PyType_GenericNew` as our new method, as we339 did before. :c func:`PyType_GenericNew` initializes all of the instance variable337 *NULL*, we could have used :c:func:`PyType_GenericNew` as our new method, as we 338 did before. :c:func:`PyType_GenericNew` initializes all of the instance variable 340 339 members to *NULL*. 341 340 … … 346 345 methods. Note that if the type supports subclassing, the type passed may not be 347 346 the type being defined. The new method calls the tp_alloc slot to allocate 348 memory. We don't fill the : attr:`tp_alloc` slot ourselves. Rather349 :c func:`PyType_Ready` fills it for us by inheriting it from our base class,347 memory. We don't fill the :c:member:`~PyTypeObject.tp_alloc` slot ourselves. Rather 348 :c:func:`PyType_Ready` fills it for us by inheriting it from our base class, 350 349 which is :class:`object` by default. Most types use the default allocation. 351 350 352 351 .. note:: 353 352 354 If you are creating a co-operative : attr:`tp_new` (one that calls a base type's355 : attr:`tp_new` or :meth:`__new__`), you must *not* try to determine what method353 If you are creating a co-operative :c:member:`~PyTypeObject.tp_new` (one that calls a base type's 354 :c:member:`~PyTypeObject.tp_new` or :meth:`__new__`), you must *not* try to determine what method 356 355 to call using method resolution order at runtime. Always statically determine 357 what type you are going to call, and call its : attr:`tp_new` directly, or via356 what type you are going to call, and call its :c:member:`~PyTypeObject.tp_new` directly, or via 358 357 ``type->tp_base->tp_new``. If you do not do this, Python subclasses of your 359 358 type that also inherit from other Python-defined classes may not work correctly. … … 392 391 } 393 392 394 by filling the : attr:`tp_init` slot. ::393 by filling the :c:member:`~PyTypeObject.tp_init` slot. :: 395 394 396 395 (initproc)Noddy_init, /* tp_init */ 397 396 398 The : attr:`tp_init` slot is exposed in Python as the :meth:`__init__` method. It397 The :c:member:`~PyTypeObject.tp_init` slot is exposed in Python as the :meth:`__init__` method. It 399 398 is used to initialize an object after it's created. Unlike the new method, we 400 399 can't guarantee that the initializer is called. The initializer isn't called … … 426 425 back into our type's code 427 426 428 * when decrementing a reference count in a : attr:`tp_dealloc` handler when427 * when decrementing a reference count in a :c:member:`~PyTypeObject.tp_dealloc` handler when 429 428 garbage-collections is not supported [#]_ 430 429 … … 442 441 }; 443 442 444 and put the definitions in the : attr:`tp_members` slot::443 and put the definitions in the :c:member:`~PyTypeObject.tp_members` slot:: 445 444 446 445 Noddy_members, /* tp_members */ 447 446 448 447 Each member definition has a member name, type, offset, access flags and 449 documentation string. See the "Generic Attribute Management"section below for448 documentation string. See the :ref:`Generic-Attribute-Management` section below for 450 449 details. 451 450 … … 518 517 }; 519 518 520 and assign them to the : attr:`tp_methods` slot::519 and assign them to the :c:member:`~PyTypeObject.tp_methods` slot:: 521 520 522 521 Noddy_methods, /* tp_methods */ … … 532 531 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ 533 532 534 We rename :c func:`initnoddy` to :cfunc:`initnoddy2` and update the module name535 passed to :c func:`Py_InitModule3`.533 We rename :c:func:`initnoddy` to :c:func:`initnoddy2` and update the module name 534 passed to :c:func:`Py_InitModule3`. 536 535 537 536 Finally, we update our :file:`setup.py` file to build the new module:: … … 599 598 attribute value is not a string. 600 599 601 We create an array of :c type:`PyGetSetDef` structures::600 We create an array of :c:type:`PyGetSetDef` structures:: 602 601 603 602 static PyGetSetDef Noddy_getseters[] = { … … 613 612 }; 614 613 615 and register it in the : attr:`tp_getset` slot::614 and register it in the :c:member:`~PyTypeObject.tp_getset` slot:: 616 615 617 616 Noddy_getseters, /* tp_getset */ … … 619 618 to register our attribute getters and setters. 620 619 621 The last item in a :c type:`PyGetSetDef` structure is the closure mentioned620 The last item in a :c:type:`PyGetSetDef` structure is the closure mentioned 622 621 above. In this case, we aren't using the closure, so we just pass *NULL*. 623 622 … … 630 629 }; 631 630 632 We also need to update the : attr:`tp_init` handler to only allow strings [#]_ to631 We also need to update the :c:member:`~PyTypeObject.tp_init` handler to only allow strings [#]_ to 633 632 be passed:: 634 633 … … 664 663 With these changes, we can assure that the :attr:`first` and :attr:`last` 665 664 members are never *NULL* so we can remove checks for *NULL* values in almost all 666 cases. This means that most of the :c func:`Py_XDECREF` calls can be converted to667 :c func:`Py_DECREF` calls. The only place we can't change these calls is in the665 cases. This means that most of the :c:func:`Py_XDECREF` calls can be converted to 666 :c:func:`Py_DECREF` calls. The only place we can't change these calls is in the 668 667 deallocator, where there is the possibility that the initialization of these 669 668 members failed in the constructor. … … 730 729 731 730 For each subobject that can participate in cycles, we need to call the 732 :c func:`visit` function, which is passed to the traversal method. The733 :c func:`visit` function takes as arguments the subobject and the extra argument731 :c:func:`visit` function, which is passed to the traversal method. The 732 :c:func:`visit` function takes as arguments the subobject and the extra argument 734 733 *arg* passed to the traversal method. It returns an integer value that must be 735 734 returned if it is non-zero. 736 735 737 Python 2.4 and higher provide a :c func:`Py_VISIT` macro that automates calling738 visit functions. With :c func:`Py_VISIT`, :cfunc:`Noddy_traverse` can be736 Python 2.4 and higher provide a :c:func:`Py_VISIT` macro that automates calling 737 visit functions. With :c:func:`Py_VISIT`, :c:func:`Noddy_traverse` can be 739 738 simplified:: 740 739 … … 749 748 .. note:: 750 749 751 Note that the : attr:`tp_traverse` implementation must name its arguments exactly752 *visit* and *arg* in order to use :c func:`Py_VISIT`. This is to encourage750 Note that the :c:member:`~PyTypeObject.tp_traverse` implementation must name its arguments exactly 751 *visit* and *arg* in order to use :c:func:`Py_VISIT`. This is to encourage 753 752 uniformity across these boring implementations. 754 753 … … 780 779 } 781 780 782 Notice the use of a temporary variable in :c func:`Noddy_clear`. We use the781 Notice the use of a temporary variable in :c:func:`Noddy_clear`. We use the 783 782 temporary variable so that we can set each member to *NULL* before decrementing 784 783 its reference count. We do this because, as was discussed earlier, if the … … 786 785 the object. In addition, because we now support garbage collection, we also 787 786 have to worry about code being run that triggers garbage collection. If garbage 788 collection is run, our : attr:`tp_traverse` handler could get called. We can't789 take a chance of having :c func:`Noddy_traverse` called when a member's reference787 collection is run, our :c:member:`~PyTypeObject.tp_traverse` handler could get called. We can't 788 take a chance of having :c:func:`Noddy_traverse` called when a member's reference 790 789 count has dropped to zero and its value hasn't been set to *NULL*. 791 790 792 Python 2.4 and higher provide a :c func:`Py_CLEAR` that automates the careful793 decrementing of reference counts. With :c func:`Py_CLEAR`, the794 :c func:`Noddy_clear` function can be simplified::791 Python 2.4 and higher provide a :c:func:`Py_CLEAR` that automates the careful 792 decrementing of reference counts. With :c:func:`Py_CLEAR`, the 793 :c:func:`Noddy_clear` function can be simplified:: 795 794 796 795 static int … … 806 805 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/ 807 806 808 That's pretty much it. If we had written custom : attr:`tp_alloc` or809 : attr:`tp_free` slots, we'd need to modify them for cyclic-garbage collection.807 That's pretty much it. If we had written custom :c:member:`~PyTypeObject.tp_alloc` or 808 :c:member:`~PyTypeObject.tp_free` slots, we'd need to modify them for cyclic-garbage collection. 810 809 Most extensions will use the versions automatically provided. 811 810 … … 847 846 The primary difference for derived type objects is that the base type's object 848 847 structure must be the first value. The base type will already include the 849 :c func:`PyObject_HEAD` at the beginning of its structure.848 :c:func:`PyObject_HEAD` at the beginning of its structure. 850 849 851 850 When a Python object is a :class:`Shoddy` instance, its *PyObject\** pointer can … … 866 865 This pattern is important when writing a type with custom :attr:`new` and 867 866 :attr:`dealloc` methods. The :attr:`new` method should not actually create the 868 memory for the object with : attr:`tp_alloc`, that will be handled by the base869 class when calling its : attr:`tp_new`.870 871 When filling out the :c func:`PyTypeObject` for the :class:`Shoddy` type, you see872 a slot for :c func:`tp_base`. Due to cross platform compiler issues, you can't873 fill that field directly with the :c func:`PyList_Type`; it can be done later in874 the module's :c func:`init` function. ::867 memory for the object with :c:member:`~PyTypeObject.tp_alloc`, that will be handled by the base 868 class when calling its :c:member:`~PyTypeObject.tp_new`. 869 870 When filling out the :c:func:`PyTypeObject` for the :class:`Shoddy` type, you see 871 a slot for :c:func:`tp_base`. Due to cross platform compiler issues, you can't 872 fill that field directly with the :c:func:`PyList_Type`; it can be done later in 873 the module's :c:func:`init` function. :: 875 874 876 875 PyMODINIT_FUNC … … 891 890 } 892 891 893 Before calling :c func:`PyType_Ready`, the type structure must have the894 : attr:`tp_base` slot filled in. When we are deriving a new type, it is not895 necessary to fill out the : attr:`tp_alloc` slot with :cfunc:`PyType_GenericNew`892 Before calling :c:func:`PyType_Ready`, the type structure must have the 893 :c:member:`~PyTypeObject.tp_base` slot filled in. When we are deriving a new type, it is not 894 necessary to fill out the :c:member:`~PyTypeObject.tp_alloc` slot with :c:func:`PyType_GenericNew` 896 895 -- the allocate function from the base type will be inherited. 897 896 898 After that, calling :c func:`PyType_Ready` and adding the type object to the897 After that, calling :c:func:`PyType_Ready` and adding the type object to the 899 898 module is the same as with the basic :class:`Noddy` examples. 900 899 … … 908 907 implement and what they do. 909 908 910 Here is the definition of :c type:`PyTypeObject`, with some fields only used in909 Here is the definition of :c:type:`PyTypeObject`, with some fields only used in 911 910 debug builds omitted: 912 911 … … 936 935 These fields tell the runtime how much memory to allocate when new objects of 937 936 this type are created. Python has some built-in support for variable length 938 structures (think: strings, lists) which is where the : attr:`tp_itemsize` field937 structures (think: strings, lists) which is where the :c:member:`~PyTypeObject.tp_itemsize` field 939 938 comes in. This will be dealt with later. :: 940 939 … … 986 985 errors from the interpreter. The proper way to protect against this is to save 987 986 a pending exception before performing the unsafe action, and restoring it when 988 done. This can be done using the :c func:`PyErr_Fetch` and989 :c func:`PyErr_Restore` functions::987 done. This can be done using the :c:func:`PyErr_Fetch` and 988 :c:func:`PyErr_Restore` functions:: 990 989 991 990 static void … … 1028 1027 :func:`str` function, and the :keyword:`print` statement. For most objects, the 1029 1028 :keyword:`print` statement is equivalent to the :func:`str` function, but it is 1030 possible to special-case printing to a :c type:`FILE\*` if necessary; this should1029 possible to special-case printing to a :c:type:`FILE\*` if necessary; this should 1031 1030 only be done if efficiency is identified as a problem and profiling suggests 1032 1031 that creating a temporary string object to be written to a file is too … … 1034 1033 1035 1034 These handlers are all optional, and most types at most need to implement the 1036 : attr:`tp_str` and :attr:`tp_repr` handlers. ::1035 :c:member:`~PyTypeObject.tp_str` and :c:member:`~PyTypeObject.tp_repr` handlers. :: 1037 1036 1038 1037 reprfunc tp_repr; … … 1040 1039 printfunc tp_print; 1041 1040 1042 The : attr:`tp_repr` handler should return a string object containing a1041 The :c:member:`~PyTypeObject.tp_repr` handler should return a string object containing a 1043 1042 representation of the instance for which it is called. Here is a simple 1044 1043 example:: … … 1051 1050 } 1052 1051 1053 If no : attr:`tp_repr` handler is specified, the interpreter will supply a1054 representation that uses the type's : attr:`tp_name` and a uniquely-identifying1052 If no :c:member:`~PyTypeObject.tp_repr` handler is specified, the interpreter will supply a 1053 representation that uses the type's :c:member:`~PyTypeObject.tp_name` and a uniquely-identifying 1055 1054 value for the object. 1056 1055 1057 The : attr:`tp_str` handler is to :func:`str` what the :attr:`tp_repr` handler1056 The :c:member:`~PyTypeObject.tp_str` handler is to :func:`str` what the :c:member:`~PyTypeObject.tp_repr` handler 1058 1057 described above is to :func:`repr`; that is, it is called when Python code calls 1059 1058 :func:`str` on an instance of your object. Its implementation is very similar 1060 to the : attr:`tp_repr` function, but the resulting string is intended for human1061 consumption. If : attr:`tp_str` is not specified, the :attr:`tp_repr` handler is1059 to the :c:member:`~PyTypeObject.tp_repr` function, but the resulting string is intended for human 1060 consumption. If :c:member:`~PyTypeObject.tp_str` is not specified, the :c:member:`~PyTypeObject.tp_repr` handler is 1062 1061 used instead. 1063 1062 … … 1112 1111 Python supports two pairs of attribute handlers; a type that supports attributes 1113 1112 only needs to implement the functions for one pair. The difference is that one 1114 pair takes the name of the attribute as a :c type:`char\*`, while the other1115 accepts a :c type:`PyObject\*`. Each type can use whichever pair makes more1113 pair takes the name of the attribute as a :c:type:`char\*`, while the other 1114 accepts a :c:type:`PyObject\*`. Each type can use whichever pair makes more 1116 1115 sense for the implementation's convenience. :: 1117 1116 … … 1124 1123 If accessing attributes of an object is always a simple operation (this will be 1125 1124 explained shortly), there are generic implementations which can be used to 1126 provide the :c type:`PyObject\*` version of the attribute management functions.1125 provide the :c:type:`PyObject\*` version of the attribute management functions. 1127 1126 The actual need for type-specific attribute handlers almost completely 1128 1127 disappeared starting with Python 2.2, though there are many examples which have … … 1130 1129 1131 1130 1131 .. _generic-attribute-management: 1132 1132 1133 Generic Attribute Management 1133 1134 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ … … 1138 1139 attributes simple? There are only a couple of conditions that must be met: 1139 1140 1140 #. The name of the attributes must be known when :c func:`PyType_Ready` is1141 #. The name of the attributes must be known when :c:func:`PyType_Ready` is 1141 1142 called. 1142 1143 … … 1147 1148 attributes, when the values are computed, or how relevant data is stored. 1148 1149 1149 When :c func:`PyType_Ready` is called, it uses three tables referenced by the1150 When :c:func:`PyType_Ready` is called, it uses three tables referenced by the 1150 1151 type object to create :term:`descriptor`\s which are placed in the dictionary of the 1151 1152 type object. Each descriptor controls access to one attribute of the instance 1152 1153 object. Each of the tables is optional; if all three are *NULL*, instances of 1153 1154 the type will only have attributes that are inherited from their base type, and 1154 should leave the : attr:`tp_getattro` and :attr:`tp_setattro` fields *NULL* as1155 should leave the :c:member:`~PyTypeObject.tp_getattro` and :c:member:`~PyTypeObject.tp_setattro` fields *NULL* as 1155 1156 well, allowing the base type to handle attributes. 1156 1157 … … 1161 1162 struct PyGetSetDef *tp_getset; 1162 1163 1163 If : attr:`tp_methods` is not *NULL*, it must refer to an array of1164 :c type:`PyMethodDef` structures. Each entry in the table is an instance of this1164 If :c:member:`~PyTypeObject.tp_methods` is not *NULL*, it must refer to an array of 1165 :c:type:`PyMethodDef` structures. Each entry in the table is an instance of this 1165 1166 structure:: 1166 1167 … … 1225 1226 single: RESTRICTED 1226 1227 1227 An interesting advantage of using the : attr:`tp_members` table to build1228 An interesting advantage of using the :c:member:`~PyTypeObject.tp_members` table to build 1228 1229 descriptors that are used at runtime is that any attribute defined this way can 1229 1230 have an associated doc string simply by providing the text in the table. An … … 1231 1232 class object, and get the doc string using its :attr:`__doc__` attribute. 1232 1233 1233 As with the : attr:`tp_methods` table, a sentinel entry with a :attr:`name` value1234 As with the :c:member:`~PyTypeObject.tp_methods` table, a sentinel entry with a :attr:`name` value 1234 1235 of *NULL* is required. 1235 1236 … … 1247 1248 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 1248 1249 1249 For simplicity, only the :c type:`char\*` version will be demonstrated here; the1250 type of the name parameter is the only difference between the :c type:`char\*`1251 and :c type:`PyObject\*` flavors of the interface. This example effectively does1250 For simplicity, only the :c:type:`char\*` version will be demonstrated here; the 1251 type of the name parameter is the only difference between the :c:type:`char\*` 1252 and :c:type:`PyObject\*` flavors of the interface. This example effectively does 1252 1253 the same thing as the generic example above, but does not use the generic 1253 1254 support added in Python 2.2. The value in showing this is two-fold: it … … 1257 1258 what needs to be done. 1258 1259 1259 The : attr:`tp_getattr` handler is called when the object requires an attribute1260 The :c:member:`~PyTypeObject.tp_getattr` handler is called when the object requires an attribute 1260 1261 look-up. It is called in the same situations where the :meth:`__getattr__` 1261 1262 method of a class would be called. 1262 1263 1263 1264 A likely way to handle this is (1) to implement a set of functions (such as 1264 :c func:`newdatatype_getSize` and :cfunc:`newdatatype_setSize` in the example1265 :c:func:`newdatatype_getSize` and :c:func:`newdatatype_setSize` in the example 1265 1266 below), (2) provide a method table listing these functions, and (3) provide a 1266 1267 getattr function that returns the result of a lookup in that table. The method 1267 table uses the same structure as the : attr:`tp_methods` field of the type1268 table uses the same structure as the :c:member:`~PyTypeObject.tp_methods` field of the type 1268 1269 object. 1269 1270 … … 1284 1285 } 1285 1286 1286 The : attr:`tp_setattr` handler is called when the :meth:`__setattr__` or1287 The :c:member:`~PyTypeObject.tp_setattr` handler is called when the :meth:`__setattr__` or 1287 1288 :meth:`__delattr__` method of a class instance would be called. When an 1288 1289 attribute should be deleted, the third parameter will be *NULL*. Here is an 1289 1290 example that simply raises an exception; if this were really all you wanted, the 1290 : attr:`tp_setattr` handler should be set to *NULL*. ::1291 :c:member:`~PyTypeObject.tp_setattr` handler should be set to *NULL*. :: 1291 1292 1292 1293 static int … … 1305 1306 cmpfunc tp_compare; 1306 1307 1307 The : attr:`tp_compare` handler is called when comparisons are needed and the1308 The :c:member:`~PyTypeObject.tp_compare` handler is called when comparisons are needed and the 1308 1309 object does not implement the specific rich comparison method which matches the 1309 1310 requested comparison. (It is always used if defined and the 1310 :c func:`PyObject_Compare` or :cfunc:`PyObject_Cmp` functions are used, or if1311 :c:func:`PyObject_Compare` or :c:func:`PyObject_Cmp` functions are used, or if 1311 1312 :func:`cmp` is used from Python.) It is analogous to the :meth:`__cmp__` method. 1312 1313 This function should return ``-1`` if *obj1* is less than *obj2*, ``0`` if they … … 1316 1317 future, other return values may be assigned a different meaning.) 1317 1318 1318 A : attr:`tp_compare` handler may raise an exception. In this case it should1319 A :c:member:`~PyTypeObject.tp_compare` handler may raise an exception. In this case it should 1319 1320 return a negative value. The caller has to test for the exception using 1320 :c func:`PyErr_Occurred`.1321 :c:func:`PyErr_Occurred`. 1321 1322 1322 1323 Here is a sample implementation:: … … 1360 1361 to indicate the presence of a slot, but a slot may still be unfilled.) :: 1361 1362 1362 PyNumberMethods tp_as_number;1363 PySequenceMethods tp_as_sequence;1364 PyMappingMethods tp_as_mapping;1363 PyNumberMethods *tp_as_number; 1364 PySequenceMethods *tp_as_sequence; 1365 PyMappingMethods *tp_as_mapping; 1365 1366 1366 1367 If you wish your object to be able to act like a number, a sequence, or a 1367 1368 mapping object, then you place the address of a structure that implements the C 1368 type :c type:`PyNumberMethods`, :ctype:`PySequenceMethods`, or1369 :c type:`PyMappingMethods`, respectively. It is up to you to fill in this1369 type :c:type:`PyNumberMethods`, :c:type:`PySequenceMethods`, or 1370 :c:type:`PyMappingMethods`, respectively. It is up to you to fill in this 1370 1371 structure with appropriate values. You can find examples of the use of each of 1371 1372 these in the :file:`Objects` directory of the Python source distribution. :: … … 1391 1392 This function is called when an instance of your data type is "called", for 1392 1393 example, if ``obj1`` is an instance of your data type and the Python script 1393 contains ``obj1('hello')``, the : attr:`tp_call` handler is invoked.1394 contains ``obj1('hello')``, the :c:member:`~PyTypeObject.tp_call` handler is invoked. 1394 1395 1395 1396 This function takes three arguments: … … 1399 1400 1400 1401 #. *arg2* is a tuple containing the arguments to the call. You can use 1401 :c func:`PyArg_ParseTuple` to extract the arguments.1402 :c:func:`PyArg_ParseTuple` to extract the arguments. 1402 1403 1403 1404 #. *arg3* is a dictionary of keyword arguments that were passed. If this is 1404 1405 non-*NULL* and you support keyword arguments, use 1405 :c func:`PyArg_ParseTupleAndKeywords` to extract the arguments. If you do not1406 :c:func:`PyArg_ParseTupleAndKeywords` to extract the arguments. If you do not 1406 1407 want to support keyword arguments and this is non-*NULL*, raise a 1407 1408 :exc:`TypeError` with a message saying that keyword arguments are not supported. … … 1478 1479 1479 1480 For an object to be weakly referencable, the extension must include a 1480 :c type:`PyObject\*` field in the instance structure for the use of the weak1481 :c:type:`PyObject\*` field in the instance structure for the use of the weak 1481 1482 reference mechanism; it must be initialized to *NULL* by the object's 1482 constructor. It must also set the : attr:`tp_weaklistoffset` field of the1483 constructor. It must also set the :c:member:`~PyTypeObject.tp_weaklistoffset` field of the 1483 1484 corresponding type object to the offset of the field. For example, the instance 1484 1485 type is defined with the following structure:: … … 1521 1522 1522 1523 The only further addition is that the destructor needs to call the weak 1523 reference manager to clear any weak references. This should be done before any 1524 other parts of the destruction have occurred, but is only required if the weak 1525 reference list is non-*NULL*:: 1524 reference manager to clear any weak references. This is only required if the 1525 weak reference list is non-*NULL*:: 1526 1526 1527 1527 static void … … 1554 1554 1555 1555 When you need to verify that an object is an instance of the type you are 1556 implementing, use the :c func:`PyObject_TypeCheck` function. A sample of its use1556 implementing, use the :c:func:`PyObject_TypeCheck` function. A sample of its use 1557 1557 might be something like the following:: 1558 1558 … … 1567 1567 float. 1568 1568 1569 .. [#] We relied on this in the : attr:`tp_dealloc` handler in this example, because our1569 .. [#] We relied on this in the :c:member:`~PyTypeObject.tp_dealloc` handler in this example, because our 1570 1570 type doesn't support garbage collection. Even if a type supports garbage 1571 1571 collection, there are calls that can be made to "untrack" the object from -
python/vendor/current/Doc/extending/windows.rst
r2 r388 99 99 not necessarily have to match the module name, but the name of the 100 100 initialization function should match the module name --- you can only import a 101 module :mod:`spam` if its initialization function is called :c func:`initspam`,102 and it should call :c func:`Py_InitModule` with the string ``"spam"`` as its101 module :mod:`spam` if its initialization function is called :c:func:`initspam`, 102 and it should call :c:func:`Py_InitModule` with the string ``"spam"`` as its 103 103 first argument (use the minimal :file:`example.c` in this directory as a guide). 104 104 By convention, it lives in a file called :file:`spam.c` or :file:`spammodule.c`. … … 115 115 116 116 #. Copy :file:`example.sln` and :file:`example.vcproj`, rename them to 117 117 :file:`spam.\*`, and edit them by hand, or 118 118 119 119 #. Create a brand new project; instructions are below. … … 176 176 PyObject_HEAD_INIT(&PyType_Type) 177 177 178 Change it to:: 178 Static type object initializers in extension modules may cause 179 compiles to fail with an error message like "initializer not a 180 constant". This shows up when building DLL under MSVC. Change it to:: 179 181 180 182 PyObject_HEAD_INIT(NULL) … … 182 184 and add the following to the module initialization function:: 183 185 184 MyObject_Type.ob_type = &PyType_Type; 185 186 Refer to section 3 of the `Python FAQ <http://www.python.org/doc/faq>`_ for 187 details on why you must do this. 186 if (PyType_Ready(&MyObject_Type) < 0) 187 return NULL; 188 188 189 189 … … 264 264 The first command created three files: :file:`spam.obj`, :file:`spam.dll` and 265 265 :file:`spam.lib`. :file:`Spam.dll` does not contain any Python functions (such 266 as :c func:`PyArg_ParseTuple`), but it does know how to find the Python code266 as :c:func:`PyArg_ParseTuple`), but it does know how to find the Python code 267 267 thanks to :file:`pythonXY.lib`. 268 268
Note:
See TracChangeset
for help on using the changeset viewer.