On 9/3/2017 11:17 AM, eryk sun wrote:
On Sun, Sep 3, 2017 at 7:56 AM, <g.morkve...@gmail.com> wrote:
What means line below:
File "<stdin>", line 1
I don't have any <stdin> file.
Indeed, on Windows you cannot create a file named "<stdin>". Python
uses this fake name for the code object it compiles when reading from
stdin (i.e. the file stream opened for console input).
It's not exactly smart about this, either, since whenever an exception
is raised in the REPL it will try to open this fake file multiple
times, including trying every entry in sys.path. For example, in a
typical Python 3.6 all-users installation, it will try opening the
following file paths:
<stdin>
<stdin>
C:\Program Files\Python36\python36.zip\<stdin>
C:\Program Files\Python36\python36.zip\<stdin>
C:\Program Files\Python36\DLLs\<stdin>
C:\Program Files\Python36\lib\<stdin>
C:\Program Files\Python36\<stdin>
C:\Program Files\Python36\lib\site-packages\<stdin>
...
Of course, all of these attempts to open "<stdin>" necessarily fail on
Windows.
The result, after doing all the above, is tracebacks like
>>> def f():
... return 1/0
...
>>> f()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in f
ZeroDivisionError: division by zero
with source lines missing. Note that 'line 1' is misleading as the
'f()' call is on line 4. It is the first line of the 2nd statement.
In IDLE, trackbacks *do* include source lines.
>>> def f():
return 1/0
>>> f()
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
f()
File "<pyshell#1>", line 2, in f
return 1/0
ZeroDivisionError: division by zero
Each statement is numbered, and treated as a file, so that line
numbering starts at 1 for each statement.
The secret to doing this is that traceback printing looks in
linecache.cache *before* trying to open a file, as described above.
When the file is read, it is added to the cache. IDLE stuffs the lines
for each statement into the cache and replaces linecache.checkcache with
a wrapper that prevents them from being deleted.
On Unix, however, this can actually succeed, which is kind of
funny:
>>> open('<stdin>', 'w').write('What the !@#$%^&*?')
18
>>> dit
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
What the !@#$%^&*?
NameError: name 'dit' is not defined
Won't happen with <pyshell#N>
--
Terry Jan Reedy
--
https://mail.python.org/mailman/listinfo/python-list