From patchwork Thu Aug 15 17:15:41 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Terekhov, Mikhail via Gdb-patches" X-Patchwork-Id: 34122 Received: (qmail 49955 invoked by alias); 15 Aug 2019 17:15:52 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 49885 invoked by uid 89); 15 Aug 2019 17:15:48 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-22.7 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.1 spammy=H*f:sk:CAPTJ0X, H*i:sk:CAPTJ0X X-HELO: mail-yw1-f73.google.com Received: from mail-yw1-f73.google.com (HELO mail-yw1-f73.google.com) (209.85.161.73) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 15 Aug 2019 17:15:45 +0000 Received: by mail-yw1-f73.google.com with SMTP id n139so2114944ywd.22 for ; Thu, 15 Aug 2019 10:15:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=y9g1wPL+DTcldokJxWEDGPnX6FDO+4iGR95y1Dq3sSE=; b=nHMlWWzrTloll91OhyqDlkb1Av1CodOZWqsjaY3ep0UBni5TpoKrpsUd5WUF5/bseI 3oj08l9+5Gd0t8X/gYbM8I20fpQajzN8pphB5TnB+f366EL+pA5gVviXpP2P7+dZSNEk NfKLLi2XAQ+y8FEmwxQ5NJBiA9qG0r0dW/uiSd1TrVsVRq/kicqRxg8B+Fl2oL5tLwqs RFYEuWLxkKQia/xrPwB3D+0Ea5gpCl6anJIwhYcbATt9gCa39Wc5Cq9HRKMClQB4JPDX AsMkuTeCflPnBRDIxMBG/FfNua9rXTsHEA6Fv4y8lOHG5GpD7GvDPO/6oNdUtRhsoRsy jeSQ== Date: Thu, 15 Aug 2019 12:15:41 -0500 In-Reply-To: Message-Id: <20190815171541.134519-1-cbiesinger@google.com> Mime-Version: 1.0 References: Subject: [PATCH] Make GDB compile with Python 3 on MinGW X-Patchwork-Original-From: "Christian Biesinger via gdb-patches" From: "Terekhov, Mikhail via Gdb-patches" Reply-To: Christian Biesinger To: gdb-patches@sourceware.org Cc: Christian Biesinger X-IsSubscribed: yes [Updated per Simon's comments] PyFile_FromString and PyFile_AsFile have been removed in Python 3. There is no obvious replacement that works here, and we can't just pass our FILE* to a DLL in Windows because it may use a different C runtime. So we just call a Python function which reads and executes file contents. Care must be taken to execute it in the context of __main__. Tested by inverting the ifdef and running the testsuite on Debian Linux (even without the patch, I failed at running the testsuite on Windows). I did test with both Python 2 and 3. gdb/ChangeLog: 2019-08-13 Christian Biesinger * python/lib/gdb/__init__.py: Add an execute_file function. * python/python.c (python_run_simple_file): Call gdb.execute_file on Windows. --- gdb/python/lib/gdb/__init__.py | 26 ++++++++++++++++++++++++++ gdb/python/python.c | 24 ++++++++++++++++-------- 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/gdb/python/lib/gdb/__init__.py b/gdb/python/lib/gdb/__init__.py index af74df80c8..d1a49499b0 100644 --- a/gdb/python/lib/gdb/__init__.py +++ b/gdb/python/lib/gdb/__init__.py @@ -106,6 +106,32 @@ def execute_unwinders(pending_frame): return None +def _execute_file(filepath): + """This function is used to replace Python 2's PyRun_SimpleFile. + + Loads and executes the given file. + + We could use the runpy module, but its documentation says: + "Furthermore, any functions and classes defined by the executed code are + not guaranteed to work correctly after a runpy function has returned." + """ + globals = sys.modules['__main__'].__dict__ + set_file = False + # Set file (if not set) so that the imported file can use it (e.g. to + # access file-relative paths). This matches what PyRun_SimpleFile does. + if not hasattr(globals, '__file__'): + globals['__file__'] = filepath + set_file = True + try: + with open(filepath, 'rb') as file: + # We pass globals also as locals to match what Python does + # in PyRun_SimpleFile. + compiled = compile(file.read(), filepath, 'exec') + exec(compiled, globals, globals) + finally: + if set_file: + del globals['__file__'] + # Convenience variable to GDB's python directory PYTHONDIR = os.path.dirname(os.path.dirname(__file__)) diff --git a/gdb/python/python.c b/gdb/python/python.c index 162470dcc0..0dd8dd5f35 100644 --- a/gdb/python/python.c +++ b/gdb/python/python.c @@ -323,9 +323,8 @@ python_interactive_command (const char *arg, int from_tty) A FILE * from one runtime does not necessarily operate correctly in the other runtime. - To work around this potential issue, we create on Windows hosts the - FILE object using Python routines, thus making sure that it is - compatible with the Python library. */ + To work around this potential issue, we run code in Python to load + the script. */ static void python_run_simple_file (FILE *file, const char *filename) @@ -339,14 +338,23 @@ python_run_simple_file (FILE *file, const char *filename) /* Because we have a string for a filename, and are using Python to open the file, we need to expand any tilde in the path first. */ gdb::unique_xmalloc_ptr full_path (tilde_expand (filename)); - gdbpy_ref<> python_file (PyFile_FromString (full_path.get (), (char *) "r")); - if (python_file == NULL) + + if (gdb_python_module == nullptr + || ! PyObject_HasAttrString (gdb_python_module, "_execute_file")) { - gdbpy_print_stack (); - error (_("Error while opening file: %s"), full_path.get ()); + error (_("Installation error: gdb.execute_file function is missing")); + return; } - PyRun_SimpleFile (PyFile_AsFile (python_file.get ()), filename); + gdbpy_ref<> return_value + (PyObject_CallMethod (gdb_python_module, "_execute_file", "s", + full_path.get ())); + if (return_value == nullptr) + { + /* Use PyErr_PrintEx instead of gdbpy_print_stack to better match the + behavior of the non-Windows codepath. */ + PyErr_PrintEx(0); + } #endif /* _WIN32 */ }