bpo-42128: Structural Pattern Matching (PEP 634) (GH-22917)

Co-authored-by: Guido van Rossum <guido@python.org>
Co-authored-by: Talin <viridia@gmail.com>
Co-authored-by: Pablo Galindo <pablogsal@gmail.com>
This commit is contained in:
Brandt Bucher 2021-02-26 14:51:55 -08:00 committed by GitHub
parent cc02b4f2e8
commit 145bf269df
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
43 changed files with 10867 additions and 2607 deletions

View file

@ -36,6 +36,8 @@ module Python
| With(withitem* items, stmt* body, string? type_comment)
| AsyncWith(withitem* items, stmt* body, string? type_comment)
| Match(expr subject, match_case* cases)
| Raise(expr? exc, expr? cause)
| Try(stmt* body, excepthandler* handlers, stmt* orelse, stmt* finalbody)
| Assert(expr test, expr? msg)
@ -87,6 +89,10 @@ module Python
-- can appear only in Subscript
| Slice(expr? lower, expr? upper, expr? step)
-- only used in patterns
| MatchAs(expr pattern, identifier name)
| MatchOr(expr* patterns)
-- col_offset is the byte offset in the utf8 string the parser uses
attributes (int lineno, int col_offset, int? end_lineno, int? end_col_offset)
@ -121,5 +127,7 @@ module Python
withitem = (expr context_expr, expr? optional_vars)
match_case = (expr pattern, expr? guard, stmt* body)
type_ignore = TypeIgnore(int lineno, string tag)
}

View file

@ -872,9 +872,10 @@ make_type(struct ast_state *state, const char *type, PyObject* base,
}
PyTuple_SET_ITEM(fnames, i, field);
}
result = PyObject_CallFunction((PyObject*)&PyType_Type, "s(O){OOOOOs}",
result = PyObject_CallFunction((PyObject*)&PyType_Type, "s(O){OOOOOOOs}",
type, base,
state->_fields, fnames,
state->__match_args__, fnames,
state->__module__,
state->ast,
state->__doc__, doc);
@ -1005,6 +1006,7 @@ static int add_ast_fields(struct ast_state *state)
empty_tuple = PyTuple_New(0);
if (!empty_tuple ||
PyObject_SetAttrString(state->AST_type, "_fields", empty_tuple) < 0 ||
PyObject_SetAttrString(state->AST_type, "__match_args__", empty_tuple) < 0 ||
PyObject_SetAttrString(state->AST_type, "_attributes", empty_tuple) < 0) {
Py_XDECREF(empty_tuple);
return -1;
@ -1405,6 +1407,7 @@ def generate_module_def(mod, f, internal_h):
state_strings = {
"ast",
"_fields",
"__match_args__",
"__doc__",
"__dict__",
"__module__",

File diff suppressed because it is too large Load diff