Skip to content
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

bpo-46048: Fix parsing of single character lines in getpath readlines() #30048

Merged
merged 1 commit into from
Dec 11, 2021
Merged
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
20 changes: 20 additions & 0 deletions Lib/test/test_site.py
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,26 @@ def _calc_sys_path_for_underpth_nosite(self, sys_prefix, lines):
sys_path.append(abs_path)
return sys_path

def test_underpth_basic(self):
libpath = test.support.STDLIB_DIR
exe_prefix = os.path.dirname(sys.executable)
pth_lines = ['#.', '# ..', *sys.path, '.', '..']
exe_file = self._create_underpth_exe(pth_lines)
sys_path = self._calc_sys_path_for_underpth_nosite(
os.path.dirname(exe_file),
pth_lines)

output = subprocess.check_output([exe_file, '-c',
'import sys; print("\\n".join(sys.path) if sys.flags.no_site else "")'
], encoding='ansi')
actual_sys_path = output.rstrip().split('\n')
self.assertTrue(actual_sys_path, "sys.flags.no_site was False")
self.assertEqual(
actual_sys_path,
sys_path,
"sys.path is incorrect"
)

def test_underpth_nosite_file(self):
libpath = test.support.STDLIB_DIR
exe_prefix = os.path.dirname(sys.executable)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixes parsing of :file:`._pth` files on startup so that single-character
paths are correctly read.
6 changes: 3 additions & 3 deletions Modules/getpath.c
Original file line number Diff line number Diff line change
Expand Up @@ -386,11 +386,11 @@ getpath_readlines(PyObject *Py_UNUSED(self), PyObject *args)
wchar_t *p1 = wbuffer;
wchar_t *p2 = p1;
while ((p2 = wcschr(p1, L'\n')) != NULL) {
size_t cb = p2 - p1;
while (cb && (p1[cb] == L'\n' || p1[cb] == L'\r')) {
Py_ssize_t cb = p2 - p1;
while (cb >= 0 && (p1[cb] == L'\n' || p1[cb] == L'\r')) {
--cb;
}
PyObject *u = PyUnicode_FromWideChar(p1, cb ? cb + 1 : 0);
PyObject *u = PyUnicode_FromWideChar(p1, cb >= 0 ? cb + 1 : 0);
if (!u || PyList_Append(r, u) < 0) {
Py_XDECREF(u);
Py_CLEAR(r);
Expand Down