Skip to content

Commit

Permalink
Merge branch 'main' into issue-127089
Browse files Browse the repository at this point in the history
  • Loading branch information
donBarbos authored Nov 22, 2024
2 parents 25bf193 + 3c770e3 commit c10a4fb
Show file tree
Hide file tree
Showing 57 changed files with 1,177 additions and 440 deletions.
36 changes: 33 additions & 3 deletions Doc/library/argparse.rst
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,8 @@ Only actions that consume command-line arguments (e.g. ``'store'``,

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

An example of a custom action::

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

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

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

Expand Down Expand Up @@ -2163,6 +2165,34 @@ Intermixed parsing
.. versionadded:: 3.7


Registering custom types or actions
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. method:: ArgumentParser.register(registry_name, value, object)

Sometimes it's desirable to use a custom string in error messages to provide
more user-friendly output. In these cases, :meth:`!register` can be used to
register custom actions or types with a parser and allow you to reference the
type by their registered name instead of their callable name.

The :meth:`!register` method accepts three arguments - a *registry_name*,
specifying the internal registry where the object will be stored (e.g.,
``action``, ``type``), *value*, which is the key under which the object will
be registered, and object, the callable to be registered.

The following example shows how to register a custom type with a parser::

>>> import argparse
>>> parser = argparse.ArgumentParser()
>>> parser.register('type', 'hexadecimal integer', lambda s: int(s, 16))
>>> parser.add_argument('--foo', type='hexadecimal integer')
_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)
>>> parser.parse_args(['--foo', '0xFA'])
Namespace(foo=250)
>>> parser.parse_args(['--foo', '1.2'])
usage: PROG [-h] [--foo FOO]
PROG: error: argument --foo: invalid 'hexadecimal integer' value: '1.2'

Exceptions
----------

Expand Down
63 changes: 48 additions & 15 deletions Doc/library/ctypes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1413,13 +1413,15 @@ way is to instantiate one of the following classes:

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

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

.. availability:: Windows

.. versionchanged:: 3.3
:exc:`WindowsError` used to be raised,
which is now an alias of :exc:`OSError`.
Expand All @@ -1431,14 +1433,17 @@ way is to instantiate one of the following classes:

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

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

.. availability:: Windows

.. versionchanged:: 3.12

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


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

Expand Down Expand Up @@ -1574,13 +1579,17 @@ These prefabricated library loaders are available:
.. data:: windll
:noindex:

Windows only: Creates :class:`WinDLL` instances.
Creates :class:`WinDLL` instances.

.. availability:: Windows


.. data:: oledll
:noindex:

Windows only: Creates :class:`OleDLL` instances.
Creates :class:`OleDLL` instances.

.. availability:: Windows


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

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

Windows only: The returned function prototype creates functions that use the
The returned function prototype creates functions that use the
``stdcall`` calling convention. The function will
release the GIL during the call. *use_errno* and *use_last_error* have the
same meaning as above.

.. availability:: Windows


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

Expand Down Expand Up @@ -1981,17 +1992,21 @@ Utility functions

.. function:: DllCanUnloadNow()

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

.. availability:: Windows


.. function:: DllGetClassObject()

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

.. availability:: Windows


.. function:: find_library(name)
:module: ctypes.util
Expand All @@ -2007,28 +2022,35 @@ Utility functions
.. function:: find_msvcrt()
:module: ctypes.util

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

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

.. availability:: Windows


.. function:: FormatError([code])

Windows only: Returns a textual description of the error code *code*. If no
Returns a textual description of the error code *code*. If no
error code is specified, the last error code is used by calling the Windows
api function GetLastError.

.. availability:: Windows


.. function:: GetLastError()

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

.. availability:: Windows


.. function:: get_errno()

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

.. function:: get_last_error()

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

.. availability:: Windows

.. audit-event:: ctypes.get_last_error "" ctypes.get_last_error


.. function:: memmove(dst, src, count)

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

.. function:: set_last_error(value)

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

.. availability:: Windows

.. audit-event:: ctypes.set_last_error error ctypes.set_last_error


Expand All @@ -2115,12 +2142,14 @@ Utility functions

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

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

.. availability:: Windows

.. versionchanged:: 3.3
An instance of :exc:`WindowsError` used to be created, which is now an
alias of :exc:`OSError`.
Expand Down Expand Up @@ -2484,9 +2513,11 @@ These are the fundamental ctypes data types:

.. class:: HRESULT

Windows only: Represents a :c:type:`!HRESULT` value, which contains success or
Represents a :c:type:`!HRESULT` value, which contains success or
error information for a function or method call.

.. availability:: Windows


.. class:: py_object

Expand Down Expand Up @@ -2755,7 +2786,7 @@ Exceptions

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

Windows only: This exception is raised when a COM method call failed.
This exception is raised when a COM method call failed.

.. attribute:: hresult

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

.. availability:: Windows

.. versionadded:: next
5 changes: 5 additions & 0 deletions Doc/library/urllib.request.rst
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ The :mod:`urllib.request` module defines the following functions:
the path component of a URL. This does not produce a complete URL. The return
value will already be quoted using the :func:`~urllib.parse.quote` function.

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


.. function:: url2pathname(path)

