gh-126782: Support qualified referencing for `ntpath.abspath()` by nineteendo · Pull Request #126784 · python/cpython · GitHub
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion Include/internal/pycore_fileutils.h
1 change: 1 addition & 0 deletions Include/internal/pycore_global_objects_fini_generated.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Include/internal/pycore_global_strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ struct _Py_global_strings {
STRUCT_FOR_ID(exception)
STRUCT_FOR_ID(existing_file_name)
STRUCT_FOR_ID(exp)
STRUCT_FOR_ID(explicit_curdir)
STRUCT_FOR_ID(extend)
STRUCT_FOR_ID(extra_tokens)
STRUCT_FOR_ID(facility)
Expand Down
1 change: 1 addition & 0 deletions Include/internal/pycore_runtime_init_generated.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Include/internal/pycore_unicodeobject_generated.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Lib/ntpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,7 @@ def normpath(path):

# Return an absolute path.
try:
from nt import _path_normpath_ex as _normpath
from nt import _getfullpathname

except ImportError: # not running on Windows - mock up something sensible
Expand All @@ -573,7 +574,7 @@ def abspath(path):
def abspath(path):
"""Return the absolute version of a path."""
try:
return _getfullpathname(normpath(path))
return _getfullpathname(_normpath(path, explicit_curdir=True))
except (OSError, ValueError):
# See gh-75230, handle outside for cleaner traceback
pass
Expand Down
14 changes: 14 additions & 0 deletions Lib/test/test_ntpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,20 @@ def test_abspath(self):
tester('ntpath.abspath("")', cwd_dir)
tester('ntpath.abspath(" ")', cwd_dir + "\\ ")
tester('ntpath.abspath("?")', cwd_dir + "\\?")
tester('ntpath.abspath("con")', r"\\.\con")
# bpo-45354: Windows 11 changed MS-DOS device name handling
if sys.getwindowsversion()[:3] < (10, 0, 22000):

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we test on Windows 10 and 11?

tester('ntpath.abspath("./con")', r"\\.\con")
tester('ntpath.abspath("foo/../con")', r"\\.\con")
tester('ntpath.abspath("con/foo/..")', r"\\.\con")
tester('ntpath.abspath("con/.")', r"\\.\con")
else:
tester('ntpath.abspath("./con")', cwd_dir + r"\con")
tester('ntpath.abspath("foo/../con")', cwd_dir + r"\con")
tester('ntpath.abspath("con/foo/..")', cwd_dir + r"\con")
tester('ntpath.abspath("con/.")', cwd_dir + r"\con")
tester('ntpath.abspath("./Z:spam")', cwd_dir + r"\Z:spam")
tester('ntpath.abspath("spam/../Z:eggs")', cwd_dir + r"\Z:eggs")
drive, _ = ntpath.splitdrive(cwd_dir)
tester('ntpath.abspath("/abc/")', drive + "\\abc")

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Support qualified referencing for :func:`os.path.abspath` on Windows.
74 changes: 73 additions & 1 deletion Modules/clinic/posixmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 26 additions & 4 deletions Modules/posixmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -5598,21 +5598,25 @@ os__path_splitroot_ex_impl(PyObject *module, path_t *path)


/*[clinic input]
os._path_normpath
os._path_normpath_ex

path: path_t(make_wide=True, nonstrict=True)
*
explicit_curdir: bool = False

Normalize path, eliminating double slashes, etc.
[clinic start generated code]*/

static PyObject *
os__path_normpath_impl(PyObject *module, path_t *path)
/*[clinic end generated code: output=d353e7ed9410c044 input=3d4ac23b06332dcb]*/
os__path_normpath_ex_impl(PyObject *module, path_t *path,
int explicit_curdir)
/*[clinic end generated code: output=4c4c3bf33a70fe57 input=90fe0dfc4b3a751b]*/
{
PyObject *result;
Py_ssize_t norm_len;
wchar_t *norm_path = _Py_normpath_and_size((wchar_t *)path->wide,
path->length, &norm_len);
path->length, &norm_len,
explicit_curdir);
if (!norm_len) {
result = PyUnicode_FromOrdinal('.');
}
Expand All @@ -5625,6 +5629,23 @@ os__path_normpath_impl(PyObject *module, path_t *path)
return result;
}


/*[clinic input]
os._path_normpath

path: path_t(make_wide=True, nonstrict=True)

Normalize path, eliminating double slashes, etc.
[clinic start generated code]*/

static PyObject *
os__path_normpath_impl(PyObject *module, path_t *path)
/*[clinic end generated code: output=d353e7ed9410c044 input=3d4ac23b06332dcb]*/
{

return os__path_normpath_ex_impl(module, path, 0);
}

/*[clinic input]
os.mkdir

Expand Down Expand Up @@ -17006,6 +17027,7 @@ static PyMethodDef posix_methods[] = {
OS__GETVOLUMEPATHNAME_METHODDEF
OS__PATH_SPLITROOT_METHODDEF
OS__PATH_SPLITROOT_EX_METHODDEF
OS__PATH_NORMPATH_EX_METHODDEF
OS__PATH_NORMPATH_METHODDEF
OS_GETLOADAVG_METHODDEF
OS_URANDOM_METHODDEF
Expand Down
43 changes: 40 additions & 3 deletions Python/fileutils.c