@@ -1211,41 +1211,9 @@ static inline void run_remote_debugger_script(const char *path)
12111211 return ;
12121212 }
12131213
1214- int fd = PyObject_AsFileDescriptor (fileobj );
1215- if (fd == -1 ) {
1216- PyErr_FormatUnraisable ("Can't find fd for debugger script %s" , path );
1217- }
1218- else {
1219- int dup_fd = -1 ;
1220- FILE * f = NULL ;
1221-
1222- #ifdef MS_WINDOWS
1223- dup_fd = _dup (fd );
1224- if (dup_fd != -1 ) {
1225- f = _fdopen (dup_fd , "r" );
1226- }
1227- if (!f ) {
1228- _close (dup_fd );
1229- }
1230- #else
1231- dup_fd = dup (fd );
1232- if (dup_fd != -1 ) {
1233- f = fdopen (dup_fd , "r" );
1234- }
1235- if (!f ) {
1236- close (dup_fd );
1237- }
1238- #endif
1239- if (!f ) {
1240- PyErr_SetFromErrno (PyExc_OSError );
1241- }
1242- else {
1243- PyRun_AnyFileEx (f , path , 1 );
1244- }
1245-
1246- if (PyErr_Occurred ()) {
1247- PyErr_FormatUnraisable ("Error executing debugger script %s" , path );
1248- }
1214+ PyObject * source = PyObject_CallMethodNoArgs (fileobj , & _Py_ID (read ));
1215+ if (!source ) {
1216+ PyErr_FormatUnraisable ("Error reading debugger script %s" , path );
12491217 }
12501218
12511219 PyObject * res = PyObject_CallMethodNoArgs (fileobj , & _Py_ID (close ));
@@ -1255,6 +1223,24 @@ static inline void run_remote_debugger_script(const char *path)
12551223 Py_DECREF (res );
12561224 }
12571225 Py_DECREF (fileobj );
1226+
1227+ if (source ) {
1228+ const char * str = PyBytes_AsString (source );
1229+ if (str ) {
1230+ // PyRun_SimpleString() automatically raises an unraisable
1231+ // exception if it fails so we don't need to check the return value.
1232+ PyRun_SimpleString (str );
1233+ } else {
1234+ PyErr_FormatUnraisable ("Error reading debugger script %s" , path );
1235+ }
1236+ Py_DECREF (source );
1237+ }
1238+
1239+ // Just in case something went wrong, don't leave this function
1240+ // with an unhandled exception.
1241+ if (PyErr_Occurred ()) {
1242+ PyErr_FormatUnraisable ("Error executing debugger script %s" , path );
1243+ }
12581244}
12591245#endif
12601246
0 commit comments