Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-118263: Generalize path_t for C level optimizations #118355

Merged
merged 31 commits into from May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
ae2e171
Add `null_embeddable`
nineteendo Apr 27, 2024
0625bf1
Add `make_wide`
nineteendo Apr 28, 2024
28b4b6e
📜🤖 Added by blurb_it.
blurb-it[bot] Apr 28, 2024
19209c6
Fix typo & add type casts
nineteendo Apr 28, 2024
7ca6036
Add test for fast paths
nineteendo Apr 28, 2024
55c1883
Build tuple faster
nineteendo Apr 29, 2024
a9b7bdd
Merge branch 'main' into generalize-path_t
nineteendo Apr 29, 2024
42b2ca1
Merge branch 'main' into generalize-path_t
nineteendo Apr 30, 2024
5b44245
Resolve merge conflicts
nineteendo Apr 30, 2024
1c93617
Support `make_wide=False` on Windows
nineteendo May 3, 2024
dc36f2e
Update generated code
nineteendo May 3, 2024
0a9d497
Add `suppress`
nineteendo May 4, 2024
14d711e
Fix `allow_fd`
nineteendo May 4, 2024
89e83fa
Allow paths of any length
nineteendo May 4, 2024
d6a174e
Update generated files
nineteendo May 4, 2024
4e8b713
Simplify bytes conversion
nineteendo May 4, 2024
0777a31
Remove leftovers
nineteendo May 4, 2024
46059d1
Update conditions to use `PyBytes_Check()`
nineteendo May 4, 2024
f95da67
Fix access violation attempt 1
nineteendo May 4, 2024
e8ab7d7
Fix segmentation fault
nineteendo May 4, 2024
3b21f5d
Fix segmentation fault attempt 2
nineteendo May 4, 2024
7b3a785
Fix access violation attempt 1
nineteendo May 4, 2024
a94c852
Clear ValueError
nineteendo May 4, 2024
5f717c9
Apply suggestions from code review
nineteendo May 4, 2024
b76774b
Same docstring for python fallback
nineteendo May 4, 2024
f2537a1
Revert newline
nineteendo May 4, 2024
aa0df5c
Merge branch 'main' into generalize-path_t
nineteendo May 22, 2024
8f67225
Merge branch 'main' into generalize-path_t
nineteendo May 22, 2024
360326d
Fix pointers
nineteendo May 22, 2024
3514bdb
Return false in case of value error
nineteendo May 22, 2024
e362054
Add null check
nineteendo May 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 5 additions & 37 deletions Lib/ntpath.py
Expand Up @@ -168,19 +168,12 @@ def splitdrive(p):


try:
from nt import _path_splitroot_ex
from nt import _path_splitroot_ex as splitroot
nineteendo marked this conversation as resolved.
Show resolved Hide resolved
except ImportError:
def splitroot(p):
"""Split a pathname into drive, root and tail. The drive is defined
exactly as in splitdrive(). On Windows, the root may be a single path
separator or an empty string. The tail contains anything after the root.
For example:

splitroot('//server/share/') == ('//server/share', '/', '')
splitroot('C:/Users/Barney') == ('C:', '/', 'Users/Barney')
splitroot('C:///spam///ham') == ('C:', '/', '//spam///ham')
splitroot('Windows/notepad') == ('', '', 'Windows/notepad')
"""
"""Split a pathname into drive, root and tail.

The tail contains anything after the root."""
p = os.fspath(p)
if isinstance(p, bytes):
sep = b'\\'
Expand Down Expand Up @@ -220,23 +213,6 @@ def splitroot(p):
else:
# Relative path, e.g. Windows
return empty, empty, p
else:
def splitroot(p):
"""Split a pathname into drive, root and tail. The drive is defined
exactly as in splitdrive(). On Windows, the root may be a single path
separator or an empty string. The tail contains anything after the root.
For example:

splitroot('//server/share/') == ('//server/share', '/', '')
splitroot('C:/Users/Barney') == ('C:', '/', 'Users/Barney')
splitroot('C:///spam///ham') == ('C:', '/', '//spam///ham')
splitroot('Windows/notepad') == ('', '', 'Windows/notepad')
"""
p = os.fspath(p)
if isinstance(p, bytes):
drive, root, tail = _path_splitroot_ex(os.fsdecode(p))
return os.fsencode(drive), os.fsencode(root), os.fsencode(tail)
return _path_splitroot_ex(p)


# Split a path in head (everything up to the last '/') and tail (the
Expand Down Expand Up @@ -538,7 +514,7 @@ def expandvars(path):
# Previously, this function also truncated pathnames to 8+3 format,
# but as this module is called "ntpath", that's obviously wrong!
try:
from nt import _path_normpath
from nt import _path_normpath as normpath

except ImportError:
def normpath(path):
Expand Down Expand Up @@ -577,14 +553,6 @@ def normpath(path):
comps.append(curdir)
return prefix + sep.join(comps)

else:
def normpath(path):
"""Normalize path, eliminating double slashes, etc."""
path = os.fspath(path)
if isinstance(path, bytes):
return os.fsencode(_path_normpath(os.fsdecode(path))) or b"."
return _path_normpath(path) or "."


