Skip to content

Commit 5749575

Browse files
Merge branch 'master' into randrange-index
2 parents 3ad3c36 + 4140f10 commit 5749575

File tree

87 files changed

+2524
-757
lines changed

Some content is hidden

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

87 files changed

+2524
-757
lines changed

.github/CODEOWNERS

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Objects/frameobject.c @markshannon
1919
Objects/call.c @markshannon
2020
Python/ceval.c @markshannon
2121
Python/compile.c @markshannon
22+
Python/ast_opt.c @isidentical
2223

2324
# Hashing
2425
**/*hashlib* @python/crypto-team @tiran
@@ -84,6 +85,12 @@ Include/pytime.h @pganssle @abalkin
8485
/Lib/test/test_peg_generator/ @pablogsal @lysnikolaou
8586
/Grammar/python.gram @pablogsal @lysnikolaou
8687

88+
# AST
89+
Python/ast.c @isidentical
90+
Parser/asdl.py @isidentical
91+
Parser/asdl_c.py @isidentical
92+
Lib/ast.py @isidentical
93+
8794
# SQLite 3
8895
**/*sqlite* @berkerpeksag
8996

.github/problem-matchers/msvc.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"__comment": "Taken from vscode's vs/workbench/contrib/tasks/common/problemMatcher.ts msCompile rule",
3+
"problemMatcher": [
4+
{
5+
"owner": "msvc-problem-matcher",
6+
"pattern": [
7+
{
8+
"regexp": "^(?:\\s+\\d+\\>)?([^\\s].*)\\((\\d+),?(\\d+)?(?:,\\d+,\\d+)?\\)\\s*:\\s+(error|warning|info)\\s+(\\w{1,2}\\d+)\\s*:\\s*(.*)$",
9+
"file": 1,
10+
"line": 2,
11+
"column": 3,
12+
"severity": 4,
13+
"code": 5,
14+
"message": 6
15+
}
16+
]
17+
}
18+
]
19+
}

.github/workflows/build.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ jobs:
9999
if: needs.check_source.outputs.run_tests == 'true'
100100
steps:
101101
- uses: actions/checkout@v2
102+
- name: Register MSVC problem matcher
103+
run: echo "::add-matcher::.github/problem-matchers/msvc.json"
102104
- name: Build CPython
103105
run: .\PCbuild\build.bat -e -p x64
104106
- name: Display build info

Doc/library/itertools.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,18 @@ which incur interpreter overhead.
786786
def dotproduct(vec1, vec2):
787787
return sum(map(operator.mul, vec1, vec2))
788788

789+
def convolve(signal, kernel):
790+
# See: https://betterexplained.com/articles/intuitive-convolution/
791+
# convolve(data, [0.25, 0.25, 0.25, 0.25]) --> Moving average (blur)
792+
# convolve(data, [1, -1]) --> 1st finite difference (1st derivative)
793+
# convolve(data, [1, -2, 1]) --> 2nd finite difference (2nd derivative)
794+
kernel = list(reversed(kernel))
795+
n = len(kernel)
796+
window = collections.deque([0] * n, maxlen=n)
797+
for x in chain(signal, repeat(0, n-1)):
798+
window.append(x)
799+
yield sum(map(operator.mul, kernel, window))
800+
789801
def flatten(list_of_lists):
790802
"Flatten one level of nesting"
791803
return chain.from_iterable(list_of_lists)

Doc/library/random.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ Functions for integers
145145
or ``randrange('10')`` will be changed from :exc:`ValueError` to
146146
:exc:`TypeError`.
147147

148-
149148
.. function:: randint(a, b)
150149

151150
Return a random integer *N* such that ``a <= N <= b``. Alias for

Doc/library/sqlite3.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ Connection Objects
546546
con.close()
547547

548548

549-
.. method:: backup(target, *, pages=0, progress=None, name="main", sleep=0.250)
549+
.. method:: backup(target, *, pages=-1, progress=None, name="main", sleep=0.250)
550550

551551
This method makes a backup of a SQLite database even while it's being accessed
552552
by other clients, or concurrently by the same connection. The copy will be

Doc/library/subprocess.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,8 +1187,9 @@ calls these functions.
11871187
The arguments shown above are merely some common ones.
11881188
The full function signature is largely the same as that of :func:`run` -
11891189
most arguments are passed directly through to that interface.
1190-
However, explicitly passing ``input=None`` to inherit the parent's
1191-
standard input file handle is not supported.
1190+
One API deviation from :func:`run` behavior exists: passing ``input=None``
1191+
will behave the same as ``input=b''`` (or ``input=''``, depending on other
1192+
arguments) rather than using the parent's standard input file handle.
11921193

