🌐 AI搜索 & 代理 主页
Skip to content
Closed
Prev Previous commit
fixup! bpo-1154351: add get_current_dir_name() to os module
  • Loading branch information
bradengroom committed Oct 27, 2018
commit 17386e1b4dbbbb62a1a4189a291b40fb800e5edc
8 changes: 4 additions & 4 deletions Doc/library/os.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1702,10 +1702,10 @@ features:
.. 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
opposed to :func:`getcwd()` which dereferences symlinks in the path. This
function is identical to :func:`getcwd()` on systems that do **not**
support the ``PWD`` environment variable.
consideration the users :envvar:`PWD` environment variable if it exists. This is
opposed to :func:`getcwd` which dereferences symlinks in the path. This
function is identical to :func:`getcwd` on systems that do **not**
support the :envvar:`PWD` environment variable.

.. versionadded:: 3.8

Expand Down
6 changes: 6 additions & 0 deletions Doc/whatsnew/3.8.rst
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@ by right-clicking the button. (Contributed by Tal Einat in :issue:`1529353`.)
The changes above have been backported to 3.7 maintenance releases.


os
--

Added :func:`~os.get_current_dir_name` function.
(Contributed by Braden Groom in :issue:`1154351`.)

os.path
-------

Expand Down
3 changes: 0 additions & 3 deletions Lib/os.py
Original file line number Diff line number Diff line change
Expand Up @@ -660,15 +660,12 @@ def get_current_dir_name():
the *PWD* environment variable.
"""
cwd = getcwd()

if name == 'nt':
Copy link
Member

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
Contributor Author

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 do not 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 the PWD environment variable in any way. I added that check so that we would not incorrectly change behavior from getcwd() if the user does set the PWD 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.

in git bash on windows:

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

Copy link
Contributor Author

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 whether python is executed in the context of an interactive shell that sets or doesn't set PWD.

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

return cwd
Copy link
Contributor Author

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 the PWD environment variable on windows. I believe this matches the behavior documented.


try:
pwd = environ["PWD"]
except KeyError:
return cwd

if path.samefile(cwd, pwd):
return pwd
return cwd
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Add get_current_dir_name() to the os module.
Copy link
Member

Choose a reason for hiding this comment

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

"Patch by ..."

Patch by Braden Groom