Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

os.path.exists() gives wrong answer for Windows special files #64740

Closed
ncoghlan opened this issue Feb 7, 2014 · 4 comments
Closed

os.path.exists() gives wrong answer for Windows special files #64740

ncoghlan opened this issue Feb 7, 2014 · 4 comments
Labels
OS-windows stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@ncoghlan
Copy link
Contributor

ncoghlan commented Feb 7, 2014

BPO 20541
Nosy @ncoghlan, @ericsnowcurrently, @vajrasky

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2014-02-07.14:37:58.320>
created_at = <Date 2014-02-07.12:47:34.335>
labels = ['invalid', 'type-bug', 'library', 'OS-windows']
title = 'os.path.exists() gives wrong answer for Windows special files'
updated_at = <Date 2014-02-07.17:28:55.890>
user = 'https://github.com/ncoghlan'

bugs.python.org fields:

activity = <Date 2014-02-07.17:28:55.890>
actor = 'eric.snow'
assignee = 'none'
closed = True
closed_date = <Date 2014-02-07.14:37:58.320>
closer = 'ncoghlan'
components = ['Library (Lib)', 'Windows']
creation = <Date 2014-02-07.12:47:34.335>
creator = 'ncoghlan'
dependencies = []
files = []
hgrepos = []
issue_num = 20541
keywords = []
message_count = 4.0
messages = ['210460', '210461', '210475', '210500']
nosy_count = 3.0
nosy_names = ['ncoghlan', 'eric.snow', 'vajrasky']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue20541'
versions = ['Python 2.7', 'Python 3.3', 'Python 3.4', 'Python 3.5']

@ncoghlan
Copy link
Contributor Author

ncoghlan commented Feb 7, 2014

In trying to figure out why my fix for bpo-20053 wasn't working on Windows, I eventually traced it back to the fact that "os.path.exists(os.devnull)" returns False on Windows, so pip isn't picking up my config file override in ensurepip, and reads the global default config file anyway.

However, if you do "if exist NUL (echo exists) ELSE (echo missing)" in cmd, it will print "exists", so this looks like a bug in our os.path.exists implementation.

That is currently implemented in genericpath and assumes that a file exists if-and-only-if os.stat(name) doesn't throw an exception.

It turns out this assumption isn't really correct on Windows - 'NUL' and 'CON' (and presumably other special files) can be opened, but trying to do os.stat() on them throws an exception.

@ncoghlan ncoghlan added stdlib Python modules in the Lib dir OS-windows type-bug An unexpected behavior, bug, or error labels Feb 7, 2014
@vajrasky
Copy link
Mannequin

vajrasky mannequin commented Feb 7, 2014

See also: http://bugs.python.org/issue1311

@ncoghlan
Copy link
Contributor Author

ncoghlan commented Feb 7, 2014

As per bpo-1311, the exists returns True <-> stat will work equivalence is deliberate. We'll have to find a different way to resolve bpo-20053 on Windows.

Due to the special files on Windows, the only reliable cross-platform way to find out whether or not *open* will work on a given filename is to actually try it.

@ericsnowcurrently
Copy link
Member

As per bpo-1311, the exists returns True <-> stat will work equivalence
is deliberate. We'll have to find a different way to resolve bpo-20053 on
Windows.

I was going t say we should note this design/impl. detail in the
os.path.exists() docs. However, it already is! :)

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
OS-windows stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants