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) *  |
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) *  |
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) *  |
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. |
|
|