Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Glyphack authored Sep 19, 2023
2 parents 0f59c8e + 754519a commit 17b4b48
Show file tree
Hide file tree
Showing 63 changed files with 2,007 additions and 219 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/mypy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ on:
- main
pull_request:
paths:
- "Tools/clinic/**"
- ".github/workflows/mypy.yml"
- "Tools/cases_generator/**"
- "Tools/clinic/**"
- "Tools/peg_generator/**"
- "Tools/requirements-dev.txt"
- ".github/workflows/mypy.yml"
- "Tools/wasm/**"
workflow_dispatch:

permissions:
Expand All @@ -34,6 +35,7 @@ jobs:
"Tools/cases_generator",
"Tools/clinic",
"Tools/peg_generator",
"Tools/wasm",
]
name: Run mypy on ${{ matrix.target }}
runs-on: ubuntu-latest
Expand Down
2 changes: 2 additions & 0 deletions Doc/library/asyncio.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ Additionally, there are **low-level** APIs for
* :ref:`bridge <asyncio-futures>` callback-based libraries and code
with async/await syntax.

.. _asyncio-cli:

You can experiment with an ``asyncio`` concurrent context in the REPL:

.. code-block:: pycon
Expand Down
57 changes: 57 additions & 0 deletions Doc/library/cmdline.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
++++++++++++++++++++++++++++++++++++
Modules command-line interface (CLI)
++++++++++++++++++++++++++++++++++++

The following modules have a command-line interface.

* :ref:`ast <ast-cli>`
* :ref:`asyncio <asyncio-cli>`
* :mod:`base64`
* :ref:`calendar <calendar-cli>`
* :mod:`code`
* :ref:`compileall <compileall-cli>`
* :mod:`cProfile`: see :ref:`profile <profile-cli>`
* :ref:`difflib <difflib-interface>`
* :mod:`dis`
* :mod:`doctest`
* :mod:`!encodings.rot_13`
* :mod:`ensurepip`
* :mod:`filecmp`
* :mod:`fileinput`
* :mod:`ftplib`
* :ref:`gzip <gzip-cli>`
* :ref:`http.server <http-server-cli>`
* :mod:`!idlelib`
* :ref:`inspect <inspect-module-cli>`
* :ref:`json.tool <json-commandline>`
* :mod:`mimetypes`
* :mod:`pdb`
* :mod:`pickle`
* :ref:`pickletools <pickletools-cli>`
* :mod:`platform`
* :mod:`poplib`
* :ref:`profile <profile-cli>`
* :mod:`pstats`
* :ref:`py_compile <py_compile-cli>`
* :mod:`pyclbr`
* :mod:`pydoc`
* :mod:`quopri`
* :mod:`runpy`
* :ref:`site <site-commandline>`
* :ref:`sqlite3 <sqlite3-cli>`
* :ref:`sysconfig <sysconfig-cli>`
* :mod:`tabnanny`
* :ref:`tarfile <tarfile-commandline>`
* :mod:`!this`
* :ref:`timeit <timeit-command-line-interface>`
* :ref:`tokenize <tokenize-cli>`
* :ref:`trace <trace-cli>`
* :mod:`turtledemo`
* :ref:`unittest <unittest-command-line-interface>`
* :ref:`uuid <uuid-cli>`
* :mod:`venv`
* :mod:`webbrowser`
* :ref:`zipapp <zipapp-command-line-interface>`
* :ref:`zipfile <zipfile-commandline>`

See also the :ref:`Python command-line interface <using-on-general>`.
2 changes: 2 additions & 0 deletions Doc/library/compileall.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ have write permission to the library directories.

.. include:: ../includes/wasm-notavail.rst

.. _compileall-cli:

Command-line use
----------------

Expand Down
2 changes: 2 additions & 0 deletions Doc/library/gzip.rst
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,8 @@ Example of how to GZIP compress a binary string::

.. program:: gzip

.. _gzip-cli:

