Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
260 commits
Select commit Hold shift + click to select a range
1d39be6
Tentative (incomplete) grammar for match statements
May 25, 2020
67354ad
Add tests for or-patterns
May 25, 2020
5d609c7
Add support for or-patterns
May 25, 2020
a222351
Merge branch 'patma' of https://github.com/brandtbucher/cpython into …
May 25, 2020
1f8bcbd
Clean up pattern matching compiler
May 26, 2020
eb704d4
Add attribute/name support
May 26, 2020
5a361bf
Validate name context
May 26, 2020
9c2e61c
Clean up pattern compiler
May 26, 2020
dddcca8
Don't bind _
May 26, 2020
af70c74
Add sequence tests
May 26, 2020
4191858
Add basic sequence support
May 26, 2020
33633c1
Add support for starred sub-patterns in sequences
May 27, 2020
a36542e
Format tests
May 27, 2020
d62a426
Merge branch 'master' of https://github.com/python/cpython into patma
May 27, 2020
76d108c
Simplify control-flow
May 27, 2020
34c54a0
Rename blocks
May 27, 2020
8bc9ae5
Add GET_MATCH_ITER opcode
May 27, 2020
2586ee6
Add string tests
May 27, 2020
adb04fd
Rename and add patma tests
May 28, 2020
6eb6783
Cleanup
May 28, 2020
f3c513d
Use Mapping/Sequence _collections_abc
May 28, 2020
fc472c7
Add missing error handling
May 28, 2020
26dd4fd
Add GET_MATCH_MAP
May 28, 2020
6ce6c34
Simplify new opcodes
May 28, 2020
ea0de16
Bump MAGIC_NUMBER
May 28, 2020
880a7c2
Add mapping support
May 29, 2020
297ceb2
Add a bunch of mapping tests, plus cleanup
May 29, 2020
1902b21
Reorganize pattern compiler
May 29, 2020
09e8fdc
Handle errors when creating new blocks
May 29, 2020
f612a27
Break out _collections_abc stuff in ceval
May 29, 2020
21ac879
Clean up new opcode docs
May 29, 2020
c103ab4
Simplify string checks
May 29, 2020
14409e0
Add basic AST optimization
May 29, 2020
e91c400
Fix asserts
May 29, 2020
7fb4d11
Implement default object.__match__
May 30, 2020
17356fb
Fix sequence matching bug
May 31, 2020
9d7bd07
BinOp -> BoolOp
May 31, 2020
77fc89d
Fix failing tests
May 31, 2020
bad2fe2
Add MATCH_TYPE opcode
May 31, 2020
2924d2f
Fix parsing/unparsing of name contexts
May 31, 2020
66852e9
Clean up grammar
Jun 1, 2020
f1a1eb9
Fix calls and error messages
Jun 1, 2020
de7c3f0
Miscellaneous cleanup
Jun 1, 2020
89974f0
Convert AST tests to native syntax
Jun 1, 2020
ac87fcb
Allow literal negative numbers
Jun 2, 2020
e3d07ab
Add checks for name bindings
Jun 2, 2020
9da2729
Cleanup
Jun 2, 2020
546b6c4
Add support for complex literals
Jun 2, 2020
1eddf27
Fix AST validation
Jun 2, 2020
04bad8e
MATCH_TYPE -> DESTRUCTURE
Jun 2, 2020
e4be187
Fix attribute parsing
Jun 2, 2020
9a06fe3
Clean up and add tests
Jun 3, 2020
bb0ed11
Support destructuring type matches
Jun 4, 2020
03212ea
Simplify final (or only) case
Jun 4, 2020
ccf8374
Tell the peepholer about all of the jumping we do
Jun 5, 2020
c7f121b
Reject f-strings, addition, and subtraction
Jun 5, 2020
8d61da2
__match_args__ must contain strings
Jun 5, 2020
0c15e70
Better error messages for tuples
Jun 5, 2020
28bf07b
Add length-checking opcodes
Jun 7, 2020
009ca7e
Start phasing in new opcodes
Jun 7, 2020
32b396f
Refactor sequence-matching opcodes
Jun 8, 2020
a7e0488
OLD_MATCH_MAP_STAR -> OLD_MATCH_MAP
Jun 9, 2020
eb0fe26
Support None/missing __match_args__
Jun 9, 2020
6da649b
Catch up with master
Jun 9, 2020
90422e4
Clean up map matches
Jun 9, 2020
73d4a2b
Get rid of LIST_POP
Jun 9, 2020
2c5226a
Validate missing attributes with __match_args__
Jun 10, 2020
f4644aa
Incomplete __match_args_required__ support
Jun 10, 2020
ce7b021
Finish __match_args_required__ support
Jun 10, 2020
99d805c
Catch up with master
Jun 10, 2020
6cc4128
Trivial grammar clean-ups
Jun 10, 2020
4973548
Add back type annotations.
Jun 11, 2020
b50e403
Simplify or-pattern parsing
Jun 11, 2020
1e0e35a
Refactor MATCH opcode.
Jun 11, 2020
f5974ed
Catch up with master
Jun 11, 2020
3130114
Tuples!
Jun 11, 2020
de78bc5
Fix line numbers
Jun 11, 2020
1aecf5f
Catch up with master
Jun 11, 2020
07b1474
Improve compilation of wildcards.
Jun 12, 2020
18ef577
Better error message for bad types.
Jun 12, 2020
4106b66
Fix duplicate test
Jun 12, 2020
5ef4df0
Progress on performance improvements
Jun 12, 2020
ff0c902
Fix segfault on missing attribute
Jun 12, 2020
38000fa
Cleanup
Jun 12, 2020
15e8824
Support for __match_args__ in dataclasses.
Jun 13, 2020
59c7a2f
Performance tweaks and __match__ protocol updates
Jun 15, 2020
db054a6
Match support for namedtuple.
Jun 18, 2020
db4a9b0
Lose __match_args_required__, speed up MATCH_SEQ
Jun 22, 2020
48f18eb
Remove a bunch of new opcodes
Jun 22, 2020
0c82655
Don't raise if no __match_args__
Jun 22, 2020
0ddf11c
Catch up with master
Jun 22, 2020
ef35201
Add ImpossibleMatchError
Jun 23, 2020
276c2b0
Clean up opcodes and fix pickle tests
Jun 23, 2020
14be5cf
Clean up ceval
Jun 24, 2020
23dc88b
Add typing.sealed
Jun 25, 2020
6fa3eb3
Allow unparenthesized tuples as targets
Jun 25, 2020
67146a3
Fix handling of copied mappings
Jun 25, 2020
d7bb7ee
Add tests
Jun 25, 2020
574593c
Fix match target parsing
Jun 25, 2020
340d66f
Add more mapping tests and a performance utility
Jun 26, 2020
ca58086
More cleanup
Jun 27, 2020
3ec5802
Rough revision of __match__ protocol
Jun 28, 2020
0d99af4
Remove old AST tests and add "simple" match tests
Jun 28, 2020
018e6ab
Cleanup
Jun 28, 2020
7a6d897
Add pattern_context
Jun 28, 2020
7953dee
Keep bound names in pattern context
Jun 28, 2020
214101e
Address recent __match__ protocol changes
Jun 29, 2020
9e170bc
Ditch __match__!
Jun 30, 2020
d696de7
Fix whitespace diff
Jun 30, 2020
5e621f6
Catch up with master
Jun 30, 2020
bb8fb1b
Clean up tests and fix refleak in mapping patterns
Jul 1, 2020
36398b1
Minor improvements for class matches
Jul 1, 2020
82055e2
Mapping destructuring improvements
Jul 1, 2020
c9b3b4e
Use tuple for dataclass __match_args__
Jul 2, 2020
793add3
Add tests for new match grammar
Jul 2, 2020
4efdb9a
Clean up compile.c and add SyntaxWarning
Jul 2, 2020
46f3350
Clean up compiler
Jul 3, 2020
9b343b7
Progress on reducing the number of blocks
Jul 3, 2020
a4fd8a3
Simplify emitted bytecode
Jul 3, 2020
c9ad4b4
Catch up with master
Jul 4, 2020
b642e08
Implement __match_args__ for AST classes
Jul 4, 2020
e39b3e2
No more leading dots
Jul 5, 2020
123e15b
Only use nonnegative integer indices
Jul 5, 2020
2f03ae0
Remove leading dot rules from unparsing logic
Jul 5, 2020
ef881fa
Use MATCH_ITEM for mapping and class patterns
Jul 5, 2020
eb86fa7
Reduce unnecessary EXTENDED_ARG instructions
Jul 6, 2020
f3d3d83
Documentation and cleanup
Jul 11, 2020
b0a02f5
Generalize conditional jump peepholing
Jul 12, 2020
da6ee98
MATCH_ITEM* -> GET_INDEX*
Jul 12, 2020
3eda95e
Revert old unparse hack
Jul 12, 2020
b3840be
Improve performance for wildcards
Jul 12, 2020
14e4eaa
Add/fix tests
Jul 13, 2020
4789bff
Fix refleaks when tracking names
Jul 13, 2020
1260139
Regenerate keyword module
Jul 13, 2020
34f8111
Add a ton of tests
Jul 14, 2020
3eadc53
Add more tests
Jul 14, 2020
b09e822
Catch up with master
Jul 14, 2020
7d23259
Update test
Jul 14, 2020
bed2e68
target -> subject
Jul 14, 2020
1a70d2c
Add tests
Jul 15, 2020
f83302f
Refactor loads
Jul 15, 2020
1ba56a0
Cleanup and better performance for default cases
Jul 17, 2020
3fd91d9
More cleanup
Jul 20, 2020
1de2a1c
Ditch unhelpful warning and add OPTIM note
Aug 16, 2020
7864718
Preserve TOS when compiling patterns
Aug 17, 2020
6fdc00c
Clean up some duplicate tests
Aug 18, 2020
ee2beb2
Improve test coverage for errors and warnings
Aug 19, 2020
b4b8416
Add more tests for runtime errors
Aug 19, 2020
f819084
Clean up comments
Aug 19, 2020
6228a57
Fix bugs in peephole optimizer changes
Aug 19, 2020
ce7aab4
Apparently the compiler's been rewritten for 3.10... :(
Aug 19, 2020
92f6c53
Disable unfinished validation
Aug 20, 2020
466becd
Skip performance benchmarking for some tests
Aug 20, 2020
a8d0a86
Fix typo
Aug 20, 2020
66d614b
Unify shared SyntaxError test logic
Aug 23, 2020
3f76403
Add two more tests for soft keywords
Aug 23, 2020
a527f66
Refactor MATCH_CLASS
Aug 23, 2020
2cc4fee
Catch up with master
Sep 9, 2020
19af7d5
Literal False/True/None compares by identity
Sep 16, 2020
601645b
Remove sealed
Sep 16, 2020
eed6806
Revert whitespace change
Sep 16, 2020
3c4796c
Assorted cleanup
Sep 16, 2020
45b1429
Add three more identity tests
Sep 16, 2020
99c5296
Catch up with master
Sep 25, 2020
5505fc3
Fixes for grammar changes
Sep 25, 2020
587ba87
Use sets to track map keys and attribute names.
Oct 19, 2020
a1e88fe
Add a few notes.
Oct 19, 2020
3a124db
Catch up with master
Oct 19, 2020
bd2bffb
Catch up with master
Oct 19, 2020
91661eb
"n := p" -> "p as n"
Oct 19, 2020
28ba072
Use new MatchAs node for named patterns
Oct 19, 2020
462796e
Add version check
Oct 19, 2020
ebd95c6
Raise for unreachable patterns
Oct 20, 2020
de1b020
Clean up error messages
Oct 20, 2020
f5c2482
Minor cleanup
Oct 20, 2020
4de619a
Reorganize some sequence/mapping logic
Oct 21, 2020
9f1c91d
More ceval cleanup
Oct 21, 2020
88bd791
Don't mark _ as local in patterns
Oct 21, 2020
12b6575
More cleanup
Oct 21, 2020
d394aa4
Add some tests for non-dict Mappings
Oct 21, 2020
2028445
Remove half-finished PyAST_Validate implementation
Oct 21, 2020
20b59b2
Remove some questionable fast paths
Oct 21, 2020
b42a3ac
Add test_patma to PGO
Oct 21, 2020
ccb5d90
Clean up SyntaxErroring
Oct 22, 2020
7a97a1f
Unsafe caching strikes again
Oct 22, 2020
ff011e0
Fix comment
Oct 22, 2020
99ae758
Remove unused get_len member
Oct 22, 2020
ad84403
Rename simple match flag
Oct 22, 2020
1b0602f
BoolOp -> MatchOr and fix constant folding
Oct 22, 2020
e212ceb
Minor cleanup
Oct 22, 2020
94b19ee
Unify grammar with PEP 634 (mostly)
Oct 22, 2020
77515ec
Clean up open_sequence_pattern usage
Oct 22, 2020
c0a06d1
More cleanup
Oct 23, 2020
26d35e5
Raise remaining SyntaxErrors in compiler
Oct 23, 2020
d43bb0b
Finish compiler cleanup
Oct 23, 2020
008f4ec
Catch up with master
Oct 23, 2020
d5f32da
Add NEWS entry
Oct 23, 2020
949822c
Improve precedence logic in ast.unparse
Oct 23, 2020
a31f2ef
Some VM cleanup
Oct 23, 2020
fe35b08
alternate -> alternative
Oct 23, 2020
e688005
Remove dict copy fast-path, add comments
Oct 24, 2020
5589f77
Finish ceval cleanup
Oct 24, 2020
a0f659a
Catch up with master
Oct 27, 2020
243f3d2
Regenerate the keyword module
Oct 27, 2020
a1ba3a7
Add regen-keyword to regen-all
Oct 27, 2020
812abd0
Catch up wih master
Oct 28, 2020
8b093eb
Remove iterator checks from sequence patterns
Oct 28, 2020
b85fde5
Catch up with master
Nov 6, 2020
130ec39
Catch up with master
Nov 11, 2020
e21d33a
Re-bump magic number
Nov 12, 2020
418c69d
Catch up with master
Nov 12, 2020
8bab3ed
Catch up with master
Dec 10, 2020
7bf4ee4
Catch up with master
Dec 10, 2020
16e218a
Fix compiler warning
Dec 10, 2020
fbd299b
Catch up with master
Dec 14, 2020
3279c57
Catch up with master
Dec 16, 2020
ff03e6b
Catch up with master
Dec 17, 2020
6f09968
Catch up with master
Dec 27, 2020
d9def0b
Catch up with master
Jan 5, 2021
ef87100
Catch up with master
Jan 8, 2021
7eee880
Catch up with master
Jan 13, 2021
f97b2eb
Catch up with master
Jan 18, 2021
165d491
Catch up with master
Feb 1, 2021
c732376
Catch up with master
Feb 2, 2021
c5729ec
Catch up with master
Feb 4, 2021
e149ab5
Catch up with master
Feb 5, 2021
d105180
Catch up with master
Feb 8, 2021
47c9218
CHECK -> RETURN_IF_FASLE
Feb 9, 2021
193f037
Cleanup
Feb 10, 2021
0618706
Catch up with master
Feb 10, 2021
b748e41
pattern_helper_load_constant -> ADDOP_LOAD_CONST
Feb 12, 2021
cc3357c
!x -> x == NULL
Feb 12, 2021
72b623c
pattern_helper_load_attr -> compiler_visit_expr
Feb 12, 2021
93cefc1
GET_INDEX/GET_INDEX_END -> BINARY_SUBSCR
Feb 12, 2021
62372c2
MATCH_KEYS -> MATCH_KEYS/COPY_DICT_WITHOUT_KEYS
Feb 12, 2021
2a5d0dd
Clean up MATCH_KEYS
Feb 12, 2021
fa97ee3
Refactor sequence matches to use existing ops
Feb 13, 2021
0de83a2
Clean up compiler_pattern_sequence
Feb 15, 2021
162f9b6
Clean up comment
Feb 15, 2021
49b2823
Factor out get_match_args
Feb 15, 2021
3f8fdaf
Replace TOS instead of pushing onto it
Feb 16, 2021
632a645
Catch up with master
Feb 16, 2021
0d56285
Catch up with master
Feb 19, 2021
69fa421
Fix NULL pointer error
Feb 20, 2021
1711207
Don't keep uninitialized tuples hanging around
Feb 20, 2021
f7c833e
Catch up with master
Feb 22, 2021
7762099
Fix new refleaks ("-R 3:3" is clean again)
Feb 22, 2021
5e269fb
Use PySet_CheckExact (thanks Pablo!)
Feb 22, 2021
ca6e332
Move some comments
Feb 23, 2021
61616fc
Catch up with master
Feb 24, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
More cleanup
  • Loading branch information
