Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

bpo-1154351: Add get_current_dir_name() to os module#10117

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed
bradengroom wants to merge9 commits intopython:masterfrombradengroom:1154351

Conversation

@bradengroom
Copy link
Contributor

@bradengroombradengroom commentedOct 26, 2018
edited by bedevere-bot
Loading

Co-authored-by: Marc Adam Andersonmarc@marcadam.com .

https://bugs.python.org/issue1154351

Co-authored-by: Marc Adam Anderson <marc@marcadam.com> .
@bradengroombradengroom changed the titleAdd get_current_dir_name() to os modulebpo-1154351: Add get_current_dir_name() to os moduleOct 26, 2018
function is identical to:func:`getcwd()` on systems that do **not**
support the ``PWD`` environment variable.

..availability::Unix.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

This function is available on all platforms.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Actually, while this is undoubtably an ancient link (https://www.gnu.org/software/gnulib/manual/html_node/get_005fcurrent_005fdir_005fname.html ) I do not believe this function is available in all POSIX environments. It may be common in GNU runtime environments.

FYI: I just checked AIX and found the routine name "exists" in AIX <unistd.h>, but there is no documentation. So, it seems it may be visible on AIX, even if they have never taken the time to a is adding dd it to the getcwd() manual page.

Confusion#1 in adding this

Lib/os.py Outdated
exceptKeyError:
returncwd

cwd_stat,pwd_stat=map(stat, [cwd,pwd])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Would be clearer just to write two stat calls.

Lib/os.py Outdated

cwd_stat,pwd_stat=map(stat, [cwd,pwd])

if (cwd_stat.st_dev==pwd_stat.st_devand

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Maybe use samefile()?

# os.getcwd() always returns the dereferenced path
withsupport.temp_cwd(self.tmp_dir):
os.chdir(self.tmp_dir)
self.assertEqual(self.tmp_dir,os.getcwd())

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Swap arguments. The first argument is an actual value, the second argument is an expected value.

self.assertEqual(self.tmp_dir,os.getcwd())
withmock.patch.dict('os.environ', {'PWD':self.tmp_lnk}):
self.assertEqual(self.tmp_dir,os.getcwd())
os.unlink(self.tmp_lnk)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

The link will be leaked in the case of failure. It is better to useaddCleanup(). Add the following line just before creating a link.

self.addCleanup(support.unlink,self.tmp_lnk)

Lib/os.py Outdated

cwd_stat,pwd_stat=map(stat, [cwd,pwd])

if (cwd_stat.st_dev==pwd_stat.st_devand
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

So, confusion#2 -, assuming that get_current_dir_name() is supported by the platform why is that not just being called rather than rewriting it? I admit my confusion comes from the lack of platform documentation, but why so hard? Wouldn't calling the "platform" implementation be more efficient than writing this in "pure" python.

More simply, why not use the platform implementation, perhaps adding it to posixmodule.c rather than here?

os.symlink(self.tmp_dir,self.tmp_lnk,True)
os.chdir(self.tmp_lnk)
ifos.name=='nt':
# windows doesn't dereference the path
Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I'm not sure if this is a bug or the expected behavior. I haven't used windows in quite some time, but this is the behavior I'm seeing in CI.

cwd=getcwd()

ifname=='nt':
returncwd
Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I added this so that we don't look at thePWD environment variable on windows. I believe this matches the behavior documented.

@bradengroom
Copy link
ContributorAuthor

@serhiy-storchaka I've addressed your feedback and the tests are passing now.

Copy link
Member

@serhiy-storchakaserhiy-storchaka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Please add also an entry in What's New.

..function::get_current_dir_name()

Return a string representing the current working directory taking into
consideration the users ``PWD`` environment variable if it exists. This is

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Use:envvar:?


Return a string representing the current working directory taking into
consideration the users ``PWD`` environment variable if it exists. This is
opposed to:func:`getcwd()` which dereferences symlinks in the path. This

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Remove(). It is added automatically.

Lib/os.py Outdated
the *PWD* environment variable.
"""
cwd=getcwd()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I think empty lines in this functions are redundant. Empty lines inside functions are used for separating groups of statements in large functions, but this function is enough small and simple. And will be smaller after removing empty lines.

"""
cwd=getcwd()

ifname=='nt':

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Why this is needed?

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I think you were performing review as I was writing a comment :)
#10117 (comment)

This function is documented with:

This function is identical to :func:getcwd on systems that donot support the :envvar:PWD environment variable.

Please correct me if I'm wrong since I'm less familiar with Windows, but I don't believe it uses thePWD environment variable in any way. I added that check so that we would not incorrectly change behavior fromgetcwd() if the user does set thePWD on Windows for some reason.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

ingit bash on windows:

Anthony@AnthonysDesktop MINGW64 ~$ python -c 'import os; print(os.getenv("PWD"))'C:/Users/Anthony

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

@asottile
Thanks. Looks like my memory was incorrect. I'll fix this up.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I'm actually not sure this function is useful/a good idea -- it's entirely dependent on whetherpython is executed in the context of an interactive shell that sets or doesn't setPWD.

I've commented on the bpo issue indicating that as well.

@@ -0,0 +1 @@
Add get_current_dir_name() to the os module.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

"Patch by ..."

@bradengroom
Copy link
ContributorAuthor

@serhiy-storchaka Thanks for the quick feedback. I addressed your comments here:
17386e1
#10117 (comment)

Copy link
Member

@vstinnervstinner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Right now I have no opinion on the feature itself, but if we decide to add it, it should be added to the shutil module instead. os is a thin wrapper to C function, whereas shutil are functions based on the os module but adding "Python logic".

Copy link
Member

@vstinnervstinner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I don't understand the purpose of this function. I suggest to reject this PR and closehttps://bugs.python.org/issue1154351

If someone needs this function, it can easily be reimplemented and copy/paste from the issue or this PR.

If someone really wants this feature to be added to Python stdlib, we need realisticuse cases. Not just "it would be nice to have this function".

@bedevere-bot
Copy link

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phraseI have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@morell5
Copy link

morell5 commentedSep 9, 2019
edited
Loading

@vstinner , may you make a key decision about this PR: close or provide requirements for merging the PR? The development of the task has stopped because there are no instructions from the core developer. As for me, I don't understand further actions on this task.

Also I don't understand what "requested changes" bedeavere-bot was talking about? Where I can find these "requested changes"?

@vstinner
Copy link
Member

The os module is thin wrappers to functions of the libc or even system calls. We don't implement heuristic using 2 variants of the same feature: there shutil module is there for that. Compare os.get_terminal_size() to shutil.get_terminal_size() for example.

I close the PR to add os.get_current_dir_name(): IMHO it's more a feature for the shutil module.

morell5 reacted with thumbs up emoji

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@serhiy-storchakaserhiy-storchakaserhiy-storchaka left review comments

@vstinnervstinnervstinner requested changes

+2 more reviewers

@asottileasottileasottile left review comments

@aixtoolsaixtoolsaixtools left review comments

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

8 participants

@bradengroom@bedevere-bot@morell5@vstinner@asottile@serhiy-storchaka@aixtools@the-knights-who-say-ni

[8]ページ先頭

©2009-2025 Movatter.jp