Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
0c9258a6
Commit
0c9258a6
authored
Mar 18, 2019
by
Pablo Galindo
Committed by
GitHub
Mar 18, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bpo-36332: Allow compile() to handle AST objects with assignment expressions (GH-12398)
parent
2ddc7f6d
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
17 additions
and
3 deletions
+17
-3
Lib/test/test_ast.py
Lib/test/test_ast.py
+11
-0
Misc/NEWS.d/next/Core and Builtins/2019-03-18-09-27-54.bpo-36332.yEC-Vz.rst
...ore and Builtins/2019-03-18-09-27-54.bpo-36332.yEC-Vz.rst
+2
-0
Python/ast.c
Python/ast.c
+4
-3
No files found.
Lib/test/test_ast.py
View file @
0c9258a6
...
...
@@ -135,6 +135,9 @@ exec_tests = [
"@deco1
\
n
@deco2()
\
n
class C: pass"
,
# Decorator with generator argument
"@deco(a for a in b)
\
n
def f(): pass"
,
# Simple assignment expression
"(a := 1)"
,
]
# These are compiled through "single"
...
...
@@ -276,6 +279,13 @@ class AST_Tests(unittest.TestCase):
with
self
.
subTest
(
action
=
"compiling"
,
input
=
i
,
kind
=
kind
):
compile
(
ast_tree
,
"?"
,
kind
)
def
test_ast_validation
(
self
):
# compile() is the only function that calls PyAST_Validate
snippets_to_validate
=
exec_tests
+
single_tests
+
eval_tests
for
snippet
in
snippets_to_validate
:
tree
=
ast
.
parse
(
snippet
)
compile
(
tree
,
'<string>'
,
'exec'
)
def
test_slice
(
self
):
slc
=
ast
.
parse
(
"x[::]"
).
body
[
0
].
value
.
slice
self
.
assertIsNone
(
slc
.
upper
)
...
...
@@ -1677,6 +1687,7 @@ exec_results = [
(
'Module'
,
[(
'AsyncFunctionDef'
,
(
3
,
0
),
'f'
,
(
'arguments'
,
[],
None
,
[],
[],
None
,
[]),
[(
'Pass'
,
(
3
,
15
))],
[(
'Name'
,
(
1
,
1
),
'deco1'
,
(
'Load'
,)),
(
'Call'
,
(
2
,
0
),
(
'Name'
,
(
2
,
1
),
'deco2'
,
(
'Load'
,)),
[],
[])],
None
,
None
)],
[]),
(
'Module'
,
[(
'ClassDef'
,
(
3
,
0
),
'C'
,
[],
[],
[(
'Pass'
,
(
3
,
9
))],
[(
'Name'
,
(
1
,
1
),
'deco1'
,
(
'Load'
,)),
(
'Call'
,
(
2
,
0
),
(
'Name'
,
(
2
,
1
),
'deco2'
,
(
'Load'
,)),
[],
[])])],
[]),
(
'Module'
,
[(
'FunctionDef'
,
(
2
,
0
),
'f'
,
(
'arguments'
,
[],
None
,
[],
[],
None
,
[]),
[(
'Pass'
,
(
2
,
9
))],
[(
'Call'
,
(
1
,
1
),
(
'Name'
,
(
1
,
1
),
'deco'
,
(
'Load'
,)),
[(
'GeneratorExp'
,
(
1
,
5
),
(
'Name'
,
(
1
,
6
),
'a'
,
(
'Load'
,)),
[(
'comprehension'
,
(
'Name'
,
(
1
,
12
),
'a'
,
(
'Store'
,)),
(
'Name'
,
(
1
,
17
),
'b'
,
(
'Load'
,)),
[],
0
)])],
[])],
None
,
None
)],
[]),
(
'Module'
,
[(
'Expr'
,
(
1
,
0
),
(
'NamedExpr'
,
(
1
,
1
),
(
'Name'
,
(
1
,
1
),
'a'
,
(
'Store'
,)),
(
'Constant'
,
(
1
,
6
),
1
,
None
)))],
[]),
]
single_results
=
[
(
'Interactive'
,
[(
'Expr'
,
(
1
,
0
),
(
'BinOp'
,
(
1
,
0
),
(
'Constant'
,
(
1
,
0
),
1
,
None
),
(
'Add'
,),
(
'Constant'
,
(
1
,
2
),
2
,
None
)))]),
...
...
Misc/NEWS.d/next/Core and Builtins/2019-03-18-09-27-54.bpo-36332.yEC-Vz.rst
0 → 100644
View file @
0c9258a6
The builtin :func:`compile` can now handle AST objects that contain
assignment expressions. Patch by Pablo Galindo.
Python/ast.c
View file @
0c9258a6
...
...
@@ -316,13 +316,14 @@ validate_expr(expr_ty exp, expr_context_ty ctx)
return
validate_exprs
(
exp
->
v
.
List
.
elts
,
ctx
,
0
);
case
Tuple_kind
:
return
validate_exprs
(
exp
->
v
.
Tuple
.
elts
,
ctx
,
0
);
case
NamedExpr_kind
:
return
validate_expr
(
exp
->
v
.
NamedExpr
.
value
,
Load
);
/* This last case doesn't have any checking. */
case
Name_kind
:
return
1
;
default:
PyErr_SetString
(
PyExc_SystemError
,
"unexpected expression"
);
return
0
;
}
PyErr_SetString
(
PyExc_SystemError
,
"unexpected expression"
);
return
0
;
}
static
int
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment