Xinmeng Xia <xi...@smail.nju.edu.cn> added the comment:
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. ---------- _______________________________________ Python tracker <rep...@bugs.python.org> <https://bugs.python.org/issue42889> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com