Expand Down
3 changes: 2 additions & 1 deletion Include/cpython/code.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ typedef struct {
\
/* redundant values (derived from co_localsplusnames and \
co_localspluskinds) */ \
int co_nlocalsplus; /* number of local + cell + free variables */ \
int co_nlocalsplus; /* number of spaces for holding local, cell, \
and free variables */ \
int co_framesize; /* Size of frame in words */ \
int co_nlocals; /* number of local variables */ \
int co_ncellvars; /* total number of cell variables */ \
Expand Down
11 changes: 11 additions & 0 deletions Include/internal/pycore_dict.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,17 @@ extern PyObject *_PyDict_FromKeys(PyObject *, PyObject *, PyObject *);
extern uint32_t _PyDictKeys_GetVersionForCurrentState(
PyInterpreterState *interp, PyDictKeysObject *dictkeys);

/* Gets a version number unique to the current state of the keys of dict, if possible.
*
* In free-threaded builds ensures that the dict can be used for lock-free
* reads if a version was assigned.
*
* The caller must hold the per-object lock on dict.
*
* Returns the version number, or zero if it was not possible to get a version number. */
extern uint32_t _PyDict_GetKeysVersionForCurrentState(
PyInterpreterState *interp, PyDictObject *dict);

extern size_t _PyDict_KeysSize(PyDictKeysObject *keys);

extern void _PyDictKeys_DecRef(PyDictKeysObject *keys);
Expand Down
18 changes: 0 additions & 18 deletions Include/internal/pycore_gc.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ static inline PyObject* _Py_FROM_GC(PyGC_Head *gc) {
# define _PyGC_BITS_UNREACHABLE (4)
# define _PyGC_BITS_FROZEN (8)
# define _PyGC_BITS_SHARED (16)
# define _PyGC_BITS_SHARED_INLINE (32)
# define _PyGC_BITS_DEFERRED (64) // Use deferred reference counting
#endif

Expand Down Expand Up @@ -119,23 +118,6 @@ static inline void _PyObject_GC_SET_SHARED(PyObject *op) {
}
#define _PyObject_GC_SET_SHARED(op) _PyObject_GC_SET_SHARED(_Py_CAST(PyObject*, op))

/* True if the memory of the object is shared between multiple
* threads and needs special purpose when freeing due to
* the possibility of in-flight lock-free reads occurring.
* Objects with this bit that are GC objects will automatically
* delay-freed by PyObject_GC_Del. */
static inline int _PyObject_GC_IS_SHARED_INLINE(PyObject *op) {
return _PyObject_HAS_GC_BITS(op, _PyGC_BITS_SHARED_INLINE);
}
#define _PyObject_GC_IS_SHARED_INLINE(op) \
_PyObject_GC_IS_SHARED_INLINE(_Py_CAST(PyObject*, op))

static inline void _PyObject_GC_SET_SHARED_INLINE(PyObject *op) {
_PyObject_SET_GC_BITS(op, _PyGC_BITS_SHARED_INLINE);
}
#define _PyObject_GC_SET_SHARED_INLINE(op) \
_PyObject_GC_SET_SHARED_INLINE(_Py_CAST(PyObject*, op))

#endif

/* Bit flags for _gc_prev */
Expand Down
1 change: 1 addition & 0 deletions Include/internal/pycore_global_objects_fini_generated.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Include/internal/pycore_global_strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,7 @@ struct _Py_global_strings {
STRUCT_FOR_ID(hi)
STRUCT_FOR_ID(hook)
STRUCT_FOR_ID(hour)
STRUCT_FOR_ID(id)
STRUCT_FOR_ID(ident)
STRUCT_FOR_ID(identity_hint)
STRUCT_FOR_ID(ignore)
Expand Down
2 changes: 0 additions & 2 deletions Include/internal/pycore_modsupport.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#ifndef Py_INTERNAL_MODSUPPORT_H
#define Py_INTERNAL_MODSUPPORT_H

#include "pycore_lock.h" // _PyOnceFlag

#ifdef __cplusplus
extern "C" {
#endif
Expand Down
15 changes: 15 additions & 0 deletions Include/internal/pycore_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ extern "C" {
#include "pycore_interp.h" // PyInterpreterState.gc
#include "pycore_pyatomic_ft_wrappers.h" // FT_ATOMIC_STORE_PTR_RELAXED
#include "pycore_pystate.h" // _PyInterpreterState_GET()
#include "pycore_stackref.h"
#include "pycore_uniqueid.h" // _PyObject_ThreadIncrefSlow()

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

static inline int
_Py_TryIncrefCompareStackRef(PyObject **src, PyObject *op, _PyStackRef *out)
{
if (_Py_IsImmortal(op) || _PyObject_HasDeferredRefcount(op)) {
*out = (_PyStackRef){ .bits = (intptr_t)op | Py_TAG_DEFERRED };
return 1;
}
if (_Py_TryIncrefCompare(src, op)) {
*out = PyStackRef_FromPyObjectSteal(op);
return 1;
}
return 0;
}

/* Loads and increfs an object from ptr, which may contain a NULL value.
Safe with concurrent (atomic) updates to ptr.
NOTE: The writer must set maybe-weakref on the stored object! */
Expand Down
Loading

0 comments on commit c10a4fb

Please sign in to comment.