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.dirname() does not behave as expected for path without /:es #62747

Closed
JonasEriksson mannequin opened this issue Jul 24, 2013 · 2 comments
Closed

os.path.dirname() does not behave as expected for path without /:es #62747

JonasEriksson mannequin opened this issue Jul 24, 2013 · 2 comments
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@JonasEriksson
Copy link
Mannequin

JonasEriksson mannequin commented Jul 24, 2013

BPO 18547
Nosy @bitdancer

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 2013-07-24.19:17:05.269>
created_at = <Date 2013-07-24.16:31:59.432>
labels = ['type-bug', 'library']
title = 'os.path.dirname() does not behave as expected for path without /:es'
updated_at = <Date 2013-07-24.19:17:05.268>
user = 'https://bugs.python.org/JonasEriksson'

bugs.python.org fields:

activity = <Date 2013-07-24.19:17:05.268>
actor = 'r.david.murray'
assignee = 'none'
closed = True
closed_date = <Date 2013-07-24.19:17:05.269>
closer = 'r.david.murray'
components = ['Library (Lib)']
creation = <Date 2013-07-24.16:31:59.432>
creator = 'Jonas.Eriksson'
dependencies = []
files = []
hgrepos = []
issue_num = 18547
keywords = []
message_count = 2.0
messages = ['193662', '193668']
nosy_count = 2.0
nosy_names = ['r.david.murray', 'Jonas.Eriksson']
pr_nums = []
priority = 'normal'
resolution = 'rejected'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue18547'
versions = ['Python 2.7', 'Python 3.2']

@JonasEriksson
Copy link
Mannequin Author

JonasEriksson mannequin commented Jul 24, 2013

Only tested on marked python versions. Checked the code in hg (a5681f50bae2) and did not see anything related to this in the current development version.

Essentially, what I see is this:
>>> os.path.dirname("asdf")
''
>>> os.path.dirname("./asdf")
'.'
>>> 

What I expect is the same output as from the unix command dirname:
$ dirname asdf
.
$ dirname ./asdf
.
$

The change is quite straight forwards, Lib/posixpath.py needs something like if head = "": head = ".", and Lib/ntpath.py something similar.

Now, this bug is a tricky one since it alters the behavior of dirname. However, I cannot see any case where "" would be useful and have seen at least one bug because of this behaviour because the return value "" is treated like an error. So I gracefully hand over the final decision to you :)

@JonasEriksson JonasEriksson mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Jul 24, 2013
@bitdancer
Copy link
Member

Oh, it would definitely be a backward compatibility issue. Imagine code that does a dirname and branches like this:

   d = dirname(somepath)
   if d:
       <handle directory>
    else:
       <no directory case>

Or to give a more concrete example:

   path = fn if dirname(fn) else os.path.join(defaultdir, fn)

As you can see, the current behavior has significant value in Python. So I think it is behaving correctly. Python is not the shell, even though it does have many functions that provide APIs very similar to the corresponding shell functions.

See bpo-17545 for a related issue, and specifically msg189401 for another reason why the current behavior of dirname is correct within the logic of Python.

Based on the above I'm going to go ahead and close this. If other developers disagree they can reopen. If they don't, and you disagree, I would suggest that talking about it on python-ideas would be your next step.

@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
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant