Author xxm
Recipients xxm
Date 2021-01-11.07:45:39
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
The following program will lead to a incorrect behavior of Python parser.  We change all variable to integer( forcely transformed to string) via ast.NodeTransformer. Then we compile the new code and execute it. It's surprising that code like "1=2; print(1)" can be compiled without error message. And more surprisingly, the execution result of "print(1)" is 2 ! !

import ast
class RewriteName(ast.NodeTransformer):
    def visit_Name(self, node):
        if != "print":
   = str(node.lineno)
        return node

code = "a = 2;print(a)"

myast = ast.parse(code)
transformer = RewriteName()
newast = transformer.visit(myast)

# print(ast.dump(newast))

print("new code:","------------")

c = compile(newast,'','exec')

output result:
new code: ------------
1 = 2

Expected result: (1). Syntax error during compilation. "1" should not be the correct identifier of program, even if it's forcely transformed to string. (2). The output of execution should be "1"  , since the parameter of "print()" in the new code is string "1".  

This incorrect behaviors exists on all version of Python(from Python2 to Python 3).
Date User Action Args
2021-01-11 07:45:39xxmsetrecipients: + xxm
2021-01-11 07:45:39xxmsetmessageid: <>
2021-01-11 07:45:39xxmlinkissue42889 messages
2021-01-11 07:45:39xxmcreate