Skip to content

Commit c10a4fb

Browse files
authored
Merge branch 'main' into issue-127089
2 parents 25bf193 + 3c770e3 commit c10a4fb

Some content is hidden

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

57 files changed

+1177
-440
lines changed

Doc/library/argparse.rst

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -801,7 +801,8 @@ Only actions that consume command-line arguments (e.g. ``'store'``,
801801

802802
The recommended way to create a custom action is to extend :class:`Action`,
803803
overriding the :meth:`!__call__` method and optionally the :meth:`!__init__` and
804-
:meth:`!format_usage` methods.
804+
:meth:`!format_usage` methods. You can also register custom actions using the
805+
:meth:`~ArgumentParser.register` method and reference them by their registered name.
805806

806807
An example of a custom action::
807808

@@ -1020,10 +1021,11 @@ necessary type-checking and type conversions to be performed.
10201021
If the type_ keyword is used with the default_ keyword, the type converter
10211022
is only applied if the default is a string.
10221023

1023-
The argument to ``type`` can be any callable that accepts a single string.
1024+
The argument to ``type`` can be a callable that accepts a single string or
1025+
the name of a registered type (see :meth:`~ArgumentParser.register`)
10241026
If the function raises :exc:`ArgumentTypeError`, :exc:`TypeError`, or
10251027
:exc:`ValueError`, the exception is caught and a nicely formatted error
1026-
message is displayed. No other exception types are handled.
1028+
message is displayed. Other exception types are not handled.
10271029

10281030
Common built-in types and functions can be used as type converters:
10291031

@@ -2163,6 +2165,34 @@ Intermixed parsing
21632165
.. versionadded:: 3.7
21642166

21652167

2168+
Registering custom types or actions
2169+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2170+
2171+
.. method:: ArgumentParser.register(registry_name, value, object)
2172+
2173+
Sometimes it's desirable to use a custom string in error messages to provide
2174+
more user-friendly output. In these cases, :meth:`!register` can be used to
2175+
register custom actions or types with a parser and allow you to reference the
2176+
type by their registered name instead of their callable name.
2177+
2178+
The :meth:`!register` method accepts three arguments - a *registry_name*,
2179+
specifying the internal registry where the object will be stored (e.g.,
2180+
``action``, ``type``), *value*, which is the key under which the object will
2181+
be registered, and object, the callable to be registered.
2182+
2183+
The following example shows how to register a custom type with a parser::
2184+
2185+
>>> import argparse
2186+
>>> parser = argparse.ArgumentParser()
2187+
>>> parser.register('type', 'hexadecimal integer', lambda s: int(s, 16))
2188+
>>> parser.add_argument('--foo', type='hexadecimal integer')
2189+
_StoreAction(option_strings=['--foo'], dest='foo', nargs=None, const=None, default=None, type='hexadecimal integer', choices=None, required=False, help=None, metavar=None, deprecated=False)
2190+
>>> parser.parse_args(['--foo', '0xFA'])
2191+
Namespace(foo=250)
2192+
>>> parser.parse_args(['--foo', '1.2'])
2193+
usage: PROG [-h] [--foo FOO]
2194+
PROG: error: argument --foo: invalid 'hexadecimal integer' value: '1.2'
2195+
21662196
Exceptions
21672197
----------
21682198

Doc/library/ctypes.rst

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1413,13 +1413,15 @@ way is to instantiate one of the following classes:
14131413

14141414
.. class:: OleDLL(name, mode=DEFAULT_MODE, handle=None, use_errno=False, use_last_error=False, winmode=None)
14151415

1416-
Windows only: Instances of this class represent loaded shared libraries,
1416+
Instances of this class represent loaded shared libraries,
14171417
functions in these libraries use the ``stdcall`` calling convention, and are
14181418
assumed to return the windows specific :class:`HRESULT` code. :class:`HRESULT`
14191419
values contain information specifying whether the function call failed or
14201420
succeeded, together with additional error code. If the return value signals a
14211421
failure, an :class:`OSError` is automatically raised.
14221422

1423+
.. availability:: Windows
1424+
14231425
.. versionchanged:: 3.3
14241426
:exc:`WindowsError` used to be raised,
14251427
which is now an alias of :exc:`OSError`.
@@ -1431,14 +1433,17 @@ way is to instantiate one of the following classes:
14311433

14321434
.. class:: WinDLL(name, mode=DEFAULT_MODE, handle=None, use_errno=False, use_last_error=False, winmode=None)
14331435

1434-
Windows only: Instances of this class represent loaded shared libraries,
1436+
Instances of this class represent loaded shared libraries,
14351437
functions in these libraries use the ``stdcall`` calling convention, and are
14361438
assumed to return :c:expr:`int` by default.
14371439

1440+
.. availability:: Windows
1441+
14381442
.. versionchanged:: 3.12
14391443

14401444
The *name* parameter can now be a :term:`path-like object`.
14411445

1446+
14421447
The Python :term:`global interpreter lock` is released before calling any
14431448
function exported by these libraries, and reacquired afterwards.
14441449

@@ -1574,13 +1579,17 @@ These prefabricated library loaders are available:
15741579
.. data:: windll
15751580
:noindex:
15761581

1577-
Windows only: Creates :class:`WinDLL` instances.
1582+
Creates :class:`WinDLL` instances.
1583+
1584+
.. availability:: Windows
15781585

15791586

15801587
.. data:: oledll
15811588
:noindex:
15821589

1583-
Windows only: Creates :class:`OleDLL` instances.
1590+
Creates :class:`OleDLL` instances.
1591+
1592+
.. availability:: Windows
15841593

15851594

15861595
.. data:: pydll
@@ -1746,11 +1755,13 @@ See :ref:`ctypes-callback-functions` for examples.
17461755

17471756
.. function:: WINFUNCTYPE(restype, *argtypes, use_errno=False, use_last_error=False)
17481757

1749-
Windows only: The returned function prototype creates functions that use the
1758+
The returned function prototype creates functions that use the
17501759
``stdcall`` calling convention. The function will
17511760
release the GIL during the call. *use_errno* and *use_last_error* have the
17521761
same meaning as above.
17531762

1763+
.. availability:: Windows
1764+
17541765

17551766
.. function:: PYFUNCTYPE(restype, *argtypes)
17561767

@@ -1981,17 +1992,21 @@ Utility functions
19811992

19821993
.. function:: DllCanUnloadNow()
19831994

1984-
Windows only: This function is a hook which allows implementing in-process
1995+
This function is a hook which allows implementing in-process
19851996
COM servers with ctypes. It is called from the DllCanUnloadNow function that
19861997
the _ctypes extension dll exports.
19871998

1999+
.. availability:: Windows
2000+
19882001

19892002
.. function:: DllGetClassObject()
19902003

1991-
Windows only: This function is a hook which allows implementing in-process
2004+
This function is a hook which allows implementing in-process
19922005
COM servers with ctypes. It is called from the DllGetClassObject function
19932006
that the ``_ctypes`` extension dll exports.
19942007

2008+
.. availability:: Windows
2009+
19952010

19962011
.. function:: find_library(name)
19972012
:module: ctypes.util
@@ -2007,28 +2022,35 @@ Utility functions
20072022
.. function:: find_msvcrt()
20082023
:module: ctypes.util
20092024

2010-
Windows only: return the filename of the VC runtime library used by Python,
2025+
Returns the filename of the VC runtime library used by Python,
20112026
and by the extension modules. If the name of the library cannot be
20122027
determined, ``None`` is returned.
20132028

20142029
If you need to free memory, for example, allocated by an extension module
20152030
with a call to the ``free(void *)``, it is important that you use the
20162031
function in the same library that allocated the memory.
20172032

2033+
.. availability:: Windows
2034+
20182035

20192036
.. function:: FormatError([code])
20202037

2021-
Windows only: Returns a textual description of the error code *code*. If no
2038+
Returns a textual description of the error code *code*. If no
20222039
error code is specified, the last error code is used by calling the Windows
20232040
api function GetLastError.
20242041

2042+
.. availability:: Windows
2043+
20252044

20262045
.. function:: GetLastError()
20272046

2028-
Windows only: Returns the last error code set by Windows in the calling thread.
2047+
Returns the last error code set by Windows in the calling thread.
20292048
This function calls the Windows ``GetLastError()`` function directly,
20302049
it does not return the ctypes-private copy of the error code.
20312050

2051+
.. availability:: Windows
2052+
2053+
20322054
.. function:: get_errno()
20332055

20342056
Returns the current value of the ctypes-private copy of the system
@@ -2038,11 +2060,14 @@ Utility functions
20382060

20392061
.. function:: get_last_error()
20402062

2041-
Windows only: returns the current value of the ctypes-private copy of the system
2063+
Returns the current value of the ctypes-private copy of the system
20422064
:data:`!LastError` variable in the calling thread.
20432065

2066+
.. availability:: Windows
2067+
20442068
.. audit-event:: ctypes.get_last_error "" ctypes.get_last_error
20452069

2070+
20462071
.. function:: memmove(dst, src, count)
20472072

20482073
Same as the standard C memmove library function: copies *count* bytes from
@@ -2091,10 +2116,12 @@ Utility functions
20912116

20922117
.. function:: set_last_error(value)
20932118

2094-
Windows only: set the current value of the ctypes-private copy of the system
2119+
Sets the current value of the ctypes-private copy of the system
20952120
:data:`!LastError` variable in the calling thread to *value* and return the
20962121
previous value.
20972122

2123+
.. availability:: Windows
2124+
20982125
.. audit-event:: ctypes.set_last_error error ctypes.set_last_error
20992126

21002127

@@ -2115,12 +2142,14 @@ Utility functions
21152142

21162143
.. function:: WinError(code=None, descr=None)
21172144

2118-
Windows only: this function is probably the worst-named thing in ctypes. It
2145+
This function is probably the worst-named thing in ctypes. It
21192146
creates an instance of :exc:`OSError`. If *code* is not specified,
21202147
``GetLastError`` is called to determine the error code. If *descr* is not
21212148
specified, :func:`FormatError` is called to get a textual description of the
21222149
error.
21232150

2151+
.. availability:: Windows
2152+
21242153
.. versionchanged:: 3.3
21252154
An instance of :exc:`WindowsError` used to be created, which is now an
21262155
alias of :exc:`OSError`.
@@ -2484,9 +2513,11 @@ These are the fundamental ctypes data types:
24842513

24852514
.. class:: HRESULT
24862515

2487-
Windows only: Represents a :c:type:`!HRESULT` value, which contains success or
2516+
Represents a :c:type:`!HRESULT` value, which contains success or
24882517
error information for a function or method call.
24892518

2519+
.. availability:: Windows
2520+
24902521

24912522
.. class:: py_object
24922523

@@ -2755,7 +2786,7 @@ Exceptions
27552786

27562787
.. exception:: COMError(hresult, text, details)
27572788

2758-
Windows only: This exception is raised when a COM method call failed.
2789+
This exception is raised when a COM method call failed.
27592790

27602791
.. attribute:: hresult
27612792

@@ -2775,4 +2806,6 @@ Exceptions
27752806
identifier. *progid* is the ``ProgID`` of the interface that defined the
27762807
error.
27772808

2809+
.. availability:: Windows
2810+
27782811
.. versionadded:: next

Doc/library/urllib.request.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,11 @@ The :mod:`urllib.request` module defines the following functions:
152152
the path component of a URL. This does not produce a complete URL. The return
153153
value will already be quoted using the :func:`~urllib.parse.quote` function.
154154

155+
.. versionchanged:: 3.14
156+
On Windows, ``:`` characters not following a drive letter are quoted. In
157+
previous versions, :exc:`OSError` was raised if a colon character was
158+
found in any position other than the second character.
159+
155160

156161
.. function:: url2pathname(path)
157162

Include/cpython/code.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ typedef struct {
131131
\
132132
/* redundant values (derived from co_localsplusnames and \
133133
co_localspluskinds) */ \
134-
int co_nlocalsplus; /* number of local + cell + free variables */ \
134+
int co_nlocalsplus; /* number of spaces for holding local, cell, \
135+
and free variables */ \
135136
int co_framesize; /* Size of frame in words */ \
136137
int co_nlocals; /* number of local variables */ \
137138
int co_ncellvars; /* total number of cell variables */ \

Include/internal/pycore_dict.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,17 @@ extern PyObject *_PyDict_FromKeys(PyObject *, PyObject *, PyObject *);
9090
extern uint32_t _PyDictKeys_GetVersionForCurrentState(
9191
PyInterpreterState *interp, PyDictKeysObject *dictkeys);
9292

93+
/* Gets a version number unique to the current state of the keys of dict, if possible.
94+
*
95+
* In free-threaded builds ensures that the dict can be used for lock-free
96+
* reads if a version was assigned.
97+
*
98+
* The caller must hold the per-object lock on dict.
99+
*
100+
* Returns the version number, or zero if it was not possible to get a version number. */
101+
extern uint32_t _PyDict_GetKeysVersionForCurrentState(
102+
PyInterpreterState *interp, PyDictObject *dict);
103+
93104
extern size_t _PyDict_KeysSize(PyDictKeysObject *keys);
94105

95106
extern void _PyDictKeys_DecRef(PyDictKeysObject *keys);

Include/internal/pycore_gc.h

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ static inline PyObject* _Py_FROM_GC(PyGC_Head *gc) {
5050
# define _PyGC_BITS_UNREACHABLE (4)
5151
# define _PyGC_BITS_FROZEN (8)
5252
# define _PyGC_BITS_SHARED (16)
53-
# define _PyGC_BITS_SHARED_INLINE (32)
5453
# define _PyGC_BITS_DEFERRED (64) // Use deferred reference counting
5554
#endif
5655

@@ -119,23 +118,6 @@ static inline void _PyObject_GC_SET_SHARED(PyObject *op) {
119118
}
120119
#define _PyObject_GC_SET_SHARED(op) _PyObject_GC_SET_SHARED(_Py_CAST(PyObject*, op))
121120

122-
/* True if the memory of the object is shared between multiple
123-
* threads and needs special purpose when freeing due to
124-
* the possibility of in-flight lock-free reads occurring.
125-
* Objects with this bit that are GC objects will automatically
126-
* delay-freed by PyObject_GC_Del. */
127-
static inline int _PyObject_GC_IS_SHARED_INLINE(PyObject *op) {
128-
return _PyObject_HAS_GC_BITS(op, _PyGC_BITS_SHARED_INLINE);
129-
}
130-
#define _PyObject_GC_IS_SHARED_INLINE(op) \
131-
_PyObject_GC_IS_SHARED_INLINE(_Py_CAST(PyObject*, op))
132-
133-
static inline void _PyObject_GC_SET_SHARED_INLINE(PyObject *op) {
134-
_PyObject_SET_GC_BITS(op, _PyGC_BITS_SHARED_INLINE);
135-
}
136-
#define _PyObject_GC_SET_SHARED_INLINE(op) \
137-
_PyObject_GC_SET_SHARED_INLINE(_Py_CAST(PyObject*, op))
138-
139121
#endif
140122

141123
/* Bit flags for _gc_prev */

Include/internal/pycore_global_objects_fini_generated.h

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_global_strings.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,7 @@ struct _Py_global_strings {
471471
STRUCT_FOR_ID(hi)
472472
STRUCT_FOR_ID(hook)
473473
STRUCT_FOR_ID(hour)
474+
STRUCT_FOR_ID(id)
474475
STRUCT_FOR_ID(ident)
475476
STRUCT_FOR_ID(identity_hint)
476477
STRUCT_FOR_ID(ignore)

Include/internal/pycore_modsupport.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#ifndef Py_INTERNAL_MODSUPPORT_H
22
#define Py_INTERNAL_MODSUPPORT_H
33

4-
#include "pycore_lock.h" // _PyOnceFlag
5-
64
#ifdef __cplusplus
75
extern "C" {
86
#endif

Include/internal/pycore_object.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ extern "C" {
1414
#include "pycore_interp.h" // PyInterpreterState.gc
1515
#include "pycore_pyatomic_ft_wrappers.h" // FT_ATOMIC_STORE_PTR_RELAXED
1616
#include "pycore_pystate.h" // _PyInterpreterState_GET()
17+
#include "pycore_stackref.h"
1718
#include "pycore_uniqueid.h" // _PyObject_ThreadIncrefSlow()
1819

1920
// This value is added to `ob_ref_shared` for objects that use deferred
@@ -595,6 +596,20 @@ _Py_TryIncrefCompare(PyObject **src, PyObject *op)
595596
return 1;
596597
}
597598

599+
static inline int
600+
_Py_TryIncrefCompareStackRef(PyObject **src, PyObject *op, _PyStackRef *out)
601+
{
602+
if (_Py_IsImmortal(op) || _PyObject_HasDeferredRefcount(op)) {
603+
*out = (_PyStackRef){ .bits = (intptr_t)op | Py_TAG_DEFERRED };
604+
return 1;
605+
}
606+
if (_Py_TryIncrefCompare(src, op)) {
607+
*out = PyStackRef_FromPyObjectSteal(op);
608+
return 1;
609+
}
610+
return 0;
611+
}
612+
598613
/* Loads and increfs an object from ptr, which may contain a NULL value.
599614
Safe with concurrent (atomic) updates to ptr.
600615
NOTE: The writer must set maybe-weakref on the stored object! */

0 commit comments

Comments
 (0)