Issue 16974: when "python -c command" does a traceback, it open the file "" (original) (raw)

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 > "" $ python -c 'open("nonexistent","r")' Traceback (most recent call last): File "", line 1, in 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 ''.... :)
msg180072 - (view) Author: Ramchandra Apte (Ramchandra Apte) * Date: 2013-01-16 03:20
Well, it should't open "" 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 '', 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 '', > 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:40 admin set github: 61178
2021-09-27 16:00:06 iritkatriel set status: open -> closedsuperseder: Traceback display code can attempt to open a file named ""resolution: duplicatestage: needs patch -> resolved
2019-10-04 17:05:50 AWhetter set nosy: + AWhettermessages: +
2018-04-09 21:00:30 ned.deily set stage: needs patchversions: + Python 3.6, Python 3.7, Python 3.8, - Python 3.2, Python 3.3, Python 3.4
2018-04-09 08:10:25 serhiy.storchaka link issue33247 superseder
2014-12-12 01:16:16 Arfrever set nosy: + Arfrever
2013-01-16 12:29:31 Ramchandra Apte set messages: +
2013-01-16 12:15:20 r.david.murray set messages: +
2013-01-16 12:13:09 r.david.murray set messages: +
2013-01-16 05:30:36 ericlammerts set messages: +
2013-01-16 04:26:13 eric.araujo set nosy: + eric.araujo
2013-01-16 03:20:39 Ramchandra Apte set nosy: + Ramchandra Aptemessages: +
2013-01-15 22:30:01 r.david.murray set nosy: + r.david.murraymessages: + versions: + Python 3.3, Python 3.4
2013-01-15 21:40:06 ericlammerts create