From 1daae937be599e43e6b5af0104904c92be249cdd Mon Sep 17 00:00:00 2001 From: Braden Groom Date: Thu, 25 Oct 2018 19:37:26 -0700 Subject: [PATCH 1/9] bpo-1154351: add get_current_dir_name() to os module Co-authored-by: Marc Adam Anderson . --- Doc/library/os.rst | 14 ++++++++++++++ Lib/os.py | 25 ++++++++++++++++++++++++- Lib/test/test_os.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 1 deletion(-) diff --git a/Doc/library/os.rst b/Doc/library/os.rst index 75d473c00d5c67..783bbf531d4b45 100644 --- a/Doc/library/os.rst +++ b/Doc/library/os.rst @@ -155,6 +155,7 @@ process and user. .. function:: chdir(path) fchdir(fd) getcwd() + get_current_dir_name() :noindex: These functions are described in :ref:`os-file-dir`. @@ -1698,6 +1699,19 @@ features: Return a bytestring representing the current working directory. +.. 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. + + .. availability:: Unix. + + .. versionadded:: 3.8 + + .. function:: lchflags(path, flags) Set the flags of *path* to the numeric *flags*, like :func:`chflags`, but do diff --git a/Lib/os.py b/Lib/os.py index 4b31e4dcc8add6..a0fd30525ff545 100644 --- a/Lib/os.py +++ b/Lib/os.py @@ -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,29 @@ 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() + + try: + pwd = environ["PWD"] + 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): + return pwd + return cwd + + # Change environ to automatically call putenv(), unsetenv if they exist. from _collections_abc import MutableMapping diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py index 3f6e48f0c8e69c..5602b5c93483fa 100644 --- a/Lib/test/test_os.py +++ b/Lib/test/test_os.py @@ -1252,6 +1252,48 @@ def tearDownClass(cls): os.rmdir(support.TESTFN) +class CurrentDirTests(unittest.TestCase): + + def setUp(self): + self.pwd = os.environ['PWD'] + 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(self.tmp_dir, os.getcwd()) + os.symlink(self.tmp_dir, self.tmp_lnk, True) + os.chdir(self.tmp_lnk) + self.assertEqual(self.tmp_dir, os.getcwd()) + os.environ['PWD'] = self.tmp_dir + self.assertEqual(self.tmp_dir, os.getcwd()) + os.environ['PWD'] = self.tmp_lnk + self.assertEqual(self.tmp_dir, os.getcwd()) + os.unlink(self.tmp_lnk) + + 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): + os.environ['PWD'] = self.tmp_dir + self.assertEqual(self.tmp_dir, os.get_current_dir_name()) + os.symlink(self.tmp_dir, self.tmp_lnk, True) + if os.name == 'posix': + os.environ['PWD'] = self.tmp_lnk + self.assertEqual(self.tmp_lnk, os.get_current_dir_name()) + else: + os.environ['PWD'] = self.tmp_lnk + self.assertEqual(self.tmp_dir, os.get_current_dir_name()) + os.unlink(self.tmp_lnk) + + def tearDown(self): + os.environ['PWD'] = self.pwd + + class RemoveDirsTests(unittest.TestCase): def setUp(self): os.makedirs(support.TESTFN) From ee4c4f6dee2bd329347a7fff90a73330327a50a1 Mon Sep 17 00:00:00 2001 From: Braden Groom Date: Thu, 25 Oct 2018 19:46:21 -0700 Subject: [PATCH 2/9] fixup! bpo-1154351: add get_current_dir_name() to os module --- .../next/Library/2018-10-25-19-44-42.bpo-1154351.KhXLos.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2018-10-25-19-44-42.bpo-1154351.KhXLos.rst diff --git a/Misc/NEWS.d/next/Library/2018-10-25-19-44-42.bpo-1154351.KhXLos.rst b/Misc/NEWS.d/next/Library/2018-10-25-19-44-42.bpo-1154351.KhXLos.rst new file mode 100644 index 00000000000000..888e6829f06c08 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2018-10-25-19-44-42.bpo-1154351.KhXLos.rst @@ -0,0 +1 @@ +Add get_current_dir_name() to the os module. From f836691283624361ee12722c8ba469f7cfe86cd1 Mon Sep 17 00:00:00 2001 From: Braden Groom Date: Thu, 25 Oct 2018 20:08:12 -0700 Subject: [PATCH 3/9] fixup! bpo-1154351: add get_current_dir_name() to os module --- Lib/test/test_os.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py index 5602b5c93483fa..0b3d7e095fac24 100644 --- a/Lib/test/test_os.py +++ b/Lib/test/test_os.py @@ -1255,7 +1255,7 @@ def tearDownClass(cls): class CurrentDirTests(unittest.TestCase): def setUp(self): - self.pwd = os.environ['PWD'] + self.pwd = os.environ.get('PWD') base = os.path.abspath(support.TESTFN) self.tmp_dir = base + '_dir' self.tmp_lnk = base + '_lnk' @@ -1291,7 +1291,8 @@ def test_get_current_dir_name(self): os.unlink(self.tmp_lnk) def tearDown(self): - os.environ['PWD'] = self.pwd + if self.pwd is not None: + os.environ['PWD'] = self.pwd class RemoveDirsTests(unittest.TestCase): From 89e1984c064251740f5871b34ed1b9b3a819fe5a Mon Sep 17 00:00:00 2001 From: Braden Groom Date: Thu, 25 Oct 2018 20:49:30 -0700 Subject: [PATCH 4/9] fixup! bpo-1154351: add get_current_dir_name() to os module --- Lib/test/test_os.py | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py index 0b3d7e095fac24..722cc4ab935253 100644 --- a/Lib/test/test_os.py +++ b/Lib/test/test_os.py @@ -28,6 +28,7 @@ import uuid import warnings from test import support +from unittest import mock try: import resource @@ -1255,7 +1256,6 @@ def tearDownClass(cls): class CurrentDirTests(unittest.TestCase): def setUp(self): - self.pwd = os.environ.get('PWD') base = os.path.abspath(support.TESTFN) self.tmp_dir = base + '_dir' self.tmp_lnk = base + '_lnk' @@ -1268,10 +1268,10 @@ def test_getcwd(self): os.symlink(self.tmp_dir, self.tmp_lnk, True) os.chdir(self.tmp_lnk) self.assertEqual(self.tmp_dir, os.getcwd()) - os.environ['PWD'] = self.tmp_dir - self.assertEqual(self.tmp_dir, os.getcwd()) - os.environ['PWD'] = self.tmp_lnk - self.assertEqual(self.tmp_dir, os.getcwd()) + with mock.patch.dict('os.environ', {'PWD': self.tmp_dir}): + self.assertEqual(self.tmp_dir, os.getcwd()) + with mock.patch.dict('os.environ', {'PWD': self.tmp_lnk}): + self.assertEqual(self.tmp_dir, os.getcwd()) os.unlink(self.tmp_lnk) def test_get_current_dir_name(self): @@ -1279,21 +1279,16 @@ def test_get_current_dir_name(self): # the PWD environment variable if it exists regardless of # whether the path contains symlinks. with support.temp_cwd(self.tmp_dir): - os.environ['PWD'] = self.tmp_dir - self.assertEqual(self.tmp_dir, os.get_current_dir_name()) - os.symlink(self.tmp_dir, self.tmp_lnk, True) - if os.name == 'posix': - os.environ['PWD'] = self.tmp_lnk - self.assertEqual(self.tmp_lnk, os.get_current_dir_name()) - else: - os.environ['PWD'] = self.tmp_lnk + with mock.patch.dict('os.environ', {'PWD': self.tmp_dir}): self.assertEqual(self.tmp_dir, os.get_current_dir_name()) + 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()) + else: + self.assertEqual(self.tmp_dir, os.get_current_dir_name()) os.unlink(self.tmp_lnk) - def tearDown(self): - if self.pwd is not None: - os.environ['PWD'] = self.pwd - class RemoveDirsTests(unittest.TestCase): def setUp(self): From 4bfb33ce73180cc5da912207ae89a3bcfb819098 Mon Sep 17 00:00:00 2001 From: Braden Groom Date: Fri, 26 Oct 2018 21:31:41 -0700 Subject: [PATCH 5/9] fixup! bpo-1154351: add get_current_dir_name() to os module --- Doc/library/os.rst | 2 -- Lib/os.py | 5 +---- Lib/test/test_os.py | 16 ++++++++-------- 3 files changed, 9 insertions(+), 14 deletions(-) diff --git a/Doc/library/os.rst b/Doc/library/os.rst index 783bbf531d4b45..bef48f4a56dc11 100644 --- a/Doc/library/os.rst +++ b/Doc/library/os.rst @@ -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 diff --git a/Lib/os.py b/Lib/os.py index a0fd30525ff545..bea7c5223a5593 100644 --- a/Lib/os.py +++ b/Lib/os.py @@ -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 diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py index 722cc4ab935253..425d0096673c51 100644 --- a/Lib/test/test_os.py +++ b/Lib/test/test_os.py @@ -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) def test_get_current_dir_name(self): @@ -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): From 963659a1f8db46163235aa5dd9f6d28e02dcbcf6 Mon Sep 17 00:00:00 2001 From: Braden Groom Date: Fri, 26 Oct 2018 21:37:05 -0700 Subject: [PATCH 6/9] fixup! bpo-1154351: add get_current_dir_name() to os module --- Lib/test/test_os.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py index 425d0096673c51..172239cbedfa49 100644 --- a/Lib/test/test_os.py +++ b/Lib/test/test_os.py @@ -1265,6 +1265,7 @@ def test_getcwd(self): 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) self.assertEqual(os.getcwd(), self.tmp_dir) @@ -1272,7 +1273,6 @@ def test_getcwd(self): self.assertEqual(os.getcwd(), self.tmp_dir) with mock.patch.dict('os.environ', {'PWD': self.tmp_lnk}): self.assertEqual(os.getcwd(), self.tmp_dir) - os.unlink(self.tmp_lnk) def test_get_current_dir_name(self): # os.get_current_dir_name() returns the direct path--mirroring From cc1735f178914711f100e3660cb8bb83155bdb62 Mon Sep 17 00:00:00 2001 From: Braden Groom Date: Fri, 26 Oct 2018 22:17:32 -0700 Subject: [PATCH 7/9] fixup! bpo-1154351: add get_current_dir_name() to os module --- Lib/os.py | 3 +++ Lib/test/test_os.py | 5 ++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/Lib/os.py b/Lib/os.py index bea7c5223a5593..d073aa7a468846 100644 --- a/Lib/os.py +++ b/Lib/os.py @@ -661,6 +661,9 @@ def get_current_dir_name(): """ cwd = getcwd() + if name == 'nt': + return cwd + try: pwd = environ["PWD"] except KeyError: diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py index 172239cbedfa49..9040d38874a92c 100644 --- a/Lib/test/test_os.py +++ b/Lib/test/test_os.py @@ -1268,7 +1268,10 @@ def test_getcwd(self): self.addCleanup(support.unlink, self.tmp_lnk) os.symlink(self.tmp_dir, self.tmp_lnk, True) os.chdir(self.tmp_lnk) - self.assertEqual(os.getcwd(), self.tmp_dir) + if os.name == 'nt': + self.assertEqual(os.getcwd(), self.tmp_lnk) + else: + self.assertEqual(os.getcwd(), self.tmp_dir) with mock.patch.dict('os.environ', {'PWD': self.tmp_dir}): self.assertEqual(os.getcwd(), self.tmp_dir) with mock.patch.dict('os.environ', {'PWD': self.tmp_lnk}): From d49fdf3ef1437de6a3c1ff1875b0246a8ac72458 Mon Sep 17 00:00:00 2001 From: Braden Groom Date: Fri, 26 Oct 2018 22:32:55 -0700 Subject: [PATCH 8/9] fixup! bpo-1154351: add get_current_dir_name() to os module --- Lib/test/test_os.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py index 9040d38874a92c..57843ad59728dc 100644 --- a/Lib/test/test_os.py +++ b/Lib/test/test_os.py @@ -1269,13 +1269,15 @@ def test_getcwd(self): os.symlink(self.tmp_dir, self.tmp_lnk, True) os.chdir(self.tmp_lnk) if os.name == 'nt': - self.assertEqual(os.getcwd(), self.tmp_lnk) + # windows doesn't dereference the path + expected_cwd = self.tmp_lnk else: - self.assertEqual(os.getcwd(), self.tmp_dir) + 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(), self.tmp_dir) + self.assertEqual(os.getcwd(), expected_cwd) with mock.patch.dict('os.environ', {'PWD': self.tmp_lnk}): - self.assertEqual(os.getcwd(), self.tmp_dir) + self.assertEqual(os.getcwd(), expected_cwd) def test_get_current_dir_name(self): # os.get_current_dir_name() returns the direct path--mirroring From 17386e1b4dbbbb62a1a4189a291b40fb800e5edc Mon Sep 17 00:00:00 2001 From: Braden Groom Date: Fri, 26 Oct 2018 23:13:36 -0700 Subject: [PATCH 9/9] fixup! bpo-1154351: add get_current_dir_name() to os module --- Doc/library/os.rst | 8 ++++---- Doc/whatsnew/3.8.rst | 6 ++++++ Lib/os.py | 3 --- .../Library/2018-10-25-19-44-42.bpo-1154351.KhXLos.rst | 1 + 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/Doc/library/os.rst b/Doc/library/os.rst index bef48f4a56dc11..c4a215d536ee40 100644 --- a/Doc/library/os.rst +++ b/Doc/library/os.rst @@ -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 diff --git a/Doc/whatsnew/3.8.rst b/Doc/whatsnew/3.8.rst index a09492137e779e..82e4d4d86ae1bc 100644 --- a/Doc/whatsnew/3.8.rst +++ b/Doc/whatsnew/3.8.rst @@ -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 ------- diff --git a/Lib/os.py b/Lib/os.py index d073aa7a468846..5af3fc5a751469 100644 --- a/Lib/os.py +++ b/Lib/os.py @@ -660,15 +660,12 @@ def get_current_dir_name(): the *PWD* environment variable. """ cwd = getcwd() - if name == 'nt': return cwd - try: pwd = environ["PWD"] except KeyError: return cwd - if path.samefile(cwd, pwd): return pwd return cwd diff --git a/Misc/NEWS.d/next/Library/2018-10-25-19-44-42.bpo-1154351.KhXLos.rst b/Misc/NEWS.d/next/Library/2018-10-25-19-44-42.bpo-1154351.KhXLos.rst index 888e6829f06c08..f092917c42349a 100644 --- a/Misc/NEWS.d/next/Library/2018-10-25-19-44-42.bpo-1154351.KhXLos.rst +++ b/Misc/NEWS.d/next/Library/2018-10-25-19-44-42.bpo-1154351.KhXLos.rst @@ -1 +1,2 @@ Add get_current_dir_name() to the os module. +Patch by Braden Groom pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy