python: Make Windows-specific code work with Python 3
Commit Message
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(-)
Comments
>>>>> "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
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
> 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
@@ -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.
@@ -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 */
}