FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[3.12] gh-86179: Implement realpath() on Windows for getpath.py calculations (GH-113033) by zooba · Pull Request #113121 · python/cpython · GitHub

Repository navigation

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .py  (1) .rst  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
37 changes: 31 additions & 6 deletions Lib/test/test_venv.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,18 @@ def check_output(cmd, encoding=None):
p = subprocess.Popen(cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding=encoding)
env={**os.environ, "PYTHONHOME": ""})
out, err = p.communicate()
if p.returncode:
if verbose and err:
print(err.decode('utf-8', 'backslashreplace'))
print(err.decode(encoding or 'utf-8', 'backslashreplace'))
raise subprocess.CalledProcessError(
p.returncode, cmd, out, err)
if encoding:
return (
out.decode(encoding, 'backslashreplace'),
err.decode(encoding, 'backslashreplace'),
)
return out, err

class BaseTest(unittest.TestCase):
Expand Down Expand Up @@ -273,8 +278,18 @@ def test_sysconfig(self):
('get_config_h_filename()', sysconfig.get_config_h_filename())):
with self.subTest(call):
cmd[2] = 'import sysconfig; print(sysconfig.%s)' % call
out, err = check_output(cmd)
self.assertEqual(out.strip(), expected.encode(), err)
out, err = check_output(cmd, encoding='utf-8')
self.assertEqual(out.strip(), expected, err)
for attr, expected in (
('executable', envpy),
# Usually compare to sys.executable, but if we're running in our own
# venv then we really need to compare to our base executable
('_base_executable', sys._base_executable),
):
with self.subTest(attr):
cmd[2] = f'import sys; print(sys.{attr})'
out, err = check_output(cmd, encoding='utf-8')
self.assertEqual(out.strip(), expected, err)

@requireVenvCreate
@unittest.skipUnless(can_symlink(), 'Needs symlinks')
Expand All @@ -296,8 +311,18 @@ def test_sysconfig_symlinks(self):
('get_config_h_filename()', sysconfig.get_config_h_filename())):
with self.subTest(call):
cmd[2] = 'import sysconfig; print(sysconfig.%s)' % call
out, err = check_output(cmd)
self.assertEqual(out.strip(), expected.encode(), err)
out, err = check_output(cmd, encoding='utf-8')
self.assertEqual(out.strip(), expected, err)
for attr, expected in (
('executable', envpy),
# Usually compare to sys.executable, but if we're running in our own
# venv then we really need to compare to our base executable
('_base_executable', sys._base_executable),
):
with self.subTest(attr):
cmd[2] = f'import sys; print(sys.{attr})'
out, err = check_output(cmd, encoding='utf-8')
self.assertEqual(out.strip(), expected, err)

if sys.platform == 'win32':
ENV_SUBDIRS = (
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixes path calculations when launching Python on Windows through a symlink.
48 changes: 48 additions & 0 deletions Modules/getpath.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,54 @@ getpath_realpath(PyObject *Py_UNUSED(self) , PyObject *args)
PyMem_Free((void *)path);
PyMem_Free((void *)narrow);
return r;
#elif defined(MS_WINDOWS)
HANDLE hFile;
wchar_t resolved[MAXPATHLEN+1];
int len = 0, err;
Py_ssize_t pathlen;
PyObject *result;

wchar_t *path = PyUnicode_AsWideCharString(pathobj, &pathlen);
if (!path) {
return NULL;
}
if (wcslen(path) != pathlen) {
PyErr_SetString(PyExc_ValueError, "path contains embedded nulls");
return NULL;
}

Py_BEGIN_ALLOW_THREADS
hFile = CreateFileW(path, 0, 0, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
if (hFile != INVALID_HANDLE_VALUE) {
len = GetFinalPathNameByHandleW(hFile, resolved, MAXPATHLEN, VOLUME_NAME_DOS);
err = len ? 0 : GetLastError();
CloseHandle(hFile);
} else {
err = GetLastError();
}
Py_END_ALLOW_THREADS

if (err) {
PyErr_SetFromWindowsErr(err);
result = NULL;
} else if (len <= MAXPATHLEN) {
const wchar_t *p = resolved;
if (0 == wcsncmp(p, L"\\\\?\\", 4)) {
if (GetFileAttributesW(&p[4]) != INVALID_FILE_ATTRIBUTES) {
p += 4;
len -= 4;
}
}
if (CompareStringOrdinal(path, (int)pathlen, p, len, TRUE) == CSTR_EQUAL) {
result = Py_NewRef(pathobj);
} else {
result = PyUnicode_FromWideChar(p, len);
}
} else {
result = Py_NewRef(pathobj);
}
PyMem_Free(path);
return result;
#endif

return Py_NewRef(pathobj);
Expand Down

Back | FazBrowse Home | New Git URL