From e0e0170e210c4c9f3bb14e2f28cb1eace3e2da38 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Tue, 17 Oct 2017 14:52:22 +0300 Subject: [PATCH 1/3] bpo-31802: Fix importing native path module before importing os. --- Lib/macpath.py | 23 ++++++++++--------- Lib/ntpath.py | 23 ++++++++++--------- Lib/posixpath.py | 22 ++++++++++-------- Lib/test/test_genericpath.py | 4 ++++ .../2017-10-17-14-52-14.bpo-31802.sYj2Zv.rst | 2 ++ 5 files changed, 42 insertions(+), 32 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2017-10-17-14-52-14.bpo-31802.sYj2Zv.rst diff --git a/Lib/macpath.py b/Lib/macpath.py index f85a91435b9044..aacf7235b011fd 100644 --- a/Lib/macpath.py +++ b/Lib/macpath.py @@ -1,5 +1,17 @@ """Pathname and path-related operations for the Macintosh.""" +# strings representing various path-related bits and pieces +# These are primarily for export; internally, they are hardcoded. +# Should be set before imports for resolving cyclic dependency. +curdir = ':' +pardir = '::' +extsep = '.' +sep = ':' +pathsep = '\n' +defpath = ':' +altsep = None +devnull = 'Dev:Null' + import os from stat import * import genericpath @@ -16,17 +28,6 @@ "curdir","pardir","sep","pathsep","defpath","altsep","extsep", "devnull","realpath","supports_unicode_filenames"] -# strings representing various path-related bits and pieces -# These are primarily for export; internally, they are hardcoded. -curdir = ':' -pardir = '::' -extsep = '.' -sep = ':' -pathsep = '\n' -defpath = ':' -altsep = None -devnull = 'Dev:Null' - def _get_colon(path): if isinstance(path, bytes): return b':' diff --git a/Lib/ntpath.py b/Lib/ntpath.py index 10d3f2dc35ba88..2182ec776cc503 100644 --- a/Lib/ntpath.py +++ b/Lib/ntpath.py @@ -5,6 +5,18 @@ module as os.path. """ +# strings representing various path-related bits and pieces +# These are primarily for export; internally, they are hardcoded. +# Should be set before imports for resolving cyclic dependency. +curdir = '.' +pardir = '..' +extsep = '.' +sep = '\\' +pathsep = ';' +altsep = '/' +defpath = '.;C:\\bin' +devnull = 'nul' + import os import sys import stat @@ -19,17 +31,6 @@ "extsep","devnull","realpath","supports_unicode_filenames","relpath", "samefile", "sameopenfile", "samestat", "commonpath"] -# strings representing various path-related bits and pieces -# These are primarily for export; internally, they are hardcoded. -curdir = '.' -pardir = '..' -extsep = '.' -sep = '\\' -pathsep = ';' -altsep = '/' -defpath = '.;C:\\bin' -devnull = 'nul' - def _get_bothseps(path): if isinstance(path, bytes): return b'\\/' diff --git a/Lib/posixpath.py b/Lib/posixpath.py index 6dbdab27497f2b..e92186c64e0ddb 100644 --- a/Lib/posixpath.py +++ b/Lib/posixpath.py @@ -10,6 +10,18 @@ for manipulation of the pathname component of URLs. """ +# Strings representing various path-related bits and pieces. +# These are primarily for export; internally, they are hardcoded. +# Should be set before imports for resolving cyclic dependency. +curdir = '.' +pardir = '..' +extsep = '.' +sep = '/' +pathsep = ':' +defpath = ':/bin:/usr/bin' +altsep = None +devnull = '/dev/null' + import os import sys import stat @@ -25,16 +37,6 @@ "devnull","realpath","supports_unicode_filenames","relpath", "commonpath"] -# Strings representing various path-related bits and pieces. -# These are primarily for export; internally, they are hardcoded. -curdir = '.' -pardir = '..' -extsep = '.' -sep = '/' -pathsep = ':' -defpath = ':/bin:/usr/bin' -altsep = None -devnull = '/dev/null' def _get_sep(path): if isinstance(path, bytes): diff --git a/Lib/test/test_genericpath.py b/Lib/test/test_genericpath.py index f698e13f68a7bf..2f53b97654a86f 100644 --- a/Lib/test/test_genericpath.py +++ b/Lib/test/test_genericpath.py @@ -8,6 +8,7 @@ import unittest import warnings from test import support +from test.support import script_helper android_not_root = support.android_not_root @@ -483,6 +484,9 @@ def test_relpath_errors(self): with self.assertRaisesRegex(TypeError, 'bytearray'): self.pathmodule.relpath(bytearray(b'foo'), bytearray(b'bar')) + def test_import(self): + script_helper.assert_python_ok('-S', '-c', 'import ' + self.pathmodule.__name__) + class PathLikeTests(unittest.TestCase): diff --git a/Misc/NEWS.d/next/Library/2017-10-17-14-52-14.bpo-31802.sYj2Zv.rst b/Misc/NEWS.d/next/Library/2017-10-17-14-52-14.bpo-31802.sYj2Zv.rst new file mode 100644 index 00000000000000..77df6bbe6950e6 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2017-10-17-14-52-14.bpo-31802.sYj2Zv.rst @@ -0,0 +1,2 @@ +Importing native path module (``posixpath``, ``ntpath``) now works even if +the ``os`` module still is not imported. From 664f0e8821c7982820f714272bab609350994677 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Thu, 4 Jan 2018 11:23:48 +0200 Subject: [PATCH 2/3] Wrap too long line. --- Lib/test/test_genericpath.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_genericpath.py b/Lib/test/test_genericpath.py index db020006ca6ba4..4f45cfa901aa24 100644 --- a/Lib/test/test_genericpath.py +++ b/Lib/test/test_genericpath.py @@ -8,7 +8,6 @@ import unittest import warnings from test import support -from test.support import script_helper def create_file(filename, data=b'foo'): @@ -488,7 +487,8 @@ def test_relpath_errors(self): self.pathmodule.relpath(bytearray(b'foo'), bytearray(b'bar')) def test_import(self): - script_helper.assert_python_ok('-S', '-c', 'import ' + self.pathmodule.__name__) + support.script_helper.assert_python_ok( + '-S', '-c', 'import ' + self.pathmodule.__name__) class PathLikeTests(unittest.TestCase): From 6ed43b2d7203a14d8f2da0c128d003be84a053fc Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Sun, 7 Jan 2018 09:58:22 +0200 Subject: [PATCH 3/3] Fix import in tests. --- Lib/test/test_genericpath.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_genericpath.py b/Lib/test/test_genericpath.py index 4f45cfa901aa24..ad5a59c44ed7bd 100644 --- a/Lib/test/test_genericpath.py +++ b/Lib/test/test_genericpath.py @@ -8,6 +8,7 @@ import unittest import warnings from test import support +from test.support.script_helper import assert_python_ok def create_file(filename, data=b'foo'): @@ -487,8 +488,7 @@ def test_relpath_errors(self): self.pathmodule.relpath(bytearray(b'foo'), bytearray(b'bar')) def test_import(self): - support.script_helper.assert_python_ok( - '-S', '-c', 'import ' + self.pathmodule.__name__) + assert_python_ok('-S', '-c', 'import ' + self.pathmodule.__name__) class PathLikeTests(unittest.TestCase):