Skip to content
Closed
Prev Previous commit
Next Next commit
fixup! bpo-1154351: add get_current_dir_name() to os module
  • Loading branch information
bradengroom committed Oct 27, 2018
commit 4bfb33ce73180cc5da912207ae89a3bcfb819098
2 changes: 0 additions & 2 deletions Doc/library/os.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1707,8 +1707,6 @@ features:
function is identical to :func:`getcwd()` on systems that do **not**
support the ``PWD`` environment variable.

.. availability:: Unix.

.. versionadded:: 3.8


Expand Down
5 changes: 1 addition & 4 deletions Lib/os.py
Original file line number Diff line number Diff line change
Expand Up @@ -666,10 +666,7 @@ def get_current_dir_name():
except KeyError:
return cwd

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

if (cwd_stat.st_dev == pwd_stat.st_dev and
cwd_stat.st_ino == pwd_stat.st_ino):
if path.samefile(cwd, pwd):
return pwd
return cwd

Expand Down
16 changes: 8 additions & 8 deletions Lib/test/test_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -1264,14 +1264,14 @@ 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(self.tmp_dir, os.getcwd())
self.assertEqual(os.getcwd(), self.tmp_dir)
os.symlink(self.tmp_dir, self.tmp_lnk, True)
os.chdir(self.tmp_lnk)
self.assertEqual(self.tmp_dir, os.getcwd())
self.assertEqual(os.getcwd(), self.tmp_dir)
with mock.patch.dict('os.environ', {'PWD': self.tmp_dir}):
self.assertEqual(self.tmp_dir, os.getcwd())
self.assertEqual(os.getcwd(), self.tmp_dir)
with mock.patch.dict('os.environ', {'PWD': self.tmp_lnk}):
self.assertEqual(self.tmp_dir, os.getcwd())
self.assertEqual(os.getcwd(), self.tmp_dir)
os.unlink(self.tmp_lnk)
Copy link
Copy Markdown
Member

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 use addCleanup(). Add the following line just before creating a link.

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


def test_get_current_dir_name(self):
Expand All @@ -1280,14 +1280,14 @@ def test_get_current_dir_name(self):
# whether the path contains symlinks.
with support.temp_cwd(self.tmp_dir):
with mock.patch.dict('os.environ', {'PWD': self.tmp_dir}):
self.assertEqual(self.tmp_dir, os.get_current_dir_name())
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(self.tmp_lnk, os.get_current_dir_name())
self.assertEqual(os.get_current_dir_name(), self.tmp_lnk)
else:
self.assertEqual(self.tmp_dir, os.get_current_dir_name())
os.unlink(self.tmp_lnk)
self.assertEqual(os.get_current_dir_name(), self.tmp_dir)


class RemoveDirsTests(unittest.TestCase):
Expand Down