From patchwork Wed Aug 14 00:05:11 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: 34079 Received: (qmail 29962 invoked by alias); 14 Aug 2019 00:05:17 -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 29952 invoked by uid 89); 14 Aug 2019 00:05:16 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-22.6 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*MI:google X-HELO: mail-yb1-f202.google.com Received: from mail-yb1-f202.google.com (HELO mail-yb1-f202.google.com) (209.85.219.202) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 14 Aug 2019 00:05:15 +0000 Received: by mail-yb1-f202.google.com with SMTP id o123so10809168ybo.7 for ; Tue, 13 Aug 2019 17:05:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:message-id:mime-version:subject:from:to:cc; bh=PpJisS/ENo97lqOQ2+cNFK65HiQagQqKgmgdTeLblE8=; b=g2akApdJI0GODB5jlBppKPJ5Zur3hTPCLlgTZ3tV6CcEXK7l9AkDs86vwMbNkuYpdy sdjUJOYCTIzCXHXvArWSphY/6kyITYBj3e5RgSFr5wGH/QDUZeN8O1Ay8DsS6ZthQ20P KyfjxFzxl+FzhlFIoGxoOT0wD0tRSsqe3yG5jEyD2ULN27M0yn9Wi+VnpqRv38XUtSzJ vs6DKTq37f+zEpz8rW0Pfw2ecYMUNSE/UExR6Bp112k9/gOCQ/QPBeiicFohMYGCQPw7 hWkFpwT7pavgpS84YvcPyjI6k7Czmq9IKpnUbZRtDK1Xt+jFiwxwpPp45oNMRIG6jLZ5 18ag== Date: Tue, 13 Aug 2019 19:05:11 -0500 Message-Id: <20190814000511.140810-1-cbiesinger@google.com> Mime-Version: 1.0 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 [Despite my comment on IRC, I made this approach work!] 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). 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 | 23 +++++++++++++++++++++++ gdb/python/python.c | 18 +++++++++++++----- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/gdb/python/lib/gdb/__init__.py b/gdb/python/lib/gdb/__init__.py index af74df80c8..f1adc5ffbe 100644 --- a/gdb/python/lib/gdb/__init__.py +++ b/gdb/python/lib/gdb/__init__.py @@ -106,6 +106,29 @@ 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 + 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. + exec(compile(file.read(), filepath, 'exec'), 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..18d0562212 100644 --- a/gdb/python/python.c +++ b/gdb/python/python.c @@ -339,14 +339,22 @@ 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 */ }