Command Line Interface
----------------------

Expand Down
1 change: 1 addition & 0 deletions Doc/library/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,6 @@ the `Python Package Index <https://pypi.org>`_.
language.rst
windows.rst
unix.rst
cmdline.rst
superseded.rst
security_warnings.rst
76 changes: 42 additions & 34 deletions Doc/library/itertools.rst
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,7 @@ which incur interpreter overhead.
return next(islice(iterable, n, None), default)

def quantify(iterable, pred=bool):
"Given a predicate that returns True or False, count the True results."
"Count how many times the predicate is True"
return sum(map(pred, iterable))

Expand Down Expand Up @@ -1028,34 +1029,6 @@ The following recipes have a more mathematical flavor:
s = list(iterable)
return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))

def sieve(n):
"Primes less than n."
# sieve(30) --> 2 3 5 7 11 13 17 19 23 29
if n > 2:
yield 2
start = 3
data = bytearray((0, 1)) * (n // 2)
limit = math.isqrt(n) + 1
for p in iter_index(data, 1, start, limit):
yield from iter_index(data, 1, start, p*p)
data[p*p : n : p+p] = bytes(len(range(p*p, n, p+p)))
start = p*p
yield from iter_index(data, 1, start)

def factor(n):
"Prime factors of n."
# factor(99) --> 3 3 11
# factor(1_000_000_000_000_007) --> 47 59 360620266859
# factor(1_000_000_000_000_403) --> 1000000000000403
for prime in sieve(math.isqrt(n) + 1):
while not n % prime:
yield prime
n //= prime
if n == 1:
return
if n > 1:
yield n

def sum_of_squares(it):
"Add up the squares of the input values."
# sum_of_squares([10, 20, 30]) -> 1400
Expand All @@ -1073,14 +1046,21 @@ The following recipes have a more mathematical flavor:
return batched(starmap(math.sumprod, product(m1, transpose(m2))), n)

def convolve(signal, kernel):
"""Linear convolution of two iterables.
"""Discrete linear convolution of two iterables.

The kernel is fully consumed before the calculations begin.
The signal is consumed lazily and can be infinite.

Convolutions are mathematically commutative.
If the signal and kernel are swapped,
the output will be the same.

Article: https://betterexplained.com/articles/intuitive-convolution/
Video: https://www.youtube.com/watch?v=KuXjwB4LzSA
"""
# convolve(data, [0.25, 0.25, 0.25, 0.25]) --> Moving average (blur)
# convolve(data, [1, -1]) --> 1st finite difference (1st derivative)
# convolve(data, [1, -2, 1]) --> 2nd finite difference (2nd derivative)
# convolve(data, [1/2, 0, -1/2]) --> 1st derivative estimate
# convolve(data, [1, -2, 1]) --> 2nd derivative estimate
kernel = tuple(kernel)[::-1]
n = len(kernel)
padded_signal = chain(repeat(0, n-1), signal, repeat(0, n-1))
Expand All @@ -1104,8 +1084,8 @@ The following recipes have a more mathematical flavor:
# Evaluate x³ -4x² -17x + 60 at x = 2.5
# polynomial_eval([1, -4, -17, 60], x=2.5) --> 8.125
n = len(coefficients)
if n == 0:
return x * 0 # coerce zero to the type of x
if not n:
return type(x)(0)
powers = map(pow, repeat(x), reversed(range(n)))
return math.sumprod(coefficients, powers)

Expand All @@ -1120,6 +1100,34 @@ The following recipes have a more mathematical flavor:
powers = reversed(range(1, n))
return list(map(operator.mul, coefficients, powers))

def sieve(n):
"Primes less than n."
# sieve(30) --> 2 3 5 7 11 13 17 19 23 29
if n > 2:
yield 2
start = 3
data = bytearray((0, 1)) * (n // 2)
limit = math.isqrt(n) + 1
for p in iter_index(data, 1, start, limit):
yield from iter_index(data, 1, start, p*p)
data[p*p : n : p+p] = bytes(len(range(p*p, n, p+p)))
start = p*p
yield from iter_index(data, 1, start)

def factor(n):
"Prime factors of n."
# factor(99) --> 3 3 11
# factor(1_000_000_000_000_007) --> 47 59 360620266859
# factor(1_000_000_000_000_403) --> 1000000000000403
for prime in sieve(math.isqrt(n) + 1):
while not n % prime:
yield prime
n //= prime
if n == 1:
return
if n > 1:
yield n

def nth_combination(iterable, r, index):
"Equivalent to list(combinations(iterable, r))[index]"
pool = tuple(iterable)
Expand Down Expand Up @@ -1295,7 +1303,7 @@ The following recipes have a more mathematical flavor:
>>> polynomial_eval([], Fraction(2, 3))
Fraction(0, 1)
>>> polynomial_eval([], Decimal('1.75'))
Decimal('0.00')
Decimal('0')
>>> polynomial_eval([11], 7) == 11
True
>>> polynomial_eval([11, 2], 7) == 11 * 7 + 2
Expand Down
2 changes: 2 additions & 0 deletions Doc/library/pickletools.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ are useful for Python core developers who are working on the :mod:`pickle`;
ordinary users of the :mod:`pickle` module probably won't find the
:mod:`pickletools` module relevant.

.. _pickletools-cli:

Command line usage
------------------

Expand Down
2 changes: 2 additions & 0 deletions Doc/library/profile.rst
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ results to a file by specifying a filename to the :func:`run` function::
The :class:`pstats.Stats` class reads profile results from a file and formats
them in various ways.

.. _profile-cli:

The files :mod:`cProfile` and :mod:`profile` can also be invoked as a script to
profile another script. For example::

Expand Down
1 change: 1 addition & 0 deletions Doc/library/py_compile.rst
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ byte-code cache files in the directory containing the source code.
This option is useful when the ``.pycs`` are kept up to date by some
system external to Python like a build system.

.. _py_compile-cli:

Command-Line Interface
----------------------
Expand Down
1 change: 1 addition & 0 deletions Doc/library/sysconfig.rst
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ Other functions

Return the path of :file:`Makefile`.

.. _sysconfig-cli:

Using :mod:`sysconfig` as a script
----------------------------------
Expand Down
1 change: 1 addition & 0 deletions Include/Python.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#include "pytypedefs.h"
#include "pybuffer.h"
#include "pystats.h"
#include "pyatomic.h"
#include "object.h"
#include "objimpl.h"
#include "typeslots.h"
Expand Down
9 changes: 3 additions & 6 deletions Include/cpython/pyatomic.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@
// # release
// ...

#ifndef Py_ATOMIC_H
#define Py_ATOMIC_H

#ifndef Py_CPYTHON_ATOMIC_H
# error "this header file must not be included directly"
#endif

// --- _Py_atomic_add --------------------------------------------------------
// Atomically adds `value` to `obj` and returns the previous value
Expand Down Expand Up @@ -501,6 +501,3 @@ static inline void _Py_atomic_fence_release(void);
#else
# error "no available pyatomic implementation for this platform/compiler"
#endif

#endif /* Py_ATOMIC_H */

2 changes: 1 addition & 1 deletion Include/cpython/pyatomic_msc.h
Original file line number Diff line number Diff line change
Expand Up @@ -906,7 +906,7 @@ _Py_atomic_store_ptr_release(void *obj, void *value)
#if defined(_M_X64) || defined(_M_IX86)
*(void * volatile *)obj = value;
#elif defined(_M_ARM64)
__stlr64(obj, (uintptr_t)value);
__stlr64((unsigned __int64 volatile *)obj, (uintptr_t)value);
#else
# error "no implementation of _Py_atomic_store_ptr_release"
#endif
Expand Down
Loading

0 comments on commit 17b4b48

Please sign in to comment.