[issue38937] NameError in list comprehension within .pth file

2019-11-29 Thread Chris Billington
Chris Billington added the comment: Sorry for the spamming, realised I misunderstood further. The original behaviour isn't because the exec'd code can't create new local variables - it can - it's because of the documented behaviour of exec when it gets different dicts for globals and locals:

[issue38937] NameError in list comprehension within .pth file

2019-11-28 Thread Roundup Robot
Change by Roundup Robot : -- keywords: +patch pull_requests: +16895 stage: -> patch review pull_request: https://github.com/python/cpython/pull/17414 ___ Python tracker ___ __

[issue38937] NameError in list comprehension within .pth file

2019-11-28 Thread Chris Billington
Chris Billington added the comment: I see. site.py calls exec() from within a function, and therefore the code is executed in the context of that function's locals and the site module globals. This means the code in .pth files can access (but not add new) local names from the site.addpackage

[issue38937] NameError in list comprehension within .pth file

2019-11-28 Thread Chris Billington
New submission from Chris Billington : The following one-liner works fine in a regular Python interpreter: $ python -c 'import sys; x = 5; [print(x + i) for i in range(5)]' 5 6 7 8 9 But in a .pth file, it raises a NameError: $ echo 'import sys; x = 5; [print(x + i) for i in range(5)]' | sudo