Skip to content
This repository was archived by the owner on Feb 13, 2025. It is now read-only.

Commit dc4a5ef

Browse files
author
Anselm Kruis
committed
Merge branch master into master-slp
2 parents 2260f0c + 3899b54 commit dc4a5ef

File tree

429 files changed

+11510
-6369
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

429 files changed

+11510
-6369
lines changed

Doc/c-api/arg.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ variable(s) whose address should be passed.
3030
Strings and buffers
3131
-------------------
3232

33-
These formats allow to access an object as a contiguous chunk of memory.
33+
These formats allow accessing an object as a contiguous chunk of memory.
3434
You don't have to provide raw storage for the returned unicode or bytes
3535
area. Also, you won't have to release any memory yourself, except with the
3636
``es``, ``es#``, ``et`` and ``et#`` formats.

Doc/c-api/exceptions.rst

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ Printing and clearing
7474
:meth:`__del__` method.
7575
7676
The function is called with a single argument *obj* that identifies the context
77-
in which the unraisable exception occurred. The repr of *obj* will be printed in
78-
the warning message.
77+
in which the unraisable exception occurred. If possible,
78+
the repr of *obj* will be printed in the warning message.
7979
8080
8181
Raising exceptions
@@ -334,6 +334,14 @@ an error value).
334334
.. versionadded:: 3.2
335335
336336
337+
.. c:function:: int PyErr_ResourceWarning(PyObject *source, Py_ssize_t stack_level, const char *format, ...)
338+
339+
Function similar to :c:func:`PyErr_WarnFormat`, but *category* is
340+
:exc:`ResourceWarning` and pass *source* to :func:`warnings.WarningMessage`.
341+
342+
.. versionadded:: 3.6
343+
344+
337345
Querying the error indicator
338346
============================
339347

Doc/c-api/function.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ There are a few functions specific to Python functions.
4040
4141
.. c:function:: PyObject* PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname)
4242
43-
As :c:func:`PyFunction_New`, but also allows to set the function object's
43+
As :c:func:`PyFunction_New`, but also allows setting the function object's
4444
``__qualname__`` attribute. *qualname* should be a unicode object or NULL;
4545
if NULL, the ``__qualname__`` attribute is set to the same value as its
4646
``__name__`` attribute.

Doc/c-api/intro.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,10 @@ The header files are typically installed with Python. On Unix, these are
6464
located in the directories :file:`{prefix}/include/pythonversion/` and
6565
:file:`{exec_prefix}/include/pythonversion/`, where :envvar:`prefix` and
6666
:envvar:`exec_prefix` are defined by the corresponding parameters to Python's
67-
:program:`configure` script and *version* is ``sys.version[:3]``. On Windows,
68-
the headers are installed in :file:`{prefix}/include`, where :envvar:`prefix` is
69-
the installation directory specified to the installer.
67+
:program:`configure` script and *version* is
68+
``'%d.%d' % sys.version_info[:2]``. On Windows, the headers are installed
69+
in :file:`{prefix}/include`, where :envvar:`prefix` is the installation
70+
directory specified to the installer.
7071

7172
To include the headers, place both directories (if different) on your compiler's
7273
search path for includes. Do *not* place the parent directories on the search

Doc/c-api/memory.rst

Lines changed: 117 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,15 @@ collection, memory compaction or other preventive procedures. Note that by using
8383
the C library allocator as shown in the previous example, the allocated memory
8484
for the I/O buffer escapes completely the Python memory manager.
8585

86+
.. seealso::
87+
88+
The :envvar:`PYTHONMALLOC` environment variable can be used to configure
89+
the memory allocators used by Python.
90+
91+
The :envvar:`PYTHONMALLOCSTATS` environment variable can be used to print
92+
statistics of the :ref:`pymalloc memory allocator <pymalloc>` every time a
93+
new pymalloc object arena is created, and on shutdown.
94+
8695