brandtbucher committed Oct 21, 2020
commit 12b65753145e38d9e4aa93ff8f5f11480b4153fe
47 changes: 26 additions & 21 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -3533,19 +3533,20 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
}

case TARGET(GET_LEN): {
Py_ssize_t l = PyObject_Length(TOP());
if (l < 0) {
// PUSH(len(TOS))
Py_ssize_t len_i = PyObject_Length(TOP());
if (len_i < 0) {
goto error;
}
// We might have one or more GET_INDEX_* instructions soon. Cache
// the length on the PyInterpreterState struct so they can reuse it:
PyInterpreterState *interp = PyInterpreterState_Get();
interp->get_len = l;
PyObject *len = PyLong_FromSsize_t(l);
if (!len) {
interp->get_len = len_i;
PyObject *len_o = PyLong_FromSsize_t(len_i);
if (!len_o) {
goto error;
}
PUSH(len);
PUSH(len_o);
DISPATCH();
}

Expand All @@ -3567,6 +3568,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
}

case TARGET(MATCH_MAPPING): {
// PUSH(isinstance(TOS, _collections_abc.Mapping))
PyObject *subject = TOP();
// Fast path for dicts:
if (PyDict_Check(subject)) {
Expand Down Expand Up @@ -3596,6 +3598,9 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
}

case TARGET(MATCH_SEQUENCE): {
// PUSH(isinstance(TOS, _collections_abc.Sequence)
// and not isinstance(TOS, (bytearray, bytes, str))
// and not hasattr(TOS, "__next__"))
PyObject *subject = TOP();
// Fast path for lists and tuples:
if (PyType_FastSubclass(Py_TYPE(subject),
Expand All @@ -3618,7 +3623,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
DISPATCH();
}
// Lazily import _collections_abc.Sequence, and keep it handy on the
// PyInterpreterState struct (it gets cleaned up at exit):
// PyInterpreterState struct (it gets cleaned up at exit):
PyInterpreterState *interp = PyInterpreterState_Get();
if (!interp->seq_abc) {
PyObject *abc = PyImport_ImportModule("_collections_abc");
Expand Down Expand Up @@ -3686,8 +3691,8 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
// PyInterpreterState struct. In addition to avoiding tons of
// redundant __len__ calls, this also handles length changes during
// extraction more intuitively. It's also much faster and simpler
// than trying to rotate it around on the stack and pop it off when
// we're done.
// than trying to rotate the length around on the stack and pop it
// off when we're done.
PyInterpreterState *interp = PyInterpreterState_Get();
assert(interp->get_len - 1 - oparg >= 0);
PyObject *item = PySequence_GetItem(TOP(), interp->get_len - 1 - oparg);
Expand All @@ -3704,27 +3709,27 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
// Ditto GET_INDEX_END's length handling.
PyInterpreterState *interp = PyInterpreterState_Get();
Py_ssize_t start = oparg & 0xFF;
Py_ssize_t stop = interp->get_len - (oparg >> 8);
assert(start <= stop);
PyObject *slice = PyList_New(stop - start);
Py_ssize_t size = interp->get_len - (oparg >> 8) - start;
assert(0 <= size);
PyObject *slice = PyList_New(size);
if (!slice) {
goto error;
}
PyObject **slice_items = PySequence_Fast_ITEMS(slice);
PyObject *subject = TOP();
if (PyList_CheckExact(subject) || PyTuple_CheckExact(subject)) {
// Fast path for lists and tuples: no errors!
assert(stop <= PySequence_Fast_GET_SIZE(subject));
PyObject **subject_items = PySequence_Fast_ITEMS(subject);
for (Py_ssize_t i = start; i < stop; i++) {
slice_items[i - start] = subject_items[i];
Py_INCREF(subject_items[i]);
// Fast path for lists and tuples (no errors)!
assert(start + size <= PySequence_Fast_GET_SIZE(subject));
memcpy(slice_items, &PySequence_Fast_ITEMS(subject)[start],
size * sizeof(PyObject*));
for (Py_ssize_t i = 0; i < size; i++) {
Py_INCREF(slice_items[i]);
}
}
else {
for (Py_ssize_t i = start; i < stop; i++) {
slice_items[i - start] = PySequence_GetItem(subject, i);
if (!slice_items[i - start]) {
for (Py_ssize_t i = 0; i < size; i++) {
slice_items[i] = PySequence_GetItem(subject, start + i);
if (!slice_items[i]) {
Py_DECREF(slice);
goto error;
}
Expand Down
5 changes: 4 additions & 1 deletion Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -5662,7 +5662,7 @@ compiler_pattern_constant(struct compiler *c, expr_ty p, pattern_context *pc)
PyObject *v = p->v.Constant.value;
// Literal True, False, and None are compared by identity.
// All others use equality.
ADDOP_COMPARE(c, v == Py_None || PyBool_Check(v) ? Is : Eq);
ADDOP_COMPARE(c, (v == Py_None || PyBool_Check(v)) ? Is : Eq);
return 1;
}

Expand Down Expand Up @@ -5946,6 +5946,9 @@ compiler_match(struct compiler *c, stmt_ty s)
}


#undef WILDCARD_CHECK


/* End of the compiler section, beginning of the assembler section */

/* do depth-first search of basic block graph, starting with block.
Expand Down