Closed nickodell closed 4 years ago
Similar issue with the command ls | python3 pyp.py '[a for a in lines if a != "README.md"]'
It evaluates the if a != "README.md"
part before assigning a, so it mistakenly imports a.
Thanks for finding these issues and for the ready-to-merge PR! I really appreciate it.
I think this change can cause issues with try except statements, e.g., python3.8 pyp.py --explain 'try: y = 1' 'except Exception: print(y)'
, but it's on me for not having a test for that / loops being totally broken is clearly a more pressing problem.
Try-except is also now fixed on master and I've added more tests. Just cut a new release, so your fix is in if you update. Thanks again!
Thanks!
Bug
I ran this command:
I got the following error:
Using
--explain
gives this:Expected behavior
pyp prints each line in a loop, as if I'd written
pyp 'x'
Fix
This function controls what order generic AST nodes are visited:
Change the function so that the body of a loop is visited after the variables created by a for loop are defined.
I wrote a test for this issue as well.
PS: This is a great project. I come from an Awk background, and this is a really nice tool.