summaryrefslogtreecommitdiffstats
path: root/Doc/c-api
diff options
context:
space:
mode:
Diffstat (limited to 'Doc/c-api')
-rw-r--r--Doc/c-api/abstract.rst5
-rw-r--r--Doc/c-api/allocation.rst69
-rw-r--r--Doc/c-api/apiabiversion.rst39
-rw-r--r--Doc/c-api/arg.rst875
-rw-r--r--Doc/c-api/bool.rst10
-rw-r--r--Doc/c-api/buffer.rst738
-rw-r--r--Doc/c-api/bytearray.rst16
-rw-r--r--Doc/c-api/bytes.rst205
-rw-r--r--Doc/c-api/call.rst411
-rw-r--r--Doc/c-api/capsule.rst42
-rw-r--r--Doc/c-api/cell.rst14
-rw-r--r--Doc/c-api/class.rst65
-rw-r--r--Doc/c-api/cobject.rst59
-rw-r--r--Doc/c-api/code.rst23
-rw-r--r--Doc/c-api/codec.rst15
-rw-r--r--Doc/c-api/complex.rst23
-rw-r--r--Doc/c-api/concrete.rst40
-rw-r--r--Doc/c-api/contextvars.rst144
-rw-r--r--Doc/c-api/conversion.rst84
-rw-r--r--Doc/c-api/coro.rst34
-rw-r--r--Doc/c-api/datetime.rst166
-rw-r--r--Doc/c-api/descriptor.rst19
-rw-r--r--Doc/c-api/dict.rst114
-rw-r--r--Doc/c-api/exceptions.rst753
-rw-r--r--Doc/c-api/file.rst166
-rw-r--r--Doc/c-api/float.rst55
-rw-r--r--Doc/c-api/function.rst41
-rw-r--r--Doc/c-api/gcsupport.rst34
-rw-r--r--Doc/c-api/gen.rst26
-rw-r--r--Doc/c-api/import.rst230
-rw-r--r--Doc/c-api/index.rst3
-rw-r--r--Doc/c-api/init.rst946
-rw-r--r--Doc/c-api/init_config.rst1056
-rw-r--r--Doc/c-api/int.rst139
-rw-r--r--Doc/c-api/intro.rst219
-rw-r--r--Doc/c-api/iter.rst15
-rw-r--r--Doc/c-api/iterator.rst14
-rw-r--r--Doc/c-api/list.rst68
-rw-r--r--Doc/c-api/long.rst307
-rw-r--r--Doc/c-api/mapping.rst91
-rw-r--r--Doc/c-api/marshal.rst64
-rw-r--r--Doc/c-api/memory.rst405
-rw-r--r--Doc/c-api/memoryview.rst63
-rw-r--r--Doc/c-api/method.rst74
-rw-r--r--Doc/c-api/module.rst440
-rw-r--r--Doc/c-api/none.rst8
-rw-r--r--Doc/c-api/number.rst160
-rw-r--r--Doc/c-api/objbuffer.rst49
-rw-r--r--Doc/c-api/object.rst306
-rw-r--r--Doc/c-api/objimpl.rst3
-rw-r--r--Doc/c-api/refcounting.rst27
-rw-r--r--Doc/c-api/reflection.rst14
-rw-r--r--Doc/c-api/sequence.rst131
-rw-r--r--Doc/c-api/set.rst47
-rw-r--r--Doc/c-api/slice.rst91
-rw-r--r--Doc/c-api/stable.rst38
-rw-r--r--Doc/c-api/string.rst333
-rw-r--r--Doc/c-api/structures.rst259
-rw-r--r--Doc/c-api/sys.rst311
-rw-r--r--Doc/c-api/tuple.rst181
-rw-r--r--Doc/c-api/type.rst158
-rw-r--r--Doc/c-api/typeobj.rst2066
-rw-r--r--Doc/c-api/unicode.rst1517
-rw-r--r--Doc/c-api/utilities.rst3
-rw-r--r--Doc/c-api/veryhigh.rst211
-rw-r--r--Doc/c-api/weakref.rst26
66 files changed, 4550 insertions, 9808 deletions
diff --git a/Doc/c-api/abstract.rst b/Doc/c-api/abstract.rst
index 1823f9d..bc9001c 100644
--- a/Doc/c-api/abstract.rst
+++ b/Doc/c-api/abstract.rst
@@ -1,4 +1,5 @@
-.. highlight:: c
+.. highlightlang:: c
+
.. _abstract:
@@ -18,10 +19,8 @@ but whose items have not been set to some non-\ ``NULL`` value yet.
.. toctree::
object.rst
- call.rst
number.rst
sequence.rst
mapping.rst
iter.rst
- buffer.rst
objbuffer.rst
diff --git a/Doc/c-api/allocation.rst b/Doc/c-api/allocation.rst
index 33b0c06..7f7435f 100644
--- a/Doc/c-api/allocation.rst
+++ b/Doc/c-api/allocation.rst
@@ -1,4 +1,4 @@
-.. highlight:: c
+.. highlightlang:: c
.. _allocating-objects:
@@ -11,6 +11,13 @@ Allocating Objects on the Heap
.. c:function:: PyVarObject* _PyObject_NewVar(PyTypeObject *type, Py_ssize_t size)
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` type for *size*. This might require
+ changes in your code for properly supporting 64-bit systems.
+
+
+.. c:function:: void _PyObject_Del(PyObject *op)
+
.. c:function:: PyObject* PyObject_Init(PyObject *op, PyTypeObject *type)
@@ -26,6 +33,10 @@ Allocating Objects on the Heap
This does everything :c:func:`PyObject_Init` does, and also initializes the
length information for a variable-size object.
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` type for *size*. This might require
+ changes in your code for properly supporting 64-bit systems.
+
.. c:function:: TYPE* PyObject_New(TYPE, PyTypeObject *type)
@@ -47,8 +58,12 @@ Allocating Objects on the Heap
fields into the same allocation decreases the number of allocations,
improving the memory management efficiency.
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` type for *size*. This might require
+ changes in your code for properly supporting 64-bit systems.
+
-.. c:function:: void PyObject_Del(void *op)
+.. c:function:: void PyObject_Del(PyObject *op)
Releases memory allocated to an object using :c:func:`PyObject_New` or
:c:func:`PyObject_NewVar`. This is normally called from the
@@ -57,15 +72,51 @@ Allocating Objects on the Heap
longer a valid Python object.
-.. c:var:: PyObject _Py_NoneStruct
+.. c:function:: PyObject* Py_InitModule(char *name, PyMethodDef *methods)
- Object which is visible in Python as ``None``. This should only be accessed
- using the :c:macro:`Py_None` macro, which evaluates to a pointer to this
- object.
+ Create a new module object based on a name and table of functions,
+ returning the new module object.
+
+ .. versionchanged:: 2.3
+ Older versions of Python did not support *NULL* as the value for the
+ *methods* argument.
+
+
+.. c:function:: PyObject* Py_InitModule3(char *name, PyMethodDef *methods, char *doc)
+ Create a new module object based on a name and table of functions,
+ returning the new module object. If *doc* is non-*NULL*, it will be used
+ to define the docstring for the module.
-.. seealso::
+ .. versionchanged:: 2.3
+ Older versions of Python did not support *NULL* as the value for the
+ *methods* argument.
- :c:func:`PyModule_Create`
- To allocate and create extension modules.
+.. c:function:: PyObject* Py_InitModule4(char *name, PyMethodDef *methods, char *doc, PyObject *self, int apiver)
+
+ Create a new module object based on a name and table of functions,
+ returning the new module object. If *doc* is non-*NULL*, it will be used
+ to define the docstring for the module. If *self* is non-*NULL*, it will
+ 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
+ :const:`PYTHON_API_VERSION`.
+
+ .. note::
+
+ Most uses of this function should probably be using the
+ :c:func:`Py_InitModule3` instead; only use this if you are sure you need
+ it.
+
+ .. versionchanged:: 2.3
+ Older versions of Python did not support *NULL* as the value for the
+ *methods* argument.
+
+
+.. c:var:: 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.
diff --git a/Doc/c-api/apiabiversion.rst b/Doc/c-api/apiabiversion.rst
deleted file mode 100644
index b8a8f2f..0000000
--- a/Doc/c-api/apiabiversion.rst
+++ /dev/null
@@ -1,39 +0,0 @@
-.. highlight:: c
-
-.. _apiabiversion:
-
-***********************
-API and ABI Versioning
-***********************
-
-``PY_VERSION_HEX`` is the Python version number encoded in a single integer.
-
-For example if the ``PY_VERSION_HEX`` is set to ``0x030401a2``, the underlying
-version information can be found by treating it as a 32 bit number in
-the following manner:
-
- +-------+-------------------------+------------------------------------------------+
- | Bytes | Bits (big endian order) | Meaning |
- +=======+=========================+================================================+
- | ``1`` | ``1-8`` | ``PY_MAJOR_VERSION`` (the ``3`` in |
- | | | ``3.4.1a2``) |
- +-------+-------------------------+------------------------------------------------+
- | ``2`` | ``9-16`` | ``PY_MINOR_VERSION`` (the ``4`` in |
- | | | ``3.4.1a2``) |
- +-------+-------------------------+------------------------------------------------+
- | ``3`` | ``17-24`` | ``PY_MICRO_VERSION`` (the ``1`` in |
- | | | ``3.4.1a2``) |
- +-------+-------------------------+------------------------------------------------+
- | ``4`` | ``25-28`` | ``PY_RELEASE_LEVEL`` (``0xA`` for alpha, |
- | | | ``0xB`` for beta, ``0xC`` for release |
- | | | candidate and ``0xF`` for final), in this |
- | | | case it is alpha. |
- +-------+-------------------------+------------------------------------------------+
- | | ``29-32`` | ``PY_RELEASE_SERIAL`` (the ``2`` in |
- | | | ``3.4.1a2``, zero for final releases) |
- +-------+-------------------------+------------------------------------------------+
-
-Thus ``3.4.1a2`` is hexversion ``0x030401a2``.
-
-All the given macros are defined in :source:`Include/patchlevel.h`.
-
diff --git a/Doc/c-api/arg.rst b/Doc/c-api/arg.rst
index f17c63d..7375683 100644
--- a/Doc/c-api/arg.rst
+++ b/Doc/c-api/arg.rst
@@ -1,4 +1,4 @@
-.. highlight:: c
+.. highlightlang:: c
.. _arg-parsing:
@@ -10,441 +10,347 @@ methods. Additional information and examples are available in
:ref:`extending-index`.
The first three of these functions described, :c:func:`PyArg_ParseTuple`,
-:c:func:`PyArg_ParseTupleAndKeywords`, and :c:func:`PyArg_Parse`, all use *format
-strings* which are used to tell the function about the expected arguments. The
-format strings use the same syntax for each of these functions.
-
------------------
-Parsing arguments
------------------
+:c:func:`PyArg_ParseTupleAndKeywords`, and :c:func:`PyArg_Parse`, all use
+*format strings* which are used to tell the function about the expected
+arguments. The format strings use the same syntax for each of these
+functions.
A format string consists of zero or more "format units." A format unit
-describes one Python object; it is usually a single character or a parenthesized
-sequence of format units. With a few exceptions, a format unit that is not a
-parenthesized sequence normally corresponds to a single address argument to
-these functions. In the following description, the quoted form is the format
-unit; the entry in (round) parentheses is the Python object type that matches
-the format unit; and the entry in [square] brackets is the type of the C
-variable(s) whose address should be passed.
-
-Strings and buffers
--------------------
+describes one Python object; it is usually a single character or a
+parenthesized sequence of format units. With a few exceptions, a format unit
+that is not a parenthesized sequence normally corresponds to a single address
+argument to these functions. In the following description, the quoted form is
+the format unit; the entry in (round) parentheses is the Python object type
+that matches the format unit; and the entry in [square] brackets is the type
+of the C variable(s) whose address should be passed.
These formats allow accessing an object as a contiguous chunk of memory.
You don't have to provide raw storage for the returned unicode or bytes
-area.
-
-In general, when a format sets a pointer to a buffer, the buffer is
-managed by the corresponding Python object, and the buffer shares
-the lifetime of this object. You won't have to release any memory yourself.
-The only exceptions are ``es``, ``es#``, ``et`` and ``et#``.
-
-However, when a :c:type:`Py_buffer` structure gets filled, the underlying
-buffer is locked so that the caller can subsequently use the buffer even
-inside a :c:type:`Py_BEGIN_ALLOW_THREADS` block without the risk of mutable data
-being resized or destroyed. As a result, **you have to call**
-:c:func:`PyBuffer_Release` after you have finished processing the data (or
-in any early abort case).
-
-Unless otherwise stated, buffers are not NUL-terminated.
-
-Some formats require a read-only :term:`bytes-like object`, and set a
-pointer instead of a buffer structure. They work by checking that
-the object's :c:member:`PyBufferProcs.bf_releasebuffer` field is ``NULL``,
-which disallows mutable objects such as :class:`bytearray`.
-
-.. note::
-
- For all ``#`` variants of formats (``s#``, ``y#``, etc.), the type of
- the length argument (int or :c:type:`Py_ssize_t`) is controlled by
- defining the macro :c:macro:`PY_SSIZE_T_CLEAN` before including
- :file:`Python.h`. If the macro was defined, length is a
- :c:type:`Py_ssize_t` rather than an :c:type:`int`. This behavior will change
- in a future Python version to only support :c:type:`Py_ssize_t` and
- drop :c:type:`int` support. It is best to always define :c:macro:`PY_SSIZE_T_CLEAN`.
-
-
-``s`` (:class:`str`) [const char \*]
- Convert a Unicode object to a C pointer to a character string.
- A pointer to an existing string is stored in the character pointer
- variable whose address you pass. The C string is NUL-terminated.
- The Python string must not contain embedded null code points; if it does,
- a :exc:`ValueError` exception is raised. Unicode objects are converted
- to C strings using ``'utf-8'`` encoding. If this conversion fails, a
- :exc:`UnicodeError` is raised.
-
- .. note::
- This format does not accept :term:`bytes-like objects
- <bytes-like object>`. If you want to accept
- filesystem paths and convert them to C character strings, it is
- preferable to use the ``O&`` format with :c:func:`PyUnicode_FSConverter`
- as *converter*.
-
- .. versionchanged:: 3.5
- Previously, :exc:`TypeError` was raised when embedded null code points
- were encountered in the Python string.
-
-``s*`` (:class:`str` or :term:`bytes-like object`) [Py_buffer]
- This format accepts Unicode objects as well as bytes-like objects.
- It fills a :c:type:`Py_buffer` structure provided by the caller.
- In this case the resulting C string may contain embedded NUL bytes.
- Unicode objects are converted to C strings using ``'utf-8'`` encoding.
-
-``s#`` (:class:`str`, read-only :term:`bytes-like object`) [const char \*, int or :c:type:`Py_ssize_t`]
- Like ``s*``, except that it doesn't accept mutable objects.
- The result is stored into two C variables,
- the first one a pointer to a C string, the second one its length.
- The string may contain embedded null bytes. Unicode objects are converted
- to C strings using ``'utf-8'`` encoding.
-
-``z`` (:class:`str` or ``None``) [const char \*]
+area. Also, you won't have to release any memory yourself, except with the
+``es``, ``es#``, ``et`` and ``et#`` formats.
+
+``s`` (string or Unicode) [const char \*]
+ Convert a Python string or Unicode object to a C pointer to a character
+ string. You must not provide storage for the string itself; a pointer to
+ an existing string is stored into the character pointer variable whose
+ address you pass. The C string is NUL-terminated. The Python string must
+ not contain embedded NUL bytes; if it does, a :exc:`TypeError` exception is
+ raised. Unicode objects are converted to C strings using the default
+ encoding. If this conversion fails, a :exc:`UnicodeError` is raised.
+
+``s#`` (string, Unicode or any read buffer compatible object) [const char \*, int (or :c:type:`Py_ssize_t`, see below)]
+ This variant on ``s`` stores into two C variables, the first one a pointer
+ to a character string, the second one its length. In this case the Python
+ string may contain embedded null bytes. Unicode objects pass back a
+ pointer to the default encoded string version of the object if such a
+ conversion is possible. All other read-buffer compatible objects pass back
+ a reference to the raw internal data representation.
+
+ Starting with Python 2.5 the type of the length argument can be controlled
+ by defining the macro :c:macro:`PY_SSIZE_T_CLEAN` before including
+ :file:`Python.h`. If the macro is defined, length is a :c:type:`Py_ssize_t`
+ rather than an int.
+
+``s*`` (string, Unicode, or any buffer compatible object) [Py_buffer]
+ Similar to ``s#``, this code fills a Py_buffer structure provided by the
+ caller. The buffer gets locked, so that the caller can subsequently use
+ the buffer even inside a ``Py_BEGIN_ALLOW_THREADS`` block; the caller is
+ responsible for calling ``PyBuffer_Release`` with the structure after it
+ has processed the data.
+
+ .. versionadded:: 2.6
+
+``z`` (string, Unicode or ``None``) [const char \*]
Like ``s``, but the Python object may also be ``None``, in which case the C
- pointer is set to ``NULL``.
-
-``z*`` (:class:`str`, :term:`bytes-like object` or ``None``) [Py_buffer]
- Like ``s*``, but the Python object may also be ``None``, in which case the
- ``buf`` member of the :c:type:`Py_buffer` structure is set to ``NULL``.
-
-``z#`` (:class:`str`, read-only :term:`bytes-like object` or ``None``) [const char \*, int]
- Like ``s#``, but the Python object may also be ``None``, in which case the C
- pointer is set to ``NULL``.
-
-``y`` (read-only :term:`bytes-like object`) [const char \*]
- This format converts a bytes-like object to a C pointer to a character
- string; it does not accept Unicode objects. The bytes buffer must not
- contain embedded null bytes; if it does, a :exc:`ValueError`
- exception is raised.
-
- .. versionchanged:: 3.5
- Previously, :exc:`TypeError` was raised when embedded null bytes were
- encountered in the bytes buffer.
-
-``y*`` (:term:`bytes-like object`) [Py_buffer]
- This variant on ``s*`` doesn't accept Unicode objects, only
- bytes-like objects. **This is the recommended way to accept
- binary data.**
-
-``y#`` (read-only :term:`bytes-like object`) [const char \*, int]
- This variant on ``s#`` doesn't accept Unicode objects, only bytes-like
- objects.
-
-``S`` (:class:`bytes`) [PyBytesObject \*]
- Requires that the Python object is a :class:`bytes` object, without
- attempting any conversion. Raises :exc:`TypeError` if the object is not
- a bytes object. The C variable may also be declared as :c:type:`PyObject\*`.
-
-``Y`` (:class:`bytearray`) [PyByteArrayObject \*]
- Requires that the Python object is a :class:`bytearray` object, without
- attempting any conversion. Raises :exc:`TypeError` if the object is not
- a :class:`bytearray` object. The C variable may also be declared as :c:type:`PyObject\*`.
-
-``u`` (:class:`str`) [const Py_UNICODE \*]
- Convert a Python Unicode object to a C pointer to a NUL-terminated buffer of
- Unicode characters. You must pass the address of a :c:type:`Py_UNICODE`
- pointer variable, which will be filled with the pointer to an existing
- Unicode buffer. Please note that the width of a :c:type:`Py_UNICODE`
- character depends on compilation options (it is either 16 or 32 bits).
- The Python string must not contain embedded null code points; if it does,
- a :exc:`ValueError` exception is raised.
-
- .. versionchanged:: 3.5
- Previously, :exc:`TypeError` was raised when embedded null code points
- were encountered in the Python string.
-
- .. deprecated-removed:: 3.3 4.0
- Part of the old-style :c:type:`Py_UNICODE` API; please migrate to using
- :c:func:`PyUnicode_AsWideCharString`.
-
-``u#`` (:class:`str`) [const Py_UNICODE \*, int]
- This variant on ``u`` stores into two C variables, the first one a pointer to a
- Unicode data buffer, the second one its length. This variant allows
- null code points.
-
- .. deprecated-removed:: 3.3 4.0
- Part of the old-style :c:type:`Py_UNICODE` API; please migrate to using
- :c:func:`PyUnicode_AsWideCharString`.
-
-``Z`` (:class:`str` or ``None``) [const Py_UNICODE \*]
- Like ``u``, but the Python object may also be ``None``, in which case the
- :c:type:`Py_UNICODE` pointer is set to ``NULL``.
-
- .. deprecated-removed:: 3.3 4.0
- Part of the old-style :c:type:`Py_UNICODE` API; please migrate to using
- :c:func:`PyUnicode_AsWideCharString`.
-
-``Z#`` (:class:`str` or ``None``) [const Py_UNICODE \*, int]
- Like ``u#``, but the Python object may also be ``None``, in which case the
- :c:type:`Py_UNICODE` pointer is set to ``NULL``.
-
- .. deprecated-removed:: 3.3 4.0
- Part of the old-style :c:type:`Py_UNICODE` API; please migrate to using
- :c:func:`PyUnicode_AsWideCharString`.
-
-``U`` (:class:`str`) [PyObject \*]
- Requires that the Python object is a Unicode object, without attempting
- any conversion. Raises :exc:`TypeError` if the object is not a Unicode
- object. The C variable may also be declared as :c:type:`PyObject\*`.
-
-``w*`` (read-write :term:`bytes-like object`) [Py_buffer]
- This format accepts any object which implements the read-write buffer
- interface. It fills a :c:type:`Py_buffer` structure provided by the caller.
- The buffer may contain embedded null bytes. The caller have to call
- :c:func:`PyBuffer_Release` when it is done with the buffer.
-
-``es`` (:class:`str`) [const char \*encoding, char \*\*buffer]
- This variant on ``s`` is used for encoding Unicode into a character buffer.
- It only works for encoded data without embedded NUL bytes.
+ pointer is set to *NULL*.
+
+``z#`` (string, Unicode, ``None`` or any read buffer compatible object) [const char \*, int]
+ This is to ``s#`` as ``z`` is to ``s``.
+
+``z*`` (string, Unicode, ``None`` or any buffer compatible object) [Py_buffer]
+ This is to ``s*`` as ``z`` is to ``s``.
+
+ .. versionadded:: 2.6
+
+``u`` (Unicode) [Py_UNICODE \*]
+ Convert a Python Unicode object to a C pointer to a NUL-terminated buffer
+ of 16-bit Unicode (UTF-16) data. As with ``s``, there is no need to
+ provide storage for the Unicode data buffer; a pointer to the existing
+ Unicode data is stored into the :c:type:`Py_UNICODE` pointer variable whose
+ address you pass.
+
+``u#`` (Unicode) [Py_UNICODE \*, int]
+ This variant on ``u`` stores into two C variables, the first one a pointer
+ to a Unicode data buffer, the second one its length. Non-Unicode objects
+ are handled by interpreting their read-buffer pointer as pointer to a
+ :c:type:`Py_UNICODE` array.
+
+``es`` (string, Unicode or character buffer compatible object) [const char \*encoding, char \*\*buffer]
+ This variant on ``s`` is used for encoding Unicode and objects convertible
+ to Unicode into a character buffer. It only works for encoded data without
+ embedded NUL bytes.
This format requires two arguments. The first is only used as input, and
- must be a :c:type:`const char\*` which points to the name of an encoding as a
- NUL-terminated string, or ``NULL``, in which case ``'utf-8'`` encoding is used.
- An exception is raised if the named encoding is not known to Python. The
- second argument must be a :c:type:`char\*\*`; the value of the pointer it
- references will be set to a buffer with the contents of the argument text.
- The text will be encoded in the encoding specified by the first argument.
-
- :c:func:`PyArg_ParseTuple` will allocate a buffer of the needed size, copy the
- encoded data into this buffer and adjust *\*buffer* to reference the newly
- allocated storage. The caller is responsible for calling :c:func:`PyMem_Free` to
- free the allocated buffer after use.
-
-``et`` (:class:`str`, :class:`bytes` or :class:`bytearray`) [const char \*encoding, char \*\*buffer]
- Same as ``es`` except that byte string objects are passed through without
- recoding them. Instead, the implementation assumes that the byte string object uses
- the encoding passed in as parameter.
-
-``es#`` (:class:`str`) [const char \*encoding, char \*\*buffer, int \*buffer_length]
- This variant on ``s#`` is used for encoding Unicode into a character buffer.
- Unlike the ``es`` format, this variant allows input data which contains NUL
- characters.
-
- It requires three arguments. The first is only used as input, and must be a
- :c:type:`const char\*` which points to the name of an encoding as a
- NUL-terminated string, or ``NULL``, in which case ``'utf-8'`` encoding is used.
- An exception is raised if the named encoding is not known to Python. The
- second argument must be a :c:type:`char\*\*`; the value of the pointer it
- references will be set to a buffer with the contents of the argument text.
- The text will be encoded in the encoding specified by the first argument.
- The third argument must be a pointer to an integer; the referenced integer
- will be set to the number of bytes in the output buffer.
+ must be a :c:type:`const char\*` which points to the name of an encoding as
+ a NUL-terminated string, or *NULL*, in which case the default encoding is
+ used. An exception is raised if the named encoding is not known to Python.
+ The second argument must be a :c:type:`char\*\*`; the value of the pointer
+ it references will be set to a buffer with the contents of the argument
+ text. The text will be encoded in the encoding specified by the first
+ argument.
+
+ :c:func:`PyArg_ParseTuple` will allocate a buffer of the needed size, copy
+ the encoded data into this buffer and adjust *\*buffer* to reference the
+ newly allocated storage. The caller is responsible for calling
+ :c:func:`PyMem_Free` to free the allocated buffer after use.
+
+``et`` (string, Unicode or character buffer compatible object) [const char \*encoding, char \*\*buffer]
+ Same as ``es`` except that 8-bit string objects are passed through without
+ recoding them. Instead, the implementation assumes that the string object
+ uses the encoding passed in as parameter.
+
+``es#`` (string, Unicode or character buffer compatible object) [const char \*encoding, char \*\*buffer, int \*buffer_length]
+ This variant on ``s#`` is used for encoding Unicode and objects convertible
+ to Unicode into a character buffer. Unlike the ``es`` format, this variant
+ allows input data which contains NUL characters.
+
+ It requires three arguments. The first is only used as input, and must be
+ a :c:type:`const char\*` which points to the name of an encoding as a
+ NUL-terminated string, or *NULL*, in which case the default encoding is
+ used. An exception is raised if the named encoding is not known to Python.
+ The second argument must be a :c:type:`char\*\*`; the value of the pointer
+ it references will be set to a buffer with the contents of the argument
+ text. The text will be encoded in the encoding specified by the first
+ argument. The third argument must be a pointer to an integer; the
+ referenced integer will be set to the number of bytes in the output buffer.
There are two modes of operation:
- If *\*buffer* points a ``NULL`` pointer, the function will allocate a buffer of
- the needed size, copy the encoded data into this buffer and set *\*buffer* to
- reference the newly allocated storage. The caller is responsible for calling
- :c:func:`PyMem_Free` to free the allocated buffer after usage.
+ If *\*buffer* points a *NULL* pointer, the function will allocate a buffer
+ of the needed size, copy the encoded data into this buffer and set
+ *\*buffer* to reference the newly allocated storage. The caller is
+ responsible for calling :c:func:`PyMem_Free` to free the allocated buffer
+ after usage.
- If *\*buffer* points to a non-``NULL`` pointer (an already allocated buffer),
- :c:func:`PyArg_ParseTuple` will use this location as the buffer and interpret the
- initial value of *\*buffer_length* as the buffer size. It will then copy the
- encoded data into the buffer and NUL-terminate it. If the buffer is not large
- enough, a :exc:`ValueError` will be set.
+ If *\*buffer* points to a non-*NULL* pointer (an already allocated buffer),
+ :c:func:`PyArg_ParseTuple` will use this location as the buffer and
+ interpret the initial value of *\*buffer_length* as the buffer size. It
+ will then copy the encoded data into the buffer and NUL-terminate it. If
+ the buffer is not large enough, a :exc:`TypeError` will be set.
+ Note: starting from Python 3.6 a :exc:`ValueError` will be set.
In both cases, *\*buffer_length* is set to the length of the encoded data
without the trailing NUL byte.
-``et#`` (:class:`str`, :class:`bytes` or :class:`bytearray`) [const char \*encoding, char \*\*buffer, int \*buffer_length]
- Same as ``es#`` except that byte string objects are passed through without recoding
- them. Instead, the implementation assumes that the byte string object uses the
- encoding passed in as parameter.
+``et#`` (string, Unicode or character buffer compatible object) [const char \*encoding, char \*\*buffer, int \*buffer_length]
+ Same as ``es#`` except that string objects are passed through without
+ recoding them. Instead, the implementation assumes that the string object
+ uses the encoding passed in as parameter.
-Numbers
--------
-
-``b`` (:class:`int`) [unsigned char]
+``b`` (integer) [unsigned char]
Convert a nonnegative Python integer to an unsigned tiny int, stored in a C
:c:type:`unsigned char`.
-``B`` (:class:`int`) [unsigned char]
- Convert a Python integer to a tiny int without overflow checking, stored in a C
- :c:type:`unsigned char`.
+``B`` (integer) [unsigned char]
+ Convert a Python integer to a tiny int without overflow checking, stored in
+ a C :c:type:`unsigned char`.
+
+ .. versionadded:: 2.3
-``h`` (:class:`int`) [short int]
+``h`` (integer) [short int]
Convert a Python integer to a C :c:type:`short int`.
-``H`` (:class:`int`) [unsigned short int]
- Convert a Python integer to a C :c:type:`unsigned short int`, without overflow
- checking.
+``H`` (integer) [unsigned short int]
+ Convert a Python integer to a C :c:type:`unsigned short int`, without
+ overflow checking.
+
+ .. versionadded:: 2.3
-``i`` (:class:`int`) [int]
+``i`` (integer) [int]
Convert a Python integer to a plain C :c:type:`int`.
-``I`` (:class:`int`) [unsigned int]
+``I`` (integer) [unsigned int]
Convert a Python integer to a C :c:type:`unsigned int`, without overflow
checking.
-``l`` (:class:`int`) [long int]
+ .. versionadded:: 2.3
+
+``l`` (integer) [long int]
Convert a Python integer to a C :c:type:`long int`.
-``k`` (:class:`int`) [unsigned long]
- Convert a Python integer to a C :c:type:`unsigned long` without
- overflow checking.
+``k`` (integer) [unsigned long]
+ Convert a Python integer or long integer to a C :c:type:`unsigned long`
+ without overflow checking.
-``L`` (:class:`int`) [long long]
- Convert a Python integer to a C :c:type:`long long`.
+ .. versionadded:: 2.3
-``K`` (:class:`int`) [unsigned long long]
- Convert a Python integer to a C :c:type:`unsigned long long`
- without overflow checking.
+``L`` (integer) [PY_LONG_LONG]
+ Convert a Python integer to a C :c:type:`long long`. This format is only
+ available on platforms that support :c:type:`long long` (or :c:type:`_int64`
+ on Windows).
+
+``K`` (integer) [unsigned PY_LONG_LONG]
+ Convert a Python integer or long integer to a C :c:type:`unsigned long long`
+ without overflow checking. This format is only available on platforms that
+ support :c:type:`unsigned long long` (or :c:type:`unsigned _int64` on
+ Windows).
-``n`` (:class:`int`) [Py_ssize_t]
- Convert a Python integer to a C :c:type:`Py_ssize_t`.
+ .. versionadded:: 2.3
-``c`` (:class:`bytes` or :class:`bytearray` of length 1) [char]
- Convert a Python byte, represented as a :class:`bytes` or
- :class:`bytearray` object of length 1, to a C :c:type:`char`.
+``n`` (integer) [Py_ssize_t]
+ Convert a Python integer or long integer to a C :c:type:`Py_ssize_t`.
- .. versionchanged:: 3.3
- Allow :class:`bytearray` objects.
+ .. versionadded:: 2.5
-``C`` (:class:`str` of length 1) [int]
- Convert a Python character, represented as a :class:`str` object of
- length 1, to a C :c:type:`int`.
+``c`` (string of length 1) [char]
+ Convert a Python character, represented as a string of length 1, to a C
+ :c:type:`char`.
-``f`` (:class:`float`) [float]
+``f`` (float) [float]
Convert a Python floating point number to a C :c:type:`float`.
-``d`` (:class:`float`) [double]
+``d`` (float) [double]
Convert a Python floating point number to a C :c:type:`double`.
-``D`` (:class:`complex`) [Py_complex]
+``D`` (complex) [Py_complex]
Convert a Python complex number to a C :c:type:`Py_complex` structure.
-Other objects
--------------
-
``O`` (object) [PyObject \*]
- Store a Python object (without any conversion) in a C object pointer. The C
- program thus receives the actual object that was passed. The object's reference
- count is not increased. The pointer stored is not ``NULL``.
+ Store a Python object (without any conversion) in a C object pointer. The
+ C program thus receives the actual object that was passed. The object's
+ reference count is not increased. The pointer stored is not *NULL*.
``O!`` (object) [*typeobject*, PyObject \*]
Store a Python object in a C object pointer. This is similar to ``O``, but
- takes two C arguments: the first is the address of a Python type object, the
- second is the address of the C variable (of type :c:type:`PyObject\*`) into which
- the object pointer is stored. If the Python object does not have the required
- type, :exc:`TypeError` is raised.
-
-.. _o_ampersand:
+ takes two C arguments: the first is the address of a Python type object,
+ the second is the address of the C variable (of type :c:type:`PyObject\*`)
+ into which the object pointer is stored. If the Python object does not
+ have the required type, :exc:`TypeError` is raised.
``O&`` (object) [*converter*, *anything*]
- Convert a Python object to a C variable through a *converter* function. This
- takes two arguments: the first is a function, the second is the address of a C
- variable (of arbitrary type), converted to :c:type:`void \*`. The *converter*
- function in turn is called as follows::
+ Convert a Python object to a C variable through a *converter* function.
+ This takes two arguments: the first is a function, the second is the
+ address of a C variable (of arbitrary type), converted to :c:type:`void \*`.
+ The *converter* function in turn is called as follows::
status = converter(object, address);
where *object* is the Python object to be converted and *address* is the
- :c:type:`void\*` argument that was passed to the :c:func:`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.
-
- If the *converter* returns ``Py_CLEANUP_SUPPORTED``, it may get called a
- second time if the argument parsing eventually fails, giving the converter a
- chance to release any memory that it had already allocated. In this second
- call, the *object* parameter will be ``NULL``; *address* will have the same value
- as in the original call.
-
- .. versionchanged:: 3.1
- ``Py_CLEANUP_SUPPORTED`` was added.
-
-``p`` (:class:`bool`) [int]
- Tests the value passed in for truth (a boolean **p**\ redicate) and converts
- the result to its equivalent C true/false integer value.
- Sets the int to ``1`` if the expression was true and ``0`` if it was false.
- This accepts any valid Python value. See :ref:`truth` for more
- information about how Python tests values for truth.
-
- .. versionadded:: 3.3
-
-``(items)`` (:class:`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.
-
-It is possible to pass "long" integers (integers whose value exceeds the
-platform's :const:`LONG_MAX`) 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).
+ :c:type:`void\*` argument that was passed to the :c:func:`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
+ :exc:`TypeError` if the object is not a string object. The C variable may
+ also be declared as :c:type:`PyObject\*`.
+
+``U`` (Unicode string) [PyUnicodeObject \*]
+ Like ``O`` but requires that the Python object is a Unicode object. Raises
+ :exc:`TypeError` if the object is not a Unicode object. The C variable may
+ also be declared as :c:type:`PyObject\*`.
+
+``t#`` (read-only character buffer) [char \*, int]
+ Like ``s#``, but accepts any object which implements the read-only buffer
+ interface. The :c:type:`char\*` variable is set to point to the first byte
+ of the buffer, and the :c:type:`int` is set to the length of the buffer.
+ Only single-segment buffer objects are accepted; :exc:`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; :exc:`TypeError` is raised for all others.
+
+``w#`` (read-write character buffer) [char \*, Py_ssize_t]
+ Like ``s#``, but accepts any object which implements the read-write buffer
+ interface. The :c:type:`char \*` variable is set to point to the first byte
+ of the buffer, and the :c:type:`Py_ssize_t` is set to the length of the
+ buffer. Only single-segment buffer objects are accepted; :exc:`TypeError`
+ is raised for all others.
+
+``w*`` (read-write byte-oriented buffer) [Py_buffer]
+ This is to ``w`` what ``s*`` is to ``s``.
+
+ .. versionadded:: 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 :exc:`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 corresponding to optional arguments should be initialized to
- their default value --- when an optional argument is not specified,
- :c:func:`PyArg_ParseTuple` does not touch the contents of the corresponding C
- variable(s).
-
-``$``
- :c:func:`PyArg_ParseTupleAndKeywords` only:
Indicates that the remaining arguments in the Python argument list are
- keyword-only. Currently, all keyword-only arguments must also be optional
- arguments, so ``|`` must always be specified before ``$`` in the format
- string.
-
- .. versionadded:: 3.3
+ optional. The C variables corresponding to optional arguments should be
+ initialized to their default value --- when an optional argument is not
+ specified, :c:func:`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
- :c:func:`PyArg_ParseTuple` raises).
+ 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 :c:func:`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.
+ 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
-:c:func:`PyArg_Parse\*` functions return true, otherwise they return
-false and raise an appropriate exception. When the
-:c:func:`PyArg_Parse\*` functions fail due to conversion failure in one
-of the format units, the variables at the addresses corresponding to that
+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 :c:func:`PyArg_Parse\*` functions
+return true, otherwise they return false and raise an appropriate exception.
+When the :c:func:`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.
-API Functions
--------------
.. c:function:: 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.
+ 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.
.. c:function:: int PyArg_VaParse(PyObject *args, const char *format, va_list vargs)
- Identical to :c:func:`PyArg_ParseTuple`, except that it accepts a va_list rather
- than a variable number of arguments.
+ Identical to :c:func:`PyArg_ParseTuple`, except that it accepts a va_list
+ rather than a variable number of arguments.
.. c:function:: int PyArg_ParseTupleAndKeywords(PyObject *args, PyObject *kw, const char *format, char *keywords[], ...)
Parse the parameters of a function that takes both positional and keyword
- parameters into local variables. The *keywords* argument is a
- ``NULL``-terminated array of keyword parameter names. Empty names denote
- :ref:`positional-only parameters <positional-only_parameter>`.
- Returns true on success; on failure, it returns false and raises the
- appropriate exception.
-
- .. versionchanged:: 3.6
- Added support for :ref:`positional-only parameters
- <positional-only_parameter>`.
+ parameters into local variables. Returns true on success; on failure, it
+ returns false and raises the appropriate exception.
.. c:function:: int PyArg_VaParseTupleAndKeywords(PyObject *args, PyObject *kw, const char *format, char *keywords[], va_list vargs)
@@ -453,45 +359,35 @@ API Functions
va_list rather than a variable number of arguments.
-.. c:function:: int PyArg_ValidateKeywordArguments(PyObject *)
-
- Ensure that the keys in the keywords argument dictionary are strings. This
- is only needed if :c:func:`PyArg_ParseTupleAndKeywords` is not used, since the
- latter already does this check.
-
- .. versionadded:: 3.2
-
-
-.. XXX deprecated, will be removed
.. c:function:: int PyArg_Parse(PyObject *args, const char *format, ...)
- Function used to deconstruct the argument lists of "old-style" functions ---
- these are functions which use the :const:`METH_OLDARGS` parameter parsing
- method, which has been removed in Python 3. This is not recommended for use
- in parameter parsing in new code, and most code in the standard interpreter
- has been modified to no longer use this for that purpose. It does remain a
- convenient way to decompose other tuples, however, and may continue to be
- used for that purpose.
+ Function used to deconstruct the argument lists of "old-style" functions
+ --- these are functions which use the :const:`METH_OLDARGS` parameter
+ parsing method. This is not recommended for use in parameter parsing in
+ new code, and most code in the standard interpreter has been modified to no
+ longer use this for that purpose. It does remain a convenient way to
+ decompose other tuples, however, and may continue to be used for that
+ purpose.
.. c:function:: int PyArg_UnpackTuple(PyObject *args, const char *name, Py_ssize_t min, Py_ssize_t max, ...)
A simpler form of parameter retrieval which does not use a format string to
- specify the types of the arguments. Functions which use this method to retrieve
- their parameters should be declared as :const:`METH_VARARGS` in function or
- method tables. The tuple containing the actual parameters should be passed as
- *args*; it must actually be a tuple. The length of the tuple must be at least
- *min* and no more than *max*; *min* and *max* may be equal. Additional
- arguments must be passed to the function, each of which should be a pointer to a
- :c:type:`PyObject\*` variable; these will be filled in with the values from
- *args*; they will contain borrowed references. The variables which correspond
- to optional parameters not given by *args* will not be filled in; these should
- be initialized by the caller. This function returns true on success and false if
- *args* is not a tuple or contains the wrong number of elements; an exception
- will be set if there was a failure.
-
- This is an example of the use of this function, taken from the sources for the
- :mod:`_weakref` helper module for weak references::
+ specify the types of the arguments. Functions which use this method to
+ retrieve their parameters should be declared as :const:`METH_VARARGS` in
+ function or method tables. The tuple containing the actual parameters
+ should be passed as *args*; it must actually be a tuple. The length of the
+ tuple must be at least *min* and no more than *max*; *min* and *max* may be
+ equal. Additional arguments must be passed to the function, each of which
+ should be a pointer to a :c:type:`PyObject\*` variable; these will be filled
+ in with the values from *args*; they will contain borrowed references. The
+ variables which correspond to optional parameters not given by *args* will
+ not be filled in; these should be initialized by the caller. This function
+ returns true on success and false if *args* is not a tuple or contains the
+ wrong number of elements; an exception will be set if there was a failure.
+
+ This is an example of the use of this function, taken from the sources for
+ the :mod:`_weakref` helper module for weak references::
static PyObject *
weakref_ref(PyObject *self, PyObject *args)
@@ -506,169 +402,162 @@ API Functions
return result;
}
- The call to :c:func:`PyArg_UnpackTuple` in this example is entirely equivalent to
- this call to :c:func:`PyArg_ParseTuple`::
+ The call to :c:func:`PyArg_UnpackTuple` in this example is entirely
+ equivalent to this call to :c:func:`PyArg_ParseTuple`::
PyArg_ParseTuple(args, "O|O:ref", &object, &callback)
+ .. versionadded:: 2.2
+
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` type for *min* and *max*. This might
+ require changes in your code for properly supporting 64-bit systems.
----------------
-Building values
----------------
.. c:function:: PyObject* Py_BuildValue(const char *format, ...)
- Create a new value based on a format string similar to those accepted by the
- :c:func:`PyArg_Parse\*` family of functions and a sequence of values. Returns
- the value or ``NULL`` in the case of an error; an exception will be raised if
- ``NULL`` is returned.
-
- :c:func:`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
- :c:func:`Py_BuildValue`. In other words, if your code invokes :c:func:`malloc`
- and passes the allocated memory to :c:func:`Py_BuildValue`, your code is
- responsible for calling :c:func:`free` for that memory once
- :c:func:`Py_BuildValue` returns.
+ Create a new value based on a format string similar to those accepted by
+ the :c:func:`PyArg_Parse\*` family of functions and a sequence of values.
+ Returns the value or *NULL* in the case of an error; an exception will be
+ raised if *NULL* is returned.
- 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.
+ :c:func:`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.
- 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.
+ 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 :c:func:`Py_BuildValue`. In other words, if your code invokes
+ :c:func:`malloc` and passes the allocated memory to :c:func:`Py_BuildValue`,
+ your code is responsible for calling :c:func:`free` for that memory once
+ :c:func:`Py_BuildValue` returns.
- ``s`` (:class:`str` or ``None``) [const char \*]
- Convert a null-terminated C string to a Python :class:`str` object using ``'utf-8'``
- encoding. If the C string pointer is ``NULL``, ``None`` is used.
+ 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.
- ``s#`` (:class:`str` or ``None``) [const char \*, int]
- Convert a C string and its length to a Python :class:`str` object using ``'utf-8'``
- encoding. If the C string pointer is ``NULL``, the length is ignored and
- ``None`` is returned.
+ 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.
- ``y`` (:class:`bytes`) [const char \*]
- This converts a C string to a Python :class:`bytes` object. If the C
- string pointer is ``NULL``, ``None`` is returned.
+ ``s`` (string) [char \*]
+ Convert a null-terminated C string to a Python object. If the C string
+ pointer is *NULL*, ``None`` is used.
- ``y#`` (:class:`bytes`) [const char \*, int]
- This converts a C string and its lengths to a Python object. If the C
- string pointer is ``NULL``, ``None`` is returned.
+ ``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`` (:class:`str` or ``None``) [const char \*]
+ ``z`` (string or ``None``) [char \*]
Same as ``s``.
- ``z#`` (:class:`str` or ``None``) [const char \*, int]
+ ``z#`` (string or ``None``) [char \*, int]
Same as ``s#``.
- ``u`` (:class:`str`) [const wchar_t \*]
- Convert a null-terminated :c:type:`wchar_t` buffer of Unicode (UTF-16 or UCS-4)
- data to a Python Unicode object. If the Unicode buffer pointer is ``NULL``,
+ ``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#`` (:class:`str`) [const wchar_t \*, int]
- Convert a Unicode (UTF-16 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.
-
- ``U`` (:class:`str` or ``None``) [const char \*]
- Same as ``s``.
-
- ``U#`` (:class:`str` or ``None``) [const char \*, int]
- Same as ``s#``.
+ ``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`` (:class:`int`) [int]
+ ``i`` (integer) [int]
Convert a plain C :c:type:`int` to a Python integer object.
- ``b`` (:class:`int`) [char]
+ ``b`` (integer) [char]
Convert a plain C :c:type:`char` to a Python integer object.
- ``h`` (:class:`int`) [short int]
+ ``h`` (integer) [short int]
Convert a plain C :c:type:`short int` to a Python integer object.
- ``l`` (:class:`int`) [long int]
+ ``l`` (integer) [long int]
Convert a C :c:type:`long int` to a Python integer object.
- ``B`` (:class:`int`) [unsigned char]
+ ``B`` (integer) [unsigned char]
Convert a C :c:type:`unsigned char` to a Python integer object.
- ``H`` (:class:`int`) [unsigned short int]
+ ``H`` (integer) [unsigned short int]
Convert a C :c:type:`unsigned short int` to a Python integer object.
- ``I`` (:class:`int`) [unsigned int]
- Convert a C :c:type:`unsigned int` to a Python integer object.
+ ``I`` (integer/long) [unsigned int]
+ Convert a C :c:type:`unsigned int` to a Python integer object or a Python
+ long integer object, if it is larger than ``sys.maxint``.
- ``k`` (:class:`int`) [unsigned long]
- Convert a C :c:type:`unsigned long` to a Python integer object.
+ ``k`` (integer/long) [unsigned long]
+ Convert a C :c:type:`unsigned long` to a Python integer object or a
+ Python long integer object, if it is larger than ``sys.maxint``.
- ``L`` (:class:`int`) [long long]
- Convert a C :c:type:`long long` to a Python integer object.
+ ``L`` (long) [PY_LONG_LONG]
+ Convert a C :c:type:`long long` to a Python long integer object. Only
+ available on platforms that support :c:type:`long long`.
- ``K`` (:class:`int`) [unsigned long long]
- Convert a C :c:type:`unsigned long long` to a Python integer object.
+ ``K`` (long) [unsigned PY_LONG_LONG]
+ Convert a C :c:type:`unsigned long long` to a Python long integer object.
+ Only available on platforms that support :c:type:`unsigned long long`.
- ``n`` (:class:`int`) [Py_ssize_t]
- Convert a C :c:type:`Py_ssize_t` to a Python integer.
+ ``n`` (int) [Py_ssize_t]
+ Convert a C :c:type:`Py_ssize_t` to a Python integer or long integer.
- ``c`` (:class:`bytes` of length 1) [char]
- Convert a C :c:type:`int` representing a byte to a Python :class:`bytes` object of
- length 1.
+ .. versionadded:: 2.5
- ``C`` (:class:`str` of length 1) [int]
- Convert a C :c:type:`int` representing a character to Python :class:`str`
- object of length 1.
+ ``c`` (string of length 1) [char]
+ Convert a C :c:type:`int` representing a character to a Python string of
+ length 1.
- ``d`` (:class:`float`) [double]
+ ``d`` (float) [double]
Convert a C :c:type:`double` to a Python floating point number.
- ``f`` (:class:`float`) [float]
- Convert a C :c:type:`float` to a Python floating point number.
+ ``f`` (float) [float]
+ Same as ``d``.
- ``D`` (:class:`complex`) [Py_complex \*]
+ ``D`` (complex) [Py_complex \*]
Convert a C :c:type:`Py_complex` structure to a Python complex number.
``O`` (object) [PyObject \*]
Pass a Python object untouched (except for its reference count, which is
- incremented by one). If the object passed in is a ``NULL`` pointer, it is assumed
- that this was caused because the call producing the argument found an error and
- set an exception. Therefore, :c:func:`Py_BuildValue` will return ``NULL`` but won't
- raise an exception. If no exception has been raised yet, :exc:`SystemError` is
- set.
+ incremented by one). If the object passed in is a *NULL* pointer, it is
+ assumed that this was caused because the call producing the argument
+ found an error and set an exception. Therefore, :c:func:`Py_BuildValue`
+ will return *NULL* but won't raise an exception. If no exception has
+ been raised yet, :exc:`SystemError` is set.
``S`` (object) [PyObject \*]
Same as ``O``.
``N`` (object) [PyObject \*]
- Same as ``O``, except it doesn't increment the reference count on the object.
- Useful when the object is created by a call to an object constructor in the
- argument list.
+ Same as ``O``, except it doesn't increment the reference count on the
+ object. Useful when the object is created by a call to an object
+ constructor in the argument list.
``O&`` (object) [*converter*, *anything*]
- Convert *anything* to a Python object through a *converter* function. The
- function is called with *anything* (which should be compatible with :c:type:`void
- \*`) as its argument and should return a "new" Python object, or ``NULL`` if an
- error occurred.
-
- ``(items)`` (:class:`tuple`) [*matching-items*]
- Convert a sequence of C values to a Python tuple with the same number of items.
-
- ``[items]`` (:class:`list`) [*matching-items*]
- Convert a sequence of C values to a Python list with the same number of items.
-
- ``{items}`` (:class:`dict`) [*matching-items*]
- Convert a sequence of C values to a Python dictionary. Each pair of consecutive
- C values adds one item to the dictionary, serving as key and value,
- respectively.
-
- If there is an error in the format string, the :exc:`SystemError` exception is
- set and ``NULL`` returned.
+ Convert *anything* to a Python object through a *converter* function.
+ The function is called with *anything* (which should be compatible with
+ :c:type:`void \*`) as its argument and should return a "new" Python
+ object, or *NULL* if an error occurred.
+
+ ``(items)`` (tuple) [*matching-items*]
+ Convert a sequence of C values to a Python tuple with the same number of
+ items.
+
+ ``[items]`` (list) [*matching-items*]
+ Convert a sequence of C values to a Python list with the same number of
+ items.
+
+ ``{items}`` (dictionary) [*matching-items*]
+ Convert a sequence of C values to a Python dictionary. Each pair of
+ consecutive C values adds one item to the dictionary, serving as key and
+ value, respectively.
+
+ If there is an error in the format string, the :exc:`SystemError` exception
+ is set and *NULL* returned.
.. c:function:: PyObject* Py_VaBuildValue(const char *format, va_list vargs)
diff --git a/Doc/c-api/bool.rst b/Doc/c-api/bool.rst
index ce8de6e..fede348 100644
--- a/Doc/c-api/bool.rst
+++ b/Doc/c-api/bool.rst
@@ -1,4 +1,4 @@
-.. highlight:: c
+.. highlightlang:: c
.. _boolobjects:
@@ -15,6 +15,8 @@ are available, however.
Return true if *o* is of type :c:data:`PyBool_Type`.
+ .. versionadded:: 2.3
+
.. c:var:: PyObject* Py_False
@@ -33,14 +35,20 @@ are available, however.
Return :const:`Py_False` from a function, properly incrementing its reference
count.
+ .. versionadded:: 2.4
+
.. c:macro:: Py_RETURN_TRUE
Return :const:`Py_True` from a function, properly incrementing its reference
count.
+ .. versionadded:: 2.4
+
.. c:function:: PyObject* PyBool_FromLong(long v)
Return a new reference to :const:`Py_True` or :const:`Py_False` depending on the
truth value of *v*.
+
+ .. versionadded:: 2.3
diff --git a/Doc/c-api/buffer.rst b/Doc/c-api/buffer.rst
index fc1430e..77940ac 100644
--- a/Doc/c-api/buffer.rst
+++ b/Doc/c-api/buffer.rst
@@ -1,525 +1,455 @@
-.. highlight:: c
-
-.. index::
- single: buffer protocol
- single: buffer interface; (see buffer protocol)
- single: buffer object; (see buffer protocol)
+.. highlightlang:: c
.. _bufferobjects:
-Buffer Protocol
----------------
+Buffers and Memoryview Objects
+------------------------------
.. sectionauthor:: Greg Stein <gstein@lyra.org>
.. sectionauthor:: Benjamin Peterson
-.. sectionauthor:: Stefan Krah
-
-
-Certain objects available in Python wrap access to an underlying memory
-array or *buffer*. Such objects include the built-in :class:`bytes` and
-:class:`bytearray`, and some extension types like :class:`array.array`.
-Third-party libraries may define their own types for special purposes, such
-as image processing or numeric analysis.
-
-While each of these types have their own semantics, they share the common
-characteristic of being backed by a possibly large memory buffer. It is
-then desirable, in some situations, to access that buffer directly and
-without intermediate copying.
-
-Python provides such a facility at the C level in the form of the :ref:`buffer
-protocol <bufferobjects>`. This protocol has two sides:
-
-.. index:: single: PyBufferProcs
-
-- on the producer side, a type can export a "buffer interface" which allows
- objects of that type to expose information about their underlying buffer.
- This interface is described in the section :ref:`buffer-structs`;
-
-- on the consumer side, several means are available to obtain a pointer to
- the raw underlying data of an object (for example a method parameter).
-
-Simple objects such as :class:`bytes` and :class:`bytearray` expose their
-underlying buffer in byte-oriented form. Other forms are possible; for example,
-the elements exposed by an :class:`array.array` can be multi-byte values.
-
-An example consumer of the buffer interface is the :meth:`~io.BufferedIOBase.write`
-method of file objects: any object that can export a series of bytes through
-the buffer interface can be written to a file. While :meth:`write` only
-needs read-only access to the internal contents of the object passed to it,
-other methods such as :meth:`~io.BufferedIOBase.readinto` need write access
-to the contents of their argument. The buffer interface allows objects to
-selectively allow or reject exporting of read-write and read-only buffers.
-There are two ways for a consumer of the buffer interface to acquire a buffer
-over a target object:
-* call :c:func:`PyObject_GetBuffer` with the right parameters;
+.. index::
+ object: buffer
+ single: buffer interface
-* call :c:func:`PyArg_ParseTuple` (or one of its siblings) with one of the
- ``y*``, ``w*`` or ``s*`` :ref:`format codes <arg-parsing>`.
+Python objects implemented in C can export a group of functions called the
+"buffer interface." These functions can be used by an object to expose its
+data in a raw, byte-oriented format. Clients of the object can use the buffer
+interface to access the object data directly, without needing to copy it
+first.
-In both cases, :c:func:`PyBuffer_Release` must be called when the buffer
-isn't needed anymore. Failure to do so could lead to various issues such as
-resource leaks.
+Two examples of objects that support the buffer interface are strings and
+arrays. The string object exposes the character contents in the buffer
+interface's byte-oriented form. An array can only expose its contents via the
+old-style buffer interface. This limitation does not apply to Python 3,
+where :class:`memoryview` objects can be constructed from arrays, too.
+Array elements may be multi-byte values.
+An example user of the buffer interface is the file object's :meth:`write`
+method. Any object that can export a series of bytes through the buffer
+interface can be written to a file. There are a number of format codes to
+:c:func:`PyArg_ParseTuple` that operate against an object's buffer interface,
+returning data from the target object.
-.. _buffer-structure:
+Starting from version 1.6, Python has been providing Python-level buffer
+objects and a C-level buffer API so that any built-in or used-defined type can
+expose its characteristics. Both, however, have been deprecated because of
+various shortcomings, and have been officially removed in Python 3 in favour
+of a new C-level buffer API and a new Python-level object named
+:class:`memoryview`.
-Buffer structure
-================
+The new buffer API has been backported to Python 2.6, and the
+:class:`memoryview` object has been backported to Python 2.7. It is strongly
+advised to use them rather than the old APIs, unless you are blocked from
+doing so for compatibility reasons.
-Buffer structures (or simply "buffers") are useful as a way to expose the
-binary data from another object 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.
-Contrary to most data types exposed by the Python interpreter, buffers
-are not :c:type:`PyObject` pointers but rather simple C structures. This
-allows them to be created and copied very simply. When a generic wrapper
-around a buffer is needed, a :ref:`memoryview <memoryview-objects>` object
-can be created.
+The new-style Py_buffer struct
+==============================
-For short instructions how to write an exporting object, see
-:ref:`Buffer Object Structures <buffer-structs>`. For obtaining
-a buffer, see :c:func:`PyObject_GetBuffer`.
.. c:type:: Py_buffer
- .. c:member:: void \*buf
-
- A pointer to the start of the logical structure described by the buffer
- fields. This can be any location within the underlying physical memory
- block of the exporter. For example, with negative :c:member:`~Py_buffer.strides`
- the value may point to the end of the memory block.
-
- For :term:`contiguous` arrays, the value points to the beginning of
- the memory block.
+ .. c:member:: void *buf
- .. c:member:: void \*obj
-
- A new reference to the exporting object. The reference is owned by
- the consumer and automatically decremented and set to ``NULL`` by
- :c:func:`PyBuffer_Release`. The field is the equivalent of the return
- value of any standard C-API function.
-
- As a special case, for *temporary* buffers that are wrapped by
- :c:func:`PyMemoryView_FromBuffer` or :c:func:`PyBuffer_FillInfo`
- this field is ``NULL``. In general, exporting objects MUST NOT
- use this scheme.
+ A pointer to the start of the memory for the object.
.. c:member:: Py_ssize_t len
+ :noindex:
- ``product(shape) * itemsize``. For contiguous arrays, this is the length
- of the underlying memory block. For non-contiguous arrays, it is the length
- that the logical structure would have if it were copied to a contiguous
- representation.
-
- Accessing ``((char *)buf)[0] up to ((char *)buf)[len-1]`` is only valid
- if the buffer has been obtained by a request that guarantees contiguity. In
- most cases such a request will be :c:macro:`PyBUF_SIMPLE` or :c:macro:`PyBUF_WRITABLE`.
+ The total length of the memory in bytes.
.. c:member:: int readonly
- An indicator of whether the buffer is read-only. This field is controlled
- by the :c:macro:`PyBUF_WRITABLE` flag.
-
- .. c:member:: Py_ssize_t itemsize
-
- Item size in bytes of a single element. Same as the value of :func:`struct.calcsize`
- called on non-``NULL`` :c:member:`~Py_buffer.format` values.
-
- Important exception: If a consumer requests a buffer without the
- :c:macro:`PyBUF_FORMAT` flag, :c:member:`~Py_buffer.format` will
- be set to ``NULL``, but :c:member:`~Py_buffer.itemsize` still has
- the value for the original format.
-
- If :c:member:`~Py_buffer.shape` is present, the equality
- ``product(shape) * itemsize == len`` still holds and the consumer
- can use :c:member:`~Py_buffer.itemsize` to navigate the buffer.
+ An indicator of whether the buffer is read only.
- If :c:member:`~Py_buffer.shape` is ``NULL`` as a result of a :c:macro:`PyBUF_SIMPLE`
- or a :c:macro:`PyBUF_WRITABLE` request, the consumer must disregard
- :c:member:`~Py_buffer.itemsize` and assume ``itemsize == 1``.
+ .. c:member:: const char *format
+ :noindex:
- .. c:member:: const char \*format
-
- A *NUL* terminated string in :mod:`struct` module style syntax describing
- the contents of a single item. If this is ``NULL``, ``"B"`` (unsigned bytes)
- is assumed.
-
- This field is controlled by the :c:macro:`PyBUF_FORMAT` flag.
+ A *NULL* terminated string in :mod:`struct` module style syntax giving
+ the contents of the elements available through the buffer. If this is
+ *NULL*, ``"B"`` (unsigned bytes) is assumed.
.. c:member:: int ndim
- The number of dimensions the memory represents as an n-dimensional array.
- If it is ``0``, :c:member:`~Py_buffer.buf` points to a single item representing
- a scalar. In this case, :c:member:`~Py_buffer.shape`, :c:member:`~Py_buffer.strides`
- and :c:member:`~Py_buffer.suboffsets` MUST be ``NULL``.
-
- The macro :c:macro:`PyBUF_MAX_NDIM` limits the maximum number of dimensions
- to 64. Exporters MUST respect this limit, consumers of multi-dimensional
- buffers SHOULD be able to handle up to :c:macro:`PyBUF_MAX_NDIM` dimensions.
-
- .. c:member:: Py_ssize_t \*shape
-
- An array of :c:type:`Py_ssize_t` of length :c:member:`~Py_buffer.ndim`
- indicating the shape of the memory as an n-dimensional array. Note that
- ``shape[0] * ... * shape[ndim-1] * itemsize`` MUST be equal to
- :c:member:`~Py_buffer.len`.
-
- Shape values are restricted to ``shape[n] >= 0``. The case
- ``shape[n] == 0`` requires special attention. See `complex arrays`_
- for further information.
+ The number of dimensions the memory represents as a multi-dimensional
+ array. If it is ``0``, :c:data:`strides` and :c:data:`suboffsets` must be
+ *NULL*.
- The shape array is read-only for the consumer.
+ .. c:member:: Py_ssize_t *shape
- .. c:member:: Py_ssize_t \*strides
+ An array of :c:type:`Py_ssize_t`\s the length of :c:data:`ndim` giving the
+ shape of the memory as a multi-dimensional array. Note that
+ ``((*shape)[0] * ... * (*shape)[ndims-1])*itemsize`` should be equal to
+ :c:data:`len`.
- An array of :c:type:`Py_ssize_t` of length :c:member:`~Py_buffer.ndim`
- giving the number of bytes to skip to get to a new element in each
- dimension.
+ .. c:member:: Py_ssize_t *strides
- Stride values can be any integer. For regular arrays, strides are
- usually positive, but a consumer MUST be able to handle the case
- ``strides[n] <= 0``. See `complex arrays`_ for further information.
+ An array of :c:type:`Py_ssize_t`\s the length of :c:data:`ndim` giving the
+ number of bytes to skip to get to a new element in each dimension.
- The strides array is read-only for the consumer.
+ .. c:member:: Py_ssize_t *suboffsets
- .. c:member:: Py_ssize_t \*suboffsets
-
- An array of :c:type:`Py_ssize_t` of length :c:member:`~Py_buffer.ndim`.
- If ``suboffsets[n] >= 0``, the values stored along the nth dimension are
- pointers and the suboffset value dictates how many bytes to add to each
- pointer after de-referencing. A suboffset value that is negative
- indicates that no de-referencing should occur (striding in a contiguous
- memory block).
+ An array of :c:type:`Py_ssize_t`\s the length of :c:data:`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).
+ 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::
+
+ void *get_item_pointer(int ndim, void *buf, Py_ssize_t *strides,
+ Py_ssize_t *suboffsets, Py_ssize_t *indices) {
+ char *pointer = (char*)buf;
+ int i;
+ for (i = 0; i < ndim; i++) {
+ pointer += strides[i] * indices[i];
+ if (suboffsets[i] >=0 ) {
+ pointer = *((char**)pointer) + suboffsets[i];
+ }
+ }
+ return (void*)pointer;
+ }
+
- This type of array representation is used by the Python Imaging Library
- (PIL). See `complex arrays`_ for further information how to access elements
- of such an array.
+ .. c:member:: Py_ssize_t itemsize
- The suboffsets array is read-only for the consumer.
+ 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 :c:func:`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.
- .. c:member:: void \*internal
+ .. c:member:: 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 MUST NOT alter this
+ freed when the buffer is released. The consumer should never alter this
value.
-.. _buffer-request-types:
-Buffer request types
-====================
+Buffer related functions
+========================
-Buffers are usually obtained by sending a buffer request to an exporting
-object via :c:func:`PyObject_GetBuffer`. Since the complexity of the logical
-structure of the memory can vary drastically, the consumer uses the *flags*
-argument to specify the exact buffer type it can handle.
-All :c:data:`Py_buffer` fields are unambiguously defined by the request
-type.
+.. c:function:: int PyObject_CheckBuffer(PyObject *obj)
-request-independent fields
-~~~~~~~~~~~~~~~~~~~~~~~~~~
-The following fields are not influenced by *flags* and must always be filled in
-with the correct values: :c:member:`~Py_buffer.obj`, :c:member:`~Py_buffer.buf`,
-:c:member:`~Py_buffer.len`, :c:member:`~Py_buffer.itemsize`, :c:member:`~Py_buffer.ndim`.
+ Return ``1`` if *obj* supports the buffer interface otherwise ``0``.
+
+
+.. c:function:: int PyObject_GetBuffer(PyObject *obj, Py_buffer *view, int flags)
+
+ Export *obj* into a :c:type:`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 :exc:`BufferError` unless
+ there is another error that is actually causing the problem. The
+ exporter can use flags information to simplify how much of the
+ :c:data:`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.
+
+ The following table gives possible values to the *flags* arguments.
+
+ +-------------------------------+---------------------------------------------------+
+ | Flag | Description |
+ +===============================+===================================================+
+ | :c:macro:`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. |
+ | | |
+ +-------------------------------+---------------------------------------------------+
+ | :c:macro:`PyBUF_WRITABLE` | The returned buffer must be writable. If it is |
+ | | not writable, then raise an error. |
+ +-------------------------------+---------------------------------------------------+
+ | :c:macro:`PyBUF_STRIDES` | This implies :c:macro:`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). |
+ | | |
+ +-------------------------------+---------------------------------------------------+
+ | :c:macro:`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*. |
+ | | |
+ | | |
+ | | |
+ +-------------------------------+---------------------------------------------------+
+ |:c:macro:`PyBUF_C_CONTIGUOUS` | These flags indicate that the contiguity returned |
+ |:c:macro:`PyBUF_F_CONTIGUOUS` | buffer must be respectively, C-contiguous (last |
+ |:c:macro:`PyBUF_ANY_CONTIGUOUS`| dimension varies the fastest), Fortran contiguous |
+ | | (first dimension varies the fastest) or either |
+ | | one. All of these flags imply |
+ | | :c:macro:`PyBUF_STRIDES` and guarantee that the |
+ | | strides buffer info structure will be filled in |
+ | | correctly. |
+ | | |
+ +-------------------------------+---------------------------------------------------+
+ | :c:macro:`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 :c:macro:`PyBUF_STRIDES`. |
+ | | |
+ | | |
+ | | |
+ +-------------------------------+---------------------------------------------------+
+ | :c:macro:`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) |
+ +-------------------------------+---------------------------------------------------+
+ | :c:macro:`PyBUF_STRIDED` | This is equivalent to ``(PyBUF_STRIDES | |
+ | | PyBUF_WRITABLE)``. |
+ +-------------------------------+---------------------------------------------------+
+ | :c:macro:`PyBUF_STRIDED_RO` | This is equivalent to ``(PyBUF_STRIDES)``. |
+ | | |
+ +-------------------------------+---------------------------------------------------+
+ | :c:macro:`PyBUF_RECORDS` | This is equivalent to ``(PyBUF_STRIDES | |
+ | | PyBUF_FORMAT | PyBUF_WRITABLE)``. |
+ +-------------------------------+---------------------------------------------------+
+ | :c:macro:`PyBUF_RECORDS_RO` | This is equivalent to ``(PyBUF_STRIDES | |
+ | | PyBUF_FORMAT)``. |
+ +-------------------------------+---------------------------------------------------+
+ | :c:macro:`PyBUF_FULL` | This is equivalent to ``(PyBUF_INDIRECT | |
+ | | PyBUF_FORMAT | PyBUF_WRITABLE)``. |
+ +-------------------------------+---------------------------------------------------+
+ | :c:macro:`PyBUF_FULL_RO` | This is equivalent to ``(PyBUF_INDIRECT | |
+ | | PyBUF_FORMAT)``. |
+ +-------------------------------+---------------------------------------------------+
+ | :c:macro:`PyBUF_CONTIG` | This is equivalent to ``(PyBUF_ND | |
+ | | PyBUF_WRITABLE)``. |
+ +-------------------------------+---------------------------------------------------+
+ | :c:macro:`PyBUF_CONTIG_RO` | This is equivalent to ``(PyBUF_ND)``. |
+ | | |
+ +-------------------------------+---------------------------------------------------+
-readonly, format
-~~~~~~~~~~~~~~~~
+.. c:function:: void PyBuffer_Release(Py_buffer *view)
- .. c:macro:: PyBUF_WRITABLE
+ Release the buffer *view*. This should be called when the buffer
+ is no longer being used as it may free memory from it.
- Controls the :c:member:`~Py_buffer.readonly` field. If set, the exporter
- MUST provide a writable buffer or else report failure. Otherwise, the
- exporter MAY provide either a read-only or writable buffer, but the choice
- MUST be consistent for all consumers.
- .. c:macro:: PyBUF_FORMAT
+.. c:function:: Py_ssize_t PyBuffer_SizeFromFormat(const char *)
- Controls the :c:member:`~Py_buffer.format` field. If set, this field MUST
- be filled in correctly. Otherwise, this field MUST be ``NULL``.
+ Return the implied :c:data:`~Py_buffer.itemsize` from the struct-stype
+ :c:data:`~Py_buffer.format`.
-:c:macro:`PyBUF_WRITABLE` can be \|'d to any of the flags in the next section.
-Since :c:macro:`PyBUF_SIMPLE` is defined as 0, :c:macro:`PyBUF_WRITABLE`
-can be used as a stand-alone flag to request a simple writable buffer.
+.. c:function:: int PyBuffer_IsContiguous(Py_buffer *view, char fortran)
-:c:macro:`PyBUF_FORMAT` can be \|'d to any of the flags except :c:macro:`PyBUF_SIMPLE`.
-The latter already implies format ``B`` (unsigned bytes).
+ 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.
-shape, strides, suboffsets
-~~~~~~~~~~~~~~~~~~~~~~~~~~
+.. c:function:: void PyBuffer_FillContiguousStrides(int ndims, Py_ssize_t *shape, Py_ssize_t *strides, int itemsize, char fortran)
-The flags that control the logical structure of the memory are listed
-in decreasing order of complexity. Note that each flag contains all bits
-of the flags below it.
+ 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.
-.. tabularcolumns:: |p{0.35\linewidth}|l|l|l|
-+-----------------------------+-------+---------+------------+
-| Request | shape | strides | suboffsets |
-+=============================+=======+=========+============+
-| .. c:macro:: PyBUF_INDIRECT | yes | yes | if needed |
-+-----------------------------+-------+---------+------------+
-| .. c:macro:: PyBUF_STRIDES | yes | yes | NULL |
-+-----------------------------+-------+---------+------------+
-| .. c:macro:: PyBUF_ND | yes | NULL | NULL |
-+-----------------------------+-------+---------+------------+
-| .. c:macro:: PyBUF_SIMPLE | NULL | NULL | NULL |
-+-----------------------------+-------+---------+------------+
+.. c:function:: int PyBuffer_FillInfo(Py_buffer *view, PyObject *obj, void *buf, Py_ssize_t len, int readonly, int infoflags)
+ Fill in a buffer-info structure, *view*, correctly for an exporter that can
+ only share a contiguous chunk of memory of "unsigned bytes" of the given
+ length. Return ``0`` on success and ``-1`` (with raising an error) on error.
-.. index:: contiguous, C-contiguous, Fortran contiguous
-contiguity requests
-~~~~~~~~~~~~~~~~~~~
+MemoryView objects
+==================
-C or Fortran :term:`contiguity <contiguous>` can be explicitly requested,
-with and without stride information. Without stride information, the buffer
-must be C-contiguous.
+.. versionadded:: 2.7
-.. tabularcolumns:: |p{0.35\linewidth}|l|l|l|l|
+A :class:`memoryview` object exposes the new C level buffer interface as a
+Python object which can then be passed around like any other object.
-+-----------------------------------+-------+---------+------------+--------+
-| Request | shape | strides | suboffsets | contig |
-+===================================+=======+=========+============+========+
-| .. c:macro:: PyBUF_C_CONTIGUOUS | yes | yes | NULL | C |
-+-----------------------------------+-------+---------+------------+--------+
-| .. c:macro:: PyBUF_F_CONTIGUOUS | yes | yes | NULL | F |
-+-----------------------------------+-------+---------+------------+--------+
-| .. c:macro:: PyBUF_ANY_CONTIGUOUS | yes | yes | NULL | C or F |
-+-----------------------------------+-------+---------+------------+--------+
-| .. c:macro:: PyBUF_ND | yes | NULL | NULL | C |
-+-----------------------------------+-------+---------+------------+--------+
+.. c:function:: PyObject *PyMemoryView_FromObject(PyObject *obj)
+ Create a memoryview object from an object that defines the new buffer
+ interface.
-compound requests
-~~~~~~~~~~~~~~~~~
-All possible requests are fully defined by some combination of the flags in
-the previous section. For convenience, the buffer protocol provides frequently
-used combinations as single flags.
+.. c:function:: PyObject *PyMemoryView_FromBuffer(Py_buffer *view)
-In the following table *U* stands for undefined contiguity. The consumer would
-have to call :c:func:`PyBuffer_IsContiguous` to determine contiguity.
+ Create a memoryview object wrapping the given buffer-info structure *view*.
+ The memoryview object then owns the buffer, which means you shouldn't
+ try to release it yourself: it will be released on deallocation of the
+ memoryview object.
-.. tabularcolumns:: |p{0.35\linewidth}|l|l|l|l|l|l|
-+-------------------------------+-------+---------+------------+--------+----------+--------+
-| Request | shape | strides | suboffsets | contig | readonly | format |
-+===============================+=======+=========+============+========+==========+========+
-| .. c:macro:: PyBUF_FULL | yes | yes | if needed | U | 0 | yes |
-+-------------------------------+-------+---------+------------+--------+----------+--------+
-| .. c:macro:: PyBUF_FULL_RO | yes | yes | if needed | U | 1 or 0 | yes |
-+-------------------------------+-------+---------+------------+--------+----------+--------+
-| .. c:macro:: PyBUF_RECORDS | yes | yes | NULL | U | 0 | yes |
-+-------------------------------+-------+---------+------------+--------+----------+--------+
-| .. c:macro:: PyBUF_RECORDS_RO | yes | yes | NULL | U | 1 or 0 | yes |
-+-------------------------------+-------+---------+------------+--------+----------+--------+
-| .. c:macro:: PyBUF_STRIDED | yes | yes | NULL | U | 0 | NULL |
-+-------------------------------+-------+---------+------------+--------+----------+--------+
-| .. c:macro:: PyBUF_STRIDED_RO | yes | yes | NULL | U | 1 or 0 | NULL |
-+-------------------------------+-------+---------+------------+--------+----------+--------+
-| .. c:macro:: PyBUF_CONTIG | yes | NULL | NULL | C | 0 | NULL |
-+-------------------------------+-------+---------+------------+--------+----------+--------+
-| .. c:macro:: PyBUF_CONTIG_RO | yes | NULL | NULL | C | 1 or 0 | NULL |
-+-------------------------------+-------+---------+------------+--------+----------+--------+
+.. c:function:: PyObject *PyMemoryView_GetContiguous(PyObject *obj, int buffertype, char order)
+ Create a memoryview object to a contiguous chunk of memory (in either
+ 'C' or 'F'ortran *order*) from an object that defines the buffer
+ interface. If memory is contiguous, the memoryview object points to the
+ original memory. Otherwise copy is made and the memoryview points to a
+ new bytes object.
-Complex arrays
-==============
-
-NumPy-style: shape and strides
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-The logical structure of NumPy-style arrays is defined by :c:member:`~Py_buffer.itemsize`,
-:c:member:`~Py_buffer.ndim`, :c:member:`~Py_buffer.shape` and :c:member:`~Py_buffer.strides`.
-
-If ``ndim == 0``, the memory location pointed to by :c:member:`~Py_buffer.buf` is
-interpreted as a scalar of size :c:member:`~Py_buffer.itemsize`. In that case,
-both :c:member:`~Py_buffer.shape` and :c:member:`~Py_buffer.strides` are ``NULL``.
-
-If :c:member:`~Py_buffer.strides` is ``NULL``, the array is interpreted as
-a standard n-dimensional C-array. Otherwise, the consumer must access an
-n-dimensional array as follows:
-
-.. code-block:: c
-
- ptr = (char *)buf + indices[0] * strides[0] + ... + indices[n-1] * strides[n-1];
- item = *((typeof(item) *)ptr);
-
-
-As noted above, :c:member:`~Py_buffer.buf` can point to any location within
-the actual memory block. An exporter can check the validity of a buffer with
-this function:
-
-.. code-block:: python
-
- def verify_structure(memlen, itemsize, ndim, shape, strides, offset):
- """Verify that the parameters represent a valid array within
- the bounds of the allocated memory:
- char *mem: start of the physical memory block
- memlen: length of the physical memory block
- offset: (char *)buf - mem
- """
- if offset % itemsize:
- return False
- if offset < 0 or offset+itemsize > memlen:
- return False
- if any(v % itemsize for v in strides):
- return False
-
- if ndim <= 0:
- return ndim == 0 and not shape and not strides
- if 0 in shape:
- return True
-
- imin = sum(strides[j]*(shape[j]-1) for j in range(ndim)
- if strides[j] <= 0)
- imax = sum(strides[j]*(shape[j]-1) for j in range(ndim)
- if strides[j] > 0)
-
- return 0 <= offset+imin and offset+imax+itemsize <= memlen
-
-
-PIL-style: shape, strides and suboffsets
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-
-In addition to the regular items, PIL-style arrays can contain pointers
-that must be followed in order to get to the next element in a dimension.
-For example, the regular three-dimensional C-array ``char v[2][2][3]`` can
-also be viewed as an array of 2 pointers to 2 two-dimensional arrays:
-``char (*v[2])[2][3]``. In suboffsets representation, those two pointers
-can be embedded at the start of :c:member:`~Py_buffer.buf`, pointing
-to two ``char x[2][3]`` arrays that can be located anywhere in memory.
-
-
-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::
-
- void *get_item_pointer(int ndim, void *buf, Py_ssize_t *strides,
- Py_ssize_t *suboffsets, Py_ssize_t *indices) {
- char *pointer = (char*)buf;
- int i;
- for (i = 0; i < ndim; i++) {
- pointer += strides[i] * indices[i];
- if (suboffsets[i] >=0 ) {
- pointer = *((char**)pointer) + suboffsets[i];
- }
- }
- return (void*)pointer;
- }
+.. c:function:: int PyMemoryView_Check(PyObject *obj)
+
+ Return true if the object *obj* is a memoryview object. It is not
+ currently allowed to create subclasses of :class:`memoryview`.
+
+
+.. c:function:: Py_buffer *PyMemoryView_GET_BUFFER(PyObject *obj)
+ Return a pointer to the buffer-info structure wrapped by the given
+ object. The object **must** be a memoryview instance; this macro doesn't
+ check its type, you must do it yourself or you will risk crashes.
-Buffer-related functions
+
+Old-style buffer objects
========================
-.. c:function:: int PyObject_CheckBuffer(PyObject *obj)
+.. index:: single: PyBufferProcs
- Return ``1`` if *obj* supports the buffer interface otherwise ``0``. When ``1`` is
- returned, it doesn't guarantee that :c:func:`PyObject_GetBuffer` will
- succeed. This function always succeeds.
+More information on the old buffer interface is provided in the section
+:ref:`buffer-structs`, under the description for :c:type:`PyBufferProcs`.
+A "buffer object" is defined in the :file:`bufferobject.h` header (included by
+:file:`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.
-.. c:function:: int PyObject_GetBuffer(PyObject *exporter, Py_buffer *view, int flags)
+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.
- Send a request to *exporter* to fill in *view* as specified by *flags*.
- If the exporter cannot provide a buffer of the exact type, it MUST raise
- :c:data:`PyExc_BufferError`, set :c:member:`view->obj` to ``NULL`` and
- return ``-1``.
- On success, fill in *view*, set :c:member:`view->obj` to a new reference
- to *exporter* and return 0. In the case of chained buffer providers
- that redirect requests to a single object, :c:member:`view->obj` MAY
- refer to this object instead of *exporter* (See :ref:`Buffer Object Structures <buffer-structs>`).
+.. c:type:: PyBufferObject
- Successful calls to :c:func:`PyObject_GetBuffer` must be paired with calls
- to :c:func:`PyBuffer_Release`, similar to :c:func:`malloc` and :c:func:`free`.
- Thus, after the consumer is done with the buffer, :c:func:`PyBuffer_Release`
- must be called exactly once.
+ This subtype of :c:type:`PyObject` represents a buffer object.
-.. c:function:: void PyBuffer_Release(Py_buffer *view)
+.. c:var:: PyTypeObject PyBuffer_Type
- Release the buffer *view* and decrement the reference count for
- :c:member:`view->obj`. This function MUST be called when the buffer
- is no longer being used, otherwise reference leaks may occur.
+ .. index:: single: BufferType (in module types)
- It is an error to call this function on a buffer that was not obtained via
- :c:func:`PyObject_GetBuffer`.
+ The instance of :c:type:`PyTypeObject` which represents the Python buffer type;
+ it is the same object as ``buffer`` and ``types.BufferType`` in the Python
+ layer. .
-.. c:function:: Py_ssize_t PyBuffer_SizeFromFormat(const char *format)
+.. c:var:: int Py_END_OF_BUFFER
- Return the implied :c:data:`~Py_buffer.itemsize` from :c:data:`~Py_buffer.format`.
- On error, raise an exception and return -1.
+ This constant may be passed as the *size* parameter to
+ :c:func:`PyBuffer_FromObject` or :c:func:`PyBuffer_FromReadWriteObject`. It
+ indicates that the new :c:type:`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.
- .. versionadded:: 3.9
+.. c:function:: int PyBuffer_Check(PyObject *p)
-.. c:function:: int PyBuffer_IsContiguous(Py_buffer *view, char order)
+ Return true if the argument has type :c:data:`PyBuffer_Type`.
- Return ``1`` if the memory defined by the *view* is C-style (*order* is
- ``'C'``) or Fortran-style (*order* is ``'F'``) :term:`contiguous` or either one
- (*order* is ``'A'``). Return ``0`` otherwise. This function always succeeds.
+.. c:function:: PyObject* PyBuffer_FromObject(PyObject *base, Py_ssize_t offset, Py_ssize_t size)
-.. c:function:: void* PyBuffer_GetPointer(Py_buffer *view, Py_ssize_t *indices)
+ Return a new read-only buffer object. This raises :exc:`TypeError` if
+ *base* doesn't support the read-only buffer protocol or doesn't provide
+ exactly one buffer segment, or it raises :exc:`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
+ :const:`Py_END_OF_BUFFER`, then the new buffer's contents extend to the
+ length of the *base* object's exported buffer data.
- Get the memory area pointed to by the *indices* inside the given *view*.
- *indices* must point to an array of ``view->ndim`` indices.
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` type for *offset* and *size*. This
+ might require changes in your code for properly supporting 64-bit
+ systems.
-.. c:function:: int PyBuffer_FromContiguous(Py_buffer *view, void *buf, Py_ssize_t len, char fort)
+.. c:function:: PyObject* PyBuffer_FromReadWriteObject(PyObject *base, Py_ssize_t offset, Py_ssize_t size)
- Copy contiguous *len* bytes from *buf* to *view*.
- *fort* can be ``'C'`` or ``'F'`` (for C-style or Fortran-style ordering).
- ``0`` is returned on success, ``-1`` on error.
+ Return a new writable buffer object. Parameters and exceptions are similar
+ to those for :c:func:`PyBuffer_FromObject`. If the *base* object does not
+ export the writeable buffer protocol, then :exc:`TypeError` is raised.
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` type for *offset* and *size*. This
+ might require changes in your code for properly supporting 64-bit
+ systems.
-.. c:function:: int PyBuffer_ToContiguous(void *buf, Py_buffer *src, Py_ssize_t len, char order)
- Copy *len* bytes from *src* to its contiguous representation in *buf*.
- *order* can be ``'C'`` or ``'F'`` or ``'A'`` (for C-style or Fortran-style
- ordering or either one). ``0`` is returned on success, ``-1`` on error.
+.. c:function:: PyObject* PyBuffer_FromMemory(void *ptr, Py_ssize_t size)
- This function fails if *len* != *src->len*.
+ 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 :exc:`ValueError` if *size* is less
+ than zero. Note that :const:`Py_END_OF_BUFFER` may *not* be passed for the
+ *size* parameter; :exc:`ValueError` will be raised in that case.
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` type for *size*. This might require
+ changes in your code for properly supporting 64-bit systems.
-.. c:function:: void PyBuffer_FillContiguousStrides(int ndims, Py_ssize_t *shape, Py_ssize_t *strides, int itemsize, char order)
- Fill the *strides* array with byte-strides of a :term:`contiguous` (C-style if
- *order* is ``'C'`` or Fortran-style if *order* is ``'F'``) array of the
- given shape with the given number of bytes per element.
+.. c:function:: PyObject* PyBuffer_FromReadWriteMemory(void *ptr, Py_ssize_t size)
+ Similar to :c:func:`PyBuffer_FromMemory`, but the returned buffer is
+ writable.
-.. c:function:: int PyBuffer_FillInfo(Py_buffer *view, PyObject *exporter, void *buf, Py_ssize_t len, int readonly, int flags)
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` type for *size*. This might require
+ changes in your code for properly supporting 64-bit systems.
- Handle buffer requests for an exporter that wants to expose *buf* of size *len*
- with writability set according to *readonly*. *buf* is interpreted as a sequence
- of unsigned bytes.
- The *flags* argument indicates the request type. This function always fills in
- *view* as specified by flags, unless *buf* has been designated as read-only
- and :c:macro:`PyBUF_WRITABLE` is set in *flags*.
+.. c:function:: PyObject* PyBuffer_New(Py_ssize_t size)
- On success, set :c:member:`view->obj` to a new reference to *exporter* and
- return 0. Otherwise, raise :c:data:`PyExc_BufferError`, set
- :c:member:`view->obj` to ``NULL`` and return ``-1``;
+ Return a new writable buffer object that maintains its own memory buffer of
+ *size* bytes. :exc:`ValueError` is returned if *size* is not zero or
+ positive. Note that the memory buffer (as returned by
+ :c:func:`PyObject_AsWriteBuffer`) is not specifically aligned.
- If this function is used as part of a :ref:`getbufferproc <buffer-structs>`,
- *exporter* MUST be set to the exporting object and *flags* must be passed
- unmodified. Otherwise, *exporter* MUST be ``NULL``.
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` type for *size*. This might require
+ changes in your code for properly supporting 64-bit systems.
diff --git a/Doc/c-api/bytearray.rst b/Doc/c-api/bytearray.rst
index b2f409c..858c8db 100644
--- a/Doc/c-api/bytearray.rst
+++ b/Doc/c-api/bytearray.rst
@@ -1,4 +1,4 @@
-.. highlight:: c
+.. highlightlang:: c
.. _bytearrayobjects:
@@ -7,6 +7,8 @@ Byte Array Objects
.. index:: object: bytearray
+.. versionadded:: 2.6
+
.. c:type:: PyByteArrayObject
@@ -16,8 +18,7 @@ Byte Array Objects
.. c:var:: PyTypeObject PyByteArray_Type
This instance of :c:type:`PyTypeObject` represents the Python bytearray type;
- it is the same object as :class:`bytearray` in the Python layer.
-
+ it is the same object as ``bytearray`` in the Python layer.
Type check macros
^^^^^^^^^^^^^^^^^
@@ -40,7 +41,7 @@ Direct API functions
.. c:function:: PyObject* PyByteArray_FromObject(PyObject *o)
Return a new bytearray object from any object, *o*, that implements the
- :ref:`buffer protocol <bufferobjects>`.
+ buffer protocol.
.. XXX expand about the buffer protocol, at least somewhere
@@ -48,7 +49,7 @@ Direct API functions
.. c:function:: 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.
+ failure, *NULL* is returned.
.. c:function:: PyObject* PyByteArray_Concat(PyObject *a, PyObject *b)
@@ -58,14 +59,13 @@ Direct API functions
.. c:function:: Py_ssize_t PyByteArray_Size(PyObject *bytearray)
- Return the size of *bytearray* after checking for a ``NULL`` pointer.
+ Return the size of *bytearray* after checking for a *NULL* pointer.
.. c:function:: char* PyByteArray_AsString(PyObject *bytearray)
Return the contents of *bytearray* as a char array after checking for a
- ``NULL`` pointer. The returned array always has an extra
- null byte appended.
+ *NULL* pointer.
.. c:function:: int PyByteArray_Resize(PyObject *bytearray, Py_ssize_t len)
diff --git a/Doc/c-api/bytes.rst b/Doc/c-api/bytes.rst
deleted file mode 100644
index 0e33ed2..0000000
--- a/Doc/c-api/bytes.rst
+++ /dev/null
@@ -1,205 +0,0 @@
-.. highlight:: c
-
-.. _bytesobjects:
-
-Bytes Objects
--------------
-
-These functions raise :exc:`TypeError` when expecting a bytes parameter and are
-called with a non-bytes parameter.
-
-.. index:: object: bytes
-
-
-.. c:type:: PyBytesObject
-
- This subtype of :c:type:`PyObject` represents a Python bytes object.
-
-
-.. c:var:: PyTypeObject PyBytes_Type
-
- This instance of :c:type:`PyTypeObject` represents the Python bytes type; it
- is the same object as :class:`bytes` in the Python layer.
-
-
-.. c:function:: int PyBytes_Check(PyObject *o)
-
- Return true if the object *o* is a bytes object or an instance of a subtype
- of the bytes type.
-
-
-.. c:function:: int PyBytes_CheckExact(PyObject *o)
-
- Return true if the object *o* is a bytes object, but not an instance of a
- subtype of the bytes type.
-
-
-.. c:function:: PyObject* PyBytes_FromString(const char *v)
-
- Return a new bytes 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.
-
-
-.. c:function:: PyObject* PyBytes_FromStringAndSize(const char *v, Py_ssize_t len)
-
- Return a new bytes object with a copy of the string *v* as value and length
- *len* on success, and ``NULL`` on failure. If *v* is ``NULL``, the contents of
- the bytes object are uninitialized.
-
-
-.. c:function:: PyObject* PyBytes_FromFormat(const char *format, ...)
-
- Take a C :c:func:`printf`\ -style *format* string and a variable number of
- arguments, calculate the size of the resulting Python bytes object and return
- a bytes object 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:
-
- .. % XXX: This should be exactly the same as the table in PyErr_Format.
- .. % One should just refer to the other.
- .. % XXX: The descriptions for %zd and %zu are wrong, but the truth is complicated
- .. % because not all compilers support the %z width modifier -- we fake it
- .. % when necessary via interpolating PY_FORMAT_SIZE_T.
-
- .. tabularcolumns:: |l|l|L|
-
- +-------------------+---------------+--------------------------------+
- | Format Characters | Type | Comment |
- +===================+===============+================================+
- | :attr:`%%` | *n/a* | The literal % character. |
- +-------------------+---------------+--------------------------------+
- | :attr:`%c` | int | A single byte, |
- | | | represented as a C int. |
- +-------------------+---------------+--------------------------------+
- | :attr:`%d` | int | Equivalent to |
- | | | ``printf("%d")``. [1]_ |
- +-------------------+---------------+--------------------------------+
- | :attr:`%u` | unsigned int | Equivalent to |
- | | | ``printf("%u")``. [1]_ |
- +-------------------+---------------+--------------------------------+
- | :attr:`%ld` | long | Equivalent to |
- | | | ``printf("%ld")``. [1]_ |
- +-------------------+---------------+--------------------------------+
- | :attr:`%lu` | unsigned long | Equivalent to |
- | | | ``printf("%lu")``. [1]_ |
- +-------------------+---------------+--------------------------------+
- | :attr:`%zd` | Py_ssize_t | Equivalent to |
- | | | ``printf("%zd")``. [1]_ |
- +-------------------+---------------+--------------------------------+
- | :attr:`%zu` | size_t | Equivalent to |
- | | | ``printf("%zu")``. [1]_ |
- +-------------------+---------------+--------------------------------+
- | :attr:`%i` | int | Equivalent to |
- | | | ``printf("%i")``. [1]_ |
- +-------------------+---------------+--------------------------------+
- | :attr:`%x` | int | Equivalent to |
- | | | ``printf("%x")``. [1]_ |
- +-------------------+---------------+--------------------------------+
- | :attr:`%s` | const char\* | A null-terminated C character |
- | | | array. |
- +-------------------+---------------+--------------------------------+
- | :attr:`%p` | const void\* | The hex representation of a C |
- | | | pointer. Mostly equivalent to |
- | | | ``printf("%p")`` except that |
- | | | it is guaranteed to start with |
- | | | the literal ``0x`` regardless |
- | | | of what the platform's |
- | | | ``printf`` yields. |
- +-------------------+---------------+--------------------------------+
-
- An unrecognized format character causes all the rest of the format string to be
- copied as-is to the result object, and any extra arguments discarded.
-
- .. [1] For integer specifiers (d, u, ld, lu, zd, zu, i, x): the 0-conversion
- flag has effect even when a precision is given.
-
-
-.. c:function:: PyObject* PyBytes_FromFormatV(const char *format, va_list vargs)
-
- Identical to :c:func:`PyBytes_FromFormat` except that it takes exactly two
- arguments.
-
-
-.. c:function:: PyObject* PyBytes_FromObject(PyObject *o)
-
- Return the bytes representation of object *o* that implements the buffer
- protocol.
-
-
-.. c:function:: Py_ssize_t PyBytes_Size(PyObject *o)
-
- Return the length of the bytes in bytes object *o*.
-
-
-.. c:function:: Py_ssize_t PyBytes_GET_SIZE(PyObject *o)
-
- Macro form of :c:func:`PyBytes_Size` but without error checking.
-
-
-.. c:function:: char* PyBytes_AsString(PyObject *o)
-
- Return a pointer to the contents of *o*. The pointer
- refers to the internal buffer of *o*, which consists of ``len(o) + 1``
- bytes. The last byte in the buffer is always null, regardless of
- whether there are any other null bytes. The data must not be
- modified in any way, unless the object was just created using
- ``PyBytes_FromStringAndSize(NULL, size)``. It must not be deallocated. If
- *o* is not a bytes object at all, :c:func:`PyBytes_AsString` returns ``NULL``
- and raises :exc:`TypeError`.
-
-
-.. c:function:: char* PyBytes_AS_STRING(PyObject *string)
-
- Macro form of :c:func:`PyBytes_AsString` but without error checking.
-
-
-.. c:function:: int PyBytes_AsStringAndSize(PyObject *obj, char **buffer, Py_ssize_t *length)
-
- Return the null-terminated contents of the object *obj*
- through the output variables *buffer* and *length*.
-
- If *length* is ``NULL``, the bytes object
- may not contain embedded null bytes;
- if it does, the function returns ``-1`` and a :exc:`ValueError` is raised.
-
- The buffer refers to an internal buffer of *obj*, which includes an
- additional null byte at the end (not counted in *length*). The data
- must not be modified in any way, unless the object was just created using
- ``PyBytes_FromStringAndSize(NULL, size)``. It must not be deallocated. If
- *obj* is not a bytes object at all, :c:func:`PyBytes_AsStringAndSize`
- returns ``-1`` and raises :exc:`TypeError`.
-
- .. versionchanged:: 3.5
- Previously, :exc:`TypeError` was raised when embedded null bytes were
- encountered in the bytes object.
-
-
-.. c:function:: void PyBytes_Concat(PyObject **bytes, PyObject *newpart)
-
- Create a new bytes object in *\*bytes* containing the contents of *newpart*
- appended to *bytes*; the caller will own the new reference. The reference to
- the old value of *bytes* will be stolen. If the new object cannot be
- created, the old reference to *bytes* will still be discarded and the value
- of *\*bytes* will be set to ``NULL``; the appropriate exception will be set.
-
-
-.. c:function:: void PyBytes_ConcatAndDel(PyObject **bytes, PyObject *newpart)
-
- Create a new bytes object in *\*bytes* containing the contents of *newpart*
- appended to *bytes*. This version decrements the reference count of
- *newpart*.
-
-
-.. c:function:: int _PyBytes_Resize(PyObject **bytes, Py_ssize_t newsize)
-
- A way to resize a bytes object even though it is "immutable". Only use this
- to build up a brand new bytes object; don't use this if the bytes may already
- be known in other parts of the code. It is an error to call this function if
- the refcount on the input bytes object is not one. Pass the address of an
- existing bytes object as an lvalue (it may be written into), and the new size
- desired. On success, *\*bytes* holds the resized bytes object and ``0`` is
- returned; the address in *\*bytes* may differ from its input value. If the
- reallocation fails, the original bytes object at *\*bytes* is deallocated,
- *\*bytes* is set to ``NULL``, :exc:`MemoryError` is set, and ``-1`` is
- returned.
diff --git a/Doc/c-api/call.rst b/Doc/c-api/call.rst
deleted file mode 100644
index 0833531..0000000
--- a/Doc/c-api/call.rst
+++ /dev/null
@@ -1,411 +0,0 @@
-.. highlight:: c
-
-.. _call:
-
-Call Protocol
-=============
-
-CPython supports two different calling protocols:
-*tp_call* and vectorcall.
-
-The *tp_call* Protocol
-----------------------
-
-Instances of classes that set :c:member:`~PyTypeObject.tp_call` are callable.
-The signature of the slot is::
-
- PyObject *tp_call(PyObject *callable, PyObject *args, PyObject *kwargs);
-
-A call is made using a tuple for the positional arguments
-and a dict for the keyword arguments, similarly to
-``callable(*args, **kwargs)`` in Python code.
-*args* must be non-NULL (use an empty tuple if there are no arguments)
-but *kwargs* may be *NULL* if there are no keyword arguments.
-
-This convention is not only used by *tp_call*:
-:c:member:`~PyTypeObject.tp_new` and :c:member:`~PyTypeObject.tp_init`
-also pass arguments this way.
-
-To call an object, use :c:func:`PyObject_Call` or other
-:ref:`call API <capi-call>`.
-
-
-.. _vectorcall:
-
-The Vectorcall Protocol
------------------------
-
-.. versionadded:: 3.8
-
-The vectorcall protocol was introduced in :pep:`590` as an additional protocol
-for making calls more efficient.
-
-.. warning::
-
- The vectorcall API is provisional and expected to become public in
- Python 3.9, with a different names and, possibly, changed semantics.
- If you use the it, plan for updating your code for Python 3.9.
-
-As rule of thumb, CPython will prefer the vectorcall for internal calls
-if the callable supports it. However, this is not a hard rule.
-Additionally, some third-party extensions use *tp_call* directly
-(rather than using :c:func:`PyObject_Call`).
-Therefore, a class supporting vectorcall must also implement
-:c:member:`~PyTypeObject.tp_call`.
-Moreover, the callable must behave the same
-regardless of which protocol is used.
-The recommended way to achieve this is by setting
-:c:member:`~PyTypeObject.tp_call` to :c:func:`PyVectorcall_Call`.
-This bears repeating:
-
-.. warning::
-
- A class supporting vectorcall **must** also implement
- :c:member:`~PyTypeObject.tp_call` with the same semantics.
-
-A class should not implement vectorcall if that would be slower
-than *tp_call*. For example, if the callee needs to convert
-the arguments to an args tuple and kwargs dict anyway, then there is no point
-in implementing vectorcall.
-
-Classes can implement the vectorcall protocol by enabling the
-:const:`_Py_TPFLAGS_HAVE_VECTORCALL` flag and setting
-:c:member:`~PyTypeObject.tp_vectorcall_offset` to the offset inside the
-object structure where a *vectorcallfunc* appears.
-This is a pointer to a function with the following signature:
-
-.. c:type:: PyObject *(*vectorcallfunc)(PyObject *callable, PyObject *const *args, size_t nargsf, PyObject *kwnames)
-
-- *callable* is the object being called.
-- *args* is a C array consisting of the positional arguments followed by the
- values of the keyword arguments.
- This can be *NULL* if there are no arguments.
-- *nargsf* is the number of positional arguments plus possibly the
- :const:`PY_VECTORCALL_ARGUMENTS_OFFSET` flag.
- To get the actual number of positional arguments from *nargsf*,
- use :c:func:`PyVectorcall_NARGS`.
-- *kwnames* is a tuple containing the names of the keyword arguments;
- in other words, the keys of the kwargs dict.
- These names must be strings (instances of ``str`` or a subclass)
- and they must be unique.
- If there are no keyword arguments, then *kwnames* can instead be *NULL*.
-
-.. c:var:: PY_VECTORCALL_ARGUMENTS_OFFSET
-
- If this flag is set in a vectorcall *nargsf* argument, the callee is allowed
- to temporarily change ``args[-1]``. In other words, *args* points to
- argument 1 (not 0) in the allocated vector.
- The callee must restore the value of ``args[-1]`` before returning.
-
- For :c:func:`_PyObject_VectorcallMethod`, this flag means instead that
- ``args[0]`` may be changed.
-
- Whenever they can do so cheaply (without additional allocation), callers
- are encouraged to use :const:`PY_VECTORCALL_ARGUMENTS_OFFSET`.
- Doing so will allow callables such as bound methods to make their onward
- calls (which include a prepended *self* argument) very efficiently.
-
-To call an object that implements vectorcall, use a :ref:`call API <capi-call>`
-function as with any other callable.
-:c:func:`_PyObject_Vectorcall` will usually be most efficient.
-
-
-Recursion Control
-.................
-
-When using *tp_call*, callees do not need to worry about
-:ref:`recursion <recursion>`: CPython uses
-:c:func:`Py_EnterRecursiveCall` and :c:func:`Py_LeaveRecursiveCall`
-for calls made using *tp_call*.
-
-For efficiency, this is not the case for calls done using vectorcall:
-the callee should use *Py_EnterRecursiveCall* and *Py_LeaveRecursiveCall*
-if needed.
-
-
-Vectorcall Support API
-......................
-
-.. c:function:: Py_ssize_t PyVectorcall_NARGS(size_t nargsf)
-
- Given a vectorcall *nargsf* argument, return the actual number of
- arguments.
- Currently equivalent to::
-
- (Py_ssize_t)(nargsf & ~PY_VECTORCALL_ARGUMENTS_OFFSET)
-
- However, the function ``PyVectorcall_NARGS`` should be used to allow
- for future extensions.
-
- .. versionadded:: 3.8
-
-.. c:function:: vectorcallfunc _PyVectorcall_Function(PyObject *op)
-
- If *op* does not support the vectorcall protocol (either because the type
- does not or because the specific instance does not), return *NULL*.
- Otherwise, return the vectorcall function pointer stored in *op*.
- This function never raises an exception.
-
- This is mostly useful to check whether or not *op* supports vectorcall,
- which can be done by checking ``_PyVectorcall_Function(op) != NULL``.
-
- .. versionadded:: 3.8
-
-.. c:function:: PyObject* PyVectorcall_Call(PyObject *callable, PyObject *tuple, PyObject *dict)
-
- Call *callable*'s :c:type:`vectorcallfunc` with positional and keyword
- arguments given in a tuple and dict, respectively.
-
- This is a specialized function, intended to be put in the
- :c:member:`~PyTypeObject.tp_call` slot or be used in an implementation of ``tp_call``.
- It does not check the :const:`_Py_TPFLAGS_HAVE_VECTORCALL` flag
- and it does not fall back to ``tp_call``.
-
- .. versionadded:: 3.8
-
-
-.. _capi-call:
-
-Object Calling API
-------------------
-
-Various functions are available for calling a Python object.
-Each converts its arguments to a convention supported by the called object –
-either *tp_call* or vectorcall.
-In order to do as litle conversion as possible, pick one that best fits
-the format of data you have available.
-
-The following table summarizes the available functions;
-please see individual documentation for details.
-
-+------------------------------------------+------------------+--------------------+---------------+
-| Function | callable | args | kwargs |
-+==========================================+==================+====================+===============+
-| :c:func:`PyObject_Call` | ``PyObject *`` | tuple | dict/``NULL`` |
-+------------------------------------------+------------------+--------------------+---------------+
-| :c:func:`PyObject_CallNoArgs` | ``PyObject *`` | --- | --- |
-+------------------------------------------+------------------+--------------------+---------------+
-| :c:func:`_PyObject_CallOneArg` | ``PyObject *`` | 1 object | --- |
-+------------------------------------------+------------------+--------------------+---------------+
-| :c:func:`PyObject_CallObject` | ``PyObject *`` | tuple/``NULL`` | --- |
-+------------------------------------------+------------------+--------------------+---------------+
-| :c:func:`PyObject_CallFunction` | ``PyObject *`` | format | --- |
-+------------------------------------------+------------------+--------------------+---------------+
-| :c:func:`PyObject_CallMethod` | obj + ``char*`` | format | --- |
-+------------------------------------------+------------------+--------------------+---------------+
-| :c:func:`PyObject_CallFunctionObjArgs` | ``PyObject *`` | variadic | --- |
-+------------------------------------------+------------------+--------------------+---------------+
-| :c:func:`PyObject_CallMethodObjArgs` | obj + name | variadic | --- |
-+------------------------------------------+------------------+--------------------+---------------+
-| :c:func:`_PyObject_CallMethodNoArgs` | obj + name | --- | --- |
-+------------------------------------------+------------------+--------------------+---------------+
-| :c:func:`_PyObject_CallMethodOneArg` | obj + name | 1 object | --- |
-+------------------------------------------+------------------+--------------------+---------------+
-| :c:func:`_PyObject_Vectorcall` | ``PyObject *`` | vectorcall | vectorcall |
-+------------------------------------------+------------------+--------------------+---------------+
-| :c:func:`_PyObject_FastCallDict` | ``PyObject *`` | vectorcall | dict/``NULL`` |
-+------------------------------------------+------------------+--------------------+---------------+
-| :c:func:`_PyObject_VectorcallMethod` | arg + name | vectorcall | vectorcall |
-+------------------------------------------+------------------+--------------------+---------------+
-
-
-.. c:function:: PyObject* PyObject_Call(PyObject *callable, PyObject *args, PyObject *kwargs)
-
- Call a callable Python object *callable*, with arguments given by the
- tuple *args*, and named arguments given by the dictionary *kwargs*.
-
- *args* must not be *NULL*; use an empty tuple if no arguments are needed.
- If no named arguments are needed, *kwargs* can be *NULL*.
-
- Return the result of the call on success, or raise an exception and return
- *NULL* on failure.
-
- This is the equivalent of the Python expression:
- ``callable(*args, **kwargs)``.
-
-
-.. c:function:: PyObject* PyObject_CallNoArgs(PyObject *callable)
-
- Call a callable Python object *callable* without any arguments. It is the
- most efficient way to call a callable Python object without any argument.
-
- Return the result of the call on success, or raise an exception and return
- *NULL* on failure.
-
- .. versionadded:: 3.9
-
-
-.. c:function:: PyObject* _PyObject_CallOneArg(PyObject *callable, PyObject *arg)
-
- Call a callable Python object *callable* with exactly 1 positional argument
- *arg* and no keyword arguments.
-
- Return the result of the call on success, or raise an exception and return
- *NULL* on failure.
-
- .. versionadded:: 3.9
-
-
-.. c:function:: PyObject* PyObject_CallObject(PyObject *callable, PyObject *args)
-
- Call a callable Python object *callable*, with arguments given by the
- tuple *args*. If no arguments are needed, then *args* can be *NULL*.
-
- Return the result of the call on success, or raise an exception and return
- *NULL* on failure.
-
- This is the equivalent of the Python expression: ``callable(*args)``.
-
-
-.. c:function:: PyObject* PyObject_CallFunction(PyObject *callable, const char *format, ...)
-
- Call a callable Python object *callable*, with a variable number of C arguments.
- The C arguments are described using a :c:func:`Py_BuildValue` style format
- string. The format can be *NULL*, indicating that no arguments are provided.
-
- Return the result of the call on success, or raise an exception and return
- *NULL* on failure.
-
- This is the equivalent of the Python expression: ``callable(*args)``.
-
- Note that if you only pass :c:type:`PyObject \*` args,
- :c:func:`PyObject_CallFunctionObjArgs` is a faster alternative.
-
- .. versionchanged:: 3.4
- The type of *format* was changed from ``char *``.
-
-
-.. c:function:: PyObject* PyObject_CallMethod(PyObject *obj, const char *name, const char *format, ...)
-
- Call the method named *name* of object *obj* with a variable number of C
- arguments. The C arguments are described by a :c:func:`Py_BuildValue` format
- string that should produce a tuple.
-
- The format can be *NULL*, indicating that no arguments are provided.
-
- Return the result of the call on success, or raise an exception and return
- *NULL* on failure.
-
- This is the equivalent of the Python expression:
- ``obj.name(arg1, arg2, ...)``.
-
- Note that if you only pass :c:type:`PyObject \*` args,
- :c:func:`PyObject_CallMethodObjArgs` is a faster alternative.
-
- .. versionchanged:: 3.4
- The types of *name* and *format* were changed from ``char *``.
-
-
-.. c:function:: PyObject* PyObject_CallFunctionObjArgs(PyObject *callable, ..., NULL)
-
- Call a callable Python object *callable*, with a variable number of
- :c:type:`PyObject \*` arguments. The arguments are provided as a variable number
- of parameters followed by *NULL*.
-
- Return the result of the call on success, or raise an exception and return
- *NULL* on failure.
-
- This is the equivalent of the Python expression:
- ``callable(arg1, arg2, ...)``.
-
-
-.. c:function:: PyObject* PyObject_CallMethodObjArgs(PyObject *obj, PyObject *name, ..., NULL)
-
- Call a method of the Python object *obj*, where the name of the method is given as a
- Python string object in *name*. It is called with a variable number of
- :c:type:`PyObject \*` arguments. The arguments are provided as a variable number
- of parameters followed by *NULL*.
-
- Return the result of the call on success, or raise an exception and return
- *NULL* on failure.
-
-
-.. c:function:: PyObject* _PyObject_CallMethodNoArgs(PyObject *obj, PyObject *name)
-
- Call a method of the Python object *obj* without arguments,
- where the name of the method is given as a Python string object in *name*.
-
- Return the result of the call on success, or raise an exception and return
- *NULL* on failure.
-
- .. versionadded:: 3.9
-
-
-.. c:function:: PyObject* _PyObject_CallMethodOneArg(PyObject *obj, PyObject *name, PyObject *arg)
-
- Call a method of the Python object *obj* with a single positional argument
- *arg*, where the name of the method is given as a Python string object in
- *name*.
-
- Return the result of the call on success, or raise an exception and return
- *NULL* on failure.
-
- .. versionadded:: 3.9
-
-
-.. c:function:: PyObject* _PyObject_Vectorcall(PyObject *callable, PyObject *const *args, size_t nargsf, PyObject *kwnames)
-
- Call a callable Python object *callable*.
- The arguments are the same as for :c:type:`vectorcallfunc`.
- If *callable* supports vectorcall_, this directly calls
- the vectorcall function stored in *callable*.
-
- Return the result of the call on success, or raise an exception and return
- *NULL* on failure.
-
- .. note::
-
- This function is provisional and expected to become public in Python 3.9,
- with a different name and, possibly, changed semantics.
- If you use the function, plan for updating your code for Python 3.9.
-
- .. versionadded:: 3.8
-
-.. c:function:: PyObject* _PyObject_FastCallDict(PyObject *callable, PyObject *const *args, size_t nargsf, PyObject *kwdict)
-
- Call *callable* with positional arguments passed exactly as in the vectorcall_ protocol,
- but with keyword arguments passed as a dictionary *kwdict*.
- The *args* array contains only the positional arguments.
-
- Regardless of which protocol is used internally,
- a conversion of arguments needs to be done.
- Therefore, this function should only be used if the caller
- already has a dictionary ready to use for the keyword arguments,
- but not a tuple for the positional arguments.
-
- .. note::
-
- This function is provisional and expected to become public in Python 3.9,
- with a different name and, possibly, changed semantics.
- If you use the function, plan for updating your code for Python 3.9.
-
- .. versionadded:: 3.8
-
-.. c:function:: PyObject* _PyObject_VectorcallMethod(PyObject *name, PyObject *const *args, size_t nargsf, PyObject *kwnames)
-
- Call a method using the vectorcall calling convention. The name of the method
- is given as a Python string *name*. The object whose method is called is
- *args[0]*, and the *args* array starting at *args[1]* represents the arguments
- of the call. There must be at least one positional argument.
- *nargsf* is the number of positional arguments including *args[0]*,
- plus :const:`PY_VECTORCALL_ARGUMENTS_OFFSET` if the value of ``args[0]`` may
- temporarily be changed. Keyword arguments can be passed just like in
- :c:func:`_PyObject_Vectorcall`.
-
- If the object has the :const:`Py_TPFLAGS_METHOD_DESCRIPTOR` feature,
- this will call the unbound method object with the full
- *args* vector as arguments.
-
- Return the result of the call on success, or raise an exception and return
- *NULL* on failure.
-
- .. versionadded:: 3.9
-
-
-Call Support API
-----------------
-
-.. c:function:: 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.
diff --git a/Doc/c-api/capsule.rst b/Doc/c-api/capsule.rst
index 78e2114..fd4279c 100644
--- a/Doc/c-api/capsule.rst
+++ b/Doc/c-api/capsule.rst
@@ -1,4 +1,4 @@
-.. highlight:: c
+.. highlightlang:: c
.. _capsules:
@@ -9,7 +9,7 @@ Capsules
Refer to :ref:`using-capsules` for more information on using these objects.
-.. versionadded:: 3.1
+.. versionadded:: 2.7
.. c:type:: PyCapsule
@@ -40,15 +40,15 @@ Refer to :ref:`using-capsules` for more information on using these objects.
.. c:function:: PyObject* PyCapsule_New(void *pointer, const char *name, PyCapsule_Destructor destructor)
Create a :c:type:`PyCapsule` encapsulating the *pointer*. The *pointer*
- argument may not be ``NULL``.
+ argument may not be *NULL*.
- On failure, set an exception and return ``NULL``.
+ On failure, set an exception and return *NULL*.
- The *name* string may either be ``NULL`` or a pointer to a valid C string. If
- non-``NULL``, this string must outlive the capsule. (Though it is permitted to
+ The *name* string may either be *NULL* or a pointer to a valid C string. If
+ non-*NULL*, this string must outlive the capsule. (Though it is permitted to
free it inside the *destructor*.)
- If the *destructor* argument is not ``NULL``, it will be called with the
+ If the *destructor* argument is not *NULL*, it will be called with the
capsule as its argument when it is destroyed.
If this capsule will be stored as an attribute of a module, the *name* should
@@ -59,20 +59,20 @@ Refer to :ref:`using-capsules` for more information on using these objects.
.. c:function:: void* PyCapsule_GetPointer(PyObject *capsule, const char *name)
Retrieve the *pointer* stored in the capsule. On failure, set an exception
- and return ``NULL``.
+ and return *NULL*.
The *name* parameter must compare exactly to the name stored in the capsule.
- If the name stored in the capsule is ``NULL``, the *name* passed in must also
- be ``NULL``. Python uses the C function :c:func:`strcmp` to compare capsule
+ If the name stored in the capsule is *NULL*, the *name* passed in must also
+ be *NULL*. Python uses the C function :c:func:`strcmp` to compare capsule
names.
.. c:function:: PyCapsule_Destructor PyCapsule_GetDestructor(PyObject *capsule)
Return the current destructor stored in the capsule. On failure, set an
- exception and return ``NULL``.
+ exception and return *NULL*.
- It is legal for a capsule to have a ``NULL`` destructor. This makes a ``NULL``
+ It is legal for a capsule to have a *NULL* destructor. This makes a *NULL*
return code somewhat ambiguous; use :c:func:`PyCapsule_IsValid` or
:c:func:`PyErr_Occurred` to disambiguate.
@@ -80,9 +80,9 @@ Refer to :ref:`using-capsules` for more information on using these objects.
.. c:function:: void* PyCapsule_GetContext(PyObject *capsule)
Return the current context stored in the capsule. On failure, set an
- exception and return ``NULL``.
+ exception and return *NULL*.
- It is legal for a capsule to have a ``NULL`` context. This makes a ``NULL``
+ It is legal for a capsule to have a *NULL* context. This makes a *NULL*
return code somewhat ambiguous; use :c:func:`PyCapsule_IsValid` or
:c:func:`PyErr_Occurred` to disambiguate.
@@ -90,9 +90,9 @@ Refer to :ref:`using-capsules` for more information on using these objects.
.. c:function:: const char* PyCapsule_GetName(PyObject *capsule)
Return the current name stored in the capsule. On failure, set an exception
- and return ``NULL``.
+ and return *NULL*.
- It is legal for a capsule to have a ``NULL`` name. This makes a ``NULL`` return
+ It is legal for a capsule to have a *NULL* name. This makes a *NULL* return
code somewhat ambiguous; use :c:func:`PyCapsule_IsValid` or
:c:func:`PyErr_Occurred` to disambiguate.
@@ -107,13 +107,13 @@ Refer to :ref:`using-capsules` for more information on using these objects.
import the module conventionally (using :c:func:`PyImport_ImportModule`).
Return the capsule's internal *pointer* on success. On failure, set an
- exception and return ``NULL``.
+ exception and return *NULL*.
.. c:function:: int PyCapsule_IsValid(PyObject *capsule, const char *name)
Determines whether or not *capsule* is a valid capsule. A valid capsule is
- non-``NULL``, passes :c:func:`PyCapsule_CheckExact`, has a non-``NULL`` pointer
+ non-*NULL*, passes :c:func:`PyCapsule_CheckExact`, has a non-*NULL* pointer
stored in it, and its internal name matches the *name* parameter. (See
:c:func:`PyCapsule_GetPointer` for information on how capsule names are
compared.)
@@ -142,9 +142,9 @@ Refer to :ref:`using-capsules` for more information on using these objects.
.. c:function:: int PyCapsule_SetName(PyObject *capsule, const char *name)
- Set the name inside *capsule* to *name*. If non-``NULL``, the name must
+ 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.
+ *NULL*, no attempt is made to free it.
Return ``0`` on success. Return nonzero and set an exception on failure.
@@ -152,6 +152,6 @@ Refer to :ref:`using-capsules` for more information on using these objects.
.. c:function:: int PyCapsule_SetPointer(PyObject *capsule, void *pointer)
Set the void pointer inside *capsule* to *pointer*. The pointer may not be
- ``NULL``.
+ *NULL*.
Return ``0`` on success. Return nonzero and set an exception on failure.
diff --git a/Doc/c-api/cell.rst b/Doc/c-api/cell.rst
index 8408f7e..427259c 100644
--- a/Doc/c-api/cell.rst
+++ b/Doc/c-api/cell.rst
@@ -1,4 +1,4 @@
-.. highlight:: c
+.. highlightlang:: c
.. _cell-objects:
@@ -27,13 +27,13 @@ Cell objects are not likely to be useful elsewhere.
.. c:function:: int PyCell_Check(ob)
- Return true if *ob* is a cell object; *ob* must not be ``NULL``.
+ Return true if *ob* is a cell object; *ob* must not be *NULL*.
.. c:function:: PyObject* PyCell_New(PyObject *ob)
Create and return a new cell object containing the value *ob*. The parameter may
- be ``NULL``.
+ be *NULL*.
.. c:function:: PyObject* PyCell_Get(PyObject *cell)
@@ -44,19 +44,19 @@ Cell objects are not likely to be useful elsewhere.
.. c:function:: PyObject* PyCell_GET(PyObject *cell)
Return the contents of the cell *cell*, but without checking that *cell* is
- non-``NULL`` and a cell object.
+ non-*NULL* and a cell object.
.. c:function:: int PyCell_Set(PyObject *cell, PyObject *value)
Set the contents of the cell object *cell* to *value*. This releases the
- reference to any current content of the cell. *value* may be ``NULL``. *cell*
- must be non-``NULL``; if it is not a cell object, ``-1`` will be returned. On
+ reference to any current content of the cell. *value* may be *NULL*. *cell*
+ must be non-*NULL*; if it is not a cell object, ``-1`` will be returned. On
success, ``0`` will be returned.
.. c:function:: void PyCell_SET(PyObject *cell, PyObject *value)
Sets the value of the cell object *cell* to *value*. No reference counts are
- adjusted, and no checks are made for safety; *cell* must be non-``NULL`` and must
+ adjusted, and no checks are made for safety; *cell* must be non-*NULL* and must
be a cell object.
diff --git a/Doc/c-api/class.rst b/Doc/c-api/class.rst
new file mode 100644
index 0000000..020309d
--- /dev/null
+++ b/Doc/c-api/class.rst
@@ -0,0 +1,65 @@
+.. highlightlang:: c
+
+.. _classobjects:
+
+Class and Instance Objects
+--------------------------
+
+.. index:: object: class
+
+Note that the class objects described here represent old-style classes, which
+will go away in Python 3. When creating new types for extension modules, you
+will want to work with type objects (section :ref:`typeobjects`).
+
+
+.. c:type:: PyClassObject
+
+ The C structure of the objects used to describe built-in classes.
+
+
+.. c:var:: PyObject* PyClass_Type
+
+ .. index:: single: ClassType (in module types)
+
+ This is the type object for class objects; it is the same object as
+ ``types.ClassType`` in the Python layer.
+
+
+.. c:function:: int PyClass_Check(PyObject *o)
+
+ Return true if the object *o* is a class object, including instances of types
+ derived from the standard class object. Return false in all other cases.
+
+
+.. c:function:: int PyClass_IsSubclass(PyObject *klass, PyObject *base)
+
+ Return true if *klass* is a subclass of *base*. Return false in all other cases.
+
+
+.. index:: object: instance
+
+There are very few functions specific to instance objects.
+
+
+.. c:var:: PyTypeObject PyInstance_Type
+
+ Type object for class instances.
+
+
+.. c:function:: int PyInstance_Check(PyObject *obj)
+
+ Return true if *obj* is an instance.
+
+
+.. c:function:: PyObject* PyInstance_New(PyObject *class, PyObject *arg, PyObject *kw)
+
+ Create a new instance of a specific class. The parameters *arg* and *kw* are
+ used as the positional and keyword parameters to the object's constructor.
+
+
+.. c:function:: PyObject* PyInstance_NewRaw(PyObject *class, PyObject *dict)
+
+ Create a new instance of a specific class without calling its constructor.
+ *class* is the class of new object. The *dict* parameter will be used as the
+ object's :attr:`~object.__dict__`; if *NULL*, a new dictionary will be created for the
+ instance.
diff --git a/Doc/c-api/cobject.rst b/Doc/c-api/cobject.rst
new file mode 100644
index 0000000..eafac0e
--- /dev/null
+++ b/Doc/c-api/cobject.rst
@@ -0,0 +1,59 @@
+.. highlightlang:: c
+
+.. _cobjects:
+
+CObjects
+--------
+
+.. index:: object: CObject
+
+
+.. warning::
+
+ The CObject API is deprecated as of Python 2.7. Please switch to the new
+ :ref:`capsules` API.
+
+.. c:type:: PyCObject
+
+ This subtype of :c:type:`PyObject` represents an opaque value, useful for C
+ extension modules who need to pass an opaque value (as a :c:type:`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.
+
+
+.. c:function:: int PyCObject_Check(PyObject *p)
+
+ Return true if its argument is a :c:type:`PyCObject`.
+
+
+.. c:function:: PyObject* PyCObject_FromVoidPtr(void* cobj, void (*destr)(void *))
+
+ Create a :c:type:`PyCObject` from the ``void *`` *cobj*. The *destr* function
+ will be called when the object is reclaimed, unless it is *NULL*.
+
+
+.. c:function:: PyObject* PyCObject_FromVoidPtrAndDesc(void* cobj, void* desc, void (*destr)(void *, void *))
+
+ Create a :c:type:`PyCObject` from the :c:type:`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.
+
+
+.. c:function:: void* PyCObject_AsVoidPtr(PyObject* self)
+
+ Return the object :c:type:`void \*` that the :c:type:`PyCObject` *self* was
+ created with.
+
+
+.. c:function:: void* PyCObject_GetDesc(PyObject* self)
+
+ Return the description :c:type:`void \*` that the :c:type:`PyCObject` *self* was
+ created with.
+
+
+.. c:function:: int PyCObject_SetVoidPtr(PyObject* self, void* cobj)
+
+ Set the void pointer inside *self* to *cobj*. The :c:type:`PyCObject` must not
+ have an associated destructor. Return true on success, false on failure.
diff --git a/Doc/c-api/code.rst b/Doc/c-api/code.rst
index 45a6b4a..946ad6e 100644
--- a/Doc/c-api/code.rst
+++ b/Doc/c-api/code.rst
@@ -1,4 +1,4 @@
-.. highlight:: c
+.. highlightlang:: c
.. _codeobjects:
@@ -29,25 +29,20 @@ bound into a function.
Return true if *co* is a :class:`code` object.
-.. c:function:: int PyCode_GetNumFree(PyCodeObject *co)
+.. c:function:: int PyCode_GetNumFree(PyObject *co)
Return the number of free variables in *co*.
-.. c:function:: PyCodeObject* PyCode_New(int argcount, int kwonlyargcount, int nlocals, int stacksize, int flags, PyObject *code, PyObject *consts, PyObject *names, PyObject *varnames, PyObject *freevars, PyObject *cellvars, PyObject *filename, PyObject *name, int firstlineno, PyObject *lnotab)
+.. c:function:: PyCodeObject *PyCode_New(int argcount, int nlocals, int stacksize, int flags, PyObject *code, PyObject *consts, PyObject *names, PyObject *varnames, PyObject *freevars, PyObject *cellvars, PyObject *filename, PyObject *name, int firstlineno, PyObject *lnotab)
- Return a new code object. If you need a dummy code object to create a frame,
- use :c:func:`PyCode_NewEmpty` instead. Calling :c:func:`PyCode_New` directly
- can bind you to a precise Python version since the definition of the bytecode
- changes often.
+ Return a new code object. If you need a dummy code object to
+ create a frame, use :c:func:`PyCode_NewEmpty` instead. Calling
+ :c:func:`PyCode_New` directly can bind you to a precise Python
+ version since the definition of the bytecode changes often.
-.. c:function:: PyCodeObject* PyCode_NewWithPosOnlyArgs(int argcount, int posonlyargcount, int kwonlyargcount, int nlocals, int stacksize, int flags, PyObject *code, PyObject *consts, PyObject *names, PyObject *varnames, PyObject *freevars, PyObject *cellvars, PyObject *filename, PyObject *name, int firstlineno, PyObject *lnotab)
- Similar to :c:func:`PyCode_New`, but with an extra "posonlyargcount" for positonal-only arguments.
-
- .. versionadded:: 3.8
-
-.. c:function:: PyCodeObject* PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno)
+.. c:function:: int PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno)
Return a new empty code object with the specified filename,
function name, and first line number. It is illegal to
- :func:`exec` or :func:`eval` the resulting code object.
+ :keyword:`exec` or :func:`eval` the resulting code object.
diff --git a/Doc/c-api/codec.rst b/Doc/c-api/codec.rst
index 172dcb3..83252af 100644
--- a/Doc/c-api/codec.rst
+++ b/Doc/c-api/codec.rst
@@ -13,7 +13,7 @@ Codec registry and support functions
.. c:function:: int PyCodec_KnownEncoding(const char *encoding)
Return ``1`` or ``0`` depending on whether there is a registered codec for
- the given *encoding*. This function always succeeds.
+ the given *encoding*.
.. c:function:: PyObject* PyCodec_Encode(PyObject *object, const char *encoding, const char *errors)
@@ -21,7 +21,7 @@ Codec registry and support functions
*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
+ be *NULL* to use the default method defined for the codec. Raises a
:exc:`LookupError` if no encoder can be found.
.. c:function:: PyObject* PyCodec_Decode(PyObject *object, const char *encoding, const char *errors)
@@ -30,7 +30,7 @@ Codec registry and support functions
*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
+ be *NULL* to use the default method defined for the codec. Raises a
:exc:`LookupError` if no encoder can be found.
@@ -40,7 +40,7 @@ Codec lookup API
In the following functions, the *encoding* string is looked up converted to all
lower-case characters, which makes encodings looked up through this mechanism
effectively case-insensitive. If no codec is found, a :exc:`KeyError` is set
-and ``NULL`` returned.
+and *NULL* returned.
.. c:function:: PyObject* PyCodec_Encoder(const char *encoding)
@@ -92,7 +92,7 @@ Registry API for Unicode encoding error handlers
.. c:function:: PyObject* PyCodec_LookupError(const char *name)
Lookup the error handling callback function registered under *name*. As a
- special case ``NULL`` can be passed, in which case the error handling callback
+ special case *NULL* can be passed, in which case the error handling callback
for "strict" will be returned.
.. c:function:: PyObject* PyCodec_StrictErrors(PyObject *exc)
@@ -116,8 +116,3 @@ Registry API for Unicode encoding error handlers
Replace the unicode encode error with backslash escapes (``\x``, ``\u`` and
``\U``).
-.. c:function:: PyObject* PyCodec_NameReplaceErrors(PyObject *exc)
-
- Replace the unicode encode error with ``\N{...}`` escapes.
-
- .. versionadded:: 3.5
diff --git a/Doc/c-api/complex.rst b/Doc/c-api/complex.rst
index 06dbb25..2547c1c 100644
--- a/Doc/c-api/complex.rst
+++ b/Doc/c-api/complex.rst
@@ -1,4 +1,4 @@
-.. highlight:: c
+.. highlightlang:: c
.. _complexobjects:
@@ -88,7 +88,7 @@ Complex Numbers as Python Objects
.. c:var:: PyTypeObject PyComplex_Type
This instance of :c:type:`PyTypeObject` represents the Python complex number
- type. It is the same object as :class:`complex` in the Python layer.
+ type. It is the same object as ``complex`` and ``types.ComplexType``.
.. c:function:: int PyComplex_Check(PyObject *p)
@@ -96,12 +96,17 @@ Complex Numbers as Python Objects
Return true if its argument is a :c:type:`PyComplexObject` or a subtype of
:c:type:`PyComplexObject`.
+ .. versionchanged:: 2.2
+ Allowed subtypes to be accepted.
+
.. c:function:: int PyComplex_CheckExact(PyObject *p)
Return true if its argument is a :c:type:`PyComplexObject`, but not a subtype of
:c:type:`PyComplexObject`.
+ .. versionadded:: 2.2
+
.. c:function:: PyObject* PyComplex_FromCComplex(Py_complex v)
@@ -126,13 +131,9 @@ Complex Numbers as Python Objects
.. c:function:: Py_complex PyComplex_AsCComplex(PyObject *op)
Return the :c:type:`Py_complex` value of the complex number *op*.
+ Upon failure, this method returns ``-1.0`` as a real value.
- If *op* is not a Python complex number object but has a :meth:`__complex__`
- method, this method will first be called to convert *op* to a Python complex
- number object. If ``__complex__()`` is not defined then it falls back to
- :meth:`__float__`. If ``__float__()`` is not defined then it falls back
- to :meth:`__index__`. Upon failure, this method returns ``-1.0`` as a real
- value.
-
- .. versionchanged:: 3.8
- Use :meth:`__index__` if available.
+ .. versionchanged:: 2.6
+ If *op* is not a Python complex number object but has a :meth:`__complex__`
+ method, this method will first be called to convert *op* to a Python complex
+ number object.
diff --git a/Doc/c-api/concrete.rst b/Doc/c-api/concrete.rst
index c1d9fa1..30eb78d 100644
--- a/Doc/c-api/concrete.rst
+++ b/Doc/c-api/concrete.rst
@@ -1,4 +1,4 @@
-.. highlight:: c
+.. highlightlang:: c
.. _concrete:
@@ -17,8 +17,8 @@ dictionary, use :c:func:`PyDict_Check`. The chapter is structured like the
.. 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
+ 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.
@@ -44,8 +44,9 @@ Numeric Objects
.. toctree::
- long.rst
+ int.rst
bool.rst
+ long.rst
float.rst
complex.rst
@@ -61,57 +62,48 @@ Generic operations on sequence objects were discussed in the previous chapter;
this section deals with the specific kinds of sequence objects that are
intrinsic to the Python language.
-.. XXX sort out unicode, str, bytes and bytearray
-
.. toctree::
- bytes.rst
bytearray.rst
+ string.rst
unicode.rst
+ buffer.rst
tuple.rst
list.rst
.. _mapobjects:
-Container Objects
-=================
+Mapping Objects
+===============
.. index:: object: mapping
.. toctree::
dict.rst
- set.rst
.. _otherobjects:
-Function Objects
-================
-
-.. toctree::
-
- function.rst
- method.rst
- cell.rst
- code.rst
-
-
Other Objects
=============
.. toctree::
+ class.rst
+ function.rst
+ method.rst
file.rst
module.rst
iterator.rst
descriptor.rst
slice.rst
- memoryview.rst
weakref.rst
capsule.rst
+ cobject.rst
+ cell.rst
gen.rst
- coro.rst
- contextvars.rst
datetime.rst
+ set.rst
+ code.rst
diff --git a/Doc/c-api/contextvars.rst b/Doc/c-api/contextvars.rst
deleted file mode 100644
index 38256a3..0000000
--- a/Doc/c-api/contextvars.rst
+++ /dev/null
@@ -1,144 +0,0 @@
-.. highlight:: c
-
-.. _contextvarsobjects:
-
-Context Variables Objects
--------------------------
-
-.. _contextvarsobjects_pointertype_change:
-.. versionchanged:: 3.7.1
-
- .. note::
-
- In Python 3.7.1 the signatures of all context variables
- C APIs were **changed** to use :c:type:`PyObject` pointers instead
- of :c:type:`PyContext`, :c:type:`PyContextVar`, and
- :c:type:`PyContextToken`, e.g.::
-
- // in 3.7.0:
- PyContext *PyContext_New(void);
-
- // in 3.7.1+:
- PyObject *PyContext_New(void);
-
- See :issue:`34762` for more details.
-
-
-.. versionadded:: 3.7
-
-This section details the public C API for the :mod:`contextvars` module.
-
-.. c:type:: PyContext
-
- The C structure used to represent a :class:`contextvars.Context`
- object.
-
-.. c:type:: PyContextVar
-
- The C structure used to represent a :class:`contextvars.ContextVar`
- object.
-
-.. c:type:: PyContextToken
-
- The C structure used to represent a :class:`contextvars.Token` object.
-
-.. c:var:: PyTypeObject PyContext_Type
-
- The type object representing the *context* type.
-
-.. c:var:: PyTypeObject PyContextVar_Type
-
- The type object representing the *context variable* type.
-
-.. c:var:: PyTypeObject PyContextToken_Type
-
- The type object representing the *context variable token* type.
-
-
-Type-check macros:
-
-.. c:function:: int PyContext_CheckExact(PyObject *o)
-
- Return true if *o* is of type :c:data:`PyContext_Type`. *o* must not be
- ``NULL``. This function always succeeds.
-
-.. c:function:: int PyContextVar_CheckExact(PyObject *o)
-
- Return true if *o* is of type :c:data:`PyContextVar_Type`. *o* must not be
- ``NULL``. This function always succeeds.
-
-.. c:function:: int PyContextToken_CheckExact(PyObject *o)
-
- Return true if *o* is of type :c:data:`PyContextToken_Type`.
- *o* must not be ``NULL``. This function always succeeds.
-
-
-Context object management functions:
-
-.. c:function:: PyObject *PyContext_New(void)
-
- Create a new empty context object. Returns ``NULL`` if an error
- has occurred.
-
-.. c:function:: PyObject *PyContext_Copy(PyObject *ctx)
-
- Create a shallow copy of the passed *ctx* context object.
- Returns ``NULL`` if an error has occurred.
-
-.. c:function:: PyObject *PyContext_CopyCurrent(void)
-
- Create a shallow copy of the current thread context.
- Returns ``NULL`` if an error has occurred.
-
-.. c:function:: int PyContext_Enter(PyObject *ctx)
-
- Set *ctx* as the current context for the current thread.
- Returns ``0`` on success, and ``-1`` on error.
-
-.. c:function:: int PyContext_Exit(PyObject *ctx)
-
- Deactivate the *ctx* context and restore the previous context as the
- current context for the current thread. Returns ``0`` on success,
- and ``-1`` on error.
-
-.. c:function:: int PyContext_ClearFreeList()
-
- Clear the context variable free list. Return the total number of
- freed items. This function always succeeds.
-
-
-Context variable functions:
-
-.. c:function:: PyObject *PyContextVar_New(const char *name, PyObject *def)
-
- Create a new ``ContextVar`` object. The *name* parameter is used
- for introspection and debug purposes. The *def* parameter may optionally
- specify the default value for the context variable. If an error has
- occurred, this function returns ``NULL``.
-
-.. c:function:: int PyContextVar_Get(PyObject *var, PyObject *default_value, PyObject **value)
-
- Get the value of a context variable. Returns ``-1`` if an error has
- occurred during lookup, and ``0`` if no error occurred, whether or not
- a value was found.
-
- If the context variable was found, *value* will be a pointer to it.
- If the context variable was *not* found, *value* will point to:
-
- - *default_value*, if not ``NULL``;
- - the default value of *var*, if not ``NULL``;
- - ``NULL``
-
- If the value was found, the function will create a new reference to it.
-
-.. c:function:: PyObject *PyContextVar_Set(PyObject *var, PyObject *value)
-
- Set the value of *var* to *value* in the current context. Returns a
- pointer to a :c:type:`PyObject` object, or ``NULL`` if an error
- has occurred.
-
-.. c:function:: int PyContextVar_Reset(PyObject *var, PyObject *token)
-
- Reset the state of the *var* context variable to that it was in before
- :c:func:`PyContextVar_Set` that returned the *token* was called.
- This function returns ``0`` on success and ``-1`` on error.
diff --git a/Doc/c-api/conversion.rst b/Doc/c-api/conversion.rst
index b310fcb..4b4d156 100644
--- a/Doc/c-api/conversion.rst
+++ b/Doc/c-api/conversion.rst
@@ -1,4 +1,4 @@
-.. highlight:: c
+.. highlightlang:: c
.. _string-conversion:
@@ -26,13 +26,13 @@ guarantee consistent behavior in corner cases, which the Standard C functions do
not.
The wrappers ensure that *str*[*size*-1] is always ``'\0'`` upon return. They
-never write more than *size* bytes (including the trailing ``'\0'``) into str.
+never write more than *size* bytes (including the trailing ``'\0'`` into str.
Both functions require that ``str != NULL``, ``size > 0`` and ``format !=
NULL``.
If the platform doesn't have :c:func:`vsnprintf` and the buffer size needed to
avoid truncation exceeds *size* by more than 512 bytes, Python aborts with a
-:c:func:`Py_FatalError`.
+*Py_FatalError*.
The return value (*rv*) for these functions should be interpreted as follows:
@@ -60,7 +60,7 @@ The following functions provide locale-independent string to number conversions.
The conversion is independent of the current locale.
If ``endptr`` is ``NULL``, convert the whole string. Raise
- :exc:`ValueError` and return ``-1.0`` if the string is not a valid
+ 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
@@ -82,7 +82,41 @@ The following functions provide locale-independent string to number conversions.
out-of-memory error), set the appropriate Python exception and
return ``-1.0``.
- .. versionadded:: 3.1
+ .. versionadded:: 2.7
+
+
+.. c:function:: double PyOS_ascii_strtod(const char *nptr, char **endptr)
+
+ Convert a string to a :c:type:`double`. This function behaves like the Standard C
+ function :c:func:`strtod` does in the C locale. It does this without changing the
+ current locale, since that would not be thread-safe.
+
+ :c:func:`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 :manpage:`strtod(2)` for details.
+
+ .. versionadded:: 2.4
+
+ .. deprecated:: 2.7
+ Use :c:func:`PyOS_string_to_double` instead.
+
+
+
+.. c:function:: char* PyOS_ascii_formatd(char *buffer, size_t buf_len, const char *format, double d)
+
+ Convert a :c:type:`double` to a string using the ``'.'`` as the decimal
+ separator. *format* is a :c:func:`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.
+
+ .. versionadded:: 2.4
+ .. deprecated:: 2.7
+ This function is removed in Python 2.7 and 3.1. Use :func:`PyOS_double_to_string`
+ instead.
.. c:function:: char* PyOS_double_to_string(double val, char format_code, int precision, int flags, int *ptype)
@@ -92,40 +126,56 @@ The following functions provide locale-independent string to number conversions.
*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
+ must be ``0`` and is ignored. The ``'r'`` format code specifies the
standard :func:`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:
+ *flags* can be zero or more of the values *Py_DTSF_SIGN*,
+ *Py_DTSF_ADD_DOT_0*, or *Py_DTSF_ALT*, or-ed together:
- * ``Py_DTSF_SIGN`` means to always precede the returned string with a sign
+ * *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
+ * *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
+ * *Py_DTSF_ALT* means to apply "alternate" formatting rules. See the
documentation for the :c:func:`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
+ 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
+ *NULL* if the conversion failed. The caller is responsible for freeing the
returned string by calling :c:func:`PyMem_Free`.
- .. versionadded:: 3.1
+ .. versionadded:: 2.7
+
+
+.. c:function:: double PyOS_ascii_atof(const char *nptr)
+
+ Convert a string to a :c:type:`double` in a locale-independent way.
+ See the Unix man page :manpage:`atof(2)` for details.
-.. c:function:: int PyOS_stricmp(const char *s1, const char *s2)
+ .. versionadded:: 2.4
+
+ .. deprecated:: 3.1
+ Use :c:func:`PyOS_string_to_double` instead.
+
+
+.. c:function:: char* PyOS_stricmp(char *s1, char *s2)
Case insensitive comparison of strings. The function works almost
identically to :c:func:`strcmp` except that it ignores the case.
+ .. versionadded:: 2.6
-.. c:function:: int PyOS_strnicmp(const char *s1, const char *s2, Py_ssize_t size)
+
+.. c:function:: char* PyOS_strnicmp(char *s1, char *s2, Py_ssize_t size)
Case insensitive comparison of strings. The function works almost
identically to :c:func:`strncmp` except that it ignores the case.
+
+ .. versionadded:: 2.6
diff --git a/Doc/c-api/coro.rst b/Doc/c-api/coro.rst
deleted file mode 100644
index 2260944..0000000
--- a/Doc/c-api/coro.rst
+++ /dev/null
@@ -1,34 +0,0 @@
-.. highlight:: c
-
-.. _coro-objects:
-
-Coroutine Objects
------------------
-
-.. versionadded:: 3.5
-
-Coroutine objects are what functions declared with an ``async`` keyword
-return.
-
-
-.. c:type:: PyCoroObject
-
- The C structure used for coroutine objects.
-
-
-.. c:var:: PyTypeObject PyCoro_Type
-
- The type object corresponding to coroutine objects.
-
-
-.. c:function:: int PyCoro_CheckExact(PyObject *ob)
-
- Return true if *ob*'s type is :c:type:`PyCoro_Type`; *ob* must not be ``NULL``.
-
-
-.. c:function:: PyObject* PyCoro_New(PyFrameObject *frame, PyObject *name, PyObject *qualname)
-
- Create and return a new coroutine object based on the *frame* object,
- with ``__name__`` and ``__qualname__`` set to *name* and *qualname*.
- A reference to *frame* is stolen by this function. The *frame* argument
- must not be ``NULL``.
diff --git a/Doc/c-api/datetime.rst b/Doc/c-api/datetime.rst
index bd4f1ff..e2d832c 100644
--- a/Doc/c-api/datetime.rst
+++ b/Doc/c-api/datetime.rst
@@ -1,4 +1,4 @@
-.. highlight:: c
+.. highlightlang:: c
.. _datetimeobjects:
@@ -13,237 +13,227 @@ the module initialisation function. The macro puts a pointer to a C structure
into a static variable, :c:data:`PyDateTimeAPI`, that is used by the following
macros.
-Macro for access to the UTC singleton:
-
-.. c:var:: PyObject* PyDateTime_TimeZone_UTC
-
- Returns the time zone singleton representing UTC, the same object as
- :attr:`datetime.timezone.utc`.
-
- .. versionadded:: 3.7
-
-
Type-check macros:
+
.. c:function:: int PyDate_Check(PyObject *ob)
Return true if *ob* is of type :c:data:`PyDateTime_DateType` or a subtype of
- :c:data:`PyDateTime_DateType`. *ob* must not be ``NULL``.
+ :c:data:`PyDateTime_DateType`. *ob* must not be *NULL*.
+
+ .. versionadded:: 2.4
.. c:function:: int PyDate_CheckExact(PyObject *ob)
Return true if *ob* is of type :c:data:`PyDateTime_DateType`. *ob* must not be
- ``NULL``.
+ *NULL*.
+
+ .. versionadded:: 2.4
.. c:function:: int PyDateTime_Check(PyObject *ob)
Return true if *ob* is of type :c:data:`PyDateTime_DateTimeType` or a subtype of
- :c:data:`PyDateTime_DateTimeType`. *ob* must not be ``NULL``.
+ :c:data:`PyDateTime_DateTimeType`. *ob* must not be *NULL*.
+
+ .. versionadded:: 2.4
.. c:function:: int PyDateTime_CheckExact(PyObject *ob)
Return true if *ob* is of type :c:data:`PyDateTime_DateTimeType`. *ob* must not
- be ``NULL``.
+ be *NULL*.
+
+ .. versionadded:: 2.4
.. c:function:: int PyTime_Check(PyObject *ob)
Return true if *ob* is of type :c:data:`PyDateTime_TimeType` or a subtype of
- :c:data:`PyDateTime_TimeType`. *ob* must not be ``NULL``.
+ :c:data:`PyDateTime_TimeType`. *ob* must not be *NULL*.
+
+ .. versionadded:: 2.4
.. c:function:: int PyTime_CheckExact(PyObject *ob)
Return true if *ob* is of type :c:data:`PyDateTime_TimeType`. *ob* must not be
- ``NULL``.
+ *NULL*.
+
+ .. versionadded:: 2.4
.. c:function:: int PyDelta_Check(PyObject *ob)
Return true if *ob* is of type :c:data:`PyDateTime_DeltaType` or a subtype of
- :c:data:`PyDateTime_DeltaType`. *ob* must not be ``NULL``.
+ :c:data:`PyDateTime_DeltaType`. *ob* must not be *NULL*.
+
+ .. versionadded:: 2.4
.. c:function:: int PyDelta_CheckExact(PyObject *ob)
Return true if *ob* is of type :c:data:`PyDateTime_DeltaType`. *ob* must not be
- ``NULL``.
+ *NULL*.
+
+ .. versionadded:: 2.4
.. c:function:: int PyTZInfo_Check(PyObject *ob)
Return true if *ob* is of type :c:data:`PyDateTime_TZInfoType` or a subtype of
- :c:data:`PyDateTime_TZInfoType`. *ob* must not be ``NULL``.
+ :c:data:`PyDateTime_TZInfoType`. *ob* must not be *NULL*.
+
+ .. versionadded:: 2.4
.. c:function:: int PyTZInfo_CheckExact(PyObject *ob)
Return true if *ob* is of type :c:data:`PyDateTime_TZInfoType`. *ob* must not be
- ``NULL``.
+ *NULL*.
+ .. versionadded:: 2.4
Macros to create objects:
+
.. c:function:: PyObject* PyDate_FromDate(int year, int month, int day)
- Return a :class:`datetime.date` object with the specified year, month and day.
+ Return a ``datetime.date`` object with the specified year, month and day.
+
+ .. versionadded:: 2.4
.. c:function:: PyObject* PyDateTime_FromDateAndTime(int year, int month, int day, int hour, int minute, int second, int usecond)
- Return a :class:`datetime.datetime` object with the specified year, month, day, hour,
+ Return a ``datetime.datetime`` object with the specified year, month, day, hour,
minute, second and microsecond.
-
-.. c:function:: PyObject* PyDateTime_FromDateAndTimeAndFold(int year, int month, int day, int hour, int minute, int second, int usecond, int fold)
-
- Return a :class:`datetime.datetime` object with the specified year, month, day, hour,
- minute, second, microsecond and fold.
-
- .. versionadded:: 3.6
+ .. versionadded:: 2.4
.. c:function:: PyObject* PyTime_FromTime(int hour, int minute, int second, int usecond)
- Return a :class:`datetime.time` object with the specified hour, minute, second and
+ Return a ``datetime.time`` object with the specified hour, minute, second and
microsecond.
-
-.. c:function:: PyObject* PyTime_FromTimeAndFold(int hour, int minute, int second, int usecond, int fold)
-
- Return a :class:`datetime.time` object with the specified hour, minute, second,
- microsecond and fold.
-
- .. versionadded:: 3.6
+ .. versionadded:: 2.4
.. c:function:: PyObject* PyDelta_FromDSU(int days, int seconds, int useconds)
- Return a :class:`datetime.timedelta` object representing the given number
- of days, seconds and microseconds. Normalization is performed so that the
- resulting number of microseconds and seconds lie in the ranges documented for
- :class:`datetime.timedelta` objects.
-
-.. c:function:: PyObject* PyTimeZone_FromOffset(PyDateTime_DeltaType* offset)
-
- Return a :class:`datetime.timezone` object with an unnamed fixed offset
- represented by the *offset* argument.
-
- .. versionadded:: 3.7
-
-.. c:function:: PyObject* PyTimeZone_FromOffsetAndName(PyDateTime_DeltaType* offset, PyUnicode* name)
-
- Return a :class:`datetime.timezone` object with a fixed offset represented
- by the *offset* argument and with tzname *name*.
-
- .. versionadded:: 3.7
+ Return a ``datetime.timedelta`` object representing the given number 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.
+ .. versionadded:: 2.4
Macros to extract fields from date objects. The argument must be an instance of
:c:data:`PyDateTime_Date`, including subclasses (such as
-:c:data:`PyDateTime_DateTime`). The argument must not be ``NULL``, and the type is
+:c:data:`PyDateTime_DateTime`). The argument must not be *NULL*, and the type is
not checked:
+
.. c:function:: int PyDateTime_GET_YEAR(PyDateTime_Date *o)
Return the year, as a positive int.
+ .. versionadded:: 2.4
+
.. c:function:: int PyDateTime_GET_MONTH(PyDateTime_Date *o)
Return the month, as an int from 1 through 12.
+ .. versionadded:: 2.4
+
.. c:function:: int PyDateTime_GET_DAY(PyDateTime_Date *o)
Return the day, as an int from 1 through 31.
+ .. versionadded:: 2.4
Macros to extract fields from datetime objects. The argument must be an
instance of :c:data:`PyDateTime_DateTime`, including subclasses. The argument
-must not be ``NULL``, and the type is not checked:
+must not be *NULL*, and the type is not checked:
+
.. c:function:: int PyDateTime_DATE_GET_HOUR(PyDateTime_DateTime *o)
Return the hour, as an int from 0 through 23.
+ .. versionadded:: 2.4
+
.. c:function:: int PyDateTime_DATE_GET_MINUTE(PyDateTime_DateTime *o)
Return the minute, as an int from 0 through 59.
+ .. versionadded:: 2.4
+
.. c:function:: int PyDateTime_DATE_GET_SECOND(PyDateTime_DateTime *o)
Return the second, as an int from 0 through 59.
+ .. versionadded:: 2.4
+
.. c:function:: int PyDateTime_DATE_GET_MICROSECOND(PyDateTime_DateTime *o)
Return the microsecond, as an int from 0 through 999999.
+ .. versionadded:: 2.4
Macros to extract fields from time objects. The argument must be an instance of
-:c:data:`PyDateTime_Time`, including subclasses. The argument must not be ``NULL``,
+:c:data:`PyDateTime_Time`, including subclasses. The argument must not be *NULL*,
and the type is not checked:
+
.. c:function:: int PyDateTime_TIME_GET_HOUR(PyDateTime_Time *o)
Return the hour, as an int from 0 through 23.
+ .. versionadded:: 2.4
+
.. c:function:: int PyDateTime_TIME_GET_MINUTE(PyDateTime_Time *o)
Return the minute, as an int from 0 through 59.
+ .. versionadded:: 2.4
+
.. c:function:: int PyDateTime_TIME_GET_SECOND(PyDateTime_Time *o)
Return the second, as an int from 0 through 59.
+ .. versionadded:: 2.4
+
.. c:function:: int PyDateTime_TIME_GET_MICROSECOND(PyDateTime_Time *o)
Return the microsecond, as an int from 0 through 999999.
-
-Macros to extract fields from time delta objects. The argument must be an
-instance of :c:data:`PyDateTime_Delta`, including subclasses. The argument must
-not be ``NULL``, and the type is not checked:
-
-.. c:function:: int PyDateTime_DELTA_GET_DAYS(PyDateTime_Delta *o)
-
- Return the number of days, as an int from -999999999 to 999999999.
-
- .. versionadded:: 3.3
-
-
-.. c:function:: int PyDateTime_DELTA_GET_SECONDS(PyDateTime_Delta *o)
-
- Return the number of seconds, as an int from 0 through 86399.
-
- .. versionadded:: 3.3
-
-
-.. c:function:: int PyDateTime_DELTA_GET_MICROSECONDS(PyDateTime_Delta *o)
-
- Return the number of microseconds, as an int from 0 through 999999.
-
- .. versionadded:: 3.3
-
+ .. versionadded:: 2.4
Macros for the convenience of modules implementing the DB API:
+
.. c:function:: PyObject* PyDateTime_FromTimestamp(PyObject *args)
- Create and return a new :class:`datetime.datetime` object given an argument
- tuple suitable for passing to :meth:`datetime.datetime.fromtimestamp()`.
+ Create and return a new ``datetime.datetime`` object given an argument tuple
+ suitable for passing to ``datetime.datetime.fromtimestamp()``.
+
+ .. versionadded:: 2.4
.. c:function:: PyObject* PyDate_FromTimestamp(PyObject *args)
- Create and return a new :class:`datetime.date` object given an argument
- tuple suitable for passing to :meth:`datetime.date.fromtimestamp()`.
+ Create and return a new ``datetime.date`` object given an argument tuple
+ suitable for passing to ``datetime.date.fromtimestamp()``.
+
+ .. versionadded:: 2.4
diff --git a/Doc/c-api/descriptor.rst b/Doc/c-api/descriptor.rst
index 1005140..43baeaf 100644
--- a/Doc/c-api/descriptor.rst
+++ b/Doc/c-api/descriptor.rst
@@ -1,4 +1,4 @@
-.. highlight:: c
+.. highlightlang:: c
.. _descriptor-objects:
@@ -8,27 +8,38 @@ Descriptor Objects
"Descriptors" are objects that describe some attribute of an object. They are
found in the dictionary of type objects.
-.. XXX document these!
.. c:var:: PyTypeObject PyProperty_Type
The type object for the built-in descriptor types.
+ .. versionadded:: 2.2
+
.. c:function:: PyObject* PyDescr_NewGetSet(PyTypeObject *type, struct PyGetSetDef *getset)
+ .. versionadded:: 2.2
+
.. c:function:: PyObject* PyDescr_NewMember(PyTypeObject *type, struct PyMemberDef *meth)
+ .. versionadded:: 2.2
+
.. c:function:: PyObject* PyDescr_NewMethod(PyTypeObject *type, struct PyMethodDef *meth)
+ .. versionadded:: 2.2
+
.. c:function:: PyObject* PyDescr_NewWrapper(PyTypeObject *type, struct wrapperbase *wrapper, void *wrapped)
+ .. versionadded:: 2.2
+
.. c:function:: PyObject* PyDescr_NewClassMethod(PyTypeObject *type, PyMethodDef *method)
+ .. versionadded:: 2.3
+
.. c:function:: int PyDescr_IsData(PyObject *descr)
@@ -36,5 +47,9 @@ found in the dictionary of type objects.
false if it describes a method. *descr* must be a descriptor object; there is
no error checking.
+ .. versionadded:: 2.2
+
.. c:function:: PyObject* PyWrapper_New(PyObject *, PyObject *)
+
+ .. versionadded:: 2.2
diff --git a/Doc/c-api/dict.rst b/Doc/c-api/dict.rst
index e7922dc..3006b6c 100644
--- a/Doc/c-api/dict.rst
+++ b/Doc/c-api/dict.rst
@@ -1,4 +1,4 @@
-.. highlight:: c
+.. highlightlang:: c
.. _dictobjects:
@@ -15,8 +15,13 @@ Dictionary Objects
.. c:var:: PyTypeObject PyDict_Type
+ .. index::
+ single: DictType (in module types)
+ single: DictionaryType (in module types)
+
This instance of :c:type:`PyTypeObject` represents the Python dictionary
- type. This is the same object as :class:`dict` in the Python layer.
+ type. This is exposed to Python programs as ``dict`` and
+ ``types.DictType``.
.. c:function:: int PyDict_Check(PyObject *p)
@@ -24,23 +29,30 @@ Dictionary Objects
Return true if *p* is a dict object or an instance of a subtype of the dict
type.
+ .. versionchanged:: 2.2
+ Allowed subtypes to be accepted.
+
.. c:function:: int PyDict_CheckExact(PyObject *p)
Return true if *p* is a dict object, but not an instance of a subtype of
the dict type.
+ .. versionadded:: 2.4
+
.. c:function:: PyObject* PyDict_New()
- Return a new empty dictionary, or ``NULL`` on failure.
+ Return a new empty dictionary, or *NULL* on failure.
-.. c:function:: PyObject* PyDictProxy_New(PyObject *mapping)
+.. c:function:: PyObject* PyDictProxy_New(PyObject *dict)
- Return a :class:`types.MappingProxyType` object for a mapping which
- enforces read-only behavior. This is normally used to create a view to
- prevent modification of the dictionary for non-dynamic class types.
+ Return a proxy object for a mapping which enforces read-only behavior.
+ This is normally used to create a proxy to prevent modification of the
+ dictionary for non-dynamic class types.
+
+ .. versionadded:: 2.2
.. c:function:: void PyDict_Clear(PyObject *p)
@@ -54,11 +66,15 @@ Dictionary Objects
*key*, return ``1``, otherwise return ``0``. On error, return ``-1``.
This is equivalent to the Python expression ``key in p``.
+ .. versionadded:: 2.4
+
.. c:function:: PyObject* PyDict_Copy(PyObject *p)
Return a new dictionary that contains the same key-value pairs as *p*.
+ .. versionadded:: 1.6
+
.. c:function:: int PyDict_SetItem(PyObject *p, PyObject *key, PyObject *val)
@@ -69,11 +85,11 @@ Dictionary Objects
.. c:function:: int PyDict_SetItemString(PyObject *p, const char *key, PyObject *val)
- .. index:: single: PyUnicode_FromString()
+ .. index:: single: PyString_FromString()
Insert *value* into the dictionary *p* using *key* as a key. *key* should
- be a :c:type:`const char\*`. The key object is created using
- ``PyUnicode_FromString(key)``. Return ``0`` on success or ``-1`` on
+ be a :c:type:`char\*`. The key object is created using
+ ``PyString_FromString(key)``. Return ``0`` on success or ``-1`` on
failure.
@@ -84,7 +100,7 @@ Dictionary Objects
on failure.
-.. c:function:: int PyDict_DelItemString(PyObject *p, const char *key)
+.. c:function:: 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.
@@ -92,57 +108,32 @@ Dictionary Objects
.. c:function:: PyObject* PyDict_GetItem(PyObject *p, PyObject *key)
- Return the object from dictionary *p* which has a key *key*. Return ``NULL``
+ 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.
- Note that exceptions which occur while calling :meth:`__hash__` and
- :meth:`__eq__` methods will get suppressed.
- To get error reporting use :c:func:`PyDict_GetItemWithError()` instead.
-
-
-.. c:function:: PyObject* PyDict_GetItemWithError(PyObject *p, PyObject *key)
-
- Variant of :c:func:`PyDict_GetItem` that does not suppress
- exceptions. Return ``NULL`` **with** an exception set if an exception
- occurred. Return ``NULL`` **without** an exception set if the key
- wasn't present.
-
.. c:function:: PyObject* PyDict_GetItemString(PyObject *p, const char *key)
This is the same as :c:func:`PyDict_GetItem`, but *key* is specified as a
- :c:type:`const char\*`, rather than a :c:type:`PyObject\*`.
-
- Note that exceptions which occur while calling :meth:`__hash__` and
- :meth:`__eq__` methods and creating a temporary string object
- will get suppressed.
- To get error reporting use :c:func:`PyDict_GetItemWithError()` instead.
-
+ :c:type:`char\*`, rather than a :c:type:`PyObject\*`.
-.. c:function:: PyObject* PyDict_SetDefault(PyObject *p, PyObject *key, PyObject *defaultobj)
-
- This is the same as the Python-level :meth:`dict.setdefault`. If present, it
- returns the value corresponding to *key* from the dictionary *p*. If the key
- is not in the dict, it is inserted with value *defaultobj* and *defaultobj*
- is returned. This function evaluates the hash function of *key* only once,
- instead of evaluating it independently for the lookup and the insertion.
-
- .. versionadded:: 3.4
.. c:function:: PyObject* PyDict_Items(PyObject *p)
- Return a :c:type:`PyListObject` containing all the items from the dictionary.
+ Return a :c:type:`PyListObject` containing all the items from the
+ dictionary, as in the dictionary method :meth:`dict.items`.
.. c:function:: PyObject* PyDict_Keys(PyObject *p)
- Return a :c:type:`PyListObject` containing all the keys from the dictionary.
+ Return a :c:type:`PyListObject` containing all the keys from the dictionary,
+ as in the dictionary method :meth:`dict.keys`.
.. c:function:: PyObject* PyDict_Values(PyObject *p)
- Return a :c:type:`PyListObject` containing all the values from the dictionary
- *p*.
+ Return a :c:type:`PyListObject` containing all the values from the
+ dictionary *p*, as in the dictionary method :meth:`dict.values`.
.. c:function:: Py_ssize_t PyDict_Size(PyObject *p)
@@ -152,6 +143,10 @@ Dictionary Objects
Return the number of items in the dictionary. This is equivalent to
``len(p)`` on a dictionary.
+ .. versionchanged:: 2.5
+ This function returned an :c:type:`int` type. This might require changes
+ in your code for properly supporting 64-bit systems.
+
.. c:function:: int PyDict_Next(PyObject *p, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
@@ -161,7 +156,7 @@ Dictionary Objects
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 :c:type:`PyObject\*` variables that will be filled in with each key
- and value, respectively, or may be ``NULL``. Any references returned through
+ 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.
@@ -176,19 +171,17 @@ Dictionary Objects
...
}
- The dictionary *p* should not be mutated during iteration. It is safe 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::
+ 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::
PyObject *key, *value;
Py_ssize_t pos = 0;
while (PyDict_Next(self->dict, &pos, &key, &value)) {
- long i = PyLong_AsLong(value);
- if (i == -1 && PyErr_Occurred()) {
- return -1;
- }
- PyObject *o = PyLong_FromLong(i + 1);
+ int i = PyInt_AS_LONG(value) + 1;
+ PyObject *o = PyInt_FromLong(i);
if (o == NULL)
return -1;
if (PyDict_SetItem(self->dict, key, o) < 0) {
@@ -198,6 +191,10 @@ Dictionary Objects
Py_DECREF(o);
}
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int *` type for *ppos*. This might require
+ changes in your code for properly supporting 64-bit systems.
+
.. c:function:: int PyDict_Merge(PyObject *a, PyObject *b, int override)
@@ -208,6 +205,8 @@ Dictionary Objects
only be added if there is not a matching key in *a*. Return ``0`` on
success or ``-1`` if an exception was raised.
+ .. versionadded:: 2.2
+
.. c:function:: int PyDict_Update(PyObject *a, PyObject *b)
@@ -217,6 +216,8 @@ Dictionary Objects
argument has no "keys" attribute. Return ``0`` on success or ``-1`` if an
exception was raised.
+ .. versionadded:: 2.2
+
.. c:function:: int PyDict_MergeFromSeq2(PyObject *a, PyObject *seq2, int override)
@@ -232,9 +233,4 @@ Dictionary Objects
if override or key not in a:
a[key] = value
-
-.. c:function:: int PyDict_ClearFreeList()
-
- Clear the free list. Return the total number of freed items.
-
- .. versionadded:: 3.3
+ .. versionadded:: 2.2
diff --git a/Doc/c-api/exceptions.rst b/Doc/c-api/exceptions.rst
index 2edcbf7..281e4c8 100644
--- a/Doc/c-api/exceptions.rst
+++ b/Doc/c-api/exceptions.rst
@@ -1,4 +1,4 @@
-.. highlight:: c
+.. highlightlang:: c
.. _exceptionhandling:
@@ -9,19 +9,13 @@ 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 POSIX :c:data:`errno` variable:
+exception handling. It works somewhat like the Unix :c:data:`errno` variable:
there is a global indicator (per thread) of the last error that occurred. Most
-C API functions don't clear this on success, but will set it to indicate the
-cause of the error on failure. Most C API 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 :c:func:`PyArg_\*` functions
-return ``1`` for success and ``0`` for failure).
-
-Concretely, the error indicator consists of three object pointers: the
-exception's type, the exception's value, and the traceback object. Any
-of those pointers can be ``NULL`` if non-set (although some combinations are
-forbidden, for example you can't have a non-``NULL`` traceback if the exception
-type is ``NULL``).
+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 :c:func:`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
@@ -33,28 +27,25 @@ 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.
-.. note::
- The error indicator is **not** the result of :func:`sys.exc_info()`.
- The former corresponds to an exception that is not yet caught (and is
- therefore still propagating), while the latter returns an exception after
- it is caught (and has therefore stopped propagating).
-
-
-Printing and clearing
-=====================
-
+.. index::
+ single: exc_type (in module sys)
+ single: exc_value (in module sys)
+ single: exc_traceback (in module sys)
-.. c:function:: void PyErr_Clear()
+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.
- Clear the error indicator. If the error indicator is not set, there is no
- effect.
+.. XXX Order of these should be more thoughtful.
+ Either alphabetical or some kind of structure.
.. c:function:: void PyErr_PrintEx(int set_sys_last_vars)
Print a standard traceback to ``sys.stderr`` and clear the error indicator.
- **Unless** the error is a ``SystemExit``, in that case no traceback is
- printed and the Python process will exit with the error code specified by
+ **Unless** the error is a ``SystemExit``. In that case the no traceback
+ is printed and Python process will exit with the error code specified by
the ``SystemExit`` instance.
Call this function **only** when the error indicator is set. Otherwise it
@@ -70,29 +61,82 @@ Printing and clearing
Alias for ``PyErr_PrintEx(1)``.
-.. c:function:: void PyErr_WriteUnraisable(PyObject *obj)
+.. c:function:: PyObject* PyErr_Occurred()
- Call :func:`sys.unraisablehook` using the current exception and *obj*
- argument.
+ Test whether the error indicator is set. If set, return the exception *type*
+ (the first argument to the last call to one of the :c:func:`PyErr_Set\*`
+ functions or to :c:func:`PyErr_Restore`). If not set, return *NULL*. You do not
+ own a reference to the return value, so you do not need to :c:func:`Py_DECREF`
+ it.
- 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
- :meth:`__del__` method.
+ .. note::
- 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.
+ Do not compare the return value to a specific exception; use
+ :c:func:`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.)
+
+
+.. c:function:: int PyErr_ExceptionMatches(PyObject *exc)
- An exception must be set when calling this function.
+ 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.
-Raising exceptions
-==================
+.. c:function:: int PyErr_GivenExceptionMatches(PyObject *given, PyObject *exc)
-These functions help you set the current thread's error indicator.
-For convenience, some of these functions will always return a
-``NULL`` pointer for use in a ``return`` statement.
+ Return true if the *given* exception matches the exception in *exc*. If
+ *exc* is a class object, this also 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.
+
+
+.. c:function:: void PyErr_NormalizeException(PyObject**exc, PyObject**val, PyObject**tb)
+
+ Under certain circumstances, the values returned by :c:func:`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.
+
+
+.. c:function:: void PyErr_Clear()
+
+ Clear the error indicator. If the error indicator is not set, there is no
+ effect.
+
+
+.. c:function:: 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.
+
+
+.. c:function:: void PyErr_Restore(PyObject *type, PyObject *value, PyObject *traceback)
+
+ Set the error indicator from the three objects. If the error indicator is
+ already set, it is cleared first. If the objects are *NULL*, the error
+ indicator is cleared. Do not pass a *NULL* type and non-*NULL* value or
+ traceback. The exception type should be a class. Do not pass an invalid
+ exception type or value. (Violating these rules will cause subtle problems
+ later.) This call takes away a reference to each object: you must own a
+ reference to each object before the call and after the call you no longer own
+ these references. (If you don't understand this, don't use this function. I
+ warned you.)
+
+ .. note::
+
+ This function is normally only used by code that needs to save and restore the
+ error indicator temporarily; use :c:func:`PyErr_Fetch` to save the current
+ exception state.
.. c:function:: void PyErr_SetString(PyObject *type, const char *message)
@@ -100,7 +144,7 @@ For convenience, some of these functions will always return a
This is the most common way to set the error indicator. The first argument
specifies the exception type; it is normally one of the standard exceptions,
e.g. :c:data:`PyExc_RuntimeError`. You need not increment its reference count.
- The second argument is an error message; it is decoded from ``'utf-8``'.
+ The second argument is an error message; it is converted to a string object.
.. c:function:: void PyErr_SetObject(PyObject *type, PyObject *value)
@@ -111,19 +155,10 @@ For convenience, some of these functions will always return a
.. c:function:: PyObject* PyErr_Format(PyObject *exception, const char *format, ...)
- This function sets the error indicator and returns ``NULL``. *exception*
+ This function sets the error indicator and returns *NULL*. *exception*
should be a Python exception class. The *format* and subsequent
parameters help format the error message; they have the same meaning and
- values as in :c:func:`PyUnicode_FromFormat`. *format* is an ASCII-encoded
- string.
-
-
-.. c:function:: PyObject* PyErr_FormatV(PyObject *exception, const char *format, va_list vargs)
-
- Same as :c:func:`PyErr_Format`, but taking a :c:type:`va_list` argument rather
- than a variable number of arguments.
-
- .. versionadded:: 3.5
+ values as in :c:func:`PyString_FromFormat`.
.. c:function:: void PyErr_SetNone(PyObject *type)
@@ -140,7 +175,7 @@ For convenience, some of these functions will always return a
.. c:function:: PyObject* PyErr_NoMemory()
- This is a shorthand for ``PyErr_SetNone(PyExc_MemoryError)``; it returns ``NULL``
+ This is a shorthand for ``PyErr_SetNone(PyExc_MemoryError)``; it returns *NULL*
so an object allocation function can write ``return PyErr_NoMemory();`` when it
runs out of memory.
@@ -156,7 +191,7 @@ For convenience, some of these functions will always return a
and then calls ``PyErr_SetObject(type, object)``. On Unix, when the
:c:data:`errno` value is :const:`EINTR`, indicating an interrupted system call,
this calls :c:func:`PyErr_CheckSignals`, and if that set the error indicator,
- leaves it set to that. The function always returns ``NULL``, so a wrapper
+ 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.
@@ -164,26 +199,16 @@ For convenience, some of these functions will always return a
.. c:function:: PyObject* PyErr_SetFromErrnoWithFilenameObject(PyObject *type, PyObject *filenameObject)
Similar to :c:func:`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 :exc:`OSError` exception,
- this is used to define the :attr:`filename` attribute of the
+ *filenameObject* is not *NULL*, it is passed to the constructor of *type* as
+ a third parameter. In the case of exceptions such as :exc:`IOError` and
+ :exc:`OSError`, this is used to define the :attr:`filename` attribute of the
exception instance.
-.. c:function:: PyObject* PyErr_SetFromErrnoWithFilenameObjects(PyObject *type, PyObject *filenameObject, PyObject *filenameObject2)
-
- Similar to :c:func:`PyErr_SetFromErrnoWithFilenameObject`, but takes a second
- filename object, for raising errors when a function that takes two filenames
- fails.
-
- .. versionadded:: 3.4
-
-
.. c:function:: PyObject* PyErr_SetFromErrnoWithFilename(PyObject *type, const char *filename)
Similar to :c:func:`PyErr_SetFromErrnoWithFilenameObject`, but the filename
- is given as a C string. *filename* is decoded from the filesystem encoding
- (:func:`os.fsdecode`).
+ is given as a C string.
.. c:function:: PyObject* PyErr_SetFromWindowsErr(int ierr)
@@ -195,86 +220,45 @@ For convenience, some of these functions will always return a
then it constructs a tuple object whose first item is the *ierr* value and whose
second item is the corresponding error message (gotten from
:c:func:`FormatMessage`), and then calls ``PyErr_SetObject(PyExc_WindowsError,
- object)``. This function always returns ``NULL``.
-
- .. availability:: Windows.
+ object)``. This function always returns *NULL*. Availability: Windows.
.. c:function:: PyObject* PyErr_SetExcFromWindowsErr(PyObject *type, int ierr)
Similar to :c:func:`PyErr_SetFromWindowsErr`, with an additional parameter
- specifying the exception type to be raised.
+ specifying the exception type to be raised. Availability: Windows.
+
+ .. versionadded:: 2.3
+
- .. availability:: Windows.
+.. c:function:: PyObject* PyErr_SetFromWindowsErrWithFilenameObject(int ierr, PyObject *filenameObject)
+
+ Similar to :c:func:`PyErr_SetFromWindowsErr`, with the additional behavior that
+ if *filenameObject* is not *NULL*, it is passed to the constructor of
+ :exc:`WindowsError` as a third parameter. Availability: Windows.
.. c:function:: PyObject* PyErr_SetFromWindowsErrWithFilename(int ierr, const char *filename)
Similar to :c:func:`PyErr_SetFromWindowsErrWithFilenameObject`, but the
- filename is given as a C string. *filename* is decoded from the filesystem
- encoding (:func:`os.fsdecode`).
-
- .. availability:: Windows.
+ filename is given as a C string. Availability: Windows.
.. c:function:: PyObject* PyErr_SetExcFromWindowsErrWithFilenameObject(PyObject *type, int ierr, PyObject *filename)
Similar to :c:func:`PyErr_SetFromWindowsErrWithFilenameObject`, with an
additional parameter specifying the exception type to be raised.
+ Availability: Windows.
- .. availability:: Windows.
-
-
-.. c:function:: PyObject* PyErr_SetExcFromWindowsErrWithFilenameObjects(PyObject *type, int ierr, PyObject *filename, PyObject *filename2)
-
- Similar to :c:func:`PyErr_SetExcFromWindowsErrWithFilenameObject`,
- but accepts a second filename object.
-
- .. availability:: Windows.
-
- .. versionadded:: 3.4
+ .. versionadded:: 2.3
.. c:function:: PyObject* PyErr_SetExcFromWindowsErrWithFilename(PyObject *type, int ierr, const char *filename)
Similar to :c:func:`PyErr_SetFromWindowsErrWithFilename`, with an additional
- parameter specifying the exception type to be raised.
-
- .. availability:: Windows.
-
-
-.. c:function:: PyObject* PyErr_SetImportError(PyObject *msg, PyObject *name, PyObject *path)
-
- This is a convenience function to raise :exc:`ImportError`. *msg* will be
- set as the exception's message string. *name* and *path*, both of which can
- be ``NULL``, will be set as the :exc:`ImportError`'s respective ``name``
- and ``path`` attributes.
-
- .. versionadded:: 3.3
-
-
-.. c:function:: void PyErr_SyntaxLocationObject(PyObject *filename, int lineno, int col_offset)
-
- Set file, line, and offset information for the current exception. If the
- current exception is not a :exc:`SyntaxError`, then it sets additional
- attributes, which make the exception printing subsystem think the exception
- is a :exc:`SyntaxError`.
+ parameter specifying the exception type to be raised. Availability: Windows.
- .. versionadded:: 3.4
-
-
-.. c:function:: void PyErr_SyntaxLocationEx(const char *filename, int lineno, int col_offset)
-
- Like :c:func:`PyErr_SyntaxLocationObject`, but *filename* is a byte string
- decoded from the filesystem encoding (:func:`os.fsdecode`).
-
- .. versionadded:: 3.2
-
-
-.. c:function:: void PyErr_SyntaxLocation(const char *filename, int lineno)
-
- Like :c:func:`PyErr_SyntaxLocationEx`, but the col_offset parameter is
- omitted.
+ .. versionadded:: 2.3
.. c:function:: void PyErr_BadInternalCall()
@@ -285,31 +269,27 @@ For convenience, some of these functions will always return a
use.
-Issuing warnings
-================
-
-Use these functions to issue warnings from C code. They mirror similar
-functions exported by the Python :mod:`warnings` module. They normally
-print 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 they will raise an exception. It is also possible that
-the functions raise an exception because of a problem with the warning machinery.
-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, :c:func:`Py_DECREF` owned references and return
-an error value).
-
-.. c:function:: int PyErr_WarnEx(PyObject *category, const char *message, Py_ssize_t stack_level)
+.. c:function:: 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 *message* argument is a UTF-8 encoded string. *stack_level* is a
+ below) or *NULL*; the *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 *stack_level* of 1
+ the currently executing line of code in that stack frame. A *stacklevel* of 1
is the function calling :c:func:`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 :mod:`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, :c:func:`Py_DECREF` owned references and return
+ an error value).
+
Warning categories must be subclasses of :c:data:`PyExc_Warning`;
:c:data:`PyExc_Warning` is a subclass of :c:data:`PyExc_Exception`;
the default warning category is :c:data:`PyExc_RuntimeWarning`. The standard
@@ -320,182 +300,32 @@ an error value).
:mod:`warnings` module and the :option:`-W` option in the command line
documentation. There is no C API for warning control.
-.. c:function:: PyObject* PyErr_SetImportErrorSubclass(PyObject *exception, PyObject *msg, PyObject *name, PyObject *path)
- Much like :c:func:`PyErr_SetImportError` but this function allows for
- specifying a subclass of :exc:`ImportError` to raise.
+.. c:function:: int PyErr_Warn(PyObject *category, char *message)
- .. versionadded:: 3.6
+ 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 :c:func:`PyErr_Warn`, equivalent to
+ calling :c:func:`PyErr_WarnEx` with a *stacklevel* of 1.
+ Deprecated; use :c:func:`PyErr_WarnEx` instead.
-.. c:function:: int PyErr_WarnExplicitObject(PyObject *category, PyObject *message, PyObject *filename, int lineno, PyObject *module, PyObject *registry)
+
+.. c:function:: 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
:func:`warnings.warn_explicit`, see there for more information. The *module*
- and *registry* arguments may be set to ``NULL`` to get the default effect
+ and *registry* arguments may be set to *NULL* to get the default effect
described there.
- .. versionadded:: 3.4
-
-
-.. c:function:: int PyErr_WarnExplicit(PyObject *category, const char *message, const char *filename, int lineno, const char *module, PyObject *registry)
-
- Similar to :c:func:`PyErr_WarnExplicitObject` except that *message* and
- *module* are UTF-8 encoded strings, and *filename* is decoded from the
- filesystem encoding (:func:`os.fsdecode`).
-
-
-.. c:function:: int PyErr_WarnFormat(PyObject *category, Py_ssize_t stack_level, const char *format, ...)
-
- Function similar to :c:func:`PyErr_WarnEx`, but use
- :c:func:`PyUnicode_FromFormat` to format the warning message. *format* is
- an ASCII-encoded string.
-
- .. versionadded:: 3.2
-
-
-.. c:function:: int PyErr_ResourceWarning(PyObject *source, Py_ssize_t stack_level, const char *format, ...)
-
- Function similar to :c:func:`PyErr_WarnFormat`, but *category* is
- :exc:`ResourceWarning` and pass *source* to :func:`warnings.WarningMessage`.
-
- .. versionadded:: 3.6
-
-
-Querying the error indicator
-============================
-.. c:function:: PyObject* PyErr_Occurred()
-
- Test whether the error indicator is set. If set, return the exception *type*
- (the first argument to the last call to one of the :c:func:`PyErr_Set\*`
- functions or to :c:func:`PyErr_Restore`). If not set, return ``NULL``. You do not
- own a reference to the return value, so you do not need to :c:func:`Py_DECREF`
- it.
-
- The caller must hold the GIL.
-
- .. note::
+.. c:function:: int PyErr_WarnPy3k(char *message, int stacklevel)
- Do not compare the return value to a specific exception; use
- :c:func:`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.)
+ Issue a :exc:`DeprecationWarning` with the given *message* and *stacklevel*
+ if the :c:data:`Py_Py3kWarningFlag` flag is enabled.
-
-.. c:function:: 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.
-
-
-.. c:function:: int PyErr_GivenExceptionMatches(PyObject *given, PyObject *exc)
-
- Return true if the *given* exception matches the exception type in *exc*. If
- *exc* is a class object, this also returns true when *given* is an instance
- of a subclass. If *exc* is a tuple, all exception types in the tuple (and
- recursively in subtuples) are searched for a match.
-
-
-.. c:function:: 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 catch exceptions or
- by code that needs to save and restore the error indicator temporarily, e.g.::
-
- {
- PyObject *type, *value, *traceback;
- PyErr_Fetch(&type, &value, &traceback);
-
- /* ... code that might produce other errors ... */
-
- PyErr_Restore(type, value, traceback);
- }
-
-
-.. c:function:: void PyErr_Restore(PyObject *type, PyObject *value, PyObject *traceback)
-
- Set the error indicator from the three objects. If the error indicator is
- already set, it is cleared first. If the objects are ``NULL``, the error
- indicator is cleared. Do not pass a ``NULL`` type and non-``NULL`` value or
- traceback. The exception type should be a class. Do not pass an invalid
- exception type or value. (Violating these rules will cause subtle problems
- later.) This call takes away a reference to each object: you must own a
- reference to each object before the call and after the call you no longer own
- these references. (If you don't understand this, don't use this function. I
- warned you.)
-
- .. note::
-
- This function is normally only used by code that needs to save and restore the
- error indicator temporarily. Use :c:func:`PyErr_Fetch` to save the current
- error indicator.
-
-
-.. c:function:: void PyErr_NormalizeException(PyObject**exc, PyObject**val, PyObject**tb)
-
- Under certain circumstances, the values returned by :c:func:`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.
-
- .. note::
-
- This function *does not* implicitly set the ``__traceback__``
- attribute on the exception value. If setting the traceback
- appropriately is desired, the following additional snippet is needed::
-
- if (tb != NULL) {
- PyException_SetTraceback(val, tb);
- }
-
-
-.. c:function:: void PyErr_GetExcInfo(PyObject **ptype, PyObject **pvalue, PyObject **ptraceback)
-
- Retrieve the exception info, as known from ``sys.exc_info()``. This refers
- to an exception that was *already caught*, not to an exception that was
- freshly raised. Returns new references for the three objects, any of which
- may be ``NULL``. Does not modify the exception info state.
-
- .. note::
-
- This function is not normally used by code that wants to handle exceptions.
- Rather, it can be used when code needs to save and restore the exception
- state temporarily. Use :c:func:`PyErr_SetExcInfo` to restore or clear the
- exception state.
-
- .. versionadded:: 3.3
-
-
-.. c:function:: void PyErr_SetExcInfo(PyObject *type, PyObject *value, PyObject *traceback)
-
- Set the exception info, as known from ``sys.exc_info()``. This refers
- to an exception that was *already caught*, not to an exception that was
- freshly raised. This function steals the references of the arguments.
- To clear the exception state, pass ``NULL`` for all three arguments.
- For general rules about the three arguments, see :c:func:`PyErr_Restore`.
-
- .. note::
-
- This function is not normally used by code that wants to handle exceptions.
- Rather, it can be used when code needs to save and restore the exception
- state temporarily. Use :c:func:`PyErr_GetExcInfo` to read the exception
- state.
-
- .. versionadded:: 3.3
-
-
-Signal Handling
-===============
+ .. versionadded:: 2.6
.. c:function:: int PyErr_CheckSignals()
@@ -521,37 +351,31 @@ Signal Handling
single: SIGINT
single: KeyboardInterrupt (built-in exception)
- Simulate the effect of a :const:`SIGINT` signal arriving. The next time
- :c:func:`PyErr_CheckSignals` is called, the Python signal handler for
- :const:`SIGINT` will be called.
+ This function simulates the effect of a :const:`SIGINT` signal arriving --- the
+ next time :c:func:`PyErr_CheckSignals` is called, :exc:`KeyboardInterrupt` will
+ be raised. It may be called without holding the interpreter lock.
- If :const:`SIGINT` isn't handled by Python (it was set to
- :data:`signal.SIG_DFL` or :data:`signal.SIG_IGN`), this function does
- nothing.
+ .. % XXX This was described as obsolete, but is used in
+ .. % thread.interrupt_main() (used from IDLE), so it's still needed.
-.. c:function:: int PySignal_SetWakeupFd(int fd)
- This utility function specifies a file descriptor to which the signal number
- is written as a single byte whenever a signal is received. *fd* must be
- non-blocking. It returns the previous such file descriptor.
+.. c:function:: int PySignal_SetWakeupFd(int fd)
- The value ``-1`` disables the feature; this is the initial state.
+ 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 :func:`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.
- .. versionchanged:: 3.5
- On Windows, the function now also supports socket handles.
+ .. versionadded:: 2.6
-Exception Classes
-=================
-
-.. c:function:: PyObject* PyErr_NewException(const char *name, PyObject *base, PyObject *dict)
+.. c:function:: PyObject* PyErr_NewException(char *name, PyObject *base, PyObject *dict)
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``.
+ ``module.classname``. The *base* and *dict* arguments are normally *NULL*.
This creates a class object derived from :exc:`Exception` (accessible in C as
:c:data:`PyExc_Exception`).
@@ -562,60 +386,25 @@ Exception Classes
argument can be used to specify a dictionary of class variables and methods.
-.. c:function:: PyObject* PyErr_NewExceptionWithDoc(const char *name, const char *doc, PyObject *base, PyObject *dict)
+.. c:function:: PyObject* PyErr_NewExceptionWithDoc(char *name, char *doc, PyObject *base, PyObject *dict)
Same as :c:func:`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
+ easily be given a docstring: If *doc* is non-*NULL*, it will be used as the
docstring for the exception class.
- .. versionadded:: 3.2
-
-
-Exception Objects
-=================
-
-.. c:function:: PyObject* PyException_GetTraceback(PyObject *ex)
+ .. versionadded:: 2.7
- Return the traceback associated with the exception as a new reference, as
- accessible from Python through :attr:`__traceback__`. If there is no
- traceback associated, this returns ``NULL``.
+.. c:function:: void PyErr_WriteUnraisable(PyObject *obj)
-.. c:function:: int PyException_SetTraceback(PyObject *ex, PyObject *tb)
-
- Set the traceback associated with the exception to *tb*. Use ``Py_None`` to
- clear it.
-
-
-.. c:function:: PyObject* PyException_GetContext(PyObject *ex)
-
- Return the context (another exception instance during whose handling *ex* was
- raised) associated with the exception as a new reference, as accessible from
- Python through :attr:`__context__`. If there is no context associated, this
- returns ``NULL``.
-
-
-.. c:function:: void PyException_SetContext(PyObject *ex, PyObject *ctx)
-
- Set the context associated with the exception to *ctx*. Use ``NULL`` to clear
- it. There is no type check to make sure that *ctx* is an exception instance.
- This steals a reference to *ctx*.
-
-
-.. c:function:: PyObject* PyException_GetCause(PyObject *ex)
-
- Return the cause (either an exception instance, or :const:`None`,
- set by ``raise ... from ...``) associated with the exception as a new
- reference, as accessible from Python through :attr:`__cause__`.
-
-
-.. c:function:: void PyException_SetCause(PyObject *ex, PyObject *cause)
-
- Set the cause associated with the exception to *cause*. Use ``NULL`` to clear
- it. There is no type check to make sure that *cause* is either an exception
- instance or :const:`None`. This steals a reference to *cause*.
+ 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
+ :meth:`__del__` method.
- :attr:`__suppress_context__` is implicitly set to ``True`` by this function.
+ 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.
.. _unicodeexceptions:
@@ -628,77 +417,73 @@ The following functions are used to create and modify Unicode exceptions from C.
.. c:function:: PyObject* PyUnicodeDecodeError_Create(const char *encoding, const char *object, Py_ssize_t length, Py_ssize_t start, Py_ssize_t end, const char *reason)
Create a :class:`UnicodeDecodeError` object with the attributes *encoding*,
- *object*, *length*, *start*, *end* and *reason*. *encoding* and *reason* are
- UTF-8 encoded strings.
+ *object*, *length*, *start*, *end* and *reason*.
.. c:function:: PyObject* PyUnicodeEncodeError_Create(const char *encoding, const Py_UNICODE *object, Py_ssize_t length, Py_ssize_t start, Py_ssize_t end, const char *reason)
Create a :class:`UnicodeEncodeError` object with the attributes *encoding*,
- *object*, *length*, *start*, *end* and *reason*. *encoding* and *reason* are
- UTF-8 encoded strings.
+ *object*, *length*, *start*, *end* and *reason*.
.. c:function:: PyObject* PyUnicodeTranslateError_Create(const Py_UNICODE *object, Py_ssize_t length, Py_ssize_t start, Py_ssize_t end, const char *reason)
Create a :class:`UnicodeTranslateError` object with the attributes *object*,
- *length*, *start*, *end* and *reason*. *reason* is a UTF-8 encoded string.
+ *length*, *start*, *end* and *reason*.
.. c:function:: PyObject* PyUnicodeDecodeError_GetEncoding(PyObject *exc)
- PyObject* PyUnicodeEncodeError_GetEncoding(PyObject *exc)
+ PyObject* PyUnicodeEncodeError_GetEncoding(PyObject *exc)
Return the *encoding* attribute of the given exception object.
.. c:function:: PyObject* PyUnicodeDecodeError_GetObject(PyObject *exc)
- PyObject* PyUnicodeEncodeError_GetObject(PyObject *exc)
- PyObject* PyUnicodeTranslateError_GetObject(PyObject *exc)
+ PyObject* PyUnicodeEncodeError_GetObject(PyObject *exc)
+ PyObject* PyUnicodeTranslateError_GetObject(PyObject *exc)
Return the *object* attribute of the given exception object.
.. c:function:: int PyUnicodeDecodeError_GetStart(PyObject *exc, Py_ssize_t *start)
- int PyUnicodeEncodeError_GetStart(PyObject *exc, Py_ssize_t *start)
- int PyUnicodeTranslateError_GetStart(PyObject *exc, Py_ssize_t *start)
+ int PyUnicodeEncodeError_GetStart(PyObject *exc, Py_ssize_t *start)
+ int PyUnicodeTranslateError_GetStart(PyObject *exc, Py_ssize_t *start)
Get the *start* attribute of the given exception object and place it into
- *\*start*. *start* must not be ``NULL``. Return ``0`` on success, ``-1`` on
+ *\*start*. *start* must not be *NULL*. Return ``0`` on success, ``-1`` on
failure.
.. c:function:: int PyUnicodeDecodeError_SetStart(PyObject *exc, Py_ssize_t start)
- int PyUnicodeEncodeError_SetStart(PyObject *exc, Py_ssize_t start)
- int PyUnicodeTranslateError_SetStart(PyObject *exc, Py_ssize_t start)
+ int PyUnicodeEncodeError_SetStart(PyObject *exc, Py_ssize_t start)
+ int PyUnicodeTranslateError_SetStart(PyObject *exc, Py_ssize_t start)
Set the *start* attribute of the given exception object to *start*. Return
``0`` on success, ``-1`` on failure.
.. c:function:: int PyUnicodeDecodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
- int PyUnicodeEncodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
- int PyUnicodeTranslateError_GetEnd(PyObject *exc, Py_ssize_t *end)
+ int PyUnicodeEncodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
+ int PyUnicodeTranslateError_GetEnd(PyObject *exc, Py_ssize_t *end)
Get the *end* attribute of the given exception object and place it into
- *\*end*. *end* must not be ``NULL``. Return ``0`` on success, ``-1`` on
+ *\*end*. *end* must not be *NULL*. Return ``0`` on success, ``-1`` on
failure.
.. c:function:: int PyUnicodeDecodeError_SetEnd(PyObject *exc, Py_ssize_t end)
- int PyUnicodeEncodeError_SetEnd(PyObject *exc, Py_ssize_t end)
- int PyUnicodeTranslateError_SetEnd(PyObject *exc, Py_ssize_t end)
+ int PyUnicodeEncodeError_SetEnd(PyObject *exc, Py_ssize_t end)
+ int PyUnicodeTranslateError_SetEnd(PyObject *exc, Py_ssize_t end)
Set the *end* attribute of the given exception object to *end*. Return ``0``
on success, ``-1`` on failure.
.. c:function:: PyObject* PyUnicodeDecodeError_GetReason(PyObject *exc)
- PyObject* PyUnicodeEncodeError_GetReason(PyObject *exc)
- PyObject* PyUnicodeTranslateError_GetReason(PyObject *exc)
+ PyObject* PyUnicodeEncodeError_GetReason(PyObject *exc)
+ PyObject* PyUnicodeTranslateError_GetReason(PyObject *exc)
Return the *reason* attribute of the given exception object.
.. c:function:: int PyUnicodeDecodeError_SetReason(PyObject *exc, const char *reason)
- int PyUnicodeEncodeError_SetReason(PyObject *exc, const char *reason)
- int PyUnicodeTranslateError_SetReason(PyObject *exc, const char *reason)
+ int PyUnicodeEncodeError_SetReason(PyObject *exc, const char *reason)
+ int PyUnicodeTranslateError_SetReason(PyObject *exc, const char *reason)
Set the *reason* attribute of the given exception object to *reason*. Return
``0`` on success, ``-1`` on failure.
-.. _recursion:
-
Recursion Control
=================
@@ -706,8 +491,6 @@ These two functions provide a way to perform safe recursive calls at the C
level, both in the core and in extension modules. They are needed if the
recursive code does not necessarily invoke Python code (which tracks its
recursion depth automatically).
-They are also not needed for *tp_call* implementations
-because the :ref:`call protocol <call>` takes care of recursion handling.
.. c:function:: int Py_EnterRecursiveCall(const char *where)
@@ -718,53 +501,18 @@ because the :ref:`call protocol <call>` takes care of recursion handling.
sets a :exc:`MemoryError` and returns a nonzero value.
The function then checks if the recursion limit is reached. If this is the
- case, a :exc:`RecursionError` is set and a nonzero value is returned.
+ case, a :exc:`RuntimeError` is set and a nonzero value is returned.
Otherwise, zero is returned.
- *where* should be a UTF-8 encoded string such as ``" in instance check"`` to
- be concatenated to the :exc:`RecursionError` message caused by the recursion
- depth limit.
-
- .. versionchanged:: 3.9
- This function is now also available in the limited API.
+ *where* should be a string such as ``" in instance check"`` to be
+ concatenated to the :exc:`RuntimeError` message caused by the recursion depth
+ limit.
-.. c:function:: void Py_LeaveRecursiveCall(void)
+.. c:function:: void Py_LeaveRecursiveCall()
Ends a :c:func:`Py_EnterRecursiveCall`. Must be called once for each
*successful* invocation of :c:func:`Py_EnterRecursiveCall`.
- .. versionchanged:: 3.9
- This function is now also available in the limited API.
-
-Properly implementing :c:member:`~PyTypeObject.tp_repr` for container types requires
-special recursion handling. In addition to protecting the stack,
-:c:member:`~PyTypeObject.tp_repr` also needs to track objects to prevent cycles. The
-following two functions facilitate this functionality. Effectively,
-these are the C equivalent to :func:`reprlib.recursive_repr`.
-
-.. c:function:: int Py_ReprEnter(PyObject *object)
-
- Called at the beginning of the :c:member:`~PyTypeObject.tp_repr` implementation to
- detect cycles.
-
- If the object has already been processed, the function returns a
- positive integer. In that case the :c:member:`~PyTypeObject.tp_repr` implementation
- should return a string object indicating a cycle. As examples,
- :class:`dict` objects return ``{...}`` and :class:`list` objects
- return ``[...]``.
-
- The function will return a negative integer if the recursion limit
- is reached. In that case the :c:member:`~PyTypeObject.tp_repr` implementation should
- typically return ``NULL``.
-
- Otherwise, the function returns zero and the :c:member:`~PyTypeObject.tp_repr`
- implementation can continue normally.
-
-.. c:function:: void Py_ReprLeave(PyObject *object)
-
- Ends a :c:func:`Py_ReprEnter`. Must be called once for each
- invocation of :c:func:`Py_ReprEnter` that returns zero.
-
.. _standardexceptions:
@@ -779,93 +527,66 @@ the variables:
.. index::
single: PyExc_BaseException
single: PyExc_Exception
+ single: PyExc_StandardError
single: PyExc_ArithmeticError
single: PyExc_AssertionError
single: PyExc_AttributeError
- single: PyExc_BlockingIOError
- single: PyExc_BrokenPipeError
single: PyExc_BufferError
- single: PyExc_ChildProcessError
- single: PyExc_ConnectionAbortedError
- single: PyExc_ConnectionError
- single: PyExc_ConnectionRefusedError
- single: PyExc_ConnectionResetError
+ single: PyExc_EnvironmentError
single: PyExc_EOFError
- single: PyExc_FileExistsError
- single: PyExc_FileNotFoundError
single: PyExc_FloatingPointError
single: PyExc_GeneratorExit
single: PyExc_ImportError
single: PyExc_IndentationError
single: PyExc_IndexError
- single: PyExc_InterruptedError
- single: PyExc_IsADirectoryError
+ single: PyExc_IOError
single: PyExc_KeyError
single: PyExc_KeyboardInterrupt
single: PyExc_LookupError
single: PyExc_MemoryError
- single: PyExc_ModuleNotFoundError
single: PyExc_NameError
- single: PyExc_NotADirectoryError
single: PyExc_NotImplementedError
single: PyExc_OSError
single: PyExc_OverflowError
- single: PyExc_PermissionError
- single: PyExc_ProcessLookupError
- single: PyExc_RecursionError
single: PyExc_ReferenceError
single: PyExc_RuntimeError
- single: PyExc_StopAsyncIteration
single: PyExc_StopIteration
single: PyExc_SyntaxError
single: PyExc_SystemError
single: PyExc_SystemExit
single: PyExc_TabError
- single: PyExc_TimeoutError
single: PyExc_TypeError
single: PyExc_UnboundLocalError
single: PyExc_UnicodeDecodeError
single: PyExc_UnicodeEncodeError
single: PyExc_UnicodeError
single: PyExc_UnicodeTranslateError
+ single: PyExc_VMSError
single: PyExc_ValueError
+ single: PyExc_WindowsError
single: PyExc_ZeroDivisionError
+-----------------------------------------+---------------------------------+----------+
| C Name | Python Name | Notes |
+=========================================+=================================+==========+
-| :c:data:`PyExc_BaseException` | :exc:`BaseException` | \(1) |
+| :c:data:`PyExc_BaseException` | :exc:`BaseException` | (1), (4) |
+-----------------------------------------+---------------------------------+----------+
| :c:data:`PyExc_Exception` | :exc:`Exception` | \(1) |
+-----------------------------------------+---------------------------------+----------+
+| :c:data:`PyExc_StandardError` | :exc:`StandardError` | \(1) |
++-----------------------------------------+---------------------------------+----------+
| :c:data:`PyExc_ArithmeticError` | :exc:`ArithmeticError` | \(1) |
+-----------------------------------------+---------------------------------+----------+
| :c:data:`PyExc_AssertionError` | :exc:`AssertionError` | |
+-----------------------------------------+---------------------------------+----------+
| :c:data:`PyExc_AttributeError` | :exc:`AttributeError` | |
+-----------------------------------------+---------------------------------+----------+
-| :c:data:`PyExc_BlockingIOError` | :exc:`BlockingIOError` | |
-+-----------------------------------------+---------------------------------+----------+
-| :c:data:`PyExc_BrokenPipeError` | :exc:`BrokenPipeError` | |
-+-----------------------------------------+---------------------------------+----------+
| :c:data:`PyExc_BufferError` | :exc:`BufferError` | |
+-----------------------------------------+---------------------------------+----------+
-| :c:data:`PyExc_ChildProcessError` | :exc:`ChildProcessError` | |
-+-----------------------------------------+---------------------------------+----------+
-| :c:data:`PyExc_ConnectionAbortedError` | :exc:`ConnectionAbortedError` | |
-+-----------------------------------------+---------------------------------+----------+
-| :c:data:`PyExc_ConnectionError` | :exc:`ConnectionError` | |
-+-----------------------------------------+---------------------------------+----------+
-| :c:data:`PyExc_ConnectionRefusedError` | :exc:`ConnectionRefusedError` | |
-+-----------------------------------------+---------------------------------+----------+
-| :c:data:`PyExc_ConnectionResetError` | :exc:`ConnectionResetError` | |
+| :c:data:`PyExc_EnvironmentError` | :exc:`EnvironmentError` | \(1) |
+-----------------------------------------+---------------------------------+----------+
| :c:data:`PyExc_EOFError` | :exc:`EOFError` | |
+-----------------------------------------+---------------------------------+----------+
-| :c:data:`PyExc_FileExistsError` | :exc:`FileExistsError` | |
-+-----------------------------------------+---------------------------------+----------+
-| :c:data:`PyExc_FileNotFoundError` | :exc:`FileNotFoundError` | |
-+-----------------------------------------+---------------------------------+----------+
| :c:data:`PyExc_FloatingPointError` | :exc:`FloatingPointError` | |
+-----------------------------------------+---------------------------------+----------+
| :c:data:`PyExc_GeneratorExit` | :exc:`GeneratorExit` | |
@@ -876,9 +597,7 @@ the variables:
+-----------------------------------------+---------------------------------+----------+
| :c:data:`PyExc_IndexError` | :exc:`IndexError` | |
+-----------------------------------------+---------------------------------+----------+
-| :c:data:`PyExc_InterruptedError` | :exc:`InterruptedError` | |
-+-----------------------------------------+---------------------------------+----------+
-| :c:data:`PyExc_IsADirectoryError` | :exc:`IsADirectoryError` | |
+| :c:data:`PyExc_IOError` | :exc:`IOError` | |
+-----------------------------------------+---------------------------------+----------+
| :c:data:`PyExc_KeyError` | :exc:`KeyError` | |
+-----------------------------------------+---------------------------------+----------+
@@ -888,30 +607,18 @@ the variables:
+-----------------------------------------+---------------------------------+----------+
| :c:data:`PyExc_MemoryError` | :exc:`MemoryError` | |
+-----------------------------------------+---------------------------------+----------+
-| :c:data:`PyExc_ModuleNotFoundError` | :exc:`ModuleNotFoundError` | |
-+-----------------------------------------+---------------------------------+----------+
| :c:data:`PyExc_NameError` | :exc:`NameError` | |
+-----------------------------------------+---------------------------------+----------+
-| :c:data:`PyExc_NotADirectoryError` | :exc:`NotADirectoryError` | |
-+-----------------------------------------+---------------------------------+----------+
| :c:data:`PyExc_NotImplementedError` | :exc:`NotImplementedError` | |
+-----------------------------------------+---------------------------------+----------+
-| :c:data:`PyExc_OSError` | :exc:`OSError` | \(1) |
+| :c:data:`PyExc_OSError` | :exc:`OSError` | |
+-----------------------------------------+---------------------------------+----------+
| :c:data:`PyExc_OverflowError` | :exc:`OverflowError` | |
+-----------------------------------------+---------------------------------+----------+
-| :c:data:`PyExc_PermissionError` | :exc:`PermissionError` | |
-+-----------------------------------------+---------------------------------+----------+
-| :c:data:`PyExc_ProcessLookupError` | :exc:`ProcessLookupError` | |
-+-----------------------------------------+---------------------------------+----------+
-| :c:data:`PyExc_RecursionError` | :exc:`RecursionError` | |
-+-----------------------------------------+---------------------------------+----------+
| :c:data:`PyExc_ReferenceError` | :exc:`ReferenceError` | \(2) |
+-----------------------------------------+---------------------------------+----------+
| :c:data:`PyExc_RuntimeError` | :exc:`RuntimeError` | |
+-----------------------------------------+---------------------------------+----------+
-| :c:data:`PyExc_StopAsyncIteration` | :exc:`StopAsyncIteration` | |
-+-----------------------------------------+---------------------------------+----------+
| :c:data:`PyExc_StopIteration` | :exc:`StopIteration` | |
+-----------------------------------------+---------------------------------+----------+
| :c:data:`PyExc_SyntaxError` | :exc:`SyntaxError` | |
@@ -922,8 +629,6 @@ the variables:
+-----------------------------------------+---------------------------------+----------+
| :c:data:`PyExc_TabError` | :exc:`TabError` | |
+-----------------------------------------+---------------------------------+----------+
-| :c:data:`PyExc_TimeoutError` | :exc:`TimeoutError` | |
-+-----------------------------------------+---------------------------------+----------+
| :c:data:`PyExc_TypeError` | :exc:`TypeError` | |
+-----------------------------------------+---------------------------------+----------+
| :c:data:`PyExc_UnboundLocalError` | :exc:`UnboundLocalError` | |
@@ -936,47 +641,15 @@ the variables:
+-----------------------------------------+---------------------------------+----------+
| :c:data:`PyExc_UnicodeTranslateError` | :exc:`UnicodeTranslateError` | |
+-----------------------------------------+---------------------------------+----------+
+| :c:data:`PyExc_VMSError` | :exc:`VMSError` | \(5) |
++-----------------------------------------+---------------------------------+----------+
| :c:data:`PyExc_ValueError` | :exc:`ValueError` | |
+-----------------------------------------+---------------------------------+----------+
+| :c:data:`PyExc_WindowsError` | :exc:`WindowsError` | \(3) |
++-----------------------------------------+---------------------------------+----------+
| :c:data:`PyExc_ZeroDivisionError` | :exc:`ZeroDivisionError` | |
+-----------------------------------------+---------------------------------+----------+
-.. versionadded:: 3.3
- :c:data:`PyExc_BlockingIOError`, :c:data:`PyExc_BrokenPipeError`,
- :c:data:`PyExc_ChildProcessError`, :c:data:`PyExc_ConnectionError`,
- :c:data:`PyExc_ConnectionAbortedError`, :c:data:`PyExc_ConnectionRefusedError`,
- :c:data:`PyExc_ConnectionResetError`, :c:data:`PyExc_FileExistsError`,
- :c:data:`PyExc_FileNotFoundError`, :c:data:`PyExc_InterruptedError`,
- :c:data:`PyExc_IsADirectoryError`, :c:data:`PyExc_NotADirectoryError`,
- :c:data:`PyExc_PermissionError`, :c:data:`PyExc_ProcessLookupError`
- and :c:data:`PyExc_TimeoutError` were introduced following :pep:`3151`.
-
-.. versionadded:: 3.5
- :c:data:`PyExc_StopAsyncIteration` and :c:data:`PyExc_RecursionError`.
-
-.. versionadded:: 3.6
- :c:data:`PyExc_ModuleNotFoundError`.
-
-These are compatibility aliases to :c:data:`PyExc_OSError`:
-
-.. index::
- single: PyExc_EnvironmentError
- single: PyExc_IOError
- single: PyExc_WindowsError
-
-+-------------------------------------+----------+
-| C Name | Notes |
-+=====================================+==========+
-| :c:data:`PyExc_EnvironmentError` | |
-+-------------------------------------+----------+
-| :c:data:`PyExc_IOError` | |
-+-------------------------------------+----------+
-| :c:data:`PyExc_WindowsError` | \(3) |
-+-------------------------------------+----------+
-
-.. versionchanged:: 3.3
- These aliases used to be separate exception types.
-
Notes:
(1)
@@ -989,6 +662,13 @@ Notes:
Only defined on Windows; protect code that uses this by testing that the
preprocessor macro ``MS_WINDOWS`` is defined.
+(4)
+ .. versionadded:: 2.5
+
+(5)
+ Only defined on VMS; protect code that uses this by testing that the
+ preprocessor macro ``__VMS`` is defined.
+
.. _standardwarningcategories:
Standard Warning Categories
@@ -1006,7 +686,6 @@ the variables:
single: PyExc_FutureWarning
single: PyExc_ImportWarning
single: PyExc_PendingDeprecationWarning
- single: PyExc_ResourceWarning
single: PyExc_RuntimeWarning
single: PyExc_SyntaxWarning
single: PyExc_UnicodeWarning
@@ -1027,8 +706,6 @@ the variables:
+------------------------------------------+---------------------------------+----------+
| :c:data:`PyExc_PendingDeprecationWarning`| :exc:`PendingDeprecationWarning`| |
+------------------------------------------+---------------------------------+----------+
-| :c:data:`PyExc_ResourceWarning` | :exc:`ResourceWarning` | |
-+------------------------------------------+---------------------------------+----------+
| :c:data:`PyExc_RuntimeWarning` | :exc:`RuntimeWarning` | |
+------------------------------------------+---------------------------------+----------+
| :c:data:`PyExc_SyntaxWarning` | :exc:`SyntaxWarning` | |
@@ -1038,10 +715,14 @@ the variables:
| :c:data:`PyExc_UserWarning` | :exc:`UserWarning` | |
+------------------------------------------+---------------------------------+----------+
-.. versionadded:: 3.2
- :c:data:`PyExc_ResourceWarning`.
-
Notes:
(1)
This is a base class for other standard warning categories.
+
+String Exceptions
+=================
+
+.. versionchanged:: 2.6
+ All exceptions to be raised or caught must be derived from :exc:`BaseException`.
+ Trying to raise a string exception now raises :exc:`TypeError`.
diff --git a/Doc/c-api/file.rst b/Doc/c-api/file.rst
index 5370c4e..e8bc6bf 100644
--- a/Doc/c-api/file.rst
+++ b/Doc/c-api/file.rst
@@ -1,4 +1,4 @@
-.. highlight:: c
+.. highlightlang:: c
.. _fileobjects:
@@ -7,42 +7,103 @@ File Objects
.. index:: object: file
-These APIs are a minimal emulation of the Python 2 C API for built-in file
-objects, which used to rely on the buffered I/O (:c:type:`FILE\*`) support
-from the C standard library. In Python 3, files and streams use the new
-:mod:`io` module, which defines several layers over the low-level unbuffered
-I/O of the operating system. The functions described below are
-convenience C wrappers over these new APIs, and meant mostly for internal
-error reporting in the interpreter; third-party code is advised to access
-the :mod:`io` APIs instead.
+Python's built-in file objects are implemented entirely on the :c:type:`FILE\*`
+support from the C standard library. This is an implementation detail and may
+change in future releases of Python.
-.. c:function:: PyFile_FromFd(int fd, const char *name, const char *mode, int buffering, const char *encoding, const char *errors, const char *newline, int closefd)
+.. c:type:: PyFileObject
- Create a Python file object from the file descriptor of an already
- opened file *fd*. The arguments *name*, *encoding*, *errors* and *newline*
- can be ``NULL`` to use the defaults; *buffering* can be *-1* to use the
- default. *name* is ignored and kept for backward compatibility. Return
- ``NULL`` on failure. For a more comprehensive description of the arguments,
- please refer to the :func:`io.open` function documentation.
+ This subtype of :c:type:`PyObject` represents a Python file object.
- .. warning::
- Since Python streams have their own buffering layer, mixing them with
- OS-level file descriptors can produce various issues (such as unexpected
- ordering of data).
+.. c:var:: PyTypeObject PyFile_Type
- .. versionchanged:: 3.2
- Ignore *name* attribute.
+ .. index:: single: FileType (in module types)
+ This instance of :c:type:`PyTypeObject` represents the Python file type. This is
+ exposed to Python programs as ``file`` and ``types.FileType``.
-.. c:function:: int PyObject_AsFileDescriptor(PyObject *p)
- Return the file descriptor associated with *p* as an :c:type:`int`. If the
- object is an integer, its value is returned. If not, the
- object's :meth:`~io.IOBase.fileno` method is called if it exists; the
- method must return an integer, which is returned as the file descriptor
- value. Sets an exception and returns ``-1`` on failure.
+.. c:function:: int PyFile_Check(PyObject *p)
+
+ Return true if its argument is a :c:type:`PyFileObject` or a subtype of
+ :c:type:`PyFileObject`.
+
+ .. versionchanged:: 2.2
+ Allowed subtypes to be accepted.
+
+
+.. c:function:: int PyFile_CheckExact(PyObject *p)
+
+ Return true if its argument is a :c:type:`PyFileObject`, but not a subtype of
+ :c:type:`PyFileObject`.
+
+ .. versionadded:: 2.2
+
+
+.. c:function:: PyObject* PyFile_FromString(char *filename, char *mode)
+
+ .. index:: single: fopen()
+
+ On success, return a new file object that is opened on the file given by
+ *filename*, with a file mode given by *mode*, where *mode* has the same
+ semantics as the standard C routine :c:func:`fopen`. On failure, return *NULL*.
+
+
+.. c:function:: PyObject* PyFile_FromFile(FILE *fp, char *name, char *mode, int (*close)(FILE*))
+
+ Create a new :c:type:`PyFileObject` from the already-open standard C file
+ pointer, *fp*. The function *close* will be called when the file should be
+ closed. Return *NULL* and close the file using *close* on failure.
+ *close* is optional and can be set to *NULL*.
+
+
+.. c:function:: FILE* PyFile_AsFile(PyObject \*p)
+
+ Return the file object associated with *p* as a :c:type:`FILE\*`.
+
+ If the caller will ever use the returned :c:type:`FILE\*` object while
+ the :term:`GIL` is released it must also call the :c:func:`PyFile_IncUseCount` and
+ :c:func:`PyFile_DecUseCount` functions described below as appropriate.
+
+
+.. c:function:: void PyFile_IncUseCount(PyFileObject \*p)
+
+ Increments the PyFileObject's internal use count to indicate
+ that the underlying :c:type:`FILE\*` is being used.
+ This prevents Python from calling f_close() on it from another thread.
+ Callers of this must call :c:func:`PyFile_DecUseCount` when they are
+ finished with the :c:type:`FILE\*`. Otherwise the file object will
+ never be closed by Python.
+
+ The :term:`GIL` must be held while calling this function.
+
+ The suggested use is to call this after :c:func:`PyFile_AsFile` and before
+ you release the GIL::
+
+ FILE *fp = PyFile_AsFile(p);
+ PyFile_IncUseCount(p);
+ /* ... */
+ Py_BEGIN_ALLOW_THREADS
+ do_something(fp);
+ Py_END_ALLOW_THREADS
+ /* ... */
+ PyFile_DecUseCount(p);
+
+ .. versionadded:: 2.6
+
+
+.. c:function:: void PyFile_DecUseCount(PyFileObject \*p)
+
+ Decrements the PyFileObject's internal unlocked_count member to
+ indicate that the caller is done with its own use of the :c:type:`FILE\*`.
+ This may only be called to undo a prior call to :c:func:`PyFile_IncUseCount`.
+
+ The :term:`GIL` must be held while calling this function (see the example
+ above).
+
+ .. versionadded:: 2.6
.. c:function:: PyObject* PyFile_GetLine(PyObject *p, int n)
@@ -60,30 +121,47 @@ the :mod:`io` APIs instead.
raised if the end of the file is reached immediately.
-.. c:function:: int PyFile_SetOpenCodeHook(Py_OpenCodeHookFunction handler)
+.. c:function:: PyObject* PyFile_Name(PyObject *p)
+
+ Return the name of the file specified by *p* as a string object.
+
+
+.. c:function:: void PyFile_SetBufSize(PyFileObject *p, int n)
+
+ .. index:: single: setvbuf()
+
+ Available on systems with :c:func:`setvbuf` only. This should only be called
+ immediately after file object creation.
+
+
+.. c:function:: int PyFile_SetEncoding(PyFileObject *p, const char *enc)
+
+ Set the file's encoding for Unicode output to *enc*. Return ``1`` on success and ``0``
+ on failure.
+
+ .. versionadded:: 2.3
- Overrides the normal behavior of :func:`io.open_code` to pass its parameter
- through the provided handler.
- The handler is a function of type :c:type:`PyObject *(\*)(PyObject *path,
- void *userData)`, where *path* is guaranteed to be :c:type:`PyUnicodeObject`.
+.. c:function:: int PyFile_SetEncodingAndErrors(PyFileObject *p, const char *enc, *errors)
- The *userData* pointer is passed into the hook function. Since hook
- functions may be called from different runtimes, this pointer should not
- refer directly to Python state.
+ Set the file's encoding for Unicode output to *enc*, and its error
+ mode to *err*. Return ``1`` on success and ``0`` on failure.
- As this hook is intentionally used during import, avoid importing new modules
- during its execution unless they are known to be frozen or available in
- ``sys.modules``.
+ .. versionadded:: 2.6
- Once a hook has been set, it cannot be removed or replaced, and later calls to
- :c:func:`PyFile_SetOpenCodeHook` will fail. On failure, the function returns
- -1 and sets an exception if the interpreter has been initialized.
- This function is safe to call before :c:func:`Py_Initialize`.
+.. c:function:: int PyFile_SoftSpace(PyObject *p, int newflag)
- .. versionadded:: 3.8
+ .. index:: single: softspace (file attribute)
+ This function exists for internal use by the interpreter. Set the
+ :attr:`softspace` attribute of *p* to *newflag* and return the previous value.
+ *p* does not have to be a file object for this function to work properly; any
+ object is supported (thought its only interesting if the :attr:`softspace`
+ attribute can be set). This function clears any errors, and will return ``0``
+ as the previous value if the attribute either does not exist or if there were
+ errors in retrieving it. There is no way to detect errors from this function,
+ but doing so should not be needed.
.. c:function:: int PyFile_WriteObject(PyObject *obj, PyObject *p, int flags)
diff --git a/Doc/c-api/float.rst b/Doc/c-api/float.rst
index bfc28a7..3aa7d18 100644
--- a/Doc/c-api/float.rst
+++ b/Doc/c-api/float.rst
@@ -1,4 +1,4 @@
-.. highlight:: c
+.. highlightlang:: c
.. _floatobjects:
@@ -15,8 +15,10 @@ Floating Point Objects
.. c:var:: PyTypeObject PyFloat_Type
+ .. index:: single: FloatType (in modules types)
+
This instance of :c:type:`PyTypeObject` represents the Python floating point
- type. This is the same object as :class:`float` in the Python layer.
+ type. This is the same object as ``float`` and ``types.FloatType``.
.. c:function:: int PyFloat_Check(PyObject *p)
@@ -24,22 +26,28 @@ Floating Point Objects
Return true if its argument is a :c:type:`PyFloatObject` or a subtype of
:c:type:`PyFloatObject`.
+ .. versionchanged:: 2.2
+ Allowed subtypes to be accepted.
+
.. c:function:: int PyFloat_CheckExact(PyObject *p)
Return true if its argument is a :c:type:`PyFloatObject`, but not a subtype of
:c:type:`PyFloatObject`.
+ .. versionadded:: 2.2
+
-.. c:function:: PyObject* PyFloat_FromString(PyObject *str)
+.. c:function:: PyObject* PyFloat_FromString(PyObject *str, char **pend)
Create a :c:type:`PyFloatObject` object based on the string value in *str*, or
- ``NULL`` on failure.
+ *NULL* on failure. The *pend* argument is ignored. It remains only for
+ backward compatibility.
.. c:function:: PyObject* PyFloat_FromDouble(double v)
- Create a :c:type:`PyFloatObject` object from *v*, or ``NULL`` on failure.
+ Create a :c:type:`PyFloatObject` object from *v*, or *NULL* on failure.
.. c:function:: double PyFloat_AsDouble(PyObject *pyfloat)
@@ -47,13 +55,9 @@ Floating Point Objects
Return a C :c:type:`double` representation of the contents of *pyfloat*. If
*pyfloat* is not a Python floating point object but has a :meth:`__float__`
method, this method will first be called to convert *pyfloat* into a float.
- If ``__float__()`` is not defined then it falls back to :meth:`__index__`.
This method returns ``-1.0`` upon failure, so one should call
:c:func:`PyErr_Occurred` to check for errors.
- .. versionchanged:: 3.8
- Use :meth:`__index__` if available.
-
.. c:function:: double PyFloat_AS_DOUBLE(PyObject *pyfloat)
@@ -67,17 +71,50 @@ Floating Point Objects
precision, minimum and maximum values of a float. It's a thin wrapper
around the header file :file:`float.h`.
+ .. versionadded:: 2.6
+
.. c:function:: double PyFloat_GetMax()
Return the maximum representable finite float *DBL_MAX* as C :c:type:`double`.
+ .. versionadded:: 2.6
+
.. c:function:: double PyFloat_GetMin()
Return the minimum normalized positive float *DBL_MIN* as C :c:type:`double`.
+ .. versionadded:: 2.6
+
+
.. c:function:: int PyFloat_ClearFreeList()
Clear the float free list. Return the number of items that could not
be freed.
+
+ .. versionadded:: 2.6
+
+
+.. c:function:: void PyFloat_AsString(char *buf, PyFloatObject *v)
+
+ Convert the argument *v* to a string, using the same rules as
+ :func:`str`. The length of *buf* should be at least 100.
+
+ This function is unsafe to call because it writes to a buffer whose
+ length it does not know.
+
+ .. deprecated:: 2.7
+ Use :func:`PyObject_Str` or :func:`PyOS_double_to_string` instead.
+
+
+.. c:function:: void PyFloat_AsReprString(char *buf, PyFloatObject *v)
+
+ Same as PyFloat_AsString, except uses the same rules as
+ :func:`repr`. The length of *buf* should be at least 100.
+
+ This function is unsafe to call because it writes to a buffer whose
+ length it does not know.
+
+ .. deprecated:: 2.7
+ Use :func:`PyObject_Repr` or :func:`PyOS_double_to_string` instead.
diff --git a/Doc/c-api/function.rst b/Doc/c-api/function.rst
index bb416f4..66f8675 100644
--- a/Doc/c-api/function.rst
+++ b/Doc/c-api/function.rst
@@ -1,4 +1,4 @@
-.. highlight:: c
+.. highlightlang:: c
.. _function-objects:
@@ -26,7 +26,7 @@ There are a few functions specific to Python functions.
.. c:function:: int PyFunction_Check(PyObject *o)
Return true if *o* is a function object (has type :c:data:`PyFunction_Type`).
- The parameter must not be ``NULL``.
+ The parameter must not be *NULL*.
.. c:function:: PyObject* PyFunction_New(PyObject *code, PyObject *globals)
@@ -34,19 +34,8 @@ There are a few functions specific to Python functions.
Return a new function object associated with the code object *code*. *globals*
must be a dictionary with the global variables accessible to the function.
- The function's docstring and name are retrieved from the code object. *__module__*
- is retrieved from *globals*. The argument defaults, annotations and closure are
- set to ``NULL``. *__qualname__* is set to the same value as the function's name.
-
-
-.. c:function:: PyObject* PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname)
-
- As :c:func:`PyFunction_New`, but also allows setting the function object's
- ``__qualname__`` attribute. *qualname* should be a unicode object or ``NULL``;
- if ``NULL``, the ``__qualname__`` attribute is set to the same value as its
- ``__name__`` attribute.
-
- .. versionadded:: 3.3
+ The function's docstring, name and *__module__* are retrieved from the code
+ object, the argument defaults and closure are set to *NULL*.
.. c:function:: PyObject* PyFunction_GetCode(PyObject *op)
@@ -69,40 +58,26 @@ There are a few functions specific to Python functions.
.. c:function:: PyObject* PyFunction_GetDefaults(PyObject *op)
Return the argument default values of the function object *op*. This can be a
- tuple of arguments or ``NULL``.
+ tuple of arguments or *NULL*.
.. c:function:: 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.
+ *Py_None* or a tuple.
Raises :exc:`SystemError` and returns ``-1`` on failure.
.. c:function:: PyObject* PyFunction_GetClosure(PyObject *op)
- Return the closure associated with the function object *op*. This can be ``NULL``
+ Return the closure associated with the function object *op*. This can be *NULL*
or a tuple of cell objects.
.. c:function:: 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 :exc:`SystemError` and returns ``-1`` on failure.
-
-
-.. c:function:: PyObject *PyFunction_GetAnnotations(PyObject *op)
-
- Return the annotations of the function object *op*. This can be a
- mutable dictionary or ``NULL``.
-
-
-.. c:function:: int PyFunction_SetAnnotations(PyObject *op, PyObject *annotations)
-
- Set the annotations for the function object *op*. *annotations*
- must be a dictionary or ``Py_None``.
+ *Py_None* or a tuple of cell objects.
Raises :exc:`SystemError` and returns ``-1`` on failure.
diff --git a/Doc/c-api/gcsupport.rst b/Doc/c-api/gcsupport.rst
index 924a7fd..18ecd07 100644
--- a/Doc/c-api/gcsupport.rst
+++ b/Doc/c-api/gcsupport.rst
@@ -1,4 +1,4 @@
-.. highlight:: c
+.. highlightlang:: c
.. _supporting-cycle-detection:
@@ -12,6 +12,9 @@ other objects, or which only store references to atomic types (such as numbers
or strings), do not need to provide any explicit support for garbage
collection.
+.. An example showing the use of these interfaces can be found in "Supporting the
+.. Cycle Collector (XXX not found: ../ext/example-cycle-support.html)".
+
To create a container type, the :c:member:`~PyTypeObject.tp_flags` field of the type object must
include the :const:`Py_TPFLAGS_HAVE_GC` and provide an implementation of the
:c:member:`~PyTypeObject.tp_traverse` handler. If instances of the type are mutable, a
@@ -45,11 +48,19 @@ Constructors for container types must conform to two rules:
Analogous to :c:func:`PyObject_NewVar` but for container objects with the
:const:`Py_TPFLAGS_HAVE_GC` flag set.
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` type for *size*. This might require
+ changes in your code for properly supporting 64-bit systems.
+
.. c:function:: TYPE* PyObject_GC_Resize(TYPE, PyVarObject *op, Py_ssize_t newsize)
Resize an object allocated by :c:func:`PyObject_NewVar`. Returns the
- resized object or ``NULL`` on failure. *op* must not be tracked by the collector yet.
+ resized object or *NULL* on failure. *op* must not be tracked by the collector yet.
+
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` type for *newsize*. This might
+ require changes in your code for properly supporting 64-bit systems.
.. c:function:: void PyObject_GC_Track(PyObject *op)
@@ -61,6 +72,11 @@ Constructors for container types must conform to two rules:
end of the constructor.
+.. c:function:: void _PyObject_GC_TRACK(PyObject *op)
+
+ A macro version of :c:func:`PyObject_GC_Track`. It should not be used for
+ extension modules.
+
Similarly, the deallocator for the object must conform to a similar pair of
rules:
@@ -85,10 +101,10 @@ rules:
the fields used by the :c:member:`~PyTypeObject.tp_traverse` handler become invalid.
-.. versionchanged:: 3.8
+.. c:function:: void _PyObject_GC_UNTRACK(PyObject *op)
- The :c:func:`_PyObject_GC_TRACK` and :c:func:`_PyObject_GC_UNTRACK` macros
- have been removed from the public C API.
+ A macro version of :c:func:`PyObject_GC_UnTrack`. It should not be used for
+ extension modules.
The :c:member:`~PyTypeObject.tp_traverse` handler accepts a function parameter of this type:
@@ -110,7 +126,7 @@ The :c:member:`~PyTypeObject.tp_traverse` handler must have the following type:
Traversal function for a container object. Implementations must call the
*visit* function for each object directly contained by *self*, with the
parameters to *visit* being the contained object and the *arg* value passed
- to the handler. The *visit* function must not be called with a ``NULL``
+ to the handler. The *visit* function must not be called with a *NULL*
object argument. If *visit* returns a non-zero value that value should be
returned immediately.
@@ -121,7 +137,7 @@ must name its arguments exactly *visit* and *arg*:
.. c:function:: void Py_VISIT(PyObject *o)
- If *o* is not ``NULL``, call the *visit* callback, with arguments *o*
+ If *o* is not *NULL*, call the *visit* callback, with arguments *o*
and *arg*. If *visit* returns a non-zero value, then return it.
Using this macro, :c:member:`~PyTypeObject.tp_traverse` handlers
look like::
@@ -134,7 +150,9 @@ must name its arguments exactly *visit* and *arg*:
return 0;
}
-The :c:member:`~PyTypeObject.tp_clear` handler must be of the :c:type:`inquiry` type, or ``NULL``
+ .. versionadded:: 2.4
+
+The :c:member:`~PyTypeObject.tp_clear` handler must be of the :c:type:`inquiry` type, or *NULL*
if the object is immutable.
diff --git a/Doc/c-api/gen.rst b/Doc/c-api/gen.rst
index 7441092..0c851a7 100644
--- a/Doc/c-api/gen.rst
+++ b/Doc/c-api/gen.rst
@@ -1,4 +1,4 @@
-.. highlight:: c
+.. highlightlang:: c
.. _gen-objects:
@@ -7,7 +7,7 @@ Generator Objects
Generator objects are what Python uses to implement generator iterators. They
are normally created by iterating over a function that yields values, rather
-than explicitly calling :c:func:`PyGen_New` or :c:func:`PyGen_NewWithQualName`.
+than explicitly calling :c:func:`PyGen_New`.
.. c:type:: PyGenObject
@@ -20,25 +20,19 @@ than explicitly calling :c:func:`PyGen_New` or :c:func:`PyGen_NewWithQualName`.
The type object corresponding to generator objects.
-.. c:function:: int PyGen_Check(PyObject *ob)
+.. c:function:: int PyGen_Check(ob)
- Return true if *ob* is a generator object; *ob* must not be ``NULL``.
+ Return true if *ob* is a generator object; *ob* must not be *NULL*.
-.. c:function:: int PyGen_CheckExact(PyObject *ob)
+.. c:function:: int PyGen_CheckExact(ob)
- Return true if *ob*'s type is :c:type:`PyGen_Type`; *ob* must not be ``NULL``.
+ Return true if *ob*'s type is *PyGen_Type* is a generator object; *ob* must not
+ be *NULL*.
.. c:function:: PyObject* PyGen_New(PyFrameObject *frame)
- Create and return a new generator object based on the *frame* object.
- A reference to *frame* is stolen by this function. The argument must not be
- ``NULL``.
-
-.. c:function:: PyObject* PyGen_NewWithQualName(PyFrameObject *frame, PyObject *name, PyObject *qualname)
-
- Create and return a new generator object based on the *frame* object,
- with ``__name__`` and ``__qualname__`` set to *name* and *qualname*.
- A reference to *frame* is stolen by this function. The *frame* argument
- must not be ``NULL``.
+ 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*.
diff --git a/Doc/c-api/import.rst b/Doc/c-api/import.rst
index c6fc330..f0ea227 100644
--- a/Doc/c-api/import.rst
+++ b/Doc/c-api/import.rst
@@ -1,4 +1,4 @@
-.. highlight:: c
+.. highlightlang:: c
.. _importing:
@@ -14,7 +14,7 @@ Importing Modules
single: modules (in module sys)
This is a simplified interface to :c:func:`PyImport_ImportModuleEx` below,
- leaving the *globals* and *locals* arguments set to ``NULL`` and *level* set
+ leaving the *globals* and *locals* arguments set to *NULL* and *level* set
to 0. When the *name*
argument contains a dot (when it specifies a submodule of a package), the
*fromlist* argument is set to the list ``['*']`` so that the return value is the
@@ -22,85 +22,98 @@ Importing Modules
be the case. (Unfortunately, this has an additional side effect when *name* in
fact specifies a subpackage instead of a submodule: the submodules specified in
the package's ``__all__`` variable are loaded.) Return a new reference to the
- imported module, or ``NULL`` with an exception set on failure. A failing
- import of a module doesn't leave the module in :data:`sys.modules`.
+ imported module, or *NULL* with an exception set on failure. Before Python 2.4,
+ the module may still be created in the failure case --- examine ``sys.modules``
+ to find out. Starting with Python 2.4, a failing import of a module no longer
+ leaves the module in ``sys.modules``.
- This function always uses absolute imports.
+ .. versionchanged:: 2.4
+ Failing imports remove incomplete module objects.
+
+ .. versionchanged:: 2.6
+ Always uses absolute imports.
.. c:function:: PyObject* PyImport_ImportModuleNoBlock(const char *name)
- This function is a deprecated alias of :c:func:`PyImport_ImportModule`.
+ This version of :c:func:`PyImport_ImportModule` does not block. It's intended
+ to be used in C functions that import other modules to execute a function.
+ The import may block if another thread holds the import lock. The function
+ :c:func:`PyImport_ImportModuleNoBlock` never blocks. It first tries to fetch
+ the module from sys.modules and falls back to :c:func:`PyImport_ImportModule`
+ unless the lock is held, in which case the function will raise an
+ :exc:`ImportError`.
- .. versionchanged:: 3.3
- This function used to fail immediately when the import lock was held
- by another thread. In Python 3.3 though, the locking scheme switched
- to per-module locks for most purposes, so this function's special
- behaviour isn't needed anymore.
+ .. versionadded:: 2.6
-.. c:function:: PyObject* PyImport_ImportModuleEx(const char *name, PyObject *globals, PyObject *locals, PyObject *fromlist)
+.. c:function:: PyObject* PyImport_ImportModuleEx(char *name, PyObject *globals, PyObject *locals, PyObject *fromlist)
.. index:: builtin: __import__
Import a module. This is best described by referring to the built-in Python
- function :func:`__import__`.
+ function :func:`__import__`, as the standard :func:`__import__` function calls
+ this function directly.
+
+ The return value is a new reference to the imported module or top-level package,
+ or *NULL* with an exception set on failure (before Python 2.4, the module may
+ still be created in this case). Like for :func:`__import__`, the return value
+ when a submodule of a package was requested is normally the top-level package,
+ unless a non-empty *fromlist* was given.
- The return value is a new reference to the imported module or top-level
- package, or ``NULL`` with an exception set on failure. Like for
- :func:`__import__`, the return value when a submodule of a package was
- requested is normally the top-level package, unless a non-empty *fromlist*
- was given.
+ .. versionchanged:: 2.4
+ Failing imports remove incomplete module objects.
- Failing imports remove incomplete module objects, like with
- :c:func:`PyImport_ImportModule`.
+ .. versionchanged:: 2.6
+ The function is an alias for :c:func:`PyImport_ImportModuleLevel` with
+ ``-1`` as level, meaning relative import.
-.. c:function:: PyObject* PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals, PyObject *locals, PyObject *fromlist, int level)
+.. c:function:: PyObject* PyImport_ImportModuleLevel(char *name, PyObject *globals, PyObject *locals, PyObject *fromlist, int level)
Import a module. This is best described by referring to the built-in Python
function :func:`__import__`, as the standard :func:`__import__` function calls
this function directly.
The return value is a new reference to the imported module or top-level package,
- or ``NULL`` with an exception set on failure. Like for :func:`__import__`,
+ or *NULL* with an exception set on failure. Like for :func:`__import__`,
the return value when a submodule of a package was requested is normally the
top-level package, unless a non-empty *fromlist* was given.
- .. versionadded:: 3.3
+ .. versionadded:: 2.5
-.. c:function:: PyObject* PyImport_ImportModuleLevel(const char *name, PyObject *globals, PyObject *locals, PyObject *fromlist, int level)
-
- Similar to :c:func:`PyImport_ImportModuleLevelObject`, but the name is a
- UTF-8 encoded string instead of a Unicode object.
-
- .. versionchanged:: 3.3
- Negative values for *level* are no longer accepted.
-
.. c:function:: PyObject* PyImport_Import(PyObject *name)
- This is a higher-level interface that calls the current "import hook
- function" (with an explicit *level* of 0, meaning absolute import). It
- invokes the :func:`__import__` function from the ``__builtins__`` of the
- current globals. This means that the import is done using whatever import
- hooks are installed in the current environment.
+ .. index::
+ module: rexec
+ module: ihooks
+
+ This is a higher-level interface that calls the current "import hook function".
+ It invokes the :func:`__import__` function from the ``__builtins__`` of the
+ current globals. This means that the import is done using whatever import hooks
+ are installed in the current environment, e.g. by :mod:`rexec` or :mod:`ihooks`.
- This function always uses absolute imports.
+ .. versionchanged:: 2.6
+ Always uses absolute imports.
.. c:function:: PyObject* PyImport_ReloadModule(PyObject *m)
- Reload a module. Return a new reference to the reloaded module, or ``NULL`` with
- an exception set on failure (the module still exists in this case).
+ .. index:: builtin: reload
+ Reload a module. This is best described by referring to the built-in Python
+ function :func:`reload`, as the standard :func:`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).
-.. c:function:: PyObject* PyImport_AddModuleObject(PyObject *name)
+
+.. c:function:: PyObject* PyImport_AddModule(const char *name)
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.
+ dictionary. Return *NULL* with an exception set on failure.
.. note::
@@ -109,37 +122,24 @@ Importing Modules
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.
- .. versionadded:: 3.3
-
-
-.. c:function:: PyObject* PyImport_AddModule(const char *name)
-
- Similar to :c:func:`PyImport_AddModuleObject`, but the name is a UTF-8
- encoded string instead of a Unicode object.
-
-.. c:function:: PyObject* PyImport_ExecCodeModule(const char *name, PyObject *co)
+.. c:function:: PyObject* PyImport_ExecCodeModule(char *name, PyObject *co)
.. index:: builtin: compile
Given a module name (possibly of the form ``package.module``) and a code object
read from a Python bytecode file or obtained from the built-in function
:func:`compile`, load the module. Return a new reference to the module object,
- or ``NULL`` with an exception set if an error occurred. *name*
- is removed from :attr:`sys.modules` in error cases, even if *name* was already
+ or *NULL* with an exception set if an error occurred. Before Python 2.4, the
+ module could still be created in error cases. Starting with Python 2.4, *name*
+ is removed from :attr:`sys.modules` in error cases, and even if *name* was already
in :attr:`sys.modules` on entry to :c:func:`PyImport_ExecCodeModule`. Leaving
incompletely initialized modules in :attr:`sys.modules` is dangerous, as imports of
such modules have no way to know that the module object is an unknown (and
probably damaged with respect to the module author's intents) state.
- The module's :attr:`__spec__` and :attr:`__loader__` will be set, if
- not set already, with the appropriate values. The spec's loader will
- be set to the module's ``__loader__`` (if set) and to an instance of
- :class:`SourceFileLoader` otherwise.
-
The module's :attr:`__file__` attribute will be set to the code object's
- :c:member:`co_filename`. If applicable, :attr:`__cached__` will also
- be set.
+ :c:member:`co_filename`.
This function will reload the module if it was already imported. See
:c:func:`PyImport_ReloadModule` for the intended way to reload a module.
@@ -147,83 +147,68 @@ Importing Modules
If *name* points to a dotted name of the form ``package.module``, any package
structures not already created will still not be created.
- See also :c:func:`PyImport_ExecCodeModuleEx` and
- :c:func:`PyImport_ExecCodeModuleWithPathnames`.
+ .. versionchanged:: 2.4
+ *name* is removed from :attr:`sys.modules` in error cases.
-.. c:function:: PyObject* PyImport_ExecCodeModuleEx(const char *name, PyObject *co, const char *pathname)
+.. c:function:: PyObject* PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname)
Like :c:func:`PyImport_ExecCodeModule`, but the :attr:`__file__` attribute of
the module object is set to *pathname* if it is non-``NULL``.
- See also :c:func:`PyImport_ExecCodeModuleWithPathnames`.
+.. c:function:: long PyImport_GetMagicNumber()
+
+ Return the magic number for Python bytecode files (a.k.a. :file:`.pyc` and
+ :file:`.pyo` files). The magic number should be present in the first four bytes
+ of the bytecode file, in little-endian byte order.
-.. c:function:: PyObject* PyImport_ExecCodeModuleObject(PyObject *name, PyObject *co, PyObject *pathname, PyObject *cpathname)
- Like :c:func:`PyImport_ExecCodeModuleEx`, but the :attr:`__cached__`
- attribute of the module object is set to *cpathname* if it is
- non-``NULL``. Of the three functions, this is the preferred one to use.
+.. c:function:: PyObject* PyImport_GetModuleDict()
- .. versionadded:: 3.3
+ Return the dictionary used for the module administration (a.k.a.
+ ``sys.modules``). Note that this is a per-interpreter variable.
-.. c:function:: PyObject* PyImport_ExecCodeModuleWithPathnames(const char *name, PyObject *co, const char *pathname, const char *cpathname)
+.. c:function:: PyObject* PyImport_GetImporter(PyObject *path)
- Like :c:func:`PyImport_ExecCodeModuleObject`, but *name*, *pathname* and
- *cpathname* are UTF-8 encoded strings. Attempts are also made to figure out
- what the value for *pathname* should be from *cpathname* if the former is
- set to ``NULL``.
+ Return an importer object for a :data:`sys.path`/:attr:`pkg.__path__` item
+ *path*, possibly by fetching it from the :data:`sys.path_importer_cache`
+ dict. If it wasn't yet cached, traverse :data:`sys.path_hooks` until a hook
+ is found that can handle the path item. Return ``None`` if no hook could;
+ this tells our caller it should fall back to the built-in import mechanism.
+ Cache the result in :data:`sys.path_importer_cache`. Return a new reference
+ to the importer object.
- .. versionadded:: 3.2
- .. versionchanged:: 3.3
- Uses :func:`imp.source_from_cache()` in calculating the source path if
- only the bytecode path is provided.
+ .. versionadded:: 2.6
-.. c:function:: long PyImport_GetMagicNumber()
+.. c:function:: void _PyImport_Init()
- Return the magic number for Python bytecode files (a.k.a. :file:`.pyc` file).
- The magic number should be present in the first four bytes of the bytecode
- file, in little-endian byte order. Returns ``-1`` on error.
+ Initialize the import mechanism. For internal use only.
- .. versionchanged:: 3.3
- Return value of ``-1`` upon failure.
+.. c:function:: void PyImport_Cleanup()
-.. c:function:: const char * PyImport_GetMagicTag()
+ Empty the module table. For internal use only.
- Return the magic tag string for :pep:`3147` format Python bytecode file
- names. Keep in mind that the value at ``sys.implementation.cache_tag`` is
- authoritative and should be used instead of this function.
- .. versionadded:: 3.2
+.. c:function:: void _PyImport_Fini()
-.. c:function:: PyObject* PyImport_GetModuleDict()
+ Finalize the import mechanism. For internal use only.
- Return the dictionary used for the module administration (a.k.a.
- ``sys.modules``). Note that this is a per-interpreter variable.
-.. c:function:: PyObject* PyImport_GetModule(PyObject *name)
+.. c:function:: PyObject* _PyImport_FindExtension(char *, char *)
- Return the already imported module with the given name. If the
- module has not been imported yet then returns ``NULL`` but does not set
- an error. Returns ``NULL`` and sets an error if the lookup failed.
+ For internal use only.
- .. versionadded:: 3.7
-.. c:function:: PyObject* PyImport_GetImporter(PyObject *path)
+.. c:function:: PyObject* _PyImport_FixupExtension(char *, char *)
- Return a finder object for a :data:`sys.path`/:attr:`pkg.__path__` item
- *path*, possibly by fetching it from the :data:`sys.path_importer_cache`
- dict. If it wasn't yet cached, traverse :data:`sys.path_hooks` until a hook
- is found that can handle the path item. Return ``None`` if no hook could;
- this tells our caller that the :term:`path based finder` could not find a
- finder for this path item. Cache the result in :data:`sys.path_importer_cache`.
- Return a new reference to the finder object.
+ For internal use only.
-.. c:function:: int PyImport_ImportFrozenModuleObject(PyObject *name)
+.. c:function:: 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
@@ -231,17 +216,6 @@ Importing Modules
:c:func:`PyImport_ImportModule`. (Note the misnomer --- this function would
reload the module if it was already imported.)
- .. versionadded:: 3.3
-
- .. versionchanged:: 3.4
- The ``__file__`` attribute is no longer set on the module.
-
-
-.. c:function:: int PyImport_ImportFrozenModule(const char *name)
-
- Similar to :c:func:`PyImport_ImportFrozenModuleObject`, but the name is a
- UTF-8 encoded string instead of a Unicode object.
-
.. c:type:: struct _frozen
@@ -253,21 +227,21 @@ Importing Modules
is::
struct _frozen {
- const char *name;
- const unsigned char *code;
+ char *name;
+ unsigned char *code;
int size;
};
-.. c:var:: const struct _frozen* PyImport_FrozenModules
+.. c:var:: struct _frozen* PyImport_FrozenModules
This pointer is initialized to point to an array of :c:type:`struct _frozen`
- records, terminated by one whose members are all ``NULL`` or zero. When a frozen
+ records, terminated by one whose members are all *NULL* or zero. When a frozen
module is imported, it is searched in this table. Third-party code could play
tricks with this to provide a dynamically created collection of frozen modules.
-.. c:function:: int PyImport_AppendInittab(const char *name, PyObject* (*initfunc)(void))
+.. c:function:: int PyImport_AppendInittab(const char *name, void (*initfunc)(void))
Add a single module to the existing table of built-in modules. This is a
convenience wrapper around :c:func:`PyImport_ExtendInittab`, returning ``-1`` if
@@ -281,21 +255,21 @@ Importing Modules
Structure describing a single entry in the list of built-in modules. Each of
these structures gives the name and initialization function for a module built
- into the interpreter. The name is an ASCII encoded string. Programs which
- embed Python may use an array of these structures in conjunction with
- :c:func:`PyImport_ExtendInittab` to provide additional built-in modules.
- The structure is defined in :file:`Include/import.h` as::
+ into the interpreter. Programs which embed Python may use an array of these
+ structures in conjunction with :c:func:`PyImport_ExtendInittab` to provide
+ additional built-in modules. The structure is defined in
+ :file:`Include/import.h` as::
struct _inittab {
- const char *name; /* ASCII encoded string */
- PyObject* (*initfunc)(void);
+ char *name;
+ void (*initfunc)(void);
};
.. c:function:: int PyImport_ExtendInittab(struct _inittab *newtab)
Add a collection of modules to the table of built-in modules. The *newtab*
- array must end with a sentinel entry which contains ``NULL`` for the :attr:`name`
+ array must end with a sentinel entry which contains *NULL* for the :attr:`name`
field; failure to provide the sentinel value can result in a memory fault.
Returns ``0`` on success or ``-1`` if insufficient memory could be allocated to
extend the internal table. In the event of failure, no modules are added to the
diff --git a/Doc/c-api/index.rst b/Doc/c-api/index.rst
index 9a8f150..2ce7b98 100644
--- a/Doc/c-api/index.rst
+++ b/Doc/c-api/index.rst
@@ -13,7 +13,6 @@ document the API functions in detail.
:maxdepth: 2
intro.rst
- stable.rst
veryhigh.rst
refcounting.rst
exceptions.rst
@@ -21,7 +20,5 @@ document the API functions in detail.
abstract.rst
concrete.rst
init.rst
- init_config.rst
memory.rst
objimpl.rst
- apiabiversion.rst
diff --git a/Doc/c-api/init.rst b/Doc/c-api/init.rst
index 86bf7f9..229cae5 100644
--- a/Doc/c-api/init.rst
+++ b/Doc/c-api/init.rst
@@ -1,4 +1,4 @@
-.. highlight:: c
+.. highlightlang:: c
.. _initialization:
@@ -7,219 +7,6 @@
Initialization, Finalization, and Threads
*****************************************
-See also :ref:`Python Initialization Configuration <init-config>`.
-
-.. _pre-init-safe:
-
-Before Python Initialization
-============================
-
-In an application embedding Python, the :c:func:`Py_Initialize` function must
-be called before using any other Python/C API functions; with the exception of
-a few functions and the :ref:`global configuration variables
-<global-conf-vars>`.
-
-The following functions can be safely called before Python is initialized:
-
-* Configuration functions:
-
- * :c:func:`PyImport_AppendInittab`
- * :c:func:`PyImport_ExtendInittab`
- * :c:func:`PyInitFrozenExtensions`
- * :c:func:`PyMem_SetAllocator`
- * :c:func:`PyMem_SetupDebugHooks`
- * :c:func:`PyObject_SetArenaAllocator`
- * :c:func:`Py_SetPath`
- * :c:func:`Py_SetProgramName`
- * :c:func:`Py_SetPythonHome`
- * :c:func:`Py_SetStandardStreamEncoding`
- * :c:func:`PySys_AddWarnOption`
- * :c:func:`PySys_AddXOption`
- * :c:func:`PySys_ResetWarnOptions`
-
-* Informative functions:
-
- * :c:func:`Py_IsInitialized`
- * :c:func:`PyMem_GetAllocator`
- * :c:func:`PyObject_GetArenaAllocator`
- * :c:func:`Py_GetBuildInfo`
- * :c:func:`Py_GetCompiler`
- * :c:func:`Py_GetCopyright`
- * :c:func:`Py_GetPlatform`
- * :c:func:`Py_GetVersion`
-
-* Utilities:
-
- * :c:func:`Py_DecodeLocale`
-
-* Memory allocators:
-
- * :c:func:`PyMem_RawMalloc`
- * :c:func:`PyMem_RawRealloc`
- * :c:func:`PyMem_RawCalloc`
- * :c:func:`PyMem_RawFree`
-
-.. note::
-
- The following functions **should not be called** before
- :c:func:`Py_Initialize`: :c:func:`Py_EncodeLocale`, :c:func:`Py_GetPath`,
- :c:func:`Py_GetPrefix`, :c:func:`Py_GetExecPrefix`,
- :c:func:`Py_GetProgramFullPath`, :c:func:`Py_GetPythonHome`,
- :c:func:`Py_GetProgramName` and :c:func:`PyEval_InitThreads`.
-
-
-.. _global-conf-vars:
-
-Global configuration variables
-==============================
-
-Python has variables for the global configuration to control different features
-and options. By default, these flags are controlled by :ref:`command line
-options <using-on-interface-options>`.
-
-When a flag is set by an option, the value of the flag is the number of times
-that the option was set. For example, ``-b`` sets :c:data:`Py_BytesWarningFlag`
-to 1 and ``-bb`` sets :c:data:`Py_BytesWarningFlag` to 2.
-
-.. c:var:: Py_BytesWarningFlag
-
- Issue a warning when comparing :class:`bytes` or :class:`bytearray` with
- :class:`str` or :class:`bytes` with :class:`int`. Issue an error if greater
- or equal to ``2``.
-
- Set by the :option:`-b` option.
-
-.. c:var:: Py_DebugFlag
-
- Turn on parser debugging output (for expert only, depending on compilation
- options).
-
- Set by the :option:`-d` option and the :envvar:`PYTHONDEBUG` environment
- variable.
-
-.. c:var:: Py_DontWriteBytecodeFlag
-
- If set to non-zero, Python won't try to write ``.pyc`` files on the
- import of source modules.
-
- Set by the :option:`-B` option and the :envvar:`PYTHONDONTWRITEBYTECODE`
- environment variable.
-
-.. c:var:: Py_FrozenFlag
-
- Suppress error messages when calculating the module search path in
- :c:func:`Py_GetPath`.
-
- Private flag used by ``_freeze_importlib`` and ``frozenmain`` programs.
-
-.. c:var:: Py_HashRandomizationFlag
-
- Set to ``1`` if the :envvar:`PYTHONHASHSEED` environment variable is set to
- a non-empty string.
-
- If the flag is non-zero, read the :envvar:`PYTHONHASHSEED` environment
- variable to initialize the secret hash seed.
-
-.. c:var:: Py_IgnoreEnvironmentFlag
-
- Ignore all :envvar:`PYTHON*` environment variables, e.g.
- :envvar:`PYTHONPATH` and :envvar:`PYTHONHOME`, that might be set.
-
- Set by the :option:`-E` and :option:`-I` options.
-
-.. c:var:: Py_InspectFlag
-
- When a script is passed as first argument or the :option:`-c` option is used,
- enter interactive mode after executing the script or the command, even when
- :data:`sys.stdin` does not appear to be a terminal.
-
- Set by the :option:`-i` option and the :envvar:`PYTHONINSPECT` environment
- variable.
-
-.. c:var:: Py_InteractiveFlag
-
- Set by the :option:`-i` option.
-
-.. c:var:: Py_IsolatedFlag
-
- Run Python in isolated mode. In isolated mode :data:`sys.path` contains
- neither the script's directory nor the user's site-packages directory.
-
- Set by the :option:`-I` option.
-
- .. versionadded:: 3.4
-
-.. c:var:: Py_LegacyWindowsFSEncodingFlag
-
- If the flag is non-zero, use the ``mbcs`` encoding instead of the UTF-8
- encoding for the filesystem encoding.
-
- Set to ``1`` if the :envvar:`PYTHONLEGACYWINDOWSFSENCODING` environment
- variable is set to a non-empty string.
-
- See :pep:`529` for more details.
-
- .. availability:: Windows.
-
-.. c:var:: Py_LegacyWindowsStdioFlag
-
- If the flag is non-zero, use :class:`io.FileIO` instead of
- :class:`WindowsConsoleIO` for :mod:`sys` standard streams.
-
- Set to ``1`` if the :envvar:`PYTHONLEGACYWINDOWSSTDIO` environment
- variable is set to a non-empty string.
-
- See :pep:`528` for more details.
-
- .. availability:: Windows.
-
-.. c:var:: Py_NoSiteFlag
-
- Disable the import of the module :mod:`site` and the site-dependent
- manipulations of :data:`sys.path` that it entails. Also disable these
- manipulations if :mod:`site` is explicitly imported later (call
- :func:`site.main` if you want them to be triggered).
-
- Set by the :option:`-S` option.
-
-.. c:var:: Py_NoUserSiteDirectory
-
- Don't add the :data:`user site-packages directory <site.USER_SITE>` to
- :data:`sys.path`.
-
- Set by the :option:`-s` and :option:`-I` options, and the
- :envvar:`PYTHONNOUSERSITE` environment variable.
-
-.. c:var:: Py_OptimizeFlag
-
- Set by the :option:`-O` option and the :envvar:`PYTHONOPTIMIZE` environment
- variable.
-
-.. c:var:: Py_QuietFlag
-
- Don't display the copyright and version messages even in interactive mode.
-
- Set by the :option:`-q` option.
-
- .. versionadded:: 3.2
-
-.. c:var:: Py_UnbufferedStdioFlag
-
- Force the stdout and stderr streams to be unbuffered.
-
- Set by the :option:`-u` option and the :envvar:`PYTHONUNBUFFERED`
- environment variable.
-
-.. c:var:: Py_VerboseFlag
-
- Print a message each time a module is initialized, showing the place
- (filename or built-in module) from which it is loaded. If greater or equal
- to ``2``, print a message for each file that is checked for when
- searching for a module. Also provides information on module cleanup at exit.
-
- Set by the :option:`-v` option and the :envvar:`PYTHONVERBOSE` environment
- variable.
-
Initializing and finalizing the interpreter
===========================================
@@ -230,32 +17,29 @@ Initializing and finalizing the interpreter
.. index::
single: Py_SetProgramName()
single: PyEval_InitThreads()
+ single: PyEval_ReleaseLock()
+ single: PyEval_AcquireLock()
single: modules (in module sys)
single: path (in module sys)
- module: builtins
+ module: __builtin__
module: __main__
module: sys
triple: module; search; path
single: PySys_SetArgv()
single: PySys_SetArgvEx()
- single: Py_FinalizeEx()
+ single: Py_Finalize()
- Initialize the Python interpreter. In an application embedding Python,
- this should be called before using any other Python/C API functions; see
- :ref:`Before Python Initialization <pre-init-safe>` for the few exceptions.
-
- This initializes
+ Initialize the Python interpreter. In an application embedding Python, this
+ should be called before using any other Python/C API functions; with the
+ exception of :c:func:`Py_SetProgramName`, :c:func:`Py_SetPythonHome`, :c:func:`PyEval_InitThreads`,
+ :c:func:`PyEval_ReleaseLock`, and :c:func:`PyEval_AcquireLock`. This initializes
the table of loaded modules (``sys.modules``), and creates the fundamental
- modules :mod:`builtins`, :mod:`__main__` and :mod:`sys`. It also initializes
+ modules :mod:`__builtin__`, :mod:`__main__` and :mod:`sys`. It also initializes
the module search path (``sys.path``). It does not set ``sys.argv``; use
:c:func:`PySys_SetArgvEx` for that. This is a no-op when called for a second time
- (without calling :c:func:`Py_FinalizeEx` first). There is no return value; it is a
+ (without calling :c:func:`Py_Finalize` first). There is no return value; it is a
fatal error if the initialization fails.
- .. note::
- On Windows, changes the console mode from ``O_TEXT`` to ``O_BINARY``, which will
- also affect non-Python uses of the console using the C Runtime.
-
.. c:function:: void Py_InitializeEx(int initsigs)
@@ -263,24 +47,25 @@ Initializing and finalizing the interpreter
*initsigs* is ``0``, it skips initialization registration of signal handlers, which
might be useful when Python is embedded.
+ .. versionadded:: 2.4
+
.. c:function:: int Py_IsInitialized()
Return true (nonzero) when the Python interpreter has been initialized, false
- (zero) if not. After :c:func:`Py_FinalizeEx` is called, this returns false until
+ (zero) if not. After :c:func:`Py_Finalize` is called, this returns false until
:c:func:`Py_Initialize` is called again.
-.. c:function:: int Py_FinalizeEx()
+.. c:function:: void Py_Finalize()
Undo all initializations made by :c:func:`Py_Initialize` and subsequent use of
Python/C API functions, and destroy all sub-interpreters (see
:c:func:`Py_NewInterpreter` below) that were created and not yet destroyed since
the last call to :c:func:`Py_Initialize`. Ideally, this frees all memory
allocated by the Python interpreter. This is a no-op when called for a second
- time (without calling :c:func:`Py_Initialize` again first). Normally the
- return value is ``0``. If there were errors during finalization
- (flushing buffered data), ``-1`` is returned.
+ time (without calling :c:func:`Py_Initialize` again first). There is no return
+ value; errors during finalization are ignored.
This function is provided for a number of reasons. An embedding application
might want to restart Python without having to restart the application itself.
@@ -299,53 +84,14 @@ Initializing and finalizing the interpreter
freed. Some memory allocated by extension modules may not be freed. Some
extensions may not work properly if their initialization routine is called more
than once; this can happen if an application calls :c:func:`Py_Initialize` and
- :c:func:`Py_FinalizeEx` more than once.
-
- .. audit-event:: cpython._PySys_ClearAuditHooks "" c.Py_FinalizeEx
-
- .. versionadded:: 3.6
-
-.. c:function:: void Py_Finalize()
-
- This is a backwards-compatible version of :c:func:`Py_FinalizeEx` that
- disregards the return value.
+ :c:func:`Py_Finalize` more than once.
Process-wide parameters
=======================
-.. c:function:: int Py_SetStandardStreamEncoding(const char *encoding, const char *errors)
-
- .. index::
- single: Py_Initialize()
- single: main()
- triple: stdin; stdout; sdterr
-
- This function should be called before :c:func:`Py_Initialize`, if it is
- called at all. It specifies which encoding and error handling to use
- with standard IO, with the same meanings as in :func:`str.encode`.
-
- It overrides :envvar:`PYTHONIOENCODING` values, and allows embedding code
- to control IO encoding when the environment variable does not work.
-
- *encoding* and/or *errors* may be ``NULL`` to use
- :envvar:`PYTHONIOENCODING` and/or default values (depending on other
- settings).
-
- Note that :data:`sys.stderr` always uses the "backslashreplace" error
- handler, regardless of this (or any other) setting.
-
- If :c:func:`Py_FinalizeEx` is called, this function will need to be called
- again in order to affect subsequent calls to :c:func:`Py_Initialize`.
-
- Returns ``0`` if successful, a nonzero value on error (e.g. calling after the
- interpreter has already been initialized).
-
- .. versionadded:: 3.4
-
-
-.. c:function:: void Py_SetProgramName(const wchar_t *name)
+.. c:function:: void Py_SetProgramName(char *name)
.. index::
single: Py_Initialize()
@@ -354,20 +100,16 @@ Process-wide parameters
This function should be called before :c:func:`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 :c:func:`main` function of the program
- (converted to wide characters).
+ of the ``argv[0]`` argument to the :c:func:`main` function of the program.
This is used by :c:func:`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 wide character string in static storage whose contents will not
+ 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.
- Use :c:func:`Py_DecodeLocale` to decode a bytes string to get a
- :c:type:`wchar_*` string.
-
-.. c:function:: wchar* Py_GetProgramName()
+.. c:function:: char* Py_GetProgramName()
.. index:: single: Py_SetProgramName()
@@ -376,7 +118,7 @@ Process-wide parameters
value.
-.. c:function:: wchar_t* Py_GetPrefix()
+.. c:function:: char* Py_GetPrefix()
Return the *prefix* for installed platform-independent files. This is derived
through a number of complicated rules from the program name set with
@@ -389,7 +131,7 @@ Process-wide parameters
It is only useful on Unix. See also the next function.
-.. c:function:: wchar_t* Py_GetExecPrefix()
+.. c: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
@@ -424,7 +166,7 @@ Process-wide parameters
platform.
-.. c:function:: wchar_t* Py_GetProgramFullPath()
+.. c:function:: char* Py_GetProgramFullPath()
.. index::
single: Py_SetProgramName()
@@ -437,12 +179,11 @@ Process-wide parameters
to Python code as ``sys.executable``.
-.. c:function:: wchar_t* Py_GetPath()
+.. c:function:: char* Py_GetPath()
.. index::
triple: module; search; path
single: path (in module sys)
- single: Py_SetPath()
Return the default module search path; this is computed from the program name
(set by :c:func:`Py_SetProgramName` above) and some environment variables.
@@ -457,50 +198,19 @@ Process-wide parameters
.. XXX should give the exact rules
-.. c:function:: void Py_SetPath(const wchar_t *)
-
- .. index::
- triple: module; search; path
- single: path (in module sys)
- single: Py_GetPath()
-
- Set the default module search path. If this function is called before
- :c:func:`Py_Initialize`, then :c:func:`Py_GetPath` won't attempt to compute a
- default search path but uses the one provided instead. This is useful if
- Python is embedded by an application that has full knowledge of the location
- of all modules. The path components should be separated by the platform
- dependent delimiter character, which is ``':'`` on Unix and Mac OS X, ``';'``
- on Windows.
-
- This also causes :data:`sys.executable` to be set to the program
- full path (see :c:func:`Py_GetProgramFullPath`) and for :data:`sys.prefix` and
- :data:`sys.exec_prefix` to be empty. It is up to the caller to modify these
- if required after calling :c:func:`Py_Initialize`.
-
- Use :c:func:`Py_DecodeLocale` to decode a bytes string to get a
- :c:type:`wchar_*` string.
-
- The path argument is copied internally, so the caller may free it after the
- call completes.
-
- .. versionchanged:: 3.8
- The program full path is now used for :data:`sys.executable`, instead
- of the program name.
-
-
.. c:function:: const char* Py_GetVersion()
Return the version of this Python interpreter. This is a string that looks
something like ::
- "3.0a5+ (py3k:63103M, May 12 2008, 00:53:55) \n[GCC 4.2.3]"
+ "1.5 (#67, Dec 31 1997, 22:34:28) [GCC 2.7.2.2]"
.. index:: single: version (in module sys)
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 :data:`sys.version`.
+ modify its value. The value is available to Python code as ``sys.version``.
.. c:function:: const char* Py_GetPlatform()
@@ -556,7 +266,7 @@ Process-wide parameters
``sys.version``.
-.. c:function:: void PySys_SetArgvEx(int argc, wchar_t **argv, int updatepath)
+.. c:function:: void PySys_SetArgvEx(int argc, char **argv, int updatepath)
.. index::
single: main()
@@ -578,45 +288,35 @@ Process-wide parameters
- 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
:data:`sys.path`.
- - Otherwise (that is, if *argc* is ``0`` or ``argv[0]`` doesn't point
+ - Otherwise (that is, if *argc* is 0 or ``argv[0]`` doesn't point
to an existing file name), an empty string is prepended to
:data:`sys.path`, which is the same as prepending the current working
directory (``"."``).
- Use :c:func:`Py_DecodeLocale` to decode a bytes string to get a
- :c:type:`wchar_*` string.
-
.. note::
It is recommended that applications embedding the Python interpreter
for purposes other than executing a single script pass ``0`` as *updatepath*,
and update :data:`sys.path` themselves if desired.
See `CVE-2008-5983 <https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2008-5983>`_.
- On versions before 3.1.3, you can achieve the same effect by manually
+ On versions before 2.6.6, you can achieve the same effect by manually
popping the first :data:`sys.path` element after having called
:c:func:`PySys_SetArgv`, for example using::
PyRun_SimpleString("import sys; sys.path.pop(0)\n");
- .. versionadded:: 3.1.3
+ .. versionadded:: 2.6.6
.. XXX impl. doesn't seem consistent in allowing ``0``/``NULL`` for the params;
check w/ Guido.
-.. c:function:: void PySys_SetArgv(int argc, wchar_t **argv)
-
- This function works like :c:func:`PySys_SetArgvEx` with *updatepath* set
- to ``1`` unless the :program:`python` interpreter was started with the
- :option:`-I`.
+.. c:function:: void PySys_SetArgv(int argc, char **argv)
- Use :c:func:`Py_DecodeLocale` to decode a bytes string to get a
- :c:type:`wchar_*` string.
+ This function works like :c:func:`PySys_SetArgvEx` with *updatepath* set to ``1``.
- .. versionchanged:: 3.4 The *updatepath* value depends on :option:`-I`.
-
-.. c:function:: void Py_SetPythonHome(const wchar_t *home)
+.. c:function:: void Py_SetPythonHome(char *home)
Set the default "home" directory, that is, the location of the standard
Python libraries. See :envvar:`PYTHONHOME` for the meaning of the
@@ -627,11 +327,8 @@ Process-wide parameters
execution. No code in the Python interpreter will change the contents of
this storage.
- Use :c:func:`Py_DecodeLocale` to decode a bytes string to get a
- :c:type:`wchar_*` string.
-
-.. c:function:: w_char* Py_GetPythonHome()
+.. c:function:: char* Py_GetPythonHome()
Return the default "home", that is, the value set by a previous call to
:c:func:`Py_SetPythonHome`, or the value of the :envvar:`PYTHONHOME`
@@ -644,6 +341,7 @@ Thread State and the Global Interpreter Lock
============================================
.. index::
+ single: GIL
single: global interpreter lock
single: interpreter lock
single: lock, interpreter
@@ -656,12 +354,12 @@ operations could cause problems in a multi-threaded program: for example, when
two threads simultaneously increment the reference count of the same object, the
reference count could end up being incremented only once instead of twice.
-.. index:: single: setswitchinterval() (in module sys)
+.. index:: single: setcheckinterval() (in module sys)
Therefore, the rule exists that only the thread that has acquired the
:term:`GIL` may operate on Python objects or call Python/C API functions.
In order to emulate concurrency of execution, the interpreter regularly
-tries to switch threads (see :func:`sys.setswitchinterval`). The lock is also
+tries to switch threads (see :func:`sys.setcheckinterval`). The lock is also
released around potentially blocking I/O operations like reading or writing
a file, so that other Python threads can run in the meantime.
@@ -698,14 +396,15 @@ This is so common that a pair of macros exists to simplify it::
The :c:macro:`Py_BEGIN_ALLOW_THREADS` macro opens a new block and declares a
hidden local variable; the :c:macro:`Py_END_ALLOW_THREADS` macro closes the
-block.
+block. These two macros are still available when Python is compiled without
+thread support (they simply have an empty expansion).
-The block above expands to the following code::
+When thread support is enabled, the block above expands to the following code::
PyThreadState *_save;
_save = PyEval_SaveThread();
- ... Do some blocking I/O operation ...
+ ...Do some blocking I/O operation...
PyEval_RestoreThread(_save);
.. index::
@@ -769,19 +468,9 @@ supports the creation of additional interpreters (using
:c:func:`Py_NewInterpreter`), but mixing multiple interpreters and the
:c:func:`PyGILState_\*` API is unsupported.
-
-.. _fork-and-threads:
-
-Cautions about fork()
----------------------
-
Another important thing to note about threads is their behaviour in the face
of the C :c:func:`fork` call. On most systems with :c:func:`fork`, after a
-process forks only the thread that issued the fork will exist. This has a
-concrete impact both on how locks must be handled and on all stored state
-in CPython's runtime.
-
-The fact that only the "current" thread remains
+process forks only the thread that issued the fork will exist. That also
means any locks held by other threads will never be released. Python solves
this for :func:`os.fork` by acquiring the locks it uses internally before
the fork, and releasing them afterwards. In addition, it resets any
@@ -793,20 +482,9 @@ Additionally, when extending or embedding Python, calling :c:func:`fork`
directly rather than through :func:`os.fork` (and returning to or calling
into Python) may result in a deadlock by one of Python's internal locks
being held by a thread that is defunct after the fork.
-:c:func:`PyOS_AfterFork_Child` tries to reset the necessary locks, but is not
+:c:func:`PyOS_AfterFork` tries to reset the necessary locks, but is not
always able to.
-The fact that all other threads go away also means that CPython's
-runtime state there must be cleaned up properly, which :func:`os.fork`
-does. This means finalizing all other :c:type:`PyThreadState` objects
-belonging to the current interpreter and all other
-:c:type:`PyInterpreterState` objects. Due to this and the special
-nature of the :ref:`"main" interpreter <sub-interpreter-support>`,
-:c:func:`fork` should only be called in that interpreter's "main"
-thread, where the CPython global runtime was originally initialized.
-The only exception is if :c:func:`exec` will be called immediately
-after.
-
High-level API
--------------
@@ -837,74 +515,92 @@ code, or when embedding the Python interpreter:
.. c:function:: void PyEval_InitThreads()
.. index::
- single: PyEval_AcquireThread()
+ single: PyEval_ReleaseLock()
single: PyEval_ReleaseThread()
single: PyEval_SaveThread()
single: PyEval_RestoreThread()
Initialize and acquire the global interpreter lock. It should be called in the
main thread before creating a second thread or engaging in any other thread
- operations such as ``PyEval_ReleaseThread(tstate)``. It is not needed before
- calling :c:func:`PyEval_SaveThread` or :c:func:`PyEval_RestoreThread`.
+ operations such as :c:func:`PyEval_ReleaseLock` or
+ ``PyEval_ReleaseThread(tstate)``. It is not needed before calling
+ :c:func:`PyEval_SaveThread` or :c:func:`PyEval_RestoreThread`.
- This is a no-op when called for a second time.
+ .. index:: single: Py_Initialize()
- .. versionchanged:: 3.7
- This function is now called by :c:func:`Py_Initialize()`, so you don't
- have to call it yourself anymore.
+ This is a no-op when called for a second time. It is safe to call this function
+ before calling :c:func:`Py_Initialize`.
- .. versionchanged:: 3.2
- This function cannot be called before :c:func:`Py_Initialize()` anymore.
+ .. index:: module: thread
- .. index:: module: _thread
+ .. 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 :mod:`_thread` module creates a new thread, knowing
+ that either it has the lock or the lock hasn't been created yet, it calls
+ :c:func:`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.
.. c:function:: int PyEval_ThreadsInitialized()
Returns a non-zero value if :c:func:`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.
+ avoid calls to the locking API when running single-threaded. This function is
+ not available when thread support is disabled at compile time.
- .. versionchanged:: 3.7
- The :term:`GIL` is now initialized by :c:func:`Py_Initialize()`.
+ .. versionadded:: 2.4
.. c:function:: PyThreadState* PyEval_SaveThread()
- Release the global interpreter lock (if it has been created) 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.
+ 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.)
.. c:function:: void PyEval_RestoreThread(PyThreadState *tstate)
- Acquire the global interpreter lock (if it has been created) 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.
+ 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.)
- .. note::
- Calling this function from a thread when the runtime is finalizing
- will terminate the thread, even if the thread was not created by Python.
- You can use :c:func:`_Py_IsFinalizing` or :func:`sys.is_finalizing` to
- check if the interpreter is in process of being finalized before calling
- this function to avoid unwanted termination.
.. c:function:: 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``).
+ When the current thread state is *NULL*, this issues a fatal error (so that
+ the caller needn't check for *NULL*).
.. c:function:: 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
+ *tstate*, which may be *NULL*. The global interpreter lock must be held
and is not released.
+.. c:function:: void PyEval_ReInitThreads()
+
+ This function is called from :c:func:`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:
@@ -930,12 +626,8 @@ with sub-interpreters:
When the function returns, the current thread will hold the GIL and be able
to call arbitrary Python code. Failure is a fatal error.
- .. note::
- Calling this function from a thread when the runtime is finalizing
- will terminate the thread, even if the thread was not created by Python.
- You can use :c:func:`_Py_IsFinalizing` or :func:`sys.is_finalizing` to
- check if the interpreter is in process of being finalized before calling
- this function to avoid unwanted termination.
+ .. versionadded:: 2.3
+
.. c:function:: void PyGILState_Release(PyGILState_STATE)
@@ -947,6 +639,8 @@ with sub-interpreters:
Every call to :c:func:`PyGILState_Ensure` must be matched by a call to
:c:func:`PyGILState_Release` on the same thread.
+ .. versionadded:: 2.3
+
.. c:function:: PyThreadState* PyGILState_GetThisThreadState()
@@ -955,19 +649,7 @@ with sub-interpreters:
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.
-
-.. c:function:: int PyGILState_Check()
-
- Return ``1`` if the current thread is holding the GIL and ``0`` otherwise.
- This function can be called from any thread at any time.
- Only if it has had its Python thread state initialized and currently is
- holding the GIL will it return ``1``.
- This is mainly a helper/diagnostic function. It can be useful
- for example in callback contexts or memory allocation functions when
- knowing that the GIL is locked can allow the caller to perform sensitive
- actions or otherwise behave differently.
-
- .. versionadded:: 3.4
+ .. versionadded:: 2.3
The following macros are normally used without a trailing semicolon; look for
@@ -979,7 +661,7 @@ example usage in the Python source distribution.
This macro expands to ``{ PyThreadState *_save; _save = PyEval_SaveThread();``.
Note that it contains an opening brace; it must be matched with a following
:c:macro:`Py_END_ALLOW_THREADS` macro. See above for further discussion of this
- macro.
+ macro. It is a no-op when thread support is disabled at compile time.
.. c:macro:: Py_END_ALLOW_THREADS
@@ -987,29 +669,29 @@ example usage in the Python source distribution.
This macro expands to ``PyEval_RestoreThread(_save); }``. Note that it contains
a closing brace; it must be matched with an earlier
:c:macro:`Py_BEGIN_ALLOW_THREADS` macro. See above for further discussion of
- this macro.
+ this macro. It is a no-op when thread support is disabled at compile time.
.. c:macro:: Py_BLOCK_THREADS
This macro expands to ``PyEval_RestoreThread(_save);``: it is equivalent to
- :c:macro:`Py_END_ALLOW_THREADS` without the closing brace.
+ :c:macro:`Py_END_ALLOW_THREADS` without the closing brace. It is a no-op when
+ thread support is disabled at compile time.
.. c:macro:: Py_UNBLOCK_THREADS
This macro expands to ``_save = PyEval_SaveThread();``: it is equivalent to
:c:macro:`Py_BEGIN_ALLOW_THREADS` without the opening brace and variable
- declaration.
+ declaration. It is a no-op when thread support is disabled at compile time.
Low-level API
-------------
-All of the following functions must be called after :c:func:`Py_Initialize`.
-
-.. versionchanged:: 3.7
- :c:func:`Py_Initialize()` now initializes the :term:`GIL`.
+All of the following functions are only available when thread support is enabled
+at compile time, and must be called only when the global interpreter lock has
+been created.
.. c:function:: PyInterpreterState* PyInterpreterState_New()
@@ -1018,16 +700,12 @@ All of the following functions must be called after :c:func:`Py_Initialize`.
be held, but may be held if it is necessary to serialize calls to this
function.
- .. audit-event:: cpython.PyInterpreterState_New "" c.PyInterpreterState_New
-
.. c:function:: void PyInterpreterState_Clear(PyInterpreterState *interp)
Reset all information in an interpreter state object. The global interpreter
lock must be held.
- .. audit-event:: cpython.PyInterpreterState_Clear "" c.PyInterpreterState_Clear
-
.. c:function:: void PyInterpreterState_Delete(PyInterpreterState *interp)
@@ -1056,44 +734,20 @@ All of the following functions must be called after :c:func:`Py_Initialize`.
:c:func:`PyThreadState_Clear`.
- .. c:function:: void PyThreadState_DeleteCurrent()
-
- Destroy the current thread state and release the global interpreter lock.
- Like :c:func:`PyThreadState_Delete`, the global interpreter lock need not
- be held. The thread state must have been reset with a previous call
- to :c:func:`PyThreadState_Clear`.
-
-
-.. c:function:: PY_INT64_T PyInterpreterState_GetID(PyInterpreterState *interp)
-
- Return the interpreter's unique ID. If there was any error in doing
- so then ``-1`` is returned and an error is set.
-
- .. versionadded:: 3.7
-
-
-.. c:function:: PyObject* PyInterpreterState_GetDict(PyInterpreterState *interp)
-
- Return a dictionary in which interpreter-specific data may be stored.
- If this function returns ``NULL`` then no exception has been raised and
- the caller should assume no interpreter-specific dict is available.
-
- This is not a replacement for :c:func:`PyModule_GetState()`, which
- extensions should use to store interpreter-specific state information.
-
- .. versionadded:: 3.8
-
-
.. c:function:: PyObject* PyThreadState_GetDict()
Return a dictionary in which extensions can store thread-specific state
information. Each extension should use a unique key to use to store state in
the dictionary. It is okay to call this function when no current thread state
- is available. If this function returns ``NULL``, no exception has been raised and
+ is available. If this function returns *NULL*, no exception has been raised and
the caller should assume no current thread state is available.
+ .. versionchanged:: 2.3
+ Previously this could only be called when a current thread is active, and *NULL*
+ meant that an exception was raised.
+
-.. c:function:: int PyThreadState_SetAsyncExc(unsigned long id, PyObject *exc)
+.. c:function:: int PyThreadState_SetAsyncExc(long id, PyObject *exc)
Asynchronously raise an exception in a thread. The *id* argument is the thread
id of the target thread; *exc* is the exception object to be raised. This
@@ -1103,42 +757,31 @@ All of the following functions must be called after :c:func:`Py_Initialize`.
zero if the thread id isn't found. If *exc* is :const:`NULL`, the pending
exception (if any) for the thread is cleared. This raises no exceptions.
- .. versionchanged:: 3.7
- The type of the *id* parameter changed from :c:type:`long` to
- :c:type:`unsigned long`.
+ .. versionadded:: 2.3
+
.. c:function:: void PyEval_AcquireThread(PyThreadState *tstate)
Acquire the global interpreter lock and set the current thread state to
- *tstate*, which should not be ``NULL``. The lock must have been created earlier.
+ *tstate*, which should not be *NULL*. The lock must have been created earlier.
If this thread already has the lock, deadlock ensues.
- .. note::
- Calling this function from a thread when the runtime is finalizing
- will terminate the thread, even if the thread was not created by Python.
- You can use :c:func:`_Py_IsFinalizing` or :func:`sys.is_finalizing` to
- check if the interpreter is in process of being finalized before calling
- this function to avoid unwanted termination.
-
- .. versionchanged:: 3.8
- Updated to be consistent with :c:func:`PyEval_RestoreThread`,
- :c:func:`Py_END_ALLOW_THREADS`, and :c:func:`PyGILState_Ensure`,
- and terminate the current thread if called while the interpreter is finalizing.
-
:c:func:`PyEval_RestoreThread` is a higher-level function which is always
- available (even when threads have not been initialized).
+ available (even when thread support isn't enabled or when threads have
+ not been initialized).
.. c:function:: void PyEval_ReleaseThread(PyThreadState *tstate)
- Reset the current thread state to ``NULL`` and release the global interpreter
+ Reset the current thread state to *NULL* and release the global interpreter
lock. The lock must have been created earlier and must be held by the current
- thread. The *tstate* argument, which must not be ``NULL``, is only used to check
+ thread. The *tstate* argument, which must not be *NULL*, is only used to check
that it represents the current thread state --- if it isn't, a fatal error is
reported.
:c:func:`PyEval_SaveThread` is a higher-level function which is always
- available (even when threads have not been initialized).
+ available (even when thread support isn't enabled or when threads have
+ not been initialized).
.. c:function:: void PyEval_AcquireLock()
@@ -1146,53 +789,31 @@ All of the following functions must be called after :c:func:`Py_Initialize`.
Acquire the global interpreter lock. The lock must have been created earlier.
If this thread already has the lock, a deadlock ensues.
- .. deprecated:: 3.2
- This function does not update the current thread state. Please use
+ .. warning::
+ This function does not change the current thread state. Please use
:c:func:`PyEval_RestoreThread` or :c:func:`PyEval_AcquireThread`
instead.
- .. note::
- Calling this function from a thread when the runtime is finalizing
- will terminate the thread, even if the thread was not created by Python.
- You can use :c:func:`_Py_IsFinalizing` or :func:`sys.is_finalizing` to
- check if the interpreter is in process of being finalized before calling
- this function to avoid unwanted termination.
-
- .. versionchanged:: 3.8
- Updated to be consistent with :c:func:`PyEval_RestoreThread`,
- :c:func:`Py_END_ALLOW_THREADS`, and :c:func:`PyGILState_Ensure`,
- and terminate the current thread if called while the interpreter is finalizing.
-
.. c:function:: void PyEval_ReleaseLock()
Release the global interpreter lock. The lock must have been created earlier.
- .. deprecated:: 3.2
- This function does not update the current thread state. Please use
+ .. warning::
+ This function does not change the current thread state. Please use
:c:func:`PyEval_SaveThread` or :c:func:`PyEval_ReleaseThread`
instead.
-.. _sub-interpreter-support:
-
Sub-interpreter support
=======================
While in most uses, you will only embed a single Python interpreter, there
are cases where you need to create several independent interpreters in the
-same process and perhaps even in the same thread. Sub-interpreters allow
-you to do that.
-
-The "main" interpreter is the first one created when the runtime initializes.
-It is usually the only Python interpreter in a process. Unlike sub-interpreters,
-the main interpreter has unique process-global responsibilities like signal
-handling. It is also responsible for execution during runtime initialization and
-is usually the active interpreter during runtime finalization. The
-:c:func:`PyInterpreterState_Main` funtion returns a pointer to its state.
-
-You can switch between sub-interpreters using the :c:func:`PyThreadState_Swap`
-function. You can create and destroy them using the following functions:
+same process and perhaps even in the same thread. Sub-interpreters allow
+you to do that. You can switch between sub-interpreters using the
+:c:func:`PyThreadState_Swap` function. You can create and destroy them
+using the following functions:
.. c:function:: PyThreadState* Py_NewInterpreter()
@@ -1218,7 +839,7 @@ function. You can create and destroy them using the following functions:
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
+ 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
@@ -1227,7 +848,7 @@ function. You can create and destroy them using the following functions:
entry.)
.. index::
- single: Py_FinalizeEx()
+ single: Py_Finalize()
single: Py_Initialize()
Extension modules are shared between (sub-)interpreters as follows: the first
@@ -1237,7 +858,7 @@ function. You can create and destroy them using the following functions:
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
- :c:func:`Py_FinalizeEx` and :c:func:`Py_Initialize`; in that case, the extension's
+ :c:func:`Py_Finalize` and :c:func:`Py_Initialize`; in that case, the extension's
``initmodule`` function *is* called again.
.. index:: single: close() (in module os)
@@ -1245,14 +866,14 @@ function. You can create and destroy them using the following functions:
.. c:function:: void Py_EndInterpreter(PyThreadState *tstate)
- .. index:: single: Py_FinalizeEx()
+ .. index:: single: Py_Finalize()
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
+ 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.) :c:func:`Py_FinalizeEx` will destroy all sub-interpreters that
+ when it returns.) :c:func:`Py_Finalize` will destroy all sub-interpreters that
haven't been explicitly destroyed at that point.
@@ -1324,7 +945,8 @@ pointer and a void pointer argument.
function is generally **not** suitable for calling Python code from
arbitrary C threads. Instead, use the :ref:`PyGILState API<gilstate>`.
- .. versionadded:: 3.1
+ .. versionadded:: 2.7
+
.. _profiling:
@@ -1338,12 +960,14 @@ The Python interpreter provides some low-level support for attaching profiling
and execution tracing facilities. These are used for profiling, debugging, and
coverage analysis tools.
-This C interface allows the profiling or tracing code to avoid the overhead of
-calling through Python-level callable objects, making a direct C function call
-instead. The essential attributes of the facility have not changed; the
-interface allows trace functions to be installed per-thread, and the basic
-events reported to the trace function are the same as had been reported to the
-Python-level trace functions in previous versions.
+Starting with Python 2.2, the implementation of this facility was substantially
+revised, and an interface from C was added. This C interface allows the
+profiling or tracing code to avoid the overhead of calling through Python-level
+callable objects, making a direct C function call instead. The essential
+attributes of the facility have not changed; the interface allows trace
+functions to be installed per-thread, and the basic events reported to the trace
+function are the same as had been reported to the Python-level trace functions
+in previous versions.
.. c:type:: int (*Py_tracefunc)(PyObject *obj, PyFrameObject *frame, int what, PyObject *arg)
@@ -1353,30 +977,29 @@ Python-level trace functions in previous versions.
registration function as *obj*, *frame* is the frame object to which the event
pertains, *what* is one of the constants :const:`PyTrace_CALL`,
:const:`PyTrace_EXCEPTION`, :const:`PyTrace_LINE`, :const:`PyTrace_RETURN`,
- :const:`PyTrace_C_CALL`, :const:`PyTrace_C_EXCEPTION`, :const:`PyTrace_C_RETURN`,
- or :const:`PyTrace_OPCODE`, and *arg* depends on the value of *what*:
-
- +------------------------------+----------------------------------------+
- | Value of *what* | Meaning of *arg* |
- +==============================+========================================+
- | :const:`PyTrace_CALL` | Always :c:data:`Py_None`. |
- +------------------------------+----------------------------------------+
- | :const:`PyTrace_EXCEPTION` | Exception information as returned by |
- | | :func:`sys.exc_info`. |
- +------------------------------+----------------------------------------+
- | :const:`PyTrace_LINE` | Always :c:data:`Py_None`. |
- +------------------------------+----------------------------------------+
- | :const:`PyTrace_RETURN` | Value being returned to the caller, |
- | | or ``NULL`` if caused by an exception. |
- +------------------------------+----------------------------------------+
- | :const:`PyTrace_C_CALL` | Function object being called. |
- +------------------------------+----------------------------------------+
- | :const:`PyTrace_C_EXCEPTION` | Function object being called. |
- +------------------------------+----------------------------------------+
- | :const:`PyTrace_C_RETURN` | Function object being called. |
- +------------------------------+----------------------------------------+
- | :const:`PyTrace_OPCODE` | Always :c:data:`Py_None`. |
- +------------------------------+----------------------------------------+
+ :const:`PyTrace_C_CALL`, :const:`PyTrace_C_EXCEPTION`, or
+ :const:`PyTrace_C_RETURN`, and *arg* depends on the value of *what*:
+
+ +------------------------------+--------------------------------------+
+ | Value of *what* | Meaning of *arg* |
+ +==============================+======================================+
+ | :const:`PyTrace_CALL` | Always :c:data:`Py_None`. |
+ +------------------------------+--------------------------------------+
+ | :const:`PyTrace_EXCEPTION` | Exception information as returned by |
+ | | :func:`sys.exc_info`. |
+ +------------------------------+--------------------------------------+
+ | :const:`PyTrace_LINE` | Always :c:data:`Py_None`. |
+ +------------------------------+--------------------------------------+
+ | :const:`PyTrace_RETURN` | Value being returned to the caller, |
+ | | or *NULL* if caused by an exception. |
+ +------------------------------+--------------------------------------+
+ | :const:`PyTrace_C_CALL` | Function object being called. |
+ +------------------------------+--------------------------------------+
+ | :const:`PyTrace_C_EXCEPTION` | Function object being called. |
+ +------------------------------+--------------------------------------+
+ | :const:`PyTrace_C_RETURN` | Function object being called. |
+ +------------------------------+--------------------------------------+
+
.. c:var:: int PyTrace_CALL
@@ -1400,9 +1023,8 @@ Python-level trace functions in previous versions.
.. c:var:: int PyTrace_LINE
- The value passed as the *what* parameter to a :c:type:`Py_tracefunc` function
- (but not a profiling function) when a line-number event is being reported.
- It may be disabled for a frame by setting :attr:`f_trace_lines` to *0* on that frame.
+ The value passed as the *what* parameter to a trace function (but not a
+ profiling function) when a line-number event is being reported.
.. c:var:: int PyTrace_RETURN
@@ -1429,32 +1051,66 @@ Python-level trace functions in previous versions.
function has returned.
-.. c:var:: int PyTrace_OPCODE
-
- The value for the *what* parameter to :c:type:`Py_tracefunc` functions (but not
- profiling functions) when a new opcode is about to be executed. This event is
- not emitted by default: it must be explicitly requested by setting
- :attr:`f_trace_opcodes` to *1* on the frame.
-
-
.. c:function:: 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
+ 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 :const:`PyTrace_LINE`
- :const:`PyTrace_OPCODE` and :const:`PyTrace_EXCEPTION`.
+ and :const:`PyTrace_EXCEPTION`.
.. c:function:: void PyEval_SetTrace(Py_tracefunc func, PyObject *obj)
Set the tracing function to *func*. This is similar to
:c:func:`PyEval_SetProfile`, except the tracing function does receive line-number
- events and per-opcode events, but does not receive any event related to C function
- objects being called. Any trace function registered using :c:func:`PyEval_SetTrace`
- will not receive :const:`PyTrace_C_CALL`, :const:`PyTrace_C_EXCEPTION` or
- :const:`PyTrace_C_RETURN` as a value for the *what* parameter.
+ events and does not receive any event related to C function objects being called. Any
+ trace function registered using :c:func:`PyEval_SetTrace` will not receive
+ :const:`PyTrace_C_CALL`, :const:`PyTrace_C_EXCEPTION` or :const:`PyTrace_C_RETURN`
+ as a value for the *what* parameter.
+
+
+.. c:function:: PyObject* PyEval_GetCallStats(PyObject *self)
+
+ Return a tuple of function call counts. There are constants defined for the
+ positions within the tuple:
+
+ +-------------------------------+-------+
+ | Name | Value |
+ +===============================+=======+
+ | :const:`PCALL_ALL` | 0 |
+ +-------------------------------+-------+
+ | :const:`PCALL_FUNCTION` | 1 |
+ +-------------------------------+-------+
+ | :const:`PCALL_FAST_FUNCTION` | 2 |
+ +-------------------------------+-------+
+ | :const:`PCALL_FASTER_FUNCTION`| 3 |
+ +-------------------------------+-------+
+ | :const:`PCALL_METHOD` | 4 |
+ +-------------------------------+-------+
+ | :const:`PCALL_BOUND_METHOD` | 5 |
+ +-------------------------------+-------+
+ | :const:`PCALL_CFUNCTION` | 6 |
+ +-------------------------------+-------+
+ | :const:`PCALL_TYPE` | 7 |
+ +-------------------------------+-------+
+ | :const:`PCALL_GENERATOR` | 8 |
+ +-------------------------------+-------+
+ | :const:`PCALL_OTHER` | 9 |
+ +-------------------------------+-------+
+ | :const:`PCALL_POP` | 10 |
+ +-------------------------------+-------+
+
+ :const:`PCALL_FAST_FUNCTION` means no argument tuple needs to be created.
+ :const:`PCALL_FASTER_FUNCTION` means that the fast-path frame setup code is used.
+
+ If there is a method call where the call can be optimized by changing
+ the argument tuple and calling the function directly, it gets recorded
+ twice.
+
+ This function is only present if Python is compiled with :const:`CALL_PROFILE`
+ defined.
.. _advanced-debugging:
@@ -1471,10 +1127,7 @@ These functions are only intended to be used by advanced debugging tools.
Return the interpreter state object at the head of the list of all such objects.
-
-.. c:function:: PyInterpreterState* PyInterpreterState_Main()
-
- Return the main interpreter state object.
+ .. versionadded:: 2.2
.. c:function:: PyInterpreterState* PyInterpreterState_Next(PyInterpreterState *interp)
@@ -1482,172 +1135,21 @@ These functions are only intended to be used by advanced debugging tools.
Return the next interpreter state object after *interp* from the list of all
such objects.
+ .. versionadded:: 2.2
+
.. c:function:: PyThreadState * PyInterpreterState_ThreadHead(PyInterpreterState *interp)
Return the pointer to the first :c:type:`PyThreadState` object in the list of
threads associated with the interpreter *interp*.
+ .. versionadded:: 2.2
+
.. c:function:: PyThreadState* PyThreadState_Next(PyThreadState *tstate)
Return the next thread state object after *tstate* from the list of all such
objects belonging to the same :c:type:`PyInterpreterState` object.
-
-.. _thread-local-storage:
-
-Thread Local Storage Support
-============================
-
-.. sectionauthor:: Masayuki Yamamoto <ma3yuki.8mamo10@gmail.com>
-
-The Python interpreter provides low-level support for thread-local storage
-(TLS) which wraps the underlying native TLS implementation to support the
-Python-level thread local storage API (:class:`threading.local`). The
-CPython C level APIs are similar to those offered by pthreads and Windows:
-use a thread key and functions to associate a :c:type:`void\*` value per
-thread.
-
-The GIL does *not* need to be held when calling these functions; they supply
-their own locking.
-
-Note that :file:`Python.h` does not include the declaration of the TLS APIs,
-you need to include :file:`pythread.h` to use thread-local storage.
-
-.. note::
- None of these API functions handle memory management on behalf of the
- :c:type:`void\*` values. You need to allocate and deallocate them yourself.
- If the :c:type:`void\*` values happen to be :c:type:`PyObject\*`, these
- functions don't do refcount operations on them either.
-
-.. _thread-specific-storage-api:
-
-Thread Specific Storage (TSS) API
----------------------------------
-
-TSS API is introduced to supersede the use of the existing TLS API within the
-CPython interpreter. This API uses a new type :c:type:`Py_tss_t` instead of
-:c:type:`int` to represent thread keys.
-
-.. versionadded:: 3.7
-
-.. seealso:: "A New C-API for Thread-Local Storage in CPython" (:pep:`539`)
-
-
-.. c:type:: Py_tss_t
-
- This data structure represents the state of a thread key, the definition of
- which may depend on the underlying TLS implementation, and it has an
- internal field representing the key's initialization state. There are no
- public members in this structure.
-
- When :ref:`Py_LIMITED_API <stable>` is not defined, static allocation of
- this type by :c:macro:`Py_tss_NEEDS_INIT` is allowed.
-
-
-.. c:macro:: Py_tss_NEEDS_INIT
-
- This macro expands to the initializer for :c:type:`Py_tss_t` variables.
- Note that this macro won't be defined with :ref:`Py_LIMITED_API <stable>`.
-
-
-Dynamic Allocation
-~~~~~~~~~~~~~~~~~~
-
-Dynamic allocation of the :c:type:`Py_tss_t`, required in extension modules
-built with :ref:`Py_LIMITED_API <stable>`, where static allocation of this type
-is not possible due to its implementation being opaque at build time.
-
-
-.. c:function:: Py_tss_t* PyThread_tss_alloc()
-
- Return a value which is the same state as a value initialized with
- :c:macro:`Py_tss_NEEDS_INIT`, or ``NULL`` in the case of dynamic allocation
- failure.
-
-
-.. c:function:: void PyThread_tss_free(Py_tss_t *key)
-
- Free the given *key* allocated by :c:func:`PyThread_tss_alloc`, after
- first calling :c:func:`PyThread_tss_delete` to ensure any associated
- thread locals have been unassigned. This is a no-op if the *key*
- argument is `NULL`.
-
- .. note::
- A freed key becomes a dangling pointer, you should reset the key to
- `NULL`.
-
-
-Methods
-~~~~~~~
-
-The parameter *key* of these functions must not be ``NULL``. Moreover, the
-behaviors of :c:func:`PyThread_tss_set` and :c:func:`PyThread_tss_get` are
-undefined if the given :c:type:`Py_tss_t` has not been initialized by
-:c:func:`PyThread_tss_create`.
-
-
-.. c:function:: int PyThread_tss_is_created(Py_tss_t *key)
-
- Return a non-zero value if the given :c:type:`Py_tss_t` has been initialized
- by :c:func:`PyThread_tss_create`.
-
-
-.. c:function:: int PyThread_tss_create(Py_tss_t *key)
-
- Return a zero value on successful initialization of a TSS key. The behavior
- is undefined if the value pointed to by the *key* argument is not
- initialized by :c:macro:`Py_tss_NEEDS_INIT`. This function can be called
- repeatedly on the same key -- calling it on an already initialized key is a
- no-op and immediately returns success.
-
-
-.. c:function:: void PyThread_tss_delete(Py_tss_t *key)
-
- Destroy a TSS key to forget the values associated with the key across all
- threads, and change the key's initialization state to uninitialized. A
- destroyed key is able to be initialized again by
- :c:func:`PyThread_tss_create`. This function can be called repeatedly on
- the same key -- calling it on an already destroyed key is a no-op.
-
-
-.. c:function:: int PyThread_tss_set(Py_tss_t *key, void *value)
-
- Return a zero value to indicate successfully associating a :c:type:`void\*`
- value with a TSS key in the current thread. Each thread has a distinct
- mapping of the key to a :c:type:`void\*` value.
-
-
-.. c:function:: void* PyThread_tss_get(Py_tss_t *key)
-
- Return the :c:type:`void\*` value associated with a TSS key in the current
- thread. This returns ``NULL`` if no value is associated with the key in the
- current thread.
-
-
-.. _thread-local-storage-api:
-
-Thread Local Storage (TLS) API
-------------------------------
-
-.. deprecated:: 3.7
- This API is superseded by
- :ref:`Thread Specific Storage (TSS) API <thread-specific-storage-api>`.
-
-.. note::
- This version of the API does not support platforms where the native TLS key
- is defined in a way that cannot be safely cast to ``int``. On such platforms,
- :c:func:`PyThread_create_key` will return immediately with a failure status,
- and the other TLS functions will all be no-ops on such platforms.
-
-Due to the compatibility problem noted above, this version of the API should not
-be used in new code.
-
-.. c:function:: int PyThread_create_key()
-.. c:function:: void PyThread_delete_key(int key)
-.. c:function:: int PyThread_set_key_value(int key, void *value)
-.. c:function:: void* PyThread_get_key_value(int key)
-.. c:function:: void PyThread_delete_key_value(int key)
-.. c:function:: void PyThread_ReInitTLS()
+ .. versionadded:: 2.2
diff --git a/Doc/c-api/init_config.rst b/Doc/c-api/init_config.rst
deleted file mode 100644
index 6b16b5b..0000000
--- a/Doc/c-api/init_config.rst
+++ /dev/null
@@ -1,1056 +0,0 @@
-.. highlight:: c
-
-.. _init-config:
-
-***********************************
-Python Initialization Configuration
-***********************************
-
-.. versionadded:: 3.8
-
-Structures:
-
-* :c:type:`PyConfig`
-* :c:type:`PyPreConfig`
-* :c:type:`PyStatus`
-* :c:type:`PyWideStringList`
-
-Functions:
-
-* :c:func:`PyConfig_Clear`
-* :c:func:`PyConfig_InitIsolatedConfig`
-* :c:func:`PyConfig_InitPythonConfig`
-* :c:func:`PyConfig_Read`
-* :c:func:`PyConfig_SetArgv`
-* :c:func:`PyConfig_SetBytesArgv`
-* :c:func:`PyConfig_SetBytesString`
-* :c:func:`PyConfig_SetString`
-* :c:func:`PyConfig_SetWideStringList`
-* :c:func:`PyPreConfig_InitIsolatedConfig`
-* :c:func:`PyPreConfig_InitPythonConfig`
-* :c:func:`PyStatus_Error`
-* :c:func:`PyStatus_Exception`
-* :c:func:`PyStatus_Exit`
-* :c:func:`PyStatus_IsError`
-* :c:func:`PyStatus_IsExit`
-* :c:func:`PyStatus_NoMemory`
-* :c:func:`PyStatus_Ok`
-* :c:func:`PyWideStringList_Append`
-* :c:func:`PyWideStringList_Insert`
-* :c:func:`Py_ExitStatusException`
-* :c:func:`Py_InitializeFromConfig`
-* :c:func:`Py_PreInitialize`
-* :c:func:`Py_PreInitializeFromArgs`
-* :c:func:`Py_PreInitializeFromBytesArgs`
-* :c:func:`Py_RunMain`
-
-The preconfiguration (``PyPreConfig`` type) is stored in
-``_PyRuntime.preconfig`` and the configuration (``PyConfig`` type) is stored in
-``PyInterpreterState.config``.
-
-See also :ref:`Initialization, Finalization, and Threads <initialization>`.
-
-.. seealso::
- :pep:`587` "Python Initialization Configuration".
-
-
-PyWideStringList
-----------------
-
-.. c:type:: PyWideStringList
-
- List of ``wchar_t*`` strings.
-
- If *length* is non-zero, *items* must be non-``NULL`` and all strings must be
- non-``NULL``.
-
- Methods:
-
- .. c:function:: PyStatus PyWideStringList_Append(PyWideStringList *list, const wchar_t *item)
-
- Append *item* to *list*.
-
- Python must be preinitialized to call this function.
-
- .. c:function:: PyStatus PyWideStringList_Insert(PyWideStringList *list, Py_ssize_t index, const wchar_t *item)
-
- Insert *item* into *list* at *index*.
-
- If *index* is greater than or equal to *list* length, append *item* to
- *list*.
-
- *index* must be greater than or equal to 0.
-
- Python must be preinitialized to call this function.
-
- Structure fields:
-
- .. c:member:: Py_ssize_t length
-
- List length.
-
- .. c:member:: wchar_t** items
-
- List items.
-
-PyStatus
---------
-
-.. c:type:: PyStatus
-
- Structure to store an initialization function status: success, error
- or exit.
-
- For an error, it can store the C function name which created the error.
-
- Structure fields:
-
- .. c:member:: int exitcode
-
- Exit code. Argument passed to ``exit()``.
-
- .. c:member:: const char *err_msg
-
- Error message.
-
- .. c:member:: const char *func
-
- Name of the function which created an error, can be ``NULL``.
-
- Functions to create a status:
-
- .. c:function:: PyStatus PyStatus_Ok(void)
-
- Success.
-
- .. c:function:: PyStatus PyStatus_Error(const char *err_msg)
-
- Initialization error with a message.
-
- .. c:function:: PyStatus PyStatus_NoMemory(void)
-
- Memory allocation failure (out of memory).
-
- .. c:function:: PyStatus PyStatus_Exit(int exitcode)
-
- Exit Python with the specified exit code.
-
- Functions to handle a status:
-
- .. c:function:: int PyStatus_Exception(PyStatus status)
-
- Is the status an error or an exit? If true, the exception must be
- handled; by calling :c:func:`Py_ExitStatusException` for example.
-
- .. c:function:: int PyStatus_IsError(PyStatus status)
-
- Is the result an error?
-
- .. c:function:: int PyStatus_IsExit(PyStatus status)
-
- Is the result an exit?
-
- .. c:function:: void Py_ExitStatusException(PyStatus status)
-
- Call ``exit(exitcode)`` if *status* is an exit. Print the error
- message and exit with a non-zero exit code if *status* is an error. Must
- only be called if ``PyStatus_Exception(status)`` is non-zero.
-
-.. note::
- Internally, Python uses macros which set ``PyStatus.func``,
- whereas functions to create a status set ``func`` to ``NULL``.
-
-Example::
-
- PyStatus alloc(void **ptr, size_t size)
- {
- *ptr = PyMem_RawMalloc(size);
- if (*ptr == NULL) {
- return PyStatus_NoMemory();
- }
- return PyStatus_Ok();
- }
-
- int main(int argc, char **argv)
- {
- void *ptr;
- PyStatus status = alloc(&ptr, 16);
- if (PyStatus_Exception(status)) {
- Py_ExitStatusException(status);
- }
- PyMem_Free(ptr);
- return 0;
- }
-
-
-PyPreConfig
------------
-
-.. c:type:: PyPreConfig
-
- Structure used to preinitialize Python:
-
- * Set the Python memory allocator
- * Configure the LC_CTYPE locale
- * Set the UTF-8 mode
-
- Function to initialize a preconfiguration:
-
- .. c:function:: void PyPreConfig_InitIsolatedConfig(PyPreConfig *preconfig)
-
- Initialize the preconfiguration with :ref:`Python Configuration
- <init-python-config>`.
-
- .. c:function:: void PyPreConfig_InitPythonConfig(PyPreConfig *preconfig)
-
- Initialize the preconfiguration with :ref:`Isolated Configuration
- <init-isolated-conf>`.
-
- Structure fields:
-
- .. c:member:: int allocator
-
- Name of the memory allocator:
-
- * ``PYMEM_ALLOCATOR_NOT_SET`` (``0``): don't change memory allocators
- (use defaults)
- * ``PYMEM_ALLOCATOR_DEFAULT`` (``1``): default memory allocators
- * ``PYMEM_ALLOCATOR_DEBUG`` (``2``): default memory allocators with
- debug hooks
- * ``PYMEM_ALLOCATOR_MALLOC`` (``3``): force usage of ``malloc()``
- * ``PYMEM_ALLOCATOR_MALLOC_DEBUG`` (``4``): force usage of
- ``malloc()`` with debug hooks
- * ``PYMEM_ALLOCATOR_PYMALLOC`` (``5``): :ref:`Python pymalloc memory
- allocator <pymalloc>`
- * ``PYMEM_ALLOCATOR_PYMALLOC_DEBUG`` (``6``): :ref:`Python pymalloc
- memory allocator <pymalloc>` with debug hooks
-
- ``PYMEM_ALLOCATOR_PYMALLOC`` and ``PYMEM_ALLOCATOR_PYMALLOC_DEBUG``
- are not supported if Python is configured using ``--without-pymalloc``
-
- See :ref:`Memory Management <memory>`.
-
- .. c:member:: int configure_locale
-
- Set the LC_CTYPE locale to the user preferred locale? If equals to 0, set
- :c:member:`coerce_c_locale` and :c:member:`coerce_c_locale_warn` to 0.
-
- .. c:member:: int coerce_c_locale
-
- If equals to 2, coerce the C locale; if equals to 1, read the LC_CTYPE
- locale to decide if it should be coerced.
-
- .. c:member:: int coerce_c_locale_warn
-
- If non-zero, emit a warning if the C locale is coerced.
-
- .. c:member:: int dev_mode
-
- See :c:member:`PyConfig.dev_mode`.
-
- .. c:member:: int isolated
-
- See :c:member:`PyConfig.isolated`.
-
- .. c:member:: int legacy_windows_fs_encoding (Windows only)
-
- If non-zero, disable UTF-8 Mode, set the Python filesystem encoding to
- ``mbcs``, set the filesystem error handler to ``replace``.
-
- Only available on Windows. ``#ifdef MS_WINDOWS`` macro can be used for
- Windows specific code.
-
- .. c:member:: int parse_argv
-
- If non-zero, :c:func:`Py_PreInitializeFromArgs` and
- :c:func:`Py_PreInitializeFromBytesArgs` parse their ``argv`` argument the
- same way the regular Python parses command line arguments: see
- :ref:`Command Line Arguments <using-on-cmdline>`.
-
- .. c:member:: int use_environment
-
- See :c:member:`PyConfig.use_environment`.
-
- .. c:member:: int utf8_mode
-
- If non-zero, enable the UTF-8 mode.
-
-Preinitialization with PyPreConfig
-----------------------------------
-
-Functions to preinitialize Python:
-
-.. c:function:: PyStatus Py_PreInitialize(const PyPreConfig *preconfig)
-
- Preinitialize Python from *preconfig* preconfiguration.
-
-.. c:function:: PyStatus Py_PreInitializeFromBytesArgs(const PyPreConfig *preconfig, int argc, char * const *argv)
-
- Preinitialize Python from *preconfig* preconfiguration and command line
- arguments (bytes strings).
-
-.. c:function:: PyStatus Py_PreInitializeFromArgs(const PyPreConfig *preconfig, int argc, wchar_t * const * argv)
-
- Preinitialize Python from *preconfig* preconfiguration and command line
- arguments (wide strings).
-
-The caller is responsible to handle exceptions (error or exit) using
-:c:func:`PyStatus_Exception` and :c:func:`Py_ExitStatusException`.
-
-For :ref:`Python Configuration <init-python-config>`
-(:c:func:`PyPreConfig_InitPythonConfig`), if Python is initialized with
-command line arguments, the command line arguments must also be passed to
-preinitialize Python, since they have an effect on the pre-configuration
-like encodings. For example, the :option:`-X utf8 <-X>` command line option
-enables the UTF-8 Mode.
-
-``PyMem_SetAllocator()`` can be called after :c:func:`Py_PreInitialize` and
-before :c:func:`Py_InitializeFromConfig` to install a custom memory allocator.
-It can be called before :c:func:`Py_PreInitialize` if
-:c:member:`PyPreConfig.allocator` is set to ``PYMEM_ALLOCATOR_NOT_SET``.
-
-Python memory allocation functions like :c:func:`PyMem_RawMalloc` must not be
-used before Python preinitialization, whereas calling directly ``malloc()`` and
-``free()`` is always safe. :c:func:`Py_DecodeLocale` must not be called before
-the preinitialization.
-
-Example using the preinitialization to enable the UTF-8 Mode::
-
- PyStatus status;
- PyPreConfig preconfig;
- PyPreConfig_InitPythonConfig(&preconfig);
-
- preconfig.utf8_mode = 1;
-
- status = Py_PreInitialize(&preconfig);
- if (PyStatus_Exception(status)) {
- Py_ExitStatusException(status);
- }
-
- /* at this point, Python will speak UTF-8 */
-
- Py_Initialize();
- /* ... use Python API here ... */
- Py_Finalize();
-
-
-PyConfig
---------
-
-.. c:type:: PyConfig
-
- Structure containing most parameters to configure Python.
-
- Structure methods:
-
- .. c:function:: void PyConfig_InitPythonConfig(PyConfig *config)
-
- Initialize configuration with :ref:`Python Configuration
- <init-python-config>`.
-
- .. c:function:: void PyConfig_InitIsolatedConfig(PyConfig *config)
-
- Initialize configuration with :ref:`Isolated Configuration
- <init-isolated-conf>`.
-
- .. c:function:: PyStatus PyConfig_SetString(PyConfig *config, wchar_t * const *config_str, const wchar_t *str)
-
- Copy the wide character string *str* into ``*config_str``.
-
- Preinitialize Python if needed.
-
- .. c:function:: PyStatus PyConfig_SetBytesString(PyConfig *config, wchar_t * const *config_str, const char *str)
-
- Decode *str* using ``Py_DecodeLocale()`` and set the result into ``*config_str``.
-
- Preinitialize Python if needed.
-
- .. c:function:: PyStatus PyConfig_SetArgv(PyConfig *config, int argc, wchar_t * const *argv)
-
- Set command line arguments from wide character strings.
-
- Preinitialize Python if needed.
-
- .. c:function:: PyStatus PyConfig_SetBytesArgv(PyConfig *config, int argc, char * const *argv)
-
- Set command line arguments: decode bytes using :c:func:`Py_DecodeLocale`.
-
- Preinitialize Python if needed.
-
- .. c:function:: PyStatus PyConfig_SetWideStringList(PyConfig *config, PyWideStringList *list, Py_ssize_t length, wchar_t **items)
-
- Set the list of wide strings *list* to *length* and *items*.
-
- Preinitialize Python if needed.
-
- .. c:function:: PyStatus PyConfig_Read(PyConfig *config)
-
- Read all Python configuration.
-
- Fields which are already initialized are left unchanged.
-
- Preinitialize Python if needed.
-
- .. c:function:: void PyConfig_Clear(PyConfig *config)
-
- Release configuration memory.
-
- Most ``PyConfig`` methods preinitialize Python if needed. In that case, the
- Python preinitialization configuration in based on the :c:type:`PyConfig`.
- If configuration fields which are in common with :c:type:`PyPreConfig` are
- tuned, they must be set before calling a :c:type:`PyConfig` method:
-
- * :c:member:`~PyConfig.dev_mode`
- * :c:member:`~PyConfig.isolated`
- * :c:member:`~PyConfig.parse_argv`
- * :c:member:`~PyConfig.use_environment`
-
- Moreover, if :c:func:`PyConfig_SetArgv` or :c:func:`PyConfig_SetBytesArgv`
- is used, this method must be called first, before other methods, since the
- preinitialization configuration depends on command line arguments (if
- :c:member:`parse_argv` is non-zero).
-
- The caller of these methods is responsible to handle exceptions (error or
- exit) using ``PyStatus_Exception()`` and ``Py_ExitStatusException()``.
-
- Structure fields:
-
- .. c:member:: PyWideStringList argv
-
- Command line arguments, :data:`sys.argv`. See
- :c:member:`~PyConfig.parse_argv` to parse :c:member:`~PyConfig.argv` the
- same way the regular Python parses Python command line arguments. If
- :c:member:`~PyConfig.argv` is empty, an empty string is added to ensure
- that :data:`sys.argv` always exists and is never empty.
-
- .. c:member:: wchar_t* base_exec_prefix
-
- :data:`sys.base_exec_prefix`.
-
- .. c:member:: wchar_t* base_executable
-
- :data:`sys._base_executable`: ``__PYVENV_LAUNCHER__`` environment
- variable value, or copy of :c:member:`PyConfig.executable`.
-
- .. c:member:: wchar_t* base_prefix
-
- :data:`sys.base_prefix`.
-
- .. c:member:: int buffered_stdio
-
- If equals to 0, enable unbuffered mode, making the stdout and stderr
- streams unbuffered.
-
- stdin is always opened in buffered mode.
-
- .. c:member:: int bytes_warning
-
- If equals to 1, issue a warning when comparing :class:`bytes` or
- :class:`bytearray` with :class:`str`, or comparing :class:`bytes` with
- :class:`int`. If equal or greater to 2, raise a :exc:`BytesWarning`
- exception.
-
- .. c:member:: wchar_t* check_hash_pycs_mode
-
- Control the validation behavior of hash-based ``.pyc`` files (see
- :pep:`552`): :option:`--check-hash-based-pycs` command line option value.
-
- Valid values: ``always``, ``never`` and ``default``.
-
- The default value is: ``default``.
-
- .. c:member:: int configure_c_stdio
-
- If non-zero, configure C standard streams (``stdio``, ``stdout``,
- ``stdout``). For example, set their mode to ``O_BINARY`` on Windows.
-
- .. c:member:: int dev_mode
-
- Development mode: see :option:`-X dev <-X>`.
-
- .. c:member:: int dump_refs
-
- If non-zero, dump all objects which are still alive at exit.
-
- Require a debug build of Python (``Py_REF_DEBUG`` macro must be defined).
-
- .. c:member:: wchar_t* exec_prefix
-
- :data:`sys.exec_prefix`.
-
- .. c:member:: wchar_t* executable
-
- :data:`sys.executable`.
-
- .. c:member:: int faulthandler
-
- If non-zero, call :func:`faulthandler.enable` at startup.
-
- .. c:member:: wchar_t* filesystem_encoding
-
- Filesystem encoding, :func:`sys.getfilesystemencoding`.
-
- .. c:member:: wchar_t* filesystem_errors
-
- Filesystem encoding errors, :func:`sys.getfilesystemencodeerrors`.
-
- .. c:member:: unsigned long hash_seed
- .. c:member:: int use_hash_seed
-
- Randomized hash function seed.
-
- If :c:member:`~PyConfig.use_hash_seed` is zero, a seed is chosen randomly
- at Pythonstartup, and :c:member:`~PyConfig.hash_seed` is ignored.
-
- .. c:member:: wchar_t* home
-
- Python home directory.
-
- Initialized from :envvar:`PYTHONHOME` environment variable value by
- default.
-
- .. c:member:: int import_time
-
- If non-zero, profile import time.
-
- .. c:member:: int inspect
-
- Enter interactive mode after executing a script or a command.
-
- .. c:member:: int install_signal_handlers
-
- Install signal handlers?
-
- .. c:member:: int interactive
-
- Interactive mode.
-
- .. c:member:: int isolated
-
- If greater than 0, enable isolated mode:
-
- * :data:`sys.path` contains neither the script's directory (computed from
- ``argv[0]`` or the current directory) nor the user's site-packages
- directory.
- * Python REPL doesn't import :mod:`readline` nor enable default readline
- configuration on interactive prompts.
- * Set :c:member:`~PyConfig.use_environment` and
- :c:member:`~PyConfig.user_site_directory` to 0.
-
- .. c:member:: int legacy_windows_stdio
-
- If non-zero, use :class:`io.FileIO` instead of
- :class:`io.WindowsConsoleIO` for :data:`sys.stdin`, :data:`sys.stdout`
- and :data:`sys.stderr`.
-
- Only available on Windows. ``#ifdef MS_WINDOWS`` macro can be used for
- Windows specific code.
-
- .. c:member:: int malloc_stats
-
- If non-zero, dump statistics on :ref:`Python pymalloc memory allocator
- <pymalloc>` at exit.
-
- The option is ignored if Python is built using ``--without-pymalloc``.
-
- .. c:member:: wchar_t* pythonpath_env
-
- Module search paths as a string separated by ``DELIM``
- (:data:`os.path.pathsep`).
-
- Initialized from :envvar:`PYTHONPATH` environment variable value by
- default.
-
- .. c:member:: PyWideStringList module_search_paths
- .. c:member:: int module_search_paths_set
-
- :data:`sys.path`. If :c:member:`~PyConfig.module_search_paths_set` is
- equal to 0, the :c:member:`~PyConfig.module_search_paths` is overridden
- by the function calculating the :ref:`Path Configuration
- <init-path-config>`.
-
- .. c:member:: int optimization_level
-
- Compilation optimization level:
-
- * 0: Peephole optimizer (and ``__debug__`` is set to ``True``)
- * 1: Remove assertions, set ``__debug__`` to ``False``
- * 2: Strip docstrings
-
- .. c:member:: int parse_argv
-
- If non-zero, parse :c:member:`~PyConfig.argv` the same way the regular
- Python command line arguments, and strip Python arguments from
- :c:member:`~PyConfig.argv`: see :ref:`Command Line Arguments
- <using-on-cmdline>`.
-
- .. c:member:: int parser_debug
-
- If non-zero, turn on parser debugging output (for expert only, depending
- on compilation options).
-
- .. c:member:: int pathconfig_warnings
-
- If equal to 0, suppress warnings when calculating the :ref:`Path
- Configuration <init-path-config>` (Unix only, Windows does not log any
- warning). Otherwise, warnings are written into ``stderr``.
-
- .. c:member:: wchar_t* prefix
-
- :data:`sys.prefix`.
-
- .. c:member:: wchar_t* program_name
-
- Program name. Used to initialize :c:member:`~PyConfig.executable`, and in
- early error messages.
-
- .. c:member:: wchar_t* pycache_prefix
-
- :data:`sys.pycache_prefix`: ``.pyc`` cache prefix.
-
- If ``NULL``, :data:`sys.pycache_prefix` is set to ``None``.
-
- .. c:member:: int quiet
-
- Quiet mode. For example, don't display the copyright and version messages
- in interactive mode.
-
- .. c:member:: wchar_t* run_command
-
- ``python3 -c COMMAND`` argument. Used by :c:func:`Py_RunMain`.
-
- .. c:member:: wchar_t* run_filename
-
- ``python3 FILENAME`` argument. Used by :c:func:`Py_RunMain`.
-
- .. c:member:: wchar_t* run_module
-
- ``python3 -m MODULE`` argument. Used by :c:func:`Py_RunMain`.
-
- .. c:member:: int show_alloc_count
-
- Show allocation counts at exit?
-
- Set to 1 by :option:`-X showalloccount <-X>` command line option.
-
- Need a special Python build with ``COUNT_ALLOCS`` macro defined.
-
- .. c:member:: int show_ref_count
-
- Show total reference count at exit?
-
- Set to 1 by :option:`-X showrefcount <-X>` command line option.
-
- Need a debug build of Python (``Py_REF_DEBUG`` macro must be defined).
-
- .. c:member:: int site_import
-
- Import the :mod:`site` module at startup?
-
- .. c:member:: int skip_source_first_line
-
- Skip the first line of the source?
-
- .. c:member:: wchar_t* stdio_encoding
- .. c:member:: wchar_t* stdio_errors
-
- Encoding and encoding errors of :data:`sys.stdin`, :data:`sys.stdout` and
- :data:`sys.stderr`.
-
- .. c:member:: int tracemalloc
-
- If non-zero, call :func:`tracemalloc.start` at startup.
-
- .. c:member:: int use_environment
-
- If greater than 0, use :ref:`environment variables <using-on-envvars>`.
-
- .. c:member:: int user_site_directory
-
- If non-zero, add user site directory to :data:`sys.path`.
-
- .. c:member:: int verbose
-
- If non-zero, enable verbose mode.
-
- .. c:member:: PyWideStringList warnoptions
-
- :data:`sys.warnoptions`: options of the :mod:`warnings` module to build
- warnings filters: lowest to highest priority.
-
- The :mod:`warnings` module adds :data:`sys.warnoptions` in the reverse
- order: the last :c:member:`PyConfig.warnoptions` item becomes the first
- item of :data:`warnings.filters` which is checked first (highest
- priority).
-
- .. c:member:: int write_bytecode
-
- If non-zero, write ``.pyc`` files.
-
- :data:`sys.dont_write_bytecode` is initialized to the inverted value of
- :c:member:`~PyConfig.write_bytecode`.
-
- .. c:member:: PyWideStringList xoptions
-
- :data:`sys._xoptions`.
-
-If ``parse_argv`` is non-zero, ``argv`` arguments are parsed the same
-way the regular Python parses command line arguments, and Python
-arguments are stripped from ``argv``: see :ref:`Command Line Arguments
-<using-on-cmdline>`.
-
-The ``xoptions`` options are parsed to set other options: see :option:`-X`
-option.
-
-
-Initialization with PyConfig
-----------------------------
-
-Function to initialize Python:
-
-.. c:function:: PyStatus Py_InitializeFromConfig(const PyConfig *config)
-
- Initialize Python from *config* configuration.
-
-The caller is responsible to handle exceptions (error or exit) using
-:c:func:`PyStatus_Exception` and :c:func:`Py_ExitStatusException`.
-
-If ``PyImport_FrozenModules``, ``PyImport_AppendInittab()`` or
-``PyImport_ExtendInittab()`` are used, they must be set or called after Python
-preinitialization and before the Python initialization.
-
-Example setting the program name::
-
- void init_python(void)
- {
- PyStatus status;
-
- PyConfig config;
- PyConfig_InitPythonConfig(&config);
-
- /* Set the program name. Implicitly preinitialize Python. */
- status = PyConfig_SetString(&config, &config.program_name,
- L"/path/to/my_program");
- if (PyStatus_Exception(status)) {
- goto fail;
- }
-
- status = Py_InitializeFromConfig(&config);
- if (PyStatus_Exception(status)) {
- goto fail;
- }
- PyConfig_Clear(&config);
- return;
-
- fail:
- PyConfig_Clear(&config);
- Py_ExitStatusException(status);
- }
-
-More complete example modifying the default configuration, read the
-configuration, and then override some parameters::
-
- PyStatus init_python(const char *program_name)
- {
- PyStatus status;
-
- PyConfig config;
- PyConfig_InitPythonConfig(&config);
-
- /* Set the program name before reading the configuraton
- (decode byte string from the locale encoding).
-
- Implicitly preinitialize Python. */
- status = PyConfig_SetBytesString(&config, &config.program_name,
- program_name);
- if (PyStatus_Exception(status)) {
- goto done;
- }
-
- /* Read all configuration at once */
- status = PyConfig_Read(&config);
- if (PyStatus_Exception(status)) {
- goto done;
- }
-
- /* Append our custom search path to sys.path */
- status = PyWideStringList_Append(&config.module_search_paths,
- L"/path/to/more/modules");
- if (PyStatus_Exception(status)) {
- goto done;
- }
-
- /* Override executable computed by PyConfig_Read() */
- status = PyConfig_SetString(&config, &config.executable,
- L"/path/to/my_executable");
- if (PyStatus_Exception(status)) {
- goto done;
- }
-
- status = Py_InitializeFromConfig(&config);
-
- done:
- PyConfig_Clear(&config);
- return status;
- }
-
-
-.. _init-isolated-conf:
-
-Isolated Configuration
-----------------------
-
-:c:func:`PyPreConfig_InitIsolatedConfig` and
-:c:func:`PyConfig_InitIsolatedConfig` functions create a configuration to
-isolate Python from the system. For example, to embed Python into an
-application.
-
-This configuration ignores global configuration variables, environments
-variables, command line arguments (:c:member:`PyConfig.argv` is not parsed)
-and user site directory. The C standard streams (ex: ``stdout``) and the
-LC_CTYPE locale are left unchanged. Signal handlers are not installed.
-
-Configuration files are still used with this configuration. Set the
-:ref:`Path Configuration <init-path-config>` ("output fields") to ignore these
-configuration files and avoid the function computing the default path
-configuration.
-
-
-.. _init-python-config:
-
-Python Configuration
---------------------
-
-:c:func:`PyPreConfig_InitPythonConfig` and :c:func:`PyConfig_InitPythonConfig`
-functions create a configuration to build a customized Python which behaves as
-the regular Python.
-
-Environments variables and command line arguments are used to configure
-Python, whereas global configuration variables are ignored.
-
-This function enables C locale coercion (:pep:`538`) and UTF-8 Mode
-(:pep:`540`) depending on the LC_CTYPE locale, :envvar:`PYTHONUTF8` and
-:envvar:`PYTHONCOERCECLOCALE` environment variables.
-
-Example of customized Python always running in isolated mode::
-
- int main(int argc, char **argv)
- {
- PyStatus status;
-
- PyConfig config;
- PyConfig_InitPythonConfig(&config);
- config.isolated = 1;
-
- /* Decode command line arguments.
- Implicitly preinitialize Python (in isolated mode). */
- status = PyConfig_SetBytesArgv(&config, argc, argv);
- if (PyStatus_Exception(status)) {
- goto fail;
- }
-
- status = Py_InitializeFromConfig(&config);
- if (PyStatus_Exception(status)) {
- goto fail;
- }
- PyConfig_Clear(&config);
-
- return Py_RunMain();
-
- fail:
- PyConfig_Clear(&config);
- if (PyStatus_IsExit(status)) {
- return status.exitcode;
- }
- /* Display the error message and exit the process with
- non-zero exit code */
- Py_ExitStatusException(status);
- }
-
-
-.. _init-path-config:
-
-Path Configuration
-------------------
-
-:c:type:`PyConfig` contains multiple fields for the path configuration:
-
-* Path configuration inputs:
-
- * :c:member:`PyConfig.home`
- * :c:member:`PyConfig.pathconfig_warnings`
- * :c:member:`PyConfig.program_name`
- * :c:member:`PyConfig.pythonpath_env`
- * current working directory: to get absolute paths
- * ``PATH`` environment variable to get the program full path
- (from :c:member:`PyConfig.program_name`)
- * ``__PYVENV_LAUNCHER__`` environment variable
- * (Windows only) Application paths in the registry under
- "Software\Python\PythonCore\X.Y\PythonPath" of HKEY_CURRENT_USER and
- HKEY_LOCAL_MACHINE (where X.Y is the Python version).
-
-* Path configuration output fields:
-
- * :c:member:`PyConfig.base_exec_prefix`
- * :c:member:`PyConfig.base_executable`
- * :c:member:`PyConfig.base_prefix`
- * :c:member:`PyConfig.exec_prefix`
- * :c:member:`PyConfig.executable`
- * :c:member:`PyConfig.module_search_paths_set`,
- :c:member:`PyConfig.module_search_paths`
- * :c:member:`PyConfig.prefix`
-
-If at least one "output field" is not set, Python calculates the path
-configuration to fill unset fields. If
-:c:member:`~PyConfig.module_search_paths_set` is equal to 0,
-:c:member:`~PyConfig.module_search_paths` is overridden and
-:c:member:`~PyConfig.module_search_paths_set` is set to 1.
-
-It is possible to completely ignore the function calculating the default
-path configuration by setting explicitly all path configuration output
-fields listed above. A string is considered as set even if it is non-empty.
-``module_search_paths`` is considered as set if
-``module_search_paths_set`` is set to 1. In this case, path
-configuration input fields are ignored as well.
-
-Set :c:member:`~PyConfig.pathconfig_warnings` to 0 to suppress warnings when
-calculating the path configuration (Unix only, Windows does not log any warning).
-
-If :c:member:`~PyConfig.base_prefix` or :c:member:`~PyConfig.base_exec_prefix`
-fields are not set, they inherit their value from :c:member:`~PyConfig.prefix`
-and :c:member:`~PyConfig.exec_prefix` respectively.
-
-:c:func:`Py_RunMain` and :c:func:`Py_Main` modify :data:`sys.path`:
-
-* If :c:member:`~PyConfig.run_filename` is set and is a directory which contains a
- ``__main__.py`` script, prepend :c:member:`~PyConfig.run_filename` to
- :data:`sys.path`.
-* If :c:member:`~PyConfig.isolated` is zero:
-
- * If :c:member:`~PyConfig.run_module` is set, prepend the current directory
- to :data:`sys.path`. Do nothing if the current directory cannot be read.
- * If :c:member:`~PyConfig.run_filename` is set, prepend the directory of the
- filename to :data:`sys.path`.
- * Otherwise, prepend an empty string to :data:`sys.path`.
-
-If :c:member:`~PyConfig.site_import` is non-zero, :data:`sys.path` can be
-modified by the :mod:`site` module. If
-:c:member:`~PyConfig.user_site_directory` is non-zero and the user's
-site-package directory exists, the :mod:`site` module appends the user's
-site-package directory to :data:`sys.path`.
-
-The following configuration files are used by the path configuration:
-
-* ``pyvenv.cfg``
-* ``python._pth`` (Windows only)
-* ``pybuilddir.txt`` (Unix only)
-
-The ``__PYVENV_LAUNCHER__`` environment variable is used to set
-:c:member:`PyConfig.base_executable`
-
-
-Py_RunMain()
-------------
-
-.. c:function:: int Py_RunMain(void)
-
- Execute the command (:c:member:`PyConfig.run_command`), the script
- (:c:member:`PyConfig.run_filename`) or the module
- (:c:member:`PyConfig.run_module`) specified on the command line or in the
- configuration.
-
- By default and when if :option:`-i` option is used, run the REPL.
-
- Finally, finalizes Python and returns an exit status that can be passed to
- the ``exit()`` function.
-
-See :ref:`Python Configuration <init-python-config>` for an example of
-customized Python always running in isolated mode using
-:c:func:`Py_RunMain`.
-
-
-Multi-Phase Initialization Private Provisional API
---------------------------------------------------
-
-This section is a private provisional API introducing multi-phase
-initialization, the core feature of the :pep:`432`:
-
-* "Core" initialization phase, "bare minimum Python":
-
- * Builtin types;
- * Builtin exceptions;
- * Builtin and frozen modules;
- * The :mod:`sys` module is only partially initialized
- (ex: :data:`sys.path` doesn't exist yet).
-
-* "Main" initialization phase, Python is fully initialized:
-
- * Install and configure :mod:`importlib`;
- * Apply the :ref:`Path Configuration <init-path-config>`;
- * Install signal handlers;
- * Finish :mod:`sys` module initialization (ex: create :data:`sys.stdout`
- and :data:`sys.path`);
- * Enable optional features like :mod:`faulthandler` and :mod:`tracemalloc`;
- * Import the :mod:`site` module;
- * etc.
-
-Private provisional API:
-
-* :c:member:`PyConfig._init_main`: if set to 0,
- :c:func:`Py_InitializeFromConfig` stops at the "Core" initialization phase.
-
-.. c:function:: PyStatus _Py_InitializeMain(void)
-
- Move to the "Main" initialization phase, finish the Python initialization.
-
-No module is imported during the "Core" phase and the ``importlib`` module is
-not configured: the :ref:`Path Configuration <init-path-config>` is only
-applied during the "Main" phase. It may allow to customize Python in Python to
-override or tune the :ref:`Path Configuration <init-path-config>`, maybe
-install a custom :data:`sys.meta_path` importer or an import hook, etc.
-
-It may become possible to calculatin the :ref:`Path Configuration
-<init-path-config>` in Python, after the Core phase and before the Main phase,
-which is one of the :pep:`432` motivation.
-
-The "Core" phase is not properly defined: what should be and what should
-not be available at this phase is not specified yet. The API is marked
-as private and provisional: the API can be modified or even be removed
-anytime until a proper public API is designed.
-
-Example running Python code between "Core" and "Main" initialization
-phases::
-
- void init_python(void)
- {
- PyStatus status;
-
- PyConfig config;
- PyConfig_InitPythonConfig(&config);
- config._init_main = 0;
-
- /* ... customize 'config' configuration ... */
-
- status = Py_InitializeFromConfig(&config);
- PyConfig_Clear(&config);
- if (PyStatus_Exception(status)) {
- Py_ExitStatusException(status);
- }
-
- /* Use sys.stderr because sys.stdout is only created
- by _Py_InitializeMain() */
- int res = PyRun_SimpleString(
- "import sys; "
- "print('Run Python code before _Py_InitializeMain', "
- "file=sys.stderr)");
- if (res < 0) {
- exit(1);
- }
-
- /* ... put more configuration code here ... */
-
- status = _Py_InitializeMain();
- if (PyStatus_Exception(status)) {
- Py_ExitStatusException(status);
- }
- }
diff --git a/Doc/c-api/int.rst b/Doc/c-api/int.rst
new file mode 100644
index 0000000..b2c5a44
--- /dev/null
+++ b/Doc/c-api/int.rst
@@ -0,0 +1,139 @@
+.. highlightlang:: c
+
+.. _intobjects:
+
+Plain Integer Objects
+---------------------
+
+.. index:: object: integer
+
+
+.. c:type:: PyIntObject
+
+ This subtype of :c:type:`PyObject` represents a Python integer object.
+
+
+.. c:var:: PyTypeObject PyInt_Type
+
+ .. index:: single: IntType (in modules types)
+
+ This instance of :c:type:`PyTypeObject` represents the Python plain integer type.
+ This is the same object as ``int`` and ``types.IntType``.
+
+
+.. c:function:: int PyInt_Check(PyObject *o)
+
+ Return true if *o* is of type :c:data:`PyInt_Type` or a subtype of
+ :c:data:`PyInt_Type`.
+
+ .. versionchanged:: 2.2
+ Allowed subtypes to be accepted.
+
+
+.. c:function:: int PyInt_CheckExact(PyObject *o)
+
+ Return true if *o* is of type :c:data:`PyInt_Type`, but not a subtype of
+ :c:data:`PyInt_Type`.
+
+ .. versionadded:: 2.2
+
+
+.. c:function:: PyObject* PyInt_FromString(char *str, char **pend, int base)
+
+ Return a new :c:type:`PyIntObject` or :c:type:`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, :exc:`ValueError` will be raised. If the string represents
+ a number too large to be contained within the machine's :c:type:`long int` type
+ and overflow warnings are being suppressed, a :c:type:`PyLongObject` will be
+ returned. If overflow warnings are not being suppressed, *NULL* will be
+ returned in this case.
+
+
+.. c:function:: PyObject* PyInt_FromLong(long ival)
+
+ 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. :-)
+
+
+.. c:function:: PyObject* PyInt_FromSsize_t(Py_ssize_t ival)
+
+ 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.
+
+ .. versionadded:: 2.5
+
+
+.. c:function:: 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.
+
+ .. versionadded:: 2.5
+
+
+.. c:function:: long PyInt_AsLong(PyObject *io)
+
+ Will first attempt to cast the object to a :c:type:`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``.
+
+
+.. c:function:: long PyInt_AS_LONG(PyObject *io)
+
+ Return the value of the object *io*. No error checking is performed.
+
+
+.. c:function:: unsigned long PyInt_AsUnsignedLongMask(PyObject *io)
+
+ Will first attempt to cast the object to a :c:type:`PyIntObject` or
+ :c:type:`PyLongObject`, if it is not already one, and then return its value as
+ unsigned long. This function does not check for overflow.
+
+ .. versionadded:: 2.3
+
+
+.. c:function:: unsigned PY_LONG_LONG PyInt_AsUnsignedLongLongMask(PyObject *io)
+
+ Will first attempt to cast the object to a :c:type:`PyIntObject` or
+ :c:type:`PyLongObject`, if it is not already one, and then return its value as
+ unsigned long long, without checking for overflow.
+
+ .. versionadded:: 2.3
+
+
+.. c:function:: Py_ssize_t PyInt_AsSsize_t(PyObject *io)
+
+ Will first attempt to cast the object to a :c:type:`PyIntObject` or
+ :c:type:`PyLongObject`, if it is not already one, and then return its value as
+ :c:type:`Py_ssize_t`.
+
+ .. versionadded:: 2.5
+
+
+.. c:function:: long PyInt_GetMax()
+
+ .. index:: single: LONG_MAX
+
+ Return the system's idea of the largest integer it can handle
+ (:const:`LONG_MAX`, as defined in the system header files).
+
+
+.. c:function:: int PyInt_ClearFreeList()
+
+ Clear the integer free list. Return the number of items that could not
+ be freed.
+
+ .. versionadded:: 2.6
diff --git a/Doc/c-api/intro.rst b/Doc/c-api/intro.rst
index d08d4f9..fa2d9f0 100644
--- a/Doc/c-api/intro.rst
+++ b/Doc/c-api/intro.rst
@@ -1,4 +1,4 @@
-.. highlight:: c
+.. highlightlang:: c
.. _api-intro:
@@ -17,11 +17,11 @@ common use. The second reason is to use Python as a component in a larger
application; this technique is generally referred to as :dfn:`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.
+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
@@ -30,16 +30,6 @@ familiar with writing an extension before attempting to embed Python in a real
application.
-Coding standards
-================
-
-If you're writing C code for inclusion in CPython, you **must** follow the
-guidelines and standards defined in :PEP:`7`. These guidelines apply
-regardless of the version of Python you are contributing to. Following these
-conventions is not necessary for your own third party extension modules,
-unless you eventually expect to contribute them to Python.
-
-
.. _api-includes:
Include Files
@@ -48,8 +38,7 @@ Include Files
All function, type and macro definitions needed to use the Python/C API are
included in your code by the following line::
- #define PY_SSIZE_T_CLEAN
- #include <Python.h>
+ #include "Python.h"
This implies inclusion of the following standard headers: ``<stdio.h>``,
``<string.h>``, ``<errno.h>``, ``<limits.h>``, ``<assert.h>`` and ``<stdlib.h>``
@@ -61,29 +50,23 @@ This implies inclusion of the following standard headers: ``<stdio.h>``,
headers on some systems, you *must* include :file:`Python.h` before any standard
headers are included.
- It is recommended to always define ``PY_SSIZE_T_CLEAN`` before including
- ``Python.h``. See :ref:`arg-parsing` for a description of this macro.
-
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.
-.. note::
-
- 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.
+**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 :file:`{prefix}/include/pythonversion/` and
:file:`{exec_prefix}/include/pythonversion/`, where :envvar:`prefix` and
:envvar:`exec_prefix` are defined by the corresponding parameters to Python's
-:program:`configure` script and *version* is
-``'%d.%d' % sys.version_info[:2]``. On Windows, the headers are installed
-in :file:`{prefix}/include`, where :envvar:`prefix` is the installation
-directory specified to the installer.
+:program:`configure` script and *version* is ``sys.version[:3]``. On Windows,
+the headers are installed in :file:`{prefix}/include`, where :envvar:`prefix` is
+the installation directory specified to the installer.
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
@@ -92,87 +75,9 @@ multi-platform builds since the platform independent headers under
:envvar:`prefix` include the platform specific headers from
:envvar:`exec_prefix`.
-C++ users should note that although the API is defined entirely using C, the
-header files properly declare the entry points to be ``extern "C"``. As a result,
-there is no need to do anything special to use the API from C++.
-
-
-Useful macros
-=============
-
-Several useful macros are defined in the Python header files. Many are
-defined closer to where they are useful (e.g. :c:macro:`Py_RETURN_NONE`).
-Others of a more general utility are defined here. This is not necessarily a
-complete listing.
-
-.. c:macro:: Py_UNREACHABLE()
-
- Use this when you have a code path that you do not expect to be reached.
- For example, in the ``default:`` clause in a ``switch`` statement for which
- all possible values are covered in ``case`` statements. Use this in places
- where you might be tempted to put an ``assert(0)`` or ``abort()`` call.
-
- .. versionadded:: 3.7
-
-.. c:macro:: Py_ABS(x)
-
- Return the absolute value of ``x``.
-
- .. versionadded:: 3.3
-
-.. c:macro:: Py_MIN(x, y)
-
- Return the minimum value between ``x`` and ``y``.
-
- .. versionadded:: 3.3
-
-.. c:macro:: Py_MAX(x, y)
-
- Return the maximum value between ``x`` and ``y``.
-
- .. versionadded:: 3.3
-
-.. c:macro:: Py_STRINGIFY(x)
-
- Convert ``x`` to a C string. E.g. ``Py_STRINGIFY(123)`` returns
- ``"123"``.
-
- .. versionadded:: 3.4
-
-.. c:macro:: Py_MEMBER_SIZE(type, member)
-
- Return the size of a structure (``type``) ``member`` in bytes.
-
- .. versionadded:: 3.6
-
-.. c:macro:: Py_CHARMASK(c)
-
- Argument must be a character or an integer in the range [-128, 127] or [0,
- 255]. This macro returns ``c`` cast to an ``unsigned char``.
-
-.. c:macro:: Py_GETENV(s)
-
- Like ``getenv(s)``, but returns ``NULL`` if :option:`-E` was passed on the
- command line (i.e. if ``Py_IgnoreEnvironmentFlag`` is set).
-
-.. c:macro:: Py_UNUSED(arg)
-
- Use this for unused arguments in a function definition to silence compiler
- warnings. Example: ``int func(int a, int Py_UNUSED(b)) { return a; }``.
-
- .. versionadded:: 3.4
-
-.. c:macro:: Py_DEPRECATED(version)
-
- Use this for deprecated declarations. The macro must be placed before the
- symbol name.
-
- Example::
-
- Py_DEPRECATED(3.8) PyAPI_FUNC(int) Py_OldFunction(void);
-
- .. versionchanged:: 3.8
- MSVC support was added.
+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++.
.. _api-objects:
@@ -252,7 +157,7 @@ duration of the call.
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 possibly deallocating it. The real danger is that innocent-looking
+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 :c:func:`Py_DECREF`, so
almost any operation is potentially dangerous.
@@ -303,11 +208,11 @@ error handling for the moment; a better way to code this is shown below)::
PyObject *t;
t = PyTuple_New(3);
- PyTuple_SetItem(t, 0, PyLong_FromLong(1L));
- PyTuple_SetItem(t, 1, PyLong_FromLong(2L));
- PyTuple_SetItem(t, 2, PyUnicode_FromString("three"));
+ PyTuple_SetItem(t, 0, PyInt_FromLong(1L));
+ PyTuple_SetItem(t, 1, PyInt_FromLong(2L));
+ PyTuple_SetItem(t, 2, PyString_FromString("three"));
-Here, :c:func:`PyLong_FromLong` returns a new reference which is immediately
+Here, :c:func:`PyInt_FromLong` returns a new reference which is immediately
stolen by :c:func:`PyTuple_SetItem`. When you want to keep using an object
although the reference to it will be stolen, use :c:func:`Py_INCREF` to grab
another reference before calling the reference-stealing function.
@@ -341,13 +246,13 @@ sets all items of a list (actually, any mutable sequence) to a given item::
int
set_all(PyObject *target, PyObject *item)
{
- Py_ssize_t i, n;
+ int i, n;
n = PyObject_Length(target);
if (n < 0)
return -1;
for (i = 0; i < n; i++) {
- PyObject *index = PyLong_FromSsize_t(i);
+ PyObject *index = PyInt_FromLong(i);
if (!index)
return -1;
if (PyObject_SetItem(target, index, item) < 0) {
@@ -389,8 +294,8 @@ using :c:func:`PySequence_GetItem`. ::
long
sum_list(PyObject *list)
{
- Py_ssize_t i, n;
- long total = 0, value;
+ int i, n;
+ long total = 0;
PyObject *item;
n = PyList_Size(list);
@@ -398,12 +303,8 @@ using :c:func:`PySequence_GetItem`. ::
return -1; /* Not a list */
for (i = 0; i < n; i++) {
item = PyList_GetItem(list, i); /* Can't fail */
- if (!PyLong_Check(item)) continue; /* Skip non-integers */
- value = PyLong_AsLong(item);
- if (value == -1 && PyErr_Occurred())
- /* Integer too big to fit in a C long, bail out */
- return -1;
- total += value;
+ if (!PyInt_Check(item)) continue; /* Skip non-integers */
+ total += PyInt_AsLong(item);
}
return total;
}
@@ -415,8 +316,8 @@ using :c:func:`PySequence_GetItem`. ::
long
sum_sequence(PyObject *sequence)
{
- Py_ssize_t i, n;
- long total = 0, value;
+ int i, n;
+ long total = 0;
PyObject *item;
n = PySequence_Length(sequence);
if (n < 0)
@@ -425,17 +326,9 @@ using :c:func:`PySequence_GetItem`. ::
item = PySequence_GetItem(sequence, i);
if (item == NULL)
return -1; /* Not a sequence, or other failure */
- if (PyLong_Check(item)) {
- value = PyLong_AsLong(item);
- Py_DECREF(item);
- if (value == -1 && PyErr_Occurred())
- /* Integer too big to fit in a C long, bail out */
- return -1;
- total += value;
- }
- else {
- Py_DECREF(item); /* Discard reference ownership */
- }
+ if (PyInt_Check(item))
+ total += PyInt_AsLong(item);
+ Py_DECREF(item); /* Discard reference ownership */
}
return total;
}
@@ -475,7 +368,7 @@ 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.
+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
@@ -490,20 +383,25 @@ using global storage in an unthreaded application). A thread can be in one of
two states: an exception has occurred, or not. The function
:c:func:`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 exception state:
+*NULL* otherwise. There are a number of functions to set the exception state:
:c:func:`PyErr_SetString` is the most common (though not the most general)
function to set the exception state, and :c:func:`PyErr_Clear` clears the
exception state.
+.. index::
+ single: exc_type (in module sys)
+ single: exc_value (in module sys)
+ single: exc_traceback (in module sys)
+
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 result of
-``sys.exc_info()``; however, they are not the same: the Python objects represent
-the last exception being handled by a Python :keyword:`try` ...
-:keyword:`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_info()`` and friends.
+*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 :keyword:`try` ... :keyword:`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.
.. index:: single: exc_info() (in module sys)
@@ -559,11 +457,11 @@ Here is the corresponding C code, in all its glory::
/* Clear the error and use zero: */
PyErr_Clear();
- item = PyLong_FromLong(0L);
+ item = PyInt_FromLong(0L);
if (item == NULL)
goto error;
}
- const_one = PyLong_FromLong(1L);
+ const_one = PyInt_FromLong(1L);
if (const_one == NULL)
goto error;
@@ -597,10 +495,10 @@ Here is the corresponding C code, in all its glory::
This example represents an endorsed use of the ``goto`` statement in C!
It illustrates the use of :c:func:`PyErr_ExceptionMatches` and
:c:func:`PyErr_Clear` to handle specific exceptions, and the use of
-:c:func:`Py_XDECREF` to dispose of owned references that may be ``NULL`` (note the
+:c:func:`Py_XDECREF` to dispose of owned references that may be *NULL* (note the
``'X'`` in the name; :c:func:`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
+*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.
@@ -617,15 +515,16 @@ interpreter can only be used after the interpreter has been initialized.
.. index::
single: Py_Initialize()
- module: builtins
+ module: __builtin__
module: __main__
module: sys
+ module: exceptions
triple: module; search; path
single: path (in module sys)
The basic initialization function is :c:func:`Py_Initialize`. This initializes
the table of loaded modules, and creates the fundamental modules
-:mod:`builtins`, :mod:`__main__`, and :mod:`sys`. It also
+:mod:`__builtin__`, :mod:`__main__`, :mod:`sys`, and :mod:`exceptions`. It also
initializes the module search path (``sys.path``).
.. index:: single: PySys_SetArgvEx()
@@ -673,9 +572,9 @@ Sometimes, it is desirable to "uninitialize" Python. For instance, the
application may want to start over (make another call to
:c:func:`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 :c:func:`Py_FinalizeEx`. The function :c:func:`Py_IsInitialized` returns
+by calling :c:func:`Py_Finalize`. The function :c:func:`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 :c:func:`Py_FinalizeEx`
+these functions is given in a later chapter. Notice that :c:func:`Py_Finalize`
does *not* free all memory allocated by the Python interpreter, e.g. memory
allocated by extension modules currently cannot be released.
@@ -716,7 +615,7 @@ extra checks are performed:
* Sanity checks of the input arguments are added to frame creation.
-* The storage for ints is initialized with a known invalid pattern to catch
+* The storage for long ints is initialized with a known invalid pattern to catch
reference to uninitialized digits.
* Low-level tracing and extra exception checking are added to the runtime
diff --git a/Doc/c-api/iter.rst b/Doc/c-api/iter.rst
index 0224d37..4f70882 100644
--- a/Doc/c-api/iter.rst
+++ b/Doc/c-api/iter.rst
@@ -1,23 +1,30 @@
-.. highlight:: c
+.. highlightlang:: c
.. _iterator:
Iterator Protocol
=================
+.. versionadded:: 2.2
+
There are two functions specifically for working with iterators.
+
.. c:function:: int PyIter_Check(PyObject *o)
Return true if the object *o* supports the iterator protocol.
+ This function can return a false positive in the case of old-style
+ classes because those classes always define a :c:member:`tp_iternext`
+ slot with logic that either invokes a :meth:`next` method or raises
+ a :exc:`TypeError`.
.. c:function:: PyObject* PyIter_Next(PyObject *o)
Return the next value from the iteration *o*. The object must be an iterator
(it is up to the caller to check this). If there are no remaining values,
- returns ``NULL`` with no exception set. If an error occurs while retrieving
- the item, returns ``NULL`` and passes along the exception.
+ returns *NULL* with no exception set. If an error occurs while retrieving
+ the item, returns *NULL* and passes along the exception.
To write a loop which iterates over an iterator, the C code should look
something like this::
@@ -29,7 +36,7 @@ something like this::
/* propagate error */
}
- while (item = PyIter_Next(iterator)) {
+ while ((item = PyIter_Next(iterator))) {
/* do something with item */
...
/* release reference when done */
diff --git a/Doc/c-api/iterator.rst b/Doc/c-api/iterator.rst
index 4d91e4a..3931bb9 100644
--- a/Doc/c-api/iterator.rst
+++ b/Doc/c-api/iterator.rst
@@ -1,4 +1,4 @@
-.. highlight:: c
+.. highlightlang:: c
.. _iterator-objects:
@@ -18,11 +18,15 @@ sentinel value is returned.
one-argument form of the :func:`iter` built-in function for built-in sequence
types.
+ .. versionadded:: 2.2
+
.. c:function:: int PySeqIter_Check(op)
Return true if the type of *op* is :c:data:`PySeqIter_Type`.
+ .. versionadded:: 2.2
+
.. c:function:: PyObject* PySeqIter_New(PyObject *seq)
@@ -30,17 +34,23 @@ sentinel value is returned.
iteration ends when the sequence raises :exc:`IndexError` for the subscripting
operation.
+ .. versionadded:: 2.2
+
.. c:var:: PyTypeObject PyCallIter_Type
Type object for iterator objects returned by :c:func:`PyCallIter_New` and the
two-argument form of the :func:`iter` built-in function.
+ .. versionadded:: 2.2
+
.. c:function:: int PyCallIter_Check(op)
Return true if the type of *op* is :c:data:`PyCallIter_Type`.
+ .. versionadded:: 2.2
+
.. c:function:: PyObject* PyCallIter_New(PyObject *callable, PyObject *sentinel)
@@ -48,3 +58,5 @@ sentinel value is returned.
callable object that can be called with no parameters; each call to it should
return the next item in the iteration. When *callable* returns a value equal to
*sentinel*, the iteration will be terminated.
+
+ .. versionadded:: 2.2
diff --git a/Doc/c-api/list.rst b/Doc/c-api/list.rst
index b247cdf..c28d6b0 100644
--- a/Doc/c-api/list.rst
+++ b/Doc/c-api/list.rst
@@ -1,4 +1,4 @@
-.. highlight:: c
+.. highlightlang:: c
.. _listobjects:
@@ -15,8 +15,8 @@ List Objects
.. c:var:: PyTypeObject PyList_Type
- This instance of :c:type:`PyTypeObject` represents the Python list type.
- This is the same object as :class:`list` in the Python layer.
+ This instance of :c:type:`PyTypeObject` represents the Python list type. This
+ is the same object as ``list`` in the Python layer.
.. c:function:: int PyList_Check(PyObject *p)
@@ -24,16 +24,21 @@ List Objects
Return true if *p* is a list object or an instance of a subtype of the list
type.
+ .. versionchanged:: 2.2
+ Allowed subtypes to be accepted.
+
.. c:function:: int PyList_CheckExact(PyObject *p)
Return true if *p* is a list object, but not an instance of a subtype of
the list type.
+ .. versionadded:: 2.2
+
.. c:function:: PyObject* PyList_New(Py_ssize_t len)
- Return a new list of length *len* on success, or ``NULL`` on failure.
+ Return a new list of length *len* on success, or *NULL* on failure.
.. note::
@@ -42,6 +47,10 @@ List Objects
:c:func:`PySequence_SetItem` or expose the object to Python code before
setting all items to a real object with :c:func:`PyList_SetItem`.
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` for *size*. This might require
+ changes in your code for properly supporting 64-bit systems.
+
.. c:function:: Py_ssize_t PyList_Size(PyObject *list)
@@ -50,24 +59,40 @@ List Objects
Return the length of the list object in *list*; this is equivalent to
``len(list)`` on a list object.
+ .. versionchanged:: 2.5
+ This function returned an :c:type:`int`. This might require changes in
+ your code for properly supporting 64-bit systems.
+
.. c:function:: Py_ssize_t PyList_GET_SIZE(PyObject *list)
Macro form of :c:func:`PyList_Size` without error checking.
+ .. versionchanged:: 2.5
+ This macro returned an :c:type:`int`. This might require changes in your
+ code for properly supporting 64-bit systems.
+
.. c:function:: PyObject* PyList_GetItem(PyObject *list, Py_ssize_t index)
Return the object at position *index* in the list pointed to by *list*. The
position must be non-negative; indexing from the end of the list is not
supported. If *index* is out of bounds (<0 or >=len(list)),
- return ``NULL`` and set an :exc:`IndexError` exception.
+ return *NULL* and set an :exc:`IndexError` exception.
+
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` for *index*. This might require
+ changes in your code for properly supporting 64-bit systems.
.. c:function:: PyObject* PyList_GET_ITEM(PyObject *list, Py_ssize_t i)
Macro form of :c:func:`PyList_GetItem` without error checking.
+ .. versionchanged:: 2.5
+ This macro used an :c:type:`int` for *i*. This might require changes in
+ your code for properly supporting 64-bit systems.
+
.. c:function:: int PyList_SetItem(PyObject *list, Py_ssize_t index, PyObject *item)
@@ -80,6 +105,10 @@ List Objects
This function "steals" a reference to *item* and discards a reference to
an item already in the list at the affected position.
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` for *index*. This might require
+ changes in your code for properly supporting 64-bit systems.
+
.. c:function:: void PyList_SET_ITEM(PyObject *list, Py_ssize_t i, PyObject *o)
@@ -90,9 +119,13 @@ List Objects
This macro "steals" a reference to *item*, and, unlike
:c:func:`PyList_SetItem`, does *not* discard a reference to any item that
- is being replaced; any reference in *list* at position *i* will be
+ it being replaced; any reference in *list* at position *i* will be
leaked.
+ .. versionchanged:: 2.5
+ This macro used an :c:type:`int` for *i*. This might require
+ changes in your code for properly supporting 64-bit systems.
+
.. c:function:: int PyList_Insert(PyObject *list, Py_ssize_t index, PyObject *item)
@@ -100,6 +133,10 @@ List Objects
``0`` if successful; return ``-1`` and set an exception if unsuccessful.
Analogous to ``list.insert(index, item)``.
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` for *index*. This might require
+ changes in your code for properly supporting 64-bit systems.
+
.. c:function:: int PyList_Append(PyObject *list, PyObject *item)
@@ -111,18 +148,26 @@ List Objects
.. c:function:: PyObject* PyList_GetSlice(PyObject *list, Py_ssize_t low, Py_ssize_t high)
Return a list of the objects in *list* containing the objects *between* *low*
- and *high*. Return ``NULL`` and set an exception if unsuccessful. Analogous
+ and *high*. Return *NULL* and set an exception if unsuccessful. Analogous
to ``list[low:high]``. Indexing from the end of the list is not supported.
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` for *low* and *high*. This might
+ require changes in your code for properly supporting 64-bit systems.
+
.. c:function:: 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).
+ be *NULL*, indicating the assignment of an empty list (slice deletion).
Return ``0`` on success, ``-1`` on failure. Indexing from the end of the
list is not supported.
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` for *low* and *high*. This might
+ require changes in your code for properly supporting 64-bit systems.
+
.. c:function:: int PyList_Sort(PyObject *list)
@@ -142,10 +187,3 @@ List Objects
Return a new tuple object containing the contents of *list*; equivalent to
``tuple(list)``.
-
-
-.. c:function:: int PyList_ClearFreeList()
-
- Clear the free list. Return the total number of freed items.
-
- .. versionadded:: 3.3
diff --git a/Doc/c-api/long.rst b/Doc/c-api/long.rst
index c360104..4684d1b 100644
--- a/Doc/c-api/long.rst
+++ b/Doc/c-api/long.rst
@@ -1,27 +1,24 @@
-.. highlight:: c
+.. highlightlang:: c
.. _longobjects:
-Integer Objects
----------------
+Long Integer Objects
+--------------------
.. index:: object: long integer
- object: integer
-All integers are implemented as "long" integer objects of arbitrary size.
-
-On error, most ``PyLong_As*`` APIs return ``(return type)-1`` which cannot be
-distinguished from a number. Use :c:func:`PyErr_Occurred` to disambiguate.
.. c:type:: PyLongObject
- This subtype of :c:type:`PyObject` represents a Python integer object.
+ This subtype of :c:type:`PyObject` represents a Python long integer object.
.. c:var:: PyTypeObject PyLong_Type
- This instance of :c:type:`PyTypeObject` represents the Python integer type.
- This is the same object as :class:`int` in the Python layer.
+ .. index:: single: LongType (in modules types)
+
+ This instance of :c:type:`PyTypeObject` represents the Python long integer type.
+ This is the same object as ``long`` and ``types.LongType``.
.. c:function:: int PyLong_Check(PyObject *p)
@@ -29,186 +26,137 @@ distinguished from a number. Use :c:func:`PyErr_Occurred` to disambiguate.
Return true if its argument is a :c:type:`PyLongObject` or a subtype of
:c:type:`PyLongObject`.
+ .. versionchanged:: 2.2
+ Allowed subtypes to be accepted.
+
.. c:function:: int PyLong_CheckExact(PyObject *p)
Return true if its argument is a :c:type:`PyLongObject`, but not a subtype of
:c:type:`PyLongObject`.
+ .. versionadded:: 2.2
-.. c:function:: PyObject* PyLong_FromLong(long v)
- Return a new :c:type:`PyLongObject` object from *v*, or ``NULL`` on failure.
+.. c:function:: PyObject* PyLong_FromLong(long v)
- 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.
+ Return a new :c:type:`PyLongObject` object from *v*, or *NULL* on failure.
.. c:function:: PyObject* PyLong_FromUnsignedLong(unsigned long v)
Return a new :c:type:`PyLongObject` object from a C :c:type:`unsigned long`, or
- ``NULL`` on failure.
+ *NULL* on failure.
.. c:function:: PyObject* PyLong_FromSsize_t(Py_ssize_t v)
Return a new :c:type:`PyLongObject` object from a C :c:type:`Py_ssize_t`, or
- ``NULL`` on failure.
+ *NULL* on failure.
+
+ .. versionadded:: 2.6
.. c:function:: PyObject* PyLong_FromSize_t(size_t v)
Return a new :c:type:`PyLongObject` object from a C :c:type:`size_t`, or
- ``NULL`` on failure.
+ *NULL* on failure.
+
+ .. versionadded:: 2.6
-.. c:function:: PyObject* PyLong_FromLongLong(long long v)
+.. c:function:: PyObject* PyLong_FromLongLong(PY_LONG_LONG v)
- Return a new :c:type:`PyLongObject` object from a C :c:type:`long long`, or ``NULL``
+ Return a new :c:type:`PyLongObject` object from a C :c:type:`long long`, or *NULL*
on failure.
-.. c:function:: PyObject* PyLong_FromUnsignedLongLong(unsigned long long v)
+.. c:function:: PyObject* PyLong_FromUnsignedLongLong(unsigned PY_LONG_LONG v)
Return a new :c:type:`PyLongObject` object from a C :c:type:`unsigned long long`,
- or ``NULL`` on failure.
+ or *NULL* on failure.
.. c:function:: PyObject* PyLong_FromDouble(double v)
Return a new :c:type:`PyLongObject` object from the integer part of *v*, or
- ``NULL`` on failure.
+ *NULL* on failure.
-.. c:function:: PyObject* PyLong_FromString(const char *str, char **pend, int base)
+.. c:function:: PyObject* PyLong_FromString(char *str, char **pend, int base)
- Return a new :c:type:`PyLongObject` based on the string value in *str*, which
- is interpreted according to the radix in *base*. If *pend* is non-``NULL``,
+ Return a new :c:type:`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``, *str* is interpreted using
- the :ref:`integers` definition; in this case, leading zeros in a
- non-zero decimal number raises a :exc:`ValueError`. If *base* is not ``0``,
- it must be between ``2`` and ``36``, inclusive. Leading spaces and single
- underscores after a base specifier and between digits are ignored. If there
- are no digits, :exc:`ValueError` will be raised.
+ 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, :exc:`ValueError` will be raised.
.. c:function:: PyObject* PyLong_FromUnicode(Py_UNICODE *u, Py_ssize_t length, int base)
- Convert a sequence of Unicode digits to a Python integer value. The Unicode
- string is first encoded to a byte string using :c:func:`PyUnicode_EncodeDecimal`
- and then converted using :c:func:`PyLong_FromString`.
+ 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, :exc:`ValueError`
+ will be raised.
- .. deprecated-removed:: 3.3 4.0
- Part of the old-style :c:type:`Py_UNICODE` API; please migrate to using
- :c:func:`PyLong_FromUnicodeObject`.
+ .. versionadded:: 1.6
-
-.. c:function:: PyObject* PyLong_FromUnicodeObject(PyObject *u, int base)
-
- Convert a sequence of Unicode digits in the string *u* to a Python integer
- value. The Unicode string is first encoded to a byte string using
- :c:func:`PyUnicode_EncodeDecimal` and then converted using
- :c:func:`PyLong_FromString`.
-
- .. versionadded:: 3.3
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` for *length*. This might require
+ changes in your code for properly supporting 64-bit systems.
.. c:function:: PyObject* PyLong_FromVoidPtr(void *p)
- Create a Python integer from the pointer *p*. The pointer value can be
- retrieved from the resulting value using :c:func:`PyLong_AsVoidPtr`.
+ Create a Python integer or long integer from the pointer *p*. The pointer value
+ can be retrieved from the resulting value using :c:func:`PyLong_AsVoidPtr`.
+ .. versionadded:: 1.5.2
-.. XXX alias PyLong_AS_LONG (for now)
-.. c:function:: long PyLong_AsLong(PyObject *obj)
-
- .. index::
- single: LONG_MAX
- single: OverflowError (built-in exception)
-
- Return a C :c:type:`long` representation of *obj*. If *obj* is not an
- instance of :c:type:`PyLongObject`, first call its :meth:`__index__` or
- :meth:`__int__` method (if present) to convert it to a
- :c:type:`PyLongObject`.
-
- Raise :exc:`OverflowError` if the value of *obj* is out of range for a
- :c:type:`long`.
-
- Returns ``-1`` on error. Use :c:func:`PyErr_Occurred` to disambiguate.
-
- .. versionchanged:: 3.8
- Use :meth:`__index__` if available.
-
- .. deprecated:: 3.8
- Using :meth:`__int__` is deprecated.
-
-
-.. c:function:: long PyLong_AsLongAndOverflow(PyObject *obj, int *overflow)
-
- Return a C :c:type:`long` representation of *obj*. If *obj* is not an
- instance of :c:type:`PyLongObject`, first call its :meth:`__index__` or
- :meth:`__int__` method (if present) to convert it to a
- :c:type:`PyLongObject`.
-
- If the value of *obj* is greater than :const:`LONG_MAX` or less than
- :const:`LONG_MIN`, set *\*overflow* to ``1`` or ``-1``, respectively, and
- return ``-1``; otherwise, set *\*overflow* to ``0``. If any other exception
- occurs set *\*overflow* to ``0`` and return ``-1`` as usual.
+ .. versionchanged:: 2.5
+ If the integer is larger than LONG_MAX, a positive long integer is returned.
- Returns ``-1`` on error. Use :c:func:`PyErr_Occurred` to disambiguate.
- .. versionchanged:: 3.8
- Use :meth:`__index__` if available.
-
- .. deprecated:: 3.8
- Using :meth:`__int__` is deprecated.
-
-
-.. c:function:: long long PyLong_AsLongLong(PyObject *obj)
+.. c:function:: long PyLong_AsLong(PyObject *pylong)
.. index::
+ single: LONG_MAX
single: OverflowError (built-in exception)
- Return a C :c:type:`long long` representation of *obj*. If *obj* is not an
- instance of :c:type:`PyLongObject`, first call its :meth:`__index__` or
- :meth:`__int__` method (if present) to convert it to a
- :c:type:`PyLongObject`.
-
- Raise :exc:`OverflowError` if the value of *obj* is out of range for a
- :c:type:`long`.
+ Return a C :c:type:`long` representation of the contents of *pylong*. If
+ *pylong* is greater than :const:`LONG_MAX`, an :exc:`OverflowError` is raised
+ and ``-1`` will be returned.
- Returns ``-1`` on error. Use :c:func:`PyErr_Occurred` to disambiguate.
- .. versionchanged:: 3.8
- Use :meth:`__index__` if available.
+.. c:function:: long PyLong_AsLongAndOverflow(PyObject *pylong, int *overflow)
- .. deprecated:: 3.8
- Using :meth:`__int__` is deprecated.
+ Return a C :c:type:`long` representation of the contents of
+ *pylong*. If *pylong* is greater than :const:`LONG_MAX` or less
+ than :const:`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``.
+ .. versionadded:: 2.7
-.. c:function:: long long PyLong_AsLongLongAndOverflow(PyObject *obj, int *overflow)
- Return a C :c:type:`long long` representation of *obj*. If *obj* is not an
- instance of :c:type:`PyLongObject`, first call its :meth:`__index__` or
- :meth:`__int__` method (if present) to convert it to a
- :c:type:`PyLongObject`.
-
- If the value of *obj* is greater than :const:`LLONG_MAX` or less than
- :const:`LLONG_MIN`, set *\*overflow* to ``1`` or ``-1``, respectively,
- and return ``-1``; otherwise, set *\*overflow* to ``0``. If any other
- exception occurs set *\*overflow* to ``0`` and return ``-1`` as usual.
-
- Returns ``-1`` on error. Use :c:func:`PyErr_Occurred` to disambiguate.
+.. c:function:: PY_LONG_LONG PyLong_AsLongLongAndOverflow(PyObject *pylong, int *overflow)
- .. versionadded:: 3.2
+ Return a C :c:type:`long long` representation of the contents of
+ *pylong*. If *pylong* is greater than :const:`PY_LLONG_MAX` or less
+ than :const:`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``.
- .. versionchanged:: 3.8
- Use :meth:`__index__` if available.
-
- .. deprecated:: 3.8
- Using :meth:`__int__` is deprecated.
+ .. versionadded:: 2.7
.. c:function:: Py_ssize_t PyLong_AsSsize_t(PyObject *pylong)
@@ -217,13 +165,11 @@ distinguished from a number. Use :c:func:`PyErr_Occurred` to disambiguate.
single: PY_SSIZE_T_MAX
single: OverflowError (built-in exception)
- Return a C :c:type:`Py_ssize_t` representation of *pylong*. *pylong* must
- be an instance of :c:type:`PyLongObject`.
-
- Raise :exc:`OverflowError` if the value of *pylong* is out of range for a
- :c:type:`Py_ssize_t`.
+ Return a C :c:type:`Py_ssize_t` representation of the contents of *pylong*. If
+ *pylong* is greater than :const:`PY_SSIZE_T_MAX`, an :exc:`OverflowError` is raised
+ and ``-1`` will be returned.
- Returns ``-1`` on error. Use :c:func:`PyErr_Occurred` to disambiguate.
+ .. versionadded:: 2.6
.. c:function:: unsigned long PyLong_AsUnsignedLong(PyObject *pylong)
@@ -232,106 +178,79 @@ distinguished from a number. Use :c:func:`PyErr_Occurred` to disambiguate.
single: ULONG_MAX
single: OverflowError (built-in exception)
- Return a C :c:type:`unsigned long` representation of *pylong*. *pylong*
- must be an instance of :c:type:`PyLongObject`.
-
- Raise :exc:`OverflowError` if the value of *pylong* is out of range for a
- :c:type:`unsigned long`.
+ Return a C :c:type:`unsigned long` representation of the contents of *pylong*.
+ If *pylong* is greater than :const:`ULONG_MAX`, an :exc:`OverflowError` is
+ raised.
- Returns ``(unsigned long)-1`` on error.
- Use :c:func:`PyErr_Occurred` to disambiguate.
-
-.. c:function:: size_t PyLong_AsSize_t(PyObject *pylong)
+.. c:function:: PY_LONG_LONG PyLong_AsLongLong(PyObject *pylong)
.. index::
- single: SIZE_MAX
single: OverflowError (built-in exception)
- Return a C :c:type:`size_t` representation of *pylong*. *pylong* must be
- an instance of :c:type:`PyLongObject`.
-
- Raise :exc:`OverflowError` if the value of *pylong* is out of range for a
- :c:type:`size_t`.
+ Return a C :c:type:`long long` from a Python long integer. If
+ *pylong* cannot be represented as a :c:type:`long long`, an
+ :exc:`OverflowError` is raised and ``-1`` is returned.
- Returns ``(size_t)-1`` on error.
- Use :c:func:`PyErr_Occurred` to disambiguate.
+ .. versionadded:: 2.2
-.. c:function:: unsigned long long PyLong_AsUnsignedLongLong(PyObject *pylong)
+.. c:function:: unsigned PY_LONG_LONG PyLong_AsUnsignedLongLong(PyObject *pylong)
.. index::
single: OverflowError (built-in exception)
- Return a C :c:type:`unsigned long long` representation of *pylong*. *pylong*
- must be an instance of :c:type:`PyLongObject`.
-
- Raise :exc:`OverflowError` if the value of *pylong* is out of range for an
- :c:type:`unsigned long long`.
+ Return a C :c:type:`unsigned long long` from a Python long integer. If
+ *pylong* cannot be represented as an :c:type:`unsigned long long`, an
+ :exc:`OverflowError` is raised and ``(unsigned long long)-1`` is
+ returned.
- Returns ``(unsigned long long)-1`` on error.
- Use :c:func:`PyErr_Occurred` to disambiguate.
+ .. versionadded:: 2.2
- .. versionchanged:: 3.1
- A negative *pylong* now raises :exc:`OverflowError`, not :exc:`TypeError`.
+ .. versionchanged:: 2.7
+ A negative *pylong* now raises :exc:`OverflowError`, not
+ :exc:`TypeError`.
-.. c:function:: unsigned long PyLong_AsUnsignedLongMask(PyObject *obj)
+.. c:function:: unsigned long PyLong_AsUnsignedLongMask(PyObject *io)
- Return a C :c:type:`unsigned long` representation of *obj*. If *obj*
- is not an instance of :c:type:`PyLongObject`, first call its
- :meth:`__index__` or :meth:`__int__` method (if present) to convert
- it to a :c:type:`PyLongObject`.
-
- If the value of *obj* is out of range for an :c:type:`unsigned long`,
- return the reduction of that value modulo ``ULONG_MAX + 1``.
+ Return a C :c:type:`unsigned long` from a Python long integer, without checking
+ for overflow.
Returns ``(unsigned long)-1`` on error. Use :c:func:`PyErr_Occurred` to
disambiguate.
- .. versionchanged:: 3.8
- Use :meth:`__index__` if available.
-
- .. deprecated:: 3.8
- Using :meth:`__int__` is deprecated.
-
+ .. versionadded:: 2.3
-.. c:function:: unsigned long long PyLong_AsUnsignedLongLongMask(PyObject *obj)
- Return a C :c:type:`unsigned long long` representation of *obj*. If *obj*
- is not an instance of :c:type:`PyLongObject`, first call its
- :meth:`__index__` or :meth:`__int__` method (if present) to convert
- it to a :c:type:`PyLongObject`.
+.. c:function:: unsigned PY_LONG_LONG PyLong_AsUnsignedLongLongMask(PyObject *io)
- If the value of *obj* is out of range for an :c:type:`unsigned long long`,
- return the reduction of that value modulo ``ULLONG_MAX + 1``.
+ Return a C :c:type:`unsigned long long` from a Python long integer, without
+ checking for overflow.
- Returns ``(unsigned long long)-1`` on error. Use :c:func:`PyErr_Occurred`
- to disambiguate.
+ Returns ``(unsigned PY_LONG_LONG)-1`` on error. Use
+ :c:func:`PyErr_Occurred` to disambiguate.
- .. versionchanged:: 3.8
- Use :meth:`__index__` if available.
-
- .. deprecated:: 3.8
- Using :meth:`__int__` is deprecated.
+ .. versionadded:: 2.3
.. c:function:: double PyLong_AsDouble(PyObject *pylong)
- Return a C :c:type:`double` representation of *pylong*. *pylong* must be
- an instance of :c:type:`PyLongObject`.
-
- Raise :exc:`OverflowError` if the value of *pylong* is out of range for a
- :c:type:`double`.
-
- Returns ``-1.0`` on error. Use :c:func:`PyErr_Occurred` to disambiguate.
+ Return a C :c:type:`double` representation of the contents of *pylong*. If
+ *pylong* cannot be approximately represented as a :c:type:`double`, an
+ :exc:`OverflowError` exception is raised and ``-1.0`` will be returned.
.. c:function:: void* PyLong_AsVoidPtr(PyObject *pylong)
- Convert a Python integer *pylong* to a C :c:type:`void` pointer.
+ Convert a Python integer or long integer *pylong* to a C :c:type:`void` pointer.
If *pylong* cannot be converted, an :exc:`OverflowError` will be raised. This
is only assured to produce a usable :c:type:`void` pointer for values created
with :c:func:`PyLong_FromVoidPtr`.
- Returns ``NULL`` on error. Use :c:func:`PyErr_Occurred` to disambiguate.
+ .. versionadded:: 1.5.2
+
+ .. versionchanged:: 2.5
+ For values outside 0..LONG_MAX, both signed and unsigned integers are accepted.
+
+
diff --git a/Doc/c-api/mapping.rst b/Doc/c-api/mapping.rst
index 6a80b03..7a83a50 100644
--- a/Doc/c-api/mapping.rst
+++ b/Doc/c-api/mapping.rst
@@ -1,20 +1,15 @@
-.. highlight:: c
+.. highlightlang:: c
.. _mapping:
Mapping Protocol
================
-See also :c:func:`PyObject_GetItem`, :c:func:`PyObject_SetItem` and
-:c:func:`PyObject_DelItem`.
-
.. c:function:: int PyMapping_Check(PyObject *o)
- Return ``1`` if the object provides mapping protocol or supports slicing,
- and ``0`` otherwise. Note that it returns ``1`` for Python classes with
- a :meth:`__getitem__` method since in general case it is impossible to
- determine what type of keys it supports. This function always succeeds.
+ Return ``1`` if the object provides mapping protocol, and ``0`` otherwise. This
+ function always succeeds.
.. c:function:: Py_ssize_t PyMapping_Size(PyObject *o)
@@ -22,81 +17,67 @@ See also :c:func:`PyObject_GetItem`, :c:func:`PyObject_SetItem` and
.. index:: builtin: len
- Returns the number of keys in object *o* on success, and ``-1`` on failure.
- This is equivalent to the Python expression ``len(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)``.
-.. c:function:: PyObject* PyMapping_GetItemString(PyObject *o, const char *key)
-
- Return element of *o* corresponding to the string *key* or ``NULL`` on failure.
- This is the equivalent of the Python expression ``o[key]``.
- See also :c:func:`PyObject_GetItem`.
+ .. versionchanged:: 2.5
+ These functions returned an :c:type:`int` type. This might require
+ changes in your code for properly supporting 64-bit systems.
-.. c:function:: int PyMapping_SetItemString(PyObject *o, const char *key, PyObject *v)
+.. c:function:: int PyMapping_DelItemString(PyObject *o, char *key)
- Map the string *key* to the value *v* in object *o*. Returns ``-1`` on
- failure. This is the equivalent of the Python statement ``o[key] = v``.
- See also :c:func:`PyObject_SetItem`.
+ Remove the mapping for object *key* from the object *o*. Return ``-1`` on
+ failure. This is equivalent to the Python statement ``del o[key]``.
.. c:function:: int PyMapping_DelItem(PyObject *o, PyObject *key)
- Remove the mapping for the object *key* from the object *o*. Return ``-1``
- on failure. This is equivalent to the Python statement ``del o[key]``.
- This is an alias of :c:func:`PyObject_DelItem`.
+ Remove the mapping for object *key* from the object *o*. Return ``-1`` on
+ failure. This is equivalent to the Python statement ``del o[key]``.
-.. c:function:: int PyMapping_DelItemString(PyObject *o, const char *key)
+.. c:function:: int PyMapping_HasKeyString(PyObject *o, char *key)
- Remove the mapping for the string *key* from the object *o*. Return ``-1``
- on failure. This is equivalent to the Python statement ``del o[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.
.. c:function:: int PyMapping_HasKey(PyObject *o, PyObject *key)
Return ``1`` if the mapping object has the key *key* and ``0`` otherwise.
- This is equivalent to the Python expression ``key in o``.
- This function always succeeds.
+ This is equivalent to ``o[key]``, returning ``True`` on success and ``False``
+ on an exception. This function always succeeds.
- Note that exceptions which occur while calling the :meth:`__getitem__`
- method will get suppressed.
- To get error reporting use :c:func:`PyObject_GetItem()` instead.
+.. c:function:: PyObject* PyMapping_Keys(PyObject *o)
-.. c:function:: int PyMapping_HasKeyString(PyObject *o, const char *key)
-
- Return ``1`` if the mapping object has the key *key* and ``0`` otherwise.
- This is equivalent to the Python expression ``key in o``.
- This function always succeeds.
-
- Note that exceptions which occur while calling the :meth:`__getitem__`
- method and creating a temporary string object will get suppressed.
- To get error reporting use :c:func:`PyMapping_GetItemString()` instead.
+ On success, return a list of the keys in object *o*. On failure, return *NULL*.
+ This is equivalent to the Python expression ``o.keys()``.
-.. c:function:: PyObject* PyMapping_Keys(PyObject *o)
+.. c:function:: PyObject* PyMapping_Values(PyObject *o)
- On success, return a list of the keys in object *o*. On failure, return
- ``NULL``.
+ On success, return a list of the values in object *o*. On failure, return
+ *NULL*. This is equivalent to the Python expression ``o.values()``.
- .. versionchanged:: 3.7
- Previously, the function returned a list or a tuple.
+.. c:function:: PyObject* PyMapping_Items(PyObject *o)
-.. c:function:: PyObject* PyMapping_Values(PyObject *o)
+ 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()``.
- On success, return a list of the values in object *o*. On failure, return
- ``NULL``.
- .. versionchanged:: 3.7
- Previously, the function returned a list or a tuple.
+.. c:function:: PyObject* PyMapping_GetItemString(PyObject *o, char *key)
+ Return element of *o* corresponding to the object *key* or *NULL* on failure.
+ This is the equivalent of the Python expression ``o[key]``.
-.. c:function:: PyObject* PyMapping_Items(PyObject *o)
- 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``.
+.. c:function:: int PyMapping_SetItemString(PyObject *o, char *key, PyObject *v)
- .. versionchanged:: 3.7
- Previously, the function returned a list or a tuple.
+ 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``.
diff --git a/Doc/c-api/marshal.rst b/Doc/c-api/marshal.rst
index 7b179e2..76120ae 100644
--- a/Doc/c-api/marshal.rst
+++ b/Doc/c-api/marshal.rst
@@ -1,4 +1,4 @@
-.. highlight:: c
+.. highlightlang:: c
.. _marshalling-utils:
@@ -13,33 +13,47 @@ binary mode.
Numeric values are stored with the least significant byte first.
-The module supports two versions of the data format: version 0 is the
-historical version, version 1 shares interned strings in the file, and upon
-unmarshalling. Version 2 uses a binary format for floating point numbers.
-``Py_MARSHAL_VERSION`` indicates the current file format (currently 2).
+The module supports two versions of the data format: version ``0`` is the
+historical version, version ``1`` (new in Python 2.4) shares interned strings in
+the file, and upon unmarshalling. Version 2 (new in Python 2.5) uses a binary
+format for floating point numbers. *Py_MARSHAL_VERSION* indicates the current
+file format (currently 2).
.. c:function:: void PyMarshal_WriteLongToFile(long value, FILE *file, int version)
Marshal a :c:type:`long` integer, *value*, to *file*. This will only write
the least-significant 32 bits of *value*; regardless of the size of the
- native :c:type:`long` type. *version* indicates the file format.
+ native :c:type:`long` type.
+
+ .. versionchanged:: 2.4
+ *version* indicates the file format.
.. c:function:: void PyMarshal_WriteObjectToFile(PyObject *value, FILE *file, int version)
Marshal a Python object, *value*, to *file*.
- *version* indicates the file format.
+
+ .. versionchanged:: 2.4
+ *version* indicates the file format.
.. c:function:: PyObject* PyMarshal_WriteObjectToString(PyObject *value, int version)
- Return a bytes object containing the marshalled representation of *value*.
- *version* indicates the file format.
+ Return a string object containing the marshalled representation of *value*.
+
+ .. versionchanged:: 2.4
+ *version* indicates the file format.
The following functions allow marshalled values to be read back in.
+XXX What about error detection? It appears that reading past the end of the
+file will always result in a negative numeric value (where that's relevant),
+but it's not clear that negative values won't be handled properly when there's
+no error. What's the right way to tell? Should only non-negative values be
+written using these routines?
+
.. c:function:: long PyMarshal_ReadLongFromFile(FILE *file)
@@ -47,9 +61,6 @@ The following functions allow marshalled values to be read back in.
for reading. Only a 32-bit value can be read in using this function,
regardless of the native size of :c:type:`long`.
- On error, sets the appropriate exception (:exc:`EOFError`) and returns
- ``-1``.
-
.. c:function:: int PyMarshal_ReadShortFromFile(FILE *file)
@@ -57,17 +68,12 @@ The following functions allow marshalled values to be read back in.
for reading. Only a 16-bit value can be read in using this function,
regardless of the native size of :c:type:`short`.
- On error, sets the appropriate exception (:exc:`EOFError`) and returns
- ``-1``.
-
.. c:function:: PyObject* PyMarshal_ReadObjectFromFile(FILE *file)
Return a Python object from the data stream in a :c:type:`FILE\*` opened for
- reading.
-
- On error, sets the appropriate exception (:exc:`EOFError`, :exc:`ValueError`
- or :exc:`TypeError`) and returns ``NULL``.
+ reading. On error, sets the appropriate exception (:exc:`EOFError` or
+ :exc:`TypeError`) and returns *NULL*.
.. c:function:: PyObject* PyMarshal_ReadLastObjectFromFile(FILE *file)
@@ -78,17 +84,17 @@ The following functions allow marshalled values to be read back in.
aggressively load file data into memory so that the de-serialization can
operate from data in memory rather than reading a byte at a time from the
file. Only use these variant if you are certain that you won't be reading
- anything else from the file.
-
- On error, sets the appropriate exception (:exc:`EOFError`, :exc:`ValueError`
- or :exc:`TypeError`) and returns ``NULL``.
-
+ anything else from the file. On error, sets the appropriate exception
+ (:exc:`EOFError` or :exc:`TypeError`) and returns *NULL*.
-.. c:function:: PyObject* PyMarshal_ReadObjectFromString(const char *data, Py_ssize_t len)
- Return a Python object from the data stream in a byte buffer
- containing *len* bytes pointed to by *data*.
+.. c:function:: PyObject* PyMarshal_ReadObjectFromString(char *string, Py_ssize_t len)
- On error, sets the appropriate exception (:exc:`EOFError`, :exc:`ValueError`
- or :exc:`TypeError`) and returns ``NULL``.
+ Return a Python object from the data stream in a character buffer
+ containing *len* bytes pointed to by *string*. On error, sets the
+ appropriate exception (:exc:`EOFError` or :exc:`TypeError`) and returns
+ *NULL*.
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` type for *len*. This might require
+ changes in your code for properly supporting 64-bit systems.
diff --git a/Doc/c-api/memory.rst b/Doc/c-api/memory.rst
index ba7bd3b..2b9fb73 100644
--- a/Doc/c-api/memory.rst
+++ b/Doc/c-api/memory.rst
@@ -1,4 +1,4 @@
-.. highlight:: c
+.. highlightlang:: c
.. _memory:
@@ -61,13 +61,13 @@ example::
if (buf == NULL)
return PyErr_NoMemory();
...Do some I/O operation involving buf...
- res = PyBytes_FromString(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 bytes object returned as a result.
+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
@@ -83,79 +83,6 @@ 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.
-.. seealso::
-
- The :envvar:`PYTHONMALLOC` environment variable can be used to configure
- the memory allocators used by Python.
-
- The :envvar:`PYTHONMALLOCSTATS` environment variable can be used to print
- statistics of the :ref:`pymalloc memory allocator <pymalloc>` every time a
- new pymalloc object arena is created, and on shutdown.
-
-
-Raw Memory Interface
-====================
-
-The following function sets are wrappers to the system allocator. These
-functions are thread-safe, the :term:`GIL <global interpreter lock>` does not
-need to be held.
-
-The :ref:`default raw memory allocator <default-memory-allocators>` uses
-the following functions: :c:func:`malloc`, :c:func:`calloc`, :c:func:`realloc`
-and :c:func:`free`; call ``malloc(1)`` (or ``calloc(1, 1)``) when requesting
-zero bytes.
-
-.. versionadded:: 3.4
-
-.. c:function:: void* PyMem_RawMalloc(size_t n)
-
- Allocates *n* bytes and returns a pointer of type :c: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 ``PyMem_RawMalloc(1)`` had been called instead. The memory will not have
- been initialized in any way.
-
-
-.. c:function:: void* PyMem_RawCalloc(size_t nelem, size_t elsize)
-
- Allocates *nelem* elements each whose size in bytes is *elsize* and returns
- a pointer of type :c:type:`void\*` to the allocated memory, or ``NULL`` if the
- request fails. The memory is initialized to zeros.
-
- Requesting zero elements or elements of size zero bytes returns a distinct
- non-``NULL`` pointer if possible, as if ``PyMem_RawCalloc(1, 1)`` had been
- called instead.
-
- .. versionadded:: 3.5
-
-
-.. c:function:: void* PyMem_RawRealloc(void *p, size_t n)
-
- Resizes the memory block pointed to by *p* to *n* bytes. The contents will
- be unchanged to the minimum of the old and the new sizes.
-
- If *p* is ``NULL``, the call is equivalent to ``PyMem_RawMalloc(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
- :c:func:`PyMem_RawMalloc`, :c:func:`PyMem_RawRealloc` or
- :c:func:`PyMem_RawCalloc`.
-
- If the request fails, :c:func:`PyMem_RawRealloc` returns ``NULL`` and *p*
- remains a valid pointer to the previous memory area.
-
-
-.. c:function:: void PyMem_RawFree(void *p)
-
- Frees the memory block pointed to by *p*, which must have been returned by a
- previous call to :c:func:`PyMem_RawMalloc`, :c:func:`PyMem_RawRealloc` or
- :c:func:`PyMem_RawCalloc`. Otherwise, or if ``PyMem_RawFree(p)`` has been
- called before, undefined behavior occurs.
-
- If *p* is ``NULL``, no operation is performed.
-
.. _memoryinterface:
@@ -164,67 +91,35 @@ Memory Interface
The following function sets, modeled after the ANSI C standard, but specifying
behavior when requesting zero bytes, are available for allocating and releasing
-memory from the Python heap.
+memory from the Python heap:
-The :ref:`default memory allocator <default-memory-allocators>` uses the
-:ref:`pymalloc memory allocator <pymalloc>`.
-
-.. warning::
-
- The :term:`GIL <global interpreter lock>` must be held when using these
- functions.
-
-.. versionchanged:: 3.6
-
- The default allocator is now pymalloc instead of system :c:func:`malloc`.
.. c:function:: void* PyMem_Malloc(size_t n)
Allocates *n* bytes and returns a pointer of type :c: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 ``PyMem_Malloc(1)`` had been called instead. The memory will not have
- been initialized in any way.
-
-
-.. c:function:: void* PyMem_Calloc(size_t nelem, size_t elsize)
-
- Allocates *nelem* elements each whose size in bytes is *elsize* and returns
- a pointer of type :c:type:`void\*` to the allocated memory, or ``NULL`` if the
- request fails. The memory is initialized to zeros.
-
- Requesting zero elements or elements of size zero bytes returns a distinct
- non-``NULL`` pointer if possible, as if ``PyMem_Calloc(1, 1)`` had been called
- instead.
-
- .. versionadded:: 3.5
+ allocated memory, or *NULL* if the request fails. Requesting zero bytes returns
+ a distinct non-*NULL* pointer if possible, as if ``PyMem_Malloc(1)`` had
+ been called instead. The memory will not have been initialized in any way.
.. c:function:: void* PyMem_Realloc(void *p, size_t n)
Resizes the memory block pointed to by *p* to *n* bytes. The contents will be
- unchanged to the minimum of the old and the new sizes.
-
- If *p* is ``NULL``, the call is equivalent to ``PyMem_Malloc(n)``; 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
- :c:func:`PyMem_Malloc`, :c:func:`PyMem_Realloc` or :c:func:`PyMem_Calloc`.
-
- If the request fails, :c:func:`PyMem_Realloc` returns ``NULL`` and *p* remains
- a valid pointer to the previous memory area.
+ unchanged to the minimum of the old and the new sizes. If *p* is *NULL*, the
+ call is equivalent to ``PyMem_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 :c:func:`PyMem_Malloc` or :c:func:`PyMem_Realloc`. If the request fails,
+ :c:func:`PyMem_Realloc` returns *NULL* and *p* remains a valid pointer to the
+ previous memory area.
.. c:function:: void PyMem_Free(void *p)
Frees the memory block pointed to by *p*, which must have been returned by a
- previous call to :c:func:`PyMem_Malloc`, :c:func:`PyMem_Realloc` or
- :c:func:`PyMem_Calloc`. Otherwise, or if ``PyMem_Free(p)`` has been called
- before, undefined behavior occurs.
-
- If *p* is ``NULL``, no operation is performed.
+ previous call to :c:func:`PyMem_Malloc` or :c:func:`PyMem_Realloc`. Otherwise, or
+ if ``PyMem_Free(p)`` has been called before, undefined behavior occurs. If
+ *p* is *NULL*, no operation is performed.
The following type-oriented macros are provided for convenience. Note that
*TYPE* refers to any C type.
@@ -241,11 +136,9 @@ The following type-oriented macros are provided for convenience. Note that
Same as :c:func:`PyMem_Realloc`, but the memory block is resized to ``(n *
sizeof(TYPE))`` bytes. Returns a pointer cast to :c:type:`TYPE\*`. On return,
- *p* will be a pointer to the new memory area, or ``NULL`` in the event of
- failure.
-
- This is a C preprocessor macro; *p* is always reassigned. Save the original
- value of *p* to avoid losing memory when handling errors.
+ *p* will be a pointer to the new memory area, or *NULL* in the event of
+ failure. This is a C preprocessor macro; p is always reassigned. Save
+ the original value of p to avoid losing memory when handling errors.
.. c:function:: void PyMem_Del(void *p)
@@ -257,12 +150,9 @@ allocator directly, without involving the C API functions listed above. However,
note that their use does not preserve binary compatibility across Python
versions and is therefore deprecated in extension modules.
-* ``PyMem_MALLOC(size)``
-* ``PyMem_NEW(type, size)``
-* ``PyMem_REALLOC(ptr, size)``
-* ``PyMem_RESIZE(ptr, type, size)``
-* ``PyMem_FREE(ptr)``
-* ``PyMem_DEL(ptr)``
+:c:func:`PyMem_MALLOC`, :c:func:`PyMem_REALLOC`, :c:func:`PyMem_FREE`.
+
+:c:func:`PyMem_NEW`, :c:func:`PyMem_RESIZE`, :c:func:`PyMem_DEL`.
Object allocators
@@ -272,8 +162,7 @@ The following function sets, modeled after the ANSI C standard, but specifying
behavior when requesting zero bytes, are available for allocating and releasing
memory from the Python heap.
-The :ref:`default object allocator <default-memory-allocators>` uses the
-:ref:`pymalloc memory allocator <pymalloc>`.
+By default, these functions use :ref:`pymalloc memory allocator <pymalloc>`.
.. warning::
@@ -283,39 +172,26 @@ The :ref:`default object allocator <default-memory-allocators>` uses the
.. c:function:: void* PyObject_Malloc(size_t n)
Allocates *n* bytes and returns a pointer of type :c:type:`void\*` to the
- allocated memory, or ``NULL`` if the request fails.
+ allocated memory, or *NULL* if the request fails.
- Requesting zero bytes returns a distinct non-``NULL`` pointer if possible, as
+ 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.
-.. c:function:: void* PyObject_Calloc(size_t nelem, size_t elsize)
-
- Allocates *nelem* elements each whose size in bytes is *elsize* and returns
- a pointer of type :c:type:`void\*` to the allocated memory, or ``NULL`` if the
- request fails. The memory is initialized to zeros.
-
- Requesting zero elements or elements of size zero bytes returns a distinct
- non-``NULL`` pointer if possible, as if ``PyObject_Calloc(1, 1)`` had been called
- instead.
-
- .. versionadded:: 3.5
-
-
.. c:function:: 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*
+ 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``.
+ returned pointer is non-*NULL*.
- Unless *p* is ``NULL``, it must have been returned by a previous call to
+ Unless *p* is *NULL*, it must have been returned by a previous call to
:c:func:`PyObject_Malloc`, :c:func:`PyObject_Realloc` or :c:func:`PyObject_Calloc`.
- If the request fails, :c:func:`PyObject_Realloc` returns ``NULL`` and *p* remains
+ If the request fails, :c:func:`PyObject_Realloc` returns *NULL* and *p* remains
a valid pointer to the previous memory area.
@@ -326,157 +202,17 @@ The :ref:`default object allocator <default-memory-allocators>` uses the
:c:func:`PyObject_Calloc`. Otherwise, or if ``PyObject_Free(p)`` has been called
before, undefined behavior occurs.
- If *p* is ``NULL``, no operation is performed.
-
+ If *p* is *NULL*, no operation is performed.
-.. _default-memory-allocators:
-Default Memory Allocators
-=========================
+In addition, the following macro sets are provided:
-Default memory allocators:
-
-=============================== ==================== ================== ===================== ====================
-Configuration Name PyMem_RawMalloc PyMem_Malloc PyObject_Malloc
-=============================== ==================== ================== ===================== ====================
-Release build ``"pymalloc"`` ``malloc`` ``pymalloc`` ``pymalloc``
-Debug build ``"pymalloc_debug"`` ``malloc`` + debug ``pymalloc`` + debug ``pymalloc`` + debug
-Release build, without pymalloc ``"malloc"`` ``malloc`` ``malloc`` ``malloc``
-Debug build, without pymalloc ``"malloc_debug"`` ``malloc`` + debug ``malloc`` + debug ``malloc`` + debug
-=============================== ==================== ================== ===================== ====================
-
-Legend:
-
-* Name: value for :envvar:`PYTHONMALLOC` environment variable
-* ``malloc``: system allocators from the standard C library, C functions:
- :c:func:`malloc`, :c:func:`calloc`, :c:func:`realloc` and :c:func:`free`
-* ``pymalloc``: :ref:`pymalloc memory allocator <pymalloc>`
-* "+ debug": with debug hooks installed by :c:func:`PyMem_SetupDebugHooks`
-
-
-Customize Memory Allocators
-===========================
-
-.. versionadded:: 3.4
-
-.. c:type:: PyMemAllocatorEx
-
- Structure used to describe a memory block allocator. The structure has
- four fields:
-
- +----------------------------------------------------------+---------------------------------------+
- | Field | Meaning |
- +==========================================================+=======================================+
- | ``void *ctx`` | user context passed as first argument |
- +----------------------------------------------------------+---------------------------------------+
- | ``void* malloc(void *ctx, size_t size)`` | allocate a memory block |
- +----------------------------------------------------------+---------------------------------------+
- | ``void* calloc(void *ctx, size_t nelem, size_t elsize)`` | allocate a memory block initialized |
- | | with zeros |
- +----------------------------------------------------------+---------------------------------------+
- | ``void* realloc(void *ctx, void *ptr, size_t new_size)`` | allocate or resize a memory block |
- +----------------------------------------------------------+---------------------------------------+
- | ``void free(void *ctx, void *ptr)`` | free a memory block |
- +----------------------------------------------------------+---------------------------------------+
-
- .. versionchanged:: 3.5
- The :c:type:`PyMemAllocator` structure was renamed to
- :c:type:`PyMemAllocatorEx` and a new ``calloc`` field was added.
-
-
-.. c:type:: PyMemAllocatorDomain
-
- Enum used to identify an allocator domain. Domains:
-
- .. c:var:: PYMEM_DOMAIN_RAW
-
- Functions:
-
- * :c:func:`PyMem_RawMalloc`
- * :c:func:`PyMem_RawRealloc`
- * :c:func:`PyMem_RawCalloc`
- * :c:func:`PyMem_RawFree`
-
- .. c:var:: PYMEM_DOMAIN_MEM
-
- Functions:
-
- * :c:func:`PyMem_Malloc`,
- * :c:func:`PyMem_Realloc`
- * :c:func:`PyMem_Calloc`
- * :c:func:`PyMem_Free`
-
- .. c:var:: PYMEM_DOMAIN_OBJ
-
- Functions:
-
- * :c:func:`PyObject_Malloc`
- * :c:func:`PyObject_Realloc`
- * :c:func:`PyObject_Calloc`
- * :c:func:`PyObject_Free`
-
-.. c:function:: void PyMem_GetAllocator(PyMemAllocatorDomain domain, PyMemAllocatorEx *allocator)
-
- Get the memory block allocator of the specified domain.
-
-
-.. c:function:: void PyMem_SetAllocator(PyMemAllocatorDomain domain, PyMemAllocatorEx *allocator)
-
- Set the memory block allocator of the specified domain.
-
- The new allocator must return a distinct non-``NULL`` pointer when requesting
- zero bytes.
-
- For the :c:data:`PYMEM_DOMAIN_RAW` domain, the allocator must be
- thread-safe: the :term:`GIL <global interpreter lock>` is not held when the
- allocator is called.
-
- If the new allocator is not a hook (does not call the previous allocator),
- the :c:func:`PyMem_SetupDebugHooks` function must be called to reinstall the
- debug hooks on top on the new allocator.
-
-
-.. c:function:: void PyMem_SetupDebugHooks(void)
-
- Setup hooks to detect bugs in the Python memory allocator functions.
-
- Newly allocated memory is filled with the byte ``0xCD`` (``CLEANBYTE``),
- freed memory is filled with the byte ``0xDD`` (``DEADBYTE``). Memory blocks
- are surrounded by "forbidden bytes" (``FORBIDDENBYTE``: byte ``0xFD``).
-
- Runtime checks:
-
- - Detect API violations, ex: :c:func:`PyObject_Free` called on a buffer
- allocated by :c:func:`PyMem_Malloc`
- - Detect write before the start of the buffer (buffer underflow)
- - Detect write after the end of the buffer (buffer overflow)
- - Check that the :term:`GIL <global interpreter lock>` is held when
- allocator functions of :c:data:`PYMEM_DOMAIN_OBJ` (ex:
- :c:func:`PyObject_Malloc`) and :c:data:`PYMEM_DOMAIN_MEM` (ex:
- :c:func:`PyMem_Malloc`) domains are called
-
- On error, the debug hooks use the :mod:`tracemalloc` module to get the
- traceback where a memory block was allocated. The traceback is only
- displayed if :mod:`tracemalloc` is tracing Python memory allocations and the
- memory block was traced.
-
- These hooks are :ref:`installed by default <default-memory-allocators>` if
- Python is compiled in debug
- mode. The :envvar:`PYTHONMALLOC` environment variable can be used to install
- debug hooks on a Python compiled in release mode.
-
- .. versionchanged:: 3.6
- This function now also works on Python compiled in release mode.
- On error, the debug hooks now use :mod:`tracemalloc` to get the traceback
- where a memory block was allocated. The debug hooks now also check
- if the GIL is held when functions of :c:data:`PYMEM_DOMAIN_OBJ` and
- :c:data:`PYMEM_DOMAIN_MEM` domains are called.
-
- .. versionchanged:: 3.8
- Byte patterns ``0xCB`` (``CLEANBYTE``), ``0xDB`` (``DEADBYTE``) and
- ``0xFB`` (``FORBIDDENBYTE``) have been replaced with ``0xCD``, ``0xDD``
- and ``0xFD`` to use the same values than Windows CRT debug ``malloc()``
- and ``free()``.
+* :c:func:`PyObject_MALLOC`: alias to :c:func:`PyObject_Malloc`
+* :c:func:`PyObject_REALLOC`: alias to :c:func:`PyObject_Realloc`
+* :c:func:`PyObject_FREE`: alias to :c:func:`PyObject_Free`
+* :c:func:`PyObject_Del`: alias to :c:func:`PyObject_Free`
+* :c:func:`PyObject_DEL`: alias to :c:func:`PyObject_FREE` (so finally an alias
+ to :c:func:`PyObject_Free`)
.. _pymalloc:
@@ -486,68 +222,19 @@ The pymalloc allocator
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 :c:func:`PyMem_RawMalloc` and
-:c:func:`PyMem_RawRealloc` for allocations larger than 512 bytes.
+with a fixed size of 256 KiB. It falls back to :c:func:`malloc` and
+:c:func:`realloc` for allocations larger than 512 bytes.
-*pymalloc* is the :ref:`default allocator <default-memory-allocators>` of the
-:c:data:`PYMEM_DOMAIN_MEM` (ex: :c:func:`PyMem_Malloc`) and
-:c:data:`PYMEM_DOMAIN_OBJ` (ex: :c:func:`PyObject_Malloc`) domains.
+*pymalloc* is the default allocator of :c:func:`PyObject_Malloc`.
The arena allocator uses the following functions:
-* :c:func:`VirtualAlloc` and :c:func:`VirtualFree` on Windows,
* :c:func:`mmap` and :c:func:`munmap` if available,
* :c:func:`malloc` and :c:func:`free` otherwise.
-Customize pymalloc Arena Allocator
-----------------------------------
-
-.. versionadded:: 3.4
-
-.. c:type:: PyObjectArenaAllocator
-
- Structure used to describe an arena allocator. The structure has
- three fields:
-
- +--------------------------------------------------+---------------------------------------+
- | Field | Meaning |
- +==================================================+=======================================+
- | ``void *ctx`` | user context passed as first argument |
- +--------------------------------------------------+---------------------------------------+
- | ``void* alloc(void *ctx, size_t size)`` | allocate an arena of size bytes |
- +--------------------------------------------------+---------------------------------------+
- | ``void free(void *ctx, size_t size, void *ptr)`` | free an arena |
- +--------------------------------------------------+---------------------------------------+
-
-.. c:function:: PyObject_GetArenaAllocator(PyObjectArenaAllocator *allocator)
-
- Get the arena allocator.
-
-.. c:function:: PyObject_SetArenaAllocator(PyObjectArenaAllocator *allocator)
-
- Set the arena allocator.
-
-
-tracemalloc C API
-=================
-
-.. versionadded:: 3.7
-
-.. c:function: int PyTraceMalloc_Track(unsigned int domain, uintptr_t ptr, size_t size)
-
- Track an allocated memory block in the :mod:`tracemalloc` module.
-
- Return ``0`` on success, return ``-1`` on error (failed to allocate memory to
- store the trace). Return ``-2`` if tracemalloc is disabled.
-
- If memory block is already tracked, update the existing trace.
-
-.. c:function: int PyTraceMalloc_Untrack(unsigned int domain, uintptr_t ptr)
-
- Untrack an allocated memory block in the :mod:`tracemalloc` module.
- Do nothing if the block was not tracked.
-
- Return ``-2`` if tracemalloc is disabled, otherwise return ``0``.
+.. versionchanged:: 2.7.7
+ The threshold changed from 256 to 512 bytes. The arena allocator now
+ uses :c:func:`mmap` if available.
.. _memoryexamples:
@@ -564,7 +251,7 @@ I/O buffer is allocated from the Python heap by using the first function set::
if (buf == NULL)
return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
- res = PyBytes_FromString(buf);
+ res = PyString_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
@@ -576,7 +263,7 @@ The same code using the type-oriented function set::
if (buf == NULL)
return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
- res = PyBytes_FromString(buf);
+ res = PyString_FromString(buf);
PyMem_Del(buf); /* allocated with PyMem_New */
return res;
diff --git a/Doc/c-api/memoryview.rst b/Doc/c-api/memoryview.rst
deleted file mode 100644
index de429e5..0000000
--- a/Doc/c-api/memoryview.rst
+++ /dev/null
@@ -1,63 +0,0 @@
-.. highlight:: c
-
-.. _memoryview-objects:
-
-.. index::
- object: memoryview
-
-MemoryView objects
-------------------
-
-A :class:`memoryview` object exposes the C level :ref:`buffer interface
-<bufferobjects>` as a Python object which can then be passed around like
-any other object.
-
-
-.. c:function:: PyObject *PyMemoryView_FromObject(PyObject *obj)
-
- Create a memoryview object from an object that provides the buffer interface.
- If *obj* supports writable buffer exports, the memoryview object will be
- read/write, otherwise it may be either read-only or read/write at the
- discretion of the exporter.
-
-.. c:function:: PyObject *PyMemoryView_FromMemory(char *mem, Py_ssize_t size, int flags)
-
- Create a memoryview object using *mem* as the underlying buffer.
- *flags* can be one of :c:macro:`PyBUF_READ` or :c:macro:`PyBUF_WRITE`.
-
- .. versionadded:: 3.3
-
-.. c:function:: PyObject *PyMemoryView_FromBuffer(Py_buffer *view)
-
- Create a memoryview object wrapping the given buffer structure *view*.
- For simple byte buffers, :c:func:`PyMemoryView_FromMemory` is the preferred
- function.
-
-.. c:function:: PyObject *PyMemoryView_GetContiguous(PyObject *obj, int buffertype, char order)
-
- Create a memoryview object to a :term:`contiguous` chunk of memory (in either
- 'C' or 'F'ortran *order*) from an object that defines the buffer
- interface. If memory is contiguous, the memoryview object points to the
- original memory. Otherwise, a copy is made and the memoryview points to a
- new bytes object.
-
-
-.. c:function:: int PyMemoryView_Check(PyObject *obj)
-
- Return true if the object *obj* is a memoryview object. It is not
- currently allowed to create subclasses of :class:`memoryview`.
-
-
-.. c:function:: Py_buffer *PyMemoryView_GET_BUFFER(PyObject *mview)
-
- Return a pointer to the memoryview's private copy of the exporter's buffer.
- *mview* **must** be a memoryview instance; this macro doesn't check its type,
- you must do it yourself or you will risk crashes.
-
-.. c:function:: Py_buffer *PyMemoryView_GET_BASE(PyObject *mview)
-
- Return either a pointer to the exporting object that the memoryview is based
- on or ``NULL`` if the memoryview has been created by one of the functions
- :c:func:`PyMemoryView_FromMemory` or :c:func:`PyMemoryView_FromBuffer`.
- *mview* **must** be a memoryview instance.
-
diff --git a/Doc/c-api/method.rst b/Doc/c-api/method.rst
index b1862d7..71ddeda 100644
--- a/Doc/c-api/method.rst
+++ b/Doc/c-api/method.rst
@@ -1,45 +1,4 @@
-.. highlight:: c
-
-.. _instancemethod-objects:
-
-Instance Method Objects
------------------------
-
-.. index:: object: instancemethod
-
-An instance method is a wrapper for a :c:data:`PyCFunction` and the new way
-to bind a :c:data:`PyCFunction` to a class object. It replaces the former call
-``PyMethod_New(func, NULL, class)``.
-
-
-.. c:var:: PyTypeObject PyInstanceMethod_Type
-
- This instance of :c:type:`PyTypeObject` represents the Python instance
- method type. It is not exposed to Python programs.
-
-
-.. c:function:: int PyInstanceMethod_Check(PyObject *o)
-
- Return true if *o* is an instance method object (has type
- :c:data:`PyInstanceMethod_Type`). The parameter must not be ``NULL``.
-
-
-.. c:function:: PyObject* PyInstanceMethod_New(PyObject *func)
-
- Return a new instance method object, with *func* being any callable object
- *func* is the function that will be called when the instance method is
- called.
-
-
-.. c:function:: PyObject* PyInstanceMethod_Function(PyObject *im)
-
- Return the function object associated with the instance method *im*.
-
-
-.. c:function:: PyObject* PyInstanceMethod_GET_FUNCTION(PyObject *im)
-
- Macro version of :c:func:`PyInstanceMethod_Function` which avoids error checking.
-
+.. highlightlang:: c
.. _method-objects:
@@ -48,9 +7,7 @@ Method Objects
.. index:: object: method
-Methods are bound function objects. Methods are always bound to an instance of
-a user-defined class. Unbound methods (methods bound to a class object) are
-no longer available.
+There are some useful functions that are useful for working with method objects.
.. c:var:: PyTypeObject PyMethod_Type
@@ -64,14 +21,27 @@ no longer available.
.. c:function:: int PyMethod_Check(PyObject *o)
Return true if *o* is a method object (has type :c:data:`PyMethod_Type`). The
- parameter must not be ``NULL``.
+ parameter must not be *NULL*.
+
+
+.. c:function:: PyObject* PyMethod_New(PyObject *func, PyObject *self, PyObject *class)
+
+ Return a new method object, with *func* being any callable object; this is the
+ function that will be called when the method is called. If this method should
+ be bound to an instance, *self* should be the instance and *class* should be the
+ class of *self*, otherwise *self* should be *NULL* and *class* should be the
+ class which provides the unbound method..
+
+
+.. c:function:: PyObject* PyMethod_Class(PyObject *meth)
+
+ Return the class object from which the method *meth* was created; if this was
+ created from an instance, it will be the class of the instance.
-.. c:function:: PyObject* PyMethod_New(PyObject *func, PyObject *self)
+.. c:function:: PyObject* PyMethod_GET_CLASS(PyObject *meth)
- Return a new method object, with *func* being any callable object and *self*
- the instance the method should be bound. *func* is the function that will
- be called when the method is called. *self* must not be ``NULL``.
+ Macro version of :c:func:`PyMethod_Class` which avoids error checking.
.. c:function:: PyObject* PyMethod_Function(PyObject *meth)
@@ -86,7 +56,8 @@ no longer available.
.. c:function:: PyObject* PyMethod_Self(PyObject *meth)
- Return the instance associated with the method *meth*.
+ Return the instance associated with the method *meth* if it is bound, otherwise
+ return *NULL*.
.. c:function:: PyObject* PyMethod_GET_SELF(PyObject *meth)
@@ -98,3 +69,4 @@ no longer available.
Clear the free list. Return the total number of freed items.
+ .. versionadded:: 2.6
diff --git a/Doc/c-api/module.rst b/Doc/c-api/module.rst
index 57902a9..80e12e1 100644
--- a/Doc/c-api/module.rst
+++ b/Doc/c-api/module.rst
@@ -1,4 +1,4 @@
-.. highlight:: c
+.. highlightlang:: c
.. _moduleobjects:
@@ -7,6 +7,8 @@ Module Objects
.. index:: object: module
+There are only a few functions special to module objects.
+
.. c:var:: PyTypeObject PyModule_Type
@@ -20,38 +22,28 @@ Module Objects
Return true if *p* is a module object, or a subtype of a module object.
+ .. versionchanged:: 2.2
+ Allowed subtypes to be accepted.
+
.. c:function:: int PyModule_CheckExact(PyObject *p)
Return true if *p* is a module object, but not a subtype of
:c:data:`PyModule_Type`.
+ .. versionadded:: 2.2
-.. c:function:: PyObject* PyModule_NewObject(PyObject *name)
+
+.. c:function:: PyObject* PyModule_New(const char *name)
.. index::
single: __name__ (module attribute)
single: __doc__ (module attribute)
single: __file__ (module attribute)
- single: __package__ (module attribute)
- single: __loader__ (module attribute)
Return a new module object with the :attr:`__name__` attribute set to *name*.
- The module's :attr:`__name__`, :attr:`__doc__`, :attr:`__package__`, and
- :attr:`__loader__` attributes are filled in (all but :attr:`__name__` are set
- to ``None``); the caller is responsible for providing a :attr:`__file__`
- attribute.
-
- .. versionadded:: 3.3
-
- .. versionchanged:: 3.4
- :attr:`__package__` and :attr:`__loader__` are set to ``None``.
-
-
-.. c:function:: PyObject* PyModule_New(const char *name)
-
- Similar to :c:func:`PyModule_NewObject`, but the name is a UTF-8 encoded
- string instead of a Unicode object.
+ Only the module's :attr:`__doc__` and :attr:`__name__` attributes are filled in;
+ the caller is responsible for providing a :attr:`__file__` attribute.
.. c:function:: PyObject* PyModule_GetDict(PyObject *module)
@@ -59,380 +51,41 @@ Module Objects
.. index:: single: __dict__ (module attribute)
Return the dictionary object that implements *module*'s namespace; this object
- is the same as the :attr:`~object.__dict__` attribute of the module object.
- If *module* is not a module object (or a subtype of a module object),
- :exc:`SystemError` is raised and ``NULL`` is returned.
-
- It is recommended extensions use other :c:func:`PyModule_\*` and
- :c:func:`PyObject_\*` functions rather than directly manipulate a module's
- :attr:`~object.__dict__`.
+ is the same as the :attr:`~object.__dict__` attribute of the module object. This
+ function never fails. It is recommended extensions use other
+ :c:func:`PyModule_\*` and :c:func:`PyObject_\*` functions rather than directly
+ manipulate a module's :attr:`~object.__dict__`.
-.. c:function:: PyObject* PyModule_GetNameObject(PyObject *module)
+.. c:function:: char* PyModule_GetName(PyObject *module)
.. index::
single: __name__ (module attribute)
single: SystemError (built-in exception)
Return *module*'s :attr:`__name__` value. If the module does not provide one,
- or if it is not a string, :exc:`SystemError` is raised and ``NULL`` is returned.
-
- .. versionadded:: 3.3
-
-
-.. c:function:: const char* PyModule_GetName(PyObject *module)
-
- Similar to :c:func:`PyModule_GetNameObject` but return the name encoded to
- ``'utf-8'``.
-
-.. c:function:: void* PyModule_GetState(PyObject *module)
-
- Return the "state" of the module, that is, a pointer to the block of memory
- allocated at module creation time, or ``NULL``. See
- :c:member:`PyModuleDef.m_size`.
+ or if it is not a string, :exc:`SystemError` is raised and *NULL* is returned.
-.. c:function:: PyModuleDef* PyModule_GetDef(PyObject *module)
-
- Return a pointer to the :c:type:`PyModuleDef` struct from which the module was
- created, or ``NULL`` if the module wasn't created from a definition.
-
-
-.. c:function:: PyObject* PyModule_GetFilenameObject(PyObject *module)
+.. c:function:: char* PyModule_GetFilename(PyObject *module)
.. index::
single: __file__ (module attribute)
single: SystemError (built-in exception)
Return the name of the file from which *module* was loaded using *module*'s
- :attr:`__file__` attribute. If this is not defined, or if it is not a
- unicode string, raise :exc:`SystemError` and return ``NULL``; otherwise return
- a reference to a Unicode object.
-
- .. versionadded:: 3.2
-
-
-.. c:function:: const char* PyModule_GetFilename(PyObject *module)
-
- Similar to :c:func:`PyModule_GetFilenameObject` but return the filename
- encoded to 'utf-8'.
-
- .. deprecated:: 3.2
- :c:func:`PyModule_GetFilename` raises :c:type:`UnicodeEncodeError` on
- unencodable filenames, use :c:func:`PyModule_GetFilenameObject` instead.
-
-
-.. _initializing-modules:
-
-Initializing C modules
-^^^^^^^^^^^^^^^^^^^^^^
-
-Modules objects are usually created from extension modules (shared libraries
-which export an initialization function), or compiled-in modules
-(where the initialization function is added using :c:func:`PyImport_AppendInittab`).
-See :ref:`building` or :ref:`extending-with-embedding` for details.
-
-The initialization function can either pass a module definition instance
-to :c:func:`PyModule_Create`, and return the resulting module object,
-or request "multi-phase initialization" by returning the definition struct itself.
-
-.. c:type:: PyModuleDef
-
- The module definition struct, which holds all information needed to create
- a module object. There is usually only one statically initialized variable
- of this type for each module.
-
- .. c:member:: PyModuleDef_Base m_base
-
- Always initialize this member to :const:`PyModuleDef_HEAD_INIT`.
-
- .. c:member:: const char *m_name
-
- Name for the new module.
-
- .. c:member:: const char *m_doc
-
- Docstring for the module; usually a docstring variable created with
- :c:func:`PyDoc_STRVAR` is used.
-
- .. c:member:: Py_ssize_t m_size
-
- Module state may be kept in a per-module memory area that can be
- retrieved with :c:func:`PyModule_GetState`, rather than in static globals.
- This makes modules safe for use in multiple sub-interpreters.
-
- This memory area is allocated based on *m_size* on module creation,
- and freed when the module object is deallocated, after the
- :c:member:`m_free` function has been called, if present.
-
- Setting ``m_size`` to ``-1`` means that the module does not support
- sub-interpreters, because it has global state.
-
- Setting it to a non-negative value means that the module can be
- re-initialized and specifies the additional amount of memory it requires
- for its state. Non-negative ``m_size`` is required for multi-phase
- initialization.
-
- See :PEP:`3121` for more details.
-
- .. c:member:: PyMethodDef* m_methods
-
- A pointer to a table of module-level functions, described by
- :c:type:`PyMethodDef` values. Can be ``NULL`` if no functions are present.
-
- .. c:member:: PyModuleDef_Slot* m_slots
-
- An array of slot definitions for multi-phase initialization, terminated by
- a ``{0, NULL}`` entry.
- When using single-phase initialization, *m_slots* must be ``NULL``.
-
- .. versionchanged:: 3.5
-
- Prior to version 3.5, this member was always set to ``NULL``,
- and was defined as:
-
- .. c:member:: inquiry m_reload
-
- .. c:member:: traverseproc m_traverse
-
- A traversal function to call during GC traversal of the module object, or
- ``NULL`` if not needed. This function may be called before module state
- is allocated (:c:func:`PyModule_GetState()` may return `NULL`),
- and before the :c:member:`Py_mod_exec` function is executed.
-
- .. c:member:: inquiry m_clear
-
- A clear function to call during GC clearing of the module object, or
- ``NULL`` if not needed. This function may be called before module state
- is allocated (:c:func:`PyModule_GetState()` may return `NULL`),
- and before the :c:member:`Py_mod_exec` function is executed.
-
- .. c:member:: freefunc m_free
-
- A function to call during deallocation of the module object, or ``NULL`` if
- not needed. This function may be called before module state
- is allocated (:c:func:`PyModule_GetState()` may return `NULL`),
- and before the :c:member:`Py_mod_exec` function is executed.
-
-Single-phase initialization
-...........................
-
-The module initialization function may create and return the module object
-directly. This is referred to as "single-phase initialization", and uses one
-of the following two module creation functions:
-
-.. c:function:: PyObject* PyModule_Create(PyModuleDef *def)
-
- Create a new module object, given the definition in *def*. This behaves
- like :c:func:`PyModule_Create2` with *module_api_version* set to
- :const:`PYTHON_API_VERSION`.
-
-
-.. c:function:: PyObject* PyModule_Create2(PyModuleDef *def, int module_api_version)
-
- Create a new module object, given the definition in *def*, assuming the
- API version *module_api_version*. If that version does not match the version
- of the running interpreter, a :exc:`RuntimeWarning` is emitted.
-
- .. note::
-
- Most uses of this function should be using :c:func:`PyModule_Create`
- instead; only use this if you are sure you need it.
-
-Before it is returned from in the initialization function, the resulting module
-object is typically populated using functions like :c:func:`PyModule_AddObject`.
-
-.. _multi-phase-initialization:
+ :attr:`__file__` attribute. If this is not defined, or if it is not a string,
+ raise :exc:`SystemError` and return *NULL*.
-Multi-phase initialization
-..........................
-
-An alternate way to specify extensions is to request "multi-phase initialization".
-Extension modules created this way behave more like Python modules: the
-initialization is split between the *creation phase*, when the module object
-is created, and the *execution phase*, when it is populated.
-The distinction is similar to the :py:meth:`__new__` and :py:meth:`__init__` methods
-of classes.
-
-Unlike modules created using single-phase initialization, these modules are not
-singletons: if the *sys.modules* entry is removed and the module is re-imported,
-a new module object is created, and the old module is subject to normal garbage
-collection -- as with Python modules.
-By default, multiple modules created from the same definition should be
-independent: changes to one should not affect the others.
-This means that all state should be specific to the module object (using e.g.
-using :c:func:`PyModule_GetState`), or its contents (such as the module's
-:attr:`__dict__` or individual classes created with :c:func:`PyType_FromSpec`).
-
-All modules created using multi-phase initialization are expected to support
-:ref:`sub-interpreters <sub-interpreter-support>`. Making sure multiple modules
-are independent is typically enough to achieve this.
-
-To request multi-phase initialization, the initialization function
-(PyInit_modulename) returns a :c:type:`PyModuleDef` instance with non-empty
-:c:member:`~PyModuleDef.m_slots`. Before it is returned, the ``PyModuleDef``
-instance must be initialized with the following function:
-
-.. c:function:: PyObject* PyModuleDef_Init(PyModuleDef *def)
-
- Ensures a module definition is a properly initialized Python object that
- correctly reports its type and reference count.
-
- Returns *def* cast to ``PyObject*``, or ``NULL`` if an error occurred.
-
- .. versionadded:: 3.5
-
-The *m_slots* member of the module definition must point to an array of
-``PyModuleDef_Slot`` structures:
-
-.. c:type:: PyModuleDef_Slot
-
- .. c:member:: int slot
-
- A slot ID, chosen from the available values explained below.
-
- .. c:member:: void* value
-
- Value of the slot, whose meaning depends on the slot ID.
-
- .. versionadded:: 3.5
-
-The *m_slots* array must be terminated by a slot with id 0.
-
-The available slot types are:
-
-.. c:var:: Py_mod_create
-
- Specifies a function that is called to create the module object itself.
- The *value* pointer of this slot must point to a function of the signature:
-
- .. c:function:: PyObject* create_module(PyObject *spec, PyModuleDef *def)
-
- The function receives a :py:class:`~importlib.machinery.ModuleSpec`
- instance, as defined in :PEP:`451`, and the module definition.
- It should return a new module object, or set an error
- and return ``NULL``.
-
- This function should be kept minimal. In particular, it should not
- call arbitrary Python code, as trying to import the same module again may
- result in an infinite loop.
-
- Multiple ``Py_mod_create`` slots may not be specified in one module
- definition.
-
- If ``Py_mod_create`` is not specified, the import machinery will create
- a normal module object using :c:func:`PyModule_New`. The name is taken from
- *spec*, not the definition, to allow extension modules to dynamically adjust
- to their place in the module hierarchy and be imported under different
- names through symlinks, all while sharing a single module definition.
-
- There is no requirement for the returned object to be an instance of
- :c:type:`PyModule_Type`. Any type can be used, as long as it supports
- setting and getting import-related attributes.
- However, only ``PyModule_Type`` instances may be returned if the
- ``PyModuleDef`` has non-``NULL`` ``m_traverse``, ``m_clear``,
- ``m_free``; non-zero ``m_size``; or slots other than ``Py_mod_create``.
-
-.. c:var:: Py_mod_exec
-
- Specifies a function that is called to *execute* the module.
- This is equivalent to executing the code of a Python module: typically,
- this function adds classes and constants to the module.
- The signature of the function is:
-
- .. c:function:: int exec_module(PyObject* module)
-
- If multiple ``Py_mod_exec`` slots are specified, they are processed in the
- order they appear in the *m_slots* array.
-
-See :PEP:`489` for more details on multi-phase initialization.
-
-Low-level module creation functions
-...................................
-
-The following functions are called under the hood when using multi-phase
-initialization. They can be used directly, for example when creating module
-objects dynamically. Note that both ``PyModule_FromDefAndSpec`` and
-``PyModule_ExecDef`` must be called to fully initialize a module.
-
-.. c:function:: PyObject * PyModule_FromDefAndSpec(PyModuleDef *def, PyObject *spec)
-
- Create a new module object, given the definition in *module* and the
- ModuleSpec *spec*. This behaves like :c:func:`PyModule_FromDefAndSpec2`
- with *module_api_version* set to :const:`PYTHON_API_VERSION`.
-
- .. versionadded:: 3.5
-
-.. c:function:: PyObject * PyModule_FromDefAndSpec2(PyModuleDef *def, PyObject *spec, int module_api_version)
-
- Create a new module object, given the definition in *module* and the
- ModuleSpec *spec*, assuming the API version *module_api_version*.
- If that version does not match the version of the running interpreter,
- a :exc:`RuntimeWarning` is emitted.
-
- .. note::
-
- Most uses of this function should be using :c:func:`PyModule_FromDefAndSpec`
- instead; only use this if you are sure you need it.
-
- .. versionadded:: 3.5
-
-.. c:function:: int PyModule_ExecDef(PyObject *module, PyModuleDef *def)
-
- Process any execution slots (:c:data:`Py_mod_exec`) given in *def*.
-
- .. versionadded:: 3.5
-
-.. c:function:: int PyModule_SetDocString(PyObject *module, const char *docstring)
-
- Set the docstring for *module* to *docstring*.
- This function is called automatically when creating a module from
- ``PyModuleDef``, using either ``PyModule_Create`` or
- ``PyModule_FromDefAndSpec``.
-
- .. versionadded:: 3.5
-
-.. c:function:: int PyModule_AddFunctions(PyObject *module, PyMethodDef *functions)
-
- Add the functions from the ``NULL`` terminated *functions* array to *module*.
- Refer to the :c:type:`PyMethodDef` documentation for details on individual
- entries (due to the lack of a shared module namespace, module level
- "functions" implemented in C typically receive the module as their first
- parameter, making them similar to instance methods on Python classes).
- This function is called automatically when creating a module from
- ``PyModuleDef``, using either ``PyModule_Create`` or
- ``PyModule_FromDefAndSpec``.
-
- .. versionadded:: 3.5
-
-Support functions
-.................
-
-The module initialization function (if using single phase initialization) or
-a function called from a module execution slot (if using multi-phase
-initialization), can use the following functions to help initialize the module
-state:
.. c:function:: 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* on success. Return ``-1`` on error, ``0`` on success.
+ *value*. Return ``-1`` on error, ``0`` on success.
- .. note::
+ .. versionadded:: 2.0
- Unlike other functions that steal references, ``PyModule_AddObject()`` only
- decrements the reference count of *value* **on success**.
-
- This means that its return value must be checked, and calling code must
- :c:func:`Py_DECREF` *value* manually on error. Example usage::
-
- Py_INCREF(spam);
- if (PyModule_AddObject(module, "spam", spam) < 0) {
- Py_DECREF(module);
- Py_DECREF(spam);
- return NULL;
- }
.. c:function:: int PyModule_AddIntConstant(PyObject *module, const char *name, long value)
@@ -440,13 +93,16 @@ state:
used from the module's initialization function. Return ``-1`` on error, ``0`` on
success.
+ .. versionadded:: 2.0
+
.. c:function:: 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.
+ null-terminated. Return ``-1`` on error, ``0`` on success.
+ .. versionadded:: 2.0
.. c:function:: int PyModule_AddIntMacro(PyObject *module, macro)
@@ -455,51 +111,11 @@ state:
constant *AF_INET* with the value of *AF_INET* to *module*.
Return ``-1`` on error, ``0`` on success.
+ .. versionadded:: 2.6
.. c:function:: int PyModule_AddStringMacro(PyObject *module, macro)
Add a string constant to *module*.
+ .. versionadded:: 2.6
-Module lookup
-^^^^^^^^^^^^^
-
-Single-phase initialization creates singleton modules that can be looked up
-in the context of the current interpreter. This allows the module object to be
-retrieved later with only a reference to the module definition.
-
-These functions will not work on modules created using multi-phase initialization,
-since multiple such modules can be created from a single definition.
-
-.. c:function:: PyObject* PyState_FindModule(PyModuleDef *def)
-
- Returns the module object that was created from *def* for the current interpreter.
- This method requires that the module object has been attached to the interpreter state with
- :c:func:`PyState_AddModule` beforehand. In case the corresponding module object is not
- found or has not been attached to the interpreter state yet, it returns ``NULL``.
-
-.. c:function:: int PyState_AddModule(PyObject *module, PyModuleDef *def)
-
- Attaches the module object passed to the function to the interpreter state. This allows
- the module object to be accessible via :c:func:`PyState_FindModule`.
-
- Only effective on modules created using single-phase initialization.
-
- Python calls ``PyState_AddModule`` automatically after importing a module,
- so it is unnecessary (but harmless) to call it from module initialization
- code. An explicit call is needed only if the module's own init code
- subsequently calls ``PyState_FindModule``.
- The function is mainly intended for implementing alternative import
- mechanisms (either by calling it directly, or by referring to its
- implementation for details of the required state updates).
-
- Return 0 on success or -1 on failure.
-
- .. versionadded:: 3.3
-
-.. c:function:: int PyState_RemoveModule(PyModuleDef *def)
-
- Removes the module object created from *def* from the interpreter state.
- Return 0 on success or -1 on failure.
-
- .. versionadded:: 3.3
diff --git a/Doc/c-api/none.rst b/Doc/c-api/none.rst
index 26d2b7a..f8909b9 100644
--- a/Doc/c-api/none.rst
+++ b/Doc/c-api/none.rst
@@ -1,4 +1,4 @@
-.. highlight:: c
+.. highlightlang:: c
.. _noneobject:
@@ -22,5 +22,7 @@ same reason.
.. c:macro:: Py_RETURN_NONE
- Properly handle returning :c:data:`Py_None` from within a C function (that is,
- increment the reference count of ``None`` and return it.)
+ Properly handle returning :c:data:`Py_None` from within a C function.
+
+ .. versionadded:: 2.4
+
diff --git a/Doc/c-api/number.rst b/Doc/c-api/number.rst
index 620204c..39f0e78 100644
--- a/Doc/c-api/number.rst
+++ b/Doc/c-api/number.rst
@@ -1,4 +1,4 @@
-.. highlight:: c
+.. highlightlang:: c
.. _number:
@@ -11,54 +11,53 @@ Number Protocol
Returns ``1`` if the object *o* provides numeric protocols, and false otherwise.
This function always succeeds.
- .. versionchanged:: 3.8
- Returns ``1`` if *o* is an index integer.
-
.. c:function:: PyObject* PyNumber_Add(PyObject *o1, PyObject *o2)
- Returns the result of adding *o1* and *o2*, or ``NULL`` on failure. This is the
+ Returns the result of adding *o1* and *o2*, or *NULL* on failure. This is the
equivalent of the Python expression ``o1 + o2``.
.. c:function:: PyObject* PyNumber_Subtract(PyObject *o1, PyObject *o2)
- Returns the result of subtracting *o2* from *o1*, or ``NULL`` on failure. This is
+ Returns the result of subtracting *o2* from *o1*, or *NULL* on failure. This is
the equivalent of the Python expression ``o1 - o2``.
.. c:function:: PyObject* PyNumber_Multiply(PyObject *o1, PyObject *o2)
- Returns the result of multiplying *o1* and *o2*, or ``NULL`` on failure. This is
+ Returns the result of multiplying *o1* and *o2*, or *NULL* on failure. This is
the equivalent of the Python expression ``o1 * o2``.
-.. c:function:: PyObject* PyNumber_MatrixMultiply(PyObject *o1, PyObject *o2)
-
- Returns the result of matrix multiplication on *o1* and *o2*, or ``NULL`` on
- failure. This is the equivalent of the Python expression ``o1 @ o2``.
+.. c:function:: PyObject* PyNumber_Divide(PyObject *o1, PyObject *o2)
- .. versionadded:: 3.5
+ Returns the result of dividing *o1* by *o2*, or *NULL* on failure. This is the
+ equivalent of the Python expression ``o1 / o2``.
.. c:function:: PyObject* PyNumber_FloorDivide(PyObject *o1, PyObject *o2)
- Return the floor of *o1* divided by *o2*, or ``NULL`` on failure. This is
+ Return the floor of *o1* divided by *o2*, or *NULL* on failure. This is
equivalent to the "classic" division of integers.
+ .. versionadded:: 2.2
+
.. c:function:: PyObject* PyNumber_TrueDivide(PyObject *o1, PyObject *o2)
Return a reasonable approximation for the mathematical value of *o1* divided by
- *o2*, or ``NULL`` on failure. The return value is "approximate" because binary
+ *o2*, or *NULL* on failure. The return value is "approximate" because binary
floating point numbers are approximate; it is not possible to represent all real
numbers in base two. This function can return a floating point value when
passed two integers.
+ .. versionadded:: 2.2
+
.. c:function:: PyObject* PyNumber_Remainder(PyObject *o1, PyObject *o2)
- Returns the remainder of dividing *o1* by *o2*, or ``NULL`` on failure. This is
+ Returns the remainder of dividing *o1* by *o2*, or *NULL* on failure. This is
the equivalent of the Python expression ``o1 % o2``.
@@ -66,7 +65,7 @@ Number Protocol
.. index:: builtin: divmod
- See the built-in function :func:`divmod`. Returns ``NULL`` on failure. This is
+ See the built-in function :func:`divmod`. Returns *NULL* on failure. This is
the equivalent of the Python expression ``divmod(o1, o2)``.
@@ -74,21 +73,21 @@ Number Protocol
.. index:: builtin: pow
- See the built-in function :func:`pow`. Returns ``NULL`` on failure. This is the
+ See the built-in function :func:`pow`. Returns *NULL* on failure. This is the
equivalent of the Python expression ``pow(o1, o2, o3)``, where *o3* is optional.
- If *o3* is to be ignored, pass :c:data:`Py_None` in its place (passing ``NULL`` for
+ If *o3* is to be ignored, pass :c:data:`Py_None` in its place (passing *NULL* for
*o3* would cause an illegal memory access).
.. c:function:: PyObject* PyNumber_Negative(PyObject *o)
- Returns the negation of *o* on success, or ``NULL`` on failure. This is the
+ Returns the negation of *o* on success, or *NULL* on failure. This is the
equivalent of the Python expression ``-o``.
.. c:function:: PyObject* PyNumber_Positive(PyObject *o)
- Returns *o* on success, or ``NULL`` on failure. This is the equivalent of the
+ Returns *o* on success, or *NULL* on failure. This is the equivalent of the
Python expression ``+o``.
@@ -96,95 +95,97 @@ Number Protocol
.. index:: builtin: abs
- Returns the absolute value of *o*, or ``NULL`` on failure. This is the equivalent
+ Returns the absolute value of *o*, or *NULL* on failure. This is the equivalent
of the Python expression ``abs(o)``.
.. c:function:: PyObject* PyNumber_Invert(PyObject *o)
- Returns the bitwise negation of *o* on success, or ``NULL`` on failure. This is
+ Returns the bitwise negation of *o* on success, or *NULL* on failure. This is
the equivalent of the Python expression ``~o``.
.. c:function:: PyObject* PyNumber_Lshift(PyObject *o1, PyObject *o2)
- Returns the result of left shifting *o1* by *o2* on success, or ``NULL`` on
+ Returns the result of left shifting *o1* by *o2* on success, or *NULL* on
failure. This is the equivalent of the Python expression ``o1 << o2``.
.. c:function:: PyObject* PyNumber_Rshift(PyObject *o1, PyObject *o2)
- Returns the result of right shifting *o1* by *o2* on success, or ``NULL`` on
+ Returns the result of right shifting *o1* by *o2* on success, or *NULL* on
failure. This is the equivalent of the Python expression ``o1 >> o2``.
.. c:function:: PyObject* PyNumber_And(PyObject *o1, PyObject *o2)
- Returns the "bitwise and" of *o1* and *o2* on success and ``NULL`` on failure.
+ Returns the "bitwise and" of *o1* and *o2* on success and *NULL* on failure.
This is the equivalent of the Python expression ``o1 & o2``.
.. c:function:: PyObject* PyNumber_Xor(PyObject *o1, PyObject *o2)
- Returns the "bitwise exclusive or" of *o1* by *o2* on success, or ``NULL`` on
+ Returns the "bitwise exclusive or" of *o1* by *o2* on success, or *NULL* on
failure. This is the equivalent of the Python expression ``o1 ^ o2``.
.. c:function:: PyObject* PyNumber_Or(PyObject *o1, PyObject *o2)
- Returns the "bitwise or" of *o1* and *o2* on success, or ``NULL`` on failure.
+ Returns the "bitwise or" of *o1* and *o2* on success, or *NULL* on failure.
This is the equivalent of the Python expression ``o1 | o2``.
.. c:function:: PyObject* PyNumber_InPlaceAdd(PyObject *o1, PyObject *o2)
- Returns the result of adding *o1* and *o2*, or ``NULL`` on failure. The operation
+ Returns the result of adding *o1* and *o2*, or *NULL* on failure. The operation
is done *in-place* when *o1* supports it. This is the equivalent of the Python
statement ``o1 += o2``.
.. c:function:: PyObject* PyNumber_InPlaceSubtract(PyObject *o1, PyObject *o2)
- Returns the result of subtracting *o2* from *o1*, or ``NULL`` on failure. The
+ Returns the result of subtracting *o2* from *o1*, or *NULL* on failure. The
operation is done *in-place* when *o1* supports it. This is the equivalent of
the Python statement ``o1 -= o2``.
.. c:function:: PyObject* PyNumber_InPlaceMultiply(PyObject *o1, PyObject *o2)
- Returns the result of multiplying *o1* and *o2*, or ``NULL`` on failure. The
+ Returns the result of multiplying *o1* and *o2*, or *NULL* on failure. The
operation is done *in-place* when *o1* supports it. This is the equivalent of
the Python statement ``o1 *= o2``.
-.. c:function:: PyObject* PyNumber_InPlaceMatrixMultiply(PyObject *o1, PyObject *o2)
+.. c:function:: PyObject* PyNumber_InPlaceDivide(PyObject *o1, PyObject *o2)
- Returns the result of matrix multiplication on *o1* and *o2*, or ``NULL`` on
- failure. The operation is done *in-place* when *o1* supports it. This is
- the equivalent of the Python statement ``o1 @= o2``.
-
- .. versionadded:: 3.5
+ Returns the result of dividing *o1* by *o2*, or *NULL* on failure. The
+ operation is done *in-place* when *o1* supports it. This is the equivalent of
+ the Python statement ``o1 /= o2``.
.. c:function:: PyObject* PyNumber_InPlaceFloorDivide(PyObject *o1, PyObject *o2)
- Returns the mathematical floor of dividing *o1* by *o2*, or ``NULL`` on failure.
+ Returns the mathematical floor of dividing *o1* by *o2*, or *NULL* on failure.
The operation is done *in-place* when *o1* supports it. This is the equivalent
of the Python statement ``o1 //= o2``.
+ .. versionadded:: 2.2
+
.. c:function:: PyObject* PyNumber_InPlaceTrueDivide(PyObject *o1, PyObject *o2)
Return a reasonable approximation for the mathematical value of *o1* divided by
- *o2*, or ``NULL`` on failure. The return value is "approximate" because binary
+ *o2*, or *NULL* on failure. The return value is "approximate" because binary
floating point numbers are approximate; it is not possible to represent all real
numbers in base two. This function can return a floating point value when
passed two integers. The operation is done *in-place* when *o1* supports it.
+ .. versionadded:: 2.2
+
.. c:function:: PyObject* PyNumber_InPlaceRemainder(PyObject *o1, PyObject *o2)
- Returns the remainder of dividing *o1* by *o2*, or ``NULL`` on failure. The
+ Returns the remainder of dividing *o1* by *o2*, or *NULL* on failure. The
operation is done *in-place* when *o1* supports it. This is the equivalent of
the Python statement ``o1 %= o2``.
@@ -193,94 +194,129 @@ Number Protocol
.. index:: builtin: pow
- See the built-in function :func:`pow`. Returns ``NULL`` on failure. The operation
+ See the built-in function :func:`pow`. Returns *NULL* on failure. The operation
is done *in-place* when *o1* supports it. This is the equivalent of the Python
statement ``o1 **= o2`` when o3 is :c:data:`Py_None`, or an in-place variant of
``pow(o1, o2, o3)`` otherwise. If *o3* is to be ignored, pass :c:data:`Py_None`
- in its place (passing ``NULL`` for *o3* would cause an illegal memory access).
+ in its place (passing *NULL* for *o3* would cause an illegal memory access).
.. c:function:: PyObject* PyNumber_InPlaceLshift(PyObject *o1, PyObject *o2)
- Returns the result of left shifting *o1* by *o2* on success, or ``NULL`` on
+ Returns the result of left shifting *o1* by *o2* on success, or *NULL* on
failure. The operation is done *in-place* when *o1* supports it. This is the
equivalent of the Python statement ``o1 <<= o2``.
.. c:function:: PyObject* PyNumber_InPlaceRshift(PyObject *o1, PyObject *o2)
- Returns the result of right shifting *o1* by *o2* on success, or ``NULL`` on
+ Returns the result of right shifting *o1* by *o2* on success, or *NULL* on
failure. The operation is done *in-place* when *o1* supports it. This is the
equivalent of the Python statement ``o1 >>= o2``.
.. c:function:: PyObject* PyNumber_InPlaceAnd(PyObject *o1, PyObject *o2)
- Returns the "bitwise and" of *o1* and *o2* on success and ``NULL`` on failure. The
+ Returns the "bitwise and" 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 statement ``o1 &= o2``.
.. c:function:: PyObject* PyNumber_InPlaceXor(PyObject *o1, PyObject *o2)
- Returns the "bitwise exclusive or" of *o1* by *o2* on success, or ``NULL`` on
+ Returns the "bitwise exclusive or" of *o1* by *o2* on success, or *NULL* on
failure. The operation is done *in-place* when *o1* supports it. This is the
equivalent of the Python statement ``o1 ^= o2``.
.. c:function:: PyObject* PyNumber_InPlaceOr(PyObject *o1, PyObject *o2)
- Returns the "bitwise or" of *o1* and *o2* on success, or ``NULL`` on failure. The
+ Returns the "bitwise or" of *o1* and *o2* on success, or *NULL* on failure. The
operation is done *in-place* when *o1* supports it. This is the equivalent of
the Python statement ``o1 |= o2``.
-.. c:function:: PyObject* PyNumber_Long(PyObject *o)
+.. c:function:: int PyNumber_Coerce(PyObject **p1, PyObject **p2)
+
+ .. index:: builtin: coerce
+
+ This function takes the addresses of two variables of type :c:type:`PyObject\*`.
+ If the objects pointed to by ``*p1`` and ``*p2`` have the same type, increment
+ their reference count and return ``0`` (success). If the objects can be
+ converted to a common numeric type, replace ``*p1`` and ``*p2`` by their
+ converted value (with 'new' reference counts), and return ``0``. If no
+ conversion is possible, or if some other error occurs, return ``-1`` (failure)
+ and don't increment the reference counts. The call ``PyNumber_Coerce(&o1,
+ &o2)`` is equivalent to the Python statement ``o1, o2 = coerce(o1, o2)``.
+
+
+.. c:function:: int PyNumber_CoerceEx(PyObject **p1, PyObject **p2)
+
+ This function is similar to :c:func:`PyNumber_Coerce`, except that it returns
+ ``1`` when the conversion is not possible and when no error is raised.
+ Reference counts are still not increased in this case.
+
+
+.. c:function:: PyObject* PyNumber_Int(PyObject *o)
.. index:: builtin: int
- Returns the *o* converted to an integer object on success, or ``NULL`` on
- failure. This is the equivalent of the Python expression ``int(o)``.
+ Returns the *o* converted to an integer object on success, or *NULL* on failure.
+ If the argument is outside the integer range a long object will be returned
+ instead. This is the equivalent of the Python expression ``int(o)``.
+
+
+.. c:function:: PyObject* PyNumber_Long(PyObject *o)
+
+ .. index:: builtin: long
+
+ Returns the *o* converted to a long integer object on success, or *NULL* on
+ failure. This is the equivalent of the Python expression ``long(o)``.
.. c:function:: PyObject* PyNumber_Float(PyObject *o)
.. index:: builtin: float
- Returns the *o* converted to a float object on success, or ``NULL`` on failure.
+ Returns the *o* converted to a float object on success, or *NULL* on failure.
This is the equivalent of the Python expression ``float(o)``.
.. c:function:: PyObject* PyNumber_Index(PyObject *o)
- Returns the *o* converted to a Python int on success or ``NULL`` with a
+ Returns the *o* converted to a Python int or long on success or *NULL* with a
:exc:`TypeError` exception raised on failure.
+ .. versionadded:: 2.5
+
.. c:function:: PyObject* PyNumber_ToBase(PyObject *n, int base)
- Returns the integer *n* converted to base *base* as a string. The *base*
- argument must be one of 2, 8, 10, or 16. For base 2, 8, or 16, the
- returned string is prefixed with a base marker of ``'0b'``, ``'0o'``, or
- ``'0x'``, respectively. If *n* is not a Python int, it is converted with
+ Returns the integer *n* converted to *base* as a string with a base
+ marker of ``'0b'``, ``'0o'``, or ``'0x'`` if applicable. When
+ *base* is not 2, 8, 10, or 16, the format is ``'x#num'`` where x is the
+ base. If *n* is not an int object, it is converted with
:c:func:`PyNumber_Index` first.
+ .. versionadded:: 2.6
+
.. c:function:: Py_ssize_t PyNumber_AsSsize_t(PyObject *o, PyObject *exc)
Returns *o* converted to a Py_ssize_t value if *o* can be interpreted as an
- integer. If the call fails, an exception is raised and ``-1`` is returned.
-
- If *o* can be converted to a Python int but the attempt to
+ integer. If *o* can be converted to a Python int or long but the attempt to
convert to a Py_ssize_t value would raise an :exc:`OverflowError`, then the
*exc* argument is the type of exception that will be raised (usually
- :exc:`IndexError` or :exc:`OverflowError`). If *exc* is ``NULL``, then the
- exception is cleared and the value is clipped to ``PY_SSIZE_T_MIN`` for a negative
- integer or ``PY_SSIZE_T_MAX`` for a positive integer.
+ :exc:`IndexError` or :exc:`OverflowError`). If *exc* is *NULL*, then the
+ exception is cleared and the value is clipped to *PY_SSIZE_T_MIN* for a negative
+ integer or *PY_SSIZE_T_MAX* for a positive integer.
+
+ .. versionadded:: 2.5
.. c:function:: int PyIndex_Check(PyObject *o)
Returns ``1`` if *o* is an index integer (has the nb_index slot of the
tp_as_number structure filled in), and ``0`` otherwise.
- This function always succeeds.
+
+ .. versionadded:: 2.5
diff --git a/Doc/c-api/objbuffer.rst b/Doc/c-api/objbuffer.rst
index 6b82a64..c5228c6 100644
--- a/Doc/c-api/objbuffer.rst
+++ b/Doc/c-api/objbuffer.rst
@@ -1,21 +1,16 @@
-.. highlight:: c
+.. highlightlang:: c
-Old Buffer Protocol
--------------------
+.. _abstract-buffer:
-.. deprecated:: 3.0
-These functions were part of the "old buffer protocol" API in Python 2.
-In Python 3, this protocol doesn't exist anymore but the functions are still
-exposed to ease porting 2.x code. They act as a compatibility wrapper
-around the :ref:`new buffer protocol <bufferobjects>`, but they don't give
-you control over the lifetime of the resources acquired when a buffer is
-exported.
+Old Buffer Protocol
+===================
-Therefore, it is recommended that you call :c:func:`PyObject_GetBuffer`
-(or the ``y*`` or ``w*`` :ref:`format codes <arg-parsing>` with the
-:c:func:`PyArg_ParseTuple` family of functions) to get a buffer view over
-an object, and :c:func:`PyBuffer_Release` when the buffer view can be released.
+This section describes the legacy buffer protocol, which has been introduced
+in Python 1.6. It is still supported but deprecated in the Python 2.x series.
+Python 3 introduces a new buffer protocol which fixes weaknesses and
+shortcomings of the protocol, and has been backported to Python 2.6. See
+:ref:`bufferobjects` for more information.
.. c:function:: int PyObject_AsCharBuffer(PyObject *obj, const char **buffer, Py_ssize_t *buffer_len)
@@ -26,6 +21,12 @@ an object, and :c:func:`PyBuffer_Release` when the buffer view can be released.
and *buffer_len* to the buffer length. Returns ``-1`` and sets a
:exc:`TypeError` on error.
+ .. versionadded:: 1.6
+
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int *` type for *buffer_len*. This might
+ require changes in your code for properly supporting 64-bit systems.
+
.. c:function:: int PyObject_AsReadBuffer(PyObject *obj, const void **buffer, Py_ssize_t *buffer_len)
@@ -35,21 +36,31 @@ an object, and :c:func:`PyBuffer_Release` when the buffer view can be released.
and *buffer_len* to the buffer length. Returns ``-1`` and sets a
:exc:`TypeError` on error.
+ .. versionadded:: 1.6
+
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int *` type for *buffer_len*. This might
+ require changes in your code for properly supporting 64-bit systems.
+
.. c:function:: int PyObject_CheckReadBuffer(PyObject *o)
Returns ``1`` if *o* supports the single-segment readable buffer interface.
- Otherwise returns ``0``. This function always succeeds.
+ Otherwise returns ``0``.
- Note that this function tries to get and release a buffer, and exceptions
- which occur while calling corresponding functions will get suppressed.
- To get error reporting use :c:func:`PyObject_GetBuffer()` instead.
+ .. versionadded:: 2.2
.. c:function:: int PyObject_AsWriteBuffer(PyObject *obj, void **buffer, Py_ssize_t *buffer_len)
- Returns a pointer to a writable memory location. The *obj* argument must
+ Returns a pointer to a writeable memory location. The *obj* argument must
support the single-segment, character buffer interface. On success,
returns ``0``, sets *buffer* to the memory location and *buffer_len* to the
buffer length. Returns ``-1`` and sets a :exc:`TypeError` on error.
+ .. versionadded:: 1.6
+
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int *` type for *buffer_len*. This might
+ require changes in your code for properly supporting 64-bit systems.
+
diff --git a/Doc/c-api/object.rst b/Doc/c-api/object.rst
index ca9db1a..295a129 100644
--- a/Doc/c-api/object.rst
+++ b/Doc/c-api/object.rst
@@ -1,4 +1,4 @@
-.. highlight:: c
+.. highlightlang:: c
.. _object:
@@ -6,19 +6,6 @@ Object Protocol
===============
-.. c:var:: PyObject* Py_NotImplemented
-
- The ``NotImplemented`` singleton, used to signal that an operation is
- not implemented for the given type combination.
-
-
-.. c:macro:: Py_RETURN_NOTIMPLEMENTED
-
- Properly handle returning :c:data:`Py_NotImplemented` from within a C
- function (that is, increment the reference count of NotImplemented and
- return it).
-
-
.. c:function:: int PyObject_Print(PyObject *o, FILE *fp, int flags)
Print an object *o*, on file *fp*. Returns ``-1`` on error. The flags argument
@@ -33,10 +20,6 @@ Object Protocol
is equivalent to the Python expression ``hasattr(o, attr_name)``. This function
always succeeds.
- Note that exceptions which occur while calling :meth:`__getattr__` and
- :meth:`__getattribute__` methods will get suppressed.
- To get error reporting use :c:func:`PyObject_GetAttr()` instead.
-
.. c:function:: int PyObject_HasAttrString(PyObject *o, const char *attr_name)
@@ -44,23 +27,18 @@ Object Protocol
is equivalent to the Python expression ``hasattr(o, attr_name)``. This function
always succeeds.
- Note that exceptions which occur while calling :meth:`__getattr__` and
- :meth:`__getattribute__` methods and creating a temporary string object
- will get suppressed.
- To get error reporting use :c:func:`PyObject_GetAttrString()` instead.
-
.. c:function:: PyObject* PyObject_GetAttr(PyObject *o, PyObject *attr_name)
Retrieve an attribute named *attr_name* from object *o*. Returns the attribute
- value on success, or ``NULL`` on failure. This is the equivalent of the Python
+ value on success, or *NULL* on failure. This is the equivalent of the Python
expression ``o.attr_name``.
.. c:function:: PyObject* PyObject_GetAttrString(PyObject *o, const char *attr_name)
Retrieve an attribute named *attr_name* from object *o*. Returns the attribute
- value on success, or ``NULL`` on failure. This is the equivalent of the Python
+ value on success, or *NULL* on failure. This is the equivalent of the Python
expression ``o.attr_name``.
@@ -81,7 +59,7 @@ Object Protocol
return ``0`` on success. This is the equivalent of the Python statement
``o.attr_name = v``.
- If *v* is ``NULL``, the attribute is deleted, however this feature is
+ If *v* is *NULL*, the attribute is deleted, however this feature is
deprecated in favour of using :c:func:`PyObject_DelAttr`.
@@ -92,7 +70,7 @@ Object Protocol
return ``0`` on success. This is the equivalent of the Python statement
``o.attr_name = v``.
- If *v* is ``NULL``, the attribute is deleted, however this feature is
+ If *v* is *NULL*, the attribute is deleted, however this feature is
deprecated in favour of using :c:func:`PyObject_DelAttrString`.
@@ -120,22 +98,6 @@ Object Protocol
This is the equivalent of the Python statement ``del o.attr_name``.
-.. c:function:: PyObject* PyObject_GenericGetDict(PyObject *o, void *context)
-
- A generic implementation for the getter of a ``__dict__`` descriptor. It
- creates the dictionary if necessary.
-
- .. versionadded:: 3.3
-
-
-.. c:function:: int PyObject_GenericSetDict(PyObject *o, void *context)
-
- A generic implementation for the setter of a ``__dict__`` descriptor. This
- implementation does not allow the dictionary to be deleted.
-
- .. versionadded:: 3.3
-
-
.. c:function:: PyObject* PyObject_RichCompare(PyObject *o1, PyObject *o2, int opid)
Compare the values of *o1* and *o2* using the operation specified by *opid*,
@@ -143,7 +105,7 @@ Object Protocol
:const:`Py_NE`, :const:`Py_GT`, or :const:`Py_GE`, corresponding to ``<``,
``<=``, ``==``, ``!=``, ``>``, or ``>=`` respectively. This is the equivalent of
the Python expression ``o1 op o2``, where ``op`` is the operator corresponding
- to *opid*. Returns the value of the comparison on success, or ``NULL`` on failure.
+ to *opid*. Returns the value of the comparison on success, or *NULL* on failure.
.. c:function:: int PyObject_RichCompareBool(PyObject *o1, PyObject *o2, int opid)
@@ -160,113 +122,203 @@ Object Protocol
If *o1* and *o2* are the same object, :c:func:`PyObject_RichCompareBool`
will always return ``1`` for :const:`Py_EQ` and ``0`` for :const:`Py_NE`.
-.. c:function:: PyObject* PyObject_Repr(PyObject *o)
+.. c:function:: int PyObject_Cmp(PyObject *o1, PyObject *o2, int *result)
- .. index:: builtin: repr
+ .. index:: builtin: cmp
- Compute a string representation of object *o*. Returns the string
- representation on success, ``NULL`` on failure. This is the equivalent of the
- Python expression ``repr(o)``. Called by the :func:`repr` built-in function.
+ Compare the values of *o1* and *o2* using a routine provided by *o1*, if one
+ exists, otherwise with a routine provided by *o2*. The result of the comparison
+ is returned in *result*. Returns ``-1`` on failure. This is the equivalent of
+ the Python statement ``result = cmp(o1, o2)``.
- .. versionchanged:: 3.4
- This function now includes a debug assertion to help ensure that it
- does not silently discard an active exception.
-.. c:function:: PyObject* PyObject_ASCII(PyObject *o)
+.. c:function:: int PyObject_Compare(PyObject *o1, PyObject *o2)
- .. index:: builtin: ascii
+ .. index:: builtin: cmp
- As :c:func:`PyObject_Repr`, compute a string representation of object *o*, but
- escape the non-ASCII characters in the string returned by
- :c:func:`PyObject_Repr` with ``\x``, ``\u`` or ``\U`` escapes. This generates
- a string similar to that returned by :c:func:`PyObject_Repr` in Python 2.
- Called by the :func:`ascii` built-in function.
+ Compare the values of *o1* and *o2* using a routine provided by *o1*, if one
+ exists, otherwise with a routine provided by *o2*. Returns the result of the
+ comparison on success. On error, the value returned is undefined; use
+ :c:func:`PyErr_Occurred` to detect an error. This is equivalent to the Python
+ expression ``cmp(o1, o2)``.
- .. index:: string; PyObject_Str (C function)
+.. c:function:: PyObject* PyObject_Repr(PyObject *o)
-.. c:function:: PyObject* PyObject_Str(PyObject *o)
+ .. index:: builtin: repr
Compute a string representation of object *o*. Returns the string
- representation on success, ``NULL`` on failure. This is the equivalent of the
- Python expression ``str(o)``. Called by the :func:`str` built-in function
- and, therefore, by the :func:`print` function.
+ representation on success, *NULL* on failure. This is the equivalent of the
+ Python expression ``repr(o)``. Called by the :func:`repr` built-in function and
+ by reverse quotes.
+
+
+.. c:function:: PyObject* PyObject_Str(PyObject *o)
+
+ .. index:: builtin: str
- .. versionchanged:: 3.4
- This function now includes a debug assertion to help ensure that it
- does not silently discard an active exception.
+ Compute a string representation of object *o*. Returns the string
+ representation on success, *NULL* on failure. This is the equivalent of the
+ Python expression ``str(o)``. Called by the :func:`str` built-in function and
+ by the :keyword:`print` statement.
.. c:function:: PyObject* PyObject_Bytes(PyObject *o)
.. index:: builtin: bytes
- Compute a bytes representation of object *o*. ``NULL`` is returned on
- failure and a bytes object on success. This is equivalent to the Python
- expression ``bytes(o)``, when *o* is not an integer. Unlike ``bytes(o)``,
- a TypeError is raised when *o* is an integer instead of a zero-initialized
- bytes object.
+ Compute a bytes representation of object *o*. In 2.x, this is just an alias
+ for :c:func:`PyObject_Str`.
+
+
+.. c:function:: PyObject* PyObject_Unicode(PyObject *o)
+
+ .. index:: builtin: unicode
+
+ 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 :func:`unicode` built-in
+ function.
+
+
+.. c: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, :c:func:`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 :attr:`~instance.__class__` attribute --- the
+ class relationship of the value of that attribute with *cls* will be used
+ to determine the result of this function.
+
+ .. versionadded:: 2.1
+
+ .. versionchanged:: 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 :class:`A` and :class:`B` are class objects, :class:`B` is a subclass of
+:class:`A` if it inherits from :class:`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*, :c:func:`PyObject_IsSubclass` returns true. If *A* and *B*
+are different objects, *B*'s :attr:`~class.__bases__` attribute is searched in
+a depth-first fashion for *A* --- the presence of the :attr:`~class.__bases__`
+attribute is considered sufficient for this determination.
.. c:function:: int PyObject_IsSubclass(PyObject *derived, PyObject *cls)
- Return ``1`` if the class *derived* is identical to or derived from the class
- *cls*, otherwise return ``0``. In case of an error, return ``-1``.
+ 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.
- 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``.
+ .. versionadded:: 2.1
- If *cls* has a :meth:`~class.__subclasscheck__` method, it will be called to
- determine the subclass status as described in :pep:`3119`. Otherwise,
- *derived* is a subclass of *cls* if it is a direct or indirect subclass,
- i.e. contained in ``cls.__mro__``.
+ .. versionchanged:: 2.3
+ Older versions of Python did not support a tuple as the second argument.
- Normally only class objects, i.e. instances of :class:`type` or a derived
- class, are considered classes. However, objects can override this by having
- a :attr:`__bases__` attribute (which must be a tuple of base classes).
+.. c:function:: int PyCallable_Check(PyObject *o)
-.. c:function:: int PyObject_IsInstance(PyObject *inst, PyObject *cls)
+ Determine if the object *o* is callable. Return ``1`` if the object is callable
+ and ``0`` otherwise. This function always succeeds.
+
+
+.. c:function:: PyObject* PyObject_Call(PyObject *callable_object, PyObject *args, PyObject *kw)
+
+ .. index:: builtin: apply
+
+ 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)``.
+
+ .. versionadded:: 2.2
+
+
+.. c:function:: PyObject* PyObject_CallObject(PyObject *callable_object, PyObject *args)
+
+ .. index:: builtin: apply
+
+ 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)``.
+
+
+.. c:function:: PyObject* PyObject_CallFunction(PyObject *callable, char *format, ...)
+
+ .. index:: builtin: apply
- Return ``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.
+ Call a callable Python object *callable*, with a variable number of C arguments.
+ The C arguments are described using a :c:func:`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 :c:type:`PyObject \*` args,
+ :c:func:`PyObject_CallFunctionObjArgs` is a faster alternative.
- 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 *cls* has a :meth:`~class.__instancecheck__` method, it will be called to
- determine the subclass status as described in :pep:`3119`. Otherwise, *inst*
- is an instance of *cls* if its class is a subclass of *cls*.
+.. c:function:: PyObject* PyObject_CallMethod(PyObject *o, char *method, char *format, ...)
- An instance *inst* can override what is considered its class by having a
- :attr:`__class__` attribute.
+ Call the method named *method* of object *o* with a variable number of C
+ arguments. The C arguments are described by a :c:func:`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 :c:type:`PyObject \*` args,
+ :c:func:`PyObject_CallMethodObjArgs` is a faster alternative.
- An object *cls* can override if it is considered a class, and what its base
- classes are, by having a :attr:`__bases__` attribute (which must be a tuple
- of base classes).
+.. c:function:: PyObject* PyObject_CallFunctionObjArgs(PyObject *callable, ..., NULL)
-.. c:function:: Py_hash_t PyObject_Hash(PyObject *o)
+ Call a callable Python object *callable*, with a variable number of
+ :c:type:`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.
+
+ .. versionadded:: 2.2
+
+
+.. c:function:: PyObject* PyObject_CallMethodObjArgs(PyObject *o, PyObject *name, ..., NULL)
+
+ 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
+ :c:type:`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.
+
+ .. versionadded:: 2.2
+
+
+.. c:function:: long PyObject_Hash(PyObject *o)
.. index:: builtin: hash
Compute and return the hash value of an object *o*. On failure, return ``-1``.
This is the equivalent of the Python expression ``hash(o)``.
- .. versionchanged:: 3.2
- The return type is now Py_hash_t. This is a signed integer the same size
- as Py_ssize_t.
-
-.. c:function:: Py_hash_t PyObject_HashNotImplemented(PyObject *o)
+.. c:function:: long PyObject_HashNotImplemented(PyObject *o)
Set a :exc:`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.
+ .. versionadded:: 2.6
+
.. c:function:: int PyObject_IsTrue(PyObject *o)
@@ -286,8 +338,8 @@ Object Protocol
.. index:: builtin: type
- When *o* is non-``NULL``, returns a type object corresponding to the object type
- of object *o*. On failure, raises :exc:`SystemError` and returns ``NULL``. This
+ When *o* is non-*NULL*, returns a type object corresponding to the object type
+ of object *o*. On failure, raises :exc:`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
@@ -298,11 +350,13 @@ Object Protocol
.. c:function:: 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``.
+ parameters must be non-*NULL*.
+
+ .. versionadded:: 2.2
-.. c:function:: Py_ssize_t PyObject_Size(PyObject *o)
- Py_ssize_t PyObject_Length(PyObject *o)
+.. c:function:: Py_ssize_t PyObject_Length(PyObject *o)
+ Py_ssize_t PyObject_Size(PyObject *o)
.. index:: builtin: len
@@ -310,20 +364,14 @@ Object Protocol
and mapping protocols, the sequence length is returned. On error, ``-1`` is
returned. This is the equivalent to the Python expression ``len(o)``.
-
-.. c:function:: Py_ssize_t PyObject_LengthHint(PyObject *o, Py_ssize_t default)
-
- Return an estimated length for the object *o*. First try to return its
- actual length, then an estimate using :meth:`~object.__length_hint__`, and
- finally return the default value. On error return ``-1``. This is the
- equivalent to the Python expression ``operator.length_hint(o, default)``.
-
- .. versionadded:: 3.4
+ .. versionchanged:: 2.5
+ These functions returned an :c:type:`int` type. This might require
+ changes in your code for properly supporting 64-bit systems.
.. c:function:: PyObject* PyObject_GetItem(PyObject *o, PyObject *key)
- Return element of *o* corresponding to the object *key* or ``NULL`` on failure.
+ Return element of *o* corresponding to the object *key* or *NULL* on failure.
This is the equivalent of the Python expression ``o[key]``.
@@ -336,22 +384,30 @@ Object Protocol
.. c:function:: int PyObject_DelItem(PyObject *o, PyObject *key)
- Remove the mapping for the object *key* from the object *o*. Return ``-1``
- on failure. This is equivalent to the Python statement ``del o[key]``.
+ Delete the mapping for *key* from *o*. Returns ``-1`` on failure. This is the
+ equivalent of the Python statement ``del o[key]``.
+
+
+.. c:function:: 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 :meth:`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.
.. c:function:: PyObject* PyObject_Dir(PyObject *o)
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()``,
+ 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 :c:func:`PyErr_Occurred` will return false.
+ is active then *NULL* is returned but :c:func:`PyErr_Occurred` will return false.
.. c:function:: PyObject* PyObject_GetIter(PyObject *o)
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 :exc:`TypeError` and returns ``NULL`` if the object cannot be
+ an iterator. Raises :exc:`TypeError` and returns *NULL* if the object cannot be
iterated.
diff --git a/Doc/c-api/objimpl.rst b/Doc/c-api/objimpl.rst
index 8bd8c10..b335188 100644
--- a/Doc/c-api/objimpl.rst
+++ b/Doc/c-api/objimpl.rst
@@ -1,4 +1,5 @@
-.. highlight:: c
+.. highlightlang:: c
+
.. _newtypes:
diff --git a/Doc/c-api/refcounting.rst b/Doc/c-api/refcounting.rst
index 0df12c6..49675a8 100644
--- a/Doc/c-api/refcounting.rst
+++ b/Doc/c-api/refcounting.rst
@@ -1,4 +1,4 @@
-.. highlight:: c
+.. highlightlang:: c
.. _countingrefs:
@@ -13,22 +13,22 @@ objects.
.. c:function:: 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 :c:func:`Py_XINCREF`.
+ Increment the reference count for object *o*. The object must not be *NULL*; if
+ you aren't sure that it isn't *NULL*, use :c:func:`Py_XINCREF`.
.. c:function:: void Py_XINCREF(PyObject *o)
- Increment the reference count for object *o*. The object may be ``NULL``, in
+ Increment the reference count for object *o*. The object may be *NULL*, in
which case the macro has no effect.
.. c:function:: 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 :c:func:`Py_XDECREF`. If the reference
+ Decrement the reference count for object *o*. The object must not be *NULL*; if
+ you aren't sure that it isn't *NULL*, use :c:func:`Py_XDECREF`. If the reference
count reaches zero, the object's type's deallocation function (which must not be
- ``NULL``) is invoked.
+ *NULL*) is invoked.
.. warning::
@@ -44,23 +44,24 @@ objects.
.. c:function:: void Py_XDECREF(PyObject *o)
- Decrement the reference count for object *o*. The object may be ``NULL``, in
+ 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
:c:func:`Py_DECREF`, and the same warning applies.
.. c:function:: void Py_CLEAR(PyObject *o)
- Decrement the reference count for object *o*. The object may be ``NULL``, in
+ 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
- :c:func:`Py_DECREF`, except that the argument is also set to ``NULL``. The warning
+ :c:func:`Py_DECREF`, except that the argument is also set to *NULL*. The warning
for :c:func:`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``
+ 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 reference
- count of an object that might be traversed during garbage collection.
+ It is a good idea to use this macro whenever decrementing the value of a
+ variable that might be traversed during garbage collection.
+ .. versionadded:: 2.4
The following functions are for runtime dynamic embedding of Python:
``Py_IncRef(PyObject *o)``, ``Py_DecRef(PyObject *o)``. They are
diff --git a/Doc/c-api/reflection.rst b/Doc/c-api/reflection.rst
index 1d86de6..59edbb3 100644
--- a/Doc/c-api/reflection.rst
+++ b/Doc/c-api/reflection.rst
@@ -1,4 +1,4 @@
-.. highlight:: c
+.. highlightlang:: c
.. _reflection:
@@ -14,18 +14,18 @@ Reflection
.. c:function:: PyObject* PyEval_GetLocals()
Return a dictionary of the local variables in the current execution frame,
- or ``NULL`` if no frame is currently executing.
+ or *NULL* if no frame is currently executing.
.. c:function:: PyObject* PyEval_GetGlobals()
Return a dictionary of the global variables in the current execution frame,
- or ``NULL`` if no frame is currently executing.
+ or *NULL* if no frame is currently executing.
.. c:function:: PyFrameObject* PyEval_GetFrame()
- Return the current thread state's frame, which is ``NULL`` if no frame is
+ Return the current thread state's frame, which is *NULL* if no frame is
currently executing.
@@ -34,6 +34,12 @@ Reflection
Return the line number that *frame* is currently executing.
+.. c:function:: int PyEval_GetRestricted()
+
+ If there is a current frame and it is executing in restricted mode, return true,
+ otherwise false.
+
+
.. c:function:: const char* PyEval_GetFuncName(PyObject *func)
Return the name of *func* if it is a function, class or instance object, else the
diff --git a/Doc/c-api/sequence.rst b/Doc/c-api/sequence.rst
index 6581885..d95ef23 100644
--- a/Doc/c-api/sequence.rst
+++ b/Doc/c-api/sequence.rst
@@ -1,4 +1,4 @@
-.. highlight:: c
+.. highlightlang:: c
.. _sequence:
@@ -9,10 +9,7 @@ Sequence Protocol
.. c:function:: int PySequence_Check(PyObject *o)
Return ``1`` if the object provides sequence protocol, and ``0`` otherwise.
- Note that it returns ``1`` for Python classes with a :meth:`__getitem__`
- method unless they are :class:`dict` subclasses since in general case it
- is impossible to determine what the type of keys it supports. This
- function always succeeds.
+ This function always succeeds.
.. c:function:: Py_ssize_t PySequence_Size(PyObject *o)
@@ -23,44 +20,64 @@ Sequence Protocol
Returns the number of objects in sequence *o* on success, and ``-1`` on
failure. This is equivalent to the Python expression ``len(o)``.
+ .. versionchanged:: 2.5
+ These functions returned an :c:type:`int` type. This might require
+ changes in your code for properly supporting 64-bit systems.
+
.. c:function:: PyObject* PySequence_Concat(PyObject *o1, PyObject *o2)
- Return the concatenation of *o1* and *o2* on success, and ``NULL`` on failure.
+ Return the concatenation of *o1* and *o2* on success, and *NULL* on failure.
This is the equivalent of the Python expression ``o1 + o2``.
.. c:function:: PyObject* PySequence_Repeat(PyObject *o, Py_ssize_t count)
- Return the result of repeating sequence object *o* *count* times, or ``NULL`` on
+ Return the result of repeating sequence object *o* *count* times, or *NULL* on
failure. This is the equivalent of the Python expression ``o * count``.
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` type for *count*. This might require
+ changes in your code for properly supporting 64-bit systems.
+
.. c:function:: PyObject* PySequence_InPlaceConcat(PyObject *o1, PyObject *o2)
- Return the concatenation of *o1* and *o2* on success, and ``NULL`` on failure.
+ 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``.
.. c:function:: PyObject* PySequence_InPlaceRepeat(PyObject *o, Py_ssize_t count)
- Return the result of repeating sequence object *o* *count* times, or ``NULL`` on
+ 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``.
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` type for *count*. This might require
+ changes in your code for properly supporting 64-bit systems.
+
.. c:function:: PyObject* PySequence_GetItem(PyObject *o, Py_ssize_t i)
- Return the *i*\ th element of *o*, or ``NULL`` on failure. This is the equivalent of
+ Return the *i*\ th element of *o*, or *NULL* on failure. This is the equivalent of
the Python expression ``o[i]``.
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` type for *i*. This might require
+ changes in your code for properly supporting 64-bit systems.
+
.. c:function:: PyObject* PySequence_GetSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2)
- Return the slice of sequence object *o* between *i1* and *i2*, or ``NULL`` on
+ 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]``.
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` type for *i1* and *i2*. This might
+ require changes in your code for properly supporting 64-bit systems.
+
.. c:function:: int PySequence_SetItem(PyObject *o, Py_ssize_t i, PyObject *v)
@@ -69,20 +86,36 @@ Sequence Protocol
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
+ If *v* is *NULL*, the element is deleted, however this feature is
deprecated in favour of using :c:func:`PySequence_DelItem`.
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` type for *i*. This might require
+ changes in your code for properly supporting 64-bit systems.
+
.. c:function:: int PySequence_DelItem(PyObject *o, Py_ssize_t i)
Delete the *i*\ th element of object *o*. Returns ``-1`` on failure. This is the
equivalent of the Python statement ``del o[i]``.
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` type for *i*. This might require
+ changes in your code for properly supporting 64-bit systems.
+
.. c:function:: int PySequence_SetSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2, PyObject *v)
Assign the sequence object *v* to the slice in sequence object *o* from *i1* to
- *i2*. This is the equivalent of the Python statement ``o[i1:i2] = v``.
+ *i2*. Raise an exception and return ``-1`` on failure; return ``0`` on success.
+ This is the equivalent of the Python statement ``o[i1:i2] = v``.
+
+ If *v* is *NULL*, the slice is deleted, however this feature is
+ deprecated in favour of using :c:func:`PySequence_DelSlice`.
+
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` type for *i1* and *i2*. This might
+ require changes in your code for properly supporting 64-bit systems.
.. c:function:: int PySequence_DelSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2)
@@ -90,6 +123,10 @@ Sequence Protocol
Delete the slice in sequence object *o* from *i1* to *i2*. Returns ``-1`` on
failure. This is the equivalent of the Python statement ``del o[i1:i2]``.
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` type for *i1* and *i2*. This might
+ require changes in your code for properly supporting 64-bit systems.
+
.. c:function:: Py_ssize_t PySequence_Count(PyObject *o, PyObject *value)
@@ -97,6 +134,10 @@ Sequence Protocol
of keys for which ``o[key] == value``. On failure, return ``-1``. This is
equivalent to the Python expression ``o.count(value)``.
+ .. versionchanged:: 2.5
+ This function returned an :c:type:`int` type. This might require changes
+ in your code for properly supporting 64-bit systems.
+
.. c:function:: int PySequence_Contains(PyObject *o, PyObject *value)
@@ -110,67 +151,75 @@ Sequence Protocol
Return the first index *i* for which ``o[i] == value``. On error, return
``-1``. This is equivalent to the Python expression ``o.index(value)``.
+ .. versionchanged:: 2.5
+ This function returned an :c:type:`int` type. This might require changes
+ in your code for properly supporting 64-bit systems.
+
.. c:function:: PyObject* PySequence_List(PyObject *o)
- Return a list object with the same contents as the sequence or iterable *o*,
- or ``NULL`` on failure. The returned list is guaranteed to be new. This is
- equivalent to the Python expression ``list(o)``.
+ Return a list object with the same contents as the arbitrary sequence *o*. The
+ returned list is guaranteed to be new.
.. c:function:: PyObject* PySequence_Tuple(PyObject *o)
.. index:: builtin: tuple
- Return a tuple object with the same contents as the sequence or iterable *o*,
- or ``NULL`` on failure. If *o* is a tuple, a new reference will be returned,
+ Return a tuple object with the same contents as the arbitrary sequence *o* or
+ *NULL* on failure. If *o* is a tuple, a new reference will be returned,
otherwise a tuple will be constructed with the appropriate contents. This is
equivalent to the Python expression ``tuple(o)``.
.. c:function:: PyObject* PySequence_Fast(PyObject *o, const char *m)
- Return the sequence or iterable *o* as an object usable by the other
- ``PySequence_Fast*`` family of functions. If the object is not a sequence or
- iterable, raises :exc:`TypeError` with *m* as the message text. Returns
- ``NULL`` on failure.
-
- The ``PySequence_Fast*`` functions are thus named because they assume
- *o* is a :c:type:`PyTupleObject` or a :c:type:`PyListObject` and access
- the data fields of *o* directly.
-
- As a CPython implementation detail, if *o* is already a sequence or list, it
- will be returned.
-
-
-.. c:function:: Py_ssize_t PySequence_Fast_GET_SIZE(PyObject *o)
-
- Returns the length of *o*, assuming that *o* was returned by
- :c:func:`PySequence_Fast` and that *o* is not ``NULL``. The size can also be
- gotten by calling :c:func:`PySequence_Size` on *o*, but
- :c:func:`PySequence_Fast_GET_SIZE` is faster because it can assume *o* is a
- list or tuple.
+ Return the sequence *o* as a list, unless it is already a tuple or list, in
+ which case *o* is returned. Use :c:func:`PySequence_Fast_GET_ITEM` to access
+ the members of the result. Returns *NULL* on failure. If the object is not
+ a sequence, raises :exc:`TypeError` with *m* as the message text.
.. c:function:: PyObject* PySequence_Fast_GET_ITEM(PyObject *o, Py_ssize_t i)
Return the *i*\ th element of *o*, assuming that *o* was returned by
- :c:func:`PySequence_Fast`, *o* is not ``NULL``, and that *i* is within bounds.
+ :c:func:`PySequence_Fast`, *o* is not *NULL*, and that *i* is within bounds.
+
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` type for *i*. This might require
+ changes in your code for properly supporting 64-bit systems.
.. c:function:: PyObject** PySequence_Fast_ITEMS(PyObject *o)
Return the underlying array of PyObject pointers. Assumes that *o* was returned
- by :c:func:`PySequence_Fast` and *o* is not ``NULL``.
+ by :c:func:`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.
+ .. versionadded:: 2.4
+
.. c:function:: PyObject* PySequence_ITEM(PyObject *o, Py_ssize_t i)
- Return the *i*\ th element of *o* or ``NULL`` on failure. Faster form of
+ Return the *i*\ th element of *o* or *NULL* on failure. Macro form of
:c:func:`PySequence_GetItem` but without checking that
:c:func:`PySequence_Check` on *o* is true and without adjustment for negative
indices.
+
+ .. versionadded:: 2.3
+
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` type for *i*. This might require
+ changes in your code for properly supporting 64-bit systems.
+
+
+.. c:function:: Py_ssize_t PySequence_Fast_GET_SIZE(PyObject *o)
+
+ Returns the length of *o*, assuming that *o* was returned by
+ :c:func:`PySequence_Fast` and that *o* is not *NULL*. The size can also be
+ gotten by calling :c:func:`PySequence_Size` on *o*, but
+ :c:func:`PySequence_Fast_GET_SIZE` is faster because it can assume *o* is a list
+ or tuple.
diff --git a/Doc/c-api/set.rst b/Doc/c-api/set.rst
index 54819e8f..8bc1c8e 100644
--- a/Doc/c-api/set.rst
+++ b/Doc/c-api/set.rst
@@ -1,4 +1,4 @@
-.. highlight:: c
+.. highlightlang:: c
.. _setobjects:
@@ -12,6 +12,8 @@ Set Objects
object: set
object: frozenset
+.. versionadded:: 2.5
+
This section details the public API for :class:`set` and :class:`frozenset`
objects. Any functionality not listed below is best accessed using the either
the abstract object protocol (including :c:func:`PyObject_CallMethod`,
@@ -54,11 +56,15 @@ the constructor functions work with any iterable Python object.
Return true if *p* is a :class:`set` object or an instance of a subtype.
+ .. versionadded:: 2.6
+
.. c:function:: int PyFrozenSet_Check(PyObject *p)
Return true if *p* is a :class:`frozenset` object or an instance of a
subtype.
+ .. versionadded:: 2.6
+
.. c:function:: int PyAnySet_Check(PyObject *p)
Return true if *p* is a :class:`set` object, a :class:`frozenset` object, or an
@@ -80,8 +86,8 @@ the constructor functions work with any iterable Python object.
.. c:function:: PyObject* PySet_New(PyObject *iterable)
Return a new :class:`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 :exc:`TypeError` if *iterable* is not
+ *iterable* may be *NULL* to create a new empty set. Return the new set on
+ success or *NULL* on failure. Raise :exc:`TypeError` if *iterable* is not
actually iterable. The constructor is also useful for copying a set
(``c=set(s)``).
@@ -89,10 +95,14 @@ the constructor functions work with any iterable Python object.
.. c:function:: PyObject* PyFrozenSet_New(PyObject *iterable)
Return a new :class:`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 :exc:`TypeError` if *iterable* is
+ The *iterable* may be *NULL* to create a new empty frozenset. Return the new
+ set on success or *NULL* on failure. Raise :exc:`TypeError` if *iterable* is
not actually iterable.
+ .. versionchanged:: 2.6
+ Now guaranteed to return a brand-new :class:`frozenset`. Formerly,
+ frozensets of zero-length were a singleton. This got in the way of
+ building-up new frozensets with :meth:`PySet_Add`.
The following functions and macros are available for instances of :class:`set`
or :class:`frozenset` or instances of their subtypes.
@@ -106,6 +116,10 @@ or :class:`frozenset` or instances of their subtypes.
``len(anyset)``. Raises a :exc:`PyExc_SystemError` if *anyset* is not a
:class:`set`, :class:`frozenset`, or an instance of a subtype.
+ .. versionchanged:: 2.5
+ This function returned an :c:type:`int`. This might require changes in
+ your code for properly supporting 64-bit systems.
+
.. c:function:: Py_ssize_t PySet_GET_SIZE(PyObject *anyset)
@@ -123,14 +137,16 @@ or :class:`frozenset` or instances of their subtypes.
.. c:function:: int PySet_Add(PyObject *set, PyObject *key)
- Add *key* to a :class:`set` instance. Also works with :class:`frozenset`
- instances (like :c:func:`PyTuple_SetItem` it can be used to fill-in the values
- of brand new frozensets before they are exposed to other code). Return ``0`` on
- success or ``-1`` on failure. Raise a :exc:`TypeError` if the *key* is
- unhashable. Raise a :exc:`MemoryError` if there is no room to grow. Raise a
- :exc:`SystemError` if *set* is not an instance of :class:`set` or its
+ Add *key* to a :class:`set` instance. Does not apply to :class:`frozenset`
+ instances. Return ``0`` on success or ``-1`` on failure. Raise a :exc:`TypeError` if
+ the *key* is unhashable. Raise a :exc:`MemoryError` if there is no room to grow.
+ Raise a :exc:`SystemError` if *set* is not an instance of :class:`set` or its
subtype.
+ .. versionchanged:: 2.6
+ Now works with instances of :class:`frozenset` or its subtypes.
+ Like :c:func:`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 :class:`set` or its
subtypes but not for instances of :class:`frozenset` or its subtypes.
@@ -149,7 +165,7 @@ subtypes but not for instances of :class:`frozenset` or its subtypes.
.. c:function:: PyObject* PySet_Pop(PyObject *set)
Return a new reference to an arbitrary object in the *set*, and removes the
- object from the *set*. Return ``NULL`` on failure. Raise :exc:`KeyError` if the
+ object from the *set*. Return *NULL* on failure. Raise :exc:`KeyError` if the
set is empty. Raise a :exc:`SystemError` if *set* is not an instance of
:class:`set` or its subtype.
@@ -157,10 +173,3 @@ subtypes but not for instances of :class:`frozenset` or its subtypes.
.. c:function:: int PySet_Clear(PyObject *set)
Empty an existing set of all elements.
-
-
-.. c:function:: int PySet_ClearFreeList()
-
- Clear the free list. Return the total number of freed items.
-
- .. versionadded:: 3.3
diff --git a/Doc/c-api/slice.rst b/Doc/c-api/slice.rst
index 48a58c6..08d5b94 100644
--- a/Doc/c-api/slice.rst
+++ b/Doc/c-api/slice.rst
@@ -1,4 +1,4 @@
-.. highlight:: c
+.. highlightlang:: c
.. _slice-objects:
@@ -8,25 +8,27 @@ Slice Objects
.. c:var:: PyTypeObject PySlice_Type
- The type object for slice objects. This is the same as :class:`slice` in the
- Python layer.
+ .. index:: single: SliceType (in module types)
+
+ The type object for slice objects. This is the same as ``slice`` and
+ ``types.SliceType``.
.. c:function:: int PySlice_Check(PyObject *ob)
- Return true if *ob* is a slice object; *ob* must not be ``NULL``.
+ Return true if *ob* is a slice object; *ob* must not be *NULL*.
.. c:function:: PyObject* PySlice_New(PyObject *start, PyObject *stop, PyObject *step)
Return a new slice object with the given values. The *start*, *stop*, and
*step* parameters are used as the values of the slice object attributes of
- the same names. Any of the values may be ``NULL``, in which case the
- ``None`` will be used for the corresponding attribute. Return ``NULL`` if
+ the same names. Any of the values may be *NULL*, in which case the
+ ``None`` will be used for the corresponding attribute. Return *NULL* if
the new object could not be allocated.
-.. c:function:: int PySlice_GetIndices(PyObject *slice, Py_ssize_t length, Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step)
+.. c:function:: int PySlice_GetIndices(PySliceObject *slice, Py_ssize_t length, Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step)
Retrieve the start, stop and step indices from the slice object *slice*,
assuming a sequence of length *length*. Treats indices greater than
@@ -36,14 +38,18 @@ Slice Objects
the indices was not :const:`None` and failed to be converted to an integer,
in which case ``-1`` is returned with an exception set).
- You probably do not want to use this function.
+ You probably do not want to use this function. If you want to use slice
+ objects in versions of Python prior to 2.3, you would probably do well to
+ incorporate the source of :c:func:`PySlice_GetIndicesEx`, suitably renamed,
+ in the source of your extension.
- .. versionchanged:: 3.2
- The parameter type for the *slice* parameter was ``PySliceObject*``
- before.
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` type for *length* and an
+ :c:type:`int *` type for *start*, *stop*, and *step*. This might require
+ changes in your code for properly supporting 64-bit systems.
-.. c:function:: int PySlice_GetIndicesEx(PyObject *slice, Py_ssize_t length, Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step, Py_ssize_t *slicelength)
+.. c:function:: int PySlice_GetIndicesEx(PySliceObject *slice, Py_ssize_t length, Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step, Py_ssize_t *slicelength)
Usable replacement for :c:func:`PySlice_GetIndices`. Retrieve the start,
stop, and step indices from the slice object *slice* assuming a sequence of
@@ -53,62 +59,13 @@ Slice Objects
Returns ``0`` on success and ``-1`` on error with exception set.
- .. note::
- This function is considered not safe for resizable sequences.
- Its invocation should be replaced by a combination of
- :c:func:`PySlice_Unpack` and :c:func:`PySlice_AdjustIndices` where ::
-
- if (PySlice_GetIndicesEx(slice, length, &start, &stop, &step, &slicelength) < 0) {
- // return error
- }
-
- is replaced by ::
-
- if (PySlice_Unpack(slice, &start, &stop, &step) < 0) {
- // return error
- }
- slicelength = PySlice_AdjustIndices(length, &start, &stop, step);
-
- .. versionchanged:: 3.2
- The parameter type for the *slice* parameter was ``PySliceObject*``
- before.
-
- .. versionchanged:: 3.6.1
- If ``Py_LIMITED_API`` is not set or set to the value between ``0x03050400``
- and ``0x03060000`` (not including) or ``0x03060100`` or higher
- :c:func:`!PySlice_GetIndicesEx` is implemented as a macro using
- :c:func:`!PySlice_Unpack` and :c:func:`!PySlice_AdjustIndices`.
- Arguments *start*, *stop* and *step* are evaluated more than once.
-
- .. deprecated:: 3.6.1
- If ``Py_LIMITED_API`` is set to the value less than ``0x03050400`` or
- between ``0x03060000`` and ``0x03060100`` (not including)
- :c:func:`!PySlice_GetIndicesEx` is a deprecated function.
-
-
-.. c:function:: int PySlice_Unpack(PyObject *slice, Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step)
-
- Extract the start, stop and step data members from a slice object as
- C integers. Silently reduce values larger than ``PY_SSIZE_T_MAX`` to
- ``PY_SSIZE_T_MAX``, silently boost the start and stop values less than
- ``PY_SSIZE_T_MIN`` to ``PY_SSIZE_T_MIN``, and silently boost the step
- values less than ``-PY_SSIZE_T_MAX`` to ``-PY_SSIZE_T_MAX``.
-
- Return ``-1`` on error, ``0`` on success.
-
- .. versionadded:: 3.6.1
-
-
-.. c:function:: Py_ssize_t PySlice_AdjustIndices(Py_ssize_t length, Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t step)
-
- Adjust start/end slice indices assuming a sequence of the specified length.
- Out of bounds indices are clipped in a manner consistent with the handling
- of normal slices.
-
- Return the length of the slice. Always successful. Doesn't call Python
- code.
+ .. versionadded:: 2.3
- .. versionadded:: 3.6.1
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` type for *length* and an
+ :c:type:`int *` type for *start*, *stop*, *step*, and *slicelength*. This
+ might require changes in your code for properly supporting 64-bit
+ systems.
Ellipsis Object
diff --git a/Doc/c-api/stable.rst b/Doc/c-api/stable.rst
deleted file mode 100644
index 9c05cb3..0000000
--- a/Doc/c-api/stable.rst
+++ /dev/null
@@ -1,38 +0,0 @@
-.. highlight:: c
-
-.. _stable:
-
-***********************************
-Stable Application Binary Interface
-***********************************
-
-Traditionally, the C API of Python will change with every release. Most changes
-will be source-compatible, typically by only adding API, rather than changing
-existing API or removing API (although some interfaces do get removed after
-being deprecated first).
-
-Unfortunately, the API compatibility does not extend to binary compatibility
-(the ABI). The reason is primarily the evolution of struct definitions, where
-addition of a new field, or changing the type of a field, might not break the
-API, but can break the ABI. As a consequence, extension modules need to be
-recompiled for every Python release (although an exception is possible on Unix
-when none of the affected interfaces are used). In addition, on Windows,
-extension modules link with a specific pythonXY.dll and need to be recompiled to
-link with a newer one.
-
-Since Python 3.2, a subset of the API has been declared to guarantee a stable
-ABI. Extension modules wishing to use this API (called "limited API") need to
-define ``Py_LIMITED_API``. A number of interpreter details then become hidden
-from the extension module; in return, a module is built that works on any 3.x
-version (x>=2) without recompilation.
-
-In some cases, the stable ABI needs to be extended with new functions.
-Extension modules wishing to use these new APIs need to set ``Py_LIMITED_API``
-to the ``PY_VERSION_HEX`` value (see :ref:`apiabiversion`) of the minimum Python
-version they want to support (e.g. ``0x03030000`` for Python 3.3). Such modules
-will work on all subsequent Python releases, but fail to load (because of
-missing symbols) on the older releases.
-
-As of Python 3.2, the set of functions available to the limited API is
-documented in :pep:`384`. In the C API documentation, API elements that are not
-part of the limited API are marked as "Not part of the limited API."
diff --git a/Doc/c-api/string.rst b/Doc/c-api/string.rst
new file mode 100644
index 0000000..a8baf29
--- /dev/null
+++ b/Doc/c-api/string.rst
@@ -0,0 +1,333 @@
+.. highlightlang:: c
+
+.. _stringobjects:
+
+String/Bytes Objects
+--------------------
+
+These functions raise :exc:`TypeError` when expecting a string parameter and are
+called with a non-string parameter.
+
+.. 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.
+
+.. index:: object: string
+
+
+.. c:type:: PyStringObject
+
+ This subtype of :c:type:`PyObject` represents a Python string object.
+
+
+.. c:var:: PyTypeObject PyString_Type
+
+ .. index:: single: StringType (in module types)
+
+ This instance of :c:type:`PyTypeObject` represents the Python string type; it is
+ the same object as ``str`` and ``types.StringType`` in the Python layer. .
+
+
+.. c:function:: 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.
+
+ .. versionchanged:: 2.2
+ Allowed subtypes to be accepted.
+
+
+.. c:function:: 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.
+
+ .. versionadded:: 2.2
+
+
+.. c:function:: PyObject* PyString_FromString(const char *v)
+
+ 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.
+
+
+.. c:function:: PyObject* PyString_FromStringAndSize(const char *v, Py_ssize_t len)
+
+ Return a new string object with a copy of the string *v* as value and length
+ *len* on success, and *NULL* on failure. If *v* is *NULL*, the contents of the
+ string are uninitialized.
+
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` type for *len*. This might require
+ changes in your code for properly supporting 64-bit systems.
+
+
+.. c:function:: PyObject* PyString_FromFormat(const char *format, ...)
+
+ Take a C :c:func:`printf`\ -style *format* string and a variable number of
+ arguments, calculate the size of the resulting Python 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:
+
+ .. % This should be exactly the same as the table in PyErr_Format.
+ .. % One should just refer to the other.
+ .. % The descriptions for %zd and %zu are wrong, but the truth is complicated
+ .. % because not all compilers support the %z width modifier -- we fake it
+ .. % when necessary via interpolating PY_FORMAT_SIZE_T.
+ .. % Similar comments apply to the %ll width modifier and
+ .. % PY_FORMAT_LONG_LONG.
+ .. % %u, %lu, %zu should have "new in Python 2.5" blurbs.
+
+ +-------------------+---------------+--------------------------------+
+ | Format Characters | Type | Comment |
+ +===================+===============+================================+
+ | :attr:`%%` | *n/a* | The literal % character. |
+ +-------------------+---------------+--------------------------------+
+ | :attr:`%c` | int | A single character, |
+ | | | represented as a C int. |
+ +-------------------+---------------+--------------------------------+
+ | :attr:`%d` | int | Exactly equivalent to |
+ | | | ``printf("%d")``. |
+ +-------------------+---------------+--------------------------------+
+ | :attr:`%u` | unsigned int | Exactly equivalent to |
+ | | | ``printf("%u")``. |
+ +-------------------+---------------+--------------------------------+
+ | :attr:`%ld` | long | Exactly equivalent to |
+ | | | ``printf("%ld")``. |
+ +-------------------+---------------+--------------------------------+
+ | :attr:`%lu` | unsigned long | Exactly equivalent to |
+ | | | ``printf("%lu")``. |
+ +-------------------+---------------+--------------------------------+
+ | :attr:`%lld` | long long | Exactly equivalent to |
+ | | | ``printf("%lld")``. |
+ +-------------------+---------------+--------------------------------+
+ | :attr:`%llu` | unsigned | Exactly equivalent to |
+ | | long long | ``printf("%llu")``. |
+ +-------------------+---------------+--------------------------------+
+ | :attr:`%zd` | Py_ssize_t | Exactly equivalent to |
+ | | | ``printf("%zd")``. |
+ +-------------------+---------------+--------------------------------+
+ | :attr:`%zu` | size_t | Exactly equivalent to |
+ | | | ``printf("%zu")``. |
+ +-------------------+---------------+--------------------------------+
+ | :attr:`%i` | int | Exactly equivalent to |
+ | | | ``printf("%i")``. |
+ +-------------------+---------------+--------------------------------+
+ | :attr:`%x` | int | Exactly equivalent to |
+ | | | ``printf("%x")``. |
+ +-------------------+---------------+--------------------------------+
+ | :attr:`%s` | char\* | A null-terminated C character |
+ | | | array. |
+ +-------------------+---------------+--------------------------------+
+ | :attr:`%p` | void\* | The hex representation of a C |
+ | | | pointer. Mostly equivalent to |
+ | | | ``printf("%p")`` except that |
+ | | | it is guaranteed to start with |
+ | | | the literal ``0x`` regardless |
+ | | | of what the platform's |
+ | | | ``printf`` yields. |
+ +-------------------+---------------+--------------------------------+
+
+ 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 :const:`HAVE_LONG_LONG` is defined.
+
+ .. versionchanged:: 2.7
+ Support for `"%lld"` and `"%llu"` added.
+
+
+.. c:function:: PyObject* PyString_FromFormatV(const char *format, va_list vargs)
+
+ Identical to :c:func:`PyString_FromFormat` except that it takes exactly two
+ arguments.
+
+
+.. c:function:: Py_ssize_t PyString_Size(PyObject *string)
+
+ Return the length of the string in string object *string*.
+
+ .. versionchanged:: 2.5
+ This function returned an :c:type:`int` type. This might require changes
+ in your code for properly supporting 64-bit systems.
+
+
+.. c:function:: Py_ssize_t PyString_GET_SIZE(PyObject *string)
+
+ Macro form of :c:func:`PyString_Size` but without error checking.
+
+ .. versionchanged:: 2.5
+ This macro returned an :c:type:`int` type. This might require changes in
+ your code for properly supporting 64-bit systems.
+
+
+.. c:function:: 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,
+ :c:func:`PyString_AsString` returns *NULL* and raises :exc:`TypeError`.
+
+
+.. c:function:: char* PyString_AS_STRING(PyObject *string)
+
+ Macro form of :c:func:`PyString_AsString` but without error checking. Only
+ string objects are supported; no Unicode objects should be passed.
+
+
+.. c:function:: 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 :exc:`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,
+ :c:func:`PyString_AsStringAndSize` returns ``-1`` and raises :exc:`TypeError`.
+
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int *` type for *length*. This might
+ require changes in your code for properly supporting 64-bit systems.
+
+
+.. c:function:: 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.
+
+
+.. c:function:: 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*.
+
+
+.. c:function:: 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.
+
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` type for *newsize*. This might
+ require changes in your code for properly supporting 64-bit systems.
+
+.. c:function:: PyObject* PyString_Format(PyObject *format, PyObject *args)
+
+ Return a new string object from *format* and *args*. Analogous to ``format %
+ args``. The *args* argument must be a tuple or dict.
+
+
+.. c:function:: void PyString_InternInPlace(PyObject **string)
+
+ Intern the argument *\*string* in place. The argument must be the address of a
+ pointer variable pointing to a Python string object. If there is an existing
+ interned string that is the same as *\*string*, it sets *\*string* to it
+ (decrementing the reference count of the old string object and incrementing the
+ reference count of the interned string object), otherwise it leaves *\*string*
+ alone and interns it (incrementing its reference count). (Clarification: even
+ though there is a lot of talk about reference counts, think of this function as
+ reference-count-neutral; you own the object after the call if and only if you
+ owned it before the call.)
+
+ .. note::
+
+ This function is not available in 3.x and does not have a PyBytes alias.
+
+
+.. c:function:: PyObject* PyString_InternFromString(const char *v)
+
+ A combination of :c:func:`PyString_FromString` and
+ :c:func:`PyString_InternInPlace`, returning either a new string object that has
+ been interned, or a new ("owned") reference to an earlier interned string object
+ with the same value.
+
+ .. note::
+
+ This function is not available in 3.x and does not have a PyBytes alias.
+
+
+.. c:function:: PyObject* PyString_Decode(const char *s, Py_ssize_t size, const char *encoding, const char *errors)
+
+ Create an object by decoding *size* bytes of the encoded buffer *s* using the
+ codec registered for *encoding*. *encoding* and *errors* have the same meaning
+ as the parameters of the same name in the :func:`unicode` 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.
+
+ .. note::
+
+ This function is not available in 3.x and does not have a PyBytes alias.
+
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` type for *size*. This might require
+ changes in your code for properly supporting 64-bit systems.
+
+
+.. c:function:: PyObject* PyString_AsDecodedObject(PyObject *str, const char *encoding, const char *errors)
+
+ 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 :meth:`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.
+
+
+.. c:function:: PyObject* PyString_Encode(const char *s, Py_ssize_t size, const char *encoding, const char *errors)
+
+ Encode the :c:type:`char` buffer of the given size by passing it to the codec
+ registered for *encoding* and return a Python object. *encoding* and *errors*
+ have the same meaning as the parameters of the same name in the string
+ :meth:`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.
+
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` type for *size*. This might require
+ changes in your code for properly supporting 64-bit systems.
+
+
+.. c:function:: PyObject* PyString_AsEncodedObject(PyObject *str, const char *encoding, const char *errors)
+
+ Encode a string object using 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 :meth:`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.
diff --git a/Doc/c-api/structures.rst b/Doc/c-api/structures.rst
index 1bd769f..af6a8c3 100644
--- a/Doc/c-api/structures.rst
+++ b/Doc/c-api/structures.rst
@@ -1,4 +1,4 @@
-.. highlight:: c
+.. highlightlang:: c
.. _common-structs:
@@ -9,10 +9,6 @@ There are a large number of structures which are used in the definition of
object types for Python. This section describes these structures and how they
are used.
-
-Base object types and macros
-----------------------------
-
All Python objects ultimately share a small number of fields at the beginning
of the object's representation in memory. These are represented by the
:c:type:`PyObject` and :c:type:`PyVarObject` types, which are defined, in turn,
@@ -25,41 +21,52 @@ the definition of all other Python objects.
All object types are extensions of this type. This is a type which
contains the information Python needs to treat a pointer to an object as an
object. In a normal "release" build, it contains only the object's
- reference count and a pointer to the corresponding type object.
- Nothing is actually declared to be a :c:type:`PyObject`, but every pointer
- to a Python object can be cast to a :c:type:`PyObject*`. Access to the
- members must be done by using the macros :c:macro:`Py_REFCNT` and
- :c:macro:`Py_TYPE`.
+ reference count and a pointer to the corresponding type object. It
+ corresponds to the fields defined by the expansion of the ``PyObject_HEAD``
+ macro.
.. c:type:: PyVarObject
This is an extension of :c:type:`PyObject` that adds the :attr:`ob_size`
field. This is only used for objects that have some notion of *length*.
- This type does not often appear in the Python/C API.
- Access to the members must be done by using the macros
- :c:macro:`Py_REFCNT`, :c:macro:`Py_TYPE`, and :c:macro:`Py_SIZE`.
+ 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 :c:type:`PyObject` and
+:c:type:`PyVarObject`:
.. c:macro:: PyObject_HEAD
- This is a macro used when declaring new types which represent objects
- without a varying length. The PyObject_HEAD macro expands to::
+ This is a macro which expands to the declarations of the fields of the
+ :c:type:`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 :c:macro:`Py_TRACE_REFS`. By default, that macro is
+ not defined, and :c:macro:`PyObject_HEAD` expands to::
- PyObject ob_base;
+ Py_ssize_t ob_refcnt;
+ PyTypeObject *ob_type;
- See documentation of :c:type:`PyObject` above.
+ When :c:macro:`Py_TRACE_REFS` is defined, it expands to::
+
+ PyObject *_ob_next, *_ob_prev;
+ Py_ssize_t ob_refcnt;
+ PyTypeObject *ob_type;
.. c:macro:: PyObject_VAR_HEAD
- This is a macro used when declaring new types which represent objects
- with a length that varies from instance to instance.
- The PyObject_VAR_HEAD macro expands to::
+ This is a macro which expands to the declarations of the fields of the
+ :c:type:`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::
- PyVarObject ob_base;
+ PyObject_HEAD
+ Py_ssize_t ob_size;
- See documentation of :c:type:`PyVarObject` above.
+ Note that :c:macro:`PyObject_HEAD` is part of the expansion, and that its own
+ expansion varies depending on the definition of :c:macro:`Py_TRACE_REFS`.
.. c:macro:: Py_TYPE(o)
@@ -69,6 +76,8 @@ the definition of all other Python objects.
(((PyObject*)(o))->ob_type)
+ .. versionadded:: 2.6
+
.. c:macro:: Py_REFCNT(o)
@@ -78,6 +87,8 @@ the definition of all other Python objects.
(((PyObject*)(o))->ob_refcnt)
+ .. versionadded:: 2.6
+
.. c:macro:: Py_SIZE(o)
@@ -86,6 +97,8 @@ the definition of all other Python objects.
(((PyVarObject*)(o))->ob_size)
+ .. versionadded:: 2.6
+
.. c:macro:: PyObject_HEAD_INIT(type)
@@ -106,56 +119,35 @@ the definition of all other Python objects.
1, type, size,
-Implementing functions and methods
-----------------------------------
-
.. c:type:: PyCFunction
Type of the functions used to implement most Python callables in C.
Functions of this type take two :c:type:`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
+ 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.
-.. c:type:: PyCFunctionWithKeywords
-
- Type of the functions used to implement Python callables in C
- with signature :const:`METH_VARARGS | METH_KEYWORDS`.
-
-
-.. c:type:: _PyCFunctionFast
-
- Type of the functions used to implement Python callables in C
- with signature :const:`METH_FASTCALL`.
-
-
-.. c:type:: _PyCFunctionFastWithKeywords
-
- Type of the functions used to implement Python callables in C
- with signature :const:`METH_FASTCALL | METH_KEYWORDS`.
-
-
.. c:type:: PyMethodDef
Structure used to describe a method of an extension type. This structure has
four fields:
- +------------------+---------------+-------------------------------+
- | Field | C Type | Meaning |
- +==================+===============+===============================+
- | :attr:`ml_name` | const char \* | name of the method |
- +------------------+---------------+-------------------------------+
- | :attr:`ml_meth` | PyCFunction | pointer to the C |
- | | | implementation |
- +------------------+---------------+-------------------------------+
- | :attr:`ml_flags` | int | flag bits indicating how the |
- | | | call should be constructed |
- +------------------+---------------+-------------------------------+
- | :attr:`ml_doc` | const char \* | points to the contents of the |
- | | | docstring |
- +------------------+---------------+-------------------------------+
+ +------------------+-------------+-------------------------------+
+ | Field | C Type | Meaning |
+ +==================+=============+===============================+
+ | :attr:`ml_name` | char \* | name of the method |
+ +------------------+-------------+-------------------------------+
+ | :attr:`ml_meth` | PyCFunction | pointer to the C |
+ | | | implementation |
+ +------------------+-------------+-------------------------------+
+ | :attr:`ml_flags` | int | flag bits indicating how the |
+ | | | call should be constructed |
+ +------------------+-------------+-------------------------------+
+ | :attr:`ml_doc` | char \* | points to the contents of the |
+ | | | docstring |
+ +------------------+-------------+-------------------------------+
The :attr:`ml_meth` is a C function pointer. The functions may be of different
types, but they always return :c:type:`PyObject\*`. If the function is not of
@@ -166,11 +158,10 @@ specific C type of the *self* object.
The :attr:`ml_flags` field is a bitfield which can include the following flags.
The individual flags indicate either a calling convention or a binding
-convention.
+convention. Of the calling convention flags, only :const:`METH_VARARGS` and
+:const:`METH_KEYWORDS` can be combined. Any of the calling convention flags
+can be combined with a binding flag.
-There are four basic calling conventions for positional arguments
-and two of them can be combined with :const:`METH_KEYWORDS` to support
-also keyword arguments. So there are a total of 6 calling conventions:
.. data:: METH_VARARGS
@@ -178,56 +169,26 @@ also keyword arguments. So there are a total of 6 calling conventions:
:c:type:`PyCFunction`. The function expects two :c:type:`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
+ object representing all arguments. This parameter is typically processed
using :c:func:`PyArg_ParseTuple` or :c:func:`PyArg_UnpackTuple`.
-.. data:: METH_VARARGS | METH_KEYWORDS
+.. data:: METH_KEYWORDS
Methods with these flags must be of type :c:type:`PyCFunctionWithKeywords`.
- The function expects three parameters: *self*, *args*, *kwargs* where
- *kwargs* is a dictionary of all the keyword arguments or possibly ``NULL``
- if there are no keyword arguments. The parameters are typically processed
- using :c:func:`PyArg_ParseTupleAndKeywords`.
-
-
-.. data:: METH_FASTCALL
-
- Fast calling convention supporting only positional arguments.
- The methods have the type :c:type:`_PyCFunctionFast`.
- The first parameter is *self*, the second parameter is a C array
- of :c:type:`PyObject\*` values indicating the arguments and the third
- parameter is the number of arguments (the length of the array).
-
- This is not part of the :ref:`limited API <stable>`.
-
- .. versionadded:: 3.7
-
-
-.. data:: METH_FASTCALL | METH_KEYWORDS
-
- Extension of :const:`METH_FASTCALL` supporting also keyword arguments,
- with methods of type :c:type:`_PyCFunctionFastWithKeywords`.
- Keyword arguments are passed the same way as in the
- :ref:`vectorcall protocol <vectorcall>`:
- there is an additional fourth :c:type:`PyObject\*` parameter
- which is a tuple representing the names of the keyword arguments
- (which are guaranteed to be strings)
- or possibly ``NULL`` if there are no keywords. The values of the keyword
- arguments are stored in the *args* array, after the positional arguments.
-
- This is not part of the :ref:`limited API <stable>`.
-
- .. versionadded:: 3.7
+ The function expects three parameters: *self*, *args*, and a dictionary of
+ all the keyword arguments. The flag is typically combined with
+ :const:`METH_VARARGS`, and the parameters are typically processed using
+ :c:func:`PyArg_ParseTupleAndKeywords`.
.. data:: METH_NOARGS
Methods without parameters don't need to check whether arguments are given if
they are listed with the :const:`METH_NOARGS` flag. They need to be of type
- :c: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``.
+ :c: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*.
.. data:: METH_O
@@ -238,6 +199,15 @@ also keyword arguments. So there are a total of 6 calling conventions:
:c:type:`PyObject\*` parameter representing the single argument.
+.. data:: METH_OLDARGS
+
+ This calling convention is deprecated. The method must be of type
+ :c: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
@@ -253,15 +223,19 @@ method.
similar to what is created when using the :func:`classmethod` built-in
function.
+ .. versionadded:: 2.3
+
.. data:: METH_STATIC
.. index:: builtin: staticmethod
- The method will be passed ``NULL`` as the first parameter rather than an
+ 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 :func:`staticmethod` built-in function.
+ .. versionadded:: 2.3
+
One other constant controls whether a method is loaded in place of another
definition with the same method name.
@@ -278,34 +252,33 @@ definition with the same method name.
slot. This is helpful because calls to PyCFunctions are optimized more
than wrapper object calls.
+ .. versionadded:: 2.4
-Accessing attributes of extension types
----------------------------------------
.. c:type:: PyMemberDef
Structure which describes an attribute of a type which corresponds to a C
struct member. Its fields are:
- +------------------+---------------+-------------------------------+
- | Field | C Type | Meaning |
- +==================+===============+===============================+
- | :attr:`name` | const char \* | name of the member |
- +------------------+---------------+-------------------------------+
- | :attr:`!type` | int | the type of the member in the |
- | | | C struct |
- +------------------+---------------+-------------------------------+
- | :attr:`offset` | Py_ssize_t | the offset in bytes that the |
- | | | member is located on the |
- | | | type's object struct |
- +------------------+---------------+-------------------------------+
- | :attr:`flags` | int | flag bits indicating if the |
- | | | field should be read-only or |
- | | | writable |
- +------------------+---------------+-------------------------------+
- | :attr:`doc` | const char \* | points to the contents of the |
- | | | docstring |
- +------------------+---------------+-------------------------------+
+ +------------------+-------------+-------------------------------+
+ | Field | C Type | Meaning |
+ +==================+=============+===============================+
+ | :attr:`name` | char \* | name of the member |
+ +------------------+-------------+-------------------------------+
+ | :attr:`!type` | int | the type of the member in the |
+ | | | C struct |
+ +------------------+-------------+-------------------------------+
+ | :attr:`offset` | Py_ssize_t | the offset in bytes that the |
+ | | | member is located on the |
+ | | | type's object struct |
+ +------------------+-------------+-------------------------------+
+ | :attr:`flags` | int | flag bits indicating if the |
+ | | | field should be read-only or |
+ | | | writable |
+ +------------------+-------------+-------------------------------+
+ | :attr:`doc` | char \* | points to the contents of the |
+ | | | docstring |
+ +------------------+-------------+-------------------------------+
:attr:`!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
@@ -319,7 +292,7 @@ Accessing attributes of extension types
T_LONG long
T_FLOAT float
T_DOUBLE double
- T_STRING const char \*
+ T_STRING char \*
T_OBJECT PyObject \*
T_OBJECT_EX PyObject \*
T_CHAR char
@@ -335,7 +308,7 @@ Accessing attributes of extension types
=============== ==================
:c:macro:`T_OBJECT` and :c:macro:`T_OBJECT_EX` differ in that
- :c:macro:`T_OBJECT` returns ``None`` if the member is ``NULL`` and
+ :c:macro:`T_OBJECT` returns ``None`` if the member is *NULL* and
:c:macro:`T_OBJECT_EX` raises an :exc:`AttributeError`. Try to use
:c:macro:`T_OBJECT_EX` over :c:macro:`T_OBJECT` because :c:macro:`T_OBJECT_EX`
handles use of the :keyword:`del` statement on that attribute more correctly
@@ -343,23 +316,9 @@ Accessing attributes of extension types
:attr:`flags` can be ``0`` for write and read access or :c:macro:`READONLY` for
read-only access. Using :c:macro:`T_STRING` for :attr:`type` implies
- :c:macro:`READONLY`. :c:macro:`T_STRING` data is interpreted as UTF-8.
- Only :c:macro:`T_OBJECT` and :c:macro:`T_OBJECT_EX`
- members can be deleted. (They are set to ``NULL``).
-
- .. _pymemberdef-offsets:
+ :c:macro:`READONLY`. Only :c:macro:`T_OBJECT` and :c:macro:`T_OBJECT_EX`
+ members can be deleted. (They are set to *NULL*).
- Heap allocated types (created using :c:func:`PyType_FromSpec` or similar),
- ``PyMemberDef`` may contain defintitions for the special members
- ``__dictoffset__`` and ``__weaklistoffset__``, corresponding to
- :c:member:`~PyTypeObject.tp_dictoffset` and
- :c:member:`~PyTypeObject.tp_weaklistoffset` in type objects.
- These must be defined with ``T_PYSSIZET`` and ``READONLY``, for example::
-
- static PyMemberDef spam_type_members[] = {
- {"__dictoffset__", T_PYSSIZET, offsetof(Spam_object, dict), READONLY},
- {NULL} /* Sentinel */
- };
.. c:type:: PyGetSetDef
@@ -369,7 +328,7 @@ Accessing attributes of extension types
+-------------+------------------+-----------------------------------+
| Field | C Type | Meaning |
+=============+==================+===================================+
- | name | const char \* | attribute name |
+ | name | char \* | attribute name |
+-------------+------------------+-----------------------------------+
| get | getter | C Function to get the attribute |
+-------------+------------------+-----------------------------------+
@@ -377,7 +336,7 @@ Accessing attributes of extension types
| | | delete the attribute, if omitted |
| | | the attribute is readonly |
+-------------+------------------+-----------------------------------+
- | doc | const char \* | optional docstring |
+ | doc | char \* | optional docstring |
+-------------+------------------+-----------------------------------+
| closure | void \* | optional function pointer, |
| | | providing additional data for |
@@ -389,7 +348,7 @@ Accessing attributes of extension types
typedef PyObject *(*getter)(PyObject *, void *);
- It should return a new reference on success or ``NULL`` with a set exception
+ It should return a new reference on success or *NULL* with a set exception
on failure.
``set`` functions take two :c:type:`PyObject\*` parameters (the instance and
@@ -397,5 +356,13 @@ Accessing attributes of extension types
typedef int (*setter)(PyObject *, PyObject *, void *);
- In case the attribute should be deleted the second parameter is ``NULL``.
+ 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.
+
+
+.. c:function:: PyObject* Py_FindMethod(PyMethodDef table[], PyObject *ob, char *name)
+
+ Return a bound method object for an extension type implemented in C. This
+ can be useful in the implementation of a :c:member:`~PyTypeObject.tp_getattro` or
+ :c:member:`~PyTypeObject.tp_getattr` handler that does not use the
+ :c:func:`PyObject_GenericGetAttr` function.
diff --git a/Doc/c-api/sys.rst b/Doc/c-api/sys.rst
index c851ff6..d354e9f 100644
--- a/Doc/c-api/sys.rst
+++ b/Doc/c-api/sys.rst
@@ -1,86 +1,20 @@
-.. highlight:: c
+.. highlightlang:: c
.. _os:
Operating System Utilities
==========================
-.. c:function:: PyObject* PyOS_FSPath(PyObject *path)
-
- Return the file system representation for *path*. If the object is a
- :class:`str` or :class:`bytes` object, then its reference count is
- incremented. If the object implements the :class:`os.PathLike` interface,
- then :meth:`~os.PathLike.__fspath__` is returned as long as it is a
- :class:`str` or :class:`bytes` object. Otherwise :exc:`TypeError` is raised
- and ``NULL`` is returned.
-
- .. versionadded:: 3.6
-
.. c:function:: int Py_FdIsInteractive(FILE *fp, const char *filename)
Return true (nonzero) if the standard I/O file *fp* with name *filename* is
deemed interactive. This is the case for files for which ``isatty(fileno(fp))``
is true. If the global flag :c:data:`Py_InteractiveFlag` is true, this function
- also returns true if the *filename* pointer is ``NULL`` or if the name is equal to
+ also returns true if the *filename* pointer is *NULL* or if the name is equal to
one of the strings ``'<stdin>'`` or ``'???'``.
-.. c:function:: void PyOS_BeforeFork()
-
- Function to prepare some internal state before a process fork. This
- should be called before calling :c:func:`fork` or any similar function
- that clones the current process.
- Only available on systems where :c:func:`fork` is defined.
-
- .. warning::
- The C :c:func:`fork` call should only be made from the
- :ref:`"main" thread <fork-and-threads>` (of the
- :ref:`"main" interpreter <sub-interpreter-support>`). The same is
- true for ``PyOS_BeforeFork()``.
-
- .. versionadded:: 3.7
-
-
-.. c:function:: void PyOS_AfterFork_Parent()
-
- Function to update some internal state after a process fork. This
- should be called from the parent process after calling :c:func:`fork`
- or any similar function that clones the current process, regardless
- of whether process cloning was successful.
- Only available on systems where :c:func:`fork` is defined.
-
- .. warning::
- The C :c:func:`fork` call should only be made from the
- :ref:`"main" thread <fork-and-threads>` (of the
- :ref:`"main" interpreter <sub-interpreter-support>`). The same is
- true for ``PyOS_AfterFork_Parent()``.
-
- .. versionadded:: 3.7
-
-
-.. c:function:: void PyOS_AfterFork_Child()
-
- Function to update internal interpreter state after a process fork.
- This must be called from the child process after calling :c:func:`fork`,
- or any similar function that clones the current process, if there is
- any chance the process will call back into the Python interpreter.
- Only available on systems where :c:func:`fork` is defined.
-
- .. warning::
- The C :c:func:`fork` call should only be made from the
- :ref:`"main" thread <fork-and-threads>` (of the
- :ref:`"main" interpreter <sub-interpreter-support>`). The same is
- true for ``PyOS_AfterFork_Child()``.
-
- .. versionadded:: 3.7
-
- .. seealso::
- :func:`os.register_at_fork` allows registering custom Python functions
- to be called by :c:func:`PyOS_BeforeFork()`,
- :c:func:`PyOS_AfterFork_Parent` and :c:func:`PyOS_AfterFork_Child`.
-
-
.. c:function:: void PyOS_AfterFork()
Function to update some internal state after a process fork; this should be
@@ -88,9 +22,6 @@ Operating System Utilities
If a new executable is loaded into the new process, this function does not need
to be called.
- .. deprecated:: 3.7
- This function is superseded by :c:func:`PyOS_AfterFork_Child()`.
-
.. c:function:: int PyOS_CheckStack()
@@ -116,98 +47,6 @@ Operating System Utilities
not call those functions directly! :c:type:`PyOS_sighandler_t` is a typedef
alias for :c:type:`void (\*)(int)`.
-.. c:function:: wchar_t* Py_DecodeLocale(const char* arg, size_t *size)
-
- Decode a byte string from the locale encoding with the :ref:`surrogateescape
- error handler <surrogateescape>`: undecodable bytes are decoded as
- characters in range U+DC80..U+DCFF. If a byte sequence can be decoded as a
- surrogate character, escape the bytes using the surrogateescape error
- handler instead of decoding them.
-
- Encoding, highest priority to lowest priority:
-
- * ``UTF-8`` on macOS, Android, and VxWorks;
- * ``UTF-8`` on Windows if :c:data:`Py_LegacyWindowsFSEncodingFlag` is zero;
- * ``UTF-8`` if the Python UTF-8 mode is enabled;
- * ``ASCII`` if the ``LC_CTYPE`` locale is ``"C"``,
- ``nl_langinfo(CODESET)`` returns the ``ASCII`` encoding (or an alias),
- and :c:func:`mbstowcs` and :c:func:`wcstombs` functions uses the
- ``ISO-8859-1`` encoding.
- * the current locale encoding.
-
- Return a pointer to a newly allocated wide character string, use
- :c:func:`PyMem_RawFree` to free the memory. If size is not ``NULL``, write
- the number of wide characters excluding the null character into ``*size``
-
- Return ``NULL`` on decoding error or memory allocation error. If *size* is
- not ``NULL``, ``*size`` is set to ``(size_t)-1`` on memory error or set to
- ``(size_t)-2`` on decoding error.
-
- Decoding errors should never happen, unless there is a bug in the C
- library.
-
- Use the :c:func:`Py_EncodeLocale` function to encode the character string
- back to a byte string.
-
- .. seealso::
-
- The :c:func:`PyUnicode_DecodeFSDefaultAndSize` and
- :c:func:`PyUnicode_DecodeLocaleAndSize` functions.
-
- .. versionadded:: 3.5
-
- .. versionchanged:: 3.7
- The function now uses the UTF-8 encoding in the UTF-8 mode.
-
- .. versionchanged:: 3.8
- The function now uses the UTF-8 encoding on Windows if
- :c:data:`Py_LegacyWindowsFSEncodingFlag` is zero;
-
-
-.. c:function:: char* Py_EncodeLocale(const wchar_t *text, size_t *error_pos)
-
- Encode a wide character string to the locale encoding with the
- :ref:`surrogateescape error handler <surrogateescape>`: surrogate characters
- in the range U+DC80..U+DCFF are converted to bytes 0x80..0xFF.
-
- Encoding, highest priority to lowest priority:
-
- * ``UTF-8`` on macOS, Android, and VxWorks;
- * ``UTF-8`` on Windows if :c:data:`Py_LegacyWindowsFSEncodingFlag` is zero;
- * ``UTF-8`` if the Python UTF-8 mode is enabled;
- * ``ASCII`` if the ``LC_CTYPE`` locale is ``"C"``,
- ``nl_langinfo(CODESET)`` returns the ``ASCII`` encoding (or an alias),
- and :c:func:`mbstowcs` and :c:func:`wcstombs` functions uses the
- ``ISO-8859-1`` encoding.
- * the current locale encoding.
-
- The function uses the UTF-8 encoding in the Python UTF-8 mode.
-
- Return a pointer to a newly allocated byte string, use :c:func:`PyMem_Free`
- to free the memory. Return ``NULL`` on encoding error or memory allocation
- error
-
- If error_pos is not ``NULL``, ``*error_pos`` is set to ``(size_t)-1`` on
- success, or set to the index of the invalid character on encoding error.
-
- Use the :c:func:`Py_DecodeLocale` function to decode the bytes string back
- to a wide character string.
-
- .. seealso::
-
- The :c:func:`PyUnicode_EncodeFSDefault` and
- :c:func:`PyUnicode_EncodeLocale` functions.
-
- .. versionadded:: 3.5
-
- .. versionchanged:: 3.7
- The function now uses the UTF-8 encoding in the UTF-8 mode.
-
- .. versionchanged:: 3.8
- The function now uses the UTF-8 encoding on Windows if
- :c:data:`Py_LegacyWindowsFSEncodingFlag` is zero;
-
-
.. _systemfunctions:
System Functions
@@ -217,38 +56,32 @@ These are utility functions that make functionality from the :mod:`sys` module
accessible to C code. They all work with the current interpreter thread's
:mod:`sys` module's dict, which is contained in the internal thread state structure.
-.. c:function:: PyObject *PySys_GetObject(const char *name)
+.. c:function:: PyObject *PySys_GetObject(char *name)
- Return the object *name* from the :mod:`sys` module or ``NULL`` if it does
+ Return the object *name* from the :mod:`sys` module or *NULL* if it does
not exist, without setting an exception.
-.. c:function:: int PySys_SetObject(const char *name, PyObject *v)
+.. c:function:: FILE *PySys_GetFile(char *name, FILE *def)
+
+ Return the :c:type:`FILE*` associated with the object *name* in the
+ :mod:`sys` module, or *def* if *name* is not in the module or is not associated
+ with a :c:type:`FILE*`.
- Set *name* in the :mod:`sys` module to *v* unless *v* is ``NULL``, in which
+.. c:function:: int PySys_SetObject(char *name, PyObject *v)
+
+ Set *name* in the :mod:`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.
.. c:function:: void PySys_ResetWarnOptions()
- Reset :data:`sys.warnoptions` to an empty list. This function may be
- called prior to :c:func:`Py_Initialize`.
-
-.. c:function:: void PySys_AddWarnOption(const wchar_t *s)
-
- Append *s* to :data:`sys.warnoptions`. This function must be called prior
- to :c:func:`Py_Initialize` in order to affect the warnings filter list.
+ Reset :data:`sys.warnoptions` to an empty list.
-.. c:function:: void PySys_AddWarnOptionUnicode(PyObject *unicode)
+.. c:function:: void PySys_AddWarnOption(char *s)
- Append *unicode* to :data:`sys.warnoptions`.
+ Append *s* to :data:`sys.warnoptions`.
- Note: this function is not currently usable from outside the CPython
- implementation, as it must be called prior to the implicit import of
- :mod:`warnings` in :c:func:`Py_Initialize` to be effective, but can't be
- called until enough of the runtime has been initialized to permit the
- creation of Unicode objects.
-
-.. c:function:: void PySys_SetPath(const wchar_t *path)
+.. c:function:: void PySys_SetPath(char *path)
Set :data:`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
@@ -272,103 +105,7 @@ accessible to C code. They all work with the current interpreter thread's
.. c:function:: void PySys_WriteStderr(const char *format, ...)
- As :c:func:`PySys_WriteStdout`, but write to :data:`sys.stderr` or *stderr*
- instead.
-
-.. c:function:: void PySys_FormatStdout(const char *format, ...)
-
- Function similar to PySys_WriteStdout() but format the message using
- :c:func:`PyUnicode_FromFormatV` and don't truncate the message to an
- arbitrary length.
-
- .. versionadded:: 3.2
-
-.. c:function:: void PySys_FormatStderr(const char *format, ...)
-
- As :c:func:`PySys_FormatStdout`, but write to :data:`sys.stderr` or *stderr*
- instead.
-
- .. versionadded:: 3.2
-
-.. c:function:: void PySys_AddXOption(const wchar_t *s)
-
- Parse *s* as a set of :option:`-X` options and add them to the current
- options mapping as returned by :c:func:`PySys_GetXOptions`. This function
- may be called prior to :c:func:`Py_Initialize`.
-
- .. versionadded:: 3.2
-
-.. c:function:: PyObject *PySys_GetXOptions()
-
- Return the current dictionary of :option:`-X` options, similarly to
- :data:`sys._xoptions`. On error, ``NULL`` is returned and an exception is
- set.
-
- .. versionadded:: 3.2
-
-
-.. c:function:: int PySys_Audit(const char *event, const char *format, ...)
-
- Raise an auditing event with any active hooks. Return zero for success
- and non-zero with an exception set on failure.
-
- If any hooks have been added, *format* and other arguments will be used
- to construct a tuple to pass. Apart from ``N``, the same format characters
- as used in :c:func:`Py_BuildValue` are available. If the built value is not
- a tuple, it will be added into a single-element tuple. (The ``N`` format
- option consumes a reference, but since there is no way to know whether
- arguments to this function will be consumed, using it may cause reference
- leaks.)
-
- Note that ``#`` format characters should always be treated as
- ``Py_ssize_t``, regardless of whether ``PY_SSIZE_T_CLEAN`` was defined.
-
- :func:`sys.audit` performs the same function from Python code.
-
- .. versionadded:: 3.8
-
- .. versionchanged:: 3.8.2
-
- Require ``Py_ssize_t`` for ``#`` format characters. Previously, an
- unavoidable deprecation warning was raised.
-
-
-.. c:function:: int PySys_AddAuditHook(Py_AuditHookFunction hook, void *userData)
-
- Append the callable *hook* to the list of active auditing hooks.
- Return zero for success
- and non-zero on failure. If the runtime has been initialized, also set an
- error on failure. Hooks added through this API are called for all
- interpreters created by the runtime.
-
- The *userData* pointer is passed into the hook function. Since hook
- functions may be called from different runtimes, this pointer should not
- refer directly to Python state.
-
- This function is safe to call before :c:func:`Py_Initialize`. When called
- after runtime initialization, existing audit hooks are notified and may
- silently abort the operation by raising an error subclassed from
- :class:`Exception` (other errors will not be silenced).
-
- The hook function is of type :c:type:`int (*)(const char *event, PyObject
- *args, void *userData)`, where *args* is guaranteed to be a
- :c:type:`PyTupleObject`. The hook function is always called with the GIL
- held by the Python interpreter that raised the event.
-
- See :pep:`578` for a detailed description of auditing. Functions in the
- runtime and standard library that raise events are listed in the
- :ref:`audit events table <audit-events>`.
- Details are in each function's documentation.
-
- .. audit-event:: sys.addaudithook "" c.PySys_AddAuditHook
-
- If the interpreter is initialized, this function raises a auditing event
- ``sys.addaudithook`` with no arguments. If any existing hooks raise an
- exception derived from :class:`Exception`, the new hook will not be
- added and the exception is cleared. As a result, callers cannot assume
- that their hook has been added unless they control all existing hooks.
-
- .. versionadded:: 3.8
+ As above, but write to :data:`sys.stderr` or *stderr* instead.
.. _processcontrol:
@@ -392,24 +129,20 @@ Process Control
.. c:function:: void Py_Exit(int status)
.. index::
- single: Py_FinalizeEx()
+ single: Py_Finalize()
single: exit()
- Exit the current process. This calls :c:func:`Py_FinalizeEx` and then calls the
- standard C library function ``exit(status)``. If :c:func:`Py_FinalizeEx`
- indicates an error, the exit status is set to 120.
-
- .. versionchanged:: 3.6
- Errors from finalization no longer ignored.
+ Exit the current process. This calls :c:func:`Py_Finalize` and then calls the
+ standard C library function ``exit(status)``.
.. c:function:: int Py_AtExit(void (*func) ())
.. index::
- single: Py_FinalizeEx()
+ single: Py_Finalize()
single: cleanup functions
- Register a cleanup function to be called by :c:func:`Py_FinalizeEx`. The cleanup
+ Register a cleanup function to be called by :c:func:`Py_Finalize`. The cleanup
function will be called with no arguments and should return no value. At most
32 cleanup functions can be registered. When the registration is successful,
:c:func:`Py_AtExit` returns ``0``; on failure, it returns ``-1``. The cleanup
diff --git a/Doc/c-api/tuple.rst b/Doc/c-api/tuple.rst
index 62bc9a5..fd85663 100644
--- a/Doc/c-api/tuple.rst
+++ b/Doc/c-api/tuple.rst
@@ -1,4 +1,4 @@
-.. highlight:: c
+.. highlightlang:: c
.. _tupleobjects:
@@ -15,8 +15,10 @@ Tuple Objects
.. c:var:: PyTypeObject PyTuple_Type
- This instance of :c:type:`PyTypeObject` represents the Python tuple type; it
- is the same object as :class:`tuple` in the Python layer.
+ .. index:: single: TupleType (in module types)
+
+ This instance of :c:type:`PyTypeObject` represents the Python tuple type; it is
+ the same object as ``tuple`` and ``types.TupleType`` in the Python layer..
.. c:function:: int PyTuple_Check(PyObject *p)
@@ -24,53 +26,88 @@ Tuple Objects
Return true if *p* is a tuple object or an instance of a subtype of the tuple
type.
+ .. versionchanged:: 2.2
+ Allowed subtypes to be accepted.
+
.. c:function:: int PyTuple_CheckExact(PyObject *p)
Return true if *p* is a tuple object, but not an instance of a subtype of the
tuple type.
+ .. versionadded:: 2.2
+
.. c:function:: PyObject* PyTuple_New(Py_ssize_t len)
- Return a new tuple object of size *len*, or ``NULL`` on failure.
+ Return a new tuple object of size *len*, or *NULL* on failure.
+
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` type for *len*. This might require
+ changes in your code for properly supporting 64-bit systems.
.. c:function:: PyObject* PyTuple_Pack(Py_ssize_t n, ...)
- Return a new tuple object of size *n*, or ``NULL`` on failure. The tuple values
+ 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)``.
+ .. versionadded:: 2.4
+
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` type for *n*. This might require
+ changes in your code for properly supporting 64-bit systems.
+
.. c:function:: Py_ssize_t PyTuple_Size(PyObject *p)
Take a pointer to a tuple object, and return the size of that tuple.
+ .. versionchanged:: 2.5
+ This function returned an :c:type:`int` type. This might require changes
+ in your code for properly supporting 64-bit systems.
+
.. c:function:: 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;
+ Return the size of the tuple *p*, which must be non-*NULL* and point to a tuple;
no error checking is performed.
+ .. versionchanged:: 2.5
+ This function returned an :c:type:`int` type. This might require changes
+ in your code for properly supporting 64-bit systems.
+
.. c:function:: PyObject* PyTuple_GetItem(PyObject *p, Py_ssize_t pos)
Return the object at position *pos* in the tuple pointed to by *p*. If *pos* is
- out of bounds, return ``NULL`` and set an :exc:`IndexError` exception.
+ out of bounds, return *NULL* and set an :exc:`IndexError` exception.
+
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` type for *pos*. This might require
+ changes in your code for properly supporting 64-bit systems.
.. c:function:: PyObject* PyTuple_GET_ITEM(PyObject *p, Py_ssize_t pos)
Like :c:func:`PyTuple_GetItem`, but does no checking of its arguments.
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` type for *pos*. This might require
+ changes in your code for properly supporting 64-bit systems.
+
.. c:function:: PyObject* PyTuple_GetSlice(PyObject *p, Py_ssize_t low, Py_ssize_t high)
Return the slice of the tuple pointed to by *p* between *low* and *high*,
- or ``NULL`` on failure. This is the equivalent of the Python expression
+ or *NULL* on failure. This is the equivalent of the Python expression
``p[low:high]``. Indexing from the end of the list is not supported.
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` type for *low* and *high*. This might
+ require changes in your code for properly supporting 64-bit systems.
+
.. c:function:: int PyTuple_SetItem(PyObject *p, Py_ssize_t pos, PyObject *o)
@@ -83,6 +120,10 @@ Tuple Objects
This function "steals" a reference to *o* and discards a reference to
an item already in the tuple at the affected position.
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` type for *pos*. This might require
+ changes in your code for properly supporting 64-bit systems.
+
.. c:function:: void PyTuple_SET_ITEM(PyObject *p, Py_ssize_t pos, PyObject *o)
@@ -96,6 +137,10 @@ Tuple Objects
is being replaced; any reference in the tuple at position *pos* will be
leaked.
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` type for *pos*. This might require
+ changes in your code for properly supporting 64-bit systems.
+
.. c:function:: int _PyTuple_Resize(PyObject **p, Py_ssize_t newsize)
@@ -107,121 +152,19 @@ Tuple Objects
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
+ ``*p`` is destroyed. On failure, returns ``-1`` and sets ``*p`` to *NULL*, and
raises :exc:`MemoryError` or :exc:`SystemError`.
+ .. versionchanged:: 2.2
+ Removed unused third parameter, *last_is_sticky*.
-.. c:function:: int PyTuple_ClearFreeList()
-
- Clear the free list. Return the total number of freed items.
-
-
-Struct Sequence Objects
------------------------
-
-Struct sequence objects are the C equivalent of :func:`~collections.namedtuple`
-objects, i.e. a sequence whose items can also be accessed through attributes.
-To create a struct sequence, you first have to create a specific struct sequence
-type.
-
-.. c:function:: PyTypeObject* PyStructSequence_NewType(PyStructSequence_Desc *desc)
-
- Create a new struct sequence type from the data in *desc*, described below. Instances
- of the resulting type can be created with :c:func:`PyStructSequence_New`.
-
-
-.. c:function:: void PyStructSequence_InitType(PyTypeObject *type, PyStructSequence_Desc *desc)
-
- Initializes a struct sequence type *type* from *desc* in place.
-
-
-.. c:function:: int PyStructSequence_InitType2(PyTypeObject *type, PyStructSequence_Desc *desc)
-
- The same as ``PyStructSequence_InitType``, but returns ``0`` on success and ``-1`` on
- failure.
-
- .. versionadded:: 3.4
-
-
-.. c:type:: PyStructSequence_Desc
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` type for *newsize*. This might
+ require changes in your code for properly supporting 64-bit systems.
- Contains the meta information of a struct sequence type to create.
- +-------------------+------------------------------+--------------------------------------+
- | Field | C Type | Meaning |
- +===================+==============================+======================================+
- | ``name`` | ``const char *`` | name of the struct sequence type |
- +-------------------+------------------------------+--------------------------------------+
- | ``doc`` | ``const char *`` | pointer to docstring for the type |
- | | | or ``NULL`` to omit |
- +-------------------+------------------------------+--------------------------------------+
- | ``fields`` | ``PyStructSequence_Field *`` | pointer to ``NULL``-terminated array |
- | | | with field names of the new type |
- +-------------------+------------------------------+--------------------------------------+
- | ``n_in_sequence`` | ``int`` | number of fields visible to the |
- | | | Python side (if used as tuple) |
- +-------------------+------------------------------+--------------------------------------+
-
-
-.. c:type:: PyStructSequence_Field
-
- Describes a field of a struct sequence. As a struct sequence is modeled as a
- tuple, all fields are typed as :c:type:`PyObject\*`. The index in the
- :attr:`fields` array of the :c:type:`PyStructSequence_Desc` determines which
- field of the struct sequence is described.
-
- +-----------+------------------+-----------------------------------------+
- | Field | C Type | Meaning |
- +===========+==================+=========================================+
- | ``name`` | ``const char *`` | name for the field or ``NULL`` to end |
- | | | the list of named fields, set to |
- | | | :c:data:`PyStructSequence_UnnamedField` |
- | | | to leave unnamed |
- +-----------+------------------+-----------------------------------------+
- | ``doc`` | ``const char *`` | field docstring or ``NULL`` to omit |
- +-----------+------------------+-----------------------------------------+
-
-
-.. c:var:: const char * const PyStructSequence_UnnamedField
-
- Special value for a field name to leave it unnamed.
-
- .. versionchanged:: 3.9
- The type was changed from ``char *``.
-
-
-.. c:function:: PyObject* PyStructSequence_New(PyTypeObject *type)
-
- Creates an instance of *type*, which must have been created with
- :c:func:`PyStructSequence_NewType`.
-
-
-.. c:function:: PyObject* PyStructSequence_GetItem(PyObject *p, Py_ssize_t pos)
-
- Return the object at position *pos* in the struct sequence pointed to by *p*.
- No bounds checking is performed.
-
-
-.. c:function:: PyObject* PyStructSequence_GET_ITEM(PyObject *p, Py_ssize_t pos)
-
- Macro equivalent of :c:func:`PyStructSequence_GetItem`.
-
-
-.. c:function:: void PyStructSequence_SetItem(PyObject *p, Py_ssize_t pos, PyObject *o)
-
- Sets the field at index *pos* of the struct sequence *p* to value *o*. Like
- :c:func:`PyTuple_SET_ITEM`, this should only be used to fill in brand new
- instances.
-
- .. note::
-
- This function "steals" a reference to *o*.
-
-
-.. c:function:: void PyStructSequence_SET_ITEM(PyObject *p, Py_ssize_t *pos, PyObject *o)
-
- Macro equivalent of :c:func:`PyStructSequence_SetItem`.
+.. c:function:: int PyTuple_ClearFreeList()
- .. note::
+ Clear the free list. Return the total number of freed items.
- This function "steals" a reference to *o*.
+ .. versionadded:: 2.6
diff --git a/Doc/c-api/type.rst b/Doc/c-api/type.rst
index 41956b7..68bebc8 100644
--- a/Doc/c-api/type.rst
+++ b/Doc/c-api/type.rst
@@ -1,4 +1,4 @@
-.. highlight:: c
+.. highlightlang:: c
.. _typeobjects:
@@ -15,8 +15,10 @@ Type Objects
.. c:var:: PyObject* PyType_Type
- This is the type object for type objects; it is the same object as
- :class:`type` in the Python layer.
+ .. index:: single: TypeType (in module types)
+
+ This is the type object for type objects; it is the same object as ``type`` and
+ ``types.TypeType`` in the Python layer.
.. c:function:: int PyType_Check(PyObject *o)
@@ -30,22 +32,14 @@ Type Objects
Return true if the object *o* is a type object, but not a subtype of the
standard type object. Return false in all other cases.
+ .. versionadded:: 2.2
+
.. c:function:: unsigned int PyType_ClearCache()
Clear the internal lookup cache. Return the current version tag.
-.. c:function:: unsigned long PyType_GetFlags(PyTypeObject* type)
-
- Return the :c:member:`~PyTypeObject.tp_flags` member of *type*. This function is primarily
- meant for use with `Py_LIMITED_API`; the individual flag bits are
- guaranteed to be stable across Python releases, but access to
- :c:member:`~PyTypeObject.tp_flags` itself is not part of the limited API.
-
- .. versionadded:: 3.2
-
- .. versionchanged:: 3.4
- The return type is now ``unsigned long`` rather than ``long``.
+ .. versionadded:: 2.6
.. c:function:: void PyType_Modified(PyTypeObject *type)
@@ -54,23 +48,29 @@ Type Objects
subtypes. This function must be called after any manual
modification of the attributes or base classes of the type.
+ .. versionadded:: 2.6
+
-.. c:function:: int PyType_HasFeature(PyTypeObject *o, int feature)
+.. c:function:: int PyType_HasFeature(PyObject *o, int feature)
Return true if the type object *o* sets the feature *feature*. Type features
are denoted by single bit flags.
-.. c:function:: int PyType_IS_GC(PyTypeObject *o)
+.. c:function:: int PyType_IS_GC(PyObject *o)
Return true if the type object includes support for the cycle detector; this
tests the type flag :const:`Py_TPFLAGS_HAVE_GC`.
+ .. versionadded:: 2.0
+
.. c:function:: int PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b)
Return true if *a* is a subtype of *b*.
+ .. versionadded:: 2.2
+
This function only checks for actual subtypes, which means that
:meth:`~class.__subclasscheck__` is not called on *b*. Call
:c:func:`PyObject_IsSubclass` to do the same check that :func:`issubclass`
@@ -79,14 +79,17 @@ Type Objects
.. c:function:: PyObject* PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems)
- Generic handler for the :c:member:`~PyTypeObject.tp_alloc` slot of a type object. Use
- Python's default memory allocation mechanism to allocate a new instance and
- initialize all its contents to ``NULL``.
+ .. versionadded:: 2.2
+
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` type for *nitems*. This might require
+ changes in your code for properly supporting 64-bit systems.
+
.. c:function:: PyObject* PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
- Generic handler for the :c:member:`~PyTypeObject.tp_new` slot of a type object. Create a
- new instance using the type's :c:member:`~PyTypeObject.tp_alloc` slot.
+ .. versionadded:: 2.2
+
.. c:function:: int PyType_Ready(PyTypeObject *type)
@@ -95,115 +98,4 @@ Type Objects
from a type's base class. Return ``0`` on success, or return ``-1`` and sets an
exception on error.
-.. c:function:: void* PyType_GetSlot(PyTypeObject *type, int slot)
-
- Return the function pointer stored in the given slot. If the
- result is ``NULL``, this indicates that either the slot is ``NULL``,
- or that the function was called with invalid parameters.
- Callers will typically cast the result pointer into the appropriate
- function type.
-
- See :c:member:`PyType_Slot.slot` for possible values of the *slot* argument.
-
- An exception is raised if *type* is not a heap type.
-
- .. versionadded:: 3.4
-
-
-Creating Heap-Allocated Types
-.............................
-
-The following functions and structs are used to create
-:ref:`heap types <heap-types>`.
-
-.. c:function:: PyObject* PyType_FromSpecWithBases(PyType_Spec *spec, PyObject *bases)
-
- Creates and returns a heap type object from the *spec*
- (:const:`Py_TPFLAGS_HEAPTYPE`).
-
- If *bases* is a tuple, the created heap type contains all types contained
- in it as base types.
-
- If *bases* is ``NULL``, the *Py_tp_base* slot is used instead.
- If that also is ``NULL``, the new type derives from :class:`object`.
-
- This function calls :c:func:`PyType_Ready` on the new type.
-
- .. versionadded:: 3.3
-
-.. c:function:: PyObject* PyType_FromSpec(PyType_Spec *spec)
-
- Equivalent to ``PyType_FromSpecWithBases(spec, NULL)``.
-
-.. c:type:: PyType_Spec
-
- Structure defining a type's behavior.
-
- .. c:member:: const char* PyType_Spec.name
-
- Name of the type, used to set :c:member:`PyTypeObject.tp_name`.
-
- .. c:member:: int PyType_Spec.basicsize
- .. c:member:: int PyType_Spec.itemsize
-
- Size of the instance in bytes, used to set
- :c:member:`PyTypeObject.tp_basicsize` and
- :c:member:`PyTypeObject.tp_itemsize`.
-
- .. c:member:: int PyType_Spec.flags
-
- Type flags, used to set :c:member:`PyTypeObject.tp_flags`.
-
- If the ``Py_TPFLAGS_HEAPTYPE`` flag is not set,
- :c:func:`PyType_FromSpecWithBases` sets it automatically.
-
- .. c:member:: PyType_Slot *PyType_Spec.slots
-
- Array of :c:type:`PyType_Slot` structures.
- Terminated by the special slot value ``{0, NULL}``.
-
-.. c:type:: PyType_Slot
-
- Structure defining optional functionality of a type, containing a slot ID
- and a value pointer.
-
- .. c:member:: int PyType_Slot.slot
-
- A slot ID.
-
- Slot IDs are named like the field names of the structures
- :c:type:`PyTypeObject`, :c:type:`PyNumberMethods`,
- :c:type:`PySequenceMethods`, :c:type:`PyMappingMethods` and
- :c:type:`PyAsyncMethods` with an added ``Py_`` prefix.
- For example, use:
-
- * ``Py_tp_dealloc`` to set :c:member:`PyTypeObject.tp_dealloc`
- * ``Py_nb_add`` to set :c:member:`PyNumberMethods.nb_add`
- * ``Py_sq_length`` to set :c:member:`PySequenceMethods.sq_length`
-
- The following fields cannot be set using :c:type:`PyType_Spec` and :c:type:`PyType_Slot`:
-
- * :c:member:`~PyTypeObject.tp_dict`
- * :c:member:`~PyTypeObject.tp_mro`
- * :c:member:`~PyTypeObject.tp_cache`
- * :c:member:`~PyTypeObject.tp_subclasses`
- * :c:member:`~PyTypeObject.tp_weaklist`
- * :c:member:`~PyTypeObject.tp_vectorcall`
- * :c:member:`~PyTypeObject.tp_weaklistoffset`
- (see :ref:`PyMemberDef <pymemberdef-offsets>`)
- * :c:member:`~PyTypeObject.tp_dictoffset`
- (see :ref:`PyMemberDef <pymemberdef-offsets>`)
- * :c:member:`~PyTypeObject.tp_vectorcall_offset`
- * :c:member:`~PyBufferProcs.bf_getbuffer`
- * :c:member:`~PyBufferProcs.bf_releasebuffer`
-
- Setting :c:data:`Py_tp_bases` may be problematic on some platforms.
- To avoid issues, use the *bases* argument of
- :py:func:`PyType_FromSpecWithBases` instead.
-
- .. c:member:: void *PyType_Slot.pfunc
-
- The desired value of the slot. In most cases, this is a pointer
- to a function.
-
- May not be ``NULL``.
+ .. versionadded:: 2.2
diff --git a/Doc/c-api/typeobj.rst b/Doc/c-api/typeobj.rst
index 7b205c0..f0ccf2e 100644
--- a/Doc/c-api/typeobj.rst
+++ b/Doc/c-api/typeobj.rst
@@ -1,4 +1,4 @@
-.. highlight:: c
+.. highlightlang:: c
.. _type-structs:
@@ -20,468 +20,18 @@ functionality. The fields of the type object are examined in detail in this
section. The fields will be described in the order in which they occur in the
structure.
-In addition to the following quick reference, the :ref:`typedef-examples`
-section provides at-a-glance insight into the meaning and use of
-:c:type:`PyTypeObject`.
-
-
-Quick Reference
----------------
-
-.. _tp-slots-table:
-
-"tp slots"
-^^^^^^^^^^
-
-.. table::
- :widths: 18,18,18,1,1,1,1
-
- +------------------------------------------------+-----------------------------------+-------------------+---------------+
- | PyTypeObject Slot [#slots]_ | :ref:`Type <slot-typedefs-table>` | special | Info [#cols]_ |
- | | | methods/attrs +---+---+---+---+
- | | | | O | T | D | I |
- +================================================+===================================+===================+===+===+===+===+
- | <R> :c:member:`~PyTypeObject.tp_name` | const char * | __name__ | X | X | | |
- +------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+
- | :c:member:`~PyTypeObject.tp_basicsize` | Py_ssize_t | | X | X | | X |
- +------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+
- | :c:member:`~PyTypeObject.tp_itemsize` | Py_ssize_t | | | X | | X |
- +------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+
- | :c:member:`~PyTypeObject.tp_dealloc` | :c:type:`destructor` | | X | X | | X |
- +------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+
- | :c:member:`~PyTypeObject.tp_vectorcall_offset` | Py_ssize_t | | | X | | X |
- +------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+
- | (:c:member:`~PyTypeObject.tp_getattr`) | :c:type:`getattrfunc` | __getattribute__, | | | | G |
- | | | __getattr__ | | | | |
- +------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+
- | (:c:member:`~PyTypeObject.tp_setattr`) | :c:type:`setattrfunc` | __setattr__, | | | | G |
- | | | __delattr__ | | | | |
- +------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+
- | :c:member:`~PyTypeObject.tp_as_async` | :c:type:`PyAsyncMethods` * | :ref:`sub-slots` | | | | % |
- +------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+
- | :c:member:`~PyTypeObject.tp_repr` | :c:type:`reprfunc` | __repr__ | X | X | | X |
- +------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+
- | :c:member:`~PyTypeObject.tp_as_number` | :c:type:`PyNumberMethods` * | :ref:`sub-slots` | | | | % |
- +------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+
- | :c:member:`~PyTypeObject.tp_as_sequence` | :c:type:`PySequenceMethods` * | :ref:`sub-slots` | | | | % |
- +------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+
- | :c:member:`~PyTypeObject.tp_as_mapping` | :c:type:`PyMappingMethods` * | :ref:`sub-slots` | | | | % |
- +------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+
- | :c:member:`~PyTypeObject.tp_hash` | :c:type:`hashfunc` | __hash__ | X | | | G |
- +------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+
- | :c:member:`~PyTypeObject.tp_call` | :c:type:`ternaryfunc` | __call__ | | X | | X |
- +------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+
- | :c:member:`~PyTypeObject.tp_str` | :c:type:`reprfunc` | __str__ | X | | | X |
- +------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+
- | :c:member:`~PyTypeObject.tp_getattro` | :c:type:`getattrofunc` | __getattribute__, | X | X | | G |
- | | | __getattr__ | | | | |
- +------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+
- | :c:member:`~PyTypeObject.tp_setattro` | :c:type:`setattrofunc` | __setattr__, | X | X | | G |
- | | | __delattr__ | | | | |
- +------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+
- | :c:member:`~PyTypeObject.tp_as_buffer` | :c:type:`PyBufferProcs` * | | | | | % |
- +------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+
- | :c:member:`~PyTypeObject.tp_flags` | unsigned long | | X | X | | ? |
- +------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+
- | :c:member:`~PyTypeObject.tp_doc` | const char * | __doc__ | X | X | | |
- +------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+
- | :c:member:`~PyTypeObject.tp_traverse` | :c:type:`traverseproc` | | | X | | G |
- +------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+
- | :c:member:`~PyTypeObject.tp_clear` | :c:type:`inquiry` | | | X | | G |
- +------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+
- | :c:member:`~PyTypeObject.tp_richcompare` | :c:type:`richcmpfunc` | __lt__, | X | | | G |
- | | | __le__, | | | | |
- | | | __eq__, | | | | |
- | | | __ne__, | | | | |
- | | | __gt__, | | | | |
- | | | __ge__ | | | | |
- +------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+
- | :c:member:`~PyTypeObject.tp_weaklistoffset` | Py_ssize_t | | | X | | ? |
- +------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+
- | :c:member:`~PyTypeObject.tp_iter` | :c:type:`getiterfunc` | __iter__ | | | | X |
- +------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+
- | :c:member:`~PyTypeObject.tp_iternext` | :c:type:`iternextfunc` | __next__ | | | | X |
- +------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+
- | :c:member:`~PyTypeObject.tp_methods` | :c:type:`PyMethodDef` [] | | X | X | | |
- +------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+
- | :c:member:`~PyTypeObject.tp_members` | :c:type:`PyMemberDef` [] | | | X | | |
- +------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+
- | :c:member:`~PyTypeObject.tp_getset` | :c:type:`PyGetSetDef` [] | | X | X | | |
- +------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+
- | :c:member:`~PyTypeObject.tp_base` | :c:type:`PyTypeObject` * | __base__ | | | X | |
- +------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+
- | :c:member:`~PyTypeObject.tp_dict` | :c:type:`PyObject` * | __dict__ | | | ? | |
- +------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+
- | :c:member:`~PyTypeObject.tp_descr_get` | :c:type:`descrgetfunc` | __get__ | | | | X |
- +------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+
- | :c:member:`~PyTypeObject.tp_descr_set` | :c:type:`descrsetfunc` | __set__, | | | | X |
- | | | __delete__ | | | | |
- +------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+
- | :c:member:`~PyTypeObject.tp_dictoffset` | Py_ssize_t | | | X | | ? |
- +------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+
- | :c:member:`~PyTypeObject.tp_init` | :c:type:`initproc` | __init__ | X | X | | X |
- +------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+
- | :c:member:`~PyTypeObject.tp_alloc` | :c:type:`allocfunc` | | X | | ? | ? |
- +------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+
- | :c:member:`~PyTypeObject.tp_new` | :c:type:`newfunc` | __new__ | X | X | ? | ? |
- +------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+
- | :c:member:`~PyTypeObject.tp_free` | :c:type:`freefunc` | | X | X | ? | ? |
- +------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+
- | :c:member:`~PyTypeObject.tp_is_gc` | :c:type:`inquiry` | | | X | | X |
- +------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+
- | <:c:member:`~PyTypeObject.tp_bases`> | :c:type:`PyObject` * | __bases__ | | | ~ | |
- +------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+
- | <:c:member:`~PyTypeObject.tp_mro`> | :c:type:`PyObject` * | __mro__ | | | ~ | |
- +------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+
- | [:c:member:`~PyTypeObject.tp_cache`] | :c:type:`PyObject` * | | | | |
- +------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+
- | [:c:member:`~PyTypeObject.tp_subclasses`] | :c:type:`PyObject` * | __subclasses__ | | | |
- +------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+
- | [:c:member:`~PyTypeObject.tp_weaklist`] | :c:type:`PyObject` * | | | | |
- +------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+
- | (:c:member:`~PyTypeObject.tp_del`) | :c:type:`destructor` | | | | | |
- +------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+
- | [:c:member:`~PyTypeObject.tp_version_tag`] | unsigned int | | | | |
- +------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+
- | :c:member:`~PyTypeObject.tp_finalize` | :c:type:`destructor` | __del__ | | | | X |
- +------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+
- | :c:member:`~PyTypeObject.tp_vectorcall` | :c:type:`vectorcallfunc` | | | | | |
- +------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+
-
-If :const:`COUNT_ALLOCS` is defined then the following (internal-only)
-fields exist as well:
-
-* :c:member:`~PyTypeObject.tp_allocs`
-* :c:member:`~PyTypeObject.tp_frees`
-* :c:member:`~PyTypeObject.tp_maxalloc`
-* :c:member:`~PyTypeObject.tp_prev`
-* :c:member:`~PyTypeObject.tp_next`
-
-.. [#slots]
- A slot name in parentheses indicates it is (effectively) deprecated.
- Names in angle brackets should be treated as read-only.
- Names in square brackets are for internal use only.
- "<R>" (as a prefix) means the field is required (must be non-``NULL``).
-.. [#cols] Columns:
-
- **"O"**: set on :c:type:`PyBaseObject_Type`
-
- **"T"**: set on :c:type:`PyType_Type`
-
- **"D"**: default (if slot is set to ``NULL``)
-
- .. code-block:: none
-
- X - PyType_Ready sets this value if it is NULL
- ~ - PyType_Ready always sets this value (it should be NULL)
- ? - PyType_Ready may set this value depending on other slots
-
- Also see the inheritance column ("I").
-
- **"I"**: inheritance
-
- .. code-block:: none
-
- X - type slot is inherited via *PyType_Ready* if defined with a *NULL* value
- % - the slots of the sub-struct are inherited individually
- G - inherited, but only in combination with other slots; see the slot's description
- ? - it's complicated; see the slot's description
-
- Note that some slots are effectively inherited through the normal
- attribute lookup chain.
-
-.. _sub-slots:
-
-sub-slots
-^^^^^^^^^
-
-.. table::
- :widths: 26,17,12
-
- +---------------------------------------------------------+-----------------------------------+--------------+
- | Slot | :ref:`Type <slot-typedefs-table>` | special |
- | | | methods |
- +=========================================================+===================================+==============+
- | :c:member:`~PyAsyncMethods.am_await` | :c:type:`unaryfunc` | __await__ |
- +---------------------------------------------------------+-----------------------------------+--------------+
- | :c:member:`~PyAsyncMethods.am_aiter` | :c:type:`unaryfunc` | __aiter__ |
- +---------------------------------------------------------+-----------------------------------+--------------+
- | :c:member:`~PyAsyncMethods.am_anext` | :c:type:`unaryfunc` | __anext__ |
- +---------------------------------------------------------+-----------------------------------+--------------+
- | |
- +---------------------------------------------------------+-----------------------------------+--------------+
- | :c:member:`~PyNumberMethods.nb_add` | :c:type:`binaryfunc` | __add__ |
- | | | __radd__ |
- +---------------------------------------------------------+-----------------------------------+--------------+
- | :c:member:`~PyNumberMethods.nb_inplace_add` | :c:type:`binaryfunc` | __iadd__ |
- +---------------------------------------------------------+-----------------------------------+--------------+
- | :c:member:`~PyNumberMethods.nb_subtract` | :c:type:`binaryfunc` | __sub__ |
- | | | __rsub__ |
- +---------------------------------------------------------+-----------------------------------+--------------+
- | :c:member:`~PyNumberMethods.nb_inplace_subtract` | :c:type:`binaryfunc` | __sub__ |
- +---------------------------------------------------------+-----------------------------------+--------------+
- | :c:member:`~PyNumberMethods.nb_multiply` | :c:type:`binaryfunc` | __mul__ |
- | | | __rmul__ |
- +---------------------------------------------------------+-----------------------------------+--------------+
- | :c:member:`~PyNumberMethods.nb_inplace_multiply` | :c:type:`binaryfunc` | __mul__ |
- +---------------------------------------------------------+-----------------------------------+--------------+
- | :c:member:`~PyNumberMethods.nb_remainder` | :c:type:`binaryfunc` | __mod__ |
- | | | __rmod__ |
- +---------------------------------------------------------+-----------------------------------+--------------+
- | :c:member:`~PyNumberMethods.nb_inplace_remainder` | :c:type:`binaryfunc` | __mod__ |
- +---------------------------------------------------------+-----------------------------------+--------------+
- | :c:member:`~PyNumberMethods.nb_divmod` | :c:type:`binaryfunc` | __divmod__ |
- | | | __rdivmod__ |
- +---------------------------------------------------------+-----------------------------------+--------------+
- | :c:member:`~PyNumberMethods.nb_power` | :c:type:`ternaryfunc` | __pow__ |
- | | | __rpow__ |
- +---------------------------------------------------------+-----------------------------------+--------------+
- | :c:member:`~PyNumberMethods.nb_inplace_power` | :c:type:`ternaryfunc` | __pow__ |
- +---------------------------------------------------------+-----------------------------------+--------------+
- | :c:member:`~PyNumberMethods.nb_negative` | :c:type:`unaryfunc` | __neg__ |
- +---------------------------------------------------------+-----------------------------------+--------------+
- | :c:member:`~PyNumberMethods.nb_positive` | :c:type:`unaryfunc` | __pos__ |
- +---------------------------------------------------------+-----------------------------------+--------------+
- | :c:member:`~PyNumberMethods.nb_absolute` | :c:type:`unaryfunc` | __abs__ |
- +---------------------------------------------------------+-----------------------------------+--------------+
- | :c:member:`~PyNumberMethods.nb_bool` | :c:type:`inquiry` | __bool__ |
- +---------------------------------------------------------+-----------------------------------+--------------+
- | :c:member:`~PyNumberMethods.nb_invert` | :c:type:`unaryfunc` | __invert__ |
- +---------------------------------------------------------+-----------------------------------+--------------+
- | :c:member:`~PyNumberMethods.nb_lshift` | :c:type:`binaryfunc` | __lshift__ |
- | | | __rlshift__ |
- +---------------------------------------------------------+-----------------------------------+--------------+
- | :c:member:`~PyNumberMethods.nb_inplace_lshift` | :c:type:`binaryfunc` | __lshift__ |
- +---------------------------------------------------------+-----------------------------------+--------------+
- | :c:member:`~PyNumberMethods.nb_rshift` | :c:type:`binaryfunc` | __rshift__ |
- | | | __rrshift__ |
- +---------------------------------------------------------+-----------------------------------+--------------+
- | :c:member:`~PyNumberMethods.nb_inplace_rshift` | :c:type:`binaryfunc` | __rshift__ |
- +---------------------------------------------------------+-----------------------------------+--------------+
- | :c:member:`~PyNumberMethods.nb_and` | :c:type:`binaryfunc` | __and__ |
- | | | __rand__ |
- +---------------------------------------------------------+-----------------------------------+--------------+
- | :c:member:`~PyNumberMethods.nb_inplace_and` | :c:type:`binaryfunc` | __and__ |
- +---------------------------------------------------------+-----------------------------------+--------------+
- | :c:member:`~PyNumberMethods.nb_xor` | :c:type:`binaryfunc` | __xor__ |
- | | | __rxor__ |
- +---------------------------------------------------------+-----------------------------------+--------------+
- | :c:member:`~PyNumberMethods.nb_inplace_xor` | :c:type:`binaryfunc` | __xor__ |
- +---------------------------------------------------------+-----------------------------------+--------------+
- | :c:member:`~PyNumberMethods.nb_or` | :c:type:`binaryfunc` | __or__ |
- | | | __ror__ |
- +---------------------------------------------------------+-----------------------------------+--------------+
- | :c:member:`~PyNumberMethods.nb_inplace_or` | :c:type:`binaryfunc` | __or__ |
- +---------------------------------------------------------+-----------------------------------+--------------+
- | :c:member:`~PyNumberMethods.nb_int` | :c:type:`unaryfunc` | __int__ |
- +---------------------------------------------------------+-----------------------------------+--------------+
- | :c:member:`~PyNumberMethods.nb_reserved` | void * | |
- +---------------------------------------------------------+-----------------------------------+--------------+
- | :c:member:`~PyNumberMethods.nb_float` | :c:type:`unaryfunc` | __float__ |
- +---------------------------------------------------------+-----------------------------------+--------------+
- | :c:member:`~PyNumberMethods.nb_floor_divide` | :c:type:`binaryfunc` | __floordiv__ |
- +---------------------------------------------------------+-----------------------------------+--------------+
- | :c:member:`~PyNumberMethods.nb_inplace_floor_divide` | :c:type:`binaryfunc` | __floordiv__ |
- +---------------------------------------------------------+-----------------------------------+--------------+
- | :c:member:`~PyNumberMethods.nb_true_divide` | :c:type:`binaryfunc` | __truediv__ |
- +---------------------------------------------------------+-----------------------------------+--------------+
- | :c:member:`~PyNumberMethods.nb_inplace_true_divide` | :c:type:`binaryfunc` | __truediv__ |
- +---------------------------------------------------------+-----------------------------------+--------------+
- | :c:member:`~PyNumberMethods.nb_index` | :c:type:`unaryfunc` | __index__ |
- +---------------------------------------------------------+-----------------------------------+--------------+
- | :c:member:`~PyNumberMethods.nb_matrix_multiply` | :c:type:`binaryfunc` | __matmul__ |
- | | | __rmatmul__ |
- +---------------------------------------------------------+-----------------------------------+--------------+
- | :c:member:`~PyNumberMethods.nb_inplace_matrix_multiply` | :c:type:`binaryfunc` | __matmul__ |
- +---------------------------------------------------------+-----------------------------------+--------------+
- | |
- +---------------------------------------------------------+-----------------------------------+--------------+
- | :c:member:`~PyMappingMethods.mp_length` | :c:type:`lenfunc` | __len__ |
- +---------------------------------------------------------+-----------------------------------+--------------+
- | :c:member:`~PyMappingMethods.mp_subscript` | :c:type:`binaryfunc` | __getitem__ |
- +---------------------------------------------------------+-----------------------------------+--------------+
- | :c:member:`~PyMappingMethods.mp_ass_subscript` | :c:type:`objobjargproc` | __setitem__, |
- | | | __delitem__ |
- +---------------------------------------------------------+-----------------------------------+--------------+
- | |
- +---------------------------------------------------------+-----------------------------------+--------------+
- | :c:member:`~PySequenceMethods.sq_length` | :c:type:`lenfunc` | __len__ |
- +---------------------------------------------------------+-----------------------------------+--------------+
- | :c:member:`~PySequenceMethods.sq_concat` | :c:type:`binaryfunc` | __add__ |
- +---------------------------------------------------------+-----------------------------------+--------------+
- | :c:member:`~PySequenceMethods.sq_repeat` | :c:type:`ssizeargfunc` | __mul__ |
- +---------------------------------------------------------+-----------------------------------+--------------+
- | :c:member:`~PySequenceMethods.sq_item` | :c:type:`ssizeargfunc` | __getitem__ |
- +---------------------------------------------------------+-----------------------------------+--------------+
- | :c:member:`~PySequenceMethods.sq_ass_item` | :c:type:`ssizeobjargproc` | __setitem__ |
- | | | __delitem__ |
- +---------------------------------------------------------+-----------------------------------+--------------+
- | :c:member:`~PySequenceMethods.sq_contains` | :c:type:`objobjproc` | __contains__ |
- +---------------------------------------------------------+-----------------------------------+--------------+
- | :c:member:`~PySequenceMethods.sq_inplace_concat` | :c:type:`binaryfunc` | __iadd__ |
- +---------------------------------------------------------+-----------------------------------+--------------+
- | :c:member:`~PySequenceMethods.sq_inplace_repeat` | :c:type:`ssizeargfunc` | __imul__ |
- +---------------------------------------------------------+-----------------------------------+--------------+
- | |
- +---------------------------------------------------------+-----------------------------------+--------------+
- | :c:member:`~PyBufferProcs.bf_getbuffer` | :c:func:`getbufferproc` | |
- +---------------------------------------------------------+-----------------------------------+--------------+
- | :c:member:`~PyBufferProcs.bf_releasebuffer` | :c:func:`releasebufferproc` | |
- +---------------------------------------------------------+-----------------------------------+--------------+
-
-.. _slot-typedefs-table:
-
-slot typedefs
-^^^^^^^^^^^^^
-
-+-----------------------------+-----------------------------+----------------------+
-| typedef | Parameter Types | Return Type |
-+=============================+=============================+======================+
-| :c:type:`allocfunc` | .. line-block:: | :c:type:`PyObject` * |
-| | | |
-| | :c:type:`PyTypeObject` * | |
-| | Py_ssize_t | |
-+-----------------------------+-----------------------------+----------------------+
-| :c:type:`destructor` | void * | void |
-+-----------------------------+-----------------------------+----------------------+
-| :c:type:`freefunc` | void * | void |
-+-----------------------------+-----------------------------+----------------------+
-| :c:type:`traverseproc` | .. line-block:: | int |
-| | | |
-| | void * | |
-| | :c:type:`visitproc` | |
-| | void * | |
-+-----------------------------+-----------------------------+----------------------+
-| :c:type:`newfunc` | .. line-block:: | :c:type:`PyObject` * |
-| | | |
-| | :c:type:`PyObject` * | |
-| | :c:type:`PyObject` * | |
-| | :c:type:`PyObject` * | |
-+-----------------------------+-----------------------------+----------------------+
-| :c:type:`initproc` | .. line-block:: | int |
-| | | |
-| | :c:type:`PyObject` * | |
-| | :c:type:`PyObject` * | |
-| | :c:type:`PyObject` * | |
-+-----------------------------+-----------------------------+----------------------+
-| :c:type:`reprfunc` | :c:type:`PyObject` * | :c:type:`PyObject` * |
-+-----------------------------+-----------------------------+----------------------+
-| :c:type:`getattrfunc` | .. line-block:: | :c:type:`PyObject` * |
-| | | |
-| | :c:type:`PyObject` * | |
-| | const char * | |
-+-----------------------------+-----------------------------+----------------------+
-| :c:type:`setattrfunc` | .. line-block:: | int |
-| | | |
-| | :c:type:`PyObject` * | |
-| | const char * | |
-| | :c:type:`PyObject` * | |
-+-----------------------------+-----------------------------+----------------------+
-| :c:type:`getattrofunc` | .. line-block:: | :c:type:`PyObject` * |
-| | | |
-| | :c:type:`PyObject` * | |
-| | :c:type:`PyObject` * | |
-+-----------------------------+-----------------------------+----------------------+
-| :c:type:`setattrofunc` | .. line-block:: | int |
-| | | |
-| | :c:type:`PyObject` * | |
-| | :c:type:`PyObject` * | |
-| | :c:type:`PyObject` * | |
-+-----------------------------+-----------------------------+----------------------+
-| :c:type:`descrgetfunc` | .. line-block:: | :c:type:`PyObject` * |
-| | | |
-| | :c:type:`PyObject` * | |
-| | :c:type:`PyObject` * | |
-| | :c:type:`PyObject` * | |
-+-----------------------------+-----------------------------+----------------------+
-| :c:type:`descrsetfunc` | .. line-block:: | int |
-| | | |
-| | :c:type:`PyObject` * | |
-| | :c:type:`PyObject` * | |
-| | :c:type:`PyObject` * | |
-+-----------------------------+-----------------------------+----------------------+
-| :c:type:`hashfunc` | :c:type:`PyObject` * | Py_hash_t |
-+-----------------------------+-----------------------------+----------------------+
-| :c:type:`richcmpfunc` | .. line-block:: | :c:type:`PyObject` * |
-| | | |
-| | :c:type:`PyObject` * | |
-| | :c:type:`PyObject` * | |
-| | int | |
-+-----------------------------+-----------------------------+----------------------+
-| :c:type:`getiterfunc` | :c:type:`PyObject` * | :c:type:`PyObject` * |
-+-----------------------------+-----------------------------+----------------------+
-| :c:type:`iternextfunc` | :c:type:`PyObject` * | :c:type:`PyObject` * |
-+-----------------------------+-----------------------------+----------------------+
-| :c:type:`lenfunc` | :c:type:`PyObject` * | Py_ssize_t |
-+-----------------------------+-----------------------------+----------------------+
-| :c:type:`getbufferproc` | .. line-block:: | int |
-| | | |
-| | :c:type:`PyObject` * | |
-| | :c:type:`Py_buffer` * | |
-| | int | |
-+-----------------------------+-----------------------------+----------------------+
-| :c:type:`releasebufferproc` | .. line-block:: | void |
-| | | |
-| | :c:type:`PyObject` * | |
-| | :c:type:`Py_buffer` * | |
-+-----------------------------+-----------------------------+----------------------+
-| :c:type:`inquiry` | void * | int |
-+-----------------------------+-----------------------------+----------------------+
-| :c:type:`unaryfunc` | .. line-block:: | :c:type:`PyObject` * |
-| | | |
-| | :c:type:`PyObject` * | |
-+-----------------------------+-----------------------------+----------------------+
-| :c:type:`binaryfunc` | .. line-block:: | :c:type:`PyObject` * |
-| | | |
-| | :c:type:`PyObject` * | |
-| | :c:type:`PyObject` * | |
-+-----------------------------+-----------------------------+----------------------+
-| :c:type:`ternaryfunc` | .. line-block:: | :c:type:`PyObject` * |
-| | | |
-| | :c:type:`PyObject` * | |
-| | :c:type:`PyObject` * | |
-| | :c:type:`PyObject` * | |
-+-----------------------------+-----------------------------+----------------------+
-| :c:type:`ssizeargfunc` | .. line-block:: | :c:type:`PyObject` * |
-| | | |
-| | :c:type:`PyObject` * | |
-| | Py_ssize_t | |
-+-----------------------------+-----------------------------+----------------------+
-| :c:type:`ssizeobjargproc` | .. line-block:: | int |
-| | | |
-| | :c:type:`PyObject` * | |
-| | Py_ssize_t | |
-+-----------------------------+-----------------------------+----------------------+
-| :c:type:`objobjproc` | .. line-block:: | int |
-| | | |
-| | :c:type:`PyObject` * | |
-| | :c:type:`PyObject` * | |
-+-----------------------------+-----------------------------+----------------------+
-| :c:type:`objobjargproc` | .. line-block:: | int |
-| | | |
-| | :c:type:`PyObject` * | |
-| | :c:type:`PyObject` * | |
-| | :c:type:`PyObject` * | |
-+-----------------------------+-----------------------------+----------------------+
-
-See :ref:`slot-typedefs` below for more detail.
-
-
-PyTypeObject Definition
------------------------
+Typedefs: unaryfunc, binaryfunc, ternaryfunc, inquiry, coercion, intargfunc,
+intintargfunc, intobjargproc, intintobjargproc, objobjargproc, destructor,
+freefunc, printfunc, getattrfunc, getattrofunc, setattrfunc, setattrofunc,
+cmpfunc, reprfunc, hashfunc
The structure definition for :c:type:`PyTypeObject` can be found in
:file:`Include/object.h`. For convenience of reference, this repeats the
definition found there:
-.. XXX Drop this?
-
.. literalinclude:: ../includes/typestruct.h
-PyObject Slots
---------------
-
The type object structure extends the :c:type:`PyVarObject` structure. The
:attr:`ob_size` field is used for dynamic types (created by :func:`type_new`,
usually called from a class statement). Note that :c:data:`PyType_Type` (the
@@ -493,16 +43,14 @@ type objects) *must* have the :attr:`ob_size` field.
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``.
+ 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 variable
:envvar:`PYTHONDUMPREFS` is set.
- **Inheritance:**
-
These fields are not inherited by subtypes.
@@ -514,10 +62,12 @@ type objects) *must* have the :attr:`ob_size` field.
*not* count as references. But for dynamically allocated type objects, the
instances *do* count as references.
- **Inheritance:**
-
This field is not inherited by subtypes.
+ .. versionchanged:: 2.5
+ This field used to be an :c:type:`int` type. This might require changes
+ in your code for properly supporting 64-bit systems.
+
.. c:member:: PyTypeObject* PyObject.ob_type
@@ -525,7 +75,7 @@ type objects) *must* have the :attr:`ob_size` field.
argument to the ``PyObject_HEAD_INIT`` macro, and its value should normally be
``&PyType_Type``. However, for dynamically loadable extension modules that must
be usable on Windows (at least), the compiler complains that this is not a valid
- initializer. Therefore, the convention is to pass ``NULL`` to the
+ initializer. Therefore, the convention is to pass *NULL* to the
``PyObject_HEAD_INIT`` macro and to initialize this field explicitly at the
start of the module's initialization function, before doing anything else. This
is typically done like this::
@@ -533,37 +83,24 @@ type objects) *must* have the :attr:`ob_size` field.
Foo_Type.ob_type = &PyType_Type;
This should be done before any instances of the type are created.
- :c:func:`PyType_Ready` checks if :attr:`ob_type` is ``NULL``, and if so,
- initializes it to the :attr:`ob_type` field of the base class.
+ :c:func:`PyType_Ready` checks if :attr:`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 :attr:`ob_type` field of the base class.
:c:func:`PyType_Ready` will not change this field if it is non-zero.
- **Inheritance:**
-
- This field is inherited by subtypes.
+ 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.
-PyVarObject Slots
------------------
-
.. c:member:: 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.
- **Inheritance:**
-
This field is not inherited by subtypes.
-PyTypeObject Slots
-------------------
-
-Each slot has a section describing inheritance. If :c:func:`PyType_Ready`
-may set a value when the field is set to ``NULL`` then there will also be
-a "Default" section. (Note that many fields set on :c:type:`PyBaseObject_Type`
-and :c:type:`PyType_Type` effectively act as defaults.)
-
-.. c:member:: const char* PyTypeObject.tp_name
+.. c:member:: 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
@@ -588,12 +125,6 @@ and :c:type:`PyType_Type` effectively act as defaults.)
type will be impossible to pickle. Additionally, it will not be listed in
module documentations created with pydoc.
- This field must not be ``NULL``. It is the only required field
- in :c:func:`PyTypeObject` (other than potentially
- :c:member:`~PyTypeObject.tp_itemsize`).
-
- **Inheritance:**
-
This field is not inherited by subtypes.
@@ -611,7 +142,7 @@ and :c:type:`PyType_Type` effectively act as defaults.)
:attr:`ob_size` field, and the instance size is :c:member:`~PyTypeObject.tp_basicsize` plus N
times :c:member:`~PyTypeObject.tp_itemsize`, where N is the "length" of the object. The value of
N is typically stored in the instance's :attr:`ob_size` field. There are
- exceptions: for example, ints use a negative :attr:`ob_size` to indicate a
+ exceptions: for example, long ints use a negative :attr:`ob_size` to indicate a
negative number, and N is ``abs(ob_size)`` there. Also, the presence of an
:attr:`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
@@ -624,7 +155,13 @@ and :c:type:`PyType_Type` effectively act as defaults.)
:attr:`_ob_next` fields if they are present. This means that the only correct
way to get an initializer for the :c:member:`~PyTypeObject.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.
+ 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 :c:member:`~PyTypeObject.tp_basicsize`).
+
+ These fields are inherited separately by subtypes. If the base type has a
+ non-zero :c:member:`~PyTypeObject.tp_itemsize`, it is generally not safe to set
+ :c:member:`~PyTypeObject.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 :c:member:`~PyTypeObject.tp_basicsize`. Example:
@@ -633,32 +170,21 @@ and :c:type:`PyType_Type` effectively act as defaults.)
:c:member:`~PyTypeObject.tp_basicsize` is a multiple of ``sizeof(double)`` (assuming this is the
alignment requirement for ``double``).
- For any type with variable-length instances, this field must not be ``NULL``.
-
- **Inheritance:**
-
- These fields are inherited separately by subtypes. If the base type has a
- non-zero :c:member:`~PyTypeObject.tp_itemsize`, it is generally not safe to set
- :c:member:`~PyTypeObject.tp_itemsize` to a different non-zero value in a subtype (though this
- depends on the implementation of the base type).
-
.. c:member:: 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 function signature is::
-
- void tp_dealloc(PyObject *self);
+ the case for the singletons ``None`` and ``Ellipsis``).
The destructor function is called by the :c:func:`Py_DECREF` and
:c:func:`Py_XDECREF` macros when the new reference 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 call the type's
- :c:member:`~PyTypeObject.tp_free` function. If the type is not subtypable
- (doesn't have the :const:`Py_TPFLAGS_BASETYPE` flag bit set), it is
+ to the allocation function used to allocate the buffer), and finally (as its
+ last action) call the type's :c:member:`~PyTypeObject.tp_free` function. If the type is not
+ subtypable (doesn't have the :const:`Py_TPFLAGS_BASETYPE` flag bit set), it is
permissible to call the object deallocator directly instead of via
:c:member:`~PyTypeObject.tp_free`. The object deallocator should be the one used to allocate the
instance; this is normally :c:func:`PyObject_Del` if the instance was allocated
@@ -666,74 +192,36 @@ and :c:type:`PyType_Type` effectively act as defaults.)
:c:func:`PyObject_GC_Del` if the instance was allocated using
:c:func:`PyObject_GC_New` or :c:func:`PyObject_GC_NewVar`.
- Finally, if the type is heap allocated (:const:`Py_TPFLAGS_HEAPTYPE`), the
- deallocator should decrement the reference count for its type object after
- calling the type deallocator. In order to avoid dangling pointers, the
- recommended way to achieve this is:
-
- .. code-block:: c
-
- static void foo_dealloc(foo_object *self) {
- PyTypeObject *tp = Py_TYPE(self);
- // free references and buffers here
- tp->tp_free(self);
- Py_DECREF(tp);
- }
-
-
- **Inheritance:**
-
This field is inherited by subtypes.
-.. c:member:: Py_ssize_t PyTypeObject.tp_vectorcall_offset
-
- An optional offset to a per-instance function that implements calling
- the object using the :ref:`vectorcall protocol <vectorcall>`,
- a more efficient alternative
- of the simpler :c:member:`~PyTypeObject.tp_call`.
+.. c:member:: printfunc PyTypeObject.tp_print
- This field is only used if the flag :const:`_Py_TPFLAGS_HAVE_VECTORCALL`
- is set. If so, this must be a positive integer containing the offset in the
- instance of a :c:type:`vectorcallfunc` pointer.
-
- The *vectorcallfunc* pointer may be ``NULL``, in which case the instance behaves
- as if :const:`_Py_TPFLAGS_HAVE_VECTORCALL` was not set: calling the instance
- falls back to :c:member:`~PyTypeObject.tp_call`.
-
- Any class that sets ``_Py_TPFLAGS_HAVE_VECTORCALL`` must also set
- :c:member:`~PyTypeObject.tp_call` and make sure its behaviour is consistent
- with the *vectorcallfunc* function.
- This can be done by setting *tp_call* to :c:func:`PyVectorcall_Call`.
-
- .. warning::
-
- It is not recommended for :ref:`heap types <heap-types>` to implement
- the vectorcall protocol.
- When a user sets :attr:`__call__` in Python code, only *tp_call* is updated,
- likely making it inconsistent with the vectorcall function.
-
- .. note::
+ An optional pointer to the instance print function.
- The semantics of the ``tp_vectorcall_offset`` slot are provisional and
- expected to be finalized in Python 3.9.
- If you use vectorcall, plan for updating your code for Python 3.9.
+ 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 :class:`~StringIO.StringIO` instance), the
+ instance's :c:member:`~PyTypeObject.tp_repr` or :c:member:`~PyTypeObject.tp_str` function is called to convert it to
+ a string. These are also called when the type's :c:member:`~PyTypeObject.tp_print` field is
+ *NULL*. A type should never implement :c:member:`~PyTypeObject.tp_print` in a way that produces
+ different output than :c:member:`~PyTypeObject.tp_repr` or :c:member:`~PyTypeObject.tp_str` would.
- .. versionchanged:: 3.8
+ The print function is called with the same signature as :c:func:`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 :const:`Py_PRINT_RAW`. When the :const:`Py_PRINT_RAW`
+ flag bit is set, the instance should be printed the same way as :c:member:`~PyTypeObject.tp_str`
+ would format it; when the :const:`Py_PRINT_RAW` flag bit is clear, the instance
+ should be printed the same was as :c:member:`~PyTypeObject.tp_repr` would format it. It should
+ return ``-1`` and set an exception condition when an error occurred during the
+ comparison.
- Before version 3.8, this slot was named ``tp_print``.
- In Python 2.x, it was used for printing to a file.
- In Python 3.0 to 3.7, it was unused.
+ It is possible that the :c:member:`~PyTypeObject.tp_print` field will be deprecated. In any case,
+ it is recommended not to define :c:member:`~PyTypeObject.tp_print`, but instead to rely on
+ :c:member:`~PyTypeObject.tp_repr` and :c:member:`~PyTypeObject.tp_str` for printing.
- **Inheritance:**
-
- This field is always inherited.
- However, the :const:`_Py_TPFLAGS_HAVE_VECTORCALL` flag is not
- always inherited. If it's not, then the subclass won't use
- :ref:`vectorcall <vectorcall>`, except when
- :c:func:`PyVectorcall_Call` is explicitly called.
- This is in particular the case for `heap types`_
- (including subclasses defined in Python).
+ This field is inherited by subtypes.
.. c:member:: getattrfunc PyTypeObject.tp_getattr
@@ -742,15 +230,13 @@ and :c:type:`PyType_Type` effectively act as defaults.)
This field is deprecated. When it is defined, it should point to a function
that acts the same as the :c:member:`~PyTypeObject.tp_getattro` function, but taking a C string
- instead of a Python string object to give the attribute name.
-
- **Inheritance:**
+ instead of a Python string object to give the attribute name. The signature is ::
- Group: :attr:`tp_getattr`, :attr:`tp_getattro`
+ PyObject * tp_getattr(PyObject *o, char *attr_name);
This field is inherited by subtypes together with :c:member:`~PyTypeObject.tp_getattro`: a subtype
inherits both :c:member:`~PyTypeObject.tp_getattr` and :c:member:`~PyTypeObject.tp_getattro` from its base type when
- the subtype's :c:member:`~PyTypeObject.tp_getattr` and :c:member:`~PyTypeObject.tp_getattro` are both ``NULL``.
+ the subtype's :c:member:`~PyTypeObject.tp_getattr` and :c:member:`~PyTypeObject.tp_getattro` are both *NULL*.
.. c:member:: setattrfunc PyTypeObject.tp_setattr
@@ -759,30 +245,29 @@ and :c:type:`PyType_Type` effectively act as defaults.)
This field is deprecated. When it is defined, it should point to a function
that acts the same as the :c:member:`~PyTypeObject.tp_setattro` function, but taking a C string
- instead of a Python string object to give the attribute name.
+ instead of a Python string object to give the attribute name. The signature is ::
- **Inheritance:**
-
- Group: :attr:`tp_setattr`, :attr:`tp_setattro`
+ PyObject * tp_setattr(PyObject *o, char *attr_name, PyObject *v);
+ The *v* argument is set to *NULL* to delete the attribute.
This field is inherited by subtypes together with :c:member:`~PyTypeObject.tp_setattro`: a subtype
inherits both :c:member:`~PyTypeObject.tp_setattr` and :c:member:`~PyTypeObject.tp_setattro` from its base type when
- the subtype's :c:member:`~PyTypeObject.tp_setattr` and :c:member:`~PyTypeObject.tp_setattro` are both ``NULL``.
-
+ the subtype's :c:member:`~PyTypeObject.tp_setattr` and :c:member:`~PyTypeObject.tp_setattro` are both *NULL*.
-.. c:member:: PyAsyncMethods* PyTypeObject.tp_as_async
- Pointer to an additional structure that contains fields relevant only to
- objects which implement :term:`awaitable` and :term:`asynchronous iterator`
- protocols at the C-level. See :ref:`async-structs` for details.
+.. c:member:: cmpfunc PyTypeObject.tp_compare
- .. versionadded:: 3.5
- Formerly known as ``tp_compare`` and ``tp_reserved``.
+ An optional pointer to the three-way comparison function.
- **Inheritance:**
+ The signature is the same as for :c:func:`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.
- The :c:member:`~PyTypeObject.tp_as_async` field is not inherited,
- but the contained fields are inherited individually.
+ This field is inherited by subtypes together with :c:member:`~PyTypeObject.tp_richcompare` and
+ :c:member:`~PyTypeObject.tp_hash`: a subtypes inherits all three of :c:member:`~PyTypeObject.tp_compare`,
+ :c:member:`~PyTypeObject.tp_richcompare`, and :c:member:`~PyTypeObject.tp_hash` when the subtype's
+ :c:member:`~PyTypeObject.tp_compare`, :c:member:`~PyTypeObject.tp_richcompare`, and :c:member:`~PyTypeObject.tp_hash` are all *NULL*.
.. c:member:: reprfunc PyTypeObject.tp_repr
@@ -792,60 +277,45 @@ and :c:type:`PyType_Type` effectively act as defaults.)
An optional pointer to a function that implements the built-in function
:func:`repr`.
- The signature is the same as for :c:func:`PyObject_Repr`::
-
- PyObject *tp_repr(PyObject *self);
-
- The function must return a string or a Unicode object. Ideally,
- this function should return a string that, when passed to
- :func:`eval`, given a suitable environment, returns an object with the
+ The signature is the same as for :c:func:`PyObject_Repr`; it must return a string
+ or a Unicode object. Ideally, this function should return a string that, when
+ passed to :func:`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.
- **Inheritance:**
-
- This field is inherited by subtypes.
-
- **Default:**
-
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.
-.. c:member:: PyNumberMethods* PyTypeObject.tp_as_number
+.. c:member:: 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
:ref:`number-structs`.
- **Inheritance:**
-
The :c:member:`~PyTypeObject.tp_as_number` field is not inherited, but the contained fields are
inherited individually.
-.. c:member:: PySequenceMethods* PyTypeObject.tp_as_sequence
+.. c:member:: 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 :ref:`sequence-structs`.
- **Inheritance:**
-
The :c:member:`~PyTypeObject.tp_as_sequence` field is not inherited, but the contained fields
are inherited individually.
-.. c:member:: PyMappingMethods* PyTypeObject.tp_as_mapping
+.. c:member:: 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
:ref:`mapping-structs`.
- **Inheritance:**
-
The :c:member:`~PyTypeObject.tp_as_mapping` field is not inherited, but the contained fields
are inherited individually.
@@ -857,17 +327,10 @@ and :c:type:`PyType_Type` effectively act as defaults.)
An optional pointer to a function that implements the built-in function
:func:`hash`.
- The signature is the same as for :c:func:`PyObject_Hash`::
-
- Py_hash_t tp_hash(PyObject *);
-
- 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``.
-
- When this field is not set (*and* :attr:`tp_richcompare` is not set),
- an attempt to take the hash of the object raises :exc:`TypeError`.
- This is the same as setting it to :c:func:`PyObject_HashNotImplemented`.
+ The signature is the same as for :c:func:`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 :c:func:`PyObject_HashNotImplemented` to
block inheritance of the hash method from a parent type. This is interpreted
@@ -877,25 +340,21 @@ and :c:type:`PyType_Type` effectively act as defaults.)
the Python level will result in the ``tp_hash`` slot being set to
:c:func:`PyObject_HashNotImplemented`.
- **Inheritance:**
+ When this field is not set, two possibilities exist: if the :c:member:`~PyTypeObject.tp_compare`
+ and :c:member:`~PyTypeObject.tp_richcompare` fields are both *NULL*, a default hash value based on
+ the object's address is returned; otherwise, a :exc:`TypeError` is raised.
- Group: :attr:`tp_hash`, :attr:`tp_richcompare`
-
- This field is inherited by subtypes together with
- :c:member:`~PyTypeObject.tp_richcompare`: a subtype inherits both of
- :c:member:`~PyTypeObject.tp_richcompare` and :c:member:`~PyTypeObject.tp_hash`, when the subtype's
- :c:member:`~PyTypeObject.tp_richcompare` and :c:member:`~PyTypeObject.tp_hash` are both ``NULL``.
+ This field is inherited by subtypes together with :c:member:`~PyTypeObject.tp_richcompare` and
+ :c:member:`~PyTypeObject.tp_compare`: a subtypes inherits all three of :c:member:`~PyTypeObject.tp_compare`,
+ :c:member:`~PyTypeObject.tp_richcompare`, and :c:member:`~PyTypeObject.tp_hash`, when the subtype's
+ :c:member:`~PyTypeObject.tp_compare`, :c:member:`~PyTypeObject.tp_richcompare` and :c:member:`~PyTypeObject.tp_hash` are all *NULL*.
.. c:member:: 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 :c:func:`PyObject_Call`::
-
- PyObject *tp_call(PyObject *self, PyObject *args, PyObject *kwargs);
-
- **Inheritance:**
+ should be *NULL* if the object is not callable. The signature is the same as
+ for :c:func:`PyObject_Call`.
This field is inherited by subtypes.
@@ -907,72 +366,42 @@ and :c:type:`PyType_Type` effectively act as defaults.)
constructor for that type. This constructor calls :c:func:`PyObject_Str` to do
the actual work, and :c:func:`PyObject_Str` will call this handler.)
- The signature is the same as for :c:func:`PyObject_Str`::
-
- PyObject *tp_str(PyObject *self);
-
- The function must return a string or a Unicode object. It should be a "friendly" string
- representation of the object, as this is the representation that will be used,
- among other things, by the :func:`print` function.
-
- **Inheritance:**
-
- This field is inherited by subtypes.
-
- **Default:**
+ The signature is the same as for :c:func:`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, :c:func:`PyObject_Repr` is called to return a string
representation.
+ This field is inherited by subtypes.
+
.. c:member:: getattrofunc PyTypeObject.tp_getattro
An optional pointer to the get-attribute function.
- The signature is the same as for :c:func:`PyObject_GetAttr`::
-
- PyObject *tp_getattro(PyObject *self, PyObject *attr);
-
- It is usually convenient to set this field to :c:func:`PyObject_GenericGetAttr`,
- which implements the normal way of looking for object attributes.
-
- **Inheritance:**
-
- Group: :attr:`tp_getattr`, :attr:`tp_getattro`
+ The signature is the same as for :c:func:`PyObject_GetAttr`. It is usually
+ convenient to set this field to :c:func:`PyObject_GenericGetAttr`, which
+ implements the normal way of looking for object attributes.
This field is inherited by subtypes together with :c:member:`~PyTypeObject.tp_getattr`: a subtype
inherits both :c:member:`~PyTypeObject.tp_getattr` and :c:member:`~PyTypeObject.tp_getattro` from its base type when
- the subtype's :c:member:`~PyTypeObject.tp_getattr` and :c:member:`~PyTypeObject.tp_getattro` are both ``NULL``.
-
- **Default:**
-
- :c:type:`PyBaseObject_Type` uses :c:func:`PyObject_GenericGetAttr`.
+ the subtype's :c:member:`~PyTypeObject.tp_getattr` and :c:member:`~PyTypeObject.tp_getattro` are both *NULL*.
.. c:member:: setattrofunc PyTypeObject.tp_setattro
An optional pointer to the function for setting and deleting attributes.
- The signature is the same as for :c:func:`PyObject_SetAttr`::
-
- int tp_setattro(PyObject *self, PyObject *attr, PyObject *value);
-
- In addition, setting *value* to ``NULL`` to delete an attribute must be
- supported. It is usually convenient to set this field to
- :c:func:`PyObject_GenericSetAttr`, which implements the normal
- way of setting object attributes.
-
- **Inheritance:**
-
- Group: :attr:`tp_setattr`, :attr:`tp_setattro`
+ The signature is the same as for :c:func:`PyObject_SetAttr`, but setting
+ *v* to *NULL* to delete an attribute must be supported. It is usually
+ convenient to set this field to :c:func:`PyObject_GenericSetAttr`, which
+ implements the normal way of setting object attributes.
This field is inherited by subtypes together with :c:member:`~PyTypeObject.tp_setattr`: a subtype
inherits both :c:member:`~PyTypeObject.tp_setattr` and :c:member:`~PyTypeObject.tp_setattro` from its base type when
- the subtype's :c:member:`~PyTypeObject.tp_setattr` and :c:member:`~PyTypeObject.tp_setattro` are both ``NULL``.
-
- **Default:**
-
- :c:type:`PyBaseObject_Type` uses :c:func:`PyObject_GenericSetAttr`.
+ the subtype's :c:member:`~PyTypeObject.tp_setattr` and :c:member:`~PyTypeObject.tp_setattro` are both *NULL*.
.. c:member:: PyBufferProcs* PyTypeObject.tp_as_buffer
@@ -981,13 +410,11 @@ and :c:type:`PyType_Type` effectively act as defaults.)
which implement the buffer interface. These fields are documented in
:ref:`buffer-structs`.
- **Inheritance:**
-
- The :c:member:`~PyTypeObject.tp_as_buffer` field is not inherited,
- but the contained fields are inherited individually.
+ The :c:member:`~PyTypeObject.tp_as_buffer` field is not inherited, but the contained fields are
+ inherited individually.
-.. c:member:: unsigned long PyTypeObject.tp_flags
+.. c:member:: 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
@@ -995,9 +422,7 @@ and :c:type:`PyType_Type` effectively act as defaults.)
:c:member:`~PyTypeObject.tp_as_number`, :c:member:`~PyTypeObject.tp_as_sequence`, :c:member:`~PyTypeObject.tp_as_mapping`, and
:c:member:`~PyTypeObject.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:**
+ 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
@@ -1007,208 +432,166 @@ and :c:type:`PyType_Type` effectively act as defaults.)
structure. The :const:`Py_TPFLAGS_HAVE_GC` flag bit is inherited together with
the :c:member:`~PyTypeObject.tp_traverse` and :c:member:`~PyTypeObject.tp_clear` fields, i.e. if the
:const:`Py_TPFLAGS_HAVE_GC` flag bit is clear in the subtype and the
- :c:member:`~PyTypeObject.tp_traverse` and :c:member:`~PyTypeObject.tp_clear` fields in the subtype exist and have
- ``NULL`` values.
-
- .. XXX are most flag bits *really* inherited individually?
-
- **Default:**
-
- :c:type:`PyBaseObject_Type` uses
- ``Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE``.
-
- **Bit Masks:**
+ :c:member:`~PyTypeObject.tp_traverse` and :c:member:`~PyTypeObject.tp_clear` fields in the subtype exist (as
+ indicated by the :const:`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 :c:member:`~PyTypeObject.tp_flags` field. The macro
:c:func:`PyType_HasFeature` takes a type and a flags value, *tp* and *f*, and
checks whether ``tp->tp_flags & f`` is non-zero.
- .. data:: Py_TPFLAGS_HEAPTYPE
- This bit is set when the type object itself is allocated on the heap, for
- example, types created dynamically using :c:func:`PyType_FromSpec`. In this
- case, the :attr:`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).
+ .. data:: Py_TPFLAGS_HAVE_GETCHARBUFFER
- **Inheritance:**
+ If this bit is set, the :c:type:`PyBufferProcs` struct referenced by
+ :c:member:`~PyTypeObject.tp_as_buffer` has the :attr:`bf_getcharbuffer` field.
- ???
+ .. data:: Py_TPFLAGS_HAVE_SEQUENCE_IN
- .. data:: Py_TPFLAGS_BASETYPE
+ If this bit is set, the :c:type:`PySequenceMethods` struct referenced by
+ :c:member:`~PyTypeObject.tp_as_sequence` has the :attr:`sq_contains` field.
- 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).
- **Inheritance:**
+ .. data:: 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.
- .. data:: Py_TPFLAGS_READY
+ .. data:: Py_TPFLAGS_HAVE_INPLACEOPS
- This bit is set when the type object has been fully initialized by
- :c:func:`PyType_Ready`.
+ If this bit is set, the :c:type:`PySequenceMethods` struct referenced by
+ :c:member:`~PyTypeObject.tp_as_sequence` and the :c:type:`PyNumberMethods` structure referenced by
+ :c:member:`~PyTypeObject.tp_as_number` contain the fields for in-place operators. In particular,
+ this means that the :c:type:`PyNumberMethods` structure has the fields
+ :attr:`nb_inplace_add`, :attr:`nb_inplace_subtract`,
+ :attr:`nb_inplace_multiply`, :attr:`nb_inplace_divide`,
+ :attr:`nb_inplace_remainder`, :attr:`nb_inplace_power`,
+ :attr:`nb_inplace_lshift`, :attr:`nb_inplace_rshift`, :attr:`nb_inplace_and`,
+ :attr:`nb_inplace_xor`, and :attr:`nb_inplace_or`; and the
+ :c:type:`PySequenceMethods` struct has the fields :attr:`sq_inplace_concat` and
+ :attr:`sq_inplace_repeat`.
- **Inheritance:**
- ???
+ .. data:: Py_TPFLAGS_CHECKTYPES
+ If this bit is set, the binary and ternary operations in the
+ :c:type:`PyNumberMethods` structure referenced by :c:member:`~PyTypeObject.tp_as_number` accept
+ arguments of arbitrary object types, and do their own type conversions 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 applies to :attr:`nb_add`, :attr:`nb_subtract`,
+ :attr:`nb_multiply`, :attr:`nb_divide`, :attr:`nb_remainder`, :attr:`nb_divmod`,
+ :attr:`nb_power`, :attr:`nb_lshift`, :attr:`nb_rshift`, :attr:`nb_and`,
+ :attr:`nb_xor`, and :attr:`nb_or`.
- .. data:: Py_TPFLAGS_READYING
-
- This bit is set while :c:func:`PyType_Ready` is in the process of initializing
- the type object.
-
- **Inheritance:**
-
- ???
-
-
- .. data:: Py_TPFLAGS_HAVE_GC
-
- This bit is set when the object supports garbage collection. If this bit
- is set, instances must be created using :c:func:`PyObject_GC_New` and
- destroyed using :c:func:`PyObject_GC_Del`. More information in section
- :ref:`supporting-cycle-detection`. This bit also implies that the
- GC-related fields :c:member:`~PyTypeObject.tp_traverse` and :c:member:`~PyTypeObject.tp_clear` are present in
- the type object.
-
- **Inheritance:**
-
- Group: :const:`Py_TPFLAGS_HAVE_GC`, :attr:`tp_traverse`, :attr:`tp_clear`
-
- The :const:`Py_TPFLAGS_HAVE_GC` flag bit is inherited
- together with the :attr:`tp_traverse` and :attr:`tp_clear`
- fields, i.e. if the :const:`Py_TPFLAGS_HAVE_GC` flag bit is
- clear in the subtype and the :attr:`tp_traverse` and
- :attr:`tp_clear` fields in the subtype exist and have ``NULL``
- values.
-
-
- .. data:: 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 following bits: :const:`Py_TPFLAGS_HAVE_STACKLESS_EXTENSION`,
- :const:`Py_TPFLAGS_HAVE_VERSION_TAG`.
- **Inheritance:**
+ .. data:: Py_TPFLAGS_HAVE_RICHCOMPARE
- ???
+ If this bit is set, the type object has the :c:member:`~PyTypeObject.tp_richcompare` field, as
+ well as the :c:member:`~PyTypeObject.tp_traverse` and the :c:member:`~PyTypeObject.tp_clear` fields.
- .. data:: Py_TPFLAGS_METHOD_DESCRIPTOR
+ .. data:: Py_TPFLAGS_HAVE_WEAKREFS
- This bit indicates that objects behave like unbound methods.
+ If this bit is set, the :c:member:`~PyTypeObject.tp_weaklistoffset` field is defined. Instances
+ of a type are weakly referenceable if the type's :c:member:`~PyTypeObject.tp_weaklistoffset` field
+ has a value greater than zero.
- If this flag is set for ``type(meth)``, then:
- - ``meth.__get__(obj, cls)(*args, **kwds)`` (with ``obj`` not None)
- must be equivalent to ``meth(obj, *args, **kwds)``.
+ .. data:: Py_TPFLAGS_HAVE_ITER
- - ``meth.__get__(None, cls)(*args, **kwds)``
- must be equivalent to ``meth(*args, **kwds)``.
+ If this bit is set, the type object has the :c:member:`~PyTypeObject.tp_iter` and
+ :c:member:`~PyTypeObject.tp_iternext` fields.
- This flag enables an optimization for typical method calls like
- ``obj.meth()``: it avoids creating a temporary "bound method" object for
- ``obj.meth``.
- .. versionadded:: 3.8
+ .. data:: Py_TPFLAGS_HAVE_CLASS
- **Inheritance:**
+ If this bit is set, the type object has several new fields defined starting in
+ Python 2.2: :c:member:`~PyTypeObject.tp_methods`, :c:member:`~PyTypeObject.tp_members`, :c:member:`~PyTypeObject.tp_getset`,
+ :c:member:`~PyTypeObject.tp_base`, :c:member:`~PyTypeObject.tp_dict`, :c:member:`~PyTypeObject.tp_descr_get`, :c:member:`~PyTypeObject.tp_descr_set`,
+ :c:member:`~PyTypeObject.tp_dictoffset`, :c:member:`~PyTypeObject.tp_init`, :c:member:`~PyTypeObject.tp_alloc`, :c:member:`~PyTypeObject.tp_new`,
+ :c:member:`~PyTypeObject.tp_free`, :c:member:`~PyTypeObject.tp_is_gc`, :c:member:`~PyTypeObject.tp_bases`, :c:member:`~PyTypeObject.tp_mro`,
+ :c:member:`~PyTypeObject.tp_cache`, :c:member:`~PyTypeObject.tp_subclasses`, and :c:member:`~PyTypeObject.tp_weaklist`.
- This flag is never inherited by heap types.
- For extension types, it is inherited whenever
- :c:member:`~PyTypeObject.tp_descr_get` is inherited.
+ .. data:: Py_TPFLAGS_HEAPTYPE
- .. XXX Document more flags here?
-
+ This bit is set when the type object itself is allocated on the heap. In this
+ case, the :attr:`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).
- .. data:: Py_TPFLAGS_LONG_SUBCLASS
- .. data:: Py_TPFLAGS_LIST_SUBCLASS
- .. data:: Py_TPFLAGS_TUPLE_SUBCLASS
- .. data:: Py_TPFLAGS_BYTES_SUBCLASS
- .. data:: Py_TPFLAGS_UNICODE_SUBCLASS
- .. data:: Py_TPFLAGS_DICT_SUBCLASS
- .. data:: Py_TPFLAGS_BASE_EXC_SUBCLASS
- .. data:: Py_TPFLAGS_TYPE_SUBCLASS
- These flags are used by functions such as
- :c:func:`PyLong_Check` to quickly determine if a type is a subclass
- of a built-in type; such specific checks are faster than a generic
- check, like :c:func:`PyObject_IsInstance`. Custom types that inherit
- from built-ins should have their :c:member:`~PyTypeObject.tp_flags`
- set appropriately, or the code that interacts with such types
- will behave differently depending on what kind of check is used.
+ .. data:: 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).
- .. data:: Py_TPFLAGS_HAVE_FINALIZE
- This bit is set when the :c:member:`~PyTypeObject.tp_finalize` slot is present in the
- type structure.
+ .. data:: Py_TPFLAGS_READY
- .. versionadded:: 3.4
+ This bit is set when the type object has been fully initialized by
+ :c:func:`PyType_Ready`.
- .. deprecated:: 3.8
- This flag isn't necessary anymore, as the interpreter assumes the
- :c:member:`~PyTypeObject.tp_finalize` slot is always present in the
- type structure.
+ .. data:: Py_TPFLAGS_READYING
- .. data:: _Py_TPFLAGS_HAVE_VECTORCALL
+ This bit is set while :c:func:`PyType_Ready` is in the process of initializing
+ the type object.
- This bit is set when the class implements
- the :ref:`vectorcall protocol <vectorcall>`.
- See :c:member:`~PyTypeObject.tp_vectorcall_offset` for details.
- **Inheritance:**
+ .. data:: Py_TPFLAGS_HAVE_GC
- This bit is inherited for *static* subtypes if
- :c:member:`~PyTypeObject.tp_call` is also inherited.
- `Heap types`_ do not inherit ``_Py_TPFLAGS_HAVE_VECTORCALL``.
+ This bit is set when the object supports garbage collection. If this bit
+ is set, instances must be created using :c:func:`PyObject_GC_New` and
+ destroyed using :c:func:`PyObject_GC_Del`. More information in section
+ :ref:`supporting-cycle-detection`. This bit also implies that the
+ GC-related fields :c:member:`~PyTypeObject.tp_traverse` and :c:member:`~PyTypeObject.tp_clear` are present in
+ the type object; but those fields also exist when
+ :const:`Py_TPFLAGS_HAVE_GC` is clear but
+ :const:`Py_TPFLAGS_HAVE_RICHCOMPARE` is set.
- .. note::
- This flag is provisional and expected to become public in Python 3.9,
- with a different name and, possibly, changed semantics.
- If you use vectorcall, plan for updating your code for Python 3.9.
+ .. data:: Py_TPFLAGS_DEFAULT
- .. versionadded:: 3.8
+ 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 following bits: :const:`Py_TPFLAGS_HAVE_GETCHARBUFFER`,
+ :const:`Py_TPFLAGS_HAVE_SEQUENCE_IN`, :const:`Py_TPFLAGS_HAVE_INPLACEOPS`,
+ :const:`Py_TPFLAGS_HAVE_RICHCOMPARE`, :const:`Py_TPFLAGS_HAVE_WEAKREFS`,
+ :const:`Py_TPFLAGS_HAVE_ITER`, and :const:`Py_TPFLAGS_HAVE_CLASS`.
-.. c:member:: const char* PyTypeObject.tp_doc
+.. c:member:: 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 :attr:`__doc__` attribute on the type and
instances of the type.
- **Inheritance:**
-
This field is *not* inherited by subtypes.
+The following three fields only exist if the
+:const:`Py_TPFLAGS_HAVE_RICHCOMPARE` flag bit is set.
+
.. c:member:: traverseproc PyTypeObject.tp_traverse
An optional pointer to a traversal function for the garbage collector. This is
- only used if the :const:`Py_TPFLAGS_HAVE_GC` flag bit is set. The signature is::
-
- int tp_traverse(PyObject *self, visitproc visit, void *arg);
-
- More information about Python's garbage collection scheme can be found
- in section :ref:`supporting-cycle-detection`.
+ only used if the :const:`Py_TPFLAGS_HAVE_GC` flag bit is set. More information
+ about Python's garbage collection scheme can be found in section
+ :ref:`supporting-cycle-detection`.
The :c:member:`~PyTypeObject.tp_traverse` pointer is used by the garbage collector to detect
reference cycles. A typical implementation of a :c:member:`~PyTypeObject.tp_traverse` function
simply calls :c:func:`Py_VISIT` on each of the instance's members that are Python
objects. For example, this is function :c:func:`local_traverse` from the
- :mod:`_thread` extension module::
+ :mod:`thread` extension module::
static int
local_traverse(localobject *self, visitproc visit, void *arg)
@@ -1221,7 +604,7 @@ and :c:type:`PyType_Type` effectively act as defaults.)
Note that :c:func:`Py_VISIT` is called only on those members that can participate
in reference cycles. Although 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.
+ 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 :mod:`gc` module's
@@ -1231,22 +614,17 @@ and :c:type:`PyType_Type` effectively act as defaults.)
:c:func:`local_traverse` to have these specific names; don't name them just
anything.
- **Inheritance:**
-
- Group: :const:`Py_TPFLAGS_HAVE_GC`, :attr:`tp_traverse`, :attr:`tp_clear`
-
This field is inherited by subtypes together with :c:member:`~PyTypeObject.tp_clear` and the
:const:`Py_TPFLAGS_HAVE_GC` flag bit: the flag bit, :c:member:`~PyTypeObject.tp_traverse`, and
:c:member:`~PyTypeObject.tp_clear` are all inherited from the base type if they are all zero in
- the subtype.
+ the subtype *and* the subtype has the :const:`Py_TPFLAGS_HAVE_RICHCOMPARE` flag
+ bit set.
.. c:member:: inquiry PyTypeObject.tp_clear
An optional pointer to a clear function for the garbage collector. This is only
- used if the :const:`Py_TPFLAGS_HAVE_GC` flag bit is set. The signature is::
-
- int tp_clear(PyObject *);
+ used if the :const:`Py_TPFLAGS_HAVE_GC` flag bit is set.
The :c:member:`~PyTypeObject.tp_clear` member function is used to break reference cycles in cyclic
garbage detected by the garbage collector. Taken together, all :c:member:`~PyTypeObject.tp_clear`
@@ -1260,7 +638,7 @@ and :c:type:`PyType_Type` effectively act as defaults.)
Implementations of :c:member:`~PyTypeObject.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::
+ members to *NULL*, as in the following example::
static int
local_clear(localobject *self)
@@ -1274,12 +652,12 @@ and :c:type:`PyType_Type` effectively act as defaults.)
The :c:func:`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
+ 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,
+ 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
:c:func:`Py_CLEAR` macro performs the operations in a safe order.
@@ -1292,30 +670,34 @@ and :c:type:`PyType_Type` effectively act as defaults.)
More information about Python's garbage collection scheme can be found in
section :ref:`supporting-cycle-detection`.
- **Inheritance:**
-
- Group: :const:`Py_TPFLAGS_HAVE_GC`, :attr:`tp_traverse`, :attr:`tp_clear`
-
This field is inherited by subtypes together with :c:member:`~PyTypeObject.tp_traverse` and the
:const:`Py_TPFLAGS_HAVE_GC` flag bit: the flag bit, :c:member:`~PyTypeObject.tp_traverse`, and
:c:member:`~PyTypeObject.tp_clear` are all inherited from the base type if they are all zero in
- the subtype.
+ the subtype *and* the subtype has the :const:`Py_TPFLAGS_HAVE_RICHCOMPARE` flag
+ bit set.
.. c:member:: richcmpfunc PyTypeObject.tp_richcompare
- An optional pointer to the rich comparison function, whose signature is::
-
- PyObject *tp_richcompare(PyObject *self, PyObject *other, int op);
-
- The first parameter is guaranteed to be an instance of the type
- that is defined by :c:type:`PyTypeObject`.
+ 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 comparison 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 :exc:`TypeError` in the rich comparison function.
+
+ This field is inherited by subtypes together with :c:member:`~PyTypeObject.tp_compare` and
+ :c:member:`~PyTypeObject.tp_hash`: a subtype inherits all three of :c:member:`~PyTypeObject.tp_compare`,
+ :c:member:`~PyTypeObject.tp_richcompare`, and :c:member:`~PyTypeObject.tp_hash`, when the subtype's
+ :c:member:`~PyTypeObject.tp_compare`, :c:member:`~PyTypeObject.tp_richcompare`, and :c:member:`~PyTypeObject.tp_hash` are all *NULL*.
+
The following constants are defined to be used as the third argument for
:c:member:`~PyTypeObject.tp_richcompare` and for :c:func:`PyObject_RichCompare`:
@@ -1335,54 +717,22 @@ and :c:type:`PyType_Type` effectively act as defaults.)
| :const:`Py_GE` | ``>=`` |
+----------------+------------+
- The following macro is defined to ease writing rich comparison functions:
-
- .. c:function:: PyObject \*Py_RETURN_RICHCOMPARE(VAL_A, VAL_B, int op)
-
- Return ``Py_True`` or ``Py_False`` from the function, depending on the
- result of a comparison.
- VAL_A and VAL_B must be orderable by C comparison operators (for example,
- they may be C ints or floats). The third argument specifies the requested
- operation, as for :c:func:`PyObject_RichCompare`.
-
- The return value's reference count is properly incremented.
-
- On error, sets an exception and returns ``NULL`` from the function.
-
- .. versionadded:: 3.7
-
- **Inheritance:**
-
- Group: :attr:`tp_hash`, :attr:`tp_richcompare`
-
- This field is inherited by subtypes together with :c:member:`~PyTypeObject.tp_hash`:
- a subtype inherits :c:member:`~PyTypeObject.tp_richcompare` and :c:member:`~PyTypeObject.tp_hash` when
- the subtype's :c:member:`~PyTypeObject.tp_richcompare` and :c:member:`~PyTypeObject.tp_hash` are both
- ``NULL``.
-
- **Default:**
- :c:type:`PyBaseObject_Type` provides a :attr:`tp_richcompare`
- implementation, which may be inherited. However, if only
- :attr:`tp_hash` is defined, not even the inherited function is used
- and instances of the type will not be able to participate in any
- comparisons.
+The next field only exists if the :const:`Py_TPFLAGS_HAVE_WEAKREFS` flag bit is
+set.
-
-.. c:member:: Py_ssize_t PyTypeObject.tp_weaklistoffset
+.. c:member:: 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
:c:func:`PyObject_ClearWeakRefs` and the :c:func:`PyWeakref_\*` functions. The
instance structure needs to include a field of type :c:type:`PyObject\*` which is
- initialized to ``NULL``.
+ initialized to *NULL*.
Do not confuse this field with :c:member:`~PyTypeObject.tp_weaklist`; that is the list head for
weak references to the type object itself.
- **Inheritance:**
-
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
@@ -1402,18 +752,18 @@ and :c:type:`PyType_Type` effectively act as defaults.)
:attr:`__weakref__`, the type inherits its :c:member:`~PyTypeObject.tp_weaklistoffset` from its
base type.
+The next two fields only exist if the :const:`Py_TPFLAGS_HAVE_ITER` flag bit is
+set.
+
.. c:member:: 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).
+ sequences may be iterable without this function, and classic instances always
+ have this function, even if they don't define an :meth:`__iter__` method).
- This function has the same signature as :c:func:`PyObject_GetIter`::
-
- PyObject *tp_iter(PyObject *self);
-
- **Inheritance:**
+ This function has the same signature as :c:func:`PyObject_GetIter`.
This field is inherited by subtypes.
@@ -1421,14 +771,11 @@ and :c:type:`PyType_Type` effectively act as defaults.)
.. c:member:: iternextfunc PyTypeObject.tp_iternext
An optional pointer to a function that returns the next item in an iterator.
- The signature is::
-
- PyObject *tp_iternext(PyObject *self);
-
- When the iterator is exhausted, it must return ``NULL``; a :exc:`StopIteration`
+ When the iterator is exhausted, it must return *NULL*; a :exc:`StopIteration`
exception may or may not be set. When another error occurs, it must return
- ``NULL`` too. Its presence signals that the instances of this type are
- iterators.
+ *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 :meth:`~iterator.next` method).
Iterator types should also define the :c:member:`~PyTypeObject.tp_iter` function, and that
function should return the iterator instance itself (not a new iterator
@@ -1436,50 +783,45 @@ and :c:type:`PyType_Type` effectively act as defaults.)
This function has the same signature as :c:func:`PyIter_Next`.
- **Inheritance:**
-
This field is inherited by subtypes.
+The next fields, up to and including :c:member:`~PyTypeObject.tp_weaklist`, only exist if the
+:const:`Py_TPFLAGS_HAVE_CLASS` flag bit is set.
+
.. c:member:: struct PyMethodDef* PyTypeObject.tp_methods
- An optional pointer to a static ``NULL``-terminated array of :c:type:`PyMethodDef`
+ An optional pointer to a static *NULL*-terminated array of :c:type:`PyMethodDef`
structures, declaring regular methods of this type.
For each entry in the array, an entry is added to the type's dictionary (see
:c:member:`~PyTypeObject.tp_dict` below) containing a method descriptor.
- **Inheritance:**
-
This field is not inherited by subtypes (methods are inherited through a
different mechanism).
.. c:member:: struct PyMemberDef* PyTypeObject.tp_members
- An optional pointer to a static ``NULL``-terminated array of :c:type:`PyMemberDef`
+ An optional pointer to a static *NULL*-terminated array of :c:type:`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
:c:member:`~PyTypeObject.tp_dict` below) containing a member descriptor.
- **Inheritance:**
-
This field is not inherited by subtypes (members are inherited through a
different mechanism).
.. c:member:: struct PyGetSetDef* PyTypeObject.tp_getset
- An optional pointer to a static ``NULL``-terminated array of :c:type:`PyGetSetDef`
+ An optional pointer to a static *NULL*-terminated array of :c:type:`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
:c:member:`~PyTypeObject.tp_dict` below) containing a getset descriptor.
- **Inheritance:**
-
This field is not inherited by subtypes (computed attributes are inherited
through a different mechanism).
@@ -1490,71 +832,34 @@ and :c:type:`PyType_Type` effectively act as defaults.)
this level, only single inheritance is supported; multiple inheritance require
dynamically creating a type object by calling the metatype.
- .. note::
-
- .. from Modules/xxmodule.c
-
- Slot initialization is subject to the rules of initializing globals.
- C99 requires the initializers to be "address constants". Function
- designators like :c:func:`PyType_GenericNew`, with implicit conversion
- to a pointer, are valid C99 address constants.
-
- However, the unary '&' operator applied to a non-static variable
- like :c:func:`PyBaseObject_Type` is not required to produce an address
- constant. Compilers may support this (gcc does), MSVC does not.
- Both compilers are strictly standard conforming in this particular
- behavior.
-
- Consequently, :c:member:`~PyTypeObject.tp_base` should be set in
- the extension module's init function.
-
- **Inheritance:**
-
- This field is not inherited by subtypes (obviously).
-
- **Default:**
-
- This field defaults to ``&PyBaseObject_Type`` (which to Python
- programmers is known as the type :class:`object`).
+ This field is not inherited by subtypes (obviously), but it defaults to
+ ``&PyBaseObject_Type`` (which to Python programmers is known as the type
+ :class:`object`).
.. c:member:: PyObject* PyTypeObject.tp_dict
The type's dictionary is stored here by :c:func:`PyType_Ready`.
- This field should normally be initialized to ``NULL`` before PyType_Ready is
+ This field should normally be initialized to *NULL* before PyType_Ready is
called; it may also be initialized to a dictionary containing initial attributes
for the type. Once :c:func:`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 :meth:`__add__`).
- **Inheritance:**
-
This field is not inherited by subtypes (though the attributes defined in here
are inherited through a different mechanism).
- **Default:**
-
- If this field is ``NULL``, :c:func:`PyType_Ready` will assign a new
- dictionary to it.
-
- .. warning::
-
- It is not safe to use :c:func:`PyDict_SetItem` on or otherwise modify
- :c:member:`~PyTypeObject.tp_dict` with the dictionary C-API.
-
.. c:member:: descrgetfunc PyTypeObject.tp_descr_get
An optional pointer to a "descriptor get" function.
- The function signature is::
+ The function signature is ::
PyObject * tp_descr_get(PyObject *self, PyObject *obj, PyObject *type);
- .. XXX explain more?
-
- **Inheritance:**
+ .. XXX explain.
This field is inherited by subtypes.
@@ -1564,20 +869,17 @@ and :c:type:`PyType_Type` effectively act as defaults.)
An optional pointer to a function for setting and deleting
a descriptor's value.
- The function signature is::
+ The function signature is ::
int tp_descr_set(PyObject *self, PyObject *obj, PyObject *value);
- The *value* argument is set to ``NULL`` to delete the value.
-
- .. XXX explain more?
-
- **Inheritance:**
-
+ The *value* argument is set to *NULL* to delete the value.
This field is inherited by subtypes.
+ .. XXX explain.
+
-.. c:member:: Py_ssize_t PyTypeObject.tp_dictoffset
+.. c:member:: 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
@@ -1608,12 +910,10 @@ and :c:type:`PyType_Type` effectively act as defaults.)
where :c:member:`~PyTypeObject.tp_basicsize`, :c:member:`~PyTypeObject.tp_itemsize` and :c:member:`~PyTypeObject.tp_dictoffset` are
taken from the type object, and :attr:`ob_size` is taken from the instance. The
- absolute value is taken because ints use the sign of :attr:`ob_size` to
+ absolute value is taken because long ints use the sign of :attr:`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 :c:func:`_PyObject_GetDictPtr`.)
- **Inheritance:**
-
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
@@ -1631,11 +931,6 @@ and :c:type:`PyType_Type` effectively act as defaults.)
not have the expected effect, it just causes confusion. Maybe this should be
added as a feature just like :attr:`__weakref__` though.)
- **Default:**
-
- This slot has no default. For static types, if the field is
- ``NULL`` then no :attr:`__dict__` gets created for instances.
-
.. c:member:: initproc PyTypeObject.tp_init
@@ -1646,67 +941,69 @@ and :c:type:`PyType_Type` effectively act as defaults.)
:meth:`__init__`, and it is possible to reinitialize an instance by calling its
:meth:`__init__` method again.
- The function signature is::
+ The function signature is ::
- int tp_init(PyObject *self, PyObject *args, PyObject *kwds);
+ int tp_init(PyObject *self, PyObject *args, PyObject *kwds)
The self argument is the instance to be initialized; the *args* and *kwds*
arguments represent positional and keyword arguments of the call to
:meth:`__init__`.
- The :c:member:`~PyTypeObject.tp_init` function, if not ``NULL``, is called when an instance is
+ The :c:member:`~PyTypeObject.tp_init` function, if not *NULL*, is called when an instance is
created normally by calling its type, after the type's :c:member:`~PyTypeObject.tp_new` function
has returned an instance of the type. If the :c:member:`~PyTypeObject.tp_new` function returns an
instance of some other type that is not a subtype of the original type, no
:c:member:`~PyTypeObject.tp_init` function is called; if :c:member:`~PyTypeObject.tp_new` returns an instance of a
- subtype of the original type, the subtype's :c:member:`~PyTypeObject.tp_init` is called.
-
- Returns ``0`` on success, ``-1`` and sets an exception on error.
-
- **Inheritance:**
+ subtype of the original type, the subtype's :c:member:`~PyTypeObject.tp_init` is called. (VERSION
+ NOTE: described here is what is implemented in Python 2.2.1 and later. In
+ Python 2.2, the :c:member:`~PyTypeObject.tp_init` of the type of the object returned by
+ :c:member:`~PyTypeObject.tp_new` was always called, if not *NULL*.)
This field is inherited by subtypes.
- **Default:**
-
- For static types this field does not have a default.
-
.. c:member:: allocfunc PyTypeObject.tp_alloc
An optional pointer to an instance allocation function.
- The function signature is::
-
- PyObject *tp_alloc(PyTypeObject *self, Py_ssize_t nitems);
-
- **Inheritance:**
+ The function signature is ::
- This field is inherited by static subtypes, but not by dynamic
- subtypes (subtypes created by a class statement).
+ PyObject *tp_alloc(PyTypeObject *self, Py_ssize_t nitems)
- **Default:**
+ 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
+ :attr:`ob_refcnt` set to ``1`` and :attr:`ob_type` set to the type argument. If
+ the type's :c:member:`~PyTypeObject.tp_itemsize` is non-zero, the object's :attr:`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 :c:member:`~PyTypeObject.tp_basicsize`.
- For dynamic subtypes, this field is always set to
- :c:func:`PyType_GenericAlloc`, to force a standard heap
- allocation strategy.
+ Do not use this function to do any other instance initialization, not even to
+ allocate additional memory; that should be done by :c:member:`~PyTypeObject.tp_new`.
- For static subtypes, :c:type:`PyBaseObject_Type` uses
- :c:func:`PyType_GenericAlloc`. That is the recommended value
- for all statically defined types.
+ 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 :c:func:`PyType_GenericAlloc`, to force a standard heap allocation strategy.
+ That is also the recommended value for statically defined types.
.. c:member:: newfunc PyTypeObject.tp_new
An optional pointer to an instance creation function.
- The function signature is::
+ 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 function signature is ::
- PyObject *tp_new(PyTypeObject *subtype, PyObject *args, PyObject *kwds);
+ PyObject *tp_new(PyTypeObject *subtype, PyObject *args, PyObject *kwds)
- The *subtype* argument is the type of the object being created; the *args* and
+ 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 :c:member:`~PyTypeObject.tp_new`
+ type. Note that subtype doesn't have to equal the type whose :c:member:`~PyTypeObject.tp_new`
function is called; it may be a subtype of that type (but not an unrelated
type).
@@ -1718,40 +1015,33 @@ and :c:type:`PyType_Type` effectively act as defaults.)
in :c:member:`~PyTypeObject.tp_new`, while for mutable types, most initialization should be
deferred to :c:member:`~PyTypeObject.tp_init`.
- **Inheritance:**
-
This field is inherited by subtypes, except it is not inherited by static types
- whose :c:member:`~PyTypeObject.tp_base` is ``NULL`` or ``&PyBaseObject_Type``.
-
- **Default:**
+ whose :c:member:`~PyTypeObject.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.
- For static types this field has no default. This means if the
- slot is defined as ``NULL``, the type cannot be called to create new
- instances; presumably there is some other way to create
- instances, like a factory function.
+.. c:member:: destructor PyTypeObject.tp_free
-.. c:member:: freefunc PyTypeObject.tp_free
+ An optional pointer to an instance deallocation function.
- An optional pointer to an instance deallocation function. Its signature is::
+ The signature of this function has changed slightly: in Python 2.2 and 2.2.1,
+ its signature is :c:type:`destructor`::
- void tp_free(void *self);
+ void tp_free(PyObject *)
- An initializer that is compatible with this signature is :c:func:`PyObject_Free`.
+ In Python 2.3 and beyond, its signature is :c:type:`freefunc`::
- **Inheritance:**
+ void tp_free(void *)
- This field is inherited by static subtypes, but not by dynamic
- subtypes (subtypes created by a class statement)
+ The only initializer that is compatible with both versions is ``_PyObject_Del``,
+ whose definition has suitably adapted in Python 2.3.
- **Default:**
-
- In dynamic subtypes, this field is set to a deallocator suitable to
- match :c:func:`PyType_GenericAlloc` and the value of the
+ 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 :c:func:`PyType_GenericAlloc` and the value of the
:const:`Py_TPFLAGS_HAVE_GC` flag bit.
- For static subtypes, :c:type:`PyBaseObject_Type` uses PyObject_Del.
-
.. c:member:: inquiry PyTypeObject.tp_is_gc
@@ -1763,33 +1053,25 @@ and :c:type:`PyType_Type` effectively act as defaults.)
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::
+ ``0`` for a non-collectible instance. The signature is ::
- int tp_is_gc(PyObject *self);
+ int tp_is_gc(PyObject *self)
(The only example of this are types themselves. The metatype,
:c:data:`PyType_Type`, defines this function to distinguish between statically
and dynamically allocated types.)
- **Inheritance:**
-
- This field is inherited by subtypes.
-
- **Default:**
-
- This slot has no default. If this field is ``NULL``,
- :const:`Py_TPFLAGS_HAVE_GC` is used as the functional equivalent.
+ 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.)
.. c:member:: PyObject* PyTypeObject.tp_bases
Tuple of base types.
- This is set for types created by a class statement. It should be ``NULL`` for
+ This is set for types created by a class statement. It should be *NULL* for
statically defined types.
- **Inheritance:**
-
This field is not inherited.
@@ -1798,29 +1080,17 @@ and :c:type:`PyType_Type` effectively act as defaults.)
Tuple containing the expanded set of base types, starting with the type itself
and ending with :class:`object`, in Method Resolution Order.
-
- **Inheritance:**
-
- This field is not inherited; it is calculated fresh by
- :c:func:`PyType_Ready`.
+ This field is not inherited; it is calculated fresh by :c:func:`PyType_Ready`.
.. c:member:: PyObject* PyTypeObject.tp_cache
- Unused. Internal use only.
-
- **Inheritance:**
-
- This field is not inherited.
+ Unused. Not inherited. Internal use only.
.. c:member:: PyObject* PyTypeObject.tp_subclasses
- List of weak references to subclasses. Internal use only.
-
- **Inheritance:**
-
- This field is not inherited.
+ List of weak references to subclasses. Not inherited. Internal use only.
.. c:member:: PyObject* PyTypeObject.tp_weaklist
@@ -1828,108 +1098,32 @@ and :c:type:`PyType_Type` effectively act as defaults.)
Weak reference list head, for weak references to this type object. Not
inherited. Internal use only.
- **Inheritance:**
-
- This field is not inherited.
-
-
-.. c:member:: destructor PyTypeObject.tp_del
-
- This field is deprecated. Use :c:member:`~PyTypeObject.tp_finalize` instead.
-
-
-.. c:member:: unsigned int PyTypeObject.tp_version_tag
-
- Used to index into the method cache. Internal use only.
-
- **Inheritance:**
-
- This field is not inherited.
-
-
-.. c:member:: destructor PyTypeObject.tp_finalize
-
- An optional pointer to an instance finalization function. Its signature is::
-
- void tp_finalize(PyObject *self);
-
- If :c:member:`~PyTypeObject.tp_finalize` is set, the interpreter calls it once when
- finalizing an instance. It is called either from the garbage
- collector (if the instance is part of an isolated reference cycle) or
- just before the object is deallocated. Either way, it is guaranteed
- to be called before attempting to break reference cycles, ensuring
- that it finds the object in a sane state.
-
- :c:member:`~PyTypeObject.tp_finalize` should not mutate the current exception status;
- therefore, a recommended way to write a non-trivial finalizer is::
-
- static void
- local_finalize(PyObject *self)
- {
- PyObject *error_type, *error_value, *error_traceback;
-
- /* Save the current exception, if any. */
- PyErr_Fetch(&error_type, &error_value, &error_traceback);
-
- /* ... */
-
- /* Restore the saved exception. */
- PyErr_Restore(error_type, error_value, error_traceback);
- }
-
- For this field to be taken into account (even through inheritance),
- you must also set the :const:`Py_TPFLAGS_HAVE_FINALIZE` flags bit.
-
- **Inheritance:**
-
- This field is inherited by subtypes.
-
- .. versionadded:: 3.4
-
- .. seealso:: "Safe object finalization" (:pep:`442`)
-
-
-.. c:member:: vectorcallfunc PyTypeObject.tp_vectorcall
-
- Vectorcall function to use for calls of this type object.
- In other words, it is used to implement
- :ref:`vectorcall <vectorcall>` for ``type.__call__``.
- If ``tp_vectorcall`` is ``NULL``, the default call implementation
- using :attr:`__new__` and :attr:`__init__` is used.
-
- **Inheritance:**
-
- This field is never inherited.
-
- .. versionadded:: 3.9 (the field exists since 3.8 but it's only used since 3.9)
-
-
The remaining fields are only defined if the feature test macro
:const:`COUNT_ALLOCS` is defined, and are for internal use only. They are
documented here for completeness. None of these fields are inherited by
-subtypes.
+subtypes. See the :envvar:`PYTHONSHOWALLOCCOUNT` environment variable.
+
.. c:member:: Py_ssize_t PyTypeObject.tp_allocs
Number of allocations.
+
.. c:member:: Py_ssize_t PyTypeObject.tp_frees
Number of frees.
+
.. c:member:: Py_ssize_t PyTypeObject.tp_maxalloc
Maximum simultaneously allocated objects.
-.. c:member:: PyTypeObject* PyTypeObject.tp_prev
-
- Pointer to the previous type object with a non-zero :c:member:`~PyTypeObject.tp_allocs` field.
.. c:member:: PyTypeObject* PyTypeObject.tp_next
Pointer to the next type object with a non-zero :c:member:`~PyTypeObject.tp_allocs` field.
-Also, note that, in a garbage collected Python, :c:member:`~PyTypeObject.tp_dealloc` may be called from
+Also, note that, in a garbage collected Python, tp_dealloc may be called from
any Python thread, not just the thread which created the object (if the object
becomes part of a refcount cycle, that cycle might be collected by a garbage
collection on any thread). This is not a problem for Python API calls, since
@@ -1940,37 +1134,6 @@ objects on the thread which called tp_dealloc will not violate any assumptions
of the library.
-.. _heap-types:
-
-Heap Types
-----------
-
-Traditionally, types defined in C code are *static*, that is,
-a static :c:type:`PyTypeObject` structure is defined directly in code
-and initialized using :c:func:`PyType_Ready`.
-
-This results in types that are limited relative to types defined in Python:
-
-* Static types are limited to one base, i.e. they cannot use multiple
- inheritance.
-* Static type objects (but not necessarily their instances) are immutable.
- It is not possible to add or modify the type object's attributes from Python.
-* Static type objects are shared across
- :ref:`sub-interpreters <sub-interpreter-support>`, so they should not
- include any subinterpreter-specific state.
-
-Also, since :c:type:`PyTypeObject` is not part of the :ref:`stable ABI <stable>`,
-any extension modules using static types must be compiled for a specific
-Python minor version.
-
-An alternative to static types is *heap-allocated types*, or *heap types*
-for short, which correspond closely to classes created by Python's
-``class`` statement.
-
-This is done by filling a :c:type:`PyType_Spec` structure and calling
-:c:func:`PyType_FromSpecWithBases`.
-
-
.. _number-structs:
Number Object Structures
@@ -1982,10 +1145,8 @@ Number Object Structures
.. c:type:: PyNumberMethods
This structure holds pointers to the functions which an object uses to
- implement the number protocol. Each function is used by the function of
- similar name documented in the :ref:`number` section.
-
- .. XXX Drop the definition?
+ implement the number protocol. Almost every function below is used by the
+ function of similar name documented in the :ref:`number` section.
Here is the structure definition::
@@ -1993,26 +1154,32 @@ Number Object Structures
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_bool;
+ inquiry nb_nonzero; /* Used by PyObject_IsTrue */
unaryfunc nb_invert;
binaryfunc nb_lshift;
binaryfunc nb_rshift;
binaryfunc nb_and;
binaryfunc nb_xor;
binaryfunc nb_or;
+ coercion nb_coerce; /* Used by the coerce() function */
unaryfunc nb_int;
- void *nb_reserved;
+ unaryfunc nb_long;
unaryfunc nb_float;
+ unaryfunc nb_oct;
+ unaryfunc nb_hex;
+ /* Added in release 2.0 */
binaryfunc nb_inplace_add;
binaryfunc nb_inplace_subtract;
binaryfunc nb_inplace_multiply;
+ binaryfunc nb_inplace_divide;
binaryfunc nb_inplace_remainder;
ternaryfunc nb_inplace_power;
binaryfunc nb_inplace_lshift;
@@ -2021,68 +1188,43 @@ Number Object Structures
binaryfunc nb_inplace_xor;
binaryfunc nb_inplace_or;
+ /* Added in release 2.2 */
binaryfunc nb_floor_divide;
binaryfunc nb_true_divide;
binaryfunc nb_inplace_floor_divide;
binaryfunc nb_inplace_true_divide;
+ /* Added in release 2.5 */
unaryfunc nb_index;
-
- binaryfunc nb_matrix_multiply;
- binaryfunc nb_inplace_matrix_multiply;
} PyNumberMethods;
- .. note::
- 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). 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.
+Binary and ternary functions may receive different kinds of arguments, depending
+on the flag bit :const:`Py_TPFLAGS_CHECKTYPES`:
- .. note::
+- If :const:`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 :attr:`nb_coerce` member to convert the
+ arguments:
+
+ .. c:member:: coercion PyNumberMethods.nb_coerce
+
+ This function is used by :c:func:`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``.
- The :c:data:`nb_reserved` field should always be ``NULL``. It
- was previously called :c:data:`nb_long`, and was renamed in
- Python 3.0.1.
-
-.. c:member:: binaryfunc PyNumberMethods.nb_add
-.. c:member:: binaryfunc PyNumberMethods.nb_subtract
-.. c:member:: binaryfunc PyNumberMethods.nb_multiply
-.. c:member:: binaryfunc PyNumberMethods.nb_remainder
-.. c:member:: binaryfunc PyNumberMethods.nb_divmod
-.. c:member:: ternaryfunc PyNumberMethods.nb_power
-.. c:member:: unaryfunc PyNumberMethods.nb_negative
-.. c:member:: unaryfunc PyNumberMethods.nb_positive
-.. c:member:: unaryfunc PyNumberMethods.nb_absolute
-.. c:member:: inquiry PyNumberMethods.nb_bool
-.. c:member:: unaryfunc PyNumberMethods.nb_invert
-.. c:member:: binaryfunc PyNumberMethods.nb_lshift
-.. c:member:: binaryfunc PyNumberMethods.nb_rshift
-.. c:member:: binaryfunc PyNumberMethods.nb_and
-.. c:member:: binaryfunc PyNumberMethods.nb_xor
-.. c:member:: binaryfunc PyNumberMethods.nb_or
-.. c:member:: unaryfunc PyNumberMethods.nb_int
-.. c:member:: void *PyNumberMethods.nb_reserved
-.. c:member:: unaryfunc PyNumberMethods.nb_float
-.. c:member:: binaryfunc PyNumberMethods.nb_inplace_add
-.. c:member:: binaryfunc PyNumberMethods.nb_inplace_subtract
-.. c:member:: binaryfunc PyNumberMethods.nb_inplace_multiply
-.. c:member:: binaryfunc PyNumberMethods.nb_inplace_remainder
-.. c:member:: ternaryfunc PyNumberMethods.nb_inplace_power
-.. c:member:: binaryfunc PyNumberMethods.nb_inplace_lshift
-.. c:member:: binaryfunc PyNumberMethods.nb_inplace_rshift
-.. c:member:: binaryfunc PyNumberMethods.nb_inplace_and
-.. c:member:: binaryfunc PyNumberMethods.nb_inplace_xor
-.. c:member:: binaryfunc PyNumberMethods.nb_inplace_or
-.. c:member:: binaryfunc PyNumberMethods.nb_floor_divide
-.. c:member:: binaryfunc PyNumberMethods.nb_true_divide
-.. c:member:: binaryfunc PyNumberMethods.nb_inplace_floor_divide
-.. c:member:: binaryfunc PyNumberMethods.nb_inplace_true_divide
-.. c:member:: unaryfunc PyNumberMethods.nb_index
-.. c:member:: binaryfunc PyNumberMethods.nb_matrix_multiply
-.. c:member:: binaryfunc PyNumberMethods.nb_inplace_matrix_multiply
+- If the :const:`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.
.. _mapping-structs:
@@ -2100,25 +1242,22 @@ Mapping Object Structures
.. c:member:: lenfunc PyMappingMethods.mp_length
- This function is used by :c:func:`PyMapping_Size` and
+ This function is used by :c:func:`PyMapping_Length` and
:c:func:`PyObject_Size`, and has the same signature. This slot may be set to
- ``NULL`` if the object has no defined length.
+ *NULL* if the object has no defined length.
.. c:member:: binaryfunc PyMappingMethods.mp_subscript
- This function is used by :c:func:`PyObject_GetItem` and
- :c:func:`PySequence_GetSlice`, and has the same signature as
- :c:func:`!PyObject_GetItem`. This slot must be filled for the
- :c:func:`PyMapping_Check` function to return ``1``, it can be ``NULL``
- otherwise.
+ This function is used by :c:func:`PyObject_GetItem` and has the same
+ signature. This slot must be filled for the :c:func:`PyMapping_Check`
+ function to return ``1``, it can be *NULL* otherwise.
.. c:member:: objobjargproc PyMappingMethods.mp_ass_subscript
- This function is used by :c:func:`PyObject_SetItem`,
- :c:func:`PyObject_DelItem`, :c:func:`PyObject_SetSlice` and
- :c:func:`PyObject_DelSlice`. It has the same signature as
- :c:func:`!PyObject_SetItem`, but *v* can also be set to ``NULL`` to delete
- an item. If this slot is ``NULL``, the object does not support item
+ This function is used by :c:func:`PyObject_SetItem` and
+ :c:func:`PyObject_DelItem`. It has the same signature as
+ :c:func:`PyObject_SetItem`, but *v* can also be set to *NULL* to delete
+ an item. If this slot is *NULL*, the object does not support item
assignment and deletion.
@@ -2137,69 +1276,58 @@ Sequence Object Structures
.. c:member:: lenfunc PySequenceMethods.sq_length
- This function is used by :c:func:`PySequence_Size` and
- :c:func:`PyObject_Size`, and has the same signature. It is also used for
- handling negative indices via the :c:member:`~PySequenceMethods.sq_item`
- and the :c:member:`~PySequenceMethods.sq_ass_item` slots.
+ This function is used by :c:func:`PySequence_Size` and :c:func:`PyObject_Size`,
+ and has the same signature.
.. c:member:: binaryfunc PySequenceMethods.sq_concat
This function is used by :c:func:`PySequence_Concat` and has the same
signature. It is also used by the ``+`` operator, after trying the numeric
- addition via the :c:member:`~PyNumberMethods.nb_add` slot.
+ addition via the :c:member:`~PyTypeObject.tp_as_number.nb_add` slot.
.. c:member:: ssizeargfunc PySequenceMethods.sq_repeat
This function is used by :c:func:`PySequence_Repeat` and has the same
signature. It is also used by the ``*`` operator, after trying numeric
- multiplication via the :c:member:`~PyNumberMethods.nb_multiply` slot.
+ multiplication via the :c:member:`~PyTypeObject.tp_as_number.nb_multiply`
+ slot.
.. c:member:: ssizeargfunc PySequenceMethods.sq_item
This function is used by :c:func:`PySequence_GetItem` and has the same
- signature. It is also used by :c:func:`PyObject_GetItem`, after trying
- the subscription via the :c:member:`~PyMappingMethods.mp_subscript` slot.
- This slot must be filled for the :c:func:`PySequence_Check`
- function to return ``1``, it can be ``NULL`` otherwise.
+ signature. This slot must be filled for the :c:func:`PySequence_Check`
+ function to return ``1``, it can be *NULL* otherwise.
Negative indexes are handled as follows: if the :attr:`sq_length` slot is
filled, it is called and the sequence length is used to compute a positive
- index which is passed to :attr:`sq_item`. If :attr:`sq_length` is ``NULL``,
+ index which is passed to :attr:`sq_item`. If :attr:`sq_length` is *NULL*,
the index is passed as is to the function.
.. c:member:: ssizeobjargproc PySequenceMethods.sq_ass_item
This function is used by :c:func:`PySequence_SetItem` and has the same
- signature. It is also used by :c:func:`PyObject_SetItem` and
- :c:func:`PyObject_DelItem`, after trying the item assignment and deletion
- via the :c:member:`~PyMappingMethods.mp_ass_subscript` slot.
- This slot may be left to ``NULL`` if the object does not support
+ signature. This slot may be left to *NULL* if the object does not support
item assignment and deletion.
.. c:member:: objobjproc PySequenceMethods.sq_contains
This function may be used by :c:func:`PySequence_Contains` and has the same
- signature. This slot may be left to ``NULL``, in this case
- :c:func:`!PySequence_Contains` simply traverses the sequence until it
- finds a match.
+ signature. This slot may be left to *NULL*, in this case
+ :c:func:`PySequence_Contains` simply traverses the sequence until it finds a
+ match.
.. c:member:: binaryfunc PySequenceMethods.sq_inplace_concat
This function is used by :c:func:`PySequence_InPlaceConcat` and has the same
- signature. It should modify its first operand, and return it. This slot
- may be left to ``NULL``, in this case :c:func:`!PySequence_InPlaceConcat`
- will fall back to :c:func:`PySequence_Concat`. It is also used by the
- augmented assignment ``+=``, after trying numeric in-place addition
- via the :c:member:`~PyNumberMethods.nb_inplace_add` slot.
+ signature. It should modify its first operand, and return it.
.. c:member:: ssizeargfunc PySequenceMethods.sq_inplace_repeat
This function is used by :c:func:`PySequence_InPlaceRepeat` and has the same
- signature. It should modify its first operand, and return it. This slot
- may be left to ``NULL``, in this case :c:func:`!PySequence_InPlaceRepeat`
- will fall back to :c:func:`PySequence_Repeat`. It is also used by the
- augmented assignment ``*=``, after trying numeric in-place multiplication
- via the :c:member:`~PyNumberMethods.nb_inplace_multiply` slot.
+ signature. It should modify its first operand, and return it.
+
+.. XXX need to explain precedence between mapping and sequence
+.. XXX explains when to implement the sq_inplace_* slots
.. _buffer-structs:
@@ -2208,392 +1336,108 @@ Buffer Object Structures
========================
.. sectionauthor:: Greg J. Stein <greg@lyra.org>
-.. sectionauthor:: Benjamin Peterson
-.. sectionauthor:: Stefan Krah
-
-.. c:type:: PyBufferProcs
-
- This structure holds pointers to the functions required by the
- :ref:`Buffer protocol <bufferobjects>`. The protocol defines how
- an exporter object can expose its internal data to consumer objects.
-
-.. c:member:: getbufferproc PyBufferProcs.bf_getbuffer
-
- The signature of this function is::
-
- int (PyObject *exporter, Py_buffer *view, int flags);
-
- Handle a request to *exporter* to fill in *view* as specified by *flags*.
- Except for point (3), an implementation of this function MUST take these
- steps:
-
- (1) Check if the request can be met. If not, raise :c:data:`PyExc_BufferError`,
- set :c:data:`view->obj` to ``NULL`` and return ``-1``.
-
- (2) Fill in the requested fields.
-
- (3) Increment an internal counter for the number of exports.
-
- (4) Set :c:data:`view->obj` to *exporter* and increment :c:data:`view->obj`.
-
- (5) Return ``0``.
-
- If *exporter* is part of a chain or tree of buffer providers, two main
- schemes can be used:
-
- * Re-export: Each member of the tree acts as the exporting object and
- sets :c:data:`view->obj` to a new reference to itself.
-
- * Redirect: The buffer request is redirected to the root object of the
- tree. Here, :c:data:`view->obj` will be a new reference to the root
- object.
-
- The individual fields of *view* are described in section
- :ref:`Buffer structure <buffer-structure>`, the rules how an exporter
- must react to specific requests are in section
- :ref:`Buffer request types <buffer-request-types>`.
-
- All memory pointed to in the :c:type:`Py_buffer` structure belongs to
- the exporter and must remain valid until there are no consumers left.
- :c:member:`~Py_buffer.format`, :c:member:`~Py_buffer.shape`,
- :c:member:`~Py_buffer.strides`, :c:member:`~Py_buffer.suboffsets`
- and :c:member:`~Py_buffer.internal`
- are read-only for the consumer.
-
- :c:func:`PyBuffer_FillInfo` provides an easy way of exposing a simple
- bytes buffer while dealing correctly with all request types.
-
- :c:func:`PyObject_GetBuffer` is the interface for the consumer that
- wraps this function.
-
-.. c:member:: releasebufferproc PyBufferProcs.bf_releasebuffer
-
- The signature of this function is::
-
- void (PyObject *exporter, Py_buffer *view);
-
- Handle a request to release the resources of the buffer. If no resources
- need to be released, :c:member:`PyBufferProcs.bf_releasebuffer` may be
- ``NULL``. Otherwise, a standard implementation of this function will take
- these optional steps:
-
- (1) Decrement an internal counter for the number of exports.
-
- (2) If the counter is ``0``, free all memory associated with *view*.
-
- The exporter MUST use the :c:member:`~Py_buffer.internal` field to keep
- track of buffer-specific resources. This field is guaranteed to remain
- constant, while a consumer MAY pass a copy of the original buffer as the
- *view* argument.
-
-
- This function MUST NOT decrement :c:data:`view->obj`, since that is
- done automatically in :c:func:`PyBuffer_Release` (this scheme is
- useful for breaking reference cycles).
-
-
- :c:func:`PyBuffer_Release` is the interface for the consumer that
- wraps this function.
-
-
-.. _async-structs:
-
-
-Async Object Structures
-=======================
-
-.. sectionauthor:: Yury Selivanov <yselivanov@sprymix.com>
-
-.. versionadded:: 3.5
-
-.. c:type:: PyAsyncMethods
-
- This structure holds pointers to the functions required to implement
- :term:`awaitable` and :term:`asynchronous iterator` objects.
-
- Here is the structure definition::
-
- typedef struct {
- unaryfunc am_await;
- unaryfunc am_aiter;
- unaryfunc am_anext;
- } PyAsyncMethods;
-
-.. c:member:: unaryfunc PyAsyncMethods.am_await
-
- The signature of this function is::
-
- PyObject *am_await(PyObject *self);
-
- The returned object must be an iterator, i.e. :c:func:`PyIter_Check` must
- return ``1`` for it.
-
- This slot may be set to ``NULL`` if an object is not an :term:`awaitable`.
-
-.. c:member:: unaryfunc PyAsyncMethods.am_aiter
-
- The signature of this function is::
- PyObject *am_aiter(PyObject *self);
- Must return an :term:`awaitable` object. See :meth:`__anext__` for details.
+The buffer interface exports a model where an object can expose its internal
+data as a set of chunks of data, where each chunk is specified as a
+pointer/length pair. These chunks are called :dfn:`segments` and are presumed
+to be non-contiguous in memory.
- This slot may be set to ``NULL`` if an object does not implement
- asynchronous iteration protocol.
+If an object does not export the buffer interface, then its :c:member:`~PyTypeObject.tp_as_buffer`
+member in the :c:type:`PyTypeObject` structure should be *NULL*. Otherwise, the
+:c:member:`~PyTypeObject.tp_as_buffer` will point to a :c:type:`PyBufferProcs` structure.
-.. c:member:: unaryfunc PyAsyncMethods.am_anext
+.. note::
- The signature of this function is::
+ It is very important that your :c:type:`PyTypeObject` structure uses
+ :const:`Py_TPFLAGS_DEFAULT` for the value of the :c:member:`~PyTypeObject.tp_flags` member rather
+ than ``0``. This tells the Python runtime that your :c:type:`PyBufferProcs`
+ structure contains the :attr:`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.
- PyObject *am_anext(PyObject *self);
- Must return an :term:`awaitable` object. See :meth:`__anext__` for details.
- This slot may be set to ``NULL``.
-
-
-.. _slot-typedefs:
-
-Slot Type typedefs
-==================
-
-.. c:type:: PyObject *(*allocfunc)(PyTypeObject *cls, Py_ssize_t nitems)
-
- 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
- :attr:`ob_refcnt` set to ``1`` and :attr:`ob_type` set to the type argument. If
- the type's :c:member:`~PyTypeObject.tp_itemsize` is non-zero, the object's :attr:`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 :c:member:`~PyTypeObject.tp_basicsize`.
-
- This function should not do any other instance initialization, not even to
- allocate additional memory; that should be done by :c:member:`~PyTypeObject.tp_new`.
-
-.. c:type:: void (*destructor)(PyObject *)
-
-.. c:type:: void (*freefunc)(void *)
-
- See :c:member:`~PyTypeObject.tp_free`.
-
-.. c:type:: PyObject *(*newfunc)(PyObject *, PyObject *, PyObject *)
-
- See :c:member:`~PyTypeObject.tp_new`.
-
-.. c:type:: int (*initproc)(PyObject *, PyObject *, PyObject *)
-
- See :c:member:`~PyTypeObject.tp_init`.
-
-.. c:type:: PyObject *(*reprfunc)(PyObject *)
-
- See :c:member:`~PyTypeObject.tp_repr`.
-
-.. c:type:: PyObject *(*getattrfunc)(PyObject *self, char *attr)
-
- Return the value of the named attribute for the object.
-
-.. c:type:: int (*setattrfunc)(PyObject *self, char *attr, PyObject *value)
-
- Set the value of the named attribute for the object.
- The value argument is set to ``NULL`` to delete the attribute.
-
-.. c:type:: PyObject *(*getattrofunc)(PyObject *self, PyObject *attr)
-
- Return the value of the named attribute for the object.
-
- See :c:member:`~PyTypeObject.tp_getattro`.
-
-.. c:type:: int (*setattrofunc)(PyObject *self, PyObject *attr, PyObject *value)
-
- Set the value of the named attribute for the object.
- The value argument is set to ``NULL`` to delete the attribute.
-
- See :c:member:`~PyTypeObject.tp_setattro`.
-
-.. c:type:: PyObject *(*descrgetfunc)(PyObject *, PyObject *, PyObject *)
-
- See :c:member:`~PyTypeObject.tp_descrget`.
-
-.. c:type:: int (*descrsetfunc)(PyObject *, PyObject *, PyObject *)
-
- See :c:member:`~PyTypeObject.tp_descrset`.
-
-.. c:type:: Py_hash_t (*hashfunc)(PyObject *)
-
- See :c:member:`~PyTypeObject.tp_hash`.
-
-.. c:type:: PyObject *(*richcmpfunc)(PyObject *, PyObject *, int)
-
- See :c:member:`~PyTypeObject.tp_richcompare`.
-
-.. c:type:: PyObject *(*getiterfunc)(PyObject *)
-
- See :c:member:`~PyTypeObject.tp_iter`.
-
-.. c:type:: PyObject *(*iternextfunc)(PyObject *)
-
- See :c:member:`~PyTypeObject.tp_iternext`.
+.. c:type:: PyBufferProcs
-.. c:type:: Py_ssize_t (*lenfunc)(PyObject *)
+ Structure used to hold the function pointers which define an implementation of
+ the buffer protocol.
+
+ The first slot is :attr:`bf_getreadbuffer`, of type :c: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 :attr:`bf_getwritebuffer` having type
+ :c:type:`writebufferproc`. This slot may be *NULL* if the object does not
+ allow writing into its returned buffers.
+
+ The third slot is :attr:`bf_getsegcount`, with type :c: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 :c:type:`PyString_Type` and
+ :c:type:`PyBuffer_Type` objects contain a single segment.
+
+ .. index:: single: PyType_HasFeature()
+
+ The last slot is :attr:`bf_getcharbuffer`, of type :c:type:`charbufferproc`.
+ This slot will only be present if the :const:`Py_TPFLAGS_HAVE_GETCHARBUFFER`
+ flag is present in the :c:member:`~PyTypeObject.tp_flags` field of the object's
+ :c:type:`PyTypeObject`. Before using this slot, the caller should test whether it
+ is present by using the :c:func:`PyType_HasFeature` function. If the flag is
+ present, :attr:`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
+ :attr:`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.
-.. c:type:: int (*getbufferproc)(PyObject *, Py_buffer *, int)
+ .. note::
-.. c:type:: void (*releasebufferproc)(PyObject *, Py_buffer *)
+ 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.
-.. c:type:: PyObject *(*unaryfunc)(PyObject *)
-.. c:type:: PyObject *(*binaryfunc)(PyObject *, PyObject *)
+.. data:: Py_TPFLAGS_HAVE_GETCHARBUFFER
-.. c:type:: PyObject *(*ternaryfunc)(PyObject *, PyObject *, PyObject *)
+ Flag bit set in the type structure to indicate that the :attr:`bf_getcharbuffer`
+ slot is known. This being set does not indicate that the object supports the
+ buffer interface or that the :attr:`bf_getcharbuffer` slot is non-*NULL*.
-.. c:type:: PyObject *(*ssizeargfunc)(PyObject *, Py_ssize_t)
-.. c:type:: int (*ssizeobjargproc)(PyObject *, Py_ssize_t)
+.. c:type:: Py_ssize_t (*readbufferproc) (PyObject *self, Py_ssize_t segment, void **ptrptr)
-.. c:type:: int (*objobjproc)(PyObject *, PyObject *)
+ 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 :attr:`bf_getsegcount` slot
+ function. On success, it returns the length of the segment, and sets
+ ``*ptrptr`` to a pointer to that memory.
-.. c:type:: int (*objobjargproc)(PyObject *, PyObject *, PyObject *)
+.. c:type:: Py_ssize_t (*writebufferproc) (PyObject *self, Py_ssize_t segment, void **ptrptr)
-.. _typedef-examples:
+ 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.
+ :exc:`TypeError` should be raised if the object only supports read-only buffers,
+ and :exc:`SystemError` should be raised when *segment* specifies a segment that
+ doesn't exist.
-Examples
-========
+ .. Why doesn't it raise ValueError for this one?
+ GJS: because you shouldn't be calling it with an invalid
+ segment. That indicates a blatant programming error in the C code.
-The following are simple examples of Python type definitions. They
-include common usage you may encounter. Some demonstrate tricky corner
-cases. For more examples, practical info, and a tutorial, see
-:ref:`defining-new-types` and :ref:`new-types-topics`.
-A basic static type::
+.. c:type:: Py_ssize_t (*segcountproc) (PyObject *self, Py_ssize_t *lenp)
- typedef struct {
- PyObject_HEAD
- const char *data;
- } MyObject;
+ Return the number of memory segments which comprise the buffer. If *lenp* is
+ not *NULL*, the implementation must report the sum of the sizes (in bytes) of
+ all segments in ``*lenp``. The function cannot fail.
- static PyTypeObject MyObject_Type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- .tp_name = "mymod.MyObject",
- .tp_basicsize = sizeof(MyObject),
- .tp_doc = "My objects",
- .tp_new = myobj_new,
- .tp_dealloc = (destructor)myobj_dealloc,
- .tp_repr = (reprfunc)myobj_repr,
- };
-You may also find older code (especially in the CPython code base)
-with a more verbose initializer::
+.. c:type:: Py_ssize_t (*charbufferproc) (PyObject *self, Py_ssize_t segment, char **ptrptr)
- static PyTypeObject MyObject_Type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "mymod.MyObject", /* tp_name */
- sizeof(MyObject), /* tp_basicsize */
- 0, /* tp_itemsize */
- (destructor)myobj_dealloc, /* tp_dealloc */
- 0, /* tp_vectorcall_offset */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_as_async */
- (reprfunc)myobj_repr, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- 0, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- 0, /* tp_flags */
- "My objects", /* tp_doc */
- 0, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- 0, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- myobj_new, /* tp_new */
- };
-
-A type that supports weakrefs, instance dicts, and hashing::
-
- typedef struct {
- PyObject_HEAD
- const char *data;
- PyObject *inst_dict;
- PyObject *weakreflist;
- } MyObject;
-
- static PyTypeObject MyObject_Type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- .tp_name = "mymod.MyObject",
- .tp_basicsize = sizeof(MyObject),
- .tp_doc = "My objects",
- .tp_weaklistoffset = offsetof(MyObject, weakreflist),
- .tp_dictoffset = offsetof(MyObject, inst_dict),
- .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
- .tp_new = myobj_new,
- .tp_traverse = (traverseproc)myobj_traverse,
- .tp_clear = (inquiry)myobj_clear,
- .tp_alloc = PyType_GenericNew,
- .tp_dealloc = (destructor)myobj_dealloc,
- .tp_repr = (reprfunc)myobj_repr,
- .tp_hash = (hashfunc)myobj_hash,
- .tp_richcompare = PyBaseObject_Type.tp_richcompare,
- };
-
-A str subclass that cannot be subclassed and cannot be called
-to create instances (e.g. uses a separate factory func)::
-
- typedef struct {
- PyUnicodeObject raw;
- char *extra;
- } MyStr;
-
- static PyTypeObject MyStr_Type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- .tp_name = "mymod.MyStr",
- .tp_basicsize = sizeof(MyStr),
- .tp_base = NULL, // set to &PyUnicode_Type in module init
- .tp_doc = "my custom str",
- .tp_flags = Py_TPFLAGS_DEFAULT,
- .tp_new = NULL,
- .tp_repr = (reprfunc)myobj_repr,
- };
-
-The simplest static type (with fixed-length instances)::
-
- typedef struct {
- PyObject_HEAD
- } MyObject;
-
- static PyTypeObject MyObject_Type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- .tp_name = "mymod.MyObject",
- };
-
-The simplest static type (with variable-length instances)::
-
- typedef struct {
- PyObject_VAR_HEAD
- const char *data[1];
- } MyObject;
-
- static PyTypeObject MyObject_Type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- .tp_name = "mymod.MyObject",
- .tp_basicsize = sizeof(MyObject) - sizeof(char *),
- .tp_itemsize = sizeof(char *),
- };
+ Return the size of the segment *segment* that *ptrptr* is set to. ``*ptrptr``
+ is set to the memory buffer. Returns ``-1`` on error.
diff --git a/Doc/c-api/unicode.rst b/Doc/c-api/unicode.rst
index 77f123c..4d1eaa1 100644
--- a/Doc/c-api/unicode.rst
+++ b/Doc/c-api/unicode.rst
@@ -1,39 +1,15 @@
-.. highlight:: c
+.. highlightlang:: c
.. _unicodeobjects:
Unicode Objects and Codecs
--------------------------
-.. sectionauthor:: Marc-André Lemburg <mal@lemburg.com>
-.. sectionauthor:: Georg Brandl <georg@python.org>
+.. sectionauthor:: Marc-Andre Lemburg <mal@lemburg.com>
Unicode Objects
^^^^^^^^^^^^^^^
-Since the implementation of :pep:`393` in Python 3.3, Unicode objects internally
-use a variety of representations, in order to allow handling the complete range
-of Unicode characters while staying memory efficient. There are special cases
-for strings where all code points are below 128, 256, or 65536; otherwise, code
-points must be below 1114112 (which is the full Unicode range).
-
-:c:type:`Py_UNICODE*` and UTF-8 representations are created on demand and cached
-in the Unicode object. The :c:type:`Py_UNICODE*` representation is deprecated
-and inefficient; it should be avoided in performance- or memory-sensitive
-situations.
-
-Due to the transition between the old APIs and the new APIs, Unicode objects
-can internally be in two states depending on how they were created:
-
-* "canonical" Unicode objects are all objects created by a non-deprecated
- Unicode API. They use the most efficient representation allowed by the
- implementation.
-
-* "legacy" Unicode objects have been created through one of the deprecated
- APIs (typically :c:func:`PyUnicode_FromUnicode`) and only bear the
- :c:type:`Py_UNICODE*` representation; you will have to call
- :c:func:`PyUnicode_READY` on them before calling any other API.
-
Unicode Type
""""""""""""
@@ -41,203 +17,93 @@ Unicode Type
These are the basic Unicode object types used for the Unicode implementation in
Python:
-.. c:type:: Py_UCS4
- Py_UCS2
- Py_UCS1
-
- These types are typedefs for unsigned integer types wide enough to contain
- characters of 32 bits, 16 bits and 8 bits, respectively. When dealing with
- single Unicode characters, use :c:type:`Py_UCS4`.
-
- .. versionadded:: 3.3
-
.. c:type:: Py_UNICODE
- This is a typedef of :c:type:`wchar_t`, which is a 16-bit type or 32-bit type
- depending on the platform.
-
- .. versionchanged:: 3.3
- In previous versions, this was a 16-bit type or a 32-bit type depending on
- whether you selected a "narrow" or "wide" Unicode version of Python at
- build time.
+ 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 :c:type:`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
+ :c:type:`Py_UNICODE` and store Unicode data internally as UCS4. On platforms
+ where :c:type:`wchar_t` is available and compatible with the chosen Python
+ Unicode build variant, :c:type:`Py_UNICODE` is a typedef alias for
+ :c:type:`wchar_t` to enhance native platform compatibility. On all other
+ platforms, :c:type:`Py_UNICODE` is a typedef alias for either :c:type:`unsigned
+ short` (UCS2) or :c:type:`unsigned long` (UCS4).
+Note that UCS2 and UCS4 Python builds are not binary compatible. Please keep
+this in mind when writing extensions or interfaces.
-.. c:type:: PyASCIIObject
- PyCompactUnicodeObject
- PyUnicodeObject
- These subtypes of :c:type:`PyObject` represent a Python Unicode object. In
- almost all cases, they shouldn't be used directly, since all API functions
- that deal with Unicode objects take and return :c:type:`PyObject` pointers.
+.. c:type:: PyUnicodeObject
- .. versionadded:: 3.3
+ This subtype of :c:type:`PyObject` represents a Python Unicode object.
.. c:var:: PyTypeObject PyUnicode_Type
This instance of :c:type:`PyTypeObject` represents the Python Unicode type. It
- is exposed to Python code as ``str``.
-
+ 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:
+
.. c:function:: int PyUnicode_Check(PyObject *o)
Return true if the object *o* is a Unicode object or an instance of a Unicode
subtype.
+ .. versionchanged:: 2.2
+ Allowed subtypes to be accepted.
+
.. c:function:: int PyUnicode_CheckExact(PyObject *o)
Return true if the object *o* is a Unicode object, but not an instance of a
subtype.
+ .. versionadded:: 2.2
-.. c:function:: int PyUnicode_READY(PyObject *o)
-
- Ensure the string object *o* is in the "canonical" representation. This is
- required before using any of the access macros described below.
-
- .. XXX expand on when it is not required
-
- Returns ``0`` on success and ``-1`` with an exception set on failure, which in
- particular happens if memory allocation fails.
-
- .. versionadded:: 3.3
-
-
-.. c:function:: Py_ssize_t PyUnicode_GET_LENGTH(PyObject *o)
-
- Return the length of the Unicode string, in code points. *o* has to be a
- Unicode object in the "canonical" representation (not checked).
-
- .. versionadded:: 3.3
-
-
-.. c:function:: Py_UCS1* PyUnicode_1BYTE_DATA(PyObject *o)
- Py_UCS2* PyUnicode_2BYTE_DATA(PyObject *o)
- Py_UCS4* PyUnicode_4BYTE_DATA(PyObject *o)
-
- Return a pointer to the canonical representation cast to UCS1, UCS2 or UCS4
- integer types for direct character access. No checks are performed if the
- canonical representation has the correct character size; use
- :c:func:`PyUnicode_KIND` to select the right macro. Make sure
- :c:func:`PyUnicode_READY` has been called before accessing this.
-
- .. versionadded:: 3.3
-
-
-.. c:macro:: PyUnicode_WCHAR_KIND
- PyUnicode_1BYTE_KIND
- PyUnicode_2BYTE_KIND
- PyUnicode_4BYTE_KIND
-
- Return values of the :c:func:`PyUnicode_KIND` macro.
-
- .. versionadded:: 3.3
-
-
-.. c:function:: int PyUnicode_KIND(PyObject *o)
-
- Return one of the PyUnicode kind constants (see above) that indicate how many
- bytes per character this Unicode object uses to store its data. *o* has to
- be a Unicode object in the "canonical" representation (not checked).
-
- .. XXX document "0" return value?
-
- .. versionadded:: 3.3
-
-
-.. c:function:: void* PyUnicode_DATA(PyObject *o)
-
- Return a void pointer to the raw Unicode buffer. *o* has to be a Unicode
- object in the "canonical" representation (not checked).
-
- .. versionadded:: 3.3
-
-
-.. c:function:: void PyUnicode_WRITE(int kind, void *data, Py_ssize_t index, \
- Py_UCS4 value)
-
- Write into a canonical representation *data* (as obtained with
- :c:func:`PyUnicode_DATA`). This macro does not do any sanity checks and is
- intended for usage in loops. The caller should cache the *kind* value and
- *data* pointer as obtained from other macro calls. *index* is the index in
- the string (starts at 0) and *value* is the new code point value which should
- be written to that location.
-
- .. versionadded:: 3.3
-
-
-.. c:function:: Py_UCS4 PyUnicode_READ(int kind, void *data, Py_ssize_t index)
-
- Read a code point from a canonical representation *data* (as obtained with
- :c:func:`PyUnicode_DATA`). No checks or ready calls are performed.
-
- .. versionadded:: 3.3
-
-
-.. c:function:: Py_UCS4 PyUnicode_READ_CHAR(PyObject *o, Py_ssize_t index)
-
- Read a character from a Unicode object *o*, which must be in the "canonical"
- representation. This is less efficient than :c:func:`PyUnicode_READ` if you
- do multiple consecutive reads.
-
- .. versionadded:: 3.3
+.. c:function:: Py_ssize_t PyUnicode_GET_SIZE(PyObject *o)
-.. c:function:: PyUnicode_MAX_CHAR_VALUE(PyObject *o)
+ Return the size of the object. *o* has to be a :c:type:`PyUnicodeObject` (not
+ checked).
- Return the maximum code point that is suitable for creating another string
- based on *o*, which must be in the "canonical" representation. This is
- always an approximation but more efficient than iterating over the string.
+ .. versionchanged:: 2.5
+ This function returned an :c:type:`int` type. This might require changes
+ in your code for properly supporting 64-bit systems.
- .. versionadded:: 3.3
+.. c:function:: Py_ssize_t PyUnicode_GET_DATA_SIZE(PyObject *o)
-.. c:function:: Py_ssize_t PyUnicode_GET_SIZE(PyObject *o)
+ Return the size of the object's internal buffer in bytes. *o* has to be a
+ :c:type:`PyUnicodeObject` (not checked).
- Return the size of the deprecated :c:type:`Py_UNICODE` representation, in
- code units (this includes surrogate pairs as 2 units). *o* has to be a
- Unicode object (not checked).
+ .. versionchanged:: 2.5
+ This function returned an :c:type:`int` type. This might require changes
+ in your code for properly supporting 64-bit systems.
- .. deprecated-removed:: 3.3 4.0
- Part of the old-style Unicode API, please migrate to using
- :c:func:`PyUnicode_GET_LENGTH`.
+.. c:function:: Py_UNICODE* PyUnicode_AS_UNICODE(PyObject *o)
-.. c:function:: Py_ssize_t PyUnicode_GET_DATA_SIZE(PyObject *o)
+ Return a pointer to the internal :c:type:`Py_UNICODE` buffer of the object. *o*
+ has to be a :c:type:`PyUnicodeObject` (not checked).
- Return the size of the deprecated :c:type:`Py_UNICODE` representation in
- bytes. *o* has to be a Unicode object (not checked).
- .. deprecated-removed:: 3.3 4.0
- Part of the old-style Unicode API, please migrate to using
- :c:func:`PyUnicode_GET_LENGTH`.
+.. c:function:: const char* PyUnicode_AS_DATA(PyObject *o)
+ Return a pointer to the internal buffer of the object. *o* has to be a
+ :c:type:`PyUnicodeObject` (not checked).
-.. c:function:: Py_UNICODE* PyUnicode_AS_UNICODE(PyObject *o)
- const char* PyUnicode_AS_DATA(PyObject *o)
- Return a pointer to a :c:type:`Py_UNICODE` representation of the object. The
- returned buffer is always terminated with an extra null code point. It
- may also contain embedded null code points, which would cause the string
- to be truncated when used in most C functions. The ``AS_DATA`` form
- casts the pointer to :c:type:`const char *`. The *o* argument has to be
- a Unicode object (not checked).
+.. c:function:: int PyUnicode_ClearFreeList()
- .. versionchanged:: 3.3
- This macro is now inefficient -- because in many cases the
- :c:type:`Py_UNICODE` representation does not exist and needs to be created
- -- and can fail (return ``NULL`` with an exception set). Try to port the
- code to use the new :c:func:`PyUnicode_nBYTE_DATA` macros or use
- :c:func:`PyUnicode_WRITE` or :c:func:`PyUnicode_READ`.
+ Clear the free list. Return the total number of freed items.
- .. deprecated-removed:: 3.3 4.0
- Part of the old-style Unicode API, please migrate to using the
- :c:func:`PyUnicode_nBYTE_DATA` family of macros.
+ .. versionadded:: 2.6
Unicode Character Properties
@@ -297,18 +163,6 @@ the Python configuration.
Return ``1`` or ``0`` depending on whether *ch* is an alphanumeric character.
-
-.. c:function:: int Py_UNICODE_ISPRINTABLE(Py_UNICODE ch)
-
- Return ``1`` or ``0`` depending on whether *ch* is a printable character.
- Nonprintable characters are those characters defined in the Unicode character
- database as "Other" or "Separator", excepting the ASCII space (0x20) which is
- considered printable. (Note that printable characters in this context are
- those which should not be escaped when :func:`repr` is invoked on a string.
- It has no bearing on the handling of strings written to :data:`sys.stdout` or
- :data:`sys.stderr`.)
-
-
These APIs can be used for fast direct character conversions:
@@ -316,25 +170,16 @@ These APIs can be used for fast direct character conversions:
Return the character *ch* converted to lower case.
- .. deprecated:: 3.3
- This function uses simple case mappings.
-
.. c:function:: Py_UNICODE Py_UNICODE_TOUPPER(Py_UNICODE ch)
Return the character *ch* converted to upper case.
- .. deprecated:: 3.3
- This function uses simple case mappings.
-
.. c:function:: Py_UNICODE Py_UNICODE_TOTITLE(Py_UNICODE ch)
Return the character *ch* converted to title case.
- .. deprecated:: 3.3
- This function uses simple case mappings.
-
.. c:function:: int Py_UNICODE_TODECIMAL(Py_UNICODE ch)
@@ -354,66 +199,37 @@ These APIs can be used for fast direct character conversions:
possible. This macro does not raise exceptions.
-These APIs can be used to work with surrogates:
-
-.. c:macro:: Py_UNICODE_IS_SURROGATE(ch)
-
- Check if *ch* is a surrogate (``0xD800 <= ch <= 0xDFFF``).
-
-.. c:macro:: Py_UNICODE_IS_HIGH_SURROGATE(ch)
-
- Check if *ch* is a high surrogate (``0xD800 <= ch <= 0xDBFF``).
-
-.. c:macro:: Py_UNICODE_IS_LOW_SURROGATE(ch)
-
- Check if *ch* is a low surrogate (``0xDC00 <= ch <= 0xDFFF``).
-
-.. c:macro:: Py_UNICODE_JOIN_SURROGATES(high, low)
-
- Join two surrogate characters and return a single Py_UCS4 value.
- *high* and *low* are respectively the leading and trailing surrogates in a
- surrogate pair.
-
-
-Creating and accessing Unicode strings
-""""""""""""""""""""""""""""""""""""""
+Plain Py_UNICODE
+""""""""""""""""
To create Unicode objects and access their basic sequence properties, use these
APIs:
-.. c:function:: PyObject* PyUnicode_New(Py_ssize_t size, Py_UCS4 maxchar)
- Create a new Unicode object. *maxchar* should be the true maximum code point
- to be placed in the string. As an approximation, it can be rounded up to the
- nearest value in the sequence 127, 255, 65535, 1114111.
-
- This is the recommended way to allocate a new Unicode object. Objects
- created using this function are not resizable.
-
- .. versionadded:: 3.3
-
-
-.. c:function:: PyObject* PyUnicode_FromKindAndData(int kind, const void *buffer, \
- Py_ssize_t size)
+.. c:function:: PyObject* PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size)
- Create a new Unicode object with the given *kind* (possible values are
- :c:macro:`PyUnicode_1BYTE_KIND` etc., as returned by
- :c:func:`PyUnicode_KIND`). The *buffer* must point to an array of *size*
- units of 1, 2 or 4 bytes per character, as given by the kind.
+ 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*.
- .. versionadded:: 3.3
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` type for *size*. This might require
+ changes in your code for properly supporting 64-bit systems.
.. c:function:: PyObject* PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size)
- Create a Unicode object from the char buffer *u*. The bytes will be
- interpreted as being UTF-8 encoded. The buffer is copied into the new
- object. If the buffer is not ``NULL``, the return value might be a shared
- object, i.e. modification of the data is not allowed.
+ 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*.
- If *u* is ``NULL``, this function behaves like :c:func:`PyUnicode_FromUnicode`
- with the buffer set to ``NULL``. This usage is deprecated in favor of
- :c:func:`PyUnicode_New`.
+ .. versionadded:: 2.6
.. c:function:: PyObject *PyUnicode_FromString(const char *u)
@@ -421,501 +237,141 @@ APIs:
Create a Unicode object from a UTF-8 encoded null-terminated char buffer
*u*.
+ .. versionadded:: 2.6
+
.. c:function:: PyObject* PyUnicode_FromFormat(const char *format, ...)
Take a C :c:func:`printf`\ -style *format* string and a variable number of
- arguments, calculate the size of the resulting Python Unicode string and return
+ arguments, 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*
- ASCII-encoded string. The following format characters are allowed:
+ string. The following format characters are allowed:
- .. % This should be exactly the same as the table in PyErr_Format.
.. % The descriptions for %zd and %zu are wrong, but the truth is complicated
.. % because not all compilers support the %z width modifier -- we fake it
.. % when necessary via interpolating PY_FORMAT_SIZE_T.
- .. % Similar comments apply to the %ll width modifier and
.. tabularcolumns:: |l|l|L|
- +-------------------+---------------------+----------------------------------+
- | Format Characters | Type | Comment |
- +===================+=====================+==================================+
- | :attr:`%%` | *n/a* | The literal % character. |
- +-------------------+---------------------+----------------------------------+
- | :attr:`%c` | int | A single character, |
- | | | represented as a C int. |
- +-------------------+---------------------+----------------------------------+
- | :attr:`%d` | int | Equivalent to |
- | | | ``printf("%d")``. [1]_ |
- +-------------------+---------------------+----------------------------------+
- | :attr:`%u` | unsigned int | Equivalent to |
- | | | ``printf("%u")``. [1]_ |
- +-------------------+---------------------+----------------------------------+
- | :attr:`%ld` | long | Equivalent to |
- | | | ``printf("%ld")``. [1]_ |
- +-------------------+---------------------+----------------------------------+
- | :attr:`%li` | long | Equivalent to |
- | | | ``printf("%li")``. [1]_ |
- +-------------------+---------------------+----------------------------------+
- | :attr:`%lu` | unsigned long | Equivalent to |
- | | | ``printf("%lu")``. [1]_ |
- +-------------------+---------------------+----------------------------------+
- | :attr:`%lld` | long long | Equivalent to |
- | | | ``printf("%lld")``. [1]_ |
- +-------------------+---------------------+----------------------------------+
- | :attr:`%lli` | long long | Equivalent to |
- | | | ``printf("%lli")``. [1]_ |
- +-------------------+---------------------+----------------------------------+
- | :attr:`%llu` | unsigned long long | Equivalent to |
- | | | ``printf("%llu")``. [1]_ |
- +-------------------+---------------------+----------------------------------+
- | :attr:`%zd` | Py_ssize_t | Equivalent to |
- | | | ``printf("%zd")``. [1]_ |
- +-------------------+---------------------+----------------------------------+
- | :attr:`%zi` | Py_ssize_t | Equivalent to |
- | | | ``printf("%zi")``. [1]_ |
- +-------------------+---------------------+----------------------------------+
- | :attr:`%zu` | size_t | Equivalent to |
- | | | ``printf("%zu")``. [1]_ |
- +-------------------+---------------------+----------------------------------+
- | :attr:`%i` | int | Equivalent to |
- | | | ``printf("%i")``. [1]_ |
- +-------------------+---------------------+----------------------------------+
- | :attr:`%x` | int | Equivalent to |
- | | | ``printf("%x")``. [1]_ |
- +-------------------+---------------------+----------------------------------+
- | :attr:`%s` | const char\* | A null-terminated C character |
- | | | array. |
- +-------------------+---------------------+----------------------------------+
- | :attr:`%p` | const void\* | The hex representation of a C |
- | | | pointer. Mostly equivalent to |
- | | | ``printf("%p")`` except that |
- | | | it is guaranteed to start with |
- | | | the literal ``0x`` regardless |
- | | | of what the platform's |
- | | | ``printf`` yields. |
- +-------------------+---------------------+----------------------------------+
- | :attr:`%A` | PyObject\* | The result of calling |
- | | | :func:`ascii`. |
- +-------------------+---------------------+----------------------------------+
- | :attr:`%U` | PyObject\* | A Unicode object. |
- +-------------------+---------------------+----------------------------------+
- | :attr:`%V` | PyObject\*, | A Unicode object (which may be |
- | | const char\* | ``NULL``) and a null-terminated |
- | | | C character array as a second |
- | | | parameter (which will be used, |
- | | | if the first parameter is |
- | | | ``NULL``). |
- +-------------------+---------------------+----------------------------------+
- | :attr:`%S` | PyObject\* | The result of calling |
- | | | :c:func:`PyObject_Str`. |
- +-------------------+---------------------+----------------------------------+
- | :attr:`%R` | PyObject\* | The result of calling |
- | | | :c:func:`PyObject_Repr`. |
- +-------------------+---------------------+----------------------------------+
+ +-------------------+---------------------+--------------------------------+
+ | Format Characters | Type | Comment |
+ +===================+=====================+================================+
+ | :attr:`%%` | *n/a* | The literal % character. |
+ +-------------------+---------------------+--------------------------------+
+ | :attr:`%c` | int | A single character, |
+ | | | represented as a C int. |
+ +-------------------+---------------------+--------------------------------+
+ | :attr:`%d` | int | Exactly equivalent to |
+ | | | ``printf("%d")``. |
+ +-------------------+---------------------+--------------------------------+
+ | :attr:`%u` | unsigned int | Exactly equivalent to |
+ | | | ``printf("%u")``. |
+ +-------------------+---------------------+--------------------------------+
+ | :attr:`%ld` | long | Exactly equivalent to |
+ | | | ``printf("%ld")``. |
+ +-------------------+---------------------+--------------------------------+
+ | :attr:`%lu` | unsigned long | Exactly equivalent to |
+ | | | ``printf("%lu")``. |
+ +-------------------+---------------------+--------------------------------+
+ | :attr:`%zd` | Py_ssize_t | Exactly equivalent to |
+ | | | ``printf("%zd")``. |
+ +-------------------+---------------------+--------------------------------+
+ | :attr:`%zu` | size_t | Exactly equivalent to |
+ | | | ``printf("%zu")``. |
+ +-------------------+---------------------+--------------------------------+
+ | :attr:`%i` | int | Exactly equivalent to |
+ | | | ``printf("%i")``. |
+ +-------------------+---------------------+--------------------------------+
+ | :attr:`%x` | int | Exactly equivalent to |
+ | | | ``printf("%x")``. |
+ +-------------------+---------------------+--------------------------------+
+ | :attr:`%s` | char\* | A null-terminated C character |
+ | | | array. |
+ +-------------------+---------------------+--------------------------------+
+ | :attr:`%p` | void\* | The hex representation of a C |
+ | | | pointer. Mostly equivalent to |
+ | | | ``printf("%p")`` except that |
+ | | | it is guaranteed to start with |
+ | | | the literal ``0x`` regardless |
+ | | | of what the platform's |
+ | | | ``printf`` yields. |
+ +-------------------+---------------------+--------------------------------+
+ | :attr:`%U` | PyObject\* | A unicode object. |
+ +-------------------+---------------------+--------------------------------+
+ | :attr:`%V` | PyObject\*, char \* | A unicode object (which may be |
+ | | | *NULL*) and a null-terminated |
+ | | | C character array as a second |
+ | | | parameter (which will be used, |
+ | | | if the first parameter is |
+ | | | *NULL*). |
+ +-------------------+---------------------+--------------------------------+
+ | :attr:`%S` | PyObject\* | The result of calling |
+ | | | :func:`PyObject_Unicode`. |
+ +-------------------+---------------------+--------------------------------+
+ | :attr:`%R` | PyObject\* | The result of calling |
+ | | | :func:`PyObject_Repr`. |
+ +-------------------+---------------------+--------------------------------+
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 width formatter unit is number of characters rather than bytes.
- The precision formatter unit is number of bytes for ``"%s"`` and
- ``"%V"`` (if the ``PyObject*`` argument is ``NULL``), and a number of
- characters for ``"%A"``, ``"%U"``, ``"%S"``, ``"%R"`` and ``"%V"``
- (if the ``PyObject*`` argument is not ``NULL``).
-
- .. [1] For integer specifiers (d, u, ld, li, lu, lld, lli, llu, zd, zi,
- zu, i, x): the 0-conversion flag has effect even when a precision is given.
-
- .. versionchanged:: 3.2
- Support for ``"%lld"`` and ``"%llu"`` added.
-
- .. versionchanged:: 3.3
- Support for ``"%li"``, ``"%lli"`` and ``"%zi"`` added.
-
- .. versionchanged:: 3.4
- Support width and precision formatter for ``"%s"``, ``"%A"``, ``"%U"``,
- ``"%V"``, ``"%S"``, ``"%R"`` added.
+ .. versionadded:: 2.6
.. c:function:: PyObject* PyUnicode_FromFormatV(const char *format, va_list vargs)
- Identical to :c:func:`PyUnicode_FromFormat` except that it takes exactly two
+ Identical to :func:`PyUnicode_FromFormat` except that it takes exactly two
arguments.
-
-.. c:function:: PyObject* PyUnicode_FromEncodedObject(PyObject *obj, \
- const char *encoding, const char *errors)
-
- Decode an encoded object *obj* to a Unicode object.
-
- :class:`bytes`, :class:`bytearray` and other
- :term:`bytes-like objects <bytes-like object>`
- 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 :ref:`builtincodecs` for details).
-
- All other objects, including Unicode objects, cause a :exc:`TypeError` to be
- set.
-
- The API returns ``NULL`` if there was an error. The caller is responsible for
- decref'ing the returned objects.
-
-
-.. c:function:: Py_ssize_t PyUnicode_GetLength(PyObject *unicode)
-
- Return the length of the Unicode object, in code points.
-
- .. versionadded:: 3.3
-
-
-.. c:function:: Py_ssize_t PyUnicode_CopyCharacters(PyObject *to, \
- Py_ssize_t to_start, \
- PyObject *from, \
- Py_ssize_t from_start, \
- Py_ssize_t how_many)
-
- Copy characters from one Unicode object into another. This function performs
- character conversion when necessary and falls back to :c:func:`memcpy` if
- possible. Returns ``-1`` and sets an exception on error, otherwise returns
- the number of copied characters.
-
- .. versionadded:: 3.3
-
-
-.. c:function:: Py_ssize_t PyUnicode_Fill(PyObject *unicode, Py_ssize_t start, \
- Py_ssize_t length, Py_UCS4 fill_char)
-
- Fill a string with a character: write *fill_char* into
- ``unicode[start:start+length]``.
-
- Fail if *fill_char* is bigger than the string maximum character, or if the
- string has more than 1 reference.
-
- Return the number of written character, or return ``-1`` and raise an
- exception on error.
-
- .. versionadded:: 3.3
-
-
-.. c:function:: int PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, \
- Py_UCS4 character)
-
- Write a character to a string. The string must have been created through
- :c:func:`PyUnicode_New`. Since Unicode strings are supposed to be immutable,
- the string must not be shared, or have been hashed yet.
-
- This function checks that *unicode* is a Unicode object, that the index is
- not out of bounds, and that the object can be modified safely (i.e. that it
- its reference count is one).
-
- .. versionadded:: 3.3
-
-
-.. c:function:: Py_UCS4 PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index)
-
- Read a character from a string. This function checks that *unicode* is a
- Unicode object and the index is not out of bounds, in contrast to the macro
- version :c:func:`PyUnicode_READ_CHAR`.
-
- .. versionadded:: 3.3
-
-
-.. c:function:: PyObject* PyUnicode_Substring(PyObject *str, Py_ssize_t start, \
- Py_ssize_t end)
-
- Return a substring of *str*, from character index *start* (included) to
- character index *end* (excluded). Negative indices are not supported.
-
- .. versionadded:: 3.3
-
-
-.. c:function:: Py_UCS4* PyUnicode_AsUCS4(PyObject *u, Py_UCS4 *buffer, \
- Py_ssize_t buflen, int copy_null)
-
- Copy the string *u* into a UCS4 buffer, including a null character, if
- *copy_null* is set. Returns ``NULL`` and sets an exception on error (in
- particular, a :exc:`SystemError` if *buflen* is smaller than the length of
- *u*). *buffer* is returned on success.
-
- .. versionadded:: 3.3
-
-
-.. c:function:: Py_UCS4* PyUnicode_AsUCS4Copy(PyObject *u)
-
- Copy the string *u* into a new UCS4 buffer that is allocated using
- :c:func:`PyMem_Malloc`. If this fails, ``NULL`` is returned with a
- :exc:`MemoryError` set. The returned buffer always has an extra
- null code point appended.
-
- .. versionadded:: 3.3
-
-
-Deprecated Py_UNICODE APIs
-""""""""""""""""""""""""""
-
-.. deprecated-removed:: 3.3 4.0
-
-These API functions are deprecated with the implementation of :pep:`393`.
-Extension modules can continue using them, as they will not be removed in Python
-3.x, but need to be aware that their use can now cause performance and memory hits.
-
-
-.. c:function:: PyObject* PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size)
-
- 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``.
-
- If the buffer is ``NULL``, :c:func:`PyUnicode_READY` must be called once the
- string content has been filled before using any of the access macros such as
- :c:func:`PyUnicode_KIND`.
-
- Please migrate to using :c:func:`PyUnicode_FromKindAndData`,
- :c:func:`PyUnicode_FromWideChar` or :c:func:`PyUnicode_New`.
+ .. versionadded:: 2.6
.. c:function:: Py_UNICODE* PyUnicode_AsUnicode(PyObject *unicode)
Return a read-only pointer to the Unicode object's internal
- :c:type:`Py_UNICODE` buffer, or ``NULL`` on error. This will create the
- :c:type:`Py_UNICODE*` representation of the object if it is not yet
- available. The buffer is always terminated with an extra null code point.
- Note that the resulting :c:type:`Py_UNICODE` string may also contain
- embedded null code points, which would cause the string to be truncated when
- used in most C functions.
-
- Please migrate to using :c:func:`PyUnicode_AsUCS4`,
- :c:func:`PyUnicode_AsWideChar`, :c:func:`PyUnicode_ReadChar` or similar new
- APIs.
-
-
-.. c:function:: PyObject* PyUnicode_TransformDecimalToASCII(Py_UNICODE *s, Py_ssize_t size)
-
- Create a Unicode object by replacing all decimal digits in
- :c:type:`Py_UNICODE` buffer of the given *size* by ASCII digits 0--9
- according to their decimal value. Return ``NULL`` if an exception occurs.
-
-
-.. c:function:: Py_UNICODE* PyUnicode_AsUnicodeAndSize(PyObject *unicode, Py_ssize_t *size)
-
- Like :c:func:`PyUnicode_AsUnicode`, but also saves the :c:func:`Py_UNICODE`
- array length (excluding the extra null terminator) in *size*.
- Note that the resulting :c:type:`Py_UNICODE*` string
- may contain embedded null code points, which would cause the string to be
- truncated when used in most C functions.
-
- .. versionadded:: 3.3
-
-
-.. c:function:: Py_UNICODE* PyUnicode_AsUnicodeCopy(PyObject *unicode)
-
- Create a copy of a Unicode string ending with a null code point. Return ``NULL``
- and raise a :exc:`MemoryError` exception on memory allocation failure,
- otherwise return a new allocated buffer (use :c:func:`PyMem_Free` to free
- the buffer). Note that the resulting :c:type:`Py_UNICODE*` string may
- contain embedded null code points, which would cause the string to be
- truncated when used in most C functions.
-
- .. versionadded:: 3.2
-
- Please migrate to using :c:func:`PyUnicode_AsUCS4Copy` or similar new APIs.
+ :c:type:`Py_UNICODE` buffer, *NULL* if *unicode* is not a Unicode object.
+ Note that the resulting :c:type:`Py_UNICODE*` string may contain embedded
+ null characters, which would cause the string to be truncated when used in
+ most C functions.
.. c:function:: Py_ssize_t PyUnicode_GetSize(PyObject *unicode)
- Return the size of the deprecated :c:type:`Py_UNICODE` representation, in
- code units (this includes surrogate pairs as 2 units).
-
- Please migrate to using :c:func:`PyUnicode_GetLength`.
-
-
-.. c:function:: PyObject* PyUnicode_FromObject(PyObject *obj)
-
- Copy an instance of a Unicode subtype to a new true Unicode object if
- necessary. If *obj* is already a true Unicode object (not a subtype),
- return the reference with incremented refcount.
-
- Objects other than Unicode or its subtypes will cause a :exc:`TypeError`.
-
-
-Locale Encoding
-"""""""""""""""
-
-The current locale encoding can be used to decode text from the operating
-system.
-
-.. c:function:: PyObject* PyUnicode_DecodeLocaleAndSize(const char *str, \
- Py_ssize_t len, \
- const char *errors)
-
- Decode a string from UTF-8 on Android and VxWorks, or from the current
- locale encoding on other platforms. The supported
- error handlers are ``"strict"`` and ``"surrogateescape"``
- (:pep:`383`). The decoder uses ``"strict"`` error handler if
- *errors* is ``NULL``. *str* must end with a null character but
- cannot contain embedded null characters.
-
- Use :c:func:`PyUnicode_DecodeFSDefaultAndSize` to decode a string from
- :c:data:`Py_FileSystemDefaultEncoding` (the locale encoding read at
- Python startup).
-
- This function ignores the Python UTF-8 mode.
-
- .. seealso::
-
- The :c:func:`Py_DecodeLocale` function.
-
- .. versionadded:: 3.3
-
- .. versionchanged:: 3.7
- The function now also uses the current locale encoding for the
- ``surrogateescape`` error handler, except on Android. Previously, :c:func:`Py_DecodeLocale`
- was used for the ``surrogateescape``, and the current locale encoding was
- used for ``strict``.
-
-
-.. c:function:: PyObject* PyUnicode_DecodeLocale(const char *str, const char *errors)
-
- Similar to :c:func:`PyUnicode_DecodeLocaleAndSize`, but compute the string
- length using :c:func:`strlen`.
-
- .. versionadded:: 3.3
-
+ Return the length of the Unicode object.
-.. c:function:: PyObject* PyUnicode_EncodeLocale(PyObject *unicode, const char *errors)
+ .. versionchanged:: 2.5
+ This function returned an :c:type:`int` type. This might require changes
+ in your code for properly supporting 64-bit systems.
- Encode a Unicode object to UTF-8 on Android and VxWorks, or to the current
- locale encoding on other platforms. The
- supported error handlers are ``"strict"`` and ``"surrogateescape"``
- (:pep:`383`). The encoder uses ``"strict"`` error handler if
- *errors* is ``NULL``. Return a :class:`bytes` object. *unicode* cannot
- contain embedded null characters.
- Use :c:func:`PyUnicode_EncodeFSDefault` to encode a string to
- :c:data:`Py_FileSystemDefaultEncoding` (the locale encoding read at
- Python startup).
+.. c:function:: PyObject* PyUnicode_FromEncodedObject(PyObject *obj, const char *encoding, const char *errors)
- This function ignores the Python UTF-8 mode.
+ Coerce an encoded object *obj* to a Unicode object and return a reference with
+ incremented refcount.
- .. seealso::
+ 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).
- The :c:func:`Py_EncodeLocale` function.
-
- .. versionadded:: 3.3
-
- .. versionchanged:: 3.7
- The function now also uses the current locale encoding for the
- ``surrogateescape`` error handler, except on Android. Previously,
- :c:func:`Py_EncodeLocale`
- was used for the ``surrogateescape``, and the current locale encoding was
- used for ``strict``.
-
-
-File System Encoding
-""""""""""""""""""""
-
-To encode and decode file names and other environment strings,
-:c:data:`Py_FileSystemDefaultEncoding` should be used as the encoding, and
-:c:data:`Py_FileSystemDefaultEncodeErrors` should be used as the error handler
-(:pep:`383` and :pep:`529`). To encode file names to :class:`bytes` during
-argument parsing, the ``"O&"`` converter should be used, passing
-:c:func:`PyUnicode_FSConverter` as the conversion function:
-
-.. c:function:: int PyUnicode_FSConverter(PyObject* obj, void* result)
-
- ParseTuple converter: encode :class:`str` objects -- obtained directly or
- through the :class:`os.PathLike` interface -- to :class:`bytes` using
- :c:func:`PyUnicode_EncodeFSDefault`; :class:`bytes` objects are output as-is.
- *result* must be a :c:type:`PyBytesObject*` which must be released when it is
- no longer used.
-
- .. versionadded:: 3.1
-
- .. versionchanged:: 3.6
- Accepts a :term:`path-like object`.
-
-To decode file names to :class:`str` during argument parsing, the ``"O&"``
-converter should be used, passing :c:func:`PyUnicode_FSDecoder` as the
-conversion function:
-
-.. c:function:: int PyUnicode_FSDecoder(PyObject* obj, void* result)
-
- ParseTuple converter: decode :class:`bytes` objects -- obtained either
- directly or indirectly through the :class:`os.PathLike` interface -- to
- :class:`str` using :c:func:`PyUnicode_DecodeFSDefaultAndSize`; :class:`str`
- objects are output as-is. *result* must be a :c:type:`PyUnicodeObject*` which
- must be released when it is no longer used.
-
- .. versionadded:: 3.2
-
- .. versionchanged:: 3.6
- Accepts a :term:`path-like object`.
-
-
-.. c:function:: PyObject* PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size)
-
- Decode a string using :c:data:`Py_FileSystemDefaultEncoding` and the
- :c:data:`Py_FileSystemDefaultEncodeErrors` error handler.
-
- If :c:data:`Py_FileSystemDefaultEncoding` is not set, fall back to the
- locale encoding.
-
- :c:data:`Py_FileSystemDefaultEncoding` is initialized at startup from the
- locale encoding and cannot be modified later. If you need to decode a string
- from the current locale encoding, use
- :c:func:`PyUnicode_DecodeLocaleAndSize`.
-
- .. seealso::
-
- The :c:func:`Py_DecodeLocale` function.
-
- .. versionchanged:: 3.6
- Use :c:data:`Py_FileSystemDefaultEncodeErrors` error handler.
-
-
-.. c:function:: PyObject* PyUnicode_DecodeFSDefault(const char *s)
-
- Decode a null-terminated string using :c:data:`Py_FileSystemDefaultEncoding`
- and the :c:data:`Py_FileSystemDefaultEncodeErrors` error handler.
-
- If :c:data:`Py_FileSystemDefaultEncoding` is not set, fall back to the
- locale encoding.
-
- Use :c:func:`PyUnicode_DecodeFSDefaultAndSize` if you know the string length.
-
- .. versionchanged:: 3.6
- Use :c:data:`Py_FileSystemDefaultEncodeErrors` error handler.
-
-
-.. c:function:: PyObject* PyUnicode_EncodeFSDefault(PyObject *unicode)
-
- Encode a Unicode object to :c:data:`Py_FileSystemDefaultEncoding` with the
- :c:data:`Py_FileSystemDefaultEncodeErrors` error handler, and return
- :class:`bytes`. Note that the resulting :class:`bytes` object may contain
- null bytes.
+ All other objects, including Unicode objects, cause a :exc:`TypeError` to be
+ set.
- If :c:data:`Py_FileSystemDefaultEncoding` is not set, fall back to the
- locale encoding.
+ The API returns *NULL* if there was an error. The caller is responsible for
+ decref'ing the returned objects.
- :c:data:`Py_FileSystemDefaultEncoding` is initialized at startup from the
- locale encoding and cannot be modified later. If you need to encode a string
- to the current locale encoding, use :c:func:`PyUnicode_EncodeLocale`.
- .. seealso::
+.. c:function:: PyObject* PyUnicode_FromObject(PyObject *obj)
- The :c:func:`Py_EncodeLocale` function.
+ Shortcut for ``PyUnicode_FromEncodedObject(obj, NULL, "strict")`` which is used
+ throughout the interpreter whenever coercion to Unicode is needed.
- .. versionadded:: 3.2
+If the platform supports :c:type:`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 :c:type:`Py_UNICODE` type is identical to
+the system's :c:type:`wchar_t`.
- .. versionchanged:: 3.6
- Use :c:data:`Py_FileSystemDefaultEncodeErrors` error handler.
wchar_t Support
"""""""""""""""
@@ -925,44 +381,29 @@ wchar_t Support
.. c:function:: PyObject* PyUnicode_FromWideChar(const wchar_t *w, Py_ssize_t size)
Create a Unicode object from the :c:type:`wchar_t` buffer *w* of the given *size*.
- Passing ``-1`` as the *size* indicates that the function must itself compute the length,
- using wcslen.
- Return ``NULL`` on failure.
+ Return *NULL* on failure.
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` type for *size*. This might require
+ changes in your code for properly supporting 64-bit systems.
-.. c:function:: Py_ssize_t PyUnicode_AsWideChar(PyObject *unicode, wchar_t *w, Py_ssize_t size)
+
+.. c:function:: Py_ssize_t PyUnicode_AsWideChar(PyUnicodeObject *unicode, wchar_t *w, Py_ssize_t size)
Copy the Unicode object contents into the :c:type:`wchar_t` buffer *w*. At most
*size* :c:type:`wchar_t` characters are copied (excluding a possibly trailing
- null termination character). Return the number of :c:type:`wchar_t` characters
- copied or ``-1`` in case of an error. Note that the resulting :c:type:`wchar_t*`
- string may or may not be null-terminated. It is the responsibility of the caller
- to make sure that the :c:type:`wchar_t*` string is null-terminated in case this is
+ 0-termination character). Return the number of :c:type:`wchar_t` characters
+ copied or ``-1`` in case of an error. Note that the resulting :c:type:`wchar_t`
+ string may or may not be 0-terminated. It is the responsibility of the caller
+ to make sure that the :c:type:`wchar_t` string is 0-terminated in case this is
required by the application. Also, note that the :c:type:`wchar_t*` string
might contain null characters, which would cause the string to be truncated
when used with most C functions.
-
-.. c:function:: wchar_t* PyUnicode_AsWideCharString(PyObject *unicode, Py_ssize_t *size)
-
- Convert the Unicode object to a wide character string. The output string
- always ends with a null character. If *size* is not ``NULL``, write the number
- of wide characters (excluding the trailing null termination character) into
- *\*size*. Note that the resulting :c:type:`wchar_t` string might contain
- null characters, which would cause the string to be truncated when used with
- most C functions. If *size* is ``NULL`` and the :c:type:`wchar_t*` string
- contains null characters a :exc:`ValueError` is raised.
-
- Returns a buffer allocated by :c:func:`PyMem_Alloc` (use
- :c:func:`PyMem_Free` to free it) on success. On error, returns ``NULL``
- and *\*size* is undefined. Raises a :exc:`MemoryError` if memory allocation
- is failed.
-
- .. versionadded:: 3.2
-
- .. versionchanged:: 3.7
- Raises a :exc:`ValueError` if *size* is ``NULL`` and the :c:type:`wchar_t*`
- string contains null characters.
+ .. versionchanged:: 2.5
+ This function returned an :c:type:`int` type and used an :c:type:`int`
+ type for *size*. This might require changes in your code for properly
+ supporting 64-bit systems.
.. _builtincodecs:
@@ -974,18 +415,16 @@ Python provides a set of built-in codecs which are written in C for speed. All o
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 :func:`str` string object
-constructor.
-
-Setting encoding to ``NULL`` causes the default encoding to be used
-which is ASCII. The file system calls should use
-:c:func:`PyUnicode_FSConverter` for encoding file names. This uses the
-variable :c:data:`Py_FileSystemDefaultEncoding` internally. 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
+have the same semantics as the ones of the built-in :func:`unicode` Unicode
+object constructor.
+
+Setting encoding to *NULL* causes the default encoding to be used which is
+ASCII. The file system calls should use :c:data:`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" (:exc:`ValueError` is raised).
@@ -999,38 +438,39 @@ Generic Codecs
These are the generic codec APIs:
-.. c:function:: PyObject* PyUnicode_Decode(const char *s, Py_ssize_t size, \
- const char *encoding, const char *errors)
+.. c:function:: PyObject* PyUnicode_Decode(const char *s, Py_ssize_t size, const char *encoding, const char *errors)
Create a Unicode object by decoding *size* bytes of the encoded string *s*.
*encoding* and *errors* have the same meaning as the parameters of the same name
- in the :func:`str` built-in function. The codec to be used is looked up
- using the Python codec registry. Return ``NULL`` if an exception was raised by
+ in the :func:`unicode` 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.
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` type for *size*. This might require
+ changes in your code for properly supporting 64-bit systems.
-.. c:function:: PyObject* PyUnicode_AsEncodedString(PyObject *unicode, \
- const char *encoding, const char *errors)
- Encode a Unicode object and return the result as Python bytes object.
- *encoding* and *errors* have the same meaning as the parameters of the same
- name in the Unicode :meth:`~str.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.
+.. c:function:: PyObject* PyUnicode_Encode(const Py_UNICODE *s, Py_ssize_t size, const char *encoding, const char *errors)
+ Encode the :c:type:`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 :meth:`~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.
-.. c:function:: PyObject* PyUnicode_Encode(const Py_UNICODE *s, Py_ssize_t size, \
- const char *encoding, const char *errors)
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` type for *size*. This might require
+ changes in your code for properly supporting 64-bit systems.
- Encode the :c:type:`Py_UNICODE` buffer *s* of the given *size* and return a Python
- bytes object. *encoding* and *errors* have the same meaning as the
- parameters of the same name in the Unicode :meth:`~str.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.
- .. deprecated-removed:: 3.3 4.0
- Part of the old-style :c:type:`Py_UNICODE` API; please migrate to using
- :c:func:`PyUnicode_AsEncodedString`.
+.. c:function:: PyObject* PyUnicode_AsEncodedString(PyObject *unicode, const char *encoding, const char *errors)
+
+ 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 :meth:`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
@@ -1042,66 +482,42 @@ These are the UTF-8 codec APIs:
.. c:function:: PyObject* PyUnicode_DecodeUTF8(const char *s, Py_ssize_t size, const char *errors)
Create a Unicode object by decoding *size* bytes of the UTF-8 encoded string
- *s*. Return ``NULL`` if an exception was raised by the codec.
+ *s*. Return *NULL* if an exception was raised by the codec.
+
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` type for *size*. This might require
+ changes in your code for properly supporting 64-bit systems.
-.. c:function:: PyObject* PyUnicode_DecodeUTF8Stateful(const char *s, Py_ssize_t size, \
- const char *errors, Py_ssize_t *consumed)
+.. c:function:: PyObject* PyUnicode_DecodeUTF8Stateful(const char *s, Py_ssize_t size, const char *errors, Py_ssize_t *consumed)
- If *consumed* is ``NULL``, behave like :c:func:`PyUnicode_DecodeUTF8`. If
- *consumed* is not ``NULL``, trailing incomplete UTF-8 byte sequences will not be
+ If *consumed* is *NULL*, behave like :c:func:`PyUnicode_DecodeUTF8`. If
+ *consumed* is not *NULL*, trailing incomplete UTF-8 byte sequences will not be
treated as an error. Those bytes will not be decoded and the number of bytes
that have been decoded will be stored in *consumed*.
+ .. versionadded:: 2.4
-.. c:function:: PyObject* PyUnicode_AsUTF8String(PyObject *unicode)
-
- Encode a Unicode object using UTF-8 and return the result as Python bytes
- object. Error handling is "strict". Return ``NULL`` if an exception was
- raised by the codec.
-
-
-.. c:function:: const char* PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *size)
-
- Return a pointer to the UTF-8 encoding of the Unicode object, and
- store the size of the encoded representation (in bytes) in *size*. The
- *size* argument can be ``NULL``; in this case no size will be stored. The
- returned buffer always has an extra null byte appended (not included in
- *size*), regardless of whether there are any other null code points.
-
- In the case of an error, ``NULL`` is returned with an exception set and no
- *size* is stored.
-
- This caches the UTF-8 representation of the string in the Unicode object, and
- subsequent calls will return a pointer to the same buffer. The caller is not
- responsible for deallocating the buffer.
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` type for *size*. This might require
+ changes in your code for properly supporting 64-bit systems.
- .. versionadded:: 3.3
- .. versionchanged:: 3.7
- The return type is now ``const char *`` rather of ``char *``.
-
-
-.. c:function:: const char* PyUnicode_AsUTF8(PyObject *unicode)
-
- As :c:func:`PyUnicode_AsUTF8AndSize`, but does not store the size.
-
- .. versionadded:: 3.3
+.. c:function:: PyObject* PyUnicode_EncodeUTF8(const Py_UNICODE *s, Py_ssize_t size, const char *errors)
- .. versionchanged:: 3.7
- The return type is now ``const char *`` rather of ``char *``.
+ Encode the :c:type:`Py_UNICODE` buffer *s* of the given *size* using UTF-8 and return a
+ Python string object. Return *NULL* if an exception was raised by the codec.
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` type for *size*. This might require
+ changes in your code for properly supporting 64-bit systems.
-.. c:function:: PyObject* PyUnicode_EncodeUTF8(const Py_UNICODE *s, Py_ssize_t size, const char *errors)
- Encode the :c:type:`Py_UNICODE` buffer *s* of the given *size* using UTF-8 and
- return a Python bytes object. Return ``NULL`` if an exception was raised by
- the codec.
+.. c:function:: PyObject* PyUnicode_AsUTF8String(PyObject *unicode)
- .. deprecated-removed:: 3.3 4.0
- Part of the old-style :c:type:`Py_UNICODE` API; please migrate to using
- :c:func:`PyUnicode_AsUTF8String`, :c:func:`PyUnicode_AsUTF8AndSize` or
- :c:func:`PyUnicode_AsEncodedString`.
+ Encode a Unicode object using UTF-8 and return the result as Python string
+ object. Error handling is "strict". Return *NULL* if an exception was raised
+ by the codec.
UTF-32 Codecs
@@ -1110,14 +526,13 @@ UTF-32 Codecs
These are the UTF-32 codec APIs:
-.. c:function:: PyObject* PyUnicode_DecodeUTF32(const char *s, Py_ssize_t size, \
- const char *errors, int *byteorder)
+.. c:function:: PyObject* PyUnicode_DecodeUTF32(const char *s, Py_ssize_t size, const char *errors, int *byteorder)
Decode *size* bytes from a UTF-32 encoded buffer string and return the
- corresponding Unicode object. *errors* (if non-``NULL``) defines the error
+ corresponding Unicode object. *errors* (if non-*NULL*) defines the error
handling. It defaults to "strict".
- If *byteorder* is non-``NULL``, the decoder starts decoding using the given byte
+ If *byteorder* is non-*NULL*, the decoder starts decoding using the given byte
order::
*byteorder == -1: little endian
@@ -1132,30 +547,27 @@ These are the UTF-32 codec APIs:
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.
+ 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.
- Return ``NULL`` if an exception was raised by the codec.
+ .. versionadded:: 2.6
-.. c:function:: PyObject* PyUnicode_DecodeUTF32Stateful(const char *s, Py_ssize_t size, \
- const char *errors, int *byteorder, Py_ssize_t *consumed)
+.. c:function:: 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 :c:func:`PyUnicode_DecodeUTF32`. If
- *consumed* is not ``NULL``, :c:func:`PyUnicode_DecodeUTF32Stateful` will not treat
+ If *consumed* is *NULL*, behave like :c:func:`PyUnicode_DecodeUTF32`. If
+ *consumed* is not *NULL*, :c:func:`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*.
-
-.. c:function:: PyObject* PyUnicode_AsUTF32String(PyObject *unicode)
-
- Return a Python byte 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.
+ .. versionadded:: 2.6
-.. c:function:: PyObject* PyUnicode_EncodeUTF32(const Py_UNICODE *s, Py_ssize_t size, \
- const char *errors, int byteorder)
+.. c:function:: 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::
@@ -1167,14 +579,21 @@ These are the UTF-32 codec APIs:
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
+ 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.
+ Return *NULL* if an exception was raised by the codec.
+
+ .. versionadded:: 2.6
+
- .. deprecated-removed:: 3.3 4.0
- Part of the old-style :c:type:`Py_UNICODE` API; please migrate to using
- :c:func:`PyUnicode_AsUTF32String` or :c:func:`PyUnicode_AsEncodedString`.
+.. c:function:: 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.
+
+ .. versionadded:: 2.6
UTF-16 Codecs
@@ -1183,14 +602,13 @@ UTF-16 Codecs
These are the UTF-16 codec APIs:
-.. c:function:: PyObject* PyUnicode_DecodeUTF16(const char *s, Py_ssize_t size, \
- const char *errors, int *byteorder)
+.. c:function:: PyObject* PyUnicode_DecodeUTF16(const char *s, Py_ssize_t size, const char *errors, int *byteorder)
Decode *size* bytes from a UTF-16 encoded buffer string and return the
- corresponding Unicode object. *errors* (if non-``NULL``) defines the error
+ corresponding Unicode object. *errors* (if non-*NULL*) defines the error
handling. It defaults to "strict".
- If *byteorder* is non-``NULL``, the decoder starts decoding using the given byte
+ If *byteorder* is non-*NULL*, the decoder starts decoding using the given byte
order::
*byteorder == -1: little endian
@@ -1206,32 +624,34 @@ These are the UTF-16 codec APIs:
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.
+ If *byteorder* is *NULL*, the codec starts in native order mode.
- Return ``NULL`` if an exception was raised by the codec.
+ Return *NULL* if an exception was raised by the codec.
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` type for *size*. This might require
+ changes in your code for properly supporting 64-bit systems.
-.. c:function:: PyObject* PyUnicode_DecodeUTF16Stateful(const char *s, Py_ssize_t size, \
- const char *errors, int *byteorder, Py_ssize_t *consumed)
- If *consumed* is ``NULL``, behave like :c:func:`PyUnicode_DecodeUTF16`. If
- *consumed* is not ``NULL``, :c:func:`PyUnicode_DecodeUTF16Stateful` will not treat
+.. c:function:: PyObject* PyUnicode_DecodeUTF16Stateful(const char *s, Py_ssize_t size, const char *errors, int *byteorder, Py_ssize_t *consumed)
+
+ If *consumed* is *NULL*, behave like :c:func:`PyUnicode_DecodeUTF16`. If
+ *consumed* is not *NULL*, :c:func:`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*.
+ .. versionadded:: 2.4
-.. c:function:: PyObject* PyUnicode_AsUTF16String(PyObject *unicode)
-
- Return a Python byte 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.
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` type for *size* and an :c:type:`int *`
+ type for *consumed*. This might require changes in your code for
+ properly supporting 64-bit systems.
-.. c:function:: PyObject* PyUnicode_EncodeUTF16(const Py_UNICODE *s, Py_ssize_t size, \
- const char *errors, int byteorder)
+.. c:function:: PyObject* PyUnicode_EncodeUTF16(const Py_UNICODE *s, Py_ssize_t size, const char *errors, int byteorder)
- Return a Python bytes object holding the UTF-16 encoded value of the Unicode
+ 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::
byteorder == -1: little endian
@@ -1241,15 +661,22 @@ These are the UTF-16 codec APIs:
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 :c:type:`Py_UNICODE` value may get
+ If *Py_UNICODE_WIDE* is defined, a single :c:type:`Py_UNICODE` value may get
represented as a surrogate pair. If it is not defined, each :c:type:`Py_UNICODE`
values is interpreted as a UCS-2 character.
- Return ``NULL`` if an exception was raised by the codec.
+ Return *NULL* if an exception was raised by the codec.
- .. deprecated-removed:: 3.3 4.0
- Part of the old-style :c:type:`Py_UNICODE` API; please migrate to using
- :c:func:`PyUnicode_AsUTF16String` or :c:func:`PyUnicode_AsEncodedString`.
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` type for *size*. This might require
+ changes in your code for properly supporting 64-bit systems.
+
+
+.. c:function:: PyObject* PyUnicode_AsUTF16String(PyObject *unicode)
+
+ 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
@@ -1261,23 +688,21 @@ These are the UTF-7 codec APIs:
.. c:function:: PyObject* PyUnicode_DecodeUTF7(const char *s, Py_ssize_t size, const char *errors)
Create a Unicode object by decoding *size* bytes of the UTF-7 encoded string
- *s*. Return ``NULL`` if an exception was raised by the codec.
+ *s*. Return *NULL* if an exception was raised by the codec.
-.. c:function:: PyObject* PyUnicode_DecodeUTF7Stateful(const char *s, Py_ssize_t size, \
- const char *errors, Py_ssize_t *consumed)
+.. c:function:: PyObject* PyUnicode_DecodeUTF7Stateful(const char *s, Py_ssize_t size, const char *errors, Py_ssize_t *consumed)
- If *consumed* is ``NULL``, behave like :c:func:`PyUnicode_DecodeUTF7`. If
- *consumed* is not ``NULL``, trailing incomplete UTF-7 base-64 sections will not
+ If *consumed* is *NULL*, behave like :c:func:`PyUnicode_DecodeUTF7`. If
+ *consumed* is not *NULL*, trailing incomplete UTF-7 base-64 sections will not
be treated as an error. Those bytes will not be decoded and the number of
bytes that have been decoded will be stored in *consumed*.
-.. c:function:: PyObject* PyUnicode_EncodeUTF7(const Py_UNICODE *s, Py_ssize_t size, \
- int base64SetO, int base64WhiteSpace, const char *errors)
+.. c:function:: PyObject* PyUnicode_EncodeUTF7(const Py_UNICODE *s, Py_ssize_t size, int base64SetO, int base64WhiteSpace, const char *errors)
Encode the :c:type:`Py_UNICODE` buffer of the given size using UTF-7 and
- return a Python bytes object. Return ``NULL`` if an exception was raised by
+ return a Python bytes object. Return *NULL* if an exception was raised by
the codec.
If *base64SetO* is nonzero, "Set O" (punctuation that has no otherwise
@@ -1285,10 +710,6 @@ These are the UTF-7 codec APIs:
nonzero, whitespace will be encoded in base-64. Both are set to zero for the
Python "utf-7" codec.
- .. deprecated-removed:: 3.3 4.0
- Part of the old-style :c:type:`Py_UNICODE` API; please migrate to using
- :c:func:`PyUnicode_AsEncodedString`.
-
Unicode-Escape Codecs
"""""""""""""""""""""
@@ -1296,28 +717,32 @@ Unicode-Escape Codecs
These are the "Unicode Escape" codec APIs:
-.. c:function:: PyObject* PyUnicode_DecodeUnicodeEscape(const char *s, \
- Py_ssize_t size, const char *errors)
+.. c:function:: PyObject* PyUnicode_DecodeUnicodeEscape(const char *s, Py_ssize_t size, const char *errors)
Create a Unicode object by decoding *size* bytes of the Unicode-Escape encoded
- string *s*. Return ``NULL`` if an exception was raised by the codec.
+ string *s*. Return *NULL* if an exception was raised by the codec.
-
-.. c:function:: PyObject* PyUnicode_AsUnicodeEscapeString(PyObject *unicode)
-
- Encode a Unicode object using Unicode-Escape and return the result as a
- bytes object. Error handling is "strict". Return ``NULL`` if an exception was
- raised by the codec.
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` type for *size*. This might require
+ changes in your code for properly supporting 64-bit systems.
.. c:function:: PyObject* PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, Py_ssize_t size)
Encode the :c:type:`Py_UNICODE` buffer of the given *size* using Unicode-Escape and
- return a bytes object. Return ``NULL`` if an exception was raised by the codec.
+ return a Python string object. Return *NULL* if an exception was raised by the
+ codec.
- .. deprecated-removed:: 3.3 4.0
- Part of the old-style :c:type:`Py_UNICODE` API; please migrate to using
- :c:func:`PyUnicode_AsUnicodeEscapeString`.
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` type for *size*. This might require
+ changes in your code for properly supporting 64-bit systems.
+
+
+.. c:function:: PyObject* PyUnicode_AsUnicodeEscapeString(PyObject *unicode)
+
+ Encode a Unicode object using Unicode-Escape and return the result as Python
+ string object. Error handling is "strict". Return *NULL* if an exception was
+ raised by the codec.
Raw-Unicode-Escape Codecs
@@ -1326,30 +751,32 @@ Raw-Unicode-Escape Codecs
These are the "Raw Unicode Escape" codec APIs:
-.. c:function:: PyObject* PyUnicode_DecodeRawUnicodeEscape(const char *s, \
- Py_ssize_t size, const char *errors)
+.. c:function:: PyObject* PyUnicode_DecodeRawUnicodeEscape(const char *s, Py_ssize_t size, const char *errors)
Create a Unicode object by decoding *size* bytes of the Raw-Unicode-Escape
- encoded string *s*. Return ``NULL`` if an exception was raised by the codec.
+ encoded string *s*. Return *NULL* if an exception was raised by the codec.
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` type for *size*. This might require
+ changes in your code for properly supporting 64-bit systems.
-.. c:function:: PyObject* PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode)
- Encode a Unicode object using Raw-Unicode-Escape and return the result as
- a bytes object. Error handling is "strict". Return ``NULL`` if an exception
- was raised by the codec.
+.. c:function:: PyObject* PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, Py_ssize_t size, const char *errors)
+ Encode the :c:type:`Py_UNICODE` buffer of the given *size* using Raw-Unicode-Escape
+ and return a Python string object. Return *NULL* if an exception was raised by
+ the codec.
-.. c:function:: PyObject* PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, \
- Py_ssize_t size)
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` type for *size*. This might require
+ changes in your code for properly supporting 64-bit systems.
- Encode the :c:type:`Py_UNICODE` buffer of the given *size* using Raw-Unicode-Escape
- and return a bytes object. Return ``NULL`` if an exception was raised by the codec.
- .. deprecated-removed:: 3.3 4.0
- Part of the old-style :c:type:`Py_UNICODE` API; please migrate to using
- :c:func:`PyUnicode_AsRawUnicodeEscapeString` or
- :c:func:`PyUnicode_AsEncodedString`.
+.. c:function:: PyObject* PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode)
+
+ Encode a Unicode object using Raw-Unicode-Escape and return the result as
+ Python string object. Error handling is "strict". Return *NULL* if an exception
+ was raised by the codec.
Latin-1 Codecs
@@ -1362,26 +789,28 @@ ordinals and only these are accepted by the codecs during encoding.
.. c:function:: PyObject* PyUnicode_DecodeLatin1(const char *s, Py_ssize_t size, const char *errors)
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.
+ *s*. Return *NULL* if an exception was raised by the codec.
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` type for *size*. This might require
+ changes in your code for properly supporting 64-bit systems.
-.. c:function:: PyObject* PyUnicode_AsLatin1String(PyObject *unicode)
- Encode a Unicode object using Latin-1 and return the result as Python bytes
- object. Error handling is "strict". Return ``NULL`` if an exception was
- raised by the codec.
+.. c:function:: PyObject* PyUnicode_EncodeLatin1(const Py_UNICODE *s, Py_ssize_t size, const char *errors)
+ Encode the :c:type:`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.
-.. c:function:: PyObject* PyUnicode_EncodeLatin1(const Py_UNICODE *s, Py_ssize_t size, const char *errors)
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` type for *size*. This might require
+ changes in your code for properly supporting 64-bit systems.
- Encode the :c:type:`Py_UNICODE` buffer of the given *size* using Latin-1 and
- return a Python bytes object. Return ``NULL`` if an exception was raised by
- the codec.
- .. deprecated-removed:: 3.3 4.0
- Part of the old-style :c:type:`Py_UNICODE` API; please migrate to using
- :c:func:`PyUnicode_AsLatin1String` or
- :c:func:`PyUnicode_AsEncodedString`.
+.. c:function:: PyObject* PyUnicode_AsLatin1String(PyObject *unicode)
+
+ 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
@@ -1394,26 +823,28 @@ codes generate errors.
.. c:function:: PyObject* PyUnicode_DecodeASCII(const char *s, Py_ssize_t size, const char *errors)
Create a Unicode object by decoding *size* bytes of the ASCII encoded string
- *s*. Return ``NULL`` if an exception was raised by the codec.
+ *s*. Return *NULL* if an exception was raised by the codec.
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` type for *size*. This might require
+ changes in your code for properly supporting 64-bit systems.
-.. c:function:: PyObject* PyUnicode_AsASCIIString(PyObject *unicode)
- Encode a Unicode object using ASCII and return the result as Python bytes
- object. Error handling is "strict". Return ``NULL`` if an exception was
- raised by the codec.
+.. c:function:: PyObject* PyUnicode_EncodeASCII(const Py_UNICODE *s, Py_ssize_t size, const char *errors)
+ Encode the :c:type:`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.
-.. c:function:: PyObject* PyUnicode_EncodeASCII(const Py_UNICODE *s, Py_ssize_t size, const char *errors)
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` type for *size*. This might require
+ changes in your code for properly supporting 64-bit systems.
- Encode the :c:type:`Py_UNICODE` buffer of the given *size* using ASCII and
- return a Python bytes object. Return ``NULL`` if an exception was raised by
- the codec.
- .. deprecated-removed:: 3.3 4.0
- Part of the old-style :c:type:`Py_UNICODE` API; please migrate to using
- :c:func:`PyUnicode_AsASCIIString` or
- :c:func:`PyUnicode_AsEncodedString`.
+.. c:function:: PyObject* PyUnicode_AsASCIIString(PyObject *unicode)
+
+ 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.
Character Map Codecs
@@ -1422,78 +853,79 @@ Character Map Codecs
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 :mod:`encodings` package). The codec uses mapping to encode and
-decode characters. The mapping objects provided must support the
-:meth:`__getitem__` mapping interface; dictionaries and sequences work well.
+decode characters.
-These are the mapping codec APIs:
+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).
-.. c:function:: PyObject* PyUnicode_DecodeCharmap(const char *data, Py_ssize_t size, \
- PyObject *mapping, const char *errors)
+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).
- 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.
+The mapping objects provided must only support the __getitem__ mapping
+interface.
- If *mapping* is ``NULL``, Latin-1 decoding will be applied. Else
- *mapping* must map bytes ordinals (integers in the range from 0 to 255)
- to Unicode strings, integers (which are then interpreted as Unicode
- ordinals) or ``None``. Unmapped data bytes -- ones which cause a
- :exc:`LookupError`, as well as ones which get mapped to ``None``,
- ``0xFFFE`` or ``'\ufffe'``, are treated as undefined mappings and cause
- an error.
+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:
-.. c:function:: PyObject* PyUnicode_AsCharmapString(PyObject *unicode, PyObject *mapping)
+.. c:function:: PyObject* PyUnicode_DecodeCharmap(const char *s, Py_ssize_t size, PyObject *mapping, const char *errors)
- Encode a Unicode object using the given *mapping* object and return the
- result as a bytes object. Error handling is "strict". Return ``NULL`` if an
- exception was raised by the codec.
+ 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".
- The *mapping* object must map Unicode ordinal integers to bytes objects,
- integers in the range from 0 to 255 or ``None``. Unmapped character
- ordinals (ones which cause a :exc:`LookupError`) as well as mapped to
- ``None`` are treated as "undefined mapping" and cause an error.
+ .. versionchanged:: 2.4
+ Allowed unicode string as mapping argument.
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` type for *size*. This might require
+ changes in your code for properly supporting 64-bit systems.
-.. c:function:: PyObject* PyUnicode_EncodeCharmap(const Py_UNICODE *s, Py_ssize_t size, \
- PyObject *mapping, const char *errors)
- Encode the :c:type:`Py_UNICODE` buffer of the given *size* using the given
- *mapping* object and return the result as a bytes object. Return ``NULL`` if
- an exception was raised by the codec.
+.. c:function:: PyObject* PyUnicode_EncodeCharmap(const Py_UNICODE *s, Py_ssize_t size, PyObject *mapping, const char *errors)
- .. deprecated-removed:: 3.3 4.0
- Part of the old-style :c:type:`Py_UNICODE` API; please migrate to using
- :c:func:`PyUnicode_AsCharmapString` or
- :c:func:`PyUnicode_AsEncodedString`.
+ Encode the :c:type:`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.
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` type for *size*. This might require
+ changes in your code for properly supporting 64-bit systems.
-The following codec API is special in that maps Unicode to Unicode.
-.. c:function:: PyObject* PyUnicode_Translate(PyObject *unicode, \
- PyObject *mapping, const char *errors)
+.. c:function:: PyObject* PyUnicode_AsCharmapString(PyObject *unicode, PyObject *mapping)
- Translate a Unicode object using the given *mapping* object and return the
- resulting Unicode object. Return ``NULL`` if an exception was raised by the
- codec.
+ 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 *mapping* object must map Unicode ordinal integers to Unicode strings,
- integers (which are then interpreted as Unicode ordinals) or ``None``
- (causing deletion of the character). Unmapped character ordinals (ones
- which cause a :exc:`LookupError`) are left untouched and are copied as-is.
+The following codec API is special in that maps Unicode to Unicode.
-.. c:function:: PyObject* PyUnicode_TranslateCharmap(const Py_UNICODE *s, Py_ssize_t size, \
- PyObject *mapping, const char *errors)
+.. c:function:: PyObject* PyUnicode_TranslateCharmap(const Py_UNICODE *s, Py_ssize_t size, PyObject *table, const char *errors)
Translate a :c:type:`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.
+ 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 :meth:`__getitem__` interface; dictionaries
+ and sequences work well. Unmapped character ordinals (ones which cause a
+ :exc:`LookupError`) are left untouched and are copied as-is.
- .. deprecated-removed:: 3.3 4.0
- Part of the old-style :c:type:`Py_UNICODE` API; please migrate to using
- :c:func:`PyUnicode_Translate`. or :ref:`generic codec based API
- <codec-registry>`
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` type for *size*. This might require
+ changes in your code for properly supporting 64-bit systems.
MBCS codecs for Windows
@@ -1504,53 +936,47 @@ 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.
+
.. c:function:: PyObject* PyUnicode_DecodeMBCS(const char *s, Py_ssize_t size, const char *errors)
Create a Unicode object by decoding *size* bytes of the MBCS encoded string *s*.
- Return ``NULL`` if an exception was raised by the codec.
+ Return *NULL* if an exception was raised by the codec.
+
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` type for *size*. This might require
+ changes in your code for properly supporting 64-bit systems.
-.. c:function:: PyObject* PyUnicode_DecodeMBCSStateful(const char *s, Py_ssize_t size, \
- const char *errors, Py_ssize_t *consumed)
+.. c:function:: PyObject* PyUnicode_DecodeMBCSStateful(const char *s, int size, const char *errors, int *consumed)
- If *consumed* is ``NULL``, behave like :c:func:`PyUnicode_DecodeMBCS`. If
- *consumed* is not ``NULL``, :c:func:`PyUnicode_DecodeMBCSStateful` will not decode
+ If *consumed* is *NULL*, behave like :c:func:`PyUnicode_DecodeMBCS`. If
+ *consumed* is not *NULL*, :c:func:`PyUnicode_DecodeMBCSStateful` will not decode
trailing lead byte and the number of bytes that have been decoded will be stored
in *consumed*.
-
-.. c:function:: PyObject* PyUnicode_AsMBCSString(PyObject *unicode)
-
- Encode a Unicode object using MBCS and return the result as Python bytes
- object. Error handling is "strict". Return ``NULL`` if an exception was
- raised by the codec.
+ .. versionadded:: 2.5
-.. c:function:: PyObject* PyUnicode_EncodeCodePage(int code_page, PyObject *unicode, const char *errors)
-
- Encode the Unicode object using the specified code page and return a Python
- bytes object. Return ``NULL`` if an exception was raised by the codec. Use
- :c:data:`CP_ACP` code page to get the MBCS encoder.
+.. c:function:: PyObject* PyUnicode_EncodeMBCS(const Py_UNICODE *s, Py_ssize_t size, const char *errors)
- .. versionadded:: 3.3
+ Encode the :c:type:`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.
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` type for *size*. This might require
+ changes in your code for properly supporting 64-bit systems.
-.. c:function:: PyObject* PyUnicode_EncodeMBCS(const Py_UNICODE *s, Py_ssize_t size, const char *errors)
- Encode the :c:type:`Py_UNICODE` buffer of the given *size* using MBCS and return
- a Python bytes object. Return ``NULL`` if an exception was raised by the
- codec.
+.. c:function:: PyObject* PyUnicode_AsMBCSString(PyObject *unicode)
- .. deprecated-removed:: 3.3 4.0
- Part of the old-style :c:type:`Py_UNICODE` API; please migrate to using
- :c:func:`PyUnicode_AsMBCSString`, :c:func:`PyUnicode_EncodeCodePage` or
- :c:func:`PyUnicode_AsEncodedString`.
+ 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.
Methods & Slots
"""""""""""""""
-
.. _unicodemethodsandslots:
Methods and Slot Functions
@@ -1560,7 +986,7 @@ 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.
+They all return *NULL* or ``-1`` if an exception occurs.
.. c:function:: PyObject* PyUnicode_Concat(PyObject *left, PyObject *right)
@@ -1570,11 +996,15 @@ They all return ``NULL`` or ``-1`` if an exception occurs.
.. c:function:: PyObject* PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit)
- Split a string giving a list of Unicode strings. If *sep* is ``NULL``, splitting
+ 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.
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` type for *maxsplit*. This might require
+ changes in your code for properly supporting 64-bit systems.
+
.. c:function:: PyObject* PyUnicode_Splitlines(PyObject *s, int keepend)
@@ -1583,8 +1013,7 @@ They all return ``NULL`` or ``-1`` if an exception occurs.
characters are not included in the resulting strings.
-.. c:function:: PyObject* PyUnicode_Translate(PyObject *str, PyObject *table, \
- const char *errors)
+.. c:function:: PyObject* PyUnicode_Translate(PyObject *str, PyObject *table, const char *errors)
Translate a string by applying a character mapping table to it and return the
resulting Unicode object.
@@ -1596,7 +1025,7 @@ They all return ``NULL`` or ``-1`` if an exception occurs.
and sequences work well. Unmapped character ordinals (ones which cause a
:exc:`LookupError`) are left untouched and are copied as-is.
- *errors* has the usual meaning for codecs. It may be ``NULL`` which indicates to
+ *errors* has the usual meaning for codecs. It may be *NULL* which indicates to
use the default error handling.
@@ -1606,16 +1035,19 @@ They all return ``NULL`` or ``-1`` if an exception occurs.
Unicode string.
-.. c:function:: Py_ssize_t PyUnicode_Tailmatch(PyObject *str, PyObject *substr, \
- Py_ssize_t start, Py_ssize_t end, int direction)
+.. c:function:: 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.
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` type for *start* and *end*. This
+ might require changes in your code for properly supporting 64-bit
+ systems.
-.. c:function:: Py_ssize_t PyUnicode_Find(PyObject *str, PyObject *substr, \
- Py_ssize_t start, Py_ssize_t end, int direction)
+
+.. c:function:: 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
@@ -1623,64 +1055,52 @@ They all return ``NULL`` or ``-1`` if an exception occurs.
``-1`` indicates that no match was found, and ``-2`` indicates that an error
occurred and an exception has been set.
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` type for *start* and *end*. This
+ might require changes in your code for properly supporting 64-bit
+ systems.
-.. c:function:: Py_ssize_t PyUnicode_FindChar(PyObject *str, Py_UCS4 ch, \
- Py_ssize_t start, Py_ssize_t end, int direction)
-
- Return the first position of the character *ch* 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.
-
- .. versionadded:: 3.3
- .. versionchanged:: 3.7
- *start* and *end* are now adjusted to behave like ``str[start:end]``.
-
-
-.. c:function:: Py_ssize_t PyUnicode_Count(PyObject *str, PyObject *substr, \
- Py_ssize_t start, Py_ssize_t end)
+.. c:function:: 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.
+ .. versionchanged:: 2.5
+ This function returned an :c:type:`int` type and used an :c:type:`int`
+ type for *start* and *end*. This might require changes in your code for
+ properly supporting 64-bit systems.
+
-.. c:function:: PyObject* PyUnicode_Replace(PyObject *str, PyObject *substr, \
- PyObject *replstr, Py_ssize_t maxcount)
+.. c:function:: PyObject* PyUnicode_Replace(PyObject *str, PyObject *substr, PyObject *replstr, Py_ssize_t maxcount)
Replace at most *maxcount* occurrences of *substr* in *str* with *replstr* and
return the resulting Unicode object. *maxcount* == ``-1`` means replace all
occurrences.
+ .. versionchanged:: 2.5
+ This function used an :c:type:`int` type for *maxcount*. This might
+ require changes in your code for properly supporting 64-bit systems.
+
.. c:function:: int PyUnicode_Compare(PyObject *left, PyObject *right)
Compare two strings and return ``-1``, ``0``, ``1`` for less than, equal, and greater than,
respectively.
- This function returns ``-1`` upon failure, so one should call
- :c:func:`PyErr_Occurred` to check for errors.
-
-.. c:function:: int PyUnicode_CompareWithASCIIString(PyObject *uni, const char *string)
+.. c:function:: int PyUnicode_RichCompare(PyObject *left, PyObject *right, int op)
- Compare a Unicode object, *uni*, with *string* and return ``-1``, ``0``, ``1`` for less
- than, equal, and greater than, respectively. It is best to pass only
- ASCII-encoded strings, but the function interprets the input string as
- ISO-8859-1 if it contains non-ASCII characters.
-
- This function does not raise exceptions.
-
-
-.. c:function:: PyObject* PyUnicode_RichCompare(PyObject *left, PyObject *right, int op)
-
- Rich compare two Unicode strings and return one of the following:
+ Rich compare two unicode strings and return one of the following:
* ``NULL`` in case an exception was raised
* :const:`Py_True` or :const:`Py_False` for successful comparisons
* :const:`Py_NotImplemented` in case the type combination is unknown
+ Note that :const:`Py_EQ` and :const:`Py_NE` comparisons can cause a
+ :exc:`UnicodeWarning` in case the conversion of the arguments to Unicode fails
+ with a :exc:`UnicodeDecodeError`.
+
Possible values for *op* are :const:`Py_GT`, :const:`Py_GE`, :const:`Py_EQ`,
:const:`Py_NE`, :const:`Py_LT`, and :const:`Py_LE`.
@@ -1696,26 +1116,5 @@ They all return ``NULL`` or ``-1`` if an exception occurs.
Check whether *element* is contained in *container* and return true or false
accordingly.
- *element* has to coerce to a one element Unicode string. ``-1`` is returned
- if there was an error.
-
-
-.. c:function:: void PyUnicode_InternInPlace(PyObject **string)
-
- Intern the argument *\*string* in place. The argument must be the address of a
- pointer variable pointing to a Python Unicode string object. If there is an
- existing interned string that is the same as *\*string*, it sets *\*string* to
- it (decrementing the reference count of the old string object and incrementing
- the reference count of the interned string object), otherwise it leaves
- *\*string* alone and interns it (incrementing its reference count).
- (Clarification: even though there is a lot of talk about reference counts, think
- of this function as reference-count-neutral; you own the object after the call
- if and only if you owned it before the call.)
-
-
-.. c:function:: PyObject* PyUnicode_InternFromString(const char *v)
-
- A combination of :c:func:`PyUnicode_FromString` and
- :c:func:`PyUnicode_InternInPlace`, returning either a new Unicode string
- object that has been interned, or a new ("owned") reference to an earlier
- interned string object with the same value.
+ *element* has to coerce to a one element Unicode string. ``-1`` is returned if
+ there was an error.
diff --git a/Doc/c-api/utilities.rst b/Doc/c-api/utilities.rst
index a805b56..f43933b 100644
--- a/Doc/c-api/utilities.rst
+++ b/Doc/c-api/utilities.rst
@@ -1,4 +1,5 @@
-.. highlight:: c
+.. highlightlang:: c
+
.. _utilities:
diff --git a/Doc/c-api/veryhigh.rst b/Doc/c-api/veryhigh.rst
index d6aecc3..9e54c14 100644
--- a/Doc/c-api/veryhigh.rst
+++ b/Doc/c-api/veryhigh.rst
@@ -1,4 +1,4 @@
-.. highlight:: c
+.. highlightlang:: c
.. _veryhigh:
@@ -25,34 +25,26 @@ are only passed to these functions if it is certain that they were created by
the same library that the Python runtime is using.
-.. c:function:: int Py_Main(int argc, wchar_t **argv)
+.. c:function:: 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 :c:func:`main`
- function (converted to wchar_t according to the user's locale). 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 (i.e., 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.
+ 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 :exc:`SystemExit` is raised, this
function will not return ``1``, but exit the process, as long as
``Py_InspectFlag`` is not set.
-.. c:function:: int Py_BytesMain(int argc, char **argv)
-
- Similar to :c:func:`Py_Main` but *argv* is an array of bytes strings.
-
- .. versionadded:: 3.8
-
-
.. c:function:: int PyRun_AnyFile(FILE *fp, const char *filename)
This is a simplified interface to :c:func:`PyRun_AnyFileExFlags` below, leaving
- *closeit* set to ``0`` and *flags* set to ``NULL``.
+ *closeit* set to ``0`` and *flags* set to *NULL*.
.. c:function:: int PyRun_AnyFileFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
@@ -64,7 +56,7 @@ the same library that the Python runtime is using.
.. c:function:: int PyRun_AnyFileEx(FILE *fp, const char *filename, int closeit)
This is a simplified interface to :c:func:`PyRun_AnyFileExFlags` below, leaving
- the *flags* argument set to ``NULL``.
+ the *flags* argument set to *NULL*.
.. c:function:: int PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit, PyCompilerFlags *flags)
@@ -72,15 +64,14 @@ the same library that the Python runtime is using.
If *fp* refers to a file associated with an interactive device (console or
terminal input or Unix pseudo-terminal), return the value of
:c:func:`PyRun_InteractiveLoop`, otherwise return the result of
- :c:func:`PyRun_SimpleFile`. *filename* is decoded from the filesystem
- encoding (:func:`sys.getfilesystemencoding`). If *filename* is ``NULL``, this
- function uses ``"???"`` as the filename.
+ :c:func:`PyRun_SimpleFile`. If *filename* is *NULL*, this function uses
+ ``"???"`` as the filename.
.. c:function:: int PyRun_SimpleString(const char *command)
This is a simplified interface to :c:func:`PyRun_SimpleStringFlags` below,
- leaving the :c:type:`PyCompilerFlags`\* argument set to ``NULL``.
+ leaving the *PyCompilerFlags\** argument set to NULL.
.. c:function:: int PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
@@ -99,42 +90,40 @@ the same library that the Python runtime is using.
.. c:function:: int PyRun_SimpleFile(FILE *fp, const char *filename)
This is a simplified interface to :c:func:`PyRun_SimpleFileExFlags` below,
- leaving *closeit* set to ``0`` and *flags* set to ``NULL``.
+ leaving *closeit* set to ``0`` and *flags* set to *NULL*.
+
+
+.. c:function:: int PyRun_SimpleFileFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
+
+ This is a simplified interface to :c:func:`PyRun_SimpleFileExFlags` below,
+ leaving *closeit* set to ``0``.
.. c:function:: int PyRun_SimpleFileEx(FILE *fp, const char *filename, int closeit)
This is a simplified interface to :c:func:`PyRun_SimpleFileExFlags` below,
- leaving *flags* set to ``NULL``.
+ leaving *flags* set to *NULL*.
.. c:function:: int PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit, PyCompilerFlags *flags)
Similar to :c:func:`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, it is decoded from the filesystem encoding
- (:func:`sys.getfilesystemencoding`). If *closeit* is true, the file is
- closed before PyRun_SimpleFileExFlags returns.
-
- .. note::
- On Windows, *fp* should be opened as binary mode (e.g. ``fopen(filename, "rb")``).
- Otherwise, Python may not handle script file with LF line ending correctly.
+ 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.
.. c:function:: int PyRun_InteractiveOne(FILE *fp, const char *filename)
This is a simplified interface to :c:func:`PyRun_InteractiveOneFlags` below,
- leaving *flags* set to ``NULL``.
+ leaving *flags* set to *NULL*.
.. c:function:: 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``. *filename* is decoded from the
- filesystem encoding (:func:`sys.getfilesystemencoding`).
-
- Returns ``0`` when the input was
+ 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 :file:`errcode.h` include file distributed as part of Python if
there was a parse error. (Note that :file:`errcode.h` is not included by
@@ -144,61 +133,28 @@ the same library that the Python runtime is using.
.. c:function:: int PyRun_InteractiveLoop(FILE *fp, const char *filename)
This is a simplified interface to :c:func:`PyRun_InteractiveLoopFlags` below,
- leaving *flags* set to ``NULL``.
+ leaving *flags* set to *NULL*.
.. c:function:: 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``. *filename* is decoded from the filesystem encoding
- (:func:`sys.getfilesystemencoding`). Returns ``0`` at EOF or a negative
- number upon failure.
-
-
-.. c:var:: int (*PyOS_InputHook)(void)
-
- Can be set to point to a function with the prototype
- ``int func(void)``. The function will be called when Python's
- interpreter prompt is about to become idle and wait for user input
- from the terminal. The return value is ignored. Overriding this
- hook can be used to integrate the interpreter's prompt with other
- event loops, as done in the :file:`Modules/_tkinter.c` in the
- Python source code.
-
-
-.. c:var:: char* (*PyOS_ReadlineFunctionPointer)(FILE *, FILE *, const char *)
-
- Can be set to point to a function with the prototype
- ``char *func(FILE *stdin, FILE *stdout, char *prompt)``,
- overriding the default function used to read a single line of input
- at the interpreter's prompt. The function is expected to output
- the string *prompt* if it's not ``NULL``, and then read a line of
- input from the provided standard input file, returning the
- resulting string. For example, The :mod:`readline` module sets
- this hook to provide line-editing and tab-completion features.
-
- The result must be a string allocated by :c:func:`PyMem_RawMalloc` or
- :c:func:`PyMem_RawRealloc`, or ``NULL`` if an error occurred.
-
- .. versionchanged:: 3.4
- The result must be allocated by :c:func:`PyMem_RawMalloc` or
- :c:func:`PyMem_RawRealloc`, instead of being allocated by
- :c:func:`PyMem_Malloc` or :c:func:`PyMem_Realloc`.
+ ``sys.ps2``. Returns ``0`` at EOF.
.. c:function:: struct _node* PyParser_SimpleParseString(const char *str, int start)
This is a simplified interface to
:c:func:`PyParser_SimpleParseStringFlagsFilename` below, leaving *filename* set
- to ``NULL`` and *flags* set to ``0``.
+ to *NULL* and *flags* set to ``0``.
.. c:function:: struct _node* PyParser_SimpleParseStringFlags( const char *str, int start, int flags)
This is a simplified interface to
:c:func:`PyParser_SimpleParseStringFlagsFilename` below, leaving *filename* set
- to ``NULL``.
+ to *NULL*.
.. c:function:: struct _node* PyParser_SimpleParseStringFlagsFilename( const char *str, const char *filename, int start, int flags)
@@ -206,8 +162,7 @@ the same library that the Python runtime is using.
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. *filename* is decoded from the filesystem encoding
- (:func:`sys.getfilesystemencoding`).
+ many times.
.. c:function:: struct _node* PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
@@ -225,31 +180,30 @@ the same library that the Python runtime is using.
.. c:function:: PyObject* PyRun_String(const char *str, int start, PyObject *globals, PyObject *locals)
This is a simplified interface to :c:func:`PyRun_StringFlags` below, leaving
- *flags* set to ``NULL``.
+ *flags* set to *NULL*.
.. c:function:: PyObject* PyRun_StringFlags(const char *str, int start, PyObject *globals, PyObject *locals, PyCompilerFlags *flags)
Execute Python source code from *str* in the context specified by the
- objects *globals* and *locals* with the compiler flags specified by
- *flags*. *globals* must be a dictionary; *locals* can be any object
- that implements the mapping protocol. The parameter *start* specifies
- the start token that should be used to parse the source code.
+ dictionaries *globals* and *locals* with the compiler flags specified by
+ *flags*. The parameter *start* specifies the start token that should be used to
+ parse the source code.
- Returns the result of executing the code as a Python object, or ``NULL`` if an
+ Returns the result of executing the code as a Python object, or *NULL* if an
exception was raised.
.. c:function:: PyObject* PyRun_File(FILE *fp, const char *filename, int start, PyObject *globals, PyObject *locals)
This is a simplified interface to :c:func:`PyRun_FileExFlags` below, leaving
- *closeit* set to ``0`` and *flags* set to ``NULL``.
+ *closeit* set to ``0`` and *flags* set to *NULL*.
.. c:function:: PyObject* PyRun_FileEx(FILE *fp, const char *filename, int start, PyObject *globals, PyObject *locals, int closeit)
This is a simplified interface to :c:func:`PyRun_FileExFlags` below, leaving
- *flags* set to ``NULL``.
+ *flags* set to *NULL*.
.. c:function:: PyObject* PyRun_FileFlags(FILE *fp, const char *filename, int start, PyObject *globals, PyObject *locals, PyCompilerFlags *flags)
@@ -261,8 +215,7 @@ the same library that the Python runtime is using.
.. c:function:: PyObject* PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals, PyObject *locals, int closeit, PyCompilerFlags *flags)
Similar to :c:func:`PyRun_StringFlags`, but the Python source code is read from
- *fp* instead of an in-memory string. *filename* should be the name of the file,
- it is decoded from the filesystem encoding (:func:`sys.getfilesystemencoding`).
+ *fp* instead of an in-memory string. *filename* should be the name of the file.
If *closeit* is true, the file is closed before :c:func:`PyRun_FileExFlags`
returns.
@@ -270,81 +223,49 @@ the same library that the Python runtime is using.
.. c:function:: PyObject* Py_CompileString(const char *str, const char *filename, int start)
This is a simplified interface to :c:func:`Py_CompileStringFlags` below, leaving
- *flags* set to ``NULL``.
+ *flags* set to *NULL*.
.. c:function:: PyObject* Py_CompileStringFlags(const char *str, const char *filename, int start, PyCompilerFlags *flags)
- This is a simplified interface to :c:func:`Py_CompileStringExFlags` below, with
- *optimize* set to ``-1``.
-
-
-.. c:function:: PyObject* Py_CompileStringObject(const char *str, PyObject *filename, int start, PyCompilerFlags *flags, int optimize)
-
Parse and compile the Python source code in *str*, returning the resulting code
object. The start token is given by *start*; this can be used to constrain the
code which can be compiled and should be :const:`Py_eval_input`,
:const:`Py_file_input`, or :const:`Py_single_input`. The filename specified by
*filename* is used to construct the code object and may appear in tracebacks or
- :exc:`SyntaxError` exception messages. This returns ``NULL`` if the code
- cannot be parsed or compiled.
-
- The integer *optimize* specifies the optimization level of the compiler; a
- value of ``-1`` selects the optimization level of the interpreter as given by
- :option:`-O` options. Explicit levels are ``0`` (no optimization;
- ``__debug__`` is true), ``1`` (asserts are removed, ``__debug__`` is false)
- or ``2`` (docstrings are removed too).
-
- .. versionadded:: 3.4
+ :exc:`SyntaxError` exception messages. This returns *NULL* if the code cannot
+ be parsed or compiled.
-.. c:function:: PyObject* Py_CompileStringExFlags(const char *str, const char *filename, int start, PyCompilerFlags *flags, int optimize)
-
- Like :c:func:`Py_CompileStringObject`, but *filename* is a byte string
- decoded from the filesystem encoding (:func:`os.fsdecode`).
-
- .. versionadded:: 3.2
-
-.. c:function:: PyObject* PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
+.. c:function:: PyObject* PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
This is a simplified interface to :c:func:`PyEval_EvalCodeEx`, with just
- the code object, and global and local variables. The other arguments are
- set to ``NULL``.
+ the code object, and the dictionaries of global and local variables.
+ The other arguments are set to *NULL*.
-.. c:function:: PyObject* PyEval_EvalCodeEx(PyObject *co, PyObject *globals, PyObject *locals, PyObject *const *args, int argcount, PyObject *const *kws, int kwcount, PyObject *const *defs, int defcount, PyObject *kwdefs, PyObject *closure)
+.. c:function:: PyObject* PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals, PyObject **args, int argcount, PyObject **kws, int kwcount, PyObject **defs, int defcount, PyObject *closure)
Evaluate a precompiled code object, given a particular environment for its
- evaluation. This environment consists of a dictionary of global variables,
- a mapping object of local variables, arrays of arguments, keywords and
- defaults, a dictionary of default values for :ref:`keyword-only
- <keyword-only_parameter>` arguments and a closure tuple of cells.
-
-
-.. c:type:: PyFrameObject
-
- The C structure of the objects used to describe frame objects. The
- fields of this type are subject to change at any time.
+ evaluation. This environment consists of dictionaries of global and local
+ variables, arrays of arguments, keywords and defaults, and a closure tuple of
+ cells.
.. c:function:: PyObject* PyEval_EvalFrame(PyFrameObject *f)
Evaluate an execution frame. This is a simplified interface to
- :c:func:`PyEval_EvalFrameEx`, for backward compatibility.
+ PyEval_EvalFrameEx, for backward compatibility.
.. c:function:: PyObject* PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
- This is the main, unvarnished function of Python interpretation. The 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 :meth:`~generator.throw`
- methods of generator objects.
-
- .. versionchanged:: 3.4
- This function now includes a debug assertion to help ensure that it
- does not silently discard an active exception.
+ This is the main, unvarnished function of Python interpretation. It is
+ literally 2000 lines long. The 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
+ :meth:`~generator.throw` methods of generator objects.
.. c:function:: int PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
@@ -386,27 +307,17 @@ the same library that the Python runtime is using.
executed, it is passed as ``PyCompilerFlags *flags``. In this case, ``from
__future__ import`` can modify *flags*.
- Whenever ``PyCompilerFlags *flags`` is ``NULL``, :attr:`cf_flags` is treated as
+ Whenever ``PyCompilerFlags *flags`` is *NULL*, :attr:`cf_flags` is treated as
equal to ``0``, and any modification due to ``from __future__ import`` is
- discarded.
-
- .. c:member:: int cf_flags
-
- Compiler flags.
+ discarded. ::
- .. c:member:: int cf_feature_version
-
- *cf_feature_version* is the minor Python version. It should be
- initialized to ``PY_MINOR_VERSION``.
-
- The field is ignored by default, it is used if and only if
- ``PyCF_ONLY_AST`` flag is set in *cf_flags*.
-
- .. versionchanged:: 3.8
- Added *cf_feature_version* field.
+ struct PyCompilerFlags {
+ int cf_flags;
+ }
.. c:var:: 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`.
+
diff --git a/Doc/c-api/weakref.rst b/Doc/c-api/weakref.rst
index e3a9bda..243cfd1 100644
--- a/Doc/c-api/weakref.rst
+++ b/Doc/c-api/weakref.rst
@@ -1,4 +1,4 @@
-.. highlight:: c
+.. highlightlang:: c
.. _weakrefobjects:
@@ -15,16 +15,22 @@ as much as it can.
Return true if *ob* is either a reference or proxy object.
+ .. versionadded:: 2.2
+
.. c:function:: int PyWeakref_CheckRef(ob)
Return true if *ob* is a reference object.
+ .. versionadded:: 2.2
+
.. c:function:: int PyWeakref_CheckProxy(ob)
Return true if *ob* is a proxy object.
+ .. versionadded:: 2.2
+
.. c:function:: PyObject* PyWeakref_NewRef(PyObject *ob, PyObject *callback)
@@ -33,9 +39,11 @@ as much as it can.
reference object may be returned. The second parameter, *callback*, can be a
callable object that receives notification when *ob* is garbage collected; it
should accept a single parameter, which will be the weak reference object
- itself. *callback* may also be ``None`` or ``NULL``. If *ob* is not a
+ itself. *callback* may also be ``None`` or *NULL*. If *ob* is not a
weakly-referencable object, or if *callback* is not callable, ``None``, or
- ``NULL``, this will return ``NULL`` and raise :exc:`TypeError`.
+ *NULL*, this will return *NULL* and raise :exc:`TypeError`.
+
+ .. versionadded:: 2.2
.. c:function:: PyObject* PyWeakref_NewProxy(PyObject *ob, PyObject *callback)
@@ -45,9 +53,11 @@ as much as it can.
existing proxy object may be returned. The second parameter, *callback*, can
be a callable object that receives notification when *ob* is garbage
collected; it should accept a single parameter, which will be the weak
- reference object itself. *callback* may also be ``None`` or ``NULL``. If *ob*
+ reference object itself. *callback* may also be ``None`` or *NULL*. If *ob*
is not a weakly-referencable object, or if *callback* is not callable,
- ``None``, or ``NULL``, this will return ``NULL`` and raise :exc:`TypeError`.
+ ``None``, or *NULL*, this will return *NULL* and raise :exc:`TypeError`.
+
+ .. versionadded:: 2.2
.. c:function:: PyObject* PyWeakref_GetObject(PyObject *ref)
@@ -55,7 +65,9 @@ as much as it can.
Return the referenced object from a weak reference, *ref*. If the referent is
no longer live, returns :const:`Py_None`.
- .. note::
+ .. versionadded:: 2.2
+
+ .. warning::
This function returns a **borrowed reference** to the referenced object.
This means that you should always call :c:func:`Py_INCREF` on the object
@@ -67,3 +79,5 @@ as much as it can.
Similar to :c:func:`PyWeakref_GetObject`, but implemented as a macro that does no
error checking.
+
+ .. versionadded:: 2.2