Skip to content

Commit b2f7691

Browse files
serhiy-storchakamiss-islington
authored andcommitted
bpo-31802: Fix importing native path module before importing os. (GH-4017)
(cherry picked from commit 3460198)
1 parent 157be7c commit b2f7691

File tree

5 files changed

+42
-32
lines changed

5 files changed

+42
-32
lines changed

Lib/macpath.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
"""Pathname and path-related operations for the Macintosh."""
22

3+
# strings representing various path-related bits and pieces
4+
# These are primarily for export; internally, they are hardcoded.
5+
# Should be set before imports for resolving cyclic dependency.
6+
curdir = ':'
7+
pardir = '::'
8+
extsep = '.'
9+
sep = ':'
10+
pathsep = '\n'
11+
defpath = ':'
12+
altsep = None
13+
devnull = 'Dev:Null'
14+
315
import os
416
from stat import *
517
import genericpath
@@ -12,17 +24,6 @@
1224
"curdir","pardir","sep","pathsep","defpath","altsep","extsep",
1325
"devnull","realpath","supports_unicode_filenames"]
1426

15-
# strings representing various path-related bits and pieces
16-
# These are primarily for export; internally, they are hardcoded.
17-
curdir = ':'
18-
pardir = '::'
19-
extsep = '.'
20-
sep = ':'
21-
pathsep = '\n'
22-
defpath = ':'
23-
altsep = None
24-
devnull = 'Dev:Null'
25-
2627
def _get_colon(path):
2728
if isinstance(path, bytes):
2829
return b':'

Lib/ntpath.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@
55
module as os.path.
66
"""
77

8+
# strings representing various path-related bits and pieces
9+
# These are primarily for export; internally, they are hardcoded.
10+
# Should be set before imports for resolving cyclic dependency.
11+
curdir = '.'
12+
pardir = '..'
13+
extsep = '.'
14+
sep = '\\'
15+
pathsep = ';'
16+
altsep = '/'
17+
defpath = '.;C:\\bin'
18+
devnull = 'nul'
19+
820
import os
921
import sys
1022
import stat
@@ -19,17 +31,6 @@
1931
"extsep","devnull","realpath","supports_unicode_filenames","relpath",
2032
"samefile", "sameopenfile", "samestat", "commonpath"]
2133

22-
# strings representing various path-related bits and pieces
23-
# These are primarily for export; internally, they are hardcoded.
24-
curdir = '.'
25-
pardir = '..'
26-
extsep = '.'
27-
sep = '\\'
28-
pathsep = ';'
29-
altsep = '/'
30-
defpath = '.;C:\\bin'
31-
devnull = 'nul'
32-
3334
def _get_bothseps(path):
3435
if isinstance(path, bytes):
3536
return b'\\/'

Lib/posixpath.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@
1010
for manipulation of the pathname component of URLs.
1111
"""
1212

13+
# Strings representing various path-related bits and pieces.
14+
# These are primarily for export; internally, they are hardcoded.
15+
# Should be set before imports for resolving cyclic dependency.
16+
curdir = '.'
17+
pardir = '..'
18+
extsep = '.'
19+
sep = '/'
20+
pathsep = ':'
21+
defpath = ':/bin:/usr/bin'
22+
altsep = None
23+
devnull = '/dev/null'
24+
1325
import os
1426
import sys
1527
import stat
@@ -25,16 +37,6 @@
2537
"devnull","realpath","supports_unicode_filenames","relpath",
2638
"commonpath"]
2739

28-
# Strings representing various path-related bits and pieces.
29-
# These are primarily for export; internally, they are hardcoded.
30-
curdir = '.'
31-
pardir = '..'
32-
extsep = '.'
33-
sep = '/'
34-
pathsep = ':'
35-
defpath = ':/bin:/usr/bin'
36-
altsep = None
37-
devnull = '/dev/null'
3840

3941
def _get_sep(path):
4042
if isinstance(path, bytes):

Lib/test/test_genericpath.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import unittest
99
import warnings
1010
from test import support
11+
from test.support.script_helper import assert_python_ok
1112

1213

1314
def create_file(filename, data=b'foo'):
@@ -486,6 +487,9 @@ def test_relpath_errors(self):
486487
with self.assertRaisesRegex(TypeError, 'bytearray'):
487488
self.pathmodule.relpath(bytearray(b'foo'), bytearray(b'bar'))
488489

490+
def test_import(self):
491+
assert_python_ok('-S', '-c', 'import ' + self.pathmodule.__name__)
492+
489493

490494
class PathLikeTests(unittest.TestCase):
491495

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Importing native path module (``posixpath``, ``ntpath``) now works even if
2+
the ``os`` module still is not imported.

0 commit comments

Comments
 (0)