Upload 280 files
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- python-3.12.6-docs-text/python-3.12.6-docs-text/about.txt +33 -0
- python-3.12.6-docs-text/python-3.12.6-docs-text/bugs.txt +111 -0
- python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/abstract.txt +62 -0
- python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/allocation.txt +64 -0
- python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/apiabiversion.txt +69 -0
- python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/arg.txt +672 -0
- python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/bool.txt +46 -0
- python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/buffer.txt +520 -0
- python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/bytearray.txt +85 -0
- python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/bytes.txt +188 -0
- python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/call.txt +416 -0
- python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/capsule.txt +156 -0
- python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/cell.txt +58 -0
- python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/code.txt +263 -0
- python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/codec.txt +152 -0
- python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/complex.txt +136 -0
- python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/concrete.txt +193 -0
- python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/contextvars.txt +135 -0
- python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/conversion.txt +162 -0
- python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/coro.txt +28 -0
- python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/datetime.txt +299 -0
- python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/descriptor.txt +34 -0
- python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/dict.txt +325 -0
- python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/exceptions.txt +1173 -0
- python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/file.txt +97 -0
- python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/float.txt +160 -0
- python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/frame.txt +161 -0
- python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/function.txt +178 -0
- python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/gcsupport.txt +282 -0
- python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/gen.txt +40 -0
- python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/hash.txt +42 -0
- python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/import.txt +311 -0
- python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/index.txt +218 -0
- python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/init.txt +2115 -0
- python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/init_config.txt +1652 -0
- python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/intro.txt +784 -0
- python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/iter.txt +76 -0
- python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/iterator.txt +47 -0
- python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/list.txt +133 -0
- python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/long.txt +331 -0
- python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/mapping.txt +96 -0
- python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/marshal.txt +92 -0
- python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/memory.txt +752 -0
- python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/memoryview.txt +70 -0
- python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/method.txt +82 -0
- python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/module.txt +668 -0
- python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/none.txt +18 -0
- python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/number.txt +297 -0
- python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/objbuffer.txt +54 -0
- python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/object.txt +437 -0
python-3.12.6-docs-text/python-3.12.6-docs-text/about.txt
ADDED
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
About these documents
|
2 |
+
*********************
|
3 |
+
|
4 |
+
These documents are generated from reStructuredText sources by Sphinx,
|
5 |
+
a document processor specifically written for the Python
|
6 |
+
documentation.
|
7 |
+
|
8 |
+
Development of the documentation and its toolchain is an entirely
|
9 |
+
volunteer effort, just like Python itself. If you want to contribute,
|
10 |
+
please take a look at the Dealing with Bugs page for information on
|
11 |
+
how to do so. New volunteers are always welcome!
|
12 |
+
|
13 |
+
Many thanks go to:
|
14 |
+
|
15 |
+
* Fred L. Drake, Jr., the creator of the original Python documentation
|
16 |
+
toolset and writer of much of the content;
|
17 |
+
|
18 |
+
* the Docutils project for creating reStructuredText and the Docutils
|
19 |
+
suite;
|
20 |
+
|
21 |
+
* Fredrik Lundh for his Alternative Python Reference project from
|
22 |
+
which Sphinx got many good ideas.
|
23 |
+
|
24 |
+
|
25 |
+
Contributors to the Python Documentation
|
26 |
+
========================================
|
27 |
+
|
28 |
+
Many people have contributed to the Python language, the Python
|
29 |
+
standard library, and the Python documentation. See Misc/ACKS in the
|
30 |
+
Python source distribution for a partial list of contributors.
|
31 |
+
|
32 |
+
It is only with the input and contributions of the Python community
|
33 |
+
that Python has such wonderful documentation -- Thank You!
|
python-3.12.6-docs-text/python-3.12.6-docs-text/bugs.txt
ADDED
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Dealing with Bugs
|
2 |
+
*****************
|
3 |
+
|
4 |
+
Python is a mature programming language which has established a
|
5 |
+
reputation for stability. In order to maintain this reputation, the
|
6 |
+
developers would like to know of any deficiencies you find in Python.
|
7 |
+
|
8 |
+
It can be sometimes faster to fix bugs yourself and contribute patches
|
9 |
+
to Python as it streamlines the process and involves less people.
|
10 |
+
Learn how to contribute.
|
11 |
+
|
12 |
+
|
13 |
+
Documentation bugs
|
14 |
+
==================
|
15 |
+
|
16 |
+
If you find a bug in this documentation or would like to propose an
|
17 |
+
improvement, please submit a bug report on the tracker. If you have a
|
18 |
+
suggestion on how to fix it, include that as well.
|
19 |
+
|
20 |
+
You can also open a discussion item on our Documentation Discourse
|
21 |
+
forum.
|
22 |
+
|
23 |
+
If you find a bug in the theme (HTML / CSS / JavaScript) of the
|
24 |
+
documentation, please submit a bug report on the python-doc-theme bug
|
25 |
+
tracker.
|
26 |
+
|
27 |
+
If you're short on time, you can also email documentation bug reports
|
28 |
+
to [email protected] (behavioral bugs can be sent to python-
|
29 |
+
[email protected]). 'docs@' is a mailing list run by volunteers; your
|
30 |
+
request will be noticed, though it may take a while to be processed.
|
31 |
+
|
32 |
+
See also:
|
33 |
+
|
34 |
+
Documentation bugs
|
35 |
+
A list of documentation bugs that have been submitted to the
|
36 |
+
Python issue tracker.
|
37 |
+
|
38 |
+
Issue Tracking
|
39 |
+
Overview of the process involved in reporting an improvement on
|
40 |
+
the tracker.
|
41 |
+
|
42 |
+
Helping with Documentation
|
43 |
+
Comprehensive guide for individuals that are interested in
|
44 |
+
contributing to Python documentation.
|
45 |
+
|
46 |
+
Documentation Translations
|
47 |
+
A list of GitHub pages for documentation translation and their
|
48 |
+
primary contacts.
|
49 |
+
|
50 |
+
|
51 |
+
Using the Python issue tracker
|
52 |
+
==============================
|
53 |
+
|
54 |
+
Issue reports for Python itself should be submitted via the GitHub
|
55 |
+
issues tracker (https://github.com/python/cpython/issues). The GitHub
|
56 |
+
issues tracker offers a web form which allows pertinent information to
|
57 |
+
be entered and submitted to the developers.
|
58 |
+
|
59 |
+
The first step in filing a report is to determine whether the problem
|
60 |
+
has already been reported. The advantage in doing so, aside from
|
61 |
+
saving the developers' time, is that you learn what has been done to
|
62 |
+
fix it; it may be that the problem has already been fixed for the next
|
63 |
+
release, or additional information is needed (in which case you are
|
64 |
+
welcome to provide it if you can!). To do this, search the tracker
|
65 |
+
using the search box at the top of the page.
|
66 |
+
|
67 |
+
If the problem you're reporting is not already in the list, log in to
|
68 |
+
GitHub. If you don't already have a GitHub account, create a new
|
69 |
+
account using the "Sign up" link. It is not possible to submit a bug
|
70 |
+
report anonymously.
|
71 |
+
|
72 |
+
Being now logged in, you can submit an issue. Click on the "New issue"
|
73 |
+
button in the top bar to report a new issue.
|
74 |
+
|
75 |
+
The submission form has two fields, "Title" and "Comment".
|
76 |
+
|
77 |
+
For the "Title" field, enter a *very* short description of the
|
78 |
+
problem; fewer than ten words is good.
|
79 |
+
|
80 |
+
In the "Comment" field, describe the problem in detail, including what
|
81 |
+
you expected to happen and what did happen. Be sure to include
|
82 |
+
whether any extension modules were involved, and what hardware and
|
83 |
+
software platform you were using (including version information as
|
84 |
+
appropriate).
|
85 |
+
|
86 |
+
Each issue report will be reviewed by a developer who will determine
|
87 |
+
what needs to be done to correct the problem. You will receive an
|
88 |
+
update each time an action is taken on the issue.
|
89 |
+
|
90 |
+
See also:
|
91 |
+
|
92 |
+
How to Report Bugs Effectively
|
93 |
+
Article which goes into some detail about how to create a useful
|
94 |
+
bug report. This describes what kind of information is useful and
|
95 |
+
why it is useful.
|
96 |
+
|
97 |
+
Bug Writing Guidelines
|
98 |
+
Information about writing a good bug report. Some of this is
|
99 |
+
specific to the Mozilla project, but describes general good
|
100 |
+
practices.
|
101 |
+
|
102 |
+
|
103 |
+
Getting started contributing to Python yourself
|
104 |
+
===============================================
|
105 |
+
|
106 |
+
Beyond just reporting bugs that you find, you are also welcome to
|
107 |
+
submit patches to fix them. You can find more information on how to
|
108 |
+
get started patching Python in the Python Developer's Guide. If you
|
109 |
+
have questions, the core-mentorship mailing list is a friendly place
|
110 |
+
to get answers to any and all questions pertaining to the process of
|
111 |
+
fixing issues in Python.
|
python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/abstract.txt
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Abstract Objects Layer
|
2 |
+
**********************
|
3 |
+
|
4 |
+
The functions in this chapter interact with Python objects regardless
|
5 |
+
of their type, or with wide classes of object types (e.g. all
|
6 |
+
numerical types, or all sequence types). When used on object types
|
7 |
+
for which they do not apply, they will raise a Python exception.
|
8 |
+
|
9 |
+
It is not possible to use these functions on objects that are not
|
10 |
+
properly initialized, such as a list object that has been created by
|
11 |
+
"PyList_New()", but whose items have not been set to some non-"NULL"
|
12 |
+
value yet.
|
13 |
+
|
14 |
+
* Object Protocol
|
15 |
+
|
16 |
+
* Call Protocol
|
17 |
+
|
18 |
+
* The *tp_call* Protocol
|
19 |
+
|
20 |
+
* The Vectorcall Protocol
|
21 |
+
|
22 |
+
* Recursion Control
|
23 |
+
|
24 |
+
* Vectorcall Support API
|
25 |
+
|
26 |
+
* Object Calling API
|
27 |
+
|
28 |
+
* Call Support API
|
29 |
+
|
30 |
+
* Number Protocol
|
31 |
+
|
32 |
+
* Sequence Protocol
|
33 |
+
|
34 |
+
* Mapping Protocol
|
35 |
+
|
36 |
+
* Iterator Protocol
|
37 |
+
|
38 |
+
* Buffer Protocol
|
39 |
+
|
40 |
+
* Buffer structure
|
41 |
+
|
42 |
+
* Buffer request types
|
43 |
+
|
44 |
+
* request-independent fields
|
45 |
+
|
46 |
+
* readonly, format
|
47 |
+
|
48 |
+
* shape, strides, suboffsets
|
49 |
+
|
50 |
+
* contiguity requests
|
51 |
+
|
52 |
+
* compound requests
|
53 |
+
|
54 |
+
* Complex arrays
|
55 |
+
|
56 |
+
* NumPy-style: shape and strides
|
57 |
+
|
58 |
+
* PIL-style: shape, strides and suboffsets
|
59 |
+
|
60 |
+
* Buffer-related functions
|
61 |
+
|
62 |
+
* Old Buffer Protocol
|
python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/allocation.txt
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Allocating Objects on the Heap
|
2 |
+
******************************
|
3 |
+
|
4 |
+
PyObject *_PyObject_New(PyTypeObject *type)
|
5 |
+
*Return value: New reference.*
|
6 |
+
|
7 |
+
PyVarObject *_PyObject_NewVar(PyTypeObject *type, Py_ssize_t size)
|
8 |
+
*Return value: New reference.*
|
9 |
+
|
10 |
+
PyObject *PyObject_Init(PyObject *op, PyTypeObject *type)
|
11 |
+
*Return value: Borrowed reference.** Part of the Stable ABI.*
|
12 |
+
|
13 |
+
Initialize a newly allocated object *op* with its type and initial
|
14 |
+
reference. Returns the initialized object. If *type* indicates
|
15 |
+
that the object participates in the cyclic garbage detector, it is
|
16 |
+
added to the detector's set of observed objects. Other fields of
|
17 |
+
the object are not affected.
|
18 |
+
|
19 |
+
PyVarObject *PyObject_InitVar(PyVarObject *op, PyTypeObject *type, Py_ssize_t size)
|
20 |
+
*Return value: Borrowed reference.** Part of the Stable ABI.*
|
21 |
+
|
22 |
+
This does everything "PyObject_Init()" does, and also initializes
|
23 |
+
the length information for a variable-size object.
|
24 |
+
|
25 |
+
PyObject_New(TYPE, typeobj)
|
26 |
+
|
27 |
+
Allocate a new Python object using the C structure type *TYPE* and
|
28 |
+
the Python type object *typeobj* ("PyTypeObject*"). Fields not
|
29 |
+
defined by the Python object header are not initialized. The caller
|
30 |
+
will own the only reference to the object (i.e. its reference count
|
31 |
+
will be one). The size of the memory allocation is determined from
|
32 |
+
the "tp_basicsize" field of the type object.
|
33 |
+
|
34 |
+
PyObject_NewVar(TYPE, typeobj, size)
|
35 |
+
|
36 |
+
Allocate a new Python object using the C structure type *TYPE* and
|
37 |
+
the Python type object *typeobj* ("PyTypeObject*"). Fields not
|
38 |
+
defined by the Python object header are not initialized. The
|
39 |
+
allocated memory allows for the *TYPE* structure plus *size*
|
40 |
+
("Py_ssize_t") fields of the size given by the "tp_itemsize" field
|
41 |
+
of *typeobj*. This is useful for implementing objects like tuples,
|
42 |
+
which are able to determine their size at construction time.
|
43 |
+
Embedding the array of fields into the same allocation decreases
|
44 |
+
the number of allocations, improving the memory management
|
45 |
+
efficiency.
|
46 |
+
|
47 |
+
void PyObject_Del(void *op)
|
48 |
+
|
49 |
+
Releases memory allocated to an object using "PyObject_New" or
|
50 |
+
"PyObject_NewVar". This is normally called from the "tp_dealloc"
|
51 |
+
handler specified in the object's type. The fields of the object
|
52 |
+
should not be accessed after this call as the memory is no longer a
|
53 |
+
valid Python object.
|
54 |
+
|
55 |
+
PyObject _Py_NoneStruct
|
56 |
+
|
57 |
+
Object which is visible in Python as "None". This should only be
|
58 |
+
accessed using the "Py_None" macro, which evaluates to a pointer to
|
59 |
+
this object.
|
60 |
+
|
61 |
+
See also:
|
62 |
+
|
63 |
+
"PyModule_Create()"
|
64 |
+
To allocate and create extension modules.
|
python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/apiabiversion.txt
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
API and ABI Versioning
|
2 |
+
**********************
|
3 |
+
|
4 |
+
CPython exposes its version number in the following macros. Note that
|
5 |
+
these correspond to the version code is **built** with, not
|
6 |
+
necessarily the version used at **run time**.
|
7 |
+
|
8 |
+
See C API Stability for a discussion of API and ABI stability across
|
9 |
+
versions.
|
10 |
+
|
11 |
+
PY_MAJOR_VERSION
|
12 |
+
|
13 |
+
The "3" in "3.4.1a2".
|
14 |
+
|
15 |
+
PY_MINOR_VERSION
|
16 |
+
|
17 |
+
The "4" in "3.4.1a2".
|
18 |
+
|
19 |
+
PY_MICRO_VERSION
|
20 |
+
|
21 |
+
The "1" in "3.4.1a2".
|
22 |
+
|
23 |
+
PY_RELEASE_LEVEL
|
24 |
+
|
25 |
+
The "a" in "3.4.1a2". This can be "0xA" for alpha, "0xB" for beta,
|
26 |
+
"0xC" for release candidate or "0xF" for final.
|
27 |
+
|
28 |
+
PY_RELEASE_SERIAL
|
29 |
+
|
30 |
+
The "2" in "3.4.1a2". Zero for final releases.
|
31 |
+
|
32 |
+
PY_VERSION_HEX
|
33 |
+
|
34 |
+
The Python version number encoded in a single integer.
|
35 |
+
|
36 |
+
The underlying version information can be found by treating it as a
|
37 |
+
32 bit number in the following manner:
|
38 |
+
|
39 |
+
+---------+---------------------------+---------------------------+----------------------------+
|
40 |
+
| Bytes | Bits (big endian order) | Meaning | Value for "3.4.1a2" |
|
41 |
+
|=========|===========================|===========================|============================|
|
42 |
+
| 1 | 1-8 | "PY_MAJOR_VERSION" | "0x03" |
|
43 |
+
+---------+---------------------------+---------------------------+----------------------------+
|
44 |
+
| 2 | 9-16 | "PY_MINOR_VERSION" | "0x04" |
|
45 |
+
+---------+---------------------------+---------------------------+----------------------------+
|
46 |
+
| 3 | 17-24 | "PY_MICRO_VERSION" | "0x01" |
|
47 |
+
+---------+---------------------------+---------------------------+----------------------------+
|
48 |
+
| 4 | 25-28 | "PY_RELEASE_LEVEL" | "0xA" |
|
49 |
+
| +---------------------------+---------------------------+----------------------------+
|
50 |
+
| | 29-32 | "PY_RELEASE_SERIAL" | "0x2" |
|
51 |
+
+---------+---------------------------+---------------------------+----------------------------+
|
52 |
+
|
53 |
+
Thus "3.4.1a2" is hexversion "0x030401a2" and "3.10.0" is
|
54 |
+
hexversion "0x030a00f0".
|
55 |
+
|
56 |
+
Use this for numeric comparisons, e.g. "#if PY_VERSION_HEX >= ...".
|
57 |
+
|
58 |
+
This version is also available via the symbol "Py_Version".
|
59 |
+
|
60 |
+
const unsigned long Py_Version
|
61 |
+
* Part of the Stable ABI since version 3.11.*
|
62 |
+
|
63 |
+
The Python runtime version number encoded in a single constant
|
64 |
+
integer, with the same format as the "PY_VERSION_HEX" macro. This
|
65 |
+
contains the Python version used at run time.
|
66 |
+
|
67 |
+
Added in version 3.11.
|
68 |
+
|
69 |
+
All the given macros are defined in Include/patchlevel.h.
|
python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/arg.txt
ADDED
@@ -0,0 +1,672 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Parsing arguments and building values
|
2 |
+
*************************************
|
3 |
+
|
4 |
+
These functions are useful when creating your own extensions functions
|
5 |
+
and methods. Additional information and examples are available in
|
6 |
+
Extending and Embedding the Python Interpreter.
|
7 |
+
|
8 |
+
The first three of these functions described, "PyArg_ParseTuple()",
|
9 |
+
"PyArg_ParseTupleAndKeywords()", and "PyArg_Parse()", all use *format
|
10 |
+
strings* which are used to tell the function about the expected
|
11 |
+
arguments. The format strings use the same syntax for each of these
|
12 |
+
functions.
|
13 |
+
|
14 |
+
|
15 |
+
Parsing arguments
|
16 |
+
=================
|
17 |
+
|
18 |
+
A format string consists of zero or more "format units." A format
|
19 |
+
unit describes one Python object; it is usually a single character or
|
20 |
+
a parenthesized sequence of format units. With a few exceptions, a
|
21 |
+
format unit that is not a parenthesized sequence normally corresponds
|
22 |
+
to a single address argument to these functions. In the following
|
23 |
+
description, the quoted form is the format unit; the entry in (round)
|
24 |
+
parentheses is the Python object type that matches the format unit;
|
25 |
+
and the entry in [square] brackets is the type of the C variable(s)
|
26 |
+
whose address should be passed.
|
27 |
+
|
28 |
+
|
29 |
+
Strings and buffers
|
30 |
+
-------------------
|
31 |
+
|
32 |
+
These formats allow accessing an object as a contiguous chunk of
|
33 |
+
memory. You don't have to provide raw storage for the returned unicode
|
34 |
+
or bytes area.
|
35 |
+
|
36 |
+
Unless otherwise stated, buffers are not NUL-terminated.
|
37 |
+
|
38 |
+
There are three ways strings and buffers can be converted to C:
|
39 |
+
|
40 |
+
* Formats such as "y*" and "s*" fill a "Py_buffer" structure. This
|
41 |
+
locks the underlying buffer so that the caller can subsequently use
|
42 |
+
the buffer even inside a "Py_BEGIN_ALLOW_THREADS" block without the
|
43 |
+
risk of mutable data being resized or destroyed. As a result, **you
|
44 |
+
have to call** "PyBuffer_Release()" after you have finished
|
45 |
+
processing the data (or in any early abort case).
|
46 |
+
|
47 |
+
* The "es", "es#", "et" and "et#" formats allocate the result buffer.
|
48 |
+
**You have to call** "PyMem_Free()" after you have finished
|
49 |
+
processing the data (or in any early abort case).
|
50 |
+
|
51 |
+
* Other formats take a "str" or a read-only *bytes-like object*, such
|
52 |
+
as "bytes", and provide a "const char *" pointer to its buffer. In
|
53 |
+
this case the buffer is "borrowed": it is managed by the
|
54 |
+
corresponding Python object, and shares the lifetime of this object.
|
55 |
+
You won't have to release any memory yourself.
|
56 |
+
|
57 |
+
To ensure that the underlying buffer may be safely borrowed, the
|
58 |
+
object's "PyBufferProcs.bf_releasebuffer" field must be "NULL". This
|
59 |
+
disallows common mutable objects such as "bytearray", but also some
|
60 |
+
read-only objects such as "memoryview" of "bytes".
|
61 |
+
|
62 |
+
Besides this "bf_releasebuffer" requirement, there is no check to
|
63 |
+
verify whether the input object is immutable (e.g. whether it would
|
64 |
+
honor a request for a writable buffer, or whether another thread can
|
65 |
+
mutate the data).
|
66 |
+
|
67 |
+
Note:
|
68 |
+
|
69 |
+
For all "#" variants of formats ("s#", "y#", etc.), the macro
|
70 |
+
"PY_SSIZE_T_CLEAN" must be defined before including "Python.h". On
|
71 |
+
Python 3.9 and older, the type of the length argument is
|
72 |
+
"Py_ssize_t" if the "PY_SSIZE_T_CLEAN" macro is defined, or int
|
73 |
+
otherwise.
|
74 |
+
|
75 |
+
"s" ("str") [const char *]
|
76 |
+
Convert a Unicode object to a C pointer to a character string. A
|
77 |
+
pointer to an existing string is stored in the character pointer
|
78 |
+
variable whose address you pass. The C string is NUL-terminated.
|
79 |
+
The Python string must not contain embedded null code points; if it
|
80 |
+
does, a "ValueError" exception is raised. Unicode objects are
|
81 |
+
converted to C strings using "'utf-8'" encoding. If this conversion
|
82 |
+
fails, a "UnicodeError" is raised.
|
83 |
+
|
84 |
+
Note:
|
85 |
+
|
86 |
+
This format does not accept *bytes-like objects*. If you want to
|
87 |
+
accept filesystem paths and convert them to C character strings,
|
88 |
+
it is preferable to use the "O&" format with
|
89 |
+
"PyUnicode_FSConverter()" as *converter*.
|
90 |
+
|
91 |
+
Changed in version 3.5: Previously, "TypeError" was raised when
|
92 |
+
embedded null code points were encountered in the Python string.
|
93 |
+
|
94 |
+
"s*" ("str" or *bytes-like object*) [Py_buffer]
|
95 |
+
This format accepts Unicode objects as well as bytes-like objects.
|
96 |
+
It fills a "Py_buffer" structure provided by the caller. In this
|
97 |
+
case the resulting C string may contain embedded NUL bytes. Unicode
|
98 |
+
objects are converted to C strings using "'utf-8'" encoding.
|
99 |
+
|
100 |
+
"s#" ("str", read-only *bytes-like object*) [const char *,
|
101 |
+
"Py_ssize_t"]
|
102 |
+
Like "s*", except that it provides a borrowed buffer. The result is
|
103 |
+
stored into two C variables, the first one a pointer to a C string,
|
104 |
+
the second one its length. The string may contain embedded null
|
105 |
+
bytes. Unicode objects are converted to C strings using "'utf-8'"
|
106 |
+
encoding.
|
107 |
+
|
108 |
+
"z" ("str" or "None") [const char *]
|
109 |
+
Like "s", but the Python object may also be "None", in which case
|
110 |
+
the C pointer is set to "NULL".
|
111 |
+
|
112 |
+
"z*" ("str", *bytes-like object* or "None") [Py_buffer]
|
113 |
+
Like "s*", but the Python object may also be "None", in which case
|
114 |
+
the "buf" member of the "Py_buffer" structure is set to "NULL".
|
115 |
+
|
116 |
+
"z#" ("str", read-only *bytes-like object* or "None") [const char *,
|
117 |
+
"Py_ssize_t"]
|
118 |
+
Like "s#", but the Python object may also be "None", in which case
|
119 |
+
the C pointer is set to "NULL".
|
120 |
+
|
121 |
+
"y" (read-only *bytes-like object*) [const char *]
|
122 |
+
This format converts a bytes-like object to a C pointer to a
|
123 |
+
borrowed character string; it does not accept Unicode objects. The
|
124 |
+
bytes buffer must not contain embedded null bytes; if it does, a
|
125 |
+
"ValueError" exception is raised.
|
126 |
+
|
127 |
+
Changed in version 3.5: Previously, "TypeError" was raised when
|
128 |
+
embedded null bytes were encountered in the bytes buffer.
|
129 |
+
|
130 |
+
"y*" (*bytes-like object*) [Py_buffer]
|
131 |
+
This variant on "s*" doesn't accept Unicode objects, only bytes-
|
132 |
+
like objects. **This is the recommended way to accept binary
|
133 |
+
data.**
|
134 |
+
|
135 |
+
"y#" (read-only *bytes-like object*) [const char *, "Py_ssize_t"]
|
136 |
+
This variant on "s#" doesn't accept Unicode objects, only bytes-
|
137 |
+
like objects.
|
138 |
+
|
139 |
+
"S" ("bytes") [PyBytesObject *]
|
140 |
+
Requires that the Python object is a "bytes" object, without
|
141 |
+
attempting any conversion. Raises "TypeError" if the object is not
|
142 |
+
a bytes object. The C variable may also be declared as PyObject*.
|
143 |
+
|
144 |
+
"Y" ("bytearray") [PyByteArrayObject *]
|
145 |
+
Requires that the Python object is a "bytearray" object, without
|
146 |
+
attempting any conversion. Raises "TypeError" if the object is not
|
147 |
+
a "bytearray" object. The C variable may also be declared as
|
148 |
+
PyObject*.
|
149 |
+
|
150 |
+
"U" ("str") [PyObject *]
|
151 |
+
Requires that the Python object is a Unicode object, without
|
152 |
+
attempting any conversion. Raises "TypeError" if the object is not
|
153 |
+
a Unicode object. The C variable may also be declared as
|
154 |
+
PyObject*.
|
155 |
+
|
156 |
+
"w*" (read-write *bytes-like object*) [Py_buffer]
|
157 |
+
This format accepts any object which implements the read-write
|
158 |
+
buffer interface. It fills a "Py_buffer" structure provided by the
|
159 |
+
caller. The buffer may contain embedded null bytes. The caller have
|
160 |
+
to call "PyBuffer_Release()" when it is done with the buffer.
|
161 |
+
|
162 |
+
"es" ("str") [const char *encoding, char **buffer]
|
163 |
+
This variant on "s" is used for encoding Unicode into a character
|
164 |
+
buffer. It only works for encoded data without embedded NUL bytes.
|
165 |
+
|
166 |
+
This format requires two arguments. The first is only used as
|
167 |
+
input, and must be a const char* which points to the name of an
|
168 |
+
encoding as a NUL-terminated string, or "NULL", in which case
|
169 |
+
"'utf-8'" encoding is used. An exception is raised if the named
|
170 |
+
encoding is not known to Python. The second argument must be a
|
171 |
+
char**; the value of the pointer it references will be set to a
|
172 |
+
buffer with the contents of the argument text. The text will be
|
173 |
+
encoded in the encoding specified by the first argument.
|
174 |
+
|
175 |
+
"PyArg_ParseTuple()" will allocate a buffer of the needed size,
|
176 |
+
copy the encoded data into this buffer and adjust **buffer* to
|
177 |
+
reference the newly allocated storage. The caller is responsible
|
178 |
+
for calling "PyMem_Free()" to free the allocated buffer after use.
|
179 |
+
|
180 |
+
"et" ("str", "bytes" or "bytearray") [const char *encoding, char
|
181 |
+
**buffer]
|
182 |
+
Same as "es" except that byte string objects are passed through
|
183 |
+
without recoding them. Instead, the implementation assumes that
|
184 |
+
the byte string object uses the encoding passed in as parameter.
|
185 |
+
|
186 |
+
"es#" ("str") [const char *encoding, char **buffer, "Py_ssize_t"
|
187 |
+
*buffer_length]
|
188 |
+
This variant on "s#" is used for encoding Unicode into a character
|
189 |
+
buffer. Unlike the "es" format, this variant allows input data
|
190 |
+
which contains NUL characters.
|
191 |
+
|
192 |
+
It requires three arguments. The first is only used as input, and
|
193 |
+
must be a const char* which points to the name of an encoding as a
|
194 |
+
NUL-terminated string, or "NULL", in which case "'utf-8'" encoding
|
195 |
+
is used. An exception is raised if the named encoding is not known
|
196 |
+
to Python. The second argument must be a char**; the value of the
|
197 |
+
pointer it references will be set to a buffer with the contents of
|
198 |
+
the argument text. The text will be encoded in the encoding
|
199 |
+
specified by the first argument. The third argument must be a
|
200 |
+
pointer to an integer; the referenced integer will be set to the
|
201 |
+
number of bytes in the output buffer.
|
202 |
+
|
203 |
+
There are two modes of operation:
|
204 |
+
|
205 |
+
If **buffer* points a "NULL" pointer, the function will allocate a
|
206 |
+
buffer of the needed size, copy the encoded data into this buffer
|
207 |
+
and set **buffer* to reference the newly allocated storage. The
|
208 |
+
caller is responsible for calling "PyMem_Free()" to free the
|
209 |
+
allocated buffer after usage.
|
210 |
+
|
211 |
+
If **buffer* points to a non-"NULL" pointer (an already allocated
|
212 |
+
buffer), "PyArg_ParseTuple()" will use this location as the buffer
|
213 |
+
and interpret the initial value of **buffer_length* as the buffer
|
214 |
+
size. It will then copy the encoded data into the buffer and NUL-
|
215 |
+
terminate it. If the buffer is not large enough, a "ValueError"
|
216 |
+
will be set.
|
217 |
+
|
218 |
+
In both cases, **buffer_length* is set to the length of the encoded
|
219 |
+
data without the trailing NUL byte.
|
220 |
+
|
221 |
+
"et#" ("str", "bytes" or "bytearray") [const char *encoding, char
|
222 |
+
**buffer, "Py_ssize_t" *buffer_length]
|
223 |
+
Same as "es#" except that byte string objects are passed through
|
224 |
+
without recoding them. Instead, the implementation assumes that the
|
225 |
+
byte string object uses the encoding passed in as parameter.
|
226 |
+
|
227 |
+
Changed in version 3.12: "u", "u#", "Z", and "Z#" are removed because
|
228 |
+
they used a legacy "Py_UNICODE*" representation.
|
229 |
+
|
230 |
+
|
231 |
+
Numbers
|
232 |
+
-------
|
233 |
+
|
234 |
+
"b" ("int") [unsigned char]
|
235 |
+
Convert a nonnegative Python integer to an unsigned tiny int,
|
236 |
+
stored in a C unsigned char.
|
237 |
+
|
238 |
+
"B" ("int") [unsigned char]
|
239 |
+
Convert a Python integer to a tiny int without overflow checking,
|
240 |
+
stored in a C unsigned char.
|
241 |
+
|
242 |
+
"h" ("int") [short int]
|
243 |
+
Convert a Python integer to a C short int.
|
244 |
+
|
245 |
+
"H" ("int") [unsigned short int]
|
246 |
+
Convert a Python integer to a C unsigned short int, without
|
247 |
+
overflow checking.
|
248 |
+
|
249 |
+
"i" ("int") [int]
|
250 |
+
Convert a Python integer to a plain C int.
|
251 |
+
|
252 |
+
"I" ("int") [unsigned int]
|
253 |
+
Convert a Python integer to a C unsigned int, without overflow
|
254 |
+
checking.
|
255 |
+
|
256 |
+
"l" ("int") [long int]
|
257 |
+
Convert a Python integer to a C long int.
|
258 |
+
|
259 |
+
"k" ("int") [unsigned long]
|
260 |
+
Convert a Python integer to a C unsigned long without overflow
|
261 |
+
checking.
|
262 |
+
|
263 |
+
"L" ("int") [long long]
|
264 |
+
Convert a Python integer to a C long long.
|
265 |
+
|
266 |
+
"K" ("int") [unsigned long long]
|
267 |
+
Convert a Python integer to a C unsigned long long without overflow
|
268 |
+
checking.
|
269 |
+
|
270 |
+
"n" ("int") ["Py_ssize_t"]
|
271 |
+
Convert a Python integer to a C "Py_ssize_t".
|
272 |
+
|
273 |
+
"c" ("bytes" or "bytearray" of length 1) [char]
|
274 |
+
Convert a Python byte, represented as a "bytes" or "bytearray"
|
275 |
+
object of length 1, to a C char.
|
276 |
+
|
277 |
+
Changed in version 3.3: Allow "bytearray" objects.
|
278 |
+
|
279 |
+
"C" ("str" of length 1) [int]
|
280 |
+
Convert a Python character, represented as a "str" object of length
|
281 |
+
1, to a C int.
|
282 |
+
|
283 |
+
"f" ("float") [float]
|
284 |
+
Convert a Python floating-point number to a C float.
|
285 |
+
|
286 |
+
"d" ("float") [double]
|
287 |
+
Convert a Python floating-point number to a C double.
|
288 |
+
|
289 |
+
"D" ("complex") [Py_complex]
|
290 |
+
Convert a Python complex number to a C "Py_complex" structure.
|
291 |
+
|
292 |
+
|
293 |
+
Other objects
|
294 |
+
-------------
|
295 |
+
|
296 |
+
"O" (object) [PyObject *]
|
297 |
+
Store a Python object (without any conversion) in a C object
|
298 |
+
pointer. The C program thus receives the actual object that was
|
299 |
+
passed. A new *strong reference* to the object is not created
|
300 |
+
(i.e. its reference count is not increased). The pointer stored is
|
301 |
+
not "NULL".
|
302 |
+
|
303 |
+
"O!" (object) [*typeobject*, PyObject *]
|
304 |
+
Store a Python object in a C object pointer. This is similar to
|
305 |
+
"O", but takes two C arguments: the first is the address of a
|
306 |
+
Python type object, the second is the address of the C variable (of
|
307 |
+
type PyObject*) into which the object pointer is stored. If the
|
308 |
+
Python object does not have the required type, "TypeError" is
|
309 |
+
raised.
|
310 |
+
|
311 |
+
"O&" (object) [*converter*, *anything*]
|
312 |
+
Convert a Python object to a C variable through a *converter*
|
313 |
+
function. This takes two arguments: the first is a function, the
|
314 |
+
second is the address of a C variable (of arbitrary type),
|
315 |
+
converted to void*. The *converter* function in turn is called as
|
316 |
+
follows:
|
317 |
+
|
318 |
+
status = converter(object, address);
|
319 |
+
|
320 |
+
where *object* is the Python object to be converted and *address*
|
321 |
+
is the void* argument that was passed to the "PyArg_Parse*"
|
322 |
+
function. The returned *status* should be "1" for a successful
|
323 |
+
conversion and "0" if the conversion has failed. When the
|
324 |
+
conversion fails, the *converter* function should raise an
|
325 |
+
exception and leave the content of *address* unmodified.
|
326 |
+
|
327 |
+
If the *converter* returns "Py_CLEANUP_SUPPORTED", it may get
|
328 |
+
called a second time if the argument parsing eventually fails,
|
329 |
+
giving the converter a chance to release any memory that it had
|
330 |
+
already allocated. In this second call, the *object* parameter will
|
331 |
+
be "NULL"; *address* will have the same value as in the original
|
332 |
+
call.
|
333 |
+
|
334 |
+
Changed in version 3.1: "Py_CLEANUP_SUPPORTED" was added.
|
335 |
+
|
336 |
+
"p" ("bool") [int]
|
337 |
+
Tests the value passed in for truth (a boolean **p**redicate) and
|
338 |
+
converts the result to its equivalent C true/false integer value.
|
339 |
+
Sets the int to "1" if the expression was true and "0" if it was
|
340 |
+
false. This accepts any valid Python value. See Truth Value
|
341 |
+
Testing for more information about how Python tests values for
|
342 |
+
truth.
|
343 |
+
|
344 |
+
Added in version 3.3.
|
345 |
+
|
346 |
+
"(items)" ("tuple") [*matching-items*]
|
347 |
+
The object must be a Python sequence whose length is the number of
|
348 |
+
format units in *items*. The C arguments must correspond to the
|
349 |
+
individual format units in *items*. Format units for sequences may
|
350 |
+
be nested.
|
351 |
+
|
352 |
+
It is possible to pass "long" integers (integers whose value exceeds
|
353 |
+
the platform's "LONG_MAX") however no proper range checking is done
|
354 |
+
--- the most significant bits are silently truncated when the
|
355 |
+
receiving field is too small to receive the value (actually, the
|
356 |
+
semantics are inherited from downcasts in C --- your mileage may
|
357 |
+
vary).
|
358 |
+
|
359 |
+
A few other characters have a meaning in a format string. These may
|
360 |
+
not occur inside nested parentheses. They are:
|
361 |
+
|
362 |
+
"|"
|
363 |
+
Indicates that the remaining arguments in the Python argument list
|
364 |
+
are optional. The C variables corresponding to optional arguments
|
365 |
+
should be initialized to their default value --- when an optional
|
366 |
+
argument is not specified, "PyArg_ParseTuple()" does not touch the
|
367 |
+
contents of the corresponding C variable(s).
|
368 |
+
|
369 |
+
"$"
|
370 |
+
"PyArg_ParseTupleAndKeywords()" only: Indicates that the remaining
|
371 |
+
arguments in the Python argument list are keyword-only. Currently,
|
372 |
+
all keyword-only arguments must also be optional arguments, so "|"
|
373 |
+
must always be specified before "$" in the format string.
|
374 |
+
|
375 |
+
Added in version 3.3.
|
376 |
+
|
377 |
+
":"
|
378 |
+
The list of format units ends here; the string after the colon is
|
379 |
+
used as the function name in error messages (the "associated value"
|
380 |
+
of the exception that "PyArg_ParseTuple()" raises).
|
381 |
+
|
382 |
+
";"
|
383 |
+
The list of format units ends here; the string after the semicolon
|
384 |
+
is used as the error message *instead* of the default error
|
385 |
+
message. ":" and ";" mutually exclude each other.
|
386 |
+
|
387 |
+
Note that any Python object references which are provided to the
|
388 |
+
caller are *borrowed* references; do not release them (i.e. do not
|
389 |
+
decrement their reference count)!
|
390 |
+
|
391 |
+
Additional arguments passed to these functions must be addresses of
|
392 |
+
variables whose type is determined by the format string; these are
|
393 |
+
used to store values from the input tuple. There are a few cases, as
|
394 |
+
described in the list of format units above, where these parameters
|
395 |
+
are used as input values; they should match what is specified for the
|
396 |
+
corresponding format unit in that case.
|
397 |
+
|
398 |
+
For the conversion to succeed, the *arg* object must match the format
|
399 |
+
and the format must be exhausted. On success, the "PyArg_Parse*"
|
400 |
+
functions return true, otherwise they return false and raise an
|
401 |
+
appropriate exception. When the "PyArg_Parse*" functions fail due to
|
402 |
+
conversion failure in one of the format units, the variables at the
|
403 |
+
addresses corresponding to that and the following format units are
|
404 |
+
left untouched.
|
405 |
+
|
406 |
+
|
407 |
+
API Functions
|
408 |
+
-------------
|
409 |
+
|
410 |
+
int PyArg_ParseTuple(PyObject *args, const char *format, ...)
|
411 |
+
* Part of the Stable ABI.*
|
412 |
+
|
413 |
+
Parse the parameters of a function that takes only positional
|
414 |
+
parameters into local variables. Returns true on success; on
|
415 |
+
failure, it returns false and raises the appropriate exception.
|
416 |
+
|
417 |
+
int PyArg_VaParse(PyObject *args, const char *format, va_list vargs)
|
418 |
+
* Part of the Stable ABI.*
|
419 |
+
|
420 |
+
Identical to "PyArg_ParseTuple()", except that it accepts a va_list
|
421 |
+
rather than a variable number of arguments.
|
422 |
+
|
423 |
+
int PyArg_ParseTupleAndKeywords(PyObject *args, PyObject *kw, const char *format, char *keywords[], ...)
|
424 |
+
* Part of the Stable ABI.*
|
425 |
+
|
426 |
+
Parse the parameters of a function that takes both positional and
|
427 |
+
keyword parameters into local variables. The *keywords* argument
|
428 |
+
is a "NULL"-terminated array of keyword parameter names. Empty
|
429 |
+
names denote positional-only parameters. Returns true on success;
|
430 |
+
on failure, it returns false and raises the appropriate exception.
|
431 |
+
|
432 |
+
Changed in version 3.6: Added support for positional-only
|
433 |
+
parameters.
|
434 |
+
|
435 |
+
int PyArg_VaParseTupleAndKeywords(PyObject *args, PyObject *kw, const char *format, char *keywords[], va_list vargs)
|
436 |
+
* Part of the Stable ABI.*
|
437 |
+
|
438 |
+
Identical to "PyArg_ParseTupleAndKeywords()", except that it
|
439 |
+
accepts a va_list rather than a variable number of arguments.
|
440 |
+
|
441 |
+
int PyArg_ValidateKeywordArguments(PyObject*)
|
442 |
+
* Part of the Stable ABI.*
|
443 |
+
|
444 |
+
Ensure that the keys in the keywords argument dictionary are
|
445 |
+
strings. This is only needed if "PyArg_ParseTupleAndKeywords()" is
|
446 |
+
not used, since the latter already does this check.
|
447 |
+
|
448 |
+
Added in version 3.2.
|
449 |
+
|
450 |
+
int PyArg_Parse(PyObject *args, const char *format, ...)
|
451 |
+
* Part of the Stable ABI.*
|
452 |
+
|
453 |
+
Function used to deconstruct the argument lists of "old-style"
|
454 |
+
functions --- these are functions which use the "METH_OLDARGS"
|
455 |
+
parameter parsing method, which has been removed in Python 3. This
|
456 |
+
is not recommended for use in parameter parsing in new code, and
|
457 |
+
most code in the standard interpreter has been modified to no
|
458 |
+
longer use this for that purpose. It does remain a convenient way
|
459 |
+
to decompose other tuples, however, and may continue to be used for
|
460 |
+
that purpose.
|
461 |
+
|
462 |
+
int PyArg_UnpackTuple(PyObject *args, const char *name, Py_ssize_t min, Py_ssize_t max, ...)
|
463 |
+
* Part of the Stable ABI.*
|
464 |
+
|
465 |
+
A simpler form of parameter retrieval which does not use a format
|
466 |
+
string to specify the types of the arguments. Functions which use
|
467 |
+
this method to retrieve their parameters should be declared as
|
468 |
+
"METH_VARARGS" in function or method tables. The tuple containing
|
469 |
+
the actual parameters should be passed as *args*; it must actually
|
470 |
+
be a tuple. The length of the tuple must be at least *min* and no
|
471 |
+
more than *max*; *min* and *max* may be equal. Additional
|
472 |
+
arguments must be passed to the function, each of which should be a
|
473 |
+
pointer to a PyObject* variable; these will be filled in with the
|
474 |
+
values from *args*; they will contain *borrowed references*. The
|
475 |
+
variables which correspond to optional parameters not given by
|
476 |
+
*args* will not be filled in; these should be initialized by the
|
477 |
+
caller. This function returns true on success and false if *args*
|
478 |
+
is not a tuple or contains the wrong number of elements; an
|
479 |
+
exception will be set if there was a failure.
|
480 |
+
|
481 |
+
This is an example of the use of this function, taken from the
|
482 |
+
sources for the "_weakref" helper module for weak references:
|
483 |
+
|
484 |
+
static PyObject *
|
485 |
+
weakref_ref(PyObject *self, PyObject *args)
|
486 |
+
{
|
487 |
+
PyObject *object;
|
488 |
+
PyObject *callback = NULL;
|
489 |
+
PyObject *result = NULL;
|
490 |
+
|
491 |
+
if (PyArg_UnpackTuple(args, "ref", 1, 2, &object, &callback)) {
|
492 |
+
result = PyWeakref_NewRef(object, callback);
|
493 |
+
}
|
494 |
+
return result;
|
495 |
+
}
|
496 |
+
|
497 |
+
The call to "PyArg_UnpackTuple()" in this example is entirely
|
498 |
+
equivalent to this call to "PyArg_ParseTuple()":
|
499 |
+
|
500 |
+
PyArg_ParseTuple(args, "O|O:ref", &object, &callback)
|
501 |
+
|
502 |
+
|
503 |
+
Building values
|
504 |
+
===============
|
505 |
+
|
506 |
+
PyObject *Py_BuildValue(const char *format, ...)
|
507 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
508 |
+
|
509 |
+
Create a new value based on a format string similar to those
|
510 |
+
accepted by the "PyArg_Parse*" family of functions and a sequence
|
511 |
+
of values. Returns the value or "NULL" in the case of an error; an
|
512 |
+
exception will be raised if "NULL" is returned.
|
513 |
+
|
514 |
+
"Py_BuildValue()" does not always build a tuple. It builds a tuple
|
515 |
+
only if its format string contains two or more format units. If
|
516 |
+
the format string is empty, it returns "None"; if it contains
|
517 |
+
exactly one format unit, it returns whatever object is described by
|
518 |
+
that format unit. To force it to return a tuple of size 0 or one,
|
519 |
+
parenthesize the format string.
|
520 |
+
|
521 |
+
When memory buffers are passed as parameters to supply data to
|
522 |
+
build objects, as for the "s" and "s#" formats, the required data
|
523 |
+
is copied. Buffers provided by the caller are never referenced by
|
524 |
+
the objects created by "Py_BuildValue()". In other words, if your
|
525 |
+
code invokes "malloc()" and passes the allocated memory to
|
526 |
+
"Py_BuildValue()", your code is responsible for calling "free()"
|
527 |
+
for that memory once "Py_BuildValue()" returns.
|
528 |
+
|
529 |
+
In the following description, the quoted form is the format unit;
|
530 |
+
the entry in (round) parentheses is the Python object type that the
|
531 |
+
format unit will return; and the entry in [square] brackets is the
|
532 |
+
type of the C value(s) to be passed.
|
533 |
+
|
534 |
+
The characters space, tab, colon and comma are ignored in format
|
535 |
+
strings (but not within format units such as "s#"). This can be
|
536 |
+
used to make long format strings a tad more readable.
|
537 |
+
|
538 |
+
"s" ("str" or "None") [const char *]
|
539 |
+
Convert a null-terminated C string to a Python "str" object
|
540 |
+
using "'utf-8'" encoding. If the C string pointer is "NULL",
|
541 |
+
"None" is used.
|
542 |
+
|
543 |
+
"s#" ("str" or "None") [const char *, "Py_ssize_t"]
|
544 |
+
Convert a C string and its length to a Python "str" object using
|
545 |
+
"'utf-8'" encoding. If the C string pointer is "NULL", the
|
546 |
+
length is ignored and "None" is returned.
|
547 |
+
|
548 |
+
"y" ("bytes") [const char *]
|
549 |
+
This converts a C string to a Python "bytes" object. If the C
|
550 |
+
string pointer is "NULL", "None" is returned.
|
551 |
+
|
552 |
+
"y#" ("bytes") [const char *, "Py_ssize_t"]
|
553 |
+
This converts a C string and its lengths to a Python object. If
|
554 |
+
the C string pointer is "NULL", "None" is returned.
|
555 |
+
|
556 |
+
"z" ("str" or "None") [const char *]
|
557 |
+
Same as "s".
|
558 |
+
|
559 |
+
"z#" ("str" or "None") [const char *, "Py_ssize_t"]
|
560 |
+
Same as "s#".
|
561 |
+
|
562 |
+
"u" ("str") [const wchar_t *]
|
563 |
+
Convert a null-terminated "wchar_t" buffer of Unicode (UTF-16 or
|
564 |
+
UCS-4) data to a Python Unicode object. If the Unicode buffer
|
565 |
+
pointer is "NULL", "None" is returned.
|
566 |
+
|
567 |
+
"u#" ("str") [const wchar_t *, "Py_ssize_t"]
|
568 |
+
Convert a Unicode (UTF-16 or UCS-4) data buffer and its length
|
569 |
+
to a Python Unicode object. If the Unicode buffer pointer is
|
570 |
+
"NULL", the length is ignored and "None" is returned.
|
571 |
+
|
572 |
+
"U" ("str" or "None") [const char *]
|
573 |
+
Same as "s".
|
574 |
+
|
575 |
+
"U#" ("str" or "None") [const char *, "Py_ssize_t"]
|
576 |
+
Same as "s#".
|
577 |
+
|
578 |
+
"i" ("int") [int]
|
579 |
+
Convert a plain C int to a Python integer object.
|
580 |
+
|
581 |
+
"b" ("int") [char]
|
582 |
+
Convert a plain C char to a Python integer object.
|
583 |
+
|
584 |
+
"h" ("int") [short int]
|
585 |
+
Convert a plain C short int to a Python integer object.
|
586 |
+
|
587 |
+
"l" ("int") [long int]
|
588 |
+
Convert a C long int to a Python integer object.
|
589 |
+
|
590 |
+
"B" ("int") [unsigned char]
|
591 |
+
Convert a C unsigned char to a Python integer object.
|
592 |
+
|
593 |
+
"H" ("int") [unsigned short int]
|
594 |
+
Convert a C unsigned short int to a Python integer object.
|
595 |
+
|
596 |
+
"I" ("int") [unsigned int]
|
597 |
+
Convert a C unsigned int to a Python integer object.
|
598 |
+
|
599 |
+
"k" ("int") [unsigned long]
|
600 |
+
Convert a C unsigned long to a Python integer object.
|
601 |
+
|
602 |
+
"L" ("int") [long long]
|
603 |
+
Convert a C long long to a Python integer object.
|
604 |
+
|
605 |
+
"K" ("int") [unsigned long long]
|
606 |
+
Convert a C unsigned long long to a Python integer object.
|
607 |
+
|
608 |
+
"n" ("int") ["Py_ssize_t"]
|
609 |
+
Convert a C "Py_ssize_t" to a Python integer.
|
610 |
+
|
611 |
+
"c" ("bytes" of length 1) [char]
|
612 |
+
Convert a C int representing a byte to a Python "bytes" object
|
613 |
+
of length 1.
|
614 |
+
|
615 |
+
"C" ("str" of length 1) [int]
|
616 |
+
Convert a C int representing a character to Python "str" object
|
617 |
+
of length 1.
|
618 |
+
|
619 |
+
"d" ("float") [double]
|
620 |
+
Convert a C double to a Python floating-point number.
|
621 |
+
|
622 |
+
"f" ("float") [float]
|
623 |
+
Convert a C float to a Python floating-point number.
|
624 |
+
|
625 |
+
"D" ("complex") [Py_complex *]
|
626 |
+
Convert a C "Py_complex" structure to a Python complex number.
|
627 |
+
|
628 |
+
"O" (object) [PyObject *]
|
629 |
+
Pass a Python object untouched but create a new *strong
|
630 |
+
reference* to it (i.e. its reference count is incremented by
|
631 |
+
one). If the object passed in is a "NULL" pointer, it is assumed
|
632 |
+
that this was caused because the call producing the argument
|
633 |
+
found an error and set an exception. Therefore,
|
634 |
+
"Py_BuildValue()" will return "NULL" but won't raise an
|
635 |
+
exception. If no exception has been raised yet, "SystemError"
|
636 |
+
is set.
|
637 |
+
|
638 |
+
"S" (object) [PyObject *]
|
639 |
+
Same as "O".
|
640 |
+
|
641 |
+
"N" (object) [PyObject *]
|
642 |
+
Same as "O", except it doesn't create a new *strong reference*.
|
643 |
+
Useful when the object is created by a call to an object
|
644 |
+
constructor in the argument list.
|
645 |
+
|
646 |
+
"O&" (object) [*converter*, *anything*]
|
647 |
+
Convert *anything* to a Python object through a *converter*
|
648 |
+
function. The function is called with *anything* (which should
|
649 |
+
be compatible with void*) as its argument and should return a
|
650 |
+
"new" Python object, or "NULL" if an error occurred.
|
651 |
+
|
652 |
+
"(items)" ("tuple") [*matching-items*]
|
653 |
+
Convert a sequence of C values to a Python tuple with the same
|
654 |
+
number of items.
|
655 |
+
|
656 |
+
"[items]" ("list") [*matching-items*]
|
657 |
+
Convert a sequence of C values to a Python list with the same
|
658 |
+
number of items.
|
659 |
+
|
660 |
+
"{items}" ("dict") [*matching-items*]
|
661 |
+
Convert a sequence of C values to a Python dictionary. Each
|
662 |
+
pair of consecutive C values adds one item to the dictionary,
|
663 |
+
serving as key and value, respectively.
|
664 |
+
|
665 |
+
If there is an error in the format string, the "SystemError"
|
666 |
+
exception is set and "NULL" returned.
|
667 |
+
|
668 |
+
PyObject *Py_VaBuildValue(const char *format, va_list vargs)
|
669 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
670 |
+
|
671 |
+
Identical to "Py_BuildValue()", except that it accepts a va_list
|
672 |
+
rather than a variable number of arguments.
|
python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/bool.txt
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Boolean Objects
|
2 |
+
***************
|
3 |
+
|
4 |
+
Booleans in Python are implemented as a subclass of integers. There
|
5 |
+
are only two booleans, "Py_False" and "Py_True". As such, the normal
|
6 |
+
creation and deletion functions don't apply to booleans. The
|
7 |
+
following macros are available, however.
|
8 |
+
|
9 |
+
PyTypeObject PyBool_Type
|
10 |
+
* Part of the Stable ABI.*
|
11 |
+
|
12 |
+
This instance of "PyTypeObject" represents the Python boolean type;
|
13 |
+
it is the same object as "bool" in the Python layer.
|
14 |
+
|
15 |
+
int PyBool_Check(PyObject *o)
|
16 |
+
|
17 |
+
Return true if *o* is of type "PyBool_Type". This function always
|
18 |
+
succeeds.
|
19 |
+
|
20 |
+
PyObject *Py_False
|
21 |
+
|
22 |
+
The Python "False" object. This object has no methods and is
|
23 |
+
immortal.
|
24 |
+
|
25 |
+
Changed in version 3.12: "Py_False" is immortal.
|
26 |
+
|
27 |
+
PyObject *Py_True
|
28 |
+
|
29 |
+
The Python "True" object. This object has no methods and is
|
30 |
+
immortal.
|
31 |
+
|
32 |
+
Changed in version 3.12: "Py_True" is immortal.
|
33 |
+
|
34 |
+
Py_RETURN_FALSE
|
35 |
+
|
36 |
+
Return "Py_False" from a function.
|
37 |
+
|
38 |
+
Py_RETURN_TRUE
|
39 |
+
|
40 |
+
Return "Py_True" from a function.
|
41 |
+
|
42 |
+
PyObject *PyBool_FromLong(long v)
|
43 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
44 |
+
|
45 |
+
Return "Py_True" or "Py_False", depending on the truth value of
|
46 |
+
*v*.
|
python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/buffer.txt
ADDED
@@ -0,0 +1,520 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Buffer Protocol
|
2 |
+
***************
|
3 |
+
|
4 |
+
Certain objects available in Python wrap access to an underlying
|
5 |
+
memory array or *buffer*. Such objects include the built-in "bytes"
|
6 |
+
and "bytearray", and some extension types like "array.array". Third-
|
7 |
+
party libraries may define their own types for special purposes, such
|
8 |
+
as image processing or numeric analysis.
|
9 |
+
|
10 |
+
While each of these types have their own semantics, they share the
|
11 |
+
common characteristic of being backed by a possibly large memory
|
12 |
+
buffer. It is then desirable, in some situations, to access that
|
13 |
+
buffer directly and without intermediate copying.
|
14 |
+
|
15 |
+
Python provides such a facility at the C level in the form of the
|
16 |
+
buffer protocol. This protocol has two sides:
|
17 |
+
|
18 |
+
* on the producer side, a type can export a "buffer interface" which
|
19 |
+
allows objects of that type to expose information about their
|
20 |
+
underlying buffer. This interface is described in the section Buffer
|
21 |
+
Object Structures;
|
22 |
+
|
23 |
+
* on the consumer side, several means are available to obtain a
|
24 |
+
pointer to the raw underlying data of an object (for example a
|
25 |
+
method parameter).
|
26 |
+
|
27 |
+
Simple objects such as "bytes" and "bytearray" expose their underlying
|
28 |
+
buffer in byte-oriented form. Other forms are possible; for example,
|
29 |
+
the elements exposed by an "array.array" can be multi-byte values.
|
30 |
+
|
31 |
+
An example consumer of the buffer interface is the "write()" method of
|
32 |
+
file objects: any object that can export a series of bytes through the
|
33 |
+
buffer interface can be written to a file. While "write()" only needs
|
34 |
+
read-only access to the internal contents of the object passed to it,
|
35 |
+
other methods such as "readinto()" need write access to the contents
|
36 |
+
of their argument. The buffer interface allows objects to selectively
|
37 |
+
allow or reject exporting of read-write and read-only buffers.
|
38 |
+
|
39 |
+
There are two ways for a consumer of the buffer interface to acquire a
|
40 |
+
buffer over a target object:
|
41 |
+
|
42 |
+
* call "PyObject_GetBuffer()" with the right parameters;
|
43 |
+
|
44 |
+
* call "PyArg_ParseTuple()" (or one of its siblings) with one of the
|
45 |
+
"y*", "w*" or "s*" format codes.
|
46 |
+
|
47 |
+
In both cases, "PyBuffer_Release()" must be called when the buffer
|
48 |
+
isn't needed anymore. Failure to do so could lead to various issues
|
49 |
+
such as resource leaks.
|
50 |
+
|
51 |
+
|
52 |
+
Buffer structure
|
53 |
+
================
|
54 |
+
|
55 |
+
Buffer structures (or simply "buffers") are useful as a way to expose
|
56 |
+
the binary data from another object to the Python programmer. They
|
57 |
+
can also be used as a zero-copy slicing mechanism. Using their
|
58 |
+
ability to reference a block of memory, it is possible to expose any
|
59 |
+
data to the Python programmer quite easily. The memory could be a
|
60 |
+
large, constant array in a C extension, it could be a raw block of
|
61 |
+
memory for manipulation before passing to an operating system library,
|
62 |
+
or it could be used to pass around structured data in its native, in-
|
63 |
+
memory format.
|
64 |
+
|
65 |
+
Contrary to most data types exposed by the Python interpreter, buffers
|
66 |
+
are not "PyObject" pointers but rather simple C structures. This
|
67 |
+
allows them to be created and copied very simply. When a generic
|
68 |
+
wrapper around a buffer is needed, a memoryview object can be created.
|
69 |
+
|
70 |
+
For short instructions how to write an exporting object, see Buffer
|
71 |
+
Object Structures. For obtaining a buffer, see "PyObject_GetBuffer()".
|
72 |
+
|
73 |
+
type Py_buffer
|
74 |
+
* Part of the Stable ABI (including all members) since version
|
75 |
+
3.11.*
|
76 |
+
|
77 |
+
void *buf
|
78 |
+
|
79 |
+
A pointer to the start of the logical structure described by the
|
80 |
+
buffer fields. This can be any location within the underlying
|
81 |
+
physical memory block of the exporter. For example, with
|
82 |
+
negative "strides" the value may point to the end of the memory
|
83 |
+
block.
|
84 |
+
|
85 |
+
For *contiguous* arrays, the value points to the beginning of
|
86 |
+
the memory block.
|
87 |
+
|
88 |
+
PyObject *obj
|
89 |
+
|
90 |
+
A new reference to the exporting object. The reference is owned
|
91 |
+
by the consumer and automatically released (i.e. reference count
|
92 |
+
decremented) and set to "NULL" by "PyBuffer_Release()". The
|
93 |
+
field is the equivalent of the return value of any standard
|
94 |
+
C-API function.
|
95 |
+
|
96 |
+
As a special case, for *temporary* buffers that are wrapped by
|
97 |
+
"PyMemoryView_FromBuffer()" or "PyBuffer_FillInfo()" this field
|
98 |
+
is "NULL". In general, exporting objects MUST NOT use this
|
99 |
+
scheme.
|
100 |
+
|
101 |
+
Py_ssize_t len
|
102 |
+
|
103 |
+
"product(shape) * itemsize". For contiguous arrays, this is the
|
104 |
+
length of the underlying memory block. For non-contiguous
|
105 |
+
arrays, it is the length that the logical structure would have
|
106 |
+
if it were copied to a contiguous representation.
|
107 |
+
|
108 |
+
Accessing "((char *)buf)[0] up to ((char *)buf)[len-1]" is only
|
109 |
+
valid if the buffer has been obtained by a request that
|
110 |
+
guarantees contiguity. In most cases such a request will be
|
111 |
+
"PyBUF_SIMPLE" or "PyBUF_WRITABLE".
|
112 |
+
|
113 |
+
int readonly
|
114 |
+
|
115 |
+
An indicator of whether the buffer is read-only. This field is
|
116 |
+
controlled by the "PyBUF_WRITABLE" flag.
|
117 |
+
|
118 |
+
Py_ssize_t itemsize
|
119 |
+
|
120 |
+
Item size in bytes of a single element. Same as the value of
|
121 |
+
"struct.calcsize()" called on non-"NULL" "format" values.
|
122 |
+
|
123 |
+
Important exception: If a consumer requests a buffer without the
|
124 |
+
"PyBUF_FORMAT" flag, "format" will be set to "NULL", but
|
125 |
+
"itemsize" still has the value for the original format.
|
126 |
+
|
127 |
+
If "shape" is present, the equality "product(shape) * itemsize
|
128 |
+
== len" still holds and the consumer can use "itemsize" to
|
129 |
+
navigate the buffer.
|
130 |
+
|
131 |
+
If "shape" is "NULL" as a result of a "PyBUF_SIMPLE" or a
|
132 |
+
"PyBUF_WRITABLE" request, the consumer must disregard "itemsize"
|
133 |
+
and assume "itemsize == 1".
|
134 |
+
|
135 |
+
char *format
|
136 |
+
|
137 |
+
A *NULL* terminated string in "struct" module style syntax
|
138 |
+
describing the contents of a single item. If this is "NULL",
|
139 |
+
""B"" (unsigned bytes) is assumed.
|
140 |
+
|
141 |
+
This field is controlled by the "PyBUF_FORMAT" flag.
|
142 |
+
|
143 |
+
int ndim
|
144 |
+
|
145 |
+
The number of dimensions the memory represents as an
|
146 |
+
n-dimensional array. If it is "0", "buf" points to a single item
|
147 |
+
representing a scalar. In this case, "shape", "strides" and
|
148 |
+
"suboffsets" MUST be "NULL". The maximum number of dimensions is
|
149 |
+
given by "PyBUF_MAX_NDIM".
|
150 |
+
|
151 |
+
Py_ssize_t *shape
|
152 |
+
|
153 |
+
An array of "Py_ssize_t" of length "ndim" indicating the shape
|
154 |
+
of the memory as an n-dimensional array. Note that "shape[0] *
|
155 |
+
... * shape[ndim-1] * itemsize" MUST be equal to "len".
|
156 |
+
|
157 |
+
Shape values are restricted to "shape[n] >= 0". The case
|
158 |
+
"shape[n] == 0" requires special attention. See complex arrays
|
159 |
+
for further information.
|
160 |
+
|
161 |
+
The shape array is read-only for the consumer.
|
162 |
+
|
163 |
+
Py_ssize_t *strides
|
164 |
+
|
165 |
+
An array of "Py_ssize_t" of length "ndim" giving the number of
|
166 |
+
bytes to skip to get to a new element in each dimension.
|
167 |
+
|
168 |
+
Stride values can be any integer. For regular arrays, strides
|
169 |
+
are usually positive, but a consumer MUST be able to handle the
|
170 |
+
case "strides[n] <= 0". See complex arrays for further
|
171 |
+
information.
|
172 |
+
|
173 |
+
The strides array is read-only for the consumer.
|
174 |
+
|
175 |
+
Py_ssize_t *suboffsets
|
176 |
+
|
177 |
+
An array of "Py_ssize_t" of length "ndim". If "suboffsets[n] >=
|
178 |
+
0", the values stored along the nth dimension are pointers and
|
179 |
+
the suboffset value dictates how many bytes to add to each
|
180 |
+
pointer after de-referencing. A suboffset value that is negative
|
181 |
+
indicates that no de-referencing should occur (striding in a
|
182 |
+
contiguous memory block).
|
183 |
+
|
184 |
+
If all suboffsets are negative (i.e. no de-referencing is
|
185 |
+
needed), then this field must be "NULL" (the default value).
|
186 |
+
|
187 |
+
This type of array representation is used by the Python Imaging
|
188 |
+
Library (PIL). See complex arrays for further information how to
|
189 |
+
access elements of such an array.
|
190 |
+
|
191 |
+
The suboffsets array is read-only for the consumer.
|
192 |
+
|
193 |
+
void *internal
|
194 |
+
|
195 |
+
This is for use internally by the exporting object. For example,
|
196 |
+
this might be re-cast as an integer by the exporter and used to
|
197 |
+
store flags about whether or not the shape, strides, and
|
198 |
+
suboffsets arrays must be freed when the buffer is released. The
|
199 |
+
consumer MUST NOT alter this value.
|
200 |
+
|
201 |
+
Constants:
|
202 |
+
|
203 |
+
PyBUF_MAX_NDIM
|
204 |
+
|
205 |
+
The maximum number of dimensions the memory represents. Exporters
|
206 |
+
MUST respect this limit, consumers of multi-dimensional buffers
|
207 |
+
SHOULD be able to handle up to "PyBUF_MAX_NDIM" dimensions.
|
208 |
+
Currently set to 64.
|
209 |
+
|
210 |
+
|
211 |
+
Buffer request types
|
212 |
+
====================
|
213 |
+
|
214 |
+
Buffers are usually obtained by sending a buffer request to an
|
215 |
+
exporting object via "PyObject_GetBuffer()". Since the complexity of
|
216 |
+
the logical structure of the memory can vary drastically, the consumer
|
217 |
+
uses the *flags* argument to specify the exact buffer type it can
|
218 |
+
handle.
|
219 |
+
|
220 |
+
All "Py_buffer" fields are unambiguously defined by the request type.
|
221 |
+
|
222 |
+
|
223 |
+
request-independent fields
|
224 |
+
--------------------------
|
225 |
+
|
226 |
+
The following fields are not influenced by *flags* and must always be
|
227 |
+
filled in with the correct values: "obj", "buf", "len", "itemsize",
|
228 |
+
"ndim".
|
229 |
+
|
230 |
+
|
231 |
+
readonly, format
|
232 |
+
----------------
|
233 |
+
|
234 |
+
PyBUF_WRITABLE
|
235 |
+
|
236 |
+
Controls the "readonly" field. If set, the exporter MUST provide
|
237 |
+
a writable buffer or else report failure. Otherwise, the
|
238 |
+
exporter MAY provide either a read-only or writable buffer, but
|
239 |
+
the choice MUST be consistent for all consumers.
|
240 |
+
|
241 |
+
PyBUF_FORMAT
|
242 |
+
|
243 |
+
Controls the "format" field. If set, this field MUST be filled
|
244 |
+
in correctly. Otherwise, this field MUST be "NULL".
|
245 |
+
|
246 |
+
"PyBUF_WRITABLE" can be |'d to any of the flags in the next section.
|
247 |
+
Since "PyBUF_SIMPLE" is defined as 0, "PyBUF_WRITABLE" can be used as
|
248 |
+
a stand-alone flag to request a simple writable buffer.
|
249 |
+
|
250 |
+
"PyBUF_FORMAT" can be |'d to any of the flags except "PyBUF_SIMPLE".
|
251 |
+
The latter already implies format "B" (unsigned bytes).
|
252 |
+
|
253 |
+
|
254 |
+
shape, strides, suboffsets
|
255 |
+
--------------------------
|
256 |
+
|
257 |
+
The flags that control the logical structure of the memory are listed
|
258 |
+
in decreasing order of complexity. Note that each flag contains all
|
259 |
+
bits of the flags below it.
|
260 |
+
|
261 |
+
+-------------------------------+---------+-----------+--------------+
|
262 |
+
| Request | shape | strides | suboffsets |
|
263 |
+
|===============================|=========|===========|==============|
|
264 |
+
| PyBUF_INDIRECT | yes | yes | if needed |
|
265 |
+
+-------------------------------+---------+-----------+--------------+
|
266 |
+
| PyBUF_STRIDES | yes | yes | NULL |
|
267 |
+
+-------------------------------+---------+-----------+--------------+
|
268 |
+
| PyBUF_ND | yes | NULL | NULL |
|
269 |
+
+-------------------------------+---------+-----------+--------------+
|
270 |
+
| PyBUF_SIMPLE | NULL | NULL | NULL |
|
271 |
+
+-------------------------------+---------+-----------+--------------+
|
272 |
+
|
273 |
+
|
274 |
+
contiguity requests
|
275 |
+
-------------------
|
276 |
+
|
277 |
+
C or Fortran *contiguity* can be explicitly requested, with and
|
278 |
+
without stride information. Without stride information, the buffer
|
279 |
+
must be C-contiguous.
|
280 |
+
|
281 |
+
+-------------------------------------+---------+-----------+--------------+----------+
|
282 |
+
| Request | shape | strides | suboffsets | contig |
|
283 |
+
|=====================================|=========|===========|==============|==========|
|
284 |
+
| PyBUF_C_CONTIGUOUS | yes | yes | NULL | C |
|
285 |
+
+-------------------------------------+---------+-----------+--------------+----------+
|
286 |
+
| PyBUF_F_CONTIGUOUS | yes | yes | NULL | F |
|
287 |
+
+-------------------------------------+---------+-----------+--------------+----------+
|
288 |
+
| PyBUF_ANY_CONTIGUOUS | yes | yes | NULL | C or F |
|
289 |
+
+-------------------------------------+---------+-----------+--------------+----------+
|
290 |
+
| "PyBUF_ND" | yes | NULL | NULL | C |
|
291 |
+
+-------------------------------------+---------+-----------+--------------+----------+
|
292 |
+
|
293 |
+
|
294 |
+
compound requests
|
295 |
+
-----------------
|
296 |
+
|
297 |
+
All possible requests are fully defined by some combination of the
|
298 |
+
flags in the previous section. For convenience, the buffer protocol
|
299 |
+
provides frequently used combinations as single flags.
|
300 |
+
|
301 |
+
In the following table *U* stands for undefined contiguity. The
|
302 |
+
consumer would have to call "PyBuffer_IsContiguous()" to determine
|
303 |
+
contiguity.
|
304 |
+
|
305 |
+
+---------------------------------+---------+-----------+--------------+----------+------------+----------+
|
306 |
+
| Request | shape | strides | suboffsets | contig | readonly | format |
|
307 |
+
|=================================|=========|===========|==============|==========|============|==========|
|
308 |
+
| PyBUF_FULL | yes | yes | if needed | U | 0 | yes |
|
309 |
+
+---------------------------------+---------+-----------+--------------+----------+------------+----------+
|
310 |
+
| PyBUF_FULL_RO | yes | yes | if needed | U | 1 or 0 | yes |
|
311 |
+
+---------------------------------+---------+-----------+--------------+----------+------------+----------+
|
312 |
+
| PyBUF_RECORDS | yes | yes | NULL | U | 0 | yes |
|
313 |
+
+---------------------------------+---------+-----------+--------------+----------+------------+----------+
|
314 |
+
| PyBUF_RECORDS_RO | yes | yes | NULL | U | 1 or 0 | yes |
|
315 |
+
+---------------------------------+---------+-----------+--------------+----------+------------+----------+
|
316 |
+
| PyBUF_STRIDED | yes | yes | NULL | U | 0 | NULL |
|
317 |
+
+---------------------------------+---------+-----------+--------------+----------+------------+----------+
|
318 |
+
| PyBUF_STRIDED_RO | yes | yes | NULL | U | 1 or 0 | NULL |
|
319 |
+
+---------------------------------+---------+-----------+--------------+----------+------------+----------+
|
320 |
+
| PyBUF_CONTIG | yes | NULL | NULL | C | 0 | NULL |
|
321 |
+
+---------------------------------+---------+-----------+--------------+----------+------------+----------+
|
322 |
+
| PyBUF_CONTIG_RO | yes | NULL | NULL | C | 1 or 0 | NULL |
|
323 |
+
+---------------------------------+---------+-----------+--------------+----------+------------+----------+
|
324 |
+
|
325 |
+
|
326 |
+
Complex arrays
|
327 |
+
==============
|
328 |
+
|
329 |
+
|
330 |
+
NumPy-style: shape and strides
|
331 |
+
------------------------------
|
332 |
+
|
333 |
+
The logical structure of NumPy-style arrays is defined by "itemsize",
|
334 |
+
"ndim", "shape" and "strides".
|
335 |
+
|
336 |
+
If "ndim == 0", the memory location pointed to by "buf" is interpreted
|
337 |
+
as a scalar of size "itemsize". In that case, both "shape" and
|
338 |
+
"strides" are "NULL".
|
339 |
+
|
340 |
+
If "strides" is "NULL", the array is interpreted as a standard
|
341 |
+
n-dimensional C-array. Otherwise, the consumer must access an
|
342 |
+
n-dimensional array as follows:
|
343 |
+
|
344 |
+
ptr = (char *)buf + indices[0] * strides[0] + ... + indices[n-1] * strides[n-1];
|
345 |
+
item = *((typeof(item) *)ptr);
|
346 |
+
|
347 |
+
As noted above, "buf" can point to any location within the actual
|
348 |
+
memory block. An exporter can check the validity of a buffer with this
|
349 |
+
function:
|
350 |
+
|
351 |
+
def verify_structure(memlen, itemsize, ndim, shape, strides, offset):
|
352 |
+
"""Verify that the parameters represent a valid array within
|
353 |
+
the bounds of the allocated memory:
|
354 |
+
char *mem: start of the physical memory block
|
355 |
+
memlen: length of the physical memory block
|
356 |
+
offset: (char *)buf - mem
|
357 |
+
"""
|
358 |
+
if offset % itemsize:
|
359 |
+
return False
|
360 |
+
if offset < 0 or offset+itemsize > memlen:
|
361 |
+
return False
|
362 |
+
if any(v % itemsize for v in strides):
|
363 |
+
return False
|
364 |
+
|
365 |
+
if ndim <= 0:
|
366 |
+
return ndim == 0 and not shape and not strides
|
367 |
+
if 0 in shape:
|
368 |
+
return True
|
369 |
+
|
370 |
+
imin = sum(strides[j]*(shape[j]-1) for j in range(ndim)
|
371 |
+
if strides[j] <= 0)
|
372 |
+
imax = sum(strides[j]*(shape[j]-1) for j in range(ndim)
|
373 |
+
if strides[j] > 0)
|
374 |
+
|
375 |
+
return 0 <= offset+imin and offset+imax+itemsize <= memlen
|
376 |
+
|
377 |
+
|
378 |
+
PIL-style: shape, strides and suboffsets
|
379 |
+
----------------------------------------
|
380 |
+
|
381 |
+
In addition to the regular items, PIL-style arrays can contain
|
382 |
+
pointers that must be followed in order to get to the next element in
|
383 |
+
a dimension. For example, the regular three-dimensional C-array "char
|
384 |
+
v[2][2][3]" can also be viewed as an array of 2 pointers to 2 two-
|
385 |
+
dimensional arrays: "char (*v[2])[2][3]". In suboffsets
|
386 |
+
representation, those two pointers can be embedded at the start of
|
387 |
+
"buf", pointing to two "char x[2][3]" arrays that can be located
|
388 |
+
anywhere in memory.
|
389 |
+
|
390 |
+
Here is a function that returns a pointer to the element in an N-D
|
391 |
+
array pointed to by an N-dimensional index when there are both
|
392 |
+
non-"NULL" strides and suboffsets:
|
393 |
+
|
394 |
+
void *get_item_pointer(int ndim, void *buf, Py_ssize_t *strides,
|
395 |
+
Py_ssize_t *suboffsets, Py_ssize_t *indices) {
|
396 |
+
char *pointer = (char*)buf;
|
397 |
+
int i;
|
398 |
+
for (i = 0; i < ndim; i++) {
|
399 |
+
pointer += strides[i] * indices[i];
|
400 |
+
if (suboffsets[i] >=0 ) {
|
401 |
+
pointer = *((char**)pointer) + suboffsets[i];
|
402 |
+
}
|
403 |
+
}
|
404 |
+
return (void*)pointer;
|
405 |
+
}
|
406 |
+
|
407 |
+
|
408 |
+
Buffer-related functions
|
409 |
+
========================
|
410 |
+
|
411 |
+
int PyObject_CheckBuffer(PyObject *obj)
|
412 |
+
* Part of the Stable ABI since version 3.11.*
|
413 |
+
|
414 |
+
Return "1" if *obj* supports the buffer interface otherwise "0".
|
415 |
+
When "1" is returned, it doesn't guarantee that
|
416 |
+
"PyObject_GetBuffer()" will succeed. This function always
|
417 |
+
succeeds.
|
418 |
+
|
419 |
+
int PyObject_GetBuffer(PyObject *exporter, Py_buffer *view, int flags)
|
420 |
+
* Part of the Stable ABI since version 3.11.*
|
421 |
+
|
422 |
+
Send a request to *exporter* to fill in *view* as specified by
|
423 |
+
*flags*. If the exporter cannot provide a buffer of the exact type,
|
424 |
+
it MUST raise "BufferError", set "view->obj" to "NULL" and return
|
425 |
+
"-1".
|
426 |
+
|
427 |
+
On success, fill in *view*, set "view->obj" to a new reference to
|
428 |
+
*exporter* and return 0. In the case of chained buffer providers
|
429 |
+
that redirect requests to a single object, "view->obj" MAY refer to
|
430 |
+
this object instead of *exporter* (See Buffer Object Structures).
|
431 |
+
|
432 |
+
Successful calls to "PyObject_GetBuffer()" must be paired with
|
433 |
+
calls to "PyBuffer_Release()", similar to "malloc()" and "free()".
|
434 |
+
Thus, after the consumer is done with the buffer,
|
435 |
+
"PyBuffer_Release()" must be called exactly once.
|
436 |
+
|
437 |
+
void PyBuffer_Release(Py_buffer *view)
|
438 |
+
* Part of the Stable ABI since version 3.11.*
|
439 |
+
|
440 |
+
Release the buffer *view* and release the *strong reference* (i.e.
|
441 |
+
decrement the reference count) to the view's supporting object,
|
442 |
+
"view->obj". This function MUST be called when the buffer is no
|
443 |
+
longer being used, otherwise reference leaks may occur.
|
444 |
+
|
445 |
+
It is an error to call this function on a buffer that was not
|
446 |
+
obtained via "PyObject_GetBuffer()".
|
447 |
+
|
448 |
+
Py_ssize_t PyBuffer_SizeFromFormat(const char *format)
|
449 |
+
* Part of the Stable ABI since version 3.11.*
|
450 |
+
|
451 |
+
Return the implied "itemsize" from "format". On error, raise an
|
452 |
+
exception and return -1.
|
453 |
+
|
454 |
+
Added in version 3.9.
|
455 |
+
|
456 |
+
int PyBuffer_IsContiguous(const Py_buffer *view, char order)
|
457 |
+
* Part of the Stable ABI since version 3.11.*
|
458 |
+
|
459 |
+
Return "1" if the memory defined by the *view* is C-style (*order*
|
460 |
+
is "'C'") or Fortran-style (*order* is "'F'") *contiguous* or
|
461 |
+
either one (*order* is "'A'"). Return "0" otherwise. This
|
462 |
+
function always succeeds.
|
463 |
+
|
464 |
+
void *PyBuffer_GetPointer(const Py_buffer *view, const Py_ssize_t *indices)
|
465 |
+
* Part of the Stable ABI since version 3.11.*
|
466 |
+
|
467 |
+
Get the memory area pointed to by the *indices* inside the given
|
468 |
+
*view*. *indices* must point to an array of "view->ndim" indices.
|
469 |
+
|
470 |
+
int PyBuffer_FromContiguous(const Py_buffer *view, const void *buf, Py_ssize_t len, char fort)
|
471 |
+
* Part of the Stable ABI since version 3.11.*
|
472 |
+
|
473 |
+
Copy contiguous *len* bytes from *buf* to *view*. *fort* can be
|
474 |
+
"'C'" or "'F'" (for C-style or Fortran-style ordering). "0" is
|
475 |
+
returned on success, "-1" on error.
|
476 |
+
|
477 |
+
int PyBuffer_ToContiguous(void *buf, const Py_buffer *src, Py_ssize_t len, char order)
|
478 |
+
* Part of the Stable ABI since version 3.11.*
|
479 |
+
|
480 |
+
Copy *len* bytes from *src* to its contiguous representation in
|
481 |
+
*buf*. *order* can be "'C'" or "'F'" or "'A'" (for C-style or
|
482 |
+
Fortran-style ordering or either one). "0" is returned on success,
|
483 |
+
"-1" on error.
|
484 |
+
|
485 |
+
This function fails if *len* != *src->len*.
|
486 |
+
|
487 |
+
int PyObject_CopyData(PyObject *dest, PyObject *src)
|
488 |
+
* Part of the Stable ABI since version 3.11.*
|
489 |
+
|
490 |
+
Copy data from *src* to *dest* buffer. Can convert between C-style
|
491 |
+
and or Fortran-style buffers.
|
492 |
+
|
493 |
+
"0" is returned on success, "-1" on error.
|
494 |
+
|
495 |
+
void PyBuffer_FillContiguousStrides(int ndims, Py_ssize_t *shape, Py_ssize_t *strides, int itemsize, char order)
|
496 |
+
* Part of the Stable ABI since version 3.11.*
|
497 |
+
|
498 |
+
Fill the *strides* array with byte-strides of a *contiguous*
|
499 |
+
(C-style if *order* is "'C'" or Fortran-style if *order* is "'F'")
|
500 |
+
array of the given shape with the given number of bytes per
|
501 |
+
element.
|
502 |
+
|
503 |
+
int PyBuffer_FillInfo(Py_buffer *view, PyObject *exporter, void *buf, Py_ssize_t len, int readonly, int flags)
|
504 |
+
* Part of the Stable ABI since version 3.11.*
|
505 |
+
|
506 |
+
Handle buffer requests for an exporter that wants to expose *buf*
|
507 |
+
of size *len* with writability set according to *readonly*. *buf*
|
508 |
+
is interpreted as a sequence of unsigned bytes.
|
509 |
+
|
510 |
+
The *flags* argument indicates the request type. This function
|
511 |
+
always fills in *view* as specified by flags, unless *buf* has been
|
512 |
+
designated as read-only and "PyBUF_WRITABLE" is set in *flags*.
|
513 |
+
|
514 |
+
On success, set "view->obj" to a new reference to *exporter* and
|
515 |
+
return 0. Otherwise, raise "BufferError", set "view->obj" to "NULL"
|
516 |
+
and return "-1";
|
517 |
+
|
518 |
+
If this function is used as part of a getbufferproc, *exporter*
|
519 |
+
MUST be set to the exporting object and *flags* must be passed
|
520 |
+
unmodified. Otherwise, *exporter* MUST be "NULL".
|
python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/bytearray.txt
ADDED
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Byte Array Objects
|
2 |
+
******************
|
3 |
+
|
4 |
+
type PyByteArrayObject
|
5 |
+
|
6 |
+
This subtype of "PyObject" represents a Python bytearray object.
|
7 |
+
|
8 |
+
PyTypeObject PyByteArray_Type
|
9 |
+
* Part of the Stable ABI.*
|
10 |
+
|
11 |
+
This instance of "PyTypeObject" represents the Python bytearray
|
12 |
+
type; it is the same object as "bytearray" in the Python layer.
|
13 |
+
|
14 |
+
|
15 |
+
Type check macros
|
16 |
+
=================
|
17 |
+
|
18 |
+
int PyByteArray_Check(PyObject *o)
|
19 |
+
|
20 |
+
Return true if the object *o* is a bytearray object or an instance
|
21 |
+
of a subtype of the bytearray type. This function always succeeds.
|
22 |
+
|
23 |
+
int PyByteArray_CheckExact(PyObject *o)
|
24 |
+
|
25 |
+
Return true if the object *o* is a bytearray object, but not an
|
26 |
+
instance of a subtype of the bytearray type. This function always
|
27 |
+
succeeds.
|
28 |
+
|
29 |
+
|
30 |
+
Direct API functions
|
31 |
+
====================
|
32 |
+
|
33 |
+
PyObject *PyByteArray_FromObject(PyObject *o)
|
34 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
35 |
+
|
36 |
+
Return a new bytearray object from any object, *o*, that implements
|
37 |
+
the buffer protocol.
|
38 |
+
|
39 |
+
On failure, return "NULL" with an exception set.
|
40 |
+
|
41 |
+
PyObject *PyByteArray_FromStringAndSize(const char *string, Py_ssize_t len)
|
42 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
43 |
+
|
44 |
+
Create a new bytearray object from *string* and its length, *len*.
|
45 |
+
|
46 |
+
On failure, return "NULL" with an exception set.
|
47 |
+
|
48 |
+
PyObject *PyByteArray_Concat(PyObject *a, PyObject *b)
|
49 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
50 |
+
|
51 |
+
Concat bytearrays *a* and *b* and return a new bytearray with the
|
52 |
+
result.
|
53 |
+
|
54 |
+
On failure, return "NULL" with an exception set.
|
55 |
+
|
56 |
+
Py_ssize_t PyByteArray_Size(PyObject *bytearray)
|
57 |
+
* Part of the Stable ABI.*
|
58 |
+
|
59 |
+
Return the size of *bytearray* after checking for a "NULL" pointer.
|
60 |
+
|
61 |
+
char *PyByteArray_AsString(PyObject *bytearray)
|
62 |
+
* Part of the Stable ABI.*
|
63 |
+
|
64 |
+
Return the contents of *bytearray* as a char array after checking
|
65 |
+
for a "NULL" pointer. The returned array always has an extra null
|
66 |
+
byte appended.
|
67 |
+
|
68 |
+
int PyByteArray_Resize(PyObject *bytearray, Py_ssize_t len)
|
69 |
+
* Part of the Stable ABI.*
|
70 |
+
|
71 |
+
Resize the internal buffer of *bytearray* to *len*.
|
72 |
+
|
73 |
+
|
74 |
+
Macros
|
75 |
+
======
|
76 |
+
|
77 |
+
These macros trade safety for speed and they don't check pointers.
|
78 |
+
|
79 |
+
char *PyByteArray_AS_STRING(PyObject *bytearray)
|
80 |
+
|
81 |
+
Similar to "PyByteArray_AsString()", but without error checking.
|
82 |
+
|
83 |
+
Py_ssize_t PyByteArray_GET_SIZE(PyObject *bytearray)
|
84 |
+
|
85 |
+
Similar to "PyByteArray_Size()", but without error checking.
|
python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/bytes.txt
ADDED
@@ -0,0 +1,188 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Bytes Objects
|
2 |
+
*************
|
3 |
+
|
4 |
+
These functions raise "TypeError" when expecting a bytes parameter and
|
5 |
+
called with a non-bytes parameter.
|
6 |
+
|
7 |
+
type PyBytesObject
|
8 |
+
|
9 |
+
This subtype of "PyObject" represents a Python bytes object.
|
10 |
+
|
11 |
+
PyTypeObject PyBytes_Type
|
12 |
+
* Part of the Stable ABI.*
|
13 |
+
|
14 |
+
This instance of "PyTypeObject" represents the Python bytes type;
|
15 |
+
it is the same object as "bytes" in the Python layer.
|
16 |
+
|
17 |
+
int PyBytes_Check(PyObject *o)
|
18 |
+
|
19 |
+
Return true if the object *o* is a bytes object or an instance of a
|
20 |
+
subtype of the bytes type. This function always succeeds.
|
21 |
+
|
22 |
+
int PyBytes_CheckExact(PyObject *o)
|
23 |
+
|
24 |
+
Return true if the object *o* is a bytes object, but not an
|
25 |
+
instance of a subtype of the bytes type. This function always
|
26 |
+
succeeds.
|
27 |
+
|
28 |
+
PyObject *PyBytes_FromString(const char *v)
|
29 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
30 |
+
|
31 |
+
Return a new bytes object with a copy of the string *v* as value on
|
32 |
+
success, and "NULL" on failure. The parameter *v* must not be
|
33 |
+
"NULL"; it will not be checked.
|
34 |
+
|
35 |
+
PyObject *PyBytes_FromStringAndSize(const char *v, Py_ssize_t len)
|
36 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
37 |
+
|
38 |
+
Return a new bytes object with a copy of the string *v* as value
|
39 |
+
and length *len* on success, and "NULL" on failure. If *v* is
|
40 |
+
"NULL", the contents of the bytes object are uninitialized.
|
41 |
+
|
42 |
+
PyObject *PyBytes_FromFormat(const char *format, ...)
|
43 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
44 |
+
|
45 |
+
Take a C "printf()"-style *format* string and a variable number of
|
46 |
+
arguments, calculate the size of the resulting Python bytes object
|
47 |
+
and return a bytes object with the values formatted into it. The
|
48 |
+
variable arguments must be C types and must correspond exactly to
|
49 |
+
the format characters in the *format* string. The following format
|
50 |
+
characters are allowed:
|
51 |
+
|
52 |
+
+---------------------+-----------------+----------------------------------+
|
53 |
+
| Format Characters | Type | Comment |
|
54 |
+
|=====================|=================|==================================|
|
55 |
+
| "%%" | *n/a* | The literal % character. |
|
56 |
+
+---------------------+-----------------+----------------------------------+
|
57 |
+
| "%c" | int | A single byte, represented as a |
|
58 |
+
| | | C int. |
|
59 |
+
+---------------------+-----------------+----------------------------------+
|
60 |
+
| "%d" | int | Equivalent to "printf("%d")". |
|
61 |
+
| | | [1] |
|
62 |
+
+---------------------+-----------------+----------------------------------+
|
63 |
+
| "%u" | unsigned int | Equivalent to "printf("%u")". |
|
64 |
+
| | | [1] |
|
65 |
+
+---------------------+-----------------+----------------------------------+
|
66 |
+
| "%ld" | long | Equivalent to "printf("%ld")". |
|
67 |
+
| | | [1] |
|
68 |
+
+---------------------+-----------------+----------------------------------+
|
69 |
+
| "%lu" | unsigned long | Equivalent to "printf("%lu")". |
|
70 |
+
| | | [1] |
|
71 |
+
+---------------------+-----------------+----------------------------------+
|
72 |
+
| "%zd" | "Py_ssize_t" | Equivalent to "printf("%zd")". |
|
73 |
+
| | | [1] |
|
74 |
+
+---------------------+-----------------+----------------------------------+
|
75 |
+
| "%zu" | size_t | Equivalent to "printf("%zu")". |
|
76 |
+
| | | [1] |
|
77 |
+
+---------------------+-----------------+----------------------------------+
|
78 |
+
| "%i" | int | Equivalent to "printf("%i")". |
|
79 |
+
| | | [1] |
|
80 |
+
+---------------------+-----------------+----------------------------------+
|
81 |
+
| "%x" | int | Equivalent to "printf("%x")". |
|
82 |
+
| | | [1] |
|
83 |
+
+---------------------+-----------------+----------------------------------+
|
84 |
+
| "%s" | const char* | A null-terminated C character |
|
85 |
+
| | | array. |
|
86 |
+
+---------------------+-----------------+----------------------------------+
|
87 |
+
| "%p" | const void* | The hex representation of a C |
|
88 |
+
| | | pointer. Mostly equivalent to |
|
89 |
+
| | | "printf("%p")" except that it is |
|
90 |
+
| | | guaranteed to start with the |
|
91 |
+
| | | literal "0x" regardless of what |
|
92 |
+
| | | the platform's "printf" yields. |
|
93 |
+
+---------------------+-----------------+----------------------------------+
|
94 |
+
|
95 |
+
An unrecognized format character causes all the rest of the format
|
96 |
+
string to be copied as-is to the result object, and any extra
|
97 |
+
arguments discarded.
|
98 |
+
|
99 |
+
[1] For integer specifiers (d, u, ld, lu, zd, zu, i, x): the
|
100 |
+
0-conversion flag has effect even when a precision is given.
|
101 |
+
|
102 |
+
PyObject *PyBytes_FromFormatV(const char *format, va_list vargs)
|
103 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
104 |
+
|
105 |
+
Identical to "PyBytes_FromFormat()" except that it takes exactly
|
106 |
+
two arguments.
|
107 |
+
|
108 |
+
PyObject *PyBytes_FromObject(PyObject *o)
|
109 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
110 |
+
|
111 |
+
Return the bytes representation of object *o* that implements the
|
112 |
+
buffer protocol.
|
113 |
+
|
114 |
+
Py_ssize_t PyBytes_Size(PyObject *o)
|
115 |
+
* Part of the Stable ABI.*
|
116 |
+
|
117 |
+
Return the length of the bytes in bytes object *o*.
|
118 |
+
|
119 |
+
Py_ssize_t PyBytes_GET_SIZE(PyObject *o)
|
120 |
+
|
121 |
+
Similar to "PyBytes_Size()", but without error checking.
|
122 |
+
|
123 |
+
char *PyBytes_AsString(PyObject *o)
|
124 |
+
* Part of the Stable ABI.*
|
125 |
+
|
126 |
+
Return a pointer to the contents of *o*. The pointer refers to the
|
127 |
+
internal buffer of *o*, which consists of "len(o) + 1" bytes. The
|
128 |
+
last byte in the buffer is always null, regardless of whether there
|
129 |
+
are any other null bytes. The data must not be modified in any
|
130 |
+
way, unless the object was just created using
|
131 |
+
"PyBytes_FromStringAndSize(NULL, size)". It must not be
|
132 |
+
deallocated. If *o* is not a bytes object at all,
|
133 |
+
"PyBytes_AsString()" returns "NULL" and raises "TypeError".
|
134 |
+
|
135 |
+
char *PyBytes_AS_STRING(PyObject *string)
|
136 |
+
|
137 |
+
Similar to "PyBytes_AsString()", but without error checking.
|
138 |
+
|
139 |
+
int PyBytes_AsStringAndSize(PyObject *obj, char **buffer, Py_ssize_t *length)
|
140 |
+
* Part of the Stable ABI.*
|
141 |
+
|
142 |
+
Return the null-terminated contents of the object *obj* through the
|
143 |
+
output variables *buffer* and *length*. Returns "0" on success.
|
144 |
+
|
145 |
+
If *length* is "NULL", the bytes object may not contain embedded
|
146 |
+
null bytes; if it does, the function returns "-1" and a
|
147 |
+
"ValueError" is raised.
|
148 |
+
|
149 |
+
The buffer refers to an internal buffer of *obj*, which includes an
|
150 |
+
additional null byte at the end (not counted in *length*). The
|
151 |
+
data must not be modified in any way, unless the object was just
|
152 |
+
created using "PyBytes_FromStringAndSize(NULL, size)". It must not
|
153 |
+
be deallocated. If *obj* is not a bytes object at all,
|
154 |
+
"PyBytes_AsStringAndSize()" returns "-1" and raises "TypeError".
|
155 |
+
|
156 |
+
Changed in version 3.5: Previously, "TypeError" was raised when
|
157 |
+
embedded null bytes were encountered in the bytes object.
|
158 |
+
|
159 |
+
void PyBytes_Concat(PyObject **bytes, PyObject *newpart)
|
160 |
+
* Part of the Stable ABI.*
|
161 |
+
|
162 |
+
Create a new bytes object in **bytes* containing the contents of
|
163 |
+
*newpart* appended to *bytes*; the caller will own the new
|
164 |
+
reference. The reference to the old value of *bytes* will be
|
165 |
+
stolen. If the new object cannot be created, the old reference to
|
166 |
+
*bytes* will still be discarded and the value of **bytes* will be
|
167 |
+
set to "NULL"; the appropriate exception will be set.
|
168 |
+
|
169 |
+
void PyBytes_ConcatAndDel(PyObject **bytes, PyObject *newpart)
|
170 |
+
* Part of the Stable ABI.*
|
171 |
+
|
172 |
+
Create a new bytes object in **bytes* containing the contents of
|
173 |
+
*newpart* appended to *bytes*. This version releases the *strong
|
174 |
+
reference* to *newpart* (i.e. decrements its reference count).
|
175 |
+
|
176 |
+
int _PyBytes_Resize(PyObject **bytes, Py_ssize_t newsize)
|
177 |
+
|
178 |
+
A way to resize a bytes object even though it is "immutable". Only
|
179 |
+
use this to build up a brand new bytes object; don't use this if
|
180 |
+
the bytes may already be known in other parts of the code. It is
|
181 |
+
an error to call this function if the refcount on the input bytes
|
182 |
+
object is not one. Pass the address of an existing bytes object as
|
183 |
+
an lvalue (it may be written into), and the new size desired. On
|
184 |
+
success, **bytes* holds the resized bytes object and "0" is
|
185 |
+
returned; the address in **bytes* may differ from its input value.
|
186 |
+
If the reallocation fails, the original bytes object at **bytes* is
|
187 |
+
deallocated, **bytes* is set to "NULL", "MemoryError" is set, and
|
188 |
+
"-1" is returned.
|
python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/call.txt
ADDED
@@ -0,0 +1,416 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Call Protocol
|
2 |
+
*************
|
3 |
+
|
4 |
+
CPython supports two different calling protocols: *tp_call* and
|
5 |
+
vectorcall.
|
6 |
+
|
7 |
+
|
8 |
+
The *tp_call* Protocol
|
9 |
+
======================
|
10 |
+
|
11 |
+
Instances of classes that set "tp_call" are callable. The signature of
|
12 |
+
the slot is:
|
13 |
+
|
14 |
+
PyObject *tp_call(PyObject *callable, PyObject *args, PyObject *kwargs);
|
15 |
+
|
16 |
+
A call is made using a tuple for the positional arguments and a dict
|
17 |
+
for the keyword arguments, similarly to "callable(*args, **kwargs)" in
|
18 |
+
Python code. *args* must be non-NULL (use an empty tuple if there are
|
19 |
+
no arguments) but *kwargs* may be *NULL* if there are no keyword
|
20 |
+
arguments.
|
21 |
+
|
22 |
+
This convention is not only used by *tp_call*: "tp_new" and "tp_init"
|
23 |
+
also pass arguments this way.
|
24 |
+
|
25 |
+
To call an object, use "PyObject_Call()" or another call API.
|
26 |
+
|
27 |
+
|
28 |
+
The Vectorcall Protocol
|
29 |
+
=======================
|
30 |
+
|
31 |
+
Added in version 3.9.
|
32 |
+
|
33 |
+
The vectorcall protocol was introduced in **PEP 590** as an additional
|
34 |
+
protocol for making calls more efficient.
|
35 |
+
|
36 |
+
As rule of thumb, CPython will prefer the vectorcall for internal
|
37 |
+
calls if the callable supports it. However, this is not a hard rule.
|
38 |
+
Additionally, some third-party extensions use *tp_call* directly
|
39 |
+
(rather than using "PyObject_Call()"). Therefore, a class supporting
|
40 |
+
vectorcall must also implement "tp_call". Moreover, the callable must
|
41 |
+
behave the same regardless of which protocol is used. The recommended
|
42 |
+
way to achieve this is by setting "tp_call" to "PyVectorcall_Call()".
|
43 |
+
This bears repeating:
|
44 |
+
|
45 |
+
Warning:
|
46 |
+
|
47 |
+
A class supporting vectorcall **must** also implement "tp_call" with
|
48 |
+
the same semantics.
|
49 |
+
|
50 |
+
Changed in version 3.12: The "Py_TPFLAGS_HAVE_VECTORCALL" flag is now
|
51 |
+
removed from a class when the class's "__call__()" method is
|
52 |
+
reassigned. (This internally sets "tp_call" only, and thus may make it
|
53 |
+
behave differently than the vectorcall function.) In earlier Python
|
54 |
+
versions, vectorcall should only be used with "immutable" or static
|
55 |
+
types.
|
56 |
+
|
57 |
+
A class should not implement vectorcall if that would be slower than
|
58 |
+
*tp_call*. For example, if the callee needs to convert the arguments
|
59 |
+
to an args tuple and kwargs dict anyway, then there is no point in
|
60 |
+
implementing vectorcall.
|
61 |
+
|
62 |
+
Classes can implement the vectorcall protocol by enabling the
|
63 |
+
"Py_TPFLAGS_HAVE_VECTORCALL" flag and setting "tp_vectorcall_offset"
|
64 |
+
to the offset inside the object structure where a *vectorcallfunc*
|
65 |
+
appears. This is a pointer to a function with the following signature:
|
66 |
+
|
67 |
+
typedef PyObject *(*vectorcallfunc)(PyObject *callable, PyObject *const *args, size_t nargsf, PyObject *kwnames)
|
68 |
+
* Part of the Stable ABI since version 3.12.*
|
69 |
+
|
70 |
+
* *callable* is the object being called.
|
71 |
+
|
72 |
+
* *args* is a C array consisting of the positional arguments followed
|
73 |
+
by the
|
74 |
+
values of the keyword arguments. This can be *NULL* if there are
|
75 |
+
no arguments.
|
76 |
+
|
77 |
+
* *nargsf* is the number of positional arguments plus possibly the
|
78 |
+
"PY_VECTORCALL_ARGUMENTS_OFFSET" flag. To get the actual number
|
79 |
+
of positional arguments from *nargsf*, use
|
80 |
+
"PyVectorcall_NARGS()".
|
81 |
+
|
82 |
+
* *kwnames* is a tuple containing the names of the keyword arguments;
|
83 |
+
in other words, the keys of the kwargs dict. These names must be
|
84 |
+
strings (instances of "str" or a subclass) and they must be
|
85 |
+
unique. If there are no keyword arguments, then *kwnames* can
|
86 |
+
instead be *NULL*.
|
87 |
+
|
88 |
+
PY_VECTORCALL_ARGUMENTS_OFFSET
|
89 |
+
* Part of the Stable ABI since version 3.12.*
|
90 |
+
|
91 |
+
If this flag is set in a vectorcall *nargsf* argument, the callee
|
92 |
+
is allowed to temporarily change "args[-1]". In other words, *args*
|
93 |
+
points to argument 1 (not 0) in the allocated vector. The callee
|
94 |
+
must restore the value of "args[-1]" before returning.
|
95 |
+
|
96 |
+
For "PyObject_VectorcallMethod()", this flag means instead that
|
97 |
+
"args[0]" may be changed.
|
98 |
+
|
99 |
+
Whenever they can do so cheaply (without additional allocation),
|
100 |
+
callers are encouraged to use "PY_VECTORCALL_ARGUMENTS_OFFSET".
|
101 |
+
Doing so will allow callables such as bound methods to make their
|
102 |
+
onward calls (which include a prepended *self* argument) very
|
103 |
+
efficiently.
|
104 |
+
|
105 |
+
Added in version 3.8.
|
106 |
+
|
107 |
+
To call an object that implements vectorcall, use a call API function
|
108 |
+
as with any other callable. "PyObject_Vectorcall()" will usually be
|
109 |
+
most efficient.
|
110 |
+
|
111 |
+
Note:
|
112 |
+
|
113 |
+
In CPython 3.8, the vectorcall API and related functions were
|
114 |
+
available provisionally under names with a leading underscore:
|
115 |
+
"_PyObject_Vectorcall", "_Py_TPFLAGS_HAVE_VECTORCALL",
|
116 |
+
"_PyObject_VectorcallMethod", "_PyVectorcall_Function",
|
117 |
+
"_PyObject_CallOneArg", "_PyObject_CallMethodNoArgs",
|
118 |
+
"_PyObject_CallMethodOneArg". Additionally,
|
119 |
+
"PyObject_VectorcallDict" was available as "_PyObject_FastCallDict".
|
120 |
+
The old names are still defined as aliases of the new, non-
|
121 |
+
underscored names.
|
122 |
+
|
123 |
+
|
124 |
+
Recursion Control
|
125 |
+
-----------------
|
126 |
+
|
127 |
+
When using *tp_call*, callees do not need to worry about recursion:
|
128 |
+
CPython uses "Py_EnterRecursiveCall()" and "Py_LeaveRecursiveCall()"
|
129 |
+
for calls made using *tp_call*.
|
130 |
+
|
131 |
+
For efficiency, this is not the case for calls done using vectorcall:
|
132 |
+
the callee should use *Py_EnterRecursiveCall* and
|
133 |
+
*Py_LeaveRecursiveCall* if needed.
|
134 |
+
|
135 |
+
|
136 |
+
Vectorcall Support API
|
137 |
+
----------------------
|
138 |
+
|
139 |
+
Py_ssize_t PyVectorcall_NARGS(size_t nargsf)
|
140 |
+
* Part of the Stable ABI since version 3.12.*
|
141 |
+
|
142 |
+
Given a vectorcall *nargsf* argument, return the actual number of
|
143 |
+
arguments. Currently equivalent to:
|
144 |
+
|
145 |
+
(Py_ssize_t)(nargsf & ~PY_VECTORCALL_ARGUMENTS_OFFSET)
|
146 |
+
|
147 |
+
However, the function "PyVectorcall_NARGS" should be used to allow
|
148 |
+
for future extensions.
|
149 |
+
|
150 |
+
Added in version 3.8.
|
151 |
+
|
152 |
+
vectorcallfunc PyVectorcall_Function(PyObject *op)
|
153 |
+
|
154 |
+
If *op* does not support the vectorcall protocol (either because
|
155 |
+
the type does not or because the specific instance does not),
|
156 |
+
return *NULL*. Otherwise, return the vectorcall function pointer
|
157 |
+
stored in *op*. This function never raises an exception.
|
158 |
+
|
159 |
+
This is mostly useful to check whether or not *op* supports
|
160 |
+
vectorcall, which can be done by checking
|
161 |
+
"PyVectorcall_Function(op) != NULL".
|
162 |
+
|
163 |
+
Added in version 3.9.
|
164 |
+
|
165 |
+
PyObject *PyVectorcall_Call(PyObject *callable, PyObject *tuple, PyObject *dict)
|
166 |
+
* Part of the Stable ABI since version 3.12.*
|
167 |
+
|
168 |
+
Call *callable*'s "vectorcallfunc" with positional and keyword
|
169 |
+
arguments given in a tuple and dict, respectively.
|
170 |
+
|
171 |
+
This is a specialized function, intended to be put in the "tp_call"
|
172 |
+
slot or be used in an implementation of "tp_call". It does not
|
173 |
+
check the "Py_TPFLAGS_HAVE_VECTORCALL" flag and it does not fall
|
174 |
+
back to "tp_call".
|
175 |
+
|
176 |
+
Added in version 3.8.
|
177 |
+
|
178 |
+
|
179 |
+
Object Calling API
|
180 |
+
==================
|
181 |
+
|
182 |
+
Various functions are available for calling a Python object. Each
|
183 |
+
converts its arguments to a convention supported by the called object
|
184 |
+
– either *tp_call* or vectorcall. In order to do as little conversion
|
185 |
+
as possible, pick one that best fits the format of data you have
|
186 |
+
available.
|
187 |
+
|
188 |
+
The following table summarizes the available functions; please see
|
189 |
+
individual documentation for details.
|
190 |
+
|
191 |
+
+--------------------------------------------+--------------------+----------------------+-----------------+
|
192 |
+
| Function | callable | args | kwargs |
|
193 |
+
|============================================|====================|======================|=================|
|
194 |
+
| "PyObject_Call()" | "PyObject *" | tuple | dict/"NULL" |
|
195 |
+
+--------------------------------------------+--------------------+----------------------+-----------------+
|
196 |
+
| "PyObject_CallNoArgs()" | "PyObject *" | --- | --- |
|
197 |
+
+--------------------------------------------+--------------------+----------------------+-----------------+
|
198 |
+
| "PyObject_CallOneArg()" | "PyObject *" | 1 object | --- |
|
199 |
+
+--------------------------------------------+--------------------+----------------------+-----------------+
|
200 |
+
| "PyObject_CallObject()" | "PyObject *" | tuple/"NULL" | --- |
|
201 |
+
+--------------------------------------------+--------------------+----------------------+-----------------+
|
202 |
+
| "PyObject_CallFunction()" | "PyObject *" | format | --- |
|
203 |
+
+--------------------------------------------+--------------------+----------------------+-----------------+
|
204 |
+
| "PyObject_CallMethod()" | obj + "char*" | format | --- |
|
205 |
+
+--------------------------------------------+--------------------+----------------------+-----------------+
|
206 |
+
| "PyObject_CallFunctionObjArgs()" | "PyObject *" | variadic | --- |
|
207 |
+
+--------------------------------------------+--------------------+----------------------+-----------------+
|
208 |
+
| "PyObject_CallMethodObjArgs()" | obj + name | variadic | --- |
|
209 |
+
+--------------------------------------------+--------------------+----------------------+-----------------+
|
210 |
+
| "PyObject_CallMethodNoArgs()" | obj + name | --- | --- |
|
211 |
+
+--------------------------------------------+--------------------+----------------------+-----------------+
|
212 |
+
| "PyObject_CallMethodOneArg()" | obj + name | 1 object | --- |
|
213 |
+
+--------------------------------------------+--------------------+----------------------+-----------------+
|
214 |
+
| "PyObject_Vectorcall()" | "PyObject *" | vectorcall | vectorcall |
|
215 |
+
+--------------------------------------------+--------------------+----------------------+-----------------+
|
216 |
+
| "PyObject_VectorcallDict()" | "PyObject *" | vectorcall | dict/"NULL" |
|
217 |
+
+--------------------------------------------+--------------------+----------------------+-----------------+
|
218 |
+
| "PyObject_VectorcallMethod()" | arg + name | vectorcall | vectorcall |
|
219 |
+
+--------------------------------------------+--------------------+----------------------+-----------------+
|
220 |
+
|
221 |
+
PyObject *PyObject_Call(PyObject *callable, PyObject *args, PyObject *kwargs)
|
222 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
223 |
+
|
224 |
+
Call a callable Python object *callable*, with arguments given by
|
225 |
+
the tuple *args*, and named arguments given by the dictionary
|
226 |
+
*kwargs*.
|
227 |
+
|
228 |
+
*args* must not be *NULL*; use an empty tuple if no arguments are
|
229 |
+
needed. If no named arguments are needed, *kwargs* can be *NULL*.
|
230 |
+
|
231 |
+
Return the result of the call on success, or raise an exception and
|
232 |
+
return *NULL* on failure.
|
233 |
+
|
234 |
+
This is the equivalent of the Python expression: "callable(*args,
|
235 |
+
**kwargs)".
|
236 |
+
|
237 |
+
PyObject *PyObject_CallNoArgs(PyObject *callable)
|
238 |
+
*Return value: New reference.** Part of the Stable ABI since
|
239 |
+
version 3.10.*
|
240 |
+
|
241 |
+
Call a callable Python object *callable* without any arguments. It
|
242 |
+
is the most efficient way to call a callable Python object without
|
243 |
+
any argument.
|
244 |
+
|
245 |
+
Return the result of the call on success, or raise an exception and
|
246 |
+
return *NULL* on failure.
|
247 |
+
|
248 |
+
Added in version 3.9.
|
249 |
+
|
250 |
+
PyObject *PyObject_CallOneArg(PyObject *callable, PyObject *arg)
|
251 |
+
*Return value: New reference.*
|
252 |
+
|
253 |
+
Call a callable Python object *callable* with exactly 1 positional
|
254 |
+
argument *arg* and no keyword arguments.
|
255 |
+
|
256 |
+
Return the result of the call on success, or raise an exception and
|
257 |
+
return *NULL* on failure.
|
258 |
+
|
259 |
+
Added in version 3.9.
|
260 |
+
|
261 |
+
PyObject *PyObject_CallObject(PyObject *callable, PyObject *args)
|
262 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
263 |
+
|
264 |
+
Call a callable Python object *callable*, with arguments given by
|
265 |
+
the tuple *args*. If no arguments are needed, then *args* can be
|
266 |
+
*NULL*.
|
267 |
+
|
268 |
+
Return the result of the call on success, or raise an exception and
|
269 |
+
return *NULL* on failure.
|
270 |
+
|
271 |
+
This is the equivalent of the Python expression: "callable(*args)".
|
272 |
+
|
273 |
+
PyObject *PyObject_CallFunction(PyObject *callable, const char *format, ...)
|
274 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
275 |
+
|
276 |
+
Call a callable Python object *callable*, with a variable number of
|
277 |
+
C arguments. The C arguments are described using a
|
278 |
+
"Py_BuildValue()" style format string. The format can be *NULL*,
|
279 |
+
indicating that no arguments are provided.
|
280 |
+
|
281 |
+
Return the result of the call on success, or raise an exception and
|
282 |
+
return *NULL* on failure.
|
283 |
+
|
284 |
+
This is the equivalent of the Python expression: "callable(*args)".
|
285 |
+
|
286 |
+
Note that if you only pass PyObject* args,
|
287 |
+
"PyObject_CallFunctionObjArgs()" is a faster alternative.
|
288 |
+
|
289 |
+
Changed in version 3.4: The type of *format* was changed from "char
|
290 |
+
*".
|
291 |
+
|
292 |
+
PyObject *PyObject_CallMethod(PyObject *obj, const char *name, const char *format, ...)
|
293 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
294 |
+
|
295 |
+
Call the method named *name* of object *obj* with a variable number
|
296 |
+
of C arguments. The C arguments are described by a
|
297 |
+
"Py_BuildValue()" format string that should produce a tuple.
|
298 |
+
|
299 |
+
The format can be *NULL*, indicating that no arguments are
|
300 |
+
provided.
|
301 |
+
|
302 |
+
Return the result of the call on success, or raise an exception and
|
303 |
+
return *NULL* on failure.
|
304 |
+
|
305 |
+
This is the equivalent of the Python expression: "obj.name(arg1,
|
306 |
+
arg2, ...)".
|
307 |
+
|
308 |
+
Note that if you only pass PyObject* args,
|
309 |
+
"PyObject_CallMethodObjArgs()" is a faster alternative.
|
310 |
+
|
311 |
+
Changed in version 3.4: The types of *name* and *format* were
|
312 |
+
changed from "char *".
|
313 |
+
|
314 |
+
PyObject *PyObject_CallFunctionObjArgs(PyObject *callable, ...)
|
315 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
316 |
+
|
317 |
+
Call a callable Python object *callable*, with a variable number of
|
318 |
+
PyObject* arguments. The arguments are provided as a variable
|
319 |
+
number of parameters followed by *NULL*.
|
320 |
+
|
321 |
+
Return the result of the call on success, or raise an exception and
|
322 |
+
return *NULL* on failure.
|
323 |
+
|
324 |
+
This is the equivalent of the Python expression: "callable(arg1,
|
325 |
+
arg2, ...)".
|
326 |
+
|
327 |
+
PyObject *PyObject_CallMethodObjArgs(PyObject *obj, PyObject *name, ...)
|
328 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
329 |
+
|
330 |
+
Call a method of the Python object *obj*, where the name of the
|
331 |
+
method is given as a Python string object in *name*. It is called
|
332 |
+
with a variable number of PyObject* arguments. The arguments are
|
333 |
+
provided as a variable number of parameters followed by *NULL*.
|
334 |
+
|
335 |
+
Return the result of the call on success, or raise an exception and
|
336 |
+
return *NULL* on failure.
|
337 |
+
|
338 |
+
PyObject *PyObject_CallMethodNoArgs(PyObject *obj, PyObject *name)
|
339 |
+
|
340 |
+
Call a method of the Python object *obj* without arguments, where
|
341 |
+
the name of the method is given as a Python string object in
|
342 |
+
*name*.
|
343 |
+
|
344 |
+
Return the result of the call on success, or raise an exception and
|
345 |
+
return *NULL* on failure.
|
346 |
+
|
347 |
+
Added in version 3.9.
|
348 |
+
|
349 |
+
PyObject *PyObject_CallMethodOneArg(PyObject *obj, PyObject *name, PyObject *arg)
|
350 |
+
|
351 |
+
Call a method of the Python object *obj* with a single positional
|
352 |
+
argument *arg*, where the name of the method is given as a Python
|
353 |
+
string object in *name*.
|
354 |
+
|
355 |
+
Return the result of the call on success, or raise an exception and
|
356 |
+
return *NULL* on failure.
|
357 |
+
|
358 |
+
Added in version 3.9.
|
359 |
+
|
360 |
+
PyObject *PyObject_Vectorcall(PyObject *callable, PyObject *const *args, size_t nargsf, PyObject *kwnames)
|
361 |
+
* Part of the Stable ABI since version 3.12.*
|
362 |
+
|
363 |
+
Call a callable Python object *callable*. The arguments are the
|
364 |
+
same as for "vectorcallfunc". If *callable* supports vectorcall,
|
365 |
+
this directly calls the vectorcall function stored in *callable*.
|
366 |
+
|
367 |
+
Return the result of the call on success, or raise an exception and
|
368 |
+
return *NULL* on failure.
|
369 |
+
|
370 |
+
Added in version 3.9.
|
371 |
+
|
372 |
+
PyObject *PyObject_VectorcallDict(PyObject *callable, PyObject *const *args, size_t nargsf, PyObject *kwdict)
|
373 |
+
|
374 |
+
Call *callable* with positional arguments passed exactly as in the
|
375 |
+
vectorcall protocol, but with keyword arguments passed as a
|
376 |
+
dictionary *kwdict*. The *args* array contains only the positional
|
377 |
+
arguments.
|
378 |
+
|
379 |
+
Regardless of which protocol is used internally, a conversion of
|
380 |
+
arguments needs to be done. Therefore, this function should only be
|
381 |
+
used if the caller already has a dictionary ready to use for the
|
382 |
+
keyword arguments, but not a tuple for the positional arguments.
|
383 |
+
|
384 |
+
Added in version 3.9.
|
385 |
+
|
386 |
+
PyObject *PyObject_VectorcallMethod(PyObject *name, PyObject *const *args, size_t nargsf, PyObject *kwnames)
|
387 |
+
* Part of the Stable ABI since version 3.12.*
|
388 |
+
|
389 |
+
Call a method using the vectorcall calling convention. The name of
|
390 |
+
the method is given as a Python string *name*. The object whose
|
391 |
+
method is called is *args[0]*, and the *args* array starting at
|
392 |
+
*args[1]* represents the arguments of the call. There must be at
|
393 |
+
least one positional argument. *nargsf* is the number of positional
|
394 |
+
arguments including *args[0]*, plus
|
395 |
+
"PY_VECTORCALL_ARGUMENTS_OFFSET" if the value of "args[0]" may
|
396 |
+
temporarily be changed. Keyword arguments can be passed just like
|
397 |
+
in "PyObject_Vectorcall()".
|
398 |
+
|
399 |
+
If the object has the "Py_TPFLAGS_METHOD_DESCRIPTOR" feature, this
|
400 |
+
will call the unbound method object with the full *args* vector as
|
401 |
+
arguments.
|
402 |
+
|
403 |
+
Return the result of the call on success, or raise an exception and
|
404 |
+
return *NULL* on failure.
|
405 |
+
|
406 |
+
Added in version 3.9.
|
407 |
+
|
408 |
+
|
409 |
+
Call Support API
|
410 |
+
================
|
411 |
+
|
412 |
+
int PyCallable_Check(PyObject *o)
|
413 |
+
* Part of the Stable ABI.*
|
414 |
+
|
415 |
+
Determine if the object *o* is callable. Return "1" if the object
|
416 |
+
is callable and "0" otherwise. This function always succeeds.
|
python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/capsule.txt
ADDED
@@ -0,0 +1,156 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Capsules
|
2 |
+
********
|
3 |
+
|
4 |
+
Refer to Providing a C API for an Extension Module for more
|
5 |
+
information on using these objects.
|
6 |
+
|
7 |
+
Added in version 3.1.
|
8 |
+
|
9 |
+
type PyCapsule
|
10 |
+
|
11 |
+
This subtype of "PyObject" represents an opaque value, useful for C
|
12 |
+
extension modules who need to pass an opaque value (as a void*
|
13 |
+
pointer) through Python code to other C code. It is often used to
|
14 |
+
make a C function pointer defined in one module available to other
|
15 |
+
modules, so the regular import mechanism can be used to access C
|
16 |
+
APIs defined in dynamically loaded modules.
|
17 |
+
|
18 |
+
type PyCapsule_Destructor
|
19 |
+
* Part of the Stable ABI.*
|
20 |
+
|
21 |
+
The type of a destructor callback for a capsule. Defined as:
|
22 |
+
|
23 |
+
typedef void (*PyCapsule_Destructor)(PyObject *);
|
24 |
+
|
25 |
+
See "PyCapsule_New()" for the semantics of PyCapsule_Destructor
|
26 |
+
callbacks.
|
27 |
+
|
28 |
+
int PyCapsule_CheckExact(PyObject *p)
|
29 |
+
|
30 |
+
Return true if its argument is a "PyCapsule". This function always
|
31 |
+
succeeds.
|
32 |
+
|
33 |
+
PyObject *PyCapsule_New(void *pointer, const char *name, PyCapsule_Destructor destructor)
|
34 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
35 |
+
|
36 |
+
Create a "PyCapsule" encapsulating the *pointer*. The *pointer*
|
37 |
+
argument may not be "NULL".
|
38 |
+
|
39 |
+
On failure, set an exception and return "NULL".
|
40 |
+
|
41 |
+
The *name* string may either be "NULL" or a pointer to a valid C
|
42 |
+
string. If non-"NULL", this string must outlive the capsule.
|
43 |
+
(Though it is permitted to free it inside the *destructor*.)
|
44 |
+
|
45 |
+
If the *destructor* argument is not "NULL", it will be called with
|
46 |
+
the capsule as its argument when it is destroyed.
|
47 |
+
|
48 |
+
If this capsule will be stored as an attribute of a module, the
|
49 |
+
*name* should be specified as "modulename.attributename". This
|
50 |
+
will enable other modules to import the capsule using
|
51 |
+
"PyCapsule_Import()".
|
52 |
+
|
53 |
+
void *PyCapsule_GetPointer(PyObject *capsule, const char *name)
|
54 |
+
* Part of the Stable ABI.*
|
55 |
+
|
56 |
+
Retrieve the *pointer* stored in the capsule. On failure, set an
|
57 |
+
exception and return "NULL".
|
58 |
+
|
59 |
+
The *name* parameter must compare exactly to the name stored in the
|
60 |
+
capsule. If the name stored in the capsule is "NULL", the *name*
|
61 |
+
passed in must also be "NULL". Python uses the C function
|
62 |
+
"strcmp()" to compare capsule names.
|
63 |
+
|
64 |
+
PyCapsule_Destructor PyCapsule_GetDestructor(PyObject *capsule)
|
65 |
+
* Part of the Stable ABI.*
|
66 |
+
|
67 |
+
Return the current destructor stored in the capsule. On failure,
|
68 |
+
set an exception and return "NULL".
|
69 |
+
|
70 |
+
It is legal for a capsule to have a "NULL" destructor. This makes
|
71 |
+
a "NULL" return code somewhat ambiguous; use "PyCapsule_IsValid()"
|
72 |
+
or "PyErr_Occurred()" to disambiguate.
|
73 |
+
|
74 |
+
void *PyCapsule_GetContext(PyObject *capsule)
|
75 |
+
* Part of the Stable ABI.*
|
76 |
+
|
77 |
+
Return the current context stored in the capsule. On failure, set
|
78 |
+
an exception and return "NULL".
|
79 |
+
|
80 |
+
It is legal for a capsule to have a "NULL" context. This makes a
|
81 |
+
"NULL" return code somewhat ambiguous; use "PyCapsule_IsValid()" or
|
82 |
+
"PyErr_Occurred()" to disambiguate.
|
83 |
+
|
84 |
+
const char *PyCapsule_GetName(PyObject *capsule)
|
85 |
+
* Part of the Stable ABI.*
|
86 |
+
|
87 |
+
Return the current name stored in the capsule. On failure, set an
|
88 |
+
exception and return "NULL".
|
89 |
+
|
90 |
+
It is legal for a capsule to have a "NULL" name. This makes a
|
91 |
+
"NULL" return code somewhat ambiguous; use "PyCapsule_IsValid()" or
|
92 |
+
"PyErr_Occurred()" to disambiguate.
|
93 |
+
|
94 |
+
void *PyCapsule_Import(const char *name, int no_block)
|
95 |
+
* Part of the Stable ABI.*
|
96 |
+
|
97 |
+
Import a pointer to a C object from a capsule attribute in a
|
98 |
+
module. The *name* parameter should specify the full name to the
|
99 |
+
attribute, as in "module.attribute". The *name* stored in the
|
100 |
+
capsule must match this string exactly.
|
101 |
+
|
102 |
+
Return the capsule's internal *pointer* on success. On failure,
|
103 |
+
set an exception and return "NULL".
|
104 |
+
|
105 |
+
Changed in version 3.3: *no_block* has no effect anymore.
|
106 |
+
|
107 |
+
int PyCapsule_IsValid(PyObject *capsule, const char *name)
|
108 |
+
* Part of the Stable ABI.*
|
109 |
+
|
110 |
+
Determines whether or not *capsule* is a valid capsule. A valid
|
111 |
+
capsule is non-"NULL", passes "PyCapsule_CheckExact()", has a
|
112 |
+
non-"NULL" pointer stored in it, and its internal name matches the
|
113 |
+
*name* parameter. (See "PyCapsule_GetPointer()" for information on
|
114 |
+
how capsule names are compared.)
|
115 |
+
|
116 |
+
In other words, if "PyCapsule_IsValid()" returns a true value,
|
117 |
+
calls to any of the accessors (any function starting with
|
118 |
+
"PyCapsule_Get") are guaranteed to succeed.
|
119 |
+
|
120 |
+
Return a nonzero value if the object is valid and matches the name
|
121 |
+
passed in. Return "0" otherwise. This function will not fail.
|
122 |
+
|
123 |
+
int PyCapsule_SetContext(PyObject *capsule, void *context)
|
124 |
+
* Part of the Stable ABI.*
|
125 |
+
|
126 |
+
Set the context pointer inside *capsule* to *context*.
|
127 |
+
|
128 |
+
Return "0" on success. Return nonzero and set an exception on
|
129 |
+
failure.
|
130 |
+
|
131 |
+
int PyCapsule_SetDestructor(PyObject *capsule, PyCapsule_Destructor destructor)
|
132 |
+
* Part of the Stable ABI.*
|
133 |
+
|
134 |
+
Set the destructor inside *capsule* to *destructor*.
|
135 |
+
|
136 |
+
Return "0" on success. Return nonzero and set an exception on
|
137 |
+
failure.
|
138 |
+
|
139 |
+
int PyCapsule_SetName(PyObject *capsule, const char *name)
|
140 |
+
* Part of the Stable ABI.*
|
141 |
+
|
142 |
+
Set the name inside *capsule* to *name*. If non-"NULL", the name
|
143 |
+
must outlive the capsule. If the previous *name* stored in the
|
144 |
+
capsule was not "NULL", no attempt is made to free it.
|
145 |
+
|
146 |
+
Return "0" on success. Return nonzero and set an exception on
|
147 |
+
failure.
|
148 |
+
|
149 |
+
int PyCapsule_SetPointer(PyObject *capsule, void *pointer)
|
150 |
+
* Part of the Stable ABI.*
|
151 |
+
|
152 |
+
Set the void pointer inside *capsule* to *pointer*. The pointer
|
153 |
+
may not be "NULL".
|
154 |
+
|
155 |
+
Return "0" on success. Return nonzero and set an exception on
|
156 |
+
failure.
|
python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/cell.txt
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Cell Objects
|
2 |
+
************
|
3 |
+
|
4 |
+
"Cell" objects are used to implement variables referenced by multiple
|
5 |
+
scopes. For each such variable, a cell object is created to store the
|
6 |
+
value; the local variables of each stack frame that references the
|
7 |
+
value contains a reference to the cells from outer scopes which also
|
8 |
+
use that variable. When the value is accessed, the value contained in
|
9 |
+
the cell is used instead of the cell object itself. This de-
|
10 |
+
referencing of the cell object requires support from the generated
|
11 |
+
byte-code; these are not automatically de-referenced when accessed.
|
12 |
+
Cell objects are not likely to be useful elsewhere.
|
13 |
+
|
14 |
+
type PyCellObject
|
15 |
+
|
16 |
+
The C structure used for cell objects.
|
17 |
+
|
18 |
+
PyTypeObject PyCell_Type
|
19 |
+
|
20 |
+
The type object corresponding to cell objects.
|
21 |
+
|
22 |
+
int PyCell_Check(PyObject *ob)
|
23 |
+
|
24 |
+
Return true if *ob* is a cell object; *ob* must not be "NULL".
|
25 |
+
This function always succeeds.
|
26 |
+
|
27 |
+
PyObject *PyCell_New(PyObject *ob)
|
28 |
+
*Return value: New reference.*
|
29 |
+
|
30 |
+
Create and return a new cell object containing the value *ob*. The
|
31 |
+
parameter may be "NULL".
|
32 |
+
|
33 |
+
PyObject *PyCell_Get(PyObject *cell)
|
34 |
+
*Return value: New reference.*
|
35 |
+
|
36 |
+
Return the contents of the cell *cell*, which can be "NULL". If
|
37 |
+
*cell* is not a cell object, returns "NULL" with an exception set.
|
38 |
+
|
39 |
+
PyObject *PyCell_GET(PyObject *cell)
|
40 |
+
*Return value: Borrowed reference.*
|
41 |
+
|
42 |
+
Return the contents of the cell *cell*, but without checking that
|
43 |
+
*cell* is non-"NULL" and a cell object.
|
44 |
+
|
45 |
+
int PyCell_Set(PyObject *cell, PyObject *value)
|
46 |
+
|
47 |
+
Set the contents of the cell object *cell* to *value*. This
|
48 |
+
releases the reference to any current content of the cell. *value*
|
49 |
+
may be "NULL". *cell* must be non-"NULL".
|
50 |
+
|
51 |
+
On success, return "0". If *cell* is not a cell object, set an
|
52 |
+
exception and return "-1".
|
53 |
+
|
54 |
+
void PyCell_SET(PyObject *cell, PyObject *value)
|
55 |
+
|
56 |
+
Sets the value of the cell object *cell* to *value*. No reference
|
57 |
+
counts are adjusted, and no checks are made for safety; *cell* must
|
58 |
+
be non-"NULL" and must be a cell object.
|
python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/code.txt
ADDED
@@ -0,0 +1,263 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Code Objects
|
2 |
+
************
|
3 |
+
|
4 |
+
Code objects are a low-level detail of the CPython implementation.
|
5 |
+
Each one represents a chunk of executable code that hasn't yet been
|
6 |
+
bound into a function.
|
7 |
+
|
8 |
+
type PyCodeObject
|
9 |
+
|
10 |
+
The C structure of the objects used to describe code objects. The
|
11 |
+
fields of this type are subject to change at any time.
|
12 |
+
|
13 |
+
PyTypeObject PyCode_Type
|
14 |
+
|
15 |
+
This is an instance of "PyTypeObject" representing the Python code
|
16 |
+
object.
|
17 |
+
|
18 |
+
int PyCode_Check(PyObject *co)
|
19 |
+
|
20 |
+
Return true if *co* is a code object. This function always
|
21 |
+
succeeds.
|
22 |
+
|
23 |
+
Py_ssize_t PyCode_GetNumFree(PyCodeObject *co)
|
24 |
+
|
25 |
+
Return the number of free variables in a code object.
|
26 |
+
|
27 |
+
int PyCode_GetFirstFree(PyCodeObject *co)
|
28 |
+
|
29 |
+
Return the position of the first free variable in a code object.
|
30 |
+
|
31 |
+
PyCodeObject *PyUnstable_Code_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, PyObject *qualname, int firstlineno, PyObject *linetable, PyObject *exceptiontable)
|
32 |
+
|
33 |
+
*This is Unstable API. It may change without warning in minor
|
34 |
+
releases.*
|
35 |
+
|
36 |
+
Return a new code object. If you need a dummy code object to
|
37 |
+
create a frame, use "PyCode_NewEmpty()" instead.
|
38 |
+
|
39 |
+
Since the definition of the bytecode changes often, calling
|
40 |
+
"PyUnstable_Code_New()" directly can bind you to a precise Python
|
41 |
+
version.
|
42 |
+
|
43 |
+
The many arguments of this function are inter-dependent in complex
|
44 |
+
ways, meaning that subtle changes to values are likely to result in
|
45 |
+
incorrect execution or VM crashes. Use this function only with
|
46 |
+
extreme care.
|
47 |
+
|
48 |
+
Changed in version 3.11: Added "qualname" and "exceptiontable"
|
49 |
+
parameters.
|
50 |
+
|
51 |
+
Changed in version 3.12: Renamed from "PyCode_New" as part of
|
52 |
+
Unstable C API. The old name is deprecated, but will remain
|
53 |
+
available until the signature changes again.
|
54 |
+
|
55 |
+
PyCodeObject *PyUnstable_Code_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, PyObject *qualname, int firstlineno, PyObject *linetable, PyObject *exceptiontable)
|
56 |
+
|
57 |
+
*This is Unstable API. It may change without warning in minor
|
58 |
+
releases.*
|
59 |
+
|
60 |
+
Similar to "PyUnstable_Code_New()", but with an extra
|
61 |
+
"posonlyargcount" for positional-only arguments. The same caveats
|
62 |
+
that apply to "PyUnstable_Code_New" also apply to this function.
|
63 |
+
|
64 |
+
Added in version 3.8: as "PyCode_NewWithPosOnlyArgs"
|
65 |
+
|
66 |
+
Changed in version 3.11: Added "qualname" and "exceptiontable"
|
67 |
+
parameters.
|
68 |
+
|
69 |
+
Changed in version 3.12: Renamed to
|
70 |
+
"PyUnstable_Code_NewWithPosOnlyArgs". The old name is deprecated,
|
71 |
+
but will remain available until the signature changes again.
|
72 |
+
|
73 |
+
PyCodeObject *PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno)
|
74 |
+
*Return value: New reference.*
|
75 |
+
|
76 |
+
Return a new empty code object with the specified filename,
|
77 |
+
function name, and first line number. The resulting code object
|
78 |
+
will raise an "Exception" if executed.
|
79 |
+
|
80 |
+
int PyCode_Addr2Line(PyCodeObject *co, int byte_offset)
|
81 |
+
|
82 |
+
Return the line number of the instruction that occurs on or before
|
83 |
+
"byte_offset" and ends after it. If you just need the line number
|
84 |
+
of a frame, use "PyFrame_GetLineNumber()" instead.
|
85 |
+
|
86 |
+
For efficiently iterating over the line numbers in a code object,
|
87 |
+
use the API described in PEP 626.
|
88 |
+
|
89 |
+
int PyCode_Addr2Location(PyObject *co, int byte_offset, int *start_line, int *start_column, int *end_line, int *end_column)
|
90 |
+
|
91 |
+
Sets the passed "int" pointers to the source code line and column
|
92 |
+
numbers for the instruction at "byte_offset". Sets the value to "0"
|
93 |
+
when information is not available for any particular element.
|
94 |
+
|
95 |
+
Returns "1" if the function succeeds and 0 otherwise.
|
96 |
+
|
97 |
+
Added in version 3.11.
|
98 |
+
|
99 |
+
PyObject *PyCode_GetCode(PyCodeObject *co)
|
100 |
+
|
101 |
+
Equivalent to the Python code "getattr(co, 'co_code')". Returns a
|
102 |
+
strong reference to a "PyBytesObject" representing the bytecode in
|
103 |
+
a code object. On error, "NULL" is returned and an exception is
|
104 |
+
raised.
|
105 |
+
|
106 |
+
This "PyBytesObject" may be created on-demand by the interpreter
|
107 |
+
and does not necessarily represent the bytecode actually executed
|
108 |
+
by CPython. The primary use case for this function is debuggers and
|
109 |
+
profilers.
|
110 |
+
|
111 |
+
Added in version 3.11.
|
112 |
+
|
113 |
+
PyObject *PyCode_GetVarnames(PyCodeObject *co)
|
114 |
+
|
115 |
+
Equivalent to the Python code "getattr(co, 'co_varnames')". Returns
|
116 |
+
a new reference to a "PyTupleObject" containing the names of the
|
117 |
+
local variables. On error, "NULL" is returned and an exception is
|
118 |
+
raised.
|
119 |
+
|
120 |
+
Added in version 3.11.
|
121 |
+
|
122 |
+
PyObject *PyCode_GetCellvars(PyCodeObject *co)
|
123 |
+
|
124 |
+
Equivalent to the Python code "getattr(co, 'co_cellvars')". Returns
|
125 |
+
a new reference to a "PyTupleObject" containing the names of the
|
126 |
+
local variables that are referenced by nested functions. On error,
|
127 |
+
"NULL" is returned and an exception is raised.
|
128 |
+
|
129 |
+
Added in version 3.11.
|
130 |
+
|
131 |
+
PyObject *PyCode_GetFreevars(PyCodeObject *co)
|
132 |
+
|
133 |
+
Equivalent to the Python code "getattr(co, 'co_freevars')". Returns
|
134 |
+
a new reference to a "PyTupleObject" containing the names of the
|
135 |
+
free variables. On error, "NULL" is returned and an exception is
|
136 |
+
raised.
|
137 |
+
|
138 |
+
Added in version 3.11.
|
139 |
+
|
140 |
+
int PyCode_AddWatcher(PyCode_WatchCallback callback)
|
141 |
+
|
142 |
+
Register *callback* as a code object watcher for the current
|
143 |
+
interpreter. Return an ID which may be passed to
|
144 |
+
"PyCode_ClearWatcher()". In case of error (e.g. no more watcher IDs
|
145 |
+
available), return "-1" and set an exception.
|
146 |
+
|
147 |
+
Added in version 3.12.
|
148 |
+
|
149 |
+
int PyCode_ClearWatcher(int watcher_id)
|
150 |
+
|
151 |
+
Clear watcher identified by *watcher_id* previously returned from
|
152 |
+
"PyCode_AddWatcher()" for the current interpreter. Return "0" on
|
153 |
+
success, or "-1" and set an exception on error (e.g. if the given
|
154 |
+
*watcher_id* was never registered.)
|
155 |
+
|
156 |
+
Added in version 3.12.
|
157 |
+
|
158 |
+
type PyCodeEvent
|
159 |
+
|
160 |
+
Enumeration of possible code object watcher events: -
|
161 |
+
"PY_CODE_EVENT_CREATE" - "PY_CODE_EVENT_DESTROY"
|
162 |
+
|
163 |
+
Added in version 3.12.
|
164 |
+
|
165 |
+
typedef int (*PyCode_WatchCallback)(PyCodeEvent event, PyCodeObject *co)
|
166 |
+
|
167 |
+
Type of a code object watcher callback function.
|
168 |
+
|
169 |
+
If *event* is "PY_CODE_EVENT_CREATE", then the callback is invoked
|
170 |
+
after *co* has been fully initialized. Otherwise, the callback is
|
171 |
+
invoked before the destruction of *co* takes place, so the prior
|
172 |
+
state of *co* can be inspected.
|
173 |
+
|
174 |
+
If *event* is "PY_CODE_EVENT_DESTROY", taking a reference in the
|
175 |
+
callback to the about-to-be-destroyed code object will resurrect it
|
176 |
+
and prevent it from being freed at this time. When the resurrected
|
177 |
+
object is destroyed later, any watcher callbacks active at that
|
178 |
+
time will be called again.
|
179 |
+
|
180 |
+
Users of this API should not rely on internal runtime
|
181 |
+
implementation details. Such details may include, but are not
|
182 |
+
limited to, the exact order and timing of creation and destruction
|
183 |
+
of code objects. While changes in these details may result in
|
184 |
+
differences observable by watchers (including whether a callback is
|
185 |
+
invoked or not), it does not change the semantics of the Python
|
186 |
+
code being executed.
|
187 |
+
|
188 |
+
If the callback sets an exception, it must return "-1"; this
|
189 |
+
exception will be printed as an unraisable exception using
|
190 |
+
"PyErr_WriteUnraisable()". Otherwise it should return "0".
|
191 |
+
|
192 |
+
There may already be a pending exception set on entry to the
|
193 |
+
callback. In this case, the callback should return "0" with the
|
194 |
+
same exception still set. This means the callback may not call any
|
195 |
+
other API that can set an exception unless it saves and clears the
|
196 |
+
exception state first, and restores it before returning.
|
197 |
+
|
198 |
+
Added in version 3.12.
|
199 |
+
|
200 |
+
|
201 |
+
Extra information
|
202 |
+
*****************
|
203 |
+
|
204 |
+
To support low-level extensions to frame evaluation, such as external
|
205 |
+
just-in-time compilers, it is possible to attach arbitrary extra data
|
206 |
+
to code objects.
|
207 |
+
|
208 |
+
These functions are part of the unstable C API tier: this
|
209 |
+
functionality is a CPython implementation detail, and the API may
|
210 |
+
change without deprecation warnings.
|
211 |
+
|
212 |
+
Py_ssize_t PyUnstable_Eval_RequestCodeExtraIndex(freefunc free)
|
213 |
+
|
214 |
+
*This is Unstable API. It may change without warning in minor
|
215 |
+
releases.*
|
216 |
+
|
217 |
+
Return a new an opaque index value used to adding data to code
|
218 |
+
objects.
|
219 |
+
|
220 |
+
You generally call this function once (per interpreter) and use the
|
221 |
+
result with "PyCode_GetExtra" and "PyCode_SetExtra" to manipulate
|
222 |
+
data on individual code objects.
|
223 |
+
|
224 |
+
If *free* is not "NULL": when a code object is deallocated, *free*
|
225 |
+
will be called on non-"NULL" data stored under the new index. Use
|
226 |
+
"Py_DecRef()" when storing "PyObject".
|
227 |
+
|
228 |
+
Added in version 3.6: as "_PyEval_RequestCodeExtraIndex"
|
229 |
+
|
230 |
+
Changed in version 3.12: Renamed to
|
231 |
+
"PyUnstable_Eval_RequestCodeExtraIndex". The old private name is
|
232 |
+
deprecated, but will be available until the API changes.
|
233 |
+
|
234 |
+
int PyUnstable_Code_GetExtra(PyObject *code, Py_ssize_t index, void **extra)
|
235 |
+
|
236 |
+
*This is Unstable API. It may change without warning in minor
|
237 |
+
releases.*
|
238 |
+
|
239 |
+
Set *extra* to the extra data stored under the given index. Return
|
240 |
+
0 on success. Set an exception and return -1 on failure.
|
241 |
+
|
242 |
+
If no data was set under the index, set *extra* to "NULL" and
|
243 |
+
return 0 without setting an exception.
|
244 |
+
|
245 |
+
Added in version 3.6: as "_PyCode_GetExtra"
|
246 |
+
|
247 |
+
Changed in version 3.12: Renamed to "PyUnstable_Code_GetExtra". The
|
248 |
+
old private name is deprecated, but will be available until the API
|
249 |
+
changes.
|
250 |
+
|
251 |
+
int PyUnstable_Code_SetExtra(PyObject *code, Py_ssize_t index, void *extra)
|
252 |
+
|
253 |
+
*This is Unstable API. It may change without warning in minor
|
254 |
+
releases.*
|
255 |
+
|
256 |
+
Set the extra data stored under the given index to *extra*. Return
|
257 |
+
0 on success. Set an exception and return -1 on failure.
|
258 |
+
|
259 |
+
Added in version 3.6: as "_PyCode_SetExtra"
|
260 |
+
|
261 |
+
Changed in version 3.12: Renamed to "PyUnstable_Code_SetExtra". The
|
262 |
+
old private name is deprecated, but will be available until the API
|
263 |
+
changes.
|
python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/codec.txt
ADDED
@@ -0,0 +1,152 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Codec registry and support functions
|
2 |
+
************************************
|
3 |
+
|
4 |
+
int PyCodec_Register(PyObject *search_function)
|
5 |
+
* Part of the Stable ABI.*
|
6 |
+
|
7 |
+
Register a new codec search function.
|
8 |
+
|
9 |
+
As side effect, this tries to load the "encodings" package, if not
|
10 |
+
yet done, to make sure that it is always first in the list of
|
11 |
+
search functions.
|
12 |
+
|
13 |
+
int PyCodec_Unregister(PyObject *search_function)
|
14 |
+
* Part of the Stable ABI since version 3.10.*
|
15 |
+
|
16 |
+
Unregister a codec search function and clear the registry's cache.
|
17 |
+
If the search function is not registered, do nothing. Return 0 on
|
18 |
+
success. Raise an exception and return -1 on error.
|
19 |
+
|
20 |
+
Added in version 3.10.
|
21 |
+
|
22 |
+
int PyCodec_KnownEncoding(const char *encoding)
|
23 |
+
* Part of the Stable ABI.*
|
24 |
+
|
25 |
+
Return "1" or "0" depending on whether there is a registered codec
|
26 |
+
for the given *encoding*. This function always succeeds.
|
27 |
+
|
28 |
+
PyObject *PyCodec_Encode(PyObject *object, const char *encoding, const char *errors)
|
29 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
30 |
+
|
31 |
+
Generic codec based encoding API.
|
32 |
+
|
33 |
+
*object* is passed through the encoder function found for the given
|
34 |
+
*encoding* using the error handling method defined by *errors*.
|
35 |
+
*errors* may be "NULL" to use the default method defined for the
|
36 |
+
codec. Raises a "LookupError" if no encoder can be found.
|
37 |
+
|
38 |
+
PyObject *PyCodec_Decode(PyObject *object, const char *encoding, const char *errors)
|
39 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
40 |
+
|
41 |
+
Generic codec based decoding API.
|
42 |
+
|
43 |
+
*object* is passed through the decoder function found for the given
|
44 |
+
*encoding* using the error handling method defined by *errors*.
|
45 |
+
*errors* may be "NULL" to use the default method defined for the
|
46 |
+
codec. Raises a "LookupError" if no encoder can be found.
|
47 |
+
|
48 |
+
|
49 |
+
Codec lookup API
|
50 |
+
================
|
51 |
+
|
52 |
+
In the following functions, the *encoding* string is looked up
|
53 |
+
converted to all lower-case characters, which makes encodings looked
|
54 |
+
up through this mechanism effectively case-insensitive. If no codec
|
55 |
+
is found, a "KeyError" is set and "NULL" returned.
|
56 |
+
|
57 |
+
PyObject *PyCodec_Encoder(const char *encoding)
|
58 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
59 |
+
|
60 |
+
Get an encoder function for the given *encoding*.
|
61 |
+
|
62 |
+
PyObject *PyCodec_Decoder(const char *encoding)
|
63 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
64 |
+
|
65 |
+
Get a decoder function for the given *encoding*.
|
66 |
+
|
67 |
+
PyObject *PyCodec_IncrementalEncoder(const char *encoding, const char *errors)
|
68 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
69 |
+
|
70 |
+
Get an "IncrementalEncoder" object for the given *encoding*.
|
71 |
+
|
72 |
+
PyObject *PyCodec_IncrementalDecoder(const char *encoding, const char *errors)
|
73 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
74 |
+
|
75 |
+
Get an "IncrementalDecoder" object for the given *encoding*.
|
76 |
+
|
77 |
+
PyObject *PyCodec_StreamReader(const char *encoding, PyObject *stream, const char *errors)
|
78 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
79 |
+
|
80 |
+
Get a "StreamReader" factory function for the given *encoding*.
|
81 |
+
|
82 |
+
PyObject *PyCodec_StreamWriter(const char *encoding, PyObject *stream, const char *errors)
|
83 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
84 |
+
|
85 |
+
Get a "StreamWriter" factory function for the given *encoding*.
|
86 |
+
|
87 |
+
|
88 |
+
Registry API for Unicode encoding error handlers
|
89 |
+
================================================
|
90 |
+
|
91 |
+
int PyCodec_RegisterError(const char *name, PyObject *error)
|
92 |
+
* Part of the Stable ABI.*
|
93 |
+
|
94 |
+
Register the error handling callback function *error* under the
|
95 |
+
given *name*. This callback function will be called by a codec when
|
96 |
+
it encounters unencodable characters/undecodable bytes and *name*
|
97 |
+
is specified as the error parameter in the call to the
|
98 |
+
encode/decode function.
|
99 |
+
|
100 |
+
The callback gets a single argument, an instance of
|
101 |
+
"UnicodeEncodeError", "UnicodeDecodeError" or
|
102 |
+
"UnicodeTranslateError" that holds information about the
|
103 |
+
problematic sequence of characters or bytes and their offset in the
|
104 |
+
original string (see Unicode Exception Objects for functions to
|
105 |
+
extract this information). The callback must either raise the
|
106 |
+
given exception, or return a two-item tuple containing the
|
107 |
+
replacement for the problematic sequence, and an integer giving the
|
108 |
+
offset in the original string at which encoding/decoding should be
|
109 |
+
resumed.
|
110 |
+
|
111 |
+
Return "0" on success, "-1" on error.
|
112 |
+
|
113 |
+
PyObject *PyCodec_LookupError(const char *name)
|
114 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
115 |
+
|
116 |
+
Lookup the error handling callback function registered under
|
117 |
+
*name*. As a special case "NULL" can be passed, in which case the
|
118 |
+
error handling callback for "strict" will be returned.
|
119 |
+
|
120 |
+
PyObject *PyCodec_StrictErrors(PyObject *exc)
|
121 |
+
*Return value: Always NULL.** Part of the Stable ABI.*
|
122 |
+
|
123 |
+
Raise *exc* as an exception.
|
124 |
+
|
125 |
+
PyObject *PyCodec_IgnoreErrors(PyObject *exc)
|
126 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
127 |
+
|
128 |
+
Ignore the unicode error, skipping the faulty input.
|
129 |
+
|
130 |
+
PyObject *PyCodec_ReplaceErrors(PyObject *exc)
|
131 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
132 |
+
|
133 |
+
Replace the unicode encode error with "?" or "U+FFFD".
|
134 |
+
|
135 |
+
PyObject *PyCodec_XMLCharRefReplaceErrors(PyObject *exc)
|
136 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
137 |
+
|
138 |
+
Replace the unicode encode error with XML character references.
|
139 |
+
|
140 |
+
PyObject *PyCodec_BackslashReplaceErrors(PyObject *exc)
|
141 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
142 |
+
|
143 |
+
Replace the unicode encode error with backslash escapes ("\x", "\u"
|
144 |
+
and "\U").
|
145 |
+
|
146 |
+
PyObject *PyCodec_NameReplaceErrors(PyObject *exc)
|
147 |
+
*Return value: New reference.** Part of the Stable ABI since
|
148 |
+
version 3.7.*
|
149 |
+
|
150 |
+
Replace the unicode encode error with "\N{...}" escapes.
|
151 |
+
|
152 |
+
Added in version 3.5.
|
python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/complex.txt
ADDED
@@ -0,0 +1,136 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Complex Number Objects
|
2 |
+
**********************
|
3 |
+
|
4 |
+
Python's complex number objects are implemented as two distinct types
|
5 |
+
when viewed from the C API: one is the Python object exposed to
|
6 |
+
Python programs, and the other is a C structure which represents the
|
7 |
+
actual complex number value. The API provides functions for working
|
8 |
+
with both.
|
9 |
+
|
10 |
+
|
11 |
+
Complex Numbers as C Structures
|
12 |
+
===============================
|
13 |
+
|
14 |
+
Note that the functions which accept these structures as parameters
|
15 |
+
and return them as results do so *by value* rather than dereferencing
|
16 |
+
them through pointers. This is consistent throughout the API.
|
17 |
+
|
18 |
+
type Py_complex
|
19 |
+
|
20 |
+
The C structure which corresponds to the value portion of a Python
|
21 |
+
complex number object. Most of the functions for dealing with
|
22 |
+
complex number objects use structures of this type as input or
|
23 |
+
output values, as appropriate.
|
24 |
+
|
25 |
+
double real
|
26 |
+
double imag
|
27 |
+
|
28 |
+
The structure is defined as:
|
29 |
+
|
30 |
+
typedef struct {
|
31 |
+
double real;
|
32 |
+
double imag;
|
33 |
+
} Py_complex;
|
34 |
+
|
35 |
+
Py_complex _Py_c_sum(Py_complex left, Py_complex right)
|
36 |
+
|
37 |
+
Return the sum of two complex numbers, using the C "Py_complex"
|
38 |
+
representation.
|
39 |
+
|
40 |
+
Py_complex _Py_c_diff(Py_complex left, Py_complex right)
|
41 |
+
|
42 |
+
Return the difference between two complex numbers, using the C
|
43 |
+
"Py_complex" representation.
|
44 |
+
|
45 |
+
Py_complex _Py_c_neg(Py_complex num)
|
46 |
+
|
47 |
+
Return the negation of the complex number *num*, using the C
|
48 |
+
"Py_complex" representation.
|
49 |
+
|
50 |
+
Py_complex _Py_c_prod(Py_complex left, Py_complex right)
|
51 |
+
|
52 |
+
Return the product of two complex numbers, using the C "Py_complex"
|
53 |
+
representation.
|
54 |
+
|
55 |
+
Py_complex _Py_c_quot(Py_complex dividend, Py_complex divisor)
|
56 |
+
|
57 |
+
Return the quotient of two complex numbers, using the C
|
58 |
+
"Py_complex" representation.
|
59 |
+
|
60 |
+
If *divisor* is null, this method returns zero and sets "errno" to
|
61 |
+
"EDOM".
|
62 |
+
|
63 |
+
Py_complex _Py_c_pow(Py_complex num, Py_complex exp)
|
64 |
+
|
65 |
+
Return the exponentiation of *num* by *exp*, using the C
|
66 |
+
"Py_complex" representation.
|
67 |
+
|
68 |
+
If *num* is null and *exp* is not a positive real number, this
|
69 |
+
method returns zero and sets "errno" to "EDOM".
|
70 |
+
|
71 |
+
|
72 |
+
Complex Numbers as Python Objects
|
73 |
+
=================================
|
74 |
+
|
75 |
+
type PyComplexObject
|
76 |
+
|
77 |
+
This subtype of "PyObject" represents a Python complex number
|
78 |
+
object.
|
79 |
+
|
80 |
+
PyTypeObject PyComplex_Type
|
81 |
+
* Part of the Stable ABI.*
|
82 |
+
|
83 |
+
This instance of "PyTypeObject" represents the Python complex
|
84 |
+
number type. It is the same object as "complex" in the Python
|
85 |
+
layer.
|
86 |
+
|
87 |
+
int PyComplex_Check(PyObject *p)
|
88 |
+
|
89 |
+
Return true if its argument is a "PyComplexObject" or a subtype of
|
90 |
+
"PyComplexObject". This function always succeeds.
|
91 |
+
|
92 |
+
int PyComplex_CheckExact(PyObject *p)
|
93 |
+
|
94 |
+
Return true if its argument is a "PyComplexObject", but not a
|
95 |
+
subtype of "PyComplexObject". This function always succeeds.
|
96 |
+
|
97 |
+
PyObject *PyComplex_FromCComplex(Py_complex v)
|
98 |
+
*Return value: New reference.*
|
99 |
+
|
100 |
+
Create a new Python complex number object from a C "Py_complex"
|
101 |
+
value. Return "NULL" with an exception set on error.
|
102 |
+
|
103 |
+
PyObject *PyComplex_FromDoubles(double real, double imag)
|
104 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
105 |
+
|
106 |
+
Return a new "PyComplexObject" object from *real* and *imag*.
|
107 |
+
Return "NULL" with an exception set on error.
|
108 |
+
|
109 |
+
double PyComplex_RealAsDouble(PyObject *op)
|
110 |
+
* Part of the Stable ABI.*
|
111 |
+
|
112 |
+
Return the real part of *op* as a C double.
|
113 |
+
|
114 |
+
Upon failure, this method returns "-1.0" with an exception set, so
|
115 |
+
one should call "PyErr_Occurred()" to check for errors.
|
116 |
+
|
117 |
+
double PyComplex_ImagAsDouble(PyObject *op)
|
118 |
+
* Part of the Stable ABI.*
|
119 |
+
|
120 |
+
Return the imaginary part of *op* as a C double.
|
121 |
+
|
122 |
+
Py_complex PyComplex_AsCComplex(PyObject *op)
|
123 |
+
|
124 |
+
Return the "Py_complex" value of the complex number *op*.
|
125 |
+
|
126 |
+
If *op* is not a Python complex number object but has a
|
127 |
+
"__complex__()" method, this method will first be called to convert
|
128 |
+
*op* to a Python complex number object. If "__complex__()" is not
|
129 |
+
defined then it falls back to "__float__()". If "__float__()" is
|
130 |
+
not defined then it falls back to "__index__()".
|
131 |
+
|
132 |
+
Upon failure, this method returns "Py_complex" with "real" set to
|
133 |
+
"-1.0" and with an exception set, so one should call
|
134 |
+
"PyErr_Occurred()" to check for errors.
|
135 |
+
|
136 |
+
Changed in version 3.8: Use "__index__()" if available.
|
python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/concrete.txt
ADDED
@@ -0,0 +1,193 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Concrete Objects Layer
|
2 |
+
**********************
|
3 |
+
|
4 |
+
The functions in this chapter are specific to certain Python object
|
5 |
+
types. Passing them an object of the wrong type is not a good idea; if
|
6 |
+
you receive an object from a Python program and you are not sure that
|
7 |
+
it has the right type, you must perform a type check first; for
|
8 |
+
example, to check that an object is a dictionary, use
|
9 |
+
"PyDict_Check()". The chapter is structured like the "family tree" of
|
10 |
+
Python object types.
|
11 |
+
|
12 |
+
Warning:
|
13 |
+
|
14 |
+
While the functions described in this chapter carefully check the
|
15 |
+
type of the objects which are passed in, many of them do not check
|
16 |
+
for "NULL" being passed instead of a valid object. Allowing "NULL"
|
17 |
+
to be passed in can cause memory access violations and immediate
|
18 |
+
termination of the interpreter.
|
19 |
+
|
20 |
+
|
21 |
+
Fundamental Objects
|
22 |
+
===================
|
23 |
+
|
24 |
+
This section describes Python type objects and the singleton object
|
25 |
+
"None".
|
26 |
+
|
27 |
+
* Type Objects
|
28 |
+
|
29 |
+
* Creating Heap-Allocated Types
|
30 |
+
|
31 |
+
* The "None" Object
|
32 |
+
|
33 |
+
|
34 |
+
Numeric Objects
|
35 |
+
===============
|
36 |
+
|
37 |
+
* Integer Objects
|
38 |
+
|
39 |
+
* Boolean Objects
|
40 |
+
|
41 |
+
* Floating-Point Objects
|
42 |
+
|
43 |
+
* Pack and Unpack functions
|
44 |
+
|
45 |
+
* Pack functions
|
46 |
+
|
47 |
+
* Unpack functions
|
48 |
+
|
49 |
+
* Complex Number Objects
|
50 |
+
|
51 |
+
* Complex Numbers as C Structures
|
52 |
+
|
53 |
+
* Complex Numbers as Python Objects
|
54 |
+
|
55 |
+
|
56 |
+
Sequence Objects
|
57 |
+
================
|
58 |
+
|
59 |
+
Generic operations on sequence objects were discussed in the previous
|
60 |
+
chapter; this section deals with the specific kinds of sequence
|
61 |
+
objects that are intrinsic to the Python language.
|
62 |
+
|
63 |
+
* Bytes Objects
|
64 |
+
|
65 |
+
* Byte Array Objects
|
66 |
+
|
67 |
+
* Type check macros
|
68 |
+
|
69 |
+
* Direct API functions
|
70 |
+
|
71 |
+
* Macros
|
72 |
+
|
73 |
+
* Unicode Objects and Codecs
|
74 |
+
|
75 |
+
* Unicode Objects
|
76 |
+
|
77 |
+
* Unicode Type
|
78 |
+
|
79 |
+
* Unicode Character Properties
|
80 |
+
|
81 |
+
* Creating and accessing Unicode strings
|
82 |
+
|
83 |
+
* Locale Encoding
|
84 |
+
|
85 |
+
* File System Encoding
|
86 |
+
|
87 |
+
* wchar_t Support
|
88 |
+
|
89 |
+
* Built-in Codecs
|
90 |
+
|
91 |
+
* Generic Codecs
|
92 |
+
|
93 |
+
* UTF-8 Codecs
|
94 |
+
|
95 |
+
* UTF-32 Codecs
|
96 |
+
|
97 |
+
* UTF-16 Codecs
|
98 |
+
|
99 |
+
* UTF-7 Codecs
|
100 |
+
|
101 |
+
* Unicode-Escape Codecs
|
102 |
+
|
103 |
+
* Raw-Unicode-Escape Codecs
|
104 |
+
|
105 |
+
* Latin-1 Codecs
|
106 |
+
|
107 |
+
* ASCII Codecs
|
108 |
+
|
109 |
+
* Character Map Codecs
|
110 |
+
|
111 |
+
* MBCS codecs for Windows
|
112 |
+
|
113 |
+
* Methods & Slots
|
114 |
+
|
115 |
+
* Methods and Slot Functions
|
116 |
+
|
117 |
+
* Tuple Objects
|
118 |
+
|
119 |
+
* Struct Sequence Objects
|
120 |
+
|
121 |
+
* List Objects
|
122 |
+
|
123 |
+
|
124 |
+
Container Objects
|
125 |
+
=================
|
126 |
+
|
127 |
+
* Dictionary Objects
|
128 |
+
|
129 |
+
* Set Objects
|
130 |
+
|
131 |
+
|
132 |
+
Function Objects
|
133 |
+
================
|
134 |
+
|
135 |
+
* Function Objects
|
136 |
+
|
137 |
+
* Instance Method Objects
|
138 |
+
|
139 |
+
* Method Objects
|
140 |
+
|
141 |
+
* Cell Objects
|
142 |
+
|
143 |
+
* Code Objects
|
144 |
+
|
145 |
+
* Extra information
|
146 |
+
|
147 |
+
|
148 |
+
Other Objects
|
149 |
+
=============
|
150 |
+
|
151 |
+
* File Objects
|
152 |
+
|
153 |
+
* Module Objects
|
154 |
+
|
155 |
+
* Initializing C modules
|
156 |
+
|
157 |
+
* Single-phase initialization
|
158 |
+
|
159 |
+
* Multi-phase initialization
|
160 |
+
|
161 |
+
* Low-level module creation functions
|
162 |
+
|
163 |
+
* Support functions
|
164 |
+
|
165 |
+
* Module lookup
|
166 |
+
|
167 |
+
* Iterator Objects
|
168 |
+
|
169 |
+
* Descriptor Objects
|
170 |
+
|
171 |
+
* Slice Objects
|
172 |
+
|
173 |
+
* Ellipsis Object
|
174 |
+
|
175 |
+
* MemoryView objects
|
176 |
+
|
177 |
+
* Weak Reference Objects
|
178 |
+
|
179 |
+
* Capsules
|
180 |
+
|
181 |
+
* Frame Objects
|
182 |
+
|
183 |
+
* Internal Frames
|
184 |
+
|
185 |
+
* Generator Objects
|
186 |
+
|
187 |
+
* Coroutine Objects
|
188 |
+
|
189 |
+
* Context Variables Objects
|
190 |
+
|
191 |
+
* DateTime Objects
|
192 |
+
|
193 |
+
* Objects for Type Hinting
|
python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/contextvars.txt
ADDED
@@ -0,0 +1,135 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Context Variables Objects
|
2 |
+
*************************
|
3 |
+
|
4 |
+
Added in version 3.7.
|
5 |
+
|
6 |
+
Changed in version 3.7.1:
|
7 |
+
|
8 |
+
Note:
|
9 |
+
|
10 |
+
In Python 3.7.1 the signatures of all context variables C APIs were
|
11 |
+
**changed** to use "PyObject" pointers instead of "PyContext",
|
12 |
+
"PyContextVar", and "PyContextToken", e.g.:
|
13 |
+
|
14 |
+
// in 3.7.0:
|
15 |
+
PyContext *PyContext_New(void);
|
16 |
+
|
17 |
+
// in 3.7.1+:
|
18 |
+
PyObject *PyContext_New(void);
|
19 |
+
|
20 |
+
See bpo-34762 for more details.
|
21 |
+
|
22 |
+
This section details the public C API for the "contextvars" module.
|
23 |
+
|
24 |
+
type PyContext
|
25 |
+
|
26 |
+
The C structure used to represent a "contextvars.Context" object.
|
27 |
+
|
28 |
+
type PyContextVar
|
29 |
+
|
30 |
+
The C structure used to represent a "contextvars.ContextVar"
|
31 |
+
object.
|
32 |
+
|
33 |
+
type PyContextToken
|
34 |
+
|
35 |
+
The C structure used to represent a "contextvars.Token" object.
|
36 |
+
|
37 |
+
PyTypeObject PyContext_Type
|
38 |
+
|
39 |
+
The type object representing the *context* type.
|
40 |
+
|
41 |
+
PyTypeObject PyContextVar_Type
|
42 |
+
|
43 |
+
The type object representing the *context variable* type.
|
44 |
+
|
45 |
+
PyTypeObject PyContextToken_Type
|
46 |
+
|
47 |
+
The type object representing the *context variable token* type.
|
48 |
+
|
49 |
+
Type-check macros:
|
50 |
+
|
51 |
+
int PyContext_CheckExact(PyObject *o)
|
52 |
+
|
53 |
+
Return true if *o* is of type "PyContext_Type". *o* must not be
|
54 |
+
"NULL". This function always succeeds.
|
55 |
+
|
56 |
+
int PyContextVar_CheckExact(PyObject *o)
|
57 |
+
|
58 |
+
Return true if *o* is of type "PyContextVar_Type". *o* must not be
|
59 |
+
"NULL". This function always succeeds.
|
60 |
+
|
61 |
+
int PyContextToken_CheckExact(PyObject *o)
|
62 |
+
|
63 |
+
Return true if *o* is of type "PyContextToken_Type". *o* must not
|
64 |
+
be "NULL". This function always succeeds.
|
65 |
+
|
66 |
+
Context object management functions:
|
67 |
+
|
68 |
+
PyObject *PyContext_New(void)
|
69 |
+
*Return value: New reference.*
|
70 |
+
|
71 |
+
Create a new empty context object. Returns "NULL" if an error has
|
72 |
+
occurred.
|
73 |
+
|
74 |
+
PyObject *PyContext_Copy(PyObject *ctx)
|
75 |
+
*Return value: New reference.*
|
76 |
+
|
77 |
+
Create a shallow copy of the passed *ctx* context object. Returns
|
78 |
+
"NULL" if an error has occurred.
|
79 |
+
|
80 |
+
PyObject *PyContext_CopyCurrent(void)
|
81 |
+
*Return value: New reference.*
|
82 |
+
|
83 |
+
Create a shallow copy of the current thread context. Returns "NULL"
|
84 |
+
if an error has occurred.
|
85 |
+
|
86 |
+
int PyContext_Enter(PyObject *ctx)
|
87 |
+
|
88 |
+
Set *ctx* as the current context for the current thread. Returns
|
89 |
+
"0" on success, and "-1" on error.
|
90 |
+
|
91 |
+
int PyContext_Exit(PyObject *ctx)
|
92 |
+
|
93 |
+
Deactivate the *ctx* context and restore the previous context as
|
94 |
+
the current context for the current thread. Returns "0" on
|
95 |
+
success, and "-1" on error.
|
96 |
+
|
97 |
+
Context variable functions:
|
98 |
+
|
99 |
+
PyObject *PyContextVar_New(const char *name, PyObject *def)
|
100 |
+
*Return value: New reference.*
|
101 |
+
|
102 |
+
Create a new "ContextVar" object. The *name* parameter is used for
|
103 |
+
introspection and debug purposes. The *def* parameter specifies a
|
104 |
+
default value for the context variable, or "NULL" for no default.
|
105 |
+
If an error has occurred, this function returns "NULL".
|
106 |
+
|
107 |
+
int PyContextVar_Get(PyObject *var, PyObject *default_value, PyObject **value)
|
108 |
+
|
109 |
+
Get the value of a context variable. Returns "-1" if an error has
|
110 |
+
occurred during lookup, and "0" if no error occurred, whether or
|
111 |
+
not a value was found.
|
112 |
+
|
113 |
+
If the context variable was found, *value* will be a pointer to it.
|
114 |
+
If the context variable was *not* found, *value* will point to:
|
115 |
+
|
116 |
+
* *default_value*, if not "NULL";
|
117 |
+
|
118 |
+
* the default value of *var*, if not "NULL";
|
119 |
+
|
120 |
+
* "NULL"
|
121 |
+
|
122 |
+
Except for "NULL", the function returns a new reference.
|
123 |
+
|
124 |
+
PyObject *PyContextVar_Set(PyObject *var, PyObject *value)
|
125 |
+
*Return value: New reference.*
|
126 |
+
|
127 |
+
Set the value of *var* to *value* in the current context. Returns
|
128 |
+
a new token object for this change, or "NULL" if an error has
|
129 |
+
occurred.
|
130 |
+
|
131 |
+
int PyContextVar_Reset(PyObject *var, PyObject *token)
|
132 |
+
|
133 |
+
Reset the state of the *var* context variable to that it was in
|
134 |
+
before "PyContextVar_Set()" that returned the *token* was called.
|
135 |
+
This function returns "0" on success and "-1" on error.
|
python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/conversion.txt
ADDED
@@ -0,0 +1,162 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
String conversion and formatting
|
2 |
+
********************************
|
3 |
+
|
4 |
+
Functions for number conversion and formatted string output.
|
5 |
+
|
6 |
+
int PyOS_snprintf(char *str, size_t size, const char *format, ...)
|
7 |
+
* Part of the Stable ABI.*
|
8 |
+
|
9 |
+
Output not more than *size* bytes to *str* according to the format
|
10 |
+
string *format* and the extra arguments. See the Unix man page
|
11 |
+
*snprintf(3)*.
|
12 |
+
|
13 |
+
int PyOS_vsnprintf(char *str, size_t size, const char *format, va_list va)
|
14 |
+
* Part of the Stable ABI.*
|
15 |
+
|
16 |
+
Output not more than *size* bytes to *str* according to the format
|
17 |
+
string *format* and the variable argument list *va*. Unix man page
|
18 |
+
*vsnprintf(3)*.
|
19 |
+
|
20 |
+
"PyOS_snprintf()" and "PyOS_vsnprintf()" wrap the Standard C library
|
21 |
+
functions "snprintf()" and "vsnprintf()". Their purpose is to
|
22 |
+
guarantee consistent behavior in corner cases, which the Standard C
|
23 |
+
functions do not.
|
24 |
+
|
25 |
+
The wrappers ensure that "str[size-1]" is always "'\0'" upon return.
|
26 |
+
They never write more than *size* bytes (including the trailing
|
27 |
+
"'\0'") into str. Both functions require that "str != NULL", "size >
|
28 |
+
0", "format != NULL" and "size < INT_MAX". Note that this means there
|
29 |
+
is no equivalent to the C99 "n = snprintf(NULL, 0, ...)" which would
|
30 |
+
determine the necessary buffer size.
|
31 |
+
|
32 |
+
The return value (*rv*) for these functions should be interpreted as
|
33 |
+
follows:
|
34 |
+
|
35 |
+
* When "0 <= rv < size", the output conversion was successful and *rv*
|
36 |
+
characters were written to *str* (excluding the trailing "'\0'" byte
|
37 |
+
at "str[rv]").
|
38 |
+
|
39 |
+
* When "rv >= size", the output conversion was truncated and a buffer
|
40 |
+
with "rv + 1" bytes would have been needed to succeed. "str[size-1]"
|
41 |
+
is "'\0'" in this case.
|
42 |
+
|
43 |
+
* When "rv < 0", "something bad happened." "str[size-1]" is "'\0'" in
|
44 |
+
this case too, but the rest of *str* is undefined. The exact cause
|
45 |
+
of the error depends on the underlying platform.
|
46 |
+
|
47 |
+
The following functions provide locale-independent string to number
|
48 |
+
conversions.
|
49 |
+
|
50 |
+
unsigned long PyOS_strtoul(const char *str, char **ptr, int base)
|
51 |
+
* Part of the Stable ABI.*
|
52 |
+
|
53 |
+
Convert the initial part of the string in "str" to an unsigned long
|
54 |
+
value according to the given "base", which must be between "2" and
|
55 |
+
"36" inclusive, or be the special value "0".
|
56 |
+
|
57 |
+
Leading white space and case of characters are ignored. If "base"
|
58 |
+
is zero it looks for a leading "0b", "0o" or "0x" to tell which
|
59 |
+
base. If these are absent it defaults to "10". Base must be 0 or
|
60 |
+
between 2 and 36 (inclusive). If "ptr" is non-"NULL" it will
|
61 |
+
contain a pointer to the end of the scan.
|
62 |
+
|
63 |
+
If the converted value falls out of range of corresponding return
|
64 |
+
type, range error occurs ("errno" is set to "ERANGE") and
|
65 |
+
"ULONG_MAX" is returned. If no conversion can be performed, "0" is
|
66 |
+
returned.
|
67 |
+
|
68 |
+
See also the Unix man page *strtoul(3)*.
|
69 |
+
|
70 |
+
Added in version 3.2.
|
71 |
+
|
72 |
+
long PyOS_strtol(const char *str, char **ptr, int base)
|
73 |
+
* Part of the Stable ABI.*
|
74 |
+
|
75 |
+
Convert the initial part of the string in "str" to an long value
|
76 |
+
according to the given "base", which must be between "2" and "36"
|
77 |
+
inclusive, or be the special value "0".
|
78 |
+
|
79 |
+
Same as "PyOS_strtoul()", but return a long value instead and
|
80 |
+
"LONG_MAX" on overflows.
|
81 |
+
|
82 |
+
See also the Unix man page *strtol(3)*.
|
83 |
+
|
84 |
+
Added in version 3.2.
|
85 |
+
|
86 |
+
double PyOS_string_to_double(const char *s, char **endptr, PyObject *overflow_exception)
|
87 |
+
* Part of the Stable ABI.*
|
88 |
+
|
89 |
+
Convert a string "s" to a double, raising a Python exception on
|
90 |
+
failure. The set of accepted strings corresponds to the set of
|
91 |
+
strings accepted by Python's "float()" constructor, except that "s"
|
92 |
+
must not have leading or trailing whitespace. The conversion is
|
93 |
+
independent of the current locale.
|
94 |
+
|
95 |
+
If "endptr" is "NULL", convert the whole string. Raise
|
96 |
+
"ValueError" and return "-1.0" if the string is not a valid
|
97 |
+
representation of a floating-point number.
|
98 |
+
|
99 |
+
If endptr is not "NULL", convert as much of the string as possible
|
100 |
+
and set "*endptr" to point to the first unconverted character. If
|
101 |
+
no initial segment of the string is the valid representation of a
|
102 |
+
floating-point number, set "*endptr" to point to the beginning of
|
103 |
+
the string, raise ValueError, and return "-1.0".
|
104 |
+
|
105 |
+
If "s" represents a value that is too large to store in a float
|
106 |
+
(for example, ""1e500"" is such a string on many platforms) then if
|
107 |
+
"overflow_exception" is "NULL" return "Py_HUGE_VAL" (with an
|
108 |
+
appropriate sign) and don't set any exception. Otherwise,
|
109 |
+
"overflow_exception" must point to a Python exception object; raise
|
110 |
+
that exception and return "-1.0". In both cases, set "*endptr" to
|
111 |
+
point to the first character after the converted value.
|
112 |
+
|
113 |
+
If any other error occurs during the conversion (for example an
|
114 |
+
out-of-memory error), set the appropriate Python exception and
|
115 |
+
return "-1.0".
|
116 |
+
|
117 |
+
Added in version 3.1.
|
118 |
+
|
119 |
+
char *PyOS_double_to_string(double val, char format_code, int precision, int flags, int *ptype)
|
120 |
+
* Part of the Stable ABI.*
|
121 |
+
|
122 |
+
Convert a double *val* to a string using supplied *format_code*,
|
123 |
+
*precision*, and *flags*.
|
124 |
+
|
125 |
+
*format_code* must be one of "'e'", "'E'", "'f'", "'F'", "'g'",
|
126 |
+
"'G'" or "'r'". For "'r'", the supplied *precision* must be 0 and
|
127 |
+
is ignored. The "'r'" format code specifies the standard "repr()"
|
128 |
+
format.
|
129 |
+
|
130 |
+
*flags* can be zero or more of the values "Py_DTSF_SIGN",
|
131 |
+
"Py_DTSF_ADD_DOT_0", or "Py_DTSF_ALT", or-ed together:
|
132 |
+
|
133 |
+
* "Py_DTSF_SIGN" means to always precede the returned string with a
|
134 |
+
sign character, even if *val* is non-negative.
|
135 |
+
|
136 |
+
* "Py_DTSF_ADD_DOT_0" means to ensure that the returned string will
|
137 |
+
not look like an integer.
|
138 |
+
|
139 |
+
* "Py_DTSF_ALT" means to apply "alternate" formatting rules. See
|
140 |
+
the documentation for the "PyOS_snprintf()" "'#'" specifier for
|
141 |
+
details.
|
142 |
+
|
143 |
+
If *ptype* is non-"NULL", then the value it points to will be set
|
144 |
+
to one of "Py_DTST_FINITE", "Py_DTST_INFINITE", or "Py_DTST_NAN",
|
145 |
+
signifying that *val* is a finite number, an infinite number, or
|
146 |
+
not a number, respectively.
|
147 |
+
|
148 |
+
The return value is a pointer to *buffer* with the converted string
|
149 |
+
or "NULL" if the conversion failed. The caller is responsible for
|
150 |
+
freeing the returned string by calling "PyMem_Free()".
|
151 |
+
|
152 |
+
Added in version 3.1.
|
153 |
+
|
154 |
+
int PyOS_stricmp(const char *s1, const char *s2)
|
155 |
+
|
156 |
+
Case insensitive comparison of strings. The function works almost
|
157 |
+
identically to "strcmp()" except that it ignores the case.
|
158 |
+
|
159 |
+
int PyOS_strnicmp(const char *s1, const char *s2, Py_ssize_t size)
|
160 |
+
|
161 |
+
Case insensitive comparison of strings. The function works almost
|
162 |
+
identically to "strncmp()" except that it ignores the case.
|
python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/coro.txt
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Coroutine Objects
|
2 |
+
*****************
|
3 |
+
|
4 |
+
Added in version 3.5.
|
5 |
+
|
6 |
+
Coroutine objects are what functions declared with an "async" keyword
|
7 |
+
return.
|
8 |
+
|
9 |
+
type PyCoroObject
|
10 |
+
|
11 |
+
The C structure used for coroutine objects.
|
12 |
+
|
13 |
+
PyTypeObject PyCoro_Type
|
14 |
+
|
15 |
+
The type object corresponding to coroutine objects.
|
16 |
+
|
17 |
+
int PyCoro_CheckExact(PyObject *ob)
|
18 |
+
|
19 |
+
Return true if *ob*'s type is "PyCoro_Type"; *ob* must not be
|
20 |
+
"NULL". This function always succeeds.
|
21 |
+
|
22 |
+
PyObject *PyCoro_New(PyFrameObject *frame, PyObject *name, PyObject *qualname)
|
23 |
+
*Return value: New reference.*
|
24 |
+
|
25 |
+
Create and return a new coroutine object based on the *frame*
|
26 |
+
object, with "__name__" and "__qualname__" set to *name* and
|
27 |
+
*qualname*. A reference to *frame* is stolen by this function. The
|
28 |
+
*frame* argument must not be "NULL".
|
python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/datetime.txt
ADDED
@@ -0,0 +1,299 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
DateTime Objects
|
2 |
+
****************
|
3 |
+
|
4 |
+
Various date and time objects are supplied by the "datetime" module.
|
5 |
+
Before using any of these functions, the header file "datetime.h" must
|
6 |
+
be included in your source (note that this is not included by
|
7 |
+
"Python.h"), and the macro "PyDateTime_IMPORT" must be invoked,
|
8 |
+
usually as part of the module initialisation function. The macro puts
|
9 |
+
a pointer to a C structure into a static variable, "PyDateTimeAPI",
|
10 |
+
that is used by the following macros.
|
11 |
+
|
12 |
+
type PyDateTime_Date
|
13 |
+
|
14 |
+
This subtype of "PyObject" represents a Python date object.
|
15 |
+
|
16 |
+
type PyDateTime_DateTime
|
17 |
+
|
18 |
+
This subtype of "PyObject" represents a Python datetime object.
|
19 |
+
|
20 |
+
type PyDateTime_Time
|
21 |
+
|
22 |
+
This subtype of "PyObject" represents a Python time object.
|
23 |
+
|
24 |
+
type PyDateTime_Delta
|
25 |
+
|
26 |
+
This subtype of "PyObject" represents the difference between two
|
27 |
+
datetime values.
|
28 |
+
|
29 |
+
PyTypeObject PyDateTime_DateType
|
30 |
+
|
31 |
+
This instance of "PyTypeObject" represents the Python date type; it
|
32 |
+
is the same object as "datetime.date" in the Python layer.
|
33 |
+
|
34 |
+
PyTypeObject PyDateTime_DateTimeType
|
35 |
+
|
36 |
+
This instance of "PyTypeObject" represents the Python datetime
|
37 |
+
type; it is the same object as "datetime.datetime" in the Python
|
38 |
+
layer.
|
39 |
+
|
40 |
+
PyTypeObject PyDateTime_TimeType
|
41 |
+
|
42 |
+
This instance of "PyTypeObject" represents the Python time type; it
|
43 |
+
is the same object as "datetime.time" in the Python layer.
|
44 |
+
|
45 |
+
PyTypeObject PyDateTime_DeltaType
|
46 |
+
|
47 |
+
This instance of "PyTypeObject" represents Python type for the
|
48 |
+
difference between two datetime values; it is the same object as
|
49 |
+
"datetime.timedelta" in the Python layer.
|
50 |
+
|
51 |
+
PyTypeObject PyDateTime_TZInfoType
|
52 |
+
|
53 |
+
This instance of "PyTypeObject" represents the Python time zone
|
54 |
+
info type; it is the same object as "datetime.tzinfo" in the Python
|
55 |
+
layer.
|
56 |
+
|
57 |
+
Macro for access to the UTC singleton:
|
58 |
+
|
59 |
+
PyObject *PyDateTime_TimeZone_UTC
|
60 |
+
|
61 |
+
Returns the time zone singleton representing UTC, the same object
|
62 |
+
as "datetime.timezone.utc".
|
63 |
+
|
64 |
+
Added in version 3.7.
|
65 |
+
|
66 |
+
Type-check macros:
|
67 |
+
|
68 |
+
int PyDate_Check(PyObject *ob)
|
69 |
+
|
70 |
+
Return true if *ob* is of type "PyDateTime_DateType" or a subtype
|
71 |
+
of "PyDateTime_DateType". *ob* must not be "NULL". This function
|
72 |
+
always succeeds.
|
73 |
+
|
74 |
+
int PyDate_CheckExact(PyObject *ob)
|
75 |
+
|
76 |
+
Return true if *ob* is of type "PyDateTime_DateType". *ob* must not
|
77 |
+
be "NULL". This function always succeeds.
|
78 |
+
|
79 |
+
int PyDateTime_Check(PyObject *ob)
|
80 |
+
|
81 |
+
Return true if *ob* is of type "PyDateTime_DateTimeType" or a
|
82 |
+
subtype of "PyDateTime_DateTimeType". *ob* must not be "NULL".
|
83 |
+
This function always succeeds.
|
84 |
+
|
85 |
+
int PyDateTime_CheckExact(PyObject *ob)
|
86 |
+
|
87 |
+
Return true if *ob* is of type "PyDateTime_DateTimeType". *ob* must
|
88 |
+
not be "NULL". This function always succeeds.
|
89 |
+
|
90 |
+
int PyTime_Check(PyObject *ob)
|
91 |
+
|
92 |
+
Return true if *ob* is of type "PyDateTime_TimeType" or a subtype
|
93 |
+
of "PyDateTime_TimeType". *ob* must not be "NULL". This function
|
94 |
+
always succeeds.
|
95 |
+
|
96 |
+
int PyTime_CheckExact(PyObject *ob)
|
97 |
+
|
98 |
+
Return true if *ob* is of type "PyDateTime_TimeType". *ob* must not
|
99 |
+
be "NULL". This function always succeeds.
|
100 |
+
|
101 |
+
int PyDelta_Check(PyObject *ob)
|
102 |
+
|
103 |
+
Return true if *ob* is of type "PyDateTime_DeltaType" or a subtype
|
104 |
+
of "PyDateTime_DeltaType". *ob* must not be "NULL". This function
|
105 |
+
always succeeds.
|
106 |
+
|
107 |
+
int PyDelta_CheckExact(PyObject *ob)
|
108 |
+
|
109 |
+
Return true if *ob* is of type "PyDateTime_DeltaType". *ob* must
|
110 |
+
not be "NULL". This function always succeeds.
|
111 |
+
|
112 |
+
int PyTZInfo_Check(PyObject *ob)
|
113 |
+
|
114 |
+
Return true if *ob* is of type "PyDateTime_TZInfoType" or a subtype
|
115 |
+
of "PyDateTime_TZInfoType". *ob* must not be "NULL". This
|
116 |
+
function always succeeds.
|
117 |
+
|
118 |
+
int PyTZInfo_CheckExact(PyObject *ob)
|
119 |
+
|
120 |
+
Return true if *ob* is of type "PyDateTime_TZInfoType". *ob* must
|
121 |
+
not be "NULL". This function always succeeds.
|
122 |
+
|
123 |
+
Macros to create objects:
|
124 |
+
|
125 |
+
PyObject *PyDate_FromDate(int year, int month, int day)
|
126 |
+
*Return value: New reference.*
|
127 |
+
|
128 |
+
Return a "datetime.date" object with the specified year, month and
|
129 |
+
day.
|
130 |
+
|
131 |
+
PyObject *PyDateTime_FromDateAndTime(int year, int month, int day, int hour, int minute, int second, int usecond)
|
132 |
+
*Return value: New reference.*
|
133 |
+
|
134 |
+
Return a "datetime.datetime" object with the specified year, month,
|
135 |
+
day, hour, minute, second and microsecond.
|
136 |
+
|
137 |
+
PyObject *PyDateTime_FromDateAndTimeAndFold(int year, int month, int day, int hour, int minute, int second, int usecond, int fold)
|
138 |
+
*Return value: New reference.*
|
139 |
+
|
140 |
+
Return a "datetime.datetime" object with the specified year, month,
|
141 |
+
day, hour, minute, second, microsecond and fold.
|
142 |
+
|
143 |
+
Added in version 3.6.
|
144 |
+
|
145 |
+
PyObject *PyTime_FromTime(int hour, int minute, int second, int usecond)
|
146 |
+
*Return value: New reference.*
|
147 |
+
|
148 |
+
Return a "datetime.time" object with the specified hour, minute,
|
149 |
+
second and microsecond.
|
150 |
+
|
151 |
+
PyObject *PyTime_FromTimeAndFold(int hour, int minute, int second, int usecond, int fold)
|
152 |
+
*Return value: New reference.*
|
153 |
+
|
154 |
+
Return a "datetime.time" object with the specified hour, minute,
|
155 |
+
second, microsecond and fold.
|
156 |
+
|
157 |
+
Added in version 3.6.
|
158 |
+
|
159 |
+
PyObject *PyDelta_FromDSU(int days, int seconds, int useconds)
|
160 |
+
*Return value: New reference.*
|
161 |
+
|
162 |
+
Return a "datetime.timedelta" object representing the given number
|
163 |
+
of days, seconds and microseconds. Normalization is performed so
|
164 |
+
that the resulting number of microseconds and seconds lie in the
|
165 |
+
ranges documented for "datetime.timedelta" objects.
|
166 |
+
|
167 |
+
PyObject *PyTimeZone_FromOffset(PyObject *offset)
|
168 |
+
*Return value: New reference.*
|
169 |
+
|
170 |
+
Return a "datetime.timezone" object with an unnamed fixed offset
|
171 |
+
represented by the *offset* argument.
|
172 |
+
|
173 |
+
Added in version 3.7.
|
174 |
+
|
175 |
+
PyObject *PyTimeZone_FromOffsetAndName(PyObject *offset, PyObject *name)
|
176 |
+
*Return value: New reference.*
|
177 |
+
|
178 |
+
Return a "datetime.timezone" object with a fixed offset represented
|
179 |
+
by the *offset* argument and with tzname *name*.
|
180 |
+
|
181 |
+
Added in version 3.7.
|
182 |
+
|
183 |
+
Macros to extract fields from date objects. The argument must be an
|
184 |
+
instance of "PyDateTime_Date", including subclasses (such as
|
185 |
+
"PyDateTime_DateTime"). The argument must not be "NULL", and the type
|
186 |
+
is not checked:
|
187 |
+
|
188 |
+
int PyDateTime_GET_YEAR(PyDateTime_Date *o)
|
189 |
+
|
190 |
+
Return the year, as a positive int.
|
191 |
+
|
192 |
+
int PyDateTime_GET_MONTH(PyDateTime_Date *o)
|
193 |
+
|
194 |
+
Return the month, as an int from 1 through 12.
|
195 |
+
|
196 |
+
int PyDateTime_GET_DAY(PyDateTime_Date *o)
|
197 |
+
|
198 |
+
Return the day, as an int from 1 through 31.
|
199 |
+
|
200 |
+
Macros to extract fields from datetime objects. The argument must be
|
201 |
+
an instance of "PyDateTime_DateTime", including subclasses. The
|
202 |
+
argument must not be "NULL", and the type is not checked:
|
203 |
+
|
204 |
+
int PyDateTime_DATE_GET_HOUR(PyDateTime_DateTime *o)
|
205 |
+
|
206 |
+
Return the hour, as an int from 0 through 23.
|
207 |
+
|
208 |
+
int PyDateTime_DATE_GET_MINUTE(PyDateTime_DateTime *o)
|
209 |
+
|
210 |
+
Return the minute, as an int from 0 through 59.
|
211 |
+
|
212 |
+
int PyDateTime_DATE_GET_SECOND(PyDateTime_DateTime *o)
|
213 |
+
|
214 |
+
Return the second, as an int from 0 through 59.
|
215 |
+
|
216 |
+
int PyDateTime_DATE_GET_MICROSECOND(PyDateTime_DateTime *o)
|
217 |
+
|
218 |
+
Return the microsecond, as an int from 0 through 999999.
|
219 |
+
|
220 |
+
int PyDateTime_DATE_GET_FOLD(PyDateTime_DateTime *o)
|
221 |
+
|
222 |
+
Return the fold, as an int from 0 through 1.
|
223 |
+
|
224 |
+
Added in version 3.6.
|
225 |
+
|
226 |
+
PyObject *PyDateTime_DATE_GET_TZINFO(PyDateTime_DateTime *o)
|
227 |
+
|
228 |
+
Return the tzinfo (which may be "None").
|
229 |
+
|
230 |
+
Added in version 3.10.
|
231 |
+
|
232 |
+
Macros to extract fields from time objects. The argument must be an
|
233 |
+
instance of "PyDateTime_Time", including subclasses. The argument must
|
234 |
+
not be "NULL", and the type is not checked:
|
235 |
+
|
236 |
+
int PyDateTime_TIME_GET_HOUR(PyDateTime_Time *o)
|
237 |
+
|
238 |
+
Return the hour, as an int from 0 through 23.
|
239 |
+
|
240 |
+
int PyDateTime_TIME_GET_MINUTE(PyDateTime_Time *o)
|
241 |
+
|
242 |
+
Return the minute, as an int from 0 through 59.
|
243 |
+
|
244 |
+
int PyDateTime_TIME_GET_SECOND(PyDateTime_Time *o)
|
245 |
+
|
246 |
+
Return the second, as an int from 0 through 59.
|
247 |
+
|
248 |
+
int PyDateTime_TIME_GET_MICROSECOND(PyDateTime_Time *o)
|
249 |
+
|
250 |
+
Return the microsecond, as an int from 0 through 999999.
|
251 |
+
|
252 |
+
int PyDateTime_TIME_GET_FOLD(PyDateTime_Time *o)
|
253 |
+
|
254 |
+
Return the fold, as an int from 0 through 1.
|
255 |
+
|
256 |
+
Added in version 3.6.
|
257 |
+
|
258 |
+
PyObject *PyDateTime_TIME_GET_TZINFO(PyDateTime_Time *o)
|
259 |
+
|
260 |
+
Return the tzinfo (which may be "None").
|
261 |
+
|
262 |
+
Added in version 3.10.
|
263 |
+
|
264 |
+
Macros to extract fields from time delta objects. The argument must
|
265 |
+
be an instance of "PyDateTime_Delta", including subclasses. The
|
266 |
+
argument must not be "NULL", and the type is not checked:
|
267 |
+
|
268 |
+
int PyDateTime_DELTA_GET_DAYS(PyDateTime_Delta *o)
|
269 |
+
|
270 |
+
Return the number of days, as an int from -999999999 to 999999999.
|
271 |
+
|
272 |
+
Added in version 3.3.
|
273 |
+
|
274 |
+
int PyDateTime_DELTA_GET_SECONDS(PyDateTime_Delta *o)
|
275 |
+
|
276 |
+
Return the number of seconds, as an int from 0 through 86399.
|
277 |
+
|
278 |
+
Added in version 3.3.
|
279 |
+
|
280 |
+
int PyDateTime_DELTA_GET_MICROSECONDS(PyDateTime_Delta *o)
|
281 |
+
|
282 |
+
Return the number of microseconds, as an int from 0 through 999999.
|
283 |
+
|
284 |
+
Added in version 3.3.
|
285 |
+
|
286 |
+
Macros for the convenience of modules implementing the DB API:
|
287 |
+
|
288 |
+
PyObject *PyDateTime_FromTimestamp(PyObject *args)
|
289 |
+
*Return value: New reference.*
|
290 |
+
|
291 |
+
Create and return a new "datetime.datetime" object given an
|
292 |
+
argument tuple suitable for passing to
|
293 |
+
"datetime.datetime.fromtimestamp()".
|
294 |
+
|
295 |
+
PyObject *PyDate_FromTimestamp(PyObject *args)
|
296 |
+
*Return value: New reference.*
|
297 |
+
|
298 |
+
Create and return a new "datetime.date" object given an argument
|
299 |
+
tuple suitable for passing to "datetime.date.fromtimestamp()".
|
python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/descriptor.txt
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Descriptor Objects
|
2 |
+
******************
|
3 |
+
|
4 |
+
"Descriptors" are objects that describe some attribute of an object.
|
5 |
+
They are found in the dictionary of type objects.
|
6 |
+
|
7 |
+
PyTypeObject PyProperty_Type
|
8 |
+
* Part of the Stable ABI.*
|
9 |
+
|
10 |
+
The type object for the built-in descriptor types.
|
11 |
+
|
12 |
+
PyObject *PyDescr_NewGetSet(PyTypeObject *type, struct PyGetSetDef *getset)
|
13 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
14 |
+
|
15 |
+
PyObject *PyDescr_NewMember(PyTypeObject *type, struct PyMemberDef *meth)
|
16 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
17 |
+
|
18 |
+
PyObject *PyDescr_NewMethod(PyTypeObject *type, struct PyMethodDef *meth)
|
19 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
20 |
+
|
21 |
+
PyObject *PyDescr_NewWrapper(PyTypeObject *type, struct wrapperbase *wrapper, void *wrapped)
|
22 |
+
*Return value: New reference.*
|
23 |
+
|
24 |
+
PyObject *PyDescr_NewClassMethod(PyTypeObject *type, PyMethodDef *method)
|
25 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
26 |
+
|
27 |
+
int PyDescr_IsData(PyObject *descr)
|
28 |
+
|
29 |
+
Return non-zero if the descriptor objects *descr* describes a data
|
30 |
+
attribute, or "0" if it describes a method. *descr* must be a
|
31 |
+
descriptor object; there is no error checking.
|
32 |
+
|
33 |
+
PyObject *PyWrapper_New(PyObject*, PyObject*)
|
34 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/dict.txt
ADDED
@@ -0,0 +1,325 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Dictionary Objects
|
2 |
+
******************
|
3 |
+
|
4 |
+
type PyDictObject
|
5 |
+
|
6 |
+
This subtype of "PyObject" represents a Python dictionary object.
|
7 |
+
|
8 |
+
PyTypeObject PyDict_Type
|
9 |
+
* Part of the Stable ABI.*
|
10 |
+
|
11 |
+
This instance of "PyTypeObject" represents the Python dictionary
|
12 |
+
type. This is the same object as "dict" in the Python layer.
|
13 |
+
|
14 |
+
int PyDict_Check(PyObject *p)
|
15 |
+
|
16 |
+
Return true if *p* is a dict object or an instance of a subtype of
|
17 |
+
the dict type. This function always succeeds.
|
18 |
+
|
19 |
+
int PyDict_CheckExact(PyObject *p)
|
20 |
+
|
21 |
+
Return true if *p* is a dict object, but not an instance of a
|
22 |
+
subtype of the dict type. This function always succeeds.
|
23 |
+
|
24 |
+
PyObject *PyDict_New()
|
25 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
26 |
+
|
27 |
+
Return a new empty dictionary, or "NULL" on failure.
|
28 |
+
|
29 |
+
PyObject *PyDictProxy_New(PyObject *mapping)
|
30 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
31 |
+
|
32 |
+
Return a "types.MappingProxyType" object for a mapping which
|
33 |
+
enforces read-only behavior. This is normally used to create a
|
34 |
+
view to prevent modification of the dictionary for non-dynamic
|
35 |
+
class types.
|
36 |
+
|
37 |
+
void PyDict_Clear(PyObject *p)
|
38 |
+
* Part of the Stable ABI.*
|
39 |
+
|
40 |
+
Empty an existing dictionary of all key-value pairs.
|
41 |
+
|
42 |
+
int PyDict_Contains(PyObject *p, PyObject *key)
|
43 |
+
* Part of the Stable ABI.*
|
44 |
+
|
45 |
+
Determine if dictionary *p* contains *key*. If an item in *p* is
|
46 |
+
matches *key*, return "1", otherwise return "0". On error, return
|
47 |
+
"-1". This is equivalent to the Python expression "key in p".
|
48 |
+
|
49 |
+
PyObject *PyDict_Copy(PyObject *p)
|
50 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
51 |
+
|
52 |
+
Return a new dictionary that contains the same key-value pairs as
|
53 |
+
*p*.
|
54 |
+
|
55 |
+
int PyDict_SetItem(PyObject *p, PyObject *key, PyObject *val)
|
56 |
+
* Part of the Stable ABI.*
|
57 |
+
|
58 |
+
Insert *val* into the dictionary *p* with a key of *key*. *key*
|
59 |
+
must be *hashable*; if it isn't, "TypeError" will be raised. Return
|
60 |
+
"0" on success or "-1" on failure. This function *does not* steal
|
61 |
+
a reference to *val*.
|
62 |
+
|
63 |
+
int PyDict_SetItemString(PyObject *p, const char *key, PyObject *val)
|
64 |
+
* Part of the Stable ABI.*
|
65 |
+
|
66 |
+
This is the same as "PyDict_SetItem()", but *key* is specified as a
|
67 |
+
const char* UTF-8 encoded bytes string, rather than a PyObject*.
|
68 |
+
|
69 |
+
int PyDict_DelItem(PyObject *p, PyObject *key)
|
70 |
+
* Part of the Stable ABI.*
|
71 |
+
|
72 |
+
Remove the entry in dictionary *p* with key *key*. *key* must be
|
73 |
+
*hashable*; if it isn't, "TypeError" is raised. If *key* is not in
|
74 |
+
the dictionary, "KeyError" is raised. Return "0" on success or "-1"
|
75 |
+
on failure.
|
76 |
+
|
77 |
+
int PyDict_DelItemString(PyObject *p, const char *key)
|
78 |
+
* Part of the Stable ABI.*
|
79 |
+
|
80 |
+
This is the same as "PyDict_DelItem()", but *key* is specified as a
|
81 |
+
const char* UTF-8 encoded bytes string, rather than a PyObject*.
|
82 |
+
|
83 |
+
PyObject *PyDict_GetItem(PyObject *p, PyObject *key)
|
84 |
+
*Return value: Borrowed reference.** Part of the Stable ABI.*
|
85 |
+
|
86 |
+
Return the object from dictionary *p* which has a key *key*.
|
87 |
+
Return "NULL" if the key *key* is not present, but *without*
|
88 |
+
setting an exception.
|
89 |
+
|
90 |
+
Note:
|
91 |
+
|
92 |
+
Exceptions that occur while this calls "__hash__()" and
|
93 |
+
"__eq__()" methods are silently ignored. Prefer the
|
94 |
+
"PyDict_GetItemWithError()" function instead.
|
95 |
+
|
96 |
+
Changed in version 3.10: Calling this API without *GIL* held had
|
97 |
+
been allowed for historical reason. It is no longer allowed.
|
98 |
+
|
99 |
+
PyObject *PyDict_GetItemWithError(PyObject *p, PyObject *key)
|
100 |
+
*Return value: Borrowed reference.** Part of the Stable ABI.*
|
101 |
+
|
102 |
+
Variant of "PyDict_GetItem()" that does not suppress exceptions.
|
103 |
+
Return "NULL" **with** an exception set if an exception occurred.
|
104 |
+
Return "NULL" **without** an exception set if the key wasn't
|
105 |
+
present.
|
106 |
+
|
107 |
+
PyObject *PyDict_GetItemString(PyObject *p, const char *key)
|
108 |
+
*Return value: Borrowed reference.** Part of the Stable ABI.*
|
109 |
+
|
110 |
+
This is the same as "PyDict_GetItem()", but *key* is specified as a
|
111 |
+
const char* UTF-8 encoded bytes string, rather than a PyObject*.
|
112 |
+
|
113 |
+
Note:
|
114 |
+
|
115 |
+
Exceptions that occur while this calls "__hash__()" and
|
116 |
+
"__eq__()" methods or while creating the temporary "str" object
|
117 |
+
are silently ignored. Prefer using the
|
118 |
+
"PyDict_GetItemWithError()" function with your own
|
119 |
+
"PyUnicode_FromString()" *key* instead.
|
120 |
+
|
121 |
+
PyObject *PyDict_SetDefault(PyObject *p, PyObject *key, PyObject *defaultobj)
|
122 |
+
*Return value: Borrowed reference.*
|
123 |
+
|
124 |
+
This is the same as the Python-level "dict.setdefault()". If
|
125 |
+
present, it returns the value corresponding to *key* from the
|
126 |
+
dictionary *p*. If the key is not in the dict, it is inserted with
|
127 |
+
value *defaultobj* and *defaultobj* is returned. This function
|
128 |
+
evaluates the hash function of *key* only once, instead of
|
129 |
+
evaluating it independently for the lookup and the insertion.
|
130 |
+
|
131 |
+
Added in version 3.4.
|
132 |
+
|
133 |
+
PyObject *PyDict_Items(PyObject *p)
|
134 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
135 |
+
|
136 |
+
Return a "PyListObject" containing all the items from the
|
137 |
+
dictionary.
|
138 |
+
|
139 |
+
PyObject *PyDict_Keys(PyObject *p)
|
140 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
141 |
+
|
142 |
+
Return a "PyListObject" containing all the keys from the
|
143 |
+
dictionary.
|
144 |
+
|
145 |
+
PyObject *PyDict_Values(PyObject *p)
|
146 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
147 |
+
|
148 |
+
Return a "PyListObject" containing all the values from the
|
149 |
+
dictionary *p*.
|
150 |
+
|
151 |
+
Py_ssize_t PyDict_Size(PyObject *p)
|
152 |
+
* Part of the Stable ABI.*
|
153 |
+
|
154 |
+
Return the number of items in the dictionary. This is equivalent
|
155 |
+
to "len(p)" on a dictionary.
|
156 |
+
|
157 |
+
int PyDict_Next(PyObject *p, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
|
158 |
+
* Part of the Stable ABI.*
|
159 |
+
|
160 |
+
Iterate over all key-value pairs in the dictionary *p*. The
|
161 |
+
"Py_ssize_t" referred to by *ppos* must be initialized to "0" prior
|
162 |
+
to the first call to this function to start the iteration; the
|
163 |
+
function returns true for each pair in the dictionary, and false
|
164 |
+
once all pairs have been reported. The parameters *pkey* and
|
165 |
+
*pvalue* should either point to PyObject* variables that will be
|
166 |
+
filled in with each key and value, respectively, or may be "NULL".
|
167 |
+
Any references returned through them are borrowed. *ppos* should
|
168 |
+
not be altered during iteration. Its value represents offsets
|
169 |
+
within the internal dictionary structure, and since the structure
|
170 |
+
is sparse, the offsets are not consecutive.
|
171 |
+
|
172 |
+
For example:
|
173 |
+
|
174 |
+
PyObject *key, *value;
|
175 |
+
Py_ssize_t pos = 0;
|
176 |
+
|
177 |
+
while (PyDict_Next(self->dict, &pos, &key, &value)) {
|
178 |
+
/* do something interesting with the values... */
|
179 |
+
...
|
180 |
+
}
|
181 |
+
|
182 |
+
The dictionary *p* should not be mutated during iteration. It is
|
183 |
+
safe to modify the values of the keys as you iterate over the
|
184 |
+
dictionary, but only so long as the set of keys does not change.
|
185 |
+
For example:
|
186 |
+
|
187 |
+
PyObject *key, *value;
|
188 |
+
Py_ssize_t pos = 0;
|
189 |
+
|
190 |
+
while (PyDict_Next(self->dict, &pos, &key, &value)) {
|
191 |
+
long i = PyLong_AsLong(value);
|
192 |
+
if (i == -1 && PyErr_Occurred()) {
|
193 |
+
return -1;
|
194 |
+
}
|
195 |
+
PyObject *o = PyLong_FromLong(i + 1);
|
196 |
+
if (o == NULL)
|
197 |
+
return -1;
|
198 |
+
if (PyDict_SetItem(self->dict, key, o) < 0) {
|
199 |
+
Py_DECREF(o);
|
200 |
+
return -1;
|
201 |
+
}
|
202 |
+
Py_DECREF(o);
|
203 |
+
}
|
204 |
+
|
205 |
+
int PyDict_Merge(PyObject *a, PyObject *b, int override)
|
206 |
+
* Part of the Stable ABI.*
|
207 |
+
|
208 |
+
Iterate over mapping object *b* adding key-value pairs to
|
209 |
+
dictionary *a*. *b* may be a dictionary, or any object supporting
|
210 |
+
"PyMapping_Keys()" and "PyObject_GetItem()". If *override* is true,
|
211 |
+
existing pairs in *a* will be replaced if a matching key is found
|
212 |
+
in *b*, otherwise pairs will only be added if there is not a
|
213 |
+
matching key in *a*. Return "0" on success or "-1" if an exception
|
214 |
+
was raised.
|
215 |
+
|
216 |
+
int PyDict_Update(PyObject *a, PyObject *b)
|
217 |
+
* Part of the Stable ABI.*
|
218 |
+
|
219 |
+
This is the same as "PyDict_Merge(a, b, 1)" in C, and is similar to
|
220 |
+
"a.update(b)" in Python except that "PyDict_Update()" doesn't fall
|
221 |
+
back to the iterating over a sequence of key value pairs if the
|
222 |
+
second argument has no "keys" attribute. Return "0" on success or
|
223 |
+
"-1" if an exception was raised.
|
224 |
+
|
225 |
+
int PyDict_MergeFromSeq2(PyObject *a, PyObject *seq2, int override)
|
226 |
+
* Part of the Stable ABI.*
|
227 |
+
|
228 |
+
Update or merge into dictionary *a*, from the key-value pairs in
|
229 |
+
*seq2*. *seq2* must be an iterable object producing iterable
|
230 |
+
objects of length 2, viewed as key-value pairs. In case of
|
231 |
+
duplicate keys, the last wins if *override* is true, else the first
|
232 |
+
wins. Return "0" on success or "-1" if an exception was raised.
|
233 |
+
Equivalent Python (except for the return value):
|
234 |
+
|
235 |
+
def PyDict_MergeFromSeq2(a, seq2, override):
|
236 |
+
for key, value in seq2:
|
237 |
+
if override or key not in a:
|
238 |
+
a[key] = value
|
239 |
+
|
240 |
+
int PyDict_AddWatcher(PyDict_WatchCallback callback)
|
241 |
+
|
242 |
+
Register *callback* as a dictionary watcher. Return a non-negative
|
243 |
+
integer id which must be passed to future calls to
|
244 |
+
"PyDict_Watch()". In case of error (e.g. no more watcher IDs
|
245 |
+
available), return "-1" and set an exception.
|
246 |
+
|
247 |
+
Added in version 3.12.
|
248 |
+
|
249 |
+
int PyDict_ClearWatcher(int watcher_id)
|
250 |
+
|
251 |
+
Clear watcher identified by *watcher_id* previously returned from
|
252 |
+
"PyDict_AddWatcher()". Return "0" on success, "-1" on error (e.g.
|
253 |
+
if the given *watcher_id* was never registered.)
|
254 |
+
|
255 |
+
Added in version 3.12.
|
256 |
+
|
257 |
+
int PyDict_Watch(int watcher_id, PyObject *dict)
|
258 |
+
|
259 |
+
Mark dictionary *dict* as watched. The callback granted
|
260 |
+
*watcher_id* by "PyDict_AddWatcher()" will be called when *dict* is
|
261 |
+
modified or deallocated. Return "0" on success or "-1" on error.
|
262 |
+
|
263 |
+
Added in version 3.12.
|
264 |
+
|
265 |
+
int PyDict_Unwatch(int watcher_id, PyObject *dict)
|
266 |
+
|
267 |
+
Mark dictionary *dict* as no longer watched. The callback granted
|
268 |
+
*watcher_id* by "PyDict_AddWatcher()" will no longer be called when
|
269 |
+
*dict* is modified or deallocated. The dict must previously have
|
270 |
+
been watched by this watcher. Return "0" on success or "-1" on
|
271 |
+
error.
|
272 |
+
|
273 |
+
Added in version 3.12.
|
274 |
+
|
275 |
+
type PyDict_WatchEvent
|
276 |
+
|
277 |
+
Enumeration of possible dictionary watcher events:
|
278 |
+
"PyDict_EVENT_ADDED", "PyDict_EVENT_MODIFIED",
|
279 |
+
"PyDict_EVENT_DELETED", "PyDict_EVENT_CLONED",
|
280 |
+
"PyDict_EVENT_CLEARED", or "PyDict_EVENT_DEALLOCATED".
|
281 |
+
|
282 |
+
Added in version 3.12.
|
283 |
+
|
284 |
+
typedef int (*PyDict_WatchCallback)(PyDict_WatchEvent event, PyObject *dict, PyObject *key, PyObject *new_value)
|
285 |
+
|
286 |
+
Type of a dict watcher callback function.
|
287 |
+
|
288 |
+
If *event* is "PyDict_EVENT_CLEARED" or "PyDict_EVENT_DEALLOCATED",
|
289 |
+
both *key* and *new_value* will be "NULL". If *event* is
|
290 |
+
"PyDict_EVENT_ADDED" or "PyDict_EVENT_MODIFIED", *new_value* will
|
291 |
+
be the new value for *key*. If *event* is "PyDict_EVENT_DELETED",
|
292 |
+
*key* is being deleted from the dictionary and *new_value* will be
|
293 |
+
"NULL".
|
294 |
+
|
295 |
+
"PyDict_EVENT_CLONED" occurs when *dict* was previously empty and
|
296 |
+
another dict is merged into it. To maintain efficiency of this
|
297 |
+
operation, per-key "PyDict_EVENT_ADDED" events are not issued in
|
298 |
+
this case; instead a single "PyDict_EVENT_CLONED" is issued, and
|
299 |
+
*key* will be the source dictionary.
|
300 |
+
|
301 |
+
The callback may inspect but must not modify *dict*; doing so could
|
302 |
+
have unpredictable effects, including infinite recursion. Do not
|
303 |
+
trigger Python code execution in the callback, as it could modify
|
304 |
+
the dict as a side effect.
|
305 |
+
|
306 |
+
If *event* is "PyDict_EVENT_DEALLOCATED", taking a new reference in
|
307 |
+
the callback to the about-to-be-destroyed dictionary will resurrect
|
308 |
+
it and prevent it from being freed at this time. When the
|
309 |
+
resurrected object is destroyed later, any watcher callbacks active
|
310 |
+
at that time will be called again.
|
311 |
+
|
312 |
+
Callbacks occur before the notified modification to *dict* takes
|
313 |
+
place, so the prior state of *dict* can be inspected.
|
314 |
+
|
315 |
+
If the callback sets an exception, it must return "-1"; this
|
316 |
+
exception will be printed as an unraisable exception using
|
317 |
+
"PyErr_WriteUnraisable()". Otherwise it should return "0".
|
318 |
+
|
319 |
+
There may already be a pending exception set on entry to the
|
320 |
+
callback. In this case, the callback should return "0" with the
|
321 |
+
same exception still set. This means the callback may not call any
|
322 |
+
other API that can set an exception unless it saves and clears the
|
323 |
+
exception state first, and restores it before returning.
|
324 |
+
|
325 |
+
Added in version 3.12.
|
python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/exceptions.txt
ADDED
@@ -0,0 +1,1173 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Exception Handling
|
2 |
+
******************
|
3 |
+
|
4 |
+
The functions described in this chapter will let you handle and raise
|
5 |
+
Python exceptions. It is important to understand some of the basics
|
6 |
+
of Python exception handling. It works somewhat like the POSIX
|
7 |
+
"errno" variable: there is a global indicator (per thread) of the last
|
8 |
+
error that occurred. Most C API functions don't clear this on
|
9 |
+
success, but will set it to indicate the cause of the error on
|
10 |
+
failure. Most C API functions also return an error indicator, usually
|
11 |
+
"NULL" if they are supposed to return a pointer, or "-1" if they
|
12 |
+
return an integer (exception: the "PyArg_*" functions return "1" for
|
13 |
+
success and "0" for failure).
|
14 |
+
|
15 |
+
Concretely, the error indicator consists of three object pointers: the
|
16 |
+
exception's type, the exception's value, and the traceback object.
|
17 |
+
Any of those pointers can be "NULL" if non-set (although some
|
18 |
+
combinations are forbidden, for example you can't have a non-"NULL"
|
19 |
+
traceback if the exception type is "NULL").
|
20 |
+
|
21 |
+
When a function must fail because some function it called failed, it
|
22 |
+
generally doesn't set the error indicator; the function it called
|
23 |
+
already set it. It is responsible for either handling the error and
|
24 |
+
clearing the exception or returning after cleaning up any resources it
|
25 |
+
holds (such as object references or memory allocations); it should
|
26 |
+
*not* continue normally if it is not prepared to handle the error. If
|
27 |
+
returning due to an error, it is important to indicate to the caller
|
28 |
+
that an error has been set. If the error is not handled or carefully
|
29 |
+
propagated, additional calls into the Python/C API may not behave as
|
30 |
+
intended and may fail in mysterious ways.
|
31 |
+
|
32 |
+
Note:
|
33 |
+
|
34 |
+
The error indicator is **not** the result of "sys.exc_info()". The
|
35 |
+
former corresponds to an exception that is not yet caught (and is
|
36 |
+
therefore still propagating), while the latter returns an exception
|
37 |
+
after it is caught (and has therefore stopped propagating).
|
38 |
+
|
39 |
+
|
40 |
+
Printing and clearing
|
41 |
+
=====================
|
42 |
+
|
43 |
+
void PyErr_Clear()
|
44 |
+
* Part of the Stable ABI.*
|
45 |
+
|
46 |
+
Clear the error indicator. If the error indicator is not set,
|
47 |
+
there is no effect.
|
48 |
+
|
49 |
+
void PyErr_PrintEx(int set_sys_last_vars)
|
50 |
+
* Part of the Stable ABI.*
|
51 |
+
|
52 |
+
Print a standard traceback to "sys.stderr" and clear the error
|
53 |
+
indicator. **Unless** the error is a "SystemExit", in that case no
|
54 |
+
traceback is printed and the Python process will exit with the
|
55 |
+
error code specified by the "SystemExit" instance.
|
56 |
+
|
57 |
+
Call this function **only** when the error indicator is set.
|
58 |
+
Otherwise it will cause a fatal error!
|
59 |
+
|
60 |
+
If *set_sys_last_vars* is nonzero, the variable "sys.last_exc" is
|
61 |
+
set to the printed exception. For backwards compatibility, the
|
62 |
+
deprecated variables "sys.last_type", "sys.last_value" and
|
63 |
+
"sys.last_traceback" are also set to the type, value and traceback
|
64 |
+
of this exception, respectively.
|
65 |
+
|
66 |
+
Changed in version 3.12: The setting of "sys.last_exc" was added.
|
67 |
+
|
68 |
+
void PyErr_Print()
|
69 |
+
* Part of the Stable ABI.*
|
70 |
+
|
71 |
+
Alias for "PyErr_PrintEx(1)".
|
72 |
+
|
73 |
+
void PyErr_WriteUnraisable(PyObject *obj)
|
74 |
+
* Part of the Stable ABI.*
|
75 |
+
|
76 |
+
Call "sys.unraisablehook()" using the current exception and *obj*
|
77 |
+
argument.
|
78 |
+
|
79 |
+
This utility function prints a warning message to "sys.stderr" when
|
80 |
+
an exception has been set but it is impossible for the interpreter
|
81 |
+
to actually raise the exception. It is used, for example, when an
|
82 |
+
exception occurs in an "__del__()" method.
|
83 |
+
|
84 |
+
The function is called with a single argument *obj* that identifies
|
85 |
+
the context in which the unraisable exception occurred. If
|
86 |
+
possible, the repr of *obj* will be printed in the warning message.
|
87 |
+
If *obj* is "NULL", only the traceback is printed.
|
88 |
+
|
89 |
+
An exception must be set when calling this function.
|
90 |
+
|
91 |
+
Changed in version 3.4: Print a traceback. Print only traceback if
|
92 |
+
*obj* is "NULL".
|
93 |
+
|
94 |
+
Changed in version 3.8: Use "sys.unraisablehook()".
|
95 |
+
|
96 |
+
void PyErr_DisplayException(PyObject *exc)
|
97 |
+
* Part of the Stable ABI since version 3.12.*
|
98 |
+
|
99 |
+
Print the standard traceback display of "exc" to "sys.stderr",
|
100 |
+
including chained exceptions and notes.
|
101 |
+
|
102 |
+
Added in version 3.12.
|
103 |
+
|
104 |
+
|
105 |
+
Raising exceptions
|
106 |
+
==================
|
107 |
+
|
108 |
+
These functions help you set the current thread's error indicator. For
|
109 |
+
convenience, some of these functions will always return a "NULL"
|
110 |
+
pointer for use in a "return" statement.
|
111 |
+
|
112 |
+
void PyErr_SetString(PyObject *type, const char *message)
|
113 |
+
* Part of the Stable ABI.*
|
114 |
+
|
115 |
+
This is the most common way to set the error indicator. The first
|
116 |
+
argument specifies the exception type; it is normally one of the
|
117 |
+
standard exceptions, e.g. "PyExc_RuntimeError". You need not
|
118 |
+
create a new *strong reference* to it (e.g. with "Py_INCREF()").
|
119 |
+
The second argument is an error message; it is decoded from
|
120 |
+
"'utf-8'".
|
121 |
+
|
122 |
+
void PyErr_SetObject(PyObject *type, PyObject *value)
|
123 |
+
* Part of the Stable ABI.*
|
124 |
+
|
125 |
+
This function is similar to "PyErr_SetString()" but lets you
|
126 |
+
specify an arbitrary Python object for the "value" of the
|
127 |
+
exception.
|
128 |
+
|
129 |
+
PyObject *PyErr_Format(PyObject *exception, const char *format, ...)
|
130 |
+
*Return value: Always NULL.** Part of the Stable ABI.*
|
131 |
+
|
132 |
+
This function sets the error indicator and returns "NULL".
|
133 |
+
*exception* should be a Python exception class. The *format* and
|
134 |
+
subsequent parameters help format the error message; they have the
|
135 |
+
same meaning and values as in "PyUnicode_FromFormat()". *format* is
|
136 |
+
an ASCII-encoded string.
|
137 |
+
|
138 |
+
PyObject *PyErr_FormatV(PyObject *exception, const char *format, va_list vargs)
|
139 |
+
*Return value: Always NULL.** Part of the Stable ABI since version
|
140 |
+
3.5.*
|
141 |
+
|
142 |
+
Same as "PyErr_Format()", but taking a "va_list" argument rather
|
143 |
+
than a variable number of arguments.
|
144 |
+
|
145 |
+
Added in version 3.5.
|
146 |
+
|
147 |
+
void PyErr_SetNone(PyObject *type)
|
148 |
+
* Part of the Stable ABI.*
|
149 |
+
|
150 |
+
This is a shorthand for "PyErr_SetObject(type, Py_None)".
|
151 |
+
|
152 |
+
int PyErr_BadArgument()
|
153 |
+
* Part of the Stable ABI.*
|
154 |
+
|
155 |
+
This is a shorthand for "PyErr_SetString(PyExc_TypeError,
|
156 |
+
message)", where *message* indicates that a built-in operation was
|
157 |
+
invoked with an illegal argument. It is mostly for internal use.
|
158 |
+
|
159 |
+
PyObject *PyErr_NoMemory()
|
160 |
+
*Return value: Always NULL.** Part of the Stable ABI.*
|
161 |
+
|
162 |
+
This is a shorthand for "PyErr_SetNone(PyExc_MemoryError)"; it
|
163 |
+
returns "NULL" so an object allocation function can write "return
|
164 |
+
PyErr_NoMemory();" when it runs out of memory.
|
165 |
+
|
166 |
+
PyObject *PyErr_SetFromErrno(PyObject *type)
|
167 |
+
*Return value: Always NULL.** Part of the Stable ABI.*
|
168 |
+
|
169 |
+
This is a convenience function to raise an exception when a C
|
170 |
+
library function has returned an error and set the C variable
|
171 |
+
"errno". It constructs a tuple object whose first item is the
|
172 |
+
integer "errno" value and whose second item is the corresponding
|
173 |
+
error message (gotten from "strerror()"), and then calls
|
174 |
+
"PyErr_SetObject(type, object)". On Unix, when the "errno" value
|
175 |
+
is "EINTR", indicating an interrupted system call, this calls
|
176 |
+
"PyErr_CheckSignals()", and if that set the error indicator, leaves
|
177 |
+
it set to that. The function always returns "NULL", so a wrapper
|
178 |
+
function around a system call can write "return
|
179 |
+
PyErr_SetFromErrno(type);" when the system call returns an error.
|
180 |
+
|
181 |
+
PyObject *PyErr_SetFromErrnoWithFilenameObject(PyObject *type, PyObject *filenameObject)
|
182 |
+
*Return value: Always NULL.** Part of the Stable ABI.*
|
183 |
+
|
184 |
+
Similar to "PyErr_SetFromErrno()", with the additional behavior
|
185 |
+
that if *filenameObject* is not "NULL", it is passed to the
|
186 |
+
constructor of *type* as a third parameter. In the case of
|
187 |
+
"OSError" exception, this is used to define the "filename"
|
188 |
+
attribute of the exception instance.
|
189 |
+
|
190 |
+
PyObject *PyErr_SetFromErrnoWithFilenameObjects(PyObject *type, PyObject *filenameObject, PyObject *filenameObject2)
|
191 |
+
*Return value: Always NULL.** Part of the Stable ABI since version
|
192 |
+
3.7.*
|
193 |
+
|
194 |
+
Similar to "PyErr_SetFromErrnoWithFilenameObject()", but takes a
|
195 |
+
second filename object, for raising errors when a function that
|
196 |
+
takes two filenames fails.
|
197 |
+
|
198 |
+
Added in version 3.4.
|
199 |
+
|
200 |
+
PyObject *PyErr_SetFromErrnoWithFilename(PyObject *type, const char *filename)
|
201 |
+
*Return value: Always NULL.** Part of the Stable ABI.*
|
202 |
+
|
203 |
+
Similar to "PyErr_SetFromErrnoWithFilenameObject()", but the
|
204 |
+
filename is given as a C string. *filename* is decoded from the
|
205 |
+
*filesystem encoding and error handler*.
|
206 |
+
|
207 |
+
PyObject *PyErr_SetFromWindowsErr(int ierr)
|
208 |
+
*Return value: Always NULL.** Part of the Stable ABI on Windows
|
209 |
+
since version 3.7.*
|
210 |
+
|
211 |
+
This is a convenience function to raise "OSError". If called with
|
212 |
+
*ierr* of "0", the error code returned by a call to
|
213 |
+
"GetLastError()" is used instead. It calls the Win32 function
|
214 |
+
"FormatMessage()" to retrieve the Windows description of error code
|
215 |
+
given by *ierr* or "GetLastError()", then it constructs a "OSError"
|
216 |
+
object with the "winerror" attribute set to the error code, the
|
217 |
+
"strerror" attribute set to the corresponding error message (gotten
|
218 |
+
from "FormatMessage()"), and then calls
|
219 |
+
"PyErr_SetObject(PyExc_OSError, object)". This function always
|
220 |
+
returns "NULL".
|
221 |
+
|
222 |
+
Availability: Windows.
|
223 |
+
|
224 |
+
PyObject *PyErr_SetExcFromWindowsErr(PyObject *type, int ierr)
|
225 |
+
*Return value: Always NULL.** Part of the Stable ABI on Windows
|
226 |
+
since version 3.7.*
|
227 |
+
|
228 |
+
Similar to "PyErr_SetFromWindowsErr()", with an additional
|
229 |
+
parameter specifying the exception type to be raised.
|
230 |
+
|
231 |
+
Availability: Windows.
|
232 |
+
|
233 |
+
PyObject *PyErr_SetFromWindowsErrWithFilename(int ierr, const char *filename)
|
234 |
+
*Return value: Always NULL.** Part of the Stable ABI on Windows
|
235 |
+
since version 3.7.*
|
236 |
+
|
237 |
+
Similar to "PyErr_SetFromWindowsErr()", with the additional
|
238 |
+
behavior that if *filename* is not "NULL", it is decoded from the
|
239 |
+
filesystem encoding ("os.fsdecode()") and passed to the constructor
|
240 |
+
of "OSError" as a third parameter to be used to define the
|
241 |
+
"filename" attribute of the exception instance.
|
242 |
+
|
243 |
+
Availability: Windows.
|
244 |
+
|
245 |
+
PyObject *PyErr_SetExcFromWindowsErrWithFilenameObject(PyObject *type, int ierr, PyObject *filename)
|
246 |
+
*Return value: Always NULL.** Part of the Stable ABI on Windows
|
247 |
+
since version 3.7.*
|
248 |
+
|
249 |
+
Similar to "PyErr_SetExcFromWindowsErr()", with the additional
|
250 |
+
behavior that if *filename* is not "NULL", it is passed to the
|
251 |
+
constructor of "OSError" as a third parameter to be used to define
|
252 |
+
the "filename" attribute of the exception instance.
|
253 |
+
|
254 |
+
Availability: Windows.
|
255 |
+
|
256 |
+
PyObject *PyErr_SetExcFromWindowsErrWithFilenameObjects(PyObject *type, int ierr, PyObject *filename, PyObject *filename2)
|
257 |
+
*Return value: Always NULL.** Part of the Stable ABI on Windows
|
258 |
+
since version 3.7.*
|
259 |
+
|
260 |
+
Similar to "PyErr_SetExcFromWindowsErrWithFilenameObject()", but
|
261 |
+
accepts a second filename object.
|
262 |
+
|
263 |
+
Availability: Windows.
|
264 |
+
|
265 |
+
Added in version 3.4.
|
266 |
+
|
267 |
+
PyObject *PyErr_SetExcFromWindowsErrWithFilename(PyObject *type, int ierr, const char *filename)
|
268 |
+
*Return value: Always NULL.** Part of the Stable ABI on Windows
|
269 |
+
since version 3.7.*
|
270 |
+
|
271 |
+
Similar to "PyErr_SetFromWindowsErrWithFilename()", with an
|
272 |
+
additional parameter specifying the exception type to be raised.
|
273 |
+
|
274 |
+
Availability: Windows.
|
275 |
+
|
276 |
+
PyObject *PyErr_SetImportError(PyObject *msg, PyObject *name, PyObject *path)
|
277 |
+
*Return value: Always NULL.** Part of the Stable ABI since version
|
278 |
+
3.7.*
|
279 |
+
|
280 |
+
This is a convenience function to raise "ImportError". *msg* will
|
281 |
+
be set as the exception's message string. *name* and *path*, both
|
282 |
+
of which can be "NULL", will be set as the "ImportError"'s
|
283 |
+
respective "name" and "path" attributes.
|
284 |
+
|
285 |
+
Added in version 3.3.
|
286 |
+
|
287 |
+
PyObject *PyErr_SetImportErrorSubclass(PyObject *exception, PyObject *msg, PyObject *name, PyObject *path)
|
288 |
+
*Return value: Always NULL.** Part of the Stable ABI since version
|
289 |
+
3.6.*
|
290 |
+
|
291 |
+
Much like "PyErr_SetImportError()" but this function allows for
|
292 |
+
specifying a subclass of "ImportError" to raise.
|
293 |
+
|
294 |
+
Added in version 3.6.
|
295 |
+
|
296 |
+
void PyErr_SyntaxLocationObject(PyObject *filename, int lineno, int col_offset)
|
297 |
+
|
298 |
+
Set file, line, and offset information for the current exception.
|
299 |
+
If the current exception is not a "SyntaxError", then it sets
|
300 |
+
additional attributes, which make the exception printing subsystem
|
301 |
+
think the exception is a "SyntaxError".
|
302 |
+
|
303 |
+
Added in version 3.4.
|
304 |
+
|
305 |
+
void PyErr_SyntaxLocationEx(const char *filename, int lineno, int col_offset)
|
306 |
+
* Part of the Stable ABI since version 3.7.*
|
307 |
+
|
308 |
+
Like "PyErr_SyntaxLocationObject()", but *filename* is a byte
|
309 |
+
string decoded from the *filesystem encoding and error handler*.
|
310 |
+
|
311 |
+
Added in version 3.2.
|
312 |
+
|
313 |
+
void PyErr_SyntaxLocation(const char *filename, int lineno)
|
314 |
+
* Part of the Stable ABI.*
|
315 |
+
|
316 |
+
Like "PyErr_SyntaxLocationEx()", but the *col_offset* parameter is
|
317 |
+
omitted.
|
318 |
+
|
319 |
+
void PyErr_BadInternalCall()
|
320 |
+
* Part of the Stable ABI.*
|
321 |
+
|
322 |
+
This is a shorthand for "PyErr_SetString(PyExc_SystemError,
|
323 |
+
message)", where *message* indicates that an internal operation
|
324 |
+
(e.g. a Python/C API function) was invoked with an illegal
|
325 |
+
argument. It is mostly for internal use.
|
326 |
+
|
327 |
+
|
328 |
+
Issuing warnings
|
329 |
+
================
|
330 |
+
|
331 |
+
Use these functions to issue warnings from C code. They mirror
|
332 |
+
similar functions exported by the Python "warnings" module. They
|
333 |
+
normally print a warning message to *sys.stderr*; however, it is also
|
334 |
+
possible that the user has specified that warnings are to be turned
|
335 |
+
into errors, and in that case they will raise an exception. It is
|
336 |
+
also possible that the functions raise an exception because of a
|
337 |
+
problem with the warning machinery. The return value is "0" if no
|
338 |
+
exception is raised, or "-1" if an exception is raised. (It is not
|
339 |
+
possible to determine whether a warning message is actually printed,
|
340 |
+
nor what the reason is for the exception; this is intentional.) If an
|
341 |
+
exception is raised, the caller should do its normal exception
|
342 |
+
handling (for example, "Py_DECREF()" owned references and return an
|
343 |
+
error value).
|
344 |
+
|
345 |
+
int PyErr_WarnEx(PyObject *category, const char *message, Py_ssize_t stack_level)
|
346 |
+
* Part of the Stable ABI.*
|
347 |
+
|
348 |
+
Issue a warning message. The *category* argument is a warning
|
349 |
+
category (see below) or "NULL"; the *message* argument is a UTF-8
|
350 |
+
encoded string. *stack_level* is a positive number giving a number
|
351 |
+
of stack frames; the warning will be issued from the currently
|
352 |
+
executing line of code in that stack frame. A *stack_level* of 1
|
353 |
+
is the function calling "PyErr_WarnEx()", 2 is the function above
|
354 |
+
that, and so forth.
|
355 |
+
|
356 |
+
Warning categories must be subclasses of "PyExc_Warning";
|
357 |
+
"PyExc_Warning" is a subclass of "PyExc_Exception"; the default
|
358 |
+
warning category is "PyExc_RuntimeWarning". The standard Python
|
359 |
+
warning categories are available as global variables whose names
|
360 |
+
are enumerated at Standard Warning Categories.
|
361 |
+
|
362 |
+
For information about warning control, see the documentation for
|
363 |
+
the "warnings" module and the "-W" option in the command line
|
364 |
+
documentation. There is no C API for warning control.
|
365 |
+
|
366 |
+
int PyErr_WarnExplicitObject(PyObject *category, PyObject *message, PyObject *filename, int lineno, PyObject *module, PyObject *registry)
|
367 |
+
|
368 |
+
Issue a warning message with explicit control over all warning
|
369 |
+
attributes. This is a straightforward wrapper around the Python
|
370 |
+
function "warnings.warn_explicit()"; see there for more
|
371 |
+
information. The *module* and *registry* arguments may be set to
|
372 |
+
"NULL" to get the default effect described there.
|
373 |
+
|
374 |
+
Added in version 3.4.
|
375 |
+
|
376 |
+
int PyErr_WarnExplicit(PyObject *category, const char *message, const char *filename, int lineno, const char *module, PyObject *registry)
|
377 |
+
* Part of the Stable ABI.*
|
378 |
+
|
379 |
+
Similar to "PyErr_WarnExplicitObject()" except that *message* and
|
380 |
+
*module* are UTF-8 encoded strings, and *filename* is decoded from
|
381 |
+
the *filesystem encoding and error handler*.
|
382 |
+
|
383 |
+
int PyErr_WarnFormat(PyObject *category, Py_ssize_t stack_level, const char *format, ...)
|
384 |
+
* Part of the Stable ABI.*
|
385 |
+
|
386 |
+
Function similar to "PyErr_WarnEx()", but use
|
387 |
+
"PyUnicode_FromFormat()" to format the warning message. *format*
|
388 |
+
is an ASCII-encoded string.
|
389 |
+
|
390 |
+
Added in version 3.2.
|
391 |
+
|
392 |
+
int PyErr_ResourceWarning(PyObject *source, Py_ssize_t stack_level, const char *format, ...)
|
393 |
+
* Part of the Stable ABI since version 3.6.*
|
394 |
+
|
395 |
+
Function similar to "PyErr_WarnFormat()", but *category* is
|
396 |
+
"ResourceWarning" and it passes *source* to
|
397 |
+
"warnings.WarningMessage".
|
398 |
+
|
399 |
+
Added in version 3.6.
|
400 |
+
|
401 |
+
|
402 |
+
Querying the error indicator
|
403 |
+
============================
|
404 |
+
|
405 |
+
PyObject *PyErr_Occurred()
|
406 |
+
*Return value: Borrowed reference.** Part of the Stable ABI.*
|
407 |
+
|
408 |
+
Test whether the error indicator is set. If set, return the
|
409 |
+
exception *type* (the first argument to the last call to one of the
|
410 |
+
"PyErr_Set*" functions or to "PyErr_Restore()"). If not set,
|
411 |
+
return "NULL". You do not own a reference to the return value, so
|
412 |
+
you do not need to "Py_DECREF()" it.
|
413 |
+
|
414 |
+
The caller must hold the GIL.
|
415 |
+
|
416 |
+
Note:
|
417 |
+
|
418 |
+
Do not compare the return value to a specific exception; use
|
419 |
+
"PyErr_ExceptionMatches()" instead, shown below. (The comparison
|
420 |
+
could easily fail since the exception may be an instance instead
|
421 |
+
of a class, in the case of a class exception, or it may be a
|
422 |
+
subclass of the expected exception.)
|
423 |
+
|
424 |
+
int PyErr_ExceptionMatches(PyObject *exc)
|
425 |
+
* Part of the Stable ABI.*
|
426 |
+
|
427 |
+
Equivalent to "PyErr_GivenExceptionMatches(PyErr_Occurred(), exc)".
|
428 |
+
This should only be called when an exception is actually set; a
|
429 |
+
memory access violation will occur if no exception has been raised.
|
430 |
+
|
431 |
+
int PyErr_GivenExceptionMatches(PyObject *given, PyObject *exc)
|
432 |
+
* Part of the Stable ABI.*
|
433 |
+
|
434 |
+
Return true if the *given* exception matches the exception type in
|
435 |
+
*exc*. If *exc* is a class object, this also returns true when
|
436 |
+
*given* is an instance of a subclass. If *exc* is a tuple, all
|
437 |
+
exception types in the tuple (and recursively in subtuples) are
|
438 |
+
searched for a match.
|
439 |
+
|
440 |
+
PyObject *PyErr_GetRaisedException(void)
|
441 |
+
*Return value: New reference.** Part of the Stable ABI since
|
442 |
+
version 3.12.*
|
443 |
+
|
444 |
+
Return the exception currently being raised, clearing the error
|
445 |
+
indicator at the same time. Return "NULL" if the error indicator is
|
446 |
+
not set.
|
447 |
+
|
448 |
+
This function is used by code that needs to catch exceptions, or
|
449 |
+
code that needs to save and restore the error indicator
|
450 |
+
temporarily.
|
451 |
+
|
452 |
+
For example:
|
453 |
+
|
454 |
+
{
|
455 |
+
PyObject *exc = PyErr_GetRaisedException();
|
456 |
+
|
457 |
+
/* ... code that might produce other errors ... */
|
458 |
+
|
459 |
+
PyErr_SetRaisedException(exc);
|
460 |
+
}
|
461 |
+
|
462 |
+
See also:
|
463 |
+
|
464 |
+
"PyErr_GetHandledException()", to save the exception currently
|
465 |
+
being handled.
|
466 |
+
|
467 |
+
Added in version 3.12.
|
468 |
+
|
469 |
+
void PyErr_SetRaisedException(PyObject *exc)
|
470 |
+
* Part of the Stable ABI since version 3.12.*
|
471 |
+
|
472 |
+
Set *exc* as the exception currently being raised, clearing the
|
473 |
+
existing exception if one is set.
|
474 |
+
|
475 |
+
Warning:
|
476 |
+
|
477 |
+
This call steals a reference to *exc*, which must be a valid
|
478 |
+
exception.
|
479 |
+
|
480 |
+
Added in version 3.12.
|
481 |
+
|
482 |
+
void PyErr_Fetch(PyObject **ptype, PyObject **pvalue, PyObject **ptraceback)
|
483 |
+
* Part of the Stable ABI.*
|
484 |
+
|
485 |
+
Deprecated since version 3.12: Use "PyErr_GetRaisedException()"
|
486 |
+
instead.
|
487 |
+
|
488 |
+
Retrieve the error indicator into three variables whose addresses
|
489 |
+
are passed. If the error indicator is not set, set all three
|
490 |
+
variables to "NULL". If it is set, it will be cleared and you own
|
491 |
+
a reference to each object retrieved. The value and traceback
|
492 |
+
object may be "NULL" even when the type object is not.
|
493 |
+
|
494 |
+
Note:
|
495 |
+
|
496 |
+
This function is normally only used by legacy code that needs to
|
497 |
+
catch exceptions or save and restore the error indicator
|
498 |
+
temporarily.For example:
|
499 |
+
|
500 |
+
{
|
501 |
+
PyObject *type, *value, *traceback;
|
502 |
+
PyErr_Fetch(&type, &value, &traceback);
|
503 |
+
|
504 |
+
/* ... code that might produce other errors ... */
|
505 |
+
|
506 |
+
PyErr_Restore(type, value, traceback);
|
507 |
+
}
|
508 |
+
|
509 |
+
void PyErr_Restore(PyObject *type, PyObject *value, PyObject *traceback)
|
510 |
+
* Part of the Stable ABI.*
|
511 |
+
|
512 |
+
Deprecated since version 3.12: Use "PyErr_SetRaisedException()"
|
513 |
+
instead.
|
514 |
+
|
515 |
+
Set the error indicator from the three objects, *type*, *value*,
|
516 |
+
and *traceback*, clearing the existing exception if one is set. If
|
517 |
+
the objects are "NULL", the error indicator is cleared. Do not
|
518 |
+
pass a "NULL" type and non-"NULL" value or traceback. The
|
519 |
+
exception type should be a class. Do not pass an invalid exception
|
520 |
+
type or value. (Violating these rules will cause subtle problems
|
521 |
+
later.) This call takes away a reference to each object: you must
|
522 |
+
own a reference to each object before the call and after the call
|
523 |
+
you no longer own these references. (If you don't understand this,
|
524 |
+
don't use this function. I warned you.)
|
525 |
+
|
526 |
+
Note:
|
527 |
+
|
528 |
+
This function is normally only used by legacy code that needs to
|
529 |
+
save and restore the error indicator temporarily. Use
|
530 |
+
"PyErr_Fetch()" to save the current error indicator.
|
531 |
+
|
532 |
+
void PyErr_NormalizeException(PyObject **exc, PyObject **val, PyObject **tb)
|
533 |
+
* Part of the Stable ABI.*
|
534 |
+
|
535 |
+
Deprecated since version 3.12: Use "PyErr_GetRaisedException()"
|
536 |
+
instead, to avoid any possible de-normalization.
|
537 |
+
|
538 |
+
Under certain circumstances, the values returned by "PyErr_Fetch()"
|
539 |
+
below can be "unnormalized", meaning that "*exc" is a class object
|
540 |
+
but "*val" is not an instance of the same class. This function
|
541 |
+
can be used to instantiate the class in that case. If the values
|
542 |
+
are already normalized, nothing happens. The delayed normalization
|
543 |
+
is implemented to improve performance.
|
544 |
+
|
545 |
+
Note:
|
546 |
+
|
547 |
+
This function *does not* implicitly set the "__traceback__"
|
548 |
+
attribute on the exception value. If setting the traceback
|
549 |
+
appropriately is desired, the following additional snippet is
|
550 |
+
needed:
|
551 |
+
|
552 |
+
if (tb != NULL) {
|
553 |
+
PyException_SetTraceback(val, tb);
|
554 |
+
}
|
555 |
+
|
556 |
+
PyObject *PyErr_GetHandledException(void)
|
557 |
+
* Part of the Stable ABI since version 3.11.*
|
558 |
+
|
559 |
+
Retrieve the active exception instance, as would be returned by
|
560 |
+
"sys.exception()". This refers to an exception that was *already
|
561 |
+
caught*, not to an exception that was freshly raised. Returns a new
|
562 |
+
reference to the exception or "NULL". Does not modify the
|
563 |
+
interpreter's exception state.
|
564 |
+
|
565 |
+
Note:
|
566 |
+
|
567 |
+
This function is not normally used by code that wants to handle
|
568 |
+
exceptions. Rather, it can be used when code needs to save and
|
569 |
+
restore the exception state temporarily. Use
|
570 |
+
"PyErr_SetHandledException()" to restore or clear the exception
|
571 |
+
state.
|
572 |
+
|
573 |
+
Added in version 3.11.
|
574 |
+
|
575 |
+
void PyErr_SetHandledException(PyObject *exc)
|
576 |
+
* Part of the Stable ABI since version 3.11.*
|
577 |
+
|
578 |
+
Set the active exception, as known from "sys.exception()". This
|
579 |
+
refers to an exception that was *already caught*, not to an
|
580 |
+
exception that was freshly raised. To clear the exception state,
|
581 |
+
pass "NULL".
|
582 |
+
|
583 |
+
Note:
|
584 |
+
|
585 |
+
This function is not normally used by code that wants to handle
|
586 |
+
exceptions. Rather, it can be used when code needs to save and
|
587 |
+
restore the exception state temporarily. Use
|
588 |
+
"PyErr_GetHandledException()" to get the exception state.
|
589 |
+
|
590 |
+
Added in version 3.11.
|
591 |
+
|
592 |
+
void PyErr_GetExcInfo(PyObject **ptype, PyObject **pvalue, PyObject **ptraceback)
|
593 |
+
* Part of the Stable ABI since version 3.7.*
|
594 |
+
|
595 |
+
Retrieve the old-style representation of the exception info, as
|
596 |
+
known from "sys.exc_info()". This refers to an exception that was
|
597 |
+
*already caught*, not to an exception that was freshly raised.
|
598 |
+
Returns new references for the three objects, any of which may be
|
599 |
+
"NULL". Does not modify the exception info state. This function
|
600 |
+
is kept for backwards compatibility. Prefer using
|
601 |
+
"PyErr_GetHandledException()".
|
602 |
+
|
603 |
+
Note:
|
604 |
+
|
605 |
+
This function is not normally used by code that wants to handle
|
606 |
+
exceptions. Rather, it can be used when code needs to save and
|
607 |
+
restore the exception state temporarily. Use
|
608 |
+
"PyErr_SetExcInfo()" to restore or clear the exception state.
|
609 |
+
|
610 |
+
Added in version 3.3.
|
611 |
+
|
612 |
+
void PyErr_SetExcInfo(PyObject *type, PyObject *value, PyObject *traceback)
|
613 |
+
* Part of the Stable ABI since version 3.7.*
|
614 |
+
|
615 |
+
Set the exception info, as known from "sys.exc_info()". This
|
616 |
+
refers to an exception that was *already caught*, not to an
|
617 |
+
exception that was freshly raised. This function steals the
|
618 |
+
references of the arguments. To clear the exception state, pass
|
619 |
+
"NULL" for all three arguments. This function is kept for backwards
|
620 |
+
compatibility. Prefer using "PyErr_SetHandledException()".
|
621 |
+
|
622 |
+
Note:
|
623 |
+
|
624 |
+
This function is not normally used by code that wants to handle
|
625 |
+
exceptions. Rather, it can be used when code needs to save and
|
626 |
+
restore the exception state temporarily. Use
|
627 |
+
"PyErr_GetExcInfo()" to read the exception state.
|
628 |
+
|
629 |
+
Added in version 3.3.
|
630 |
+
|
631 |
+
Changed in version 3.11: The "type" and "traceback" arguments are
|
632 |
+
no longer used and can be NULL. The interpreter now derives them
|
633 |
+
from the exception instance (the "value" argument). The function
|
634 |
+
still steals references of all three arguments.
|
635 |
+
|
636 |
+
|
637 |
+
Signal Handling
|
638 |
+
===============
|
639 |
+
|
640 |
+
int PyErr_CheckSignals()
|
641 |
+
* Part of the Stable ABI.*
|
642 |
+
|
643 |
+
This function interacts with Python's signal handling.
|
644 |
+
|
645 |
+
If the function is called from the main thread and under the main
|
646 |
+
Python interpreter, it checks whether a signal has been sent to the
|
647 |
+
processes and if so, invokes the corresponding signal handler. If
|
648 |
+
the "signal" module is supported, this can invoke a signal handler
|
649 |
+
written in Python.
|
650 |
+
|
651 |
+
The function attempts to handle all pending signals, and then
|
652 |
+
returns "0". However, if a Python signal handler raises an
|
653 |
+
exception, the error indicator is set and the function returns "-1"
|
654 |
+
immediately (such that other pending signals may not have been
|
655 |
+
handled yet: they will be on the next "PyErr_CheckSignals()"
|
656 |
+
invocation).
|
657 |
+
|
658 |
+
If the function is called from a non-main thread, or under a non-
|
659 |
+
main Python interpreter, it does nothing and returns "0".
|
660 |
+
|
661 |
+
This function can be called by long-running C code that wants to be
|
662 |
+
interruptible by user requests (such as by pressing Ctrl-C).
|
663 |
+
|
664 |
+
Note:
|
665 |
+
|
666 |
+
The default Python signal handler for "SIGINT" raises the
|
667 |
+
"KeyboardInterrupt" exception.
|
668 |
+
|
669 |
+
void PyErr_SetInterrupt()
|
670 |
+
* Part of the Stable ABI.*
|
671 |
+
|
672 |
+
Simulate the effect of a "SIGINT" signal arriving. This is
|
673 |
+
equivalent to "PyErr_SetInterruptEx(SIGINT)".
|
674 |
+
|
675 |
+
Note:
|
676 |
+
|
677 |
+
This function is async-signal-safe. It can be called without the
|
678 |
+
*GIL* and from a C signal handler.
|
679 |
+
|
680 |
+
int PyErr_SetInterruptEx(int signum)
|
681 |
+
* Part of the Stable ABI since version 3.10.*
|
682 |
+
|
683 |
+
Simulate the effect of a signal arriving. The next time
|
684 |
+
"PyErr_CheckSignals()" is called, the Python signal handler for
|
685 |
+
the given signal number will be called.
|
686 |
+
|
687 |
+
This function can be called by C code that sets up its own signal
|
688 |
+
handling and wants Python signal handlers to be invoked as expected
|
689 |
+
when an interruption is requested (for example when the user
|
690 |
+
presses Ctrl-C to interrupt an operation).
|
691 |
+
|
692 |
+
If the given signal isn't handled by Python (it was set to
|
693 |
+
"signal.SIG_DFL" or "signal.SIG_IGN"), it will be ignored.
|
694 |
+
|
695 |
+
If *signum* is outside of the allowed range of signal numbers, "-1"
|
696 |
+
is returned. Otherwise, "0" is returned. The error indicator is
|
697 |
+
never changed by this function.
|
698 |
+
|
699 |
+
Note:
|
700 |
+
|
701 |
+
This function is async-signal-safe. It can be called without the
|
702 |
+
*GIL* and from a C signal handler.
|
703 |
+
|
704 |
+
Added in version 3.10.
|
705 |
+
|
706 |
+
int PySignal_SetWakeupFd(int fd)
|
707 |
+
|
708 |
+
This utility function specifies a file descriptor to which the
|
709 |
+
signal number is written as a single byte whenever a signal is
|
710 |
+
received. *fd* must be non-blocking. It returns the previous such
|
711 |
+
file descriptor.
|
712 |
+
|
713 |
+
The value "-1" disables the feature; this is the initial state.
|
714 |
+
This is equivalent to "signal.set_wakeup_fd()" in Python, but
|
715 |
+
without any error checking. *fd* should be a valid file
|
716 |
+
descriptor. The function should only be called from the main
|
717 |
+
thread.
|
718 |
+
|
719 |
+
Changed in version 3.5: On Windows, the function now also supports
|
720 |
+
socket handles.
|
721 |
+
|
722 |
+
|
723 |
+
Exception Classes
|
724 |
+
=================
|
725 |
+
|
726 |
+
PyObject *PyErr_NewException(const char *name, PyObject *base, PyObject *dict)
|
727 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
728 |
+
|
729 |
+
This utility function creates and returns a new exception class.
|
730 |
+
The *name* argument must be the name of the new exception, a C
|
731 |
+
string of the form "module.classname". The *base* and *dict*
|
732 |
+
arguments are normally "NULL". This creates a class object derived
|
733 |
+
from "Exception" (accessible in C as "PyExc_Exception").
|
734 |
+
|
735 |
+
The "__module__" attribute of the new class is set to the first
|
736 |
+
part (up to the last dot) of the *name* argument, and the class
|
737 |
+
name is set to the last part (after the last dot). The *base*
|
738 |
+
argument can be used to specify alternate base classes; it can
|
739 |
+
either be only one class or a tuple of classes. The *dict* argument
|
740 |
+
can be used to specify a dictionary of class variables and methods.
|
741 |
+
|
742 |
+
PyObject *PyErr_NewExceptionWithDoc(const char *name, const char *doc, PyObject *base, PyObject *dict)
|
743 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
744 |
+
|
745 |
+
Same as "PyErr_NewException()", except that the new exception class
|
746 |
+
can easily be given a docstring: If *doc* is non-"NULL", it will be
|
747 |
+
used as the docstring for the exception class.
|
748 |
+
|
749 |
+
Added in version 3.2.
|
750 |
+
|
751 |
+
|
752 |
+
Exception Objects
|
753 |
+
=================
|
754 |
+
|
755 |
+
PyObject *PyException_GetTraceback(PyObject *ex)
|
756 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
757 |
+
|
758 |
+
Return the traceback associated with the exception as a new
|
759 |
+
reference, as accessible from Python through the "__traceback__"
|
760 |
+
attribute. If there is no traceback associated, this returns
|
761 |
+
"NULL".
|
762 |
+
|
763 |
+
int PyException_SetTraceback(PyObject *ex, PyObject *tb)
|
764 |
+
* Part of the Stable ABI.*
|
765 |
+
|
766 |
+
Set the traceback associated with the exception to *tb*. Use
|
767 |
+
"Py_None" to clear it.
|
768 |
+
|
769 |
+
PyObject *PyException_GetContext(PyObject *ex)
|
770 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
771 |
+
|
772 |
+
Return the context (another exception instance during whose
|
773 |
+
handling *ex* was raised) associated with the exception as a new
|
774 |
+
reference, as accessible from Python through the "__context__"
|
775 |
+
attribute. If there is no context associated, this returns "NULL".
|
776 |
+
|
777 |
+
void PyException_SetContext(PyObject *ex, PyObject *ctx)
|
778 |
+
* Part of the Stable ABI.*
|
779 |
+
|
780 |
+
Set the context associated with the exception to *ctx*. Use "NULL"
|
781 |
+
to clear it. There is no type check to make sure that *ctx* is an
|
782 |
+
exception instance. This steals a reference to *ctx*.
|
783 |
+
|
784 |
+
PyObject *PyException_GetCause(PyObject *ex)
|
785 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
786 |
+
|
787 |
+
Return the cause (either an exception instance, or "None", set by
|
788 |
+
"raise ... from ...") associated with the exception as a new
|
789 |
+
reference, as accessible from Python through the "__cause__"
|
790 |
+
attribute.
|
791 |
+
|
792 |
+
void PyException_SetCause(PyObject *ex, PyObject *cause)
|
793 |
+
* Part of the Stable ABI.*
|
794 |
+
|
795 |
+
Set the cause associated with the exception to *cause*. Use "NULL"
|
796 |
+
to clear it. There is no type check to make sure that *cause* is
|
797 |
+
either an exception instance or "None". This steals a reference to
|
798 |
+
*cause*.
|
799 |
+
|
800 |
+
The "__suppress_context__" attribute is implicitly set to "True" by
|
801 |
+
this function.
|
802 |
+
|
803 |
+
PyObject *PyException_GetArgs(PyObject *ex)
|
804 |
+
*Return value: New reference.** Part of the Stable ABI since
|
805 |
+
version 3.12.*
|
806 |
+
|
807 |
+
Return "args" of exception *ex*.
|
808 |
+
|
809 |
+
void PyException_SetArgs(PyObject *ex, PyObject *args)
|
810 |
+
* Part of the Stable ABI since version 3.12.*
|
811 |
+
|
812 |
+
Set "args" of exception *ex* to *args*.
|
813 |
+
|
814 |
+
PyObject *PyUnstable_Exc_PrepReraiseStar(PyObject *orig, PyObject *excs)
|
815 |
+
|
816 |
+
*This is Unstable API. It may change without warning in minor
|
817 |
+
releases.*
|
818 |
+
|
819 |
+
Implement part of the interpreter's implementation of "except*".
|
820 |
+
*orig* is the original exception that was caught, and *excs* is the
|
821 |
+
list of the exceptions that need to be raised. This list contains
|
822 |
+
the unhandled part of *orig*, if any, as well as the exceptions
|
823 |
+
that were raised from the "except*" clauses (so they have a
|
824 |
+
different traceback from *orig*) and those that were reraised (and
|
825 |
+
have the same traceback as *orig*). Return the "ExceptionGroup"
|
826 |
+
that needs to be reraised in the end, or "None" if there is nothing
|
827 |
+
to reraise.
|
828 |
+
|
829 |
+
Added in version 3.12.
|
830 |
+
|
831 |
+
|
832 |
+
Unicode Exception Objects
|
833 |
+
=========================
|
834 |
+
|
835 |
+
The following functions are used to create and modify Unicode
|
836 |
+
exceptions from C.
|
837 |
+
|
838 |
+
PyObject *PyUnicodeDecodeError_Create(const char *encoding, const char *object, Py_ssize_t length, Py_ssize_t start, Py_ssize_t end, const char *reason)
|
839 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
840 |
+
|
841 |
+
Create a "UnicodeDecodeError" object with the attributes
|
842 |
+
*encoding*, *object*, *length*, *start*, *end* and *reason*.
|
843 |
+
*encoding* and *reason* are UTF-8 encoded strings.
|
844 |
+
|
845 |
+
PyObject *PyUnicodeDecodeError_GetEncoding(PyObject *exc)
|
846 |
+
PyObject *PyUnicodeEncodeError_GetEncoding(PyObject *exc)
|
847 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
848 |
+
|
849 |
+
Return the *encoding* attribute of the given exception object.
|
850 |
+
|
851 |
+
PyObject *PyUnicodeDecodeError_GetObject(PyObject *exc)
|
852 |
+
PyObject *PyUnicodeEncodeError_GetObject(PyObject *exc)
|
853 |
+
PyObject *PyUnicodeTranslateError_GetObject(PyObject *exc)
|
854 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
855 |
+
|
856 |
+
Return the *object* attribute of the given exception object.
|
857 |
+
|
858 |
+
int PyUnicodeDecodeError_GetStart(PyObject *exc, Py_ssize_t *start)
|
859 |
+
int PyUnicodeEncodeError_GetStart(PyObject *exc, Py_ssize_t *start)
|
860 |
+
int PyUnicodeTranslateError_GetStart(PyObject *exc, Py_ssize_t *start)
|
861 |
+
* Part of the Stable ABI.*
|
862 |
+
|
863 |
+
Get the *start* attribute of the given exception object and place
|
864 |
+
it into **start*. *start* must not be "NULL". Return "0" on
|
865 |
+
success, "-1" on failure.
|
866 |
+
|
867 |
+
int PyUnicodeDecodeError_SetStart(PyObject *exc, Py_ssize_t start)
|
868 |
+
int PyUnicodeEncodeError_SetStart(PyObject *exc, Py_ssize_t start)
|
869 |
+
int PyUnicodeTranslateError_SetStart(PyObject *exc, Py_ssize_t start)
|
870 |
+
* Part of the Stable ABI.*
|
871 |
+
|
872 |
+
Set the *start* attribute of the given exception object to *start*.
|
873 |
+
Return "0" on success, "-1" on failure.
|
874 |
+
|
875 |
+
int PyUnicodeDecodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
|
876 |
+
int PyUnicodeEncodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
|
877 |
+
int PyUnicodeTranslateError_GetEnd(PyObject *exc, Py_ssize_t *end)
|
878 |
+
* Part of the Stable ABI.*
|
879 |
+
|
880 |
+
Get the *end* attribute of the given exception object and place it
|
881 |
+
into **end*. *end* must not be "NULL". Return "0" on success,
|
882 |
+
"-1" on failure.
|
883 |
+
|
884 |
+
int PyUnicodeDecodeError_SetEnd(PyObject *exc, Py_ssize_t end)
|
885 |
+
int PyUnicodeEncodeError_SetEnd(PyObject *exc, Py_ssize_t end)
|
886 |
+
int PyUnicodeTranslateError_SetEnd(PyObject *exc, Py_ssize_t end)
|
887 |
+
* Part of the Stable ABI.*
|
888 |
+
|
889 |
+
Set the *end* attribute of the given exception object to *end*.
|
890 |
+
Return "0" on success, "-1" on failure.
|
891 |
+
|
892 |
+
PyObject *PyUnicodeDecodeError_GetReason(PyObject *exc)
|
893 |
+
PyObject *PyUnicodeEncodeError_GetReason(PyObject *exc)
|
894 |
+
PyObject *PyUnicodeTranslateError_GetReason(PyObject *exc)
|
895 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
896 |
+
|
897 |
+
Return the *reason* attribute of the given exception object.
|
898 |
+
|
899 |
+
int PyUnicodeDecodeError_SetReason(PyObject *exc, const char *reason)
|
900 |
+
int PyUnicodeEncodeError_SetReason(PyObject *exc, const char *reason)
|
901 |
+
int PyUnicodeTranslateError_SetReason(PyObject *exc, const char *reason)
|
902 |
+
* Part of the Stable ABI.*
|
903 |
+
|
904 |
+
Set the *reason* attribute of the given exception object to
|
905 |
+
*reason*. Return "0" on success, "-1" on failure.
|
906 |
+
|
907 |
+
|
908 |
+
Recursion Control
|
909 |
+
=================
|
910 |
+
|
911 |
+
These two functions provide a way to perform safe recursive calls at
|
912 |
+
the C level, both in the core and in extension modules. They are
|
913 |
+
needed if the recursive code does not necessarily invoke Python code
|
914 |
+
(which tracks its recursion depth automatically). They are also not
|
915 |
+
needed for *tp_call* implementations because the call protocol takes
|
916 |
+
care of recursion handling.
|
917 |
+
|
918 |
+
int Py_EnterRecursiveCall(const char *where)
|
919 |
+
* Part of the Stable ABI since version 3.9.*
|
920 |
+
|
921 |
+
Marks a point where a recursive C-level call is about to be
|
922 |
+
performed.
|
923 |
+
|
924 |
+
If "USE_STACKCHECK" is defined, this function checks if the OS
|
925 |
+
stack overflowed using "PyOS_CheckStack()". If this is the case,
|
926 |
+
it sets a "MemoryError" and returns a nonzero value.
|
927 |
+
|
928 |
+
The function then checks if the recursion limit is reached. If
|
929 |
+
this is the case, a "RecursionError" is set and a nonzero value is
|
930 |
+
returned. Otherwise, zero is returned.
|
931 |
+
|
932 |
+
*where* should be a UTF-8 encoded string such as "" in instance
|
933 |
+
check"" to be concatenated to the "RecursionError" message caused
|
934 |
+
by the recursion depth limit.
|
935 |
+
|
936 |
+
Changed in version 3.9: This function is now also available in the
|
937 |
+
limited API.
|
938 |
+
|
939 |
+
void Py_LeaveRecursiveCall(void)
|
940 |
+
* Part of the Stable ABI since version 3.9.*
|
941 |
+
|
942 |
+
Ends a "Py_EnterRecursiveCall()". Must be called once for each
|
943 |
+
*successful* invocation of "Py_EnterRecursiveCall()".
|
944 |
+
|
945 |
+
Changed in version 3.9: This function is now also available in the
|
946 |
+
limited API.
|
947 |
+
|
948 |
+
Properly implementing "tp_repr" for container types requires special
|
949 |
+
recursion handling. In addition to protecting the stack, "tp_repr"
|
950 |
+
also needs to track objects to prevent cycles. The following two
|
951 |
+
functions facilitate this functionality. Effectively, these are the C
|
952 |
+
equivalent to "reprlib.recursive_repr()".
|
953 |
+
|
954 |
+
int Py_ReprEnter(PyObject *object)
|
955 |
+
* Part of the Stable ABI.*
|
956 |
+
|
957 |
+
Called at the beginning of the "tp_repr" implementation to detect
|
958 |
+
cycles.
|
959 |
+
|
960 |
+
If the object has already been processed, the function returns a
|
961 |
+
positive integer. In that case the "tp_repr" implementation should
|
962 |
+
return a string object indicating a cycle. As examples, "dict"
|
963 |
+
objects return "{...}" and "list" objects return "[...]".
|
964 |
+
|
965 |
+
The function will return a negative integer if the recursion limit
|
966 |
+
is reached. In that case the "tp_repr" implementation should
|
967 |
+
typically return "NULL".
|
968 |
+
|
969 |
+
Otherwise, the function returns zero and the "tp_repr"
|
970 |
+
implementation can continue normally.
|
971 |
+
|
972 |
+
void Py_ReprLeave(PyObject *object)
|
973 |
+
* Part of the Stable ABI.*
|
974 |
+
|
975 |
+
Ends a "Py_ReprEnter()". Must be called once for each invocation
|
976 |
+
of "Py_ReprEnter()" that returns zero.
|
977 |
+
|
978 |
+
|
979 |
+
Standard Exceptions
|
980 |
+
===================
|
981 |
+
|
982 |
+
All standard Python exceptions are available as global variables whose
|
983 |
+
names are "PyExc_" followed by the Python exception name. These have
|
984 |
+
the type PyObject*; they are all class objects. For completeness,
|
985 |
+
here are all the variables:
|
986 |
+
|
987 |
+
+-------------------------------------------+-----------------------------------+------------+
|
988 |
+
| C Name | Python Name | Notes |
|
989 |
+
|===========================================|===================================|============|
|
990 |
+
| "PyExc_BaseException" | "BaseException" | [1] |
|
991 |
+
+-------------------------------------------+-----------------------------------+------------+
|
992 |
+
| "PyExc_Exception" | "Exception" | [1] |
|
993 |
+
+-------------------------------------------+-----------------------------------+------------+
|
994 |
+
| "PyExc_ArithmeticError" | "ArithmeticError" | [1] |
|
995 |
+
+-------------------------------------------+-----------------------------------+------------+
|
996 |
+
| "PyExc_AssertionError" | "AssertionError" | |
|
997 |
+
+-------------------------------------------+-----------------------------------+------------+
|
998 |
+
| "PyExc_AttributeError" | "AttributeError" | |
|
999 |
+
+-------------------------------------------+-----------------------------------+------------+
|
1000 |
+
| "PyExc_BlockingIOError" | "BlockingIOError" | |
|
1001 |
+
+-------------------------------------------+-----------------------------------+------------+
|
1002 |
+
| "PyExc_BrokenPipeError" | "BrokenPipeError" | |
|
1003 |
+
+-------------------------------------------+-----------------------------------+------------+
|
1004 |
+
| "PyExc_BufferError" | "BufferError" | |
|
1005 |
+
+-------------------------------------------+-----------------------------------+------------+
|
1006 |
+
| "PyExc_ChildProcessError" | "ChildProcessError" | |
|
1007 |
+
+-------------------------------------------+-----------------------------------+------------+
|
1008 |
+
| "PyExc_ConnectionAbortedError" | "ConnectionAbortedError" | |
|
1009 |
+
+-------------------------------------------+-----------------------------------+------------+
|
1010 |
+
| "PyExc_ConnectionError" | "ConnectionError" | |
|
1011 |
+
+-------------------------------------------+-----------------------------------+------------+
|
1012 |
+
| "PyExc_ConnectionRefusedError" | "ConnectionRefusedError" | |
|
1013 |
+
+-------------------------------------------+-----------------------------------+------------+
|
1014 |
+
| "PyExc_ConnectionResetError" | "ConnectionResetError" | |
|
1015 |
+
+-------------------------------------------+-----------------------------------+------------+
|
1016 |
+
| "PyExc_EOFError" | "EOFError" | |
|
1017 |
+
+-------------------------------------------+-----------------------------------+------------+
|
1018 |
+
| "PyExc_FileExistsError" | "FileExistsError" | |
|
1019 |
+
+-------------------------------------------+-----------------------------------+------------+
|
1020 |
+
| "PyExc_FileNotFoundError" | "FileNotFoundError" | |
|
1021 |
+
+-------------------------------------------+-----------------------------------+------------+
|
1022 |
+
| "PyExc_FloatingPointError" | "FloatingPointError" | |
|
1023 |
+
+-------------------------------------------+-----------------------------------+------------+
|
1024 |
+
| "PyExc_GeneratorExit" | "GeneratorExit" | |
|
1025 |
+
+-------------------------------------------+-----------------------------------+------------+
|
1026 |
+
| "PyExc_ImportError" | "ImportError" | |
|
1027 |
+
+-------------------------------------------+-----------------------------------+------------+
|
1028 |
+
| "PyExc_IndentationError" | "IndentationError" | |
|
1029 |
+
+-------------------------------------------+-----------------------------------+------------+
|
1030 |
+
| "PyExc_IndexError" | "IndexError" | |
|
1031 |
+
+-------------------------------------------+-----------------------------------+------------+
|
1032 |
+
| "PyExc_InterruptedError" | "InterruptedError" | |
|
1033 |
+
+-------------------------------------------+-----------------------------------+------------+
|
1034 |
+
| "PyExc_IsADirectoryError" | "IsADirectoryError" | |
|
1035 |
+
+-------------------------------------------+-----------------------------------+------------+
|
1036 |
+
| "PyExc_KeyError" | "KeyError" | |
|
1037 |
+
+-------------------------------------------+-----------------------------------+------------+
|
1038 |
+
| "PyExc_KeyboardInterrupt" | "KeyboardInterrupt" | |
|
1039 |
+
+-------------------------------------------+-----------------------------------+------------+
|
1040 |
+
| "PyExc_LookupError" | "LookupError" | [1] |
|
1041 |
+
+-------------------------------------------+-----------------------------------+------------+
|
1042 |
+
| "PyExc_MemoryError" | "MemoryError" | |
|
1043 |
+
+-------------------------------------------+-----------------------------------+------------+
|
1044 |
+
| "PyExc_ModuleNotFoundError" | "ModuleNotFoundError" | |
|
1045 |
+
+-------------------------------------------+-----------------------------------+------------+
|
1046 |
+
| "PyExc_NameError" | "NameError" | |
|
1047 |
+
+-------------------------------------------+-----------------------------------+------------+
|
1048 |
+
| "PyExc_NotADirectoryError" | "NotADirectoryError" | |
|
1049 |
+
+-------------------------------------------+-----------------------------------+------------+
|
1050 |
+
| "PyExc_NotImplementedError" | "NotImplementedError" | |
|
1051 |
+
+-------------------------------------------+-----------------------------------+------------+
|
1052 |
+
| "PyExc_OSError" | "OSError" | [1] |
|
1053 |
+
+-------------------------------------------+-----------------------------------+------------+
|
1054 |
+
| "PyExc_OverflowError" | "OverflowError" | |
|
1055 |
+
+-------------------------------------------+-----------------------------------+------------+
|
1056 |
+
| "PyExc_PermissionError" | "PermissionError" | |
|
1057 |
+
+-------------------------------------------+-----------------------------------+------------+
|
1058 |
+
| "PyExc_ProcessLookupError" | "ProcessLookupError" | |
|
1059 |
+
+-------------------------------------------+-----------------------------------+------------+
|
1060 |
+
| "PyExc_RecursionError" | "RecursionError" | |
|
1061 |
+
+-------------------------------------------+-----------------------------------+------------+
|
1062 |
+
| "PyExc_ReferenceError" | "ReferenceError" | |
|
1063 |
+
+-------------------------------------------+-----------------------------------+------------+
|
1064 |
+
| "PyExc_RuntimeError" | "RuntimeError" | |
|
1065 |
+
+-------------------------------------------+-----------------------------------+------------+
|
1066 |
+
| "PyExc_StopAsyncIteration" | "StopAsyncIteration" | |
|
1067 |
+
+-------------------------------------------+-----------------------------------+------------+
|
1068 |
+
| "PyExc_StopIteration" | "StopIteration" | |
|
1069 |
+
+-------------------------------------------+-----------------------------------+------------+
|
1070 |
+
| "PyExc_SyntaxError" | "SyntaxError" | |
|
1071 |
+
+-------------------------------------------+-----------------------------------+------------+
|
1072 |
+
| "PyExc_SystemError" | "SystemError" | |
|
1073 |
+
+-------------------------------------------+-----------------------------------+------------+
|
1074 |
+
| "PyExc_SystemExit" | "SystemExit" | |
|
1075 |
+
+-------------------------------------------+-----------------------------------+------------+
|
1076 |
+
| "PyExc_TabError" | "TabError" | |
|
1077 |
+
+-------------------------------------------+-----------------------------------+------------+
|
1078 |
+
| "PyExc_TimeoutError" | "TimeoutError" | |
|
1079 |
+
+-------------------------------------------+-----------------------------------+------------+
|
1080 |
+
| "PyExc_TypeError" | "TypeError" | |
|
1081 |
+
+-------------------------------------------+-----------------------------------+------------+
|
1082 |
+
| "PyExc_UnboundLocalError" | "UnboundLocalError" | |
|
1083 |
+
+-------------------------------------------+-----------------------------------+------------+
|
1084 |
+
| "PyExc_UnicodeDecodeError" | "UnicodeDecodeError" | |
|
1085 |
+
+-------------------------------------------+-----------------------------------+------------+
|
1086 |
+
| "PyExc_UnicodeEncodeError" | "UnicodeEncodeError" | |
|
1087 |
+
+-------------------------------------------+-----------------------------------+------------+
|
1088 |
+
| "PyExc_UnicodeError" | "UnicodeError" | |
|
1089 |
+
+-------------------------------------------+-----------------------------------+------------+
|
1090 |
+
| "PyExc_UnicodeTranslateError" | "UnicodeTranslateError" | |
|
1091 |
+
+-------------------------------------------+-----------------------------------+------------+
|
1092 |
+
| "PyExc_ValueError" | "ValueError" | |
|
1093 |
+
+-------------------------------------------+-----------------------------------+------------+
|
1094 |
+
| "PyExc_ZeroDivisionError" | "ZeroDivisionError" | |
|
1095 |
+
+-------------------------------------------+-----------------------------------+------------+
|
1096 |
+
|
1097 |
+
Added in version 3.3: "PyExc_BlockingIOError",
|
1098 |
+
"PyExc_BrokenPipeError", "PyExc_ChildProcessError",
|
1099 |
+
"PyExc_ConnectionError", "PyExc_ConnectionAbortedError",
|
1100 |
+
"PyExc_ConnectionRefusedError", "PyExc_ConnectionResetError",
|
1101 |
+
"PyExc_FileExistsError", "PyExc_FileNotFoundError",
|
1102 |
+
"PyExc_InterruptedError", "PyExc_IsADirectoryError",
|
1103 |
+
"PyExc_NotADirectoryError", "PyExc_PermissionError",
|
1104 |
+
"PyExc_ProcessLookupError" and "PyExc_TimeoutError" were introduced
|
1105 |
+
following **PEP 3151**.
|
1106 |
+
|
1107 |
+
Added in version 3.5: "PyExc_StopAsyncIteration" and
|
1108 |
+
"PyExc_RecursionError".
|
1109 |
+
|
1110 |
+
Added in version 3.6: "PyExc_ModuleNotFoundError".
|
1111 |
+
|
1112 |
+
These are compatibility aliases to "PyExc_OSError":
|
1113 |
+
|
1114 |
+
+---------------------------------------+------------+
|
1115 |
+
| C Name | Notes |
|
1116 |
+
|=======================================|============|
|
1117 |
+
| "PyExc_EnvironmentError" | |
|
1118 |
+
+---------------------------------------+------------+
|
1119 |
+
| "PyExc_IOError" | |
|
1120 |
+
+---------------------------------------+------------+
|
1121 |
+
| "PyExc_WindowsError" | [2] |
|
1122 |
+
+---------------------------------------+------------+
|
1123 |
+
|
1124 |
+
Changed in version 3.3: These aliases used to be separate exception
|
1125 |
+
types.
|
1126 |
+
|
1127 |
+
Notes:
|
1128 |
+
|
1129 |
+
[1] This is a base class for other standard exceptions.
|
1130 |
+
|
1131 |
+
[2] Only defined on Windows; protect code that uses this by testing
|
1132 |
+
that the preprocessor macro "MS_WINDOWS" is defined.
|
1133 |
+
|
1134 |
+
|
1135 |
+
Standard Warning Categories
|
1136 |
+
===========================
|
1137 |
+
|
1138 |
+
All standard Python warning categories are available as global
|
1139 |
+
variables whose names are "PyExc_" followed by the Python exception
|
1140 |
+
name. These have the type PyObject*; they are all class objects. For
|
1141 |
+
completeness, here are all the variables:
|
1142 |
+
|
1143 |
+
+--------------------------------------------+-----------------------------------+------------+
|
1144 |
+
| C Name | Python Name | Notes |
|
1145 |
+
|============================================|===================================|============|
|
1146 |
+
| "PyExc_Warning" | "Warning" | [3] |
|
1147 |
+
+--------------------------------------------+-----------------------------------+------------+
|
1148 |
+
| "PyExc_BytesWarning" | "BytesWarning" | |
|
1149 |
+
+--------------------------------------------+-----------------------------------+------------+
|
1150 |
+
| "PyExc_DeprecationWarning" | "DeprecationWarning" | |
|
1151 |
+
+--------------------------------------------+-----------------------------------+------------+
|
1152 |
+
| "PyExc_FutureWarning" | "FutureWarning" | |
|
1153 |
+
+--------------------------------------------+-----------------------------------+------------+
|
1154 |
+
| "PyExc_ImportWarning" | "ImportWarning" | |
|
1155 |
+
+--------------------------------------------+-----------------------------------+------------+
|
1156 |
+
| "PyExc_PendingDeprecationWarning" | "PendingDeprecationWarning" | |
|
1157 |
+
+--------------------------------------------+-----------------------------------+------------+
|
1158 |
+
| "PyExc_ResourceWarning" | "ResourceWarning" | |
|
1159 |
+
+--------------------------------------------+-----------------------------------+------------+
|
1160 |
+
| "PyExc_RuntimeWarning" | "RuntimeWarning" | |
|
1161 |
+
+--------------------------------------------+-----------------------------------+------------+
|
1162 |
+
| "PyExc_SyntaxWarning" | "SyntaxWarning" | |
|
1163 |
+
+--------------------------------------------+-----------------------------------+------------+
|
1164 |
+
| "PyExc_UnicodeWarning" | "UnicodeWarning" | |
|
1165 |
+
+--------------------------------------------+-----------------------------------+------------+
|
1166 |
+
| "PyExc_UserWarning" | "UserWarning" | |
|
1167 |
+
+--------------------------------------------+-----------------------------------+------------+
|
1168 |
+
|
1169 |
+
Added in version 3.2: "PyExc_ResourceWarning".
|
1170 |
+
|
1171 |
+
Notes:
|
1172 |
+
|
1173 |
+
[3] This is a base class for other standard warning categories.
|
python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/file.txt
ADDED
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
File Objects
|
2 |
+
************
|
3 |
+
|
4 |
+
These APIs are a minimal emulation of the Python 2 C API for built-in
|
5 |
+
file objects, which used to rely on the buffered I/O (FILE*) support
|
6 |
+
from the C standard library. In Python 3, files and streams use the
|
7 |
+
new "io" module, which defines several layers over the low-level
|
8 |
+
unbuffered I/O of the operating system. The functions described below
|
9 |
+
are convenience C wrappers over these new APIs, and meant mostly for
|
10 |
+
internal error reporting in the interpreter; third-party code is
|
11 |
+
advised to access the "io" APIs instead.
|
12 |
+
|
13 |
+
PyObject *PyFile_FromFd(int fd, const char *name, const char *mode, int buffering, const char *encoding, const char *errors, const char *newline, int closefd)
|
14 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
15 |
+
|
16 |
+
Create a Python file object from the file descriptor of an already
|
17 |
+
opened file *fd*. The arguments *name*, *encoding*, *errors* and
|
18 |
+
*newline* can be "NULL" to use the defaults; *buffering* can be
|
19 |
+
*-1* to use the default. *name* is ignored and kept for backward
|
20 |
+
compatibility. Return "NULL" on failure. For a more comprehensive
|
21 |
+
description of the arguments, please refer to the "io.open()"
|
22 |
+
function documentation.
|
23 |
+
|
24 |
+
Warning:
|
25 |
+
|
26 |
+
Since Python streams have their own buffering layer, mixing them
|
27 |
+
with OS-level file descriptors can produce various issues (such
|
28 |
+
as unexpected ordering of data).
|
29 |
+
|
30 |
+
Changed in version 3.2: Ignore *name* attribute.
|
31 |
+
|
32 |
+
int PyObject_AsFileDescriptor(PyObject *p)
|
33 |
+
* Part of the Stable ABI.*
|
34 |
+
|
35 |
+
Return the file descriptor associated with *p* as an int. If the
|
36 |
+
object is an integer, its value is returned. If not, the object's
|
37 |
+
"fileno()" method is called if it exists; the method must return an
|
38 |
+
integer, which is returned as the file descriptor value. Sets an
|
39 |
+
exception and returns "-1" on failure.
|
40 |
+
|
41 |
+
PyObject *PyFile_GetLine(PyObject *p, int n)
|
42 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
43 |
+
|
44 |
+
Equivalent to "p.readline([n])", this function reads one line from
|
45 |
+
the object *p*. *p* may be a file object or any object with a
|
46 |
+
"readline()" method. If *n* is "0", exactly one line is read,
|
47 |
+
regardless of the length of the line. If *n* is greater than "0",
|
48 |
+
no more than *n* bytes will be read from the file; a partial line
|
49 |
+
can be returned. In both cases, an empty string is returned if the
|
50 |
+
end of the file is reached immediately. If *n* is less than "0",
|
51 |
+
however, one line is read regardless of length, but "EOFError" is
|
52 |
+
raised if the end of the file is reached immediately.
|
53 |
+
|
54 |
+
int PyFile_SetOpenCodeHook(Py_OpenCodeHookFunction handler)
|
55 |
+
|
56 |
+
Overrides the normal behavior of "io.open_code()" to pass its
|
57 |
+
parameter through the provided handler.
|
58 |
+
|
59 |
+
The handler is a function of type:
|
60 |
+
|
61 |
+
type Py_OpenCodeHookFunction
|
62 |
+
|
63 |
+
Equivalent of PyObject *(*)(PyObject *path, void *userData),
|
64 |
+
where *path* is guaranteed to be "PyUnicodeObject".
|
65 |
+
|
66 |
+
The *userData* pointer is passed into the hook function. Since hook
|
67 |
+
functions may be called from different runtimes, this pointer
|
68 |
+
should not refer directly to Python state.
|
69 |
+
|
70 |
+
As this hook is intentionally used during import, avoid importing
|
71 |
+
new modules during its execution unless they are known to be frozen
|
72 |
+
or available in "sys.modules".
|
73 |
+
|
74 |
+
Once a hook has been set, it cannot be removed or replaced, and
|
75 |
+
later calls to "PyFile_SetOpenCodeHook()" will fail. On failure,
|
76 |
+
the function returns -1 and sets an exception if the interpreter
|
77 |
+
has been initialized.
|
78 |
+
|
79 |
+
This function is safe to call before "Py_Initialize()".
|
80 |
+
|
81 |
+
Raises an auditing event "setopencodehook" with no arguments.
|
82 |
+
|
83 |
+
Added in version 3.8.
|
84 |
+
|
85 |
+
int PyFile_WriteObject(PyObject *obj, PyObject *p, int flags)
|
86 |
+
* Part of the Stable ABI.*
|
87 |
+
|
88 |
+
Write object *obj* to file object *p*. The only supported flag for
|
89 |
+
*flags* is "Py_PRINT_RAW"; if given, the "str()" of the object is
|
90 |
+
written instead of the "repr()". Return "0" on success or "-1" on
|
91 |
+
failure; the appropriate exception will be set.
|
92 |
+
|
93 |
+
int PyFile_WriteString(const char *s, PyObject *p)
|
94 |
+
* Part of the Stable ABI.*
|
95 |
+
|
96 |
+
Write string *s* to file object *p*. Return "0" on success or "-1"
|
97 |
+
on failure; the appropriate exception will be set.
|
python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/float.txt
ADDED
@@ -0,0 +1,160 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Floating-Point Objects
|
2 |
+
**********************
|
3 |
+
|
4 |
+
type PyFloatObject
|
5 |
+
|
6 |
+
This subtype of "PyObject" represents a Python floating-point
|
7 |
+
object.
|
8 |
+
|
9 |
+
PyTypeObject PyFloat_Type
|
10 |
+
* Part of the Stable ABI.*
|
11 |
+
|
12 |
+
This instance of "PyTypeObject" represents the Python floating-
|
13 |
+
point type. This is the same object as "float" in the Python
|
14 |
+
layer.
|
15 |
+
|
16 |
+
int PyFloat_Check(PyObject *p)
|
17 |
+
|
18 |
+
Return true if its argument is a "PyFloatObject" or a subtype of
|
19 |
+
"PyFloatObject". This function always succeeds.
|
20 |
+
|
21 |
+
int PyFloat_CheckExact(PyObject *p)
|
22 |
+
|
23 |
+
Return true if its argument is a "PyFloatObject", but not a subtype
|
24 |
+
of "PyFloatObject". This function always succeeds.
|
25 |
+
|
26 |
+
PyObject *PyFloat_FromString(PyObject *str)
|
27 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
28 |
+
|
29 |
+
Create a "PyFloatObject" object based on the string value in *str*,
|
30 |
+
or "NULL" on failure.
|
31 |
+
|
32 |
+
PyObject *PyFloat_FromDouble(double v)
|
33 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
34 |
+
|
35 |
+
Create a "PyFloatObject" object from *v*, or "NULL" on failure.
|
36 |
+
|
37 |
+
double PyFloat_AsDouble(PyObject *pyfloat)
|
38 |
+
* Part of the Stable ABI.*
|
39 |
+
|
40 |
+
Return a C double representation of the contents of *pyfloat*. If
|
41 |
+
*pyfloat* is not a Python floating-point object but has a
|
42 |
+
"__float__()" method, this method will first be called to convert
|
43 |
+
*pyfloat* into a float. If "__float__()" is not defined then it
|
44 |
+
falls back to "__index__()". This method returns "-1.0" upon
|
45 |
+
failure, so one should call "PyErr_Occurred()" to check for errors.
|
46 |
+
|
47 |
+
Changed in version 3.8: Use "__index__()" if available.
|
48 |
+
|
49 |
+
double PyFloat_AS_DOUBLE(PyObject *pyfloat)
|
50 |
+
|
51 |
+
Return a C double representation of the contents of *pyfloat*, but
|
52 |
+
without error checking.
|
53 |
+
|
54 |
+
PyObject *PyFloat_GetInfo(void)
|
55 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
56 |
+
|
57 |
+
Return a structseq instance which contains information about the
|
58 |
+
precision, minimum and maximum values of a float. It's a thin
|
59 |
+
wrapper around the header file "float.h".
|
60 |
+
|
61 |
+
double PyFloat_GetMax()
|
62 |
+
* Part of the Stable ABI.*
|
63 |
+
|
64 |
+
Return the maximum representable finite float *DBL_MAX* as C
|
65 |
+
double.
|
66 |
+
|
67 |
+
double PyFloat_GetMin()
|
68 |
+
* Part of the Stable ABI.*
|
69 |
+
|
70 |
+
Return the minimum normalized positive float *DBL_MIN* as C double.
|
71 |
+
|
72 |
+
|
73 |
+
Pack and Unpack functions
|
74 |
+
=========================
|
75 |
+
|
76 |
+
The pack and unpack functions provide an efficient platform-
|
77 |
+
independent way to store floating-point values as byte strings. The
|
78 |
+
Pack routines produce a bytes string from a C double, and the Unpack
|
79 |
+
routines produce a C double from such a bytes string. The suffix (2, 4
|
80 |
+
or 8) specifies the number of bytes in the bytes string.
|
81 |
+
|
82 |
+
On platforms that appear to use IEEE 754 formats these functions work
|
83 |
+
by copying bits. On other platforms, the 2-byte format is identical to
|
84 |
+
the IEEE 754 binary16 half-precision format, the 4-byte format
|
85 |
+
(32-bit) is identical to the IEEE 754 binary32 single precision
|
86 |
+
format, and the 8-byte format to the IEEE 754 binary64 double
|
87 |
+
precision format, although the packing of INFs and NaNs (if such
|
88 |
+
things exist on the platform) isn't handled correctly, and attempting
|
89 |
+
to unpack a bytes string containing an IEEE INF or NaN will raise an
|
90 |
+
exception.
|
91 |
+
|
92 |
+
On non-IEEE platforms with more precision, or larger dynamic range,
|
93 |
+
than IEEE 754 supports, not all values can be packed; on non-IEEE
|
94 |
+
platforms with less precision, or smaller dynamic range, not all
|
95 |
+
values can be unpacked. What happens in such cases is partly
|
96 |
+
accidental (alas).
|
97 |
+
|
98 |
+
Added in version 3.11.
|
99 |
+
|
100 |
+
|
101 |
+
Pack functions
|
102 |
+
--------------
|
103 |
+
|
104 |
+
The pack routines write 2, 4 or 8 bytes, starting at *p*. *le* is an
|
105 |
+
int argument, non-zero if you want the bytes string in little-endian
|
106 |
+
format (exponent last, at "p+1", "p+3", or "p+6" "p+7"), zero if you
|
107 |
+
want big-endian format (exponent first, at *p*). The "PY_BIG_ENDIAN"
|
108 |
+
constant can be used to use the native endian: it is equal to "1" on
|
109 |
+
big endian processor, or "0" on little endian processor.
|
110 |
+
|
111 |
+
Return value: "0" if all is OK, "-1" if error (and an exception is
|
112 |
+
set, most likely "OverflowError").
|
113 |
+
|
114 |
+
There are two problems on non-IEEE platforms:
|
115 |
+
|
116 |
+
* What this does is undefined if *x* is a NaN or infinity.
|
117 |
+
|
118 |
+
* "-0.0" and "+0.0" produce the same bytes string.
|
119 |
+
|
120 |
+
int PyFloat_Pack2(double x, unsigned char *p, int le)
|
121 |
+
|
122 |
+
Pack a C double as the IEEE 754 binary16 half-precision format.
|
123 |
+
|
124 |
+
int PyFloat_Pack4(double x, unsigned char *p, int le)
|
125 |
+
|
126 |
+
Pack a C double as the IEEE 754 binary32 single precision format.
|
127 |
+
|
128 |
+
int PyFloat_Pack8(double x, unsigned char *p, int le)
|
129 |
+
|
130 |
+
Pack a C double as the IEEE 754 binary64 double precision format.
|
131 |
+
|
132 |
+
|
133 |
+
Unpack functions
|
134 |
+
----------------
|
135 |
+
|
136 |
+
The unpack routines read 2, 4 or 8 bytes, starting at *p*. *le* is an
|
137 |
+
int argument, non-zero if the bytes string is in little-endian format
|
138 |
+
(exponent last, at "p+1", "p+3" or "p+6" and "p+7"), zero if big-
|
139 |
+
endian (exponent first, at *p*). The "PY_BIG_ENDIAN" constant can be
|
140 |
+
used to use the native endian: it is equal to "1" on big endian
|
141 |
+
processor, or "0" on little endian processor.
|
142 |
+
|
143 |
+
Return value: The unpacked double. On error, this is "-1.0" and
|
144 |
+
"PyErr_Occurred()" is true (and an exception is set, most likely
|
145 |
+
"OverflowError").
|
146 |
+
|
147 |
+
Note that on a non-IEEE platform this will refuse to unpack a bytes
|
148 |
+
string that represents a NaN or infinity.
|
149 |
+
|
150 |
+
double PyFloat_Unpack2(const unsigned char *p, int le)
|
151 |
+
|
152 |
+
Unpack the IEEE 754 binary16 half-precision format as a C double.
|
153 |
+
|
154 |
+
double PyFloat_Unpack4(const unsigned char *p, int le)
|
155 |
+
|
156 |
+
Unpack the IEEE 754 binary32 single precision format as a C double.
|
157 |
+
|
158 |
+
double PyFloat_Unpack8(const unsigned char *p, int le)
|
159 |
+
|
160 |
+
Unpack the IEEE 754 binary64 double precision format as a C double.
|
python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/frame.txt
ADDED
@@ -0,0 +1,161 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Frame Objects
|
2 |
+
*************
|
3 |
+
|
4 |
+
type PyFrameObject
|
5 |
+
* Part of the Limited API (as an opaque struct).*
|
6 |
+
|
7 |
+
The C structure of the objects used to describe frame objects.
|
8 |
+
|
9 |
+
There are no public members in this structure.
|
10 |
+
|
11 |
+
Changed in version 3.11: The members of this structure were removed
|
12 |
+
from the public C API. Refer to the What's New entry for details.
|
13 |
+
|
14 |
+
The "PyEval_GetFrame()" and "PyThreadState_GetFrame()" functions can
|
15 |
+
be used to get a frame object.
|
16 |
+
|
17 |
+
See also Reflection.
|
18 |
+
|
19 |
+
PyTypeObject PyFrame_Type
|
20 |
+
|
21 |
+
The type of frame objects. It is the same object as
|
22 |
+
"types.FrameType" in the Python layer.
|
23 |
+
|
24 |
+
Changed in version 3.11: Previously, this type was only available
|
25 |
+
after including "<frameobject.h>".
|
26 |
+
|
27 |
+
int PyFrame_Check(PyObject *obj)
|
28 |
+
|
29 |
+
Return non-zero if *obj* is a frame object.
|
30 |
+
|
31 |
+
Changed in version 3.11: Previously, this function was only
|
32 |
+
available after including "<frameobject.h>".
|
33 |
+
|
34 |
+
PyFrameObject *PyFrame_GetBack(PyFrameObject *frame)
|
35 |
+
|
36 |
+
Get the *frame* next outer frame.
|
37 |
+
|
38 |
+
Return a *strong reference*, or "NULL" if *frame* has no outer
|
39 |
+
frame.
|
40 |
+
|
41 |
+
Added in version 3.9.
|
42 |
+
|
43 |
+
PyObject *PyFrame_GetBuiltins(PyFrameObject *frame)
|
44 |
+
|
45 |
+
Get the *frame*'s "f_builtins" attribute.
|
46 |
+
|
47 |
+
Return a *strong reference*. The result cannot be "NULL".
|
48 |
+
|
49 |
+
Added in version 3.11.
|
50 |
+
|
51 |
+
PyCodeObject *PyFrame_GetCode(PyFrameObject *frame)
|
52 |
+
* Part of the Stable ABI since version 3.10.*
|
53 |
+
|
54 |
+
Get the *frame* code.
|
55 |
+
|
56 |
+
Return a *strong reference*.
|
57 |
+
|
58 |
+
The result (frame code) cannot be "NULL".
|
59 |
+
|
60 |
+
Added in version 3.9.
|
61 |
+
|
62 |
+
PyObject *PyFrame_GetGenerator(PyFrameObject *frame)
|
63 |
+
|
64 |
+
Get the generator, coroutine, or async generator that owns this
|
65 |
+
frame, or "NULL" if this frame is not owned by a generator. Does
|
66 |
+
not raise an exception, even if the return value is "NULL".
|
67 |
+
|
68 |
+
Return a *strong reference*, or "NULL".
|
69 |
+
|
70 |
+
Added in version 3.11.
|
71 |
+
|
72 |
+
PyObject *PyFrame_GetGlobals(PyFrameObject *frame)
|
73 |
+
|
74 |
+
Get the *frame*'s "f_globals" attribute.
|
75 |
+
|
76 |
+
Return a *strong reference*. The result cannot be "NULL".
|
77 |
+
|
78 |
+
Added in version 3.11.
|
79 |
+
|
80 |
+
int PyFrame_GetLasti(PyFrameObject *frame)
|
81 |
+
|
82 |
+
Get the *frame*'s "f_lasti" attribute.
|
83 |
+
|
84 |
+
Returns -1 if "frame.f_lasti" is "None".
|
85 |
+
|
86 |
+
Added in version 3.11.
|
87 |
+
|
88 |
+
PyObject *PyFrame_GetVar(PyFrameObject *frame, PyObject *name)
|
89 |
+
|
90 |
+
Get the variable *name* of *frame*.
|
91 |
+
|
92 |
+
* Return a *strong reference* to the variable value on success.
|
93 |
+
|
94 |
+
* Raise "NameError" and return "NULL" if the variable does not
|
95 |
+
exist.
|
96 |
+
|
97 |
+
* Raise an exception and return "NULL" on error.
|
98 |
+
|
99 |
+
*name* type must be a "str".
|
100 |
+
|
101 |
+
Added in version 3.12.
|
102 |
+
|
103 |
+
PyObject *PyFrame_GetVarString(PyFrameObject *frame, const char *name)
|
104 |
+
|
105 |
+
Similar to "PyFrame_GetVar()", but the variable name is a C string
|
106 |
+
encoded in UTF-8.
|
107 |
+
|
108 |
+
Added in version 3.12.
|
109 |
+
|
110 |
+
PyObject *PyFrame_GetLocals(PyFrameObject *frame)
|
111 |
+
|
112 |
+
Get the *frame*'s "f_locals" attribute ("dict").
|
113 |
+
|
114 |
+
Return a *strong reference*.
|
115 |
+
|
116 |
+
Added in version 3.11.
|
117 |
+
|
118 |
+
int PyFrame_GetLineNumber(PyFrameObject *frame)
|
119 |
+
* Part of the Stable ABI since version 3.10.*
|
120 |
+
|
121 |
+
Return the line number that *frame* is currently executing.
|
122 |
+
|
123 |
+
|
124 |
+
Internal Frames
|
125 |
+
===============
|
126 |
+
|
127 |
+
Unless using **PEP 523**, you will not need this.
|
128 |
+
|
129 |
+
struct _PyInterpreterFrame
|
130 |
+
|
131 |
+
The interpreter's internal frame representation.
|
132 |
+
|
133 |
+
Added in version 3.11.
|
134 |
+
|
135 |
+
PyObject *PyUnstable_InterpreterFrame_GetCode(struct _PyInterpreterFrame *frame);
|
136 |
+
|
137 |
+
*This is Unstable API. It may change without warning in minor
|
138 |
+
releases.*
|
139 |
+
|
140 |
+
Return a *strong reference* to the code object for the frame.
|
141 |
+
|
142 |
+
Added in version 3.12.
|
143 |
+
|
144 |
+
int PyUnstable_InterpreterFrame_GetLasti(struct _PyInterpreterFrame *frame);
|
145 |
+
|
146 |
+
*This is Unstable API. It may change without warning in minor
|
147 |
+
releases.*
|
148 |
+
|
149 |
+
Return the byte offset into the last executed instruction.
|
150 |
+
|
151 |
+
Added in version 3.12.
|
152 |
+
|
153 |
+
int PyUnstable_InterpreterFrame_GetLine(struct _PyInterpreterFrame *frame);
|
154 |
+
|
155 |
+
*This is Unstable API. It may change without warning in minor
|
156 |
+
releases.*
|
157 |
+
|
158 |
+
Return the currently executing line number, or -1 if there is no
|
159 |
+
line number.
|
160 |
+
|
161 |
+
Added in version 3.12.
|
python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/function.txt
ADDED
@@ -0,0 +1,178 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Function Objects
|
2 |
+
****************
|
3 |
+
|
4 |
+
There are a few functions specific to Python functions.
|
5 |
+
|
6 |
+
type PyFunctionObject
|
7 |
+
|
8 |
+
The C structure used for functions.
|
9 |
+
|
10 |
+
PyTypeObject PyFunction_Type
|
11 |
+
|
12 |
+
This is an instance of "PyTypeObject" and represents the Python
|
13 |
+
function type. It is exposed to Python programmers as
|
14 |
+
"types.FunctionType".
|
15 |
+
|
16 |
+
int PyFunction_Check(PyObject *o)
|
17 |
+
|
18 |
+
Return true if *o* is a function object (has type
|
19 |
+
"PyFunction_Type"). The parameter must not be "NULL". This
|
20 |
+
function always succeeds.
|
21 |
+
|
22 |
+
PyObject *PyFunction_New(PyObject *code, PyObject *globals)
|
23 |
+
*Return value: New reference.*
|
24 |
+
|
25 |
+
Return a new function object associated with the code object
|
26 |
+
*code*. *globals* must be a dictionary with the global variables
|
27 |
+
accessible to the function.
|
28 |
+
|
29 |
+
The function's docstring and name are retrieved from the code
|
30 |
+
object. "__module__" is retrieved from *globals*. The argument
|
31 |
+
defaults, annotations and closure are set to "NULL". "__qualname__"
|
32 |
+
is set to the same value as the code object's "co_qualname" field.
|
33 |
+
|
34 |
+
PyObject *PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname)
|
35 |
+
*Return value: New reference.*
|
36 |
+
|
37 |
+
As "PyFunction_New()", but also allows setting the function
|
38 |
+
object's "__qualname__" attribute. *qualname* should be a unicode
|
39 |
+
object or "NULL"; if "NULL", the "__qualname__" attribute is set to
|
40 |
+
the same value as the code object's "co_qualname" field.
|
41 |
+
|
42 |
+
Added in version 3.3.
|
43 |
+
|
44 |
+
PyObject *PyFunction_GetCode(PyObject *op)
|
45 |
+
*Return value: Borrowed reference.*
|
46 |
+
|
47 |
+
Return the code object associated with the function object *op*.
|
48 |
+
|
49 |
+
PyObject *PyFunction_GetGlobals(PyObject *op)
|
50 |
+
*Return value: Borrowed reference.*
|
51 |
+
|
52 |
+
Return the globals dictionary associated with the function object
|
53 |
+
*op*.
|
54 |
+
|
55 |
+
PyObject *PyFunction_GetModule(PyObject *op)
|
56 |
+
*Return value: Borrowed reference.*
|
57 |
+
|
58 |
+
Return a *borrowed reference* to the "__module__" attribute of the
|
59 |
+
function object *op*. It can be *NULL*.
|
60 |
+
|
61 |
+
This is normally a "string" containing the module name, but can be
|
62 |
+
set to any other object by Python code.
|
63 |
+
|
64 |
+
PyObject *PyFunction_GetDefaults(PyObject *op)
|
65 |
+
*Return value: Borrowed reference.*
|
66 |
+
|
67 |
+
Return the argument default values of the function object *op*.
|
68 |
+
This can be a tuple of arguments or "NULL".
|
69 |
+
|
70 |
+
int PyFunction_SetDefaults(PyObject *op, PyObject *defaults)
|
71 |
+
|
72 |
+
Set the argument default values for the function object *op*.
|
73 |
+
*defaults* must be "Py_None" or a tuple.
|
74 |
+
|
75 |
+
Raises "SystemError" and returns "-1" on failure.
|
76 |
+
|
77 |
+
void PyFunction_SetVectorcall(PyFunctionObject *func, vectorcallfunc vectorcall)
|
78 |
+
|
79 |
+
Set the vectorcall field of a given function object *func*.
|
80 |
+
|
81 |
+
Warning: extensions using this API must preserve the behavior of
|
82 |
+
the unaltered (default) vectorcall function!
|
83 |
+
|
84 |
+
Added in version 3.12.
|
85 |
+
|
86 |
+
PyObject *PyFunction_GetClosure(PyObject *op)
|
87 |
+
*Return value: Borrowed reference.*
|
88 |
+
|
89 |
+
Return the closure associated with the function object *op*. This
|
90 |
+
can be "NULL" or a tuple of cell objects.
|
91 |
+
|
92 |
+
int PyFunction_SetClosure(PyObject *op, PyObject *closure)
|
93 |
+
|
94 |
+
Set the closure associated with the function object *op*. *closure*
|
95 |
+
must be "Py_None" or a tuple of cell objects.
|
96 |
+
|
97 |
+
Raises "SystemError" and returns "-1" on failure.
|
98 |
+
|
99 |
+
PyObject *PyFunction_GetAnnotations(PyObject *op)
|
100 |
+
*Return value: Borrowed reference.*
|
101 |
+
|
102 |
+
Return the annotations of the function object *op*. This can be a
|
103 |
+
mutable dictionary or "NULL".
|
104 |
+
|
105 |
+
int PyFunction_SetAnnotations(PyObject *op, PyObject *annotations)
|
106 |
+
|
107 |
+
Set the annotations for the function object *op*. *annotations*
|
108 |
+
must be a dictionary or "Py_None".
|
109 |
+
|
110 |
+
Raises "SystemError" and returns "-1" on failure.
|
111 |
+
|
112 |
+
int PyFunction_AddWatcher(PyFunction_WatchCallback callback)
|
113 |
+
|
114 |
+
Register *callback* as a function watcher for the current
|
115 |
+
interpreter. Return an ID which may be passed to
|
116 |
+
"PyFunction_ClearWatcher()". In case of error (e.g. no more watcher
|
117 |
+
IDs available), return "-1" and set an exception.
|
118 |
+
|
119 |
+
Added in version 3.12.
|
120 |
+
|
121 |
+
int PyFunction_ClearWatcher(int watcher_id)
|
122 |
+
|
123 |
+
Clear watcher identified by *watcher_id* previously returned from
|
124 |
+
"PyFunction_AddWatcher()" for the current interpreter. Return "0"
|
125 |
+
on success, or "-1" and set an exception on error (e.g. if the
|
126 |
+
given *watcher_id* was never registered.)
|
127 |
+
|
128 |
+
Added in version 3.12.
|
129 |
+
|
130 |
+
type PyFunction_WatchEvent
|
131 |
+
|
132 |
+
Enumeration of possible function watcher events: -
|
133 |
+
"PyFunction_EVENT_CREATE" - "PyFunction_EVENT_DESTROY" -
|
134 |
+
"PyFunction_EVENT_MODIFY_CODE" - "PyFunction_EVENT_MODIFY_DEFAULTS"
|
135 |
+
- "PyFunction_EVENT_MODIFY_KWDEFAULTS"
|
136 |
+
|
137 |
+
Added in version 3.12.
|
138 |
+
|
139 |
+
typedef int (*PyFunction_WatchCallback)(PyFunction_WatchEvent event, PyFunctionObject *func, PyObject *new_value)
|
140 |
+
|
141 |
+
Type of a function watcher callback function.
|
142 |
+
|
143 |
+
If *event* is "PyFunction_EVENT_CREATE" or
|
144 |
+
"PyFunction_EVENT_DESTROY" then *new_value* will be "NULL".
|
145 |
+
Otherwise, *new_value* will hold a *borrowed reference* to the new
|
146 |
+
value that is about to be stored in *func* for the attribute that
|
147 |
+
is being modified.
|
148 |
+
|
149 |
+
The callback may inspect but must not modify *func*; doing so could
|
150 |
+
have unpredictable effects, including infinite recursion.
|
151 |
+
|
152 |
+
If *event* is "PyFunction_EVENT_CREATE", then the callback is
|
153 |
+
invoked after *func* has been fully initialized. Otherwise, the
|
154 |
+
callback is invoked before the modification to *func* takes place,
|
155 |
+
so the prior state of *func* can be inspected. The runtime is
|
156 |
+
permitted to optimize away the creation of function objects when
|
157 |
+
possible. In such cases no event will be emitted. Although this
|
158 |
+
creates the possibility of an observable difference of runtime
|
159 |
+
behavior depending on optimization decisions, it does not change
|
160 |
+
the semantics of the Python code being executed.
|
161 |
+
|
162 |
+
If *event* is "PyFunction_EVENT_DESTROY", Taking a reference in
|
163 |
+
the callback to the about-to-be-destroyed function will resurrect
|
164 |
+
it, preventing it from being freed at this time. When the
|
165 |
+
resurrected object is destroyed later, any watcher callbacks active
|
166 |
+
at that time will be called again.
|
167 |
+
|
168 |
+
If the callback sets an exception, it must return "-1"; this
|
169 |
+
exception will be printed as an unraisable exception using
|
170 |
+
"PyErr_WriteUnraisable()". Otherwise it should return "0".
|
171 |
+
|
172 |
+
There may already be a pending exception set on entry to the
|
173 |
+
callback. In this case, the callback should return "0" with the
|
174 |
+
same exception still set. This means the callback may not call any
|
175 |
+
other API that can set an exception unless it saves and clears the
|
176 |
+
exception state first, and restores it before returning.
|
177 |
+
|
178 |
+
Added in version 3.12.
|
python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/gcsupport.txt
ADDED
@@ -0,0 +1,282 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Supporting Cyclic Garbage Collection
|
2 |
+
************************************
|
3 |
+
|
4 |
+
Python's support for detecting and collecting garbage which involves
|
5 |
+
circular references requires support from object types which are
|
6 |
+
"containers" for other objects which may also be containers. Types
|
7 |
+
which do not store references to other objects, or which only store
|
8 |
+
references to atomic types (such as numbers or strings), do not need
|
9 |
+
to provide any explicit support for garbage collection.
|
10 |
+
|
11 |
+
To create a container type, the "tp_flags" field of the type object
|
12 |
+
must include the "Py_TPFLAGS_HAVE_GC" and provide an implementation of
|
13 |
+
the "tp_traverse" handler. If instances of the type are mutable, a
|
14 |
+
"tp_clear" implementation must also be provided.
|
15 |
+
|
16 |
+
"Py_TPFLAGS_HAVE_GC"
|
17 |
+
Objects with a type with this flag set must conform with the rules
|
18 |
+
documented here. For convenience these objects will be referred to
|
19 |
+
as container objects.
|
20 |
+
|
21 |
+
Constructors for container types must conform to two rules:
|
22 |
+
|
23 |
+
1. The memory for the object must be allocated using "PyObject_GC_New"
|
24 |
+
or "PyObject_GC_NewVar".
|
25 |
+
|
26 |
+
2. Once all the fields which may contain references to other
|
27 |
+
containers are initialized, it must call "PyObject_GC_Track()".
|
28 |
+
|
29 |
+
Similarly, the deallocator for the object must conform to a similar
|
30 |
+
pair of rules:
|
31 |
+
|
32 |
+
1. Before fields which refer to other containers are invalidated,
|
33 |
+
"PyObject_GC_UnTrack()" must be called.
|
34 |
+
|
35 |
+
2. The object's memory must be deallocated using "PyObject_GC_Del()".
|
36 |
+
|
37 |
+
Warning:
|
38 |
+
|
39 |
+
If a type adds the Py_TPFLAGS_HAVE_GC, then it *must* implement
|
40 |
+
at least a "tp_traverse" handler or explicitly use one from its
|
41 |
+
subclass or subclasses.When calling "PyType_Ready()" or some of
|
42 |
+
the APIs that indirectly call it like
|
43 |
+
"PyType_FromSpecWithBases()" or "PyType_FromSpec()" the
|
44 |
+
interpreter will automatically populate the "tp_flags",
|
45 |
+
"tp_traverse" and "tp_clear" fields if the type inherits from a
|
46 |
+
class that implements the garbage collector protocol and the
|
47 |
+
child class does *not* include the "Py_TPFLAGS_HAVE_GC" flag.
|
48 |
+
|
49 |
+
PyObject_GC_New(TYPE, typeobj)
|
50 |
+
|
51 |
+
Analogous to "PyObject_New" but for container objects with the
|
52 |
+
"Py_TPFLAGS_HAVE_GC" flag set.
|
53 |
+
|
54 |
+
PyObject_GC_NewVar(TYPE, typeobj, size)
|
55 |
+
|
56 |
+
Analogous to "PyObject_NewVar" but for container objects with the
|
57 |
+
"Py_TPFLAGS_HAVE_GC" flag set.
|
58 |
+
|
59 |
+
PyObject *PyUnstable_Object_GC_NewWithExtraData(PyTypeObject *type, size_t extra_size)
|
60 |
+
|
61 |
+
*This is Unstable API. It may change without warning in minor
|
62 |
+
releases.*
|
63 |
+
|
64 |
+
Analogous to "PyObject_GC_New" but allocates *extra_size* bytes at
|
65 |
+
the end of the object (at offset "tp_basicsize"). The allocated
|
66 |
+
memory is initialized to zeros, except for the "Python object
|
67 |
+
header".
|
68 |
+
|
69 |
+
The extra data will be deallocated with the object, but otherwise
|
70 |
+
it is not managed by Python.
|
71 |
+
|
72 |
+
Warning:
|
73 |
+
|
74 |
+
The function is marked as unstable because the final mechanism
|
75 |
+
for reserving extra data after an instance is not yet decided.
|
76 |
+
For allocating a variable number of fields, prefer using
|
77 |
+
"PyVarObject" and "tp_itemsize" instead.
|
78 |
+
|
79 |
+
Added in version 3.12.
|
80 |
+
|
81 |
+
PyObject_GC_Resize(TYPE, op, newsize)
|
82 |
+
|
83 |
+
Resize an object allocated by "PyObject_NewVar". Returns the
|
84 |
+
resized object of type "TYPE*" (refers to any C type) or "NULL" on
|
85 |
+
failure.
|
86 |
+
|
87 |
+
*op* must be of type PyVarObject* and must not be tracked by the
|
88 |
+
collector yet. *newsize* must be of type "Py_ssize_t".
|
89 |
+
|
90 |
+
void PyObject_GC_Track(PyObject *op)
|
91 |
+
* Part of the Stable ABI.*
|
92 |
+
|
93 |
+
Adds the object *op* to the set of container objects tracked by the
|
94 |
+
collector. The collector can run at unexpected times so objects
|
95 |
+
must be valid while being tracked. This should be called once all
|
96 |
+
the fields followed by the "tp_traverse" handler become valid,
|
97 |
+
usually near the end of the constructor.
|
98 |
+
|
99 |
+
int PyObject_IS_GC(PyObject *obj)
|
100 |
+
|
101 |
+
Returns non-zero if the object implements the garbage collector
|
102 |
+
protocol, otherwise returns 0.
|
103 |
+
|
104 |
+
The object cannot be tracked by the garbage collector if this
|
105 |
+
function returns 0.
|
106 |
+
|
107 |
+
int PyObject_GC_IsTracked(PyObject *op)
|
108 |
+
* Part of the Stable ABI since version 3.9.*
|
109 |
+
|
110 |
+
Returns 1 if the object type of *op* implements the GC protocol and
|
111 |
+
*op* is being currently tracked by the garbage collector and 0
|
112 |
+
otherwise.
|
113 |
+
|
114 |
+
This is analogous to the Python function "gc.is_tracked()".
|
115 |
+
|
116 |
+
Added in version 3.9.
|
117 |
+
|
118 |
+
int PyObject_GC_IsFinalized(PyObject *op)
|
119 |
+
* Part of the Stable ABI since version 3.9.*
|
120 |
+
|
121 |
+
Returns 1 if the object type of *op* implements the GC protocol and
|
122 |
+
*op* has been already finalized by the garbage collector and 0
|
123 |
+
otherwise.
|
124 |
+
|
125 |
+
This is analogous to the Python function "gc.is_finalized()".
|
126 |
+
|
127 |
+
Added in version 3.9.
|
128 |
+
|
129 |
+
void PyObject_GC_Del(void *op)
|
130 |
+
* Part of the Stable ABI.*
|
131 |
+
|
132 |
+
Releases memory allocated to an object using "PyObject_GC_New" or
|
133 |
+
"PyObject_GC_NewVar".
|
134 |
+
|
135 |
+
void PyObject_GC_UnTrack(void *op)
|
136 |
+
* Part of the Stable ABI.*
|
137 |
+
|
138 |
+
Remove the object *op* from the set of container objects tracked by
|
139 |
+
the collector. Note that "PyObject_GC_Track()" can be called again
|
140 |
+
on this object to add it back to the set of tracked objects. The
|
141 |
+
deallocator ("tp_dealloc" handler) should call this for the object
|
142 |
+
before any of the fields used by the "tp_traverse" handler become
|
143 |
+
invalid.
|
144 |
+
|
145 |
+
Changed in version 3.8: The "_PyObject_GC_TRACK()" and
|
146 |
+
"_PyObject_GC_UNTRACK()" macros have been removed from the public C
|
147 |
+
API.
|
148 |
+
|
149 |
+
The "tp_traverse" handler accepts a function parameter of this type:
|
150 |
+
|
151 |
+
typedef int (*visitproc)(PyObject *object, void *arg)
|
152 |
+
* Part of the Stable ABI.*
|
153 |
+
|
154 |
+
Type of the visitor function passed to the "tp_traverse" handler.
|
155 |
+
The function should be called with an object to traverse as
|
156 |
+
*object* and the third parameter to the "tp_traverse" handler as
|
157 |
+
*arg*. The Python core uses several visitor functions to implement
|
158 |
+
cyclic garbage detection; it's not expected that users will need to
|
159 |
+
write their own visitor functions.
|
160 |
+
|
161 |
+
The "tp_traverse" handler must have the following type:
|
162 |
+
|
163 |
+
typedef int (*traverseproc)(PyObject *self, visitproc visit, void *arg)
|
164 |
+
* Part of the Stable ABI.*
|
165 |
+
|
166 |
+
Traversal function for a container object. Implementations must
|
167 |
+
call the *visit* function for each object directly contained by
|
168 |
+
*self*, with the parameters to *visit* being the contained object
|
169 |
+
and the *arg* value passed to the handler. The *visit* function
|
170 |
+
must not be called with a "NULL" object argument. If *visit*
|
171 |
+
returns a non-zero value that value should be returned immediately.
|
172 |
+
|
173 |
+
To simplify writing "tp_traverse" handlers, a "Py_VISIT()" macro is
|
174 |
+
provided. In order to use this macro, the "tp_traverse"
|
175 |
+
implementation must name its arguments exactly *visit* and *arg*:
|
176 |
+
|
177 |
+
void Py_VISIT(PyObject *o)
|
178 |
+
|
179 |
+
If *o* is not "NULL", call the *visit* callback, with arguments *o*
|
180 |
+
and *arg*. If *visit* returns a non-zero value, then return it.
|
181 |
+
Using this macro, "tp_traverse" handlers look like:
|
182 |
+
|
183 |
+
static int
|
184 |
+
my_traverse(Noddy *self, visitproc visit, void *arg)
|
185 |
+
{
|
186 |
+
Py_VISIT(self->foo);
|
187 |
+
Py_VISIT(self->bar);
|
188 |
+
return 0;
|
189 |
+
}
|
190 |
+
|
191 |
+
The "tp_clear" handler must be of the "inquiry" type, or "NULL" if the
|
192 |
+
object is immutable.
|
193 |
+
|
194 |
+
typedef int (*inquiry)(PyObject *self)
|
195 |
+
* Part of the Stable ABI.*
|
196 |
+
|
197 |
+
Drop references that may have created reference cycles. Immutable
|
198 |
+
objects do not have to define this method since they can never
|
199 |
+
directly create reference cycles. Note that the object must still
|
200 |
+
be valid after calling this method (don't just call "Py_DECREF()"
|
201 |
+
on a reference). The collector will call this method if it detects
|
202 |
+
that this object is involved in a reference cycle.
|
203 |
+
|
204 |
+
|
205 |
+
Controlling the Garbage Collector State
|
206 |
+
=======================================
|
207 |
+
|
208 |
+
The C-API provides the following functions for controlling garbage
|
209 |
+
collection runs.
|
210 |
+
|
211 |
+
Py_ssize_t PyGC_Collect(void)
|
212 |
+
* Part of the Stable ABI.*
|
213 |
+
|
214 |
+
Perform a full garbage collection, if the garbage collector is
|
215 |
+
enabled. (Note that "gc.collect()" runs it unconditionally.)
|
216 |
+
|
217 |
+
Returns the number of collected + unreachable objects which cannot
|
218 |
+
be collected. If the garbage collector is disabled or already
|
219 |
+
collecting, returns "0" immediately. Errors during garbage
|
220 |
+
collection are passed to "sys.unraisablehook". This function does
|
221 |
+
not raise exceptions.
|
222 |
+
|
223 |
+
int PyGC_Enable(void)
|
224 |
+
* Part of the Stable ABI since version 3.10.*
|
225 |
+
|
226 |
+
Enable the garbage collector: similar to "gc.enable()". Returns the
|
227 |
+
previous state, 0 for disabled and 1 for enabled.
|
228 |
+
|
229 |
+
Added in version 3.10.
|
230 |
+
|
231 |
+
int PyGC_Disable(void)
|
232 |
+
* Part of the Stable ABI since version 3.10.*
|
233 |
+
|
234 |
+
Disable the garbage collector: similar to "gc.disable()". Returns
|
235 |
+
the previous state, 0 for disabled and 1 for enabled.
|
236 |
+
|
237 |
+
Added in version 3.10.
|
238 |
+
|
239 |
+
int PyGC_IsEnabled(void)
|
240 |
+
* Part of the Stable ABI since version 3.10.*
|
241 |
+
|
242 |
+
Query the state of the garbage collector: similar to
|
243 |
+
"gc.isenabled()". Returns the current state, 0 for disabled and 1
|
244 |
+
for enabled.
|
245 |
+
|
246 |
+
Added in version 3.10.
|
247 |
+
|
248 |
+
|
249 |
+
Querying Garbage Collector State
|
250 |
+
================================
|
251 |
+
|
252 |
+
The C-API provides the following interface for querying information
|
253 |
+
about the garbage collector.
|
254 |
+
|
255 |
+
void PyUnstable_GC_VisitObjects(gcvisitobjects_t callback, void *arg)
|
256 |
+
|
257 |
+
*This is Unstable API. It may change without warning in minor
|
258 |
+
releases.*
|
259 |
+
|
260 |
+
Run supplied *callback* on all live GC-capable objects. *arg* is
|
261 |
+
passed through to all invocations of *callback*.
|
262 |
+
|
263 |
+
Warning:
|
264 |
+
|
265 |
+
If new objects are (de)allocated by the callback it is undefined
|
266 |
+
if they will be visited.Garbage collection is disabled during
|
267 |
+
operation. Explicitly running a collection in the callback may
|
268 |
+
lead to undefined behaviour e.g. visiting the same objects
|
269 |
+
multiple times or not at all.
|
270 |
+
|
271 |
+
Added in version 3.12.
|
272 |
+
|
273 |
+
typedef int (*gcvisitobjects_t)(PyObject *object, void *arg)
|
274 |
+
|
275 |
+
Type of the visitor function to be passed to
|
276 |
+
"PyUnstable_GC_VisitObjects()". *arg* is the same as the *arg*
|
277 |
+
passed to "PyUnstable_GC_VisitObjects". Return "0" to continue
|
278 |
+
iteration, return "1" to stop iteration. Other return values are
|
279 |
+
reserved for now so behavior on returning anything else is
|
280 |
+
undefined.
|
281 |
+
|
282 |
+
Added in version 3.12.
|
python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/gen.txt
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Generator Objects
|
2 |
+
*****************
|
3 |
+
|
4 |
+
Generator objects are what Python uses to implement generator
|
5 |
+
iterators. They are normally created by iterating over a function that
|
6 |
+
yields values, rather than explicitly calling "PyGen_New()" or
|
7 |
+
"PyGen_NewWithQualName()".
|
8 |
+
|
9 |
+
type PyGenObject
|
10 |
+
|
11 |
+
The C structure used for generator objects.
|
12 |
+
|
13 |
+
PyTypeObject PyGen_Type
|
14 |
+
|
15 |
+
The type object corresponding to generator objects.
|
16 |
+
|
17 |
+
int PyGen_Check(PyObject *ob)
|
18 |
+
|
19 |
+
Return true if *ob* is a generator object; *ob* must not be "NULL".
|
20 |
+
This function always succeeds.
|
21 |
+
|
22 |
+
int PyGen_CheckExact(PyObject *ob)
|
23 |
+
|
24 |
+
Return true if *ob*'s type is "PyGen_Type"; *ob* must not be
|
25 |
+
"NULL". This function always succeeds.
|
26 |
+
|
27 |
+
PyObject *PyGen_New(PyFrameObject *frame)
|
28 |
+
*Return value: New reference.*
|
29 |
+
|
30 |
+
Create and return a new generator object based on the *frame*
|
31 |
+
object. A reference to *frame* is stolen by this function. The
|
32 |
+
argument must not be "NULL".
|
33 |
+
|
34 |
+
PyObject *PyGen_NewWithQualName(PyFrameObject *frame, PyObject *name, PyObject *qualname)
|
35 |
+
*Return value: New reference.*
|
36 |
+
|
37 |
+
Create and return a new generator object based on the *frame*
|
38 |
+
object, with "__name__" and "__qualname__" set to *name* and
|
39 |
+
*qualname*. A reference to *frame* is stolen by this function. The
|
40 |
+
*frame* argument must not be "NULL".
|
python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/hash.txt
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
PyHash API
|
2 |
+
**********
|
3 |
+
|
4 |
+
See also the "PyTypeObject.tp_hash" member.
|
5 |
+
|
6 |
+
type Py_hash_t
|
7 |
+
|
8 |
+
Hash value type: signed integer.
|
9 |
+
|
10 |
+
Added in version 3.2.
|
11 |
+
|
12 |
+
type Py_uhash_t
|
13 |
+
|
14 |
+
Hash value type: unsigned integer.
|
15 |
+
|
16 |
+
Added in version 3.2.
|
17 |
+
|
18 |
+
type PyHash_FuncDef
|
19 |
+
|
20 |
+
Hash function definition used by "PyHash_GetFuncDef()".
|
21 |
+
|
22 |
+
const char *name
|
23 |
+
|
24 |
+
Hash function name (UTF-8 encoded string).
|
25 |
+
|
26 |
+
const int hash_bits
|
27 |
+
|
28 |
+
Internal size of the hash value in bits.
|
29 |
+
|
30 |
+
const int seed_bits
|
31 |
+
|
32 |
+
Size of seed input in bits.
|
33 |
+
|
34 |
+
Added in version 3.4.
|
35 |
+
|
36 |
+
PyHash_FuncDef *PyHash_GetFuncDef(void)
|
37 |
+
|
38 |
+
Get the hash function definition.
|
39 |
+
|
40 |
+
See also: **PEP 456** "Secure and interchangeable hash algorithm".
|
41 |
+
|
42 |
+
Added in version 3.4.
|
python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/import.txt
ADDED
@@ -0,0 +1,311 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Importing Modules
|
2 |
+
*****************
|
3 |
+
|
4 |
+
PyObject *PyImport_ImportModule(const char *name)
|
5 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
6 |
+
|
7 |
+
This is a wrapper around "PyImport_Import()" which takes a const
|
8 |
+
char* as an argument instead of a PyObject*.
|
9 |
+
|
10 |
+
PyObject *PyImport_ImportModuleNoBlock(const char *name)
|
11 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
12 |
+
|
13 |
+
This function is a deprecated alias of "PyImport_ImportModule()".
|
14 |
+
|
15 |
+
Changed in version 3.3: This function used to fail immediately when
|
16 |
+
the import lock was held by another thread. In Python 3.3 though,
|
17 |
+
the locking scheme switched to per-module locks for most purposes,
|
18 |
+
so this function's special behaviour isn't needed anymore.
|
19 |
+
|
20 |
+
PyObject *PyImport_ImportModuleEx(const char *name, PyObject *globals, PyObject *locals, PyObject *fromlist)
|
21 |
+
*Return value: New reference.*
|
22 |
+
|
23 |
+
Import a module. This is best described by referring to the built-
|
24 |
+
in Python function "__import__()".
|
25 |
+
|
26 |
+
The return value is a new reference to the imported module or top-
|
27 |
+
level package, or "NULL" with an exception set on failure. Like
|
28 |
+
for "__import__()", the return value when a submodule of a package
|
29 |
+
was requested is normally the top-level package, unless a non-empty
|
30 |
+
*fromlist* was given.
|
31 |
+
|
32 |
+
Failing imports remove incomplete module objects, like with
|
33 |
+
"PyImport_ImportModule()".
|
34 |
+
|
35 |
+
PyObject *PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals, PyObject *locals, PyObject *fromlist, int level)
|
36 |
+
*Return value: New reference.** Part of the Stable ABI since
|
37 |
+
version 3.7.*
|
38 |
+
|
39 |
+
Import a module. This is best described by referring to the built-
|
40 |
+
in Python function "__import__()", as the standard "__import__()"
|
41 |
+
function calls this function directly.
|
42 |
+
|
43 |
+
The return value is a new reference to the imported module or top-
|
44 |
+
level package, or "NULL" with an exception set on failure. Like
|
45 |
+
for "__import__()", the return value when a submodule of a package
|
46 |
+
was requested is normally the top-level package, unless a non-empty
|
47 |
+
*fromlist* was given.
|
48 |
+
|
49 |
+
Added in version 3.3.
|
50 |
+
|
51 |
+
PyObject *PyImport_ImportModuleLevel(const char *name, PyObject *globals, PyObject *locals, PyObject *fromlist, int level)
|
52 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
53 |
+
|
54 |
+
Similar to "PyImport_ImportModuleLevelObject()", but the name is a
|
55 |
+
UTF-8 encoded string instead of a Unicode object.
|
56 |
+
|
57 |
+
Changed in version 3.3: Negative values for *level* are no longer
|
58 |
+
accepted.
|
59 |
+
|
60 |
+
PyObject *PyImport_Import(PyObject *name)
|
61 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
62 |
+
|
63 |
+
This is a higher-level interface that calls the current "import
|
64 |
+
hook function" (with an explicit *level* of 0, meaning absolute
|
65 |
+
import). It invokes the "__import__()" function from the
|
66 |
+
"__builtins__" of the current globals. This means that the import
|
67 |
+
is done using whatever import hooks are installed in the current
|
68 |
+
environment.
|
69 |
+
|
70 |
+
This function always uses absolute imports.
|
71 |
+
|
72 |
+
PyObject *PyImport_ReloadModule(PyObject *m)
|
73 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
74 |
+
|
75 |
+
Reload a module. Return a new reference to the reloaded module, or
|
76 |
+
"NULL" with an exception set on failure (the module still exists in
|
77 |
+
this case).
|
78 |
+
|
79 |
+
PyObject *PyImport_AddModuleObject(PyObject *name)
|
80 |
+
*Return value: Borrowed reference.** Part of the Stable ABI since
|
81 |
+
version 3.7.*
|
82 |
+
|
83 |
+
Return the module object corresponding to a module name. The
|
84 |
+
*name* argument may be of the form "package.module". First check
|
85 |
+
the modules dictionary if there's one there, and if not, create a
|
86 |
+
new one and insert it in the modules dictionary. Return "NULL" with
|
87 |
+
an exception set on failure.
|
88 |
+
|
89 |
+
Note:
|
90 |
+
|
91 |
+
This function does not load or import the module; if the module
|
92 |
+
wasn't already loaded, you will get an empty module object. Use
|
93 |
+
"PyImport_ImportModule()" or one of its variants to import a
|
94 |
+
module. Package structures implied by a dotted name for *name*
|
95 |
+
are not created if not already present.
|
96 |
+
|
97 |
+
Added in version 3.3.
|
98 |
+
|
99 |
+
PyObject *PyImport_AddModule(const char *name)
|
100 |
+
*Return value: Borrowed reference.** Part of the Stable ABI.*
|
101 |
+
|
102 |
+
Similar to "PyImport_AddModuleObject()", but the name is a UTF-8
|
103 |
+
encoded string instead of a Unicode object.
|
104 |
+
|
105 |
+
PyObject *PyImport_ExecCodeModule(const char *name, PyObject *co)
|
106 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
107 |
+
|
108 |
+
Given a module name (possibly of the form "package.module") and a
|
109 |
+
code object read from a Python bytecode file or obtained from the
|
110 |
+
built-in function "compile()", load the module. Return a new
|
111 |
+
reference to the module object, or "NULL" with an exception set if
|
112 |
+
an error occurred. *name* is removed from "sys.modules" in error
|
113 |
+
cases, even if *name* was already in "sys.modules" on entry to
|
114 |
+
"PyImport_ExecCodeModule()". Leaving incompletely initialized
|
115 |
+
modules in "sys.modules" is dangerous, as imports of such modules
|
116 |
+
have no way to know that the module object is an unknown (and
|
117 |
+
probably damaged with respect to the module author's intents)
|
118 |
+
state.
|
119 |
+
|
120 |
+
The module's "__spec__" and "__loader__" will be set, if not set
|
121 |
+
already, with the appropriate values. The spec's loader will be
|
122 |
+
set to the module's "__loader__" (if set) and to an instance of
|
123 |
+
"SourceFileLoader" otherwise.
|
124 |
+
|
125 |
+
The module's "__file__" attribute will be set to the code object's
|
126 |
+
"co_filename". If applicable, "__cached__" will also be set.
|
127 |
+
|
128 |
+
This function will reload the module if it was already imported.
|
129 |
+
See "PyImport_ReloadModule()" for the intended way to reload a
|
130 |
+
module.
|
131 |
+
|
132 |
+
If *name* points to a dotted name of the form "package.module", any
|
133 |
+
package structures not already created will still not be created.
|
134 |
+
|
135 |
+
See also "PyImport_ExecCodeModuleEx()" and
|
136 |
+
"PyImport_ExecCodeModuleWithPathnames()".
|
137 |
+
|
138 |
+
Changed in version 3.12: The setting of "__cached__" and
|
139 |
+
"__loader__" is deprecated. See "ModuleSpec" for alternatives.
|
140 |
+
|
141 |
+
PyObject *PyImport_ExecCodeModuleEx(const char *name, PyObject *co, const char *pathname)
|
142 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
143 |
+
|
144 |
+
Like "PyImport_ExecCodeModule()", but the "__file__" attribute of
|
145 |
+
the module object is set to *pathname* if it is non-"NULL".
|
146 |
+
|
147 |
+
See also "PyImport_ExecCodeModuleWithPathnames()".
|
148 |
+
|
149 |
+
PyObject *PyImport_ExecCodeModuleObject(PyObject *name, PyObject *co, PyObject *pathname, PyObject *cpathname)
|
150 |
+
*Return value: New reference.** Part of the Stable ABI since
|
151 |
+
version 3.7.*
|
152 |
+
|
153 |
+
Like "PyImport_ExecCodeModuleEx()", but the "__cached__" attribute
|
154 |
+
of the module object is set to *cpathname* if it is non-"NULL". Of
|
155 |
+
the three functions, this is the preferred one to use.
|
156 |
+
|
157 |
+
Added in version 3.3.
|
158 |
+
|
159 |
+
Changed in version 3.12: Setting "__cached__" is deprecated. See
|
160 |
+
"ModuleSpec" for alternatives.
|
161 |
+
|
162 |
+
PyObject *PyImport_ExecCodeModuleWithPathnames(const char *name, PyObject *co, const char *pathname, const char *cpathname)
|
163 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
164 |
+
|
165 |
+
Like "PyImport_ExecCodeModuleObject()", but *name*, *pathname* and
|
166 |
+
*cpathname* are UTF-8 encoded strings. Attempts are also made to
|
167 |
+
figure out what the value for *pathname* should be from *cpathname*
|
168 |
+
if the former is set to "NULL".
|
169 |
+
|
170 |
+
Added in version 3.2.
|
171 |
+
|
172 |
+
Changed in version 3.3: Uses "imp.source_from_cache()" in
|
173 |
+
calculating the source path if only the bytecode path is provided.
|
174 |
+
|
175 |
+
Changed in version 3.12: No longer uses the removed "imp" module.
|
176 |
+
|
177 |
+
long PyImport_GetMagicNumber()
|
178 |
+
* Part of the Stable ABI.*
|
179 |
+
|
180 |
+
Return the magic number for Python bytecode files (a.k.a. ".pyc"
|
181 |
+
file). The magic number should be present in the first four bytes
|
182 |
+
of the bytecode file, in little-endian byte order. Returns "-1" on
|
183 |
+
error.
|
184 |
+
|
185 |
+
Changed in version 3.3: Return value of "-1" upon failure.
|
186 |
+
|
187 |
+
const char *PyImport_GetMagicTag()
|
188 |
+
* Part of the Stable ABI.*
|
189 |
+
|
190 |
+
Return the magic tag string for **PEP 3147** format Python bytecode
|
191 |
+
file names. Keep in mind that the value at
|
192 |
+
"sys.implementation.cache_tag" is authoritative and should be used
|
193 |
+
instead of this function.
|
194 |
+
|
195 |
+
Added in version 3.2.
|
196 |
+
|
197 |
+
PyObject *PyImport_GetModuleDict()
|
198 |
+
*Return value: Borrowed reference.** Part of the Stable ABI.*
|
199 |
+
|
200 |
+
Return the dictionary used for the module administration (a.k.a.
|
201 |
+
"sys.modules"). Note that this is a per-interpreter variable.
|
202 |
+
|
203 |
+
PyObject *PyImport_GetModule(PyObject *name)
|
204 |
+
*Return value: New reference.** Part of the Stable ABI since
|
205 |
+
version 3.8.*
|
206 |
+
|
207 |
+
Return the already imported module with the given name. If the
|
208 |
+
module has not been imported yet then returns "NULL" but does not
|
209 |
+
set an error. Returns "NULL" and sets an error if the lookup
|
210 |
+
failed.
|
211 |
+
|
212 |
+
Added in version 3.7.
|
213 |
+
|
214 |
+
PyObject *PyImport_GetImporter(PyObject *path)
|
215 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
216 |
+
|
217 |
+
Return a finder object for a "sys.path"/"pkg.__path__" item *path*,
|
218 |
+
possibly by fetching it from the "sys.path_importer_cache" dict.
|
219 |
+
If it wasn't yet cached, traverse "sys.path_hooks" until a hook is
|
220 |
+
found that can handle the path item. Return "None" if no hook
|
221 |
+
could; this tells our caller that the *path based finder* could not
|
222 |
+
find a finder for this path item. Cache the result in
|
223 |
+
"sys.path_importer_cache". Return a new reference to the finder
|
224 |
+
object.
|
225 |
+
|
226 |
+
int PyImport_ImportFrozenModuleObject(PyObject *name)
|
227 |
+
* Part of the Stable ABI since version 3.7.*
|
228 |
+
|
229 |
+
Load a frozen module named *name*. Return "1" for success, "0" if
|
230 |
+
the module is not found, and "-1" with an exception set if the
|
231 |
+
initialization failed. To access the imported module on a
|
232 |
+
successful load, use "PyImport_ImportModule()". (Note the misnomer
|
233 |
+
--- this function would reload the module if it was already
|
234 |
+
imported.)
|
235 |
+
|
236 |
+
Added in version 3.3.
|
237 |
+
|
238 |
+
Changed in version 3.4: The "__file__" attribute is no longer set
|
239 |
+
on the module.
|
240 |
+
|
241 |
+
int PyImport_ImportFrozenModule(const char *name)
|
242 |
+
* Part of the Stable ABI.*
|
243 |
+
|
244 |
+
Similar to "PyImport_ImportFrozenModuleObject()", but the name is a
|
245 |
+
UTF-8 encoded string instead of a Unicode object.
|
246 |
+
|
247 |
+
struct _frozen
|
248 |
+
|
249 |
+
This is the structure type definition for frozen module
|
250 |
+
descriptors, as generated by the **freeze** utility (see
|
251 |
+
"Tools/freeze/" in the Python source distribution). Its
|
252 |
+
definition, found in "Include/import.h", is:
|
253 |
+
|
254 |
+
struct _frozen {
|
255 |
+
const char *name;
|
256 |
+
const unsigned char *code;
|
257 |
+
int size;
|
258 |
+
bool is_package;
|
259 |
+
};
|
260 |
+
|
261 |
+
Changed in version 3.11: The new "is_package" field indicates
|
262 |
+
whether the module is a package or not. This replaces setting the
|
263 |
+
"size" field to a negative value.
|
264 |
+
|
265 |
+
const struct _frozen *PyImport_FrozenModules
|
266 |
+
|
267 |
+
This pointer is initialized to point to an array of "_frozen"
|
268 |
+
records, terminated by one whose members are all "NULL" or zero.
|
269 |
+
When a frozen module is imported, it is searched in this table.
|
270 |
+
Third-party code could play tricks with this to provide a
|
271 |
+
dynamically created collection of frozen modules.
|
272 |
+
|
273 |
+
int PyImport_AppendInittab(const char *name, PyObject *(*initfunc)(void))
|
274 |
+
* Part of the Stable ABI.*
|
275 |
+
|
276 |
+
Add a single module to the existing table of built-in modules.
|
277 |
+
This is a convenience wrapper around "PyImport_ExtendInittab()",
|
278 |
+
returning "-1" if the table could not be extended. The new module
|
279 |
+
can be imported by the name *name*, and uses the function
|
280 |
+
*initfunc* as the initialization function called on the first
|
281 |
+
attempted import. This should be called before "Py_Initialize()".
|
282 |
+
|
283 |
+
struct _inittab
|
284 |
+
|
285 |
+
Structure describing a single entry in the list of built-in
|
286 |
+
modules. Programs which embed Python may use an array of these
|
287 |
+
structures in conjunction with "PyImport_ExtendInittab()" to
|
288 |
+
provide additional built-in modules. The structure consists of two
|
289 |
+
members:
|
290 |
+
|
291 |
+
const char *name
|
292 |
+
|
293 |
+
The module name, as an ASCII encoded string.
|
294 |
+
|
295 |
+
PyObject *(*initfunc)(void)
|
296 |
+
|
297 |
+
Initialization function for a module built into the interpreter.
|
298 |
+
|
299 |
+
int PyImport_ExtendInittab(struct _inittab *newtab)
|
300 |
+
|
301 |
+
Add a collection of modules to the table of built-in modules. The
|
302 |
+
*newtab* array must end with a sentinel entry which contains "NULL"
|
303 |
+
for the "name" field; failure to provide the sentinel value can
|
304 |
+
result in a memory fault. Returns "0" on success or "-1" if
|
305 |
+
insufficient memory could be allocated to extend the internal
|
306 |
+
table. In the event of failure, no modules are added to the
|
307 |
+
internal table. This must be called before "Py_Initialize()".
|
308 |
+
|
309 |
+
If Python is initialized multiple times, "PyImport_AppendInittab()"
|
310 |
+
or "PyImport_ExtendInittab()" must be called before each Python
|
311 |
+
initialization.
|
python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/index.txt
ADDED
@@ -0,0 +1,218 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Python/C API Reference Manual
|
2 |
+
*****************************
|
3 |
+
|
4 |
+
This manual documents the API used by C and C++ programmers who want
|
5 |
+
to write extension modules or embed Python. It is a companion to
|
6 |
+
Extending and Embedding the Python Interpreter, which describes the
|
7 |
+
general principles of extension writing but does not document the API
|
8 |
+
functions in detail.
|
9 |
+
|
10 |
+
* Introduction
|
11 |
+
|
12 |
+
* Coding standards
|
13 |
+
|
14 |
+
* Include Files
|
15 |
+
|
16 |
+
* Useful macros
|
17 |
+
|
18 |
+
* Objects, Types and Reference Counts
|
19 |
+
|
20 |
+
* Exceptions
|
21 |
+
|
22 |
+
* Embedding Python
|
23 |
+
|
24 |
+
* Debugging Builds
|
25 |
+
|
26 |
+
* C API Stability
|
27 |
+
|
28 |
+
* Unstable C API
|
29 |
+
|
30 |
+
* Stable Application Binary Interface
|
31 |
+
|
32 |
+
* Platform Considerations
|
33 |
+
|
34 |
+
* Contents of Limited API
|
35 |
+
|
36 |
+
* The Very High Level Layer
|
37 |
+
|
38 |
+
* Reference Counting
|
39 |
+
|
40 |
+
* Exception Handling
|
41 |
+
|
42 |
+
* Printing and clearing
|
43 |
+
|
44 |
+
* Raising exceptions
|
45 |
+
|
46 |
+
* Issuing warnings
|
47 |
+
|
48 |
+
* Querying the error indicator
|
49 |
+
|
50 |
+
* Signal Handling
|
51 |
+
|
52 |
+
* Exception Classes
|
53 |
+
|
54 |
+
* Exception Objects
|
55 |
+
|
56 |
+
* Unicode Exception Objects
|
57 |
+
|
58 |
+
* Recursion Control
|
59 |
+
|
60 |
+
* Standard Exceptions
|
61 |
+
|
62 |
+
* Standard Warning Categories
|
63 |
+
|
64 |
+
* Utilities
|
65 |
+
|
66 |
+
* Operating System Utilities
|
67 |
+
|
68 |
+
* System Functions
|
69 |
+
|
70 |
+
* Process Control
|
71 |
+
|
72 |
+
* Importing Modules
|
73 |
+
|
74 |
+
* Data marshalling support
|
75 |
+
|
76 |
+
* Parsing arguments and building values
|
77 |
+
|
78 |
+
* String conversion and formatting
|
79 |
+
|
80 |
+
* PyHash API
|
81 |
+
|
82 |
+
* Reflection
|
83 |
+
|
84 |
+
* Codec registry and support functions
|
85 |
+
|
86 |
+
* Support for Perf Maps
|
87 |
+
|
88 |
+
* Abstract Objects Layer
|
89 |
+
|
90 |
+
* Object Protocol
|
91 |
+
|
92 |
+
* Call Protocol
|
93 |
+
|
94 |
+
* Number Protocol
|
95 |
+
|
96 |
+
* Sequence Protocol
|
97 |
+
|
98 |
+
* Mapping Protocol
|
99 |
+
|
100 |
+
* Iterator Protocol
|
101 |
+
|
102 |
+
* Buffer Protocol
|
103 |
+
|
104 |
+
* Old Buffer Protocol
|
105 |
+
|
106 |
+
* Concrete Objects Layer
|
107 |
+
|
108 |
+
* Fundamental Objects
|
109 |
+
|
110 |
+
* Numeric Objects
|
111 |
+
|
112 |
+
* Sequence Objects
|
113 |
+
|
114 |
+
* Container Objects
|
115 |
+
|
116 |
+
* Function Objects
|
117 |
+
|
118 |
+
* Other Objects
|
119 |
+
|
120 |
+
* Initialization, Finalization, and Threads
|
121 |
+
|
122 |
+
* Before Python Initialization
|
123 |
+
|
124 |
+
* Global configuration variables
|
125 |
+
|
126 |
+
* Initializing and finalizing the interpreter
|
127 |
+
|
128 |
+
* Process-wide parameters
|
129 |
+
|
130 |
+
* Thread State and the Global Interpreter Lock
|
131 |
+
|
132 |
+
* Sub-interpreter support
|
133 |
+
|
134 |
+
* Asynchronous Notifications
|
135 |
+
|
136 |
+
* Profiling and Tracing
|
137 |
+
|
138 |
+
* Advanced Debugger Support
|
139 |
+
|
140 |
+
* Thread Local Storage Support
|
141 |
+
|
142 |
+
* Python Initialization Configuration
|
143 |
+
|
144 |
+
* Example
|
145 |
+
|
146 |
+
* PyWideStringList
|
147 |
+
|
148 |
+
* PyStatus
|
149 |
+
|
150 |
+
* PyPreConfig
|
151 |
+
|
152 |
+
* Preinitialize Python with PyPreConfig
|
153 |
+
|
154 |
+
* PyConfig
|
155 |
+
|
156 |
+
* Initialization with PyConfig
|
157 |
+
|
158 |
+
* Isolated Configuration
|
159 |
+
|
160 |
+
* Python Configuration
|
161 |
+
|
162 |
+
* Python Path Configuration
|
163 |
+
|
164 |
+
* Py_RunMain()
|
165 |
+
|
166 |
+
* Py_GetArgcArgv()
|
167 |
+
|
168 |
+
* Multi-Phase Initialization Private Provisional API
|
169 |
+
|
170 |
+
* Memory Management
|
171 |
+
|
172 |
+
* Overview
|
173 |
+
|
174 |
+
* Allocator Domains
|
175 |
+
|
176 |
+
* Raw Memory Interface
|
177 |
+
|
178 |
+
* Memory Interface
|
179 |
+
|
180 |
+
* Object allocators
|
181 |
+
|
182 |
+
* Default Memory Allocators
|
183 |
+
|
184 |
+
* Customize Memory Allocators
|
185 |
+
|
186 |
+
* Debug hooks on the Python memory allocators
|
187 |
+
|
188 |
+
* The pymalloc allocator
|
189 |
+
|
190 |
+
* tracemalloc C API
|
191 |
+
|
192 |
+
* Examples
|
193 |
+
|
194 |
+
* Object Implementation Support
|
195 |
+
|
196 |
+
* Allocating Objects on the Heap
|
197 |
+
|
198 |
+
* Common Object Structures
|
199 |
+
|
200 |
+
* Type Objects
|
201 |
+
|
202 |
+
* Number Object Structures
|
203 |
+
|
204 |
+
* Mapping Object Structures
|
205 |
+
|
206 |
+
* Sequence Object Structures
|
207 |
+
|
208 |
+
* Buffer Object Structures
|
209 |
+
|
210 |
+
* Async Object Structures
|
211 |
+
|
212 |
+
* Slot Type typedefs
|
213 |
+
|
214 |
+
* Examples
|
215 |
+
|
216 |
+
* Supporting Cyclic Garbage Collection
|
217 |
+
|
218 |
+
* API and ABI Versioning
|
python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/init.txt
ADDED
@@ -0,0 +1,2115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Initialization, Finalization, and Threads
|
2 |
+
*****************************************
|
3 |
+
|
4 |
+
See also Python Initialization Configuration.
|
5 |
+
|
6 |
+
|
7 |
+
Before Python Initialization
|
8 |
+
============================
|
9 |
+
|
10 |
+
In an application embedding Python, the "Py_Initialize()" function
|
11 |
+
must be called before using any other Python/C API functions; with the
|
12 |
+
exception of a few functions and the global configuration variables.
|
13 |
+
|
14 |
+
The following functions can be safely called before Python is
|
15 |
+
initialized:
|
16 |
+
|
17 |
+
* Configuration functions:
|
18 |
+
|
19 |
+
* "PyImport_AppendInittab()"
|
20 |
+
|
21 |
+
* "PyImport_ExtendInittab()"
|
22 |
+
|
23 |
+
* "PyInitFrozenExtensions()"
|
24 |
+
|
25 |
+
* "PyMem_SetAllocator()"
|
26 |
+
|
27 |
+
* "PyMem_SetupDebugHooks()"
|
28 |
+
|
29 |
+
* "PyObject_SetArenaAllocator()"
|
30 |
+
|
31 |
+
* "Py_SetPath()"
|
32 |
+
|
33 |
+
* "Py_SetProgramName()"
|
34 |
+
|
35 |
+
* "Py_SetPythonHome()"
|
36 |
+
|
37 |
+
* "Py_SetStandardStreamEncoding()"
|
38 |
+
|
39 |
+
* "PySys_AddWarnOption()"
|
40 |
+
|
41 |
+
* "PySys_AddXOption()"
|
42 |
+
|
43 |
+
* "PySys_ResetWarnOptions()"
|
44 |
+
|
45 |
+
* Informative functions:
|
46 |
+
|
47 |
+
* "Py_IsInitialized()"
|
48 |
+
|
49 |
+
* "PyMem_GetAllocator()"
|
50 |
+
|
51 |
+
* "PyObject_GetArenaAllocator()"
|
52 |
+
|
53 |
+
* "Py_GetBuildInfo()"
|
54 |
+
|
55 |
+
* "Py_GetCompiler()"
|
56 |
+
|
57 |
+
* "Py_GetCopyright()"
|
58 |
+
|
59 |
+
* "Py_GetPlatform()"
|
60 |
+
|
61 |
+
* "Py_GetVersion()"
|
62 |
+
|
63 |
+
* Utilities:
|
64 |
+
|
65 |
+
* "Py_DecodeLocale()"
|
66 |
+
|
67 |
+
* Memory allocators:
|
68 |
+
|
69 |
+
* "PyMem_RawMalloc()"
|
70 |
+
|
71 |
+
* "PyMem_RawRealloc()"
|
72 |
+
|
73 |
+
* "PyMem_RawCalloc()"
|
74 |
+
|
75 |
+
* "PyMem_RawFree()"
|
76 |
+
|
77 |
+
Note:
|
78 |
+
|
79 |
+
The following functions **should not be called** before
|
80 |
+
"Py_Initialize()": "Py_EncodeLocale()", "Py_GetPath()",
|
81 |
+
"Py_GetPrefix()", "Py_GetExecPrefix()", "Py_GetProgramFullPath()",
|
82 |
+
"Py_GetPythonHome()", "Py_GetProgramName()" and
|
83 |
+
"PyEval_InitThreads()".
|
84 |
+
|
85 |
+
|
86 |
+
Global configuration variables
|
87 |
+
==============================
|
88 |
+
|
89 |
+
Python has variables for the global configuration to control different
|
90 |
+
features and options. By default, these flags are controlled by
|
91 |
+
command line options.
|
92 |
+
|
93 |
+
When a flag is set by an option, the value of the flag is the number
|
94 |
+
of times that the option was set. For example, "-b" sets
|
95 |
+
"Py_BytesWarningFlag" to 1 and "-bb" sets "Py_BytesWarningFlag" to 2.
|
96 |
+
|
97 |
+
int Py_BytesWarningFlag
|
98 |
+
|
99 |
+
This API is kept for backward compatibility: setting
|
100 |
+
"PyConfig.bytes_warning" should be used instead, see Python
|
101 |
+
Initialization Configuration.
|
102 |
+
|
103 |
+
Issue a warning when comparing "bytes" or "bytearray" with "str" or
|
104 |
+
"bytes" with "int". Issue an error if greater or equal to "2".
|
105 |
+
|
106 |
+
Set by the "-b" option.
|
107 |
+
|
108 |
+
Deprecated since version 3.12.
|
109 |
+
|
110 |
+
int Py_DebugFlag
|
111 |
+
|
112 |
+
This API is kept for backward compatibility: setting
|
113 |
+
"PyConfig.parser_debug" should be used instead, see Python
|
114 |
+
Initialization Configuration.
|
115 |
+
|
116 |
+
Turn on parser debugging output (for expert only, depending on
|
117 |
+
compilation options).
|
118 |
+
|
119 |
+
Set by the "-d" option and the "PYTHONDEBUG" environment variable.
|
120 |
+
|
121 |
+
Deprecated since version 3.12.
|
122 |
+
|
123 |
+
int Py_DontWriteBytecodeFlag
|
124 |
+
|
125 |
+
This API is kept for backward compatibility: setting
|
126 |
+
"PyConfig.write_bytecode" should be used instead, see Python
|
127 |
+
Initialization Configuration.
|
128 |
+
|
129 |
+
If set to non-zero, Python won't try to write ".pyc" files on the
|
130 |
+
import of source modules.
|
131 |
+
|
132 |
+
Set by the "-B" option and the "PYTHONDONTWRITEBYTECODE"
|
133 |
+
environment variable.
|
134 |
+
|
135 |
+
Deprecated since version 3.12.
|
136 |
+
|
137 |
+
int Py_FrozenFlag
|
138 |
+
|
139 |
+
This API is kept for backward compatibility: setting
|
140 |
+
"PyConfig.pathconfig_warnings" should be used instead, see Python
|
141 |
+
Initialization Configuration.
|
142 |
+
|
143 |
+
Suppress error messages when calculating the module search path in
|
144 |
+
"Py_GetPath()".
|
145 |
+
|
146 |
+
Private flag used by "_freeze_module" and "frozenmain" programs.
|
147 |
+
|
148 |
+
Deprecated since version 3.12.
|
149 |
+
|
150 |
+
int Py_HashRandomizationFlag
|
151 |
+
|
152 |
+
This API is kept for backward compatibility: setting
|
153 |
+
"PyConfig.hash_seed" and "PyConfig.use_hash_seed" should be used
|
154 |
+
instead, see Python Initialization Configuration.
|
155 |
+
|
156 |
+
Set to "1" if the "PYTHONHASHSEED" environment variable is set to a
|
157 |
+
non-empty string.
|
158 |
+
|
159 |
+
If the flag is non-zero, read the "PYTHONHASHSEED" environment
|
160 |
+
variable to initialize the secret hash seed.
|
161 |
+
|
162 |
+
Deprecated since version 3.12.
|
163 |
+
|
164 |
+
int Py_IgnoreEnvironmentFlag
|
165 |
+
|
166 |
+
This API is kept for backward compatibility: setting
|
167 |
+
"PyConfig.use_environment" should be used instead, see Python
|
168 |
+
Initialization Configuration.
|
169 |
+
|
170 |
+
Ignore all "PYTHON*" environment variables, e.g. "PYTHONPATH" and
|
171 |
+
"PYTHONHOME", that might be set.
|
172 |
+
|
173 |
+
Set by the "-E" and "-I" options.
|
174 |
+
|
175 |
+
Deprecated since version 3.12.
|
176 |
+
|
177 |
+
int Py_InspectFlag
|
178 |
+
|
179 |
+
This API is kept for backward compatibility: setting
|
180 |
+
"PyConfig.inspect" should be used instead, see Python
|
181 |
+
Initialization Configuration.
|
182 |
+
|
183 |
+
When a script is passed as first argument or the "-c" option is
|
184 |
+
used, enter interactive mode after executing the script or the
|
185 |
+
command, even when "sys.stdin" does not appear to be a terminal.
|
186 |
+
|
187 |
+
Set by the "-i" option and the "PYTHONINSPECT" environment
|
188 |
+
variable.
|
189 |
+
|
190 |
+
Deprecated since version 3.12.
|
191 |
+
|
192 |
+
int Py_InteractiveFlag
|
193 |
+
|
194 |
+
This API is kept for backward compatibility: setting
|
195 |
+
"PyConfig.interactive" should be used instead, see Python
|
196 |
+
Initialization Configuration.
|
197 |
+
|
198 |
+
Set by the "-i" option.
|
199 |
+
|
200 |
+
Deprecated since version 3.12.
|
201 |
+
|
202 |
+
int Py_IsolatedFlag
|
203 |
+
|
204 |
+
This API is kept for backward compatibility: setting
|
205 |
+
"PyConfig.isolated" should be used instead, see Python
|
206 |
+
Initialization Configuration.
|
207 |
+
|
208 |
+
Run Python in isolated mode. In isolated mode "sys.path" contains
|
209 |
+
neither the script's directory nor the user's site-packages
|
210 |
+
directory.
|
211 |
+
|
212 |
+
Set by the "-I" option.
|
213 |
+
|
214 |
+
Added in version 3.4.
|
215 |
+
|
216 |
+
Deprecated since version 3.12.
|
217 |
+
|
218 |
+
int Py_LegacyWindowsFSEncodingFlag
|
219 |
+
|
220 |
+
This API is kept for backward compatibility: setting
|
221 |
+
"PyPreConfig.legacy_windows_fs_encoding" should be used instead,
|
222 |
+
see Python Initialization Configuration.
|
223 |
+
|
224 |
+
If the flag is non-zero, use the "mbcs" encoding with "replace"
|
225 |
+
error handler, instead of the UTF-8 encoding with "surrogatepass"
|
226 |
+
error handler, for the *filesystem encoding and error handler*.
|
227 |
+
|
228 |
+
Set to "1" if the "PYTHONLEGACYWINDOWSFSENCODING" environment
|
229 |
+
variable is set to a non-empty string.
|
230 |
+
|
231 |
+
See **PEP 529** for more details.
|
232 |
+
|
233 |
+
Availability: Windows.
|
234 |
+
|
235 |
+
Deprecated since version 3.12.
|
236 |
+
|
237 |
+
int Py_LegacyWindowsStdioFlag
|
238 |
+
|
239 |
+
This API is kept for backward compatibility: setting
|
240 |
+
"PyConfig.legacy_windows_stdio" should be used instead, see Python
|
241 |
+
Initialization Configuration.
|
242 |
+
|
243 |
+
If the flag is non-zero, use "io.FileIO" instead of
|
244 |
+
"io._WindowsConsoleIO" for "sys" standard streams.
|
245 |
+
|
246 |
+
Set to "1" if the "PYTHONLEGACYWINDOWSSTDIO" environment variable
|
247 |
+
is set to a non-empty string.
|
248 |
+
|
249 |
+
See **PEP 528** for more details.
|
250 |
+
|
251 |
+
Availability: Windows.
|
252 |
+
|
253 |
+
Deprecated since version 3.12.
|
254 |
+
|
255 |
+
int Py_NoSiteFlag
|
256 |
+
|
257 |
+
This API is kept for backward compatibility: setting
|
258 |
+
"PyConfig.site_import" should be used instead, see Python
|
259 |
+
Initialization Configuration.
|
260 |
+
|
261 |
+
Disable the import of the module "site" and the site-dependent
|
262 |
+
manipulations of "sys.path" that it entails. Also disable these
|
263 |
+
manipulations if "site" is explicitly imported later (call
|
264 |
+
"site.main()" if you want them to be triggered).
|
265 |
+
|
266 |
+
Set by the "-S" option.
|
267 |
+
|
268 |
+
Deprecated since version 3.12.
|
269 |
+
|
270 |
+
int Py_NoUserSiteDirectory
|
271 |
+
|
272 |
+
This API is kept for backward compatibility: setting
|
273 |
+
"PyConfig.user_site_directory" should be used instead, see Python
|
274 |
+
Initialization Configuration.
|
275 |
+
|
276 |
+
Don't add the "user site-packages directory" to "sys.path".
|
277 |
+
|
278 |
+
Set by the "-s" and "-I" options, and the "PYTHONNOUSERSITE"
|
279 |
+
environment variable.
|
280 |
+
|
281 |
+
Deprecated since version 3.12.
|
282 |
+
|
283 |
+
int Py_OptimizeFlag
|
284 |
+
|
285 |
+
This API is kept for backward compatibility: setting
|
286 |
+
"PyConfig.optimization_level" should be used instead, see Python
|
287 |
+
Initialization Configuration.
|
288 |
+
|
289 |
+
Set by the "-O" option and the "PYTHONOPTIMIZE" environment
|
290 |
+
variable.
|
291 |
+
|
292 |
+
Deprecated since version 3.12.
|
293 |
+
|
294 |
+
int Py_QuietFlag
|
295 |
+
|
296 |
+
This API is kept for backward compatibility: setting
|
297 |
+
"PyConfig.quiet" should be used instead, see Python Initialization
|
298 |
+
Configuration.
|
299 |
+
|
300 |
+
Don't display the copyright and version messages even in
|
301 |
+
interactive mode.
|
302 |
+
|
303 |
+
Set by the "-q" option.
|
304 |
+
|
305 |
+
Added in version 3.2.
|
306 |
+
|
307 |
+
Deprecated since version 3.12.
|
308 |
+
|
309 |
+
int Py_UnbufferedStdioFlag
|
310 |
+
|
311 |
+
This API is kept for backward compatibility: setting
|
312 |
+
"PyConfig.buffered_stdio" should be used instead, see Python
|
313 |
+
Initialization Configuration.
|
314 |
+
|
315 |
+
Force the stdout and stderr streams to be unbuffered.
|
316 |
+
|
317 |
+
Set by the "-u" option and the "PYTHONUNBUFFERED" environment
|
318 |
+
variable.
|
319 |
+
|
320 |
+
Deprecated since version 3.12.
|
321 |
+
|
322 |
+
int Py_VerboseFlag
|
323 |
+
|
324 |
+
This API is kept for backward compatibility: setting
|
325 |
+
"PyConfig.verbose" should be used instead, see Python
|
326 |
+
Initialization Configuration.
|
327 |
+
|
328 |
+
Print a message each time a module is initialized, showing the
|
329 |
+
place (filename or built-in module) from which it is loaded. If
|
330 |
+
greater or equal to "2", print a message for each file that is
|
331 |
+
checked for when searching for a module. Also provides information
|
332 |
+
on module cleanup at exit.
|
333 |
+
|
334 |
+
Set by the "-v" option and the "PYTHONVERBOSE" environment
|
335 |
+
variable.
|
336 |
+
|
337 |
+
Deprecated since version 3.12.
|
338 |
+
|
339 |
+
|
340 |
+
Initializing and finalizing the interpreter
|
341 |
+
===========================================
|
342 |
+
|
343 |
+
void Py_Initialize()
|
344 |
+
* Part of the Stable ABI.*
|
345 |
+
|
346 |
+
Initialize the Python interpreter. In an application embedding
|
347 |
+
Python, this should be called before using any other Python/C API
|
348 |
+
functions; see Before Python Initialization for the few exceptions.
|
349 |
+
|
350 |
+
This initializes the table of loaded modules ("sys.modules"), and
|
351 |
+
creates the fundamental modules "builtins", "__main__" and "sys".
|
352 |
+
It also initializes the module search path ("sys.path"). It does
|
353 |
+
not set "sys.argv"; use "PySys_SetArgvEx()" for that. This is a
|
354 |
+
no-op when called for a second time (without calling
|
355 |
+
"Py_FinalizeEx()" first). There is no return value; it is a fatal
|
356 |
+
error if the initialization fails.
|
357 |
+
|
358 |
+
Use the "Py_InitializeFromConfig()" function to customize the
|
359 |
+
Python Initialization Configuration.
|
360 |
+
|
361 |
+
Note:
|
362 |
+
|
363 |
+
On Windows, changes the console mode from "O_TEXT" to "O_BINARY",
|
364 |
+
which will also affect non-Python uses of the console using the C
|
365 |
+
Runtime.
|
366 |
+
|
367 |
+
void Py_InitializeEx(int initsigs)
|
368 |
+
* Part of the Stable ABI.*
|
369 |
+
|
370 |
+
This function works like "Py_Initialize()" if *initsigs* is "1". If
|
371 |
+
*initsigs* is "0", it skips initialization registration of signal
|
372 |
+
handlers, which might be useful when Python is embedded.
|
373 |
+
|
374 |
+
Use the "Py_InitializeFromConfig()" function to customize the
|
375 |
+
Python Initialization Configuration.
|
376 |
+
|
377 |
+
int Py_IsInitialized()
|
378 |
+
* Part of the Stable ABI.*
|
379 |
+
|
380 |
+
Return true (nonzero) when the Python interpreter has been
|
381 |
+
initialized, false (zero) if not. After "Py_FinalizeEx()" is
|
382 |
+
called, this returns false until "Py_Initialize()" is called again.
|
383 |
+
|
384 |
+
int Py_FinalizeEx()
|
385 |
+
* Part of the Stable ABI since version 3.6.*
|
386 |
+
|
387 |
+
Undo all initializations made by "Py_Initialize()" and subsequent
|
388 |
+
use of Python/C API functions, and destroy all sub-interpreters
|
389 |
+
(see "Py_NewInterpreter()" below) that were created and not yet
|
390 |
+
destroyed since the last call to "Py_Initialize()". Ideally, this
|
391 |
+
frees all memory allocated by the Python interpreter. This is a
|
392 |
+
no-op when called for a second time (without calling
|
393 |
+
"Py_Initialize()" again first).
|
394 |
+
|
395 |
+
Since this is the reverse of "Py_Initialize()", it should be called
|
396 |
+
in the same thread with the same interpreter active. That means
|
397 |
+
the main thread and the main interpreter. This should never be
|
398 |
+
called while "Py_RunMain()" is running.
|
399 |
+
|
400 |
+
Normally the return value is "0". If there were errors during
|
401 |
+
finalization (flushing buffered data), "-1" is returned.
|
402 |
+
|
403 |
+
This function is provided for a number of reasons. An embedding
|
404 |
+
application might want to restart Python without having to restart
|
405 |
+
the application itself. An application that has loaded the Python
|
406 |
+
interpreter from a dynamically loadable library (or DLL) might want
|
407 |
+
to free all memory allocated by Python before unloading the DLL.
|
408 |
+
During a hunt for memory leaks in an application a developer might
|
409 |
+
want to free all memory allocated by Python before exiting from the
|
410 |
+
application.
|
411 |
+
|
412 |
+
**Bugs and caveats:** The destruction of modules and objects in
|
413 |
+
modules is done in random order; this may cause destructors
|
414 |
+
("__del__()" methods) to fail when they depend on other objects
|
415 |
+
(even functions) or modules. Dynamically loaded extension modules
|
416 |
+
loaded by Python are not unloaded. Small amounts of memory
|
417 |
+
allocated by the Python interpreter may not be freed (if you find a
|
418 |
+
leak, please report it). Memory tied up in circular references
|
419 |
+
between objects is not freed. Some memory allocated by extension
|
420 |
+
modules may not be freed. Some extensions may not work properly if
|
421 |
+
their initialization routine is called more than once; this can
|
422 |
+
happen if an application calls "Py_Initialize()" and
|
423 |
+
"Py_FinalizeEx()" more than once.
|
424 |
+
|
425 |
+
Raises an auditing event "cpython._PySys_ClearAuditHooks" with no
|
426 |
+
arguments.
|
427 |
+
|
428 |
+
Added in version 3.6.
|
429 |
+
|
430 |
+
void Py_Finalize()
|
431 |
+
* Part of the Stable ABI.*
|
432 |
+
|
433 |
+
This is a backwards-compatible version of "Py_FinalizeEx()" that
|
434 |
+
disregards the return value.
|
435 |
+
|
436 |
+
|
437 |
+
Process-wide parameters
|
438 |
+
=======================
|
439 |
+
|
440 |
+
int Py_SetStandardStreamEncoding(const char *encoding, const char *errors)
|
441 |
+
|
442 |
+
This API is kept for backward compatibility: setting
|
443 |
+
"PyConfig.stdio_encoding" and "PyConfig.stdio_errors" should be
|
444 |
+
used instead, see Python Initialization Configuration.
|
445 |
+
|
446 |
+
This function should be called before "Py_Initialize()", if it is
|
447 |
+
called at all. It specifies which encoding and error handling to
|
448 |
+
use with standard IO, with the same meanings as in "str.encode()".
|
449 |
+
|
450 |
+
It overrides "PYTHONIOENCODING" values, and allows embedding code
|
451 |
+
to control IO encoding when the environment variable does not work.
|
452 |
+
|
453 |
+
*encoding* and/or *errors* may be "NULL" to use "PYTHONIOENCODING"
|
454 |
+
and/or default values (depending on other settings).
|
455 |
+
|
456 |
+
Note that "sys.stderr" always uses the "backslashreplace" error
|
457 |
+
handler, regardless of this (or any other) setting.
|
458 |
+
|
459 |
+
If "Py_FinalizeEx()" is called, this function will need to be
|
460 |
+
called again in order to affect subsequent calls to
|
461 |
+
"Py_Initialize()".
|
462 |
+
|
463 |
+
Returns "0" if successful, a nonzero value on error (e.g. calling
|
464 |
+
after the interpreter has already been initialized).
|
465 |
+
|
466 |
+
Added in version 3.4.
|
467 |
+
|
468 |
+
Deprecated since version 3.11.
|
469 |
+
|
470 |
+
void Py_SetProgramName(const wchar_t *name)
|
471 |
+
* Part of the Stable ABI.*
|
472 |
+
|
473 |
+
This API is kept for backward compatibility: setting
|
474 |
+
"PyConfig.program_name" should be used instead, see Python
|
475 |
+
Initialization Configuration.
|
476 |
+
|
477 |
+
This function should be called before "Py_Initialize()" is called
|
478 |
+
for the first time, if it is called at all. It tells the
|
479 |
+
interpreter the value of the "argv[0]" argument to the "main()"
|
480 |
+
function of the program (converted to wide characters). This is
|
481 |
+
used by "Py_GetPath()" and some other functions below to find the
|
482 |
+
Python run-time libraries relative to the interpreter executable.
|
483 |
+
The default value is "'python'". The argument should point to a
|
484 |
+
zero-terminated wide character string in static storage whose
|
485 |
+
contents will not change for the duration of the program's
|
486 |
+
execution. No code in the Python interpreter will change the
|
487 |
+
contents of this storage.
|
488 |
+
|
489 |
+
Use "Py_DecodeLocale()" to decode a bytes string to get a wchar_t*
|
490 |
+
string.
|
491 |
+
|
492 |
+
Deprecated since version 3.11.
|
493 |
+
|
494 |
+
wchar_t *Py_GetProgramName()
|
495 |
+
* Part of the Stable ABI.*
|
496 |
+
|
497 |
+
Return the program name set with "Py_SetProgramName()", or the
|
498 |
+
default. The returned string points into static storage; the caller
|
499 |
+
should not modify its value.
|
500 |
+
|
501 |
+
This function should not be called before "Py_Initialize()",
|
502 |
+
otherwise it returns "NULL".
|
503 |
+
|
504 |
+
Changed in version 3.10: It now returns "NULL" if called before
|
505 |
+
"Py_Initialize()".
|
506 |
+
|
507 |
+
wchar_t *Py_GetPrefix()
|
508 |
+
* Part of the Stable ABI.*
|
509 |
+
|
510 |
+
Return the *prefix* for installed platform-independent files. This
|
511 |
+
is derived through a number of complicated rules from the program
|
512 |
+
name set with "Py_SetProgramName()" and some environment variables;
|
513 |
+
for example, if the program name is "'/usr/local/bin/python'", the
|
514 |
+
prefix is "'/usr/local'". The returned string points into static
|
515 |
+
storage; the caller should not modify its value. This corresponds
|
516 |
+
to the **prefix** variable in the top-level "Makefile" and the "--
|
517 |
+
prefix" argument to the **configure** script at build time. The
|
518 |
+
value is available to Python code as "sys.prefix". It is only
|
519 |
+
useful on Unix. See also the next function.
|
520 |
+
|
521 |
+
This function should not be called before "Py_Initialize()",
|
522 |
+
otherwise it returns "NULL".
|
523 |
+
|
524 |
+
Changed in version 3.10: It now returns "NULL" if called before
|
525 |
+
"Py_Initialize()".
|
526 |
+
|
527 |
+
wchar_t *Py_GetExecPrefix()
|
528 |
+
* Part of the Stable ABI.*
|
529 |
+
|
530 |
+
Return the *exec-prefix* for installed platform-*dependent* files.
|
531 |
+
This is derived through a number of complicated rules from the
|
532 |
+
program name set with "Py_SetProgramName()" and some environment
|
533 |
+
variables; for example, if the program name is
|
534 |
+
"'/usr/local/bin/python'", the exec-prefix is "'/usr/local'". The
|
535 |
+
returned string points into static storage; the caller should not
|
536 |
+
modify its value. This corresponds to the **exec_prefix** variable
|
537 |
+
in the top-level "Makefile" and the "--exec-prefix" argument to the
|
538 |
+
**configure** script at build time. The value is available to
|
539 |
+
Python code as "sys.exec_prefix". It is only useful on Unix.
|
540 |
+
|
541 |
+
Background: The exec-prefix differs from the prefix when platform
|
542 |
+
dependent files (such as executables and shared libraries) are
|
543 |
+
installed in a different directory tree. In a typical
|
544 |
+
installation, platform dependent files may be installed in the
|
545 |
+
"/usr/local/plat" subtree while platform independent may be
|
546 |
+
installed in "/usr/local".
|
547 |
+
|
548 |
+
Generally speaking, a platform is a combination of hardware and
|
549 |
+
software families, e.g. Sparc machines running the Solaris 2.x
|
550 |
+
operating system are considered the same platform, but Intel
|
551 |
+
machines running Solaris 2.x are another platform, and Intel
|
552 |
+
machines running Linux are yet another platform. Different major
|
553 |
+
revisions of the same operating system generally also form
|
554 |
+
different platforms. Non-Unix operating systems are a different
|
555 |
+
story; the installation strategies on those systems are so
|
556 |
+
different that the prefix and exec-prefix are meaningless, and set
|
557 |
+
to the empty string. Note that compiled Python bytecode files are
|
558 |
+
platform independent (but not independent from the Python version
|
559 |
+
by which they were compiled!).
|
560 |
+
|
561 |
+
System administrators will know how to configure the **mount** or
|
562 |
+
**automount** programs to share "/usr/local" between platforms
|
563 |
+
while having "/usr/local/plat" be a different filesystem for each
|
564 |
+
platform.
|
565 |
+
|
566 |
+
This function should not be called before "Py_Initialize()",
|
567 |
+
otherwise it returns "NULL".
|
568 |
+
|
569 |
+
Changed in version 3.10: It now returns "NULL" if called before
|
570 |
+
"Py_Initialize()".
|
571 |
+
|
572 |
+
wchar_t *Py_GetProgramFullPath()
|
573 |
+
* Part of the Stable ABI.*
|
574 |
+
|
575 |
+
Return the full program name of the Python executable; this is
|
576 |
+
computed as a side-effect of deriving the default module search
|
577 |
+
path from the program name (set by "Py_SetProgramName()" above).
|
578 |
+
The returned string points into static storage; the caller should
|
579 |
+
not modify its value. The value is available to Python code as
|
580 |
+
"sys.executable".
|
581 |
+
|
582 |
+
This function should not be called before "Py_Initialize()",
|
583 |
+
otherwise it returns "NULL".
|
584 |
+
|
585 |
+
Changed in version 3.10: It now returns "NULL" if called before
|
586 |
+
"Py_Initialize()".
|
587 |
+
|
588 |
+
wchar_t *Py_GetPath()
|
589 |
+
* Part of the Stable ABI.*
|
590 |
+
|
591 |
+
Return the default module search path; this is computed from the
|
592 |
+
program name (set by "Py_SetProgramName()" above) and some
|
593 |
+
environment variables. The returned string consists of a series of
|
594 |
+
directory names separated by a platform dependent delimiter
|
595 |
+
character. The delimiter character is "':'" on Unix and macOS,
|
596 |
+
"';'" on Windows. The returned string points into static storage;
|
597 |
+
the caller should not modify its value. The list "sys.path" is
|
598 |
+
initialized with this value on interpreter startup; it can be (and
|
599 |
+
usually is) modified later to change the search path for loading
|
600 |
+
modules.
|
601 |
+
|
602 |
+
This function should not be called before "Py_Initialize()",
|
603 |
+
otherwise it returns "NULL".
|
604 |
+
|
605 |
+
Changed in version 3.10: It now returns "NULL" if called before
|
606 |
+
"Py_Initialize()".
|
607 |
+
|
608 |
+
void Py_SetPath(const wchar_t*)
|
609 |
+
* Part of the Stable ABI since version 3.7.*
|
610 |
+
|
611 |
+
This API is kept for backward compatibility: setting
|
612 |
+
"PyConfig.module_search_paths" and
|
613 |
+
"PyConfig.module_search_paths_set" should be used instead, see
|
614 |
+
Python Initialization Configuration.
|
615 |
+
|
616 |
+
Set the default module search path. If this function is called
|
617 |
+
before "Py_Initialize()", then "Py_GetPath()" won't attempt to
|
618 |
+
compute a default search path but uses the one provided instead.
|
619 |
+
This is useful if Python is embedded by an application that has
|
620 |
+
full knowledge of the location of all modules. The path components
|
621 |
+
should be separated by the platform dependent delimiter character,
|
622 |
+
which is "':'" on Unix and macOS, "';'" on Windows.
|
623 |
+
|
624 |
+
This also causes "sys.executable" to be set to the program full
|
625 |
+
path (see "Py_GetProgramFullPath()") and for "sys.prefix" and
|
626 |
+
"sys.exec_prefix" to be empty. It is up to the caller to modify
|
627 |
+
these if required after calling "Py_Initialize()".
|
628 |
+
|
629 |
+
Use "Py_DecodeLocale()" to decode a bytes string to get a wchar_*
|
630 |
+
string.
|
631 |
+
|
632 |
+
The path argument is copied internally, so the caller may free it
|
633 |
+
after the call completes.
|
634 |
+
|
635 |
+
Changed in version 3.8: The program full path is now used for
|
636 |
+
"sys.executable", instead of the program name.
|
637 |
+
|
638 |
+
Deprecated since version 3.11.
|
639 |
+
|
640 |
+
const char *Py_GetVersion()
|
641 |
+
* Part of the Stable ABI.*
|
642 |
+
|
643 |
+
Return the version of this Python interpreter. This is a string
|
644 |
+
that looks something like
|
645 |
+
|
646 |
+
"3.0a5+ (py3k:63103M, May 12 2008, 00:53:55) \n[GCC 4.2.3]"
|
647 |
+
|
648 |
+
The first word (up to the first space character) is the current
|
649 |
+
Python version; the first characters are the major and minor
|
650 |
+
version separated by a period. The returned string points into
|
651 |
+
static storage; the caller should not modify its value. The value
|
652 |
+
is available to Python code as "sys.version".
|
653 |
+
|
654 |
+
See also the "Py_Version" constant.
|
655 |
+
|
656 |
+
const char *Py_GetPlatform()
|
657 |
+
* Part of the Stable ABI.*
|
658 |
+
|
659 |
+
Return the platform identifier for the current platform. On Unix,
|
660 |
+
this is formed from the "official" name of the operating system,
|
661 |
+
converted to lower case, followed by the major revision number;
|
662 |
+
e.g., for Solaris 2.x, which is also known as SunOS 5.x, the value
|
663 |
+
is "'sunos5'". On macOS, it is "'darwin'". On Windows, it is
|
664 |
+
"'win'". The returned string points into static storage; the
|
665 |
+
caller should not modify its value. The value is available to
|
666 |
+
Python code as "sys.platform".
|
667 |
+
|
668 |
+
const char *Py_GetCopyright()
|
669 |
+
* Part of the Stable ABI.*
|
670 |
+
|
671 |
+
Return the official copyright string for the current Python
|
672 |
+
version, for example
|
673 |
+
|
674 |
+
"'Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam'"
|
675 |
+
|
676 |
+
The returned string points into static storage; the caller should
|
677 |
+
not modify its value. The value is available to Python code as
|
678 |
+
"sys.copyright".
|
679 |
+
|
680 |
+
const char *Py_GetCompiler()
|
681 |
+
* Part of the Stable ABI.*
|
682 |
+
|
683 |
+
Return an indication of the compiler used to build the current
|
684 |
+
Python version, in square brackets, for example:
|
685 |
+
|
686 |
+
"[GCC 2.7.2.2]"
|
687 |
+
|
688 |
+
The returned string points into static storage; the caller should
|
689 |
+
not modify its value. The value is available to Python code as
|
690 |
+
part of the variable "sys.version".
|
691 |
+
|
692 |
+
const char *Py_GetBuildInfo()
|
693 |
+
* Part of the Stable ABI.*
|
694 |
+
|
695 |
+
Return information about the sequence number and build date and
|
696 |
+
time of the current Python interpreter instance, for example
|
697 |
+
|
698 |
+
"#67, Aug 1 1997, 22:34:28"
|
699 |
+
|
700 |
+
The returned string points into static storage; the caller should
|
701 |
+
not modify its value. The value is available to Python code as
|
702 |
+
part of the variable "sys.version".
|
703 |
+
|
704 |
+
void PySys_SetArgvEx(int argc, wchar_t **argv, int updatepath)
|
705 |
+
* Part of the Stable ABI.*
|
706 |
+
|
707 |
+
This API is kept for backward compatibility: setting
|
708 |
+
"PyConfig.argv", "PyConfig.parse_argv" and "PyConfig.safe_path"
|
709 |
+
should be used instead, see Python Initialization Configuration.
|
710 |
+
|
711 |
+
Set "sys.argv" based on *argc* and *argv*. These parameters are
|
712 |
+
similar to those passed to the program's "main()" function with the
|
713 |
+
difference that the first entry should refer to the script file to
|
714 |
+
be executed rather than the executable hosting the Python
|
715 |
+
interpreter. If there isn't a script that will be run, the first
|
716 |
+
entry in *argv* can be an empty string. If this function fails to
|
717 |
+
initialize "sys.argv", a fatal condition is signalled using
|
718 |
+
"Py_FatalError()".
|
719 |
+
|
720 |
+
If *updatepath* is zero, this is all the function does. If
|
721 |
+
*updatepath* is non-zero, the function also modifies "sys.path"
|
722 |
+
according to the following algorithm:
|
723 |
+
|
724 |
+
* If the name of an existing script is passed in "argv[0]", the
|
725 |
+
absolute path of the directory where the script is located is
|
726 |
+
prepended to "sys.path".
|
727 |
+
|
728 |
+
* Otherwise (that is, if *argc* is "0" or "argv[0]" doesn't point
|
729 |
+
to an existing file name), an empty string is prepended to
|
730 |
+
"sys.path", which is the same as prepending the current working
|
731 |
+
directory (""."").
|
732 |
+
|
733 |
+
Use "Py_DecodeLocale()" to decode a bytes string to get a wchar_*
|
734 |
+
string.
|
735 |
+
|
736 |
+
See also "PyConfig.orig_argv" and "PyConfig.argv" members of the
|
737 |
+
Python Initialization Configuration.
|
738 |
+
|
739 |
+
Note:
|
740 |
+
|
741 |
+
It is recommended that applications embedding the Python
|
742 |
+
interpreter for purposes other than executing a single script
|
743 |
+
pass "0" as *updatepath*, and update "sys.path" themselves if
|
744 |
+
desired. See CVE-2008-5983.On versions before 3.1.3, you can
|
745 |
+
achieve the same effect by manually popping the first "sys.path"
|
746 |
+
element after having called "PySys_SetArgv()", for example using:
|
747 |
+
|
748 |
+
PyRun_SimpleString("import sys; sys.path.pop(0)\n");
|
749 |
+
|
750 |
+
Added in version 3.1.3.
|
751 |
+
|
752 |
+
Deprecated since version 3.11.
|
753 |
+
|
754 |
+
void PySys_SetArgv(int argc, wchar_t **argv)
|
755 |
+
* Part of the Stable ABI.*
|
756 |
+
|
757 |
+
This API is kept for backward compatibility: setting
|
758 |
+
"PyConfig.argv" and "PyConfig.parse_argv" should be used instead,
|
759 |
+
see Python Initialization Configuration.
|
760 |
+
|
761 |
+
This function works like "PySys_SetArgvEx()" with *updatepath* set
|
762 |
+
to "1" unless the **python** interpreter was started with the "-I".
|
763 |
+
|
764 |
+
Use "Py_DecodeLocale()" to decode a bytes string to get a wchar_*
|
765 |
+
string.
|
766 |
+
|
767 |
+
See also "PyConfig.orig_argv" and "PyConfig.argv" members of the
|
768 |
+
Python Initialization Configuration.
|
769 |
+
|
770 |
+
Changed in version 3.4: The *updatepath* value depends on "-I".
|
771 |
+
|
772 |
+
Deprecated since version 3.11.
|
773 |
+
|
774 |
+
void Py_SetPythonHome(const wchar_t *home)
|
775 |
+
* Part of the Stable ABI.*
|
776 |
+
|
777 |
+
This API is kept for backward compatibility: setting
|
778 |
+
"PyConfig.home" should be used instead, see Python Initialization
|
779 |
+
Configuration.
|
780 |
+
|
781 |
+
Set the default "home" directory, that is, the location of the
|
782 |
+
standard Python libraries. See "PYTHONHOME" for the meaning of the
|
783 |
+
argument string.
|
784 |
+
|
785 |
+
The argument should point to a zero-terminated character string in
|
786 |
+
static storage whose contents will not change for the duration of
|
787 |
+
the program's execution. No code in the Python interpreter will
|
788 |
+
change the contents of this storage.
|
789 |
+
|
790 |
+
Use "Py_DecodeLocale()" to decode a bytes string to get a wchar_*
|
791 |
+
string.
|
792 |
+
|
793 |
+
Deprecated since version 3.11.
|
794 |
+
|
795 |
+
wchar_t *Py_GetPythonHome()
|
796 |
+
* Part of the Stable ABI.*
|
797 |
+
|
798 |
+
Return the default "home", that is, the value set by a previous
|
799 |
+
call to "Py_SetPythonHome()", or the value of the "PYTHONHOME"
|
800 |
+
environment variable if it is set.
|
801 |
+
|
802 |
+
This function should not be called before "Py_Initialize()",
|
803 |
+
otherwise it returns "NULL".
|
804 |
+
|
805 |
+
Changed in version 3.10: It now returns "NULL" if called before
|
806 |
+
"Py_Initialize()".
|
807 |
+
|
808 |
+
|
809 |
+
Thread State and the Global Interpreter Lock
|
810 |
+
============================================
|
811 |
+
|
812 |
+
The Python interpreter is not fully thread-safe. In order to support
|
813 |
+
multi-threaded Python programs, there's a global lock, called the
|
814 |
+
*global interpreter lock* or *GIL*, that must be held by the current
|
815 |
+
thread before it can safely access Python objects. Without the lock,
|
816 |
+
even the simplest operations could cause problems in a multi-threaded
|
817 |
+
program: for example, when two threads simultaneously increment the
|
818 |
+
reference count of the same object, the reference count could end up
|
819 |
+
being incremented only once instead of twice.
|
820 |
+
|
821 |
+
Therefore, the rule exists that only the thread that has acquired the
|
822 |
+
*GIL* may operate on Python objects or call Python/C API functions. In
|
823 |
+
order to emulate concurrency of execution, the interpreter regularly
|
824 |
+
tries to switch threads (see "sys.setswitchinterval()"). The lock is
|
825 |
+
also released around potentially blocking I/O operations like reading
|
826 |
+
or writing a file, so that other Python threads can run in the
|
827 |
+
meantime.
|
828 |
+
|
829 |
+
The Python interpreter keeps some thread-specific bookkeeping
|
830 |
+
information inside a data structure called "PyThreadState". There's
|
831 |
+
also one global variable pointing to the current "PyThreadState": it
|
832 |
+
can be retrieved using "PyThreadState_Get()".
|
833 |
+
|
834 |
+
|
835 |
+
Releasing the GIL from extension code
|
836 |
+
-------------------------------------
|
837 |
+
|
838 |
+
Most extension code manipulating the *GIL* has the following simple
|
839 |
+
structure:
|
840 |
+
|
841 |
+
Save the thread state in a local variable.
|
842 |
+
Release the global interpreter lock.
|
843 |
+
... Do some blocking I/O operation ...
|
844 |
+
Reacquire the global interpreter lock.
|
845 |
+
Restore the thread state from the local variable.
|
846 |
+
|
847 |
+
This is so common that a pair of macros exists to simplify it:
|
848 |
+
|
849 |
+
Py_BEGIN_ALLOW_THREADS
|
850 |
+
... Do some blocking I/O operation ...
|
851 |
+
Py_END_ALLOW_THREADS
|
852 |
+
|
853 |
+
The "Py_BEGIN_ALLOW_THREADS" macro opens a new block and declares a
|
854 |
+
hidden local variable; the "Py_END_ALLOW_THREADS" macro closes the
|
855 |
+
block.
|
856 |
+
|
857 |
+
The block above expands to the following code:
|
858 |
+
|
859 |
+
PyThreadState *_save;
|
860 |
+
|
861 |
+
_save = PyEval_SaveThread();
|
862 |
+
... Do some blocking I/O operation ...
|
863 |
+
PyEval_RestoreThread(_save);
|
864 |
+
|
865 |
+
Here is how these functions work: the global interpreter lock is used
|
866 |
+
to protect the pointer to the current thread state. When releasing
|
867 |
+
the lock and saving the thread state, the current thread state pointer
|
868 |
+
must be retrieved before the lock is released (since another thread
|
869 |
+
could immediately acquire the lock and store its own thread state in
|
870 |
+
the global variable). Conversely, when acquiring the lock and
|
871 |
+
restoring the thread state, the lock must be acquired before storing
|
872 |
+
the thread state pointer.
|
873 |
+
|
874 |
+
Note:
|
875 |
+
|
876 |
+
Calling system I/O functions is the most common use case for
|
877 |
+
releasing the GIL, but it can also be useful before calling long-
|
878 |
+
running computations which don't need access to Python objects, such
|
879 |
+
as compression or cryptographic functions operating over memory
|
880 |
+
buffers. For example, the standard "zlib" and "hashlib" modules
|
881 |
+
release the GIL when compressing or hashing data.
|
882 |
+
|
883 |
+
|
884 |
+
Non-Python created threads
|
885 |
+
--------------------------
|
886 |
+
|
887 |
+
When threads are created using the dedicated Python APIs (such as the
|
888 |
+
"threading" module), a thread state is automatically associated to
|
889 |
+
them and the code showed above is therefore correct. However, when
|
890 |
+
threads are created from C (for example by a third-party library with
|
891 |
+
its own thread management), they don't hold the GIL, nor is there a
|
892 |
+
thread state structure for them.
|
893 |
+
|
894 |
+
If you need to call Python code from these threads (often this will be
|
895 |
+
part of a callback API provided by the aforementioned third-party
|
896 |
+
library), you must first register these threads with the interpreter
|
897 |
+
by creating a thread state data structure, then acquiring the GIL, and
|
898 |
+
finally storing their thread state pointer, before you can start using
|
899 |
+
the Python/C API. When you are done, you should reset the thread
|
900 |
+
state pointer, release the GIL, and finally free the thread state data
|
901 |
+
structure.
|
902 |
+
|
903 |
+
The "PyGILState_Ensure()" and "PyGILState_Release()" functions do all
|
904 |
+
of the above automatically. The typical idiom for calling into Python
|
905 |
+
from a C thread is:
|
906 |
+
|
907 |
+
PyGILState_STATE gstate;
|
908 |
+
gstate = PyGILState_Ensure();
|
909 |
+
|
910 |
+
/* Perform Python actions here. */
|
911 |
+
result = CallSomeFunction();
|
912 |
+
/* evaluate result or handle exception */
|
913 |
+
|
914 |
+
/* Release the thread. No Python API allowed beyond this point. */
|
915 |
+
PyGILState_Release(gstate);
|
916 |
+
|
917 |
+
Note that the "PyGILState_*" functions assume there is only one global
|
918 |
+
interpreter (created automatically by "Py_Initialize()"). Python
|
919 |
+
supports the creation of additional interpreters (using
|
920 |
+
"Py_NewInterpreter()"), but mixing multiple interpreters and the
|
921 |
+
"PyGILState_*" API is unsupported.
|
922 |
+
|
923 |
+
|
924 |
+
Cautions about fork()
|
925 |
+
---------------------
|
926 |
+
|
927 |
+
Another important thing to note about threads is their behaviour in
|
928 |
+
the face of the C "fork()" call. On most systems with "fork()", after
|
929 |
+
a process forks only the thread that issued the fork will exist. This
|
930 |
+
has a concrete impact both on how locks must be handled and on all
|
931 |
+
stored state in CPython's runtime.
|
932 |
+
|
933 |
+
The fact that only the "current" thread remains means any locks held
|
934 |
+
by other threads will never be released. Python solves this for
|
935 |
+
"os.fork()" by acquiring the locks it uses internally before the fork,
|
936 |
+
and releasing them afterwards. In addition, it resets any Lock Objects
|
937 |
+
in the child. When extending or embedding Python, there is no way to
|
938 |
+
inform Python of additional (non-Python) locks that need to be
|
939 |
+
acquired before or reset after a fork. OS facilities such as
|
940 |
+
"pthread_atfork()" would need to be used to accomplish the same thing.
|
941 |
+
Additionally, when extending or embedding Python, calling "fork()"
|
942 |
+
directly rather than through "os.fork()" (and returning to or calling
|
943 |
+
into Python) may result in a deadlock by one of Python's internal
|
944 |
+
locks being held by a thread that is defunct after the fork.
|
945 |
+
"PyOS_AfterFork_Child()" tries to reset the necessary locks, but is
|
946 |
+
not always able to.
|
947 |
+
|
948 |
+
The fact that all other threads go away also means that CPython's
|
949 |
+
runtime state there must be cleaned up properly, which "os.fork()"
|
950 |
+
does. This means finalizing all other "PyThreadState" objects
|
951 |
+
belonging to the current interpreter and all other
|
952 |
+
"PyInterpreterState" objects. Due to this and the special nature of
|
953 |
+
the "main" interpreter, "fork()" should only be called in that
|
954 |
+
interpreter's "main" thread, where the CPython global runtime was
|
955 |
+
originally initialized. The only exception is if "exec()" will be
|
956 |
+
called immediately after.
|
957 |
+
|
958 |
+
|
959 |
+
High-level API
|
960 |
+
--------------
|
961 |
+
|
962 |
+
These are the most commonly used types and functions when writing C
|
963 |
+
extension code, or when embedding the Python interpreter:
|
964 |
+
|
965 |
+
type PyInterpreterState
|
966 |
+
* Part of the Limited API (as an opaque struct).*
|
967 |
+
|
968 |
+
This data structure represents the state shared by a number of
|
969 |
+
cooperating threads. Threads belonging to the same interpreter
|
970 |
+
share their module administration and a few other internal items.
|
971 |
+
There are no public members in this structure.
|
972 |
+
|
973 |
+
Threads belonging to different interpreters initially share
|
974 |
+
nothing, except process state like available memory, open file
|
975 |
+
descriptors and such. The global interpreter lock is also shared
|
976 |
+
by all threads, regardless of to which interpreter they belong.
|
977 |
+
|
978 |
+
type PyThreadState
|
979 |
+
* Part of the Limited API (as an opaque struct).*
|
980 |
+
|
981 |
+
This data structure represents the state of a single thread. The
|
982 |
+
only public data member is:
|
983 |
+
|
984 |
+
PyInterpreterState *interp
|
985 |
+
|
986 |
+
This thread's interpreter state.
|
987 |
+
|
988 |
+
void PyEval_InitThreads()
|
989 |
+
* Part of the Stable ABI.*
|
990 |
+
|
991 |
+
Deprecated function which does nothing.
|
992 |
+
|
993 |
+
In Python 3.6 and older, this function created the GIL if it didn't
|
994 |
+
exist.
|
995 |
+
|
996 |
+
Changed in version 3.9: The function now does nothing.
|
997 |
+
|
998 |
+
Changed in version 3.7: This function is now called by
|
999 |
+
"Py_Initialize()", so you don't have to call it yourself anymore.
|
1000 |
+
|
1001 |
+
Changed in version 3.2: This function cannot be called before
|
1002 |
+
"Py_Initialize()" anymore.
|
1003 |
+
|
1004 |
+
Deprecated since version 3.9.
|
1005 |
+
|
1006 |
+
int PyEval_ThreadsInitialized()
|
1007 |
+
* Part of the Stable ABI.*
|
1008 |
+
|
1009 |
+
Returns a non-zero value if "PyEval_InitThreads()" has been called.
|
1010 |
+
This function can be called without holding the GIL, and therefore
|
1011 |
+
can be used to avoid calls to the locking API when running single-
|
1012 |
+
threaded.
|
1013 |
+
|
1014 |
+
Changed in version 3.7: The *GIL* is now initialized by
|
1015 |
+
"Py_Initialize()".
|
1016 |
+
|
1017 |
+
Deprecated since version 3.9.
|
1018 |
+
|
1019 |
+
PyThreadState *PyEval_SaveThread()
|
1020 |
+
* Part of the Stable ABI.*
|
1021 |
+
|
1022 |
+
Release the global interpreter lock (if it has been created) and
|
1023 |
+
reset the thread state to "NULL", returning the previous thread
|
1024 |
+
state (which is not "NULL"). If the lock has been created, the
|
1025 |
+
current thread must have acquired it.
|
1026 |
+
|
1027 |
+
void PyEval_RestoreThread(PyThreadState *tstate)
|
1028 |
+
* Part of the Stable ABI.*
|
1029 |
+
|
1030 |
+
Acquire the global interpreter lock (if it has been created) and
|
1031 |
+
set the thread state to *tstate*, which must not be "NULL". If the
|
1032 |
+
lock has been created, the current thread must not have acquired
|
1033 |
+
it, otherwise deadlock ensues.
|
1034 |
+
|
1035 |
+
Note:
|
1036 |
+
|
1037 |
+
Calling this function from a thread when the runtime is
|
1038 |
+
finalizing will terminate the thread, even if the thread was not
|
1039 |
+
created by Python. You can use "_Py_IsFinalizing()" or
|
1040 |
+
"sys.is_finalizing()" to check if the interpreter is in process
|
1041 |
+
of being finalized before calling this function to avoid unwanted
|
1042 |
+
termination.
|
1043 |
+
|
1044 |
+
PyThreadState *PyThreadState_Get()
|
1045 |
+
* Part of the Stable ABI.*
|
1046 |
+
|
1047 |
+
Return the current thread state. The global interpreter lock must
|
1048 |
+
be held. When the current thread state is "NULL", this issues a
|
1049 |
+
fatal error (so that the caller needn't check for "NULL").
|
1050 |
+
|
1051 |
+
PyThreadState *PyThreadState_Swap(PyThreadState *tstate)
|
1052 |
+
* Part of the Stable ABI.*
|
1053 |
+
|
1054 |
+
Swap the current thread state with the thread state given by the
|
1055 |
+
argument *tstate*, which may be "NULL". The global interpreter
|
1056 |
+
lock must be held and is not released.
|
1057 |
+
|
1058 |
+
The following functions use thread-local storage, and are not
|
1059 |
+
compatible with sub-interpreters:
|
1060 |
+
|
1061 |
+
PyGILState_STATE PyGILState_Ensure()
|
1062 |
+
* Part of the Stable ABI.*
|
1063 |
+
|
1064 |
+
Ensure that the current thread is ready to call the Python C API
|
1065 |
+
regardless of the current state of Python, or of the global
|
1066 |
+
interpreter lock. This may be called as many times as desired by a
|
1067 |
+
thread as long as each call is matched with a call to
|
1068 |
+
"PyGILState_Release()". In general, other thread-related APIs may
|
1069 |
+
be used between "PyGILState_Ensure()" and "PyGILState_Release()"
|
1070 |
+
calls as long as the thread state is restored to its previous state
|
1071 |
+
before the Release(). For example, normal usage of the
|
1072 |
+
"Py_BEGIN_ALLOW_THREADS" and "Py_END_ALLOW_THREADS" macros is
|
1073 |
+
acceptable.
|
1074 |
+
|
1075 |
+
The return value is an opaque "handle" to the thread state when
|
1076 |
+
"PyGILState_Ensure()" was called, and must be passed to
|
1077 |
+
"PyGILState_Release()" to ensure Python is left in the same state.
|
1078 |
+
Even though recursive calls are allowed, these handles *cannot* be
|
1079 |
+
shared - each unique call to "PyGILState_Ensure()" must save the
|
1080 |
+
handle for its call to "PyGILState_Release()".
|
1081 |
+
|
1082 |
+
When the function returns, the current thread will hold the GIL and
|
1083 |
+
be able to call arbitrary Python code. Failure is a fatal error.
|
1084 |
+
|
1085 |
+
Note:
|
1086 |
+
|
1087 |
+
Calling this function from a thread when the runtime is
|
1088 |
+
finalizing will terminate the thread, even if the thread was not
|
1089 |
+
created by Python. You can use "_Py_IsFinalizing()" or
|
1090 |
+
"sys.is_finalizing()" to check if the interpreter is in process
|
1091 |
+
of being finalized before calling this function to avoid unwanted
|
1092 |
+
termination.
|
1093 |
+
|
1094 |
+
void PyGILState_Release(PyGILState_STATE)
|
1095 |
+
* Part of the Stable ABI.*
|
1096 |
+
|
1097 |
+
Release any resources previously acquired. After this call,
|
1098 |
+
Python's state will be the same as it was prior to the
|
1099 |
+
corresponding "PyGILState_Ensure()" call (but generally this state
|
1100 |
+
will be unknown to the caller, hence the use of the GILState API).
|
1101 |
+
|
1102 |
+
Every call to "PyGILState_Ensure()" must be matched by a call to
|
1103 |
+
"PyGILState_Release()" on the same thread.
|
1104 |
+
|
1105 |
+
PyThreadState *PyGILState_GetThisThreadState()
|
1106 |
+
* Part of the Stable ABI.*
|
1107 |
+
|
1108 |
+
Get the current thread state for this thread. May return "NULL" if
|
1109 |
+
no GILState API has been used on the current thread. Note that the
|
1110 |
+
main thread always has such a thread-state, even if no auto-thread-
|
1111 |
+
state call has been made on the main thread. This is mainly a
|
1112 |
+
helper/diagnostic function.
|
1113 |
+
|
1114 |
+
int PyGILState_Check()
|
1115 |
+
|
1116 |
+
Return "1" if the current thread is holding the GIL and "0"
|
1117 |
+
otherwise. This function can be called from any thread at any time.
|
1118 |
+
Only if it has had its Python thread state initialized and
|
1119 |
+
currently is holding the GIL will it return "1". This is mainly a
|
1120 |
+
helper/diagnostic function. It can be useful for example in
|
1121 |
+
callback contexts or memory allocation functions when knowing that
|
1122 |
+
the GIL is locked can allow the caller to perform sensitive actions
|
1123 |
+
or otherwise behave differently.
|
1124 |
+
|
1125 |
+
Added in version 3.4.
|
1126 |
+
|
1127 |
+
The following macros are normally used without a trailing semicolon;
|
1128 |
+
look for example usage in the Python source distribution.
|
1129 |
+
|
1130 |
+
Py_BEGIN_ALLOW_THREADS
|
1131 |
+
* Part of the Stable ABI.*
|
1132 |
+
|
1133 |
+
This macro expands to "{ PyThreadState *_save; _save =
|
1134 |
+
PyEval_SaveThread();". Note that it contains an opening brace; it
|
1135 |
+
must be matched with a following "Py_END_ALLOW_THREADS" macro. See
|
1136 |
+
above for further discussion of this macro.
|
1137 |
+
|
1138 |
+
Py_END_ALLOW_THREADS
|
1139 |
+
* Part of the Stable ABI.*
|
1140 |
+
|
1141 |
+
This macro expands to "PyEval_RestoreThread(_save); }". Note that
|
1142 |
+
it contains a closing brace; it must be matched with an earlier
|
1143 |
+
"Py_BEGIN_ALLOW_THREADS" macro. See above for further discussion
|
1144 |
+
of this macro.
|
1145 |
+
|
1146 |
+
Py_BLOCK_THREADS
|
1147 |
+
* Part of the Stable ABI.*
|
1148 |
+
|
1149 |
+
This macro expands to "PyEval_RestoreThread(_save);": it is
|
1150 |
+
equivalent to "Py_END_ALLOW_THREADS" without the closing brace.
|
1151 |
+
|
1152 |
+
Py_UNBLOCK_THREADS
|
1153 |
+
* Part of the Stable ABI.*
|
1154 |
+
|
1155 |
+
This macro expands to "_save = PyEval_SaveThread();": it is
|
1156 |
+
equivalent to "Py_BEGIN_ALLOW_THREADS" without the opening brace
|
1157 |
+
and variable declaration.
|
1158 |
+
|
1159 |
+
|
1160 |
+
Low-level API
|
1161 |
+
-------------
|
1162 |
+
|
1163 |
+
All of the following functions must be called after "Py_Initialize()".
|
1164 |
+
|
1165 |
+
Changed in version 3.7: "Py_Initialize()" now initializes the *GIL*.
|
1166 |
+
|
1167 |
+
PyInterpreterState *PyInterpreterState_New()
|
1168 |
+
* Part of the Stable ABI.*
|
1169 |
+
|
1170 |
+
Create a new interpreter state object. The global interpreter lock
|
1171 |
+
need not be held, but may be held if it is necessary to serialize
|
1172 |
+
calls to this function.
|
1173 |
+
|
1174 |
+
Raises an auditing event "cpython.PyInterpreterState_New" with no
|
1175 |
+
arguments.
|
1176 |
+
|
1177 |
+
void PyInterpreterState_Clear(PyInterpreterState *interp)
|
1178 |
+
* Part of the Stable ABI.*
|
1179 |
+
|
1180 |
+
Reset all information in an interpreter state object. The global
|
1181 |
+
interpreter lock must be held.
|
1182 |
+
|
1183 |
+
Raises an auditing event "cpython.PyInterpreterState_Clear" with no
|
1184 |
+
arguments.
|
1185 |
+
|
1186 |
+
void PyInterpreterState_Delete(PyInterpreterState *interp)
|
1187 |
+
* Part of the Stable ABI.*
|
1188 |
+
|
1189 |
+
Destroy an interpreter state object. The global interpreter lock
|
1190 |
+
need not be held. The interpreter state must have been reset with
|
1191 |
+
a previous call to "PyInterpreterState_Clear()".
|
1192 |
+
|
1193 |
+
PyThreadState *PyThreadState_New(PyInterpreterState *interp)
|
1194 |
+
* Part of the Stable ABI.*
|
1195 |
+
|
1196 |
+
Create a new thread state object belonging to the given interpreter
|
1197 |
+
object. The global interpreter lock need not be held, but may be
|
1198 |
+
held if it is necessary to serialize calls to this function.
|
1199 |
+
|
1200 |
+
void PyThreadState_Clear(PyThreadState *tstate)
|
1201 |
+
* Part of the Stable ABI.*
|
1202 |
+
|
1203 |
+
Reset all information in a thread state object. The global
|
1204 |
+
interpreter lock must be held.
|
1205 |
+
|
1206 |
+
Changed in version 3.9: This function now calls the
|
1207 |
+
"PyThreadState.on_delete" callback. Previously, that happened in
|
1208 |
+
"PyThreadState_Delete()".
|
1209 |
+
|
1210 |
+
void PyThreadState_Delete(PyThreadState *tstate)
|
1211 |
+
* Part of the Stable ABI.*
|
1212 |
+
|
1213 |
+
Destroy a thread state object. The global interpreter lock need
|
1214 |
+
not be held. The thread state must have been reset with a previous
|
1215 |
+
call to "PyThreadState_Clear()".
|
1216 |
+
|
1217 |
+
void PyThreadState_DeleteCurrent(void)
|
1218 |
+
|
1219 |
+
Destroy the current thread state and release the global interpreter
|
1220 |
+
lock. Like "PyThreadState_Delete()", the global interpreter lock
|
1221 |
+
need not be held. The thread state must have been reset with a
|
1222 |
+
previous call to "PyThreadState_Clear()".
|
1223 |
+
|
1224 |
+
PyFrameObject *PyThreadState_GetFrame(PyThreadState *tstate)
|
1225 |
+
* Part of the Stable ABI since version 3.10.*
|
1226 |
+
|
1227 |
+
Get the current frame of the Python thread state *tstate*.
|
1228 |
+
|
1229 |
+
Return a *strong reference*. Return "NULL" if no frame is currently
|
1230 |
+
executing.
|
1231 |
+
|
1232 |
+
See also "PyEval_GetFrame()".
|
1233 |
+
|
1234 |
+
*tstate* must not be "NULL".
|
1235 |
+
|
1236 |
+
Added in version 3.9.
|
1237 |
+
|
1238 |
+
uint64_t PyThreadState_GetID(PyThreadState *tstate)
|
1239 |
+
* Part of the Stable ABI since version 3.10.*
|
1240 |
+
|
1241 |
+
Get the unique thread state identifier of the Python thread state
|
1242 |
+
*tstate*.
|
1243 |
+
|
1244 |
+
*tstate* must not be "NULL".
|
1245 |
+
|
1246 |
+
Added in version 3.9.
|
1247 |
+
|
1248 |
+
PyInterpreterState *PyThreadState_GetInterpreter(PyThreadState *tstate)
|
1249 |
+
* Part of the Stable ABI since version 3.10.*
|
1250 |
+
|
1251 |
+
Get the interpreter of the Python thread state *tstate*.
|
1252 |
+
|
1253 |
+
*tstate* must not be "NULL".
|
1254 |
+
|
1255 |
+
Added in version 3.9.
|
1256 |
+
|
1257 |
+
void PyThreadState_EnterTracing(PyThreadState *tstate)
|
1258 |
+
|
1259 |
+
Suspend tracing and profiling in the Python thread state *tstate*.
|
1260 |
+
|
1261 |
+
Resume them using the "PyThreadState_LeaveTracing()" function.
|
1262 |
+
|
1263 |
+
Added in version 3.11.
|
1264 |
+
|
1265 |
+
void PyThreadState_LeaveTracing(PyThreadState *tstate)
|
1266 |
+
|
1267 |
+
Resume tracing and profiling in the Python thread state *tstate*
|
1268 |
+
suspended by the "PyThreadState_EnterTracing()" function.
|
1269 |
+
|
1270 |
+
See also "PyEval_SetTrace()" and "PyEval_SetProfile()" functions.
|
1271 |
+
|
1272 |
+
Added in version 3.11.
|
1273 |
+
|
1274 |
+
PyInterpreterState *PyInterpreterState_Get(void)
|
1275 |
+
* Part of the Stable ABI since version 3.9.*
|
1276 |
+
|
1277 |
+
Get the current interpreter.
|
1278 |
+
|
1279 |
+
Issue a fatal error if there no current Python thread state or no
|
1280 |
+
current interpreter. It cannot return NULL.
|
1281 |
+
|
1282 |
+
The caller must hold the GIL.
|
1283 |
+
|
1284 |
+
Added in version 3.9.
|
1285 |
+
|
1286 |
+
int64_t PyInterpreterState_GetID(PyInterpreterState *interp)
|
1287 |
+
* Part of the Stable ABI since version 3.7.*
|
1288 |
+
|
1289 |
+
Return the interpreter's unique ID. If there was any error in
|
1290 |
+
doing so then "-1" is returned and an error is set.
|
1291 |
+
|
1292 |
+
The caller must hold the GIL.
|
1293 |
+
|
1294 |
+
Added in version 3.7.
|
1295 |
+
|
1296 |
+
PyObject *PyInterpreterState_GetDict(PyInterpreterState *interp)
|
1297 |
+
* Part of the Stable ABI since version 3.8.*
|
1298 |
+
|
1299 |
+
Return a dictionary in which interpreter-specific data may be
|
1300 |
+
stored. If this function returns "NULL" then no exception has been
|
1301 |
+
raised and the caller should assume no interpreter-specific dict is
|
1302 |
+
available.
|
1303 |
+
|
1304 |
+
This is not a replacement for "PyModule_GetState()", which
|
1305 |
+
extensions should use to store interpreter-specific state
|
1306 |
+
information.
|
1307 |
+
|
1308 |
+
Added in version 3.8.
|
1309 |
+
|
1310 |
+
typedef PyObject *(*_PyFrameEvalFunction)(PyThreadState *tstate, _PyInterpreterFrame *frame, int throwflag)
|
1311 |
+
|
1312 |
+
Type of a frame evaluation function.
|
1313 |
+
|
1314 |
+
The *throwflag* parameter is used by the "throw()" method of
|
1315 |
+
generators: if non-zero, handle the current exception.
|
1316 |
+
|
1317 |
+
Changed in version 3.9: The function now takes a *tstate*
|
1318 |
+
parameter.
|
1319 |
+
|
1320 |
+
Changed in version 3.11: The *frame* parameter changed from
|
1321 |
+
"PyFrameObject*" to "_PyInterpreterFrame*".
|
1322 |
+
|
1323 |
+
_PyFrameEvalFunction _PyInterpreterState_GetEvalFrameFunc(PyInterpreterState *interp)
|
1324 |
+
|
1325 |
+
Get the frame evaluation function.
|
1326 |
+
|
1327 |
+
See the **PEP 523** "Adding a frame evaluation API to CPython".
|
1328 |
+
|
1329 |
+
Added in version 3.9.
|
1330 |
+
|
1331 |
+
void _PyInterpreterState_SetEvalFrameFunc(PyInterpreterState *interp, _PyFrameEvalFunction eval_frame)
|
1332 |
+
|
1333 |
+
Set the frame evaluation function.
|
1334 |
+
|
1335 |
+
See the **PEP 523** "Adding a frame evaluation API to CPython".
|
1336 |
+
|
1337 |
+
Added in version 3.9.
|
1338 |
+
|
1339 |
+
PyObject *PyThreadState_GetDict()
|
1340 |
+
*Return value: Borrowed reference.** Part of the Stable ABI.*
|
1341 |
+
|
1342 |
+
Return a dictionary in which extensions can store thread-specific
|
1343 |
+
state information. Each extension should use a unique key to use
|
1344 |
+
to store state in the dictionary. It is okay to call this function
|
1345 |
+
when no current thread state is available. If this function returns
|
1346 |
+
"NULL", no exception has been raised and the caller should assume
|
1347 |
+
no current thread state is available.
|
1348 |
+
|
1349 |
+
int PyThreadState_SetAsyncExc(unsigned long id, PyObject *exc)
|
1350 |
+
* Part of the Stable ABI.*
|
1351 |
+
|
1352 |
+
Asynchronously raise an exception in a thread. The *id* argument is
|
1353 |
+
the thread id of the target thread; *exc* is the exception object
|
1354 |
+
to be raised. This function does not steal any references to *exc*.
|
1355 |
+
To prevent naive misuse, you must write your own C extension to
|
1356 |
+
call this. Must be called with the GIL held. Returns the number of
|
1357 |
+
thread states modified; this is normally one, but will be zero if
|
1358 |
+
the thread id isn't found. If *exc* is "NULL", the pending
|
1359 |
+
exception (if any) for the thread is cleared. This raises no
|
1360 |
+
exceptions.
|
1361 |
+
|
1362 |
+
Changed in version 3.7: The type of the *id* parameter changed from
|
1363 |
+
long to unsigned long.
|
1364 |
+
|
1365 |
+
void PyEval_AcquireThread(PyThreadState *tstate)
|
1366 |
+
* Part of the Stable ABI.*
|
1367 |
+
|
1368 |
+
Acquire the global interpreter lock and set the current thread
|
1369 |
+
state to *tstate*, which must not be "NULL". The lock must have
|
1370 |
+
been created earlier. If this thread already has the lock, deadlock
|
1371 |
+
ensues.
|
1372 |
+
|
1373 |
+
Note:
|
1374 |
+
|
1375 |
+
Calling this function from a thread when the runtime is
|
1376 |
+
finalizing will terminate the thread, even if the thread was not
|
1377 |
+
created by Python. You can use "_Py_IsFinalizing()" or
|
1378 |
+
"sys.is_finalizing()" to check if the interpreter is in process
|
1379 |
+
of being finalized before calling this function to avoid unwanted
|
1380 |
+
termination.
|
1381 |
+
|
1382 |
+
Changed in version 3.8: Updated to be consistent with
|
1383 |
+
"PyEval_RestoreThread()", "Py_END_ALLOW_THREADS()", and
|
1384 |
+
"PyGILState_Ensure()", and terminate the current thread if called
|
1385 |
+
while the interpreter is finalizing.
|
1386 |
+
|
1387 |
+
"PyEval_RestoreThread()" is a higher-level function which is always
|
1388 |
+
available (even when threads have not been initialized).
|
1389 |
+
|
1390 |
+
void PyEval_ReleaseThread(PyThreadState *tstate)
|
1391 |
+
* Part of the Stable ABI.*
|
1392 |
+
|
1393 |
+
Reset the current thread state to "NULL" and release the global
|
1394 |
+
interpreter lock. The lock must have been created earlier and must
|
1395 |
+
be held by the current thread. The *tstate* argument, which must
|
1396 |
+
not be "NULL", is only used to check that it represents the current
|
1397 |
+
thread state --- if it isn't, a fatal error is reported.
|
1398 |
+
|
1399 |
+
"PyEval_SaveThread()" is a higher-level function which is always
|
1400 |
+
available (even when threads have not been initialized).
|
1401 |
+
|
1402 |
+
void PyEval_AcquireLock()
|
1403 |
+
* Part of the Stable ABI.*
|
1404 |
+
|
1405 |
+
Acquire the global interpreter lock. The lock must have been
|
1406 |
+
created earlier. If this thread already has the lock, a deadlock
|
1407 |
+
ensues.
|
1408 |
+
|
1409 |
+
Deprecated since version 3.2: This function does not update the
|
1410 |
+
current thread state. Please use "PyEval_RestoreThread()" or
|
1411 |
+
"PyEval_AcquireThread()" instead.
|
1412 |
+
|
1413 |
+
Note:
|
1414 |
+
|
1415 |
+
Calling this function from a thread when the runtime is
|
1416 |
+
finalizing will terminate the thread, even if the thread was not
|
1417 |
+
created by Python. You can use "_Py_IsFinalizing()" or
|
1418 |
+
"sys.is_finalizing()" to check if the interpreter is in process
|
1419 |
+
of being finalized before calling this function to avoid unwanted
|
1420 |
+
termination.
|
1421 |
+
|
1422 |
+
Changed in version 3.8: Updated to be consistent with
|
1423 |
+
"PyEval_RestoreThread()", "Py_END_ALLOW_THREADS()", and
|
1424 |
+
"PyGILState_Ensure()", and terminate the current thread if called
|
1425 |
+
while the interpreter is finalizing.
|
1426 |
+
|
1427 |
+
void PyEval_ReleaseLock()
|
1428 |
+
* Part of the Stable ABI.*
|
1429 |
+
|
1430 |
+
Release the global interpreter lock. The lock must have been
|
1431 |
+
created earlier.
|
1432 |
+
|
1433 |
+
Deprecated since version 3.2: This function does not update the
|
1434 |
+
current thread state. Please use "PyEval_SaveThread()" or
|
1435 |
+
"PyEval_ReleaseThread()" instead.
|
1436 |
+
|
1437 |
+
|
1438 |
+
Sub-interpreter support
|
1439 |
+
=======================
|
1440 |
+
|
1441 |
+
While in most uses, you will only embed a single Python interpreter,
|
1442 |
+
there are cases where you need to create several independent
|
1443 |
+
interpreters in the same process and perhaps even in the same thread.
|
1444 |
+
Sub-interpreters allow you to do that.
|
1445 |
+
|
1446 |
+
The "main" interpreter is the first one created when the runtime
|
1447 |
+
initializes. It is usually the only Python interpreter in a process.
|
1448 |
+
Unlike sub-interpreters, the main interpreter has unique process-
|
1449 |
+
global responsibilities like signal handling. It is also responsible
|
1450 |
+
for execution during runtime initialization and is usually the active
|
1451 |
+
interpreter during runtime finalization. The
|
1452 |
+
"PyInterpreterState_Main()" function returns a pointer to its state.
|
1453 |
+
|
1454 |
+
You can switch between sub-interpreters using the
|
1455 |
+
"PyThreadState_Swap()" function. You can create and destroy them using
|
1456 |
+
the following functions:
|
1457 |
+
|
1458 |
+
type PyInterpreterConfig
|
1459 |
+
|
1460 |
+
Structure containing most parameters to configure a sub-
|
1461 |
+
interpreter. Its values are used only in
|
1462 |
+
"Py_NewInterpreterFromConfig()" and never modified by the runtime.
|
1463 |
+
|
1464 |
+
Added in version 3.12.
|
1465 |
+
|
1466 |
+
Structure fields:
|
1467 |
+
|
1468 |
+
int use_main_obmalloc
|
1469 |
+
|
1470 |
+
If this is "0" then the sub-interpreter will use its own
|
1471 |
+
"object" allocator state. Otherwise it will use (share) the main
|
1472 |
+
interpreter's.
|
1473 |
+
|
1474 |
+
If this is "0" then "check_multi_interp_extensions" must be "1"
|
1475 |
+
(non-zero). If this is "1" then "gil" must not be
|
1476 |
+
"PyInterpreterConfig_OWN_GIL".
|
1477 |
+
|
1478 |
+
int allow_fork
|
1479 |
+
|
1480 |
+
If this is "0" then the runtime will not support forking the
|
1481 |
+
process in any thread where the sub-interpreter is currently
|
1482 |
+
active. Otherwise fork is unrestricted.
|
1483 |
+
|
1484 |
+
Note that the "subprocess" module still works when fork is
|
1485 |
+
disallowed.
|
1486 |
+
|
1487 |
+
int allow_exec
|
1488 |
+
|
1489 |
+
If this is "0" then the runtime will not support replacing the
|
1490 |
+
current process via exec (e.g. "os.execv()") in any thread where
|
1491 |
+
the sub-interpreter is currently active. Otherwise exec is
|
1492 |
+
unrestricted.
|
1493 |
+
|
1494 |
+
Note that the "subprocess" module still works when exec is
|
1495 |
+
disallowed.
|
1496 |
+
|
1497 |
+
int allow_threads
|
1498 |
+
|
1499 |
+
If this is "0" then the sub-interpreter's "threading" module
|
1500 |
+
won't create threads. Otherwise threads are allowed.
|
1501 |
+
|
1502 |
+
int allow_daemon_threads
|
1503 |
+
|
1504 |
+
If this is "0" then the sub-interpreter's "threading" module
|
1505 |
+
won't create daemon threads. Otherwise daemon threads are
|
1506 |
+
allowed (as long as "allow_threads" is non-zero).
|
1507 |
+
|
1508 |
+
int check_multi_interp_extensions
|
1509 |
+
|
1510 |
+
If this is "0" then all extension modules may be imported,
|
1511 |
+
including legacy (single-phase init) modules, in any thread
|
1512 |
+
where the sub-interpreter is currently active. Otherwise only
|
1513 |
+
multi-phase init extension modules (see **PEP 489**) may be
|
1514 |
+
imported. (Also see "Py_mod_multiple_interpreters".)
|
1515 |
+
|
1516 |
+
This must be "1" (non-zero) if "use_main_obmalloc" is "0".
|
1517 |
+
|
1518 |
+
int gil
|
1519 |
+
|
1520 |
+
This determines the operation of the GIL for the sub-
|
1521 |
+
interpreter. It may be one of the following:
|
1522 |
+
|
1523 |
+
PyInterpreterConfig_DEFAULT_GIL
|
1524 |
+
|
1525 |
+
Use the default selection ("PyInterpreterConfig_SHARED_GIL").
|
1526 |
+
|
1527 |
+
PyInterpreterConfig_SHARED_GIL
|
1528 |
+
|
1529 |
+
Use (share) the main interpreter's GIL.
|
1530 |
+
|
1531 |
+
PyInterpreterConfig_OWN_GIL
|
1532 |
+
|
1533 |
+
Use the sub-interpreter's own GIL.
|
1534 |
+
|
1535 |
+
If this is "PyInterpreterConfig_OWN_GIL" then
|
1536 |
+
"PyInterpreterConfig.use_main_obmalloc" must be "0".
|
1537 |
+
|
1538 |
+
PyStatus Py_NewInterpreterFromConfig(PyThreadState **tstate_p, const PyInterpreterConfig *config)
|
1539 |
+
|
1540 |
+
Create a new sub-interpreter. This is an (almost) totally separate
|
1541 |
+
environment for the execution of Python code. In particular, the
|
1542 |
+
new interpreter has separate, independent versions of all imported
|
1543 |
+
modules, including the fundamental modules "builtins", "__main__"
|
1544 |
+
and "sys". The table of loaded modules ("sys.modules") and the
|
1545 |
+
module search path ("sys.path") are also separate. The new
|
1546 |
+
environment has no "sys.argv" variable. It has new standard I/O
|
1547 |
+
stream file objects "sys.stdin", "sys.stdout" and "sys.stderr"
|
1548 |
+
(however these refer to the same underlying file descriptors).
|
1549 |
+
|
1550 |
+
The given *config* controls the options with which the interpreter
|
1551 |
+
is initialized.
|
1552 |
+
|
1553 |
+
Upon success, *tstate_p* will be set to the first thread state
|
1554 |
+
created in the new sub-interpreter. This thread state is made in
|
1555 |
+
the current thread state. Note that no actual thread is created;
|
1556 |
+
see the discussion of thread states below. If creation of the new
|
1557 |
+
interpreter is unsuccessful, *tstate_p* is set to "NULL"; no
|
1558 |
+
exception is set since the exception state is stored in the current
|
1559 |
+
thread state and there may not be a current thread state.
|
1560 |
+
|
1561 |
+
Like all other Python/C API functions, the global interpreter lock
|
1562 |
+
must be held before calling this function and is still held when it
|
1563 |
+
returns. Likewise a current thread state must be set on entry. On
|
1564 |
+
success, the returned thread state will be set as current. If the
|
1565 |
+
sub-interpreter is created with its own GIL then the GIL of the
|
1566 |
+
calling interpreter will be released. When the function returns,
|
1567 |
+
the new interpreter's GIL will be held by the current thread and
|
1568 |
+
the previously interpreter's GIL will remain released here.
|
1569 |
+
|
1570 |
+
Added in version 3.12.
|
1571 |
+
|
1572 |
+
Sub-interpreters are most effective when isolated from each other,
|
1573 |
+
with certain functionality restricted:
|
1574 |
+
|
1575 |
+
PyInterpreterConfig config = {
|
1576 |
+
.use_main_obmalloc = 0,
|
1577 |
+
.allow_fork = 0,
|
1578 |
+
.allow_exec = 0,
|
1579 |
+
.allow_threads = 1,
|
1580 |
+
.allow_daemon_threads = 0,
|
1581 |
+
.check_multi_interp_extensions = 1,
|
1582 |
+
.gil = PyInterpreterConfig_OWN_GIL,
|
1583 |
+
};
|
1584 |
+
PyThreadState *tstate = Py_NewInterpreterFromConfig(&config);
|
1585 |
+
|
1586 |
+
Note that the config is used only briefly and does not get
|
1587 |
+
modified. During initialization the config's values are converted
|
1588 |
+
into various "PyInterpreterState" values. A read-only copy of the
|
1589 |
+
config may be stored internally on the "PyInterpreterState".
|
1590 |
+
|
1591 |
+
Extension modules are shared between (sub-)interpreters as follows:
|
1592 |
+
|
1593 |
+
* For modules using multi-phase initialization, e.g.
|
1594 |
+
"PyModule_FromDefAndSpec()", a separate module object is created
|
1595 |
+
and initialized for each interpreter. Only C-level static and
|
1596 |
+
global variables are shared between these module objects.
|
1597 |
+
|
1598 |
+
* For modules using single-phase initialization, e.g.
|
1599 |
+
"PyModule_Create()", the first time a particular extension is
|
1600 |
+
imported, it is initialized normally, and a (shallow) copy of its
|
1601 |
+
module's dictionary is squirreled away. When the same extension
|
1602 |
+
is imported by another (sub-)interpreter, a new module is
|
1603 |
+
initialized and filled with the contents of this copy; the
|
1604 |
+
extension's "init" function is not called. Objects in the
|
1605 |
+
module's dictionary thus end up shared across (sub-)interpreters,
|
1606 |
+
which might cause unwanted behavior (see Bugs and caveats below).
|
1607 |
+
|
1608 |
+
Note that this is different from what happens when an extension
|
1609 |
+
is imported after the interpreter has been completely re-
|
1610 |
+
initialized by calling "Py_FinalizeEx()" and "Py_Initialize()";
|
1611 |
+
in that case, the extension's "initmodule" function *is* called
|
1612 |
+
again. As with multi-phase initialization, this means that only
|
1613 |
+
C-level static and global variables are shared between these
|
1614 |
+
modules.
|
1615 |
+
|
1616 |
+
PyThreadState *Py_NewInterpreter(void)
|
1617 |
+
* Part of the Stable ABI.*
|
1618 |
+
|
1619 |
+
Create a new sub-interpreter. This is essentially just a wrapper
|
1620 |
+
around "Py_NewInterpreterFromConfig()" with a config that preserves
|
1621 |
+
the existing behavior. The result is an unisolated sub-interpreter
|
1622 |
+
that shares the main interpreter's GIL, allows fork/exec, allows
|
1623 |
+
daemon threads, and allows single-phase init modules.
|
1624 |
+
|
1625 |
+
void Py_EndInterpreter(PyThreadState *tstate)
|
1626 |
+
* Part of the Stable ABI.*
|
1627 |
+
|
1628 |
+
Destroy the (sub-)interpreter represented by the given thread
|
1629 |
+
state. The given thread state must be the current thread state.
|
1630 |
+
See the discussion of thread states below. When the call returns,
|
1631 |
+
the current thread state is "NULL". All thread states associated
|
1632 |
+
with this interpreter are destroyed. The global interpreter lock
|
1633 |
+
used by the target interpreter must be held before calling this
|
1634 |
+
function. No GIL is held when it returns.
|
1635 |
+
|
1636 |
+
"Py_FinalizeEx()" will destroy all sub-interpreters that haven't
|
1637 |
+
been explicitly destroyed at that point.
|
1638 |
+
|
1639 |
+
|
1640 |
+
A Per-Interpreter GIL
|
1641 |
+
---------------------
|
1642 |
+
|
1643 |
+
Using "Py_NewInterpreterFromConfig()" you can create a sub-interpreter
|
1644 |
+
that is completely isolated from other interpreters, including having
|
1645 |
+
its own GIL. The most important benefit of this isolation is that
|
1646 |
+
such an interpreter can execute Python code without being blocked by
|
1647 |
+
other interpreters or blocking any others. Thus a single Python
|
1648 |
+
process can truly take advantage of multiple CPU cores when running
|
1649 |
+
Python code. The isolation also encourages a different approach to
|
1650 |
+
concurrency than that of just using threads. (See **PEP 554**.)
|
1651 |
+
|
1652 |
+
Using an isolated interpreter requires vigilance in preserving that
|
1653 |
+
isolation. That especially means not sharing any objects or mutable
|
1654 |
+
state without guarantees about thread-safety. Even objects that are
|
1655 |
+
otherwise immutable (e.g. "None", "(1, 5)") can't normally be shared
|
1656 |
+
because of the refcount. One simple but less-efficient approach
|
1657 |
+
around this is to use a global lock around all use of some state (or
|
1658 |
+
object). Alternately, effectively immutable objects (like integers or
|
1659 |
+
strings) can be made safe in spite of their refcounts by making them
|
1660 |
+
"immortal". In fact, this has been done for the builtin singletons,
|
1661 |
+
small integers, and a number of other builtin objects.
|
1662 |
+
|
1663 |
+
If you preserve isolation then you will have access to proper multi-
|
1664 |
+
core computing without the complications that come with free-
|
1665 |
+
threading. Failure to preserve isolation will expose you to the full
|
1666 |
+
consequences of free-threading, including races and hard-to-debug
|
1667 |
+
crashes.
|
1668 |
+
|
1669 |
+
Aside from that, one of the main challenges of using multiple isolated
|
1670 |
+
interpreters is how to communicate between them safely (not break
|
1671 |
+
isolation) and efficiently. The runtime and stdlib do not provide any
|
1672 |
+
standard approach to this yet. A future stdlib module would help
|
1673 |
+
mitigate the effort of preserving isolation and expose effective tools
|
1674 |
+
for communicating (and sharing) data between interpreters.
|
1675 |
+
|
1676 |
+
Added in version 3.12.
|
1677 |
+
|
1678 |
+
|
1679 |
+
Bugs and caveats
|
1680 |
+
----------------
|
1681 |
+
|
1682 |
+
Because sub-interpreters (and the main interpreter) are part of the
|
1683 |
+
same process, the insulation between them isn't perfect --- for
|
1684 |
+
example, using low-level file operations like "os.close()" they can
|
1685 |
+
(accidentally or maliciously) affect each other's open files. Because
|
1686 |
+
of the way extensions are shared between (sub-)interpreters, some
|
1687 |
+
extensions may not work properly; this is especially likely when using
|
1688 |
+
single-phase initialization or (static) global variables. It is
|
1689 |
+
possible to insert objects created in one sub-interpreter into a
|
1690 |
+
namespace of another (sub-)interpreter; this should be avoided if
|
1691 |
+
possible.
|
1692 |
+
|
1693 |
+
Special care should be taken to avoid sharing user-defined functions,
|
1694 |
+
methods, instances or classes between sub-interpreters, since import
|
1695 |
+
operations executed by such objects may affect the wrong
|
1696 |
+
(sub-)interpreter's dictionary of loaded modules. It is equally
|
1697 |
+
important to avoid sharing objects from which the above are reachable.
|
1698 |
+
|
1699 |
+
Also note that combining this functionality with "PyGILState_*" APIs
|
1700 |
+
is delicate, because these APIs assume a bijection between Python
|
1701 |
+
thread states and OS-level threads, an assumption broken by the
|
1702 |
+
presence of sub-interpreters. It is highly recommended that you don't
|
1703 |
+
switch sub-interpreters between a pair of matching
|
1704 |
+
"PyGILState_Ensure()" and "PyGILState_Release()" calls. Furthermore,
|
1705 |
+
extensions (such as "ctypes") using these APIs to allow calling of
|
1706 |
+
Python code from non-Python created threads will probably be broken
|
1707 |
+
when using sub-interpreters.
|
1708 |
+
|
1709 |
+
|
1710 |
+
Asynchronous Notifications
|
1711 |
+
==========================
|
1712 |
+
|
1713 |
+
A mechanism is provided to make asynchronous notifications to the main
|
1714 |
+
interpreter thread. These notifications take the form of a function
|
1715 |
+
pointer and a void pointer argument.
|
1716 |
+
|
1717 |
+
int Py_AddPendingCall(int (*func)(void*), void *arg)
|
1718 |
+
* Part of the Stable ABI.*
|
1719 |
+
|
1720 |
+
Schedule a function to be called from the main interpreter thread.
|
1721 |
+
On success, "0" is returned and *func* is queued for being called
|
1722 |
+
in the main thread. On failure, "-1" is returned without setting
|
1723 |
+
any exception.
|
1724 |
+
|
1725 |
+
When successfully queued, *func* will be *eventually* called from
|
1726 |
+
the main interpreter thread with the argument *arg*. It will be
|
1727 |
+
called asynchronously with respect to normally running Python code,
|
1728 |
+
but with both these conditions met:
|
1729 |
+
|
1730 |
+
* on a *bytecode* boundary;
|
1731 |
+
|
1732 |
+
* with the main thread holding the *global interpreter lock*
|
1733 |
+
(*func* can therefore use the full C API).
|
1734 |
+
|
1735 |
+
*func* must return "0" on success, or "-1" on failure with an
|
1736 |
+
exception set. *func* won't be interrupted to perform another
|
1737 |
+
asynchronous notification recursively, but it can still be
|
1738 |
+
interrupted to switch threads if the global interpreter lock is
|
1739 |
+
released.
|
1740 |
+
|
1741 |
+
This function doesn't need a current thread state to run, and it
|
1742 |
+
doesn't need the global interpreter lock.
|
1743 |
+
|
1744 |
+
To call this function in a subinterpreter, the caller must hold the
|
1745 |
+
GIL. Otherwise, the function *func* can be scheduled to be called
|
1746 |
+
from the wrong interpreter.
|
1747 |
+
|
1748 |
+
Warning:
|
1749 |
+
|
1750 |
+
This is a low-level function, only useful for very special cases.
|
1751 |
+
There is no guarantee that *func* will be called as quick as
|
1752 |
+
possible. If the main thread is busy executing a system call,
|
1753 |
+
*func* won't be called before the system call returns. This
|
1754 |
+
function is generally **not** suitable for calling Python code
|
1755 |
+
from arbitrary C threads. Instead, use the PyGILState API.
|
1756 |
+
|
1757 |
+
Added in version 3.1.
|
1758 |
+
|
1759 |
+
Changed in version 3.9: If this function is called in a
|
1760 |
+
subinterpreter, the function *func* is now scheduled to be called
|
1761 |
+
from the subinterpreter, rather than being called from the main
|
1762 |
+
interpreter. Each subinterpreter now has its own list of scheduled
|
1763 |
+
calls.
|
1764 |
+
|
1765 |
+
|
1766 |
+
Profiling and Tracing
|
1767 |
+
=====================
|
1768 |
+
|
1769 |
+
The Python interpreter provides some low-level support for attaching
|
1770 |
+
profiling and execution tracing facilities. These are used for
|
1771 |
+
profiling, debugging, and coverage analysis tools.
|
1772 |
+
|
1773 |
+
This C interface allows the profiling or tracing code to avoid the
|
1774 |
+
overhead of calling through Python-level callable objects, making a
|
1775 |
+
direct C function call instead. The essential attributes of the
|
1776 |
+
facility have not changed; the interface allows trace functions to be
|
1777 |
+
installed per-thread, and the basic events reported to the trace
|
1778 |
+
function are the same as had been reported to the Python-level trace
|
1779 |
+
functions in previous versions.
|
1780 |
+
|
1781 |
+
typedef int (*Py_tracefunc)(PyObject *obj, PyFrameObject *frame, int what, PyObject *arg)
|
1782 |
+
|
1783 |
+
The type of the trace function registered using
|
1784 |
+
"PyEval_SetProfile()" and "PyEval_SetTrace()". The first parameter
|
1785 |
+
is the object passed to the registration function as *obj*, *frame*
|
1786 |
+
is the frame object to which the event pertains, *what* is one of
|
1787 |
+
the constants "PyTrace_CALL", "PyTrace_EXCEPTION", "PyTrace_LINE",
|
1788 |
+
"PyTrace_RETURN", "PyTrace_C_CALL", "PyTrace_C_EXCEPTION",
|
1789 |
+
"PyTrace_C_RETURN", or "PyTrace_OPCODE", and *arg* depends on the
|
1790 |
+
value of *what*:
|
1791 |
+
|
1792 |
+
+---------------------------------+------------------------------------------+
|
1793 |
+
| Value of *what* | Meaning of *arg* |
|
1794 |
+
|=================================|==========================================|
|
1795 |
+
| "PyTrace_CALL" | Always "Py_None". |
|
1796 |
+
+---------------------------------+------------------------------------------+
|
1797 |
+
| "PyTrace_EXCEPTION" | Exception information as returned by |
|
1798 |
+
| | "sys.exc_info()". |
|
1799 |
+
+---------------------------------+------------------------------------------+
|
1800 |
+
| "PyTrace_LINE" | Always "Py_None". |
|
1801 |
+
+---------------------------------+------------------------------------------+
|
1802 |
+
| "PyTrace_RETURN" | Value being returned to the caller, or |
|
1803 |
+
| | "NULL" if caused by an exception. |
|
1804 |
+
+---------------------------------+------------------------------------------+
|
1805 |
+
| "PyTrace_C_CALL" | Function object being called. |
|
1806 |
+
+---------------------------------+------------------------------------------+
|
1807 |
+
| "PyTrace_C_EXCEPTION" | Function object being called. |
|
1808 |
+
+---------------------------------+------------------------------------------+
|
1809 |
+
| "PyTrace_C_RETURN" | Function object being called. |
|
1810 |
+
+---------------------------------+------------------------------------------+
|
1811 |
+
| "PyTrace_OPCODE" | Always "Py_None". |
|
1812 |
+
+---------------------------------+------------------------------------------+
|
1813 |
+
|
1814 |
+
int PyTrace_CALL
|
1815 |
+
|
1816 |
+
The value of the *what* parameter to a "Py_tracefunc" function when
|
1817 |
+
a new call to a function or method is being reported, or a new
|
1818 |
+
entry into a generator. Note that the creation of the iterator for
|
1819 |
+
a generator function is not reported as there is no control
|
1820 |
+
transfer to the Python bytecode in the corresponding frame.
|
1821 |
+
|
1822 |
+
int PyTrace_EXCEPTION
|
1823 |
+
|
1824 |
+
The value of the *what* parameter to a "Py_tracefunc" function when
|
1825 |
+
an exception has been raised. The callback function is called with
|
1826 |
+
this value for *what* when after any bytecode is processed after
|
1827 |
+
which the exception becomes set within the frame being executed.
|
1828 |
+
The effect of this is that as exception propagation causes the
|
1829 |
+
Python stack to unwind, the callback is called upon return to each
|
1830 |
+
frame as the exception propagates. Only trace functions receives
|
1831 |
+
these events; they are not needed by the profiler.
|
1832 |
+
|
1833 |
+
int PyTrace_LINE
|
1834 |
+
|
1835 |
+
The value passed as the *what* parameter to a "Py_tracefunc"
|
1836 |
+
function (but not a profiling function) when a line-number event is
|
1837 |
+
being reported. It may be disabled for a frame by setting
|
1838 |
+
"f_trace_lines" to *0* on that frame.
|
1839 |
+
|
1840 |
+
int PyTrace_RETURN
|
1841 |
+
|
1842 |
+
The value for the *what* parameter to "Py_tracefunc" functions when
|
1843 |
+
a call is about to return.
|
1844 |
+
|
1845 |
+
int PyTrace_C_CALL
|
1846 |
+
|
1847 |
+
The value for the *what* parameter to "Py_tracefunc" functions when
|
1848 |
+
a C function is about to be called.
|
1849 |
+
|
1850 |
+
int PyTrace_C_EXCEPTION
|
1851 |
+
|
1852 |
+
The value for the *what* parameter to "Py_tracefunc" functions when
|
1853 |
+
a C function has raised an exception.
|
1854 |
+
|
1855 |
+
int PyTrace_C_RETURN
|
1856 |
+
|
1857 |
+
The value for the *what* parameter to "Py_tracefunc" functions when
|
1858 |
+
a C function has returned.
|
1859 |
+
|
1860 |
+
int PyTrace_OPCODE
|
1861 |
+
|
1862 |
+
The value for the *what* parameter to "Py_tracefunc" functions (but
|
1863 |
+
not profiling functions) when a new opcode is about to be executed.
|
1864 |
+
This event is not emitted by default: it must be explicitly
|
1865 |
+
requested by setting "f_trace_opcodes" to *1* on the frame.
|
1866 |
+
|
1867 |
+
void PyEval_SetProfile(Py_tracefunc func, PyObject *obj)
|
1868 |
+
|
1869 |
+
Set the profiler function to *func*. The *obj* parameter is passed
|
1870 |
+
to the function as its first parameter, and may be any Python
|
1871 |
+
object, or "NULL". If the profile function needs to maintain
|
1872 |
+
state, using a different value for *obj* for each thread provides a
|
1873 |
+
convenient and thread-safe place to store it. The profile function
|
1874 |
+
is called for all monitored events except "PyTrace_LINE"
|
1875 |
+
"PyTrace_OPCODE" and "PyTrace_EXCEPTION".
|
1876 |
+
|
1877 |
+
See also the "sys.setprofile()" function.
|
1878 |
+
|
1879 |
+
The caller must hold the *GIL*.
|
1880 |
+
|
1881 |
+
void PyEval_SetProfileAllThreads(Py_tracefunc func, PyObject *obj)
|
1882 |
+
|
1883 |
+
Like "PyEval_SetProfile()" but sets the profile function in all
|
1884 |
+
running threads belonging to the current interpreter instead of the
|
1885 |
+
setting it only on the current thread.
|
1886 |
+
|
1887 |
+
The caller must hold the *GIL*.
|
1888 |
+
|
1889 |
+
As "PyEval_SetProfile()", this function ignores any exceptions
|
1890 |
+
raised while setting the profile functions in all threads.
|
1891 |
+
|
1892 |
+
Added in version 3.12.
|
1893 |
+
|
1894 |
+
void PyEval_SetTrace(Py_tracefunc func, PyObject *obj)
|
1895 |
+
|
1896 |
+
Set the tracing function to *func*. This is similar to
|
1897 |
+
"PyEval_SetProfile()", except the tracing function does receive
|
1898 |
+
line-number events and per-opcode events, but does not receive any
|
1899 |
+
event related to C function objects being called. Any trace
|
1900 |
+
function registered using "PyEval_SetTrace()" will not receive
|
1901 |
+
"PyTrace_C_CALL", "PyTrace_C_EXCEPTION" or "PyTrace_C_RETURN" as a
|
1902 |
+
value for the *what* parameter.
|
1903 |
+
|
1904 |
+
See also the "sys.settrace()" function.
|
1905 |
+
|
1906 |
+
The caller must hold the *GIL*.
|
1907 |
+
|
1908 |
+
void PyEval_SetTraceAllThreads(Py_tracefunc func, PyObject *obj)
|
1909 |
+
|
1910 |
+
Like "PyEval_SetTrace()" but sets the tracing function in all
|
1911 |
+
running threads belonging to the current interpreter instead of the
|
1912 |
+
setting it only on the current thread.
|
1913 |
+
|
1914 |
+
The caller must hold the *GIL*.
|
1915 |
+
|
1916 |
+
As "PyEval_SetTrace()", this function ignores any exceptions raised
|
1917 |
+
while setting the trace functions in all threads.
|
1918 |
+
|
1919 |
+
Added in version 3.12.
|
1920 |
+
|
1921 |
+
|
1922 |
+
Advanced Debugger Support
|
1923 |
+
=========================
|
1924 |
+
|
1925 |
+
These functions are only intended to be used by advanced debugging
|
1926 |
+
tools.
|
1927 |
+
|
1928 |
+
PyInterpreterState *PyInterpreterState_Head()
|
1929 |
+
|
1930 |
+
Return the interpreter state object at the head of the list of all
|
1931 |
+
such objects.
|
1932 |
+
|
1933 |
+
PyInterpreterState *PyInterpreterState_Main()
|
1934 |
+
|
1935 |
+
Return the main interpreter state object.
|
1936 |
+
|
1937 |
+
PyInterpreterState *PyInterpreterState_Next(PyInterpreterState *interp)
|
1938 |
+
|
1939 |
+
Return the next interpreter state object after *interp* from the
|
1940 |
+
list of all such objects.
|
1941 |
+
|
1942 |
+
PyThreadState *PyInterpreterState_ThreadHead(PyInterpreterState *interp)
|
1943 |
+
|
1944 |
+
Return the pointer to the first "PyThreadState" object in the list
|
1945 |
+
of threads associated with the interpreter *interp*.
|
1946 |
+
|
1947 |
+
PyThreadState *PyThreadState_Next(PyThreadState *tstate)
|
1948 |
+
|
1949 |
+
Return the next thread state object after *tstate* from the list of
|
1950 |
+
all such objects belonging to the same "PyInterpreterState" object.
|
1951 |
+
|
1952 |
+
|
1953 |
+
Thread Local Storage Support
|
1954 |
+
============================
|
1955 |
+
|
1956 |
+
The Python interpreter provides low-level support for thread-local
|
1957 |
+
storage (TLS) which wraps the underlying native TLS implementation to
|
1958 |
+
support the Python-level thread local storage API ("threading.local").
|
1959 |
+
The CPython C level APIs are similar to those offered by pthreads and
|
1960 |
+
Windows: use a thread key and functions to associate a void* value per
|
1961 |
+
thread.
|
1962 |
+
|
1963 |
+
The GIL does *not* need to be held when calling these functions; they
|
1964 |
+
supply their own locking.
|
1965 |
+
|
1966 |
+
Note that "Python.h" does not include the declaration of the TLS APIs,
|
1967 |
+
you need to include "pythread.h" to use thread-local storage.
|
1968 |
+
|
1969 |
+
Note:
|
1970 |
+
|
1971 |
+
None of these API functions handle memory management on behalf of
|
1972 |
+
the void* values. You need to allocate and deallocate them
|
1973 |
+
yourself. If the void* values happen to be PyObject*, these
|
1974 |
+
functions don't do refcount operations on them either.
|
1975 |
+
|
1976 |
+
|
1977 |
+
Thread Specific Storage (TSS) API
|
1978 |
+
---------------------------------
|
1979 |
+
|
1980 |
+
TSS API is introduced to supersede the use of the existing TLS API
|
1981 |
+
within the CPython interpreter. This API uses a new type "Py_tss_t"
|
1982 |
+
instead of int to represent thread keys.
|
1983 |
+
|
1984 |
+
Added in version 3.7.
|
1985 |
+
|
1986 |
+
See also:
|
1987 |
+
|
1988 |
+
"A New C-API for Thread-Local Storage in CPython" (**PEP 539**)
|
1989 |
+
|
1990 |
+
type Py_tss_t
|
1991 |
+
|
1992 |
+
This data structure represents the state of a thread key, the
|
1993 |
+
definition of which may depend on the underlying TLS
|
1994 |
+
implementation, and it has an internal field representing the key's
|
1995 |
+
initialization state. There are no public members in this
|
1996 |
+
structure.
|
1997 |
+
|
1998 |
+
When Py_LIMITED_API is not defined, static allocation of this type
|
1999 |
+
by "Py_tss_NEEDS_INIT" is allowed.
|
2000 |
+
|
2001 |
+
Py_tss_NEEDS_INIT
|
2002 |
+
|
2003 |
+
This macro expands to the initializer for "Py_tss_t" variables.
|
2004 |
+
Note that this macro won't be defined with Py_LIMITED_API.
|
2005 |
+
|
2006 |
+
|
2007 |
+
Dynamic Allocation
|
2008 |
+
~~~~~~~~~~~~~~~~~~
|
2009 |
+
|
2010 |
+
Dynamic allocation of the "Py_tss_t", required in extension modules
|
2011 |
+
built with Py_LIMITED_API, where static allocation of this type is not
|
2012 |
+
possible due to its implementation being opaque at build time.
|
2013 |
+
|
2014 |
+
Py_tss_t *PyThread_tss_alloc()
|
2015 |
+
* Part of the Stable ABI since version 3.7.*
|
2016 |
+
|
2017 |
+
Return a value which is the same state as a value initialized with
|
2018 |
+
"Py_tss_NEEDS_INIT", or "NULL" in the case of dynamic allocation
|
2019 |
+
failure.
|
2020 |
+
|
2021 |
+
void PyThread_tss_free(Py_tss_t *key)
|
2022 |
+
* Part of the Stable ABI since version 3.7.*
|
2023 |
+
|
2024 |
+
Free the given *key* allocated by "PyThread_tss_alloc()", after
|
2025 |
+
first calling "PyThread_tss_delete()" to ensure any associated
|
2026 |
+
thread locals have been unassigned. This is a no-op if the *key*
|
2027 |
+
argument is "NULL".
|
2028 |
+
|
2029 |
+
Note:
|
2030 |
+
|
2031 |
+
A freed key becomes a dangling pointer. You should reset the key
|
2032 |
+
to "NULL".
|
2033 |
+
|
2034 |
+
|
2035 |
+
Methods
|
2036 |
+
~~~~~~~
|
2037 |
+
|
2038 |
+
The parameter *key* of these functions must not be "NULL". Moreover,
|
2039 |
+
the behaviors of "PyThread_tss_set()" and "PyThread_tss_get()" are
|
2040 |
+
undefined if the given "Py_tss_t" has not been initialized by
|
2041 |
+
"PyThread_tss_create()".
|
2042 |
+
|
2043 |
+
int PyThread_tss_is_created(Py_tss_t *key)
|
2044 |
+
* Part of the Stable ABI since version 3.7.*
|
2045 |
+
|
2046 |
+
Return a non-zero value if the given "Py_tss_t" has been
|
2047 |
+
initialized by "PyThread_tss_create()".
|
2048 |
+
|
2049 |
+
int PyThread_tss_create(Py_tss_t *key)
|
2050 |
+
* Part of the Stable ABI since version 3.7.*
|
2051 |
+
|
2052 |
+
Return a zero value on successful initialization of a TSS key. The
|
2053 |
+
behavior is undefined if the value pointed to by the *key* argument
|
2054 |
+
is not initialized by "Py_tss_NEEDS_INIT". This function can be
|
2055 |
+
called repeatedly on the same key -- calling it on an already
|
2056 |
+
initialized key is a no-op and immediately returns success.
|
2057 |
+
|
2058 |
+
void PyThread_tss_delete(Py_tss_t *key)
|
2059 |
+
* Part of the Stable ABI since version 3.7.*
|
2060 |
+
|
2061 |
+
Destroy a TSS key to forget the values associated with the key
|
2062 |
+
across all threads, and change the key's initialization state to
|
2063 |
+
uninitialized. A destroyed key is able to be initialized again by
|
2064 |
+
"PyThread_tss_create()". This function can be called repeatedly on
|
2065 |
+
the same key -- calling it on an already destroyed key is a no-op.
|
2066 |
+
|
2067 |
+
int PyThread_tss_set(Py_tss_t *key, void *value)
|
2068 |
+
* Part of the Stable ABI since version 3.7.*
|
2069 |
+
|
2070 |
+
Return a zero value to indicate successfully associating a void*
|
2071 |
+
value with a TSS key in the current thread. Each thread has a
|
2072 |
+
distinct mapping of the key to a void* value.
|
2073 |
+
|
2074 |
+
void *PyThread_tss_get(Py_tss_t *key)
|
2075 |
+
* Part of the Stable ABI since version 3.7.*
|
2076 |
+
|
2077 |
+
Return the void* value associated with a TSS key in the current
|
2078 |
+
thread. This returns "NULL" if no value is associated with the key
|
2079 |
+
in the current thread.
|
2080 |
+
|
2081 |
+
|
2082 |
+
Thread Local Storage (TLS) API
|
2083 |
+
------------------------------
|
2084 |
+
|
2085 |
+
Deprecated since version 3.7: This API is superseded by Thread
|
2086 |
+
Specific Storage (TSS) API.
|
2087 |
+
|
2088 |
+
Note:
|
2089 |
+
|
2090 |
+
This version of the API does not support platforms where the native
|
2091 |
+
TLS key is defined in a way that cannot be safely cast to "int". On
|
2092 |
+
such platforms, "PyThread_create_key()" will return immediately with
|
2093 |
+
a failure status, and the other TLS functions will all be no-ops on
|
2094 |
+
such platforms.
|
2095 |
+
|
2096 |
+
Due to the compatibility problem noted above, this version of the API
|
2097 |
+
should not be used in new code.
|
2098 |
+
|
2099 |
+
int PyThread_create_key()
|
2100 |
+
* Part of the Stable ABI.*
|
2101 |
+
|
2102 |
+
void PyThread_delete_key(int key)
|
2103 |
+
* Part of the Stable ABI.*
|
2104 |
+
|
2105 |
+
int PyThread_set_key_value(int key, void *value)
|
2106 |
+
* Part of the Stable ABI.*
|
2107 |
+
|
2108 |
+
void *PyThread_get_key_value(int key)
|
2109 |
+
* Part of the Stable ABI.*
|
2110 |
+
|
2111 |
+
void PyThread_delete_key_value(int key)
|
2112 |
+
* Part of the Stable ABI.*
|
2113 |
+
|
2114 |
+
void PyThread_ReInitTLS()
|
2115 |
+
* Part of the Stable ABI.*
|
python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/init_config.txt
ADDED
@@ -0,0 +1,1652 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Python Initialization Configuration
|
2 |
+
***********************************
|
3 |
+
|
4 |
+
Added in version 3.8.
|
5 |
+
|
6 |
+
Python can be initialized with "Py_InitializeFromConfig()" and the
|
7 |
+
"PyConfig" structure. It can be preinitialized with
|
8 |
+
"Py_PreInitialize()" and the "PyPreConfig" structure.
|
9 |
+
|
10 |
+
There are two kinds of configuration:
|
11 |
+
|
12 |
+
* The Python Configuration can be used to build a customized Python
|
13 |
+
which behaves as the regular Python. For example, environment
|
14 |
+
variables and command line arguments are used to configure Python.
|
15 |
+
|
16 |
+
* The Isolated Configuration can be used to embed Python into an
|
17 |
+
application. It isolates Python from the system. For example,
|
18 |
+
environment variables are ignored, the LC_CTYPE locale is left
|
19 |
+
unchanged and no signal handler is registered.
|
20 |
+
|
21 |
+
The "Py_RunMain()" function can be used to write a customized Python
|
22 |
+
program.
|
23 |
+
|
24 |
+
See also Initialization, Finalization, and Threads.
|
25 |
+
|
26 |
+
See also: **PEP 587** "Python Initialization Configuration".
|
27 |
+
|
28 |
+
|
29 |
+
Example
|
30 |
+
=======
|
31 |
+
|
32 |
+
Example of customized Python always running in isolated mode:
|
33 |
+
|
34 |
+
int main(int argc, char **argv)
|
35 |
+
{
|
36 |
+
PyStatus status;
|
37 |
+
|
38 |
+
PyConfig config;
|
39 |
+
PyConfig_InitPythonConfig(&config);
|
40 |
+
config.isolated = 1;
|
41 |
+
|
42 |
+
/* Decode command line arguments.
|
43 |
+
Implicitly preinitialize Python (in isolated mode). */
|
44 |
+
status = PyConfig_SetBytesArgv(&config, argc, argv);
|
45 |
+
if (PyStatus_Exception(status)) {
|
46 |
+
goto exception;
|
47 |
+
}
|
48 |
+
|
49 |
+
status = Py_InitializeFromConfig(&config);
|
50 |
+
if (PyStatus_Exception(status)) {
|
51 |
+
goto exception;
|
52 |
+
}
|
53 |
+
PyConfig_Clear(&config);
|
54 |
+
|
55 |
+
return Py_RunMain();
|
56 |
+
|
57 |
+
exception:
|
58 |
+
PyConfig_Clear(&config);
|
59 |
+
if (PyStatus_IsExit(status)) {
|
60 |
+
return status.exitcode;
|
61 |
+
}
|
62 |
+
/* Display the error message and exit the process with
|
63 |
+
non-zero exit code */
|
64 |
+
Py_ExitStatusException(status);
|
65 |
+
}
|
66 |
+
|
67 |
+
|
68 |
+
PyWideStringList
|
69 |
+
================
|
70 |
+
|
71 |
+
type PyWideStringList
|
72 |
+
|
73 |
+
List of "wchar_t*" strings.
|
74 |
+
|
75 |
+
If *length* is non-zero, *items* must be non-"NULL" and all strings
|
76 |
+
must be non-"NULL".
|
77 |
+
|
78 |
+
Methods:
|
79 |
+
|
80 |
+
PyStatus PyWideStringList_Append(PyWideStringList *list, const wchar_t *item)
|
81 |
+
|
82 |
+
Append *item* to *list*.
|
83 |
+
|
84 |
+
Python must be preinitialized to call this function.
|
85 |
+
|
86 |
+
PyStatus PyWideStringList_Insert(PyWideStringList *list, Py_ssize_t index, const wchar_t *item)
|
87 |
+
|
88 |
+
Insert *item* into *list* at *index*.
|
89 |
+
|
90 |
+
If *index* is greater than or equal to *list* length, append
|
91 |
+
*item* to *list*.
|
92 |
+
|
93 |
+
*index* must be greater than or equal to "0".
|
94 |
+
|
95 |
+
Python must be preinitialized to call this function.
|
96 |
+
|
97 |
+
Structure fields:
|
98 |
+
|
99 |
+
Py_ssize_t length
|
100 |
+
|
101 |
+
List length.
|
102 |
+
|
103 |
+
wchar_t **items
|
104 |
+
|
105 |
+
List items.
|
106 |
+
|
107 |
+
|
108 |
+
PyStatus
|
109 |
+
========
|
110 |
+
|
111 |
+
type PyStatus
|
112 |
+
|
113 |
+
Structure to store an initialization function status: success,
|
114 |
+
error or exit.
|
115 |
+
|
116 |
+
For an error, it can store the C function name which created the
|
117 |
+
error.
|
118 |
+
|
119 |
+
Structure fields:
|
120 |
+
|
121 |
+
int exitcode
|
122 |
+
|
123 |
+
Exit code. Argument passed to "exit()".
|
124 |
+
|
125 |
+
const char *err_msg
|
126 |
+
|
127 |
+
Error message.
|
128 |
+
|
129 |
+
const char *func
|
130 |
+
|
131 |
+
Name of the function which created an error, can be "NULL".
|
132 |
+
|
133 |
+
Functions to create a status:
|
134 |
+
|
135 |
+
PyStatus PyStatus_Ok(void)
|
136 |
+
|
137 |
+
Success.
|
138 |
+
|
139 |
+
PyStatus PyStatus_Error(const char *err_msg)
|
140 |
+
|
141 |
+
Initialization error with a message.
|
142 |
+
|
143 |
+
*err_msg* must not be "NULL".
|
144 |
+
|
145 |
+
PyStatus PyStatus_NoMemory(void)
|
146 |
+
|
147 |
+
Memory allocation failure (out of memory).
|
148 |
+
|
149 |
+
PyStatus PyStatus_Exit(int exitcode)
|
150 |
+
|
151 |
+
Exit Python with the specified exit code.
|
152 |
+
|
153 |
+
Functions to handle a status:
|
154 |
+
|
155 |
+
int PyStatus_Exception(PyStatus status)
|
156 |
+
|
157 |
+
Is the status an error or an exit? If true, the exception must
|
158 |
+
be handled; by calling "Py_ExitStatusException()" for example.
|
159 |
+
|
160 |
+
int PyStatus_IsError(PyStatus status)
|
161 |
+
|
162 |
+
Is the result an error?
|
163 |
+
|
164 |
+
int PyStatus_IsExit(PyStatus status)
|
165 |
+
|
166 |
+
Is the result an exit?
|
167 |
+
|
168 |
+
void Py_ExitStatusException(PyStatus status)
|
169 |
+
|
170 |
+
Call "exit(exitcode)" if *status* is an exit. Print the error
|
171 |
+
message and exit with a non-zero exit code if *status* is an
|
172 |
+
error. Must only be called if "PyStatus_Exception(status)" is
|
173 |
+
non-zero.
|
174 |
+
|
175 |
+
Note:
|
176 |
+
|
177 |
+
Internally, Python uses macros which set "PyStatus.func", whereas
|
178 |
+
functions to create a status set "func" to "NULL".
|
179 |
+
|
180 |
+
Example:
|
181 |
+
|
182 |
+
PyStatus alloc(void **ptr, size_t size)
|
183 |
+
{
|
184 |
+
*ptr = PyMem_RawMalloc(size);
|
185 |
+
if (*ptr == NULL) {
|
186 |
+
return PyStatus_NoMemory();
|
187 |
+
}
|
188 |
+
return PyStatus_Ok();
|
189 |
+
}
|
190 |
+
|
191 |
+
int main(int argc, char **argv)
|
192 |
+
{
|
193 |
+
void *ptr;
|
194 |
+
PyStatus status = alloc(&ptr, 16);
|
195 |
+
if (PyStatus_Exception(status)) {
|
196 |
+
Py_ExitStatusException(status);
|
197 |
+
}
|
198 |
+
PyMem_Free(ptr);
|
199 |
+
return 0;
|
200 |
+
}
|
201 |
+
|
202 |
+
|
203 |
+
PyPreConfig
|
204 |
+
===========
|
205 |
+
|
206 |
+
type PyPreConfig
|
207 |
+
|
208 |
+
Structure used to preinitialize Python.
|
209 |
+
|
210 |
+
Function to initialize a preconfiguration:
|
211 |
+
|
212 |
+
void PyPreConfig_InitPythonConfig(PyPreConfig *preconfig)
|
213 |
+
|
214 |
+
Initialize the preconfiguration with Python Configuration.
|
215 |
+
|
216 |
+
void PyPreConfig_InitIsolatedConfig(PyPreConfig *preconfig)
|
217 |
+
|
218 |
+
Initialize the preconfiguration with Isolated Configuration.
|
219 |
+
|
220 |
+
Structure fields:
|
221 |
+
|
222 |
+
int allocator
|
223 |
+
|
224 |
+
Name of the Python memory allocators:
|
225 |
+
|
226 |
+
* "PYMEM_ALLOCATOR_NOT_SET" ("0"): don't change memory
|
227 |
+
allocators (use defaults).
|
228 |
+
|
229 |
+
* "PYMEM_ALLOCATOR_DEFAULT" ("1"): default memory allocators.
|
230 |
+
|
231 |
+
* "PYMEM_ALLOCATOR_DEBUG" ("2"): default memory allocators with
|
232 |
+
debug hooks.
|
233 |
+
|
234 |
+
* "PYMEM_ALLOCATOR_MALLOC" ("3"): use "malloc()" of the C
|
235 |
+
library.
|
236 |
+
|
237 |
+
* "PYMEM_ALLOCATOR_MALLOC_DEBUG" ("4"): force usage of
|
238 |
+
"malloc()" with debug hooks.
|
239 |
+
|
240 |
+
* "PYMEM_ALLOCATOR_PYMALLOC" ("5"): Python pymalloc memory
|
241 |
+
allocator.
|
242 |
+
|
243 |
+
* "PYMEM_ALLOCATOR_PYMALLOC_DEBUG" ("6"): Python pymalloc memory
|
244 |
+
allocator with debug hooks.
|
245 |
+
|
246 |
+
"PYMEM_ALLOCATOR_PYMALLOC" and "PYMEM_ALLOCATOR_PYMALLOC_DEBUG"
|
247 |
+
are not supported if Python is "configured using --without-
|
248 |
+
pymalloc".
|
249 |
+
|
250 |
+
See Memory Management.
|
251 |
+
|
252 |
+
Default: "PYMEM_ALLOCATOR_NOT_SET".
|
253 |
+
|
254 |
+
int configure_locale
|
255 |
+
|
256 |
+
Set the LC_CTYPE locale to the user preferred locale.
|
257 |
+
|
258 |
+
If equals to "0", set "coerce_c_locale" and
|
259 |
+
"coerce_c_locale_warn" members to "0".
|
260 |
+
|
261 |
+
See the *locale encoding*.
|
262 |
+
|
263 |
+
Default: "1" in Python config, "0" in isolated config.
|
264 |
+
|
265 |
+
int coerce_c_locale
|
266 |
+
|
267 |
+
If equals to "2", coerce the C locale.
|
268 |
+
|
269 |
+
If equals to "1", read the LC_CTYPE locale to decide if it
|
270 |
+
should be coerced.
|
271 |
+
|
272 |
+
See the *locale encoding*.
|
273 |
+
|
274 |
+
Default: "-1" in Python config, "0" in isolated config.
|
275 |
+
|
276 |
+
int coerce_c_locale_warn
|
277 |
+
|
278 |
+
If non-zero, emit a warning if the C locale is coerced.
|
279 |
+
|
280 |
+
Default: "-1" in Python config, "0" in isolated config.
|
281 |
+
|
282 |
+
int dev_mode
|
283 |
+
|
284 |
+
Python Development Mode: see "PyConfig.dev_mode".
|
285 |
+
|
286 |
+
Default: "-1" in Python mode, "0" in isolated mode.
|
287 |
+
|
288 |
+
int isolated
|
289 |
+
|
290 |
+
Isolated mode: see "PyConfig.isolated".
|
291 |
+
|
292 |
+
Default: "0" in Python mode, "1" in isolated mode.
|
293 |
+
|
294 |
+
int legacy_windows_fs_encoding
|
295 |
+
|
296 |
+
If non-zero:
|
297 |
+
|
298 |
+
* Set "PyPreConfig.utf8_mode" to "0",
|
299 |
+
|
300 |
+
* Set "PyConfig.filesystem_encoding" to ""mbcs"",
|
301 |
+
|
302 |
+
* Set "PyConfig.filesystem_errors" to ""replace"".
|
303 |
+
|
304 |
+
Initialized from the "PYTHONLEGACYWINDOWSFSENCODING" environment
|
305 |
+
variable value.
|
306 |
+
|
307 |
+
Only available on Windows. "#ifdef MS_WINDOWS" macro can be used
|
308 |
+
for Windows specific code.
|
309 |
+
|
310 |
+
Default: "0".
|
311 |
+
|
312 |
+
int parse_argv
|
313 |
+
|
314 |
+
If non-zero, "Py_PreInitializeFromArgs()" and
|
315 |
+
"Py_PreInitializeFromBytesArgs()" parse their "argv" argument
|
316 |
+
the same way the regular Python parses command line arguments:
|
317 |
+
see Command Line Arguments.
|
318 |
+
|
319 |
+
Default: "1" in Python config, "0" in isolated config.
|
320 |
+
|
321 |
+
int use_environment
|
322 |
+
|
323 |
+
Use environment variables? See "PyConfig.use_environment".
|
324 |
+
|
325 |
+
Default: "1" in Python config and "0" in isolated config.
|
326 |
+
|
327 |
+
int utf8_mode
|
328 |
+
|
329 |
+
If non-zero, enable the Python UTF-8 Mode.
|
330 |
+
|
331 |
+
Set to "0" or "1" by the "-X utf8" command line option and the
|
332 |
+
"PYTHONUTF8" environment variable.
|
333 |
+
|
334 |
+
Also set to "1" if the "LC_CTYPE" locale is "C" or "POSIX".
|
335 |
+
|
336 |
+
Default: "-1" in Python config and "0" in isolated config.
|
337 |
+
|
338 |
+
|
339 |
+
Preinitialize Python with PyPreConfig
|
340 |
+
=====================================
|
341 |
+
|
342 |
+
The preinitialization of Python:
|
343 |
+
|
344 |
+
* Set the Python memory allocators ("PyPreConfig.allocator")
|
345 |
+
|
346 |
+
* Configure the LC_CTYPE locale (*locale encoding*)
|
347 |
+
|
348 |
+
* Set the Python UTF-8 Mode ("PyPreConfig.utf8_mode")
|
349 |
+
|
350 |
+
The current preconfiguration ("PyPreConfig" type) is stored in
|
351 |
+
"_PyRuntime.preconfig".
|
352 |
+
|
353 |
+
Functions to preinitialize Python:
|
354 |
+
|
355 |
+
PyStatus Py_PreInitialize(const PyPreConfig *preconfig)
|
356 |
+
|
357 |
+
Preinitialize Python from *preconfig* preconfiguration.
|
358 |
+
|
359 |
+
*preconfig* must not be "NULL".
|
360 |
+
|
361 |
+
PyStatus Py_PreInitializeFromBytesArgs(const PyPreConfig *preconfig, int argc, char *const *argv)
|
362 |
+
|
363 |
+
Preinitialize Python from *preconfig* preconfiguration.
|
364 |
+
|
365 |
+
Parse *argv* command line arguments (bytes strings) if "parse_argv"
|
366 |
+
of *preconfig* is non-zero.
|
367 |
+
|
368 |
+
*preconfig* must not be "NULL".
|
369 |
+
|
370 |
+
PyStatus Py_PreInitializeFromArgs(const PyPreConfig *preconfig, int argc, wchar_t *const *argv)
|
371 |
+
|
372 |
+
Preinitialize Python from *preconfig* preconfiguration.
|
373 |
+
|
374 |
+
Parse *argv* command line arguments (wide strings) if "parse_argv"
|
375 |
+
of *preconfig* is non-zero.
|
376 |
+
|
377 |
+
*preconfig* must not be "NULL".
|
378 |
+
|
379 |
+
The caller is responsible to handle exceptions (error or exit) using
|
380 |
+
"PyStatus_Exception()" and "Py_ExitStatusException()".
|
381 |
+
|
382 |
+
For Python Configuration ("PyPreConfig_InitPythonConfig()"), if Python
|
383 |
+
is initialized with command line arguments, the command line arguments
|
384 |
+
must also be passed to preinitialize Python, since they have an effect
|
385 |
+
on the pre-configuration like encodings. For example, the "-X utf8"
|
386 |
+
command line option enables the Python UTF-8 Mode.
|
387 |
+
|
388 |
+
"PyMem_SetAllocator()" can be called after "Py_PreInitialize()" and
|
389 |
+
before "Py_InitializeFromConfig()" to install a custom memory
|
390 |
+
allocator. It can be called before "Py_PreInitialize()" if
|
391 |
+
"PyPreConfig.allocator" is set to "PYMEM_ALLOCATOR_NOT_SET".
|
392 |
+
|
393 |
+
Python memory allocation functions like "PyMem_RawMalloc()" must not
|
394 |
+
be used before the Python preinitialization, whereas calling directly
|
395 |
+
"malloc()" and "free()" is always safe. "Py_DecodeLocale()" must not
|
396 |
+
be called before the Python preinitialization.
|
397 |
+
|
398 |
+
Example using the preinitialization to enable the Python UTF-8 Mode:
|
399 |
+
|
400 |
+
PyStatus status;
|
401 |
+
PyPreConfig preconfig;
|
402 |
+
PyPreConfig_InitPythonConfig(&preconfig);
|
403 |
+
|
404 |
+
preconfig.utf8_mode = 1;
|
405 |
+
|
406 |
+
status = Py_PreInitialize(&preconfig);
|
407 |
+
if (PyStatus_Exception(status)) {
|
408 |
+
Py_ExitStatusException(status);
|
409 |
+
}
|
410 |
+
|
411 |
+
/* at this point, Python speaks UTF-8 */
|
412 |
+
|
413 |
+
Py_Initialize();
|
414 |
+
/* ... use Python API here ... */
|
415 |
+
Py_Finalize();
|
416 |
+
|
417 |
+
|
418 |
+
PyConfig
|
419 |
+
========
|
420 |
+
|
421 |
+
type PyConfig
|
422 |
+
|
423 |
+
Structure containing most parameters to configure Python.
|
424 |
+
|
425 |
+
When done, the "PyConfig_Clear()" function must be used to release
|
426 |
+
the configuration memory.
|
427 |
+
|
428 |
+
Structure methods:
|
429 |
+
|
430 |
+
void PyConfig_InitPythonConfig(PyConfig *config)
|
431 |
+
|
432 |
+
Initialize configuration with the Python Configuration.
|
433 |
+
|
434 |
+
void PyConfig_InitIsolatedConfig(PyConfig *config)
|
435 |
+
|
436 |
+
Initialize configuration with the Isolated Configuration.
|
437 |
+
|
438 |
+
PyStatus PyConfig_SetString(PyConfig *config, wchar_t *const *config_str, const wchar_t *str)
|
439 |
+
|
440 |
+
Copy the wide character string *str* into "*config_str".
|
441 |
+
|
442 |
+
Preinitialize Python if needed.
|
443 |
+
|
444 |
+
PyStatus PyConfig_SetBytesString(PyConfig *config, wchar_t *const *config_str, const char *str)
|
445 |
+
|
446 |
+
Decode *str* using "Py_DecodeLocale()" and set the result into
|
447 |
+
"*config_str".
|
448 |
+
|
449 |
+
Preinitialize Python if needed.
|
450 |
+
|
451 |
+
PyStatus PyConfig_SetArgv(PyConfig *config, int argc, wchar_t *const *argv)
|
452 |
+
|
453 |
+
Set command line arguments ("argv" member of *config*) from the
|
454 |
+
*argv* list of wide character strings.
|
455 |
+
|
456 |
+
Preinitialize Python if needed.
|
457 |
+
|
458 |
+
PyStatus PyConfig_SetBytesArgv(PyConfig *config, int argc, char *const *argv)
|
459 |
+
|
460 |
+
Set command line arguments ("argv" member of *config*) from the
|
461 |
+
*argv* list of bytes strings. Decode bytes using
|
462 |
+
"Py_DecodeLocale()".
|
463 |
+
|
464 |
+
Preinitialize Python if needed.
|
465 |
+
|
466 |
+
PyStatus PyConfig_SetWideStringList(PyConfig *config, PyWideStringList *list, Py_ssize_t length, wchar_t **items)
|
467 |
+
|
468 |
+
Set the list of wide strings *list* to *length* and *items*.
|
469 |
+
|
470 |
+
Preinitialize Python if needed.
|
471 |
+
|
472 |
+
PyStatus PyConfig_Read(PyConfig *config)
|
473 |
+
|
474 |
+
Read all Python configuration.
|
475 |
+
|
476 |
+
Fields which are already initialized are left unchanged.
|
477 |
+
|
478 |
+
Fields for path configuration are no longer calculated or
|
479 |
+
modified when calling this function, as of Python 3.11.
|
480 |
+
|
481 |
+
The "PyConfig_Read()" function only parses "PyConfig.argv"
|
482 |
+
arguments once: "PyConfig.parse_argv" is set to "2" after
|
483 |
+
arguments are parsed. Since Python arguments are stripped from
|
484 |
+
"PyConfig.argv", parsing arguments twice would parse the
|
485 |
+
application options as Python options.
|
486 |
+
|
487 |
+
Preinitialize Python if needed.
|
488 |
+
|
489 |
+
Changed in version 3.10: The "PyConfig.argv" arguments are now
|
490 |
+
only parsed once, "PyConfig.parse_argv" is set to "2" after
|
491 |
+
arguments are parsed, and arguments are only parsed if
|
492 |
+
"PyConfig.parse_argv" equals "1".
|
493 |
+
|
494 |
+
Changed in version 3.11: "PyConfig_Read()" no longer calculates
|
495 |
+
all paths, and so fields listed under Python Path Configuration
|
496 |
+
may no longer be updated until "Py_InitializeFromConfig()" is
|
497 |
+
called.
|
498 |
+
|
499 |
+
void PyConfig_Clear(PyConfig *config)
|
500 |
+
|
501 |
+
Release configuration memory.
|
502 |
+
|
503 |
+
Most "PyConfig" methods preinitialize Python if needed. In that
|
504 |
+
case, the Python preinitialization configuration ("PyPreConfig") in
|
505 |
+
based on the "PyConfig". If configuration fields which are in
|
506 |
+
common with "PyPreConfig" are tuned, they must be set before
|
507 |
+
calling a "PyConfig" method:
|
508 |
+
|
509 |
+
* "PyConfig.dev_mode"
|
510 |
+
|
511 |
+
* "PyConfig.isolated"
|
512 |
+
|
513 |
+
* "PyConfig.parse_argv"
|
514 |
+
|
515 |
+
* "PyConfig.use_environment"
|
516 |
+
|
517 |
+
Moreover, if "PyConfig_SetArgv()" or "PyConfig_SetBytesArgv()" is
|
518 |
+
used, this method must be called before other methods, since the
|
519 |
+
preinitialization configuration depends on command line arguments
|
520 |
+
(if "parse_argv" is non-zero).
|
521 |
+
|
522 |
+
The caller of these methods is responsible to handle exceptions
|
523 |
+
(error or exit) using "PyStatus_Exception()" and
|
524 |
+
"Py_ExitStatusException()".
|
525 |
+
|
526 |
+
Structure fields:
|
527 |
+
|
528 |
+
PyWideStringList argv
|
529 |
+
|
530 |
+
Command line arguments: "sys.argv".
|
531 |
+
|
532 |
+
Set "parse_argv" to "1" to parse "argv" the same way the regular
|
533 |
+
Python parses Python command line arguments and then to strip
|
534 |
+
Python arguments from "argv".
|
535 |
+
|
536 |
+
If "argv" is empty, an empty string is added to ensure that
|
537 |
+
"sys.argv" always exists and is never empty.
|
538 |
+
|
539 |
+
Default: "NULL".
|
540 |
+
|
541 |
+
See also the "orig_argv" member.
|
542 |
+
|
543 |
+
int safe_path
|
544 |
+
|
545 |
+
If equals to zero, "Py_RunMain()" prepends a potentially unsafe
|
546 |
+
path to "sys.path" at startup:
|
547 |
+
|
548 |
+
* If "argv[0]" is equal to "L"-m"" ("python -m module"), prepend
|
549 |
+
the current working directory.
|
550 |
+
|
551 |
+
* If running a script ("python script.py"), prepend the script's
|
552 |
+
directory. If it's a symbolic link, resolve symbolic links.
|
553 |
+
|
554 |
+
* Otherwise ("python -c code" and "python"), prepend an empty
|
555 |
+
string, which means the current working directory.
|
556 |
+
|
557 |
+
Set to "1" by the "-P" command line option and the
|
558 |
+
"PYTHONSAFEPATH" environment variable.
|
559 |
+
|
560 |
+
Default: "0" in Python config, "1" in isolated config.
|
561 |
+
|
562 |
+
Added in version 3.11.
|
563 |
+
|
564 |
+
wchar_t *base_exec_prefix
|
565 |
+
|
566 |
+
"sys.base_exec_prefix".
|
567 |
+
|
568 |
+
Default: "NULL".
|
569 |
+
|
570 |
+
Part of the Python Path Configuration output.
|
571 |
+
|
572 |
+
wchar_t *base_executable
|
573 |
+
|
574 |
+
Python base executable: "sys._base_executable".
|
575 |
+
|
576 |
+
Set by the "__PYVENV_LAUNCHER__" environment variable.
|
577 |
+
|
578 |
+
Set from "PyConfig.executable" if "NULL".
|
579 |
+
|
580 |
+
Default: "NULL".
|
581 |
+
|
582 |
+
Part of the Python Path Configuration output.
|
583 |
+
|
584 |
+
wchar_t *base_prefix
|
585 |
+
|
586 |
+
"sys.base_prefix".
|
587 |
+
|
588 |
+
Default: "NULL".
|
589 |
+
|
590 |
+
Part of the Python Path Configuration output.
|
591 |
+
|
592 |
+
int buffered_stdio
|
593 |
+
|
594 |
+
If equals to "0" and "configure_c_stdio" is non-zero, disable
|
595 |
+
buffering on the C streams stdout and stderr.
|
596 |
+
|
597 |
+
Set to "0" by the "-u" command line option and the
|
598 |
+
"PYTHONUNBUFFERED" environment variable.
|
599 |
+
|
600 |
+
stdin is always opened in buffered mode.
|
601 |
+
|
602 |
+
Default: "1".
|
603 |
+
|
604 |
+
int bytes_warning
|
605 |
+
|
606 |
+
If equals to "1", issue a warning when comparing "bytes" or
|
607 |
+
"bytearray" with "str", or comparing "bytes" with "int".
|
608 |
+
|
609 |
+
If equal or greater to "2", raise a "BytesWarning" exception in
|
610 |
+
these cases.
|
611 |
+
|
612 |
+
Incremented by the "-b" command line option.
|
613 |
+
|
614 |
+
Default: "0".
|
615 |
+
|
616 |
+
int warn_default_encoding
|
617 |
+
|
618 |
+
If non-zero, emit a "EncodingWarning" warning when
|
619 |
+
"io.TextIOWrapper" uses its default encoding. See Opt-in
|
620 |
+
EncodingWarning for details.
|
621 |
+
|
622 |
+
Default: "0".
|
623 |
+
|
624 |
+
Added in version 3.10.
|
625 |
+
|
626 |
+
int code_debug_ranges
|
627 |
+
|
628 |
+
If equals to "0", disables the inclusion of the end line and
|
629 |
+
column mappings in code objects. Also disables traceback
|
630 |
+
printing carets to specific error locations.
|
631 |
+
|
632 |
+
Set to "0" by the "PYTHONNODEBUGRANGES" environment variable and
|
633 |
+
by the "-X no_debug_ranges" command line option.
|
634 |
+
|
635 |
+
Default: "1".
|
636 |
+
|
637 |
+
Added in version 3.11.
|
638 |
+
|
639 |
+
wchar_t *check_hash_pycs_mode
|
640 |
+
|
641 |
+
Control the validation behavior of hash-based ".pyc" files:
|
642 |
+
value of the "--check-hash-based-pycs" command line option.
|
643 |
+
|
644 |
+
Valid values:
|
645 |
+
|
646 |
+
* "L"always"": Hash the source file for invalidation regardless
|
647 |
+
of value of the 'check_source' flag.
|
648 |
+
|
649 |
+
* "L"never"": Assume that hash-based pycs always are valid.
|
650 |
+
|
651 |
+
* "L"default"": The 'check_source' flag in hash-based pycs
|
652 |
+
determines invalidation.
|
653 |
+
|
654 |
+
Default: "L"default"".
|
655 |
+
|
656 |
+
See also **PEP 552** "Deterministic pycs".
|
657 |
+
|
658 |
+
int configure_c_stdio
|
659 |
+
|
660 |
+
If non-zero, configure C standard streams:
|
661 |
+
|
662 |
+
* On Windows, set the binary mode ("O_BINARY") on stdin, stdout
|
663 |
+
and stderr.
|
664 |
+
|
665 |
+
* If "buffered_stdio" equals zero, disable buffering of stdin,
|
666 |
+
stdout and stderr streams.
|
667 |
+
|
668 |
+
* If "interactive" is non-zero, enable stream buffering on stdin
|
669 |
+
and stdout (only stdout on Windows).
|
670 |
+
|
671 |
+
Default: "1" in Python config, "0" in isolated config.
|
672 |
+
|
673 |
+
int dev_mode
|
674 |
+
|
675 |
+
If non-zero, enable the Python Development Mode.
|
676 |
+
|
677 |
+
Set to "1" by the "-X dev" option and the "PYTHONDEVMODE"
|
678 |
+
environment variable.
|
679 |
+
|
680 |
+
Default: "-1" in Python mode, "0" in isolated mode.
|
681 |
+
|
682 |
+
int dump_refs
|
683 |
+
|
684 |
+
Dump Python references?
|
685 |
+
|
686 |
+
If non-zero, dump all objects which are still alive at exit.
|
687 |
+
|
688 |
+
Set to "1" by the "PYTHONDUMPREFS" environment variable.
|
689 |
+
|
690 |
+
Need a special build of Python with the "Py_TRACE_REFS" macro
|
691 |
+
defined: see the "configure --with-trace-refs option".
|
692 |
+
|
693 |
+
Default: "0".
|
694 |
+
|
695 |
+
wchar_t *exec_prefix
|
696 |
+
|
697 |
+
The site-specific directory prefix where the platform-dependent
|
698 |
+
Python files are installed: "sys.exec_prefix".
|
699 |
+
|
700 |
+
Default: "NULL".
|
701 |
+
|
702 |
+
Part of the Python Path Configuration output.
|
703 |
+
|
704 |
+
wchar_t *executable
|
705 |
+
|
706 |
+
The absolute path of the executable binary for the Python
|
707 |
+
interpreter: "sys.executable".
|
708 |
+
|
709 |
+
Default: "NULL".
|
710 |
+
|
711 |
+
Part of the Python Path Configuration output.
|
712 |
+
|
713 |
+
int faulthandler
|
714 |
+
|
715 |
+
Enable faulthandler?
|
716 |
+
|
717 |
+
If non-zero, call "faulthandler.enable()" at startup.
|
718 |
+
|
719 |
+
Set to "1" by "-X faulthandler" and the "PYTHONFAULTHANDLER"
|
720 |
+
environment variable.
|
721 |
+
|
722 |
+
Default: "-1" in Python mode, "0" in isolated mode.
|
723 |
+
|
724 |
+
wchar_t *filesystem_encoding
|
725 |
+
|
726 |
+
*Filesystem encoding*: "sys.getfilesystemencoding()".
|
727 |
+
|
728 |
+
On macOS, Android and VxWorks: use ""utf-8"" by default.
|
729 |
+
|
730 |
+
On Windows: use ""utf-8"" by default, or ""mbcs"" if
|
731 |
+
"legacy_windows_fs_encoding" of "PyPreConfig" is non-zero.
|
732 |
+
|
733 |
+
Default encoding on other platforms:
|
734 |
+
|
735 |
+
* ""utf-8"" if "PyPreConfig.utf8_mode" is non-zero.
|
736 |
+
|
737 |
+
* ""ascii"" if Python detects that "nl_langinfo(CODESET)"
|
738 |
+
announces the ASCII encoding, whereas the "mbstowcs()"
|
739 |
+
function decodes from a different encoding (usually Latin1).
|
740 |
+
|
741 |
+
* ""utf-8"" if "nl_langinfo(CODESET)" returns an empty string.
|
742 |
+
|
743 |
+
* Otherwise, use the *locale encoding*: "nl_langinfo(CODESET)"
|
744 |
+
result.
|
745 |
+
|
746 |
+
At Python startup, the encoding name is normalized to the Python
|
747 |
+
codec name. For example, ""ANSI_X3.4-1968"" is replaced with
|
748 |
+
""ascii"".
|
749 |
+
|
750 |
+
See also the "filesystem_errors" member.
|
751 |
+
|
752 |
+
wchar_t *filesystem_errors
|
753 |
+
|
754 |
+
*Filesystem error handler*: "sys.getfilesystemencodeerrors()".
|
755 |
+
|
756 |
+
On Windows: use ""surrogatepass"" by default, or ""replace"" if
|
757 |
+
"legacy_windows_fs_encoding" of "PyPreConfig" is non-zero.
|
758 |
+
|
759 |
+
On other platforms: use ""surrogateescape"" by default.
|
760 |
+
|
761 |
+
Supported error handlers:
|
762 |
+
|
763 |
+
* ""strict""
|
764 |
+
|
765 |
+
* ""surrogateescape""
|
766 |
+
|
767 |
+
* ""surrogatepass"" (only supported with the UTF-8 encoding)
|
768 |
+
|
769 |
+
See also the "filesystem_encoding" member.
|
770 |
+
|
771 |
+
unsigned long hash_seed
|
772 |
+
|
773 |
+
int use_hash_seed
|
774 |
+
|
775 |
+
Randomized hash function seed.
|
776 |
+
|
777 |
+
If "use_hash_seed" is zero, a seed is chosen randomly at Python
|
778 |
+
startup, and "hash_seed" is ignored.
|
779 |
+
|
780 |
+
Set by the "PYTHONHASHSEED" environment variable.
|
781 |
+
|
782 |
+
Default *use_hash_seed* value: "-1" in Python mode, "0" in
|
783 |
+
isolated mode.
|
784 |
+
|
785 |
+
wchar_t *home
|
786 |
+
|
787 |
+
Python home directory.
|
788 |
+
|
789 |
+
If "Py_SetPythonHome()" has been called, use its argument if it
|
790 |
+
is not "NULL".
|
791 |
+
|
792 |
+
Set by the "PYTHONHOME" environment variable.
|
793 |
+
|
794 |
+
Default: "NULL".
|
795 |
+
|
796 |
+
Part of the Python Path Configuration input.
|
797 |
+
|
798 |
+
int import_time
|
799 |
+
|
800 |
+
If non-zero, profile import time.
|
801 |
+
|
802 |
+
Set the "1" by the "-X importtime" option and the
|
803 |
+
"PYTHONPROFILEIMPORTTIME" environment variable.
|
804 |
+
|
805 |
+
Default: "0".
|
806 |
+
|
807 |
+
int inspect
|
808 |
+
|
809 |
+
Enter interactive mode after executing a script or a command.
|
810 |
+
|
811 |
+
If greater than "0", enable inspect: when a script is passed as
|
812 |
+
first argument or the -c option is used, enter interactive mode
|
813 |
+
after executing the script or the command, even when "sys.stdin"
|
814 |
+
does not appear to be a terminal.
|
815 |
+
|
816 |
+
Incremented by the "-i" command line option. Set to "1" if the
|
817 |
+
"PYTHONINSPECT" environment variable is non-empty.
|
818 |
+
|
819 |
+
Default: "0".
|
820 |
+
|
821 |
+
int install_signal_handlers
|
822 |
+
|
823 |
+
Install Python signal handlers?
|
824 |
+
|
825 |
+
Default: "1" in Python mode, "0" in isolated mode.
|
826 |
+
|
827 |
+
int interactive
|
828 |
+
|
829 |
+
If greater than "0", enable the interactive mode (REPL).
|
830 |
+
|
831 |
+
Incremented by the "-i" command line option.
|
832 |
+
|
833 |
+
Default: "0".
|
834 |
+
|
835 |
+
int int_max_str_digits
|
836 |
+
|
837 |
+
Configures the integer string conversion length limitation. An
|
838 |
+
initial value of "-1" means the value will be taken from the
|
839 |
+
command line or environment or otherwise default to 4300
|
840 |
+
("sys.int_info.default_max_str_digits"). A value of "0"
|
841 |
+
disables the limitation. Values greater than zero but less than
|
842 |
+
640 ("sys.int_info.str_digits_check_threshold") are unsupported
|
843 |
+
and will produce an error.
|
844 |
+
|
845 |
+
Configured by the "-X int_max_str_digits" command line flag or
|
846 |
+
the "PYTHONINTMAXSTRDIGITS" environment variable.
|
847 |
+
|
848 |
+
Default: "-1" in Python mode. 4300
|
849 |
+
("sys.int_info.default_max_str_digits") in isolated mode.
|
850 |
+
|
851 |
+
Added in version 3.12.
|
852 |
+
|
853 |
+
int isolated
|
854 |
+
|
855 |
+
If greater than "0", enable isolated mode:
|
856 |
+
|
857 |
+
* Set "safe_path" to "1": don't prepend a potentially unsafe
|
858 |
+
path to "sys.path" at Python startup, such as the current
|
859 |
+
directory, the script's directory or an empty string.
|
860 |
+
|
861 |
+
* Set "use_environment" to "0": ignore "PYTHON" environment
|
862 |
+
variables.
|
863 |
+
|
864 |
+
* Set "user_site_directory" to "0": don't add the user site
|
865 |
+
directory to "sys.path".
|
866 |
+
|
867 |
+
* Python REPL doesn't import "readline" nor enable default
|
868 |
+
readline configuration on interactive prompts.
|
869 |
+
|
870 |
+
Set to "1" by the "-I" command line option.
|
871 |
+
|
872 |
+
Default: "0" in Python mode, "1" in isolated mode.
|
873 |
+
|
874 |
+
See also the Isolated Configuration and "PyPreConfig.isolated".
|
875 |
+
|
876 |
+
int legacy_windows_stdio
|
877 |
+
|
878 |
+
If non-zero, use "io.FileIO" instead of "io._WindowsConsoleIO"
|
879 |
+
for "sys.stdin", "sys.stdout" and "sys.stderr".
|
880 |
+
|
881 |
+
Set to "1" if the "PYTHONLEGACYWINDOWSSTDIO" environment
|
882 |
+
variable is set to a non-empty string.
|
883 |
+
|
884 |
+
Only available on Windows. "#ifdef MS_WINDOWS" macro can be used
|
885 |
+
for Windows specific code.
|
886 |
+
|
887 |
+
Default: "0".
|
888 |
+
|
889 |
+
See also the **PEP 528** (Change Windows console encoding to
|
890 |
+
UTF-8).
|
891 |
+
|
892 |
+
int malloc_stats
|
893 |
+
|
894 |
+
If non-zero, dump statistics on Python pymalloc memory allocator
|
895 |
+
at exit.
|
896 |
+
|
897 |
+
Set to "1" by the "PYTHONMALLOCSTATS" environment variable.
|
898 |
+
|
899 |
+
The option is ignored if Python is "configured using the
|
900 |
+
--without-pymalloc option".
|
901 |
+
|
902 |
+
Default: "0".
|
903 |
+
|
904 |
+
wchar_t *platlibdir
|
905 |
+
|
906 |
+
Platform library directory name: "sys.platlibdir".
|
907 |
+
|
908 |
+
Set by the "PYTHONPLATLIBDIR" environment variable.
|
909 |
+
|
910 |
+
Default: value of the "PLATLIBDIR" macro which is set by the
|
911 |
+
"configure --with-platlibdir option" (default: ""lib"", or
|
912 |
+
""DLLs"" on Windows).
|
913 |
+
|
914 |
+
Part of the Python Path Configuration input.
|
915 |
+
|
916 |
+
Added in version 3.9.
|
917 |
+
|
918 |
+
Changed in version 3.11: This macro is now used on Windows to
|
919 |
+
locate the standard library extension modules, typically under
|
920 |
+
"DLLs". However, for compatibility, note that this value is
|
921 |
+
ignored for any non-standard layouts, including in-tree builds
|
922 |
+
and virtual environments.
|
923 |
+
|
924 |
+
wchar_t *pythonpath_env
|
925 |
+
|
926 |
+
Module search paths ("sys.path") as a string separated by
|
927 |
+
"DELIM" ("os.pathsep").
|
928 |
+
|
929 |
+
Set by the "PYTHONPATH" environment variable.
|
930 |
+
|
931 |
+
Default: "NULL".
|
932 |
+
|
933 |
+
Part of the Python Path Configuration input.
|
934 |
+
|
935 |
+
PyWideStringList module_search_paths
|
936 |
+
|
937 |
+
int module_search_paths_set
|
938 |
+
|
939 |
+
Module search paths: "sys.path".
|
940 |
+
|
941 |
+
If "module_search_paths_set" is equal to "0",
|
942 |
+
"Py_InitializeFromConfig()" will replace "module_search_paths"
|
943 |
+
and sets "module_search_paths_set" to "1".
|
944 |
+
|
945 |
+
Default: empty list ("module_search_paths") and "0"
|
946 |
+
("module_search_paths_set").
|
947 |
+
|
948 |
+
Part of the Python Path Configuration output.
|
949 |
+
|
950 |
+
int optimization_level
|
951 |
+
|
952 |
+
Compilation optimization level:
|
953 |
+
|
954 |
+
* "0": Peephole optimizer, set "__debug__" to "True".
|
955 |
+
|
956 |
+
* "1": Level 0, remove assertions, set "__debug__" to "False".
|
957 |
+
|
958 |
+
* "2": Level 1, strip docstrings.
|
959 |
+
|
960 |
+
Incremented by the "-O" command line option. Set to the
|
961 |
+
"PYTHONOPTIMIZE" environment variable value.
|
962 |
+
|
963 |
+
Default: "0".
|
964 |
+
|
965 |
+
PyWideStringList orig_argv
|
966 |
+
|
967 |
+
The list of the original command line arguments passed to the
|
968 |
+
Python executable: "sys.orig_argv".
|
969 |
+
|
970 |
+
If "orig_argv" list is empty and "argv" is not a list only
|
971 |
+
containing an empty string, "PyConfig_Read()" copies "argv" into
|
972 |
+
"orig_argv" before modifying "argv" (if "parse_argv" is non-
|
973 |
+
zero).
|
974 |
+
|
975 |
+
See also the "argv" member and the "Py_GetArgcArgv()" function.
|
976 |
+
|
977 |
+
Default: empty list.
|
978 |
+
|
979 |
+
Added in version 3.10.
|
980 |
+
|
981 |
+
int parse_argv
|
982 |
+
|
983 |
+
Parse command line arguments?
|
984 |
+
|
985 |
+
If equals to "1", parse "argv" the same way the regular Python
|
986 |
+
parses command line arguments, and strip Python arguments from
|
987 |
+
"argv".
|
988 |
+
|
989 |
+
The "PyConfig_Read()" function only parses "PyConfig.argv"
|
990 |
+
arguments once: "PyConfig.parse_argv" is set to "2" after
|
991 |
+
arguments are parsed. Since Python arguments are stripped from
|
992 |
+
"PyConfig.argv", parsing arguments twice would parse the
|
993 |
+
application options as Python options.
|
994 |
+
|
995 |
+
Default: "1" in Python mode, "0" in isolated mode.
|
996 |
+
|
997 |
+
Changed in version 3.10: The "PyConfig.argv" arguments are now
|
998 |
+
only parsed if "PyConfig.parse_argv" equals to "1".
|
999 |
+
|
1000 |
+
int parser_debug
|
1001 |
+
|
1002 |
+
Parser debug mode. If greater than "0", turn on parser debugging
|
1003 |
+
output (for expert only, depending on compilation options).
|
1004 |
+
|
1005 |
+
Incremented by the "-d" command line option. Set to the
|
1006 |
+
"PYTHONDEBUG" environment variable value.
|
1007 |
+
|
1008 |
+
Need a debug build of Python (the "Py_DEBUG" macro must be
|
1009 |
+
defined).
|
1010 |
+
|
1011 |
+
Default: "0".
|
1012 |
+
|
1013 |
+
int pathconfig_warnings
|
1014 |
+
|
1015 |
+
If non-zero, calculation of path configuration is allowed to log
|
1016 |
+
warnings into "stderr". If equals to "0", suppress these
|
1017 |
+
warnings.
|
1018 |
+
|
1019 |
+
Default: "1" in Python mode, "0" in isolated mode.
|
1020 |
+
|
1021 |
+
Part of the Python Path Configuration input.
|
1022 |
+
|
1023 |
+
Changed in version 3.11: Now also applies on Windows.
|
1024 |
+
|
1025 |
+
wchar_t *prefix
|
1026 |
+
|
1027 |
+
The site-specific directory prefix where the platform
|
1028 |
+
independent Python files are installed: "sys.prefix".
|
1029 |
+
|
1030 |
+
Default: "NULL".
|
1031 |
+
|
1032 |
+
Part of the Python Path Configuration output.
|
1033 |
+
|
1034 |
+
wchar_t *program_name
|
1035 |
+
|
1036 |
+
Program name used to initialize "executable" and in early error
|
1037 |
+
messages during Python initialization.
|
1038 |
+
|
1039 |
+
* If "Py_SetProgramName()" has been called, use its argument.
|
1040 |
+
|
1041 |
+
* On macOS, use "PYTHONEXECUTABLE" environment variable if set.
|
1042 |
+
|
1043 |
+
* If the "WITH_NEXT_FRAMEWORK" macro is defined, use
|
1044 |
+
"__PYVENV_LAUNCHER__" environment variable if set.
|
1045 |
+
|
1046 |
+
* Use "argv[0]" of "argv" if available and non-empty.
|
1047 |
+
|
1048 |
+
* Otherwise, use "L"python"" on Windows, or "L"python3"" on
|
1049 |
+
other platforms.
|
1050 |
+
|
1051 |
+
Default: "NULL".
|
1052 |
+
|
1053 |
+
Part of the Python Path Configuration input.
|
1054 |
+
|
1055 |
+
wchar_t *pycache_prefix
|
1056 |
+
|
1057 |
+
Directory where cached ".pyc" files are written:
|
1058 |
+
"sys.pycache_prefix".
|
1059 |
+
|
1060 |
+
Set by the "-X pycache_prefix=PATH" command line option and the
|
1061 |
+
"PYTHONPYCACHEPREFIX" environment variable.
|
1062 |
+
|
1063 |
+
If "NULL", "sys.pycache_prefix" is set to "None".
|
1064 |
+
|
1065 |
+
Default: "NULL".
|
1066 |
+
|
1067 |
+
int quiet
|
1068 |
+
|
1069 |
+
Quiet mode. If greater than "0", don't display the copyright and
|
1070 |
+
version at Python startup in interactive mode.
|
1071 |
+
|
1072 |
+
Incremented by the "-q" command line option.
|
1073 |
+
|
1074 |
+
Default: "0".
|
1075 |
+
|
1076 |
+
wchar_t *run_command
|
1077 |
+
|
1078 |
+
Value of the "-c" command line option.
|
1079 |
+
|
1080 |
+
Used by "Py_RunMain()".
|
1081 |
+
|
1082 |
+
Default: "NULL".
|
1083 |
+
|
1084 |
+
wchar_t *run_filename
|
1085 |
+
|
1086 |
+
Filename passed on the command line: trailing command line
|
1087 |
+
argument without "-c" or "-m". It is used by the "Py_RunMain()"
|
1088 |
+
function.
|
1089 |
+
|
1090 |
+
For example, it is set to "script.py" by the "python3 script.py
|
1091 |
+
arg" command line.
|
1092 |
+
|
1093 |
+
See also the "PyConfig.skip_source_first_line" option.
|
1094 |
+
|
1095 |
+
Default: "NULL".
|
1096 |
+
|
1097 |
+
wchar_t *run_module
|
1098 |
+
|
1099 |
+
Value of the "-m" command line option.
|
1100 |
+
|
1101 |
+
Used by "Py_RunMain()".
|
1102 |
+
|
1103 |
+
Default: "NULL".
|
1104 |
+
|
1105 |
+
int show_ref_count
|
1106 |
+
|
1107 |
+
Show total reference count at exit (excluding immortal objects)?
|
1108 |
+
|
1109 |
+
Set to "1" by "-X showrefcount" command line option.
|
1110 |
+
|
1111 |
+
Need a debug build of Python (the "Py_REF_DEBUG" macro must be
|
1112 |
+
defined).
|
1113 |
+
|
1114 |
+
Default: "0".
|
1115 |
+
|
1116 |
+
int site_import
|
1117 |
+
|
1118 |
+
Import the "site" module at startup?
|
1119 |
+
|
1120 |
+
If equal to zero, disable the import of the module site and the
|
1121 |
+
site-dependent manipulations of "sys.path" that it entails.
|
1122 |
+
|
1123 |
+
Also disable these manipulations if the "site" module is
|
1124 |
+
explicitly imported later (call "site.main()" if you want them
|
1125 |
+
to be triggered).
|
1126 |
+
|
1127 |
+
Set to "0" by the "-S" command line option.
|
1128 |
+
|
1129 |
+
"sys.flags.no_site" is set to the inverted value of
|
1130 |
+
"site_import".
|
1131 |
+
|
1132 |
+
Default: "1".
|
1133 |
+
|
1134 |
+
int skip_source_first_line
|
1135 |
+
|
1136 |
+
If non-zero, skip the first line of the "PyConfig.run_filename"
|
1137 |
+
source.
|
1138 |
+
|
1139 |
+
It allows the usage of non-Unix forms of "#!cmd". This is
|
1140 |
+
intended for a DOS specific hack only.
|
1141 |
+
|
1142 |
+
Set to "1" by the "-x" command line option.
|
1143 |
+
|
1144 |
+
Default: "0".
|
1145 |
+
|
1146 |
+
wchar_t *stdio_encoding
|
1147 |
+
|
1148 |
+
wchar_t *stdio_errors
|
1149 |
+
|
1150 |
+
Encoding and encoding errors of "sys.stdin", "sys.stdout" and
|
1151 |
+
"sys.stderr" (but "sys.stderr" always uses ""backslashreplace""
|
1152 |
+
error handler).
|
1153 |
+
|
1154 |
+
If "Py_SetStandardStreamEncoding()" has been called, use its
|
1155 |
+
*error* and *errors* arguments if they are not "NULL".
|
1156 |
+
|
1157 |
+
Use the "PYTHONIOENCODING" environment variable if it is non-
|
1158 |
+
empty.
|
1159 |
+
|
1160 |
+
Default encoding:
|
1161 |
+
|
1162 |
+
* ""UTF-8"" if "PyPreConfig.utf8_mode" is non-zero.
|
1163 |
+
|
1164 |
+
* Otherwise, use the *locale encoding*.
|
1165 |
+
|
1166 |
+
Default error handler:
|
1167 |
+
|
1168 |
+
* On Windows: use ""surrogateescape"".
|
1169 |
+
|
1170 |
+
* ""surrogateescape"" if "PyPreConfig.utf8_mode" is non-zero, or
|
1171 |
+
if the LC_CTYPE locale is "C" or "POSIX".
|
1172 |
+
|
1173 |
+
* ""strict"" otherwise.
|
1174 |
+
|
1175 |
+
int tracemalloc
|
1176 |
+
|
1177 |
+
Enable tracemalloc?
|
1178 |
+
|
1179 |
+
If non-zero, call "tracemalloc.start()" at startup.
|
1180 |
+
|
1181 |
+
Set by "-X tracemalloc=N" command line option and by the
|
1182 |
+
"PYTHONTRACEMALLOC" environment variable.
|
1183 |
+
|
1184 |
+
Default: "-1" in Python mode, "0" in isolated mode.
|
1185 |
+
|
1186 |
+
int perf_profiling
|
1187 |
+
|
1188 |
+
Enable compatibility mode with the perf profiler?
|
1189 |
+
|
1190 |
+
If non-zero, initialize the perf trampoline. See Python support
|
1191 |
+
for the Linux perf profiler for more information.
|
1192 |
+
|
1193 |
+
Set by "-X perf" command line option and by the
|
1194 |
+
"PYTHONPERFSUPPORT" environment variable.
|
1195 |
+
|
1196 |
+
Default: "-1".
|
1197 |
+
|
1198 |
+
Added in version 3.12.
|
1199 |
+
|
1200 |
+
int use_environment
|
1201 |
+
|
1202 |
+
Use environment variables?
|
1203 |
+
|
1204 |
+
If equals to zero, ignore the environment variables.
|
1205 |
+
|
1206 |
+
Set to "0" by the "-E" environment variable.
|
1207 |
+
|
1208 |
+
Default: "1" in Python config and "0" in isolated config.
|
1209 |
+
|
1210 |
+
int user_site_directory
|
1211 |
+
|
1212 |
+
If non-zero, add the user site directory to "sys.path".
|
1213 |
+
|
1214 |
+
Set to "0" by the "-s" and "-I" command line options.
|
1215 |
+
|
1216 |
+
Set to "0" by the "PYTHONNOUSERSITE" environment variable.
|
1217 |
+
|
1218 |
+
Default: "1" in Python mode, "0" in isolated mode.
|
1219 |
+
|
1220 |
+
int verbose
|
1221 |
+
|
1222 |
+
Verbose mode. If greater than "0", print a message each time a
|
1223 |
+
module is imported, showing the place (filename or built-in
|
1224 |
+
module) from which it is loaded.
|
1225 |
+
|
1226 |
+
If greater than or equal to "2", print a message for each file
|
1227 |
+
that is checked for when searching for a module. Also provides
|
1228 |
+
information on module cleanup at exit.
|
1229 |
+
|
1230 |
+
Incremented by the "-v" command line option.
|
1231 |
+
|
1232 |
+
Set by the "PYTHONVERBOSE" environment variable value.
|
1233 |
+
|
1234 |
+
Default: "0".
|
1235 |
+
|
1236 |
+
PyWideStringList warnoptions
|
1237 |
+
|
1238 |
+
Options of the "warnings" module to build warnings filters,
|
1239 |
+
lowest to highest priority: "sys.warnoptions".
|
1240 |
+
|
1241 |
+
The "warnings" module adds "sys.warnoptions" in the reverse
|
1242 |
+
order: the last "PyConfig.warnoptions" item becomes the first
|
1243 |
+
item of "warnings.filters" which is checked first (highest
|
1244 |
+
priority).
|
1245 |
+
|
1246 |
+
The "-W" command line options adds its value to "warnoptions",
|
1247 |
+
it can be used multiple times.
|
1248 |
+
|
1249 |
+
The "PYTHONWARNINGS" environment variable can also be used to
|
1250 |
+
add warning options. Multiple options can be specified,
|
1251 |
+
separated by commas (",").
|
1252 |
+
|
1253 |
+
Default: empty list.
|
1254 |
+
|
1255 |
+
int write_bytecode
|
1256 |
+
|
1257 |
+
If equal to "0", Python won't try to write ".pyc" files on the
|
1258 |
+
import of source modules.
|
1259 |
+
|
1260 |
+
Set to "0" by the "-B" command line option and the
|
1261 |
+
"PYTHONDONTWRITEBYTECODE" environment variable.
|
1262 |
+
|
1263 |
+
"sys.dont_write_bytecode" is initialized to the inverted value
|
1264 |
+
of "write_bytecode".
|
1265 |
+
|
1266 |
+
Default: "1".
|
1267 |
+
|
1268 |
+
PyWideStringList xoptions
|
1269 |
+
|
1270 |
+
Values of the "-X" command line options: "sys._xoptions".
|
1271 |
+
|
1272 |
+
Default: empty list.
|
1273 |
+
|
1274 |
+
If "parse_argv" is non-zero, "argv" arguments are parsed the same way
|
1275 |
+
the regular Python parses command line arguments, and Python arguments
|
1276 |
+
are stripped from "argv".
|
1277 |
+
|
1278 |
+
The "xoptions" options are parsed to set other options: see the "-X"
|
1279 |
+
command line option.
|
1280 |
+
|
1281 |
+
Changed in version 3.9: The "show_alloc_count" field has been removed.
|
1282 |
+
|
1283 |
+
|
1284 |
+
Initialization with PyConfig
|
1285 |
+
============================
|
1286 |
+
|
1287 |
+
Function to initialize Python:
|
1288 |
+
|
1289 |
+
PyStatus Py_InitializeFromConfig(const PyConfig *config)
|
1290 |
+
|
1291 |
+
Initialize Python from *config* configuration.
|
1292 |
+
|
1293 |
+
The caller is responsible to handle exceptions (error or exit) using
|
1294 |
+
"PyStatus_Exception()" and "Py_ExitStatusException()".
|
1295 |
+
|
1296 |
+
If "PyImport_FrozenModules()", "PyImport_AppendInittab()" or
|
1297 |
+
"PyImport_ExtendInittab()" are used, they must be set or called after
|
1298 |
+
Python preinitialization and before the Python initialization. If
|
1299 |
+
Python is initialized multiple times, "PyImport_AppendInittab()" or
|
1300 |
+
"PyImport_ExtendInittab()" must be called before each Python
|
1301 |
+
initialization.
|
1302 |
+
|
1303 |
+
The current configuration ("PyConfig" type) is stored in
|
1304 |
+
"PyInterpreterState.config".
|
1305 |
+
|
1306 |
+
Example setting the program name:
|
1307 |
+
|
1308 |
+
void init_python(void)
|
1309 |
+
{
|
1310 |
+
PyStatus status;
|
1311 |
+
|
1312 |
+
PyConfig config;
|
1313 |
+
PyConfig_InitPythonConfig(&config);
|
1314 |
+
|
1315 |
+
/* Set the program name. Implicitly preinitialize Python. */
|
1316 |
+
status = PyConfig_SetString(&config, &config.program_name,
|
1317 |
+
L"/path/to/my_program");
|
1318 |
+
if (PyStatus_Exception(status)) {
|
1319 |
+
goto exception;
|
1320 |
+
}
|
1321 |
+
|
1322 |
+
status = Py_InitializeFromConfig(&config);
|
1323 |
+
if (PyStatus_Exception(status)) {
|
1324 |
+
goto exception;
|
1325 |
+
}
|
1326 |
+
PyConfig_Clear(&config);
|
1327 |
+
return;
|
1328 |
+
|
1329 |
+
exception:
|
1330 |
+
PyConfig_Clear(&config);
|
1331 |
+
Py_ExitStatusException(status);
|
1332 |
+
}
|
1333 |
+
|
1334 |
+
More complete example modifying the default configuration, read the
|
1335 |
+
configuration, and then override some parameters. Note that since
|
1336 |
+
3.11, many parameters are not calculated until initialization, and so
|
1337 |
+
values cannot be read from the configuration structure. Any values set
|
1338 |
+
before initialize is called will be left unchanged by initialization:
|
1339 |
+
|
1340 |
+
PyStatus init_python(const char *program_name)
|
1341 |
+
{
|
1342 |
+
PyStatus status;
|
1343 |
+
|
1344 |
+
PyConfig config;
|
1345 |
+
PyConfig_InitPythonConfig(&config);
|
1346 |
+
|
1347 |
+
/* Set the program name before reading the configuration
|
1348 |
+
(decode byte string from the locale encoding).
|
1349 |
+
|
1350 |
+
Implicitly preinitialize Python. */
|
1351 |
+
status = PyConfig_SetBytesString(&config, &config.program_name,
|
1352 |
+
program_name);
|
1353 |
+
if (PyStatus_Exception(status)) {
|
1354 |
+
goto done;
|
1355 |
+
}
|
1356 |
+
|
1357 |
+
/* Read all configuration at once */
|
1358 |
+
status = PyConfig_Read(&config);
|
1359 |
+
if (PyStatus_Exception(status)) {
|
1360 |
+
goto done;
|
1361 |
+
}
|
1362 |
+
|
1363 |
+
/* Specify sys.path explicitly */
|
1364 |
+
/* If you want to modify the default set of paths, finish
|
1365 |
+
initialization first and then use PySys_GetObject("path") */
|
1366 |
+
config.module_search_paths_set = 1;
|
1367 |
+
status = PyWideStringList_Append(&config.module_search_paths,
|
1368 |
+
L"/path/to/stdlib");
|
1369 |
+
if (PyStatus_Exception(status)) {
|
1370 |
+
goto done;
|
1371 |
+
}
|
1372 |
+
status = PyWideStringList_Append(&config.module_search_paths,
|
1373 |
+
L"/path/to/more/modules");
|
1374 |
+
if (PyStatus_Exception(status)) {
|
1375 |
+
goto done;
|
1376 |
+
}
|
1377 |
+
|
1378 |
+
/* Override executable computed by PyConfig_Read() */
|
1379 |
+
status = PyConfig_SetString(&config, &config.executable,
|
1380 |
+
L"/path/to/my_executable");
|
1381 |
+
if (PyStatus_Exception(status)) {
|
1382 |
+
goto done;
|
1383 |
+
}
|
1384 |
+
|
1385 |
+
status = Py_InitializeFromConfig(&config);
|
1386 |
+
|
1387 |
+
done:
|
1388 |
+
PyConfig_Clear(&config);
|
1389 |
+
return status;
|
1390 |
+
}
|
1391 |
+
|
1392 |
+
|
1393 |
+
Isolated Configuration
|
1394 |
+
======================
|
1395 |
+
|
1396 |
+
"PyPreConfig_InitIsolatedConfig()" and "PyConfig_InitIsolatedConfig()"
|
1397 |
+
functions create a configuration to isolate Python from the system.
|
1398 |
+
For example, to embed Python into an application.
|
1399 |
+
|
1400 |
+
This configuration ignores global configuration variables, environment
|
1401 |
+
variables, command line arguments ("PyConfig.argv" is not parsed) and
|
1402 |
+
user site directory. The C standard streams (ex: "stdout") and the
|
1403 |
+
LC_CTYPE locale are left unchanged. Signal handlers are not installed.
|
1404 |
+
|
1405 |
+
Configuration files are still used with this configuration to
|
1406 |
+
determine paths that are unspecified. Ensure "PyConfig.home" is
|
1407 |
+
specified to avoid computing the default path configuration.
|
1408 |
+
|
1409 |
+
|
1410 |
+
Python Configuration
|
1411 |
+
====================
|
1412 |
+
|
1413 |
+
"PyPreConfig_InitPythonConfig()" and "PyConfig_InitPythonConfig()"
|
1414 |
+
functions create a configuration to build a customized Python which
|
1415 |
+
behaves as the regular Python.
|
1416 |
+
|
1417 |
+
Environments variables and command line arguments are used to
|
1418 |
+
configure Python, whereas global configuration variables are ignored.
|
1419 |
+
|
1420 |
+
This function enables C locale coercion (**PEP 538**) and Python UTF-8
|
1421 |
+
Mode (**PEP 540**) depending on the LC_CTYPE locale, "PYTHONUTF8" and
|
1422 |
+
"PYTHONCOERCECLOCALE" environment variables.
|
1423 |
+
|
1424 |
+
|
1425 |
+
Python Path Configuration
|
1426 |
+
=========================
|
1427 |
+
|
1428 |
+
"PyConfig" contains multiple fields for the path configuration:
|
1429 |
+
|
1430 |
+
* Path configuration inputs:
|
1431 |
+
|
1432 |
+
* "PyConfig.home"
|
1433 |
+
|
1434 |
+
* "PyConfig.platlibdir"
|
1435 |
+
|
1436 |
+
* "PyConfig.pathconfig_warnings"
|
1437 |
+
|
1438 |
+
* "PyConfig.program_name"
|
1439 |
+
|
1440 |
+
* "PyConfig.pythonpath_env"
|
1441 |
+
|
1442 |
+
* current working directory: to get absolute paths
|
1443 |
+
|
1444 |
+
* "PATH" environment variable to get the program full path (from
|
1445 |
+
"PyConfig.program_name")
|
1446 |
+
|
1447 |
+
* "__PYVENV_LAUNCHER__" environment variable
|
1448 |
+
|
1449 |
+
* (Windows only) Application paths in the registry under
|
1450 |
+
"SoftwarePythonPythonCoreX.YPythonPath" of HKEY_CURRENT_USER and
|
1451 |
+
HKEY_LOCAL_MACHINE (where X.Y is the Python version).
|
1452 |
+
|
1453 |
+
* Path configuration output fields:
|
1454 |
+
|
1455 |
+
* "PyConfig.base_exec_prefix"
|
1456 |
+
|
1457 |
+
* "PyConfig.base_executable"
|
1458 |
+
|
1459 |
+
* "PyConfig.base_prefix"
|
1460 |
+
|
1461 |
+
* "PyConfig.exec_prefix"
|
1462 |
+
|
1463 |
+
* "PyConfig.executable"
|
1464 |
+
|
1465 |
+
* "PyConfig.module_search_paths_set", "PyConfig.module_search_paths"
|
1466 |
+
|
1467 |
+
* "PyConfig.prefix"
|
1468 |
+
|
1469 |
+
If at least one "output field" is not set, Python calculates the path
|
1470 |
+
configuration to fill unset fields. If "module_search_paths_set" is
|
1471 |
+
equal to "0", "module_search_paths" is overridden and
|
1472 |
+
"module_search_paths_set" is set to "1".
|
1473 |
+
|
1474 |
+
It is possible to completely ignore the function calculating the
|
1475 |
+
default path configuration by setting explicitly all path
|
1476 |
+
configuration output fields listed above. A string is considered as
|
1477 |
+
set even if it is non-empty. "module_search_paths" is considered as
|
1478 |
+
set if "module_search_paths_set" is set to "1". In this case,
|
1479 |
+
"module_search_paths" will be used without modification.
|
1480 |
+
|
1481 |
+
Set "pathconfig_warnings" to "0" to suppress warnings when calculating
|
1482 |
+
the path configuration (Unix only, Windows does not log any warning).
|
1483 |
+
|
1484 |
+
If "base_prefix" or "base_exec_prefix" fields are not set, they
|
1485 |
+
inherit their value from "prefix" and "exec_prefix" respectively.
|
1486 |
+
|
1487 |
+
"Py_RunMain()" and "Py_Main()" modify "sys.path":
|
1488 |
+
|
1489 |
+
* If "run_filename" is set and is a directory which contains a
|
1490 |
+
"__main__.py" script, prepend "run_filename" to "sys.path".
|
1491 |
+
|
1492 |
+
* If "isolated" is zero:
|
1493 |
+
|
1494 |
+
* If "run_module" is set, prepend the current directory to
|
1495 |
+
"sys.path". Do nothing if the current directory cannot be read.
|
1496 |
+
|
1497 |
+
* If "run_filename" is set, prepend the directory of the filename to
|
1498 |
+
"sys.path".
|
1499 |
+
|
1500 |
+
* Otherwise, prepend an empty string to "sys.path".
|
1501 |
+
|
1502 |
+
If "site_import" is non-zero, "sys.path" can be modified by the "site"
|
1503 |
+
module. If "user_site_directory" is non-zero and the user's site-
|
1504 |
+
package directory exists, the "site" module appends the user's site-
|
1505 |
+
package directory to "sys.path".
|
1506 |
+
|
1507 |
+
The following configuration files are used by the path configuration:
|
1508 |
+
|
1509 |
+
* "pyvenv.cfg"
|
1510 |
+
|
1511 |
+
* "._pth" file (ex: "python._pth")
|
1512 |
+
|
1513 |
+
* "pybuilddir.txt" (Unix only)
|
1514 |
+
|
1515 |
+
If a "._pth" file is present:
|
1516 |
+
|
1517 |
+
* Set "isolated" to "1".
|
1518 |
+
|
1519 |
+
* Set "use_environment" to "0".
|
1520 |
+
|
1521 |
+
* Set "site_import" to "0".
|
1522 |
+
|
1523 |
+
* Set "safe_path" to "1".
|
1524 |
+
|
1525 |
+
The "__PYVENV_LAUNCHER__" environment variable is used to set
|
1526 |
+
"PyConfig.base_executable"
|
1527 |
+
|
1528 |
+
|
1529 |
+
Py_RunMain()
|
1530 |
+
============
|
1531 |
+
|
1532 |
+
int Py_RunMain(void)
|
1533 |
+
|
1534 |
+
Execute the command ("PyConfig.run_command"), the script
|
1535 |
+
("PyConfig.run_filename") or the module ("PyConfig.run_module")
|
1536 |
+
specified on the command line or in the configuration.
|
1537 |
+
|
1538 |
+
By default and when if "-i" option is used, run the REPL.
|
1539 |
+
|
1540 |
+
Finally, finalizes Python and returns an exit status that can be
|
1541 |
+
passed to the "exit()" function.
|
1542 |
+
|
1543 |
+
See Python Configuration for an example of customized Python always
|
1544 |
+
running in isolated mode using "Py_RunMain()".
|
1545 |
+
|
1546 |
+
|
1547 |
+
Py_GetArgcArgv()
|
1548 |
+
================
|
1549 |
+
|
1550 |
+
void Py_GetArgcArgv(int *argc, wchar_t ***argv)
|
1551 |
+
|
1552 |
+
Get the original command line arguments, before Python modified
|
1553 |
+
them.
|
1554 |
+
|
1555 |
+
See also "PyConfig.orig_argv" member.
|
1556 |
+
|
1557 |
+
|
1558 |
+
Multi-Phase Initialization Private Provisional API
|
1559 |
+
==================================================
|
1560 |
+
|
1561 |
+
This section is a private provisional API introducing multi-phase
|
1562 |
+
initialization, the core feature of **PEP 432**:
|
1563 |
+
|
1564 |
+
* "Core" initialization phase, "bare minimum Python":
|
1565 |
+
|
1566 |
+
* Builtin types;
|
1567 |
+
|
1568 |
+
* Builtin exceptions;
|
1569 |
+
|
1570 |
+
* Builtin and frozen modules;
|
1571 |
+
|
1572 |
+
* The "sys" module is only partially initialized (ex: "sys.path"
|
1573 |
+
doesn't exist yet).
|
1574 |
+
|
1575 |
+
* "Main" initialization phase, Python is fully initialized:
|
1576 |
+
|
1577 |
+
* Install and configure "importlib";
|
1578 |
+
|
1579 |
+
* Apply the Path Configuration;
|
1580 |
+
|
1581 |
+
* Install signal handlers;
|
1582 |
+
|
1583 |
+
* Finish "sys" module initialization (ex: create "sys.stdout" and
|
1584 |
+
"sys.path");
|
1585 |
+
|
1586 |
+
* Enable optional features like "faulthandler" and "tracemalloc";
|
1587 |
+
|
1588 |
+
* Import the "site" module;
|
1589 |
+
|
1590 |
+
* etc.
|
1591 |
+
|
1592 |
+
Private provisional API:
|
1593 |
+
|
1594 |
+
* "PyConfig._init_main": if set to "0", "Py_InitializeFromConfig()"
|
1595 |
+
stops at the "Core" initialization phase.
|
1596 |
+
|
1597 |
+
PyStatus _Py_InitializeMain(void)
|
1598 |
+
|
1599 |
+
Move to the "Main" initialization phase, finish the Python
|
1600 |
+
initialization.
|
1601 |
+
|
1602 |
+
No module is imported during the "Core" phase and the "importlib"
|
1603 |
+
module is not configured: the Path Configuration is only applied
|
1604 |
+
during the "Main" phase. It may allow to customize Python in Python to
|
1605 |
+
override or tune the Path Configuration, maybe install a custom
|
1606 |
+
"sys.meta_path" importer or an import hook, etc.
|
1607 |
+
|
1608 |
+
It may become possible to calculate the Path Configuration in Python,
|
1609 |
+
after the Core phase and before the Main phase, which is one of the
|
1610 |
+
**PEP 432** motivation.
|
1611 |
+
|
1612 |
+
The "Core" phase is not properly defined: what should be and what
|
1613 |
+
should not be available at this phase is not specified yet. The API is
|
1614 |
+
marked as private and provisional: the API can be modified or even be
|
1615 |
+
removed anytime until a proper public API is designed.
|
1616 |
+
|
1617 |
+
Example running Python code between "Core" and "Main" initialization
|
1618 |
+
phases:
|
1619 |
+
|
1620 |
+
void init_python(void)
|
1621 |
+
{
|
1622 |
+
PyStatus status;
|
1623 |
+
|
1624 |
+
PyConfig config;
|
1625 |
+
PyConfig_InitPythonConfig(&config);
|
1626 |
+
config._init_main = 0;
|
1627 |
+
|
1628 |
+
/* ... customize 'config' configuration ... */
|
1629 |
+
|
1630 |
+
status = Py_InitializeFromConfig(&config);
|
1631 |
+
PyConfig_Clear(&config);
|
1632 |
+
if (PyStatus_Exception(status)) {
|
1633 |
+
Py_ExitStatusException(status);
|
1634 |
+
}
|
1635 |
+
|
1636 |
+
/* Use sys.stderr because sys.stdout is only created
|
1637 |
+
by _Py_InitializeMain() */
|
1638 |
+
int res = PyRun_SimpleString(
|
1639 |
+
"import sys; "
|
1640 |
+
"print('Run Python code before _Py_InitializeMain', "
|
1641 |
+
"file=sys.stderr)");
|
1642 |
+
if (res < 0) {
|
1643 |
+
exit(1);
|
1644 |
+
}
|
1645 |
+
|
1646 |
+
/* ... put more configuration code here ... */
|
1647 |
+
|
1648 |
+
status = _Py_InitializeMain();
|
1649 |
+
if (PyStatus_Exception(status)) {
|
1650 |
+
Py_ExitStatusException(status);
|
1651 |
+
}
|
1652 |
+
}
|
python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/intro.txt
ADDED
@@ -0,0 +1,784 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Introduction
|
2 |
+
************
|
3 |
+
|
4 |
+
The Application Programmer's Interface to Python gives C and C++
|
5 |
+
programmers access to the Python interpreter at a variety of levels.
|
6 |
+
The API is equally usable from C++, but for brevity it is generally
|
7 |
+
referred to as the Python/C API. There are two fundamentally
|
8 |
+
different reasons for using the Python/C API. The first reason is to
|
9 |
+
write *extension modules* for specific purposes; these are C modules
|
10 |
+
that extend the Python interpreter. This is probably the most common
|
11 |
+
use. The second reason is to use Python as a component in a larger
|
12 |
+
application; this technique is generally referred to as *embedding*
|
13 |
+
Python in an application.
|
14 |
+
|
15 |
+
Writing an extension module is a relatively well-understood process,
|
16 |
+
where a "cookbook" approach works well. There are several tools that
|
17 |
+
automate the process to some extent. While people have embedded
|
18 |
+
Python in other applications since its early existence, the process of
|
19 |
+
embedding Python is less straightforward than writing an extension.
|
20 |
+
|
21 |
+
Many API functions are useful independent of whether you're embedding
|
22 |
+
or extending Python; moreover, most applications that embed Python
|
23 |
+
will need to provide a custom extension as well, so it's probably a
|
24 |
+
good idea to become familiar with writing an extension before
|
25 |
+
attempting to embed Python in a real application.
|
26 |
+
|
27 |
+
|
28 |
+
Coding standards
|
29 |
+
================
|
30 |
+
|
31 |
+
If you're writing C code for inclusion in CPython, you **must** follow
|
32 |
+
the guidelines and standards defined in **PEP 7**. These guidelines
|
33 |
+
apply regardless of the version of Python you are contributing to.
|
34 |
+
Following these conventions is not necessary for your own third party
|
35 |
+
extension modules, unless you eventually expect to contribute them to
|
36 |
+
Python.
|
37 |
+
|
38 |
+
|
39 |
+
Include Files
|
40 |
+
=============
|
41 |
+
|
42 |
+
All function, type and macro definitions needed to use the Python/C
|
43 |
+
API are included in your code by the following line:
|
44 |
+
|
45 |
+
#define PY_SSIZE_T_CLEAN
|
46 |
+
#include <Python.h>
|
47 |
+
|
48 |
+
This implies inclusion of the following standard headers: "<stdio.h>",
|
49 |
+
"<string.h>", "<errno.h>", "<limits.h>", "<assert.h>" and "<stdlib.h>"
|
50 |
+
(if available).
|
51 |
+
|
52 |
+
Note:
|
53 |
+
|
54 |
+
Since Python may define some pre-processor definitions which affect
|
55 |
+
the standard headers on some systems, you *must* include "Python.h"
|
56 |
+
before any standard headers are included.It is recommended to always
|
57 |
+
define "PY_SSIZE_T_CLEAN" before including "Python.h". See Parsing
|
58 |
+
arguments and building values for a description of this macro.
|
59 |
+
|
60 |
+
All user visible names defined by Python.h (except those defined by
|
61 |
+
the included standard headers) have one of the prefixes "Py" or "_Py".
|
62 |
+
Names beginning with "_Py" are for internal use by the Python
|
63 |
+
implementation and should not be used by extension writers. Structure
|
64 |
+
member names do not have a reserved prefix.
|
65 |
+
|
66 |
+
Note:
|
67 |
+
|
68 |
+
User code should never define names that begin with "Py" or "_Py".
|
69 |
+
This confuses the reader, and jeopardizes the portability of the
|
70 |
+
user code to future Python versions, which may define additional
|
71 |
+
names beginning with one of these prefixes.
|
72 |
+
|
73 |
+
The header files are typically installed with Python. On Unix, these
|
74 |
+
are located in the directories "*prefix*/include/pythonversion/" and
|
75 |
+
"*exec_prefix*/include/pythonversion/", where "prefix" and
|
76 |
+
"exec_prefix" are defined by the corresponding parameters to Python's
|
77 |
+
**configure** script and *version* is "'%d.%d' %
|
78 |
+
sys.version_info[:2]". On Windows, the headers are installed in
|
79 |
+
"*prefix*/include", where "prefix" is the installation directory
|
80 |
+
specified to the installer.
|
81 |
+
|
82 |
+
To include the headers, place both directories (if different) on your
|
83 |
+
compiler's search path for includes. Do *not* place the parent
|
84 |
+
directories on the search path and then use "#include
|
85 |
+
<pythonX.Y/Python.h>"; this will break on multi-platform builds since
|
86 |
+
the platform independent headers under "prefix" include the platform
|
87 |
+
specific headers from "exec_prefix".
|
88 |
+
|
89 |
+
C++ users should note that although the API is defined entirely using
|
90 |
+
C, the header files properly declare the entry points to be "extern
|
91 |
+
"C"". As a result, there is no need to do anything special to use the
|
92 |
+
API from C++.
|
93 |
+
|
94 |
+
|
95 |
+
Useful macros
|
96 |
+
=============
|
97 |
+
|
98 |
+
Several useful macros are defined in the Python header files. Many
|
99 |
+
are defined closer to where they are useful (e.g. "Py_RETURN_NONE").
|
100 |
+
Others of a more general utility are defined here. This is not
|
101 |
+
necessarily a complete listing.
|
102 |
+
|
103 |
+
PyMODINIT_FUNC
|
104 |
+
|
105 |
+
Declare an extension module "PyInit" initialization function. The
|
106 |
+
function return type is PyObject*. The macro declares any special
|
107 |
+
linkage declarations required by the platform, and for C++ declares
|
108 |
+
the function as "extern "C"".
|
109 |
+
|
110 |
+
The initialization function must be named "PyInit_*name*", where
|
111 |
+
*name* is the name of the module, and should be the only
|
112 |
+
non-"static" item defined in the module file. Example:
|
113 |
+
|
114 |
+
static struct PyModuleDef spam_module = {
|
115 |
+
PyModuleDef_HEAD_INIT,
|
116 |
+
.m_name = "spam",
|
117 |
+
...
|
118 |
+
};
|
119 |
+
|
120 |
+
PyMODINIT_FUNC
|
121 |
+
PyInit_spam(void)
|
122 |
+
{
|
123 |
+
return PyModule_Create(&spam_module);
|
124 |
+
}
|
125 |
+
|
126 |
+
Py_ABS(x)
|
127 |
+
|
128 |
+
Return the absolute value of "x".
|
129 |
+
|
130 |
+
Added in version 3.3.
|
131 |
+
|
132 |
+
Py_ALWAYS_INLINE
|
133 |
+
|
134 |
+
Ask the compiler to always inline a static inline function. The
|
135 |
+
compiler can ignore it and decides to not inline the function.
|
136 |
+
|
137 |
+
It can be used to inline performance critical static inline
|
138 |
+
functions when building Python in debug mode with function inlining
|
139 |
+
disabled. For example, MSC disables function inlining when building
|
140 |
+
in debug mode.
|
141 |
+
|
142 |
+
Marking blindly a static inline function with Py_ALWAYS_INLINE can
|
143 |
+
result in worse performances (due to increased code size for
|
144 |
+
example). The compiler is usually smarter than the developer for
|
145 |
+
the cost/benefit analysis.
|
146 |
+
|
147 |
+
If Python is built in debug mode (if the "Py_DEBUG" macro is
|
148 |
+
defined), the "Py_ALWAYS_INLINE" macro does nothing.
|
149 |
+
|
150 |
+
It must be specified before the function return type. Usage:
|
151 |
+
|
152 |
+
static inline Py_ALWAYS_INLINE int random(void) { return 4; }
|
153 |
+
|
154 |
+
Added in version 3.11.
|
155 |
+
|
156 |
+
Py_CHARMASK(c)
|
157 |
+
|
158 |
+
Argument must be a character or an integer in the range [-128, 127]
|
159 |
+
or [0, 255]. This macro returns "c" cast to an "unsigned char".
|
160 |
+
|
161 |
+
Py_DEPRECATED(version)
|
162 |
+
|
163 |
+
Use this for deprecated declarations. The macro must be placed
|
164 |
+
before the symbol name.
|
165 |
+
|
166 |
+
Example:
|
167 |
+
|
168 |
+
Py_DEPRECATED(3.8) PyAPI_FUNC(int) Py_OldFunction(void);
|
169 |
+
|
170 |
+
Changed in version 3.8: MSVC support was added.
|
171 |
+
|
172 |
+
Py_GETENV(s)
|
173 |
+
|
174 |
+
Like "getenv(s)", but returns "NULL" if "-E" was passed on the
|
175 |
+
command line (see "PyConfig.use_environment").
|
176 |
+
|
177 |
+
Py_MAX(x, y)
|
178 |
+
|
179 |
+
Return the maximum value between "x" and "y".
|
180 |
+
|
181 |
+
Added in version 3.3.
|
182 |
+
|
183 |
+
Py_MEMBER_SIZE(type, member)
|
184 |
+
|
185 |
+
Return the size of a structure ("type") "member" in bytes.
|
186 |
+
|
187 |
+
Added in version 3.6.
|
188 |
+
|
189 |
+
Py_MIN(x, y)
|
190 |
+
|
191 |
+
Return the minimum value between "x" and "y".
|
192 |
+
|
193 |
+
Added in version 3.3.
|
194 |
+
|
195 |
+
Py_NO_INLINE
|
196 |
+
|
197 |
+
Disable inlining on a function. For example, it reduces the C stack
|
198 |
+
consumption: useful on LTO+PGO builds which heavily inline code
|
199 |
+
(see bpo-33720).
|
200 |
+
|
201 |
+
Usage:
|
202 |
+
|
203 |
+
Py_NO_INLINE static int random(void) { return 4; }
|
204 |
+
|
205 |
+
Added in version 3.11.
|
206 |
+
|
207 |
+
Py_STRINGIFY(x)
|
208 |
+
|
209 |
+
Convert "x" to a C string. E.g. "Py_STRINGIFY(123)" returns
|
210 |
+
""123"".
|
211 |
+
|
212 |
+
Added in version 3.4.
|
213 |
+
|
214 |
+
Py_UNREACHABLE()
|
215 |
+
|
216 |
+
Use this when you have a code path that cannot be reached by
|
217 |
+
design. For example, in the "default:" clause in a "switch"
|
218 |
+
statement for which all possible values are covered in "case"
|
219 |
+
statements. Use this in places where you might be tempted to put
|
220 |
+
an "assert(0)" or "abort()" call.
|
221 |
+
|
222 |
+
In release mode, the macro helps the compiler to optimize the code,
|
223 |
+
and avoids a warning about unreachable code. For example, the
|
224 |
+
macro is implemented with "__builtin_unreachable()" on GCC in
|
225 |
+
release mode.
|
226 |
+
|
227 |
+
A use for "Py_UNREACHABLE()" is following a call a function that
|
228 |
+
never returns but that is not declared "_Py_NO_RETURN".
|
229 |
+
|
230 |
+
If a code path is very unlikely code but can be reached under
|
231 |
+
exceptional case, this macro must not be used. For example, under
|
232 |
+
low memory condition or if a system call returns a value out of the
|
233 |
+
expected range. In this case, it's better to report the error to
|
234 |
+
the caller. If the error cannot be reported to caller,
|
235 |
+
"Py_FatalError()" can be used.
|
236 |
+
|
237 |
+
Added in version 3.7.
|
238 |
+
|
239 |
+
Py_UNUSED(arg)
|
240 |
+
|
241 |
+
Use this for unused arguments in a function definition to silence
|
242 |
+
compiler warnings. Example: "int func(int a, int Py_UNUSED(b)) {
|
243 |
+
return a; }".
|
244 |
+
|
245 |
+
Added in version 3.4.
|
246 |
+
|
247 |
+
PyDoc_STRVAR(name, str)
|
248 |
+
|
249 |
+
Creates a variable with name "name" that can be used in docstrings.
|
250 |
+
If Python is built without docstrings, the value will be empty.
|
251 |
+
|
252 |
+
Use "PyDoc_STRVAR" for docstrings to support building Python
|
253 |
+
without docstrings, as specified in **PEP 7**.
|
254 |
+
|
255 |
+
Example:
|
256 |
+
|
257 |
+
PyDoc_STRVAR(pop_doc, "Remove and return the rightmost element.");
|
258 |
+
|
259 |
+
static PyMethodDef deque_methods[] = {
|
260 |
+
// ...
|
261 |
+
{"pop", (PyCFunction)deque_pop, METH_NOARGS, pop_doc},
|
262 |
+
// ...
|
263 |
+
}
|
264 |
+
|
265 |
+
PyDoc_STR(str)
|
266 |
+
|
267 |
+
Creates a docstring for the given input string or an empty string
|
268 |
+
if docstrings are disabled.
|
269 |
+
|
270 |
+
Use "PyDoc_STR" in specifying docstrings to support building Python
|
271 |
+
without docstrings, as specified in **PEP 7**.
|
272 |
+
|
273 |
+
Example:
|
274 |
+
|
275 |
+
static PyMethodDef pysqlite_row_methods[] = {
|
276 |
+
{"keys", (PyCFunction)pysqlite_row_keys, METH_NOARGS,
|
277 |
+
PyDoc_STR("Returns the keys of the row.")},
|
278 |
+
{NULL, NULL}
|
279 |
+
};
|
280 |
+
|
281 |
+
|
282 |
+
Objects, Types and Reference Counts
|
283 |
+
===================================
|
284 |
+
|
285 |
+
Most Python/C API functions have one or more arguments as well as a
|
286 |
+
return value of type PyObject*. This type is a pointer to an opaque
|
287 |
+
data type representing an arbitrary Python object. Since all Python
|
288 |
+
object types are treated the same way by the Python language in most
|
289 |
+
situations (e.g., assignments, scope rules, and argument passing), it
|
290 |
+
is only fitting that they should be represented by a single C type.
|
291 |
+
Almost all Python objects live on the heap: you never declare an
|
292 |
+
automatic or static variable of type "PyObject", only pointer
|
293 |
+
variables of type PyObject* can be declared. The sole exception are
|
294 |
+
the type objects; since these must never be deallocated, they are
|
295 |
+
typically static "PyTypeObject" objects.
|
296 |
+
|
297 |
+
All Python objects (even Python integers) have a *type* and a
|
298 |
+
*reference count*. An object's type determines what kind of object it
|
299 |
+
is (e.g., an integer, a list, or a user-defined function; there are
|
300 |
+
many more as explained in The standard type hierarchy). For each of
|
301 |
+
the well-known types there is a macro to check whether an object is of
|
302 |
+
that type; for instance, "PyList_Check(a)" is true if (and only if)
|
303 |
+
the object pointed to by *a* is a Python list.
|
304 |
+
|
305 |
+
|
306 |
+
Reference Counts
|
307 |
+
----------------
|
308 |
+
|
309 |
+
The reference count is important because today's computers have a
|
310 |
+
finite (and often severely limited) memory size; it counts how many
|
311 |
+
different places there are that have a *strong reference* to an
|
312 |
+
object. Such a place could be another object, or a global (or static)
|
313 |
+
C variable, or a local variable in some C function. When the last
|
314 |
+
*strong reference* to an object is released (i.e. its reference count
|
315 |
+
becomes zero), the object is deallocated. If it contains references to
|
316 |
+
other objects, those references are released. Those other objects may
|
317 |
+
be deallocated in turn, if there are no more references to them, and
|
318 |
+
so on. (There's an obvious problem with objects that reference each
|
319 |
+
other here; for now, the solution is "don't do that.")
|
320 |
+
|
321 |
+
Reference counts are always manipulated explicitly. The normal way is
|
322 |
+
to use the macro "Py_INCREF()" to take a new reference to an object
|
323 |
+
(i.e. increment its reference count by one), and "Py_DECREF()" to
|
324 |
+
release that reference (i.e. decrement the reference count by one).
|
325 |
+
The "Py_DECREF()" macro is considerably more complex than the incref
|
326 |
+
one, since it must check whether the reference count becomes zero and
|
327 |
+
then cause the object's deallocator to be called. The deallocator is
|
328 |
+
a function pointer contained in the object's type structure. The
|
329 |
+
type-specific deallocator takes care of releasing references for other
|
330 |
+
objects contained in the object if this is a compound object type,
|
331 |
+
such as a list, as well as performing any additional finalization
|
332 |
+
that's needed. There's no chance that the reference count can
|
333 |
+
overflow; at least as many bits are used to hold the reference count
|
334 |
+
as there are distinct memory locations in virtual memory (assuming
|
335 |
+
"sizeof(Py_ssize_t) >= sizeof(void*)"). Thus, the reference count
|
336 |
+
increment is a simple operation.
|
337 |
+
|
338 |
+
It is not necessary to hold a *strong reference* (i.e. increment the
|
339 |
+
reference count) for every local variable that contains a pointer to
|
340 |
+
an object. In theory, the object's reference count goes up by one
|
341 |
+
when the variable is made to point to it and it goes down by one when
|
342 |
+
the variable goes out of scope. However, these two cancel each other
|
343 |
+
out, so at the end the reference count hasn't changed. The only real
|
344 |
+
reason to use the reference count is to prevent the object from being
|
345 |
+
deallocated as long as our variable is pointing to it. If we know
|
346 |
+
that there is at least one other reference to the object that lives
|
347 |
+
at least as long as our variable, there is no need to take a new
|
348 |
+
*strong reference* (i.e. increment the reference count) temporarily.
|
349 |
+
An important situation where this arises is in objects that are
|
350 |
+
passed as arguments to C functions in an extension module that are
|
351 |
+
called from Python; the call mechanism guarantees to hold a reference
|
352 |
+
to every argument for the duration of the call.
|
353 |
+
|
354 |
+
However, a common pitfall is to extract an object from a list and hold
|
355 |
+
on to it for a while without taking a new reference. Some other
|
356 |
+
operation might conceivably remove the object from the list, releasing
|
357 |
+
that reference, and possibly deallocating it. The real danger is that
|
358 |
+
innocent-looking operations may invoke arbitrary Python code which
|
359 |
+
could do this; there is a code path which allows control to flow back
|
360 |
+
to the user from a "Py_DECREF()", so almost any operation is
|
361 |
+
potentially dangerous.
|
362 |
+
|
363 |
+
A safe approach is to always use the generic operations (functions
|
364 |
+
whose name begins with "PyObject_", "PyNumber_", "PySequence_" or
|
365 |
+
"PyMapping_"). These operations always create a new *strong reference*
|
366 |
+
(i.e. increment the reference count) of the object they return. This
|
367 |
+
leaves the caller with the responsibility to call "Py_DECREF()" when
|
368 |
+
they are done with the result; this soon becomes second nature.
|
369 |
+
|
370 |
+
|
371 |
+
Reference Count Details
|
372 |
+
~~~~~~~~~~~~~~~~~~~~~~~
|
373 |
+
|
374 |
+
The reference count behavior of functions in the Python/C API is best
|
375 |
+
explained in terms of *ownership of references*. Ownership pertains
|
376 |
+
to references, never to objects (objects are not owned: they are
|
377 |
+
always shared). "Owning a reference" means being responsible for
|
378 |
+
calling Py_DECREF on it when the reference is no longer needed.
|
379 |
+
Ownership can also be transferred, meaning that the code that receives
|
380 |
+
ownership of the reference then becomes responsible for eventually
|
381 |
+
releasing it by calling "Py_DECREF()" or "Py_XDECREF()" when it's no
|
382 |
+
longer needed---or passing on this responsibility (usually to its
|
383 |
+
caller). When a function passes ownership of a reference on to its
|
384 |
+
caller, the caller is said to receive a *new* reference. When no
|
385 |
+
ownership is transferred, the caller is said to *borrow* the
|
386 |
+
reference. Nothing needs to be done for a *borrowed reference*.
|
387 |
+
|
388 |
+
Conversely, when a calling function passes in a reference to an
|
389 |
+
object, there are two possibilities: the function *steals* a
|
390 |
+
reference to the object, or it does not. *Stealing a reference* means
|
391 |
+
that when you pass a reference to a function, that function assumes
|
392 |
+
that it now owns that reference, and you are not responsible for it
|
393 |
+
any longer.
|
394 |
+
|
395 |
+
Few functions steal references; the two notable exceptions are
|
396 |
+
"PyList_SetItem()" and "PyTuple_SetItem()", which steal a reference
|
397 |
+
to the item (but not to the tuple or list into which the item is
|
398 |
+
put!). These functions were designed to steal a reference because of
|
399 |
+
a common idiom for populating a tuple or list with newly created
|
400 |
+
objects; for example, the code to create the tuple "(1, 2, "three")"
|
401 |
+
could look like this (forgetting about error handling for the moment;
|
402 |
+
a better way to code this is shown below):
|
403 |
+
|
404 |
+
PyObject *t;
|
405 |
+
|
406 |
+
t = PyTuple_New(3);
|
407 |
+
PyTuple_SetItem(t, 0, PyLong_FromLong(1L));
|
408 |
+
PyTuple_SetItem(t, 1, PyLong_FromLong(2L));
|
409 |
+
PyTuple_SetItem(t, 2, PyUnicode_FromString("three"));
|
410 |
+
|
411 |
+
Here, "PyLong_FromLong()" returns a new reference which is immediately
|
412 |
+
stolen by "PyTuple_SetItem()". When you want to keep using an object
|
413 |
+
although the reference to it will be stolen, use "Py_INCREF()" to grab
|
414 |
+
another reference before calling the reference-stealing function.
|
415 |
+
|
416 |
+
Incidentally, "PyTuple_SetItem()" is the *only* way to set tuple
|
417 |
+
items; "PySequence_SetItem()" and "PyObject_SetItem()" refuse to do
|
418 |
+
this since tuples are an immutable data type. You should only use
|
419 |
+
"PyTuple_SetItem()" for tuples that you are creating yourself.
|
420 |
+
|
421 |
+
Equivalent code for populating a list can be written using
|
422 |
+
"PyList_New()" and "PyList_SetItem()".
|
423 |
+
|
424 |
+
However, in practice, you will rarely use these ways of creating and
|
425 |
+
populating a tuple or list. There's a generic function,
|
426 |
+
"Py_BuildValue()", that can create most common objects from C values,
|
427 |
+
directed by a *format string*. For example, the above two blocks of
|
428 |
+
code could be replaced by the following (which also takes care of the
|
429 |
+
error checking):
|
430 |
+
|
431 |
+
PyObject *tuple, *list;
|
432 |
+
|
433 |
+
tuple = Py_BuildValue("(iis)", 1, 2, "three");
|
434 |
+
list = Py_BuildValue("[iis]", 1, 2, "three");
|
435 |
+
|
436 |
+
It is much more common to use "PyObject_SetItem()" and friends with
|
437 |
+
items whose references you are only borrowing, like arguments that
|
438 |
+
were passed in to the function you are writing. In that case, their
|
439 |
+
behaviour regarding references is much saner, since you don't have to
|
440 |
+
take a new reference just so you can give that reference away ("have
|
441 |
+
it be stolen"). For example, this function sets all items of a list
|
442 |
+
(actually, any mutable sequence) to a given item:
|
443 |
+
|
444 |
+
int
|
445 |
+
set_all(PyObject *target, PyObject *item)
|
446 |
+
{
|
447 |
+
Py_ssize_t i, n;
|
448 |
+
|
449 |
+
n = PyObject_Length(target);
|
450 |
+
if (n < 0)
|
451 |
+
return -1;
|
452 |
+
for (i = 0; i < n; i++) {
|
453 |
+
PyObject *index = PyLong_FromSsize_t(i);
|
454 |
+
if (!index)
|
455 |
+
return -1;
|
456 |
+
if (PyObject_SetItem(target, index, item) < 0) {
|
457 |
+
Py_DECREF(index);
|
458 |
+
return -1;
|
459 |
+
}
|
460 |
+
Py_DECREF(index);
|
461 |
+
}
|
462 |
+
return 0;
|
463 |
+
}
|
464 |
+
|
465 |
+
The situation is slightly different for function return values.
|
466 |
+
While passing a reference to most functions does not change your
|
467 |
+
ownership responsibilities for that reference, many functions that
|
468 |
+
return a reference to an object give you ownership of the reference.
|
469 |
+
The reason is simple: in many cases, the returned object is created
|
470 |
+
on the fly, and the reference you get is the only reference to the
|
471 |
+
object. Therefore, the generic functions that return object
|
472 |
+
references, like "PyObject_GetItem()" and "PySequence_GetItem()",
|
473 |
+
always return a new reference (the caller becomes the owner of the
|
474 |
+
reference).
|
475 |
+
|
476 |
+
It is important to realize that whether you own a reference returned
|
477 |
+
by a function depends on which function you call only --- *the
|
478 |
+
plumage* (the type of the object passed as an argument to the
|
479 |
+
function) *doesn't enter into it!* Thus, if you extract an item from
|
480 |
+
a list using "PyList_GetItem()", you don't own the reference --- but
|
481 |
+
if you obtain the same item from the same list using
|
482 |
+
"PySequence_GetItem()" (which happens to take exactly the same
|
483 |
+
arguments), you do own a reference to the returned object.
|
484 |
+
|
485 |
+
Here is an example of how you could write a function that computes the
|
486 |
+
sum of the items in a list of integers; once using
|
487 |
+
"PyList_GetItem()", and once using "PySequence_GetItem()".
|
488 |
+
|
489 |
+
long
|
490 |
+
sum_list(PyObject *list)
|
491 |
+
{
|
492 |
+
Py_ssize_t i, n;
|
493 |
+
long total = 0, value;
|
494 |
+
PyObject *item;
|
495 |
+
|
496 |
+
n = PyList_Size(list);
|
497 |
+
if (n < 0)
|
498 |
+
return -1; /* Not a list */
|
499 |
+
for (i = 0; i < n; i++) {
|
500 |
+
item = PyList_GetItem(list, i); /* Can't fail */
|
501 |
+
if (!PyLong_Check(item)) continue; /* Skip non-integers */
|
502 |
+
value = PyLong_AsLong(item);
|
503 |
+
if (value == -1 && PyErr_Occurred())
|
504 |
+
/* Integer too big to fit in a C long, bail out */
|
505 |
+
return -1;
|
506 |
+
total += value;
|
507 |
+
}
|
508 |
+
return total;
|
509 |
+
}
|
510 |
+
|
511 |
+
long
|
512 |
+
sum_sequence(PyObject *sequence)
|
513 |
+
{
|
514 |
+
Py_ssize_t i, n;
|
515 |
+
long total = 0, value;
|
516 |
+
PyObject *item;
|
517 |
+
n = PySequence_Length(sequence);
|
518 |
+
if (n < 0)
|
519 |
+
return -1; /* Has no length */
|
520 |
+
for (i = 0; i < n; i++) {
|
521 |
+
item = PySequence_GetItem(sequence, i);
|
522 |
+
if (item == NULL)
|
523 |
+
return -1; /* Not a sequence, or other failure */
|
524 |
+
if (PyLong_Check(item)) {
|
525 |
+
value = PyLong_AsLong(item);
|
526 |
+
Py_DECREF(item);
|
527 |
+
if (value == -1 && PyErr_Occurred())
|
528 |
+
/* Integer too big to fit in a C long, bail out */
|
529 |
+
return -1;
|
530 |
+
total += value;
|
531 |
+
}
|
532 |
+
else {
|
533 |
+
Py_DECREF(item); /* Discard reference ownership */
|
534 |
+
}
|
535 |
+
}
|
536 |
+
return total;
|
537 |
+
}
|
538 |
+
|
539 |
+
|
540 |
+
Types
|
541 |
+
-----
|
542 |
+
|
543 |
+
There are few other data types that play a significant role in the
|
544 |
+
Python/C API; most are simple C types such as int, long, double and
|
545 |
+
char*. A few structure types are used to describe static tables used
|
546 |
+
to list the functions exported by a module or the data attributes of
|
547 |
+
a new object type, and another is used to describe the value of a
|
548 |
+
complex number. These will be discussed together with the functions
|
549 |
+
that use them.
|
550 |
+
|
551 |
+
type Py_ssize_t
|
552 |
+
* Part of the Stable ABI.*
|
553 |
+
|
554 |
+
A signed integral type such that "sizeof(Py_ssize_t) ==
|
555 |
+
sizeof(size_t)". C99 doesn't define such a thing directly (size_t
|
556 |
+
is an unsigned integral type). See **PEP 353** for details.
|
557 |
+
"PY_SSIZE_T_MAX" is the largest positive value of type
|
558 |
+
"Py_ssize_t".
|
559 |
+
|
560 |
+
|
561 |
+
Exceptions
|
562 |
+
==========
|
563 |
+
|
564 |
+
The Python programmer only needs to deal with exceptions if specific
|
565 |
+
error handling is required; unhandled exceptions are automatically
|
566 |
+
propagated to the caller, then to the caller's caller, and so on,
|
567 |
+
until they reach the top-level interpreter, where they are reported to
|
568 |
+
the user accompanied by a stack traceback.
|
569 |
+
|
570 |
+
For C programmers, however, error checking always has to be explicit.
|
571 |
+
All functions in the Python/C API can raise exceptions, unless an
|
572 |
+
explicit claim is made otherwise in a function's documentation. In
|
573 |
+
general, when a function encounters an error, it sets an exception,
|
574 |
+
discards any object references that it owns, and returns an error
|
575 |
+
indicator. If not documented otherwise, this indicator is either
|
576 |
+
"NULL" or "-1", depending on the function's return type. A few
|
577 |
+
functions return a Boolean true/false result, with false indicating an
|
578 |
+
error. Very few functions return no explicit error indicator or have
|
579 |
+
an ambiguous return value, and require explicit testing for errors
|
580 |
+
with "PyErr_Occurred()". These exceptions are always explicitly
|
581 |
+
documented.
|
582 |
+
|
583 |
+
Exception state is maintained in per-thread storage (this is
|
584 |
+
equivalent to using global storage in an unthreaded application). A
|
585 |
+
thread can be in one of two states: an exception has occurred, or not.
|
586 |
+
The function "PyErr_Occurred()" can be used to check for this: it
|
587 |
+
returns a borrowed reference to the exception type object when an
|
588 |
+
exception has occurred, and "NULL" otherwise. There are a number of
|
589 |
+
functions to set the exception state: "PyErr_SetString()" is the most
|
590 |
+
common (though not the most general) function to set the exception
|
591 |
+
state, and "PyErr_Clear()" clears the exception state.
|
592 |
+
|
593 |
+
The full exception state consists of three objects (all of which can
|
594 |
+
be "NULL"): the exception type, the corresponding exception value,
|
595 |
+
and the traceback. These have the same meanings as the Python result
|
596 |
+
of "sys.exc_info()"; however, they are not the same: the Python
|
597 |
+
objects represent the last exception being handled by a Python "try"
|
598 |
+
... "except" statement, while the C level exception state only exists
|
599 |
+
while an exception is being passed on between C functions until it
|
600 |
+
reaches the Python bytecode interpreter's main loop, which takes care
|
601 |
+
of transferring it to "sys.exc_info()" and friends.
|
602 |
+
|
603 |
+
Note that starting with Python 1.5, the preferred, thread-safe way to
|
604 |
+
access the exception state from Python code is to call the function
|
605 |
+
"sys.exc_info()", which returns the per-thread exception state for
|
606 |
+
Python code. Also, the semantics of both ways to access the exception
|
607 |
+
state have changed so that a function which catches an exception will
|
608 |
+
save and restore its thread's exception state so as to preserve the
|
609 |
+
exception state of its caller. This prevents common bugs in exception
|
610 |
+
handling code caused by an innocent-looking function overwriting the
|
611 |
+
exception being handled; it also reduces the often unwanted lifetime
|
612 |
+
extension for objects that are referenced by the stack frames in the
|
613 |
+
traceback.
|
614 |
+
|
615 |
+
As a general principle, a function that calls another function to
|
616 |
+
perform some task should check whether the called function raised an
|
617 |
+
exception, and if so, pass the exception state on to its caller. It
|
618 |
+
should discard any object references that it owns, and return an
|
619 |
+
error indicator, but it should *not* set another exception --- that
|
620 |
+
would overwrite the exception that was just raised, and lose important
|
621 |
+
information about the exact cause of the error.
|
622 |
+
|
623 |
+
A simple example of detecting exceptions and passing them on is shown
|
624 |
+
in the "sum_sequence()" example above. It so happens that this
|
625 |
+
example doesn't need to clean up any owned references when it detects
|
626 |
+
an error. The following example function shows some error cleanup.
|
627 |
+
First, to remind you why you like Python, we show the equivalent
|
628 |
+
Python code:
|
629 |
+
|
630 |
+
def incr_item(dict, key):
|
631 |
+
try:
|
632 |
+
item = dict[key]
|
633 |
+
except KeyError:
|
634 |
+
item = 0
|
635 |
+
dict[key] = item + 1
|
636 |
+
|
637 |
+
Here is the corresponding C code, in all its glory:
|
638 |
+
|
639 |
+
int
|
640 |
+
incr_item(PyObject *dict, PyObject *key)
|
641 |
+
{
|
642 |
+
/* Objects all initialized to NULL for Py_XDECREF */
|
643 |
+
PyObject *item = NULL, *const_one = NULL, *incremented_item = NULL;
|
644 |
+
int rv = -1; /* Return value initialized to -1 (failure) */
|
645 |
+
|
646 |
+
item = PyObject_GetItem(dict, key);
|
647 |
+
if (item == NULL) {
|
648 |
+
/* Handle KeyError only: */
|
649 |
+
if (!PyErr_ExceptionMatches(PyExc_KeyError))
|
650 |
+
goto error;
|
651 |
+
|
652 |
+
/* Clear the error and use zero: */
|
653 |
+
PyErr_Clear();
|
654 |
+
item = PyLong_FromLong(0L);
|
655 |
+
if (item == NULL)
|
656 |
+
goto error;
|
657 |
+
}
|
658 |
+
const_one = PyLong_FromLong(1L);
|
659 |
+
if (const_one == NULL)
|
660 |
+
goto error;
|
661 |
+
|
662 |
+
incremented_item = PyNumber_Add(item, const_one);
|
663 |
+
if (incremented_item == NULL)
|
664 |
+
goto error;
|
665 |
+
|
666 |
+
if (PyObject_SetItem(dict, key, incremented_item) < 0)
|
667 |
+
goto error;
|
668 |
+
rv = 0; /* Success */
|
669 |
+
/* Continue with cleanup code */
|
670 |
+
|
671 |
+
error:
|
672 |
+
/* Cleanup code, shared by success and failure path */
|
673 |
+
|
674 |
+
/* Use Py_XDECREF() to ignore NULL references */
|
675 |
+
Py_XDECREF(item);
|
676 |
+
Py_XDECREF(const_one);
|
677 |
+
Py_XDECREF(incremented_item);
|
678 |
+
|
679 |
+
return rv; /* -1 for error, 0 for success */
|
680 |
+
}
|
681 |
+
|
682 |
+
This example represents an endorsed use of the "goto" statement in C!
|
683 |
+
It illustrates the use of "PyErr_ExceptionMatches()" and
|
684 |
+
"PyErr_Clear()" to handle specific exceptions, and the use of
|
685 |
+
"Py_XDECREF()" to dispose of owned references that may be "NULL" (note
|
686 |
+
the "'X'" in the name; "Py_DECREF()" would crash when confronted with
|
687 |
+
a "NULL" reference). It is important that the variables used to hold
|
688 |
+
owned references are initialized to "NULL" for this to work; likewise,
|
689 |
+
the proposed return value is initialized to "-1" (failure) and only
|
690 |
+
set to success after the final call made is successful.
|
691 |
+
|
692 |
+
|
693 |
+
Embedding Python
|
694 |
+
================
|
695 |
+
|
696 |
+
The one important task that only embedders (as opposed to extension
|
697 |
+
writers) of the Python interpreter have to worry about is the
|
698 |
+
initialization, and possibly the finalization, of the Python
|
699 |
+
interpreter. Most functionality of the interpreter can only be used
|
700 |
+
after the interpreter has been initialized.
|
701 |
+
|
702 |
+
The basic initialization function is "Py_Initialize()". This
|
703 |
+
initializes the table of loaded modules, and creates the fundamental
|
704 |
+
modules "builtins", "__main__", and "sys". It also initializes the
|
705 |
+
module search path ("sys.path").
|
706 |
+
|
707 |
+
"Py_Initialize()" does not set the "script argument list"
|
708 |
+
("sys.argv"). If this variable is needed by Python code that will be
|
709 |
+
executed later, setting "PyConfig.argv" and "PyConfig.parse_argv" must
|
710 |
+
be set: see Python Initialization Configuration.
|
711 |
+
|
712 |
+
On most systems (in particular, on Unix and Windows, although the
|
713 |
+
details are slightly different), "Py_Initialize()" calculates the
|
714 |
+
module search path based upon its best guess for the location of the
|
715 |
+
standard Python interpreter executable, assuming that the Python
|
716 |
+
library is found in a fixed location relative to the Python
|
717 |
+
interpreter executable. In particular, it looks for a directory named
|
718 |
+
"lib/python*X.Y*" relative to the parent directory where the
|
719 |
+
executable named "python" is found on the shell command search path
|
720 |
+
(the environment variable "PATH").
|
721 |
+
|
722 |
+
For instance, if the Python executable is found in
|
723 |
+
"/usr/local/bin/python", it will assume that the libraries are in
|
724 |
+
"/usr/local/lib/python*X.Y*". (In fact, this particular path is also
|
725 |
+
the "fallback" location, used when no executable file named "python"
|
726 |
+
is found along "PATH".) The user can override this behavior by
|
727 |
+
setting the environment variable "PYTHONHOME", or insert additional
|
728 |
+
directories in front of the standard path by setting "PYTHONPATH".
|
729 |
+
|
730 |
+
The embedding application can steer the search by calling
|
731 |
+
"Py_SetProgramName(file)" *before* calling "Py_Initialize()". Note
|
732 |
+
that "PYTHONHOME" still overrides this and "PYTHONPATH" is still
|
733 |
+
inserted in front of the standard path. An application that requires
|
734 |
+
total control has to provide its own implementation of "Py_GetPath()",
|
735 |
+
"Py_GetPrefix()", "Py_GetExecPrefix()", and "Py_GetProgramFullPath()"
|
736 |
+
(all defined in "Modules/getpath.c").
|
737 |
+
|
738 |
+
Sometimes, it is desirable to "uninitialize" Python. For instance,
|
739 |
+
the application may want to start over (make another call to
|
740 |
+
"Py_Initialize()") or the application is simply done with its use of
|
741 |
+
Python and wants to free memory allocated by Python. This can be
|
742 |
+
accomplished by calling "Py_FinalizeEx()". The function
|
743 |
+
"Py_IsInitialized()" returns true if Python is currently in the
|
744 |
+
initialized state. More information about these functions is given in
|
745 |
+
a later chapter. Notice that "Py_FinalizeEx()" does *not* free all
|
746 |
+
memory allocated by the Python interpreter, e.g. memory allocated by
|
747 |
+
extension modules currently cannot be released.
|
748 |
+
|
749 |
+
|
750 |
+
Debugging Builds
|
751 |
+
================
|
752 |
+
|
753 |
+
Python can be built with several macros to enable extra checks of the
|
754 |
+
interpreter and extension modules. These checks tend to add a large
|
755 |
+
amount of overhead to the runtime so they are not enabled by default.
|
756 |
+
|
757 |
+
A full list of the various types of debugging builds is in the file
|
758 |
+
"Misc/SpecialBuilds.txt" in the Python source distribution. Builds are
|
759 |
+
available that support tracing of reference counts, debugging the
|
760 |
+
memory allocator, or low-level profiling of the main interpreter loop.
|
761 |
+
Only the most frequently used builds will be described in the
|
762 |
+
remainder of this section.
|
763 |
+
|
764 |
+
Py_DEBUG
|
765 |
+
|
766 |
+
Compiling the interpreter with the "Py_DEBUG" macro defined produces
|
767 |
+
what is generally meant by a debug build of Python. "Py_DEBUG" is
|
768 |
+
enabled in the Unix build by adding "--with-pydebug" to the
|
769 |
+
"./configure" command. It is also implied by the presence of the not-
|
770 |
+
Python-specific "_DEBUG" macro. When "Py_DEBUG" is enabled in the
|
771 |
+
Unix build, compiler optimization is disabled.
|
772 |
+
|
773 |
+
In addition to the reference count debugging described below, extra
|
774 |
+
checks are performed, see Python Debug Build.
|
775 |
+
|
776 |
+
Defining "Py_TRACE_REFS" enables reference tracing (see the "configure
|
777 |
+
--with-trace-refs option"). When defined, a circular doubly linked
|
778 |
+
list of active objects is maintained by adding two extra fields to
|
779 |
+
every "PyObject". Total allocations are tracked as well. Upon exit,
|
780 |
+
all existing references are printed. (In interactive mode this
|
781 |
+
happens after every statement run by the interpreter.)
|
782 |
+
|
783 |
+
Please refer to "Misc/SpecialBuilds.txt" in the Python source
|
784 |
+
distribution for more detailed information.
|
python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/iter.txt
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Iterator Protocol
|
2 |
+
*****************
|
3 |
+
|
4 |
+
There are two functions specifically for working with iterators.
|
5 |
+
|
6 |
+
int PyIter_Check(PyObject *o)
|
7 |
+
* Part of the Stable ABI since version 3.8.*
|
8 |
+
|
9 |
+
Return non-zero if the object *o* can be safely passed to
|
10 |
+
"PyIter_Next()", and "0" otherwise. This function always succeeds.
|
11 |
+
|
12 |
+
int PyAIter_Check(PyObject *o)
|
13 |
+
* Part of the Stable ABI since version 3.10.*
|
14 |
+
|
15 |
+
Return non-zero if the object *o* provides the "AsyncIterator"
|
16 |
+
protocol, and "0" otherwise. This function always succeeds.
|
17 |
+
|
18 |
+
Added in version 3.10.
|
19 |
+
|
20 |
+
PyObject *PyIter_Next(PyObject *o)
|
21 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
22 |
+
|
23 |
+
Return the next value from the iterator *o*. The object must be an
|
24 |
+
iterator according to "PyIter_Check()" (it is up to the caller to
|
25 |
+
check this). If there are no remaining values, returns "NULL" with
|
26 |
+
no exception set. If an error occurs while retrieving the item,
|
27 |
+
returns "NULL" and passes along the exception.
|
28 |
+
|
29 |
+
To write a loop which iterates over an iterator, the C code should
|
30 |
+
look something like this:
|
31 |
+
|
32 |
+
PyObject *iterator = PyObject_GetIter(obj);
|
33 |
+
PyObject *item;
|
34 |
+
|
35 |
+
if (iterator == NULL) {
|
36 |
+
/* propagate error */
|
37 |
+
}
|
38 |
+
|
39 |
+
while ((item = PyIter_Next(iterator))) {
|
40 |
+
/* do something with item */
|
41 |
+
...
|
42 |
+
/* release reference when done */
|
43 |
+
Py_DECREF(item);
|
44 |
+
}
|
45 |
+
|
46 |
+
Py_DECREF(iterator);
|
47 |
+
|
48 |
+
if (PyErr_Occurred()) {
|
49 |
+
/* propagate error */
|
50 |
+
}
|
51 |
+
else {
|
52 |
+
/* continue doing useful work */
|
53 |
+
}
|
54 |
+
|
55 |
+
type PySendResult
|
56 |
+
|
57 |
+
The enum value used to represent different results of
|
58 |
+
"PyIter_Send()".
|
59 |
+
|
60 |
+
Added in version 3.10.
|
61 |
+
|
62 |
+
PySendResult PyIter_Send(PyObject *iter, PyObject *arg, PyObject **presult)
|
63 |
+
* Part of the Stable ABI since version 3.10.*
|
64 |
+
|
65 |
+
Sends the *arg* value into the iterator *iter*. Returns:
|
66 |
+
|
67 |
+
* "PYGEN_RETURN" if iterator returns. Return value is returned via
|
68 |
+
*presult*.
|
69 |
+
|
70 |
+
* "PYGEN_NEXT" if iterator yields. Yielded value is returned via
|
71 |
+
*presult*.
|
72 |
+
|
73 |
+
* "PYGEN_ERROR" if iterator has raised and exception. *presult* is
|
74 |
+
set to "NULL".
|
75 |
+
|
76 |
+
Added in version 3.10.
|
python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/iterator.txt
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Iterator Objects
|
2 |
+
****************
|
3 |
+
|
4 |
+
Python provides two general-purpose iterator objects. The first, a
|
5 |
+
sequence iterator, works with an arbitrary sequence supporting the
|
6 |
+
"__getitem__()" method. The second works with a callable object and a
|
7 |
+
sentinel value, calling the callable for each item in the sequence,
|
8 |
+
and ending the iteration when the sentinel value is returned.
|
9 |
+
|
10 |
+
PyTypeObject PySeqIter_Type
|
11 |
+
* Part of the Stable ABI.*
|
12 |
+
|
13 |
+
Type object for iterator objects returned by "PySeqIter_New()" and
|
14 |
+
the one-argument form of the "iter()" built-in function for built-
|
15 |
+
in sequence types.
|
16 |
+
|
17 |
+
int PySeqIter_Check(PyObject *op)
|
18 |
+
|
19 |
+
Return true if the type of *op* is "PySeqIter_Type". This function
|
20 |
+
always succeeds.
|
21 |
+
|
22 |
+
PyObject *PySeqIter_New(PyObject *seq)
|
23 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
24 |
+
|
25 |
+
Return an iterator that works with a general sequence object,
|
26 |
+
*seq*. The iteration ends when the sequence raises "IndexError"
|
27 |
+
for the subscripting operation.
|
28 |
+
|
29 |
+
PyTypeObject PyCallIter_Type
|
30 |
+
* Part of the Stable ABI.*
|
31 |
+
|
32 |
+
Type object for iterator objects returned by "PyCallIter_New()" and
|
33 |
+
the two-argument form of the "iter()" built-in function.
|
34 |
+
|
35 |
+
int PyCallIter_Check(PyObject *op)
|
36 |
+
|
37 |
+
Return true if the type of *op* is "PyCallIter_Type". This
|
38 |
+
function always succeeds.
|
39 |
+
|
40 |
+
PyObject *PyCallIter_New(PyObject *callable, PyObject *sentinel)
|
41 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
42 |
+
|
43 |
+
Return a new iterator. The first parameter, *callable*, can be any
|
44 |
+
Python callable object that can be called with no parameters; each
|
45 |
+
call to it should return the next item in the iteration. When
|
46 |
+
*callable* returns a value equal to *sentinel*, the iteration will
|
47 |
+
be terminated.
|
python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/list.txt
ADDED
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
List Objects
|
2 |
+
************
|
3 |
+
|
4 |
+
type PyListObject
|
5 |
+
|
6 |
+
This subtype of "PyObject" represents a Python list object.
|
7 |
+
|
8 |
+
PyTypeObject PyList_Type
|
9 |
+
* Part of the Stable ABI.*
|
10 |
+
|
11 |
+
This instance of "PyTypeObject" represents the Python list type.
|
12 |
+
This is the same object as "list" in the Python layer.
|
13 |
+
|
14 |
+
int PyList_Check(PyObject *p)
|
15 |
+
|
16 |
+
Return true if *p* is a list object or an instance of a subtype of
|
17 |
+
the list type. This function always succeeds.
|
18 |
+
|
19 |
+
int PyList_CheckExact(PyObject *p)
|
20 |
+
|
21 |
+
Return true if *p* is a list object, but not an instance of a
|
22 |
+
subtype of the list type. This function always succeeds.
|
23 |
+
|
24 |
+
PyObject *PyList_New(Py_ssize_t len)
|
25 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
26 |
+
|
27 |
+
Return a new list of length *len* on success, or "NULL" on failure.
|
28 |
+
|
29 |
+
Note:
|
30 |
+
|
31 |
+
If *len* is greater than zero, the returned list object's items
|
32 |
+
are set to "NULL". Thus you cannot use abstract API functions
|
33 |
+
such as "PySequence_SetItem()" or expose the object to Python
|
34 |
+
code before setting all items to a real object with
|
35 |
+
"PyList_SetItem()".
|
36 |
+
|
37 |
+
Py_ssize_t PyList_Size(PyObject *list)
|
38 |
+
* Part of the Stable ABI.*
|
39 |
+
|
40 |
+
Return the length of the list object in *list*; this is equivalent
|
41 |
+
to "len(list)" on a list object.
|
42 |
+
|
43 |
+
Py_ssize_t PyList_GET_SIZE(PyObject *list)
|
44 |
+
|
45 |
+
Similar to "PyList_Size()", but without error checking.
|
46 |
+
|
47 |
+
PyObject *PyList_GetItem(PyObject *list, Py_ssize_t index)
|
48 |
+
*Return value: Borrowed reference.** Part of the Stable ABI.*
|
49 |
+
|
50 |
+
Return the object at position *index* in the list pointed to by
|
51 |
+
*list*. The position must be non-negative; indexing from the end
|
52 |
+
of the list is not supported. If *index* is out of bounds (<0 or
|
53 |
+
>=len(list)), return "NULL" and set an "IndexError" exception.
|
54 |
+
|
55 |
+
PyObject *PyList_GET_ITEM(PyObject *list, Py_ssize_t i)
|
56 |
+
*Return value: Borrowed reference.*
|
57 |
+
|
58 |
+
Similar to "PyList_GetItem()", but without error checking.
|
59 |
+
|
60 |
+
int PyList_SetItem(PyObject *list, Py_ssize_t index, PyObject *item)
|
61 |
+
* Part of the Stable ABI.*
|
62 |
+
|
63 |
+
Set the item at index *index* in list to *item*. Return "0" on
|
64 |
+
success. If *index* is out of bounds, return "-1" and set an
|
65 |
+
"IndexError" exception.
|
66 |
+
|
67 |
+
Note:
|
68 |
+
|
69 |
+
This function "steals" a reference to *item* and discards a
|
70 |
+
reference to an item already in the list at the affected
|
71 |
+
position.
|
72 |
+
|
73 |
+
void PyList_SET_ITEM(PyObject *list, Py_ssize_t i, PyObject *o)
|
74 |
+
|
75 |
+
Macro form of "PyList_SetItem()" without error checking. This is
|
76 |
+
normally only used to fill in new lists where there is no previous
|
77 |
+
content.
|
78 |
+
|
79 |
+
Note:
|
80 |
+
|
81 |
+
This macro "steals" a reference to *item*, and, unlike
|
82 |
+
"PyList_SetItem()", does *not* discard a reference to any item
|
83 |
+
that is being replaced; any reference in *list* at position *i*
|
84 |
+
will be leaked.
|
85 |
+
|
86 |
+
int PyList_Insert(PyObject *list, Py_ssize_t index, PyObject *item)
|
87 |
+
* Part of the Stable ABI.*
|
88 |
+
|
89 |
+
Insert the item *item* into list *list* in front of index *index*.
|
90 |
+
Return "0" if successful; return "-1" and set an exception if
|
91 |
+
unsuccessful. Analogous to "list.insert(index, item)".
|
92 |
+
|
93 |
+
int PyList_Append(PyObject *list, PyObject *item)
|
94 |
+
* Part of the Stable ABI.*
|
95 |
+
|
96 |
+
Append the object *item* at the end of list *list*. Return "0" if
|
97 |
+
successful; return "-1" and set an exception if unsuccessful.
|
98 |
+
Analogous to "list.append(item)".
|
99 |
+
|
100 |
+
PyObject *PyList_GetSlice(PyObject *list, Py_ssize_t low, Py_ssize_t high)
|
101 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
102 |
+
|
103 |
+
Return a list of the objects in *list* containing the objects
|
104 |
+
*between* *low* and *high*. Return "NULL" and set an exception if
|
105 |
+
unsuccessful. Analogous to "list[low:high]". Indexing from the
|
106 |
+
end of the list is not supported.
|
107 |
+
|
108 |
+
int PyList_SetSlice(PyObject *list, Py_ssize_t low, Py_ssize_t high, PyObject *itemlist)
|
109 |
+
* Part of the Stable ABI.*
|
110 |
+
|
111 |
+
Set the slice of *list* between *low* and *high* to the contents of
|
112 |
+
*itemlist*. Analogous to "list[low:high] = itemlist". The
|
113 |
+
*itemlist* may be "NULL", indicating the assignment of an empty
|
114 |
+
list (slice deletion). Return "0" on success, "-1" on failure.
|
115 |
+
Indexing from the end of the list is not supported.
|
116 |
+
|
117 |
+
int PyList_Sort(PyObject *list)
|
118 |
+
* Part of the Stable ABI.*
|
119 |
+
|
120 |
+
Sort the items of *list* in place. Return "0" on success, "-1" on
|
121 |
+
failure. This is equivalent to "list.sort()".
|
122 |
+
|
123 |
+
int PyList_Reverse(PyObject *list)
|
124 |
+
* Part of the Stable ABI.*
|
125 |
+
|
126 |
+
Reverse the items of *list* in place. Return "0" on success, "-1"
|
127 |
+
on failure. This is the equivalent of "list.reverse()".
|
128 |
+
|
129 |
+
PyObject *PyList_AsTuple(PyObject *list)
|
130 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
131 |
+
|
132 |
+
Return a new tuple object containing the contents of *list*;
|
133 |
+
equivalent to "tuple(list)".
|
python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/long.txt
ADDED
@@ -0,0 +1,331 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Integer Objects
|
2 |
+
***************
|
3 |
+
|
4 |
+
All integers are implemented as "long" integer objects of arbitrary
|
5 |
+
size.
|
6 |
+
|
7 |
+
On error, most "PyLong_As*" APIs return "(return type)-1" which cannot
|
8 |
+
be distinguished from a number. Use "PyErr_Occurred()" to
|
9 |
+
disambiguate.
|
10 |
+
|
11 |
+
type PyLongObject
|
12 |
+
* Part of the Limited API (as an opaque struct).*
|
13 |
+
|
14 |
+
This subtype of "PyObject" represents a Python integer object.
|
15 |
+
|
16 |
+
PyTypeObject PyLong_Type
|
17 |
+
* Part of the Stable ABI.*
|
18 |
+
|
19 |
+
This instance of "PyTypeObject" represents the Python integer type.
|
20 |
+
This is the same object as "int" in the Python layer.
|
21 |
+
|
22 |
+
int PyLong_Check(PyObject *p)
|
23 |
+
|
24 |
+
Return true if its argument is a "PyLongObject" or a subtype of
|
25 |
+
"PyLongObject". This function always succeeds.
|
26 |
+
|
27 |
+
int PyLong_CheckExact(PyObject *p)
|
28 |
+
|
29 |
+
Return true if its argument is a "PyLongObject", but not a subtype
|
30 |
+
of "PyLongObject". This function always succeeds.
|
31 |
+
|
32 |
+
PyObject *PyLong_FromLong(long v)
|
33 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
34 |
+
|
35 |
+
Return a new "PyLongObject" object from *v*, or "NULL" on failure.
|
36 |
+
|
37 |
+
The current implementation keeps an array of integer objects for
|
38 |
+
all integers between "-5" and "256". When you create an int in that
|
39 |
+
range you actually just get back a reference to the existing
|
40 |
+
object.
|
41 |
+
|
42 |
+
PyObject *PyLong_FromUnsignedLong(unsigned long v)
|
43 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
44 |
+
|
45 |
+
Return a new "PyLongObject" object from a C unsigned long, or
|
46 |
+
"NULL" on failure.
|
47 |
+
|
48 |
+
PyObject *PyLong_FromSsize_t(Py_ssize_t v)
|
49 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
50 |
+
|
51 |
+
Return a new "PyLongObject" object from a C "Py_ssize_t", or "NULL"
|
52 |
+
on failure.
|
53 |
+
|
54 |
+
PyObject *PyLong_FromSize_t(size_t v)
|
55 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
56 |
+
|
57 |
+
Return a new "PyLongObject" object from a C "size_t", or "NULL" on
|
58 |
+
failure.
|
59 |
+
|
60 |
+
PyObject *PyLong_FromLongLong(long long v)
|
61 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
62 |
+
|
63 |
+
Return a new "PyLongObject" object from a C long long, or "NULL" on
|
64 |
+
failure.
|
65 |
+
|
66 |
+
PyObject *PyLong_FromUnsignedLongLong(unsigned long long v)
|
67 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
68 |
+
|
69 |
+
Return a new "PyLongObject" object from a C unsigned long long, or
|
70 |
+
"NULL" on failure.
|
71 |
+
|
72 |
+
PyObject *PyLong_FromDouble(double v)
|
73 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
74 |
+
|
75 |
+
Return a new "PyLongObject" object from the integer part of *v*, or
|
76 |
+
"NULL" on failure.
|
77 |
+
|
78 |
+
PyObject *PyLong_FromString(const char *str, char **pend, int base)
|
79 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
80 |
+
|
81 |
+
Return a new "PyLongObject" based on the string value in *str*,
|
82 |
+
which is interpreted according to the radix in *base*, or "NULL" on
|
83 |
+
failure. If *pend* is non-"NULL", **pend* will point to the end of
|
84 |
+
*str* on success or to the first character that could not be
|
85 |
+
processed on error. If *base* is "0", *str* is interpreted using
|
86 |
+
the Integer literals definition; in this case, leading zeros in a
|
87 |
+
non-zero decimal number raises a "ValueError". If *base* is not
|
88 |
+
"0", it must be between "2" and "36", inclusive. Leading and
|
89 |
+
trailing whitespace and single underscores after a base specifier
|
90 |
+
and between digits are ignored. If there are no digits or *str* is
|
91 |
+
not NULL-terminated following the digits and trailing whitespace,
|
92 |
+
"ValueError" will be raised.
|
93 |
+
|
94 |
+
See also:
|
95 |
+
|
96 |
+
Python methods "int.to_bytes()" and "int.from_bytes()" to convert
|
97 |
+
a "PyLongObject" to/from an array of bytes in base "256". You can
|
98 |
+
call those from C using "PyObject_CallMethod()".
|
99 |
+
|
100 |
+
PyObject *PyLong_FromUnicodeObject(PyObject *u, int base)
|
101 |
+
*Return value: New reference.*
|
102 |
+
|
103 |
+
Convert a sequence of Unicode digits in the string *u* to a Python
|
104 |
+
integer value.
|
105 |
+
|
106 |
+
Added in version 3.3.
|
107 |
+
|
108 |
+
PyObject *PyLong_FromVoidPtr(void *p)
|
109 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
110 |
+
|
111 |
+
Create a Python integer from the pointer *p*. The pointer value can
|
112 |
+
be retrieved from the resulting value using "PyLong_AsVoidPtr()".
|
113 |
+
|
114 |
+
long PyLong_AsLong(PyObject *obj)
|
115 |
+
* Part of the Stable ABI.*
|
116 |
+
|
117 |
+
Return a C long representation of *obj*. If *obj* is not an
|
118 |
+
instance of "PyLongObject", first call its "__index__()" method (if
|
119 |
+
present) to convert it to a "PyLongObject".
|
120 |
+
|
121 |
+
Raise "OverflowError" if the value of *obj* is out of range for a
|
122 |
+
long.
|
123 |
+
|
124 |
+
Returns "-1" on error. Use "PyErr_Occurred()" to disambiguate.
|
125 |
+
|
126 |
+
Changed in version 3.8: Use "__index__()" if available.
|
127 |
+
|
128 |
+
Changed in version 3.10: This function will no longer use
|
129 |
+
"__int__()".
|
130 |
+
|
131 |
+
long PyLong_AsLongAndOverflow(PyObject *obj, int *overflow)
|
132 |
+
* Part of the Stable ABI.*
|
133 |
+
|
134 |
+
Return a C long representation of *obj*. If *obj* is not an
|
135 |
+
instance of "PyLongObject", first call its "__index__()" method (if
|
136 |
+
present) to convert it to a "PyLongObject".
|
137 |
+
|
138 |
+
If the value of *obj* is greater than "LONG_MAX" or less than
|
139 |
+
"LONG_MIN", set **overflow* to "1" or "-1", respectively, and
|
140 |
+
return "-1"; otherwise, set **overflow* to "0". If any other
|
141 |
+
exception occurs set **overflow* to "0" and return "-1" as usual.
|
142 |
+
|
143 |
+
Returns "-1" on error. Use "PyErr_Occurred()" to disambiguate.
|
144 |
+
|
145 |
+
Changed in version 3.8: Use "__index__()" if available.
|
146 |
+
|
147 |
+
Changed in version 3.10: This function will no longer use
|
148 |
+
"__int__()".
|
149 |
+
|
150 |
+
long long PyLong_AsLongLong(PyObject *obj)
|
151 |
+
* Part of the Stable ABI.*
|
152 |
+
|
153 |
+
Return a C long long representation of *obj*. If *obj* is not an
|
154 |
+
instance of "PyLongObject", first call its "__index__()" method (if
|
155 |
+
present) to convert it to a "PyLongObject".
|
156 |
+
|
157 |
+
Raise "OverflowError" if the value of *obj* is out of range for a
|
158 |
+
long long.
|
159 |
+
|
160 |
+
Returns "-1" on error. Use "PyErr_Occurred()" to disambiguate.
|
161 |
+
|
162 |
+
Changed in version 3.8: Use "__index__()" if available.
|
163 |
+
|
164 |
+
Changed in version 3.10: This function will no longer use
|
165 |
+
"__int__()".
|
166 |
+
|
167 |
+
long long PyLong_AsLongLongAndOverflow(PyObject *obj, int *overflow)
|
168 |
+
* Part of the Stable ABI.*
|
169 |
+
|
170 |
+
Return a C long long representation of *obj*. If *obj* is not an
|
171 |
+
instance of "PyLongObject", first call its "__index__()" method (if
|
172 |
+
present) to convert it to a "PyLongObject".
|
173 |
+
|
174 |
+
If the value of *obj* is greater than "LLONG_MAX" or less than
|
175 |
+
"LLONG_MIN", set **overflow* to "1" or "-1", respectively, and
|
176 |
+
return "-1"; otherwise, set **overflow* to "0". If any other
|
177 |
+
exception occurs set **overflow* to "0" and return "-1" as usual.
|
178 |
+
|
179 |
+
Returns "-1" on error. Use "PyErr_Occurred()" to disambiguate.
|
180 |
+
|
181 |
+
Added in version 3.2.
|
182 |
+
|
183 |
+
Changed in version 3.8: Use "__index__()" if available.
|
184 |
+
|
185 |
+
Changed in version 3.10: This function will no longer use
|
186 |
+
"__int__()".
|
187 |
+
|
188 |
+
Py_ssize_t PyLong_AsSsize_t(PyObject *pylong)
|
189 |
+
* Part of the Stable ABI.*
|
190 |
+
|
191 |
+
Return a C "Py_ssize_t" representation of *pylong*. *pylong* must
|
192 |
+
be an instance of "PyLongObject".
|
193 |
+
|
194 |
+
Raise "OverflowError" if the value of *pylong* is out of range for
|
195 |
+
a "Py_ssize_t".
|
196 |
+
|
197 |
+
Returns "-1" on error. Use "PyErr_Occurred()" to disambiguate.
|
198 |
+
|
199 |
+
unsigned long PyLong_AsUnsignedLong(PyObject *pylong)
|
200 |
+
* Part of the Stable ABI.*
|
201 |
+
|
202 |
+
Return a C unsigned long representation of *pylong*. *pylong* must
|
203 |
+
be an instance of "PyLongObject".
|
204 |
+
|
205 |
+
Raise "OverflowError" if the value of *pylong* is out of range for
|
206 |
+
a unsigned long.
|
207 |
+
|
208 |
+
Returns "(unsigned long)-1" on error. Use "PyErr_Occurred()" to
|
209 |
+
disambiguate.
|
210 |
+
|
211 |
+
size_t PyLong_AsSize_t(PyObject *pylong)
|
212 |
+
* Part of the Stable ABI.*
|
213 |
+
|
214 |
+
Return a C "size_t" representation of *pylong*. *pylong* must be
|
215 |
+
an instance of "PyLongObject".
|
216 |
+
|
217 |
+
Raise "OverflowError" if the value of *pylong* is out of range for
|
218 |
+
a "size_t".
|
219 |
+
|
220 |
+
Returns "(size_t)-1" on error. Use "PyErr_Occurred()" to
|
221 |
+
disambiguate.
|
222 |
+
|
223 |
+
unsigned long long PyLong_AsUnsignedLongLong(PyObject *pylong)
|
224 |
+
* Part of the Stable ABI.*
|
225 |
+
|
226 |
+
Return a C unsigned long long representation of *pylong*. *pylong*
|
227 |
+
must be an instance of "PyLongObject".
|
228 |
+
|
229 |
+
Raise "OverflowError" if the value of *pylong* is out of range for
|
230 |
+
an unsigned long long.
|
231 |
+
|
232 |
+
Returns "(unsigned long long)-1" on error. Use "PyErr_Occurred()"
|
233 |
+
to disambiguate.
|
234 |
+
|
235 |
+
Changed in version 3.1: A negative *pylong* now raises
|
236 |
+
"OverflowError", not "TypeError".
|
237 |
+
|
238 |
+
unsigned long PyLong_AsUnsignedLongMask(PyObject *obj)
|
239 |
+
* Part of the Stable ABI.*
|
240 |
+
|
241 |
+
Return a C unsigned long representation of *obj*. If *obj* is not
|
242 |
+
an instance of "PyLongObject", first call its "__index__()" method
|
243 |
+
(if present) to convert it to a "PyLongObject".
|
244 |
+
|
245 |
+
If the value of *obj* is out of range for an unsigned long, return
|
246 |
+
the reduction of that value modulo "ULONG_MAX + 1".
|
247 |
+
|
248 |
+
Returns "(unsigned long)-1" on error. Use "PyErr_Occurred()" to
|
249 |
+
disambiguate.
|
250 |
+
|
251 |
+
Changed in version 3.8: Use "__index__()" if available.
|
252 |
+
|
253 |
+
Changed in version 3.10: This function will no longer use
|
254 |
+
"__int__()".
|
255 |
+
|
256 |
+
unsigned long long PyLong_AsUnsignedLongLongMask(PyObject *obj)
|
257 |
+
* Part of the Stable ABI.*
|
258 |
+
|
259 |
+
Return a C unsigned long long representation of *obj*. If *obj* is
|
260 |
+
not an instance of "PyLongObject", first call its "__index__()"
|
261 |
+
method (if present) to convert it to a "PyLongObject".
|
262 |
+
|
263 |
+
If the value of *obj* is out of range for an unsigned long long,
|
264 |
+
return the reduction of that value modulo "ULLONG_MAX + 1".
|
265 |
+
|
266 |
+
Returns "(unsigned long long)-1" on error. Use "PyErr_Occurred()"
|
267 |
+
to disambiguate.
|
268 |
+
|
269 |
+
Changed in version 3.8: Use "__index__()" if available.
|
270 |
+
|
271 |
+
Changed in version 3.10: This function will no longer use
|
272 |
+
"__int__()".
|
273 |
+
|
274 |
+
double PyLong_AsDouble(PyObject *pylong)
|
275 |
+
* Part of the Stable ABI.*
|
276 |
+
|
277 |
+
Return a C double representation of *pylong*. *pylong* must be an
|
278 |
+
instance of "PyLongObject".
|
279 |
+
|
280 |
+
Raise "OverflowError" if the value of *pylong* is out of range for
|
281 |
+
a double.
|
282 |
+
|
283 |
+
Returns "-1.0" on error. Use "PyErr_Occurred()" to disambiguate.
|
284 |
+
|
285 |
+
void *PyLong_AsVoidPtr(PyObject *pylong)
|
286 |
+
* Part of the Stable ABI.*
|
287 |
+
|
288 |
+
Convert a Python integer *pylong* to a C void pointer. If *pylong*
|
289 |
+
cannot be converted, an "OverflowError" will be raised. This is
|
290 |
+
only assured to produce a usable void pointer for values created
|
291 |
+
with "PyLong_FromVoidPtr()".
|
292 |
+
|
293 |
+
Returns "NULL" on error. Use "PyErr_Occurred()" to disambiguate.
|
294 |
+
|
295 |
+
PyObject *PyLong_GetInfo(void)
|
296 |
+
* Part of the Stable ABI.*
|
297 |
+
|
298 |
+
On success, return a read only *named tuple*, that holds
|
299 |
+
information about Python's internal representation of integers. See
|
300 |
+
"sys.int_info" for description of individual fields.
|
301 |
+
|
302 |
+
On failure, return "NULL" with an exception set.
|
303 |
+
|
304 |
+
Added in version 3.1.
|
305 |
+
|
306 |
+
int PyUnstable_Long_IsCompact(const PyLongObject *op)
|
307 |
+
|
308 |
+
*This is Unstable API. It may change without warning in minor
|
309 |
+
releases.*
|
310 |
+
|
311 |
+
Return 1 if *op* is compact, 0 otherwise.
|
312 |
+
|
313 |
+
This function makes it possible for performance-critical code to
|
314 |
+
implement a “fast path” for small integers. For compact values use
|
315 |
+
"PyUnstable_Long_CompactValue()"; for others fall back to a
|
316 |
+
"PyLong_As*" function or "calling" "int.to_bytes()".
|
317 |
+
|
318 |
+
The speedup is expected to be negligible for most users.
|
319 |
+
|
320 |
+
Exactly what values are considered compact is an implementation
|
321 |
+
detail and is subject to change.
|
322 |
+
|
323 |
+
Py_ssize_t PyUnstable_Long_CompactValue(const PyLongObject *op)
|
324 |
+
|
325 |
+
*This is Unstable API. It may change without warning in minor
|
326 |
+
releases.*
|
327 |
+
|
328 |
+
If *op* is compact, as determined by "PyUnstable_Long_IsCompact()",
|
329 |
+
return its value.
|
330 |
+
|
331 |
+
Otherwise, the return value is undefined.
|
python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/mapping.txt
ADDED
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Mapping Protocol
|
2 |
+
****************
|
3 |
+
|
4 |
+
See also "PyObject_GetItem()", "PyObject_SetItem()" and
|
5 |
+
"PyObject_DelItem()".
|
6 |
+
|
7 |
+
int PyMapping_Check(PyObject *o)
|
8 |
+
* Part of the Stable ABI.*
|
9 |
+
|
10 |
+
Return "1" if the object provides the mapping protocol or supports
|
11 |
+
slicing, and "0" otherwise. Note that it returns "1" for Python
|
12 |
+
classes with a "__getitem__()" method, since in general it is
|
13 |
+
impossible to determine what type of keys the class supports. This
|
14 |
+
function always succeeds.
|
15 |
+
|
16 |
+
Py_ssize_t PyMapping_Size(PyObject *o)
|
17 |
+
Py_ssize_t PyMapping_Length(PyObject *o)
|
18 |
+
* Part of the Stable ABI.*
|
19 |
+
|
20 |
+
Returns the number of keys in object *o* on success, and "-1" on
|
21 |
+
failure. This is equivalent to the Python expression "len(o)".
|
22 |
+
|
23 |
+
PyObject *PyMapping_GetItemString(PyObject *o, const char *key)
|
24 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
25 |
+
|
26 |
+
This is the same as "PyObject_GetItem()", but *key* is specified as
|
27 |
+
a const char* UTF-8 encoded bytes string, rather than a PyObject*.
|
28 |
+
|
29 |
+
int PyMapping_SetItemString(PyObject *o, const char *key, PyObject *v)
|
30 |
+
* Part of the Stable ABI.*
|
31 |
+
|
32 |
+
This is the same as "PyObject_SetItem()", but *key* is specified as
|
33 |
+
a const char* UTF-8 encoded bytes string, rather than a PyObject*.
|
34 |
+
|
35 |
+
int PyMapping_DelItem(PyObject *o, PyObject *key)
|
36 |
+
|
37 |
+
This is an alias of "PyObject_DelItem()".
|
38 |
+
|
39 |
+
int PyMapping_DelItemString(PyObject *o, const char *key)
|
40 |
+
|
41 |
+
This is the same as "PyObject_DelItem()", but *key* is specified as
|
42 |
+
a const char* UTF-8 encoded bytes string, rather than a PyObject*.
|
43 |
+
|
44 |
+
int PyMapping_HasKey(PyObject *o, PyObject *key)
|
45 |
+
* Part of the Stable ABI.*
|
46 |
+
|
47 |
+
Return "1" if the mapping object has the key *key* and "0"
|
48 |
+
otherwise. This is equivalent to the Python expression "key in o".
|
49 |
+
This function always succeeds.
|
50 |
+
|
51 |
+
Note:
|
52 |
+
|
53 |
+
Exceptions which occur when this calls "__getitem__()" method are
|
54 |
+
silently ignored. For proper error handling, use
|
55 |
+
"PyObject_GetItem()" instead.
|
56 |
+
|
57 |
+
int PyMapping_HasKeyString(PyObject *o, const char *key)
|
58 |
+
* Part of the Stable ABI.*
|
59 |
+
|
60 |
+
This is the same as "PyMapping_HasKey()", but *key* is specified as
|
61 |
+
a const char* UTF-8 encoded bytes string, rather than a PyObject*.
|
62 |
+
|
63 |
+
Note:
|
64 |
+
|
65 |
+
Exceptions that occur when this calls "__getitem__()" method or
|
66 |
+
while creating the temporary "str" object are silently ignored.
|
67 |
+
For proper error handling, use "PyMapping_GetItemString()"
|
68 |
+
instead.
|
69 |
+
|
70 |
+
PyObject *PyMapping_Keys(PyObject *o)
|
71 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
72 |
+
|
73 |
+
On success, return a list of the keys in object *o*. On failure,
|
74 |
+
return "NULL".
|
75 |
+
|
76 |
+
Changed in version 3.7: Previously, the function returned a list or
|
77 |
+
a tuple.
|
78 |
+
|
79 |
+
PyObject *PyMapping_Values(PyObject *o)
|
80 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
81 |
+
|
82 |
+
On success, return a list of the values in object *o*. On failure,
|
83 |
+
return "NULL".
|
84 |
+
|
85 |
+
Changed in version 3.7: Previously, the function returned a list or
|
86 |
+
a tuple.
|
87 |
+
|
88 |
+
PyObject *PyMapping_Items(PyObject *o)
|
89 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
90 |
+
|
91 |
+
On success, return a list of the items in object *o*, where each
|
92 |
+
item is a tuple containing a key-value pair. On failure, return
|
93 |
+
"NULL".
|
94 |
+
|
95 |
+
Changed in version 3.7: Previously, the function returned a list or
|
96 |
+
a tuple.
|
python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/marshal.txt
ADDED
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Data marshalling support
|
2 |
+
************************
|
3 |
+
|
4 |
+
These routines allow C code to work with serialized objects using the
|
5 |
+
same data format as the "marshal" module. There are functions to
|
6 |
+
write data into the serialization format, and additional functions
|
7 |
+
that can be used to read the data back. Files used to store
|
8 |
+
marshalled data must be opened in binary mode.
|
9 |
+
|
10 |
+
Numeric values are stored with the least significant byte first.
|
11 |
+
|
12 |
+
The module supports two versions of the data format: version 0 is the
|
13 |
+
historical version, version 1 shares interned strings in the file, and
|
14 |
+
upon unmarshalling. Version 2 uses a binary format for floating-point
|
15 |
+
numbers. "Py_MARSHAL_VERSION" indicates the current file format
|
16 |
+
(currently 2).
|
17 |
+
|
18 |
+
void PyMarshal_WriteLongToFile(long value, FILE *file, int version)
|
19 |
+
|
20 |
+
Marshal a long integer, *value*, to *file*. This will only write
|
21 |
+
the least-significant 32 bits of *value*; regardless of the size of
|
22 |
+
the native long type. *version* indicates the file format.
|
23 |
+
|
24 |
+
This function can fail, in which case it sets the error indicator.
|
25 |
+
Use "PyErr_Occurred()" to check for that.
|
26 |
+
|
27 |
+
void PyMarshal_WriteObjectToFile(PyObject *value, FILE *file, int version)
|
28 |
+
|
29 |
+
Marshal a Python object, *value*, to *file*. *version* indicates
|
30 |
+
the file format.
|
31 |
+
|
32 |
+
This function can fail, in which case it sets the error indicator.
|
33 |
+
Use "PyErr_Occurred()" to check for that.
|
34 |
+
|
35 |
+
PyObject *PyMarshal_WriteObjectToString(PyObject *value, int version)
|
36 |
+
*Return value: New reference.*
|
37 |
+
|
38 |
+
Return a bytes object containing the marshalled representation of
|
39 |
+
*value*. *version* indicates the file format.
|
40 |
+
|
41 |
+
The following functions allow marshalled values to be read back in.
|
42 |
+
|
43 |
+
long PyMarshal_ReadLongFromFile(FILE *file)
|
44 |
+
|
45 |
+
Return a C long from the data stream in a FILE* opened for reading.
|
46 |
+
Only a 32-bit value can be read in using this function, regardless
|
47 |
+
of the native size of long.
|
48 |
+
|
49 |
+
On error, sets the appropriate exception ("EOFError") and returns
|
50 |
+
"-1".
|
51 |
+
|
52 |
+
int PyMarshal_ReadShortFromFile(FILE *file)
|
53 |
+
|
54 |
+
Return a C short from the data stream in a FILE* opened for
|
55 |
+
reading. Only a 16-bit value can be read in using this function,
|
56 |
+
regardless of the native size of short.
|
57 |
+
|
58 |
+
On error, sets the appropriate exception ("EOFError") and returns
|
59 |
+
"-1".
|
60 |
+
|
61 |
+
PyObject *PyMarshal_ReadObjectFromFile(FILE *file)
|
62 |
+
*Return value: New reference.*
|
63 |
+
|
64 |
+
Return a Python object from the data stream in a FILE* opened for
|
65 |
+
reading.
|
66 |
+
|
67 |
+
On error, sets the appropriate exception ("EOFError", "ValueError"
|
68 |
+
or "TypeError") and returns "NULL".
|
69 |
+
|
70 |
+
PyObject *PyMarshal_ReadLastObjectFromFile(FILE *file)
|
71 |
+
*Return value: New reference.*
|
72 |
+
|
73 |
+
Return a Python object from the data stream in a FILE* opened for
|
74 |
+
reading. Unlike "PyMarshal_ReadObjectFromFile()", this function
|
75 |
+
assumes that no further objects will be read from the file,
|
76 |
+
allowing it to aggressively load file data into memory so that the
|
77 |
+
de-serialization can operate from data in memory rather than
|
78 |
+
reading a byte at a time from the file. Only use these variant if
|
79 |
+
you are certain that you won't be reading anything else from the
|
80 |
+
file.
|
81 |
+
|
82 |
+
On error, sets the appropriate exception ("EOFError", "ValueError"
|
83 |
+
or "TypeError") and returns "NULL".
|
84 |
+
|
85 |
+
PyObject *PyMarshal_ReadObjectFromString(const char *data, Py_ssize_t len)
|
86 |
+
*Return value: New reference.*
|
87 |
+
|
88 |
+
Return a Python object from the data stream in a byte buffer
|
89 |
+
containing *len* bytes pointed to by *data*.
|
90 |
+
|
91 |
+
On error, sets the appropriate exception ("EOFError", "ValueError"
|
92 |
+
or "TypeError") and returns "NULL".
|
python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/memory.txt
ADDED
@@ -0,0 +1,752 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Memory Management
|
2 |
+
*****************
|
3 |
+
|
4 |
+
|
5 |
+
Overview
|
6 |
+
========
|
7 |
+
|
8 |
+
Memory management in Python involves a private heap containing all
|
9 |
+
Python objects and data structures. The management of this private
|
10 |
+
heap is ensured internally by the *Python memory manager*. The Python
|
11 |
+
memory manager has different components which deal with various
|
12 |
+
dynamic storage management aspects, like sharing, segmentation,
|
13 |
+
preallocation or caching.
|
14 |
+
|
15 |
+
At the lowest level, a raw memory allocator ensures that there is
|
16 |
+
enough room in the private heap for storing all Python-related data by
|
17 |
+
interacting with the memory manager of the operating system. On top of
|
18 |
+
the raw memory allocator, several object-specific allocators operate
|
19 |
+
on the same heap and implement distinct memory management policies
|
20 |
+
adapted to the peculiarities of every object type. For example,
|
21 |
+
integer objects are managed differently within the heap than strings,
|
22 |
+
tuples or dictionaries because integers imply different storage
|
23 |
+
requirements and speed/space tradeoffs. The Python memory manager thus
|
24 |
+
delegates some of the work to the object-specific allocators, but
|
25 |
+
ensures that the latter operate within the bounds of the private heap.
|
26 |
+
|
27 |
+
It is important to understand that the management of the Python heap
|
28 |
+
is performed by the interpreter itself and that the user has no
|
29 |
+
control over it, even if they regularly manipulate object pointers to
|
30 |
+
memory blocks inside that heap. The allocation of heap space for
|
31 |
+
Python objects and other internal buffers is performed on demand by
|
32 |
+
the Python memory manager through the Python/C API functions listed in
|
33 |
+
this document.
|
34 |
+
|
35 |
+
To avoid memory corruption, extension writers should never try to
|
36 |
+
operate on Python objects with the functions exported by the C
|
37 |
+
library: "malloc()", "calloc()", "realloc()" and "free()". This will
|
38 |
+
result in mixed calls between the C allocator and the Python memory
|
39 |
+
manager with fatal consequences, because they implement different
|
40 |
+
algorithms and operate on different heaps. However, one may safely
|
41 |
+
allocate and release memory blocks with the C library allocator for
|
42 |
+
individual purposes, as shown in the following example:
|
43 |
+
|
44 |
+
PyObject *res;
|
45 |
+
char *buf = (char *) malloc(BUFSIZ); /* for I/O */
|
46 |
+
|
47 |
+
if (buf == NULL)
|
48 |
+
return PyErr_NoMemory();
|
49 |
+
...Do some I/O operation involving buf...
|
50 |
+
res = PyBytes_FromString(buf);
|
51 |
+
free(buf); /* malloc'ed */
|
52 |
+
return res;
|
53 |
+
|
54 |
+
In this example, the memory request for the I/O buffer is handled by
|
55 |
+
the C library allocator. The Python memory manager is involved only in
|
56 |
+
the allocation of the bytes object returned as a result.
|
57 |
+
|
58 |
+
In most situations, however, it is recommended to allocate memory from
|
59 |
+
the Python heap specifically because the latter is under control of
|
60 |
+
the Python memory manager. For example, this is required when the
|
61 |
+
interpreter is extended with new object types written in C. Another
|
62 |
+
reason for using the Python heap is the desire to *inform* the Python
|
63 |
+
memory manager about the memory needs of the extension module. Even
|
64 |
+
when the requested memory is used exclusively for internal, highly
|
65 |
+
specific purposes, delegating all memory requests to the Python memory
|
66 |
+
manager causes the interpreter to have a more accurate image of its
|
67 |
+
memory footprint as a whole. Consequently, under certain
|
68 |
+
circumstances, the Python memory manager may or may not trigger
|
69 |
+
appropriate actions, like garbage collection, memory compaction or
|
70 |
+
other preventive procedures. Note that by using the C library
|
71 |
+
allocator as shown in the previous example, the allocated memory for
|
72 |
+
the I/O buffer escapes completely the Python memory manager.
|
73 |
+
|
74 |
+
See also:
|
75 |
+
|
76 |
+
The "PYTHONMALLOC" environment variable can be used to configure the
|
77 |
+
memory allocators used by Python.
|
78 |
+
|
79 |
+
The "PYTHONMALLOCSTATS" environment variable can be used to print
|
80 |
+
statistics of the pymalloc memory allocator every time a new
|
81 |
+
pymalloc object arena is created, and on shutdown.
|
82 |
+
|
83 |
+
|
84 |
+
Allocator Domains
|
85 |
+
=================
|
86 |
+
|
87 |
+
All allocating functions belong to one of three different "domains"
|
88 |
+
(see also "PyMemAllocatorDomain"). These domains represent different
|
89 |
+
allocation strategies and are optimized for different purposes. The
|
90 |
+
specific details on how every domain allocates memory or what internal
|
91 |
+
functions each domain calls is considered an implementation detail,
|
92 |
+
but for debugging purposes a simplified table can be found at here.
|
93 |
+
There is no hard requirement to use the memory returned by the
|
94 |
+
allocation functions belonging to a given domain for only the purposes
|
95 |
+
hinted by that domain (although this is the recommended practice). For
|
96 |
+
example, one could use the memory returned by "PyMem_RawMalloc()" for
|
97 |
+
allocating Python objects or the memory returned by
|
98 |
+
"PyObject_Malloc()" for allocating memory for buffers.
|
99 |
+
|
100 |
+
The three allocation domains are:
|
101 |
+
|
102 |
+
* Raw domain: intended for allocating memory for general-purpose
|
103 |
+
memory buffers where the allocation *must* go to the system
|
104 |
+
allocator or where the allocator can operate without the *GIL*. The
|
105 |
+
memory is requested directly to the system.
|
106 |
+
|
107 |
+
* "Mem" domain: intended for allocating memory for Python buffers and
|
108 |
+
general-purpose memory buffers where the allocation must be
|
109 |
+
performed with the *GIL* held. The memory is taken from the Python
|
110 |
+
private heap.
|
111 |
+
|
112 |
+
* Object domain: intended for allocating memory belonging to Python
|
113 |
+
objects. The memory is taken from the Python private heap.
|
114 |
+
|
115 |
+
When freeing memory previously allocated by the allocating functions
|
116 |
+
belonging to a given domain,the matching specific deallocating
|
117 |
+
functions must be used. For example, "PyMem_Free()" must be used to
|
118 |
+
free memory allocated using "PyMem_Malloc()".
|
119 |
+
|
120 |
+
|
121 |
+
Raw Memory Interface
|
122 |
+
====================
|
123 |
+
|
124 |
+
The following function sets are wrappers to the system allocator.
|
125 |
+
These functions are thread-safe, the *GIL* does not need to be held.
|
126 |
+
|
127 |
+
The default raw memory allocator uses the following functions:
|
128 |
+
"malloc()", "calloc()", "realloc()" and "free()"; call "malloc(1)" (or
|
129 |
+
"calloc(1, 1)") when requesting zero bytes.
|
130 |
+
|
131 |
+
Added in version 3.4.
|
132 |
+
|
133 |
+
void *PyMem_RawMalloc(size_t n)
|
134 |
+
|
135 |
+
Allocates *n* bytes and returns a pointer of type void* to the
|
136 |
+
allocated memory, or "NULL" if the request fails.
|
137 |
+
|
138 |
+
Requesting zero bytes returns a distinct non-"NULL" pointer if
|
139 |
+
possible, as if "PyMem_RawMalloc(1)" had been called instead. The
|
140 |
+
memory will not have been initialized in any way.
|
141 |
+
|
142 |
+
void *PyMem_RawCalloc(size_t nelem, size_t elsize)
|
143 |
+
|
144 |
+
Allocates *nelem* elements each whose size in bytes is *elsize* and
|
145 |
+
returns a pointer of type void* to the allocated memory, or "NULL"
|
146 |
+
if the request fails. The memory is initialized to zeros.
|
147 |
+
|
148 |
+
Requesting zero elements or elements of size zero bytes returns a
|
149 |
+
distinct non-"NULL" pointer if possible, as if "PyMem_RawCalloc(1,
|
150 |
+
1)" had been called instead.
|
151 |
+
|
152 |
+
Added in version 3.5.
|
153 |
+
|
154 |
+
void *PyMem_RawRealloc(void *p, size_t n)
|
155 |
+
|
156 |
+
Resizes the memory block pointed to by *p* to *n* bytes. The
|
157 |
+
contents will be unchanged to the minimum of the old and the new
|
158 |
+
sizes.
|
159 |
+
|
160 |
+
If *p* is "NULL", the call is equivalent to "PyMem_RawMalloc(n)";
|
161 |
+
else if *n* is equal to zero, the memory block is resized but is
|
162 |
+
not freed, and the returned pointer is non-"NULL".
|
163 |
+
|
164 |
+
Unless *p* is "NULL", it must have been returned by a previous call
|
165 |
+
to "PyMem_RawMalloc()", "PyMem_RawRealloc()" or
|
166 |
+
"PyMem_RawCalloc()".
|
167 |
+
|
168 |
+
If the request fails, "PyMem_RawRealloc()" returns "NULL" and *p*
|
169 |
+
remains a valid pointer to the previous memory area.
|
170 |
+
|
171 |
+
void PyMem_RawFree(void *p)
|
172 |
+
|
173 |
+
Frees the memory block pointed to by *p*, which must have been
|
174 |
+
returned by a previous call to "PyMem_RawMalloc()",
|
175 |
+
"PyMem_RawRealloc()" or "PyMem_RawCalloc()". Otherwise, or if
|
176 |
+
"PyMem_RawFree(p)" has been called before, undefined behavior
|
177 |
+
occurs.
|
178 |
+
|
179 |
+
If *p* is "NULL", no operation is performed.
|
180 |
+
|
181 |
+
|
182 |
+
Memory Interface
|
183 |
+
================
|
184 |
+
|
185 |
+
The following function sets, modeled after the ANSI C standard, but
|
186 |
+
specifying behavior when requesting zero bytes, are available for
|
187 |
+
allocating and releasing memory from the Python heap.
|
188 |
+
|
189 |
+
The default memory allocator uses the pymalloc memory allocator.
|
190 |
+
|
191 |
+
Warning:
|
192 |
+
|
193 |
+
The *GIL* must be held when using these functions.
|
194 |
+
|
195 |
+
Changed in version 3.6: The default allocator is now pymalloc instead
|
196 |
+
of system "malloc()".
|
197 |
+
|
198 |
+
void *PyMem_Malloc(size_t n)
|
199 |
+
* Part of the Stable ABI.*
|
200 |
+
|
201 |
+
Allocates *n* bytes and returns a pointer of type void* to the
|
202 |
+
allocated memory, or "NULL" if the request fails.
|
203 |
+
|
204 |
+
Requesting zero bytes returns a distinct non-"NULL" pointer if
|
205 |
+
possible, as if "PyMem_Malloc(1)" had been called instead. The
|
206 |
+
memory will not have been initialized in any way.
|
207 |
+
|
208 |
+
void *PyMem_Calloc(size_t nelem, size_t elsize)
|
209 |
+
* Part of the Stable ABI since version 3.7.*
|
210 |
+
|
211 |
+
Allocates *nelem* elements each whose size in bytes is *elsize* and
|
212 |
+
returns a pointer of type void* to the allocated memory, or "NULL"
|
213 |
+
if the request fails. The memory is initialized to zeros.
|
214 |
+
|
215 |
+
Requesting zero elements or elements of size zero bytes returns a
|
216 |
+
distinct non-"NULL" pointer if possible, as if "PyMem_Calloc(1, 1)"
|
217 |
+
had been called instead.
|
218 |
+
|
219 |
+
Added in version 3.5.
|
220 |
+
|
221 |
+
void *PyMem_Realloc(void *p, size_t n)
|
222 |
+
* Part of the Stable ABI.*
|
223 |
+
|
224 |
+
Resizes the memory block pointed to by *p* to *n* bytes. The
|
225 |
+
contents will be unchanged to the minimum of the old and the new
|
226 |
+
sizes.
|
227 |
+
|
228 |
+
If *p* is "NULL", the call is equivalent to "PyMem_Malloc(n)"; else
|
229 |
+
if *n* is equal to zero, the memory block is resized but is not
|
230 |
+
freed, and the returned pointer is non-"NULL".
|
231 |
+
|
232 |
+
Unless *p* is "NULL", it must have been returned by a previous call
|
233 |
+
to "PyMem_Malloc()", "PyMem_Realloc()" or "PyMem_Calloc()".
|
234 |
+
|
235 |
+
If the request fails, "PyMem_Realloc()" returns "NULL" and *p*
|
236 |
+
remains a valid pointer to the previous memory area.
|
237 |
+
|
238 |
+
void PyMem_Free(void *p)
|
239 |
+
* Part of the Stable ABI.*
|
240 |
+
|
241 |
+
Frees the memory block pointed to by *p*, which must have been
|
242 |
+
returned by a previous call to "PyMem_Malloc()", "PyMem_Realloc()"
|
243 |
+
or "PyMem_Calloc()". Otherwise, or if "PyMem_Free(p)" has been
|
244 |
+
called before, undefined behavior occurs.
|
245 |
+
|
246 |
+
If *p* is "NULL", no operation is performed.
|
247 |
+
|
248 |
+
The following type-oriented macros are provided for convenience. Note
|
249 |
+
that *TYPE* refers to any C type.
|
250 |
+
|
251 |
+
PyMem_New(TYPE, n)
|
252 |
+
|
253 |
+
Same as "PyMem_Malloc()", but allocates "(n * sizeof(TYPE))" bytes
|
254 |
+
of memory. Returns a pointer cast to "TYPE*". The memory will not
|
255 |
+
have been initialized in any way.
|
256 |
+
|
257 |
+
PyMem_Resize(p, TYPE, n)
|
258 |
+
|
259 |
+
Same as "PyMem_Realloc()", but the memory block is resized to "(n *
|
260 |
+
sizeof(TYPE))" bytes. Returns a pointer cast to "TYPE*". On
|
261 |
+
return, *p* will be a pointer to the new memory area, or "NULL" in
|
262 |
+
the event of failure.
|
263 |
+
|
264 |
+
This is a C preprocessor macro; *p* is always reassigned. Save the
|
265 |
+
original value of *p* to avoid losing memory when handling errors.
|
266 |
+
|
267 |
+
void PyMem_Del(void *p)
|
268 |
+
|
269 |
+
Same as "PyMem_Free()".
|
270 |
+
|
271 |
+
In addition, the following macro sets are provided for calling the
|
272 |
+
Python memory allocator directly, without involving the C API
|
273 |
+
functions listed above. However, note that their use does not preserve
|
274 |
+
binary compatibility across Python versions and is therefore
|
275 |
+
deprecated in extension modules.
|
276 |
+
|
277 |
+
* "PyMem_MALLOC(size)"
|
278 |
+
|
279 |
+
* "PyMem_NEW(type, size)"
|
280 |
+
|
281 |
+
* "PyMem_REALLOC(ptr, size)"
|
282 |
+
|
283 |
+
* "PyMem_RESIZE(ptr, type, size)"
|
284 |
+
|
285 |
+
* "PyMem_FREE(ptr)"
|
286 |
+
|
287 |
+
* "PyMem_DEL(ptr)"
|
288 |
+
|
289 |
+
|
290 |
+
Object allocators
|
291 |
+
=================
|
292 |
+
|
293 |
+
The following function sets, modeled after the ANSI C standard, but
|
294 |
+
specifying behavior when requesting zero bytes, are available for
|
295 |
+
allocating and releasing memory from the Python heap.
|
296 |
+
|
297 |
+
Note:
|
298 |
+
|
299 |
+
There is no guarantee that the memory returned by these allocators
|
300 |
+
can be successfully cast to a Python object when intercepting the
|
301 |
+
allocating functions in this domain by the methods described in the
|
302 |
+
Customize Memory Allocators section.
|
303 |
+
|
304 |
+
The default object allocator uses the pymalloc memory allocator.
|
305 |
+
|
306 |
+
Warning:
|
307 |
+
|
308 |
+
The *GIL* must be held when using these functions.
|
309 |
+
|
310 |
+
void *PyObject_Malloc(size_t n)
|
311 |
+
* Part of the Stable ABI.*
|
312 |
+
|
313 |
+
Allocates *n* bytes and returns a pointer of type void* to the
|
314 |
+
allocated memory, or "NULL" if the request fails.
|
315 |
+
|
316 |
+
Requesting zero bytes returns a distinct non-"NULL" pointer if
|
317 |
+
possible, as if "PyObject_Malloc(1)" had been called instead. The
|
318 |
+
memory will not have been initialized in any way.
|
319 |
+
|
320 |
+
void *PyObject_Calloc(size_t nelem, size_t elsize)
|
321 |
+
* Part of the Stable ABI since version 3.7.*
|
322 |
+
|
323 |
+
Allocates *nelem* elements each whose size in bytes is *elsize* and
|
324 |
+
returns a pointer of type void* to the allocated memory, or "NULL"
|
325 |
+
if the request fails. The memory is initialized to zeros.
|
326 |
+
|
327 |
+
Requesting zero elements or elements of size zero bytes returns a
|
328 |
+
distinct non-"NULL" pointer if possible, as if "PyObject_Calloc(1,
|
329 |
+
1)" had been called instead.
|
330 |
+
|
331 |
+
Added in version 3.5.
|
332 |
+
|
333 |
+
void *PyObject_Realloc(void *p, size_t n)
|
334 |
+
* Part of the Stable ABI.*
|
335 |
+
|
336 |
+
Resizes the memory block pointed to by *p* to *n* bytes. The
|
337 |
+
contents will be unchanged to the minimum of the old and the new
|
338 |
+
sizes.
|
339 |
+
|
340 |
+
If *p* is "NULL", the call is equivalent to "PyObject_Malloc(n)";
|
341 |
+
else if *n* is equal to zero, the memory block is resized but is
|
342 |
+
not freed, and the returned pointer is non-"NULL".
|
343 |
+
|
344 |
+
Unless *p* is "NULL", it must have been returned by a previous call
|
345 |
+
to "PyObject_Malloc()", "PyObject_Realloc()" or
|
346 |
+
"PyObject_Calloc()".
|
347 |
+
|
348 |
+
If the request fails, "PyObject_Realloc()" returns "NULL" and *p*
|
349 |
+
remains a valid pointer to the previous memory area.
|
350 |
+
|
351 |
+
void PyObject_Free(void *p)
|
352 |
+
* Part of the Stable ABI.*
|
353 |
+
|
354 |
+
Frees the memory block pointed to by *p*, which must have been
|
355 |
+
returned by a previous call to "PyObject_Malloc()",
|
356 |
+
"PyObject_Realloc()" or "PyObject_Calloc()". Otherwise, or if
|
357 |
+
"PyObject_Free(p)" has been called before, undefined behavior
|
358 |
+
occurs.
|
359 |
+
|
360 |
+
If *p* is "NULL", no operation is performed.
|
361 |
+
|
362 |
+
|
363 |
+
Default Memory Allocators
|
364 |
+
=========================
|
365 |
+
|
366 |
+
Default memory allocators:
|
367 |
+
|
368 |
+
+---------------------------------+----------------------+--------------------+-----------------------+----------------------+
|
369 |
+
| Configuration | Name | PyMem_RawMalloc | PyMem_Malloc | PyObject_Malloc |
|
370 |
+
|=================================|======================|====================|=======================|======================|
|
371 |
+
| Release build | ""pymalloc"" | "malloc" | "pymalloc" | "pymalloc" |
|
372 |
+
+---------------------------------+----------------------+--------------------+-----------------------+----------------------+
|
373 |
+
| Debug build | ""pymalloc_debug"" | "malloc" + debug | "pymalloc" + debug | "pymalloc" + debug |
|
374 |
+
+---------------------------------+----------------------+--------------------+-----------------------+----------------------+
|
375 |
+
| Release build, without pymalloc | ""malloc"" | "malloc" | "malloc" | "malloc" |
|
376 |
+
+---------------------------------+----------------------+--------------------+-----------------------+----------------------+
|
377 |
+
| Debug build, without pymalloc | ""malloc_debug"" | "malloc" + debug | "malloc" + debug | "malloc" + debug |
|
378 |
+
+---------------------------------+----------------------+--------------------+-----------------------+----------------------+
|
379 |
+
|
380 |
+
Legend:
|
381 |
+
|
382 |
+
* Name: value for "PYTHONMALLOC" environment variable.
|
383 |
+
|
384 |
+
* "malloc": system allocators from the standard C library, C
|
385 |
+
functions: "malloc()", "calloc()", "realloc()" and "free()".
|
386 |
+
|
387 |
+
* "pymalloc": pymalloc memory allocator.
|
388 |
+
|
389 |
+
* "+ debug": with debug hooks on the Python memory allocators.
|
390 |
+
|
391 |
+
* "Debug build": Python build in debug mode.
|
392 |
+
|
393 |
+
|
394 |
+
Customize Memory Allocators
|
395 |
+
===========================
|
396 |
+
|
397 |
+
Added in version 3.4.
|
398 |
+
|
399 |
+
type PyMemAllocatorEx
|
400 |
+
|
401 |
+
Structure used to describe a memory block allocator. The structure
|
402 |
+
has the following fields:
|
403 |
+
|
404 |
+
+------------------------------------------------------------+-----------------------------------------+
|
405 |
+
| Field | Meaning |
|
406 |
+
|============================================================|=========================================|
|
407 |
+
| "void *ctx" | user context passed as first argument |
|
408 |
+
+------------------------------------------------------------+-----------------------------------------+
|
409 |
+
| "void* malloc(void *ctx, size_t size)" | allocate a memory block |
|
410 |
+
+------------------------------------------------------------+-----------------------------------------+
|
411 |
+
| "void* calloc(void *ctx, size_t nelem, size_t elsize)" | allocate a memory block initialized |
|
412 |
+
| | with zeros |
|
413 |
+
+------------------------------------------------------------+-----------------------------------------+
|
414 |
+
| "void* realloc(void *ctx, void *ptr, size_t new_size)" | allocate or resize a memory block |
|
415 |
+
+------------------------------------------------------------+-----------------------------------------+
|
416 |
+
| "void free(void *ctx, void *ptr)" | free a memory block |
|
417 |
+
+------------------------------------------------------------+-----------------------------------------+
|
418 |
+
|
419 |
+
Changed in version 3.5: The "PyMemAllocator" structure was renamed
|
420 |
+
to "PyMemAllocatorEx" and a new "calloc" field was added.
|
421 |
+
|
422 |
+
type PyMemAllocatorDomain
|
423 |
+
|
424 |
+
Enum used to identify an allocator domain. Domains:
|
425 |
+
|
426 |
+
PYMEM_DOMAIN_RAW
|
427 |
+
|
428 |
+
Functions:
|
429 |
+
|
430 |
+
* "PyMem_RawMalloc()"
|
431 |
+
|
432 |
+
* "PyMem_RawRealloc()"
|
433 |
+
|
434 |
+
* "PyMem_RawCalloc()"
|
435 |
+
|
436 |
+
* "PyMem_RawFree()"
|
437 |
+
|
438 |
+
PYMEM_DOMAIN_MEM
|
439 |
+
|
440 |
+
Functions:
|
441 |
+
|
442 |
+
* "PyMem_Malloc()",
|
443 |
+
|
444 |
+
* "PyMem_Realloc()"
|
445 |
+
|
446 |
+
* "PyMem_Calloc()"
|
447 |
+
|
448 |
+
* "PyMem_Free()"
|
449 |
+
|
450 |
+
PYMEM_DOMAIN_OBJ
|
451 |
+
|
452 |
+
Functions:
|
453 |
+
|
454 |
+
* "PyObject_Malloc()"
|
455 |
+
|
456 |
+
* "PyObject_Realloc()"
|
457 |
+
|
458 |
+
* "PyObject_Calloc()"
|
459 |
+
|
460 |
+
* "PyObject_Free()"
|
461 |
+
|
462 |
+
void PyMem_GetAllocator(PyMemAllocatorDomain domain, PyMemAllocatorEx *allocator)
|
463 |
+
|
464 |
+
Get the memory block allocator of the specified domain.
|
465 |
+
|
466 |
+
void PyMem_SetAllocator(PyMemAllocatorDomain domain, PyMemAllocatorEx *allocator)
|
467 |
+
|
468 |
+
Set the memory block allocator of the specified domain.
|
469 |
+
|
470 |
+
The new allocator must return a distinct non-"NULL" pointer when
|
471 |
+
requesting zero bytes.
|
472 |
+
|
473 |
+
For the "PYMEM_DOMAIN_RAW" domain, the allocator must be thread-
|
474 |
+
safe: the *GIL* is not held when the allocator is called.
|
475 |
+
|
476 |
+
For the remaining domains, the allocator must also be thread-safe:
|
477 |
+
the allocator may be called in different interpreters that do not
|
478 |
+
share a "GIL".
|
479 |
+
|
480 |
+
If the new allocator is not a hook (does not call the previous
|
481 |
+
allocator), the "PyMem_SetupDebugHooks()" function must be called
|
482 |
+
to reinstall the debug hooks on top on the new allocator.
|
483 |
+
|
484 |
+
See also "PyPreConfig.allocator" and Preinitialize Python with
|
485 |
+
PyPreConfig.
|
486 |
+
|
487 |
+
Warning:
|
488 |
+
|
489 |
+
"PyMem_SetAllocator()" does have the following contract:
|
490 |
+
|
491 |
+
* It can be called after "Py_PreInitialize()" and before
|
492 |
+
"Py_InitializeFromConfig()" to install a custom memory
|
493 |
+
allocator. There are no restrictions over the installed
|
494 |
+
allocator other than the ones imposed by the domain (for
|
495 |
+
instance, the Raw Domain allows the allocator to be called
|
496 |
+
without the GIL held). See the section on allocator domains for
|
497 |
+
more information.
|
498 |
+
|
499 |
+
* If called after Python has finish initializing (after
|
500 |
+
"Py_InitializeFromConfig()" has been called) the allocator
|
501 |
+
**must** wrap the existing allocator. Substituting the current
|
502 |
+
allocator for some other arbitrary one is **not supported**.
|
503 |
+
|
504 |
+
Changed in version 3.12: All allocators must be thread-safe.
|
505 |
+
|
506 |
+
void PyMem_SetupDebugHooks(void)
|
507 |
+
|
508 |
+
Setup debug hooks in the Python memory allocators to detect memory
|
509 |
+
errors.
|
510 |
+
|
511 |
+
|
512 |
+
Debug hooks on the Python memory allocators
|
513 |
+
===========================================
|
514 |
+
|
515 |
+
When Python is built in debug mode, the "PyMem_SetupDebugHooks()"
|
516 |
+
function is called at the Python preinitialization to setup debug
|
517 |
+
hooks on Python memory allocators to detect memory errors.
|
518 |
+
|
519 |
+
The "PYTHONMALLOC" environment variable can be used to install debug
|
520 |
+
hooks on a Python compiled in release mode (ex: "PYTHONMALLOC=debug").
|
521 |
+
|
522 |
+
The "PyMem_SetupDebugHooks()" function can be used to set debug hooks
|
523 |
+
after calling "PyMem_SetAllocator()".
|
524 |
+
|
525 |
+
These debug hooks fill dynamically allocated memory blocks with
|
526 |
+
special, recognizable bit patterns. Newly allocated memory is filled
|
527 |
+
with the byte "0xCD" ("PYMEM_CLEANBYTE"), freed memory is filled with
|
528 |
+
the byte "0xDD" ("PYMEM_DEADBYTE"). Memory blocks are surrounded by
|
529 |
+
"forbidden bytes" filled with the byte "0xFD" ("PYMEM_FORBIDDENBYTE").
|
530 |
+
Strings of these bytes are unlikely to be valid addresses, floats, or
|
531 |
+
ASCII strings.
|
532 |
+
|
533 |
+
Runtime checks:
|
534 |
+
|
535 |
+
* Detect API violations. For example, detect if "PyObject_Free()" is
|
536 |
+
called on a memory block allocated by "PyMem_Malloc()".
|
537 |
+
|
538 |
+
* Detect write before the start of the buffer (buffer underflow).
|
539 |
+
|
540 |
+
* Detect write after the end of the buffer (buffer overflow).
|
541 |
+
|
542 |
+
* Check that the *GIL* is held when allocator functions of
|
543 |
+
"PYMEM_DOMAIN_OBJ" (ex: "PyObject_Malloc()") and "PYMEM_DOMAIN_MEM"
|
544 |
+
(ex: "PyMem_Malloc()") domains are called.
|
545 |
+
|
546 |
+
On error, the debug hooks use the "tracemalloc" module to get the
|
547 |
+
traceback where a memory block was allocated. The traceback is only
|
548 |
+
displayed if "tracemalloc" is tracing Python memory allocations and
|
549 |
+
the memory block was traced.
|
550 |
+
|
551 |
+
Let *S* = "sizeof(size_t)". "2*S" bytes are added at each end of each
|
552 |
+
block of *N* bytes requested. The memory layout is like so, where p
|
553 |
+
represents the address returned by a malloc-like or realloc-like
|
554 |
+
function ("p[i:j]" means the slice of bytes from "*(p+i)" inclusive up
|
555 |
+
to "*(p+j)" exclusive; note that the treatment of negative indices
|
556 |
+
differs from a Python slice):
|
557 |
+
|
558 |
+
"p[-2*S:-S]"
|
559 |
+
Number of bytes originally asked for. This is a size_t, big-endian
|
560 |
+
(easier to read in a memory dump).
|
561 |
+
|
562 |
+
"p[-S]"
|
563 |
+
API identifier (ASCII character):
|
564 |
+
|
565 |
+
* "'r'" for "PYMEM_DOMAIN_RAW".
|
566 |
+
|
567 |
+
* "'m'" for "PYMEM_DOMAIN_MEM".
|
568 |
+
|
569 |
+
* "'o'" for "PYMEM_DOMAIN_OBJ".
|
570 |
+
|
571 |
+
"p[-S+1:0]"
|
572 |
+
Copies of PYMEM_FORBIDDENBYTE. Used to catch under- writes and
|
573 |
+
reads.
|
574 |
+
|
575 |
+
"p[0:N]"
|
576 |
+
The requested memory, filled with copies of PYMEM_CLEANBYTE, used
|
577 |
+
to catch reference to uninitialized memory. When a realloc-like
|
578 |
+
function is called requesting a larger memory block, the new excess
|
579 |
+
bytes are also filled with PYMEM_CLEANBYTE. When a free-like
|
580 |
+
function is called, these are overwritten with PYMEM_DEADBYTE, to
|
581 |
+
catch reference to freed memory. When a realloc- like function is
|
582 |
+
called requesting a smaller memory block, the excess old bytes are
|
583 |
+
also filled with PYMEM_DEADBYTE.
|
584 |
+
|
585 |
+
"p[N:N+S]"
|
586 |
+
Copies of PYMEM_FORBIDDENBYTE. Used to catch over- writes and
|
587 |
+
reads.
|
588 |
+
|
589 |
+
"p[N+S:N+2*S]"
|
590 |
+
Only used if the "PYMEM_DEBUG_SERIALNO" macro is defined (not
|
591 |
+
defined by default).
|
592 |
+
|
593 |
+
A serial number, incremented by 1 on each call to a malloc-like or
|
594 |
+
realloc-like function. Big-endian "size_t". If "bad memory" is
|
595 |
+
detected later, the serial number gives an excellent way to set a
|
596 |
+
breakpoint on the next run, to capture the instant at which this
|
597 |
+
block was passed out. The static function bumpserialno() in
|
598 |
+
obmalloc.c is the only place the serial number is incremented, and
|
599 |
+
exists so you can set such a breakpoint easily.
|
600 |
+
|
601 |
+
A realloc-like or free-like function first checks that the
|
602 |
+
PYMEM_FORBIDDENBYTE bytes at each end are intact. If they've been
|
603 |
+
altered, diagnostic output is written to stderr, and the program is
|
604 |
+
aborted via Py_FatalError(). The other main failure mode is provoking
|
605 |
+
a memory error when a program reads up one of the special bit patterns
|
606 |
+
and tries to use it as an address. If you get in a debugger then and
|
607 |
+
look at the object, you're likely to see that it's entirely filled
|
608 |
+
with PYMEM_DEADBYTE (meaning freed memory is getting used) or
|
609 |
+
PYMEM_CLEANBYTE (meaning uninitialized memory is getting used).
|
610 |
+
|
611 |
+
Changed in version 3.6: The "PyMem_SetupDebugHooks()" function now
|
612 |
+
also works on Python compiled in release mode. On error, the debug
|
613 |
+
hooks now use "tracemalloc" to get the traceback where a memory block
|
614 |
+
was allocated. The debug hooks now also check if the GIL is held when
|
615 |
+
functions of "PYMEM_DOMAIN_OBJ" and "PYMEM_DOMAIN_MEM" domains are
|
616 |
+
called.
|
617 |
+
|
618 |
+
Changed in version 3.8: Byte patterns "0xCB" ("PYMEM_CLEANBYTE"),
|
619 |
+
"0xDB" ("PYMEM_DEADBYTE") and "0xFB" ("PYMEM_FORBIDDENBYTE") have been
|
620 |
+
replaced with "0xCD", "0xDD" and "0xFD" to use the same values than
|
621 |
+
Windows CRT debug "malloc()" and "free()".
|
622 |
+
|
623 |
+
|
624 |
+
The pymalloc allocator
|
625 |
+
======================
|
626 |
+
|
627 |
+
Python has a *pymalloc* allocator optimized for small objects (smaller
|
628 |
+
or equal to 512 bytes) with a short lifetime. It uses memory mappings
|
629 |
+
called "arenas" with a fixed size of either 256 KiB on 32-bit
|
630 |
+
platforms or 1 MiB on 64-bit platforms. It falls back to
|
631 |
+
"PyMem_RawMalloc()" and "PyMem_RawRealloc()" for allocations larger
|
632 |
+
than 512 bytes.
|
633 |
+
|
634 |
+
*pymalloc* is the default allocator of the "PYMEM_DOMAIN_MEM" (ex:
|
635 |
+
"PyMem_Malloc()") and "PYMEM_DOMAIN_OBJ" (ex: "PyObject_Malloc()")
|
636 |
+
domains.
|
637 |
+
|
638 |
+
The arena allocator uses the following functions:
|
639 |
+
|
640 |
+
* "VirtualAlloc()" and "VirtualFree()" on Windows,
|
641 |
+
|
642 |
+
* "mmap()" and "munmap()" if available,
|
643 |
+
|
644 |
+
* "malloc()" and "free()" otherwise.
|
645 |
+
|
646 |
+
This allocator is disabled if Python is configured with the "--
|
647 |
+
without-pymalloc" option. It can also be disabled at runtime using the
|
648 |
+
"PYTHONMALLOC" environment variable (ex: "PYTHONMALLOC=malloc").
|
649 |
+
|
650 |
+
|
651 |
+
Customize pymalloc Arena Allocator
|
652 |
+
----------------------------------
|
653 |
+
|
654 |
+
Added in version 3.4.
|
655 |
+
|
656 |
+
type PyObjectArenaAllocator
|
657 |
+
|
658 |
+
Structure used to describe an arena allocator. The structure has
|
659 |
+
three fields:
|
660 |
+
|
661 |
+
+----------------------------------------------------+-----------------------------------------+
|
662 |
+
| Field | Meaning |
|
663 |
+
|====================================================|=========================================|
|
664 |
+
| "void *ctx" | user context passed as first argument |
|
665 |
+
+----------------------------------------------------+-----------------------------------------+
|
666 |
+
| "void* alloc(void *ctx, size_t size)" | allocate an arena of size bytes |
|
667 |
+
+----------------------------------------------------+-----------------------------------------+
|
668 |
+
| "void free(void *ctx, void *ptr, size_t size)" | free an arena |
|
669 |
+
+----------------------------------------------------+-----------------------------------------+
|
670 |
+
|
671 |
+
void PyObject_GetArenaAllocator(PyObjectArenaAllocator *allocator)
|
672 |
+
|
673 |
+
Get the arena allocator.
|
674 |
+
|
675 |
+
void PyObject_SetArenaAllocator(PyObjectArenaAllocator *allocator)
|
676 |
+
|
677 |
+
Set the arena allocator.
|
678 |
+
|
679 |
+
|
680 |
+
tracemalloc C API
|
681 |
+
=================
|
682 |
+
|
683 |
+
Added in version 3.7.
|
684 |
+
|
685 |
+
int PyTraceMalloc_Track(unsigned int domain, uintptr_t ptr, size_t size)
|
686 |
+
|
687 |
+
Track an allocated memory block in the "tracemalloc" module.
|
688 |
+
|
689 |
+
Return "0" on success, return "-1" on error (failed to allocate
|
690 |
+
memory to store the trace). Return "-2" if tracemalloc is disabled.
|
691 |
+
|
692 |
+
If memory block is already tracked, update the existing trace.
|
693 |
+
|
694 |
+
int PyTraceMalloc_Untrack(unsigned int domain, uintptr_t ptr)
|
695 |
+
|
696 |
+
Untrack an allocated memory block in the "tracemalloc" module. Do
|
697 |
+
nothing if the block was not tracked.
|
698 |
+
|
699 |
+
Return "-2" if tracemalloc is disabled, otherwise return "0".
|
700 |
+
|
701 |
+
|
702 |
+
Examples
|
703 |
+
========
|
704 |
+
|
705 |
+
Here is the example from section Overview, rewritten so that the I/O
|
706 |
+
buffer is allocated from the Python heap by using the first function
|
707 |
+
set:
|
708 |
+
|
709 |
+
PyObject *res;
|
710 |
+
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */
|
711 |
+
|
712 |
+
if (buf == NULL)
|
713 |
+
return PyErr_NoMemory();
|
714 |
+
/* ...Do some I/O operation involving buf... */
|
715 |
+
res = PyBytes_FromString(buf);
|
716 |
+
PyMem_Free(buf); /* allocated with PyMem_Malloc */
|
717 |
+
return res;
|
718 |
+
|
719 |
+
The same code using the type-oriented function set:
|
720 |
+
|
721 |
+
PyObject *res;
|
722 |
+
char *buf = PyMem_New(char, BUFSIZ); /* for I/O */
|
723 |
+
|
724 |
+
if (buf == NULL)
|
725 |
+
return PyErr_NoMemory();
|
726 |
+
/* ...Do some I/O operation involving buf... */
|
727 |
+
res = PyBytes_FromString(buf);
|
728 |
+
PyMem_Del(buf); /* allocated with PyMem_New */
|
729 |
+
return res;
|
730 |
+
|
731 |
+
Note that in the two examples above, the buffer is always manipulated
|
732 |
+
via functions belonging to the same set. Indeed, it is required to use
|
733 |
+
the same memory API family for a given memory block, so that the risk
|
734 |
+
of mixing different allocators is reduced to a minimum. The following
|
735 |
+
code sequence contains two errors, one of which is labeled as *fatal*
|
736 |
+
because it mixes two different allocators operating on different
|
737 |
+
heaps.
|
738 |
+
|
739 |
+
char *buf1 = PyMem_New(char, BUFSIZ);
|
740 |
+
char *buf2 = (char *) malloc(BUFSIZ);
|
741 |
+
char *buf3 = (char *) PyMem_Malloc(BUFSIZ);
|
742 |
+
...
|
743 |
+
PyMem_Del(buf3); /* Wrong -- should be PyMem_Free() */
|
744 |
+
free(buf2); /* Right -- allocated via malloc() */
|
745 |
+
free(buf1); /* Fatal -- should be PyMem_Del() */
|
746 |
+
|
747 |
+
In addition to the functions aimed at handling raw memory blocks from
|
748 |
+
the Python heap, objects in Python are allocated and released with
|
749 |
+
"PyObject_New", "PyObject_NewVar" and "PyObject_Del()".
|
750 |
+
|
751 |
+
These will be explained in the next chapter on defining and
|
752 |
+
implementing new object types in C.
|
python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/memoryview.txt
ADDED
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
MemoryView objects
|
2 |
+
******************
|
3 |
+
|
4 |
+
A "memoryview" object exposes the C level buffer interface as a Python
|
5 |
+
object which can then be passed around like any other object.
|
6 |
+
|
7 |
+
PyObject *PyMemoryView_FromObject(PyObject *obj)
|
8 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
9 |
+
|
10 |
+
Create a memoryview object from an object that provides the buffer
|
11 |
+
interface. If *obj* supports writable buffer exports, the
|
12 |
+
memoryview object will be read/write, otherwise it may be either
|
13 |
+
read-only or read/write at the discretion of the exporter.
|
14 |
+
|
15 |
+
PyBUF_READ
|
16 |
+
|
17 |
+
Flag to request a readonly buffer.
|
18 |
+
|
19 |
+
PyBUF_WRITE
|
20 |
+
|
21 |
+
Flag to request a writable buffer.
|
22 |
+
|
23 |
+
PyObject *PyMemoryView_FromMemory(char *mem, Py_ssize_t size, int flags)
|
24 |
+
*Return value: New reference.** Part of the Stable ABI since
|
25 |
+
version 3.7.*
|
26 |
+
|
27 |
+
Create a memoryview object using *mem* as the underlying buffer.
|
28 |
+
*flags* can be one of "PyBUF_READ" or "PyBUF_WRITE".
|
29 |
+
|
30 |
+
Added in version 3.3.
|
31 |
+
|
32 |
+
PyObject *PyMemoryView_FromBuffer(const Py_buffer *view)
|
33 |
+
*Return value: New reference.** Part of the Stable ABI since
|
34 |
+
version 3.11.*
|
35 |
+
|
36 |
+
Create a memoryview object wrapping the given buffer structure
|
37 |
+
*view*. For simple byte buffers, "PyMemoryView_FromMemory()" is the
|
38 |
+
preferred function.
|
39 |
+
|
40 |
+
PyObject *PyMemoryView_GetContiguous(PyObject *obj, int buffertype, char order)
|
41 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
42 |
+
|
43 |
+
Create a memoryview object to a *contiguous* chunk of memory (in
|
44 |
+
either 'C' or 'F'ortran *order*) from an object that defines the
|
45 |
+
buffer interface. If memory is contiguous, the memoryview object
|
46 |
+
points to the original memory. Otherwise, a copy is made and the
|
47 |
+
memoryview points to a new bytes object.
|
48 |
+
|
49 |
+
*buffertype* can be one of "PyBUF_READ" or "PyBUF_WRITE".
|
50 |
+
|
51 |
+
int PyMemoryView_Check(PyObject *obj)
|
52 |
+
|
53 |
+
Return true if the object *obj* is a memoryview object. It is not
|
54 |
+
currently allowed to create subclasses of "memoryview". This
|
55 |
+
function always succeeds.
|
56 |
+
|
57 |
+
Py_buffer *PyMemoryView_GET_BUFFER(PyObject *mview)
|
58 |
+
|
59 |
+
Return a pointer to the memoryview's private copy of the exporter's
|
60 |
+
buffer. *mview* **must** be a memoryview instance; this macro
|
61 |
+
doesn't check its type, you must do it yourself or you will risk
|
62 |
+
crashes.
|
63 |
+
|
64 |
+
PyObject *PyMemoryView_GET_BASE(PyObject *mview)
|
65 |
+
|
66 |
+
Return either a pointer to the exporting object that the memoryview
|
67 |
+
is based on or "NULL" if the memoryview has been created by one of
|
68 |
+
the functions "PyMemoryView_FromMemory()" or
|
69 |
+
"PyMemoryView_FromBuffer()". *mview* **must** be a memoryview
|
70 |
+
instance.
|
python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/method.txt
ADDED
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Instance Method Objects
|
2 |
+
***********************
|
3 |
+
|
4 |
+
An instance method is a wrapper for a "PyCFunction" and the new way to
|
5 |
+
bind a "PyCFunction" to a class object. It replaces the former call
|
6 |
+
"PyMethod_New(func, NULL, class)".
|
7 |
+
|
8 |
+
PyTypeObject PyInstanceMethod_Type
|
9 |
+
|
10 |
+
This instance of "PyTypeObject" represents the Python instance
|
11 |
+
method type. It is not exposed to Python programs.
|
12 |
+
|
13 |
+
int PyInstanceMethod_Check(PyObject *o)
|
14 |
+
|
15 |
+
Return true if *o* is an instance method object (has type
|
16 |
+
"PyInstanceMethod_Type"). The parameter must not be "NULL". This
|
17 |
+
function always succeeds.
|
18 |
+
|
19 |
+
PyObject *PyInstanceMethod_New(PyObject *func)
|
20 |
+
*Return value: New reference.*
|
21 |
+
|
22 |
+
Return a new instance method object, with *func* being any callable
|
23 |
+
object. *func* is the function that will be called when the
|
24 |
+
instance method is called.
|
25 |
+
|
26 |
+
PyObject *PyInstanceMethod_Function(PyObject *im)
|
27 |
+
*Return value: Borrowed reference.*
|
28 |
+
|
29 |
+
Return the function object associated with the instance method
|
30 |
+
*im*.
|
31 |
+
|
32 |
+
PyObject *PyInstanceMethod_GET_FUNCTION(PyObject *im)
|
33 |
+
*Return value: Borrowed reference.*
|
34 |
+
|
35 |
+
Macro version of "PyInstanceMethod_Function()" which avoids error
|
36 |
+
checking.
|
37 |
+
|
38 |
+
|
39 |
+
Method Objects
|
40 |
+
**************
|
41 |
+
|
42 |
+
Methods are bound function objects. Methods are always bound to an
|
43 |
+
instance of a user-defined class. Unbound methods (methods bound to a
|
44 |
+
class object) are no longer available.
|
45 |
+
|
46 |
+
PyTypeObject PyMethod_Type
|
47 |
+
|
48 |
+
This instance of "PyTypeObject" represents the Python method type.
|
49 |
+
This is exposed to Python programs as "types.MethodType".
|
50 |
+
|
51 |
+
int PyMethod_Check(PyObject *o)
|
52 |
+
|
53 |
+
Return true if *o* is a method object (has type "PyMethod_Type").
|
54 |
+
The parameter must not be "NULL". This function always succeeds.
|
55 |
+
|
56 |
+
PyObject *PyMethod_New(PyObject *func, PyObject *self)
|
57 |
+
*Return value: New reference.*
|
58 |
+
|
59 |
+
Return a new method object, with *func* being any callable object
|
60 |
+
and *self* the instance the method should be bound. *func* is the
|
61 |
+
function that will be called when the method is called. *self* must
|
62 |
+
not be "NULL".
|
63 |
+
|
64 |
+
PyObject *PyMethod_Function(PyObject *meth)
|
65 |
+
*Return value: Borrowed reference.*
|
66 |
+
|
67 |
+
Return the function object associated with the method *meth*.
|
68 |
+
|
69 |
+
PyObject *PyMethod_GET_FUNCTION(PyObject *meth)
|
70 |
+
*Return value: Borrowed reference.*
|
71 |
+
|
72 |
+
Macro version of "PyMethod_Function()" which avoids error checking.
|
73 |
+
|
74 |
+
PyObject *PyMethod_Self(PyObject *meth)
|
75 |
+
*Return value: Borrowed reference.*
|
76 |
+
|
77 |
+
Return the instance associated with the method *meth*.
|
78 |
+
|
79 |
+
PyObject *PyMethod_GET_SELF(PyObject *meth)
|
80 |
+
*Return value: Borrowed reference.*
|
81 |
+
|
82 |
+
Macro version of "PyMethod_Self()" which avoids error checking.
|
python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/module.txt
ADDED
@@ -0,0 +1,668 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Module Objects
|
2 |
+
**************
|
3 |
+
|
4 |
+
PyTypeObject PyModule_Type
|
5 |
+
* Part of the Stable ABI.*
|
6 |
+
|
7 |
+
This instance of "PyTypeObject" represents the Python module type.
|
8 |
+
This is exposed to Python programs as "types.ModuleType".
|
9 |
+
|
10 |
+
int PyModule_Check(PyObject *p)
|
11 |
+
|
12 |
+
Return true if *p* is a module object, or a subtype of a module
|
13 |
+
object. This function always succeeds.
|
14 |
+
|
15 |
+
int PyModule_CheckExact(PyObject *p)
|
16 |
+
|
17 |
+
Return true if *p* is a module object, but not a subtype of
|
18 |
+
"PyModule_Type". This function always succeeds.
|
19 |
+
|
20 |
+
PyObject *PyModule_NewObject(PyObject *name)
|
21 |
+
*Return value: New reference.** Part of the Stable ABI since
|
22 |
+
version 3.7.*
|
23 |
+
|
24 |
+
Return a new module object with the "__name__" attribute set to
|
25 |
+
*name*. The module's "__name__", "__doc__", "__package__", and
|
26 |
+
"__loader__" attributes are filled in (all but "__name__" are set
|
27 |
+
to "None"); the caller is responsible for providing a "__file__"
|
28 |
+
attribute.
|
29 |
+
|
30 |
+
Return "NULL" with an exception set on error.
|
31 |
+
|
32 |
+
Added in version 3.3.
|
33 |
+
|
34 |
+
Changed in version 3.4: "__package__" and "__loader__" are set to
|
35 |
+
"None".
|
36 |
+
|
37 |
+
PyObject *PyModule_New(const char *name)
|
38 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
39 |
+
|
40 |
+
Similar to "PyModule_NewObject()", but the name is a UTF-8 encoded
|
41 |
+
string instead of a Unicode object.
|
42 |
+
|
43 |
+
PyObject *PyModule_GetDict(PyObject *module)
|
44 |
+
*Return value: Borrowed reference.** Part of the Stable ABI.*
|
45 |
+
|
46 |
+
Return the dictionary object that implements *module*'s namespace;
|
47 |
+
this object is the same as the "__dict__" attribute of the module
|
48 |
+
object. If *module* is not a module object (or a subtype of a
|
49 |
+
module object), "SystemError" is raised and "NULL" is returned.
|
50 |
+
|
51 |
+
It is recommended extensions use other "PyModule_*" and
|
52 |
+
"PyObject_*" functions rather than directly manipulate a module's
|
53 |
+
"__dict__".
|
54 |
+
|
55 |
+
PyObject *PyModule_GetNameObject(PyObject *module)
|
56 |
+
*Return value: New reference.** Part of the Stable ABI since
|
57 |
+
version 3.7.*
|
58 |
+
|
59 |
+
Return *module*'s "__name__" value. If the module does not provide
|
60 |
+
one, or if it is not a string, "SystemError" is raised and "NULL"
|
61 |
+
is returned.
|
62 |
+
|
63 |
+
Added in version 3.3.
|
64 |
+
|
65 |
+
const char *PyModule_GetName(PyObject *module)
|
66 |
+
* Part of the Stable ABI.*
|
67 |
+
|
68 |
+
Similar to "PyModule_GetNameObject()" but return the name encoded
|
69 |
+
to "'utf-8'".
|
70 |
+
|
71 |
+
void *PyModule_GetState(PyObject *module)
|
72 |
+
* Part of the Stable ABI.*
|
73 |
+
|
74 |
+
Return the "state" of the module, that is, a pointer to the block
|
75 |
+
of memory allocated at module creation time, or "NULL". See
|
76 |
+
"PyModuleDef.m_size".
|
77 |
+
|
78 |
+
PyModuleDef *PyModule_GetDef(PyObject *module)
|
79 |
+
* Part of the Stable ABI.*
|
80 |
+
|
81 |
+
Return a pointer to the "PyModuleDef" struct from which the module
|
82 |
+
was created, or "NULL" if the module wasn't created from a
|
83 |
+
definition.
|
84 |
+
|
85 |
+
PyObject *PyModule_GetFilenameObject(PyObject *module)
|
86 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
87 |
+
|
88 |
+
Return the name of the file from which *module* was loaded using
|
89 |
+
*module*'s "__file__" attribute. If this is not defined, or if it
|
90 |
+
is not a unicode string, raise "SystemError" and return "NULL";
|
91 |
+
otherwise return a reference to a Unicode object.
|
92 |
+
|
93 |
+
Added in version 3.2.
|
94 |
+
|
95 |
+
const char *PyModule_GetFilename(PyObject *module)
|
96 |
+
* Part of the Stable ABI.*
|
97 |
+
|
98 |
+
Similar to "PyModule_GetFilenameObject()" but return the filename
|
99 |
+
encoded to 'utf-8'.
|
100 |
+
|
101 |
+
Deprecated since version 3.2: "PyModule_GetFilename()" raises
|
102 |
+
"UnicodeEncodeError" on unencodable filenames, use
|
103 |
+
"PyModule_GetFilenameObject()" instead.
|
104 |
+
|
105 |
+
|
106 |
+
Initializing C modules
|
107 |
+
======================
|
108 |
+
|
109 |
+
Modules objects are usually created from extension modules (shared
|
110 |
+
libraries which export an initialization function), or compiled-in
|
111 |
+
modules (where the initialization function is added using
|
112 |
+
"PyImport_AppendInittab()"). See Building C and C++ Extensions or
|
113 |
+
Extending Embedded Python for details.
|
114 |
+
|
115 |
+
The initialization function can either pass a module definition
|
116 |
+
instance to "PyModule_Create()", and return the resulting module
|
117 |
+
object, or request "multi-phase initialization" by returning the
|
118 |
+
definition struct itself.
|
119 |
+
|
120 |
+
type PyModuleDef
|
121 |
+
* Part of the Stable ABI (including all members).*
|
122 |
+
|
123 |
+
The module definition struct, which holds all information needed to
|
124 |
+
create a module object. There is usually only one statically
|
125 |
+
initialized variable of this type for each module.
|
126 |
+
|
127 |
+
PyModuleDef_Base m_base
|
128 |
+
|
129 |
+
Always initialize this member to "PyModuleDef_HEAD_INIT".
|
130 |
+
|
131 |
+
const char *m_name
|
132 |
+
|
133 |
+
Name for the new module.
|
134 |
+
|
135 |
+
const char *m_doc
|
136 |
+
|
137 |
+
Docstring for the module; usually a docstring variable created
|
138 |
+
with "PyDoc_STRVAR" is used.
|
139 |
+
|
140 |
+
Py_ssize_t m_size
|
141 |
+
|
142 |
+
Module state may be kept in a per-module memory area that can be
|
143 |
+
retrieved with "PyModule_GetState()", rather than in static
|
144 |
+
globals. This makes modules safe for use in multiple sub-
|
145 |
+
interpreters.
|
146 |
+
|
147 |
+
This memory area is allocated based on *m_size* on module
|
148 |
+
creation, and freed when the module object is deallocated, after
|
149 |
+
the "m_free" function has been called, if present.
|
150 |
+
|
151 |
+
Setting "m_size" to "-1" means that the module does not support
|
152 |
+
sub-interpreters, because it has global state.
|
153 |
+
|
154 |
+
Setting it to a non-negative value means that the module can be
|
155 |
+
re-initialized and specifies the additional amount of memory it
|
156 |
+
requires for its state. Non-negative "m_size" is required for
|
157 |
+
multi-phase initialization.
|
158 |
+
|
159 |
+
See **PEP 3121** for more details.
|
160 |
+
|
161 |
+
PyMethodDef *m_methods
|
162 |
+
|
163 |
+
A pointer to a table of module-level functions, described by
|
164 |
+
"PyMethodDef" values. Can be "NULL" if no functions are
|
165 |
+
present.
|
166 |
+
|
167 |
+
PyModuleDef_Slot *m_slots
|
168 |
+
|
169 |
+
An array of slot definitions for multi-phase initialization,
|
170 |
+
terminated by a "{0, NULL}" entry. When using single-phase
|
171 |
+
initialization, *m_slots* must be "NULL".
|
172 |
+
|
173 |
+
Changed in version 3.5: Prior to version 3.5, this member was
|
174 |
+
always set to "NULL", and was defined as:
|
175 |
+
|
176 |
+
inquiry m_reload
|
177 |
+
|
178 |
+
traverseproc m_traverse
|
179 |
+
|
180 |
+
A traversal function to call during GC traversal of the module
|
181 |
+
object, or "NULL" if not needed.
|
182 |
+
|
183 |
+
This function is not called if the module state was requested
|
184 |
+
but is not allocated yet. This is the case immediately after the
|
185 |
+
module is created and before the module is executed
|
186 |
+
("Py_mod_exec" function). More precisely, this function is not
|
187 |
+
called if "m_size" is greater than 0 and the module state (as
|
188 |
+
returned by "PyModule_GetState()") is "NULL".
|
189 |
+
|
190 |
+
Changed in version 3.9: No longer called before the module state
|
191 |
+
is allocated.
|
192 |
+
|
193 |
+
inquiry m_clear
|
194 |
+
|
195 |
+
A clear function to call during GC clearing of the module
|
196 |
+
object, or "NULL" if not needed.
|
197 |
+
|
198 |
+
This function is not called if the module state was requested
|
199 |
+
but is not allocated yet. This is the case immediately after the
|
200 |
+
module is created and before the module is executed
|
201 |
+
("Py_mod_exec" function). More precisely, this function is not
|
202 |
+
called if "m_size" is greater than 0 and the module state (as
|
203 |
+
returned by "PyModule_GetState()") is "NULL".
|
204 |
+
|
205 |
+
Like "PyTypeObject.tp_clear", this function is not *always*
|
206 |
+
called before a module is deallocated. For example, when
|
207 |
+
reference counting is enough to determine that an object is no
|
208 |
+
longer used, the cyclic garbage collector is not involved and
|
209 |
+
"m_free" is called directly.
|
210 |
+
|
211 |
+
Changed in version 3.9: No longer called before the module state
|
212 |
+
is allocated.
|
213 |
+
|
214 |
+
freefunc m_free
|
215 |
+
|
216 |
+
A function to call during deallocation of the module object, or
|
217 |
+
"NULL" if not needed.
|
218 |
+
|
219 |
+
This function is not called if the module state was requested
|
220 |
+
but is not allocated yet. This is the case immediately after the
|
221 |
+
module is created and before the module is executed
|
222 |
+
("Py_mod_exec" function). More precisely, this function is not
|
223 |
+
called if "m_size" is greater than 0 and the module state (as
|
224 |
+
returned by "PyModule_GetState()") is "NULL".
|
225 |
+
|
226 |
+
Changed in version 3.9: No longer called before the module state
|
227 |
+
is allocated.
|
228 |
+
|
229 |
+
|
230 |
+
Single-phase initialization
|
231 |
+
---------------------------
|
232 |
+
|
233 |
+
The module initialization function may create and return the module
|
234 |
+
object directly. This is referred to as "single-phase initialization",
|
235 |
+
and uses one of the following two module creation functions:
|
236 |
+
|
237 |
+
PyObject *PyModule_Create(PyModuleDef *def)
|
238 |
+
*Return value: New reference.*
|
239 |
+
|
240 |
+
Create a new module object, given the definition in *def*. This
|
241 |
+
behaves like "PyModule_Create2()" with *module_api_version* set to
|
242 |
+
"PYTHON_API_VERSION".
|
243 |
+
|
244 |
+
PyObject *PyModule_Create2(PyModuleDef *def, int module_api_version)
|
245 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
246 |
+
|
247 |
+
Create a new module object, given the definition in *def*, assuming
|
248 |
+
the API version *module_api_version*. If that version does not
|
249 |
+
match the version of the running interpreter, a "RuntimeWarning" is
|
250 |
+
emitted.
|
251 |
+
|
252 |
+
Return "NULL" with an exception set on error.
|
253 |
+
|
254 |
+
Note:
|
255 |
+
|
256 |
+
Most uses of this function should be using "PyModule_Create()"
|
257 |
+
instead; only use this if you are sure you need it.
|
258 |
+
|
259 |
+
Before it is returned from in the initialization function, the
|
260 |
+
resulting module object is typically populated using functions like
|
261 |
+
"PyModule_AddObjectRef()".
|
262 |
+
|
263 |
+
|
264 |
+
Multi-phase initialization
|
265 |
+
--------------------------
|
266 |
+
|
267 |
+
An alternate way to specify extensions is to request "multi-phase
|
268 |
+
initialization". Extension modules created this way behave more like
|
269 |
+
Python modules: the initialization is split between the *creation
|
270 |
+
phase*, when the module object is created, and the *execution phase*,
|
271 |
+
when it is populated. The distinction is similar to the "__new__()"
|
272 |
+
and "__init__()" methods of classes.
|
273 |
+
|
274 |
+
Unlike modules created using single-phase initialization, these
|
275 |
+
modules are not singletons: if the *sys.modules* entry is removed and
|
276 |
+
the module is re-imported, a new module object is created, and the old
|
277 |
+
module is subject to normal garbage collection -- as with Python
|
278 |
+
modules. By default, multiple modules created from the same definition
|
279 |
+
should be independent: changes to one should not affect the others.
|
280 |
+
This means that all state should be specific to the module object
|
281 |
+
(using e.g. using "PyModule_GetState()"), or its contents (such as the
|
282 |
+
module's "__dict__" or individual classes created with
|
283 |
+
"PyType_FromSpec()").
|
284 |
+
|
285 |
+
All modules created using multi-phase initialization are expected to
|
286 |
+
support sub-interpreters. Making sure multiple modules are independent
|
287 |
+
is typically enough to achieve this.
|
288 |
+
|
289 |
+
To request multi-phase initialization, the initialization function
|
290 |
+
(PyInit_modulename) returns a "PyModuleDef" instance with non-empty
|
291 |
+
"m_slots". Before it is returned, the "PyModuleDef" instance must be
|
292 |
+
initialized with the following function:
|
293 |
+
|
294 |
+
PyObject *PyModuleDef_Init(PyModuleDef *def)
|
295 |
+
*Return value: Borrowed reference.** Part of the Stable ABI since
|
296 |
+
version 3.5.*
|
297 |
+
|
298 |
+
Ensures a module definition is a properly initialized Python object
|
299 |
+
that correctly reports its type and reference count.
|
300 |
+
|
301 |
+
Returns *def* cast to "PyObject*", or "NULL" if an error occurred.
|
302 |
+
|
303 |
+
Added in version 3.5.
|
304 |
+
|
305 |
+
The *m_slots* member of the module definition must point to an array
|
306 |
+
of "PyModuleDef_Slot" structures:
|
307 |
+
|
308 |
+
type PyModuleDef_Slot
|
309 |
+
|
310 |
+
int slot
|
311 |
+
|
312 |
+
A slot ID, chosen from the available values explained below.
|
313 |
+
|
314 |
+
void *value
|
315 |
+
|
316 |
+
Value of the slot, whose meaning depends on the slot ID.
|
317 |
+
|
318 |
+
Added in version 3.5.
|
319 |
+
|
320 |
+
The *m_slots* array must be terminated by a slot with id 0.
|
321 |
+
|
322 |
+
The available slot types are:
|
323 |
+
|
324 |
+
Py_mod_create
|
325 |
+
|
326 |
+
Specifies a function that is called to create the module object
|
327 |
+
itself. The *value* pointer of this slot must point to a function
|
328 |
+
of the signature:
|
329 |
+
|
330 |
+
PyObject *create_module(PyObject *spec, PyModuleDef *def)
|
331 |
+
|
332 |
+
The function receives a "ModuleSpec" instance, as defined in **PEP
|
333 |
+
451**, and the module definition. It should return a new module
|
334 |
+
object, or set an error and return "NULL".
|
335 |
+
|
336 |
+
This function should be kept minimal. In particular, it should not
|
337 |
+
call arbitrary Python code, as trying to import the same module
|
338 |
+
again may result in an infinite loop.
|
339 |
+
|
340 |
+
Multiple "Py_mod_create" slots may not be specified in one module
|
341 |
+
definition.
|
342 |
+
|
343 |
+
If "Py_mod_create" is not specified, the import machinery will
|
344 |
+
create a normal module object using "PyModule_New()". The name is
|
345 |
+
taken from *spec*, not the definition, to allow extension modules
|
346 |
+
to dynamically adjust to their place in the module hierarchy and be
|
347 |
+
imported under different names through symlinks, all while sharing
|
348 |
+
a single module definition.
|
349 |
+
|
350 |
+
There is no requirement for the returned object to be an instance
|
351 |
+
of "PyModule_Type". Any type can be used, as long as it supports
|
352 |
+
setting and getting import-related attributes. However, only
|
353 |
+
"PyModule_Type" instances may be returned if the "PyModuleDef" has
|
354 |
+
non-"NULL" "m_traverse", "m_clear", "m_free"; non-zero "m_size"; or
|
355 |
+
slots other than "Py_mod_create".
|
356 |
+
|
357 |
+
Py_mod_exec
|
358 |
+
|
359 |
+
Specifies a function that is called to *execute* the module. This
|
360 |
+
is equivalent to executing the code of a Python module: typically,
|
361 |
+
this function adds classes and constants to the module. The
|
362 |
+
signature of the function is:
|
363 |
+
|
364 |
+
int exec_module(PyObject *module)
|
365 |
+
|
366 |
+
If multiple "Py_mod_exec" slots are specified, they are processed
|
367 |
+
in the order they appear in the *m_slots* array.
|
368 |
+
|
369 |
+
Py_mod_multiple_interpreters
|
370 |
+
|
371 |
+
Specifies one of the following values:
|
372 |
+
|
373 |
+
Py_MOD_MULTIPLE_INTERPRETERS_NOT_SUPPORTED
|
374 |
+
|
375 |
+
The module does not support being imported in subinterpreters.
|
376 |
+
|
377 |
+
Py_MOD_MULTIPLE_INTERPRETERS_SUPPORTED
|
378 |
+
|
379 |
+
The module supports being imported in subinterpreters, but only
|
380 |
+
when they share the main interpreter's GIL. (See Isolating
|
381 |
+
Extension Modules.)
|
382 |
+
|
383 |
+
Py_MOD_PER_INTERPRETER_GIL_SUPPORTED
|
384 |
+
|
385 |
+
The module supports being imported in subinterpreters, even when
|
386 |
+
they have their own GIL. (See Isolating Extension Modules.)
|
387 |
+
|
388 |
+
This slot determines whether or not importing this module in a
|
389 |
+
subinterpreter will fail.
|
390 |
+
|
391 |
+
Multiple "Py_mod_multiple_interpreters" slots may not be specified
|
392 |
+
in one module definition.
|
393 |
+
|
394 |
+
If "Py_mod_multiple_interpreters" is not specified, the import
|
395 |
+
machinery defaults to "Py_MOD_MULTIPLE_INTERPRETERS_NOT_SUPPORTED".
|
396 |
+
|
397 |
+
Added in version 3.12.
|
398 |
+
|
399 |
+
See **PEP 489** for more details on multi-phase initialization.
|
400 |
+
|
401 |
+
|
402 |
+
Low-level module creation functions
|
403 |
+
-----------------------------------
|
404 |
+
|
405 |
+
The following functions are called under the hood when using multi-
|
406 |
+
phase initialization. They can be used directly, for example when
|
407 |
+
creating module objects dynamically. Note that both
|
408 |
+
"PyModule_FromDefAndSpec" and "PyModule_ExecDef" must be called to
|
409 |
+
fully initialize a module.
|
410 |
+
|
411 |
+
PyObject *PyModule_FromDefAndSpec(PyModuleDef *def, PyObject *spec)
|
412 |
+
*Return value: New reference.*
|
413 |
+
|
414 |
+
Create a new module object, given the definition in *def* and the
|
415 |
+
ModuleSpec *spec*. This behaves like "PyModule_FromDefAndSpec2()"
|
416 |
+
with *module_api_version* set to "PYTHON_API_VERSION".
|
417 |
+
|
418 |
+
Added in version 3.5.
|
419 |
+
|
420 |
+
PyObject *PyModule_FromDefAndSpec2(PyModuleDef *def, PyObject *spec, int module_api_version)
|
421 |
+
*Return value: New reference.** Part of the Stable ABI since
|
422 |
+
version 3.7.*
|
423 |
+
|
424 |
+
Create a new module object, given the definition in *def* and the
|
425 |
+
ModuleSpec *spec*, assuming the API version *module_api_version*.
|
426 |
+
If that version does not match the version of the running
|
427 |
+
interpreter, a "RuntimeWarning" is emitted.
|
428 |
+
|
429 |
+
Return "NULL" with an exception set on error.
|
430 |
+
|
431 |
+
Note:
|
432 |
+
|
433 |
+
Most uses of this function should be using
|
434 |
+
"PyModule_FromDefAndSpec()" instead; only use this if you are
|
435 |
+
sure you need it.
|
436 |
+
|
437 |
+
Added in version 3.5.
|
438 |
+
|
439 |
+
int PyModule_ExecDef(PyObject *module, PyModuleDef *def)
|
440 |
+
* Part of the Stable ABI since version 3.7.*
|
441 |
+
|
442 |
+
Process any execution slots ("Py_mod_exec") given in *def*.
|
443 |
+
|
444 |
+
Added in version 3.5.
|
445 |
+
|
446 |
+
int PyModule_SetDocString(PyObject *module, const char *docstring)
|
447 |
+
* Part of the Stable ABI since version 3.7.*
|
448 |
+
|
449 |
+
Set the docstring for *module* to *docstring*. This function is
|
450 |
+
called automatically when creating a module from "PyModuleDef",
|
451 |
+
using either "PyModule_Create" or "PyModule_FromDefAndSpec".
|
452 |
+
|
453 |
+
Added in version 3.5.
|
454 |
+
|
455 |
+
int PyModule_AddFunctions(PyObject *module, PyMethodDef *functions)
|
456 |
+
* Part of the Stable ABI since version 3.7.*
|
457 |
+
|
458 |
+
Add the functions from the "NULL" terminated *functions* array to
|
459 |
+
*module*. Refer to the "PyMethodDef" documentation for details on
|
460 |
+
individual entries (due to the lack of a shared module namespace,
|
461 |
+
module level "functions" implemented in C typically receive the
|
462 |
+
module as their first parameter, making them similar to instance
|
463 |
+
methods on Python classes). This function is called automatically
|
464 |
+
when creating a module from "PyModuleDef", using either
|
465 |
+
"PyModule_Create" or "PyModule_FromDefAndSpec".
|
466 |
+
|
467 |
+
Added in version 3.5.
|
468 |
+
|
469 |
+
|
470 |
+
Support functions
|
471 |
+
-----------------
|
472 |
+
|
473 |
+
The module initialization function (if using single phase
|
474 |
+
initialization) or a function called from a module execution slot (if
|
475 |
+
using multi-phase initialization), can use the following functions to
|
476 |
+
help initialize the module state:
|
477 |
+
|
478 |
+
int PyModule_AddObjectRef(PyObject *module, const char *name, PyObject *value)
|
479 |
+
* Part of the Stable ABI since version 3.10.*
|
480 |
+
|
481 |
+
Add an object to *module* as *name*. This is a convenience
|
482 |
+
function which can be used from the module's initialization
|
483 |
+
function.
|
484 |
+
|
485 |
+
On success, return "0". On error, raise an exception and return
|
486 |
+
"-1".
|
487 |
+
|
488 |
+
Return "-1" if *value* is "NULL". It must be called with an
|
489 |
+
exception raised in this case.
|
490 |
+
|
491 |
+
Example usage:
|
492 |
+
|
493 |
+
static int
|
494 |
+
add_spam(PyObject *module, int value)
|
495 |
+
{
|
496 |
+
PyObject *obj = PyLong_FromLong(value);
|
497 |
+
if (obj == NULL) {
|
498 |
+
return -1;
|
499 |
+
}
|
500 |
+
int res = PyModule_AddObjectRef(module, "spam", obj);
|
501 |
+
Py_DECREF(obj);
|
502 |
+
return res;
|
503 |
+
}
|
504 |
+
|
505 |
+
The example can also be written without checking explicitly if
|
506 |
+
*obj* is "NULL":
|
507 |
+
|
508 |
+
static int
|
509 |
+
add_spam(PyObject *module, int value)
|
510 |
+
{
|
511 |
+
PyObject *obj = PyLong_FromLong(value);
|
512 |
+
int res = PyModule_AddObjectRef(module, "spam", obj);
|
513 |
+
Py_XDECREF(obj);
|
514 |
+
return res;
|
515 |
+
}
|
516 |
+
|
517 |
+
Note that "Py_XDECREF()" should be used instead of "Py_DECREF()" in
|
518 |
+
this case, since *obj* can be "NULL".
|
519 |
+
|
520 |
+
Added in version 3.10.
|
521 |
+
|
522 |
+
int PyModule_AddObject(PyObject *module, const char *name, PyObject *value)
|
523 |
+
* Part of the Stable ABI.*
|
524 |
+
|
525 |
+
Similar to "PyModule_AddObjectRef()", but steals a reference to
|
526 |
+
*value* on success (if it returns "0").
|
527 |
+
|
528 |
+
The new "PyModule_AddObjectRef()" function is recommended, since it
|
529 |
+
is easy to introduce reference leaks by misusing the
|
530 |
+
"PyModule_AddObject()" function.
|
531 |
+
|
532 |
+
Note:
|
533 |
+
|
534 |
+
Unlike other functions that steal references,
|
535 |
+
"PyModule_AddObject()" only releases the reference to *value*
|
536 |
+
**on success**.This means that its return value must be checked,
|
537 |
+
and calling code must "Py_DECREF()" *value* manually on error.
|
538 |
+
|
539 |
+
Example usage:
|
540 |
+
|
541 |
+
static int
|
542 |
+
add_spam(PyObject *module, int value)
|
543 |
+
{
|
544 |
+
PyObject *obj = PyLong_FromLong(value);
|
545 |
+
if (obj == NULL) {
|
546 |
+
return -1;
|
547 |
+
}
|
548 |
+
if (PyModule_AddObject(module, "spam", obj) < 0) {
|
549 |
+
Py_DECREF(obj);
|
550 |
+
return -1;
|
551 |
+
}
|
552 |
+
// PyModule_AddObject() stole a reference to obj:
|
553 |
+
// Py_DECREF(obj) is not needed here
|
554 |
+
return 0;
|
555 |
+
}
|
556 |
+
|
557 |
+
The example can also be written without checking explicitly if
|
558 |
+
*obj* is "NULL":
|
559 |
+
|
560 |
+
static int
|
561 |
+
add_spam(PyObject *module, int value)
|
562 |
+
{
|
563 |
+
PyObject *obj = PyLong_FromLong(value);
|
564 |
+
if (PyModule_AddObject(module, "spam", obj) < 0) {
|
565 |
+
Py_XDECREF(obj);
|
566 |
+
return -1;
|
567 |
+
}
|
568 |
+
// PyModule_AddObject() stole a reference to obj:
|
569 |
+
// Py_DECREF(obj) is not needed here
|
570 |
+
return 0;
|
571 |
+
}
|
572 |
+
|
573 |
+
Note that "Py_XDECREF()" should be used instead of "Py_DECREF()" in
|
574 |
+
this case, since *obj* can be "NULL".
|
575 |
+
|
576 |
+
int PyModule_AddIntConstant(PyObject *module, const char *name, long value)
|
577 |
+
* Part of the Stable ABI.*
|
578 |
+
|
579 |
+
Add an integer constant to *module* as *name*. This convenience
|
580 |
+
function can be used from the module's initialization function.
|
581 |
+
Return "-1" with an exception set on error, "0" on success.
|
582 |
+
|
583 |
+
int PyModule_AddStringConstant(PyObject *module, const char *name, const char *value)
|
584 |
+
* Part of the Stable ABI.*
|
585 |
+
|
586 |
+
Add a string constant to *module* as *name*. This convenience
|
587 |
+
function can be used from the module's initialization function.
|
588 |
+
The string *value* must be "NULL"-terminated. Return "-1" with an
|
589 |
+
exception set on error, "0" on success.
|
590 |
+
|
591 |
+
PyModule_AddIntMacro(module, macro)
|
592 |
+
|
593 |
+
Add an int constant to *module*. The name and the value are taken
|
594 |
+
from *macro*. For example "PyModule_AddIntMacro(module, AF_INET)"
|
595 |
+
adds the int constant *AF_INET* with the value of *AF_INET* to
|
596 |
+
*module*. Return "-1" with an exception set on error, "0" on
|
597 |
+
success.
|
598 |
+
|
599 |
+
PyModule_AddStringMacro(module, macro)
|
600 |
+
|
601 |
+
Add a string constant to *module*.
|
602 |
+
|
603 |
+
int PyModule_AddType(PyObject *module, PyTypeObject *type)
|
604 |
+
* Part of the Stable ABI since version 3.10.*
|
605 |
+
|
606 |
+
Add a type object to *module*. The type object is finalized by
|
607 |
+
calling internally "PyType_Ready()". The name of the type object is
|
608 |
+
taken from the last component of "tp_name" after dot. Return "-1"
|
609 |
+
with an exception set on error, "0" on success.
|
610 |
+
|
611 |
+
Added in version 3.9.
|
612 |
+
|
613 |
+
|
614 |
+
Module lookup
|
615 |
+
=============
|
616 |
+
|
617 |
+
Single-phase initialization creates singleton modules that can be
|
618 |
+
looked up in the context of the current interpreter. This allows the
|
619 |
+
module object to be retrieved later with only a reference to the
|
620 |
+
module definition.
|
621 |
+
|
622 |
+
These functions will not work on modules created using multi-phase
|
623 |
+
initialization, since multiple such modules can be created from a
|
624 |
+
single definition.
|
625 |
+
|
626 |
+
PyObject *PyState_FindModule(PyModuleDef *def)
|
627 |
+
*Return value: Borrowed reference.** Part of the Stable ABI.*
|
628 |
+
|
629 |
+
Returns the module object that was created from *def* for the
|
630 |
+
current interpreter. This method requires that the module object
|
631 |
+
has been attached to the interpreter state with
|
632 |
+
"PyState_AddModule()" beforehand. In case the corresponding module
|
633 |
+
object is not found or has not been attached to the interpreter
|
634 |
+
state yet, it returns "NULL".
|
635 |
+
|
636 |
+
int PyState_AddModule(PyObject *module, PyModuleDef *def)
|
637 |
+
* Part of the Stable ABI since version 3.3.*
|
638 |
+
|
639 |
+
Attaches the module object passed to the function to the
|
640 |
+
interpreter state. This allows the module object to be accessible
|
641 |
+
via "PyState_FindModule()".
|
642 |
+
|
643 |
+
Only effective on modules created using single-phase
|
644 |
+
initialization.
|
645 |
+
|
646 |
+
Python calls "PyState_AddModule" automatically after importing a
|
647 |
+
module, so it is unnecessary (but harmless) to call it from module
|
648 |
+
initialization code. An explicit call is needed only if the
|
649 |
+
module's own init code subsequently calls "PyState_FindModule". The
|
650 |
+
function is mainly intended for implementing alternative import
|
651 |
+
mechanisms (either by calling it directly, or by referring to its
|
652 |
+
implementation for details of the required state updates).
|
653 |
+
|
654 |
+
The caller must hold the GIL.
|
655 |
+
|
656 |
+
Return "-1" with an exception set on error, "0" on success.
|
657 |
+
|
658 |
+
Added in version 3.3.
|
659 |
+
|
660 |
+
int PyState_RemoveModule(PyModuleDef *def)
|
661 |
+
* Part of the Stable ABI since version 3.3.*
|
662 |
+
|
663 |
+
Removes the module object created from *def* from the interpreter
|
664 |
+
state. Return "-1" with an exception set on error, "0" on success.
|
665 |
+
|
666 |
+
The caller must hold the GIL.
|
667 |
+
|
668 |
+
Added in version 3.3.
|
python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/none.txt
ADDED
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
The "None" Object
|
2 |
+
*****************
|
3 |
+
|
4 |
+
Note that the "PyTypeObject" for "None" is not directly exposed in the
|
5 |
+
Python/C API. Since "None" is a singleton, testing for object
|
6 |
+
identity (using "==" in C) is sufficient. There is no "PyNone_Check()"
|
7 |
+
function for the same reason.
|
8 |
+
|
9 |
+
PyObject *Py_None
|
10 |
+
|
11 |
+
The Python "None" object, denoting lack of value. This object has
|
12 |
+
no methods and is immortal.
|
13 |
+
|
14 |
+
Changed in version 3.12: "Py_None" is immortal.
|
15 |
+
|
16 |
+
Py_RETURN_NONE
|
17 |
+
|
18 |
+
Return "Py_None" from a function.
|
python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/number.txt
ADDED
@@ -0,0 +1,297 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Number Protocol
|
2 |
+
***************
|
3 |
+
|
4 |
+
int PyNumber_Check(PyObject *o)
|
5 |
+
* Part of the Stable ABI.*
|
6 |
+
|
7 |
+
Returns "1" if the object *o* provides numeric protocols, and false
|
8 |
+
otherwise. This function always succeeds.
|
9 |
+
|
10 |
+
Changed in version 3.8: Returns "1" if *o* is an index integer.
|
11 |
+
|
12 |
+
PyObject *PyNumber_Add(PyObject *o1, PyObject *o2)
|
13 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
14 |
+
|
15 |
+
Returns the result of adding *o1* and *o2*, or "NULL" on failure.
|
16 |
+
This is the equivalent of the Python expression "o1 + o2".
|
17 |
+
|
18 |
+
PyObject *PyNumber_Subtract(PyObject *o1, PyObject *o2)
|
19 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
20 |
+
|
21 |
+
Returns the result of subtracting *o2* from *o1*, or "NULL" on
|
22 |
+
failure. This is the equivalent of the Python expression "o1 -
|
23 |
+
o2".
|
24 |
+
|
25 |
+
PyObject *PyNumber_Multiply(PyObject *o1, PyObject *o2)
|
26 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
27 |
+
|
28 |
+
Returns the result of multiplying *o1* and *o2*, or "NULL" on
|
29 |
+
failure. This is the equivalent of the Python expression "o1 *
|
30 |
+
o2".
|
31 |
+
|
32 |
+
PyObject *PyNumber_MatrixMultiply(PyObject *o1, PyObject *o2)
|
33 |
+
*Return value: New reference.** Part of the Stable ABI since
|
34 |
+
version 3.7.*
|
35 |
+
|
36 |
+
Returns the result of matrix multiplication on *o1* and *o2*, or
|
37 |
+
"NULL" on failure. This is the equivalent of the Python expression
|
38 |
+
"o1 @ o2".
|
39 |
+
|
40 |
+
Added in version 3.5.
|
41 |
+
|
42 |
+
PyObject *PyNumber_FloorDivide(PyObject *o1, PyObject *o2)
|
43 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
44 |
+
|
45 |
+
Return the floor of *o1* divided by *o2*, or "NULL" on failure.
|
46 |
+
This is the equivalent of the Python expression "o1 // o2".
|
47 |
+
|
48 |
+
PyObject *PyNumber_TrueDivide(PyObject *o1, PyObject *o2)
|
49 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
50 |
+
|
51 |
+
Return a reasonable approximation for the mathematical value of
|
52 |
+
*o1* divided by *o2*, or "NULL" on failure. The return value is
|
53 |
+
"approximate" because binary floating-point numbers are
|
54 |
+
approximate; it is not possible to represent all real numbers in
|
55 |
+
base two. This function can return a floating-point value when
|
56 |
+
passed two integers. This is the equivalent of the Python
|
57 |
+
expression "o1 / o2".
|
58 |
+
|
59 |
+
PyObject *PyNumber_Remainder(PyObject *o1, PyObject *o2)
|
60 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
61 |
+
|
62 |
+
Returns the remainder of dividing *o1* by *o2*, or "NULL" on
|
63 |
+
failure. This is the equivalent of the Python expression "o1 %
|
64 |
+
o2".
|
65 |
+
|
66 |
+
PyObject *PyNumber_Divmod(PyObject *o1, PyObject *o2)
|
67 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
68 |
+
|
69 |
+
See the built-in function "divmod()". Returns "NULL" on failure.
|
70 |
+
This is the equivalent of the Python expression "divmod(o1, o2)".
|
71 |
+
|
72 |
+
PyObject *PyNumber_Power(PyObject *o1, PyObject *o2, PyObject *o3)
|
73 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
74 |
+
|
75 |
+
See the built-in function "pow()". Returns "NULL" on failure. This
|
76 |
+
is the equivalent of the Python expression "pow(o1, o2, o3)", where
|
77 |
+
*o3* is optional. If *o3* is to be ignored, pass "Py_None" in its
|
78 |
+
place (passing "NULL" for *o3* would cause an illegal memory
|
79 |
+
access).
|
80 |
+
|
81 |
+
PyObject *PyNumber_Negative(PyObject *o)
|
82 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
83 |
+
|
84 |
+
Returns the negation of *o* on success, or "NULL" on failure. This
|
85 |
+
is the equivalent of the Python expression "-o".
|
86 |
+
|
87 |
+
PyObject *PyNumber_Positive(PyObject *o)
|
88 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
89 |
+
|
90 |
+
Returns *o* on success, or "NULL" on failure. This is the
|
91 |
+
equivalent of the Python expression "+o".
|
92 |
+
|
93 |
+
PyObject *PyNumber_Absolute(PyObject *o)
|
94 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
95 |
+
|
96 |
+
Returns the absolute value of *o*, or "NULL" on failure. This is
|
97 |
+
the equivalent of the Python expression "abs(o)".
|
98 |
+
|
99 |
+
PyObject *PyNumber_Invert(PyObject *o)
|
100 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
101 |
+
|
102 |
+
Returns the bitwise negation of *o* on success, or "NULL" on
|
103 |
+
failure. This is the equivalent of the Python expression "~o".
|
104 |
+
|
105 |
+
PyObject *PyNumber_Lshift(PyObject *o1, PyObject *o2)
|
106 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
107 |
+
|
108 |
+
Returns the result of left shifting *o1* by *o2* on success, or
|
109 |
+
"NULL" on failure. This is the equivalent of the Python expression
|
110 |
+
"o1 << o2".
|
111 |
+
|
112 |
+
PyObject *PyNumber_Rshift(PyObject *o1, PyObject *o2)
|
113 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
114 |
+
|
115 |
+
Returns the result of right shifting *o1* by *o2* on success, or
|
116 |
+
"NULL" on failure. This is the equivalent of the Python expression
|
117 |
+
"o1 >> o2".
|
118 |
+
|
119 |
+
PyObject *PyNumber_And(PyObject *o1, PyObject *o2)
|
120 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
121 |
+
|
122 |
+
Returns the "bitwise and" of *o1* and *o2* on success and "NULL" on
|
123 |
+
failure. This is the equivalent of the Python expression "o1 & o2".
|
124 |
+
|
125 |
+
PyObject *PyNumber_Xor(PyObject *o1, PyObject *o2)
|
126 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
127 |
+
|
128 |
+
Returns the "bitwise exclusive or" of *o1* by *o2* on success, or
|
129 |
+
"NULL" on failure. This is the equivalent of the Python expression
|
130 |
+
"o1 ^ o2".
|
131 |
+
|
132 |
+
PyObject *PyNumber_Or(PyObject *o1, PyObject *o2)
|
133 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
134 |
+
|
135 |
+
Returns the "bitwise or" of *o1* and *o2* on success, or "NULL" on
|
136 |
+
failure. This is the equivalent of the Python expression "o1 | o2".
|
137 |
+
|
138 |
+
PyObject *PyNumber_InPlaceAdd(PyObject *o1, PyObject *o2)
|
139 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
140 |
+
|
141 |
+
Returns the result of adding *o1* and *o2*, or "NULL" on failure.
|
142 |
+
The operation is done *in-place* when *o1* supports it. This is
|
143 |
+
the equivalent of the Python statement "o1 += o2".
|
144 |
+
|
145 |
+
PyObject *PyNumber_InPlaceSubtract(PyObject *o1, PyObject *o2)
|
146 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
147 |
+
|
148 |
+
Returns the result of subtracting *o2* from *o1*, or "NULL" on
|
149 |
+
failure. The operation is done *in-place* when *o1* supports it.
|
150 |
+
This is the equivalent of the Python statement "o1 -= o2".
|
151 |
+
|
152 |
+
PyObject *PyNumber_InPlaceMultiply(PyObject *o1, PyObject *o2)
|
153 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
154 |
+
|
155 |
+
Returns the result of multiplying *o1* and *o2*, or "NULL" on
|
156 |
+
failure. The operation is done *in-place* when *o1* supports it.
|
157 |
+
This is the equivalent of the Python statement "o1 *= o2".
|
158 |
+
|
159 |
+
PyObject *PyNumber_InPlaceMatrixMultiply(PyObject *o1, PyObject *o2)
|
160 |
+
*Return value: New reference.** Part of the Stable ABI since
|
161 |
+
version 3.7.*
|
162 |
+
|
163 |
+
Returns the result of matrix multiplication on *o1* and *o2*, or
|
164 |
+
"NULL" on failure. The operation is done *in-place* when *o1*
|
165 |
+
supports it. This is the equivalent of the Python statement "o1 @=
|
166 |
+
o2".
|
167 |
+
|
168 |
+
Added in version 3.5.
|
169 |
+
|
170 |
+
PyObject *PyNumber_InPlaceFloorDivide(PyObject *o1, PyObject *o2)
|
171 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
172 |
+
|
173 |
+
Returns the mathematical floor of dividing *o1* by *o2*, or "NULL"
|
174 |
+
on failure. The operation is done *in-place* when *o1* supports it.
|
175 |
+
This is the equivalent of the Python statement "o1 //= o2".
|
176 |
+
|
177 |
+
PyObject *PyNumber_InPlaceTrueDivide(PyObject *o1, PyObject *o2)
|
178 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
179 |
+
|
180 |
+
Return a reasonable approximation for the mathematical value of
|
181 |
+
*o1* divided by *o2*, or "NULL" on failure. The return value is
|
182 |
+
"approximate" because binary floating-point numbers are
|
183 |
+
approximate; it is not possible to represent all real numbers in
|
184 |
+
base two. This function can return a floating-point value when
|
185 |
+
passed two integers. The operation is done *in-place* when *o1*
|
186 |
+
supports it. This is the equivalent of the Python statement "o1 /=
|
187 |
+
o2".
|
188 |
+
|
189 |
+
PyObject *PyNumber_InPlaceRemainder(PyObject *o1, PyObject *o2)
|
190 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
191 |
+
|
192 |
+
Returns the remainder of dividing *o1* by *o2*, or "NULL" on
|
193 |
+
failure. The operation is done *in-place* when *o1* supports it.
|
194 |
+
This is the equivalent of the Python statement "o1 %= o2".
|
195 |
+
|
196 |
+
PyObject *PyNumber_InPlacePower(PyObject *o1, PyObject *o2, PyObject *o3)
|
197 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
198 |
+
|
199 |
+
See the built-in function "pow()". Returns "NULL" on failure. The
|
200 |
+
operation is done *in-place* when *o1* supports it. This is the
|
201 |
+
equivalent of the Python statement "o1 **= o2" when o3 is
|
202 |
+
"Py_None", or an in-place variant of "pow(o1, o2, o3)" otherwise.
|
203 |
+
If *o3* is to be ignored, pass "Py_None" in its place (passing
|
204 |
+
"NULL" for *o3* would cause an illegal memory access).
|
205 |
+
|
206 |
+
PyObject *PyNumber_InPlaceLshift(PyObject *o1, PyObject *o2)
|
207 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
208 |
+
|
209 |
+
Returns the result of left shifting *o1* by *o2* on success, or
|
210 |
+
"NULL" on failure. The operation is done *in-place* when *o1*
|
211 |
+
supports it. This is the equivalent of the Python statement "o1
|
212 |
+
<<= o2".
|
213 |
+
|
214 |
+
PyObject *PyNumber_InPlaceRshift(PyObject *o1, PyObject *o2)
|
215 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
216 |
+
|
217 |
+
Returns the result of right shifting *o1* by *o2* on success, or
|
218 |
+
"NULL" on failure. The operation is done *in-place* when *o1*
|
219 |
+
supports it. This is the equivalent of the Python statement "o1
|
220 |
+
>>= o2".
|
221 |
+
|
222 |
+
PyObject *PyNumber_InPlaceAnd(PyObject *o1, PyObject *o2)
|
223 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
224 |
+
|
225 |
+
Returns the "bitwise and" of *o1* and *o2* on success and "NULL" on
|
226 |
+
failure. The operation is done *in-place* when *o1* supports it.
|
227 |
+
This is the equivalent of the Python statement "o1 &= o2".
|
228 |
+
|
229 |
+
PyObject *PyNumber_InPlaceXor(PyObject *o1, PyObject *o2)
|
230 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
231 |
+
|
232 |
+
Returns the "bitwise exclusive or" of *o1* by *o2* on success, or
|
233 |
+
"NULL" on failure. The operation is done *in-place* when *o1*
|
234 |
+
supports it. This is the equivalent of the Python statement "o1 ^=
|
235 |
+
o2".
|
236 |
+
|
237 |
+
PyObject *PyNumber_InPlaceOr(PyObject *o1, PyObject *o2)
|
238 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
239 |
+
|
240 |
+
Returns the "bitwise or" of *o1* and *o2* on success, or "NULL" on
|
241 |
+
failure. The operation is done *in-place* when *o1* supports it.
|
242 |
+
This is the equivalent of the Python statement "o1 |= o2".
|
243 |
+
|
244 |
+
PyObject *PyNumber_Long(PyObject *o)
|
245 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
246 |
+
|
247 |
+
Returns the *o* converted to an integer object on success, or
|
248 |
+
"NULL" on failure. This is the equivalent of the Python expression
|
249 |
+
"int(o)".
|
250 |
+
|
251 |
+
PyObject *PyNumber_Float(PyObject *o)
|
252 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
253 |
+
|
254 |
+
Returns the *o* converted to a float object on success, or "NULL"
|
255 |
+
on failure. This is the equivalent of the Python expression
|
256 |
+
"float(o)".
|
257 |
+
|
258 |
+
PyObject *PyNumber_Index(PyObject *o)
|
259 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
260 |
+
|
261 |
+
Returns the *o* converted to a Python int on success or "NULL" with
|
262 |
+
a "TypeError" exception raised on failure.
|
263 |
+
|
264 |
+
Changed in version 3.10: The result always has exact type "int".
|
265 |
+
Previously, the result could have been an instance of a subclass of
|
266 |
+
"int".
|
267 |
+
|
268 |
+
PyObject *PyNumber_ToBase(PyObject *n, int base)
|
269 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
270 |
+
|
271 |
+
Returns the integer *n* converted to base *base* as a string. The
|
272 |
+
*base* argument must be one of 2, 8, 10, or 16. For base 2, 8, or
|
273 |
+
16, the returned string is prefixed with a base marker of "'0b'",
|
274 |
+
"'0o'", or "'0x'", respectively. If *n* is not a Python int, it is
|
275 |
+
converted with "PyNumber_Index()" first.
|
276 |
+
|
277 |
+
Py_ssize_t PyNumber_AsSsize_t(PyObject *o, PyObject *exc)
|
278 |
+
* Part of the Stable ABI.*
|
279 |
+
|
280 |
+
Returns *o* converted to a "Py_ssize_t" value if *o* can be
|
281 |
+
interpreted as an integer. If the call fails, an exception is
|
282 |
+
raised and "-1" is returned.
|
283 |
+
|
284 |
+
If *o* can be converted to a Python int but the attempt to convert
|
285 |
+
to a "Py_ssize_t" value would raise an "OverflowError", then the
|
286 |
+
*exc* argument is the type of exception that will be raised
|
287 |
+
(usually "IndexError" or "OverflowError"). If *exc* is "NULL",
|
288 |
+
then the exception is cleared and the value is clipped to
|
289 |
+
"PY_SSIZE_T_MIN" for a negative integer or "PY_SSIZE_T_MAX" for a
|
290 |
+
positive integer.
|
291 |
+
|
292 |
+
int PyIndex_Check(PyObject *o)
|
293 |
+
* Part of the Stable ABI since version 3.8.*
|
294 |
+
|
295 |
+
Returns "1" if *o* is an index integer (has the "nb_index" slot of
|
296 |
+
the "tp_as_number" structure filled in), and "0" otherwise. This
|
297 |
+
function always succeeds.
|
python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/objbuffer.txt
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Old Buffer Protocol
|
2 |
+
*******************
|
3 |
+
|
4 |
+
Deprecated since version 3.0.
|
5 |
+
|
6 |
+
These functions were part of the "old buffer protocol" API in Python
|
7 |
+
2. In Python 3, this protocol doesn't exist anymore but the functions
|
8 |
+
are still exposed to ease porting 2.x code. They act as a
|
9 |
+
compatibility wrapper around the new buffer protocol, but they don't
|
10 |
+
give you control over the lifetime of the resources acquired when a
|
11 |
+
buffer is exported.
|
12 |
+
|
13 |
+
Therefore, it is recommended that you call "PyObject_GetBuffer()" (or
|
14 |
+
the "y*" or "w*" format codes with the "PyArg_ParseTuple()" family of
|
15 |
+
functions) to get a buffer view over an object, and
|
16 |
+
"PyBuffer_Release()" when the buffer view can be released.
|
17 |
+
|
18 |
+
int PyObject_AsCharBuffer(PyObject *obj, const char **buffer, Py_ssize_t *buffer_len)
|
19 |
+
* Part of the Stable ABI.*
|
20 |
+
|
21 |
+
Returns a pointer to a read-only memory location usable as
|
22 |
+
character-based input. The *obj* argument must support the single-
|
23 |
+
segment character buffer interface. On success, returns "0", sets
|
24 |
+
*buffer* to the memory location and *buffer_len* to the buffer
|
25 |
+
length. Returns "-1" and sets a "TypeError" on error.
|
26 |
+
|
27 |
+
int PyObject_AsReadBuffer(PyObject *obj, const void **buffer, Py_ssize_t *buffer_len)
|
28 |
+
* Part of the Stable ABI.*
|
29 |
+
|
30 |
+
Returns a pointer to a read-only memory location containing
|
31 |
+
arbitrary data. The *obj* argument must support the single-segment
|
32 |
+
readable buffer interface. On success, returns "0", sets *buffer*
|
33 |
+
to the memory location and *buffer_len* to the buffer length.
|
34 |
+
Returns "-1" and sets a "TypeError" on error.
|
35 |
+
|
36 |
+
int PyObject_CheckReadBuffer(PyObject *o)
|
37 |
+
* Part of the Stable ABI.*
|
38 |
+
|
39 |
+
Returns "1" if *o* supports the single-segment readable buffer
|
40 |
+
interface. Otherwise returns "0". This function always succeeds.
|
41 |
+
|
42 |
+
Note that this function tries to get and release a buffer, and
|
43 |
+
exceptions which occur while calling corresponding functions will
|
44 |
+
get suppressed. To get error reporting use "PyObject_GetBuffer()"
|
45 |
+
instead.
|
46 |
+
|
47 |
+
int PyObject_AsWriteBuffer(PyObject *obj, void **buffer, Py_ssize_t *buffer_len)
|
48 |
+
* Part of the Stable ABI.*
|
49 |
+
|
50 |
+
Returns a pointer to a writable memory location. The *obj*
|
51 |
+
argument must support the single-segment, character buffer
|
52 |
+
interface. On success, returns "0", sets *buffer* to the memory
|
53 |
+
location and *buffer_len* to the buffer length. Returns "-1" and
|
54 |
+
sets a "TypeError" on error.
|
python-3.12.6-docs-text/python-3.12.6-docs-text/c-api/object.txt
ADDED
@@ -0,0 +1,437 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Object Protocol
|
2 |
+
***************
|
3 |
+
|
4 |
+
PyObject *Py_NotImplemented
|
5 |
+
|
6 |
+
The "NotImplemented" singleton, used to signal that an operation is
|
7 |
+
not implemented for the given type combination.
|
8 |
+
|
9 |
+
Py_RETURN_NOTIMPLEMENTED
|
10 |
+
|
11 |
+
Properly handle returning "Py_NotImplemented" from within a C
|
12 |
+
function (that is, create a new *strong reference* to
|
13 |
+
"NotImplemented" and return it).
|
14 |
+
|
15 |
+
Py_PRINT_RAW
|
16 |
+
|
17 |
+
Flag to be used with multiple functions that print the object (like
|
18 |
+
"PyObject_Print()" and "PyFile_WriteObject()"). If passed, these
|
19 |
+
function would use the "str()" of the object instead of the
|
20 |
+
"repr()".
|
21 |
+
|
22 |
+
int PyObject_Print(PyObject *o, FILE *fp, int flags)
|
23 |
+
|
24 |
+
Print an object *o*, on file *fp*. Returns "-1" on error. The
|
25 |
+
flags argument is used to enable certain printing options. The
|
26 |
+
only option currently supported is "Py_PRINT_RAW"; if given, the
|
27 |
+
"str()" of the object is written instead of the "repr()".
|
28 |
+
|
29 |
+
int PyObject_HasAttr(PyObject *o, PyObject *attr_name)
|
30 |
+
* Part of the Stable ABI.*
|
31 |
+
|
32 |
+
Returns "1" if *o* has the attribute *attr_name*, and "0"
|
33 |
+
otherwise. This is equivalent to the Python expression "hasattr(o,
|
34 |
+
attr_name)". This function always succeeds.
|
35 |
+
|
36 |
+
Note:
|
37 |
+
|
38 |
+
Exceptions that occur when this calls "__getattr__()" and
|
39 |
+
"__getattribute__()" methods are silently ignored. For proper
|
40 |
+
error handling, use "PyObject_GetAttr()" instead.
|
41 |
+
|
42 |
+
int PyObject_HasAttrString(PyObject *o, const char *attr_name)
|
43 |
+
* Part of the Stable ABI.*
|
44 |
+
|
45 |
+
This is the same as "PyObject_HasAttr()", but *attr_name* is
|
46 |
+
specified as a const char* UTF-8 encoded bytes string, rather than
|
47 |
+
a PyObject*.
|
48 |
+
|
49 |
+
Note:
|
50 |
+
|
51 |
+
Exceptions that occur when this calls "__getattr__()" and
|
52 |
+
"__getattribute__()" methods or while creating the temporary
|
53 |
+
"str" object are silently ignored. For proper error handling, use
|
54 |
+
"PyObject_GetAttrString()" instead.
|
55 |
+
|
56 |
+
PyObject *PyObject_GetAttr(PyObject *o, PyObject *attr_name)
|
57 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
58 |
+
|
59 |
+
Retrieve an attribute named *attr_name* from object *o*. Returns
|
60 |
+
the attribute value on success, or "NULL" on failure. This is the
|
61 |
+
equivalent of the Python expression "o.attr_name".
|
62 |
+
|
63 |
+
PyObject *PyObject_GetAttrString(PyObject *o, const char *attr_name)
|
64 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
65 |
+
|
66 |
+
This is the same as "PyObject_GetAttr()", but *attr_name* is
|
67 |
+
specified as a const char* UTF-8 encoded bytes string, rather than
|
68 |
+
a PyObject*.
|
69 |
+
|
70 |
+
PyObject *PyObject_GenericGetAttr(PyObject *o, PyObject *name)
|
71 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
72 |
+
|
73 |
+
Generic attribute getter function that is meant to be put into a
|
74 |
+
type object's "tp_getattro" slot. It looks for a descriptor in the
|
75 |
+
dictionary of classes in the object's MRO as well as an attribute
|
76 |
+
in the object's "__dict__" (if present). As outlined in
|
77 |
+
Implementing Descriptors, data descriptors take preference over
|
78 |
+
instance attributes, while non-data descriptors don't. Otherwise,
|
79 |
+
an "AttributeError" is raised.
|
80 |
+
|
81 |
+
int PyObject_SetAttr(PyObject *o, PyObject *attr_name, PyObject *v)
|
82 |
+
* Part of the Stable ABI.*
|
83 |
+
|
84 |
+
Set the value of the attribute named *attr_name*, for object *o*,
|
85 |
+
to the value *v*. Raise an exception and return "-1" on failure;
|
86 |
+
return "0" on success. This is the equivalent of the Python
|
87 |
+
statement "o.attr_name = v".
|
88 |
+
|
89 |
+
If *v* is "NULL", the attribute is deleted. This behaviour is
|
90 |
+
deprecated in favour of using "PyObject_DelAttr()", but there are
|
91 |
+
currently no plans to remove it.
|
92 |
+
|
93 |
+
int PyObject_SetAttrString(PyObject *o, const char *attr_name, PyObject *v)
|
94 |
+
* Part of the Stable ABI.*
|
95 |
+
|
96 |
+
This is the same as "PyObject_SetAttr()", but *attr_name* is
|
97 |
+
specified as a const char* UTF-8 encoded bytes string, rather than
|
98 |
+
a PyObject*.
|
99 |
+
|
100 |
+
If *v* is "NULL", the attribute is deleted, but this feature is
|
101 |
+
deprecated in favour of using "PyObject_DelAttrString()".
|
102 |
+
|
103 |
+
int PyObject_GenericSetAttr(PyObject *o, PyObject *name, PyObject *value)
|
104 |
+
* Part of the Stable ABI.*
|
105 |
+
|
106 |
+
Generic attribute setter and deleter function that is meant to be
|
107 |
+
put into a type object's "tp_setattro" slot. It looks for a data
|
108 |
+
descriptor in the dictionary of classes in the object's MRO, and if
|
109 |
+
found it takes preference over setting or deleting the attribute in
|
110 |
+
the instance dictionary. Otherwise, the attribute is set or deleted
|
111 |
+
in the object's "__dict__" (if present). On success, "0" is
|
112 |
+
returned, otherwise an "AttributeError" is raised and "-1" is
|
113 |
+
returned.
|
114 |
+
|
115 |
+
int PyObject_DelAttr(PyObject *o, PyObject *attr_name)
|
116 |
+
|
117 |
+
Delete attribute named *attr_name*, for object *o*. Returns "-1" on
|
118 |
+
failure. This is the equivalent of the Python statement "del
|
119 |
+
o.attr_name".
|
120 |
+
|
121 |
+
int PyObject_DelAttrString(PyObject *o, const char *attr_name)
|
122 |
+
|
123 |
+
This is the same as "PyObject_DelAttr()", but *attr_name* is
|
124 |
+
specified as a const char* UTF-8 encoded bytes string, rather than
|
125 |
+
a PyObject*.
|
126 |
+
|
127 |
+
PyObject *PyObject_GenericGetDict(PyObject *o, void *context)
|
128 |
+
*Return value: New reference.** Part of the Stable ABI since
|
129 |
+
version 3.10.*
|
130 |
+
|
131 |
+
A generic implementation for the getter of a "__dict__" descriptor.
|
132 |
+
It creates the dictionary if necessary.
|
133 |
+
|
134 |
+
This function may also be called to get the "__dict__" of the
|
135 |
+
object *o*. Pass "NULL" for *context* when calling it. Since this
|
136 |
+
function may need to allocate memory for the dictionary, it may be
|
137 |
+
more efficient to call "PyObject_GetAttr()" when accessing an
|
138 |
+
attribute on the object.
|
139 |
+
|
140 |
+
On failure, returns "NULL" with an exception set.
|
141 |
+
|
142 |
+
Added in version 3.3.
|
143 |
+
|
144 |
+
int PyObject_GenericSetDict(PyObject *o, PyObject *value, void *context)
|
145 |
+
* Part of the Stable ABI since version 3.7.*
|
146 |
+
|
147 |
+
A generic implementation for the setter of a "__dict__" descriptor.
|
148 |
+
This implementation does not allow the dictionary to be deleted.
|
149 |
+
|
150 |
+
Added in version 3.3.
|
151 |
+
|
152 |
+
PyObject **_PyObject_GetDictPtr(PyObject *obj)
|
153 |
+
|
154 |
+
Return a pointer to "__dict__" of the object *obj*. If there is no
|
155 |
+
"__dict__", return "NULL" without setting an exception.
|
156 |
+
|
157 |
+
This function may need to allocate memory for the dictionary, so it
|
158 |
+
may be more efficient to call "PyObject_GetAttr()" when accessing
|
159 |
+
an attribute on the object.
|
160 |
+
|
161 |
+
PyObject *PyObject_RichCompare(PyObject *o1, PyObject *o2, int opid)
|
162 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
163 |
+
|
164 |
+
Compare the values of *o1* and *o2* using the operation specified
|
165 |
+
by *opid*, which must be one of "Py_LT", "Py_LE", "Py_EQ", "Py_NE",
|
166 |
+
"Py_GT", or "Py_GE", corresponding to "<", "<=", "==", "!=", ">",
|
167 |
+
or ">=" respectively. This is the equivalent of the Python
|
168 |
+
expression "o1 op o2", where "op" is the operator corresponding to
|
169 |
+
*opid*. Returns the value of the comparison on success, or "NULL"
|
170 |
+
on failure.
|
171 |
+
|
172 |
+
int PyObject_RichCompareBool(PyObject *o1, PyObject *o2, int opid)
|
173 |
+
* Part of the Stable ABI.*
|
174 |
+
|
175 |
+
Compare the values of *o1* and *o2* using the operation specified
|
176 |
+
by *opid*, like "PyObject_RichCompare()", but returns "-1" on
|
177 |
+
error, "0" if the result is false, "1" otherwise.
|
178 |
+
|
179 |
+
Note:
|
180 |
+
|
181 |
+
If *o1* and *o2* are the same object, "PyObject_RichCompareBool()"
|
182 |
+
will always return "1" for "Py_EQ" and "0" for "Py_NE".
|
183 |
+
|
184 |
+
PyObject *PyObject_Format(PyObject *obj, PyObject *format_spec)
|
185 |
+
* Part of the Stable ABI.*
|
186 |
+
|
187 |
+
Format *obj* using *format_spec*. This is equivalent to the Python
|
188 |
+
expression "format(obj, format_spec)".
|
189 |
+
|
190 |
+
*format_spec* may be "NULL". In this case the call is equivalent to
|
191 |
+
"format(obj)". Returns the formatted string on success, "NULL" on
|
192 |
+
failure.
|
193 |
+
|
194 |
+
PyObject *PyObject_Repr(PyObject *o)
|
195 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
196 |
+
|
197 |
+
Compute a string representation of object *o*. Returns the string
|
198 |
+
representation on success, "NULL" on failure. This is the
|
199 |
+
equivalent of the Python expression "repr(o)". Called by the
|
200 |
+
"repr()" built-in function.
|
201 |
+
|
202 |
+
Changed in version 3.4: This function now includes a debug
|
203 |
+
assertion to help ensure that it does not silently discard an
|
204 |
+
active exception.
|
205 |
+
|
206 |
+
PyObject *PyObject_ASCII(PyObject *o)
|
207 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
208 |
+
|
209 |
+
As "PyObject_Repr()", compute a string representation of object
|
210 |
+
*o*, but escape the non-ASCII characters in the string returned by
|
211 |
+
"PyObject_Repr()" with "\x", "\u" or "\U" escapes. This generates
|
212 |
+
a string similar to that returned by "PyObject_Repr()" in Python 2.
|
213 |
+
Called by the "ascii()" built-in function.
|
214 |
+
|
215 |
+
PyObject *PyObject_Str(PyObject *o)
|
216 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
217 |
+
|
218 |
+
Compute a string representation of object *o*. Returns the string
|
219 |
+
representation on success, "NULL" on failure. This is the
|
220 |
+
equivalent of the Python expression "str(o)". Called by the
|
221 |
+
"str()" built-in function and, therefore, by the "print()"
|
222 |
+
function.
|
223 |
+
|
224 |
+
Changed in version 3.4: This function now includes a debug
|
225 |
+
assertion to help ensure that it does not silently discard an
|
226 |
+
active exception.
|
227 |
+
|
228 |
+
PyObject *PyObject_Bytes(PyObject *o)
|
229 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
230 |
+
|
231 |
+
Compute a bytes representation of object *o*. "NULL" is returned
|
232 |
+
on failure and a bytes object on success. This is equivalent to
|
233 |
+
the Python expression "bytes(o)", when *o* is not an integer.
|
234 |
+
Unlike "bytes(o)", a TypeError is raised when *o* is an integer
|
235 |
+
instead of a zero-initialized bytes object.
|
236 |
+
|
237 |
+
int PyObject_IsSubclass(PyObject *derived, PyObject *cls)
|
238 |
+
* Part of the Stable ABI.*
|
239 |
+
|
240 |
+
Return "1" if the class *derived* is identical to or derived from
|
241 |
+
the class *cls*, otherwise return "0". In case of an error, return
|
242 |
+
"-1".
|
243 |
+
|
244 |
+
If *cls* is a tuple, the check will be done against every entry in
|
245 |
+
*cls*. The result will be "1" when at least one of the checks
|
246 |
+
returns "1", otherwise it will be "0".
|
247 |
+
|
248 |
+
If *cls* has a "__subclasscheck__()" method, it will be called to
|
249 |
+
determine the subclass status as described in **PEP 3119**.
|
250 |
+
Otherwise, *derived* is a subclass of *cls* if it is a direct or
|
251 |
+
indirect subclass, i.e. contained in "cls.__mro__".
|
252 |
+
|
253 |
+
Normally only class objects, i.e. instances of "type" or a derived
|
254 |
+
class, are considered classes. However, objects can override this
|
255 |
+
by having a "__bases__" attribute (which must be a tuple of base
|
256 |
+
classes).
|
257 |
+
|
258 |
+
int PyObject_IsInstance(PyObject *inst, PyObject *cls)
|
259 |
+
* Part of the Stable ABI.*
|
260 |
+
|
261 |
+
Return "1" if *inst* is an instance of the class *cls* or a
|
262 |
+
subclass of *cls*, or "0" if not. On error, returns "-1" and sets
|
263 |
+
an exception.
|
264 |
+
|
265 |
+
If *cls* is a tuple, the check will be done against every entry in
|
266 |
+
*cls*. The result will be "1" when at least one of the checks
|
267 |
+
returns "1", otherwise it will be "0".
|
268 |
+
|
269 |
+
If *cls* has a "__instancecheck__()" method, it will be called to
|
270 |
+
determine the subclass status as described in **PEP 3119**.
|
271 |
+
Otherwise, *inst* is an instance of *cls* if its class is a
|
272 |
+
subclass of *cls*.
|
273 |
+
|
274 |
+
An instance *inst* can override what is considered its class by
|
275 |
+
having a "__class__" attribute.
|
276 |
+
|
277 |
+
An object *cls* can override if it is considered a class, and what
|
278 |
+
its base classes are, by having a "__bases__" attribute (which must
|
279 |
+
be a tuple of base classes).
|
280 |
+
|
281 |
+
Py_hash_t PyObject_Hash(PyObject *o)
|
282 |
+
* Part of the Stable ABI.*
|
283 |
+
|
284 |
+
Compute and return the hash value of an object *o*. On failure,
|
285 |
+
return "-1". This is the equivalent of the Python expression
|
286 |
+
"hash(o)".
|
287 |
+
|
288 |
+
Changed in version 3.2: The return type is now Py_hash_t. This is
|
289 |
+
a signed integer the same size as "Py_ssize_t".
|
290 |
+
|
291 |
+
Py_hash_t PyObject_HashNotImplemented(PyObject *o)
|
292 |
+
* Part of the Stable ABI.*
|
293 |
+
|
294 |
+
Set a "TypeError" indicating that "type(o)" is not *hashable* and
|
295 |
+
return "-1". This function receives special treatment when stored
|
296 |
+
in a "tp_hash" slot, allowing a type to explicitly indicate to the
|
297 |
+
interpreter that it is not hashable.
|
298 |
+
|
299 |
+
int PyObject_IsTrue(PyObject *o)
|
300 |
+
* Part of the Stable ABI.*
|
301 |
+
|
302 |
+
Returns "1" if the object *o* is considered to be true, and "0"
|
303 |
+
otherwise. This is equivalent to the Python expression "not not o".
|
304 |
+
On failure, return "-1".
|
305 |
+
|
306 |
+
int PyObject_Not(PyObject *o)
|
307 |
+
* Part of the Stable ABI.*
|
308 |
+
|
309 |
+
Returns "0" if the object *o* is considered to be true, and "1"
|
310 |
+
otherwise. This is equivalent to the Python expression "not o". On
|
311 |
+
failure, return "-1".
|
312 |
+
|
313 |
+
PyObject *PyObject_Type(PyObject *o)
|
314 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
315 |
+
|
316 |
+
When *o* is non-"NULL", returns a type object corresponding to the
|
317 |
+
object type of object *o*. On failure, raises "SystemError" and
|
318 |
+
returns "NULL". This is equivalent to the Python expression
|
319 |
+
"type(o)". This function creates a new *strong reference* to the
|
320 |
+
return value. There's really no reason to use this function instead
|
321 |
+
of the "Py_TYPE()" function, which returns a pointer of type
|
322 |
+
PyTypeObject*, except when a new *strong reference* is needed.
|
323 |
+
|
324 |
+
int PyObject_TypeCheck(PyObject *o, PyTypeObject *type)
|
325 |
+
|
326 |
+
Return non-zero if the object *o* is of type *type* or a subtype of
|
327 |
+
*type*, and "0" otherwise. Both parameters must be non-"NULL".
|
328 |
+
|
329 |
+
Py_ssize_t PyObject_Size(PyObject *o)
|
330 |
+
Py_ssize_t PyObject_Length(PyObject *o)
|
331 |
+
* Part of the Stable ABI.*
|
332 |
+
|
333 |
+
Return the length of object *o*. If the object *o* provides either
|
334 |
+
the sequence and mapping protocols, the sequence length is
|
335 |
+
returned. On error, "-1" is returned. This is the equivalent to
|
336 |
+
the Python expression "len(o)".
|
337 |
+
|
338 |
+
Py_ssize_t PyObject_LengthHint(PyObject *o, Py_ssize_t defaultvalue)
|
339 |
+
|
340 |
+
Return an estimated length for the object *o*. First try to return
|
341 |
+
its actual length, then an estimate using "__length_hint__()", and
|
342 |
+
finally return the default value. On error return "-1". This is the
|
343 |
+
equivalent to the Python expression "operator.length_hint(o,
|
344 |
+
defaultvalue)".
|
345 |
+
|
346 |
+
Added in version 3.4.
|
347 |
+
|
348 |
+
PyObject *PyObject_GetItem(PyObject *o, PyObject *key)
|
349 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
350 |
+
|
351 |
+
Return element of *o* corresponding to the object *key* or "NULL"
|
352 |
+
on failure. This is the equivalent of the Python expression
|
353 |
+
"o[key]".
|
354 |
+
|
355 |
+
int PyObject_SetItem(PyObject *o, PyObject *key, PyObject *v)
|
356 |
+
* Part of the Stable ABI.*
|
357 |
+
|
358 |
+
Map the object *key* to the value *v*. Raise an exception and
|
359 |
+
return "-1" on failure; return "0" on success. This is the
|
360 |
+
equivalent of the Python statement "o[key] = v". This function
|
361 |
+
*does not* steal a reference to *v*.
|
362 |
+
|
363 |
+
int PyObject_DelItem(PyObject *o, PyObject *key)
|
364 |
+
* Part of the Stable ABI.*
|
365 |
+
|
366 |
+
Remove the mapping for the object *key* from the object *o*.
|
367 |
+
Return "-1" on failure. This is equivalent to the Python statement
|
368 |
+
"del o[key]".
|
369 |
+
|
370 |
+
PyObject *PyObject_Dir(PyObject *o)
|
371 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
372 |
+
|
373 |
+
This is equivalent to the Python expression "dir(o)", returning a
|
374 |
+
(possibly empty) list of strings appropriate for the object
|
375 |
+
argument, or "NULL" if there was an error. If the argument is
|
376 |
+
"NULL", this is like the Python "dir()", returning the names of the
|
377 |
+
current locals; in this case, if no execution frame is active then
|
378 |
+
"NULL" is returned but "PyErr_Occurred()" will return false.
|
379 |
+
|
380 |
+
PyObject *PyObject_GetIter(PyObject *o)
|
381 |
+
*Return value: New reference.** Part of the Stable ABI.*
|
382 |
+
|
383 |
+
This is equivalent to the Python expression "iter(o)". It returns a
|
384 |
+
new iterator for the object argument, or the object itself if the
|
385 |
+
object is already an iterator. Raises "TypeError" and returns
|
386 |
+
"NULL" if the object cannot be iterated.
|
387 |
+
|
388 |
+
PyObject *PyObject_GetAIter(PyObject *o)
|
389 |
+
*Return value: New reference.** Part of the Stable ABI since
|
390 |
+
version 3.10.*
|
391 |
+
|
392 |
+
This is the equivalent to the Python expression "aiter(o)". Takes
|
393 |
+
an "AsyncIterable" object and returns an "AsyncIterator" for it.
|
394 |
+
This is typically a new iterator but if the argument is an
|
395 |
+
"AsyncIterator", this returns itself. Raises "TypeError" and
|
396 |
+
returns "NULL" if the object cannot be iterated.
|
397 |
+
|
398 |
+
Added in version 3.10.
|
399 |
+
|
400 |
+
void *PyObject_GetTypeData(PyObject *o, PyTypeObject *cls)
|
401 |
+
* Part of the Stable ABI since version 3.12.*
|
402 |
+
|
403 |
+
Get a pointer to subclass-specific data reserved for *cls*.
|
404 |
+
|
405 |
+
The object *o* must be an instance of *cls*, and *cls* must have
|
406 |
+
been created using negative "PyType_Spec.basicsize". Python does
|
407 |
+
not check this.
|
408 |
+
|
409 |
+
On error, set an exception and return "NULL".
|
410 |
+
|
411 |
+
Added in version 3.12.
|
412 |
+
|
413 |
+
Py_ssize_t PyType_GetTypeDataSize(PyTypeObject *cls)
|
414 |
+
* Part of the Stable ABI since version 3.12.*
|
415 |
+
|
416 |
+
Return the size of the instance memory space reserved for *cls*,
|
417 |
+
i.e. the size of the memory "PyObject_GetTypeData()" returns.
|
418 |
+
|
419 |
+
This may be larger than requested using "-PyType_Spec.basicsize";
|
420 |
+
it is safe to use this larger size (e.g. with "memset()").
|
421 |
+
|
422 |
+
The type *cls* **must** have been created using negative
|
423 |
+
"PyType_Spec.basicsize". Python does not check this.
|
424 |
+
|
425 |
+
On error, set an exception and return a negative value.
|
426 |
+
|
427 |
+
Added in version 3.12.
|
428 |
+
|
429 |
+
void *PyObject_GetItemData(PyObject *o)
|
430 |
+
|
431 |
+
Get a pointer to per-item data for a class with
|
432 |
+
"Py_TPFLAGS_ITEMS_AT_END".
|
433 |
+
|
434 |
+
On error, set an exception and return "NULL". "TypeError" is raised
|
435 |
+
if *o* does not have "Py_TPFLAGS_ITEMS_AT_END" set.
|
436 |
+
|
437 |
+
Added in version 3.12.
|