8796
Raw Memory Interface
8897
====================
@@ -100,41 +109,51 @@ The default raw memory block allocator uses the following functions:
100109
.. c:function:: void* PyMem_RawMalloc(size_t n)
101110
102111
Allocates *n* bytes and returns a pointer of type :c:type:`void\*` to the
103-
allocated memory, or *NULL* if the request fails. Requesting zero bytes
104-
returns a distinct non-*NULL* pointer if possible, as if
105-
``PyMem_RawMalloc(1)`` had been called instead. The memory will not have
112+
allocated memory, or *NULL* if the request fails.
113+
114+
Requesting zero bytes returns a distinct non-*NULL* pointer if possible, as
115+
if ``PyMem_RawMalloc(1)`` had been called instead. The memory will not have
106116
been initialized in any way.
107117
108118
109119
.. c:function:: void* PyMem_RawCalloc(size_t nelem, size_t elsize)
110120
111121
Allocates *nelem* elements each whose size in bytes is *elsize* and returns
112122
a pointer of type :c:type:`void\*` to the allocated memory, or *NULL* if the
113-
request fails. The memory is initialized to zeros. Requesting zero elements
114-
or elements of size zero bytes returns a distinct non-*NULL* pointer if
115-
possible, as if ``PyMem_RawCalloc(1, 1)`` had been called instead.
123+
request fails. The memory is initialized to zeros.
124+
125+
Requesting zero elements or elements of size zero bytes returns a distinct
126+
non-*NULL* pointer if possible, as if ``PyMem_RawCalloc(1, 1)`` had been
127+
called instead.
116128
117129
.. versionadded:: 3.5
118130
119131
120132
.. c:function:: void* PyMem_RawRealloc(void *p, size_t n)
121133
122134
Resizes the memory block pointed to by *p* to *n* bytes. The contents will
123-
be unchanged to the minimum of the old and the new sizes. If *p* is *NULL*,
124-
the call is equivalent to ``PyMem_RawMalloc(n)``; else if *n* is equal to
125-
zero, the memory block is resized but is not freed, and the returned pointer
126-
is non-*NULL*. Unless *p* is *NULL*, it must have been returned by a
127-
previous call to :c:func:`PyMem_RawMalloc` or :c:func:`PyMem_RawRealloc`. If
128-
the request fails, :c:func:`PyMem_RawRealloc` returns *NULL* and *p* remains
129-
a valid pointer to the previous memory area.
135+
be unchanged to the minimum of the old and the new sizes.
136+
137+
If *p* is *NULL*, the call is equivalent to ``PyMem_RawMalloc(n)``; else if
138+
*n* is equal to zero, the memory block is resized but is not freed, and the
139+
returned pointer is non-*NULL*.
140+
141+
Unless *p* is *NULL*, it must have been returned by a previous call to
142+
:c:func:`PyMem_RawMalloc`, :c:func:`PyMem_RawRealloc` or
143+
:c:func:`PyMem_RawCalloc`.
144+
145+
If the request fails, :c:func:`PyMem_RawRealloc` returns *NULL* and *p*
146+
remains a valid pointer to the previous memory area.
130147
131148
132149
.. c:function:: void PyMem_RawFree(void *p)
133150
134151
Frees the memory block pointed to by *p*, which must have been returned by a
135-
previous call to :c:func:`PyMem_RawMalloc` or :c:func:`PyMem_RawRealloc`.
136-
Otherwise, or if ``PyMem_Free(p)`` has been called before, undefined
137-
behavior occurs. If *p* is *NULL*, no operation is performed.
152+
previous call to :c:func:`PyMem_RawMalloc`, :c:func:`PyMem_RawRealloc` or
153+
:c:func:`PyMem_RawCalloc`. Otherwise, or if ``PyMem_Free(p)`` has been
154+
called before, undefined behavior occurs.
155+
156+
If *p* is *NULL*, no operation is performed.
138157
139158
140159
.. _memoryinterface:
@@ -158,40 +177,50 @@ The default memory block allocator uses the following functions:
158177
.. c:function:: void* PyMem_Malloc(size_t n)
159178
160179
Allocates *n* bytes and returns a pointer of type :c:type:`void\*` to the
161-
allocated memory, or *NULL* if the request fails. Requesting zero bytes returns
162-
a distinct non-*NULL* pointer if possible, as if ``PyMem_Malloc(1)`` had
163-
been called instead. The memory will not have been initialized in any way.
180+
allocated memory, or *NULL* if the request fails.
181+
182+
Requesting zero bytes returns a distinct non-*NULL* pointer if possible, as
183+
if ``PyMem_Malloc(1)`` had been called instead. The memory will not have
184+
been initialized in any way.
164185
165186
166187
.. c:function:: void* PyMem_Calloc(size_t nelem, size_t elsize)
167188
168189
Allocates *nelem* elements each whose size in bytes is *elsize* and returns
169190
a pointer of type :c:type:`void\*` to the allocated memory, or *NULL* if the
170-
request fails. The memory is initialized to zeros. Requesting zero elements
171-
or elements of size zero bytes returns a distinct non-*NULL* pointer if
172-
possible, as if ``PyMem_Calloc(1, 1)`` had been called instead.
191+
request fails. The memory is initialized to zeros.
192+
193+
Requesting zero elements or elements of size zero bytes returns a distinct
194+
non-*NULL* pointer if possible, as if ``PyMem_Calloc(1, 1)`` had been called
195+
instead.
173196
174197
.. versionadded:: 3.5
175198
176199
177200
.. c:function:: void* PyMem_Realloc(void *p, size_t n)
178201
179202
Resizes the memory block pointed to by *p* to *n* bytes. The contents will be
180-
unchanged to the minimum of the old and the new sizes. If *p* is *NULL*, the
181-
call is equivalent to ``PyMem_Malloc(n)``; else if *n* is equal to zero,
182-
the memory block is resized but is not freed, and the returned pointer is
183-
non-*NULL*. Unless *p* is *NULL*, it must have been returned by a previous call
184-
to :c:func:`PyMem_Malloc` or :c:func:`PyMem_Realloc`. If the request fails,
185-
:c:func:`PyMem_Realloc` returns *NULL* and *p* remains a valid pointer to the
186-
previous memory area.
203+
unchanged to the minimum of the old and the new sizes.
204+
205+
If *p* is *NULL*, the call is equivalent to ``PyMem_Malloc(n)``; else if *n*
206+
is equal to zero, the memory block is resized but is not freed, and the
207+
returned pointer is non-*NULL*.
208+
209+
Unless *p* is *NULL*, it must have been returned by a previous call to
210+
:c:func:`PyMem_Malloc`, :c:func:`PyMem_Realloc` or :c:func:`PyMem_Calloc`.
211+
212+
If the request fails, :c:func:`PyMem_Realloc` returns *NULL* and *p* remains
213+
a valid pointer to the previous memory area.
187214
188215
189216
.. c:function:: void PyMem_Free(void *p)
190217
191218
Frees the memory block pointed to by *p*, which must have been returned by a
192-
previous call to :c:func:`PyMem_Malloc` or :c:func:`PyMem_Realloc`. Otherwise, or
193-
if ``PyMem_Free(p)`` has been called before, undefined behavior occurs. If
194-
*p* is *NULL*, no operation is performed.
219+
previous call to :c:func:`PyMem_Malloc`, :c:func:`PyMem_Realloc` or
220+
:c:func:`PyMem_Calloc`. Otherwise, or if ``PyMem_Free(p)`` has been called
221+
before, undefined behavior occurs.
222+
223+
If *p* is *NULL*, no operation is performed.
195224
196225
The following type-oriented macros are provided for convenience. Note that
197226
*TYPE* refers to any C type.
@@ -209,8 +238,10 @@ The following type-oriented macros are provided for convenience. Note that
209238
Same as :c:func:`PyMem_Realloc`, but the memory block is resized to ``(n *
210239
sizeof(TYPE))`` bytes. Returns a pointer cast to :c:type:`TYPE\*`. On return,
211240
*p* will be a pointer to the new memory area, or *NULL* in the event of
212-
failure. This is a C preprocessor macro; p is always reassigned. Save
213-
the original value of p to avoid losing memory when handling errors.
241+
failure.
242+
243+
This is a C preprocessor macro; *p* is always reassigned. Save the original
244+
value of *p* to avoid losing memory when handling errors.
214245
215246
216247
.. c:function:: void PyMem_Del(void *p)
@@ -222,9 +253,12 @@ allocator directly, without involving the C API functions listed above. However,
222253
note that their use does not preserve binary compatibility across Python
223254
versions and is therefore deprecated in extension modules.
224255
225-
:c:func:`PyMem_MALLOC`, :c:func:`PyMem_REALLOC`, :c:func:`PyMem_FREE`.
226-
227-
:c:func:`PyMem_NEW`, :c:func:`PyMem_RESIZE`, :c:func:`PyMem_DEL`.
256+
* ``PyMem_MALLOC(size)``
257+
* ``PyMem_NEW(type, size)``
258+
* ``PyMem_REALLOC(ptr, size)``
259+
* ``PyMem_RESIZE(ptr, type, size)``
260+
* ``PyMem_FREE(ptr)``
261+
* ``PyMem_DEL(ptr)``
228262
229263
230264
Customize Memory Allocators
@@ -262,11 +296,13 @@ Customize Memory Allocators
262296
Enum used to identify an allocator domain. Domains:
263297
264298
* :c:data:`PYMEM_DOMAIN_RAW`: functions :c:func:`PyMem_RawMalloc`,
265-
:c:func:`PyMem_RawRealloc` and :c:func:`PyMem_RawFree`
299+
:c:func:`PyMem_RawRealloc`, :c:func:`PyMem_RawCalloc` and
300+
:c:func:`PyMem_RawFree`
266301
* :c:data:`PYMEM_DOMAIN_MEM`: functions :c:func:`PyMem_Malloc`,
267-
:c:func:`PyMem_Realloc` and :c:func:`PyMem_Free`
302+
:c:func:`PyMem_Realloc`, :c:func:`PyMem_Calloc` and :c:func:`PyMem_Free`
268303
* :c:data:`PYMEM_DOMAIN_OBJ`: functions :c:func:`PyObject_Malloc`,
269-
:c:func:`PyObject_Realloc` and :c:func:`PyObject_Free`
304+
:c:func:`PyObject_Realloc`, :c:func:`PyObject_Calloc` and
305+
:c:func:`PyObject_Free`
270306
271307
272308
.. c:function:: void PyMem_GetAllocator(PyMemAllocatorDomain domain, PyMemAllocatorEx *allocator)
@@ -296,38 +332,63 @@ Customize Memory Allocators
296332
functions:
297333
298334
- :c:func:`PyMem_RawMalloc`, :c:func:`PyMem_RawRealloc`,
299-
:c:func:`PyMem_RawFree`
300-
- :c:func:`PyMem_Malloc`, :c:func:`PyMem_Realloc`, :c:func:`PyMem_Free`
335+
:c:func:`PyMem_RawCalloc`, :c:func:`PyMem_RawFree`
336+
- :c:func:`PyMem_Malloc`, :c:func:`PyMem_Realloc`, :c:func:`PyMem_Calloc`,
337+
:c:func:`PyMem_Free`
301338
- :c:func:`PyObject_Malloc`, :c:func:`PyObject_Realloc`,
302-
:c:func:`PyObject_Free`
339+
:c:func:`PyObject_Calloc`, :c:func:`PyObject_Free`
303340
304341
Newly allocated memory is filled with the byte ``0xCB``, freed memory is
305342
filled with the byte ``0xDB``. Additional checks:
306343
307-
- detect API violations, ex: :c:func:`PyObject_Free` called on a buffer
344+
- Detect API violations, ex: :c:func:`PyObject_Free` called on a buffer
308345
allocated by :c:func:`PyMem_Malloc`
309-
- detect write before the start of the buffer (buffer underflow)
310-
- detect write after the end of the buffer (buffer overflow)
346+
- Detect write before the start of the buffer (buffer underflow)
347+
- Detect write after the end of the buffer (buffer overflow)
348+
- Check that the :term:`GIL <global interpreter lock>` is held when
349+
allocator functions of :c:data:`PYMEM_DOMAIN_OBJ` (ex:
350+
:c:func:`PyObject_Malloc`) and :c:data:`PYMEM_DOMAIN_MEM` (ex:
351+
:c:func:`PyMem_Malloc`) domains are called
352+
353+
On error, the debug hooks use the :mod:`tracemalloc` module to get the
354+
traceback where a memory block was allocated. The traceback is only
355+
displayed if :mod:`tracemalloc` is tracing Python memory allocations and the
356+
memory block was traced.
357+
358+
These hooks are installed by default if Python is compiled in debug
359+
mode. The :envvar:`PYTHONMALLOC` environment variable can be used to install
360+
debug hooks on a Python compiled in release mode.
311361
312-
The function does nothing if Python is not compiled is debug mode.
362+
.. versionchanged:: 3.6
363+
This function now also works on Python compiled in release mode.
364+
On error, the debug hooks now use :mod:`tracemalloc` to get the traceback
365+
where a memory block was allocated. The debug hooks now also check
366+
if the GIL is held when functions of :c:data:`PYMEM_DOMAIN_OBJ` and
367+
:c:data:`PYMEM_DOMAIN_MEM` domains are called.
313368
314369
315-
Customize PyObject Arena Allocator
316-
==================================
370+
.. _pymalloc:
317371
318-
Python has a *pymalloc* allocator for allocations smaller than 512 bytes. This
319-
allocator is optimized for small objects with a short lifetime. It uses memory
320-
mappings called "arenas" with a fixed size of 256 KB. It falls back to
321-
:c:func:`PyMem_RawMalloc` and :c:func:`PyMem_RawRealloc` for allocations larger
322-
than 512 bytes. *pymalloc* is the default allocator used by
323-
:c:func:`PyObject_Malloc`.
372+
The pymalloc allocator
373+
======================
324374
325-
The default arena allocator uses the following functions:
375+
Python has a *pymalloc* allocator optimized for small objects (smaller or equal
376+
to 512 bytes) with a short lifetime. It uses memory mappings called "arenas"
377+
with a fixed size of 256 KB. It falls back to :c:func:`PyMem_RawMalloc` and
378+
:c:func:`PyMem_RawRealloc` for allocations larger than 512 bytes.
379+
380+
*pymalloc* is the default allocator of the :c:data:`PYMEM_DOMAIN_OBJ` domain
381+
(ex: :c:func:`PyObject_Malloc`).
382+
383+
The arena allocator uses the following functions:
326384
327385
* :c:func:`VirtualAlloc` and :c:func:`VirtualFree` on Windows,
328386
* :c:func:`mmap` and :c:func:`munmap` if available,
329387
* :c:func:`malloc` and :c:func:`free` otherwise.
330388
389+
Customize pymalloc Arena Allocator
390+
----------------------------------
391+
331392
.. versionadded:: 3.4
332393
333394
.. c:type:: PyObjectArenaAllocator

