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: when "python -c command" does a traceback, it open the file ""
Type: behavior Stage: resolved
Components: Versions: Python 3.8, Python 3.7, Python 3.6, Python 2.7
process
Status: closed Resolution: duplicate
Dependencies: Superseder: Traceback display code can attempt to open a file named "<stdin>"
View: 1514420
Assigned To: Nosy List: AWhetter, Arfrever, Ramchandra Apte, eric.araujo, ericlammerts, r.david.murray
Priority: normal Keywords:

Created on 2013-01-15 21:40 by ericlammerts, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (8)
msg180055 - (view) Author: Eric Lammerts (ericlammerts) Date: 2013-01-15 21:40
$ echo lovely spam > "<string>"

$ python -c 'open("nonexistent","r")'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
    lovely spam
IOError: [Errno 2] No such file or directory: 'nonexistent'

I see this in python 2.7.3 and 3.2.3 from Ubuntu 12.04.
msg180063 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2013-01-15 22:30
Heh.  Nice find.  I'm not sure how practical it is to fix, though.  We don't have any actual rules about what indicates a 'non-file-stand-in' for the __file__ attribute, just a loose convention that it is an identifier in angle brackets.  If we make that a hard rule, though, and someone really has a file named '<string>'.... :)
msg180072 - (view) Author: Ramchandra Apte (Ramchandra Apte) * Date: 2013-01-16 03:20
Well, it should't open "<string>" IMHO.
msg180074 - (view) Author: Eric Lammerts (ericlammerts) Date: 2013-01-16 05:30
Does it have to be an identifier in angle brackets? An empty string makes more sense to me.
msg180083 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2013-01-16 12:13
Ideally there would be an unambiguous way to know if the object came from a file or some other source (say, __file__ is None and another special attribute gives the clue to the actual source), but that's not the way things work now, and for backward compatibility reasons I doubt that we can change it.  I'm sure there are programs that depend on at least '<string>', if not some of the other places where a similar thing is done.
msg180084 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2013-01-16 12:15
Hmm.  A backward compatible fix would be to add an attribute that indicates whether or not the __file__ attribute is supposed to be pointing to a real file.
msg180086 - (view) Author: Ramchandra Apte (Ramchandra Apte) * Date: 2013-01-16 12:29
+1

On 16 January 2013 17:43, R. David Murray <report@bugs.python.org> wrote:

>
> R. David Murray added the comment:
>
> Ideally there would be an unambiguous way to know if the object came from
> a file or some other source (say, __file__ is None and another special
> attribute gives the clue to the actual source), but that's not the way
> things work now, and for backward compatibility reasons I doubt that we can
> change it.  I'm sure there are programs that depend on at least '<string>',
> if not some of the other places where a similar thing is done.
>
> ----------
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue16974>
> _______________________________________
>
msg353957 - (view) Author: Ashley Whetter (AWhetter) * Date: 2019-10-04 17:05
If we were to add a new attribute to indicate whether a file is real or not, there would need to be a way for users to indicate whether a file is real or not to functions such as `compile()` (there's lots more!) that take a filename. Without enforcing this being set and introducing backward incompatible changes, it would need a default value. To be backwards compatible we could default to True and the existing behaviour persists.

It's also worth mentioning that there's a few places (there might be more!) where Python already assumes that a file in angle brackets is not a real file:
* https://github.com/python/cpython/blob/abd7cd856ba326bd7574135c7d034e98492ab695/Lib/bdb.py#L45
* https://github.com/python/cpython/blob/abd7cd856ba326bd7574135c7d034e98492ab695/Lib/pdb.py#L694
* https://github.com/python/cpython/blob/abd7cd856ba326bd7574135c7d034e98492ab695/Lib/pickle.py#L324

Nothing major though and easily changeable so it's definitely possible but it would be a lot of work to make sure that everything is setting the new attribute properly.

Is there a preference on what the name of this attribute should be? Maybe `__is_real_file__`. It's clear but long.
History
Date User Action Args
2022-04-11 14:57:40adminsetgithub: 61178
2021-09-27 16:00:06iritkatrielsetstatus: open -> closed
superseder: Traceback display code can attempt to open a file named "<stdin>"
resolution: duplicate
stage: needs patch -> resolved
2019-10-04 17:05:50AWhettersetnosy: + AWhetter
messages: + msg353957
2018-04-09 21:00:30ned.deilysetstage: needs patch
versions: + Python 3.6, Python 3.7, Python 3.8, - Python 3.2, Python 3.3, Python 3.4
2018-04-09 08:10:25serhiy.storchakalinkissue33247 superseder
2014-12-12 01:16:16Arfreversetnosy: + Arfrever
2013-01-16 12:29:31Ramchandra Aptesetmessages: + msg180086
2013-01-16 12:15:20r.david.murraysetmessages: + msg180084
2013-01-16 12:13:09r.david.murraysetmessages: + msg180083
2013-01-16 05:30:36ericlammertssetmessages: + msg180074
2013-01-16 04:26:13eric.araujosetnosy: + eric.araujo
2013-01-16 03:20:39Ramchandra Aptesetnosy: + Ramchandra Apte
messages: + msg180072
2013-01-15 22:30:01r.david.murraysetnosy: + r.david.murray

messages: + msg180063
versions: + Python 3.3, Python 3.4
2013-01-15 21:40:06ericlammertscreate