Skip to content

Commit 90776ac

Browse files
Try to fix encoding on Windows.
1 parent daa818b commit 90776ac

File tree

2 files changed

+16
-18
lines changed

2 files changed

+16
-18
lines changed

Lib/test/test_sys.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2094,14 +2094,13 @@ def test_remote_exec_bytes(self):
20942094
@unittest.skipIf(sys.platform == 'darwin',
20952095
'undecodable paths are not supported on macOS')
20962096
def test_remote_exec_undecodable(self):
2097-
script = '''
2098-
print("Remote script executed successfully!")
2099-
'''
2097+
script = 'print("Remote script executed successfully!")'
21002098
script_path = os_helper.TESTFN_UNDECODABLE + b'_undecodable_remote.py'
2101-
returncode, stdout, stderr = self._run_remote_exec_test(script,
2102-
script_path=script_path)
2103-
self.assertIn(b"Remote script executed successfully!", stdout)
2104-
self.assertEqual(stderr, b"")
2099+
for script_path in [script_path, os.fsdecode(script_path)]:
2100+
returncode, stdout, stderr = self._run_remote_exec_test(script,
2101+
script_path=script_path)
2102+
self.assertIn(b"Remote script executed successfully!", stdout)
2103+
self.assertEqual(stderr, b"")
21052104

21062105
def test_remote_exec_with_self_process(self):
21072106
"""Test remote exec with the target process being the same as the test process"""

Python/sysmodule.c

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2483,25 +2483,28 @@ sys_remote_exec_impl(PyObject *module, int pid, PyObject *script)
24832483
{
24842484
PyObject *path;
24852485
const char *debugger_script_path;
2486-
#ifdef MS_WINDOWS
2487-
if (PyUnicode_FSDecoder(script, &path) < 0) {
2486+
2487+
if (PyUnicode_FSConverter(script, &path) < 0) {
24882488
return NULL;
24892489
}
2490-
debugger_script_path = PyUnicode_AsUTF8(path);
2491-
if (debugger_script_path == NULL) {
2490+
debugger_script_path = PyBytes_AS_STRING(path);
2491+
#ifdef MS_WINDOWS
2492+
PyObject *unicode_path;
2493+
if (PyUnicode_FSDecoder(path, &unicode_path) < 0) {
24922494
Py_DECREF(path);
24932495
return NULL;
24942496
}
24952497
// Use UTF-16 (wide char) version of the path for permission checks
2496-
wchar_t *debugger_script_path_w = PyUnicode_AsWideCharString(path, NULL);
2498+
wchar_t *debugger_script_path_w = PyUnicode_AsWideCharString(unicode_path, NULL);
2499+
Py_DECREF(unicode_path);
24972500
if (debugger_script_path_w == NULL) {
24982501
Py_DECREF(path);
24992502
return NULL;
25002503
}
25012504
DWORD attr = GetFileAttributesW(debugger_script_path_w);
2502-
PyMem_Free(debugger_script_path_w);
25032505
if (attr == INVALID_FILE_ATTRIBUTES) {
25042506
DWORD err = GetLastError();
2507+
PyMem_Free(debugger_script_path_w);
25052508
if (err == ERROR_FILE_NOT_FOUND || err == ERROR_PATH_NOT_FOUND) {
25062509
PyErr_SetString(PyExc_FileNotFoundError, "Script file does not exist");
25072510
}
@@ -2514,12 +2517,8 @@ sys_remote_exec_impl(PyObject *module, int pid, PyObject *script)
25142517
Py_DECREF(path);
25152518
return NULL;
25162519
}
2520+
PyMem_Free(debugger_script_path_w);
25172521
#else // MS_WINDOWS
2518-
if (PyUnicode_FSConverter(script, &path) < 0) {
2519-
return NULL;
2520-
}
2521-
debugger_script_path = PyBytes_AS_STRING(path);
2522-
25232522
if (access(debugger_script_path, F_OK | R_OK) != 0) {
25242523
switch (errno) {
25252524
case ENOENT:

0 commit comments

Comments
 (0)