Skip to content

Commit 63b74de

Browse files
authored
Merge pull request #505 from python/main
Sync Fork from Upstream Repo
2 parents ddd5304 + 82e5c28 commit 63b74de

241 files changed

Lines changed: 12696 additions & 10089 deletions

File tree

Some content is hidden

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

.github/workflows/build.yml

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ jobs:
179179
strategy:
180180
fail-fast: false
181181
matrix:
182-
openssl_ver: [1.1.1k, 3.0.0-alpha17]
182+
openssl_ver: [1.1.1k, 3.0.0-beta1]
183183
env:
184184
OPENSSL_VER: ${{ matrix.openssl_ver }}
185185
MULTISSL_DIR: ${{ github.workspace }}/multissl
@@ -218,3 +218,46 @@ jobs:
218218
run: make pythoninfo
219219
- name: SSL tests
220220
run: ./python Lib/test/ssltests.py
221+
222+
223+
build_asan:
224+
name: 'Address sanitizer'
225+
runs-on: ubuntu-20.04
226+
needs: check_source
227+
if: needs.check_source.outputs.run_tests == 'true'
228+
env:
229+
OPENSSL_VER: 1.1.1k
230+
ASAN_OPTIONS: detect_leaks=0:allocator_may_return_null=1:handle_segv=0
231+
steps:
232+
- uses: actions/checkout@v2
233+
- name: Register gcc problem matcher
234+
run: echo "::add-matcher::.github/problem-matchers/gcc.json"
235+
- name: Install Dependencies
236+
run: sudo ./.github/workflows/posix-deps-apt.sh
237+
- name: Configure OpenSSL env vars
238+
run: |
239+
echo "MULTISSL_DIR=${GITHUB_WORKSPACE}/multissl" >> $GITHUB_ENV
240+
echo "OPENSSL_DIR=${GITHUB_WORKSPACE}/multissl/openssl/${OPENSSL_VER}" >> $GITHUB_ENV
241+
echo "LD_LIBRARY_PATH=${GITHUB_WORKSPACE}/multissl/openssl/${OPENSSL_VER}/lib" >> $GITHUB_ENV
242+
- name: 'Restore OpenSSL build'
243+
id: cache-openssl
244+
uses: actions/cache@v2.1.6
245+
with:
246+
path: ./multissl/openssl/${{ env.OPENSSL_VER }}
247+
key: ${{ runner.os }}-multissl-openssl-${{ env.OPENSSL_VER }}
248+
- name: Install OpenSSL
249+
if: steps.cache-openssl.outputs.cache-hit != 'true'
250+
run: python3 Tools/ssl/multissltests.py --steps=library --base-directory $MULTISSL_DIR --openssl $OPENSSL_VER --system Linux
251+
- name: Add ccache to PATH
252+
run: |
253+
echo "PATH=/usr/lib/ccache:$PATH" >> $GITHUB_ENV
254+
- name: Configure ccache action
255+
uses: hendrikmuhs/ccache-action@v1
256+
- name: Configure CPython
257+
run: ./configure --with-address-sanitizer --without-pymalloc
258+
- name: Build CPython
259+
run: make -j4
260+
- name: Display build info
261+
run: make pythoninfo
262+
- name: Tests
263+
run: xvfb-run make buildbottest TESTOPTS="-j4 -uall,-cpu -x test_ctypes test_crypt test_decimal test_faulthandler test_interpreters test___all__ test_idle test_tix test_tk test_ttk_guionly test_ttk_textonly"

.github/workflows/doc.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jobs:
3838
- name: 'Install build dependencies'
3939
run: make -C Doc/ PYTHON=../python venv
4040
- name: 'Build documentation'
41-
run: xvfb-run make -C Doc/ PYTHON=../python SPHINXOPTS="-q -W --keep-going -j4" doctest html suspicious
41+
run: xvfb-run make -C Doc/ PYTHON=../python SPHINXOPTS="-q -W --keep-going -j4" doctest html
4242
- name: 'Upload'
4343
uses: actions/upload-artifact@v2.2.3
4444
with:

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ matrix:
5353
- cd Doc
5454
- make venv PYTHON=python
5555
script:
56-
- make check html suspicious SPHINXOPTS="-q -W -j4"
56+
- make check html SPHINXOPTS="-q -W -j4"
5757
- name: "Documentation tests"
5858
os: linux
5959
language: c

Doc/c-api/complex.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ pointers. This is consistent throughout the API.
4646
:c:type:`Py_complex` representation.
4747
4848
49-
.. c:function:: Py_complex _Py_c_neg(Py_complex complex)
49+
.. c:function:: Py_complex _Py_c_neg(Py_complex num)
5050
51-
Return the negation of the complex number *complex*, using the C
51+
Return the negation of the complex number *num*, using the C
5252
:c:type:`Py_complex` representation.
5353
5454

