@@ -284,12 +284,14 @@ gdbpy_check_quit_flag (const struct extension_language_defn *extlang)
284284 return PyOS_InterruptOccurred ();
285285}
286286
287- /* Evaluate a Python command like PyRun_SimpleString, but uses
288- Py_single_input which prints the result of expressions, and does
289- not automatically print the stack on errors. */
287+ /* Evaluate a Python command like PyRun_SimpleString, but takes a
288+ Python start symbol, and does not automatically print the stack on
289+ errors. FILENAME is used to set the file name in error
290+ messages. */
290291
291292static int
292- eval_python_command (const char * command )
293+ eval_python_command (const char * command , int start_symbol ,
294+ const char * filename = "<string>" )
293295{
294296 PyObject * m , * d ;
295297
@@ -300,8 +302,15 @@ eval_python_command (const char *command)
300302 d = PyModule_GetDict (m );
301303 if (d == NULL )
302304 return -1 ;
303- gdbpy_ref < > v (PyRun_StringFlags (command , Py_single_input , d , d , NULL ));
304- if (v == NULL )
305+
306+ /* Use this API because it is in Python 3.2. */
307+ gdbpy_ref < > code (Py_CompileStringExFlags (command , filename , start_symbol ,
308+ nullptr , -1 ));
309+ if (code == nullptr )
310+ return -1 ;
311+
312+ gdbpy_ref < > result (PyEval_EvalCode (code .get (), d , d ));
313+ if (result == nullptr )
305314 return -1 ;
306315
307316 return 0 ;
@@ -324,7 +333,8 @@ python_interactive_command (const char *arg, int from_tty)
324333 if (arg && * arg )
325334 {
326335 std ::string script = std ::string (arg ) + "\n" ;
327- err = eval_python_command (script .c_str ());
336+ /* Py_single_input causes the result to be displayed. */
337+ err = eval_python_command (script .c_str (), Py_single_input );
328338 }
329339 else
330340 {
@@ -333,14 +343,12 @@ python_interactive_command (const char *arg, int from_tty)
333343 }
334344
335345 if (err )
336- {
337- gdbpy_print_stack ();
338- error (_ ("Error while executing Python code." ));
339- }
346+ gdbpy_handle_exception ();
340347}
341348
342- /* A wrapper around PyRun_SimpleFile. FILE is the Python script to run
343- named FILENAME.
349+ /* Like PyRun_SimpleFile, but if there is an exception, it is not
350+ automatically displayed. FILE is the Python script to run named
351+ FILENAME.
344352
345353 On Windows hosts few users would build Python themselves (this is no
346354 trivial task on this platform), and thus use binaries built by
@@ -349,39 +357,13 @@ python_interactive_command (const char *arg, int from_tty)
349357 library. Python, being built with VC, would use one version of the
350358 msvcr DLL (Eg. msvcr100.dll), while MinGW uses msvcrt.dll.
351359 A FILE * from one runtime does not necessarily operate correctly in
352- the other runtime.
360+ the other runtime. */
353361
354- To work around this potential issue, we run code in Python to load
355- the script. */
356-
357- static void
362+ static int
358363python_run_simple_file (FILE * file , const char * filename )
359364{
360- #ifndef _WIN32
361-
362- PyRun_SimpleFile (file , filename );
363-
364- #else /* _WIN32 */
365-
366- /* Because we have a string for a filename, and are using Python to
367- open the file, we need to expand any tilde in the path first. */
368- gdb ::unique_xmalloc_ptr < char > full_path (tilde_expand (filename ));
369-
370- if (gdb_python_module == nullptr
371- || ! PyObject_HasAttrString (gdb_python_module , "_execute_file" ))
372- error (_ ("Installation error: gdb._execute_file function is missing" ));
373-
374- gdbpy_ref < > return_value
375- (PyObject_CallMethod (gdb_python_module , "_execute_file" , "s" ,
376- full_path .get ()));
377- if (return_value == nullptr )
378- {
379- /* Use PyErr_PrintEx instead of gdbpy_print_stack to better match the
380- behavior of the non-Windows codepath. */
381- PyErr_PrintEx (0 );
382- }
383-
384- #endif /* _WIN32 */
365+ std ::string contents = read_remainder_of_file (file );
366+ return eval_python_command (contents .c_str (), Py_file_input , filename );
385367}
386368
387369/* Given a command_line, return a command string suitable for passing
@@ -408,17 +390,15 @@ static void
408390gdbpy_eval_from_control_command (const struct extension_language_defn * extlang ,
409391 struct command_line * cmd )
410392{
411- int ret ;
412-
413393 if (cmd -> body_list_1 != nullptr )
414394 error (_ ("Invalid \"python\" block structure." ));
415395
416396 gdbpy_enter enter_py ;
417397
418398 std ::string script = compute_python_string (cmd -> body_list_0 .get ());
419- ret = PyRun_SimpleString (script .c_str ());
420- if (ret )
421- error ( _ ( "Error while executing Python code." ) );
399+ int ret = eval_python_command (script .c_str (), Py_file_input );
400+ if (ret != 0 )
401+ gdbpy_handle_exception ( );
422402}
423403
424404/* Implementation of the gdb "python" command. */
@@ -433,8 +413,9 @@ python_command (const char *arg, int from_tty)
433413 arg = skip_spaces (arg );
434414 if (arg && * arg )
435415 {
436- if (PyRun_SimpleString (arg ))
437- error (_ ("Error while executing Python code." ));
416+ int ret = eval_python_command (arg , Py_file_input );
417+ if (ret != 0 )
418+ gdbpy_handle_exception ();
438419 }
439420 else
440421 {
@@ -1050,7 +1031,9 @@ gdbpy_source_script (const struct extension_language_defn *extlang,
10501031 FILE * file , const char * filename )
10511032{
10521033 gdbpy_enter enter_py ;
1053- python_run_simple_file (file , filename );
1034+ int result = python_run_simple_file (file , filename );
1035+ if (result != 0 )
1036+ gdbpy_handle_exception ();
10541037}
10551038
10561039
@@ -1682,7 +1665,9 @@ gdbpy_source_objfile_script (const struct extension_language_defn *extlang,
16821665 scoped_restore restire_current_objfile
16831666 = make_scoped_restore (& gdbpy_current_objfile , objfile );
16841667
1685- python_run_simple_file (file , filename );
1668+ int result = python_run_simple_file (file , filename );
1669+ if (result != 0 )
1670+ gdbpy_print_stack ();
16861671}
16871672
16881673/* Set the current objfile to OBJFILE and then execute SCRIPT
@@ -1703,7 +1688,9 @@ gdbpy_execute_objfile_script (const struct extension_language_defn *extlang,
17031688 scoped_restore restire_current_objfile
17041689 = make_scoped_restore (& gdbpy_current_objfile , objfile );
17051690
1706- PyRun_SimpleString (script );
1691+ int ret = eval_python_command (script , Py_file_input );
1692+ if (ret != 0 )
1693+ gdbpy_print_stack ();
17071694}
17081695
17091696/* Return the current Objfile, or None if there isn't one. */
@@ -2361,21 +2348,15 @@ test_python ()
23612348 {
23622349 CMD (output );
23632350 }
2364- catch (const gdb_exception & e )
2351+ catch (const gdb_exception_quit & e )
23652352 {
23662353 saw_exception = true;
2367- SELF_CHECK (e .reason == RETURN_ERROR );
2368- SELF_CHECK (e .error == GENERIC_ERROR );
2369- SELF_CHECK (* e .message == "Error while executing Python code. " );
2354+ SELF_CHECK (e .reason == RETURN_QUIT );
2355+ SELF_CHECK (e .error == GDB_NO_ERROR );
2356+ SELF_CHECK (* e .message == "Quit " );
23702357 }
23712358 SELF_CHECK (saw_exception );
2372- std ::string ref_output_0 ("Traceback (most recent call last):\n"
2373- " File \"<string>\", line 0, in <module>\n"
2374- "KeyboardInterrupt\n" );
2375- std ::string ref_output_1 ("Traceback (most recent call last):\n"
2376- " File \"<string>\", line 1, in <module>\n"
2377- "KeyboardInterrupt\n" );
2378- SELF_CHECK (output == ref_output_0 || output == ref_output_1 );
2359+ SELF_CHECK (output .empty ());
23792360 }
23802361
23812362#undef CMD
0 commit comments