Skip to content

Commit

Permalink
Merge branch 'main' into split-classes-again
Browse files Browse the repository at this point in the history
  • Loading branch information
barneygale authored Jan 8, 2025
2 parents 620818c + 004f9fd commit 484c48a
Show file tree
Hide file tree
Showing 106 changed files with 2,486 additions and 1,077 deletions.
34 changes: 23 additions & 11 deletions Doc/c-api/arg.rst
Original file line number Diff line number Diff line change
Expand Up @@ -229,12 +229,24 @@ There are three ways strings and buffers can be converted to C:
Numbers
-------

These formats allow representing Python numbers or single characters as C numbers.
Formats that require :class:`int`, :class:`float` or :class:`complex` can
also use the corresponding special methods :meth:`~object.__index__`,
:meth:`~object.__float__` or :meth:`~object.__complex__` to convert
the Python object to the required type.

For signed integer formats, :exc:`OverflowError` is raised if the value
is out of range for the C type.
For unsigned integer formats, no range checking is done --- the
most significant bits are silently truncated when the receiving field is too
small to receive the value.

``b`` (:class:`int`) [unsigned char]
Convert a nonnegative Python integer to an unsigned tiny int, stored in a C
Convert a nonnegative Python integer to an unsigned tiny integer, stored in a C
:c:expr:`unsigned char`.

``B`` (:class:`int`) [unsigned char]
Convert a Python integer to a tiny int without overflow checking, stored in a C
Convert a Python integer to a tiny integer without overflow checking, stored in a C
:c:expr:`unsigned char`.

``h`` (:class:`int`) [short int]
Expand Down Expand Up @@ -307,7 +319,7 @@ Other objects

.. _o_ampersand:

``O&`` (object) [*converter*, *anything*]
``O&`` (object) [*converter*, *address*]
Convert a Python object to a C variable through a *converter* function. This
takes two arguments: the first is a function, the second is the address of a C
variable (of arbitrary type), converted to :c:expr:`void *`. The *converter*
Expand All @@ -321,14 +333,20 @@ Other objects
the conversion has failed. When the conversion fails, the *converter* function
should raise an exception and leave the content of *address* unmodified.

If the *converter* returns ``Py_CLEANUP_SUPPORTED``, it may get called a
.. c:macro:: Py_CLEANUP_SUPPORTED
:no-typesetting:

If the *converter* returns :c:macro:`!Py_CLEANUP_SUPPORTED`, it may get called a
second time if the argument parsing eventually fails, giving the converter a
chance to release any memory that it had already allocated. In this second
call, the *object* parameter will be ``NULL``; *address* will have the same value
as in the original call.

Examples of converters: :c:func:`PyUnicode_FSConverter` and
:c:func:`PyUnicode_FSDecoder`.

.. versionchanged:: 3.1
``Py_CLEANUP_SUPPORTED`` was added.
:c:macro:`!Py_CLEANUP_SUPPORTED` was added.

``p`` (:class:`bool`) [int]
Tests the value passed in for truth (a boolean **p**\ redicate) and converts
Expand All @@ -344,12 +362,6 @@ Other objects
in *items*. The C arguments must correspond to the individual format units in
*items*. Format units for sequences may be nested.

