This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Traceback display code can attempt to open a file named ""
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.11
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: ajaksu2, ctheune, iritkatriel, lukasz.langa, nmm, pitrou, zbysz
Priority: low Keywords: patch

Created on 2006-06-29 16:13 by nmm, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 28143 merged iritkatriel, 2021-09-03 16:16
Messages (11)
msg60931 - (view) Author: Nick Maclaren (nmm) Date: 2006-06-29 16:13
Now, exactly WHY is it looking for a file called
<stdin>? :-)

This bug has been present since at least 2.3.3 - I
can't be bothered to
check back further.  Not surprisingly, it causes
misbehaviour if there
is a file called <stdin> in any of the places searched,
but it doesn't
strike me as the world's most catastrophic bug.



strace -e open python
Python 2.5b1 (trunk:47059, Jun 29 2006, 14:26:46)
[GCC 4.1.0 (SUSE Linux)] on linux2
>>> import dismal
open("dismal.so", O_RDONLY)             = -1 ENOENT (No
such file or directory)open("dismalmodule.so",
O_RDONLY)       = -1 ENOENT (No such file or directory)
open("dismal.py", O_RDONLY)             = -1 ENOENT (No
such file or directory)
open("dismal.pyc", O_RDONLY)            = -1 ENOENT (No
such file or directory)
open("/home/nmm/Python_2.5/lib/python2.5/dismal.so",
O_RDONLY) = -1 ENOENT (No such file or directory)
open("/home/nmm/Python_2.5/lib/python2.5/<stdin>",
O_RDONLY) = -1 ENOENT (No such file or directory)
open("/home/nmm/Python_2.5/lib/python2.5/plat-linux2/<stdin>",
O_RDONLY) = -1 ENOENT (No such file or directory)
open("/home/nmm/Python_2.5/lib/python2.5/lib-tk/<stdin>",
O_RDONLY) = -1 ENOENT (No such file or directory)
open("/home/nmm/Python_2.5/lib/python2.5/lib-dynload/<stdin>",
O_RDONLY) = -1 ENOENT (No such file or directory)
open("/home/nmm/Python_2.5/lib/python2.5/site-packages/<stdin>",
O_RDONLY) = -1 ENOENT (No such file or directory)
  File "<stdin>", line 1, in <module>
ImportError: No module named dismal
>>>
msg60932 - (view) Author: Ronald Oussoren (ronaldoussoren) * (Python committer) Date: 2006-07-13 12:04
Logged In: YES 
user_id=580910

It's probably looking for a file named <stdin> because the co_filename 
attribute for code that's executed from the interactive prompt has that value:

>>> import sys
>>> f = sys._getframe(0)
>>> f.f_code.co_filename
'<stdin>'

I agree that looking for that file is rather pointless and a bug.
msg84515 - (view) Author: Daniel Diniz (ajaksu2) * (Python triager) Date: 2009-03-30 05:56
Confirmed in py3k and trunk. It's also possible to create a file named
"<stdin>", I seem to recall discussion on this.
msg86735 - (view) Author: Zbyszek Jędrzejewski-Szmek (zbysz) * Date: 2009-04-28 13:10
The bug is certainly not catastrophic, but creates
a slight security risk:

ln -s /etc/shadow '<stdin>'
some-suid-program -with-error

or whatever.
msg216697 - (view) Author: Christian Theune (ctheune) * Date: 2014-04-17 15:29
I don't think the security risk exists due to this bug. As Python is searching for various places anyway, an attacker could just symlink one of those places anyway instead of '<stdin>'.
msg216742 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2014-04-17 21:05
The problem is not in the import, but when displaying the traceback of the exception. In other words, if you catch the exception, no attempt to open "<stdin>" happens:

$ strace -e open ./python
[...]
Python 3.5.0a0 (default:3417a95df7e2, Apr 16 2014, 17:57:12) 
[GCC 4.8.1] on linux
[...]
>>> 
>>> try: import dismal
... except ImportError: pass
... 
>>>
msg216743 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2014-04-17 21:06
Also, by construction it will only happen if the import happens under the interpreter prompt (hence the "<stdin>" filename).

I honestly don't think this deserves introducing some complication, only to avoid a couple filesystem accesses.
msg380347 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2020-11-04 17:55
I was able to reproduce it on 3.8, but I'm confused about where the open is happening because linecache.updatecache tries to avoid this:

if not filename or (filename.startswith('<') and filename.endswith('>')):
        return []
msg383094 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2020-12-15 20:03
Ok, I'm unconfused now - this is the C version of the traceback, in _Py_DisplaySourceLine, not the traceback.py one that uses linecache.

It wouldn't be hard to add the check for "<>" in the filename there. Is there a reason not to do it?
msg402241 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-09-20 15:10
New changeset f71300cb0442f16ee9abc938e12537aec1eb5979 by Irit Katriel in branch 'main':
bpo-1514420: Do not attempt to open files with names in <>s when formatting an exception (GH-28143)
https://github.com/python/cpython/commit/f71300cb0442f16ee9abc938e12537aec1eb5979
msg402242 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-09-20 15:11
Fixed for Python 3.11. Thanks! ✨ 🍰 ✨
History
Date User Action Args
2022-04-11 14:56:18adminsetgithub: 43572
2021-10-24 21:02:13iritkatriellinkissue45596 superseder
2021-09-27 16:00:06iritkatriellinkissue16974 superseder
2021-09-20 15:11:21lukasz.langasetstatus: open -> closed
resolution: fixed
messages: + msg402242

stage: patch review -> resolved
2021-09-20 15:10:37lukasz.langasetnosy: + lukasz.langa
messages: + msg402241
2021-09-03 16:18:35iritkatrielsetversions: + Python 3.11, - Python 3.8, Python 3.9, Python 3.10
2021-09-03 16:16:31iritkatrielsetkeywords: + patch
stage: patch review
pull_requests: + pull_request26582
2020-12-15 20:03:26iritkatrielsetmessages: + msg383094
2020-11-15 23:41:51iritkatriellinkissue14531 superseder
2020-11-04 17:55:49iritkatrielsetnosy: + iritkatriel

messages: + msg380347
versions: + Python 3.8, Python 3.9, Python 3.10, - Python 3.5
2014-04-17 21:06:38pitrousetmessages: + msg216743
2014-04-17 21:05:32pitrousetpriority: normal -> low


title: Missing module code does spurious file search -> Traceback display code can attempt to open a file named "<stdin>"
nosy: + pitrou
versions: + Python 3.5, - Python 3.1, Python 2.7, Python 3.2
messages: + msg216742
stage: test needed -> (no value)
2014-04-17 15:29:18ctheunesetnosy: + ctheune
messages: + msg216697
2010-08-22 11:06:09BreamoreBoysetversions: + Python 3.1, Python 2.7, Python 3.2, - Python 2.6, Python 3.0
2009-04-28 13:29:08ronaldoussorensetnosy: - ronaldoussoren
2009-04-28 13:10:45zbyszsetnosy: + zbysz
messages: + msg86735
2009-03-30 05:56:57ajaksu2setversions: + Python 2.6, Python 3.0, - Python 2.5
nosy: + ajaksu2

messages: + msg84515

type: behavior
stage: test needed
2006-06-29 16:13:21nmmcreate