This is /home/pdm/install/Python-2.1/Doc/api/python-api.info, produced by makeinfo version 4.0 from api.texi. April 15, 2001 2.1  File: python-api.info, Node: Module Objects, Next: CObjects, Prev: Instance Objects, Up: Other Objects Module Objects -------------- There are only a few functions special to module objects. `PyTypeObject PyModule_Type' This instance of `PyTypeObject' represents the Python module type. This is exposed to Python programs as `types.ModuleType'. `int PyModule_Check(PyObject *p)' Returns true if its argument is a module object. `PyObject* PyModule_New(char *name)' Return a new module object with the `__name__' attribute set to NAME. Only the module's `__doc__' and `__name__' attributes are filled in; the caller is responsible for providing a `__file__' attribute. `PyObject* PyModule_GetDict(PyObject *module)' Return the dictionary object that implements MODULE's namespace; this object is the same as the `__dict__' attribute of the module object. This function never fails. `char* PyModule_GetName(PyObject *module)' Return MODULE's `__name__' value. If the module does not provide one, or if it is not a string, `SystemError' is raised and `NULL' is returned. `char* PyModule_GetFilename(PyObject *module)' Return the name of the file from which MODULE was loaded using MODULE's `__file__' attribute. If this is not defined, or if it is not a string, raise `SystemError' and return `NULL'. `int PyModule_AddObject(PyObject *module, char *name, PyObject *value)' Add an object to MODULE as NAME. This is a convenience function which can be used from the module's initialization function. This steals a reference to VALUE. Returns `-1' on error, `0' on success. _Added in Python version 2.0_ `int PyModule_AddIntConstant(PyObject *module, char *name, int value)' Add an integer constant to MODULE as NAME. This convenience function can be used from the module's initialization function. Returns `-1' on error, `0' on success. _Added in Python version 2.0_ `int PyModule_AddStringConstant(PyObject *module, char *name, char *value)' Add a string constant to MODULE as NAME. This convenience function can be used from the module's initialization function. The string VALUE must be null-terminated. Returns `-1' on error, `0' on success. _Added in Python version 2.0_  File: python-api.info, Node: CObjects, Prev: Module Objects, Up: Other Objects CObjects -------- Refer to _Extending and Embedding the Python Interpreter_, section 1.12 ("Providing a C API for an Extension Module"), for more information on using these objects. `PyCObject' This subtype of `PyObject' represents an opaque value, useful for C extension modules who need to pass an opaque value (as a `void*' pointer) through Python code to other C code. It is often used to make a C function pointer defined in one module available to other modules, so the regular import mechanism can be used to access C APIs defined in dynamically loaded modules. `int PyCObject_Check(PyObject *p)' Returns true if its argument is a `PyCObject'. `PyObject* PyCObject_FromVoidPtr(void* cobj, void (*destr)(void *))' Creates a `PyCObject' from the `void *'COBJ. The DESTR function will be called when the object is reclaimed, unless it is `NULL'. `PyObject* PyCObject_FromVoidPtrAndDesc(void* cobj, void* desc, void (*destr)(void *, void *) )' Creates a `PyCObject' from the `void *'COBJ. The DESTR function will be called when the object is reclaimed. The DESC argument can be used to pass extra callback data for the destructor function. `void* PyCObject_AsVoidPtr(PyObject* self)' Returns the object `void *' that the `PyCObject' SELF was created with. `void* PyCObject_GetDesc(PyObject* self)' Returns the description `void *' that the `PyCObject' SELF was created with.  File: python-api.info, Node: Initialization, Next: Memory Management, Prev: Concrete Objects Layer, Up: Top Initialization, Finalization, and Threads ***************************************** `void Py_Initialize()' Initialize the Python interpreter. In an application embedding Python, this should be called before using any other Python/C API functions; with the exception of `Py_SetProgramName()', `PyEval_InitThreads()', `PyEval_ReleaseLock()', and `PyEval_AcquireLock()'. This initializes the table of loaded modules (`sys.modules'), and creates the fundamental modules `__builtin__', `__main__' and `sys'. It also initializes the module search path (`sys.path'). It does not set `sys.argv'; use `PySys_SetArgv()' for that. This is a no-op when called for a second time (without calling `Py_Finalize()' first). There is no return value; it is a fatal error if the initialization fails. `int Py_IsInitialized()' Return true (nonzero) when the Python interpreter has been initialized, false (zero) if not. After `Py_Finalize()' is called, this returns false until `Py_Initialize()' is called again. `void Py_Finalize()' Undo all initializations made by `Py_Initialize()' and subsequent use of Python/C API functions, and destroy all sub-interpreters (see `Py_NewInterpreter()' below) that were created and not yet destroyed since the last call to `Py_Initialize()'. Ideally, this frees all memory allocated by the Python interpreter. This is a no-op when called for a second time (without calling `Py_Initialize()' again first). There is no return value; errors during finalization are ignored. This function is provided for a number of reasons. An embedding application might want to restart Python without having to restart the application itself. An application that has loaded the Python interpreter from a dynamically loadable library (or DLL) might want to free all memory allocated by Python before unloading the DLL. During a hunt for memory leaks in an application a developer might want to free all memory allocated by Python before exiting from the application. *Bugs and caveats:* The destruction of modules and objects in modules is done in random order; this may cause destructors (`__del__()' methods) to fail when they depend on other objects (even functions) or modules. Dynamically loaded extension modules loaded by Python are not unloaded. Small amounts of memory allocated by the Python interpreter may not be freed (if you find a leak, please report it). Memory tied up in circular references between objects is not freed. Some memory allocated by extension modules may not be freed. Some extension may not work properly if their initialization routine is called more than once; this can happen if an applcation calls `Py_Initialize()' and `Py_Finalize()' more than once. `PyThreadState* Py_NewInterpreter()' Create a new sub-interpreter. This is an (almost) totally separate environment for the execution of Python code. In particular, the new interpreter has separate, independent versions of all imported modules, including the fundamental modules `__builtin__', `__main__' and `sys'. The table of loaded modules (`sys.modules') and the module search path (`sys.path') are also separate. The new environment has no `sys.argv' variable. It has new standard I/O stream file objects `sys.stdin', `sys.stdout' and `sys.stderr' (however these refer to the same underlying `FILE' structures in the C library). The return value points to the first thread state created in the new sub-interpreter. This thread state is made the current thread state. Note that no actual thread is created; see the discussion of thread states below. If creation of the new interpreter is unsuccessful, `NULL' is returned; no exception is set since the exception state is stored in the current thread state and there may not be a current thread state. (Like all other Python/C API functions, the global interpreter lock must be held before calling this function and is still held when it returns; however, unlike most other Python/C API functions, there needn't be a current thread state on entry.) Extension modules are shared between (sub-)interpreters as follows: the first time a particular extension is imported, it is initialized normally, and a (shallow) copy of its module's dictionary is squirreled away. When the same extension is imported by another (sub-)interpreter, a new module is initialized and filled with the contents of this copy; the extension's `init' function is not called. Note that this is different from what happens when an extension is imported after the interpreter has been completely re-initialized by calling `Py_Finalize()' and `Py_Initialize()'; in that case, the extension's `initMODULE' function _is_ called again. *Bugs and caveats:* Because sub-interpreters (and the main interpreter) are part of the same process, the insulation between them isn't perfect -- for example, using low-level file operations like `os.close()' they can (accidentally or maliciously) affect each other's open files. Because of the way extensions are shared between (sub-)interpreters, some extensions may not work properly; this is especially likely when the extension makes use of (static) global variables, or when the extension manipulates its module's dictionary after its initialization. It is possible to insert objects created in one sub-interpreter into a namespace of another sub-interpreter; this should be done with great care to avoid sharing user-defined functions, methods, instances or classes between sub-interpreters, since import operations executed by such objects may affect the wrong (sub-)interpreter's dictionary of loaded modules. (XXX This is a hard-to-fix bug that will be addressed in a future release.) `void Py_EndInterpreter(PyThreadState *tstate)' Destroy the (sub-)interpreter represented by the given thread state. The given thread state must be the current thread state. See the discussion of thread states below. When the call returns, the current thread state is `NULL'. All thread states associated with this interpreted are destroyed. (The global interpreter lock must be held before calling this function and is still held when it returns.) `Py_Finalize()' will destroy all sub-interpreters that haven't been explicitly destroyed at that point. `void Py_SetProgramName(char *name)' This function should be called before `Py_Initialize()' is called for the first time, if it is called at all. It tells the interpreter the value of the `argv[0]' argument to the `main()' function of the program. This is used by `Py_GetPath()' and some other functions below to find the Python run-time libraries relative to the interpreter executable. The default value is `'python''. The argument should point to a zero-terminated character string in static storage whose contents will not change for the duration of the program's execution. No code in the Python interpreter will change the contents of this storage. `char* Py_GetProgramName()' Return the program name set with `Py_SetProgramName()', or the default. The returned string points into static storage; the caller should not modify its value. `char* Py_GetPrefix()' Return the _prefix_ for installed platform-independent files. This is derived through a number of complicated rules from the program name set with `Py_SetProgramName()' and some environment variables; for example, if the program name is `'/usr/local/bin/python'', the prefix is `'/usr/local''. The returned string points into static storage; the caller should not modify its value. This corresponds to the `prefix' variable in the top-level `Makefile' and the `--prefix' argument to the `configure' script at build time. The value is available to Python code as `sys.prefix'. It is only useful on UNIX. See also the next function. `char* Py_GetExecPrefix()' Return the _exec-prefix_ for installed platform-_de_pendent files. This is derived through a number of complicated rules from the program name set with `Py_SetProgramName()' and some environment variables; for example, if the program name is `'/usr/local/bin/python'', the exec-prefix is `'/usr/local''. The returned string points into static storage; the caller should not modify its value. This corresponds to the `exec_prefix' variable in the top-level `Makefile' and the `--exec-prefix' argument to the `configure' script at build time. The value is available to Python code as `sys.exec_prefix'. It is only useful on UNIX. Background: The exec-prefix differs from the prefix when platform dependent files (such as executables and shared libraries) are installed in a different directory tree. In a typical installation, platform dependent files may be installed in the `/usr/local/plat' subtree while platform independent may be installed in `/usr/local'. Generally speaking, a platform is a combination of hardware and software families, e.g. Sparc machines running the Solaris 2.x operating system are considered the same platform, but Intel machines running Solaris 2.x are another platform, and Intel machines running Linux are yet another platform. Different major revisions of the same operating system generally also form different platforms. Non-UNIX operating systems are a different story; the installation strategies on those systems are so different that the prefix and exec-prefix are meaningless, and set to the empty string. Note that compiled Python bytecode files are platform independent (but not independent from the Python version by which they were compiled!). System administrators will know how to configure the `mount' or `automount' programs to share `/usr/local' between platforms while having `/usr/local/plat' be a different filesystem for each platform. `char* Py_GetProgramFullPath()' Return the full program name of the Python executable; this is computed as a side-effect of deriving the default module search path from the program name (set by `Py_SetProgramName()' above). The returned string points into static storage; the caller should not modify its value. The value is available to Python code as `sys.executable'. `char* Py_GetPath()' Return the default module search path; this is computed from the program name (set by `Py_SetProgramName()' above) and some environment variables. The returned string consists of a series of directory names separated by a platform dependent delimiter character. The delimiter character is `:' on UNIX, `;' on DOS/Windows, and `\n' (the ASCII newline character) on Macintosh. The returned string points into static storage; the caller should not modify its value. The value is available to Python code as the list `sys.path', which may be modified to change the future search path for loaded modules. `const char* Py_GetVersion()' Return the version of this Python interpreter. This is a string that looks something like "1.5 (#67, Dec 31 1997, 22:34:28) [GCC 2.7.2.2]" The first word (up to the first space character) is the current Python version; the first three characters are the major and minor version separated by a period. The returned string points into static storage; the caller should not modify its value. The value is available to Python code as the list `sys.version'. `const char* Py_GetPlatform()' Return the platform identifier for the current platform. On UNIX, this is formed from the "official" name of the operating system, converted to lower case, followed by the major revision number; e.g., for Solaris 2.x, which is also known as SunOS 5.x, the value is `'sunos5''. On Macintosh, it is `'mac''. On Windows, it is `'win''. The returned string points into static storage; the caller should not modify its value. The value is available to Python code as `sys.platform'. `const char* Py_GetCopyright()' Return the official copyright string for the current Python version, for example `'Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam'' The returned string points into static storage; the caller should not modify its value. The value is available to Python code as the list `sys.copyright'. `const char* Py_GetCompiler()' Return an indication of the compiler used to build the current Python version, in square brackets, for example: "[GCC 2.7.2.2]" The returned string points into static storage; the caller should not modify its value. The value is available to Python code as part of the variable `sys.version'. `const char* Py_GetBuildInfo()' Return information about the sequence number and build date and time of the current Python interpreter instance, for example "#67, Aug 1 1997, 22:34:28" The returned string points into static storage; the caller should not modify its value. The value is available to Python code as part of the variable `sys.version'. `int PySys_SetArgv(int argc, char **argv)' Set `sys.argv' based on ARGC and ARGV. These parameters are similar to those passed to the program's `main()' function with the difference that the first entry should refer to the script file to be executed rather than the executable hosting the Python interpreter. If there isn't a script that will be run, the first entry in ARGV can be an empty string. If this function fails to initialize `sys.argv', a fatal condition is signalled using `Py_FatalError()'. * Menu: * Thread State and the Global Interpreter Lock::  File: python-api.info, Node: Thread State and the Global Interpreter Lock, Prev: Initialization, Up: Initialization Thread State and the Global Interpreter Lock ============================================ The Python interpreter is not fully thread safe. In order to support multi-threaded Python programs, there's a global lock that must be held by the current thread before it can safely access Python objects. Without the lock, even the simplest operations could cause problems in a multi-threaded program: for example, when two threads simultaneously increment the reference count of the same object, the reference count could end up being incremented only once instead of twice. Therefore, the rule exists that only the thread that has acquired the global interpreter lock may operate on Python objects or call Python/C API functions. In order to support multi-threaded Python programs, the interpreter regularly releases and reacquires the lock -- by default, every ten bytecode instructions (this can be changed with `sys.setcheckinterval()'). The lock is also released and reacquired around potentially blocking I/O operations like reading or writing a file, so that other threads can run while the thread that requests the I/O is waiting for the I/O operation to complete. The Python interpreter needs to keep some bookkeeping information separate per thread -- for this it uses a data structure called `PyThreadState'. This is new in Python 1.5; in earlier versions, such state was stored in global variables, and switching threads could cause problems. In particular, exception handling is now thread safe, when the application uses `sys.exc_info()' to access the exception last raised in the current thread. There's one global variable left, however: the pointer to the current `PyThreadState' structure. While most thread packages have a way to store "per-thread global data," Python's internal platform independent thread abstraction doesn't support this yet. Therefore, the current thread state must be manipulated explicitly. This is easy enough in most cases. Most code manipulating the global interpreter lock has the following simple structure: Save the thread state in a local variable. Release the interpreter lock. ...Do some blocking I/O operation... Reacquire the interpreter lock. Restore the thread state from the local variable. This is so common that a pair of macros exists to simplify it: Py_BEGIN_ALLOW_THREADS ...Do some blocking I/O operation... Py_END_ALLOW_THREADS The `Py_BEGIN_ALLOW_THREADS' macro opens a new block and declares a hidden local variable; the `Py_END_ALLOW_THREADS' macro closes the block. Another advantage of using these two macros is that when Python is compiled without thread support, they are defined empty, thus saving the thread state and lock manipulations. When thread support is enabled, the block above expands to the following code: PyThreadState *_save; _save = PyEval_SaveThread(); ...Do some blocking I/O operation... PyEval_RestoreThread(_save); Using even lower level primitives, we can get roughly the same effect as follows: PyThreadState *_save; _save = PyThreadState_Swap(NULL); PyEval_ReleaseLock(); ...Do some blocking I/O operation... PyEval_AcquireLock(); PyThreadState_Swap(_save); There are some subtle differences; in particular, `PyEval_RestoreThread()' saves and restores the value of the global variable `errno', since the lock manipulation does not guarantee that `errno' is left alone. Also, when thread support is disabled, `PyEval_SaveThread()' and `PyEval_RestoreThread()' don't manipulate the lock; in this case, `PyEval_ReleaseLock()' and `PyEval_AcquireLock()' are not available. This is done so that dynamically loaded extensions compiled with thread support enabled can be loaded by an interpreter that was compiled with disabled thread support. The global interpreter lock is used to protect the pointer to the current thread state. When releasing the lock and saving the thread state, the current thread state pointer must be retrieved before the lock is released (since another thread could immediately acquire the lock and store its own thread state in the global variable). Conversely, when acquiring the lock and restoring the thread state, the lock must be acquired before storing the thread state pointer. Why am I going on with so much detail about this? Because when threads are created from C, they don't have the global interpreter lock, nor is there a thread state data structure for them. Such threads must bootstrap themselves into existence, by first creating a thread state data structure, then acquiring the lock, and finally storing their thread state pointer, before they can start using the Python/C API. When they are done, they should reset the thread state pointer, release the lock, and finally free their thread state data structure. When creating a thread data structure, you need to provide an interpreter state data structure. The interpreter state data structure hold global data that is shared by all threads in an interpreter, for example the module administration (`sys.modules'). Depending on your needs, you can either create a new interpreter state data structure, or share the interpreter state data structure used by the Python main thread (to access the latter, you must obtain the thread state and access its `interp' member; this must be done by a thread that is created by Python or by the main thread after Python is initialized). `PyInterpreterState' This data structure represents the state shared by a number of cooperating threads. Threads belonging to the same interpreter share their module administration and a few other internal items. There are no public members in this structure. Threads belonging to different interpreters initially share nothing, except process state like available memory, open file descriptors and such. The global interpreter lock is also shared by all threads, regardless of to which interpreter they belong. `PyThreadState' This data structure represents the state of a single thread. The only public data member is `PyInterpreterState *'`interp', which points to this thread's interpreter state. `void PyEval_InitThreads()' Initialize and acquire the global interpreter lock. It should be called in the main thread before creating a second thread or engaging in any other thread operations such as `PyEval_ReleaseLock()' or `PyEval_ReleaseThread(TSTATE)'. It is not needed before calling `PyEval_SaveThread()' or `PyEval_RestoreThread()'. This is a no-op when called for a second time. It is safe to call this function before calling `Py_Initialize()'. When only the main thread exists, no lock operations are needed. This is a common situation (most Python programs do not use threads), and the lock operations slow the interpreter down a bit. Therefore, the lock is not created initially. This situation is equivalent to having acquired the lock: when there is only a single thread, all object accesses are safe. Therefore, when this function initializes the lock, it also acquires it. Before the Python `thread' module creates a new thread, knowing that either it has the lock or the lock hasn't been created yet, it calls `PyEval_InitThreads()'. When this call returns, it is guaranteed that the lock has been created and that it has acquired it. It is *not* safe to call this function when it is unknown which thread (if any) currently has the global interpreter lock. This function is not available when thread support is disabled at compile time. `void PyEval_AcquireLock()' Acquire the global interpreter lock. The lock must have been created earlier. If this thread already has the lock, a deadlock ensues. This function is not available when thread support is disabled at compile time. `void PyEval_ReleaseLock()' Release the global interpreter lock. The lock must have been created earlier. This function is not available when thread support is disabled at compile time. `void PyEval_AcquireThread(PyThreadState *tstate)' Acquire the global interpreter lock and then set the current thread state to TSTATE, which should not be `NULL'. The lock must have been created earlier. If this thread already has the lock, deadlock ensues. This function is not available when thread support is disabled at compile time. `void PyEval_ReleaseThread(PyThreadState *tstate)' Reset the current thread state to `NULL' and release the global interpreter lock. The lock must have been created earlier and must be held by the current thread. The TSTATE argument, which must not be `NULL', is only used to check that it represents the current thread state -- if it isn't, a fatal error is reported. This function is not available when thread support is disabled at compile time. `PyThreadState* PyEval_SaveThread()' Release the interpreter lock (if it has been created and thread support is enabled) and reset the thread state to `NULL', returning the previous thread state (which is not `NULL'). If the lock has been created, the current thread must have acquired it. (This function is available even when thread support is disabled at compile time.) `void PyEval_RestoreThread(PyThreadState *tstate)' Acquire the interpreter lock (if it has been created and thread support is enabled) and set the thread state to TSTATE, which must not be `NULL'. If the lock has been created, the current thread must not have acquired it, otherwise deadlock ensues. (This function is available even when thread support is disabled at compile time.) The following macros are normally used without a trailing semicolon; look for example usage in the Python source distribution. `Py_BEGIN_ALLOW_THREADS' This macro expands to `{ PyThreadState *_save; _save = PyEval_SaveThread();'. Note that it contains an opening brace; it must be matched with a following `Py_END_ALLOW_THREADS' macro. See above for further discussion of this macro. It is a no-op when thread support is disabled at compile time. `Py_END_ALLOW_THREADS' This macro expands to `PyEval_RestoreThread(_save); }'. Note that it contains a closing brace; it must be matched with an earlier `Py_BEGIN_ALLOW_THREADS' macro. See above for further discussion of this macro. It is a no-op when thread support is disabled at compile time. `Py_BEGIN_BLOCK_THREADS' This macro expands to `PyEval_RestoreThread(_save);' i.e. it is equivalent to `Py_END_ALLOW_THREADS' without the closing brace. It is a no-op when thread support is disabled at compile time. `Py_BEGIN_UNBLOCK_THREADS' This macro expands to `_save = PyEval_SaveThread();' i.e. it is equivalent to `Py_BEGIN_ALLOW_THREADS' without the opening brace and variable declaration. It is a no-op when thread support is disabled at compile time. All of the following functions are only available when thread support is enabled at compile time, and must be called only when the interpreter lock has been created. `PyInterpreterState* PyInterpreterState_New()' Create a new interpreter state object. The interpreter lock need not be held, but may be held if it is necessary to serialize calls to this function. `void PyInterpreterState_Clear(PyInterpreterState *interp)' Reset all information in an interpreter state object. The interpreter lock must be held. `void PyInterpreterState_Delete(PyInterpreterState *interp)' Destroy an interpreter state object. The interpreter lock need not be held. The interpreter state must have been reset with a previous call to `PyInterpreterState_Clear()'. `PyThreadState* PyThreadState_New(PyInterpreterState *interp)' Create a new thread state object belonging to the given interpreter object. The interpreter lock need not be held, but may be held if it is necessary to serialize calls to this function. `void PyThreadState_Clear(PyThreadState *tstate)' Reset all information in a thread state object. The interpreter lock must be held. `void PyThreadState_Delete(PyThreadState *tstate)' Destroy a thread state object. The interpreter lock need not be held. The thread state must have been reset with a previous call to `PyThreadState_Clear()'. `PyThreadState* PyThreadState_Get()' Return the current thread state. The interpreter lock must be held. When the current thread state is `NULL', this issues a fatal error (so that the caller needn't check for `NULL'). `PyThreadState* PyThreadState_Swap(PyThreadState *tstate)' Swap the current thread state with the thread state given by the argument TSTATE, which may be `NULL'. The interpreter lock must be held.  File: python-api.info, Node: Memory Management, Next: Defining New Object Types, Prev: Initialization, Up: Top Memory Management ***************** This section was written by Vladimir Marangozov . * Menu: * Overview:: * Memory Interface:: * Examples::  File: python-api.info, Node: Overview, Next: Memory Interface, Prev: Memory Management, Up: Memory Management Overview ======== Memory management in Python involves a private heap containing all Python objects and data structures. The management of this private heap is ensured internally by the _Python memory manager_. The Python memory manager has different components which deal with various dynamic storage management aspects, like sharing, segmentation, preallocation or caching. At the lowest level, a raw memory allocator ensures that there is enough room in the private heap for storing all Python-related data by interacting with the memory manager of the operating system. On top of the raw memory allocator, several object-specific allocators operate on the same heap and implement distinct memory management policies adapted to the peculiarities of every object type. For example, integer objects are managed differently within the heap than strings, tuples or dictionaries because integers imply different storage requirements and speed/space tradeoffs. The Python memory manager thus delegates some of the work to the object-specific allocators, but ensures that the latter operate within the bounds of the private heap. It is important to understand that the management of the Python heap is performed by the interpreter itself and that the user has no control on it, even if she regularly manipulates object pointers to memory blocks inside that heap. The allocation of heap space for Python objects and other internal buffers is performed on demand by the Python memory manager through the Python/C API functions listed in this document. To avoid memory corruption, extension writers should never try to operate on Python objects with the functions exported by the C library: `malloc()', `calloc()', `realloc()' and `free()'. This will result in mixed calls between the C allocator and the Python memory manager with fatal consequences, because they implement different algorithms and operate on different heaps. However, one may safely allocate and release memory blocks with the C library allocator for individual purposes, as shown in the following example: PyObject *res; char *buf = (char *) malloc(BUFSIZ); /* for I/O */ if (buf == NULL) return PyErr_NoMemory(); ...Do some I/O operation involving buf... res = PyString_FromString(buf); free(buf); /* malloc'ed */ return res; In this example, the memory request for the I/O buffer is handled by the C library allocator. The Python memory manager is involved only in the allocation of the string object returned as a result. In most situations, however, it is recommended to allocate memory from the Python heap specifically because the latter is under control of the Python memory manager. For example, this is required when the interpreter is extended with new object types written in C. Another reason for using the Python heap is the desire to _inform_ the Python memory manager about the memory needs of the extension module. Even when the requested memory is used exclusively for internal, highly-specific purposes, delegating all memory requests to the Python memory manager causes the interpreter to have a more accurate image of its memory footprint as a whole. Consequently, under certain circumstances, the Python memory manager may or may not trigger appropriate actions, like garbage collection, memory compaction or other preventive procedures. Note that by using the C library allocator as shown in the previous example, the allocated memory for the I/O buffer escapes completely the Python memory manager.  File: python-api.info, Node: Memory Interface, Next: Examples, Prev: Overview, Up: Memory Management Memory Interface ================ The following function sets, modeled after the ANSI C standard, are available for allocating and releasing memory from the Python heap: `void* PyMem_Malloc(size_t n)' Allocates N bytes and returns a pointer of type `void*' to the allocated memory, or `NULL' if the request fails. Requesting zero bytes returns a non-`NULL' pointer. `void* PyMem_Realloc(void *p, size_t n)' Resizes the memory block pointed to by P to N bytes. The contents will be unchanged to the minimum of the old and the new sizes. If P is `NULL', the call is equivalent to `PyMem_Malloc(N)'; if N is equal to zero, the memory block is resized but is not freed, and the returned pointer is non-`NULL'. Unless P is `NULL', it must have been returned by a previous call to `PyMem_Malloc()' or `PyMem_Realloc()'. `void PyMem_Free(void *p)' Frees the memory block pointed to by P, which must have been returned by a previous call to `PyMem_Malloc()' or `PyMem_Realloc()'. Otherwise, or if `PyMem_Free(p)' has been called before, undefined behaviour occurs. If P is `NULL', no operation is performed. The following type-oriented macros are provided for convenience. Note that TYPE refers to any C type. `TYPE* PyMem_New(TYPE, size_t n)' Same as `PyMem_Malloc()', but allocates `(N * sizeof(TYPE))' bytes of memory. Returns a pointer cast to `TYPE*'. `TYPE* PyMem_Resize(void *p, TYPE, size_t n)' Same as `PyMem_Realloc()', but the memory block is resized to `(N * sizeof(TYPE))' bytes. Returns a pointer cast to `TYPE*'. `void PyMem_Del(void *p)' Same as `PyMem_Free()'. In addition, the following macro sets are provided for calling the Python memory allocator directly, without involving the C API functions listed above. However, note that their use does not preserve binary compatibility accross Python versions and is therefore deprecated in extension modules. `PyMem_MALLOC()', `PyMem_REALLOC()', `PyMem_FREE()'. `PyMem_NEW()', `PyMem_RESIZE()', `PyMem_DEL()'.  File: python-api.info, Node: Examples, Prev: Memory Interface, Up: Memory Management Examples ======== Here is the example from section *Note Overview::, rewritten so that the I/O buffer is allocated from the Python heap by using the first function set: PyObject *res; char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */ if (buf == NULL) return PyErr_NoMemory(); /* ...Do some I/O operation involving buf... */ res = PyString_FromString(buf); PyMem_Free(buf); /* allocated with PyMem_Malloc */ return res; The same code using the type-oriented function set: PyObject *res; char *buf = PyMem_New(char, BUFSIZ); /* for I/O */ if (buf == NULL) return PyErr_NoMemory(); /* ...Do some I/O operation involving buf... */ res = PyString_FromString(buf); PyMem_Del(buf); /* allocated with PyMem_New */ return res; Note that in the two examples above, the buffer is always manipulated via functions belonging to the same set. Indeed, it is required to use the same memory API family for a given memory block, so that the risk of mixing different allocators is reduced to a minimum. The following code sequence contains two errors, one of which is labeled as _fatal_ because it mixes two different allocators operating on different heaps. char *buf1 = PyMem_New(char, BUFSIZ); char *buf2 = (char *) malloc(BUFSIZ); char *buf3 = (char *) PyMem_Malloc(BUFSIZ); ... PyMem_Del(buf3); /* Wrong -- should be PyMem_Free() */ free(buf2); /* Right -- allocated via malloc() */ free(buf1); /* Fatal -- should be PyMem_Del() */ In addition to the functions aimed at handling raw memory blocks from the Python heap, objects in Python are allocated and released with `PyObject_New()', `PyObject_NewVar()' and `PyObject_Del()', or with their corresponding macros `PyObject_NEW()', `PyObject_NEW_VAR()' and `PyObject_DEL()'. These will be explained in the next chapter on defining and implementing new object types in C.  File: python-api.info, Node: Defining New Object Types, Next: Reporting Bugs, Prev: Memory Management, Up: Top Defining New Object Types ************************* `PyObject* _PyObject_New(PyTypeObject *type)' `PyVarObject* _PyObject_NewVar(PyTypeObject *type, int size)' `void _PyObject_Del(PyObject *op)' `PyObject* PyObject_Init(PyObject *op, PyTypeObject *type)' `PyVarObject* PyObject_InitVar(PyVarObject *op, PyTypeObject *type, int size)' `TYPE* PyObject_New(TYPE, PyTypeObject *type)' `TYPE* PyObject_NewVar(TYPE, PyTypeObject *type, int size)' `void PyObject_Del(PyObject *op)' `TYPE* PyObject_NEW(TYPE, PyTypeObject *type)' `TYPE* PyObject_NEW_VAR(TYPE, PyTypeObject *type, int size)' `void PyObject_DEL(PyObject *op)' `PyObject* Py_InitModule(char *name, PyMethodDef *methods)' Create a new module object based on a name and table of functions, returning the new module object. `PyObject* Py_InitModule3(char *name, PyMethodDef *methods, char *doc)' Create a new module object based on a name and table of functions, returning the new module object. If DOC is non-`NULL', it will be used to define the docstring for the module. `PyObject* Py_InitModule4(char *name, PyMethodDef *methods, char *doc, PyObject *self, int apiver)' Create a new module object based on a name and table of functions, returning the new module object. If DOC is non-`NULL', it will be used to define the docstring for the module. If SELF is non-`NULL', it will passed to the functions of the module as their (otherwise `NULL') first parameter. (This was added as an experimental feature, and there are no known uses in the current version of Python.) For APIVER, the only value which should be passed is defined by the constant `PYTHON_API_VERSION'. *Note:* Most uses of this function should probably be using the `Py_InitModule3()' instead; only use this if you are sure you need it. PyArg_ParseTupleAndKeywords, PyArg_ParseTuple, PyArg_Parse Py_BuildValue DL_IMPORT _Py_NoneStruct * Menu: * Common Object Structures:: * Mapping Object Structures:: * Number Object Structures:: * Sequence Object Structures:: * Buffer Object Structures:: * Supporting Cyclic Garbarge Collection::  File: python-api.info, Node: Common Object Structures, Next: Mapping Object Structures, Prev: Defining New Object Types, Up: Defining New Object Types Common Object Structures ======================== PyObject, PyVarObject PyObject_HEAD, PyObject_HEAD_INIT, PyObject_VAR_HEAD Typedefs: unaryfunc, binaryfunc, ternaryfunc, inquiry, coercion, intargfunc, intintargfunc, intobjargproc, intintobjargproc, objobjargproc, destructor, printfunc, getattrfunc, getattrofunc, setattrfunc, setattrofunc, cmpfunc, reprfunc, hashfunc `PyCFunction' Type of the functions used to implement most Python callables in C. `PyMethodDef' Structure used to describe a method of an extension type. This structure has four fields: Field C Type Meaning ------ ----- ----- ml_name char * name of the method ml_meth PyCFunction pointer to the C implementation ml_flags int flag bits indicating how the call should be constructed ml_doc char * points to the contents of the docstring `PyObject* Py_FindMethod(PyMethodDef[] table, PyObject *ob, char *name)' Return a bound method object for an extension type implemented in C. This function also handles the special attribute `__methods__', returning a list of all the method names defined in TABLE.  File: python-api.info, Node: Mapping Object Structures, Next: Number Object Structures, Prev: Common Object Structures, Up: Defining New Object Types Mapping Object Structures ========================= `PyMappingMethods' Structure used to hold pointers to the functions used to implement the mapping protocol for an extension type.  File: python-api.info, Node: Number Object Structures, Next: Sequence Object Structures, Prev: Mapping Object Structures, Up: Defining New Object Types Number Object Structures ======================== `PyNumberMethods' Structure used to hold pointers to the functions an extension type uses to implement the number protocol.  File: python-api.info, Node: Sequence Object Structures, Next: Buffer Object Structures, Prev: Number Object Structures, Up: Defining New Object Types Sequence Object Structures ========================== `PySequenceMethods' Structure used to hold pointers to the functions which an object uses to implement the sequence protocol.  File: python-api.info, Node: Buffer Object Structures, Next: Supporting Cyclic Garbarge Collection, Prev: Sequence Object Structures, Up: Defining New Object Types Buffer Object Structures ======================== This section was written by Greg J. Stein . The buffer interface exports a model where an object can expose its internal data as a set of chunks of data, where each chunk is specified as a pointer/length pair. These chunks are called "segments" and are presumed to be non-contiguous in memory. If an object does not export the buffer interface, then its `tp_as_buffer' member in the `PyTypeObject' structure should be `NULL'. Otherwise, the `tp_as_buffer' will point to a `PyBufferProcs' structure. *Note:* It is very important that your `PyTypeObject' structure uses `Py_TPFLAGS_DEFAULT' for the value of the `tp_flags' member rather than `0'. This tells the Python runtime that your `PyBufferProcs' structure contains the `bf_getcharbuffer' slot. Older versions of Python did not have this member, so a new Python interpreter using an old extension needs to be able to test for its presence before using it. `PyBufferProcs' Structure used to hold the function pointers which define an implementation of the buffer protocol. The first slot is `bf_getreadbuffer', of type `getreadbufferproc'. If this slot is `NULL', then the object does not support reading from the internal data. This is non-sensical, so implementors should fill this in, but callers should test that the slot contains a non-`NULL' value. The next slot is `bf_getwritebuffer' having type `getwritebufferproc'. This slot may be `NULL' if the object does not allow writing into its returned buffers. The third slot is `bf_getsegcount', with type `getsegcountproc'. This slot must not be `NULL' and is used to inform the caller how many segments the object contains. Simple objects such as `PyString_Type' and `PyBuffer_Type' objects contain a single segment. The last slot is `bf_getcharbuffer', of type `getcharbufferproc'. This slot will only be present if the `Py_TPFLAGS_HAVE_GETCHARBUFFER' flag is present in the `tp_flags' field of the object's `PyTypeObject'. Before using this slot, the caller should test whether it is present by using the `PyType_HasFeature()' function. If present, it may be `NULL', indicating that the object's contents cannot be used as _8-bit characters_. The slot function may also raise an error if the object's contents cannot be interpreted as 8-bit characters. For example, if the object is an array which is configured to hold floating point values, an exception may be raised if a caller attempts to use `bf_getcharbuffer' to fetch a sequence of 8-bit characters. This notion of exporting the internal buffers as "text" is used to distinguish between objects that are binary in nature, and those which have character-based content. *Note:* The current policy seems to state that these characters may be multi-byte characters. This implies that a buffer size of N does not mean there are N characters present. `Py_TPFLAGS_HAVE_GETCHARBUFFER' Flag bit set in the type structure to indicate that the `bf_getcharbuffer' slot is known. This being set does not indicate that the object supports the buffer interface or that the `bf_getcharbuffer' slot is non-`NULL'. `int (*getreadbufferproc) (PyObject *self, int segment, void **ptrptr)' Return a pointer to a readable segment of the buffer. This function is allowed to raise an exception, in which case it must return `-1'. The SEGMENT which is passed must be zero or positive, and strictly less than the number of segments returned by the `bf_getsegcount' slot function. On success, it returns the length of the buffer memory, and sets `*PTRPTR' to a pointer to that memory. `int (*getwritebufferproc) (PyObject *self, int segment, void **ptrptr)' Return a pointer to a writable memory buffer in `*PTRPTR', and the length of that segment as the function return value. The memory buffer must correspond to buffer segment SEGMENT. Must return `-1' and set an exception on error. `TypeError' should be raised if the object only supports read-only buffers, and `SystemError' should be raised when SEGMENT specifies a segment that doesn't exist. `int (*getsegcountproc) (PyObject *self, int *lenp)' Return the number of memory segments which comprise the buffer. If LENP is not `NULL', the implementation must report the sum of the sizes (in bytes) of all segments in `*LENP'. The function cannot fail. `int (*getcharbufferproc) (PyObject *self, int segment, const char **ptrptr)'