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.isdir returns true for dots #75897

Closed
morha13 mannequin opened this issue Oct 6, 2017 · 2 comments
Closed

os.path.isdir returns true for dots #75897

morha13 mannequin opened this issue Oct 6, 2017 · 2 comments
Labels
OS-windows type-bug An unexpected behavior, bug, or error

Comments

@morha13
Copy link
Mannequin

morha13 mannequin commented Oct 6, 2017

BPO 31716
Nosy @pfmoore, @tjguk, @zware, @eryksun, @zooba

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 2017-10-06.15:42:58.912>
created_at = <Date 2017-10-06.15:26:29.614>
labels = ['type-bug', 'invalid', 'OS-windows']
title = 'os.path.isdir returns true for dots'
updated_at = <Date 2017-10-06.15:55:11.031>
user = 'https://bugs.python.org/morha13'

bugs.python.org fields:

activity = <Date 2017-10-06.15:55:11.031>
actor = 'eryksun'
assignee = 'none'
closed = True
closed_date = <Date 2017-10-06.15:42:58.912>
closer = 'morha13'
components = ['Windows']
creation = <Date 2017-10-06.15:26:29.614>
creator = 'morha13'
dependencies = []
files = []
hgrepos = []
issue_num = 31716
keywords = []
message_count = 2.0
messages = ['303827', '303832']
nosy_count = 6.0
nosy_names = ['paul.moore', 'tim.golden', 'zach.ware', 'eryksun', 'steve.dower', 'morha13']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue31716'
versions = ['Python 2.7']

@morha13
Copy link
Mannequin Author

morha13 mannequin commented Oct 6, 2017

I uploaded this as a question on Stack Overflow and I suspect it might be a bug. Here is the link for the Stack Overflow question: https://stackoverflow.com/questions/46608731/python-os-path-isdir-returns-true-for-dots/46608842#46608842

The problem itself (copied from what I uploaded on Stack Overflow):

I'm programming my own shell in python. Right now I'm trying to implement the cd command to my shell.

The function that performs this command has several variables:

self.current_dir = "C:\\" - The default value, it changes depends on the user's input using the cd command

dir = "..." - The requested directory that the user types. "..." is an example for an input that causes the problem.

Here is my code:

    def command_cd(self, dir):
        if os.path.isdir(self.shell.current_dir + dir):
            self.shell.current_dir = self.shell.current_dir + dir + "\\"

The problem is that for some strange reason, os.path.isdir(self.shell.current_dir + dir) returns True when the user types dots (Just like the example inputs for the variables which I gave above).

The problem occurs even if you change the amount of dots (even above 5 dots) and I really have no idea what causes it.

There's obviously no folder named ... or anything like it.

**If my problem isn't clear enough please comment and I'll edit it**

@morha13 morha13 mannequin added OS-windows type-bug An unexpected behavior, bug, or error labels Oct 6, 2017
@morha13 morha13 mannequin closed this as completed Oct 6, 2017
@morha13 morha13 mannequin added the invalid label Oct 6, 2017
@eryksun
Copy link
Contributor

eryksun commented Oct 6, 2017

This is standard Windows API behavior for the final path component. A single dot component means the current directory. Two dots means the parent directory. More than two dots and/or trailing spaces, gets reduced to a single dot, meaning the current directory. For example:

    >>> os.path.abspath('.')
    'C:\\Temp'
    >>> os.path.abspath('..')
    'C:\\'
    >>> os.path.abspath('...')
    'C:\\Temp'
    >>> os.path.abspath('... ... ...')
    'C:\\Temp'

Specifically, os.path.isdir is implemented as nt._isdir, which calls WinAPI GetFileAttributes to check for FILE_ATTRIBUTE_DIRECTORY, which in turn calls the NT system function NtQueryAttributesFile.

GetFileAttributes has to translate the DOS path to an NT kernel path. In the kernel, none of this "." business exists. The kernel doesn't even have a concept of a working directory. Depending on your Windows version, it might call the runtime library function RtlDosPathNameToNtPathName_U_WithStatus to convert the path to a native OBJECT_ATTRIBUTES record. The first step is to normalize the path via RtlGetFullPathName_Ustr, which is what the Windows API GetFullPathName function calls like in the above abspath() examples.

@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 type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant