[Python-checkins] bpo-46048: Fix parsing of single character lines in getpath readlines() (GH-30048)
zooba
webhook-mailer at python.org
Sat Dec 11 08:43:48 EST 2021
https://github.com/python/cpython/commit/971ece8e1738b1107dda692cc44c6d8ddce384cd
commit: 971ece8e1738b1107dda692cc44c6d8ddce384cd
branch: main
author: Steve Dower <steve.dower at python.org>
committer: zooba <steve.dower at microsoft.com>
date: 2021-12-11T13:43:40Z
summary:
bpo-46048: Fix parsing of single character lines in getpath readlines() (GH-30048)
files:
A Misc/NEWS.d/next/Core and Builtins/2021-12-11-13-14-42.bpo-46048._-OGD9.rst
M Lib/test/test_site.py
M Modules/getpath.c
diff --git a/Lib/test/test_site.py b/Lib/test/test_site.py
index 76d35daed0b80..199022a1054f3 100644
--- a/Lib/test/test_site.py
+++ b/Lib/test/test_site.py
@@ -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)
diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-12-11-13-14-42.bpo-46048._-OGD9.rst b/Misc/NEWS.d/next/Core and Builtins/2021-12-11-13-14-42.bpo-46048._-OGD9.rst
new file mode 100644
index 0000000000000..647fb6df7a87b
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2021-12-11-13-14-42.bpo-46048._-OGD9.rst
@@ -0,0 +1,2 @@
+Fixes parsing of :file:`._pth` files on startup so that single-character
+paths are correctly read.
diff --git a/Modules/getpath.c b/Modules/getpath.c
index fedb41cdb3788..3adce4653342f 100644
--- a/Modules/getpath.c
+++ b/Modules/getpath.c
@@ -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);
More information about the Python-checkins
mailing list