From d7de8e3ce962db7df6f4107101a87f53129e98eb Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Thu, 20 Sep 2012 20:54:11 +0000 Subject: [PATCH] * NEWS: Update. * python/python.c (finalize_python): New function. (_initialize_python): Make a final cleanup. testsuite * gdb.python/python.exp: Test atexit.register. --- gdb/ChangeLog | 6 ++++++ gdb/NEWS | 2 ++ gdb/python/python.c | 19 +++++++++++++++++++ gdb/testsuite/ChangeLog | 4 ++++ gdb/testsuite/gdb.python/python.exp | 18 ++++++++++++++++++ 5 files changed, 49 insertions(+) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index d36c95e..b11ac7c 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,9 @@ +2012-09-20 Tom Tromey + + * NEWS: Update. + * python/python.c (finalize_python): New function. + (_initialize_python): Make a final cleanup. + 2012-09-19 Doug Evans * buildsym.h (param_symbols): Delete, unused. diff --git a/gdb/NEWS b/gdb/NEWS index 365b2d4..86f57d8 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -7,6 +7,8 @@ ** Vectors can be created with gdb.Type.vector. + ** Python's atexit.register now works in GDB. + * New Python-based convenience functions: ** $_memeq(buf1, buf2, length) diff --git a/gdb/python/python.c b/gdb/python/python.c index 3cfb284..c66dc60 100644 --- a/gdb/python/python.c +++ b/gdb/python/python.c @@ -1266,6 +1266,24 @@ user_show_python (char *args, int from_tty) /* Initialize the Python code. */ +/* This is installed as a final cleanup and cleans up the + interpreter. This lets Python's 'atexit' work. */ + +static void +finalize_python (void *ignore) +{ + /* We don't use ensure_python_env here because if we ever ran the + cleanup, gdb would crash -- because the cleanup calls into the + Python interpreter, which we are about to destroy. It seems + clearer to make the needed calls explicitly here than to create a + cleanup and then mysteriously discard it. */ + PyGILState_Ensure (); + python_gdbarch = target_gdbarch; + python_language = current_language; + + Py_Finalize (); +} + /* Provide a prototype to silence -Wmissing-prototypes. */ extern initialize_file_ftype _initialize_python; @@ -1427,6 +1445,7 @@ message == an error message without a stack will be printed."), PyThreadState_Swap (NULL); PyEval_ReleaseLock (); + make_final_cleanup (finalize_python, NULL); #endif /* HAVE_PYTHON */ } diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog index b7e0f0b..789b49d 100644 --- a/gdb/testsuite/ChangeLog +++ b/gdb/testsuite/ChangeLog @@ -1,3 +1,7 @@ +2012-09-20 Tom Tromey + + * gdb.python/python.exp: Test atexit.register. + 2012-09-20 Doug Evans * boards/dwarf4-gdb-index.exp: New file. diff --git a/gdb/testsuite/gdb.python/python.exp b/gdb/testsuite/gdb.python/python.exp index 9f29e3a..6f0ce1c 100644 --- a/gdb/testsuite/gdb.python/python.exp +++ b/gdb/testsuite/gdb.python/python.exp @@ -141,6 +141,24 @@ gdb_test "python print a" ".*aliases -- Aliases of other commands.*" "verify hel gdb_py_test_silent_cmd "python nothread = gdb.selected_thread()" "Attempt to aquire thread with no inferior" 1 gdb_test "python print nothread == None" "True" "Ensure that no threads are returned" +gdb_py_test_multiple "register atexit function" \ + "python" "" \ + "import atexit" "" \ + "def printit(arg):" "" \ + " print arg" "" \ + "atexit.register(printit, 'good bye world')" "" \ + "end" "" + +send_gdb "quit\n" +gdb_expect { + -re "good bye world" { + pass "atexit handling" + } + default { + fail "atexit handling" + } +} + # Start with a fresh gdb. clean_restart ${testfile} -- 2.7.4