def _abspath_fallback(path):
"""Return the absolute version of a path as a fallback function in case
Expand Down
41 changes: 5 additions & 36 deletions Lib/posixpath.py
Expand Up @@ -135,18 +135,12 @@ def splitdrive(p):


try:
from posix import _path_splitroot_ex
from posix import _path_splitroot_ex as splitroot
except ImportError:
def splitroot(p):
"""Split a pathname into drive, root and tail. On Posix, drive is always
empty; the root may be empty, a single slash, or two slashes. The tail
contains anything after the root. For example:

splitroot('foo/bar') == ('', '', 'foo/bar')
splitroot('/foo/bar') == ('', '/', 'foo/bar')
splitroot('//foo/bar') == ('', '//', 'foo/bar')
splitroot('///foo/bar') == ('', '/', '//foo/bar')
"""
"""Split a pathname into drive, root and tail.

The tail contains anything after the root."""
p = os.fspath(p)
if isinstance(p, bytes):
sep = b'/'
Expand All @@ -164,23 +158,6 @@ def splitroot(p):
# Precisely two leading slashes, e.g.: '//foo'. Implementation defined per POSIX, see
# https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_13
return empty, p[:2], p[2:]
else:
def splitroot(p):
"""Split a pathname into drive, root and tail. On Posix, drive is always
empty; the root may be empty, a single slash, or two slashes. The tail
contains anything after the root. For example:

splitroot('foo/bar') == ('', '', 'foo/bar')
splitroot('/foo/bar') == ('', '/', 'foo/bar')
splitroot('//foo/bar') == ('', '//', 'foo/bar')
splitroot('///foo/bar') == ('', '/', '//foo/bar')
"""
p = os.fspath(p)
if isinstance(p, bytes):
# Optimisation: the drive is always empty
_, root, tail = _path_splitroot_ex(os.fsdecode(p))
return b'', os.fsencode(root), os.fsencode(tail)
return _path_splitroot_ex(p)


# Return the tail (basename) part of a path, same as split(path)[1].
Expand Down Expand Up @@ -363,7 +340,7 @@ def expandvars(path):
# if it contains symbolic links!

try:
from posix import _path_normpath
from posix import _path_normpath as normpath

except ImportError:
def normpath(path):
Expand Down Expand Up @@ -394,14 +371,6 @@ def normpath(path):
path = initial_slashes + sep.join(comps)
return path or dot

else:
def normpath(path):
"""Normalize path, eliminating double slashes, etc."""
path = os.fspath(path)
if isinstance(path, bytes):
return os.fsencode(_path_normpath(os.fsdecode(path))) or b"."
return _path_normpath(path) or "."


def abspath(path):
"""Return an absolute path."""
Expand Down
4 changes: 4 additions & 0 deletions Lib/test/test_ntpath.py
Expand Up @@ -1129,6 +1129,10 @@ def test_fast_paths_in_use(self):
# There are fast paths of these functions implemented in posixmodule.c.
# Confirm that they are being used, and not the Python fallbacks in
# genericpath.py.
self.assertTrue(os.path.splitroot is nt._path_splitroot_ex)
self.assertFalse(inspect.isfunction(os.path.splitroot))
self.assertTrue(os.path.normpath is nt._path_normpath)
self.assertFalse(inspect.isfunction(os.path.normpath))
self.assertTrue(os.path.isdir is nt._path_isdir)
self.assertFalse(inspect.isfunction(os.path.isdir))
self.assertTrue(os.path.isfile is nt._path_isfile)
Expand Down
13 changes: 12 additions & 1 deletion Lib/test/test_posixpath.py
@@ -1,11 +1,12 @@
import inspect
import os
import posixpath
import sys
import unittest
from posixpath import realpath, abspath, dirname, basename
from test import test_genericpath
from test.support import import_helper
from test.support import os_helper
from test.support import cpython_only, os_helper
from test.support.os_helper import FakePath
from unittest import mock

Expand Down Expand Up @@ -283,6 +284,16 @@ def fake_lstat(path):
def test_isjunction(self):
self.assertFalse(posixpath.isjunction(ABSTFN))

@unittest.skipIf(sys.platform == 'win32', "Fast paths are not for win32")
@cpython_only
def test_fast_paths_in_use(self):
# There are fast paths of these functions implemented in posixmodule.c.
# Confirm that they are being used, and not the Python fallbacks
self.assertTrue(os.path.splitroot is posix._path_splitroot_ex)
self.assertFalse(inspect.isfunction(os.path.splitroot))
self.assertTrue(os.path.normpath is posix._path_normpath)
self.assertFalse(inspect.isfunction(os.path.normpath))

def test_expanduser(self):
self.assertEqual(posixpath.expanduser("foo"), "foo")
self.assertEqual(posixpath.expanduser(b"foo"), b"foo")
Expand Down
@@ -0,0 +1 @@
Speed up :func:`os.path.splitroot` & :func:`os.path.normpath` with a direct C call.