From 4e724798347fb81ed3dd3efddf2d52ef99486f13 Mon Sep 17 00:00:00 2001 From: pmuldoon Date: Thu, 29 Aug 2013 10:06:17 +0000 Subject: 2013-08-29 Phil Muldoon * python/py-framefilter.c (py_print_frame): Remove usage of PyString_AsString. Use python_string_to_host_string instead. Refactor function to work with a string as a new allocation instead of a pointer. (py_print_frame): Ditto. * python/lib/gdb/frames.py (return_list): Cain iterators together instead of adding them as a list. (_sort_list): Call return_list, and remove duplicate code. (execute_frame_filters): Convert iterator to a list with list(). * python/lib/gdb/command/frame_filters.py (SetFrameFilterPriority._set_filter_priority): Convert priority attribute to an integer. * python/lib/gdb/FrameIterator.py (FrameIterator.next): Define wrapper function __next__. * python/lib/gdb/FrameDecorator.py: If basestring not defined, define as "str". 2013-08-29 Phil Muldoon * gdb.python/py-framefilter.py (FrameFilter.filter): Check itertools for imap attribute. Otherwise use map(). (ElidingIterator): Define wrapper function __next__. * gdb.python/py-framefilter-mi.exp: Do not use execfile, use exec (open (read ())) instead. * gdb.python/py-framefilter.exp: Ditto. * gdb.python/py-arch.exp: Update print based test to Python 3.x compliance. * gdb.python/py-frame.exp: Ditto. * gdb.python/py-type.exp: Ditto. --- gdb/python/lib/gdb/FrameDecorator.py | 9 ++++++++ gdb/python/lib/gdb/FrameIterator.py | 6 ++++++ gdb/python/lib/gdb/command/frame_filters.py | 5 ++++- gdb/python/lib/gdb/frames.py | 33 ++++++++++++++--------------- gdb/python/py-framefilter.c | 10 +++++++-- 5 files changed, 43 insertions(+), 20 deletions(-) (limited to 'gdb/python') diff --git a/gdb/python/lib/gdb/FrameDecorator.py b/gdb/python/lib/gdb/FrameDecorator.py index cacab4dde14..4e9cd7af072 100644 --- a/gdb/python/lib/gdb/FrameDecorator.py +++ b/gdb/python/lib/gdb/FrameDecorator.py @@ -15,6 +15,15 @@ import gdb +# This small code snippet deals with problem of strings in Python 2.x +# and Python 3.x. Python 2.x has str and unicode classes which are +# sub-classes of basestring. In Python 3.x all strings are encoded +# and basestring has been removed. +try: + basestring +except NameError: + basestring = str + class FrameDecorator(object): """Basic implementation of a Frame Decorator""" diff --git a/gdb/python/lib/gdb/FrameIterator.py b/gdb/python/lib/gdb/FrameIterator.py index b3af94b8acd..3f33bbbfebf 100644 --- a/gdb/python/lib/gdb/FrameIterator.py +++ b/gdb/python/lib/gdb/FrameIterator.py @@ -43,3 +43,9 @@ class FrameIterator(object): raise StopIteration self.frame = result.older() return result + + # Python 3.x requires __next__(self) while Python 2.x requires + # next(self). Define next(self), and for Python 3.x create this + # wrapper. + def __next__(self): + return self.next() diff --git a/gdb/python/lib/gdb/command/frame_filters.py b/gdb/python/lib/gdb/command/frame_filters.py index 1b7305927e5..b5d34ade3db 100644 --- a/gdb/python/lib/gdb/command/frame_filters.py +++ b/gdb/python/lib/gdb/command/frame_filters.py @@ -335,7 +335,10 @@ class SetFrameFilterPriority(gdb.Command): list_op = command_tuple[0] frame_filter = command_tuple[1] - priority = command_tuple[2] + + # GDB returns arguments as a string, so convert priority to + # a number. + priority = int(command_tuple[2]) op_list = gdb.frames.return_list(list_op) diff --git a/gdb/python/lib/gdb/frames.py b/gdb/python/lib/gdb/frames.py index 10dce8e7b42..c148b9826e8 100644 --- a/gdb/python/lib/gdb/frames.py +++ b/gdb/python/lib/gdb/frames.py @@ -108,13 +108,15 @@ def return_list(name): # cannot return a combined dictionary as keys() may clash in # between different dictionaries. As we just want all the frame # filters to enable/disable them all, just return the combined - # items() as a list. + # items() as a chained iterator of dictionary values. if name == "all": - all_dicts = gdb.frame_filters.values() - all_dicts = all_dicts + gdb.current_progspace().frame_filters.values() + glob = gdb.frame_filters.values() + prog = gdb.current_progspace().frame_filters.values() + return_iter = itertools.chain(glob, prog) for objfile in gdb.objfiles(): - all_dicts = all_dicts + objfile.frame_filters.values() - return all_dicts + return_iter = itertools.chain(return_iter, objfile.frame_filters.values()) + + return return_iter if name == "global": return gdb.frame_filters @@ -140,14 +142,7 @@ def _sort_list(): execute. """ - all_filters = [] - for objfile in gdb.objfiles(): - all_filters = all_filters + objfile.frame_filters.values() - cp = gdb.current_progspace() - - all_filters = all_filters + cp.frame_filters.values() - all_filters = all_filters + gdb.frame_filters.values() - + all_filters = return_list("all") sorted_frame_filters = sorted(all_filters, key = get_priority, reverse = True) @@ -180,7 +175,7 @@ def execute_frame_filters(frame, frame_low, frame_high): """ # Get a sorted list of frame filters. - sorted_list = _sort_list() + sorted_list = list(_sort_list()) # Check to see if there are any frame-filters. If not, just # return None and let default backtrace printing occur. @@ -189,9 +184,13 @@ def execute_frame_filters(frame, frame_low, frame_high): frame_iterator = FrameIterator(frame) - # Apply a basic frame decorator to all gdb.Frames. This unifies the - # interface. - frame_iterator = itertools.imap(FrameDecorator, frame_iterator) + # Apply a basic frame decorator to all gdb.Frames. This unifies + # the interface. Python 3.x moved the itertools.imap + # functionality to map(), so check if it is available. + if hasattr(itertools,"imap"): + frame_iterator = itertools.imap(FrameDecorator, frame_iterator) + else: + frame_iterator = map(FrameDecorator, frame_iterator) for ff in sorted_list: frame_iterator = ff.filter(frame_iterator) diff --git a/gdb/python/py-framefilter.c b/gdb/python/py-framefilter.c index 5ac8e477011..871c2457ab3 100644 --- a/gdb/python/py-framefilter.c +++ b/gdb/python/py-framefilter.c @@ -1170,13 +1170,17 @@ py_print_frame (PyObject *filter, int flags, enum py_frame_args args_type, if (gdbpy_is_string (py_func)) { - function = PyString_AsString (py_func); + char *function_to_free = NULL; + + function = function_to_free = + python_string_to_host_string (py_func); if (function == NULL) { Py_DECREF (py_func); goto error; } + make_cleanup (xfree, function_to_free); } else if (PyLong_Check (py_func)) { @@ -1251,13 +1255,15 @@ py_print_frame (PyObject *filter, int flags, enum py_frame_args args_type, { if (py_fn != Py_None) { - char *filename = PyString_AsString (py_fn); + char *filename = python_string_to_host_string (py_fn); if (filename == NULL) { Py_DECREF (py_fn); goto error; } + + make_cleanup (xfree, filename); TRY_CATCH (except, RETURN_MASK_ALL) { ui_out_wrap_hint (out, " "); -- cgit v1.2.1