Skip to content

Commit f6dec59

Browse files
committed
Ensure the debugger script is always closed
Previously it was leaked if `PyObject_AsFileDescriptor` failed.
1 parent 997b557 commit f6dec59

1 file changed

Lines changed: 18 additions & 19 deletions

File tree

Python/ceval_gil.c

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,43 +1198,42 @@ _PyEval_DisableGIL(PyThreadState *tstate)
11981198
static inline void run_remote_debugger_script(const char *path)
11991199
{
12001200
if (0 != PySys_Audit("remote_debugger_script", "s", path)) {
1201-
PyErr_FormatUnraisable("Error when auditing remote debugger script %s", path);
1201+
PyErr_FormatUnraisable(
1202+
"Audit hook failed for remote debugger script %s", path);
12021203
return;
12031204
}
12041205

1205-
// Open the debugger script with the open code hook. Unfortunately this forces us to handle
1206-
// the resulting Python object, which is a file object and therefore we need to call
1207-
// Python methods on it instead of the simpler C equivalents.
1206+
// Open the debugger script with the open code hook, and reopen the
1207+
// resulting file object to get a C FILE* object.
12081208
PyObject* fileobj = PyFile_OpenCode(path);
12091209
if (!fileobj) {
1210-
PyErr_FormatUnraisable("Error when opening debugger script %s", path);
1210+
PyErr_FormatUnraisable("Can't open debugger script %s", path);
12111211
return;
12121212
}
12131213

12141214
int fd = PyObject_AsFileDescriptor(fileobj);
12151215
if (fd == -1) {
1216-
PyErr_FormatUnraisable("Error when getting file descriptor for debugger script %s", path);
1217-
return;
1218-
}
1216+
PyErr_FormatUnraisable("Can't find fd for debugger script %s", path);
1217+
} else {
12191218
#ifdef MS_WINDOWS
1220-
FILE* f = _fdopen(fd, "r");
1219+
FILE* f = _fdopen(fd, "r");
12211220
#else
1222-
FILE* f = fdopen(fd, "r");
1221+
FILE* f = fdopen(fd, "r");
12231222
#endif
1223+
if (!f) {
1224+
PyErr_SetFromErrno(PyExc_OSError);
1225+
} else {
1226+
PyRun_AnyFile(f, path);
1227+
}
12241228

1225-
if (!f) {
1226-
PyErr_SetFromErrno(PyExc_OSError);
1227-
} else {
1228-
PyRun_AnyFile(f, path);
1229-
}
1230-
1231-
if (PyErr_Occurred()) {
1232-
PyErr_FormatUnraisable("Error executing debugger script %s", path);
1229+
if (PyErr_Occurred()) {
1230+
PyErr_FormatUnraisable("Error executing debugger script %s", path);
1231+
}
12331232
}
12341233

12351234
PyObject* res = PyObject_CallMethod(fileobj, "close", "");
12361235
if (!res) {
1237-
PyErr_FormatUnraisable("Error when closing debugger script %s", path);
1236+
PyErr_FormatUnraisable("Error closing debugger script %s", path);
12381237
} else {
12391238
Py_DECREF(res);
12401239
}

0 commit comments

Comments
 (0)