Skip to content

Commit 09a85ea

Browse files
authored
gh-118263: Add additional arguments to path_t (Argument Clinic type) in posixmodule (GH-119608)
1 parent ab9b605 commit 09a85ea

File tree

7 files changed

+314
-267
lines changed

7 files changed

+314
-267
lines changed

Lib/ntpath.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ def expandvars(path):
521521
# Previously, this function also truncated pathnames to 8+3 format,
522522
# but as this module is called "ntpath", that's obviously wrong!
523523
try:
524-
from nt import _path_normpath
524+
from nt import _path_normpath as normpath
525525

526526
except ImportError:
527527
def normpath(path):
@@ -560,14 +560,6 @@ def normpath(path):
560560
comps.append(curdir)
561561
return prefix + sep.join(comps)
562562

563-
else:
564-
def normpath(path):
565-
"""Normalize path, eliminating double slashes, etc."""
566-
path = os.fspath(path)
567-
if isinstance(path, bytes):
568-
return os.fsencode(_path_normpath(os.fsdecode(path))) or b"."
569-
return _path_normpath(path) or "."
570-
571563

572564
def _abspath_fallback(path):
573565
"""Return the absolute version of a path as a fallback function in case

Lib/posixpath.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ def expandvars(path):
371371
# if it contains symbolic links!
372372

373373
try:
374-
from posix import _path_normpath
374+
from posix import _path_normpath as normpath
375375

376376
except ImportError:
377377
def normpath(path):
@@ -404,14 +404,6 @@ def normpath(path):
404404
path = initial_slashes + sep.join(comps)
405405
return path or dot
406406

407-
else:
408-
def normpath(path):
409-
"""Normalize path, eliminating double slashes, etc."""
410-
path = os.fspath(path)
411-
if isinstance(path, bytes):
412-
return os.fsencode(_path_normpath(os.fsdecode(path))) or b"."
413-
return _path_normpath(path) or "."
414-
415407

416408
def abspath(path):
417409
"""Return an absolute path."""

Lib/test/test_ntpath.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,6 +1010,8 @@ def test_fast_paths_in_use(self):
10101010
# There are fast paths of these functions implemented in posixmodule.c.
10111011
# Confirm that they are being used, and not the Python fallbacks in
10121012
# genericpath.py.
1013+
self.assertTrue(os.path.normpath is nt._path_normpath)
1014+
self.assertFalse(inspect.isfunction(os.path.normpath))
10131015
self.assertTrue(os.path.isdir is nt._path_isdir)
10141016
self.assertFalse(inspect.isfunction(os.path.isdir))
10151017
self.assertTrue(os.path.isfile is nt._path_isfile)

Lib/test/test_posixpath.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
import inspect
12
import os
23
import posixpath
34
import sys
45
import unittest
56
from posixpath import realpath, abspath, dirname, basename
67
from test import test_genericpath
78
from test.support import import_helper
8-
from test.support import os_helper
9+
from test.support import cpython_only, os_helper
910
from test.support.os_helper import FakePath
1011
from unittest import mock
1112

@@ -273,6 +274,14 @@ def fake_lstat(path):
273274
def test_isjunction(self):
274275
self.assertFalse(posixpath.isjunction(ABSTFN))
275276

277+
@unittest.skipIf(sys.platform == 'win32', "Fast paths are not for win32")
278+
@cpython_only
279+
def test_fast_paths_in_use(self):
280+
# There are fast paths of these functions implemented in posixmodule.c.
281+
# Confirm that they are being used, and not the Python fallbacks
282+
self.assertTrue(os.path.normpath is posix._path_normpath)
283+
self.assertFalse(inspect.isfunction(os.path.normpath))
284+
276285
def test_expanduser(self):
277286
self.assertEqual(posixpath.expanduser("foo"), "foo")
278287
self.assertEqual(posixpath.expanduser(b"foo"), b"foo")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Speed up :func:`os.path.normpath` with a direct C call.

0 commit comments

Comments
 (0)