From 22bdecb10d9b5718e45b6e247307edfb4adfc990 Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Tue, 19 May 2015 09:44:09 -0600 Subject: [PATCH] make it work with both python2 and python3 I ran 2to3, fixed up the output a tiny bit, and fixed the C module as well. I smoke tested with both versions of Python but didn't use it in anger yet. Fix #29 --- Makefile | 6 +++++- gui/adapt.py | 12 ++++++------ gui/commands.py | 4 ++-- gui/fix-signals.c | 27 ++++++++++++++++++++++++++- gui/params.py | 2 +- gui/startup.py | 15 +++++++++++---- gui/storage.py | 13 +++++++++---- gui/toplevel.py | 10 +++++----- 8 files changed, 65 insertions(+), 24 deletions(-) diff --git a/Makefile b/Makefile index 05308e2..741241b 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,7 @@ +# This is passed to pkg-config to determine which python to use. It +# has to match your gdb. +pyname = python + all: gdb-gui.py gui/fix_signals.so @: @@ -5,7 +9,7 @@ gdb-gui.py: gdb-gui.py.in sed -e "s,HERE,`pwd`," < gdb-gui.py.in > gdb-gui.py gui/fix_signals.so: gui/fix-signals.c - gcc -shared -fPIC -g -o gui/fix_signals.so gui/fix-signals.c `pkg-config --cflags python` `pkg-config --libs python` + gcc -shared -fPIC -g -o gui/fix_signals.so gui/fix-signals.c `pkg-config --cflags $(pyname)` `pkg-config --libs $(pyname)` clean: -rm gdb-gui.py gui/fix_signals.so diff --git a/gui/adapt.py b/gui/adapt.py index 497cce6..420ae88 100644 --- a/gui/adapt.py +++ b/gui/adapt.py @@ -46,10 +46,10 @@ def notify_bug(bugno): return if not (bugno in bugs): return - print "################" - print bugs[bugno] - print _warning % bugno - print "" - print "You can use 'set gui mention-missing off' to disable this message." - print "################" + print("################") + print(bugs[bugno]) + print(_warning % bugno) + print("") + print("You can use 'set gui mention-missing off' to disable this message.") + print("################") del bugs[bugno] diff --git a/gui/commands.py b/gui/commands.py index 9a6b039..5531c73 100644 --- a/gui/commands.py +++ b/gui/commands.py @@ -65,7 +65,7 @@ LINESPEC is a line specification of the form given to 'break'.""" if sals is None: raise gdb.GdbError('not found') if len(sals) > 1: - print "Ambiguous linespec, only showing first result" + print("Ambiguous linespec, only showing first result") sal = sals[0] if sal.symtab is None or sal.symtab.filename is None: raise gdb.GdbError('could not find file for symbol') @@ -122,7 +122,7 @@ gui print @5 variable""" def invoke(self, arg, from_tty): self.dont_repeat() window = gui.logwindow.LogWindow() - print "Created log window %d; now the default" % window.number + print("Created log window %d; now the default" % window.number) class GuiPrintBase(gdb.Command): def __init__(self, command): diff --git a/gui/fix-signals.c b/gui/fix-signals.c index aaee731..c962524 100644 --- a/gui/fix-signals.c +++ b/gui/fix-signals.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013 Tom Tromey +/* Copyright (C) 2013, 2015 Tom Tromey This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -53,8 +53,33 @@ static PyMethodDef methods[] = { NULL, NULL, 0, NULL } }; +#if PY_MAJOR_VERSION >= 3 + +static struct PyModuleDef module = +{ + PyModuleDef_HEAD_INIT, + "fix_signals", + NULL, + -1, + methods, + NULL, + NULL, + NULL, + NULL +}; + +PyMODINIT_FUNC +PyInit_fix_signals (void) +{ + PyModule_Create (&module); +} + +#else + PyMODINIT_FUNC initfix_signals (void) { Py_InitModule ("fix_signals", methods); } + +#endif diff --git a/gui/params.py b/gui/params.py index 1cb0b41..08d4099 100644 --- a/gui/params.py +++ b/gui/params.py @@ -70,7 +70,7 @@ class _StoredParameter(gdb.Parameter): elif p_kind is gdb.PARAM_ZINTEGER: val = storage.getint(self.storage_name) else: - raise "WHOOPS" + raise Error("missing case in gdb gui code") # Don't record the first setting. self.storage = None if val is None: diff --git a/gui/startup.py b/gui/startup.py index dbc9f9a..a3caf43 100644 --- a/gui/startup.py +++ b/gui/startup.py @@ -15,23 +15,30 @@ import gdb import threading -import Queue +try: + import queue +except ImportError: + # Python 2. + import Queue + queue = Queue import os import os.path import gui -import fix_signals +from . import fix_signals fix_signals.save() from gi.repository import Gtk, Gdk, GObject, GtkSource, GLib, GdkPixbuf (read_pipe, write_pipe) = os.pipe() -_event_queue = Queue.Queue() +_event_queue = queue.Queue() def send_to_gtk(func): _event_queue.put(func) - os.write(write_pipe, 'x') + # The payload is arbitrary, and bytes(1) is chosen to work on both + # Python 2 and Python 3. + os.write(write_pipe, bytes(1)) class _GtkThread(threading.Thread): def handle_queue(self, source, condition): diff --git a/gui/storage.py b/gui/storage.py index 12cdb14..34aba46 100644 --- a/gui/storage.py +++ b/gui/storage.py @@ -18,7 +18,12 @@ from gi.repository import GLib import os import errno -import ConfigParser +try: + import configparser +except ImportError: + # Python 2. + import ConfigParser + configparser = ConfigParser import atexit class StorageManager: @@ -26,11 +31,11 @@ class StorageManager: self.dir = os.path.join(GLib.get_user_config_dir(), 'gdb') self.file = os.path.join(self.dir, 'settings') try: - os.mkdir(self.dir, 0700) + os.mkdir(self.dir, 0o700) except OSError as exc: if exc.errno is not errno.EEXIST: self.file = None - self.config = ConfigParser.RawConfigParser() + self.config = configparser.RawConfigParser() if self.file is not None: self.config.read(self.file) if not self.config.has_section('general'): @@ -56,7 +61,7 @@ class StorageManager: self.config.set('general', name, value) def write(self): - with open(self.file, 'wb') as save_file: + with open(self.file, 'wt') as save_file: self.config.write(save_file) storage_manager = StorageManager() diff --git a/gui/toplevel.py b/gui/toplevel.py index d08eb35..f33e4fa 100644 --- a/gui/toplevel.py +++ b/gui/toplevel.py @@ -70,15 +70,15 @@ class _ToplevelState(object): def display(self): with self.toplevel_lock: if len(self.toplevels) == 0: - print "No windows" + print("No windows") return - print ' Num Name' + print(' Num Name') for winno in range(1, self.next_toplevel): if winno in self.toplevels: window = self.toplevels[winno] - print ' %3d %s' % (window.number, - window.window.get_title()) + print(' %3d %s' % (window.number, + window.window.get_title())) @in_gtk_thread def _do_set_font(self, font_name): @@ -123,7 +123,7 @@ class _ToplevelState(object): @in_gtk_thread def windows(self): - return self.toplevels.values() + return list(self.toplevels.values()) state = _ToplevelState()