Skip to content

Commit dcbcb02

Browse files
authored
Merge branch 'python:main' into patch-1
2 parents 1e27106 + cf59bf7 commit dcbcb02

Some content is hidden

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

47 files changed

+1375
-397
lines changed

Doc/c-api/import.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,8 +372,10 @@ Importing Modules
372372
373373
Sets the current lazy imports filter. The *filter* should be a callable that
374374
will receive ``(importing_module_name, imported_module_name, [fromlist])``
375-
when an import can potentially be lazy and that must return ``True`` if
376-
the import should be lazy and ``False`` otherwise.
375+
when an import can potentially be lazy. The ``imported_module_name`` value
376+
is the resolved module name, so ``lazy from .spam import eggs`` passes
377+
``package.spam``. The callable must return ``True`` if the import should be
378+
lazy and ``False`` otherwise.
377379
378380
Return ``0`` on success and ``-1`` with an exception set otherwise.
379381

Doc/library/base64.rst

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,11 @@ POST request.
8585

8686
If *padded* is true, the last group of 4 base 64 alphabet characters must
8787
be padded with the '=' character.
88-
If *padded* is false, the '=' character is treated as other non-alphabet
89-
characters (depending on the value of *validate* and *ignorechars*).
88+
If *padded* is false, padding is neither required nor recognized:
89+
the '=' character is not treated as padding but as a non-alphabet
90+
character, which means it is silently discarded when *validate* is false,
91+
or causes an :exc:`~binascii.Error` when *validate* is true unless
92+
b'=' is included in *ignorechars*.
9093

9194
A :exc:`binascii.Error` exception is raised
9295
if *s* is incorrectly padded.
@@ -194,8 +197,10 @@ POST request.
194197

195198
If *padded* is true, the last group of 8 base 32 alphabet characters must
196199
be padded with the '=' character.
197-
If *padded* is false, the '=' character is treated as other non-alphabet
198-
characters (depending on the value of *ignorechars*).
200+
If *padded* is false, padding is neither required nor recognized:
201+
the '=' character is not treated as padding but as a non-alphabet
202+
character, which means it raises an :exc:`~binascii.Error` unless
203+
b'=' is included in *ignorechars*.
199204

200205
*ignorechars* should be a :term:`bytes-like object` containing characters
201206
to ignore from the input.

Doc/library/binascii.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,11 @@ The :mod:`!binascii` module defines the following functions:
5959

6060
If *padded* is true, the last group of 4 base 64 alphabet characters must
6161
be padded with the '=' character.
62-
If *padded* is false, the '=' character is treated as other non-alphabet
63-
characters (depending on the value of *strict_mode* and *ignorechars*).
62+
If *padded* is false, padding is neither required nor recognized:
63+
the '=' character is not treated as padding but as a non-alphabet
64+
character, which means it is silently discarded when *strict_mode* is false,
65+
or causes an :exc:`~binascii.Error` when *strict_mode* is true unless
66+
b'=' is included in *ignorechars*.
6467

6568
If *ignorechars* is specified, it should be a :term:`bytes-like object`
6669
containing characters to ignore from the input when *strict_mode* is true.

Doc/library/pkgutil.rst

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,24 +151,48 @@ support.
151151
:meth:`get_data <importlib.abc.ResourceLoader.get_data>` API. The
152152
*package* argument should be the name of a package, in standard module format
153153
(``foo.bar``). The *resource* argument should be in the form of a relative
154-
filename, using ``/`` as the path separator. The parent directory name
155-
``..`` is not allowed, and nor is a rooted name (starting with a ``/``).
154+
filename, using ``/`` as the path separator.
156155

157156
The function returns a binary string that is the contents of the specified
158157
resource.
159158

159+
This function uses the :term:`loader` method
160+
:func:`~importlib.abc.FileLoader.get_data`
161+
to support modules installed in the filesystem, but also in zip files,
162+
databases, or elsewhere.
163+
160164
For packages located in the filesystem, which have already been imported,
161165
this is the rough equivalent of::
162166

163167
d = os.path.dirname(sys.modules[package].__file__)
164168
data = open(os.path.join(d, resource), 'rb').read()
165169

