diff options
author | Christian Biesinger <cbiesinger@google.com> | 2019-08-13 14:48:05 -0500 |
---|---|---|
committer | Christian Biesinger <cbiesinger@google.com> | 2019-08-22 17:44:58 -0500 |
commit | 272044897e178835f596c96740c5a1800ec6f9fb (patch) | |
tree | bd40b72b2b650526a3f3dbbc53e96ccd2ca67bac /gdb/python/lib | |
parent | 395fad095c9cbc5a8b10557443da981cc3f61885 (diff) | |
download | binutils-272044897e178835f596c96740c5a1800ec6f9fb.zip binutils-272044897e178835f596c96740c5a1800ec6f9fb.tar.gz binutils-272044897e178835f596c96740c5a1800ec6f9fb.tar.bz2 |
Make GDB compile with Python 3 on MinGW
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-22 Christian Biesinger <cbiesinger@google.com>
* python/lib/gdb/__init__.py (_execute_file): New function.
* python/python.c (python_run_simple_file): Call gdb._execute_file
on Windows.
Diffstat (limited to 'gdb/python/lib')
-rw-r--r-- | gdb/python/lib/gdb/__init__.py | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/gdb/python/lib/gdb/__init__.py b/gdb/python/lib/gdb/__init__.py index 8af9e47..9c23970 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__)) |