11931194
By default, this function will return the data as encoded bytes. The actual
11941195
encoding of the output data may depend on the command being invoked, so the

Doc/library/tkinter.font.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ The different font weights and slants are:
9191

9292
Return the names of defined fonts.
9393

94-
.. function:: nametofont(name)
94+
.. function:: nametofont(name, root=None)
9595

96-
Return a :class:`Font` representation of a tk named font.
96+
Return a :class:`Font` representation of a tk named font.
97+
98+
.. versionchanged:: 3.10
99+
The *root* parameter was added.

Doc/whatsnew/3.9.rst

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1482,4 +1482,37 @@ and to match the behavior of static type checkers specified in the PEP.
14821482
File "<stdin>", line 1, in <module>
14831483
TypeError: unhashable type: 'set'
14841484

1485-
(Contributed by Yurii Karabas in :issue:`42345`.)
1485+
(Contributed by Yurii Karabas in :issue:`42345`.)
1486+
1487+
macOS 11.0 (Big Sur) and Apple Silicon Mac support
1488+
--------------------------------------------------
1489+
1490+
As of 3.9.1, Python now fully supports building and running on macOS 11.0
1491+
(Big Sur) and on Apple Silicon Macs (based on the ``ARM64`` architecture).
1492+
A new universal build variant, ``universal2``, is now available to natively
1493+
support both ``ARM64`` and ``Intel 64`` in one set of executables. Binaries
1494+
can also now be built on current versions of macOS to be deployed on a range
1495+
of older macOS versions (tested to 10.9) while making some newer OS
1496+
functions and options conditionally available based on the operating system
1497+
version in use at runtime ("weaklinking").
1498+
1499+
(Contributed by Ronald Oussoren and Lawrence D'Anna in :issue:`41100`.)
1500+
1501+
Notable changes in Python 3.9.2
1502+
===============================
1503+
1504+
collections.abc
1505+
---------------
1506+
1507+
:class:`collections.abc.Callable` generic now flattens type parameters, similar
1508+
to what :data:`typing.Callable` currently does. This means that
1509+
``collections.abc.Callable[[int, str], str]`` will have ``__args__`` of
1510+
``(int, str, str)``; previously this was ``([int, str], str)``. To allow this
1511+
change, :class:`types.GenericAlias` can now be subclassed, and a subclass will
1512+
be returned when subscripting the :class:`collections.abc.Callable` type.
1513+
Code which accesses the arguments via :func:`typing.get_args` or ``__args__``
1514+
need to account for this change. A :exc:`DeprecationWarning` may be emitted for
1515+
invalid forms of parameterizing :class:`collections.abc.Callable` which may have
1516+
passed silently in Python 3.9.1. This :exc:`DeprecationWarning` will
1517+
become a :exc:`TypeError` in Python 3.10.
1518+
(Contributed by Ken Jin in :issue:`42195`.)

Include/cpython/object.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,13 @@ PyAPI_FUNC(Py_ssize_t) _Py_GetRefTotal(void);
3535
_PyObject_{Get,Set,Has}AttrId are __getattr__ versions using _Py_Identifier*.
3636
*/
3737
typedef struct _Py_Identifier {
38-
struct _Py_Identifier *next;
3938
const char* string;
40-
PyObject *object;
39+
// Index in PyInterpreterState.unicode.ids.array. It is process-wide
40+
// unique and must be initialized to -1.
41+
Py_ssize_t index;
4142
} _Py_Identifier;
4243

43-
#define _Py_static_string_init(value) { .next = NULL, .string = value, .object = NULL }
44+
#define _Py_static_string_init(value) { .string = value, .index = -1 }
4445
#define _Py_static_string(varname, value) static _Py_Identifier varname = _Py_static_string_init(value)
4546
#define _Py_IDENTIFIER(varname) _Py_static_string(PyId_##varname, #varname)
4647

Include/internal/pycore_interp.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,31 @@ struct _Py_bytes_state {
6464
PyBytesObject *characters[256];
6565
};
6666

67+
struct _Py_unicode_ids {
68+
Py_ssize_t size;
69+
PyObject **array;
70+
};
71+
6772
struct _Py_unicode_state {
6873
// The empty Unicode object is a singleton to improve performance.
6974
PyObject *empty_string;
7075
/* Single character Unicode strings in the Latin-1 range are being
7176
shared as well. */
7277
PyObject *latin1[256];
7378
struct _Py_unicode_fs_codec fs_codec;
79+
80+
/* This dictionary holds all interned unicode strings. Note that references
81+
to strings in this dictionary are *not* counted in the string's ob_refcnt.
82+
When the interned string reaches a refcnt of 0 the string deallocation
83+
function will delete the reference from this dictionary.
84+
85+
Another way to look at this is that to say that the actual reference
86+
count of a string is: s->ob_refcnt + (s->state ? 2 : 0)
87+
*/
88+
PyObject *interned;
89+
90+
// Unicode identifiers (_Py_Identifier): see _PyUnicode_FromId()
91+
struct _Py_unicode_ids ids;
7492
};
7593

7694
struct _Py_float_state {
@@ -173,6 +191,27 @@ struct atexit_state {
173191
};
174192

175193

194+
// Type attribute lookup cache: speed up attribute and method lookups,
195+
// see _PyType_Lookup().
196+
struct type_cache_entry {
197+
unsigned int version; // initialized from type->tp_version_tag
198+
PyObject *name; // reference to exactly a str or None
199+
PyObject *value; // borrowed reference or NULL
200+
};
201+
202+
#define MCACHE_SIZE_EXP 12
203+
#define MCACHE_STATS 0
204+
205+
struct type_cache {
206+
struct type_cache_entry hashtable[1 << MCACHE_SIZE_EXP];
207+
#if MCACHE_STATS
208+
size_t hits;
209+
size_t misses;
210+
size_t collisions;
211+
#endif
212+
};
213+
214+
176215
/* interpreter state */
177216

178217
#define _PY_NSMALLPOSINTS 257
@@ -277,6 +316,7 @@ struct _is {
277316
struct _Py_exc_state exc_state;
278317

279318
struct ast_state ast;
319+
struct type_cache type_cache;
280320
};
281321

282322
extern void _PyInterpreterState_ClearModules(PyInterpreterState *interp);

Include/internal/pycore_object.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ _PyType_HasFeature(PyTypeObject *type, unsigned long feature) {
2727
return ((type->tp_flags & feature) != 0);
2828
}
2929

30+
extern void _PyType_InitCache(PyInterpreterState *interp);
31+
32+
3033
/* Inline functions trading binary compatibility for speed:
3134
_PyObject_Init() is the fast version of PyObject_Init(), and
3235
_PyObject_InitVar() is the fast version of PyObject_InitVar().

Include/internal/pycore_pylifecycle.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ extern void _PyExc_Fini(PyThreadState *tstate);
7676
extern void _PyImport_Fini(void);
7777
extern void _PyImport_Fini2(void);
7878
extern void _PyGC_Fini(PyThreadState *tstate);
79-
extern void _PyType_Fini(void);
79+
extern void _PyType_Fini(PyThreadState *tstate);
8080
extern void _Py_HashRandomization_Fini(void);
8181
extern void _PyUnicode_Fini(PyThreadState *tstate);
8282
extern void _PyUnicode_ClearInterned(PyThreadState *tstate);

Include/internal/pycore_runtime.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ typedef struct _Py_AuditHookEntry {
4949
void *userData;
5050
} _Py_AuditHookEntry;
5151

52+
struct _Py_unicode_runtime_ids {
53+
PyThread_type_lock lock;
54+
Py_ssize_t next_index;
55+
};
56+
5257
/* Full Python runtime state */
5358

5459
typedef struct pyruntimestate {
@@ -106,6 +111,8 @@ typedef struct pyruntimestate {
106111
void *open_code_userdata;
107112
_Py_AuditHookEntry *audit_hook_head;
108113

114+
struct _Py_unicode_runtime_ids unicode_ids;
115+
109116
// XXX Consolidate globals found via the check-c-globals script.
110117
} _PyRuntimeState;
111118

Lib/ast.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ def literal_eval(node_or_string):
6363
if isinstance(node_or_string, Expression):
6464
node_or_string = node_or_string.body
6565
def _raise_malformed_node(node):
66-
raise ValueError(f'malformed node or string: {node!r}')
66+
msg = "malformed node or string"
67+
if lno := getattr(node, 'lineno', None):
68+
msg += f' on line {lno}'
69+
raise ValueError(msg + f': {node!r}')
6770
def _convert_num(node):
6871
if not isinstance(node, Constant) or type(node.value) not in (int, float, complex):
6972
_raise_malformed_node(node)

Lib/enum.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ class EnumMeta(type):
178178
Metaclass for Enum
179179
"""
180180
@classmethod
181-
def __prepare__(metacls, cls, bases):
181+
def __prepare__(metacls, cls, bases, **kwds):
182182
# check that previous enum members do not exist
183183
metacls._check_for_existing_members(cls, bases)
184184
# create the namespace dict
@@ -235,10 +235,10 @@ def __new__(metacls, cls, bases, classdict, **kwds):
235235
# create our new Enum type
236236
if bases:
237237
bases = (_NoInitSubclass, ) + bases
238-
enum_class = type.__new__(metacls, cls, bases, classdict)
238+
enum_class = super().__new__(metacls, cls, bases, classdict, **kwds)
239239
enum_class.__bases__ = enum_class.__bases__[1:] #or (object, )
240240
else:
241-
enum_class = type.__new__(metacls, cls, bases, classdict)
241+
enum_class = super().__new__(metacls, cls, bases, classdict, **kwds)
242242
old_init_subclass = getattr(enum_class, '__init_subclass__', None)
243243
# and restore the new one (if there was one)
244244
if new_init_subclass is not None:

Lib/random.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@
5151
from math import tau as TWOPI, floor as _floor, isfinite as _isfinite
5252
from os import urandom as _urandom
5353
from _collections_abc import Set as _Set, Sequence as _Sequence
54+
from operator import index as _index
5455
from itertools import accumulate as _accumulate, repeat as _repeat
5556
from bisect import bisect as _bisect
56-
from operator import index as _index
5757
import os as _os
5858
import _random
5959

Lib/subprocess.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,11 @@ def check_output(*popenargs, timeout=None, **kwargs):
420420
if 'input' in kwargs and kwargs['input'] is None:
421421
# Explicitly passing input=None was previously equivalent to passing an
422422
# empty string. That is maintained here for backwards compatibility.
423-
kwargs['input'] = '' if kwargs.get('universal_newlines', False) else b''
423+
if kwargs.get('universal_newlines') or kwargs.get('text'):
424+
empty = ''
425+
else:
426+
empty = b''
427+
kwargs['input'] = empty
424428

425429
return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
426430
**kwargs).stdout

Lib/test/crashers/bogus_code_obj.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@
1414

1515
import types
1616

17-
co = types.CodeType(0, 0, 0, 0, 0, b'\x04\x71\x00\x00',
17+
co = types.CodeType(0, 0, 0, 0, 0, 0, b'\x04\x00\x71\x00',
1818
(), (), (), '', '', 1, b'')
1919
exec(co)

Lib/test/test_asdl_parser.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Tests for the asdl parser in Parser/asdl.py"""
22

33
import importlib.machinery
4+
import importlib.util
45
import os
56
from os.path import dirname
67
import sys
@@ -26,7 +27,10 @@ def setUpClass(cls):
2627
sys.path.insert(0, parser_dir)
2728
loader = importlib.machinery.SourceFileLoader(
2829
'asdl', os.path.join(parser_dir, 'asdl.py'))
29-
cls.asdl = loader.load_module()
30+
spec = importlib.util.spec_from_loader('asdl', loader)
31+
module = importlib.util.module_from_spec(spec)
32+
loader.exec_module(module)
33+
cls.asdl = module
3034
cls.mod = cls.asdl.parse(os.path.join(parser_dir, 'Python.asdl'))
3135
cls.assertTrue(cls.asdl.check(cls.mod), 'Module validation failed')
3236

Lib/test/test_ast.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,6 +1011,18 @@ def test_literal_eval_trailing_ws(self):
10111011
self.assertEqual(ast.literal_eval(" \t -1"), -1)
10121012
self.assertRaises(IndentationError, ast.literal_eval, "\n -1")
10131013

1014+
def test_literal_eval_malformed_lineno(self):
1015+
msg = r'malformed node or string on line 3:'
1016+
with self.assertRaisesRegex(ValueError, msg):
1017+
ast.literal_eval("{'a': 1,\n'b':2,\n'c':++3,\n'd':4}")
1018+
1019+
node = ast.UnaryOp(
1020+
ast.UAdd(), ast.UnaryOp(ast.UAdd(), ast.Constant(6)))
1021+
self.assertIsNone(getattr(node, 'lineno', None))
1022+
msg = r'malformed node or string:'
1023+
with self.assertRaisesRegex(ValueError, msg):
1024+
ast.literal_eval(node)
1025+
10141026
def test_bad_integer(self):
10151027
# issue13436: Bad error message with invalid numeric values
10161028
body = [ast.ImportFrom(module='time',

0 commit comments

Comments
 (0)