C Api
C Api
Release 2.7.15
1 Introduction 3
1.1 Include Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 Objects, Types and Reference Counts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.3 Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.4 Embedding Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.5 Debugging Builds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3 Reference Counting 15
4 Exception Handling 17
4.1 Unicode Exception Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
4.2 Recursion Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
4.3 Standard Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
4.4 Standard Warning Categories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
4.5 String Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
5 Utilities 25
5.1 Operating System Utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
5.2 System Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
5.3 Process Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
5.4 Importing Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
5.5 Data marshalling support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
5.6 Parsing arguments and building values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
5.7 String conversion and formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
5.8 Reflection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
5.9 Codec registry and support functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
i
7.4 Mapping Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
7.5 Other Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
A Glossary 155
D Copyright 183
Index 185
ii
The Python/C API, Release 2.7.15
This manual documents the API used by C and C++ programmers who want to write extension modules
or embed Python. It is a companion to extending-index, which describes the general principles of extension
writing but does not document the API functions in detail.
CONTENTS 1
The Python/C API, Release 2.7.15
2 CONTENTS
CHAPTER
ONE
INTRODUCTION
The Application Programmer’s Interface to Python gives C and C++ programmers access to the Python
interpreter at a variety of levels. The API is equally usable from C++, but for brevity it is generally referred
to as the Python/C API. There are two fundamentally different reasons for using the Python/C API. The
first reason is to write extension modules for specific purposes; these are C modules that extend the Python
interpreter. This is probably the most common use. The second reason is to use Python as a component in
a larger application; this technique is generally referred to as embedding Python in an application.
Writing an extension module is a relatively well-understood process, where a “cookbook” approach works
well. There are several tools that automate the process to some extent. While people have embedded Python
in other applications since its early existence, the process of embedding Python is less straightforward than
writing an extension.
Many API functions are useful independent of whether you’re embedding or extending Python; moreover,
most applications that embed Python will need to provide a custom extension as well, so it’s probably a good
idea to become familiar with writing an extension before attempting to embed Python in a real application.
All function, type and macro definitions needed to use the Python/C API are included in your code by the
following line:
#include "Python.h"
This implies inclusion of the following standard headers: <stdio.h>, <string.h>, <errno.h>, <limits.h>,
<assert.h> and <stdlib.h> (if available).
Note: Since Python may define some pre-processor definitions which affect the standard headers on some
systems, you must include Python.h before any standard headers are included.
All user visible names defined by Python.h (except those defined by the included standard headers) have
one of the prefixes Py or _Py. Names beginning with _Py are for internal use by the Python implementation
and should not be used by extension writers. Structure member names do not have a reserved prefix.
Important: user code should never define names that begin with Py or _Py. This confuses the reader, and
jeopardizes the portability of the user code to future Python versions, which may define additional names
beginning with one of these prefixes.
The header files are typically installed with Python. On Unix, these are located in the directories prefix/
include/pythonversion/ and exec_prefix/include/pythonversion/, where prefix and exec_prefix
are defined by the corresponding parameters to Python’s configure script and version is sys.version[:3].
On Windows, the headers are installed in prefix/include, where prefix is the installation directory spec-
ified to the installer.
3
The Python/C API, Release 2.7.15
To include the headers, place both directories (if different) on your compiler’s search path for includes. Do
not place the parent directories on the search path and then use #include <pythonX.Y/Python.h>; this will
break on multi-platform builds since the platform independent headers under prefix include the platform
specific headers from exec_prefix.
C++ users should note that though the API is defined entirely using C, the header files do properly declare
the entry points to be extern "C", so there is no need to do anything special to use the API from C++.
Most Python/C API functions have one or more arguments as well as a return value of type PyObject*.
This type is a pointer to an opaque data type representing an arbitrary Python object. Since all Python
object types are treated the same way by the Python language in most situations (e.g., assignments, scope
rules, and argument passing), it is only fitting that they should be represented by a single C type. Almost
all Python objects live on the heap: you never declare an automatic or static variable of type PyObject, only
pointer variables of type PyObject* can be declared. The sole exception are the type objects; since these
must never be deallocated, they are typically static PyTypeObject objects.
All Python objects (even Python integers) have a type and a reference count. An object’s type determines
what kind of object it is (e.g., an integer, a list, or a user-defined function; there are many more as explained
in types). For each of the well-known types there is a macro to check whether an object is of that type; for
instance, PyList_Check(a) is true if (and only if) the object pointed to by a is a Python list.
4 Chapter 1. Introduction
The Python/C API, Release 2.7.15
However, a common pitfall is to extract an object from a list and hold on to it for a while without incrementing
its reference count. Some other operation might conceivably remove the object from the list, decrementing
its reference count and possible deallocating it. The real danger is that innocent-looking operations may
invoke arbitrary Python code which could do this; there is a code path which allows control to flow back to
the user from a Py_DECREF(), so almost any operation is potentially dangerous.
A safe approach is to always use the generic operations (functions whose name begins with PyObject_,
PyNumber_, PySequence_ or PyMapping_). These operations always increment the reference count of the
object they return. This leaves the caller with the responsibility to call Py_DECREF() when they are done
with the result; this soon becomes second nature.
The reference count behavior of functions in the Python/C API is best explained in terms of ownership
of references. Ownership pertains to references, never to objects (objects are not owned: they are always
shared). “Owning a reference” means being responsible for calling Py_DECREF on it when the reference
is no longer needed. Ownership can also be transferred, meaning that the code that receives ownership of
the reference then becomes responsible for eventually decref’ing it by calling Py_DECREF() or Py_XDECREF()
when it’s no longer needed—or passing on this responsibility (usually to its caller). When a function passes
ownership of a reference on to its caller, the caller is said to receive a new reference. When no ownership is
transferred, the caller is said to borrow the reference. Nothing needs to be done for a borrowed reference.
Conversely, when a calling function passes in a reference to an object, there are two possibilities: the function
steals a reference to the object, or it does not. Stealing a reference means that when you pass a reference
to a function, that function assumes that it now owns that reference, and you are not responsible for it any
longer.
Few functions steal references; the two notable exceptions are PyList_SetItem() and PyTuple_SetItem(),
which steal a reference to the item (but not to the tuple or list into which the item is put!). These functions
were designed to steal a reference because of a common idiom for populating a tuple or list with newly
created objects; for example, the code to create the tuple (1, 2, "three") could look like this (forgetting
about error handling for the moment; a better way to code this is shown below):
PyObject *t;
t = PyTuple_New(3);
PyTuple_SetItem(t, 0, PyInt_FromLong(1L));
PyTuple_SetItem(t, 1, PyInt_FromLong(2L));
PyTuple_SetItem(t, 2, PyString_FromString("three"));
Here, PyInt_FromLong() returns a new reference which is immediately stolen by PyTuple_SetItem(). When
you want to keep using an object although the reference to it will be stolen, use Py_INCREF() to grab another
reference before calling the reference-stealing function.
Incidentally, PyTuple_SetItem() is the only way to set tuple items; PySequence_SetItem() and
PyObject_SetItem() refuse to do this since tuples are an immutable data type. You should only use
PyTuple_SetItem() for tuples that you are creating yourself.
Equivalent code for populating a list can be written using PyList_New() and PyList_SetItem().
However, in practice, you will rarely use these ways of creating and populating a tuple or list. There’s
a generic function, Py_BuildValue(), that can create most common objects from C values, directed by a
format string. For example, the above two blocks of code could be replaced by the following (which also
takes care of the error checking):
It is much more common to use PyObject_SetItem() and friends with items whose references you are only
borrowing, like arguments that were passed in to the function you are writing. In that case, their behaviour
regarding reference counts is much saner, since you don’t have to increment a reference count so you can
give a reference away (“have it be stolen”). For example, this function sets all items of a list (actually, any
mutable sequence) to a given item:
int
set_all(PyObject *target, PyObject *item)
{
int i, n;
n = PyObject_Length(target);
if (n < 0)
return -1;
for (i = 0; i < n; i++) {
PyObject *index = PyInt_FromLong(i);
if (!index)
return -1;
if (PyObject_SetItem(target, index, item) < 0) {
Py_DECREF(index);
return -1;
}
Py_DECREF(index);
}
return 0;
}
The situation is slightly different for function return values. While passing a reference to most functions does
not change your ownership responsibilities for that reference, many functions that return a reference to an
object give you ownership of the reference. The reason is simple: in many cases, the returned object is created
on the fly, and the reference you get is the only reference to the object. Therefore, the generic functions
that return object references, like PyObject_GetItem() and PySequence_GetItem(), always return a new
reference (the caller becomes the owner of the reference).
It is important to realize that whether you own a reference returned by a function depends on which function
you call only — the plumage (the type of the object passed as an argument to the function) doesn’t enter
into it! Thus, if you extract an item from a list using PyList_GetItem(), you don’t own the reference —
but if you obtain the same item from the same list using PySequence_GetItem() (which happens to take
exactly the same arguments), you do own a reference to the returned object.
Here is an example of how you could write a function that computes the sum of the items in a list of integers;
once using PyList_GetItem(), and once using PySequence_GetItem().
long
sum_list(PyObject *list)
{
int i, n;
long total = 0;
PyObject *item;
n = PyList_Size(list);
if (n < 0)
return -1; /* Not a list */
(continues on next page)
6 Chapter 1. Introduction
The Python/C API, Release 2.7.15
long
sum_sequence(PyObject *sequence)
{
int i, n;
long total = 0;
PyObject *item;
n = PySequence_Length(sequence);
if (n < 0)
return -1; /* Has no length */
for (i = 0; i < n; i++) {
item = PySequence_GetItem(sequence, i);
if (item == NULL)
return -1; /* Not a sequence, or other failure */
if (PyInt_Check(item))
total += PyInt_AsLong(item);
Py_DECREF(item); /* Discard reference ownership */
}
return total;
}
1.2.2 Types
There are few other data types that play a significant role in the Python/C API; most are simple C types
such as int, long, double and char*. A few structure types are used to describe static tables used to list
the functions exported by a module or the data attributes of a new object type, and another is used to
describe the value of a complex number. These will be discussed together with the functions that use them.
1.3 Exceptions
The Python programmer only needs to deal with exceptions if specific error handling is required; unhandled
exceptions are automatically propagated to the caller, then to the caller’s caller, and so on, until they reach
the top-level interpreter, where they are reported to the user accompanied by a stack traceback.
For C programmers, however, error checking always has to be explicit. All functions in the Python/C API
can raise exceptions, unless an explicit claim is made otherwise in a function’s documentation. In general,
when a function encounters an error, it sets an exception, discards any object references that it owns, and
returns an error indicator. If not documented otherwise, this indicator is either NULL or -1, depending on
the function’s return type. A few functions return a Boolean true/false result, with false indicating an error.
Very few functions return no explicit error indicator or have an ambiguous return value, and require explicit
testing for errors with PyErr_Occurred(). These exceptions are always explicitly documented.
Exception state is maintained in per-thread storage (this is equivalent to using global storage in an unthreaded
application). A thread can be in one of two states: an exception has occurred, or not. The function
PyErr_Occurred() can be used to check for this: it returns a borrowed reference to the exception type
object when an exception has occurred, and NULL otherwise. There are a number of functions to set the
1.3. Exceptions 7
The Python/C API, Release 2.7.15
exception state: PyErr_SetString() is the most common (though not the most general) function to set the
exception state, and PyErr_Clear() clears the exception state.
The full exception state consists of three objects (all of which can be NULL): the exception type, the
corresponding exception value, and the traceback. These have the same meanings as the Python objects
sys.exc_type, sys.exc_value, and sys.exc_traceback; however, they are not the same: the Python
objects represent the last exception being handled by a Python try … except statement, while the C level
exception state only exists while an exception is being passed on between C functions until it reaches the
Python bytecode interpreter’s main loop, which takes care of transferring it to sys.exc_type and friends.
Note that starting with Python 1.5, the preferred, thread-safe way to access the exception state from Python
code is to call the function sys.exc_info(), which returns the per-thread exception state for Python code.
Also, the semantics of both ways to access the exception state have changed so that a function which catches
an exception will save and restore its thread’s exception state so as to preserve the exception state of its
caller. This prevents common bugs in exception handling code caused by an innocent-looking function
overwriting the exception being handled; it also reduces the often unwanted lifetime extension for objects
that are referenced by the stack frames in the traceback.
As a general principle, a function that calls another function to perform some task should check whether the
called function raised an exception, and if so, pass the exception state on to its caller. It should discard any
object references that it owns, and return an error indicator, but it should not set another exception — that
would overwrite the exception that was just raised, and lose important information about the exact cause of
the error.
A simple example of detecting exceptions and passing them on is shown in the sum_sequence() example
above. It so happens that this example doesn’t need to clean up any owned references when it detects an
error. The following example function shows some error cleanup. First, to remind you why you like Python,
we show the equivalent Python code:
int
incr_item(PyObject *dict, PyObject *key)
{
/* Objects all initialized to NULL for Py_XDECREF */
PyObject *item = NULL, *const_one = NULL, *incremented_item = NULL;
int rv = -1; /* Return value initialized to -1 (failure) */
8 Chapter 1. Introduction
The Python/C API, Release 2.7.15
error:
/* Cleanup code, shared by success and failure path */
This example represents an endorsed use of the goto statement in C! It illustrates the use of
PyErr_ExceptionMatches() and PyErr_Clear() to handle specific exceptions, and the use of Py_XDECREF()
to dispose of owned references that may be NULL (note the 'X' in the name; Py_DECREF() would crash
when confronted with a NULL reference). It is important that the variables used to hold owned references
are initialized to NULL for this to work; likewise, the proposed return value is initialized to -1 (failure) and
only set to success after the final call made is successful.
The one important task that only embedders (as opposed to extension writers) of the Python interpreter
have to worry about is the initialization, and possibly the finalization, of the Python interpreter. Most
functionality of the interpreter can only be used after the interpreter has been initialized.
The basic initialization function is Py_Initialize(). This initializes the table of loaded modules, and
creates the fundamental modules __builtin__, __main__, sys, and exceptions. It also initializes the
module search path (sys.path).
Py_Initialize() does not set the “script argument list” (sys.argv). If this variable is needed by Python
code that will be executed later, it must be set explicitly with a call to PySys_SetArgvEx(argc, argv,
updatepath) after the call to Py_Initialize().
On most systems (in particular, on Unix and Windows, although the details are slightly different),
Py_Initialize() calculates the module search path based upon its best guess for the location of the stan-
dard Python interpreter executable, assuming that the Python library is found in a fixed location relative
to the Python interpreter executable. In particular, it looks for a directory named lib/pythonX.Y relative
to the parent directory where the executable named python is found on the shell command search path (the
environment variable PATH).
For instance, if the Python executable is found in /usr/local/bin/python, it will assume that the libraries
are in /usr/local/lib/pythonX.Y . (In fact, this particular path is also the “fallback” location, used when
no executable file named python is found along PATH.) The user can override this behavior by setting the
environment variable PYTHONHOME, or insert additional directories in front of the standard path by setting
PYTHONPATH.
The embedding application can steer the search by calling Py_SetProgramName(file) before calling
Py_Initialize(). Note that PYTHONHOME still overrides this and PYTHONPATH is still inserted in front
of the standard path. An application that requires total control has to provide its own implementation
of Py_GetPath(), Py_GetPrefix(), Py_GetExecPrefix(), and Py_GetProgramFullPath() (all defined in
Modules/getpath.c).
Sometimes, it is desirable to “uninitialize” Python. For instance, the application may want to start over
(make another call to Py_Initialize()) or the application is simply done with its use of Python and wants
to free memory allocated by Python. This can be accomplished by calling Py_Finalize(). The function
Py_IsInitialized() returns true if Python is currently in the initialized state. More information about
these functions is given in a later chapter. Notice that Py_Finalize() does not free all memory allocated
by the Python interpreter, e.g. memory allocated by extension modules currently cannot be released.
10 Chapter 1. Introduction
CHAPTER
TWO
The functions in this chapter will let you execute Python source code given in a file or a buffer, but they
will not let you interact in a more detailed way with the interpreter.
Several of these functions accept a start symbol from the grammar as a parameter. The available start
symbols are Py_eval_input, Py_file_input, and Py_single_input. These are described following the
functions which accept them as parameters.
Note also that several of these functions take FILE* parameters. One particular issue which needs to be
handled carefully is that the FILE structure for different C libraries can be different and incompatible. Under
Windows (at least), it is possible for dynamically linked extensions to actually use different libraries, so care
should be taken that FILE* parameters are only passed to these functions if it is certain that they were
created by the same library that the Python runtime is using.
int Py_Main(int argc, char **argv)
The main program for the standard interpreter. This is made available for programs which embed
Python. The argc and argv parameters should be prepared exactly as those which are passed to a C
program’s main() function. It is important to note that the argument list may be modified (but the
contents of the strings pointed to by the argument list are not). The return value will be 0 if the
interpreter exits normally (ie, without an exception), 1 if the interpreter exits due to an exception, or
2 if the parameter list does not represent a valid Python command line.
Note that if an otherwise unhandled SystemExit is raised, this function will not return 1, but exit the
process, as long as Py_InspectFlag is not set.
int PyRun_AnyFile(FILE *fp, const char *filename)
This is a simplified interface to PyRun_AnyFileExFlags() below, leaving closeit set to 0 and flags set
to NULL.
int PyRun_AnyFileFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
This is a simplified interface to PyRun_AnyFileExFlags() below, leaving the closeit argument set to
0.
int PyRun_AnyFileEx(FILE *fp, const char *filename, int closeit)
This is a simplified interface to PyRun_AnyFileExFlags() below, leaving the flags argument set to
NULL.
int PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit, PyCompilerFlags *flags)
If fp refers to a file associated with an interactive device (console or terminal input or Unix
pseudo-terminal), return the value of PyRun_InteractiveLoop(), otherwise return the result of
PyRun_SimpleFile(). If filename is NULL, this function uses "???" as the filename.
int PyRun_SimpleString(const char *command)
This is a simplified interface to PyRun_SimpleStringFlags() below, leaving the PyCompilerFlags*
argument set to NULL.
int PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
Executes the Python source code from command in the __main__ module according to the flags ar-
11
The Python/C API, Release 2.7.15
gument. If __main__ does not already exist, it is created. Returns 0 on success or -1 if an exception
was raised. If there was an error, there is no way to get the exception information. For the meaning
of flags, see below.
Note that if an otherwise unhandled SystemExit is raised, this function will not return -1, but exit
the process, as long as Py_InspectFlag is not set.
int PyRun_SimpleFile(FILE *fp, const char *filename)
This is a simplified interface to PyRun_SimpleFileExFlags() below, leaving closeit set to 0 and flags
set to NULL.
int PyRun_SimpleFileFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
This is a simplified interface to PyRun_SimpleFileExFlags() below, leaving closeit set to 0.
int PyRun_SimpleFileEx(FILE *fp, const char *filename, int closeit)
This is a simplified interface to PyRun_SimpleFileExFlags() below, leaving flags set to NULL.
int PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit, PyCompilerFlags *flags)
Similar to PyRun_SimpleStringFlags(), but the Python source code is read from fp instead of an
in-memory string. filename should be the name of the file. If closeit is true, the file is closed before
PyRun_SimpleFileExFlags returns.
int PyRun_InteractiveOne(FILE *fp, const char *filename)
This is a simplified interface to PyRun_InteractiveOneFlags() below, leaving flags set to NULL.
int PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Read and execute a single statement from a file associated with an interactive device according to the
flags argument. The user will be prompted using sys.ps1 and sys.ps2. Returns 0 when the input
was executed successfully, -1 if there was an exception, or an error code from the errcode.h include
file distributed as part of Python if there was a parse error. (Note that errcode.h is not included by
Python.h, so must be included specifically if needed.)
int PyRun_InteractiveLoop(FILE *fp, const char *filename)
This is a simplified interface to PyRun_InteractiveLoopFlags() below, leaving flags set to NULL.
int PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Read and execute statements from a file associated with an interactive device until EOF is reached.
The user will be prompted using sys.ps1 and sys.ps2. Returns 0 at EOF.
struct _node* PyParser_SimpleParseString(const char *str, int start)
This is a simplified interface to PyParser_SimpleParseStringFlagsFilename() below, leaving file-
name set to NULL and flags set to 0.
struct _node* PyParser_SimpleParseStringFlags(const char *str, int start, int flags)
This is a simplified interface to PyParser_SimpleParseStringFlagsFilename() below, leaving file-
name set to NULL.
struct _node* PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename,
int start, int flags)
Parse Python source code from str using the start token start according to the flags argument. The
result can be used to create a code object which can be evaluated efficiently. This is useful if a code
fragment must be evaluated many times.
struct _node* PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
This is a simplified interface to PyParser_SimpleParseFileFlags() below, leaving flags set to 0.
struct _node* PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start,
int flags)
Similar to PyParser_SimpleParseStringFlagsFilename(), but the Python source code is read from
fp instead of an in-memory string.
13
The Python/C API, Release 2.7.15
code object associated with the execution frame f is executed, interpreting bytecode and executing
calls as needed. The additional throwflag parameter can mostly be ignored - if true, then it causes an
exception to immediately be thrown; this is used for the throw() methods of generator objects.
int PyEval_MergeCompilerFlags(PyCompilerFlags *cf )
This function changes the flags of the current evaluation frame, and returns true on success, false on
failure.
int Py_eval_input
The start symbol from the Python grammar for isolated expressions; for use with Py_CompileString().
int Py_file_input
The start symbol from the Python grammar for sequences of statements as read from a file or other
source; for use with Py_CompileString(). This is the symbol to use when compiling arbitrarily long
Python source code.
int Py_single_input
The start symbol from the Python grammar for a single statement; for use with Py_CompileString().
This is the symbol used for the interactive interpreter loop.
struct PyCompilerFlags
This is the structure used to hold compiler flags. In cases where code is only being compiled, it is
passed as int flags, and in cases where code is being executed, it is passed as PyCompilerFlags
*flags. In this case, from __future__ import can modify flags.
Whenever PyCompilerFlags *flags is NULL, cf_flags is treated as equal to 0, and any modification
due to from __future__ import is discarded.
struct PyCompilerFlags {
int cf_flags;
}
int CO_FUTURE_DIVISION
This bit can be set in flags to cause division operator / to be interpreted as “true division” according
to PEP 238.
THREE
REFERENCE COUNTING
The macros in this section are used for managing reference counts of Python objects.
void Py_INCREF(PyObject *o)
Increment the reference count for object o. The object must not be NULL; if you aren’t sure that it
isn’t NULL, use Py_XINCREF().
void Py_XINCREF(PyObject *o)
Increment the reference count for object o. The object may be NULL, in which case the macro has no
effect.
void Py_DECREF(PyObject *o)
Decrement the reference count for object o. The object must not be NULL; if you aren’t sure that it
isn’t NULL, use Py_XDECREF(). If the reference count reaches zero, the object’s type’s deallocation
function (which must not be NULL) is invoked.
Warning: The deallocation function can cause arbitrary Python code to be invoked (e.g. when
a class instance with a __del__() method is deallocated). While exceptions in such code are not
propagated, the executed code has free access to all Python global variables. This means that any
object that is reachable from a global variable should be in a consistent state before Py_DECREF()
is invoked. For example, code to delete an object from a list should copy a reference to the deleted
object in a temporary variable, update the list data structure, and then call Py_DECREF() for the
temporary variable.
void Py_XDECREF(PyObject *o)
Decrement the reference count for object o. The object may be NULL, in which case the macro has
no effect; otherwise the effect is the same as for Py_DECREF(), and the same warning applies.
void Py_CLEAR(PyObject *o)
Decrement the reference count for object o. The object may be NULL, in which case the macro has
no effect; otherwise the effect is the same as for Py_DECREF(), except that the argument is also set to
NULL. The warning for Py_DECREF() does not apply with respect to the object passed because the
macro carefully uses a temporary variable and sets the argument to NULL before decrementing its
reference count.
It is a good idea to use this macro whenever decrementing the value of a variable that might be
traversed during garbage collection.
New in version 2.4.
The following functions are for runtime dynamic embedding of Python: Py_IncRef(PyObject *o),
Py_DecRef(PyObject *o). They are simply exported function versions of Py_XINCREF() and Py_XDECREF(),
respectively.
The following functions or macros are only for use within the interpreter core: _Py_Dealloc(),
_Py_ForgetReference(), _Py_NewReference(), as well as the global variable _Py_RefTotal.
15
The Python/C API, Release 2.7.15
FOUR
EXCEPTION HANDLING
The functions described in this chapter will let you handle and raise Python exceptions. It is important
to understand some of the basics of Python exception handling. It works somewhat like the Unix errno
variable: there is a global indicator (per thread) of the last error that occurred. Most functions don’t clear
this on success, but will set it to indicate the cause of the error on failure. Most functions also return an error
indicator, usually NULL if they are supposed to return a pointer, or -1 if they return an integer (exception:
the PyArg_*() functions return 1 for success and 0 for failure).
When a function must fail because some function it called failed, it generally doesn’t set the error indicator;
the function it called already set it. It is responsible for either handling the error and clearing the exception
or returning after cleaning up any resources it holds (such as object references or memory allocations); it
should not continue normally if it is not prepared to handle the error. If returning due to an error, it
is important to indicate to the caller that an error has been set. If the error is not handled or carefully
propagated, additional calls into the Python/C API may not behave as intended and may fail in mysterious
ways.
The error indicator consists of three Python objects corresponding to the Python variables sys.exc_type,
sys.exc_value and sys.exc_traceback. API functions exist to interact with the error indicator in various
ways. There is a separate error indicator for each thread.
void PyErr_PrintEx(int set_sys_last_vars)
Print a standard traceback to sys.stderr and clear the error indicator. Call this function only when
the error indicator is set. (Otherwise it will cause a fatal error!)
If set_sys_last_vars is nonzero, the variables sys.last_type, sys.last_value and sys.
last_traceback will be set to the type, value and traceback of the printed exception, respectively.
void PyErr_Print()
Alias for PyErr_PrintEx(1).
PyObject* PyErr_Occurred()
Return value: Borrowed reference. Test whether the error indicator is set. If set, return the exception
type (the first argument to the last call to one of the PyErr_Set*() functions or to PyErr_Restore()).
If not set, return NULL. You do not own a reference to the return value, so you do not need to
Py_DECREF() it.
Note: Do not compare the return value to a specific exception; use PyErr_ExceptionMatches()
instead, shown below. (The comparison could easily fail since the exception may be an instance
instead of a class, in the case of a class exception, or it may be a subclass of the expected exception.)
int PyErr_ExceptionMatches(PyObject *exc)
Equivalent to PyErr_GivenExceptionMatches(PyErr_Occurred(), exc). This should only be called
when an exception is actually set; a memory access violation will occur if no exception has been raised.
int PyErr_GivenExceptionMatches(PyObject *given, PyObject *exc)
Return true if the given exception matches the exception in exc. If exc is a class object, this also
17
The Python/C API, Release 2.7.15
returns true when given is an instance of a subclass. If exc is a tuple, all exceptions in the tuple (and
recursively in subtuples) are searched for a match.
void PyErr_NormalizeException(PyObject**exc, PyObject**val, PyObject**tb)
Under certain circumstances, the values returned by PyErr_Fetch() below can be “unnormalized”,
meaning that *exc is a class object but *val is not an instance of the same class. This function can
be used to instantiate the class in that case. If the values are already normalized, nothing happens.
The delayed normalization is implemented to improve performance.
void PyErr_Clear()
Clear the error indicator. If the error indicator is not set, there is no effect.
void PyErr_Fetch(PyObject **ptype, PyObject **pvalue, PyObject **ptraceback)
Retrieve the error indicator into three variables whose addresses are passed. If the error indicator is
not set, set all three variables to NULL. If it is set, it will be cleared and you own a reference to each
object retrieved. The value and traceback object may be NULL even when the type object is not.
Note: This function is normally only used by code that needs to handle exceptions or by code that
needs to save and restore the error indicator temporarily.
Note: This function is normally only used by code that needs to save and restore the error indicator
temporarily; use PyErr_Fetch() to save the current exception state.
PyObject* PyErr_SetFromErrno(PyObject *type)
Return value: Always NULL. This is a convenience function to raise an exception when a C library
function has returned an error and set the C variable errno. It constructs a tuple object whose first
item is the integer errno value and whose second item is the corresponding error message (gotten from
strerror()), and then calls PyErr_SetObject(type, object). On Unix, when the errno value is
EINTR, indicating an interrupted system call, this calls PyErr_CheckSignals(), and if that set the
error indicator, leaves it set to that. The function always returns NULL, so a wrapper function around
a system call can write return PyErr_SetFromErrno(type); when the system call returns an error.
PyObject* PyErr_SetFromErrnoWithFilenameObject(PyObject *type, PyObject *filenameObject)
Similar to PyErr_SetFromErrno(), with the additional behavior that if filenameObject is not NULL,
it is passed to the constructor of type as a third parameter. In the case of exceptions such as IOError
and OSError, this is used to define the filename attribute of the exception instance.
PyObject* PyErr_SetFromErrnoWithFilename(PyObject *type, const char *filename)
Return value: Always NULL. Similar to PyErr_SetFromErrnoWithFilenameObject(), but the file-
name is given as a C string.
PyObject* PyErr_SetFromWindowsErr(int ierr)
Return value: Always NULL. This is a convenience function to raise WindowsError. If called
with ierr of 0, the error code returned by a call to GetLastError() is used instead. It calls the
Win32 function FormatMessage() to retrieve the Windows description of error code given by ierr
or GetLastError(), then it constructs a tuple object whose first item is the ierr value and whose
second item is the corresponding error message (gotten from FormatMessage()), and then calls
PyErr_SetObject(PyExc_WindowsError, object). This function always returns NULL. Availabil-
ity: Windows.
PyObject* PyErr_SetExcFromWindowsErr(PyObject *type, int ierr)
Return value: Always NULL. Similar to PyErr_SetFromWindowsErr(), with an additional parameter
specifying the exception type to be raised. Availability: Windows.
New in version 2.3.
PyObject* PyErr_SetFromWindowsErrWithFilenameObject(int ierr, PyObject *filenameObject)
Similar to PyErr_SetFromWindowsErr(), with the additional behavior that if filenameObject is not
NULL, it is passed to the constructor of WindowsError as a third parameter. Availability: Windows.
PyObject* PyErr_SetFromWindowsErrWithFilename(int ierr, const char *filename)
Return value: Always NULL. Similar to PyErr_SetFromWindowsErrWithFilenameObject(), but the
filename is given as a C string. Availability: Windows.
PyObject* PyErr_SetExcFromWindowsErrWithFilenameObject(PyObject *type, int ierr, PyOb-
ject *filename)
Similar to PyErr_SetFromWindowsErrWithFilenameObject(), with an additional parameter specify-
ing the exception type to be raised. Availability: Windows.
New in version 2.3.
PyObject* PyErr_SetExcFromWindowsErrWithFilename(PyObject *type, int ierr, const
char *filename)
Return value: Always NULL. Similar to PyErr_SetFromWindowsErrWithFilename(), with an addi-
tional parameter specifying the exception type to be raised. Availability: Windows.
New in version 2.3.
void PyErr_BadInternalCall()
This is a shorthand for PyErr_SetString(PyExc_SystemError, message), where message indicates
that an internal operation (e.g. a Python/C API function) was invoked with an illegal argument. It is
mostly for internal use.
int PyErr_WarnEx(PyObject *category, char *message, int stacklevel)
Issue a warning message. The category argument is a warning category (see below) or NULL; the
19
The Python/C API, Release 2.7.15
message argument is a message string. stacklevel is a positive number giving a number of stack frames;
the warning will be issued from the currently executing line of code in that stack frame. A stacklevel
of 1 is the function calling PyErr_WarnEx(), 2 is the function above that, and so forth.
This function normally prints a warning message to sys.stderr; however, it is also possible that the user
has specified that warnings are to be turned into errors, and in that case this will raise an exception. It
is also possible that the function raises an exception because of a problem with the warning machinery
(the implementation imports the warnings module to do the heavy lifting). The return value is 0
if no exception is raised, or -1 if an exception is raised. (It is not possible to determine whether a
warning message is actually printed, nor what the reason is for the exception; this is intentional.) If
an exception is raised, the caller should do its normal exception handling (for example, Py_DECREF()
owned references and return an error value).
Warning categories must be subclasses of PyExc_Warning; PyExc_Warning is a subclass of
PyExc_Exception; the default warning category is PyExc_RuntimeWarning. The standard Python
warning categories are available as global variables whose names are enumerated at Standard Warning
Categories.
For information about warning control, see the documentation for the warnings module and the -W
option in the command line documentation. There is no C API for warning control.
int PyErr_Warn(PyObject *category, char *message)
Issue a warning message. The category argument is a warning category (see below) or NULL; the
message argument is a message string. The warning will appear to be issued from the function calling
PyErr_Warn(), equivalent to calling PyErr_WarnEx() with a stacklevel of 1.
Deprecated; use PyErr_WarnEx() instead.
int PyErr_WarnExplicit(PyObject *category, const char *message, const char *filename, int lineno,
const char *module, PyObject *registry)
Issue a warning message with explicit control over all warning attributes. This is a straightforward
wrapper around the Python function warnings.warn_explicit(), see there for more information.
The module and registry arguments may be set to NULL to get the default effect described there.
int PyErr_WarnPy3k(char *message, int stacklevel)
Issue a DeprecationWarning with the given message and stacklevel if the Py_Py3kWarningFlag flag
is enabled.
New in version 2.6.
int PyErr_CheckSignals()
This function interacts with Python’s signal handling. It checks whether a signal has been sent to the
processes and if so, invokes the corresponding signal handler. If the signal module is supported, this
can invoke a signal handler written in Python. In all cases, the default effect for SIGINT is to raise
the KeyboardInterrupt exception. If an exception is raised the error indicator is set and the function
returns -1; otherwise the function returns 0. The error indicator may or may not be cleared if it was
previously set.
void PyErr_SetInterrupt()
This function simulates the effect of a SIGINT signal arriving — the next time PyErr_CheckSignals()
is called, KeyboardInterrupt will be raised. It may be called without holding the interpreter lock.
int PySignal_SetWakeupFd(int fd)
This utility function specifies a file descriptor to which a '\0' byte will be written whenever a signal
is received. It returns the previous such file descriptor. The value -1 disables the feature; this is
the initial state. This is equivalent to signal.set_wakeup_fd() in Python, but without any error
checking. fd should be a valid file descriptor. The function should only be called from the main thread.
New in version 2.6.
PyObject* PyErr_NewException(char *name, PyObject *base, PyObject *dict)
Return value: New reference. This utility function creates and returns a new exception class. The
name argument must be the name of the new exception, a C string of the form module.classname.
The base and dict arguments are normally NULL. This creates a class object derived from Exception
(accessible in C as PyExc_Exception).
The __module__ attribute of the new class is set to the first part (up to the last dot) of the name
argument, and the class name is set to the last part (after the last dot). The base argument can be
used to specify alternate base classes; it can either be only one class or a tuple of classes. The dict
argument can be used to specify a dictionary of class variables and methods.
PyObject* PyErr_NewExceptionWithDoc(char *name, char *doc, PyObject *base, PyObject *dict)
Return value: New reference. Same as PyErr_NewException(), except that the new exception class
can easily be given a docstring: If doc is non-NULL, it will be used as the docstring for the exception
class.
New in version 2.7.
void PyErr_WriteUnraisable(PyObject *obj)
This utility function prints a warning message to sys.stderr when an exception has been set but
it is impossible for the interpreter to actually raise the exception. It is used, for example, when an
exception occurs in an __del__() method.
The function is called with a single argument obj that identifies the context in which the unraisable
exception occurred. If possible, the repr of obj will be printed in the warning message.
Notes:
1. This is a base class for other standard exceptions.
2. This is the same as weakref.ReferenceError.
3. Only defined on Windows; protect code that uses this by testing that the preprocessor macro
MS_WINDOWS is defined.
4. New in version 2.5.
5. Only defined on VMS; protect code that uses this by testing that the preprocessor macro __VMS is
defined.
Notes:
1. This is a base class for other standard warning categories.
FIVE
UTILITIES
The functions in this chapter perform various utility tasks, ranging from helping C code be more portable
across platforms, using Python modules from C, and parsing function arguments and constructing Python
values from C values.
25
The Python/C API, Release 2.7.15
PyObject *PySys_GetObject(char *name)
Return value: Borrowed reference. Return the object name from the sys module or NULL if it does
not exist, without setting an exception.
FILE *PySys_GetFile(char *name, FILE *def )
Return the FILE* associated with the object name in the sys module, or def if name is not in the
module or is not associated with a FILE*.
int PySys_SetObject(char *name, PyObject *v)
Set name in the sys module to v unless v is NULL, in which case name is deleted from the sys module.
Returns 0 on success, -1 on error.
void PySys_ResetWarnOptions()
Reset sys.warnoptions to an empty list.
void PySys_AddWarnOption(char *s)
Append s to sys.warnoptions.
void PySys_SetPath(char *path)
Set sys.path to a list object of paths found in path which should be a list of paths separated with the
platform’s search path delimiter (: on Unix, ; on Windows).
void PySys_WriteStdout(const char *format, ...)
Write the output string described by format to sys.stdout. No exceptions are raised, even if truncation
occurs (see below).
format should limit the total size of the formatted output string to 1000 bytes or less – after 1000
bytes, the output string is truncated. In particular, this means that no unrestricted “%s” formats
should occur; these should be limited using “%.<N>s” where <N> is a decimal number calculated so
that <N> plus the maximum size of other formatted text does not exceed 1000 bytes. Also watch out
for “%f”, which can print hundreds of digits for very large numbers.
If a problem occurs, or sys.stdout is unset, the formatted message is written to the real (C level)
stdout.
void PySys_WriteStderr(const char *format, ...)
As above, but write to sys.stderr or stderr instead.
26 Chapter 5. Utilities
The Python/C API, Release 2.7.15
PyObject* PyImport_ReloadModule(PyObject *m)
Return value: New reference. Reload a module. This is best described by referring to the built-in
Python function reload(), as the standard reload() function calls this function directly. Return a
new reference to the reloaded module, or NULL with an exception set on failure (the module still exists
in this case).
PyObject* PyImport_AddModule(const char *name)
Return value: Borrowed reference. Return the module object corresponding to a module name. The
name argument may be of the form package.module. First check the modules dictionary if there’s
one there, and if not, create a new one and insert it in the modules dictionary. Return NULL with an
exception set on failure.
Note: This function does not load or import the module; if the module wasn’t already loaded, you
will get an empty module object. Use PyImport_ImportModule() or one of its variants to import a
module. Package structures implied by a dotted name for name are not created if not already present.
28 Chapter 5. Utilities
The Python/C API, Release 2.7.15
void PyImport_Cleanup()
Empty the module table. For internal use only.
void _PyImport_Fini()
Finalize the import mechanism. For internal use only.
PyObject* _PyImport_FindExtension(char *, char *)
For internal use only.
PyObject* _PyImport_FixupExtension(char *, char *)
For internal use only.
int PyImport_ImportFrozenModule(char *name)
Load a frozen module named name. Return 1 for success, 0 if the module is not found, and -1 with
an exception set if the initialization failed. To access the imported module on a successful load, use
PyImport_ImportModule(). (Note the misnomer — this function would reload the module if it was
already imported.)
struct _frozen
This is the structure type definition for frozen module descriptors, as generated by the freeze utility
(see Tools/freeze/ in the Python source distribution). Its definition, found in Include/import.h,
is:
struct _frozen {
char *name;
unsigned char *code;
int size;
};
struct _inittab {
char *name;
void (*initfunc)(void);
};
30 Chapter 5. Utilities
The Python/C API, Release 2.7.15
32 Chapter 5. Utilities
The Python/C API, Release 2.7.15
where object is the Python object to be converted and address is the void* argument that was passed
to the PyArg_Parse*() function. The returned status should be 1 for a successful conversion and 0 if
the conversion has failed. When the conversion fails, the converter function should raise an exception
and leave the content of address unmodified.
S (string) [PyStringObject *] Like O but requires that the Python object is a string object. Raises
TypeError if the object is not a string object. The C variable may also be declared as PyObject*.
U (Unicode string) [PyUnicodeObject *] Like O but requires that the Python object is a Unicode ob-
ject. Raises TypeError if the object is not a Unicode object. The C variable may also be declared as
PyObject*.
t# (read-only character buffer) [char *, int] Like s#, but accepts any object which implements the
read-only buffer interface. The char* variable is set to point to the first byte of the buffer, and the
int is set to the length of the buffer. Only single-segment buffer objects are accepted; TypeError is
raised for all others.
w (read-write character buffer) [char *] Similar to s, but accepts any object which implements the
read-write buffer interface. The caller must determine the length of the buffer by other means, or use
w# instead. Only single-segment buffer objects are accepted; TypeError is raised for all others.
w# (read-write character buffer) [char *, Py_ssize_t] Like s#, but accepts any object which imple-
ments the read-write buffer interface. The char * variable is set to point to the first byte of the buffer,
and the Py_ssize_t is set to the length of the buffer. Only single-segment buffer objects are accepted;
TypeError is raised for all others.
w* (read-write byte-oriented buffer) [Py_buffer] This is to w what s* is to s.
New in version 2.6.
(items) (tuple) [matching-items] The object must be a Python sequence whose length is the number
of format units in items. The C arguments must correspond to the individual format units in items.
Format units for sequences may be nested.
Note: Prior to Python version 1.5.2, this format specifier only accepted a tuple containing the
individual parameters, not an arbitrary sequence. Code which previously caused TypeError to be
raised here may now proceed without an exception. This is not expected to be a problem for existing
code.
It is possible to pass Python long integers where integers are requested; however no proper range checking
is done — the most significant bits are silently truncated when the receiving field is too small to receive the
value (actually, the semantics are inherited from downcasts in C — your mileage may vary).
A few other characters have a meaning in a format string. These may not occur inside nested parentheses.
They are:
| Indicates that the remaining arguments in the Python argument list are optional. The C variables cor-
responding to optional arguments should be initialized to their default value — when an optional
argument is not specified, PyArg_ParseTuple() does not touch the contents of the corresponding C
variable(s).
: The list of format units ends here; the string after the colon is used as the function name in error messages
(the “associated value” of the exception that PyArg_ParseTuple() raises).
; The list of format units ends here; the string after the semicolon is used as the error message instead of
the default error message. : and ; mutually exclude each other.
Note that any Python object references which are provided to the caller are borrowed references; do not
decrement their reference count!
Additional arguments passed to these functions must be addresses of variables whose type is determined by
the format string; these are used to store values from the input tuple. There are a few cases, as described in
the list of format units above, where these parameters are used as input values; they should match what is
specified for the corresponding format unit in that case.
For the conversion to succeed, the arg object must match the format and the format must be exhausted.
On success, the PyArg_Parse*() functions return true, otherwise they return false and raise an appropriate
exception. When the PyArg_Parse*() functions fail due to conversion failure in one of the format units, the
variables at the addresses corresponding to that and the following format units are left untouched.
int PyArg_ParseTuple(PyObject *args, const char *format, ...)
Parse the parameters of a function that takes only positional parameters into local variables. Returns
true on success; on failure, it returns false and raises the appropriate exception.
int PyArg_VaParse(PyObject *args, const char *format, va_list vargs)
Identical to PyArg_ParseTuple(), except that it accepts a va_list rather than a variable number of
arguments.
34 Chapter 5. Utilities
The Python/C API, Release 2.7.15
static PyObject *
weakref_ref(PyObject *self, PyObject *args)
{
PyObject *object;
PyObject *callback = NULL;
PyObject *result = NULL;
Py_BuildValue() does not always build a tuple. It builds a tuple only if its format string contains two
or more format units. If the format string is empty, it returns None; if it contains exactly one format
unit, it returns whatever object is described by that format unit. To force it to return a tuple of size
0 or one, parenthesize the format string.
When memory buffers are passed as parameters to supply data to build objects, as for the s and
s# formats, the required data is copied. Buffers provided by the caller are never referenced by the
objects created by Py_BuildValue(). In other words, if your code invokes malloc() and passes the
allocated memory to Py_BuildValue(), your code is responsible for calling free() for that memory
once Py_BuildValue() returns.
In the following description, the quoted form is the format unit; the entry in (round) parentheses is
the Python object type that the format unit will return; and the entry in [square] brackets is the type
of the C value(s) to be passed.
The characters space, tab, colon and comma are ignored in format strings (but not within format units
such as s#). This can be used to make long format strings a tad more readable.
s (string) [char *] Convert a null-terminated C string to a Python object. If the C string pointer is
NULL, None is used.
s# (string) [char *, int] Convert a C string and its length to a Python object. If the C string pointer
is NULL, the length is ignored and None is returned.
z (string or None) [char *] Same as s.
z# (string or None) [char *, int] Same as s#.
u (Unicode string) [Py_UNICODE *] Convert a null-terminated buffer of Unicode (UCS-2 or
UCS-4) data to a Python Unicode object. If the Unicode buffer pointer is NULL, None is returned.
u# (Unicode string) [Py_UNICODE *, int] Convert a Unicode (UCS-2 or UCS-4) data buffer
and its length to a Python Unicode object. If the Unicode buffer pointer is NULL, the length is
ignored and None is returned.
i (integer) [int] Convert a plain C int to a Python integer object.
b (integer) [char] Convert a plain C char to a Python integer object.
h (integer) [short int] Convert a plain C short int to a Python integer object.
l (integer) [long int] Convert a C long int to a Python integer object.
B (integer) [unsigned char] Convert a C unsigned char to a Python integer object.
H (integer) [unsigned short int] Convert a C unsigned short int to a Python integer object.
I (integer/long) [unsigned int] Convert a C unsigned int to a Python integer object or a Python
long integer object, if it is larger than sys.maxint.
k (integer/long) [unsigned long] Convert a C unsigned long to a Python integer object or a
Python long integer object, if it is larger than sys.maxint.
L (long) [PY_LONG_LONG] Convert a C long long to a Python long integer object. Only
available on platforms that support long long.
K (long) [unsigned PY_LONG_LONG] Convert a C unsigned long long to a Python long
integer object. Only available on platforms that support unsigned long long.
n (int) [Py_ssize_t] Convert a C Py_ssize_t to a Python integer or long integer.
New in version 2.5.
c (string of length 1) [char] Convert a C int representing a character to a Python string of length
1.
d (float) [double] Convert a C double to a Python floating point number.
36 Chapter 5. Utilities
The Python/C API, Release 2.7.15
• When rv >= size, the output conversion was truncated and a buffer with rv + 1 bytes would have
been needed to succeed. str*[*size-1] is '\0' in this case.
• When rv < 0, “something bad happened.” str*[*size-1] is '\0' in this case too, but the rest of str is
undefined. The exact cause of the error depends on the underlying platform.
The following functions provide locale-independent string to number conversions.
double PyOS_string_to_double(const char *s, char **endptr, PyObject *overflow_exception)
Convert a string s to a double, raising a Python exception on failure. The set of accepted strings
corresponds to the set of strings accepted by Python’s float() constructor, except that s must not
have leading or trailing whitespace. The conversion is independent of the current locale.
If endptr is NULL, convert the whole string. Raise ValueError and return -1.0 if the string is not a
valid representation of a floating-point number.
If endptr is not NULL, convert as much of the string as possible and set *endptr to point to the first
unconverted character. If no initial segment of the string is the valid representation of a floating-point
number, set *endptr to point to the beginning of the string, raise ValueError, and return -1.0.
If s represents a value that is too large to store in a float (for example, "1e500" is such a string on many
platforms) then if overflow_exception is NULL return Py_HUGE_VAL (with an appropriate sign) and
don’t set any exception. Otherwise, overflow_exception must point to a Python exception object;
raise that exception and return -1.0. In both cases, set *endptr to point to the first character after
the converted value.
If any other error occurs during the conversion (for example an out-of-memory error), set the appro-
priate Python exception and return -1.0.
New in version 2.7.
double PyOS_ascii_strtod(const char *nptr, char **endptr)
Convert a string to a double. This function behaves like the Standard C function strtod() does in
the C locale. It does this without changing the current locale, since that would not be thread-safe.
PyOS_ascii_strtod() should typically be used for reading configuration files or other non-user input
that should be locale independent.
See the Unix man page strtod(2) for details.
New in version 2.4.
Deprecated since version 2.7: Use PyOS_string_to_double() instead.
char* PyOS_ascii_formatd(char *buffer, size_t buf_len, const char *format, double d)
Convert a double to a string using the '.' as the decimal separator. format is a printf()-style format
string specifying the number format. Allowed conversion characters are 'e', 'E', 'f', 'F', 'g' and
'G'.
The return value is a pointer to buffer with the converted string or NULL if the conversion failed.
New in version 2.4.
Deprecated since version 2.7: This function is removed in Python 2.7 and 3.1. Use
PyOS_double_to_string() instead.
char* PyOS_double_to_string(double val, char format_code, int precision, int flags, int *ptype)
Convert a double val to a string using supplied format_code, precision, and flags.
format_code must be one of 'e', 'E', 'f', 'F', 'g', 'G' or 'r'. For 'r', the supplied precision must
be 0 and is ignored. The 'r' format code specifies the standard repr() format.
flags can be zero or more of the values Py_DTSF_SIGN, Py_DTSF_ADD_DOT_0, or
Py_DTSF_ALT, or-ed together:
38 Chapter 5. Utilities
The Python/C API, Release 2.7.15
• Py_DTSF_SIGN means to always precede the returned string with a sign character, even if val
is non-negative.
• Py_DTSF_ADD_DOT_0 means to ensure that the returned string will not look like an integer.
• Py_DTSF_ALT means to apply “alternate” formatting rules. See the documentation for the
PyOS_snprintf() '#' specifier for details.
If ptype is non-NULL, then the value it points to will be set to one of Py_DTST_FINITE,
Py_DTST_INFINITE, or Py_DTST_NAN, signifying that val is a finite number, an infinite number,
or not a number, respectively.
The return value is a pointer to buffer with the converted string or NULL if the conversion failed. The
caller is responsible for freeing the returned string by calling PyMem_Free().
New in version 2.7.
double PyOS_ascii_atof(const char *nptr)
Convert a string to a double in a locale-independent way.
See the Unix man page atof(2) for details.
New in version 2.4.
Deprecated since version 3.1: Use PyOS_string_to_double() instead.
char* PyOS_stricmp(char *s1, char *s2)
Case insensitive comparison of strings. The function works almost identically to strcmp() except that
it ignores the case.
New in version 2.6.
char* PyOS_strnicmp(char *s1, char *s2, Py_ssize_t size)
Case insensitive comparison of strings. The function works almost identically to strncmp() except
that it ignores the case.
New in version 2.6.
5.8 Reflection
PyObject* PyEval_GetBuiltins()
Return value: Borrowed reference. Return a dictionary of the builtins in the current execution frame,
or the interpreter of the thread state if no frame is currently executing.
PyObject* PyEval_GetLocals()
Return value: Borrowed reference. Return a dictionary of the local variables in the current execution
frame, or NULL if no frame is currently executing.
PyObject* PyEval_GetGlobals()
Return value: Borrowed reference. Return a dictionary of the global variables in the current execution
frame, or NULL if no frame is currently executing.
PyFrameObject* PyEval_GetFrame()
Return value: Borrowed reference. Return the current thread state’s frame, which is NULL if no frame
is currently executing.
int PyFrame_GetLineNumber(PyFrameObject *frame)
Return the line number that frame is currently executing.
int PyEval_GetRestricted()
If there is a current frame and it is executing in restricted mode, return true, otherwise false.
5.8. Reflection 39
The Python/C API, Release 2.7.15
int PyCodec_Register(PyObject *search_function)
Register a new codec search function.
As side effect, this tries to load the encodings package, if not yet done, to make sure that it is always
first in the list of search functions.
int PyCodec_KnownEncoding(const char *encoding)
Return 1 or 0 depending on whether there is a registered codec for the given encoding.
PyObject* PyCodec_Encode(PyObject *object, const char *encoding, const char *errors)
Generic codec based encoding API.
object is passed through the encoder function found for the given encoding using the error handling
method defined by errors. errors may be NULL to use the default method defined for the codec. Raises
a LookupError if no encoder can be found.
PyObject* PyCodec_Decode(PyObject *object, const char *encoding, const char *errors)
Generic codec based decoding API.
object is passed through the decoder function found for the given encoding using the error handling
method defined by errors. errors may be NULL to use the default method defined for the codec. Raises
a LookupError if no encoder can be found.
40 Chapter 5. Utilities
The Python/C API, Release 2.7.15
42 Chapter 5. Utilities
CHAPTER
SIX
The functions in this chapter interact with Python objects regardless of their type, or with wide classes of
object types (e.g. all numerical types, or all sequence types). When used on object types for which they do
not apply, they will raise a Python exception.
It is not possible to use these functions on objects that are not properly initialized, such as a list object that
has been created by PyList_New(), but whose items have not been set to some non-NULL value yet.
43
The Python/C API, Release 2.7.15
Note: If o1 and o2 are the same object, PyObject_RichCompareBool() will always return 1 for Py_EQ and
0 for Py_NE.
PyObject* PyObject_Bytes(PyObject *o)
Compute a bytes representation of object o. In 2.x, this is just an alias for PyObject_Str().
PyObject* PyObject_Unicode(PyObject *o)
Return value: New reference. Compute a Unicode string representation of object o. Returns the
Unicode string representation on success, NULL on failure. This is the equivalent of the Python
expression unicode(o). Called by the unicode() built-in function.
int PyObject_IsInstance(PyObject *inst, PyObject *cls)
Returns 1 if inst is an instance of the class cls or a subclass of cls, or 0 if not. On error, returns -1 and
sets an exception. If cls is a type object rather than a class object, PyObject_IsInstance() returns
1 if inst is of type cls. If cls is a tuple, the check will be done against every entry in cls. The result
will be 1 when at least one of the checks returns 1, otherwise it will be 0. If inst is not a class instance
and cls is neither a type object, nor a class object, nor a tuple, inst must have a __class__ attribute
— the class relationship of the value of that attribute with cls will be used to determine the result of
this function.
New in version 2.1.
Changed in version 2.2: Support for a tuple as the second argument added.
Subclass determination is done in a fairly straightforward way, but includes a wrinkle that implementors of
extensions to the class system may want to be aware of. If A and B are class objects, B is a subclass of A
if it inherits from A either directly or indirectly. If either is not a class object, a more general mechanism
is used to determine the class relationship of the two objects. When testing if B is a subclass of A, if A
is B, PyObject_IsSubclass() returns true. If A and B are different objects, B’s __bases__ attribute is
searched in a depth-first fashion for A — the presence of the __bases__ attribute is considered sufficient for
this determination.
int PyObject_IsSubclass(PyObject *derived, PyObject *cls)
Returns 1 if the class derived is identical to or derived from the class cls, otherwise returns 0. In case
of an error, returns -1. If cls is a tuple, the check will be done against every entry in cls. The result
will be 1 when at least one of the checks returns 1, otherwise it will be 0. If either derived or cls is not
an actual class object (or tuple), this function uses the generic algorithm described above.
New in version 2.1.
Changed in version 2.3: Older versions of Python did not support a tuple as the second argument.
int PyCallable_Check(PyObject *o)
Determine if the object o is callable. Return 1 if the object is callable and 0 otherwise. This function
always succeeds.
PyObject* PyObject_Call(PyObject *callable_object, PyObject *args, PyObject *kw)
Return value: New reference. Call a callable Python object callable_object, with arguments given by
the tuple args, and named arguments given by the dictionary kw. If no named arguments are needed,
kw may be NULL. args must not be NULL, use an empty tuple if no arguments are needed. Returns
the result of the call on success, or NULL on failure. This is the equivalent of the Python expression
apply(callable_object, args, kw) or callable_object(*args, **kw).
New in version 2.2.
PyObject* PyObject_CallObject(PyObject *callable_object, PyObject *args)
Return value: New reference. Call a callable Python object callable_object, with arguments given by
the tuple args. If no arguments are needed, then args may be NULL. Returns the result of the call on
success, or NULL on failure. This is the equivalent of the Python expression apply(callable_object,
args) or callable_object(*args).
PyObject* PyObject_CallFunction(PyObject *callable, char *format, ...)
Return value: New reference. Call a callable Python object callable, with a variable number of C
arguments. The C arguments are described using a Py_BuildValue() style format string. The format
may be NULL, indicating that no arguments are provided. Returns the result of the call on success,
or NULL on failure. This is the equivalent of the Python expression apply(callable, args) or
callable(*args). Note that if you only pass PyObject * args, PyObject_CallFunctionObjArgs()
is a faster alternative.
PyObject* PyObject_CallMethod(PyObject *o, char *method, char *format, ...)
Return value: New reference. Call the method named method of object o with a variable number of C
arguments. The C arguments are described by a Py_BuildValue() format string that should produce a
tuple. The format may be NULL, indicating that no arguments are provided. Returns the result of the
call on success, or NULL on failure. This is the equivalent of the Python expression o.method(args).
Note that if you only pass PyObject * args, PyObject_CallMethodObjArgs() is a faster alternative.
PyObject* PyObject_CallFunctionObjArgs(PyObject *callable, ..., NULL)
Return value: New reference. Call a callable Python object callable, with a variable number of
PyObject* arguments. The arguments are provided as a variable number of parameters followed
by NULL. Returns the result of the call on success, or NULL on failure.
New in version 2.2.
PyObject* PyObject_CallMethodObjArgs(PyObject *o, PyObject *name, ..., NULL)
Return value: New reference. Calls a method of the object o, where the name of the method is given
as a Python string object in name. It is called with a variable number of PyObject* arguments. The
arguments are provided as a variable number of parameters followed by NULL. Returns the result of
the call on success, or NULL on failure.
New in version 2.2.
long PyObject_Hash(PyObject *o)
Compute and return the hash value of an object o. On failure, return -1. This is the equivalent of the
Python expression hash(o).
long PyObject_HashNotImplemented(PyObject *o)
Set a TypeError indicating that type(o) is not hashable and return -1. This function receives special
treatment when stored in a tp_hash slot, allowing a type to explicitly indicate to the interpreter that
it is not hashable.
New in version 2.6.
int PyObject_IsTrue(PyObject *o)
Returns 1 if the object o is considered to be true, and 0 otherwise. This is equivalent to the Python
expression not not o. On failure, return -1.
int PyObject_Not(PyObject *o)
Returns 0 if the object o is considered to be true, and 1 otherwise. This is equivalent to the Python
expression not o. On failure, return -1.
PyObject* PyObject_Type(PyObject *o)
Return value: New reference. When o is non-NULL, returns a type object corresponding to the object
type of object o. On failure, raises SystemError and returns NULL. This is equivalent to the Python
expression type(o). This function increments the reference count of the return value. There’s really
no reason to use this function instead of the common expression o->ob_type, which returns a pointer
of type PyTypeObject*, except when the incremented reference count is needed.
int PyObject_TypeCheck(PyObject *o, PyTypeObject *type)
Return true if the object o is of type type or a subtype of type. Both parameters must be non-NULL.
New in version 2.2.
Py_ssize_t PyObject_Length(PyObject *o)
Py_ssize_t PyObject_Size(PyObject *o)
Return the length of object o. If the object o provides either the sequence and mapping protocols, the
sequence length is returned. On error, -1 is returned. This is the equivalent to the Python expression
len(o).
Changed in version 2.5: These functions returned an int type. This might require changes in your
code for properly supporting 64-bit systems.
PyObject* PyObject_GetItem(PyObject *o, PyObject *key)
Return value: New reference. Return element of o corresponding to the object key or NULL on failure.
This is the equivalent of the Python expression o[key].
int PyObject_SetItem(PyObject *o, PyObject *key, PyObject *v)
Map the object key to the value v. Raise an exception and return -1 on failure; return 0 on success.
This is the equivalent of the Python statement o[key] = v.
int PyObject_DelItem(PyObject *o, PyObject *key)
Delete the mapping for key from o. Returns -1 on failure. This is the equivalent of the Python
statement del o[key].
int PyObject_AsFileDescriptor(PyObject *o)
Derives a file descriptor from a Python object. If the object is an integer or long integer, its value is
returned. If not, the object’s fileno() method is called if it exists; the method must return an integer
or long integer, which is returned as the file descriptor value. Returns -1 on failure.
PyObject* PyObject_Dir(PyObject *o)
Return value: New reference. This is equivalent to the Python expression dir(o), returning a (possibly
empty) list of strings appropriate for the object argument, or NULL if there was an error. If the
argument is NULL, this is like the Python dir(), returning the names of the current locals; in this
case, if no execution frame is active then NULL is returned but PyErr_Occurred() will return false.
PyObject* PyObject_GetIter(PyObject *o)
Return value: New reference. This is equivalent to the Python expression iter(o). It returns a
new iterator for the object argument, or the object itself if the object is already an iterator. Raises
TypeError and returns NULL if the object cannot be iterated.
int PyNumber_Check(PyObject *o)
Returns 1 if the object o provides numeric protocols, and false otherwise. This function always succeeds.
PyObject* PyNumber_Add(PyObject *o1, PyObject *o2)
Return value: New reference. Returns the result of adding o1 and o2, or NULL on failure. This is the
equivalent of the Python expression o1 + o2.
PyObject* PyNumber_Subtract(PyObject *o1, PyObject *o2)
Return value: New reference. Returns the result of subtracting o2 from o1, or NULL on failure. This
is the equivalent of the Python expression o1 - o2.
PyObject* PyNumber_Multiply(PyObject *o1, PyObject *o2)
Return value: New reference. Returns the result of multiplying o1 and o2, or NULL on failure. This
is the equivalent of the Python expression o1 * o2.
PyObject* PyNumber_Divide(PyObject *o1, PyObject *o2)
Return value: New reference. Returns the result of dividing o1 by o2, or NULL on failure. This is the
equivalent of the Python expression o1 / o2.
PyObject* PyNumber_FloorDivide(PyObject *o1, PyObject *o2)
Return value: New reference. Return the floor of o1 divided by o2, or NULL on failure. This is
equivalent to the “classic” division of integers.
New in version 2.2.
int PySequence_Check(PyObject *o)
Return 1 if the object provides sequence protocol, and 0 otherwise. This function always succeeds.
Py_ssize_t PySequence_Size(PyObject *o)
Py_ssize_t PySequence_Length(PyObject *o)
Returns the number of objects in sequence o on success, and -1 on failure. This is equivalent to the
Python expression len(o).
Changed in version 2.5: These functions returned an int type. This might require changes in your
code for properly supporting 64-bit systems.
PyObject* PySequence_Concat(PyObject *o1, PyObject *o2)
Return value: New reference. Return the concatenation of o1 and o2 on success, and NULL on failure.
This is the equivalent of the Python expression o1 + o2.
PyObject* PySequence_Repeat(PyObject *o, Py_ssize_t count)
Return value: New reference. Return the result of repeating sequence object o count times, or NULL
on failure. This is the equivalent of the Python expression o * count.
Changed in version 2.5: This function used an int type for count. This might require changes in your
code for properly supporting 64-bit systems.
PyObject* PySequence_InPlaceConcat(PyObject *o1, PyObject *o2)
Return value: New reference. Return the concatenation of o1 and o2 on success, and NULL on failure.
The operation is done in-place when o1 supports it. This is the equivalent of the Python expression
o1 += o2.
PyObject* PySequence_InPlaceRepeat(PyObject *o, Py_ssize_t count)
Return value: New reference. Return the result of repeating sequence object o count times, or NULL
on failure. The operation is done in-place when o supports it. This is the equivalent of the Python
expression o *= count.
Changed in version 2.5: This function used an int type for count. This might require changes in your
code for properly supporting 64-bit systems.
PyObject* PySequence_GetItem(PyObject *o, Py_ssize_t i)
Return value: New reference. Return the ith element of o, or NULL on failure. This is the equivalent
of the Python expression o[i].
Changed in version 2.5: This function used an int type for i. This might require changes in your code
for properly supporting 64-bit systems.
PyObject* PySequence_GetSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2)
Return value: New reference. Return the slice of sequence object o between i1 and i2, or NULL on
failure. This is the equivalent of the Python expression o[i1:i2].
Changed in version 2.5: This function used an int type for i1 and i2. This might require changes in
your code for properly supporting 64-bit systems.
int PySequence_SetItem(PyObject *o, Py_ssize_t i, PyObject *v)
Assign object v to the ith element of o. Raise an exception and return -1 on failure; return 0 on
success. This is the equivalent of the Python statement o[i] = v. This function does not steal a
reference to v.
If v is NULL, the element is deleted, however this feature is deprecated in favour of using
PySequence_DelItem().
Changed in version 2.5: This function used an int type for i. This might require changes in your code
for properly supporting 64-bit systems.
Changed in version 2.5: This function used an int type for i. This might require changes in your code
for properly supporting 64-bit systems.
PyObject** PySequence_Fast_ITEMS(PyObject *o)
Return the underlying array of PyObject pointers. Assumes that o was returned by
PySequence_Fast() and o is not NULL.
Note, if a list gets resized, the reallocation may relocate the items array. So, only use the underlying
array pointer in contexts where the sequence cannot change.
New in version 2.4.
PyObject* PySequence_ITEM(PyObject *o, Py_ssize_t i)
Return value: New reference. Return the ith element of o or NULL on failure. Macro form of
PySequence_GetItem() but without checking that PySequence_Check() on o is true and without
adjustment for negative indices.
New in version 2.3.
Changed in version 2.5: This function used an int type for i. This might require changes in your code
for properly supporting 64-bit systems.
Py_ssize_t PySequence_Fast_GET_SIZE(PyObject *o)
Returns the length of o, assuming that o was returned by PySequence_Fast() and that o is not NULL.
The size can also be gotten by calling PySequence_Size() on o, but PySequence_Fast_GET_SIZE()
is faster because it can assume o is a list or tuple.
int PyMapping_Check(PyObject *o)
Return 1 if the object provides mapping protocol, and 0 otherwise. This function always succeeds.
Py_ssize_t PyMapping_Size(PyObject *o)
Py_ssize_t PyMapping_Length(PyObject *o)
Returns the number of keys in object o on success, and -1 on failure. For objects that do not provide
mapping protocol, this is equivalent to the Python expression len(o).
Changed in version 2.5: These functions returned an int type. This might require changes in your
code for properly supporting 64-bit systems.
int PyMapping_DelItemString(PyObject *o, char *key)
Remove the mapping for object key from the object o. Return -1 on failure. This is equivalent to the
Python statement del o[key].
int PyMapping_DelItem(PyObject *o, PyObject *key)
Remove the mapping for object key from the object o. Return -1 on failure. This is equivalent to the
Python statement del o[key].
int PyMapping_HasKeyString(PyObject *o, char *key)
On success, return 1 if the mapping object has the key key and 0 otherwise. This is equivalent to
o[key], returning True on success and False on an exception. This function always succeeds.
int PyMapping_HasKey(PyObject *o, PyObject *key)
Return 1 if the mapping object has the key key and 0 otherwise. This is equivalent to o[key], returning
True on success and False on an exception. This function always succeeds.
PyObject* PyMapping_Keys(PyObject *o)
Return value: New reference. On success, return a list of the keys in object o. On failure, return
NULL. This is equivalent to the Python expression o.keys().
PyObject* PyMapping_Values(PyObject *o)
Return value: New reference. On success, return a list of the values in object o. On failure, return
NULL. This is equivalent to the Python expression o.values().
PyObject* PyMapping_Items(PyObject *o)
Return value: New reference. On success, return a list of the items in object o, where each item is a
tuple containing a key-value pair. On failure, return NULL. This is equivalent to the Python expression
o.items().
PyObject* PyMapping_GetItemString(PyObject *o, char *key)
Return value: New reference. Return element of o corresponding to the object key or NULL on failure.
This is the equivalent of the Python expression o[key].
int PyMapping_SetItemString(PyObject *o, char *key, PyObject *v)
Map the object key to the value v in object o. Returns -1 on failure. This is the equivalent of the
Python statement o[key] = v.
if (iterator == NULL) {
/* propagate error */
}
Py_DECREF(iterator);
if (PyErr_Occurred()) {
/* propagate error */
}
else {
/* continue doing useful work */
}
SEVEN
The functions in this chapter are specific to certain Python object types. Passing them an object of the
wrong type is not a good idea; if you receive an object from a Python program and you are not sure that it
has the right type, you must perform a type check first; for example, to check that an object is a dictionary,
use PyDict_Check(). The chapter is structured like the “family tree” of Python object types.
Warning: While the functions described in this chapter carefully check the type of the objects which
are passed in, many of them do not check for NULL being passed instead of a valid object. Allowing
NULL to be passed in can cause memory access violations and immediate termination of the interpreter.
57
The Python/C API, Release 2.7.15
int PyInt_Check(PyObject *o)
Return true if o is of type PyInt_Type or a subtype of PyInt_Type.
Changed in version 2.2: Allowed subtypes to be accepted.
int PyInt_CheckExact(PyObject *o)
Return true if o is of type PyInt_Type, but not a subtype of PyInt_Type.
New in version 2.2.
PyObject* PyInt_FromString(char *str, char **pend, int base)
Return value: New reference. Return a new PyIntObject or PyLongObject based on the string value
in str, which is interpreted according to the radix in base. If pend is non-NULL, *pend will point to
the first character in str which follows the representation of the number. If base is 0, the radix will
be determined based on the leading characters of str: if str starts with '0x' or '0X', radix 16 will be
used; if str starts with '0', radix 8 will be used; otherwise radix 10 will be used. If base is not 0, it
must be between 2 and 36, inclusive. Leading spaces are ignored. If there are no digits, ValueError
will be raised. If the string represents a number too large to be contained within the machine’s long
int type and overflow warnings are being suppressed, a PyLongObject will be returned. If overflow
warnings are not being suppressed, NULL will be returned in this case.
PyObject* PyInt_FromLong(long ival)
Return value: New reference. Create a new integer object with a value of ival.
The current implementation keeps an array of integer objects for all integers between -5 and 256, when
you create an int in that range you actually just get back a reference to the existing object. So it should
be possible to change the value of 1. I suspect the behaviour of Python in this case is undefined. :-)
PyObject* PyInt_FromSsize_t(Py_ssize_t ival)
Return value: New reference. Create a new integer object with a value of ival. If the value is larger
than LONG_MAX or smaller than LONG_MIN, a long integer object is returned.
New in version 2.5.
PyObject* PyInt_FromSize_t(size_t ival)
Create a new integer object with a value of ival. If the value exceeds LONG_MAX, a long integer object
is returned.
New in version 2.5.
long PyInt_AsLong(PyObject *io)
Will first attempt to cast the object to a PyIntObject, if it is not already one, and then return its
value. If there is an error, -1 is returned, and the caller should check PyErr_Occurred() to find out
whether there was an error, or whether the value just happened to be -1.
long PyInt_AS_LONG(PyObject *io)
Return the value of the object io. No error checking is performed.
unsigned long PyInt_AsUnsignedLongMask(PyObject *io)
Will first attempt to cast the object to a PyIntObject or PyLongObject, if it is not already one, and
then return its value as unsigned long. This function does not check for overflow.
New in version 2.3.
unsigned PY_LONG_LONG PyInt_AsUnsignedLongLongMask(PyObject *io)
Will first attempt to cast the object to a PyIntObject or PyLongObject, if it is not already one, and
then return its value as unsigned long long, without checking for overflow.
New in version 2.3.
Py_ssize_t PyInt_AsSsize_t(PyObject *io)
Will first attempt to cast the object to a PyIntObject or PyLongObject, if it is not already one, and
then return its value as Py_ssize_t.
int PyLong_CheckExact(PyObject *p)
Return true if its argument is a PyLongObject, but not a subtype of PyLongObject.
New in version 2.2.
PyObject* PyLong_FromLong(long v)
Return value: New reference. Return a new PyLongObject object from v, or NULL on failure.
PyObject* PyLong_FromUnsignedLong(unsigned long v)
Return value: New reference. Return a new PyLongObject object from a C unsigned long, or NULL
on failure.
PyObject* PyLong_FromSsize_t(Py_ssize_t v)
Return value: New reference. Return a new PyLongObject object from a C Py_ssize_t, or NULL on
failure.
New in version 2.6.
PyObject* PyLong_FromSize_t(size_t v)
Return value: New reference. Return a new PyLongObject object from a C size_t, or NULL on
failure.
New in version 2.6.
PyObject* PyLong_FromLongLong(PY_LONG_LONG v)
Return value: New reference. Return a new PyLongObject object from a C long long, or NULL on
failure.
PyObject* PyLong_FromUnsignedLongLong(unsigned PY_LONG_LONG v)
Return value: New reference. Return a new PyLongObject object from a C unsigned long long, or
NULL on failure.
PyObject* PyLong_FromDouble(double v)
Return value: New reference. Return a new PyLongObject object from the integer part of v, or NULL
on failure.
PyObject* PyLong_FromString(char *str, char **pend, int base)
Return value: New reference. Return a new PyLongObject based on the string value in str, which is
interpreted according to the radix in base. If pend is non-NULL, *pend will point to the first character
in str which follows the representation of the number. If base is 0, the radix will be determined based
on the leading characters of str: if str starts with '0x' or '0X', radix 16 will be used; if str starts with
'0', radix 8 will be used; otherwise radix 10 will be used. If base is not 0, it must be between 2 and
36, inclusive. Leading spaces are ignored. If there are no digits, ValueError will be raised.
PyObject* PyLong_FromUnicode(Py_UNICODE *u, Py_ssize_t length, int base)
Return value: New reference. Convert a sequence of Unicode digits to a Python long integer value.
The first parameter, u, points to the first character of the Unicode string, length gives the number of
characters, and base is the radix for the conversion. The radix must be in the range [2, 36]; if it is out
of range, ValueError will be raised.
New in version 1.6.
Changed in version 2.5: This function used an int for length. This might require changes in your code
for properly supporting 64-bit systems.
PyObject* PyLong_FromVoidPtr(void *p)
Return value: New reference. Create a Python integer or long integer from the pointer p. The pointer
value can be retrieved from the resulting value using PyLong_AsVoidPtr().
New in version 1.5.2.
Changed in version 2.5: If the integer is larger than LONG_MAX, a positive long integer is returned.
long PyLong_AsLong(PyObject *pylong)
Return a C long representation of the contents of pylong. If pylong is greater than LONG_MAX, an
OverflowError is raised and -1 will be returned.
long PyLong_AsLongAndOverflow(PyObject *pylong, int *overflow)
Return a C long representation of the contents of pylong. If pylong is greater than LONG_MAX or less
than LONG_MIN, set *overflow to 1 or -1, respectively, and return -1; otherwise, set *overflow to 0. If
any other exception occurs (for example a TypeError or MemoryError), then -1 will be returned and
*overflow will be 0.
New in version 2.7.
PY_LONG_LONG PyLong_AsLongLongAndOverflow(PyObject *pylong, int *overflow)
Return a C long long representation of the contents of pylong. If pylong is greater than PY_LLONG_MAX
or less than PY_LLONG_MIN, set *overflow to 1 or -1, respectively, and return -1; otherwise, set *overflow
to 0. If any other exception occurs (for example a TypeError or MemoryError), then -1 will be returned
and *overflow will be 0.
New in version 2.7.
Py_ssize_t PyLong_AsSsize_t(PyObject *pylong)
Return a C Py_ssize_t representation of the contents of pylong. If pylong is greater than
PY_SSIZE_T_MAX, an OverflowError is raised and -1 will be returned.
New in version 2.6.
unsigned long PyLong_AsUnsignedLong(PyObject *pylong)
Return a C unsigned long representation of the contents of pylong. If pylong is greater than
ULONG_MAX, an OverflowError is raised.
PY_LONG_LONG PyLong_AsLongLong(PyObject *pylong)
Return a C long long from a Python long integer. If pylong cannot be represented as a long long,
an OverflowError is raised and -1 is returned.
New in version 2.2.
unsigned PY_LONG_LONG PyLong_AsUnsignedLongLong(PyObject *pylong)
Return a C unsigned long long from a Python long integer. If pylong cannot be represented as an
unsigned long long, an OverflowError is raised and (unsigned long long)-1 is returned.
New in version 2.2.
Changed in version 2.7: A negative pylong now raises OverflowError, not TypeError.
unsigned long PyLong_AsUnsignedLongMask(PyObject *io)
Return a C unsigned long from a Python long integer, without checking for overflow.
New in version 2.3.
unsigned PY_LONG_LONG PyLong_AsUnsignedLongLongMask(PyObject *io)
Return a C unsigned long long from a Python long integer, without checking for overflow.
New in version 2.3.
double PyLong_AsDouble(PyObject *pylong)
Return a C double representation of the contents of pylong. If pylong cannot be approximately
represented as a double, an OverflowError exception is raised and -1.0 will be returned.
void* PyLong_AsVoidPtr(PyObject *pylong)
Convert a Python integer or long integer pylong to a C void pointer. If pylong cannot be converted,
an OverflowError will be raised. This is only assured to produce a usable void pointer for values
created with PyLong_FromVoidPtr().
New in version 1.5.2.
Changed in version 2.5: For values outside 0..LONG_MAX, both signed and unsigned integers are
accepted.
Note that the functions which accept these structures as parameters and return them as results do so by
value rather than dereferencing them through pointers. This is consistent throughout the API.
Py_complex
The C structure which corresponds to the value portion of a Python complex number object. Most of
the functions for dealing with complex number objects use structures of this type as input or output
values, as appropriate. It is defined as:
typedef struct {
double real;
double imag;
} Py_complex;
PyComplexObject
This subtype of PyObject represents a Python complex number object.
PyTypeObject PyComplex_Type
This instance of PyTypeObject represents the Python complex number type. It is the same object as
complex and types.ComplexType.
int PyComplex_Check(PyObject *p)
Return true if its argument is a PyComplexObject or a subtype of PyComplexObject.
Changed in version 2.2: Allowed subtypes to be accepted.
int PyComplex_CheckExact(PyObject *p)
Return true if its argument is a PyComplexObject, but not a subtype of PyComplexObject.
New in version 2.2.
PyObject* PyComplex_FromCComplex(Py_complex v)
Return value: New reference. Create a new Python complex number object from a C Py_complex
value.
PyObject* PyComplex_FromDoubles(double real, double imag)
Return value: New reference. Return a new PyComplexObject object from real and imag.
double PyComplex_RealAsDouble(PyObject *op)
Return the real part of op as a C double.
double PyComplex_ImagAsDouble(PyObject *op)
Return the imaginary part of op as a C double.
Py_complex PyComplex_AsCComplex(PyObject *op)
Return the Py_complex value of the complex number op. Upon failure, this method returns -1.0 as a
real value.
Changed in version 2.6: If op is not a Python complex number object but has a __complex__() method,
this method will first be called to convert op to a Python complex number object.
int PyByteArray_Check(PyObject *o)
Return true if the object o is a bytearray object or an instance of a subtype of the bytearray type.
int PyByteArray_CheckExact(PyObject *o)
Return true if the object o is a bytearray object, but not an instance of a subtype of the bytearray
type.
PyObject* PyByteArray_FromObject(PyObject *o)
Return a new bytearray object from any object, o, that implements the buffer protocol.
PyObject* PyByteArray_FromStringAndSize(const char *string, Py_ssize_t len)
Create a new bytearray object from string and its length, len. On failure, NULL is returned.
PyObject* PyByteArray_Concat(PyObject *a, PyObject *b)
Concat bytearrays a and b and return a new bytearray with the result.
Py_ssize_t PyByteArray_Size(PyObject *bytearray)
Return the size of bytearray after checking for a NULL pointer.
char* PyByteArray_AsString(PyObject *bytearray)
Return the contents of bytearray as a char array after checking for a NULL pointer.
int PyByteArray_Resize(PyObject *bytearray, Py_ssize_t len)
Resize the internal buffer of bytearray to len.
Macros
These macros trade safety for speed and they don’t check pointers.
char* PyByteArray_AS_STRING(PyObject *bytearray)
Macro version of PyByteArray_AsString().
Py_ssize_t PyByteArray_GET_SIZE(PyObject *bytearray)
Macro version of PyByteArray_Size().
Note: These functions have been renamed to PyBytes_* in Python 3.x. Unless otherwise noted, the
PyBytes functions available in 3.x are aliased to their PyString_* equivalents to help porting.
PyStringObject
This subtype of PyObject represents a Python string object.
PyTypeObject PyString_Type
This instance of PyTypeObject represents the Python string type; it is the same object as str and
types.StringType in the Python layer. .
int PyString_Check(PyObject *o)
Return true if the object o is a string object or an instance of a subtype of the string type.
Changed in version 2.2: Allowed subtypes to be accepted.
int PyString_CheckExact(PyObject *o)
Return true if the object o is a string object, but not an instance of a subtype of the string type.
New in version 2.2.
PyObject* PyString_FromString(const char *v)
Return value: New reference. Return a new string object with a copy of the string v as value on
success, and NULL on failure. The parameter v must not be NULL; it will not be checked.
An unrecognized format character causes all the rest of the format string to be copied as-is to the
result string, and any extra arguments discarded.
Note: The “%lld” and “%llu” format specifiers are only available when HAVE_LONG_LONG is defined.
Changed in version 2.5: This function returned an int type. This might require changes in your code
for properly supporting 64-bit systems.
Py_ssize_t PyString_GET_SIZE(PyObject *string)
Macro form of PyString_Size() but without error checking.
Changed in version 2.5: This macro returned an int type. This might require changes in your code
for properly supporting 64-bit systems.
char* PyString_AsString(PyObject *string)
Return a NUL-terminated representation of the contents of string. The pointer refers to the internal
buffer of string, not a copy. The data must not be modified in any way, unless the string was just
created using PyString_FromStringAndSize(NULL, size). It must not be deallocated. If string is a
Unicode object, this function computes the default encoding of string and operates on that. If string
is not a string object at all, PyString_AsString() returns NULL and raises TypeError.
char* PyString_AS_STRING(PyObject *string)
Macro form of PyString_AsString() but without error checking. Only string objects are supported;
no Unicode objects should be passed.
int PyString_AsStringAndSize(PyObject *obj, char **buffer, Py_ssize_t *length)
Return a NUL-terminated representation of the contents of the object obj through the output variables
buffer and length.
The function accepts both string and Unicode objects as input. For Unicode objects it returns the
default encoded version of the object. If length is NULL, the resulting buffer may not contain NUL
characters; if it does, the function returns -1 and a TypeError is raised.
The buffer refers to an internal string buffer of obj, not a copy. The data must not be modified in any
way, unless the string was just created using PyString_FromStringAndSize(NULL, size). It must
not be deallocated. If string is a Unicode object, this function computes the default encoding of string
and operates on that. If string is not a string object at all, PyString_AsStringAndSize() returns -1
and raises TypeError.
Changed in version 2.5: This function used an int * type for length. This might require changes in
your code for properly supporting 64-bit systems.
void PyString_Concat(PyObject **string, PyObject *newpart)
Create a new string object in *string containing the contents of newpart appended to string; the caller
will own the new reference. The reference to the old value of string will be stolen. If the new string
cannot be created, the old reference to string will still be discarded and the value of *string will be set
to NULL; the appropriate exception will be set.
void PyString_ConcatAndDel(PyObject **string, PyObject *newpart)
Create a new string object in *string containing the contents of newpart appended to string. This
version decrements the reference count of newpart.
int _PyString_Resize(PyObject **string, Py_ssize_t newsize)
A way to resize a string object even though it is “immutable”. Only use this to build up a brand new
string object; don’t use this if the string may already be known in other parts of the code. It is an
error to call this function if the refcount on the input string object is not one. Pass the address of
an existing string object as an lvalue (it may be written into), and the new size desired. On success,
*string holds the resized string object and 0 is returned; the address in *string may differ from its
input value. If the reallocation fails, the original string object at *string is deallocated, *string is set
to NULL, a memory exception is set, and -1 is returned.
Changed in version 2.5: This function used an int type for newsize. This might require changes in
your code for properly supporting 64-bit systems.
PyObject* PyString_Format(PyObject *format, PyObject *args)
Return value: New reference. Return a new string object from format and args. Analogous to format
Note: This function is not available in 3.x and does not have a PyBytes alias.
Note: This function is not available in 3.x and does not have a PyBytes alias.
Note: This function is not available in 3.x and does not have a PyBytes alias.
Changed in version 2.5: This function used an int type for size. This might require changes in your
code for properly supporting 64-bit systems.
PyObject* PyString_AsDecodedObject(PyObject *str, const char *encoding, const char *errors)
Return value: New reference. Decode a string object by passing it to the codec registered for encoding
and return the result as Python object. encoding and errors have the same meaning as the parameters
of the same name in the string encode() method. The codec to be used is looked up using the Python
codec registry. Return NULL if an exception was raised by the codec.
Note: This function is not available in 3.x and does not have a PyBytes alias.
Note: This function is not available in 3.x and does not have a PyBytes alias.
Changed in version 2.5: This function used an int type for size. This might require changes in your
code for properly supporting 64-bit systems.
Note: This function is not available in 3.x and does not have a PyBytes alias.
Unicode Objects
Unicode Type
These are the basic Unicode object types used for the Unicode implementation in Python:
Py_UNICODE
This type represents the storage type which is used by Python internally as basis for holding Unicode
ordinals. Python’s default builds use a 16-bit type for Py_UNICODE and store Unicode values internally
as UCS2. It is also possible to build a UCS4 version of Python (most recent Linux distributions come
with UCS4 builds of Python). These builds then use a 32-bit type for Py_UNICODE and store Unicode
data internally as UCS4. On platforms where wchar_t is available and compatible with the chosen
Python Unicode build variant, Py_UNICODE is a typedef alias for wchar_t to enhance native platform
compatibility. On all other platforms, Py_UNICODE is a typedef alias for either unsigned short (UCS2)
or unsigned long (UCS4).
Note that UCS2 and UCS4 Python builds are not binary compatible. Please keep this in mind when writing
extensions or interfaces.
PyUnicodeObject
This subtype of PyObject represents a Python Unicode object.
PyTypeObject PyUnicode_Type
This instance of PyTypeObject represents the Python Unicode type. It is exposed to Python code as
unicode and types.UnicodeType.
The following APIs are really C macros and can be used to do fast checks and to access internal read-only
data of Unicode objects:
int PyUnicode_Check(PyObject *o)
Return true if the object o is a Unicode object or an instance of a Unicode subtype.
Changed in version 2.2: Allowed subtypes to be accepted.
int PyUnicode_CheckExact(PyObject *o)
Return true if the object o is a Unicode object, but not an instance of a subtype.
New in version 2.2.
Py_ssize_t PyUnicode_GET_SIZE(PyObject *o)
Return the size of the object. o has to be a PyUnicodeObject (not checked).
Changed in version 2.5: This function returned an int type. This might require changes in your code
for properly supporting 64-bit systems.
Py_ssize_t PyUnicode_GET_DATA_SIZE(PyObject *o)
Return the size of the object’s internal buffer in bytes. o has to be a PyUnicodeObject (not checked).
Changed in version 2.5: This function returned an int type. This might require changes in your code
for properly supporting 64-bit systems.
Py_UNICODE* PyUnicode_AS_UNICODE(PyObject *o)
Return a pointer to the internal Py_UNICODE buffer of the object. o has to be a PyUnicodeObject (not
checked).
const char* PyUnicode_AS_DATA(PyObject *o)
Return a pointer to the internal buffer of the object. o has to be a PyUnicodeObject (not checked).
int PyUnicode_ClearFreeList()
Clear the free list. Return the total number of freed items.
New in version 2.6.
Unicode provides many different character properties. The most often needed ones are available through
these macros which are mapped to C functions depending on the Python configuration.
int Py_UNICODE_ISSPACE(Py_UNICODE ch)
Return 1 or 0 depending on whether ch is a whitespace character.
int Py_UNICODE_ISLOWER(Py_UNICODE ch)
Return 1 or 0 depending on whether ch is a lowercase character.
int Py_UNICODE_ISUPPER(Py_UNICODE ch)
Return 1 or 0 depending on whether ch is an uppercase character.
int Py_UNICODE_ISTITLE(Py_UNICODE ch)
Return 1 or 0 depending on whether ch is a titlecase character.
int Py_UNICODE_ISLINEBREAK(Py_UNICODE ch)
Return 1 or 0 depending on whether ch is a linebreak character.
int Py_UNICODE_ISDECIMAL(Py_UNICODE ch)
Return 1 or 0 depending on whether ch is a decimal character.
int Py_UNICODE_ISDIGIT(Py_UNICODE ch)
Return 1 or 0 depending on whether ch is a digit character.
int Py_UNICODE_ISNUMERIC(Py_UNICODE ch)
Return 1 or 0 depending on whether ch is a numeric character.
int Py_UNICODE_ISALPHA(Py_UNICODE ch)
Return 1 or 0 depending on whether ch is an alphabetic character.
int Py_UNICODE_ISALNUM(Py_UNICODE ch)
Return 1 or 0 depending on whether ch is an alphanumeric character.
These APIs can be used for fast direct character conversions:
Py_UNICODE Py_UNICODE_TOLOWER(Py_UNICODE ch)
Return the character ch converted to lower case.
Py_UNICODE Py_UNICODE_TOUPPER(Py_UNICODE ch)
Return the character ch converted to upper case.
Py_UNICODE Py_UNICODE_TOTITLE(Py_UNICODE ch)
Return the character ch converted to title case.
int Py_UNICODE_TODECIMAL(Py_UNICODE ch)
Return the character ch converted to a decimal positive integer. Return -1 if this is not possible. This
macro does not raise exceptions.
int Py_UNICODE_TODIGIT(Py_UNICODE ch)
Return the character ch converted to a single digit integer. Return -1 if this is not possible. This
macro does not raise exceptions.
double Py_UNICODE_TONUMERIC(Py_UNICODE ch)
Return the character ch converted to a double. Return -1.0 if this is not possible. This macro does
not raise exceptions.
Plain Py_UNICODE
To create Unicode objects and access their basic sequence properties, use these APIs:
PyObject* PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size)
Return value: New reference. Create a Unicode object from the Py_UNICODE buffer u of the given
size. u may be NULL which causes the contents to be undefined. It is the user’s responsibility to fill
in the needed data. The buffer is copied into the new object. If the buffer is not NULL, the return
value might be a shared object. Therefore, modification of the resulting Unicode object is only allowed
when u is NULL.
Changed in version 2.5: This function used an int type for size. This might require changes in your
code for properly supporting 64-bit systems.
PyObject* PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size)
Return value: New reference. Create a Unicode object from the char buffer u. The bytes will be
interpreted as being UTF-8 encoded. u may also be NULL which causes the contents to be undefined.
It is the user’s responsibility to fill in the needed data. The buffer is copied into the new object. If the
buffer is not NULL, the return value might be a shared object. Therefore, modification of the resulting
Unicode object is only allowed when u is NULL.
New in version 2.6.
PyObject *PyUnicode_FromString(const char *u)
Return value: New reference. Create a Unicode object from a UTF-8 encoded null-terminated char
buffer u.
New in version 2.6.
PyObject* PyUnicode_FromFormat(const char *format, ...)
Return value: New reference. Take a C printf()-style format string and a variable number of argu-
ments, calculate the size of the resulting Python unicode string and return a string with the values
formatted into it. The variable arguments must be C types and must correspond exactly to the format
characters in the format string. The following format characters are allowed:
An unrecognized format character causes all the rest of the format string to be copied as-is to the
result string, and any extra arguments discarded.
New in version 2.6.
PyObject* PyUnicode_FromFormatV(const char *format, va_list vargs)
Return value: New reference. Identical to PyUnicode_FromFormat() except that it takes exactly two
arguments.
New in version 2.6.
Py_UNICODE* PyUnicode_AsUnicode(PyObject *unicode)
Return a read-only pointer to the Unicode object’s internal Py_UNICODE buffer, NULL if unicode is not
a Unicode object. Note that the resulting Py_UNICODE* string may contain embedded null characters,
which would cause the string to be truncated when used in most C functions.
Py_ssize_t PyUnicode_GetSize(PyObject *unicode)
Return the length of the Unicode object.
Changed in version 2.5: This function returned an int type. This might require changes in your code
for properly supporting 64-bit systems.
PyObject* PyUnicode_FromEncodedObject(PyObject *obj, const char *encoding, const char *errors)
Return value: New reference. Coerce an encoded object obj to a Unicode object and return a reference
with incremented refcount.
String and other char buffer compatible objects are decoded according to the given encoding and using
the error handling defined by errors. Both can be NULL to have the interface use the default values
(see the next section for details).
All other objects, including Unicode objects, cause a TypeError to be set.
The API returns NULL if there was an error. The caller is responsible for decref’ing the returned
objects.
PyObject* PyUnicode_FromObject(PyObject *obj)
Return value: New reference. Shortcut for PyUnicode_FromEncodedObject(obj, NULL, "strict")
which is used throughout the interpreter whenever coercion to Unicode is needed.
If the platform supports wchar_t and provides a header file wchar.h, Python can interface directly to this
type using the following functions. Support is optimized if Python’s own Py_UNICODE type is identical to
the system’s wchar_t.
wchar_t Support
Built-in Codecs
Python provides a set of built-in codecs which are written in C for speed. All of these codecs are directly
usable via the following functions.
Many of the following APIs take two arguments encoding and errors, and they have the same semantics as
the ones of the built-in unicode() Unicode object constructor.
Setting encoding to NULL causes the default encoding to be used which is ASCII. The file system calls
should use Py_FileSystemDefaultEncoding as the encoding for file names. This variable should be treated
as read-only: on some systems, it will be a pointer to a static string, on others, it will change at run-time
(such as when the application invokes setlocale).
Error handling is set by errors which may also be set to NULL meaning to use the default handling defined
for the codec. Default error handling for all built-in codecs is “strict” (ValueError is raised).
The codecs all use a similar interface. Only deviation from the following generic ones are documented for
simplicity.
Generic Codecs
built-in function. The codec to be used is looked up using the Python codec registry. Return NULL if
an exception was raised by the codec.
Changed in version 2.5: This function used an int type for size. This might require changes in your
code for properly supporting 64-bit systems.
PyObject* PyUnicode_Encode(const Py_UNICODE *s, Py_ssize_t size, const char *encoding,
const char *errors)
Return value: New reference. Encode the Py_UNICODE buffer s of the given size and return a Python
string object. encoding and errors have the same meaning as the parameters of the same name in the
Unicode encode() method. The codec to be used is looked up using the Python codec registry. Return
NULL if an exception was raised by the codec.
Changed in version 2.5: This function used an int type for size. This might require changes in your
code for properly supporting 64-bit systems.
PyObject* PyUnicode_AsEncodedString(PyObject *unicode, const char *encoding, const
char *errors)
Return value: New reference. Encode a Unicode object and return the result as Python string object.
encoding and errors have the same meaning as the parameters of the same name in the Unicode
encode() method. The codec to be used is looked up using the Python codec registry. Return NULL
if an exception was raised by the codec.
UTF-8 Codecs
UTF-32 Codecs
If *byteorder is zero, and the first four bytes of the input data are a byte order mark (BOM), the
decoder switches to this byte order and the BOM is not copied into the resulting Unicode string. If
*byteorder is -1 or 1, any byte order mark is copied to the output.
After completion, *byteorder is set to the current byte order at the end of input data.
In a narrow build code points outside the BMP will be decoded as surrogate pairs.
If byteorder is NULL, the codec starts in native order mode.
Return NULL if an exception was raised by the codec.
New in version 2.6.
PyObject* PyUnicode_DecodeUTF32Stateful(const char *s, Py_ssize_t size, const char *errors,
int *byteorder, Py_ssize_t *consumed)
If consumed is NULL, behave like PyUnicode_DecodeUTF32(). If consumed is not NULL,
PyUnicode_DecodeUTF32Stateful() will not treat trailing incomplete UTF-32 byte sequences (such
as a number of bytes not divisible by four) as an error. Those bytes will not be decoded and the
number of bytes that have been decoded will be stored in consumed.
New in version 2.6.
PyObject* PyUnicode_EncodeUTF32(const Py_UNICODE *s, Py_ssize_t size, const char *errors,
int byteorder)
Return a Python bytes object holding the UTF-32 encoded value of the Unicode data in s. Output is
written according to the following byte order:
If byteorder is 0, the output string will always start with the Unicode BOM mark (U+FEFF). In the
other two modes, no BOM mark is prepended.
If Py_UNICODE_WIDE is not defined, surrogate pairs will be output as a single code point.
Return NULL if an exception was raised by the codec.
New in version 2.6.
PyObject* PyUnicode_AsUTF32String(PyObject *unicode)
Return a Python string using the UTF-32 encoding in native byte order. The string always starts with
a BOM mark. Error handling is “strict”. Return NULL if an exception was raised by the codec.
New in version 2.6.
UTF-16 Codecs
If *byteorder is zero, and the first two bytes of the input data are a byte order mark (BOM), the
decoder switches to this byte order and the BOM is not copied into the resulting Unicode string. If
*byteorder is -1 or 1, any byte order mark is copied to the output (where it will result in either a
\ufeff or a \ufffe character).
After completion, *byteorder is set to the current byte order at the end of input data.
If byteorder is NULL, the codec starts in native order mode.
Return NULL if an exception was raised by the codec.
Changed in version 2.5: This function used an int type for size. This might require changes in your
code for properly supporting 64-bit systems.
PyObject* PyUnicode_DecodeUTF16Stateful(const char *s, Py_ssize_t size, const char *errors,
int *byteorder, Py_ssize_t *consumed)
Return value: New reference. If consumed is NULL, behave like PyUnicode_DecodeUTF16(). If con-
sumed is not NULL, PyUnicode_DecodeUTF16Stateful() will not treat trailing incomplete UTF-16
byte sequences (such as an odd number of bytes or a split surrogate pair) as an error. Those bytes will
not be decoded and the number of bytes that have been decoded will be stored in consumed.
New in version 2.4.
Changed in version 2.5: This function used an int type for size and an int * type for consumed. This
might require changes in your code for properly supporting 64-bit systems.
PyObject* PyUnicode_EncodeUTF16(const Py_UNICODE *s, Py_ssize_t size, const char *errors,
int byteorder)
Return value: New reference. Return a Python string object holding the UTF-16 encoded value of the
Unicode data in s. Output is written according to the following byte order:
If byteorder is 0, the output string will always start with the Unicode BOM mark (U+FEFF). In the
other two modes, no BOM mark is prepended.
If Py_UNICODE_WIDE is defined, a single Py_UNICODE value may get represented as a surrogate
pair. If it is not defined, each Py_UNICODE values is interpreted as a UCS-2 character.
Return NULL if an exception was raised by the codec.
Changed in version 2.5: This function used an int type for size. This might require changes in your
code for properly supporting 64-bit systems.
PyObject* PyUnicode_AsUTF16String(PyObject *unicode)
Return value: New reference. Return a Python string using the UTF-16 encoding in native byte order.
The string always starts with a BOM mark. Error handling is “strict”. Return NULL if an exception
was raised by the codec.
UTF-7 Codecs
Unicode-Escape Codecs
Raw-Unicode-Escape Codecs
Latin-1 Codecs
These are the Latin-1 codec APIs: Latin-1 corresponds to the first 256 Unicode ordinals and only these are
accepted by the codecs during encoding.
PyObject* PyUnicode_DecodeLatin1(const char *s, Py_ssize_t size, const char *errors)
Return value: New reference. Create a Unicode object by decoding size bytes of the Latin-1 encoded
string s. Return NULL if an exception was raised by the codec.
Changed in version 2.5: This function used an int type for size. This might require changes in your
code for properly supporting 64-bit systems.
PyObject* PyUnicode_EncodeLatin1(const Py_UNICODE *s, Py_ssize_t size, const char *errors)
Return value: New reference. Encode the Py_UNICODE buffer of the given size using Latin-1 and return
a Python string object. Return NULL if an exception was raised by the codec.
Changed in version 2.5: This function used an int type for size. This might require changes in your
code for properly supporting 64-bit systems.
PyObject* PyUnicode_AsLatin1String(PyObject *unicode)
Return value: New reference. Encode a Unicode object using Latin-1 and return the result as Python
string object. Error handling is “strict”. Return NULL if an exception was raised by the codec.
ASCII Codecs
These are the ASCII codec APIs. Only 7-bit ASCII data is accepted. All other codes generate errors.
PyObject* PyUnicode_DecodeASCII(const char *s, Py_ssize_t size, const char *errors)
Return value: New reference. Create a Unicode object by decoding size bytes of the ASCII encoded
string s. Return NULL if an exception was raised by the codec.
Changed in version 2.5: This function used an int type for size. This might require changes in your
code for properly supporting 64-bit systems.
PyObject* PyUnicode_EncodeASCII(const Py_UNICODE *s, Py_ssize_t size, const char *errors)
Return value: New reference. Encode the Py_UNICODE buffer of the given size using ASCII and return
a Python string object. Return NULL if an exception was raised by the codec.
Changed in version 2.5: This function used an int type for size. This might require changes in your
code for properly supporting 64-bit systems.
PyObject* PyUnicode_AsASCIIString(PyObject *unicode)
Return value: New reference. Encode a Unicode object using ASCII and return the result as Python
string object. Error handling is “strict”. Return NULL if an exception was raised by the codec.
This codec is special in that it can be used to implement many different codecs (and this is in fact what was
done to obtain most of the standard codecs included in the encodings package). The codec uses mapping
to encode and decode characters.
Decoding mappings must map single string characters to single Unicode characters, integers (which are then
interpreted as Unicode ordinals) or None (meaning “undefined mapping” and causing an error).
Encoding mappings must map single Unicode characters to single string characters, integers (which are then
interpreted as Latin-1 ordinals) or None (meaning “undefined mapping” and causing an error).
The mapping objects provided must only support the __getitem__ mapping interface.
If a character lookup fails with a LookupError, the character is copied as-is meaning that its ordinal value
will be interpreted as Unicode or Latin-1 ordinal resp. Because of this, mappings only need to contain those
mappings which map characters to different code points.
These are the mapping codec APIs:
PyObject* PyUnicode_DecodeCharmap(const char *s, Py_ssize_t size, PyObject *mapping, const
char *errors)
Return value: New reference. Create a Unicode object by decoding size bytes of the encoded string s
using the given mapping object. Return NULL if an exception was raised by the codec. If mapping
is NULL latin-1 decoding will be done. Else it can be a dictionary mapping byte or a unicode string,
which is treated as a lookup table. Byte values greater that the length of the string and U+FFFE
“characters” are treated as “undefined mapping”.
Changed in version 2.4: Allowed unicode string as mapping argument.
Changed in version 2.5: This function used an int type for size. This might require changes in your
code for properly supporting 64-bit systems.
PyObject* PyUnicode_EncodeCharmap(const Py_UNICODE *s, Py_ssize_t size, PyOb-
ject *mapping, const char *errors)
Return value: New reference. Encode the Py_UNICODE buffer of the given size using the given mapping
object and return a Python string object. Return NULL if an exception was raised by the codec.
Changed in version 2.5: This function used an int type for size. This might require changes in your
code for properly supporting 64-bit systems.
PyObject* PyUnicode_AsCharmapString(PyObject *unicode, PyObject *mapping)
Return value: New reference. Encode a Unicode object using the given mapping object and return the
result as Python string object. Error handling is “strict”. Return NULL if an exception was raised by
the codec.
The following codec API is special in that maps Unicode to Unicode.
PyObject* PyUnicode_TranslateCharmap(const Py_UNICODE *s, Py_ssize_t size, PyOb-
ject *table, const char *errors)
Return value: New reference. Translate a Py_UNICODE buffer of the given size by applying a character
mapping table to it and return the resulting Unicode object. Return NULL when an exception was
raised by the codec.
The mapping table must map Unicode ordinal integers to Unicode ordinal integers or None (causing
deletion of the character).
Mapping tables need only provide the __getitem__() interface; dictionaries and sequences work well.
Unmapped character ordinals (ones which cause a LookupError) are left untouched and are copied
as-is.
Changed in version 2.5: This function used an int type for size. This might require changes in your
code for properly supporting 64-bit systems.
These are the MBCS codec APIs. They are currently only available on Windows and use the Win32 MBCS
converters to implement the conversions. Note that MBCS (or DBCS) is a class of encodings, not just one.
The target encoding is defined by the user settings on the machine running the codec.
PyObject* PyUnicode_DecodeMBCS(const char *s, Py_ssize_t size, const char *errors)
Return value: New reference. Create a Unicode object by decoding size bytes of the MBCS encoded
string s. Return NULL if an exception was raised by the codec.
Changed in version 2.5: This function used an int type for size. This might require changes in your
code for properly supporting 64-bit systems.
PyObject* PyUnicode_DecodeMBCSStateful(const char *s, int size, const char *errors,
int *consumed)
If consumed is NULL, behave like PyUnicode_DecodeMBCS(). If consumed is not NULL,
PyUnicode_DecodeMBCSStateful() will not decode trailing lead byte and the number of bytes that
have been decoded will be stored in consumed.
New in version 2.5.
PyObject* PyUnicode_EncodeMBCS(const Py_UNICODE *s, Py_ssize_t size, const char *errors)
Return value: New reference. Encode the Py_UNICODE buffer of the given size using MBCS and return
a Python string object. Return NULL if an exception was raised by the codec.
Changed in version 2.5: This function used an int type for size. This might require changes in your
code for properly supporting 64-bit systems.
PyObject* PyUnicode_AsMBCSString(PyObject *unicode)
Return value: New reference. Encode a Unicode object using MBCS and return the result as Python
string object. Error handling is “strict”. Return NULL if an exception was raised by the codec.
The following APIs are capable of handling Unicode objects and strings on input (we refer to them as strings
in the descriptions) and return Unicode objects or integers as appropriate.
They all return NULL or -1 if an exception occurs.
PyObject* PyUnicode_Concat(PyObject *left, PyObject *right)
Return value: New reference. Concat two strings giving a new Unicode string.
PyObject* PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit)
Return value: New reference. Split a string giving a list of Unicode strings. If sep is NULL, splitting
will be done at all whitespace substrings. Otherwise, splits occur at the given separator. At most
maxsplit splits will be done. If negative, no limit is set. Separators are not included in the resulting
list.
Changed in version 2.5: This function used an int type for maxsplit. This might require changes in
your code for properly supporting 64-bit systems.
PyObject* PyUnicode_Splitlines(PyObject *s, int keepend)
Return value: New reference. Split a Unicode string at line breaks, returning a list of Unicode strings.
CRLF is considered to be one line break. If keepend is 0, the Line break characters are not included
in the resulting strings.
PyObject* PyUnicode_Translate(PyObject *str, PyObject *table, const char *errors)
Return value: New reference. Translate a string by applying a character mapping table to it and return
the resulting Unicode object.
The mapping table must map Unicode ordinal integers to Unicode ordinal integers or None (causing
deletion of the character).
Mapping tables need only provide the __getitem__() interface; dictionaries and sequences work well.
Unmapped character ordinals (ones which cause a LookupError) are left untouched and are copied
as-is.
errors has the usual meaning for codecs. It may be NULL which indicates to use the default error
handling.
PyObject* PyUnicode_Join(PyObject *separator, PyObject *seq)
Return value: New reference. Join a sequence of strings using the given separator and return the
resulting Unicode string.
Py_ssize_t PyUnicode_Tailmatch(PyObject *str, PyObject *substr, Py_ssize_t start,
Py_ssize_t end, int direction)
Return 1 if substr matches str[start:end] at the given tail end (direction == -1 means to do a
prefix match, direction == 1 a suffix match), 0 otherwise. Return -1 if an error occurred.
Changed in version 2.5: This function used an int type for start and end. This might require changes
in your code for properly supporting 64-bit systems.
Py_ssize_t PyUnicode_Find(PyObject *str, PyObject *substr, Py_ssize_t start, Py_ssize_t end,
int direction)
Return the first position of substr in str[start:end] using the given direction (direction == 1 means
to do a forward search, direction == -1 a backward search). The return value is the index of the first
match; a value of -1 indicates that no match was found, and -2 indicates that an error occurred and
an exception has been set.
Changed in version 2.5: This function used an int type for start and end. This might require changes
in your code for properly supporting 64-bit systems.
Py_ssize_t PyUnicode_Count(PyObject *str, PyObject *substr, Py_ssize_t start, Py_ssize_t end)
Return the number of non-overlapping occurrences of substr in str[start:end]. Return -1 if an error
occurred.
Changed in version 2.5: This function returned an int type and used an int type for start and end.
This might require changes in your code for properly supporting 64-bit systems.
PyObject* PyUnicode_Replace(PyObject *str, PyObject *substr, PyObject *replstr,
Py_ssize_t maxcount)
Return value: New reference. Replace at most maxcount occurrences of substr in str with replstr and
return the resulting Unicode object. maxcount == -1 means replace all occurrences.
Changed in version 2.5: This function used an int type for maxcount. This might require changes in
your code for properly supporting 64-bit systems.
int PyUnicode_Compare(PyObject *left, PyObject *right)
Compare two strings and return -1, 0, 1 for less than, equal, and greater than, respectively.
int PyUnicode_RichCompare(PyObject *left, PyObject *right, int op)
Rich compare two unicode strings and return one of the following:
• NULL in case an exception was raised
• Py_True or Py_False for successful comparisons
• Py_NotImplemented in case the type combination is unknown
Note that Py_EQ and Py_NE comparisons can cause a UnicodeWarning in case the conversion of the
arguments to Unicode fails with a UnicodeDecodeError.
Possible values for op are Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, and Py_LE.
Py_buffer
void *buf
A pointer to the start of the memory for the object.
Py_ssize_t len
The total length of the memory in bytes.
int readonly
An indicator of whether the buffer is read only.
const char *format
A NULL terminated string in struct module style syntax giving the contents of the elements
available through the buffer. If this is NULL, "B" (unsigned bytes) is assumed.
int ndim
The number of dimensions the memory represents as a multi-dimensional array. If it is 0, strides
and suboffsets must be NULL.
Py_ssize_t *shape
An array of Py_ssize_ts the length of ndim giving the shape of the memory as a multi-dimensional
array. Note that ((*shape)[0] * ... * (*shape)[ndims-1])*itemsize should be equal to
len.
Py_ssize_t *strides
An array of Py_ssize_ts the length of ndim giving the number of bytes to skip to get to a new
element in each dimension.
Py_ssize_t *suboffsets
An array of Py_ssize_ts the length of ndim. If these suboffset numbers are greater than or equal
to 0, then the value stored along the indicated dimension is a pointer and the suboffset value
dictates how many bytes to add to the pointer after de-referencing. A suboffset value that it
negative indicates that no de-referencing should occur (striding in a contiguous memory block).
If all suboffsets are negative (i.e. no de-referencing is needed, then this field must be NULL (the
default value).
Here is a function that returns a pointer to the element in an N-D array pointed to by an N-
dimensional index when there are both non-NULL strides and suboffsets:
Py_ssize_t itemsize
This is a storage for the itemsize (in bytes) of each element of the shared memory. It is technically
un-necessary as it can be obtained using PyBuffer_SizeFromFormat(), however an exporter may
know this information without parsing the format string and it is necessary to know the itemsize
for proper interpretation of striding. Therefore, storing it is more convenient and faster.
void *internal
This is for use internally by the exporting object. For example, this might be re-cast as an integer
by the exporter and used to store flags about whether or not the shape, strides, and suboffsets
arrays must be freed when the buffer is released. The consumer should never alter this value.
int PyObject_CheckBuffer(PyObject *obj)
Return 1 if obj supports the buffer interface otherwise 0.
int PyObject_GetBuffer(PyObject *obj, Py_buffer *view, int flags)
Export obj into a Py_buffer, view. These arguments must never be NULL. The flags argument is a
bit field indicating what kind of buffer the caller is prepared to deal with and therefore what kind of
buffer the exporter is allowed to return. The buffer interface allows for complicated memory sharing
possibilities, but some caller may not be able to handle all the complexity but may want to see if the
exporter will let them take a simpler view to its memory.
Some exporters may not be able to share memory in every possible way and may need to raise errors to
signal to some consumers that something is just not possible. These errors should be a BufferError
unless there is another error that is actually causing the problem. The exporter can use flags information
to simplify how much of the Py_buffer structure is filled in with non-default values and/or raise an
error if the object can’t support a simpler view of its memory.
0 is returned on success and -1 on error.
Flag Description
PyBUF_SIMPLE This is the default flag state. The returned buffer may or may not have writable
memory. The format of the data will be assumed to be unsigned bytes. This is a
“stand-alone” flag constant. It never needs to be ‘|’d to the others. The exporter
will raise an error if it cannot provide such a contiguous buffer of bytes.
PyBUF_WRITABLE The returned buffer must be writable. If it is not writable, then raise an error.
PyBUF_STRIDES This implies PyBUF_ND. The returned buffer must provide strides information (i.e.
the strides cannot be NULL). This would be used when the consumer can handle
strided, discontiguous arrays. Handling strides automatically assumes you can
handle shape. The exporter can raise an error if a strided representation of the
data is not possible (i.e. without the suboffsets).
PyBUF_ND The returned buffer must provide shape information. The memory will be assumed
C-style contiguous (last dimension varies the fastest). The exporter may raise an
error if it cannot provide this kind of contiguous buffer. If this is not given then
shape will be NULL.
These flags indicate that the contiguity returned buffer must be respectively, C-
PyBUF_C_CONTIGUOUS
contiguous (last dimension varies the fastest), Fortran contiguous (first dimension
PyBUF_F_CONTIGUOUS
varies the fastest) or either one. All of these flags imply PyBUF_STRIDES and
PyBUF_ANY_CONTIGUOUS
guarantee that the strides buffer info structure will be filled in correctly.
PyBUF_INDIRECT This flag indicates the returned buffer must have suboffsets information (which
can be NULL if no suboffsets are needed). This can be used when the consumer
can handle indirect array referencing implied by these suboffsets. This implies
PyBUF_STRIDES.
PyBUF_FORMAT The returned buffer must have true format information if this flag is provided.
This would be used when the consumer is going to be checking for what ‘kind’
of data is actually stored. An exporter should always be able to provide this
information if requested. If format is not explicitly requested then the format
must be returned as NULL (which means 'B', or unsigned bytes)
PyBUF_STRIDED This is equivalent to (PyBUF_STRIDES | PyBUF_WRITABLE).
This is equivalent to (PyBUF_STRIDES).
PyBUF_STRIDED_RO
PyBUF_RECORDS This is equivalent to (PyBUF_STRIDES | PyBUF_FORMAT | PyBUF_WRITABLE).
This is equivalent to (PyBUF_STRIDES | PyBUF_FORMAT).
PyBUF_RECORDS_RO
PyBUF_FULL This is equivalent to (PyBUF_INDIRECT | PyBUF_FORMAT | PyBUF_WRITABLE).
PyBUF_FULL_RO This is equivalent to (PyBUF_INDIRECT | PyBUF_FORMAT).
PyBUF_CONTIG This is equivalent to (PyBUF_ND | PyBUF_WRITABLE).
PyBUF_CONTIG_ROThis is equivalent to (PyBUF_ND).
void PyBuffer_Release(Py_buffer *view)
Release the buffer view. This should be called when the buffer is no longer being used as it may free
memory from it.
Py_ssize_t PyBuffer_SizeFromFormat(const char *)
Return the implied itemsize from the struct-stype format.
int PyBuffer_IsContiguous(Py_buffer *view, char fortran)
Return 1 if the memory defined by the view is C-style (fortran is 'C') or Fortran-style (fortran is 'F')
contiguous or either one (fortran is 'A'). Return 0 otherwise.
void PyBuffer_FillContiguousStrides(int ndims, Py_ssize_t *shape, Py_ssize_t *strides,
int itemsize, char fortran)
Fill the strides array with byte-strides of a contiguous (C-style if fortran is 'C' or Fortran-style if
fortran is 'F') array of the given shape with the given number of bytes per element.
MemoryView objects
More information on the old buffer interface is provided in the section Buffer Object Structures, under the
description for PyBufferProcs.
A “buffer object” is defined in the bufferobject.h header (included by Python.h). These objects look very
similar to string objects at the Python programming level: they support slicing, indexing, concatenation,
and some other standard string operations. However, their data can come from one of two sources: from a
block of memory, or from another object which exports the buffer interface.
Buffer objects are useful as a way to expose the data from another object’s buffer interface to the Python
programmer. They can also be used as a zero-copy slicing mechanism. Using their ability to reference a
block of memory, it is possible to expose any data to the Python programmer quite easily. The memory
could be a large, constant array in a C extension, it could be a raw block of memory for manipulation before
passing to an operating system library, or it could be used to pass around structured data in its native,
in-memory format.
PyBufferObject
This subtype of PyObject represents a buffer object.
PyTypeObject PyBuffer_Type
The instance of PyTypeObject which represents the Python buffer type; it is the same object as buffer
and types.BufferType in the Python layer. .
int Py_END_OF_BUFFER
This constant may be passed as the size parameter to PyBuffer_FromObject() or
PyBuffer_FromReadWriteObject(). It indicates that the new PyBufferObject should refer to
base object from the specified offset to the end of its exported buffer. Using this enables the caller to
avoid querying the base object for its length.
int PyBuffer_Check(PyObject *p)
Return true if the argument has type PyBuffer_Type.
PyObject* PyBuffer_FromObject(PyObject *base, Py_ssize_t offset, Py_ssize_t size)
Return value: New reference. Return a new read-only buffer object. This raises TypeError if base
doesn’t support the read-only buffer protocol or doesn’t provide exactly one buffer segment, or it raises
ValueError if offset is less than zero. The buffer will hold a reference to the base object, and the
buffer’s contents will refer to the base object’s buffer interface, starting as position offset and extending
for size bytes. If size is Py_END_OF_BUFFER, then the new buffer’s contents extend to the length of the
base object’s exported buffer data.
Changed in version 2.5: This function used an int type for offset and size. This might require changes
in your code for properly supporting 64-bit systems.
PyObject* PyBuffer_FromReadWriteObject(PyObject *base, Py_ssize_t offset, Py_ssize_t size)
Return value: New reference. Return a new writable buffer object. Parameters and exceptions are
similar to those for PyBuffer_FromObject(). If the base object does not export the writeable buffer
protocol, then TypeError is raised.
Changed in version 2.5: This function used an int type for offset and size. This might require changes
in your code for properly supporting 64-bit systems.
PyObject* PyBuffer_FromMemory(void *ptr, Py_ssize_t size)
Return value: New reference. Return a new read-only buffer object that reads from a specified location
in memory, with a specified size. The caller is responsible for ensuring that the memory buffer, passed
in as ptr, is not deallocated while the returned buffer object exists. Raises ValueError if size is less
than zero. Note that Py_END_OF_BUFFER may not be passed for the size parameter; ValueError will
be raised in that case.
Changed in version 2.5: This function used an int type for size. This might require changes in your
code for properly supporting 64-bit systems.
PyObject* PyBuffer_FromReadWriteMemory(void *ptr, Py_ssize_t size)
Return value: New reference. Similar to PyBuffer_FromMemory(), but the returned buffer is writable.
Changed in version 2.5: This function used an int type for size. This might require changes in your
code for properly supporting 64-bit systems.
PyObject* PyBuffer_New(Py_ssize_t size)
Return value: New reference. Return a new writable buffer object that maintains its own memory
buffer of size bytes. ValueError is returned if size is not zero or positive. Note that the memory buffer
(as returned by PyObject_AsWriteBuffer()) is not specifically aligned.
Changed in version 2.5: This function used an int type for size. This might require changes in your
code for properly supporting 64-bit systems.
int PyTuple_Check(PyObject *p)
Return true if p is a tuple object or an instance of a subtype of the tuple type.
Changed in version 2.2: Allowed subtypes to be accepted.
int PyTuple_CheckExact(PyObject *p)
Return true if p is a tuple object, but not an instance of a subtype of the tuple type.
New in version 2.2.
PyObject* PyTuple_New(Py_ssize_t len)
Return value: New reference. Return a new tuple object of size len, or NULL on failure.
Changed in version 2.5: This function used an int type for len. This might require changes in your
code for properly supporting 64-bit systems.
PyObject* PyTuple_Pack(Py_ssize_t n, ...)
Return value: New reference. Return a new tuple object of size n, or NULL on failure. The tuple
values are initialized to the subsequent n C arguments pointing to Python objects. PyTuple_Pack(2,
a, b) is equivalent to Py_BuildValue("(OO)", a, b).
New in version 2.4.
Changed in version 2.5: This function used an int type for n. This might require changes in your code
for properly supporting 64-bit systems.
Py_ssize_t PyTuple_Size(PyObject *p)
Take a pointer to a tuple object, and return the size of that tuple.
Changed in version 2.5: This function returned an int type. This might require changes in your code
for properly supporting 64-bit systems.
Py_ssize_t PyTuple_GET_SIZE(PyObject *p)
Return the size of the tuple p, which must be non-NULL and point to a tuple; no error checking is
performed.
Changed in version 2.5: This function returned an int type. This might require changes in your code
for properly supporting 64-bit systems.
PyObject* PyTuple_GetItem(PyObject *p, Py_ssize_t pos)
Return value: Borrowed reference. Return the object at position pos in the tuple pointed to by p. If
pos is out of bounds, return NULL and sets an IndexError exception.
Changed in version 2.5: This function used an int type for pos. This might require changes in your
code for properly supporting 64-bit systems.
PyObject* PyTuple_GET_ITEM(PyObject *p, Py_ssize_t pos)
Return value: Borrowed reference. Like PyTuple_GetItem(), but does no checking of its arguments.
Changed in version 2.5: This function used an int type for pos. This might require changes in your
code for properly supporting 64-bit systems.
PyObject* PyTuple_GetSlice(PyObject *p, Py_ssize_t low, Py_ssize_t high)
Return value: New reference. Take a slice of the tuple pointed to by p from low to high and return it
as a new tuple.
Changed in version 2.5: This function used an int type for low and high. This might require changes
in your code for properly supporting 64-bit systems.
int PyTuple_SetItem(PyObject *p, Py_ssize_t pos, PyObject *o)
Insert a reference to object o at position pos of the tuple pointed to by p. Return 0 on success.
Changed in version 2.5: This function used an int type for pos. This might require changes in your
code for properly supporting 64-bit systems.
void PyTuple_SET_ITEM(PyObject *p, Py_ssize_t pos, PyObject *o)
Like PyTuple_SetItem(), but does no error checking, and should only be used to fill in brand new
tuples.
Changed in version 2.5: This function used an int type for pos. This might require changes in your
code for properly supporting 64-bit systems.
int _PyTuple_Resize(PyObject **p, Py_ssize_t newsize)
Can be used to resize a tuple. newsize will be the new length of the tuple. Because tuples are supposed
to be immutable, this should only be used if there is only one reference to the object. Do not use this if
the tuple may already be known to some other part of the code. The tuple will always grow or shrink
at the end. Think of this as destroying the old tuple and creating a new one, only more efficiently.
Returns 0 on success. Client code should never assume that the resulting value of *p will be the same
as before calling this function. If the object referenced by *p is replaced, the original *p is destroyed.
On failure, returns -1 and sets *p to NULL, and raises MemoryError or SystemError.
Changed in version 2.2: Removed unused third parameter, last_is_sticky.
Changed in version 2.5: This function used an int type for newsize. This might require changes in
your code for properly supporting 64-bit systems.
int PyTuple_ClearFreeList()
Clear the free list. Return the total number of freed items.
New in version 2.6.
Note: If len is greater than zero, the returned list object’s items are set to NULL. Thus you cannot
use abstract API functions such as PySequence_SetItem() or expose the object to Python code before
setting all items to a real object with PyList_SetItem().
Changed in version 2.5: This function used an int for size. This might require changes in your code
for properly supporting 64-bit systems.
Py_ssize_t PyList_Size(PyObject *list)
Return the length of the list object in list; this is equivalent to len(list) on a list object.
Changed in version 2.5: This function returned an int. This might require changes in your code for
properly supporting 64-bit systems.
Py_ssize_t PyList_GET_SIZE(PyObject *list)
Macro form of PyList_Size() without error checking.
Changed in version 2.5: This macro returned an int. This might require changes in your code for
properly supporting 64-bit systems.
PyObject* PyList_GetItem(PyObject *list, Py_ssize_t index)
Return value: Borrowed reference. Return the object at position index in the list pointed to by list.
The position must be positive, indexing from the end of the list is not supported. If index is out of
bounds, return NULL and set an IndexError exception.
Changed in version 2.5: This function used an int for index. This might require changes in your code
for properly supporting 64-bit systems.
PyObject* PyList_GET_ITEM(PyObject *list, Py_ssize_t i)
Return value: Borrowed reference. Macro form of PyList_GetItem() without error checking.
Changed in version 2.5: This macro used an int for i. This might require changes in your code for
properly supporting 64-bit systems.
int PyList_SetItem(PyObject *list, Py_ssize_t index, PyObject *item)
Set the item at index index in list to item. Return 0 on success or -1 on failure.
Note: This function “steals” a reference to item and discards a reference to an item already in the
list at the affected position.
Changed in version 2.5: This function used an int for index. This might require changes in your code
for properly supporting 64-bit systems.
void PyList_SET_ITEM(PyObject *list, Py_ssize_t i, PyObject *o)
Macro form of PyList_SetItem() without error checking. This is normally only used to fill in new
lists where there is no previous content.
Note: This macro “steals” a reference to item, and, unlike PyList_SetItem(), does not discard a
reference to any item that it being replaced; any reference in list at position i will be leaked.
Changed in version 2.5: This macro used an int for i. This might require changes in your code for
properly supporting 64-bit systems.
int PyList_Insert(PyObject *list, Py_ssize_t index, PyObject *item)
Insert the item item into list list in front of index index. Return 0 if successful; return -1 and set an
exception if unsuccessful. Analogous to list.insert(index, item).
Changed in version 2.5: This function used an int for index. This might require changes in your code
for properly supporting 64-bit systems.
int PyList_Append(PyObject *list, PyObject *item)
Append the object item at the end of list list. Return 0 if successful; return -1 and set an exception
if unsuccessful. Analogous to list.append(item).
PyObject* PyList_GetSlice(PyObject *list, Py_ssize_t low, Py_ssize_t high)
Return value: New reference. Return a list of the objects in list containing the objects between low and
high. Return NULL and set an exception if unsuccessful. Analogous to list[low:high]. Negative
indices, as when slicing from Python, are not supported.
Changed in version 2.5: This function used an int for low and high. This might require changes in
your code for properly supporting 64-bit systems.
int PyList_SetSlice(PyObject *list, Py_ssize_t low, Py_ssize_t high, PyObject *itemlist)
Set the slice of list between low and high to the contents of itemlist. Analogous to list[low:high]
= itemlist. The itemlist may be NULL, indicating the assignment of an empty list (slice deletion).
Return 0 on success, -1 on failure. Negative indices, as when slicing from Python, are not supported.
Changed in version 2.5: This function used an int for low and high. This might require changes in
your code for properly supporting 64-bit systems.
int PyList_Sort(PyObject *list)
Sort the items of list in place. Return 0 on success, -1 on failure. This is equivalent to list.sort().
int PyList_Reverse(PyObject *list)
Reverse the items of list in place. Return 0 on success, -1 on failure. This is the equivalent of
list.reverse().
PyObject* PyList_AsTuple(PyObject *list)
Return value: New reference. Return a new tuple object containing the contents of list; equivalent to
tuple(list).
PyObject* PyDict_Copy(PyObject *p)
Return value: New reference. Return a new dictionary that contains the same key-value pairs as p.
New in version 1.6.
int PyDict_SetItem(PyObject *p, PyObject *key, PyObject *val)
Insert value into the dictionary p with a key of key. key must be hashable; if it isn’t, TypeError will
be raised. Return 0 on success or -1 on failure.
int PyDict_SetItemString(PyObject *p, const char *key, PyObject *val)
Insert value into the dictionary p using key as a key. key should be a char*. The key object is created
using PyString_FromString(key). Return 0 on success or -1 on failure.
int PyDict_DelItem(PyObject *p, PyObject *key)
Remove the entry in dictionary p with key key. key must be hashable; if it isn’t, TypeError is raised.
Return 0 on success or -1 on failure.
int PyDict_DelItemString(PyObject *p, char *key)
Remove the entry in dictionary p which has a key specified by the string key. Return 0 on success or
-1 on failure.
PyObject* PyDict_GetItem(PyObject *p, PyObject *key)
Return value: Borrowed reference. Return the object from dictionary p which has a key key. Return
NULL if the key key is not present, but without setting an exception.
PyObject* PyDict_GetItemString(PyObject *p, const char *key)
Return value: Borrowed reference. This is the same as PyDict_GetItem(), but key is specified as a
char*, rather than a PyObject*.
PyObject* PyDict_Items(PyObject *p)
Return value: New reference. Return a PyListObject containing all the items from the dictionary, as
in the dictionary method dict.items().
PyObject* PyDict_Keys(PyObject *p)
Return value: New reference. Return a PyListObject containing all the keys from the dictionary, as
in the dictionary method dict.keys().
PyObject* PyDict_Values(PyObject *p)
Return value: New reference. Return a PyListObject containing all the values from the dictionary p,
as in the dictionary method dict.values().
Py_ssize_t PyDict_Size(PyObject *p)
Return the number of items in the dictionary. This is equivalent to len(p) on a dictionary.
Changed in version 2.5: This function returned an int type. This might require changes in your code
for properly supporting 64-bit systems.
int PyDict_Next(PyObject *p, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Iterate over all key-value pairs in the dictionary p. The Py_ssize_t referred to by ppos must be
initialized to 0 prior to the first call to this function to start the iteration; the function returns true
for each pair in the dictionary, and false once all pairs have been reported. The parameters pkey
and pvalue should either point to PyObject* variables that will be filled in with each key and value,
respectively, or may be NULL. Any references returned through them are borrowed. ppos should not
be altered during iteration. Its value represents offsets within the internal dictionary structure, and
since the structure is sparse, the offsets are not consecutive.
For example:
PyObject *key, *value;
Py_ssize_t pos = 0;
The dictionary p should not be mutated during iteration. It is safe (since Python 2.1) to modify the
values of the keys as you iterate over the dictionary, but only so long as the set of keys does not change.
For example:
Changed in version 2.5: This function used an int * type for ppos. This might require changes in your
code for properly supporting 64-bit systems.
int PyDict_Merge(PyObject *a, PyObject *b, int override)
Iterate over mapping object b adding key-value pairs to dictionary a. b may be a dictionary, or any
object supporting PyMapping_Keys() and PyObject_GetItem(). If override is true, existing pairs in
a will be replaced if a matching key is found in b, otherwise pairs will only be added if there is not a
matching key in a. Return 0 on success or -1 if an exception was raised.
New in version 2.2.
int PyDict_Update(PyObject *a, PyObject *b)
This is the same as PyDict_Merge(a, b, 1) in C, and is similar to a.update(b) in Python except
that PyDict_Update() doesn’t fall back to the iterating over a sequence of key value pairs if the second
argument has no “keys” attribute. Return 0 on success or -1 if an exception was raised.
New in version 2.2.
int PyDict_MergeFromSeq2(PyObject *a, PyObject *seq2, int override)
Update or merge into dictionary a, from the key-value pairs in seq2. seq2 must be an iterable object
producing iterable objects of length 2, viewed as key-value pairs. In case of duplicate keys, the last wins
if override is true, else the first wins. Return 0 on success or -1 if an exception was raised. Equivalent
Python (except for the return value):
PyObject* PyFunction_GetGlobals(PyObject *op)
Return value: Borrowed reference. Return the globals dictionary associated with the function object
op.
PyObject* PyFunction_GetModule(PyObject *op)
Return value: Borrowed reference. Return the __module__ attribute of the function object op. This
is normally a string containing the module name, but can be set to any other object by Python code.
PyObject* PyFunction_GetDefaults(PyObject *op)
Return value: Borrowed reference. Return the argument default values of the function object op. This
can be a tuple of arguments or NULL.
int PyFunction_SetDefaults(PyObject *op, PyObject *defaults)
Set the argument default values for the function object op. defaults must be Py_None or a tuple.
Raises SystemError and returns -1 on failure.
PyObject* PyFunction_GetClosure(PyObject *op)
Return value: Borrowed reference. Return the closure associated with the function object op. This
can be NULL or a tuple of cell objects.
int PyFunction_SetClosure(PyObject *op, PyObject *closure)
Set the closure associated with the function object op. closure must be Py_None or a tuple of cell
objects.
Raises SystemError and returns -1 on failure.
PyObject* PyMethod_GET_SELF(PyObject *meth)
Return value: Borrowed reference. Macro version of PyMethod_Self() which avoids error checking.
int PyMethod_ClearFreeList()
Clear the free list. Return the total number of freed items.
New in version 2.6.
PyTypeObject PyModule_Type
This instance of PyTypeObject represents the Python module type. This is exposed to Python pro-
grams as types.ModuleType.
int PyModule_Check(PyObject *p)
Return true if p is a module object, or a subtype of a module object.
Changed in version 2.2: Allowed subtypes to be accepted.
int PyModule_CheckExact(PyObject *p)
Return true if p is a module object, but not a subtype of PyModule_Type.
New in version 2.2.
PyObject* PyModule_New(const char *name)
Return value: New reference. 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 value: Borrowed reference. 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. It
is recommended extensions use other PyModule_*() and PyObject_*() functions rather than directly
manipulate a module’s __dict__.
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, const 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. Return -1 on error, 0 on success.
New in version 2.0.
int PyModule_AddIntConstant(PyObject *module, const char *name, long value)
Add an integer constant to module as name. This convenience function can be used from the module’s
initialization function. Return -1 on error, 0 on success.
New in version 2.0.
int PyModule_AddStringConstant(PyObject *module, const char *name, const 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. Return -1 on error, 0 on success.
New in version 2.0.
int PyModule_AddIntMacro(PyObject *module, macro)
Add an int constant to module. The name and the value are taken from macro. For exam-
ple PyModule_AddIntMacro(module, AF_INET) adds the int constant AF_INET with the value of
AF_INET to module. Return -1 on error, 0 on success.
New in version 2.6.
int PyModule_AddStringMacro(PyObject *module, macro)
Add a string constant to module.
New in version 2.6.
Warning: This function returns a borrowed reference to the referenced object. This means that
you should always call Py_INCREF() on the object except if you know that it cannot be destroyed
while you are still using it.
PyObject* PyWeakref_GET_OBJECT(PyObject *ref )
Return value: Borrowed reference. Similar to PyWeakref_GetObject(), but implemented as a macro
that does no error checking.
New in version 2.2.
7.5.11 Capsules
Refer to using-capsules for more information on using these objects.
Return the capsule’s internal pointer on success. On failure, set an exception and return NULL.
int PyCapsule_IsValid(PyObject *capsule, const char *name)
Determines whether or not capsule is a valid capsule. A valid capsule is non-NULL, passes
PyCapsule_CheckExact(), has a non-NULL pointer stored in it, and its internal name matches the
name parameter. (See PyCapsule_GetPointer() for information on how capsule names are compared.)
In other words, if PyCapsule_IsValid() returns a true value, calls to any of the accessors (any function
starting with PyCapsule_Get()) are guaranteed to succeed.
Return a nonzero value if the object is valid and matches the name passed in. Return 0 otherwise.
This function will not fail.
int PyCapsule_SetContext(PyObject *capsule, void *context)
Set the context pointer inside capsule to context.
Return 0 on success. Return nonzero and set an exception on failure.
int PyCapsule_SetDestructor(PyObject *capsule, PyCapsule_Destructor destructor)
Set the destructor inside capsule to destructor.
Return 0 on success. Return nonzero and set an exception on failure.
int PyCapsule_SetName(PyObject *capsule, const char *name)
Set the name inside capsule to name. If non-NULL, the name must outlive the capsule. If the previous
name stored in the capsule was not NULL, no attempt is made to free it.
Return 0 on success. Return nonzero and set an exception on failure.
int PyCapsule_SetPointer(PyObject *capsule, void *pointer)
Set the void pointer inside capsule to pointer. The pointer may not be NULL.
Return 0 on success. Return nonzero and set an exception on failure.
7.5.12 CObjects
Warning: The CObject API is deprecated as of Python 2.7. Please switch to the new Capsules API.
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)
Return true if its argument is a PyCObject.
PyObject* PyCObject_FromVoidPtr(void* cobj, void (*destr)(void *))
Return value: New reference. Create 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 *))
Return value: New reference. Create 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 )
Return the object void * that the PyCObject self was created with.
void* PyCObject_GetDesc(PyObject* self )
Return the description void * that the PyCObject self was created with.
PyObject* PyGen_New(PyFrameObject *frame)
Return value: New reference. Create and return a new generator object based on the frame object. A
reference to frame is stolen by this function. The parameter must not be NULL.
int PyTZInfo_CheckExact(PyObject *ob)
Return true if ob is of type PyDateTime_TZInfoType. ob must not be NULL.
New in version 2.4.
Macros to create objects:
PyObject* PyDate_FromDate(int year, int month, int day)
Return value: New reference. Return a datetime.date object with the specified year, month and day.
New in version 2.4.
PyObject* PyDateTime_FromDateAndTime(int year, int month, int day, int hour, int minute,
int second, int usecond)
Return value: New reference. Return a datetime.datetime object with the specified year, month,
day, hour, minute, second and microsecond.
New in version 2.4.
PyObject* PyTime_FromTime(int hour, int minute, int second, int usecond)
Return value: New reference. Return a datetime.time object with the specified hour, minute, second
and microsecond.
New in version 2.4.
PyObject* PyDelta_FromDSU(int days, int seconds, int useconds)
Return value: New reference. Return a datetime.timedelta object representing the given num-
ber of days, seconds and microseconds. Normalization is performed so that the resulting number of
microseconds and seconds lie in the ranges documented for datetime.timedelta objects.
New in version 2.4.
Macros to extract fields from date objects. The argument must be an instance of PyDateTime_Date, including
subclasses (such as PyDateTime_DateTime). The argument must not be NULL, and the type is not checked:
int PyDateTime_GET_YEAR(PyDateTime_Date *o)
Return the year, as a positive int.
New in version 2.4.
int PyDateTime_GET_MONTH(PyDateTime_Date *o)
Return the month, as an int from 1 through 12.
New in version 2.4.
int PyDateTime_GET_DAY(PyDateTime_Date *o)
Return the day, as an int from 1 through 31.
New in version 2.4.
Macros to extract fields from datetime objects. The argument must be an instance of PyDateTime_DateTime,
including subclasses. The argument must not be NULL, and the type is not checked:
int PyDateTime_DATE_GET_HOUR(PyDateTime_DateTime *o)
Return the hour, as an int from 0 through 23.
New in version 2.4.
int PyDateTime_DATE_GET_MINUTE(PyDateTime_DateTime *o)
Return the minute, as an int from 0 through 59.
New in version 2.4.
int PyDateTime_DATE_GET_SECOND(PyDateTime_DateTime *o)
Return the second, as an int from 0 through 59.
New in version 2.4.
int PyDateTime_DATE_GET_MICROSECOND(PyDateTime_DateTime *o)
Return the microsecond, as an int from 0 through 999999.
New in version 2.4.
Macros to extract fields from time objects. The argument must be an instance of PyDateTime_Time, including
subclasses. The argument must not be NULL, and the type is not checked:
int PyDateTime_TIME_GET_HOUR(PyDateTime_Time *o)
Return the hour, as an int from 0 through 23.
New in version 2.4.
int PyDateTime_TIME_GET_MINUTE(PyDateTime_Time *o)
Return the minute, as an int from 0 through 59.
New in version 2.4.
int PyDateTime_TIME_GET_SECOND(PyDateTime_Time *o)
Return the second, as an int from 0 through 59.
New in version 2.4.
int PyDateTime_TIME_GET_MICROSECOND(PyDateTime_Time *o)
Return the microsecond, as an int from 0 through 999999.
New in version 2.4.
Macros for the convenience of modules implementing the DB API:
PyObject* PyDateTime_FromTimestamp(PyObject *args)
Return value: New reference. Create and return a new datetime.datetime object given an argument
tuple suitable for passing to datetime.datetime.fromtimestamp().
New in version 2.4.
PyObject* PyDate_FromTimestamp(PyObject *args)
Return value: New reference. Create and return a new datetime.date object given an argument tuple
suitable for passing to datetime.date.fromtimestamp().
New in version 2.4.
PyTypeObject PyFrozenSet_Type
This is an instance of PyTypeObject representing the Python frozenset type.
The following type check macros work on pointers to any Python object. Likewise, the constructor functions
work with any iterable Python object.
int PySet_Check(PyObject *p)
Return true if p is a set object or an instance of a subtype.
New in version 2.6.
int PyFrozenSet_Check(PyObject *p)
Return true if p is a frozenset object or an instance of a subtype.
New in version 2.6.
int PyAnySet_Check(PyObject *p)
Return true if p is a set object, a frozenset object, or an instance of a subtype.
int PyAnySet_CheckExact(PyObject *p)
Return true if p is a set object or a frozenset object but not an instance of a subtype.
int PyFrozenSet_CheckExact(PyObject *p)
Return true if p is a frozenset object but not an instance of a subtype.
PyObject* PySet_New(PyObject *iterable)
Return value: New reference. Return a new set containing objects returned by the iterable. The
iterable may be NULL to create a new empty set. Return the new set on success or NULL on failure.
Raise TypeError if iterable is not actually iterable. The constructor is also useful for copying a set
(c=set(s)).
PyObject* PyFrozenSet_New(PyObject *iterable)
Return value: New reference. Return a new frozenset containing objects returned by the iterable.
The iterable may be NULL to create a new empty frozenset. Return the new set on success or NULL
on failure. Raise TypeError if iterable is not actually iterable.
Changed in version 2.6: Now guaranteed to return a brand-new frozenset. Formerly, frozensets of
zero-length were a singleton. This got in the way of building-up new frozensets with PySet_Add().
The following functions and macros are available for instances of set or frozenset or instances of their
subtypes.
Py_ssize_t PySet_Size(PyObject *anyset)
Return the length of a set or frozenset object. Equivalent to len(anyset). Raises a
PyExc_SystemError if anyset is not a set, frozenset, or an instance of a subtype.
Changed in version 2.5: This function returned an int. This might require changes in your code for
properly supporting 64-bit systems.
Py_ssize_t PySet_GET_SIZE(PyObject *anyset)
Macro form of PySet_Size() without error checking.
int PySet_Contains(PyObject *anyset, PyObject *key)
Return 1 if found, 0 if not found, and -1 if an error is encountered. Unlike the Python __contains__()
method, this function does not automatically convert unhashable sets into temporary frozensets. Raise
a TypeError if the key is unhashable. Raise PyExc_SystemError if anyset is not a set, frozenset, or
an instance of a subtype.
int PySet_Add(PyObject *set, PyObject *key)
Add key to a set instance. Does not apply to frozenset instances. Return 0 on success or -1 on
failure. Raise a TypeError if the key is unhashable. Raise a MemoryError if there is no room to grow.
Raise a SystemError if set is not an instance of set or its subtype.
Changed in version 2.6: Now works with instances of frozenset or its subtypes. Like
PyTuple_SetItem() in that it can be used to fill-in the values of brand new frozensets before they are
exposed to other code.
The following functions are available for instances of set or its subtypes but not for instances of frozenset
or its subtypes.
int PySet_Discard(PyObject *set, PyObject *key)
Return 1 if found and removed, 0 if not found (no action taken), and -1 if an error is encountered.
Does not raise KeyError for missing keys. Raise a TypeError if the key is unhashable. Unlike the
Python discard() method, this function does not automatically convert unhashable sets into tempo-
rary frozensets. Raise PyExc_SystemError if set is not an instance of set or its subtype.
PyObject* PySet_Pop(PyObject *set)
Return value: New reference. Return a new reference to an arbitrary object in the set, and removes the
object from the set. Return NULL on failure. Raise KeyError if the set is empty. Raise a SystemError
if set is not an instance of set or its subtype.
int PySet_Clear(PyObject *set)
Empty an existing set of all elements.
EIGHT
111
The Python/C API, Release 2.7.15
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 compli-
cated 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-dependent 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 op-
erating 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 char-
acter is ':' on Unix and Mac OS X, ';' on Windows. The returned string points into static storage;
the caller should not modify its value. The list sys.path is initialized with this value on interpreter
startup; it can be (and usually is) modified later to change the search path for loading 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 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 Mac OS X, it is 'darwin'.
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 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 inter-
preter 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.
void PySys_SetArgvEx(int argc, char **argv, int updatepath)
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().
If updatepath is zero, this is all the function does. If updatepath is non-zero, the function also modifies
sys.path according to the following algorithm:
• If the name of an existing script is passed in argv[0], the absolute path of the directory where
the script is located is prepended to sys.path.
• Otherwise (that is, if argc is 0 or argv[0] doesn’t point to an existing file name), an empty string
is prepended to sys.path, which is the same as prepending the current working directory (".").
Note: It is recommended that applications embedding the Python interpreter for purposes other
than executing a single script pass 0 as updatepath, and update sys.path themselves if desired. See
CVE-2008-5983.
On versions before 2.6.6, you can achieve the same effect by manually popping the first sys.path
element after having called PySys_SetArgv(), for example using:
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. These two macros are still available when Python is com-
piled without thread support (they simply have an empty expansion).
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);
Here is how these functions work: 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.
Note: Calling system I/O functions is the most common use case for releasing the GIL, but it can also
be useful before calling long-running computations which don’t need access to Python objects, such as
compression or cryptographic functions operating over memory buffers. For example, the standard zlib and
hashlib modules release the GIL when compressing or hashing data.
PyGILState_STATE gstate;
gstate = PyGILState_Ensure();
Note that the PyGILState_*() functions assume there is only one global interpreter (created automatically by
Py_Initialize()). Python supports the creation of additional interpreters (using Py_NewInterpreter()),
Note: When only the main thread exists, no GIL 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 global interpreter 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 the calling thread 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.
int PyEval_ThreadsInitialized()
Returns a non-zero value if PyEval_InitThreads() has been called. This function can be called
without holding the GIL, and therefore can be used to avoid calls to the locking API when running
single-threaded. This function is not available when thread support is disabled at compile time.
New in version 2.4.
PyThreadState* PyEval_SaveThread()
Release the global 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 global 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.)
PyThreadState* PyThreadState_Get()
Return the current thread state. The global 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 global interpreter lock must be held and is not released.
void PyEval_ReInitThreads()
This function is called from PyOS_AfterFork() to ensure that newly created child processes don’t hold
locks referring to threads which are not running in the child process.
The following functions use thread-local storage, and are not compatible with sub-interpreters:
PyGILState_STATE PyGILState_Ensure()
Ensure that the current thread is ready to call the Python C API regardless of the current state of
Python, or of the global interpreter lock. This may be called as many times as desired by a thread as
long as each call is matched with a call to PyGILState_Release(). In general, other thread-related
APIs may be used between PyGILState_Ensure() and PyGILState_Release() calls as long as the
thread state is restored to its previous state before the Release(). For example, normal usage of the
Py_BEGIN_ALLOW_THREADS and Py_END_ALLOW_THREADS macros is acceptable.
The return value is an opaque “handle” to the thread state when PyGILState_Ensure() was called,
and must be passed to PyGILState_Release() to ensure Python is left in the same state. Even though
recursive calls are allowed, these handles cannot be shared - each unique call to PyGILState_Ensure()
must save the handle for its call to PyGILState_Release().
When the function returns, the current thread will hold the GIL and be able to call arbitrary Python
code. Failure is a fatal error.
New in version 2.3.
void PyGILState_Release(PyGILState_STATE)
Release any resources previously acquired. After this call, Python’s state will be the same as it was
prior to the corresponding PyGILState_Ensure() call (but generally this state will be unknown to the
caller, hence the use of the GILState API).
Every call to PyGILState_Ensure() must be matched by a call to PyGILState_Release() on the same
thread.
New in version 2.3.
PyThreadState* PyGILState_GetThisThreadState()
Get the current thread state for this thread. May return NULL if no GILState API has been used on the
current thread. Note that the main thread always has such a thread-state, even if no auto-thread-state
call has been made on the main thread. This is mainly a helper/diagnostic function.
Warning: This function does not change the current thread state. Please use
PyEval_RestoreThread() or PyEval_AcquireThread() instead.
void PyEval_ReleaseLock()
Release the global interpreter lock. The lock must have been created earlier.
Warning: This function does not change the current thread state. Please use
PyEval_SaveThread() or PyEval_ReleaseThread() instead.
The return value points to the first thread state created in the new sub-interpreter. This thread state
is made in 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.
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 interpreter 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.
• on a bytecode boundary;
• with the main thread holding the global interpreter lock (func can therefore use the full C API).
func must return 0 on success, or -1 on failure with an exception set. func won’t be interrupted to
perform another asynchronous notification recursively, but it can still be interrupted to switch threads
if the global interpreter lock is released.
This function doesn’t need a current thread state to run, and it doesn’t need the global interpreter
lock.
Warning: This is a low-level function, only useful for very special cases. There is no guarantee
that func will be called as quick as possible. If the main thread is busy executing a system call, func
won’t be called before the system call returns. This function is generally not suitable for calling
Python code from arbitrary C threads. Instead, use the PyGILState API .
int PyTrace_CALL
The value of the what parameter to a Py_tracefunc function when a new call to a function or method
is being reported, or a new entry into a generator. Note that the creation of the iterator for a generator
function is not reported as there is no control transfer to the Python bytecode in the corresponding
frame.
int PyTrace_EXCEPTION
The value of the what parameter to a Py_tracefunc function when an exception has been raised. The
callback function is called with this value for what when after any bytecode is processed after which
the exception becomes set within the frame being executed. The effect of this is that as exception
propagation causes the Python stack to unwind, the callback is called upon return to each frame as the
exception propagates. Only trace functions receives these events; they are not needed by the profiler.
int PyTrace_LINE
The value passed as the what parameter to a trace function (but not a profiling function) when a
line-number event is being reported.
int PyTrace_RETURN
The value for the what parameter to Py_tracefunc functions when a call is about to return.
int PyTrace_C_CALL
The value for the what parameter to Py_tracefunc functions when a C function is about to be called.
int PyTrace_C_EXCEPTION
The value for the what parameter to Py_tracefunc functions when a C function has raised an exception.
int PyTrace_C_RETURN
The value for the what parameter to Py_tracefunc functions when a C function has returned.
void PyEval_SetProfile(Py_tracefunc func, PyObject *obj)
Set the profiler function to func. The obj parameter is passed to the function as its first parameter,
and may be any Python object, or NULL. If the profile function needs to maintain state, using a
different value for obj for each thread provides a convenient and thread-safe place to store it. The
profile function is called for all monitored events except PyTrace_LINE and PyTrace_EXCEPTION.
void PyEval_SetTrace(Py_tracefunc func, PyObject *obj)
Set the tracing function to func. This is similar to PyEval_SetProfile(), except the tracing function
does receive line-number events and does not receive any event related to C function objects being
called. Any trace function registered using PyEval_SetTrace() will not receive PyTrace_C_CALL,
PyTrace_C_EXCEPTION or PyTrace_C_RETURN as a value for the what parameter.
PyObject* PyEval_GetCallStats(PyObject *self )
Return a tuple of function call counts. There are constants defined for the positions within the tuple:
Name Value
PCALL_ALL 0
PCALL_FUNCTION 1
PCALL_FAST_FUNCTION 2
PCALL_FASTER_FUNCTION 3
PCALL_METHOD 4
PCALL_BOUND_METHOD 5
PCALL_CFUNCTION 6
PCALL_TYPE 7
PCALL_GENERATOR 8
PCALL_OTHER 9
PCALL_POP 10
NINE
MEMORY MANAGEMENT
9.1 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 over it, even if they regularly manipulate 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
125
The Python/C API, Release 2.7.15
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.
void* PyObject_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 distinct non-NULL pointer if possible, as if PyObject_Malloc(1) had
been called instead. The memory will not have been initialized in any way.
void* PyObject_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 PyObject_Malloc(n); else 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 PyObject_Malloc(),
PyObject_Realloc() or PyObject_Calloc().
If the request fails, PyObject_Realloc() returns NULL and p remains a valid pointer to the previous
memory area.
void PyObject_Free(void *p)
Frees the memory block pointed to by p, which must have been returned by a previous
call to PyObject_Malloc(), PyObject_Realloc() or PyObject_Calloc(). Otherwise, or if
PyObject_Free(p) has been called before, undefined behavior occurs.
If p is NULL, no operation is performed.
In addition, the following macro sets are provided:
• PyObject_MALLOC(): alias to PyObject_Malloc()
• PyObject_REALLOC(): alias to PyObject_Realloc()
• PyObject_FREE(): alias to PyObject_Free()
• PyObject_Del(): alias to PyObject_Free()
• PyObject_DEL(): alias to PyObject_FREE() (so finally an alias to PyObject_Free())
Python has a pymalloc allocator optimized for small objects (smaller or equal to 512 bytes) with a short
lifetime. It uses memory mappings called “arenas” with a fixed size of 256 KiB. It falls back to malloc()
and realloc() for allocations larger than 512 bytes.
pymalloc is the default allocator of PyObject_Malloc().
The arena allocator uses the following functions:
• mmap() and munmap() if available,
• malloc() and free() otherwise.
Changed in version 2.7.7: The threshold changed from 256 to 512 bytes. The arena allocator now uses
mmap() if available.
9.5 Examples
Here is the example from section 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;
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.
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().
These will be explained in the next chapter on defining and implementing new object types in C.
TEN
This chapter describes the functions, types, and macros used when defining new object types.
PyObject* _PyObject_New(PyTypeObject *type)
Return value: New reference.
PyVarObject* _PyObject_NewVar(PyTypeObject *type, Py_ssize_t size)
Return value: New reference. Changed in version 2.5: This function used an int type for size. This
might require changes in your code for properly supporting 64-bit systems.
void _PyObject_Del(PyObject *op)
PyObject* PyObject_Init(PyObject *op, PyTypeObject *type)
Return value: Borrowed reference. Initialize a newly-allocated object op with its type and initial
reference. Returns the initialized object. If type indicates that the object participates in the cyclic
garbage detector, it is added to the detector’s set of observed objects. Other fields of the object are
not affected.
PyVarObject* PyObject_InitVar(PyVarObject *op, PyTypeObject *type, Py_ssize_t size)
Return value: Borrowed reference. This does everything PyObject_Init() does, and also initializes
the length information for a variable-size object.
Changed in version 2.5: This function used an int type for size. This might require changes in your
code for properly supporting 64-bit systems.
TYPE* PyObject_New(TYPE, PyTypeObject *type)
Return value: New reference. Allocate a new Python object using the C structure type TYPE and the
Python type object type. Fields not defined by the Python object header are not initialized; the object’s
reference count will be one. The size of the memory allocation is determined from the tp_basicsize
field of the type object.
TYPE* PyObject_NewVar(TYPE, PyTypeObject *type, Py_ssize_t size)
Return value: New reference. Allocate a new Python object using the C structure type TYPE and
the Python type object type. Fields not defined by the Python object header are not initialized. The
allocated memory allows for the TYPE structure plus size fields of the size given by the tp_itemsize
field of type. This is useful for implementing objects like tuples, which are able to determine their size
at construction time. Embedding the array of fields into the same allocation decreases the number of
allocations, improving the memory management efficiency.
Changed in version 2.5: This function used an int type for size. This might require changes in your
code for properly supporting 64-bit systems.
129
The Python/C API, Release 2.7.15
void PyObject_Del(PyObject *op)
Releases memory allocated to an object using PyObject_New() or PyObject_NewVar(). This is nor-
mally called from the tp_dealloc handler specified in the object’s type. The fields of the object should
not be accessed after this call as the memory is no longer a valid Python object.
PyObject* Py_InitModule(char *name, PyMethodDef *methods)
Return value: Borrowed reference. Create a new module object based on a name and table of functions,
returning the new module object.
Changed in version 2.3: Older versions of Python did not support NULL as the value for the methods
argument.
PyObject* Py_InitModule3(char *name, PyMethodDef *methods, char *doc)
Return value: Borrowed reference. 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.
Changed in version 2.3: Older versions of Python did not support NULL as the value for the methods
argument.
PyObject* Py_InitModule4(char *name, PyMethodDef *methods, char *doc, PyObject *self,
int apiver)
Return value: Borrowed reference. 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 be 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.
Changed in version 2.3: Older versions of Python did not support NULL as the value for the methods
argument.
PyObject _Py_NoneStruct
Object which is visible in Python as None. This should only be accessed using the Py_None macro,
which evaluates to a pointer to this object.
some notion of length. This type does not often appear in the Python/C API. It corresponds to the
fields defined by the expansion of the PyObject_VAR_HEAD macro.
These macros are used in the definition of PyObject and PyVarObject:
PyObject_HEAD
This is a macro which expands to the declarations of the fields of the PyObject type; it is used when
declaring new types which represent objects without a varying length. The specific fields it expands to
depend on the definition of Py_TRACE_REFS. By default, that macro is not defined, and PyObject_HEAD
expands to:
Py_ssize_t ob_refcnt;
PyTypeObject *ob_type;
PyObject_VAR_HEAD
This is a macro which expands to the declarations of the fields of the PyVarObject type; it is used
when declaring new types which represent objects with a length that varies from instance to instance.
This macro always expands to:
PyObject_HEAD
Py_ssize_t ob_size;
Note that PyObject_HEAD is part of the expansion, and that its own expansion varies depending on
the definition of Py_TRACE_REFS.
Py_TYPE(o)
This macro is used to access the ob_type member of a Python object. It expands to:
(((PyObject*)(o))->ob_type)
(((PyObject*)(o))->ob_refcnt)
(((PyVarObject*)(o))->ob_size)
_PyObject_EXTRA_INIT
1, type,
PyVarObject_HEAD_INIT(type, size)
This is a macro which expands to initialization values for a new PyVarObject type, including the
ob_size field. This macro expands to:
_PyObject_EXTRA_INIT
1, type, size,
PyCFunction
Type of the functions used to implement most Python callables in C. Functions of this type take two
PyObject* parameters and return one such value. If the return value is NULL, an exception shall have
been set. If not NULL, the return value is interpreted as the return value of the function as exposed
in Python. The function must return a new reference.
PyMethodDef
Structure used to describe a method of an extension type. This structure has four fields:
The ml_meth is a C function pointer. The functions may be of different types, but they always return
PyObject*. If the function is not of the PyCFunction, the compiler will require a cast in the method
table. Even though PyCFunction defines the first parameter as PyObject*, it is common that the method
implementation uses the specific C type of the self object.
The ml_flags field is a bitfield which can include the following flags. The individual flags indicate either
a calling convention or a binding convention. Of the calling convention flags, only METH_VARARGS and
METH_KEYWORDS can be combined. Any of the calling convention flags can be combined with a binding flag.
METH_VARARGS
This is the typical calling convention, where the methods have the type PyCFunction. The function ex-
pects two PyObject* values. The first one is the self object for methods; for module functions, it is the
module object. The second parameter (often called args) is a tuple object representing all arguments.
This parameter is typically processed using PyArg_ParseTuple() or PyArg_UnpackTuple().
METH_KEYWORDS
Methods with these flags must be of type PyCFunctionWithKeywords. The function expects three pa-
rameters: self, args, and a dictionary of all the keyword arguments. The flag is typically combined with
METH_VARARGS, and the parameters are typically processed using PyArg_ParseTupleAndKeywords().
METH_NOARGS
Methods without parameters don’t need to check whether arguments are given if they are listed with
the METH_NOARGS flag. They need to be of type PyCFunction. The first parameter is typically named
self and will hold a reference to the module or object instance. In all cases the second parameter will
be NULL.
METH_O
Methods with a single object argument can be listed with the METH_O flag, instead of invoking
PyArg_ParseTuple() with a "O" argument. They have the type PyCFunction, with the self pa-
rameter, and a PyObject* parameter representing the single argument.
METH_OLDARGS
This calling convention is deprecated. The method must be of type PyCFunction. The second argument
is NULL if no arguments are given, a single object if exactly one argument is given, and a tuple of
objects if more than one argument is given. There is no way for a function using this convention to
distinguish between a call with multiple arguments and a call with a tuple as the only argument.
These two constants are not used to indicate the calling convention but the binding when use with methods
of classes. These may not be used for functions defined for modules. At most one of these flags may be set
for any given method.
METH_CLASS
The method will be passed the type object as the first parameter rather than an instance of the type.
This is used to create class methods, similar to what is created when using the classmethod() built-in
function.
New in version 2.3.
METH_STATIC
The method will be passed NULL as the first parameter rather than an instance of the type. This
is used to create static methods, similar to what is created when using the staticmethod() built-in
function.
New in version 2.3.
One other constant controls whether a method is loaded in place of another definition with the same method
name.
METH_COEXIST
The method will be loaded in place of existing definitions. Without METH_COEXIST, the default
is to skip repeated definitions. Since slot wrappers are loaded before the method table, the existence
of a sq_contains slot, for example, would generate a wrapped method named __contains__() and
preclude the loading of a corresponding PyCFunction with the same name. With the flag defined, the
PyCFunction will be loaded in place of the wrapper object and will co-exist with the slot. This is
helpful because calls to PyCFunctions are optimized more than wrapper object calls.
New in version 2.4.
PyMemberDef
Structure which describes an attribute of a type which corresponds to a C struct member. Its fields
are:
type can be one of many T_ macros corresponding to various C types. When the member is accessed
in Python, it will be converted to the equivalent Python type.
T_OBJECT and T_OBJECT_EX differ in that T_OBJECT returns None if the member is NULL and
T_OBJECT_EX raises an AttributeError. Try to use T_OBJECT_EX over T_OBJECT because T_OBJECT_EX
handles use of the del statement on that attribute more correctly than T_OBJECT.
flags can be 0 for write and read access or READONLY for read-only access. Using T_STRING for type
implies READONLY. Only T_OBJECT and T_OBJECT_EX members can be deleted. (They are set to NULL).
PyGetSetDef
Structure to define property-like access for a type. See also description of the PyTypeObject.tp_getset
slot.
Field C Meaning
Type
name char * attribute name
get getter C Function to get the attribute
set setter optional C function to set or delete the attribute, if omitted the attribute is
readonly
doc char * optional docstring
clo- void * optional function pointer, providing additional data for getter and setter
sure
The get function takes one PyObject* parameter (the instance) and a function pointer (the associated
closure):
typedef PyObject *(*getter)(PyObject *, void *);
It should return a new reference on success or NULL with a set exception on failure.
set functions take two PyObject* parameters (the instance and the value to be set) and a function
pointer (the associated closure):
typedef int (*setter)(PyObject *, PyObject *, void *);
In case the attribute should be deleted the second parameter is NULL. Should return 0 on success or
-1 with a set exception on failure.
destructor tp_dealloc;
printfunc tp_print;
getattrfunc tp_getattr;
setattrfunc tp_setattr;
cmpfunc tp_compare;
reprfunc tp_repr;
PyNumberMethods *tp_as_number;
PySequenceMethods *tp_as_sequence;
PyMappingMethods *tp_as_mapping;
hashfunc tp_hash;
ternaryfunc tp_call;
reprfunc tp_str;
getattrofunc tp_getattro;
setattrofunc tp_setattro;
} PyTypeObject;
The type object structure extends the PyVarObject structure. The ob_size field is used for dynamic types
(created by type_new(), usually called from a class statement). Note that PyType_Type (the metatype)
initializes tp_itemsize, which means that its instances (i.e. type objects) must have the ob_size field.
PyObject* PyObject._ob_next
PyObject* PyObject._ob_prev
These fields are only present when the macro Py_TRACE_REFS is defined. Their initialization to NULL
is taken care of by the PyObject_HEAD_INIT macro. For statically allocated objects, these fields always
remain NULL. For dynamically allocated objects, these two fields are used to link the object into a
doubly-linked list of all live objects on the heap. This could be used for various debugging purposes;
currently the only use is to print the objects that are still alive at the end of a run when the environment
Foo_Type.ob_type = &PyType_Type;
This should be done before any instances of the type are created. PyType_Ready() checks if ob_type
is NULL, and if so, initializes it: in Python 2.2, it is set to &PyType_Type; in Python 2.2.1 and later
it is initialized to the ob_type field of the base class. PyType_Ready() will not change this field if it
is non-zero.
In Python 2.2, this field is not inherited by subtypes. In 2.2.1, and in 2.3 and beyond, it is inherited
by subtypes.
Py_ssize_t PyVarObject.ob_size
For statically allocated type objects, this should be initialized to zero. For dynamically allocated type
objects, this field has a special internal meaning.
This field is not inherited by subtypes.
char* PyTypeObject.tp_name
Pointer to a NUL-terminated string containing the name of the type. For types that are accessible as
module globals, the string should be the full module name, followed by a dot, followed by the type
name; for built-in types, it should be just the type name. If the module is a submodule of a package,
the full package name is part of the full module name. For example, a type named T defined in module
M in subpackage Q in package P should have the tp_name initializer "P.Q.M.T".
For dynamically allocated type objects, this should just be the type name, and the module name
explicitly stored in the type dict as the value for key '__module__'.
For statically allocated type objects, the tp_name field should contain a dot. Everything before the
last dot is made accessible as the __module__ attribute, and everything after the last dot is made
accessible as the __name__ attribute.
If no dot is present, the entire tp_name field is made accessible as the __name__ attribute, and the
__module__ attribute is undefined (unless explicitly set in the dictionary, as explained above). This
means your type will be impossible to pickle. Additionally, it will not be listed in module documenta-
tions created with pydoc.
This field is not inherited by subtypes.
Py_ssize_t PyTypeObject.tp_basicsize
Py_ssize_t PyTypeObject.tp_itemsize
These fields allow calculating the size in bytes of instances of the type.
There are two kinds of types: types with fixed-length instances have a zero tp_itemsize field, types
with variable-length instances have a non-zero tp_itemsize field. For a type with fixed-length in-
stances, all instances have the same size, given in tp_basicsize.
For a type with variable-length instances, the instances must have an ob_size field, and the instance
size is tp_basicsize plus N times tp_itemsize, where N is the “length” of the object. The value of
N is typically stored in the instance’s ob_size field. There are exceptions: for example, long ints use
a negative ob_size to indicate a negative number, and N is abs(ob_size) there. Also, the presence
of an ob_size field in the instance layout doesn’t mean that the instance structure is variable-length
(for example, the structure for the list type has fixed-length instances, yet those instances have a
meaningful ob_size field).
The basic size includes the fields in the instance declared by the macro PyObject_HEAD or
PyObject_VAR_HEAD (whichever is used to declare the instance struct) and this in turn includes the
_ob_prev and _ob_next fields if they are present. This means that the only correct way to get an
initializer for the tp_basicsize is to use the sizeof operator on the struct used to declare the instance
layout. The basic size does not include the GC header size (this is new in Python 2.2; in 2.1 and 2.0,
the GC header size was included in tp_basicsize).
These fields are inherited separately by subtypes. If the base type has a non-zero tp_itemsize, it is
generally not safe to set tp_itemsize to a different non-zero value in a subtype (though this depends
on the implementation of the base type).
A note about alignment: if the variable items require a particular alignment, this should be taken
care of by the value of tp_basicsize. Example: suppose a type implements an array of double.
tp_itemsize is sizeof(double). It is the programmer’s responsibility that tp_basicsize is a multiple
of sizeof(double) (assuming this is the alignment requirement for double).
destructor PyTypeObject.tp_dealloc
A pointer to the instance destructor function. This function must be defined unless the type guarantees
that its instances will never be deallocated (as is the case for the singletons None and Ellipsis).
The destructor function is called by the Py_DECREF() and Py_XDECREF() macros when the new ref-
erence count is zero. At this point, the instance is still in existence, but there are no references to
it. The destructor function should free all references which the instance owns, free all memory buffers
owned by the instance (using the freeing function corresponding to the allocation function used to
allocate the buffer), and finally (as its last action) call the type’s tp_free function. If the type is not
subtypable (doesn’t have the Py_TPFLAGS_BASETYPE flag bit set), it is permissible to call the object
deallocator directly instead of via tp_free. The object deallocator should be the one used to allocate
the instance; this is normally PyObject_Del() if the instance was allocated using PyObject_New() or
PyObject_VarNew(), or PyObject_GC_Del() if the instance was allocated using PyObject_GC_New()
or PyObject_GC_NewVar().
This field is inherited by subtypes.
printfunc PyTypeObject.tp_print
An optional pointer to the instance print function.
The print function is only called when the instance is printed to a real file; when it is printed to a
pseudo-file (like a StringIO instance), the instance’s tp_repr or tp_str function is called to convert
it to a string. These are also called when the type’s tp_print field is NULL. A type should never
implement tp_print in a way that produces different output than tp_repr or tp_str would.
The print function is called with the same signature as PyObject_Print(): int tp_print(PyObject
*self, FILE *file, int flags). The self argument is the instance to be printed. The file argument
is the stdio file to which it is to be printed. The flags argument is composed of flag bits. The only flag
bit currently defined is Py_PRINT_RAW. When the Py_PRINT_RAW flag bit is set, the instance should be
printed the same way as tp_str would format it; when the Py_PRINT_RAW flag bit is clear, the instance
should be printed the same was as tp_repr would format it. It should return -1 and set an exception
condition when an error occurred during the comparison.
It is possible that the tp_print field will be deprecated. In any case, it is recommended not to define
tp_print, but instead to rely on tp_repr and tp_str for printing.
This field is inherited by subtypes.
getattrfunc PyTypeObject.tp_getattr
An optional pointer to the get-attribute-string function.
This field is deprecated. When it is defined, it should point to a function that acts the same as the
tp_getattro function, but taking a C string instead of a Python string object to give the attribute
name. The signature is
This field is inherited by subtypes together with tp_getattro: a subtype inherits both tp_getattr
and tp_getattro from its base type when the subtype’s tp_getattr and tp_getattro are both NULL.
setattrfunc PyTypeObject.tp_setattr
An optional pointer to the function for setting and deleting attributes.
This field is deprecated. When it is defined, it should point to a function that acts the same as the
tp_setattro function, but taking a C string instead of a Python string object to give the attribute
name. The signature is
The v argument is set to NULL to delete the attribute. This field is inherited by subtypes together
with tp_setattro: a subtype inherits both tp_setattr and tp_setattro from its base type when
the subtype’s tp_setattr and tp_setattro are both NULL.
cmpfunc PyTypeObject.tp_compare
An optional pointer to the three-way comparison function.
The signature is the same as for PyObject_Compare(). The function should return 1 if self greater
than other, 0 if self is equal to other, and -1 if self less than other. It should return -1 and set an
exception condition when an error occurred during the comparison.
This field is inherited by subtypes together with tp_richcompare and tp_hash: a subtypes in-
herits all three of tp_compare, tp_richcompare, and tp_hash when the subtype’s tp_compare,
tp_richcompare, and tp_hash are all NULL.
reprfunc PyTypeObject.tp_repr
An optional pointer to a function that implements the built-in function repr().
The signature is the same as for PyObject_Repr(); it must return a string or a Unicode object. Ideally,
this function should return a string that, when passed to eval(), given a suitable environment, returns
an object with the same value. If this is not feasible, it should return a string starting with '<' and
ending with '>' from which both the type and the value of the object can be deduced.
When this field is not set, a string of the form <%s object at %p> is returned, where %s is replaced
by the type name, and %p by the object’s memory address.
This field is inherited by subtypes.
PyNumberMethods* tp_as_number
Pointer to an additional structure that contains fields relevant only to objects which implement the
number protocol. These fields are documented in Number Object Structures.
The tp_as_number field is not inherited, but the contained fields are inherited individually.
PySequenceMethods* tp_as_sequence
Pointer to an additional structure that contains fields relevant only to objects which implement the
sequence protocol. These fields are documented in Sequence Object Structures.
The tp_as_sequence field is not inherited, but the contained fields are inherited individually.
PyMappingMethods* tp_as_mapping
Pointer to an additional structure that contains fields relevant only to objects which implement the
mapping protocol. These fields are documented in Mapping Object Structures.
The tp_as_mapping field is not inherited, but the contained fields are inherited individually.
hashfunc PyTypeObject.tp_hash
An optional pointer to a function that implements the built-in function hash().
The signature is the same as for PyObject_Hash(); it must return a C long. The value -1 should not
be returned as a normal return value; when an error occurs during the computation of the hash value,
the function should set an exception and return -1.
This field can be set explicitly to PyObject_HashNotImplemented() to block inheritance of the hash
method from a parent type. This is interpreted as the equivalent of __hash__ = None at the Python
level, causing isinstance(o, collections.Hashable) to correctly return False. Note that the
converse is also true - setting __hash__ = None on a class at the Python level will result in the
tp_hash slot being set to PyObject_HashNotImplemented().
When this field is not set, two possibilities exist: if the tp_compare and tp_richcompare fields are
both NULL, a default hash value based on the object’s address is returned; otherwise, a TypeError is
raised.
This field is inherited by subtypes together with tp_richcompare and tp_compare: a subtypes in-
herits all three of tp_compare, tp_richcompare, and tp_hash, when the subtype’s tp_compare,
tp_richcompare and tp_hash are all NULL.
ternaryfunc PyTypeObject.tp_call
An optional pointer to a function that implements calling the object. This should be NULL if the
object is not callable. The signature is the same as for PyObject_Call().
This field is inherited by subtypes.
reprfunc PyTypeObject.tp_str
An optional pointer to a function that implements the built-in operation str(). (Note that str is a
type now, and str() calls the constructor for that type. This constructor calls PyObject_Str() to do
the actual work, and PyObject_Str() will call this handler.)
The signature is the same as for PyObject_Str(); it must return a string or a Unicode object. This
function should return a “friendly” string representation of the object, as this is the representation
that will be used by the print statement.
When this field is not set, PyObject_Repr() is called to return a string representation.
This field is inherited by subtypes.
getattrofunc PyTypeObject.tp_getattro
An optional pointer to the get-attribute function.
The signature is the same as for PyObject_GetAttr(). It is usually convenient to set this field to
PyObject_GenericGetAttr(), which implements the normal way of looking for object attributes.
This field is inherited by subtypes together with tp_getattr: a subtype inherits both tp_getattr and
tp_getattro from its base type when the subtype’s tp_getattr and tp_getattro are both NULL.
setattrofunc PyTypeObject.tp_setattro
An optional pointer to the function for setting and deleting attributes.
The signature is the same as for PyObject_SetAttr(), but setting v to NULL to delete an attribute
must be supported. It is usually convenient to set this field to PyObject_GenericSetAttr(), which
implements the normal way of setting object attributes.
This field is inherited by subtypes together with tp_setattr: a subtype inherits both tp_setattr and
tp_setattro from its base type when the subtype’s tp_setattr and tp_setattro are both NULL.
PyBufferProcs* PyTypeObject.tp_as_buffer
Pointer to an additional structure that contains fields relevant only to objects which implement the
buffer interface. These fields are documented in Buffer Object Structures.
The tp_as_buffer field is not inherited, but the contained fields are inherited individually.
long PyTypeObject.tp_flags
This field is a bit mask of various flags. Some flags indicate variant semantics for certain situations;
others are used to indicate that certain fields in the type object (or in the extension structures referenced
via tp_as_number, tp_as_sequence, tp_as_mapping, and tp_as_buffer) that were historically not
always present are valid; if such a flag bit is clear, the type fields it guards must not be accessed and
must be considered to have a zero or NULL value instead.
Inheritance of this field is complicated. Most flag bits are inherited individually, i.e. if the base type has
a flag bit set, the subtype inherits this flag bit. The flag bits that pertain to extension structures are
strictly inherited if the extension structure is inherited, i.e. the base type’s value of the flag bit is copied
into the subtype together with a pointer to the extension structure. The Py_TPFLAGS_HAVE_GC flag bit
is inherited together with the tp_traverse and tp_clear fields, i.e. if the Py_TPFLAGS_HAVE_GC flag
bit is clear in the subtype and the tp_traverse and tp_clear fields in the subtype exist (as indicated
by the Py_TPFLAGS_HAVE_RICHCOMPARE flag bit) and have NULL values.
The following bit masks are currently defined; these can be ORed together using the | operator to
form the value of the tp_flags field. The macro PyType_HasFeature() takes a type and a flags value,
tp and f, and checks whether tp->tp_flags & f is non-zero.
Py_TPFLAGS_HAVE_GETCHARBUFFER
If this bit is set, the PyBufferProcs struct referenced by tp_as_buffer has the
bf_getcharbuffer field.
Py_TPFLAGS_HAVE_SEQUENCE_IN
If this bit is set, the PySequenceMethods struct referenced by tp_as_sequence has the
sq_contains field.
Py_TPFLAGS_GC
This bit is obsolete. The bit it used to name is no longer in use. The symbol is now defined as
zero.
Py_TPFLAGS_HAVE_INPLACEOPS
If this bit is set, the PySequenceMethods struct referenced by tp_as_sequence and the
PyNumberMethods structure referenced by tp_as_number contain the fields for in-place
operators. In particular, this means that the PyNumberMethods structure has the fields
nb_inplace_add, nb_inplace_subtract, nb_inplace_multiply, nb_inplace_divide,
nb_inplace_remainder, nb_inplace_power, nb_inplace_lshift, nb_inplace_rshift,
nb_inplace_and, nb_inplace_xor, and nb_inplace_or; and the PySequenceMethods struct
has the fields sq_inplace_concat and sq_inplace_repeat.
Py_TPFLAGS_CHECKTYPES
If this bit is set, the binary and ternary operations in the PyNumberMethods structure referenced
by tp_as_number accept arguments of arbitrary object types, and do their own type conver-
sions if needed. If this bit is clear, those operations require that all arguments have the current
type as their type, and the caller is supposed to perform a coercion operation first. This ap-
plies to nb_add, nb_subtract, nb_multiply, nb_divide, nb_remainder, nb_divmod, nb_power,
nb_lshift, nb_rshift, nb_and, nb_xor, and nb_or.
Py_TPFLAGS_HAVE_RICHCOMPARE
If this bit is set, the type object has the tp_richcompare field, as well as the tp_traverse and
the tp_clear fields.
Py_TPFLAGS_HAVE_WEAKREFS
If this bit is set, the tp_weaklistoffset field is defined. Instances of a type are weakly refer-
enceable if the type’s tp_weaklistoffset field has a value greater than zero.
Py_TPFLAGS_HAVE_ITER
If this bit is set, the type object has the tp_iter and tp_iternext fields.
Py_TPFLAGS_HAVE_CLASS
If this bit is set, the type object has several new fields defined starting in Python 2.2: tp_methods,
tp_members, tp_getset, tp_base, tp_dict, tp_descr_get, tp_descr_set, tp_dictoffset,
tp_init, tp_alloc, tp_new, tp_free, tp_is_gc, tp_bases, tp_mro, tp_cache, tp_subclasses,
and tp_weaklist.
Py_TPFLAGS_HEAPTYPE
This bit is set when the type object itself is allocated on the heap. In this case, the ob_type field
of its instances is considered a reference to the type, and the type object is INCREF’ed when
a new instance is created, and DECREF’ed when an instance is destroyed (this does not apply
to instances of subtypes; only the type referenced by the instance’s ob_type gets INCREF’ed or
DECREF’ed).
Py_TPFLAGS_BASETYPE
This bit is set when the type can be used as the base type of another type. If this bit is clear, the
type cannot be subtyped (similar to a “final” class in Java).
Py_TPFLAGS_READY
This bit is set when the type object has been fully initialized by PyType_Ready().
Py_TPFLAGS_READYING
This bit is set while PyType_Ready() is in the process of initializing the type object.
Py_TPFLAGS_HAVE_GC
This bit is set when the object supports garbage collection. If this bit is set, instances must be
created using PyObject_GC_New() and destroyed using PyObject_GC_Del(). More information
in section Supporting Cyclic Garbage Collection. This bit also implies that the GC-related fields
tp_traverse and tp_clear are present in the type object; but those fields also exist when
Py_TPFLAGS_HAVE_GC is clear but Py_TPFLAGS_HAVE_RICHCOMPARE is set.
Py_TPFLAGS_DEFAULT
This is a bitmask of all the bits that pertain to the existence of certain fields
in the type object and its extension structures. Currently, it includes the fol-
lowing bits: Py_TPFLAGS_HAVE_GETCHARBUFFER, Py_TPFLAGS_HAVE_SEQUENCE_IN ,
Py_TPFLAGS_HAVE_INPLACEOPS, Py_TPFLAGS_HAVE_RICHCOMPARE , Py_TPFLAGS_HAVE_WEAKREFS,
Py_TPFLAGS_HAVE_ITER, and Py_TPFLAGS_HAVE_CLASS.
char* PyTypeObject.tp_doc
An optional pointer to a NUL-terminated C string giving the docstring for this type object. This is
exposed as the __doc__ attribute on the type and instances of the type.
This field is not inherited by subtypes.
The following three fields only exist if the Py_TPFLAGS_HAVE_RICHCOMPARE flag bit is set.
traverseproc PyTypeObject.tp_traverse
An optional pointer to a traversal function for the garbage collector. This is only used if the
Py_TPFLAGS_HAVE_GC flag bit is set. More information about Python’s garbage collection scheme
can be found in section Supporting Cyclic Garbage Collection.
The tp_traverse pointer is used by the garbage collector to detect reference cycles. A typical im-
plementation of a tp_traverse function simply calls Py_VISIT() on each of the instance’s members
that are Python objects. For example, this is function local_traverse() from the thread extension
module:
static int
local_traverse(localobject *self, visitproc visit, void *arg)
{
Py_VISIT(self->args);
Py_VISIT(self->kw);
Py_VISIT(self->dict);
return 0;
}
Note that Py_VISIT() is called only on those members that can participate in reference cycles. Al-
though there is also a self->key member, it can only be NULL or a Python string and therefore
cannot be part of a reference cycle.
On the other hand, even if you know a member can never be part of a cycle, as a debugging aid you
may want to visit it anyway just so the gc module’s get_referents() function will include it.
Note that Py_VISIT() requires the visit and arg parameters to local_traverse() to have these specific
names; don’t name them just anything.
This field is inherited by subtypes together with tp_clear and the Py_TPFLAGS_HAVE_GC flag bit: the
flag bit, tp_traverse, and tp_clear are all inherited from the base type if they are all zero in the
subtype and the subtype has the Py_TPFLAGS_HAVE_RICHCOMPARE flag bit set.
inquiry PyTypeObject.tp_clear
An optional pointer to a clear function for the garbage collector. This is only used if the
Py_TPFLAGS_HAVE_GC flag bit is set.
The tp_clear member function is used to break reference cycles in cyclic garbage detected by the
garbage collector. Taken together, all tp_clear functions in the system must combine to break all
reference cycles. This is subtle, and if in any doubt supply a tp_clear function. For example, the
tuple type does not implement a tp_clear function, because it’s possible to prove that no reference
cycle can be composed entirely of tuples. Therefore the tp_clear functions of other types must be
sufficient to break any cycle containing a tuple. This isn’t immediately obvious, and there’s rarely a
good reason to avoid implementing tp_clear.
Implementations of tp_clear should drop the instance’s references to those of its members that may
be Python objects, and set its pointers to those members to NULL, as in the following example:
static int
local_clear(localobject *self)
{
Py_CLEAR(self->key);
Py_CLEAR(self->args);
Py_CLEAR(self->kw);
Py_CLEAR(self->dict);
return 0;
}
The Py_CLEAR() macro should be used, because clearing references is delicate: the reference to the
contained object must not be decremented until after the pointer to the contained object is set to
NULL. This is because decrementing the reference count may cause the contained object to become
trash, triggering a chain of reclamation activity that may include invoking arbitrary Python code (due
to finalizers, or weakref callbacks, associated with the contained object). If it’s possible for such code
to reference self again, it’s important that the pointer to the contained object be NULL at that time,
so that self knows the contained object can no longer be used. The Py_CLEAR() macro performs the
operations in a safe order.
Because the goal of tp_clear functions is to break reference cycles, it’s not necessary to clear contained
objects like Python strings or Python integers, which can’t participate in reference cycles. On the other
hand, it may be convenient to clear all contained Python objects, and write the type’s tp_dealloc
function to invoke tp_clear.
More information about Python’s garbage collection scheme can be found in section Supporting Cyclic
Garbage Collection.
This field is inherited by subtypes together with tp_traverse and the Py_TPFLAGS_HAVE_GC flag bit:
the flag bit, tp_traverse, and tp_clear are all inherited from the base type if they are all zero in the
subtype and the subtype has the Py_TPFLAGS_HAVE_RICHCOMPARE flag bit set.
richcmpfunc PyTypeObject.tp_richcompare
An optional pointer to the rich comparison function, whose signature is PyObject
*tp_richcompare(PyObject *a, PyObject *b, int op).
The function should return the result of the comparison (usually Py_True or Py_False). If the com-
parison is undefined, it must return Py_NotImplemented, if another error occurred it must return NULL
and set an exception condition.
Note: If you want to implement a type for which only a limited set of comparisons makes sense (e.g.
== and !=, but not < and friends), directly raise TypeError in the rich comparison function.
This field is inherited by subtypes together with tp_compare and tp_hash: a subtype inherits all three
of tp_compare, tp_richcompare, and tp_hash, when the subtype’s tp_compare, tp_richcompare,
and tp_hash are all NULL.
The following constants are defined to be used as the third argument for tp_richcompare and for
PyObject_RichCompare():
Constant Comparison
Py_LT <
Py_LE <=
Py_EQ ==
Py_NE !=
Py_GT >
Py_GE >=
The next field only exists if the Py_TPFLAGS_HAVE_WEAKREFS flag bit is set.
long PyTypeObject.tp_weaklistoffset
If the instances of this type are weakly referenceable, this field is greater than zero and contains the
offset in the instance structure of the weak reference list head (ignoring the GC header, if present);
this offset is used by PyObject_ClearWeakRefs() and the PyWeakref_*() functions. The instance
structure needs to include a field of type PyObject* which is initialized to NULL.
Do not confuse this field with tp_weaklist; that is the list head for weak references to the type object
itself.
This field is inherited by subtypes, but see the rules listed below. A subtype may override this offset;
this means that the subtype uses a different weak reference list head than the base type. Since the list
head is always found via tp_weaklistoffset, this should not be a problem.
When a type defined by a class statement has no __slots__ declaration, and none of its base types
are weakly referenceable, the type is made weakly referenceable by adding a weak reference list head
slot to the instance layout and setting the tp_weaklistoffset of that slot’s offset.
When a type’s __slots__ declaration contains a slot named __weakref__, that slot becomes the
weak reference list head for instances of the type, and the slot’s offset is stored in the type’s
tp_weaklistoffset.
When a type’s __slots__ declaration does not contain a slot named __weakref__, the type inherits
its tp_weaklistoffset from its base type.
The next two fields only exist if the Py_TPFLAGS_HAVE_ITER flag bit is set.
getiterfunc PyTypeObject.tp_iter
An optional pointer to a function that returns an iterator for the object. Its presence normally signals
that the instances of this type are iterable (although sequences may be iterable without this function,
and classic instances always have this function, even if they don’t define an __iter__() method).
This function has the same signature as PyObject_GetIter().
This field is inherited by subtypes.
iternextfunc PyTypeObject.tp_iternext
An optional pointer to a function that returns the next item in an iterator. When the iterator is
exhausted, it must return NULL; a StopIteration exception may or may not be set. When another
error occurs, it must return NULL too. Its presence normally signals that the instances of this type
are iterators (although classic instances always have this function, even if they don’t define a next()
method).
Iterator types should also define the tp_iter function, and that function should return the iterator
instance itself (not a new iterator instance).
This function has the same signature as PyIter_Next().
This field is inherited by subtypes.
The next fields, up to and including tp_weaklist, only exist if the Py_TPFLAGS_HAVE_CLASS flag bit is set.
struct PyMethodDef * PyTypeObject.tp_methods
An optional pointer to a static NULL-terminated array of PyMethodDef structures, declaring regular
methods of this type.
For each entry in the array, an entry is added to the type’s dictionary (see tp_dict below) containing
a method descriptor.
This field is not inherited by subtypes (methods are inherited through a different mechanism).
struct PyMemberDef * PyTypeObject.tp_members
An optional pointer to a static NULL-terminated array of PyMemberDef structures, declaring regular
data members (fields or slots) of instances of this type.
For each entry in the array, an entry is added to the type’s dictionary (see tp_dict below) containing
a member descriptor.
This field is not inherited by subtypes (members are inherited through a different mechanism).
struct PyGetSetDef * PyTypeObject.tp_getset
An optional pointer to a static NULL-terminated array of PyGetSetDef structures, declaring computed
attributes of instances of this type.
For each entry in the array, an entry is added to the type’s dictionary (see tp_dict below) containing
a getset descriptor.
This field is not inherited by subtypes (computed attributes are inherited through a different mecha-
nism).
PyTypeObject* PyTypeObject.tp_base
An optional pointer to a base type from which type properties are inherited. At this level, only single
inheritance is supported; multiple inheritance require dynamically creating a type object by calling the
metatype.
This field is not inherited by subtypes (obviously), but it defaults to &PyBaseObject_Type (which to
Python programmers is known as the type object).
PyObject* PyTypeObject.tp_dict
The type’s dictionary is stored here by PyType_Ready().
This field should normally be initialized to NULL before PyType_Ready is called; it may also be ini-
tialized to a dictionary containing initial attributes for the type. Once PyType_Ready() has initialized
the type, extra attributes for the type may be added to this dictionary only if they don’t correspond
to overloaded operations (like __add__()).
This field is not inherited by subtypes (though the attributes defined in here are inherited through a
different mechanism).
descrgetfunc PyTypeObject.tp_descr_get
An optional pointer to a “descriptor get” function.
The function signature is
The value argument is set to NULL to delete the value. This field is inherited by subtypes.
long PyTypeObject.tp_dictoffset
If the instances of this type have a dictionary containing instance variables, this field is non-zero and
contains the offset in the instances of the type of the instance variable dictionary; this offset is used
by PyObject_GenericGetAttr().
Do not confuse this field with tp_dict; that is the dictionary for attributes of the type object itself.
If the value of this field is greater than zero, it specifies the offset from the start of the instance
structure. If the value is less than zero, it specifies the offset from the end of the instance structure. A
negative offset is more expensive to use, and should only be used when the instance structure contains
a variable-length part. This is used for example to add an instance variable dictionary to subtypes of
str or tuple. Note that the tp_basicsize field should account for the dictionary added to the end
in that case, even though the dictionary is not included in the basic object layout. On a system with
a pointer size of 4 bytes, tp_dictoffset should be set to -4 to indicate that the dictionary is at the
very end of the structure.
The real dictionary offset in an instance can be computed from a negative tp_dictoffset as follows:
where tp_basicsize, tp_itemsize and tp_dictoffset are taken from the type object, and ob_size
is taken from the instance. The absolute value is taken because long ints use the sign of ob_size to
store the sign of the number. (There’s never a need to do this calculation yourself; it is done for you
by _PyObject_GetDictPtr().)
This field is inherited by subtypes, but see the rules listed below. A subtype may override this offset;
this means that the subtype instances store the dictionary at a difference offset than the base type.
Since the dictionary is always found via tp_dictoffset, this should not be a problem.
When a type defined by a class statement has no __slots__ declaration, and none of its base types has
an instance variable dictionary, a dictionary slot is added to the instance layout and the tp_dictoffset
is set to that slot’s offset.
When a type defined by a class statement has a __slots__ declaration, the type inherits its
tp_dictoffset from its base type.
(Adding a slot named __dict__ to the __slots__ declaration does not have the expected effect, it
just causes confusion. Maybe this should be added as a feature just like __weakref__ though.)
initproc PyTypeObject.tp_init
An optional pointer to an instance initialization function.
This function corresponds to the __init__() method of classes. Like __init__(), it is possible to
create an instance without calling __init__(), and it is possible to reinitialize an instance by calling
its __init__() method again.
The function signature is
The self argument is the instance to be initialized; the args and kwds arguments represent positional
and keyword arguments of the call to __init__().
The tp_init function, if not NULL, is called when an instance is created normally by calling its type,
after the type’s tp_new function has returned an instance of the type. If the tp_new function returns
an instance of some other type that is not a subtype of the original type, no tp_init function is
called; if tp_new returns an instance of a subtype of the original type, the subtype’s tp_init is called.
(VERSION NOTE: described here is what is implemented in Python 2.2.1 and later. In Python 2.2,
the tp_init of the type of the object returned by tp_new was always called, if not NULL.)
This field is inherited by subtypes.
allocfunc PyTypeObject.tp_alloc
An optional pointer to an instance allocation function.
The function signature is
The purpose of this function is to separate memory allocation from memory initialization. It should
return a pointer to a block of memory of adequate length for the instance, suitably aligned, and
initialized to zeros, but with ob_refcnt set to 1 and ob_type set to the type argument. If the type’s
tp_itemsize is non-zero, the object’s ob_size field should be initialized to nitems and the length of the
allocated memory block should be tp_basicsize + nitems*tp_itemsize, rounded up to a multiple
of sizeof(void*); otherwise, nitems is not used and the length of the block should be tp_basicsize.
Do not use this function to do any other instance initialization, not even to allocate additional memory;
that should be done by tp_new.
This field is inherited by static subtypes, but not by dynamic subtypes (subtypes created by a class
statement); in the latter, this field is always set to PyType_GenericAlloc(), to force a standard heap
allocation strategy. That is also the recommended value for statically defined types.
newfunc PyTypeObject.tp_new
An optional pointer to an instance creation function.
If this function is NULL for a particular type, that type cannot be called to create new instances;
presumably there is some other way to create instances, like a factory function.
The subtype argument is the type of the object being created; the args and kwds arguments represent
positional and keyword arguments of the call to the type. Note that subtype doesn’t have to equal the
type whose tp_new function is called; it may be a subtype of that type (but not an unrelated type).
The tp_new function should call subtype->tp_alloc(subtype, nitems) to allocate space for the
object, and then do only as much further initialization as is absolutely necessary. Initialization that
can safely be ignored or repeated should be placed in the tp_init handler. A good rule of thumb is
that for immutable types, all initialization should take place in tp_new, while for mutable types, most
initialization should be deferred to tp_init.
This field is inherited by subtypes, except it is not inherited by static types whose tp_base is NULL or
&PyBaseObject_Type. The latter exception is a precaution so that old extension types don’t become
callable simply by being linked with Python 2.2.
destructor PyTypeObject.tp_free
An optional pointer to an instance deallocation function.
The signature of this function has changed slightly: in Python 2.2 and 2.2.1, its signature is destructor:
void tp_free(PyObject *)
The only initializer that is compatible with both versions is _PyObject_Del, whose definition has
suitably adapted in Python 2.3.
This field is inherited by static subtypes, but not by dynamic subtypes (subtypes created by a class
statement); in the latter, this field is set to a deallocator suitable to match PyType_GenericAlloc()
and the value of the Py_TPFLAGS_HAVE_GC flag bit.
inquiry PyTypeObject.tp_is_gc
An optional pointer to a function called by the garbage collector.
The garbage collector needs to know whether a particular object is collectible or not. Normally, it is
sufficient to look at the object’s type’s tp_flags field, and check the Py_TPFLAGS_HAVE_GC flag bit.
But some types have a mixture of statically and dynamically allocated instances, and the statically
allocated instances are not collectible. Such types should define this function; it should return 1 for a
collectible instance, and 0 for a non-collectible instance. The signature is
int tp_is_gc(PyObject *self)
(The only example of this are types themselves. The metatype, PyType_Type, defines this function to
distinguish between statically and dynamically allocated types.)
This field is inherited by subtypes. (VERSION NOTE: in Python 2.2, it was not inherited. It is
inherited in 2.2.1 and later versions.)
PyObject* PyTypeObject.tp_bases
Tuple of base types.
This is set for types created by a class statement. It should be NULL for statically defined types.
This field is not inherited.
PyObject* PyTypeObject.tp_mro
Tuple containing the expanded set of base types, starting with the type itself and ending with object,
in Method Resolution Order.
typedef struct {
binaryfunc nb_add;
binaryfunc nb_subtract;
binaryfunc nb_multiply;
binaryfunc nb_divide;
binaryfunc nb_remainder;
binaryfunc nb_divmod;
ternaryfunc nb_power;
unaryfunc nb_negative;
unaryfunc nb_positive;
unaryfunc nb_absolute;
inquiry nb_nonzero; /* Used by PyObject_IsTrue */
unaryfunc nb_invert;
binaryfunc nb_lshift;
binaryfunc nb_rshift;
(continues on next page)
Binary and ternary functions may receive different kinds of arguments, depending on the flag bit
Py_TPFLAGS_CHECKTYPES:
• If Py_TPFLAGS_CHECKTYPES is not set, the function arguments are guaranteed to be of the object’s
type; the caller is responsible for calling the coercion method specified by the nb_coerce member to
convert the arguments:
coercion PyNumberMethods.nb_coerce
This function is used by PyNumber_CoerceEx() and has the same signature. The first argument
is always a pointer to an object of the defined type. If the conversion to a common “larger” type
is possible, the function replaces the pointers with new references to the converted objects and
returns 0. If the conversion is not possible, the function returns 1. If an error condition is set, it
will return -1.
• If the Py_TPFLAGS_CHECKTYPES flag is set, binary and ternary functions must check the type of all
their operands, and implement the necessary conversions (at least one of the operands is an instance
of the defined type). This is the recommended way; with Python 3 coercion will disappear completely.
If the operation is not defined for the given operands, binary and ternary functions must return
Py_NotImplemented, if another error occurred they must return NULL and set an exception.
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 readbufferproc. 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 writebufferproc. This slot may be NULL if the
object does not allow writing into its returned buffers.
The third slot is bf_getsegcount, with type segcountproc. 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 charbufferproc. 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 the flag is present, bf_getcharbuffer 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.
Py_ssize_t (*readbufferproc)(PyObject *self, Py_ssize_t segment, void **ptrptr)
Return a pointer to a readable segment of the buffer in *ptrptr. This function is allowed to raise an
exception, in which case it must return -1. The segment which is specified 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 segment, and sets *ptrptr to a pointer to that memory.
Py_ssize_t (*writebufferproc)(PyObject *self, Py_ssize_t 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.
Py_ssize_t (*segcountproc)(PyObject *self, Py_ssize_t *lenp)
Return the number of memory segments which comprise the buffer. If lenp is not NULL, the imple-
mentation must report the sum of the sizes (in bytes) of all segments in *lenp. The function cannot
fail.
Py_ssize_t (*charbufferproc)(PyObject *self, Py_ssize_t segment, char **ptrptr)
Return the size of the segment segment that ptrptr is set to. *ptrptr is set to the memory buffer.
Returns -1 on error.
static int
my_traverse(Noddy *self, visitproc visit, void *arg)
{
Py_VISIT(self->foo);
Py_VISIT(self->bar);
return 0;
}
GLOSSARY
>>> The default Python prompt of the interactive shell. Often seen for code examples which can be executed
interactively in the interpreter.
... The default Python prompt of the interactive shell when entering code for an indented code block, when
within a pair of matching left and right delimiters (parentheses, square brackets, curly braces or triple
quotes), or after specifying a decorator.
2to3 A tool that tries to convert Python 2.x code to Python 3.x code by handling most of the incompati-
bilities which can be detected by parsing the source and traversing the parse tree.
2to3 is available in the standard library as lib2to3; a standalone entry point is provided as Tools/
scripts/2to3. See 2to3-reference.
abstract base class Abstract base classes complement duck-typing by providing a way to define interfaces
when other techniques like hasattr() would be clumsy or subtly wrong (for example with magic
methods). ABCs introduce virtual subclasses, which are classes that don’t inherit from a class but
are still recognized by isinstance() and issubclass(); see the abc module documentation. Python
comes with many built-in ABCs for data structures (in the collections module), numbers (in the
numbers module), and streams (in the io module). You can create your own ABCs with the abc
module.
argument A value passed to a function (or method) when calling the function. There are two types of
arguments:
• keyword argument: an argument preceded by an identifier (e.g. name=) in a function call or passed
as a value in a dictionary preceded by **. For example, 3 and 5 are both keyword arguments in
the following calls to complex():
complex(real=3, imag=5)
complex(**{'real': 3, 'imag': 5})
• positional argument: an argument that is not a keyword argument. Positional arguments can
appear at the beginning of an argument list and/or be passed as elements of an iterable preceded
by *. For example, 3 and 5 are both positional arguments in the following calls:
complex(3, 5)
complex(*(3, 5))
Arguments are assigned to the named local variables in a function body. See the calls section for the
rules governing this assignment. Syntactically, any expression can be used to represent an argument;
the evaluated value is assigned to the local variable.
See also the parameter glossary entry and the FAQ question on the difference between arguments and
parameters.
attribute A value associated with an object which is referenced by name using dotted expressions. For
example, if an object o has an attribute a it would be referenced as o.a.
155
The Python/C API, Release 2.7.15
BDFL Benevolent Dictator For Life, a.k.a. Guido van Rossum, Python’s creator.
bytes-like object An object that supports the buffer protocol, like str, bytearray or memoryview. Bytes-
like objects can be used for various operations that expect binary data, such as compression, saving to
a binary file or sending over a socket. Some operations need the binary data to be mutable, in which
case not all bytes-like objects can apply.
bytecode Python source code is compiled into bytecode, the internal representation of a Python program in
the CPython interpreter. The bytecode is also cached in .pyc and .pyo files so that executing the same
file is faster the second time (recompilation from source to bytecode can be avoided). This “intermediate
language” is said to run on a virtual machine that executes the machine code corresponding to each
bytecode. Do note that bytecodes are not expected to work between different Python virtual machines,
nor to be stable between Python releases.
A list of bytecode instructions can be found in the documentation for the dis module.
class A template for creating user-defined objects. Class definitions normally contain method definitions
which operate on instances of the class.
classic class Any class which does not inherit from object. See new-style class. Classic classes have been
removed in Python 3.
coercion The implicit conversion of an instance of one type to another during an operation which involves
two arguments of the same type. For example, int(3.15) converts the floating point number to the
integer 3, but in 3+4.5, each argument is of a different type (one int, one float), and both must be
converted to the same type before they can be added or it will raise a TypeError. Coercion between
two operands can be performed with the coerce built-in function; thus, 3+4.5 is equivalent to calling
operator.add(*coerce(3, 4.5)) and results in operator.add(3.0, 4.5). Without coercion, all
arguments of even compatible types would have to be normalized to the same value by the programmer,
e.g., float(3)+4.5 rather than just 3+4.5.
complex number An extension of the familiar real number system in which all numbers are expressed as
a sum of a real part and an imaginary part. Imaginary numbers are real multiples of the imaginary
unit (the square root of -1), often written i in mathematics or j in engineering. Python has built-in
support for complex numbers, which are written with this latter notation; the imaginary part is written
with a j suffix, e.g., 3+1j. To get access to complex equivalents of the math module, use cmath. Use
of complex numbers is a fairly advanced mathematical feature. If you’re not aware of a need for them,
it’s almost certain you can safely ignore them.
context manager An object which controls the environment seen in a with statement by defining
__enter__() and __exit__() methods. See PEP 343.
CPython The canonical implementation of the Python programming language, as distributed on
python.org. The term “CPython” is used when necessary to distinguish this implementation from
others such as Jython or IronPython.
decorator A function returning another function, usually applied as a function transformation using the
@wrapper syntax. Common examples for decorators are classmethod() and staticmethod().
The decorator syntax is merely syntactic sugar, the following two function definitions are semantically
equivalent:
def f(...):
...
f = staticmethod(f)
@staticmethod
def f(...):
...
The same concept exists for classes, but is less commonly used there. See the documentation for
function definitions and class definitions for more about decorators.
descriptor Any new-style object which defines the methods __get__(), __set__(), or __delete__().
When a class attribute is a descriptor, its special binding behavior is triggered upon attribute lookup.
Normally, using a.b to get, set or delete an attribute looks up the object named b in the class dictionary
for a, but if b is a descriptor, the respective descriptor method gets called. Understanding descriptors
is a key to a deep understanding of Python because they are the basis for many features including
functions, methods, properties, class methods, static methods, and reference to super classes.
For more information about descriptors’ methods, see descriptors.
dictionary An associative array, where arbitrary keys are mapped to values. The keys can be any object
with __hash__() and __eq__() methods. Called a hash in Perl.
dictionary view The objects returned from dict.viewkeys(), dict.viewvalues(), and dict.
viewitems() are called dictionary views. They provide a dynamic view on the dictionary’s entries,
which means that when the dictionary changes, the view reflects these changes. To force the dictionary
view to become a full list use list(dictview). See dict-views.
docstring A string literal which appears as the first expression in a class, function or module. While ignored
when the suite is executed, it is recognized by the compiler and put into the __doc__ attribute of the
enclosing class, function or module. Since it is available via introspection, it is the canonical place for
documentation of the object.
duck-typing A programming style which does not look at an object’s type to determine if it has the right
interface; instead, the method or attribute is simply called or used (“If it looks like a duck and quacks
like a duck, it must be a duck.”) By emphasizing interfaces rather than specific types, well-designed
code improves its flexibility by allowing polymorphic substitution. Duck-typing avoids tests using
type() or isinstance(). (Note, however, that duck-typing can be complemented with abstract base
classes.) Instead, it typically employs hasattr() tests or EAFP programming.
EAFP Easier to ask for forgiveness than permission. This common Python coding style assumes the
existence of valid keys or attributes and catches exceptions if the assumption proves false. This clean
and fast style is characterized by the presence of many try and except statements. The technique
contrasts with the LBYL style common to many other languages such as C.
expression A piece of syntax which can be evaluated to some value. In other words, an expression is
an accumulation of expression elements like literals, names, attribute access, operators or function
calls which all return a value. In contrast to many other languages, not all language constructs are
expressions. There are also statements which cannot be used as expressions, such as print or if.
Assignments are also statements, not expressions.
extension module A module written in C or C++, using Python’s C API to interact with the core and
with user code.
file object An object exposing a file-oriented API (with methods such as read() or write()) to an underly-
ing resource. Depending on the way it was created, a file object can mediate access to a real on-disk file
or to another type of storage or communication device (for example standard input/output, in-memory
buffers, sockets, pipes, etc.). File objects are also called file-like objects or streams.
There are actually three categories of file objects: raw binary files, buffered binary files and text files.
Their interfaces are defined in the io module. The canonical way to create a file object is by using the
open() function.
file-like object A synonym for file object.
finder An object that tries to find the loader for a module. It must implement a method named
find_module(). See PEP 302 for details.
floor division Mathematical division that rounds down to nearest integer. The floor division operator is
//. For example, the expression 11 // 4 evaluates to 2 in contrast to the 2.75 returned by float true
157
The Python/C API, Release 2.7.15
division. Note that (-11) // 4 is -3 because that is -2.75 rounded downward. See PEP 238.
function A series of statements which returns some value to a caller. It can also be passed zero or more
arguments which may be used in the execution of the body. See also parameter, method, and the
function section.
__future__ A pseudo-module which programmers can use to enable new language features which are
not compatible with the current interpreter. For example, the expression 11/4 currently evaluates to
2. If the module in which it is executed had enabled true division by executing:
the expression 11/4 would evaluate to 2.75. By importing the __future__ module and evaluating its
variables, you can see when a new feature was first added to the language and when it will become the
default:
garbage collection The process of freeing memory when it is not used anymore. Python performs garbage
collection via reference counting and a cyclic garbage collector that is able to detect and break reference
cycles.
generator A function which returns an iterator. It looks like a normal function except that it contains
yield statements for producing a series of values usable in a for-loop or that can be retrieved one
at a time with the next() function. Each yield temporarily suspends processing, remembering the
location execution state (including local variables and pending try-statements). When the generator
resumes, it picks up where it left off (in contrast to functions which start fresh on every invocation).
generator expression An expression that returns an iterator. It looks like a normal expression followed
by a for expression defining a loop variable, range, and an optional if expression. The combined
expression generates values for an enclosing function:
All of Python’s immutable built-in objects are hashable, while no mutable containers (such as lists or
dictionaries) are. Objects which are instances of user-defined classes are hashable by default; they all
compare unequal (except with themselves), and their hash value is derived from their id().
IDLE An Integrated Development Environment for Python. IDLE is a basic editor and interpreter envi-
ronment which ships with the standard distribution of Python.
immutable An object with a fixed value. Immutable objects include numbers, strings and tuples. Such an
object cannot be altered. A new object has to be created if a different value has to be stored. They
play an important role in places where a constant hash value is needed, for example as a key in a
dictionary.
integer division Mathematical division discarding any remainder. For example, the expression 11/4 cur-
rently evaluates to 2 in contrast to the 2.75 returned by float division. Also called floor division. When
dividing two integers the outcome will always be another integer (having the floor function applied
to it). However, if one of the operands is another numeric type (such as a float), the result will be
coerced (see coercion) to a common type. For example, an integer divided by a float will result in a
float value, possibly with a decimal fraction. Integer division can be forced by using the // operator
instead of the / operator. See also __future__.
importing The process by which Python code in one module is made available to Python code in another
module.
importer An object that both finds and loads a module; both a finder and loader object.
interactive Python has an interactive interpreter which means you can enter statements and expressions
at the interpreter prompt, immediately execute them and see their results. Just launch python with
no arguments (possibly by selecting it from your computer’s main menu). It is a very powerful way to
test out new ideas or inspect modules and packages (remember help(x)).
interpreted Python is an interpreted language, as opposed to a compiled one, though the distinction can
be blurry because of the presence of the bytecode compiler. This means that source files can be run
directly without explicitly creating an executable which is then run. Interpreted languages typically
have a shorter development/debug cycle than compiled ones, though their programs generally also run
more slowly. See also interactive.
iterable An object capable of returning its members one at a time. Examples of iterables include all sequence
types (such as list, str, and tuple) and some non-sequence types like dict and file and objects of
any classes you define with an __iter__() or __getitem__() method. Iterables can be used in a for
loop and in many other places where a sequence is needed (zip(), map(), …). When an iterable object
is passed as an argument to the built-in function iter(), it returns an iterator for the object. This
iterator is good for one pass over the set of values. When using iterables, it is usually not necessary
to call iter() or deal with iterator objects yourself. The for statement does that automatically for
you, creating a temporary unnamed variable to hold the iterator for the duration of the loop. See also
iterator, sequence, and generator.
iterator An object representing a stream of data. Repeated calls to the iterator’s next() method return
successive items in the stream. When no more data are available a StopIteration exception is raised
instead. At this point, the iterator object is exhausted and any further calls to its next() method
just raise StopIteration again. Iterators are required to have an __iter__() method that returns
the iterator object itself so every iterator is also iterable and may be used in most places where other
iterables are accepted. One notable exception is code which attempts multiple iteration passes. A
container object (such as a list) produces a fresh new iterator each time you pass it to the iter()
function or use it in a for loop. Attempting this with an iterator will just return the same exhausted
iterator object used in the previous iteration pass, making it appear like an empty container.
More information can be found in typeiter.
key function A key function or collation function is a callable that returns a value used for sorting or
ordering. For example, locale.strxfrm() is used to produce a sort key that is aware of locale specific
159
The Python/C API, Release 2.7.15
sort conventions.
A number of tools in Python accept key functions to control how elements are ordered or grouped.
They include min(), max(), sorted(), list.sort(), heapq.nsmallest(), heapq.nlargest(), and
itertools.groupby().
There are several ways to create a key function. For example. the str.lower() method can serve
as a key function for case insensitive sorts. Alternatively, an ad-hoc key function can be built from a
lambda expression such as lambda r: (r[0], r[2]). Also, the operator module provides three key
function constructors: attrgetter(), itemgetter(), and methodcaller(). See the Sorting HOW
TO for examples of how to create and use key functions.
keyword argument See argument.
lambda An anonymous inline function consisting of a single expression which is evaluated when the function
is called. The syntax to create a lambda function is lambda [parameters]: expression
LBYL Look before you leap. This coding style explicitly tests for pre-conditions before making calls or
lookups. This style contrasts with the EAFP approach and is characterized by the presence of many
if statements.
In a multi-threaded environment, the LBYL approach can risk introducing a race condition between
“the looking” and “the leaping”. For example, the code, if key in mapping: return mapping[key]
can fail if another thread removes key from mapping after the test, but before the lookup. This issue
can be solved with locks or by using the EAFP approach.
list A built-in Python sequence. Despite its name it is more akin to an array in other languages than to a
linked list since access to elements is O(1).
list comprehension A compact way to process all or part of the elements in a sequence and return a list
with the results. result = ["0x%02x" % x for x in range(256) if x % 2 == 0] generates a list
of strings containing even hex numbers (0x..) in the range from 0 to 255. The if clause is optional. If
omitted, all elements in range(256) are processed.
loader An object that loads a module. It must define a method named load_module(). A loader is typically
returned by a finder. See PEP 302 for details.
mapping A container object that supports arbitrary key lookups and implements the methods specified
in the Mapping or MutableMapping abstract base classes. Examples include dict, collections.
defaultdict, collections.OrderedDict and collections.Counter.
metaclass The class of a class. Class definitions create a class name, a class dictionary, and a list of base
classes. The metaclass is responsible for taking those three arguments and creating the class. Most
object oriented programming languages provide a default implementation. What makes Python special
is that it is possible to create custom metaclasses. Most users never need this tool, but when the need
arises, metaclasses can provide powerful, elegant solutions. They have been used for logging attribute
access, adding thread-safety, tracking object creation, implementing singletons, and many other tasks.
More information can be found in metaclasses.
method A function which is defined inside a class body. If called as an attribute of an instance of that
class, the method will get the instance object as its first argument (which is usually called self). See
function and nested scope.
method resolution order Method Resolution Order is the order in which base classes are searched for
a member during lookup. See The Python 2.3 Method Resolution Order for details of the algorithm
used by the Python interpreter since the 2.3 release.
module An object that serves as an organizational unit of Python code. Modules have a namespace
containing arbitrary Python objects. Modules are loaded into Python by the process of importing.
See also package.
MRO See method resolution order.
mutable Mutable objects can change their value but keep their id(). See also immutable.
named tuple Any tuple-like class whose indexable elements are also accessible using named attributes (for
example, time.localtime() returns a tuple-like object where the year is accessible either with an
index such as t[0] or with a named attribute like t.tm_year).
A named tuple can be a built-in type such as time.struct_time, or it can be created with a regular
class definition. A full featured named tuple can also be created with the factory function collections.
namedtuple(). The latter approach automatically provides extra features such as a self-documenting
representation like Employee(name='jones', title='programmer').
namespace The place where a variable is stored. Namespaces are implemented as dictionaries. There are
the local, global and built-in namespaces as well as nested namespaces in objects (in methods). Names-
paces support modularity by preventing naming conflicts. For instance, the functions __builtin__.
open() and os.open() are distinguished by their namespaces. Namespaces also aid readability and
maintainability by making it clear which module implements a function. For instance, writing random.
seed() or itertools.izip() makes it clear that those functions are implemented by the random and
itertools modules, respectively.
nested scope The ability to refer to a variable in an enclosing definition. For instance, a function defined
inside another function can refer to variables in the outer function. Note that nested scopes work only
for reference and not for assignment which will always write to the innermost scope. In contrast, local
variables both read and write in the innermost scope. Likewise, global variables read and write to the
global namespace.
new-style class Any class which inherits from object. This includes all built-in types like list and
dict. Only new-style classes can use Python’s newer, versatile features like __slots__, descriptors,
properties, and __getattribute__().
More information can be found in newstyle.
object Any data with state (attributes or value) and defined behavior (methods). Also the ultimate base
class of any new-style class.
package A Python module which can contain submodules or recursively, subpackages. Technically, a pack-
age is a Python module with an __path__ attribute.
parameter A named entity in a function (or method) definition that specifies an argument (or in some
cases, arguments) that the function can accept. There are four types of parameters:
• positional-or-keyword: specifies an argument that can be passed either positionally or as a keyword
argument. This is the default kind of parameter, for example foo and bar in the following:
• positional-only: specifies an argument that can be supplied only by position. Python has no
syntax for defining positional-only parameters. However, some built-in functions have positional-
only parameters (e.g. abs()).
• var-positional: specifies that an arbitrary sequence of positional arguments can be provided (in
addition to any positional arguments already accepted by other parameters). Such a parameter
can be defined by prepending the parameter name with *, for example args in the following:
• var-keyword: specifies that arbitrarily many keyword arguments can be provided (in addition to
any keyword arguments already accepted by other parameters). Such a parameter can be defined
by prepending the parameter name with **, for example kwargs in the example above.
Parameters can specify both optional and required arguments, as well as default values for some
optional arguments.
161
The Python/C API, Release 2.7.15
See also the argument glossary entry, the FAQ question on the difference between arguments and
parameters, and the function section.
PEP Python Enhancement Proposal. A PEP is a design document providing information to the Python
community, or describing a new feature for Python or its processes or environment. PEPs should
provide a concise technical specification and a rationale for proposed features.
PEPs are intended to be the primary mechanisms for proposing major new features, for collecting com-
munity input on an issue, and for documenting the design decisions that have gone into Python. The
PEP author is responsible for building consensus within the community and documenting dissenting
opinions.
See PEP 1.
positional argument See argument.
Python 3000 Nickname for the Python 3.x release line (coined long ago when the release of version 3 was
something in the distant future.) This is also abbreviated “Py3k”.
Pythonic An idea or piece of code which closely follows the most common idioms of the Python language,
rather than implementing code using concepts common to other languages. For example, a common
idiom in Python is to loop over all elements of an iterable using a for statement. Many other languages
don’t have this type of construct, so people unfamiliar with Python sometimes use a numerical counter
instead:
for i in range(len(food)):
print food[i]
reference count The number of references to an object. When the reference count of an object drops to
zero, it is deallocated. Reference counting is generally not visible to Python code, but it is a key element
of the CPython implementation. The sys module defines a getrefcount() function that programmers
can call to return the reference count for a particular object.
__slots__ A declaration inside a new-style class that saves memory by pre-declaring space for instance
attributes and eliminating instance dictionaries. Though popular, the technique is somewhat tricky to
get right and is best reserved for rare cases where there are large numbers of instances in a memory-
critical application.
sequence An iterable which supports efficient element access using integer indices via the __getitem__()
special method and defines a len() method that returns the length of the sequence. Some built-in
sequence types are list, str, tuple, and unicode. Note that dict also supports __getitem__() and
__len__(), but is considered a mapping rather than a sequence because the lookups use arbitrary
immutable keys rather than integers.
slice An object usually containing a portion of a sequence. A slice is created using the subscript notation,
[] with colons between numbers when several are given, such as in variable_name[1:3:5]. The
bracket (subscript) notation uses slice objects internally (or in older versions, __getslice__() and
__setslice__()).
special method A method that is called implicitly by Python to execute a certain operation on a type,
such as addition. Such methods have names starting and ending with double underscores. Special
methods are documented in specialnames.
statement A statement is part of a suite (a “block” of code). A statement is either an expression or one of
several constructs with a keyword, such as if, while or for.
struct sequence A tuple with named elements. Struct sequences expose an interface similiar to named
tuple in that elements can either be accessed either by index or as an attribute. However, they do
not have any of the named tuple methods like _make() or _asdict(). Examples of struct sequences
include sys.float_info and the return value of os.stat().
triple-quoted string A string which is bound by three instances of either a quotation mark (“) or an
apostrophe (‘). While they don’t provide any functionality not available with single-quoted strings,
they are useful for a number of reasons. They allow you to include unescaped single and double quotes
within a string and they can span multiple lines without the use of the continuation character, making
them especially useful when writing docstrings.
type The type of a Python object determines what kind of object it is; every object has a type. An object’s
type is accessible as its __class__ attribute or can be retrieved with type(obj).
universal newlines A manner of interpreting text streams in which all of the following are recognized as
ending a line: the Unix end-of-line convention '\n', the Windows convention '\r\n', and the old
Macintosh convention '\r'. See PEP 278 and PEP 3116, as well as str.splitlines() for an
additional use.
virtual environment A cooperatively isolated runtime environment that allows Python users and appli-
cations to install and upgrade Python distribution packages without interfering with the behaviour of
other Python applications running on the same system.
virtual machine A computer defined entirely in software. Python’s virtual machine executes the bytecode
emitted by the bytecode compiler.
Zen of Python Listing of Python design principles and philosophies that are helpful in understanding and
using the language. The listing can be found by typing “import this” at the interactive prompt.
163
The Python/C API, Release 2.7.15
These documents are generated from reStructuredText sources by Sphinx, a document processor specifically
written for the Python documentation.
Development of the documentation and its toolchain is an entirely volunteer effort, just like Python itself. If
you want to contribute, please take a look at the reporting-bugs page for information on how to do so. New
volunteers are always welcome!
Many thanks go to:
• Fred L. Drake, Jr., the creator of the original Python documentation toolset and writer of much of the
content;
• the Docutils project for creating reStructuredText and the Docutils suite;
• Fredrik Lundh for his Alternative Python Reference project from which Sphinx got many good ideas.
165
The Python/C API, Release 2.7.15
Python was created in the early 1990s by Guido van Rossum at Stichting Mathematisch Centrum (CWI, see
https://www.cwi.nl/) in the Netherlands as a successor of a language called ABC. Guido remains Python’s
principal author, although it includes many contributions from others.
In 1995, Guido continued his work on Python at the Corporation for National Research Initiatives (CNRI,
see https://www.cnri.reston.va.us/) in Reston, Virginia where he released several versions of the software.
In May 2000, Guido and the Python core development team moved to BeOpen.com to form the BeOpen
PythonLabs team. In October of the same year, the PythonLabs team moved to Digital Creations (now
Zope Corporation; see http://www.zope.com/). In 2001, the Python Software Foundation (PSF, see https:
//www.python.org/psf/) was formed, a non-profit organization created specifically to own Python-related
Intellectual Property. Zope Corporation is a sponsoring member of the PSF.
All Python releases are Open Source (see https://opensource.org/ for the Open Source Definition). Histor-
ically, most, but not all, Python releases have also been GPL-compatible; the table below summarizes the
various releases.
Note: GPL-compatible doesn’t mean that we’re distributing Python under the GPL. All Python licenses,
unlike the GPL, let you distribute a modified version without making your changes open source. The GPL-
compatible licenses make it possible to combine Python with other software that is released under the GPL;
the others don’t.
Thanks to the many outside volunteers who have worked under Guido’s direction to make these releases
possible.
167
The Python/C API, Release 2.7.15
2. Subject to the terms and conditions of this License Agreement, PSF hereby
grants Licensee a nonexclusive, royalty-free, world-wide license to reproduce,
analyze, test, perform and/or display publicly, prepare derivative works,
distribute, and otherwise use Python 2.7.15 alone or in any derivative
version, provided, however, that PSF's License Agreement and PSF's notice of
copyright, i.e., "Copyright © 2001-2018 Python Software Foundation; All Rights
Reserved" are retained in Python 2.7.15 alone or in any derivative version
prepared by Licensee.
5. PSF SHALL NOT BE LIABLE TO LICENSEE OR ANY OTHER USERS OF PYTHON 2.7.15
FOR ANY INCIDENTAL, SPECIAL, OR CONSEQUENTIAL DAMAGES OR LOSS AS A RESULT OF
MODIFYING, DISTRIBUTING, OR OTHERWISE USING PYTHON 2.7.15, OR ANY DERIVATIVE
THEREOF, EVEN IF ADVISED OF THE POSSIBILITY THEREOF.
2. Subject to the terms and conditions of this BeOpen Python License Agreement,
BeOpen hereby grants Licensee a non-exclusive, royalty-free, world-wide license
to reproduce, analyze, test, perform and/or display publicly, prepare derivative
works, distribute, and otherwise use the Software alone or in any derivative
version, provided, however, that the BeOpen Python License is retained in the
Software, alone or in any derivative version prepared by Licensee.
4. BEOPEN SHALL NOT BE LIABLE TO LICENSEE OR ANY OTHER USERS OF THE SOFTWARE FOR
ANY INCIDENTAL, SPECIAL, OR CONSEQUENTIAL DAMAGES OR LOSS AS A RESULT OF USING,
MODIFYING OR DISTRIBUTING THE SOFTWARE, OR ANY DERIVATIVE THEREOF, EVEN IF
ADVISED OF THE POSSIBILITY THEREOF.
2. Subject to the terms and conditions of this License Agreement, CNRI hereby
grants Licensee a nonexclusive, royalty-free, world-wide license to reproduce,
analyze, test, perform and/or display publicly, prepare derivative works,
distribute, and otherwise use Python 1.6.1 alone or in any derivative version,
provided, however, that CNRI's License Agreement and CNRI's notice of copyright,
i.e., "Copyright © 1995-2001 Corporation for National Research Initiatives; All
Rights Reserved" are retained in Python 1.6.1 alone or in any derivative version
prepared by Licensee. Alternately, in lieu of CNRI's License Agreement,
Licensee may substitute the following text (omitting the quotes): "Python 1.6.1
is made available subject to the terms and conditions in CNRI's License
(continues on next page)
C.2. Terms and conditions for accessing or otherwise using Python 169
The Python/C API, Release 2.7.15
4. CNRI is making Python 1.6.1 available to Licensee on an "AS IS" basis. CNRI
MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. BY WAY OF EXAMPLE,
BUT NOT LIMITATION, CNRI MAKES NO AND DISCLAIMS ANY REPRESENTATION OR WARRANTY
OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF
PYTHON 1.6.1 WILL NOT INFRINGE ANY THIRD PARTY RIGHTS.
5. CNRI SHALL NOT BE LIABLE TO LICENSEE OR ANY OTHER USERS OF PYTHON 1.6.1 FOR
ANY INCIDENTAL, SPECIAL, OR CONSEQUENTIAL DAMAGES OR LOSS AS A RESULT OF
MODIFYING, DISTRIBUTING, OR OTHERWISE USING PYTHON 1.6.1, OR ANY DERIVATIVE
THEREOF, EVEN IF ADVISED OF THE POSSIBILITY THEREOF.
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted, provided that
the above copyright notice appear in all copies and that both that copyright
notice and this permission notice appear in supporting documentation, and that
the name of Stichting Mathematisch Centrum or CWI not be used in advertising or
publicity pertaining to distribution of the software without specific, written
prior permission.
(continues on next page)
C.3.2 Sockets
The socket module uses the functions, getaddrinfo(), and getnameinfo(), which are coded in separate
source files from the WIDE Project, http://www.wide.ad.jp/.
THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
---------------------------------------------------------------------
/ Copyright (c) 1996. \
| The Regents of the University of California. |
| All rights reserved. |
| |
| Permission to use, copy, modify, and distribute this software for |
| any purpose without fee is hereby granted, provided that this en- |
| tire notice is included in all copies of any software which is or |
| includes a copy or modification of this software and in all |
| copies of the supporting documentation for such software. |
(continues on next page)
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
L. Peter Deutsch
[email protected]
This code implements the MD5 Algorithm defined in RFC 1321, whose
text is available at
(continues on next page)
Permission to use, copy, modify, and distribute this Python software and
its associated documentation for any purpose without fee is hereby
granted, provided that the above copyright notice appears in all copies,
and that both that copyright notice and this permission notice appear in
supporting documentation, and that the name of neither Automatrix,
Bioreason or Mojam Media be used in advertising or publicity pertaining to
distribution of the software without specific, written prior permission.
SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANT-
ABILITY AND FITNESS. IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR
BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
OF THIS SOFTWARE.
C.3.10 test_epoll
The test_epoll contains the following notice:
Copyright (c) 2000 Doug White, 2006 James Knight, 2007 Christian Heimes
All rights reserved.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
C.3.13 OpenSSL
The modules hashlib, posix, ssl, crypt use the OpenSSL library for added performance if made available
by the operating system. Additionally, the Windows and Mac OS X installers for Python may include a
copy of the OpenSSL libraries, so we include a copy of the OpenSSL license here:
LICENSE ISSUES
==============
The OpenSSL toolkit stays under a dual license, i.e. both the conditions of
the OpenSSL License and the original SSLeay license apply to the toolkit.
See below for the actual license texts. Actually both licenses are BSD-style
Open Source licenses. In case of any license issues related to OpenSSL
please contact [email protected].
OpenSSL License
---------------
/* ====================================================================
* Copyright (c) 1998-2008 The OpenSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
(continues on next page)
C.3.14 expat
The pyexpat extension is built using an included copy of the expat sources unless the build is configured
--with-system-expat:
Copyright (c) 1998, 1999, 2000 Thai Open Source Software Center Ltd
and Clark Cooper
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
C.3.15 libffi
The _ctypes extension is built using an included copy of the libffi sources unless the build is configured
--with-system-libffi:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
C.3.16 zlib
The zlib extension is built using an included copy of the zlib sources if the zlib version found on the system
is too old to be used for the build:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
COPYRIGHT
See History and License for complete license and permissions information.
183
The Python/C API, Release 2.7.15
185
The Python/C API, Release 2.7.15
186 Index
The Python/C API, Release 2.7.15
Index 187
The Python/C API, Release 2.7.15
188 Index
The Python/C API, Release 2.7.15
Index 189
The Python/C API, Release 2.7.15
190 Index
The Python/C API, Release 2.7.15
Index 191
The Python/C API, Release 2.7.15
192 Index
The Python/C API, Release 2.7.15
Index 193
The Python/C API, Release 2.7.15
194 Index
The Python/C API, Release 2.7.15
Index 195
The Python/C API, Release 2.7.15
196 Index
The Python/C API, Release 2.7.15
Index 197
The Python/C API, Release 2.7.15
S U
search ULONG_MAX, 62
path, module, 9, 111, 112 unicode
segcountproc (C type), 153 built-in function, 45
sequence, 162 universal newlines, 163
object, 65
198 Index
The Python/C API, Release 2.7.15
V
version (in module sys), 113
virtual environment, 163
virtual machine, 163
visitproc (C type), 154
W
writebufferproc (C type), 152
Z
Zen of Python, 163
Index 199