From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 10075 invoked by alias); 6 Sep 2012 15:43:44 -0000 Received: (qmail 10062 invoked by uid 22791); 6 Sep 2012 15:43:42 -0000 X-SWARE-Spam-Status: No, hits=-6.5 required=5.0 tests=AWL,BAYES_00,KHOP_RCVD_UNTRUST,RCVD_IN_DNSWL_HI,RCVD_IN_HOSTKARMA_W,RP_MATCHES_RCVD,SPF_HELO_PASS X-Spam-Check-By: sourceware.org Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Thu, 06 Sep 2012 15:43:28 +0000 Received: from int-mx12.intmail.prod.int.phx2.redhat.com (int-mx12.intmail.prod.int.phx2.redhat.com [10.5.11.25]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id q86FhSM5018423 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Thu, 6 Sep 2012 11:43:28 -0400 Received: from barimba (ovpn01.gateway.prod.ext.phx2.redhat.com [10.5.9.1]) by int-mx12.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id q86FhQoI027067 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES128-SHA bits=128 verify=NO); Thu, 6 Sep 2012 11:43:27 -0400 From: Tom Tromey To: gdb-patches@sourceware.org Subject: RFC: fix atexit.register Date: Thu, 06 Sep 2012 15:43:00 -0000 Message-ID: <87d31znom9.fsf@fleche.redhat.com> MIME-Version: 1.0 Content-Type: text/plain Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org X-SW-Source: 2012-09/txt/msg00046.txt.bz2 A user on irc reported that Python's atexit.register did not work properly in gdb. This patch fixes the problem by installing a final cleanup that calls Py_Finalize. Regression tested on x86-64 F16. New test case included. Tom * NEWS: Update. * python/python.c (finalize_python): New function. (_initialize_python): Make a final cleanup. * gdb.python/python.exp: Test atexit.register. --- gdb/NEWS | 2 ++ gdb/python/python.c | 14 ++++++++++++++ gdb/testsuite/gdb.python/python.exp | 18 ++++++++++++++++++ 3 files changed, 34 insertions(+), 0 deletions(-) diff --git a/gdb/NEWS b/gdb/NEWS index e625e25..9571bcf 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -10,6 +10,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 cb60877..a75a421 100644 --- a/gdb/python/python.c +++ b/gdb/python/python.c @@ -1396,6 +1396,19 @@ 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) +{ + struct cleanup *cleanup = ensure_python_env (target_gdbarch, + current_language); + + Py_Finalize (); + do_cleanups (cleanup); +} + /* Provide a prototype to silence -Wmissing-prototypes. */ extern initialize_file_ftype _initialize_python; @@ -1573,6 +1586,7 @@ gdb.type_printers = []\n\ PyThreadState_Swap (NULL); PyEval_ReleaseLock (); + make_final_cleanup (finalize_python, NULL); #endif /* HAVE_PYTHON */ } diff --git a/gdb/testsuite/gdb.python/python.exp b/gdb/testsuite/gdb.python/python.exp index 9683b1c..177f94e 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} -- 1.7.7.6