From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 12412 invoked by alias); 11 Jan 2012 20:51:56 -0000 Received: (qmail 12397 invoked by uid 22791); 11 Jan 2012 20:51:55 -0000 X-SWARE-Spam-Status: No, hits=-7.0 required=5.0 tests=AWL,BAYES_00,RCVD_IN_DNSWL_HI,SPF_HELO_PASS,T_RP_MATCHES_RCVD 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; Wed, 11 Jan 2012 20:51:38 +0000 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id q0BKpb1I013747 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Wed, 11 Jan 2012 15:51:37 -0500 Received: from ns3.rdu.redhat.com (ns3.rdu.redhat.com [10.11.255.199]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id q0BKpavU012959; Wed, 11 Jan 2012 15:51:36 -0500 Received: from barimba (ovpn01.gateway.prod.ext.phx2.redhat.com [10.5.9.1]) by ns3.rdu.redhat.com (8.13.8/8.13.8) with ESMTP id q0BKpZ0s007975; Wed, 11 Jan 2012 15:51:35 -0500 From: Tom Tromey To: Khoo Yit Phang Cc: gdb-patches@sourceware.org Subject: Re: Make the "python" command resemble the standard Python interpreter References: Date: Wed, 11 Jan 2012 20:56:00 -0000 In-Reply-To: (Khoo Yit Phang's message of "Tue, 10 Jan 2012 19:18:37 -0500") Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.0.92 (gnu/linux) 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-01/txt/msg00367.txt.bz2 >>>>> "Yit" == Khoo Yit Phang writes: Yit> I'd like to contribute a patch to improve the "python" command by Yit> making it resemble the standard Python interpreter in behavior. Thanks. Do you have a copyright assignment in place? If not, contact me off-list and I will get you started. We need this before we can incorporate this patch. Yit> For "python" with arguments, this prints the results of expressions, Yit> e.g., "python 1 + 2" will print "3" (previously, it will not print Yit> anything). Did you run the test suite? I'm curious if this caused any failures. On balance, I'm ok with this idea, since I don't think we make many promises about CLI output remaining the same across versions. Yit> For "python" without arguments, this uses Python's built-in Yit> interactive loop (PyRun_InteractiveLoop) so that individual Yit> statements/expressions are immediately evaluated and results of Yit> expressions are printed. Sounds nice. This also seems like it could cause some (spurious) test failures. Yit> It also hooks GDB's readline wrappers Yit> (command_line_input) to Python so that line editing and history Yit> editing works. Additionally, Python's standard readline module is Yit> stubbed out because it conflicts with GDB's use of readline. Can you expand more on how it conflicts? I think it would be better to make it not conflict somehow. Yit> + TRY_CATCH (except, RETURN_MASK_ALL) Yit> + { Yit> + struct cleanup *cleanup = gdbpy_suspend_sigint_handler (); Yit> + p = command_line_input (prompt, 0, "python"); Yit> + do_cleanups (cleanup); Yit> + } Yit> + Yit> + /* Detect Ctrl-C and treat as KeyboardInterrupt. */ Yit> + if (except.reason == RETURN_QUIT) Yit> + return NULL; Does this case need the Python exception to be set? If not, I think it would be good to expand this comment to explain the situation. Yit> + m = PyImport_AddModule ("__main__"); Yit> + if (m == NULL) Yit> + error (_("Error while executing Python code.")); You have to do something with the Python exception here. Usually we use gdbpy_print_stack, but sometimes other things are appropriate. Yit> + d = PyModule_GetDict (m); Do we need error checking? I didn't look at the API docs. Yit> + v = PyRun_StringFlags (command, Yit> + from_tty ? Py_single_input : Py_file_input, Yit> + d, d, NULL); Yit> + if (v == NULL) Yit> + { Yit> + int interrupt = PyErr_ExceptionMatches (PyExc_KeyboardInterrupt); Yit> + PyErr_Print (); gdbpy_print_stack. Yit> + if (! interrupt) Yit> + error (_("Error while executing Python code.")); Why the special case for interrupts here? Tom