Doc/c-api/stable.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,5 @@ will work on all subsequent Python releases, but fail to load (because of
3434
missing symbols) on the older releases.
3535

3636
As of Python 3.2, the set of functions available to the limited API is
37-
documented in PEP 384. In the C API documentation, API elements that are not
37+
documented in :pep:`384`. In the C API documentation, API elements that are not
3838
part of the limited API are marked as "Not part of the limited API."

Doc/distributing/index.rst

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Key terms
3535
repository of open source licensed packages made available for use by
3636
other Python users
3737
* the `Python Packaging Authority
38-
<https://packaging.python.org/en/latest/future.html>`__ are the group of
38+
<https://www.pypa.io/>`__ are the group of
3939
developers and documentation authors responsible for the maintenance and
4040
evolution of the standard packaging tools and the associated metadata and
4141
file format standards. They maintain a variety of tools, documentation
@@ -62,7 +62,7 @@ Key terms
6262
locally.
6363

6464
.. _setuptools: https://setuptools.pypa.io/en/latest/setuptools.html
65-
.. _wheel: http://wheel.readthedocs.org
65+
.. _wheel: https://wheel.readthedocs.org
6666

6767
Open source licensing and collaboration
6868
=======================================
@@ -111,7 +111,7 @@ by invoking the ``pip`` module at the command line::
111111
The Python Packaging User Guide includes more details on the `currently
112112
recommended tools`_.
113113

