python: Make Windows-specific code work with Python 3

Message ID 20181026093501.9377-1-jan.vrany@fit.cvut.cz
State New
Headers show
Series
  • python: Make Windows-specific code work with Python 3
Related show

Commit Message

Jan Vrany Oct. 26, 2018, 9:35 a.m.
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(-)

-- 
2.19.1

Comments

Tom Tromey Oct. 30, 2018, 8:08 p.m. | #1
>>>>> "Jan" == Jan Vrany <jan.vrany@fit.cvut.cz> writes:


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

Jan> +# ifdef IS_PY3K
Jan> +  FILE *python_file = _Py_fopen (full_path.get (), (char *) "r");

I'm a bit reluctant to rely on an undocumented API.
I'd guess from the name that this is supposed to be internal...?

Also, nothing closes this new file.

Tom
Jan Vrany Oct. 30, 2018, 8:28 p.m. | #2
On Tue, 2018-10-30 at 14:08 -0600, Tom Tromey wrote:
> > > > > > "Jan" == Jan Vrany <jan.vrany@fit.cvut.cz> writes:

> 

> Jan> Windows workaround in python_run_simple_file() used Python 2

> Jan> APIs which were removed in Python 3. This commit adds a

> Jan> conditionally compiled variant that uses Python 3 APIs.

> 

> Jan> +# ifdef IS_PY3K

> Jan> +  FILE *python_file = _Py_fopen (full_path.get (), (char *) "r");

> 

> I'm a bit reluctant to rely on an undocumented API.


Me to, to be honest, but I could not find any other way...

> I'd guess from the name that this is supposed to be internal...?


...and then I actually looked at the Python header file,. The _Py_fopen()
is declared as follows (see fileutils.h).

PyAPI_FUNC(FILE*) _Py_fopen(
    const char *pathname,
    const char *mode);

Looking at pyport.h, it says:

/* Declarations for symbol visibility.

  PyAPI_FUNC(type): Declares a public Python API function and return type
  PyAPI_DATA(type): Declares public Python data and its type
  ...
*/

which made me thing using _Py_fopen() would be okish, at least for some 
time. I'm not a python hacker though.

> Also, nothing closes this new file.


Argh, good point. Will check that. Thanks!

Jan
Paul Koning Oct. 30, 2018, 9:18 p.m. | #3
> On Oct 30, 2018, at 4:28 PM, Jan Vrany <jan.vrany@fit.cvut.cz> wrote:

> 

> On Tue, 2018-10-30 at 14:08 -0600, Tom Tromey wrote:

>>>>>>> "Jan" == Jan Vrany <jan.vrany@fit.cvut.cz> writes:

>> 

>> Jan> Windows workaround in python_run_simple_file() used Python 2

>> Jan> APIs which were removed in Python 3. This commit adds a

>> Jan> conditionally compiled variant that uses Python 3 APIs.

>> 

>> Jan> +# ifdef IS_PY3K

>> Jan> +  FILE *python_file = _Py_fopen (full_path.get (), (char *) "r");

>> 

>> I'm a bit reluctant to rely on an undocumented API.

> 

> Me to, to be honest, but I could not find any other way...


I've been at the receiving end of grief for using a _Py_foo call.  It might stop working in some release and that would be a hassle.  You might ask on the Python list for advice.  Given what you're looking for there clearly has to be some approved way of doing this with just public APIs.

	paul

Patch

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 */
 }