10000 bpo-42658: Using LCMapStringEx in ntpath.normcase by aisk · Pull Request #32010 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-42658: Using LCMapStringEx in ntpath.normcase #32010

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

Merged
merged 17 commits into from
Jun 6, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update for review and fix tests
  • Loading branch information
aisk committed Jun 4, 2022
commit 3b4a5cb1bdd3723904de3eeb89b34e1611feb3f6
60 changes: 35 additions & 25 deletions Lib/ntpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,6 @@
from genericpath import *


try:
import _winapi

def _normcase(s):
is_bytes = isinstance(s, bytes)
if is_bytes:
s = os.fsdecode(s)
s = _winapi.LCMapStringEx(_winapi.LOCALE_NAME_INVARIANT,
_winapi.LCMAP_LOWERCASE,
s.replace('/', '\\'))
if is_bytes:
s = os.fsencode(s)
return s
except ImportError:
def _normcase(s):
if isinstance(s, bytes):
return s.replace(b'/', b'\\').lower()
return s.replace('/', '\\').lower()



__all__ = ["normcase","isabs","join","splitdrive","split","splitext",
"basename","dirname","commonprefix","getsize","getmtime",
"getatime","getctime", "islink","exists","lexists","isdir","isfile",
Expand All @@ -63,11 +42,42 @@ def _get_bothseps(path):
# Other normalizations (such as optimizing '../' away) are not done
# (this is done by normpath).

def normcase(s):
"""Normalize case of pathname.
try:
from _winapi import (
LCMapStringEx as _LCMapStringEx,
LOCALE_NAME_INVARIANT as _LOCALE_NAME_INVARIANT,
LCMAP_LOWERCASE as _LCMAP_LOWERCASE)

Makes all characters lowercase and all slashes into backslashes."""
return _normcase(os.fspath(s))
def normcase(s):
"""Normalize case of pathname.

Makes all characters lowercase and all slashes into backslashes.
"""
if isinstance(s, bytes):
if s == b'':
return s
s = os.fsdecode(s).replace('/', '\\')
return os.fsencode(_LCMapStringEx(_LOCALE_NAME_INVARIANT,
_LCMAP_LOWERCASE, s))
else:
if isinstance(s, str) and s == '':
return s
s = os.fspath(s)
return _LCMapStringEx(_LOCALE_NAME_INVARIANT,
_LCMAP_LOWERCASE,
s.replace('/', '\\'))
# else:
# msg = f"expected str, bytes or os.PathLike object, not {type(s)}"
# raise TypeError(msg)
except ImportError:
def normcase(s):
"""Normalize case of pathname.

Makes all characters lowercase and all slashes into backslashes.
"""
if isinstance(s, bytes):
return os.fsencode(os.fsdecode(s).replace('/', '\\').lower())
return s.replace('/', '\\').lower()


# Return whether a path is absolute.
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_ntpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -852,7 +852,8 @@ def _check_function(self, func):

def test_path_normcase(self):
self._check_function(self.path.normcase)
self.assertEqual(ntpath.normcase('\u03a9\u2126'), 'ωΩ')
if sys.platform == 'win32':
self.assertEqual(ntpath.normcase('\u03a9\u2126'), 'ωΩ')

def test_path_isabs(self):
self._check_function(self.path.isabs)
Expand Down
0