It is possible to pass "long" integers (integers whose value exceeds the
platform's :c:macro:`LONG_MAX`) however no proper range checking is done --- the
most significant bits are silently truncated when the receiving field is too
small to receive the value (actually, the semantics are inherited from downcasts
in C --- your mileage may vary).

A few other characters have a meaning in a format string. These may not occur
inside nested parentheses. They are:

Expand Down
32 changes: 32 additions & 0 deletions Doc/c-api/sys.rst
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,38 @@ Operating System Utilities
The function now uses the UTF-8 encoding on Windows if
:c:member:`PyPreConfig.legacy_windows_fs_encoding` is zero.
.. c:function:: FILE* Py_fopen(PyObject *path, const char *mode)
Similar to :c:func:`!fopen`, but *path* is a Python object and
an exception is set on error.
*path* must be a :class:`str` object, a :class:`bytes` object,
or a :term:`path-like object`.
On success, return the new file pointer.
On error, set an exception and return ``NULL``.
The file must be closed by :c:func:`Py_fclose` rather than calling directly
:c:func:`!fclose`.
The file descriptor is created non-inheritable (:pep:`446`).
The caller must hold the GIL.
.. versionadded:: next
.. c:function:: int Py_fclose(FILE *file)
Close a file that was opened by :c:func:`Py_fopen`.
On success, return ``0``.
On error, return ``EOF`` and ``errno`` is set to indicate the error.
In either case, any further access (including another call to
:c:func:`Py_fclose`) to the stream results in undefined behavior.
.. versionadded:: next
.. _systemfunctions:
Expand Down
35 changes: 27 additions & 8 deletions Doc/c-api/unicode.rst
Original file line number Diff line number Diff line change
Expand Up @@ -786,33 +786,52 @@ Functions encoding to and decoding from the :term:`filesystem encoding and
error handler` (:pep:`383` and :pep:`529`).
To encode file names to :class:`bytes` during argument parsing, the ``"O&"``
converter should be used, passing :c:func:`PyUnicode_FSConverter` as the
converter should be used, passing :c:func:`!PyUnicode_FSConverter` as the
conversion function:
.. c:function:: int PyUnicode_FSConverter(PyObject* obj, void* result)
ParseTuple converter: encode :class:`str` objects -- obtained directly or
:ref:`PyArg_Parse\* converter <arg-parsing>`: encode :class:`str` objects -- obtained directly or
through the :class:`os.PathLike` interface -- to :class:`bytes` using
:c:func:`PyUnicode_EncodeFSDefault`; :class:`bytes` objects are output as-is.
*result* must be a :c:expr:`PyBytesObject*` which must be released when it is
no longer used.
*result* must be an address of a C variable of type :c:expr:`PyObject*`
(or :c:expr:`PyBytesObject*`).
On success, set the variable to a new :term:`strong reference` to
a :ref:`bytes object <bytesobjects>` which must be released
when it is no longer used and return a non-zero value
(:c:macro:`Py_CLEANUP_SUPPORTED`).
Embedded null bytes are not allowed in the result.
On failure, return ``0`` with an exception set.
If *obj* is ``NULL``, the function releases a strong reference
stored in the variable referred by *result* and returns ``1``.
.. versionadded:: 3.1
.. versionchanged:: 3.6
Accepts a :term:`path-like object`.
To decode file names to :class:`str` during argument parsing, the ``"O&"``
converter should be used, passing :c:func:`PyUnicode_FSDecoder` as the
converter should be used, passing :c:func:`!PyUnicode_FSDecoder` as the
conversion function:
.. c:function:: int PyUnicode_FSDecoder(PyObject* obj, void* result)
ParseTuple converter: decode :class:`bytes` objects -- obtained either
:ref:`PyArg_Parse\* converter <arg-parsing>`: decode :class:`bytes` objects -- obtained either
directly or indirectly through the :class:`os.PathLike` interface -- to
:class:`str` using :c:func:`PyUnicode_DecodeFSDefaultAndSize`; :class:`str`
objects are output as-is. *result* must be a :c:expr:`PyUnicodeObject*` which
must be released when it is no longer used.
objects are output as-is.
*result* must be an address of a C variable of type :c:expr:`PyObject*`
(or :c:expr:`PyUnicodeObject*`).
On success, set the variable to a new :term:`strong reference` to
a :ref:`Unicode object <unicodeobjects>` which must be released
when it is no longer used and return a non-zero value
(:c:macro:`Py_CLEANUP_SUPPORTED`).
Embedded null characters are not allowed in the result.
On failure, return ``0`` with an exception set.
If *obj* is ``NULL``, release the strong reference
to the object referred to by *result* and return ``1``.
.. versionadded:: 3.2
Expand Down
24 changes: 22 additions & 2 deletions Doc/library/calendar.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,33 @@ interpreted as prescribed by the ISO 8601 standard. Year 0 is 1 BC, year -1 is
itself. This is the job of subclasses.


:class:`Calendar` instances have the following methods:
:class:`Calendar` instances have the following methods and attributes:

.. attribute:: firstweekday

The first weekday as an integer (0--6).

This property can also be set and read using
:meth:`~Calendar.setfirstweekday` and
:meth:`~Calendar.getfirstweekday` respectively.

.. method:: getfirstweekday()

Return an :class:`int` for the current first weekday (0--6).

Identical to reading the :attr:`~Calendar.firstweekday` property.

.. method:: setfirstweekday(firstweekday)

Set the first weekday to *firstweekday*, passed as an :class:`int` (0--6)

Identical to setting the :attr:`~Calendar.firstweekday` property.

.. method:: iterweekdays()

Return an iterator for the week day numbers that will be used for one
week. The first value from the iterator will be the same as the value of
the :attr:`firstweekday` property.
the :attr:`~Calendar.firstweekday` property.


.. method:: itermonthdates(year, month)
Expand Down
2 changes: 2 additions & 0 deletions Doc/library/ctypes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1812,6 +1812,8 @@ different ways, depending on the type and number of the parameters in the call:
the COM interface as first argument, in addition to those parameters that
are specified in the :attr:`!argtypes` tuple.

.. availability:: Windows


The optional *paramflags* parameter creates foreign function wrappers with much
more functionality than the features described above.
Expand Down
4 changes: 2 additions & 2 deletions Doc/library/email.policy.rst
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ added matters. To illustrate::

Handle a *defect* found on *obj*. When the email package calls this
method, *defect* will always be a subclass of
:class:`~email.errors.Defect`.
:class:`~email.errors.MessageDefect`.

The default implementation checks the :attr:`raise_on_defect` flag. If
it is ``True``, *defect* is raised as an exception. If it is ``False``
Expand All @@ -277,7 +277,7 @@ added matters. To illustrate::
.. method:: register_defect(obj, defect)

Register a *defect* on *obj*. In the email package, *defect* will always
be a subclass of :class:`~email.errors.Defect`.
be a subclass of :class:`~email.errors.MessageDefect`.

The default implementation calls the ``append`` method of the ``defects``
attribute of *obj*. When the email package calls :attr:`handle_defect`,
Expand Down
125 changes: 75 additions & 50 deletions Doc/library/json.rst
Original file line number Diff line number Diff line change
Expand Up @@ -258,75 +258,100 @@ Basic Usage
the original one. That is, ``loads(dumps(x)) != x`` if x has non-string
keys.

.. function:: load(fp, *, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)
.. function:: load(fp, *, cls=None, object_hook=None, parse_float=None, \
parse_int=None, parse_constant=None, \
object_pairs_hook=None, **kw)
Deserialize *fp* (a ``.read()``-supporting :term:`text file` or
:term:`binary file` containing a JSON document) to a Python object using
this :ref:`conversion table <json-to-py-table>`.
Deserialize *fp* to a Python object
using the :ref:`JSON-to-Python conversion table <json-to-py-table>`.

*object_hook* is an optional function that will be called with the result of
any object literal decoded (a :class:`dict`). The return value of
*object_hook* will be used instead of the :class:`dict`. This feature can
be used to implement custom decoders (e.g. `JSON-RPC
<https://www.jsonrpc.org>`_ class hinting).
:param fp:
A ``.read()``-supporting :term:`text file` or :term:`binary file`
containing the JSON document to be deserialized.
:type fp: :term:`file-like object`

*object_pairs_hook* is an optional function that will be called with the
result of any object literal decoded with an ordered list of pairs. The
return value of *object_pairs_hook* will be used instead of the
:class:`dict`. This feature can be used to implement custom decoders. If
*object_hook* is also defined, the *object_pairs_hook* takes priority.
:param cls:
If set, a custom JSON decoder.
Additional keyword arguments to :func:`!load`
will be passed to the constructor of *cls*.
If ``None`` (the default), :class:`!JSONDecoder` is used.
:type cls: a :class:`JSONDecoder` subclass

:param object_hook:
If set, a function that is called with the result of
any object literal decoded (a :class:`dict`).
The return value of this function will be used
instead of the :class:`dict`.
This feature can be used to implement custom decoders,
for example `JSON-RPC <https://www.jsonrpc.org>`_ class hinting.
Default ``None``.
:type object_hook: :term:`callable` | None

:param object_pairs_hook:
If set, a function that is called with the result of
any object literal decoded with an ordered list of pairs.
The return value of this function will be used
instead of the :class:`dict`.
This feature can be used to implement custom decoders.
If *object_hook* is also set, *object_pairs_hook* takes priority.
Default ``None``.
:type object_pairs_hook: :term:`callable` | None

:param parse_float:
If set, a function that is called with
the string of every JSON float to be decoded.
If ``None`` (the default), it is equivalent to ``float(num_str)``.
This can be used to parse JSON floats into custom datatypes,
for example :class:`decimal.Decimal`.
:type parse_float: :term:`callable` | None

:param parse_int:
If set, a function that is called with
the string of every JSON int to be decoded.
If ``None`` (the default), it is equivalent to ``int(num_str)``.
This can be used to parse JSON integers into custom datatypes,
for example :class:`float`.
:type parse_int: :term:`callable` | None

:param parse_constant:
If set, a function that is called with one of the following strings:
``'-Infinity'``, ``'Infinity'``, or ``'NaN'``.
This can be used to raise an exception
if invalid JSON numbers are encountered.
Default ``None``.
:type parse_constant: :term:`callable` | None

:raises JSONDecodeError:
When the data being deserialized is not a valid JSON document.

:raises UnicodeDecodeError:
When the data being deserialized does not contain
UTF-8, UTF-16 or UTF-32 encoded data.

.. versionchanged:: 3.1
Added support for *object_pairs_hook*.

*parse_float* is an optional function that will be called with the string of
every JSON float to be decoded. By default, this is equivalent to
``float(num_str)``. This can be used to use another datatype or parser for
JSON floats (e.g. :class:`decimal.Decimal`).
* Added the optional *object_pairs_hook* parameter.
* *parse_constant* doesn't get called on 'null', 'true', 'false' anymore.

*parse_int* is an optional function that will be called with the string of
every JSON int to be decoded. By default, this is equivalent to
``int(num_str)``. This can be used to use another datatype or parser for
JSON integers (e.g. :class:`float`).
.. versionchanged:: 3.6

* All optional parameters are now :ref:`keyword-only <keyword-only_parameter>`.
* *fp* can now be a :term:`binary file`.
The input encoding should be UTF-8, UTF-16 or UTF-32.

.. versionchanged:: 3.11
The default *parse_int* of :func:`int` now limits the maximum length of
the integer string via the interpreter's :ref:`integer string
conversion length limitation <int_max_str_digits>` to help avoid denial
of service attacks.

*parse_constant* is an optional function that will be called with one of the
following strings: ``'-Infinity'``, ``'Infinity'``, ``'NaN'``. This can be
used to raise an exception if invalid JSON numbers are encountered.

.. versionchanged:: 3.1
*parse_constant* doesn't get called on 'null', 'true', 'false' anymore.

To use a custom :class:`JSONDecoder` subclass, specify it with the ``cls``
kwarg; otherwise :class:`JSONDecoder` is used. Additional keyword arguments
will be passed to the constructor of the class.

If the data being deserialized is not a valid JSON document, a
:exc:`JSONDecodeError` will be raised.

.. versionchanged:: 3.6
All optional parameters are now :ref:`keyword-only <keyword-only_parameter>`.

.. versionchanged:: 3.6
*fp* can now be a :term:`binary file`. The input encoding should be
UTF-8, UTF-16 or UTF-32.

.. function:: loads(s, *, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)

Deserialize *s* (a :class:`str`, :class:`bytes` or :class:`bytearray`
Identical to :func:`load`, but instead of a file-like object,
deserialize *s* (a :class:`str`, :class:`bytes` or :class:`bytearray`
instance containing a JSON document) to a Python object using this
:ref:`conversion table <json-to-py-table>`.

The other arguments have the same meaning as in :func:`load`.

If the data being deserialized is not a valid JSON document, a
:exc:`JSONDecodeError` will be raised.

.. versionchanged:: 3.6
*s* can now be of type :class:`bytes` or :class:`bytearray`. The
input encoding should be UTF-8, UTF-16 or UTF-32.
Expand Down
1 change: 0 additions & 1 deletion Doc/tools/.nitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ Doc/library/email.charset.rst
Doc/library/email.compat32-message.rst
Doc/library/email.errors.rst
Doc/library/email.parser.rst
Doc/library/email.policy.rst
Doc/library/exceptions.rst
Doc/library/functools.rst
Doc/library/http.cookiejar.rst
Expand Down
Loading

0 comments on commit 484c48a

Please sign in to comment.