Skip to content

Commit

Permalink
Merge branch 'main' into fastmock
Browse files Browse the repository at this point in the history
* main:
  pythongh-89727: Fix os.walk RecursionError on deep trees (python#99803)
  Docs: Don't upload CI artifacts (python#100330)
  pythongh-94912: Added marker for non-standard coroutine function detection (python#99247)
  Correct CVE-2020-10735 documentation (python#100306)
  pythongh-100272: Fix JSON serialization of OrderedDict (pythonGH-100273)
  pythongh-93649: Split tracemalloc tests from _testcapimodule.c (python#99551)
  Docs: Use `PY_VERSION_HEX` for version comparison (python#100179)
  pythongh-97909: Fix markup for `PyMethodDef` members (python#100089)
  pythongh-99240: Reset pointer to NULL when the pointed memory is freed in argument parsing (python#99890)
  pythongh-99240: Reset pointer to NULL when the pointed memory is freed in argument parsing (python#99890)
  pythonGH-98831: Add DECREF_INPUTS(), expanding to DECREF() each stack input (python#100205)
  pythongh-78707: deprecate passing >1 argument to `PurePath.[is_]relative_to()` (pythonGH-94469)
  • Loading branch information
carljm committed Dec 19, 2022
2 parents 6595272 + 797edb2 commit a9af20a
Show file tree
Hide file tree
Showing 33 changed files with 615 additions and 393 deletions.
10 changes: 0 additions & 10 deletions .github/workflows/doc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,8 @@ jobs:
run: make -C Doc/ venv
- name: 'Check documentation'
run: make -C Doc/ check
- name: 'Upload NEWS'
uses: actions/upload-artifact@v3
with:
name: NEWS
path: Doc/build/NEWS
- name: 'Build HTML documentation'
run: make -C Doc/ SPHINXOPTS="-q" SPHINXERRORHANDLING="-W --keep-going" html
- name: 'Upload docs'
uses: actions/upload-artifact@v3
with:
name: doc-html
path: Doc/build/html

# Run "doctest" on HEAD as new syntax doesn't exist in the latest stable release
doctest:
Expand Down
2 changes: 2 additions & 0 deletions Doc/c-api/apiabiversion.rst
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ See :ref:`stable` for a discussion of API and ABI stability across versions.
Thus ``3.4.1a2`` is hexversion ``0x030401a2`` and ``3.10.0`` is
hexversion ``0x030a00f0``.

Use this for numeric comparisons, e.g. ``#if PY_VERSION_HEX >= ...``.

This version is also available via the symbol :data:`Py_Version`.

.. c:var:: const unsigned long Py_Version
Expand Down
35 changes: 18 additions & 17 deletions Doc/c-api/structures.rst
Original file line number Diff line number Diff line change
Expand Up @@ -228,29 +228,30 @@ Implementing functions and methods
Structure used to describe a method of an extension type. This structure has
four fields:
+------------------+---------------+-------------------------------+
| Field | C Type | Meaning |
+==================+===============+===============================+
| :attr:`ml_name` | const char \* | name of the method |
+------------------+---------------+-------------------------------+
| :attr:`ml_meth` | PyCFunction | pointer to the C |
| | | implementation |
+------------------+---------------+-------------------------------+
| :attr:`ml_flags` | int | flag bits indicating how the |
| | | call should be constructed |
+------------------+---------------+-------------------------------+
| :attr:`ml_doc` | const char \* | points to the contents of the |
| | | docstring |
+------------------+---------------+-------------------------------+
The :attr:`ml_meth` is a C function pointer. The functions may be of different
.. c:member:: const char* ml_name
name of the method
.. c:member:: PyCFunction ml_meth
pointer to the C implementation
.. c:member:: int ml_flags
flags bits indicating how the call should be constructed
.. c:member:: const char* ml_doc
points to the contents of the docstring
The :c:member:`ml_meth` is a C function pointer. The functions may be of different
types, but they always return :c:expr:`PyObject*`. If the function is not of
the :c:type:`PyCFunction`, the compiler will require a cast in the method table.
Even though :c:type:`PyCFunction` defines the first parameter as
:c:expr:`PyObject*`, it is common that the method implementation uses the
specific C type of the *self* object.
The :attr:`ml_flags` field is a bitfield which can include the following flags.
The :c:member:`ml_flags` field is a bitfield which can include the following flags.
The individual flags indicate either a calling convention or a binding
convention.
Expand Down
25 changes: 23 additions & 2 deletions Doc/library/inspect.rst
Original file line number Diff line number Diff line change
Expand Up @@ -343,15 +343,36 @@ attributes (see :ref:`import-mod-attrs` for module attributes):

.. function:: iscoroutinefunction(object)

Return ``True`` if the object is a :term:`coroutine function`
(a function defined with an :keyword:`async def` syntax).
Return ``True`` if the object is a :term:`coroutine function` (a function
defined with an :keyword:`async def` syntax), a :func:`functools.partial`
wrapping a :term:`coroutine function`, or a sync function marked with
:func:`markcoroutinefunction`.

.. versionadded:: 3.5

.. versionchanged:: 3.8
Functions wrapped in :func:`functools.partial` now return ``True`` if the
wrapped function is a :term:`coroutine function`.

.. versionchanged:: 3.12
Sync functions marked with :func:`markcoroutinefunction` now return
``True``.


.. function:: markcoroutinefunction(func)

Decorator to mark a callable as a :term:`coroutine function` if it would not
otherwise be detected by :func:`iscoroutinefunction`.

This may be of use for sync functions that return a :term:`coroutine`, if
the function is passed to an API that requires :func:`iscoroutinefunction`.

When possible, using an :keyword:`async def` function is preferred. Also
acceptable is calling the function and testing the return with
:func:`iscoroutine`.

.. versionadded:: 3.12


.. function:: iscoroutine(object)

Expand Down
14 changes: 11 additions & 3 deletions Doc/library/pathlib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ Pure paths provide the following methods and properties:
True


.. method:: PurePath.is_relative_to(*other)
.. method:: PurePath.is_relative_to(other)

Return whether or not this path is relative to the *other* path.

Expand All @@ -502,6 +502,10 @@ Pure paths provide the following methods and properties:

.. versionadded:: 3.9

.. deprecated-removed:: 3.12 3.14

Passing additional arguments is deprecated; if supplied, they are joined
with *other*.

.. method:: PurePath.is_reserved()

Expand Down Expand Up @@ -564,7 +568,7 @@ Pure paths provide the following methods and properties:
True


.. method:: PurePath.relative_to(*other, walk_up=False)
.. method:: PurePath.relative_to(other, walk_up=False)

Compute a version of this path relative to the path represented by
*other*. If it's impossible, :exc:`ValueError` is raised::
Expand All @@ -581,7 +585,7 @@ Pure paths provide the following methods and properties:
raise ValueError(error_message.format(str(self), str(formatted)))
ValueError: '/etc/passwd' is not in the subpath of '/usr' OR one path is relative and the other is absolute.

When *walk_up* is False (the default), the path must start with *other*.
When *walk_up* is False (the default), the path must start with *other*.
When the argument is True, ``..`` entries may be added to form the
relative path. In all other cases, such as the paths referencing
different drives, :exc:`ValueError` is raised.::
Expand All @@ -605,6 +609,10 @@ When *walk_up* is False (the default), the path must start with *other*.
.. versionadded:: 3.12
The *walk_up* argument (old behavior is the same as ``walk_up=False``).

.. deprecated-removed:: 3.12 3.14

Passing additional positional arguments is deprecated; if supplied,
they are joined with *other*.

.. method:: PurePath.with_name(name)

Expand Down
6 changes: 3 additions & 3 deletions Doc/library/stdtypes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5480,7 +5480,7 @@ to mitigate denial of service attacks. This limit *only* applies to decimal or
other non-power-of-two number bases. Hexadecimal, octal, and binary conversions
are unlimited. The limit can be configured.

The :class:`int` type in CPython is an abitrary length number stored in binary
The :class:`int` type in CPython is an arbitrary length number stored in binary
form (commonly known as a "bignum"). There exists no algorithm that can convert
a string to a binary integer or a binary integer to a string in linear time,
*unless* the base is a power of 2. Even the best known algorithms for base 10
Expand Down Expand Up @@ -5544,7 +5544,7 @@ and :class:`str` or :class:`bytes`:
* ``int(string)`` with default base 10.
* ``int(string, base)`` for all bases that are not a power of 2.
* ``str(integer)``.
* ``repr(integer)``
* ``repr(integer)``.
* any other string conversion to base 10, for example ``f"{integer}"``,
``"{}".format(integer)``, or ``b"%d" % integer``.

Expand Down Expand Up @@ -5572,7 +5572,7 @@ command line flag to configure the limit:
:envvar:`PYTHONINTMAXSTRDIGITS` or :option:`-X int_max_str_digits <-X>`.
If both the env var and the ``-X`` option are set, the ``-X`` option takes
precedence. A value of *-1* indicates that both were unset, thus a value of
:data:`sys.int_info.default_max_str_digits` was used during initilization.
:data:`sys.int_info.default_max_str_digits` was used during initialization.

From code, you can inspect the current limit and set a new one using these
:mod:`sys` APIs:
Expand Down
2 changes: 1 addition & 1 deletion Doc/whatsnew/3.11.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2319,7 +2319,7 @@ Porting to Python 3.11
can define the following macros and use them throughout
the code (credit: these were copied from the ``mypy`` codebase)::

#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 8
#if PY_VERSION_HEX >= 0x03080000
# define CPy_TRASHCAN_BEGIN(op, dealloc) Py_TRASHCAN_BEGIN(op, dealloc)
# define CPy_TRASHCAN_END(op) Py_TRASHCAN_END
#else
Expand Down
6 changes: 6 additions & 0 deletions Doc/whatsnew/3.12.rst
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,12 @@ asyncio
a custom event loop factory.
(Contributed by Kumar Aditya in :gh:`99388`.)

inspect
-------

* Add :func:`inspect.markcoroutinefunction` to mark sync functions that return
a :term:`coroutine` for use with :func:`iscoroutinefunction`.
(Contributed Carlton Gibson in :gh:`99247`.)

pathlib
-------
Expand Down
26 changes: 24 additions & 2 deletions Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@
"ismodule",
"isroutine",
"istraceback",
"markcoroutinefunction",
"signature",
"stack",
"trace",
Expand Down Expand Up @@ -391,12 +392,33 @@ def isgeneratorfunction(obj):
See help(isfunction) for a list of attributes."""
return _has_code_flag(obj, CO_GENERATOR)

# A marker for markcoroutinefunction and iscoroutinefunction.
_is_coroutine_marker = object()

def _has_coroutine_mark(f):
while ismethod(f):
f = f.__func__
f = functools._unwrap_partial(f)
if not (isfunction(f) or _signature_is_functionlike(f)):
return False
return getattr(f, "_is_coroutine_marker", None) is _is_coroutine_marker

def markcoroutinefunction(func):
"""
Decorator to ensure callable is recognised as a coroutine function.
"""
if hasattr(func, '__func__'):
func = func.__func__
func._is_coroutine_marker = _is_coroutine_marker
return func

def iscoroutinefunction(obj):
"""Return true if the object is a coroutine function.
Coroutine functions are defined with "async def" syntax.
Coroutine functions are normally defined with "async def" syntax, but may
be marked via markcoroutinefunction.
"""
return _has_code_flag(obj, CO_COROUTINE)
return _has_code_flag(obj, CO_COROUTINE) or _has_coroutine_mark(obj)

def isasyncgenfunction(obj):
"""Return true if the object is an asynchronous generator function.
Expand Down
Loading

0 comments on commit a9af20a

Please sign in to comment.