114-
.. _currently recommended tools: https://packaging.python.org/en/latest/current.html#packaging-tool-recommendations
114+
.. _currently recommended tools: https://packaging.python.org/en/latest/current/#packaging-tool-recommendations
115115

116116
Reading the guide
117117
=================
@@ -124,11 +124,11 @@ involved in creating a project:
124124
* `Uploading the project to the Python Packaging Index`_
125125

126126
.. _Project structure: \
127-
https://packaging.python.org/en/latest/distributing.html#creating-your-own-project
127+
https://packaging.python.org/en/latest/distributing/
128128
.. _Building and packaging the project: \
129-
https://packaging.python.org/en/latest/distributing.html#packaging-your-project
129+
https://packaging.python.org/en/latest/distributing/#packaging-your-project
130130
.. _Uploading the project to the Python Packaging Index: \
131-
https://packaging.python.org/en/latest/distributing.html#uploading-your-project-to-pypi
131+
https://packaging.python.org/en/latest/distributing/#uploading-your-project-to-pypi
132132

133133

134134
How do I...?
@@ -160,11 +160,11 @@ Python Packaging User Guide for more information and recommendations.
160160
.. seealso::
161161

162162
`Python Packaging User Guide: Binary Extensions
163-
<https://packaging.python.org/en/latest/extensions.html>`__
163+
<https://packaging.python.org/en/latest/extensions>`__
164164

165165
.. other topics:
166166
167167
Once the Development & Deployment part of PPUG is fleshed out, some of
168168
those sections should be linked from new questions here (most notably,
169169
we should have a question about avoiding depending on PyPI that links to
170-
https://packaging.python.org/en/latest/deployment.html#pypi-mirrors-and-caches)
170+
https://packaging.python.org/en/latest/mirrors/)

0 commit comments

Comments
 (0)