Doc/c-api/concrete.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,4 @@ Other Objects
115115
coro.rst
116116
contextvars.rst
117117
datetime.rst
118+
typehints.rst

Doc/c-api/object.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,12 +311,12 @@ Object Protocol
311311
returned. This is the equivalent to the Python expression ``len(o)``.
312312
313313
314-
.. c:function:: Py_ssize_t PyObject_LengthHint(PyObject *o, Py_ssize_t default)
314+
.. c:function:: Py_ssize_t PyObject_LengthHint(PyObject *o, Py_ssize_t defaultvalue)
315315
316316
Return an estimated length for the object *o*. First try to return its
317317
actual length, then an estimate using :meth:`~object.__length_hint__`, and
318318
finally return the default value. On error return ``-1``. This is the
319-
equivalent to the Python expression ``operator.length_hint(o, default)``.
319+
equivalent to the Python expression ``operator.length_hint(o, defaultvalue)``.
320320
321321
.. versionadded:: 3.4
322322

Doc/c-api/structures.rst

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,7 @@ the definition of all other Python objects.
9999
100100
Return a :term:`borrowed reference`.
101101
102-
Use the :c:func:`Py_SET_TYPE` function to set an object type.
103-
104-
.. versionchanged:: 3.11
105-
:c:func:`Py_TYPE()` is changed to an inline static function.
102+
The :c:func:`Py_SET_TYPE` function must be used to set an object type.
106103
107104
108105
.. c:function:: int Py_IS_TYPE(PyObject *o, PyTypeObject *type)
@@ -124,10 +121,9 @@ the definition of all other Python objects.
124121
125122
Get the reference count of the Python object *o*.
126123
127-
Use the :c:func:`Py_SET_REFCNT()` function to set an object reference count.
128-
129124
.. versionchanged:: 3.10
130125
:c:func:`Py_REFCNT()` is changed to the inline static function.
126+
Use :c:func:`Py_SET_REFCNT()` to set an object reference count.
131127
132128
133129
.. c:function:: void Py_SET_REFCNT(PyObject *o, Py_ssize_t refcnt)
@@ -141,10 +137,7 @@ the definition of all other Python objects.
141137
142138
Get the size of the Python object *o*.
143139
144-
Use the :c:func:`Py_SET_SIZE` function to set an object size.
145-
146-
.. versionchanged:: 3.11
147-
:c:func:`Py_SIZE()` is changed to an inline static function.
140+
The :c:func:`Py_SET_SIZE` function must be used to set an object size.
148141
149142
150143
.. c:function:: void Py_SET_SIZE(PyVarObject *o, Py_ssize_t size)

Doc/c-api/typehints.rst

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
.. highlight:: c
2+
3+
.. _typehintobjects:
4+
5+
Objects for Type Hinting
6+
------------------------
7+
8+
Various built-in types for type hinting are provided. Currently,
9+
two types exist -- :ref:`GenericAlias <types-genericalias>` and
10+
:ref:`Union <types-union>`. Only ``GenericAlias`` is exposed to C.
11+
12+
.. c:function:: PyObject* Py_GenericAlias(PyObject *origin, PyObject *args)
13+
14+
Create a :ref:`GenericAlias <types-genericalias>` object.
15+
Equivalent to calling the Python class
16+
:class:`types.GenericAlias`. The *origin* and *args* arguments set the
17+
``GenericAlias``\ 's ``__origin__`` and ``__args__`` attributes respectively.
18+
*origin* should be a :c:type:`PyTypeObject*`, and *args* can be a
19+
:c:type:`PyTupleObject*` or any ``PyObject*``. If *args* passed is
20+
not a tuple, a 1-tuple is automatically constructed and ``__args__`` is set
21+
to ``(args,)``.
22+
Minimal checking is done for the arguments, so the function will succeed even
23+
if *origin* is not a type.
24+
The ``GenericAlias``\ 's ``__parameters__`` attribute is constructed lazily
25+
from ``__args__``. On failure, an exception is raised and ``NULL`` is
26+
returned.
27+
28+
Here's an example of how to make an extension type generic::
29+
30+
...
31+
static PyMethodDef my_obj_methods[] = {
32+
// Other methods.
33+
...
34+
{"__class_getitem__", (PyCFunction)Py_GenericAlias, METH_O|METH_CLASS, "See PEP 585"}
35+
...
36+
}
37+
38+
.. seealso:: The data model method :meth:`__class_getitem__`.
39+
40+
.. versionadded:: 3.9
41+
42+
.. c:var:: PyTypeObject Py_GenericAliasType
43+
44+
The C type of the object returned by :c:func:`Py_GenericAlias`. Equivalent to
45+
:class:`types.GenericAlias` in Python.
46+
47+
.. versionadded:: 3.9

