Skip to content

Commit

Permalink
getpath: use normpath on all generated paths
Browse files Browse the repository at this point in the history
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
  • Loading branch information
lazka authored and naveen521kk committed Jul 29, 2023
1 parent 920c434 commit 19d313b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 15 deletions.
6 changes: 6 additions & 0 deletions Lib/test/test_getpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,9 @@ def __missing__(self, key):
except AttributeError:
raise KeyError(key) from None

def normpath(self, path):
return ntpath.normpath(path)

def abspath(self, path):
if self.isabs(path):
return path
Expand Down Expand Up @@ -1092,6 +1095,9 @@ def __missing__(self, key):
except AttributeError:
raise KeyError(key) from None

def normpath(self, path):
return path

def abspath(self, path):
if self.isabs(path):
return path
Expand Down
21 changes: 20 additions & 1 deletion Modules/getpath.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,25 @@

/* HELPER FUNCTIONS for getpath.py */

static PyObject *
getpath_normpath(PyObject *Py_UNUSED(self), PyObject *args)
{
PyObject *r = NULL;
PyObject *pathobj;
wchar_t *path;
if (!PyArg_ParseTuple(args, "U", &pathobj)) {
return NULL;
}
Py_ssize_t len;
wchar_t *buffer = PyUnicode_AsWideCharString(pathobj, &len);
if (!buffer) {
return NULL;
}
r = PyUnicode_FromWideChar(_Py_normpath(buffer, len), -1);
PyMem_Free(buffer);
return r;
}

static PyObject *
getpath_abspath(PyObject *Py_UNUSED(self), PyObject *args)
{
Expand All @@ -68,7 +87,6 @@ getpath_abspath(PyObject *Py_UNUSED(self), PyObject *args)
if (path) {
wchar_t *abs;
if (_Py_abspath((const wchar_t *)_Py_normpath(path, -1), &abs) == 0 && abs) {
abs = _Py_normpath(abs, -1);
r = PyUnicode_FromWideChar(abs, -1);
PyMem_RawFree((void *)abs);
} else {
Expand Down Expand Up @@ -526,6 +544,7 @@ getpath_realpath(PyObject *Py_UNUSED(self) , PyObject *args)


static PyMethodDef getpath_methods[] = {
{"normpath", getpath_normpath, METH_VARARGS, NULL},
{"abspath", getpath_abspath, METH_VARARGS, NULL},
{"basename", getpath_basename, METH_VARARGS, NULL},
{"dirname", getpath_dirname, METH_VARARGS, NULL},
Expand Down
30 changes: 16 additions & 14 deletions Modules/getpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ def search_up(prefix, *landmarks, test=isfile):
return prefix
prefix = dirname(prefix)

def _normpath(p):
return normpath(p) if p is not None else None

# ******************************************************************************
# READ VARIABLES FROM config
Expand Down Expand Up @@ -670,7 +672,7 @@ def search_up(prefix, *landmarks, test=isfile):

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

elif not pythonpath_was_set:
Expand Down Expand Up @@ -757,7 +759,7 @@ def search_up(prefix, *landmarks, test=isfile):
if platstdlib_dir:
pythonpath.append(platstdlib_dir)

config['module_search_paths'] = pythonpath
config['module_search_paths'] = [_normpath(p) for p in pythonpath]
config['module_search_paths_set'] = 1


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

# ******************************************************************************
# UPDATE config FROM CALCULATED VALUES
# ******************************************************************************

config['program_name'] = program_name
config['home'] = home
config['executable'] = executable
config['base_executable'] = base_executable
config['prefix'] = prefix
config['exec_prefix'] = exec_prefix
config['base_prefix'] = base_prefix or prefix
config['base_exec_prefix'] = base_exec_prefix or exec_prefix
config['program_name'] = _normpath(program_name)
config['home'] = _normpath(home)
config['executable'] = _normpath(executable)
config['base_executable'] = _normpath(base_executable)
config['prefix'] = _normpath(prefix)
config['exec_prefix'] = _normpath(exec_prefix)
config['base_prefix'] = _normpath(base_prefix or prefix)
config['base_exec_prefix'] = _normpath(base_exec_prefix or exec_prefix)

config['platlibdir'] = platlibdir
config['platlibdir'] = _normpath(platlibdir)
# test_embed expects empty strings, not None
config['stdlib_dir'] = stdlib_dir or ''
config['platstdlib_dir'] = platstdlib_dir or ''
config['stdlib_dir'] = _normpath(stdlib_dir or '')
config['platstdlib_dir'] = _normpath(platstdlib_dir or '')

0 comments on commit 19d313b

Please sign in to comment.