Message384879
Sorry, my description is a little confusing. My points lie on function 'compile' and 'exec'. Yes, I agree. AST can be modified and don't correspond to valid programs. But I don't think this invaild program can be compiled and exec without any check. It's dangerous.
See the following program: For "compile" and "exec", no error is reported on Python 3.5-3.8 while error messages are reported on Python 3.9 and 3.10
======================================
import ast
class RewriteName(ast.NodeTransformer):
def visit_Name(self, node):
if node.id != "print":
node.id = str(False)
print(type(node.id))
return node
code = "a = 2;print(a)"
myast = ast.parse(code)
transformer = RewriteName()
newast = transformer.visit(myast)
c = compile(newast,'','exec')
exec(c)
=====================================
Error message on Python 3.9 and 3.10.
-------------------------------------
<class 'str'>
<class 'str'>
Traceback (most recent call last):
File "/home/xxm/Desktop/nameChanging/report/test1.py", line 574, in <module>
c = compile(newast,'','exec')
ValueError: Name node can't be used with 'False' constant
-------------------------------------
In fact, in class RewriteName, when "node.id" is assigned, the parser will check whether the identifier is a "str". If not,"TypeError: AST identifier must be of type str" will be reported. However, it's not enough. In Python, identifier names have their own naming rules. "str" could be "+","1","False", but these are not legally id. So the above error could be reported. |
|
Date |
User |
Action |
Args |
2021-01-12 03:18:07 | xxm | set | recipients:
+ xxm, gvanrossum |
2021-01-12 03:18:07 | xxm | set | messageid: <1610421487.76.0.321662384872.issue42889@roundup.psfhosted.org> |
2021-01-12 03:18:07 | xxm | link | issue42889 messages |
2021-01-12 03:18:05 | xxm | create | |
|