FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

bpo-43892: Make match patterns explicit in AST by ncoghlan · Pull Request #25585 · python/cpython · GitHub

Repository navigation

Merged
Changes from 1 commit
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
1d729b4
WIP bpo-43892: Make match patterns explicit in AST
ncoghlan Apr 20, 2021
6f62cc9
Merge remote-tracking branch 'origin/master' into bpo-43892-explicit-…
ncoghlan Apr 22, 2021
2d25ff7
Everything except symtable.c now compiles
ncoghlan Apr 22, 2021
2a319fd
Link to the validator ticket
ncoghlan Apr 22, 2021
14a5b2b
Ensure unparsing tests always cover pattern matching
ncoghlan Apr 22, 2021
ba2932f
Get symtable.c compiling
ncoghlan Apr 23, 2021
1dccb6f
Require end attributes on pattern nodes
ncoghlan Apr 23, 2021
207a43e
MatchConstant -> MatchSingleton
ncoghlan Apr 23, 2021
ba104d1
Formatting tweak
ncoghlan Apr 23, 2021
7cdb735
Fix segfault in wildcard name bindings
ncoghlan Apr 23, 2021
efc0f73
Get test_patma compiling
ncoghlan Apr 23, 2021
a0e13da
Reject matching 0+0 and f-strings
ncoghlan Apr 23, 2021
1680513
Merge remote-tracking branch 'origin/master' into bpo-43892-explicit-…
ncoghlan Apr 23, 2021
54940a3
Dedicated MatchMapping arg for storing remaining keys
ncoghlan Apr 23, 2021
0b21140
Merge remote-tracking branch 'origin/master' into bpo-43892-explicit-…
ncoghlan Apr 25, 2021
d6e69f0
Use 'name' for identifier AST fields, not 'target'
ncoghlan Apr 25, 2021
5a0b4f2
Implement unparsing
ncoghlan Apr 25, 2021
e07781e
Add news entry
ncoghlan Apr 25, 2021
5cb9f0b
Merge remote-tracking branch 'origin/master' into bpo-43892-explicit-…
ncoghlan Apr 26, 2021
b17e7f4
Update AST documentation
ncoghlan Apr 26, 2021
466fccf
Update recursion depth for nested patterns in AST optimiser
ncoghlan Apr 26, 2021
6a9fbc8
Fix remaining AST doctest
ncoghlan Apr 26, 2021
e8a181a
Validate the AST emitted by the parser
ncoghlan Apr 26, 2021
0563d22
Merge remote-tracking branch 'origin/master' into bpo-43892-explicit-…
ncoghlan Apr 26, 2021
aa429c2
AST validator did not handle FunctionType nodes
ncoghlan Apr 26, 2021
e4dfa5a
Apply suggestions from code review
ncoghlan Apr 27, 2021
2c289e7
Replace MatchAlways() with MatchAs(NULL, NULL)
ncoghlan Apr 27, 2021
4ed22ca
Remove and ignore local doctest execution artifacts
ncoghlan Apr 27, 2021
8e5083c
Fix trailing whitespace
ncoghlan Apr 27, 2021
398929d
Apply suggestions from code review
brandtbucher Apr 27, 2021
fe182d4
Delete Doc/.gitignore
brandtbucher Apr 27, 2021
2b07617
Update assert
brandtbucher Apr 27, 2021
b8add8a
Doon asdl_seq_LEN
brandtbucher Apr 27, 2021
490db51
Revert AST validation step
brandtbucher Apr 27, 2021
ea4ea93
Reorder tests
brandtbucher Apr 28, 2021
e4cc5d7
Remove overly-aggressive optimizations
brandtbucher Apr 28, 2021
4faea47
Numeric literals are validated in the compiler
brandtbucher Apr 28, 2021
fe26e14
Validate numeric literals in the compiler
brandtbucher Apr 28, 2021
ac154fa
Revert to checking subexpressions in the validator
ncoghlan Apr 28, 2021
3ef2880
Ensure parser rejects 0+0 in match patterns
ncoghlan Apr 28, 2021
8ed9847
Don't add renamed test case back
ncoghlan Apr 28, 2021
46e7895
Restore optimisations as compiler is not longer going validation
ncoghlan Apr 28, 2021
8ba335e
Move syntax checking helper to pegen
ncoghlan Apr 28, 2021
File filter

Filter by extension

Filter by extension .c  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
Prev Previous commit
Next Next commit
Numeric literals are validated in the compiler
  • Loading branch information
brandtbucher authored Apr 28, 2021
commit 4faea477deb32a81ba22951dcc48479710b941dd
112 changes: 1 addition & 111 deletions Python/ast.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -368,116 +368,6 @@ validate_expr(struct validator *state, expr_ty exp, expr_context_ty ctx)
}


// Note: the ensure_literal_* functions are only used to validate a restricted
// set of non-recursive literals that have already been checked with
// validate_expr, so they don't accept the validator state
static int
ensure_literal_number(expr_ty exp, bool allow_real, bool allow_imaginary)
{
assert(exp->kind == Constant_kind);
PyObject *value = exp->v.Constant.value;
return (allow_real && PyFloat_CheckExact(value)) ||
(allow_real && PyLong_CheckExact(value)) ||
(allow_imaginary && PyComplex_CheckExact(value));
}

static int
ensure_literal_negative(expr_ty exp, bool allow_real, bool allow_imaginary)
{
assert(exp->kind == UnaryOp_kind);
// Must be negation ...
if (exp->v.UnaryOp.op != USub) {
return 0;
}
// ... of a constant ...
expr_ty operand = exp->v.UnaryOp.operand;
if (operand->kind != Constant_kind) {
return 0;
}
// ... number
return ensure_literal_number(operand, allow_real, allow_imaginary);
}

static int
ensure_literal_complex(expr_ty exp)
{
assert(exp->kind == BinOp_kind);
expr_ty left = exp->v.BinOp.left;
expr_ty right = exp->v.BinOp.right;
// Ensure op is addition or subtraction
if (exp->v.BinOp.op != Add && exp->v.BinOp.op != Sub) {
return 0;
}
// Check LHS is a real number
switch (left->kind)
{
case Constant_kind:
if (!ensure_literal_number(left, /*real=*/true, /*imaginary=*/false)) {
return 0;
}
break;
case UnaryOp_kind:
if (!ensure_literal_negative(left, /*real=*/true, /*imaginary=*/false)) {
return 0;
}
break;
default:
return 0;
}
// Check RHS is an imaginary number
switch (right->kind)
{
case Constant_kind:
if (!ensure_literal_number(right, /*real=*/false, /*imaginary=*/true)) {
return 0;
}
break;
case UnaryOp_kind:
if (!ensure_literal_negative(right, /*real=*/false, /*imaginary=*/true)) {
return 0;
}
break;
default:
return 0;
}
return 1;
}

static int
validate_pattern_match_value(struct validator *state, expr_ty exp)
{
if (!validate_expr(state, exp, Load)) {
return 0;
}

switch (exp->kind)
{
case Constant_kind:
case Attribute_kind:
// Constants and attribute lookups are always permitted
return 1;
case UnaryOp_kind:
// Negated numbers are permitted (whether real or imaginary)
// Compiler will complain if AST folding doesn't create a constant
if (ensure_literal_negative(exp, /*real=*/true, /*imaginary=*/true)) {
return 1;
}
break;
case BinOp_kind:
// Complex literals are permitted
// Compiler will complain if AST folding doesn't create a constant
if (ensure_literal_complex(exp)) {
return 1;
}
break;
default:
break;
}
PyErr_SetString(PyExc_SyntaxError,
"patterns may only match literals and attribute lookups");
return 0;
}

static int
validate_pattern(struct validator *state, pattern_ty p)
{
Expand All @@ -491,7 +381,7 @@ validate_pattern(struct validator *state, pattern_ty p)
// TODO: Ensure no subnodes use "_" as an ordinary identifier
switch (p->kind) {
case MatchValue_kind:
ret = validate_pattern_match_value(state, p->v.MatchValue.value);
ret = validate_expr(state, p->v.MatchValue.value, Load);
break;
case MatchSingleton_kind:
// TODO: Check constant is specifically None, True, or False
Expand Down

Back | FazBrowse Home | New Git URL