[PATCH] python: Make Windows-specific code work with Python 3

Jan Vrany jan.vrany@fit.cvut.cz
Fri Oct 26 09:35:00 GMT 2018


Windows workaround in python_run_simple_file() used Python 2
APIs which were removed in Python 3. This commit adds a
conditionally compiled variant that uses Python 3 APIs.

gdb/ChangeLog:

	* python/python.c (python_run_simple_file): Fix for
	Python 3.
---
 gdb/ChangeLog       |  5 +++++
 gdb/python/python.c | 13 +++++++++++--
 2 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index b3cc64659f..5cdf6d2600 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,8 @@
+2018-16-11  Jan Vrany <jan.vrany@fit.cvut.cz>
+
+	* python/python.c (python_run_simple_file): Fix for
+	Python 3.
+
 2018-10-11  Gary Benson <gbenson@redhat.com>
 
 	* interps.h (interp::m_name): Make private and mutable.
diff --git a/gdb/python/python.c b/gdb/python/python.c
index 8fbce78469..3cf0a7a325 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -346,15 +346,24 @@ 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<char> full_path (tilde_expand (filename));
+
+# ifdef IS_PY3K
+  FILE *python_file = _Py_fopen (full_path.get (), (char *) "r");
+  if (python_file == NULL)
+    {
+      gdbpy_print_stack ();
+      error (_("Error while opening file: %s"), full_path.get ());
+    }
+  PyRun_SimpleFile (python_file, filename);
+# else
   gdbpy_ref<> python_file (PyFile_FromString (full_path.get (), (char *) "r"));
   if (python_file == NULL)
     {
       gdbpy_print_stack ();
       error (_("Error while opening file: %s"), full_path.get ());
     }
-
   PyRun_SimpleFile (PyFile_AsFile (python_file.get ()), filename);
-
+# endif /* IS_PY3K */
 #endif /* _WIN32 */
 }
 
-- 
2.19.1



More information about the Gdb-patches mailing list