170+
Like the :func:`open` function, :func:`!get_data` can follow parent
171+
directories (``../``) and absolute paths (starting with ``/`` or ``C:/``,
172+
for example).
173+
It can open compilation/installation artifacts like ``.py`` and ``.pyc``
174+
files or files with :func:`reserved filenames <os.path.isreserved>`.
175+
To be compatible with non-filesystem loaders, avoid using these features.
176+
177+
.. warning::
178+
179+
This function is intended for trusted input.
180+
It does not verify that *resource* "belongs" to *package*.
181+
182+
If you use a user-provided *resource* path, consider verifying it.
183+
For example, require an alphanumeric filename with a known extension, or
184+
install and check a list of known resources.
185+
166186
If the package cannot be located or loaded, or it uses a :term:`loader`
167187
which does not support :meth:`get_data <importlib.abc.ResourceLoader.get_data>`,
168188
then ``None`` is returned. In particular, the :term:`loader` for
169189
:term:`namespace packages <namespace package>` does not support
170190
:meth:`get_data <importlib.abc.ResourceLoader.get_data>`.
171191

192+
.. seealso::
193+
194+
The :mod:`importlib.resources` module provides structured access to
195+
module resources.
172196

173197
.. function:: resolve_name(name)
174198

Doc/library/pprint.rst

Lines changed: 104 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ Functions
3131
---------
3232

3333
.. function:: pp(object, stream=None, indent=1, width=80, depth=None, *, \
34-
compact=False, sort_dicts=False, underscore_numbers=False)
34+
compact=False, expand=False, sort_dicts=False, \
35+
underscore_numbers=False)
3536

3637
Prints the formatted representation of *object*, followed by a newline.
3738
This function may be used in the interactive interpreter
@@ -69,6 +70,13 @@ Functions
6970
each item of a sequence will be formatted on a separate line,
7071
otherwise as many items as will fit within the *width*
7172
will be formatted on each output line.
73+
Incompatible with *expand*.
74+
75+
:param bool expand:
76+
If ``True``,
77+
opening parentheses and brackets will be followed by a newline and the
78+
following content will be indented by one level, similar to
79+
pretty-printed JSON. Incompatible with *compact*.
7280

7381
:param bool sort_dicts:
7482
If ``True``, dictionaries will be formatted with
@@ -95,18 +103,20 @@ Functions
95103

96104

97105
.. function:: pprint(object, stream=None, indent=1, width=80, depth=None, *, \
98-
compact=False, sort_dicts=True, underscore_numbers=False)
106+
compact=False, expand=False, sort_dicts=True, \
107+
underscore_numbers=False)
99108

100109
Alias for :func:`~pprint.pp` with *sort_dicts* set to ``True`` by default,
101110
which would automatically sort the dictionaries' keys,
102111
you might want to use :func:`~pprint.pp` instead where it is ``False`` by default.
103112

104113

105114
.. function:: pformat(object, indent=1, width=80, depth=None, *, \
106-
compact=False, sort_dicts=True, underscore_numbers=False)
115+
compact=False, expand=False, sort_dicts=True, \
116+
underscore_numbers=False)
107117

108118
Return the formatted representation of *object* as a string. *indent*,
109-
*width*, *depth*, *compact*, *sort_dicts* and *underscore_numbers* are
119+
*width*, *depth*, *compact*, *expand*, *sort_dicts* and *underscore_numbers* are
110120
passed to the :class:`PrettyPrinter` constructor as formatting parameters
111121
and their meanings are as described in the documentation above.
112122

@@ -150,7 +160,8 @@ PrettyPrinter Objects
150160
.. index:: single: ...; placeholder
151161

152162
.. class:: PrettyPrinter(indent=1, width=80, depth=None, stream=None, *, \
153-
compact=False, sort_dicts=True, underscore_numbers=False)
163+
compact=False, expand=False, sort_dicts=True, \
164+
underscore_numbers=False)
154165

155166
Construct a :class:`PrettyPrinter` instance.
156167

@@ -174,6 +185,22 @@ PrettyPrinter Objects
174185
'knights', 'ni'],
175186
'spam', 'eggs', 'lumberjack', 'knights',
176187
'ni']
188+
>>> pp = pprint.PrettyPrinter(width=41, expand=True, indent=3)
189+
>>> pp.pprint(stuff)
190+
[
191+
[
192+
'spam',
193+
'eggs',
194+
'lumberjack',
195+
'knights',
196+
'ni',
197+
],
198+
'spam',
199+
'eggs',
200+
'lumberjack',
201+
'knights',
202+
'ni',
203+
]
177204
>>> tup = ('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead',
178205
... ('parrot', ('fresh fruit',))))))))
179206
>>> pp = pprint.PrettyPrinter(depth=6)
@@ -193,6 +220,9 @@ PrettyPrinter Objects
193220
.. versionchanged:: 3.11
194221
No longer attempts to write to :data:`!sys.stdout` if it is ``None``.
195222

223+
.. versionchanged:: next
224+
Added the *expand* parameter.
225+
196226

197227
:class:`PrettyPrinter` instances have the following methods:
198228

