-
-
Notifications
You must be signed in to change notification settings - Fork 31.6k
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
1daae93
ee4c4f6
f836691
89e1984
4bfb33c
963659a
cc1735f
d49fdf3
17386e1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,7 +32,7 @@ | |
__all__ = ["altsep", "curdir", "pardir", "sep", "pathsep", "linesep", | ||
"defpath", "name", "path", "devnull", "SEEK_SET", "SEEK_CUR", | ||
"SEEK_END", "fsencode", "fsdecode", "get_exec_path", "fdopen", | ||
"popen", "extsep"] | ||
"popen", "extsep", "get_current_dir_name"] | ||
|
||
def _exists(name): | ||
return name in globals() | ||
|
@@ -651,6 +651,26 @@ def get_exec_path(env=None): | |
return path_list.split(pathsep) | ||
|
||
|
||
def 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 getcwd() which dereferences symlinks in the path. This | ||
function is identical to getcwd() on systems that do *not* support | ||
the *PWD* environment variable. | ||
""" | ||
cwd = getcwd() | ||
if name == 'nt': | ||
return cwd | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added this so that we don't look at the |
||
try: | ||
pwd = environ["PWD"] | ||
except KeyError: | ||
return cwd | ||
if path.samefile(cwd, pwd): | ||
return pwd | ||
return cwd | ||
|
||
|
||
# Change environ to automatically call putenv(), unsetenv if they exist. | ||
from _collections_abc import MutableMapping | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ | |
import uuid | ||
import warnings | ||
from test import support | ||
from unittest import mock | ||
|
||
try: | ||
import resource | ||
|
@@ -1252,6 +1253,48 @@ def tearDownClass(cls): | |
os.rmdir(support.TESTFN) | ||
|
||
|
||
class CurrentDirTests(unittest.TestCase): | ||
|
||
def setUp(self): | ||
base = os.path.abspath(support.TESTFN) | ||
self.tmp_dir = base + '_dir' | ||
self.tmp_lnk = base + '_lnk' | ||
|
||
def test_getcwd(self): | ||
# os.getcwd() always returns the dereferenced path | ||
with support.temp_cwd(self.tmp_dir): | ||
os.chdir(self.tmp_dir) | ||
self.assertEqual(os.getcwd(), self.tmp_dir) | ||
self.addCleanup(support.unlink, self.tmp_lnk) | ||
os.symlink(self.tmp_dir, self.tmp_lnk, True) | ||
os.chdir(self.tmp_lnk) | ||
if os.name == 'nt': | ||
# windows doesn't dereference the path | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
expected_cwd = self.tmp_lnk | ||
else: | ||
expected_cwd = self.tmp_dir | ||
self.assertEqual(os.getcwd(), expected_cwd) | ||
with mock.patch.dict('os.environ', {'PWD': self.tmp_dir}): | ||
self.assertEqual(os.getcwd(), expected_cwd) | ||
with mock.patch.dict('os.environ', {'PWD': self.tmp_lnk}): | ||
self.assertEqual(os.getcwd(), expected_cwd) | ||
|
||
def test_get_current_dir_name(self): | ||
# os.get_current_dir_name() returns the direct path--mirroring | ||
# the PWD environment variable if it exists regardless of | ||
# whether the path contains symlinks. | ||
with support.temp_cwd(self.tmp_dir): | ||
with mock.patch.dict('os.environ', {'PWD': self.tmp_dir}): | ||
self.assertEqual(os.get_current_dir_name(), self.tmp_dir) | ||
self.addCleanup(support.unlink, self.tmp_lnk) | ||
os.symlink(self.tmp_dir, self.tmp_lnk, True) | ||
with mock.patch.dict('os.environ', {'PWD': self.tmp_lnk}): | ||
if os.name == 'posix': | ||
self.assertEqual(os.get_current_dir_name(), self.tmp_lnk) | ||
else: | ||
self.assertEqual(os.get_current_dir_name(), self.tmp_dir) | ||
|
||
|
||
class RemoveDirsTests(unittest.TestCase): | ||
def setUp(self): | ||
os.makedirs(support.TESTFN) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Add get_current_dir_name() to the os module. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "Patch by ..." |
||
Patch by Braden Groom |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why this is needed?
There was a problem hiding this comment.
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:
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 fromgetcwd()
if the user does set thePWD
on Windows for some reason.There was a problem hiding this comment.
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:There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 setPWD
.I've commented on the bpo issue indicating that as well.