Doc/data/stable_abi.dat

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ function,PyFloat_GetInfo,3.2,
272272
function,PyFloat_GetMax,3.2,
273273
function,PyFloat_GetMin,3.2,
274274
var,PyFloat_Type,3.2,
275+
type,PyFrameObject,3.2,
275276
function,PyFrame_GetCode,3.10,
276277
function,PyFrame_GetLineNumber,3.10,
277278
function,PyFrozenSet_New,3.2,
@@ -784,7 +785,7 @@ var,Py_FileSystemDefaultEncoding,3.2,
784785
function,Py_Finalize,3.2,
785786
function,Py_FinalizeEx,3.6,
786787
function,Py_GenericAlias,3.9,
787-
function,Py_GenericAliasType,3.9,
788+
var,Py_GenericAliasType,3.9,
788789
function,Py_GetBuildInfo,3.2,
789790
function,Py_GetCompiler,3.2,
790791
function,Py_GetCopyright,3.2,
@@ -825,8 +826,6 @@ function,Py_XNewRef,3.10,
825826
type,Py_intptr_t,3.2,
826827
type,Py_ssize_t,3.2,
827828
type,Py_uintptr_t,3.2,
828-
type,_frame,3.2,
829-
type,_node,3.2,
830829
type,allocfunc,3.2,
831830
type,binaryfunc,3.2,
832831
type,descrgetfunc,3.2,

Doc/faq/programming.rst

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1826,6 +1826,85 @@ For example, here is the implementation of
18261826
return True
18271827
return False
18281828

1829+
How do I cache method calls?
1830+
----------------------------
1831+
1832+
The two principal tools for caching methods are
1833+
:func:`functools.cached_property` and :func:`functools.lru_cache`. The
1834+
former stores results at the instance level and the latter at the class
1835+
level.
1836+
1837+
The *cached_property* approach only works with methods that do not take
1838+
any arguments. It does not create a reference to the instance. The
1839+
cached method result will be kept only as long as the instance is alive.
1840+
1841+
The advantage is that when an instance is not longer used, the cached
1842+
method result will be released right away. The disadvantage is that if
1843+
instances accumulate, so too will the accumulated method results. They
1844+
can grow without bound.
1845+
1846+
The *lru_cache* approach works with methods that have hashable
1847+
arguments. It creates a reference to the instance unless special
1848+
efforts are made to pass in weak references.
1849+
1850+
The advantage of the least recently used algorithm is that the cache is
1851+
bounded by the specified *maxsize*. The disadvantage is that instances
1852+
are kept alive until they age out of the cache or until the cache is
1853+
cleared.
1854+
1855+
This example shows the various techniques::
1856+
1857+
class Weather:
1858+
"Lookup weather information on a government website"
1859+
1860+
def __init__(self, station_id):
1861+
self._station_id = station_id
1862+
# The _station_id is private and immutable
1863+
1864+
def current_temperature(self):
1865+
"Latest hourly observation"
1866+
# Do not cache this because old results
1867+
# can be out of date.
1868+
1869+
@cached_property
1870+
def location(self):
1871+
"Return the longitude/latitude coordinates of the station"
1872+
# Result only depends on the station_id
1873+
1874+
@lru_cache(maxsize=20)
1875+
def historic_rainfall(self, date, units='mm'):
1876+
"Rainfall on a given date"
1877+
# Depends on the station_id, date, and units.
1878+
1879+
The above example assumes that the *station_id* never changes. If the
1880+
relevant instance attributes are mutable, the *cached_property* approach
1881+
can't be made to work because it cannot detect changes to the
1882+
attributes.
1883+
1884+
The *lru_cache* approach can be made to work, but the class needs to define the
1885+
*__eq__* and *__hash__* methods so the cache can detect relevant attribute
1886+
updates::
1887+
1888+
class Weather:
1889+
"Example with a mutable station identifier"
1890+
1891+
def __init__(self, station_id):
1892+
self.station_id = station_id
1893+
1894+
def change_station(self, station_id):
1895+
self.station_id = station_id
1896+
1897+
def __eq__(self, other):
1898+
return self.station_id == other.station_id
1899+
1900+
def __hash__(self):
1901+
return hash(self.station_id)
1902+
1903+
@lru_cache(maxsize=20)
1904+
def historic_rainfall(self, date, units='cm'):
1905+
'Rainfall on a given date'
1906+
# Depends on the station_id, date, and units.
1907+
18291908

18301909
Modules
18311910
=======

0 commit comments

Comments
 (0)