Skip to content

Commit 4af1744

Browse files
committed
Simplify handling of Windows paths
We can clean this up by freeing strings as soon as we're done with them, and remove some duplicate code.
1 parent c9a2146 commit 4af1744

File tree

1 file changed

+8
-17
lines changed

1 file changed

+8
-17
lines changed

Python/sysmodule.c

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2470,19 +2470,23 @@ static PyObject *
24702470
sys_remote_exec_impl(PyObject *module, int pid, PyObject *script)
24712471
/*[clinic end generated code: output=7d94c56afe4a52c0 input=5749b0253d5b588c]*/
24722472
{
2473+
const char *debugger_script_path = PyUnicode_AsUTF8(script);
2474+
if (debugger_script_path == NULL) {
2475+
return NULL;
2476+
}
2477+
24732478
#ifdef MS_WINDOWS
2474-
// Get UTF-16 (wide char) version of the path for Windows
2479+
// Use UTF-16 (wide char) version of the path for permission checks
24752480
wchar_t *debugger_script_path_w = PyUnicode_AsWideCharString(script, NULL);
24762481
if (debugger_script_path_w == NULL) {
24772482
return NULL;
24782483
}
24792484

24802485
// Check file attributes using wide character version (W) instead of ANSI (A)
24812486
DWORD attr = GetFileAttributesW(debugger_script_path_w);
2487+
PyMem_Free(debugger_script_path_w);
24822488
if (attr == INVALID_FILE_ATTRIBUTES) {
24832489
DWORD err = GetLastError();
2484-
PyMem_Free(debugger_script_path_w);
2485-
24862490
if (err == ERROR_FILE_NOT_FOUND || err == ERROR_PATH_NOT_FOUND) {
24872491
PyErr_SetString(PyExc_FileNotFoundError, "Script file does not exist");
24882492
}
@@ -2494,21 +2498,7 @@ sys_remote_exec_impl(PyObject *module, int pid, PyObject *script)
24942498
}
24952499
return NULL;
24962500
}
2497-
2498-
// Get UTF-8 version for the rest of the code
2499-
const char *debugger_script_path = PyUnicode_AsUTF8(script);
2500-
if (debugger_script_path == NULL) {
2501-
PyMem_Free(debugger_script_path_w);
2502-
return NULL;
2503-
}
2504-
2505-
PyMem_Free(debugger_script_path_w);
25062501
#else
2507-
const char *debugger_script_path = PyUnicode_AsUTF8(script);
2508-
if (debugger_script_path == NULL) {
2509-
return NULL;
2510-
}
2511-
25122502
if (access(debugger_script_path, F_OK | R_OK) != 0) {
25132503
switch (errno) {
25142504
case ENOENT:
@@ -2523,6 +2513,7 @@ sys_remote_exec_impl(PyObject *module, int pid, PyObject *script)
25232513
return NULL;
25242514
}
25252515
#endif
2516+
25262517
if (_PySysRemoteDebug_SendExec(pid, 0, debugger_script_path) < 0) {
25272518
return NULL;
25282519
}

0 commit comments

Comments
 (0)