prompt-toolkit / ptpython

A better Python REPL
BSD 3-Clause "New" or "Revised" License
5.2k stars 277 forks source link

Incorrect code execution #326

Open pinghedm opened 4 years ago

pinghedm commented 4 years ago

I have the following script in a file [simplified from the actual case]

# /tmp/test.py
from collections import defaultdict
a = defaultdict(lambda: defaultdict(list))
a[1][1].append(1)
print(a)

When I execute these in the ptpython REPL, everything works fine - prints out defaultdict(<function <lambda> at 0x7f8ecc06a488>, {'1': defaultdict(<class 'list'>, {1: [1]})})

When I try to execute this script from the command line as

ptpython /tmp/test.py

an error is thrown:

Traceback (most recent call last):
  File "/home/danny/ciq/env36/bin/ptpython", line 11, in <module>
    sys.exit(run())
  File "/home/danny/ciq/env36/lib/python3.6/site-packages/ptpython/entry_points/run_ptpython.py", line 58, in run
    six.exec_(code)
  File "/tmp/test.py", line 3, in <module>
    a['1'][1].append(1)                                                                                                        
  File "/tmp/test.py", line 2, in <lambda>
    a = defaultdict(lambda: defaultdict(list))                                                                                 
NameError: name 'defaultdict' is not defined

I am in a virtual environment (though leaving my virtual environment doesn't change the error). I am using ptpython version 2.0.6, installed with pip, on ubuntu 18.04, with python 3.6.3

When I run this file with python /tmp/test.py , bpython /tmp/test.py, and ipython /tmp/test.py, each of them correctly prints out my defaultdict - only ptpython throws this NameError

Let me know if any further information would be helpful!

jonathanslenders commented 4 years ago

Hi @DannyPinghero, for pointing out this bug.

Apparently the fix is easy: https://github.com/prompt-toolkit/ptpython/commit/c7a11ba484cc015d7a5ef21d78c4f748e0bd6870 (fixed on the prompt-toolkit 3.0 branch).