@@ -415,3 +445,72 @@ cannot be split, the specified width will be exceeded::
415445
'requires_python': None,
416446
'summary': 'A sample Python project',
417447
'version': '1.2.0'}
448+
449+
Lastly, we can format like pretty-printed JSON with the *expand* parameter.
450+
Best results are achieved with a higher *indent* value::
451+
452+
>>> pprint.pp(project_info, indent=4, expand=True)
453+
{
454+
'author': 'The Python Packaging Authority',
455+
'author_email': 'pypa-dev@googlegroups.com',
456+
'bugtrack_url': None,
457+
'classifiers': [
458+
'Development Status :: 3 - Alpha',
459+
'Intended Audience :: Developers',
460+
'License :: OSI Approved :: MIT License',
461+
'Programming Language :: Python :: 2',
462+
'Programming Language :: Python :: 2.6',
463+
'Programming Language :: Python :: 2.7',
464+
'Programming Language :: Python :: 3',
465+
'Programming Language :: Python :: 3.2',
466+
'Programming Language :: Python :: 3.3',
467+
'Programming Language :: Python :: 3.4',
468+
'Topic :: Software Development :: Build Tools',
469+
],
470+
'description': 'A sample Python project\n'
471+
'=======================\n'
472+
'\n'
473+
'This is the description file for the project.\n'
474+
'\n'
475+
'The file should use UTF-8 encoding and be written using ReStructured '
476+
'Text. It\n'
477+
'will be used to generate the project webpage on PyPI, and should be '
478+
'written for\n'
479+
'that purpose.\n'
480+
'\n'
481+
'Typical contents for this file would include an overview of the project, '
482+
'basic\n'
483+
'usage examples, etc. Generally, including the project changelog in here '
484+
'is not\n'
485+
'a good idea, although a simple "What\'s New" section for the most recent '
486+
'version\n'
487+
'may be appropriate.',
488+
'description_content_type': None,
489+
'docs_url': None,
490+
'download_url': 'UNKNOWN',
491+
'downloads': {'last_day': -1, 'last_month': -1, 'last_week': -1},
492+
'dynamic': None,
493+
'home_page': 'https://github.com/pypa/sampleproject',
494+
'keywords': 'sample setuptools development',
495+
'license': 'MIT',
496+
'license_expression': None,
497+
'license_files': None,
498+
'maintainer': None,
499+
'maintainer_email': None,
500+
'name': 'sampleproject',
501+
'package_url': 'https://pypi.org/project/sampleproject/',
502+
'platform': 'UNKNOWN',
503+
'project_url': 'https://pypi.org/project/sampleproject/',
504+
'project_urls': {
505+
'Download': 'UNKNOWN',
506+
'Homepage': 'https://github.com/pypa/sampleproject',
507+
},
508+
'provides_extra': None,
509+
'release_url': 'https://pypi.org/project/sampleproject/1.2.0/',
510+
'requires_dist': None,
511+
'requires_python': None,
512+
'summary': 'A sample Python project',
513+
'version': '1.2.0',
514+
'yanked': False,
515+
'yanked_reason': None,
516+
}

Doc/library/sys.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1788,7 +1788,9 @@ always available. Unless explicitly noted otherwise, all variables are read-only
17881788
Where:
17891789

17901790
* *importing_module* is the name of the module doing the import
1791-
* *imported_module* is the name of the module being imported
1791+
* *imported_module* is the resolved name of the module being imported
1792+
(for example, ``lazy from .spam import eggs`` passes
1793+
``package.spam``)
17921794
* *fromlist* is the tuple of names being imported (for ``from ... import``
17931795
statements), or ``None`` for regular imports
17941796

Doc/whatsnew/3.15.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1507,6 +1507,16 @@ platform
15071507
(Contributed by Alexey Makridenko in :gh:`133604`.)
15081508

15091509

1510+
pprint
1511+
------
1512+
1513+
* Add an *expand* keyword argument for :func:`pprint.pprint`,
1514+
:func:`pprint.pformat`, :func:`pprint.pp`. If true, the output will be
1515+
formatted similar to pretty-printed :func:`json.dumps` when
1516+
*indent* is supplied.
1517+
(Contributed by Stefan Todoran and Semyon Moroz in :gh:`112632`.)
1518+
1519+
15101520
sre_*
15111521
-----
15121522

Include/cpython/object.h

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,3 +495,82 @@ PyAPI_FUNC(void) PyUnstable_EnableTryIncRef(PyObject *);
495495
PyAPI_FUNC(int) PyUnstable_Object_IsUniquelyReferenced(PyObject *);
496496

