Author terry.reedy
Recipients gvanrossum, lys.nikolaou, serhiy.storchaka, terry.reedy, xxm
Date 2021-01-16.00:38:23
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
The parser is not involved here.  The transformed code is *not* equivalent to "1=2; print(1)" because you replace 'a' with the string '1', not the int 1.  The result is to transform
 "globals()['a']=2; print(globals()['a'])" to 
 "globals()['1']=2; print(globals()['1'])"
This works because globals() is a regular dict.

If one does try to make the code equivalent to "1=2; print(1)" by replacing with 1, the result is an error.

Traceback (most recent call last):
  File "F:\Python\a\", line 19, in <module>
  File "C:\Programs\Python310\lib\", line 1567, in unparse
    return unparser.visit(ast_obj)
  File "C:\Programs\Python310\lib\", line 805, in visit
    return "".join(self._source)
TypeError: sequence item 1: expected str instance, int found

With unparse removed, I get a compile error when the identifier type is checked.

Traceback (most recent call last):
  File "F:\Python\a\", line 19, in <module>
    c = compile(newast,'','exec')
TypeError: AST identifier must be of type str

In the binary example, unparsing gives an infinite recursion error with this repeated sequence.

  File "C:\Programs\Python310\lib\", line 1372, in visit_BinOp
  File "C:\Programs\Python310\lib\", line 798, in traverse
  File "C:\Programs\Python310\lib\", line 410, in visit
    return visitor(node)

Without unparse, the compile call crashes.  (In IDLE, this means there is an unrequested restart of the Shell subprocess that executes user code, without IDLE crashing.)  I suspect that there is a related loop in the compile C code that crashes the process before any checking is done.  If so, the situation would be similar to your #42887 and we may not be able to do anything.
Date User Action Args
2021-01-16 00:38:24terry.reedysetrecipients: + terry.reedy, gvanrossum, serhiy.storchaka, lys.nikolaou, xxm
2021-01-16 00:38:24terry.reedysetmessageid: <>
2021-01-16 00:38:24terry.reedylinkissue42889 messages
2021-01-16 00:38:23terry.reedycreate