Skip to content

Commit 19d313b

Browse files
lazkanaveen521kk
authored andcommitted
getpath: use normpath on all generated paths
Instead of just calling normpath in abspath just call normpath on all the config results. This makes sure we don't change getpath.py too much and still cover all outputs. This fixes sys.exec_prefix not matching sys.prefix, see #142
1 parent 920c434 commit 19d313b

File tree

3 files changed

+42
-15
lines changed

3 files changed

+42
-15
lines changed

Lib/test/test_getpath.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -914,6 +914,9 @@ def __missing__(self, key):
914914
except AttributeError:
915915
raise KeyError(key) from None
916916

917+
def normpath(self, path):
918+
return ntpath.normpath(path)
919+
917920
def abspath(self, path):
918921
if self.isabs(path):
919922
return path
@@ -1092,6 +1095,9 @@ def __missing__(self, key):
10921095
except AttributeError:
10931096
raise KeyError(key) from None
10941097

1098+
def normpath(self, path):
1099+
return path
1100+
10951101
def abspath(self, path):
10961102
if self.isabs(path):
10971103
return path

Modules/getpath.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,25 @@
5454

5555
/* HELPER FUNCTIONS for getpath.py */
5656

57+
static PyObject *
58+
getpath_normpath(PyObject *Py_UNUSED(self), PyObject *args)
59+
{
60+
PyObject *r = NULL;
61+
PyObject *pathobj;
62+
wchar_t *path;
63+
if (!PyArg_ParseTuple(args, "U", &pathobj)) {
64+
return NULL;
65+
}
66+
Py_ssize_t len;
67+
wchar_t *buffer = PyUnicode_AsWideCharString(pathobj, &len);
68+
if (!buffer) {
69+
return NULL;
70+
}
71+
r = PyUnicode_FromWideChar(_Py_normpath(buffer, len), -1);
72+
PyMem_Free(buffer);
73+
return r;
74+
}
75+
5776
static PyObject *
5877
getpath_abspath(PyObject *Py_UNUSED(self), PyObject *args)
5978
{
@@ -68,7 +87,6 @@ getpath_abspath(PyObject *Py_UNUSED(self), PyObject *args)
6887
if (path) {
6988
wchar_t *abs;
7089
if (_Py_abspath((const wchar_t *)_Py_normpath(path, -1), &abs) == 0 && abs) {
71-
abs = _Py_normpath(abs, -1);
7290
r = PyUnicode_FromWideChar(abs, -1);
7391
PyMem_RawFree((void *)abs);
7492
} else {
@@ -526,6 +544,7 @@ getpath_realpath(PyObject *Py_UNUSED(self) , PyObject *args)
526544

527545

528546
static PyMethodDef getpath_methods[] = {
547+
{"normpath", getpath_normpath, METH_VARARGS, NULL},
529548
{"abspath", getpath_abspath, METH_VARARGS, NULL},
530549
{"basename", getpath_basename, METH_VARARGS, NULL},
531550
{"dirname", getpath_dirname, METH_VARARGS, NULL},

Modules/getpath.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,8 @@ def search_up(prefix, *landmarks, test=isfile):
233233
return prefix
234234
prefix = dirname(prefix)
235235

236+
def _normpath(p):
237+
return normpath(p) if p is not None else None
236238

237239
# ******************************************************************************
238240
# READ VARIABLES FROM config
@@ -670,7 +672,7 @@ def search_up(prefix, *landmarks, test=isfile):
670672

671673
if py_setpath:
672674
# If Py_SetPath was called then it overrides any existing search path
673-
config['module_search_paths'] = py_setpath.split(DELIM)
675+
config['module_search_paths'] = [_normpath(p) for p in py_setpath.split(DELIM)]
674676
config['module_search_paths_set'] = 1
675677

676678
elif not pythonpath_was_set:
@@ -757,7 +759,7 @@ def search_up(prefix, *landmarks, test=isfile):
757759
if platstdlib_dir:
758760
pythonpath.append(platstdlib_dir)
759761

760-
config['module_search_paths'] = pythonpath
762+
config['module_search_paths'] = [_normpath(p) for p in pythonpath]
761763
config['module_search_paths_set'] = 1
762764

763765

@@ -792,23 +794,23 @@ def search_up(prefix, *landmarks, test=isfile):
792794
warn("unsupported 'import' line in ._pth file")
793795
else:
794796
pythonpath.append(joinpath(pth_dir, line))
795-
config['module_search_paths'] = pythonpath
797+
config['module_search_paths'] = [_normpath(p) for p in pythonpath]
796798
config['module_search_paths_set'] = 1
797799

798800
# ******************************************************************************
799801
# UPDATE config FROM CALCULATED VALUES
800802
# ******************************************************************************
801803

802-
config['program_name'] = program_name
803-
config['home'] = home
804-
config['executable'] = executable
805-
config['base_executable'] = base_executable
806-
config['prefix'] = prefix
807-
config['exec_prefix'] = exec_prefix
808-
config['base_prefix'] = base_prefix or prefix
809-
config['base_exec_prefix'] = base_exec_prefix or exec_prefix
804+
config['program_name'] = _normpath(program_name)
805+
config['home'] = _normpath(home)
806+
config['executable'] = _normpath(executable)
807+
config['base_executable'] = _normpath(base_executable)
808+
config['prefix'] = _normpath(prefix)
809+
config['exec_prefix'] = _normpath(exec_prefix)
810+
config['base_prefix'] = _normpath(base_prefix or prefix)
811+
config['base_exec_prefix'] = _normpath(base_exec_prefix or exec_prefix)
810812

811-
config['platlibdir'] = platlibdir
813+
config['platlibdir'] = _normpath(platlibdir)
812814
# test_embed expects empty strings, not None
813-
config['stdlib_dir'] = stdlib_dir or ''
814-
config['platstdlib_dir'] = platstdlib_dir or ''
815+
config['stdlib_dir'] = _normpath(stdlib_dir or '')
816+
config['platstdlib_dir'] = _normpath(platstdlib_dir or '')

0 commit comments

Comments
 (0)