497497
PyAPI_FUNC(int) PyUnstable_SetImmortal(PyObject *op);
498+
499+
#if defined(Py_GIL_DISABLED)
500+
PyAPI_FUNC(uintptr_t) _Py_GetThreadLocal_Addr(void);
501+
502+
static inline uintptr_t
503+
_Py_ThreadId(void)
504+
{
505+
uintptr_t tid;
506+
#if defined(_MSC_VER) && defined(_M_X64)
507+
tid = __readgsqword(48);
508+
#elif defined(_MSC_VER) && defined(_M_IX86)
509+
tid = __readfsdword(24);
510+
#elif defined(_MSC_VER) && defined(_M_ARM64)
511+
tid = __getReg(18);
512+
#elif defined(__MINGW32__) && defined(_M_X64)
513+
tid = __readgsqword(48);
514+
#elif defined(__MINGW32__) && defined(_M_IX86)
515+
tid = __readfsdword(24);
516+
#elif defined(__MINGW32__) && defined(_M_ARM64)
517+
tid = __getReg(18);
518+
#elif defined(__i386__)
519+
__asm__("{movl %%gs:0, %0|mov %0, dword ptr gs:[0]}" : "=r" (tid)); // 32-bit always uses GS
520+
#elif defined(__MACH__) && defined(__x86_64__)
521+
__asm__("{movq %%gs:0, %0|mov %0, qword ptr gs:[0]}" : "=r" (tid)); // x86_64 macOSX uses GS
522+
#elif defined(__x86_64__)
523+
__asm__("{movq %%fs:0, %0|mov %0, qword ptr fs:[0]}" : "=r" (tid)); // x86_64 Linux, BSD uses FS
524+
#elif defined(__arm__) && __ARM_ARCH >= 7
525+
__asm__ ("mrc p15, 0, %0, c13, c0, 3\nbic %0, %0, #3" : "=r" (tid));
526+
#elif defined(__aarch64__) && defined(__APPLE__)
527+
__asm__ ("mrs %0, tpidrro_el0" : "=r" (tid));
528+
#elif defined(__aarch64__)
529+
__asm__ ("mrs %0, tpidr_el0" : "=r" (tid));
530+
#elif defined(__powerpc64__)
531+
#if defined(__clang__) && _Py__has_builtin(__builtin_thread_pointer)
532+
tid = (uintptr_t)__builtin_thread_pointer();
533+
#else
534+
// r13 is reserved for use as system thread ID by the Power 64-bit ABI.
535+
register uintptr_t tp __asm__ ("r13");
536+
__asm__("" : "=r" (tp));
537+
tid = tp;
538+
#endif
539+
#elif defined(__powerpc__)
540+
#if defined(__clang__) && _Py__has_builtin(__builtin_thread_pointer)
541+
tid = (uintptr_t)__builtin_thread_pointer();
542+
#else
543+
// r2 is reserved for use as system thread ID by the Power 32-bit ABI.
544+
register uintptr_t tp __asm__ ("r2");
545+
__asm__ ("" : "=r" (tp));
546+
tid = tp;
547+
#endif
548+
#elif defined(__s390__) && defined(__GNUC__)
549+
// Both GCC and Clang have supported __builtin_thread_pointer
550+
// for s390 from long time ago.
551+
tid = (uintptr_t)__builtin_thread_pointer();
552+
#elif defined(__riscv)
553+
#if defined(__clang__) && _Py__has_builtin(__builtin_thread_pointer)
554+
tid = (uintptr_t)__builtin_thread_pointer();
555+
#else
556+
// tp is Thread Pointer provided by the RISC-V ABI.
557+
__asm__ ("mv %0, tp" : "=r" (tid));
558+
#endif
559+
#else
560+
// Fallback to a portable implementation if we do not have a faster
561+
// platform-specific implementation.
562+
tid = _Py_GetThreadLocal_Addr();
563+
#endif
564+
return tid;
565+
}
566+
567+
static inline Py_ALWAYS_INLINE int
568+
_Py_IsOwnedByCurrentThread(PyObject *ob)
569+
{
570+
#ifdef _Py_THREAD_SANITIZER
571+
return _Py_atomic_load_uintptr_relaxed(&ob->ob_tid) == _Py_ThreadId();
572+
#else
573+
return ob->ob_tid == _Py_ThreadId();
574+
#endif
575+
}
576+
#endif

Include/internal/pycore_crossinterp.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,12 @@ typedef struct {
265265
// heap types
266266
PyObject *PyExc_NotShareableError;
267267
} exceptions;
268+
269+
// Cached references to pickle.dumps/loads (per-interpreter).
270+
struct {
271+
PyObject *dumps;
272+
PyObject *loads;
273+
} pickle;
268274
} _PyXI_state_t;
269275

270276
#define _PyXI_GET_GLOBAL_STATE(interp) (&(interp)->runtime->xi)

0 commit comments

Comments
 (0)