Changing Gdb python version (GDB Printers)

Ralf Habacker ralf.habacker at freenet.de
Mon Jul 19 14:25:50 BST 2021


Am 19.07.21 um 12:32 schrieb Da Viper via Kde-finance-apps:
> Hello there, i was trying to install Mingw on windows but all the
> pre-built binaries have only python 2.7 if there is python support. Is
> there one that has python 3 support that  i can use, as i am not that
> experienced on the setting up Mingw ?
> 
> If not is there a way i can change the version that Mingw uses for
> python, because when i run `gdb --configuration`
>  there is this option
> `--with-python=/usr (relocatable)`
> then at the bottom it says that `("Relocatable" means the directory
> can be moved with the GDB installation tree, and GDB will still find
> it.)`
> 
> but i could not figure out how to set the python that gdb uses

The version of the python library is embedded into the gdb binary, see
for example see the cross compiled gdb for Windows (on opensuse distro)

$ objdump -x /usr/i686-w64-mingw32/sys-root/mingw/bin/gdb.exe | grep python
        DLL Name: libpython2.6.dll

gdb seems to be able to run with python 3.6 as it is used on linux
(opensuse)

$ ldd /usr/bin/gdb | grep python
        libpython3.6m.so.1.0 => /usr/lib64/libpython3.6m.so.1.0

but that has been added by compiling gdb with the development headers
and library for python 3.6.

I currently don't know if Python 3.x is binary compatible with 2.6,
which would be required to replace the libpython2.6.dll library used
with libpython3.6.dll simply by renaming it. I suspect that gdb will
have to be recompiled with this, but haven't tried.

Another option would be add python 2 support to your code.

It may be possible to use only language constructs that are supported by
both major versions.

For example

    print "test"

is python2 only, but

   print("test")

is supported by both major versions.

For the cases where this is not possible there is an api support to
detect the python major version

$ python
>>> import sys
>>> print (sys.version_info.major)
2

which could be used to select/include version specific Python code

The best way is to put such differences in a separate python file and
include them depending on the version something like


$> cat python2.py
import sys

def myfunc():
    print "test2"

$ cat python3.py
import sys

def myfunc():
    print("test3")

$ cat test.py
import sys

if sys.version_info.major == 2:
    from python2 import *
else:
    from python3 import *

myfunc()


$ python3 test.py
test3
$ python2 test.py
test2

Regards
Ralf


> thanks
> regards.
> 


More information about the Kde-finance-apps mailing list