aboutsummaryrefslogtreecommitdiff
path: root/gdb/python
diff options
context:
space:
mode:
Diffstat (limited to 'gdb/python')
-rw-r--r--gdb/python/lib/gdb/__init__.py118
-rw-r--r--gdb/python/lib/gdb/dap/__init__.py1
-rw-r--r--gdb/python/lib/gdb/dap/breakpoint.py4
-rw-r--r--gdb/python/lib/gdb/dap/events.py1
-rw-r--r--gdb/python/lib/gdb/dap/frames.py6
-rw-r--r--gdb/python/lib/gdb/dap/globalvars.py1
-rw-r--r--gdb/python/lib/gdb/dap/launch.py1
-rw-r--r--gdb/python/lib/gdb/dap/scopes.py3
-rw-r--r--gdb/python/lib/gdb/dap/server.py11
-rw-r--r--gdb/python/lib/gdb/dap/sources.py9
-rw-r--r--gdb/python/lib/gdb/dap/varref.py3
-rw-r--r--gdb/python/lib/gdb/styling.py1
-rw-r--r--gdb/python/py-breakpoint.c2
-rw-r--r--gdb/python/py-cmd.c2
-rw-r--r--gdb/python/py-color.c77
-rw-r--r--gdb/python/py-disasm.c11
-rw-r--r--gdb/python/py-param.c18
-rw-r--r--gdb/python/py-registers.c3
-rw-r--r--gdb/python/py-unwind.c6
-rw-r--r--gdb/python/python.c9
20 files changed, 201 insertions, 86 deletions
diff --git a/gdb/python/lib/gdb/__init__.py b/gdb/python/lib/gdb/__init__.py
index 4ea5d06..866c312 100644
--- a/gdb/python/lib/gdb/__init__.py
+++ b/gdb/python/lib/gdb/__init__.py
@@ -392,3 +392,121 @@ def _handle_missing_objfile(pspace, buildid, filename):
return _handle_missing_files(
pspace, "objfile", lambda h: h(pspace, buildid, filename)
)
+
+
+class ParameterPrefix:
+ # A wrapper around gdb.Command for creating set/show prefixes.
+ #
+ # When creating a gdb.Parameter sub-classes, it is sometimes necessary
+ # to first create a gdb.Command object in order to create the needed
+ # command prefix. However, for parameters, we actually need two
+ # prefixes, a 'set' prefix, and a 'show' prefix. With this helper
+ # class, a single instance of this class will create both prefixes at
+ # once.
+ #
+ # It is important that this class-level documentation not be a __doc__
+ # string. Users are expected to sub-class this ParameterPrefix class
+ # and add their own documentation. If they don't, then GDB will
+ # generate a suitable doc string. But, if this (parent) class has a
+ # __doc__ sting of its own, then sub-classes will inherit that __doc__
+ # string, and GDB will not understand that it needs to generate one.
+
+ class _PrefixCommand(Command):
+ """A gdb.Command used to implement both the set and show prefixes.
+
+ This documentation string is not used as the prefix command
+ documentation as it is overridden in the __init__ method below."""
+
+ # This private method is connected to the 'invoke' attribute within
+ # this _PrefixCommand object if the containing ParameterPrefix
+ # object has an invoke_set or invoke_show method.
+ #
+ # This method records within self.__delegate which _PrefixCommand
+ # object is currently active, and then calls the correct invoke
+ # method on the delegat object (the ParameterPrefix sub-class
+ # object).
+ #
+ # Recording the currently active _PrefixCommand object is important;
+ # if from the invoke method the user calls dont_repeat, then this is
+ # forwarded to the currently active _PrefixCommand object.
+ def __invoke(self, args, from_tty):
+
+ # A helper class for use as part of a Python 'with' block.
+ # Records which gdb.Command object is currently running its
+ # invoke method.
+ class MarkActiveCallback:
+ # The CMD is a _PrefixCommand object, and the DELEGATE is
+ # the ParameterPrefix class, or sub-class object. At this
+ # point we simple record both of these within the
+ # MarkActiveCallback object.
+ def __init__(self, cmd, delegate):
+ self.__cmd = cmd
+ self.__delegate = delegate
+
+ # Record the currently active _PrefixCommand object within
+ # the outer ParameterPrefix sub-class object.
+ def __enter__(self):
+ self.__delegate.active_prefix = self.__cmd
+
+ # Once the invoke method has completed, then clear the
+ # _PrefixCommand object that was stored into the outer
+ # ParameterPrefix sub-class object.
+ def __exit__(self, exception_type, exception_value, traceback):
+ self.__delegate.active_prefix = None
+
+ # The self.__cb attribute is set when the _PrefixCommand object
+ # is created, and is either invoke_set or invoke_show within the
+ # ParameterPrefix sub-class object.
+ assert callable(self.__cb)
+
+ # Record the currently active _PrefixCommand object within the
+ # ParameterPrefix sub-class object, then call the relevant
+ # invoke method within the ParameterPrefix sub-class object.
+ with MarkActiveCallback(self, self.__delegate):
+ self.__cb(args, from_tty)
+
+ @staticmethod
+ def __find_callback(delegate, mode):
+ """The MODE is either 'set' or 'show'. Look for an invoke_MODE method
+ on DELEGATE, if a suitable method is found, then return it, otherwise,
+ return None.
+ """
+ cb = getattr(delegate, "invoke_" + mode, None)
+ if callable(cb):
+ return cb
+ return None
+
+ def __init__(self, mode, name, cmd_class, delegate, doc=None):
+ """Setup this gdb.Command. Mode is a string, either 'set' or 'show'.
+ NAME is the name for this prefix command, that is, the
+ words that appear after both 'set' and 'show' in the
+ command name. CMD_CLASS is the usual enum. And DELEGATE
+ is the gdb.ParameterPrefix object this prefix is part of.
+ """
+ assert mode == "set" or mode == "show"
+ if doc is None:
+ self.__doc__ = delegate.__doc__
+ else:
+ self.__doc__ = doc
+ self.__cb = self.__find_callback(delegate, mode)
+ self.__delegate = delegate
+ if not self.__cb is None:
+ self.invoke = self.__invoke
+ super().__init__(mode + " " + name, cmd_class, prefix=True)
+
+ def __init__(self, name, cmd_class, doc=None):
+ """Create a _PrefixCommand for both the set and show prefix commands.
+ NAME is the command name without either the leading 'set ' or
+ 'show ' strings, and CMD_CLASS is the usual enum value.
+ """
+ self.active_prefix = None
+ self._set_prefix_cmd = self._PrefixCommand("set", name, cmd_class, self, doc)
+ self._show_prefix_cmd = self._PrefixCommand("show", name, cmd_class, self, doc)
+
+ # When called from within an invoke method the self.active_prefix
+ # attribute should be set to a gdb.Command sub-class (a _PrefixCommand
+ # object, see above). Forward the dont_repeat call to this object to
+ # register the actual command as none repeating.
+ def dont_repeat(self):
+ if self.active_prefix is not None:
+ self.active_prefix.dont_repeat()
diff --git a/gdb/python/lib/gdb/dap/__init__.py b/gdb/python/lib/gdb/dap/__init__.py
index f908c91..1c3cf8e 100644
--- a/gdb/python/lib/gdb/dap/__init__.py
+++ b/gdb/python/lib/gdb/dap/__init__.py
@@ -96,5 +96,4 @@ def pre_command_loop():
# These are handy for bug reports.
startup.exec_and_log("show version")
startup.exec_and_log("show configuration")
- global server
startup.start_dap(server.main_loop)
diff --git a/gdb/python/lib/gdb/dap/breakpoint.py b/gdb/python/lib/gdb/dap/breakpoint.py
index bfb7bd3..59da120 100644
--- a/gdb/python/lib/gdb/dap/breakpoint.py
+++ b/gdb/python/lib/gdb/dap/breakpoint.py
@@ -51,7 +51,6 @@ def suppress_new_breakpoint_event():
@in_gdb_thread
def _bp_modified(event):
- global _suppress_bp
if not _suppress_bp:
send_event(
"breakpoint",
@@ -64,7 +63,6 @@ def _bp_modified(event):
@in_gdb_thread
def _bp_created(event):
- global _suppress_bp
if not _suppress_bp:
send_event(
"breakpoint",
@@ -77,7 +75,6 @@ def _bp_created(event):
@in_gdb_thread
def _bp_deleted(event):
- global _suppress_bp
if not _suppress_bp:
send_event(
"breakpoint",
@@ -151,7 +148,6 @@ def _remove_entries(table, *names):
# the breakpoint.
@in_gdb_thread
def _set_breakpoints_callback(kind, specs, creator):
- global breakpoint_map
# Try to reuse existing breakpoints if possible.
if kind in breakpoint_map:
saved_map = breakpoint_map[kind]
diff --git a/gdb/python/lib/gdb/dap/events.py b/gdb/python/lib/gdb/dap/events.py
index 54caea0..4dc9d65 100644
--- a/gdb/python/lib/gdb/dap/events.py
+++ b/gdb/python/lib/gdb/dap/events.py
@@ -238,7 +238,6 @@ def _on_stop(event):
):
obj["reason"] = "pause"
else:
- global stop_reason_map
obj["reason"] = stop_reason_map[event.details["reason"]]
_expected_pause = False
send_event_maybe_later("stopped", obj)
diff --git a/gdb/python/lib/gdb/dap/frames.py b/gdb/python/lib/gdb/dap/frames.py
index 6433dbe..4dacb87 100644
--- a/gdb/python/lib/gdb/dap/frames.py
+++ b/gdb/python/lib/gdb/dap/frames.py
@@ -53,12 +53,11 @@ gdb.events.cont.connect(_clear_frame_ids)
@in_gdb_thread
def frame_for_id(id):
"""Given a frame identifier ID, return the corresponding frame."""
- global thread_ids
if id in thread_ids:
thread_id = thread_ids[id]
if thread_id != gdb.selected_thread().global_num:
set_thread(thread_id)
- global _all_frames
+
return _all_frames[id]
@@ -103,10 +102,8 @@ def _frame_id_generator():
# Helper function to assign an ID to a frame.
def get_id(frame):
- global _all_frames
num = len(_all_frames)
_all_frames.append(frame)
- global thread_ids
thread_ids[num] = gdb.selected_thread().global_num
return num
@@ -128,7 +125,6 @@ def _frame_id_generator():
@in_gdb_thread
def _get_frame_iterator():
thread_id = gdb.selected_thread().global_num
- global _iter_map
if thread_id not in _iter_map:
_iter_map[thread_id] = _MemoizingIterator(_frame_id_generator())
return _iter_map[thread_id]
diff --git a/gdb/python/lib/gdb/dap/globalvars.py b/gdb/python/lib/gdb/dap/globalvars.py
index 2e4b2a6..9d64d28 100644
--- a/gdb/python/lib/gdb/dap/globalvars.py
+++ b/gdb/python/lib/gdb/dap/globalvars.py
@@ -78,7 +78,6 @@ def get_global_scope(frame):
except RuntimeError:
return None
- global _id_to_scope
block = block.static_block
if block in _id_to_scope:
return _id_to_scope[block]
diff --git a/gdb/python/lib/gdb/dap/launch.py b/gdb/python/lib/gdb/dap/launch.py
index e093e60..8ac4c77 100644
--- a/gdb/python/lib/gdb/dap/launch.py
+++ b/gdb/python/lib/gdb/dap/launch.py
@@ -164,7 +164,6 @@ def attach(
@request("configurationDone", on_dap_thread=True)
def config_done(**args):
# Handle the launch or attach.
- global _launch_or_attach_promise
if _launch_or_attach_promise is None:
raise DAPException("launch or attach not specified")
# Resolve the launch or attach, but only after the
diff --git a/gdb/python/lib/gdb/dap/scopes.py b/gdb/python/lib/gdb/dap/scopes.py
index f499dcb..7ce3a7f 100644
--- a/gdb/python/lib/gdb/dap/scopes.py
+++ b/gdb/python/lib/gdb/dap/scopes.py
@@ -120,7 +120,6 @@ class _FinishScopeReference(_ScopeReference):
def fetch_one_child(self, idx):
assert idx == 0
- global _last_return_value
return ("(return)", _last_return_value)
@@ -145,8 +144,6 @@ class _RegisterReference(_ScopeReference):
@request("scopes")
def scopes(*, frameId: int, **extra):
- global _last_return_value
- global frame_to_scope
if frameId in frame_to_scope:
scopes = frame_to_scope[frameId]
else:
diff --git a/gdb/python/lib/gdb/dap/server.py b/gdb/python/lib/gdb/dap/server.py
index d4314e8..c4fa781 100644
--- a/gdb/python/lib/gdb/dap/server.py
+++ b/gdb/python/lib/gdb/dap/server.py
@@ -307,7 +307,6 @@ class Server:
args = {}
def fn():
- global _commands
return _commands[params["command"]](**args)
self.invoke_request(req, result, fn)
@@ -440,7 +439,6 @@ def send_event(event, body=None):
"""Send an event to the DAP client.
EVENT is the name of the event, a string.
BODY is the body of the event, an arbitrary object."""
- global _server
_server.send_event(event, body)
@@ -449,13 +447,11 @@ def send_event_maybe_later(event, body=None):
within the dap thread and that request is configured to delay the event,
wait until the response has been sent until the event is sent back to
the client."""
- global _server
_server.send_event_maybe_later(event, body)
def call_function_later(fn):
"""Call FN later -- after the current request's response has been sent."""
- global _server
_server.call_function_later(fn)
@@ -533,7 +529,6 @@ def request(
if response:
if defer_stop_events:
- global _server
if _server is not None:
with _server.delayed_events_lock:
_server.defer_stop_events = True
@@ -555,7 +550,6 @@ def request(
if expect_stopped:
cmd = _check_not_running(cmd)
- global _commands
assert name not in _commands
_commands[name] = cmd
return cmd
@@ -568,7 +562,6 @@ def capability(name, value=True):
the DAP capability NAME."""
def wrap(func):
- global _capabilities
assert name not in _capabilities
_capabilities[name] = value
return func
@@ -581,7 +574,6 @@ def client_bool_capability(name, default=False):
If the capability was not specified, or did not have boolean type,
DEFAULT is returned. DEFAULT defaults to False."""
- global _server
if name in _server.config and isinstance(_server.config[name], bool):
return _server.config[name]
return default
@@ -589,7 +581,6 @@ def client_bool_capability(name, default=False):
@request("initialize", on_dap_thread=True)
def initialize(**args):
- global _server, _capabilities
_server.config = args
_server.send_event_later("initialized")
global _lines_start_at_1
@@ -705,7 +696,6 @@ def export_line(line: int) -> int:
"""Rewrite LINE according to client capability.
This applies the linesStartAt1 capability as needed,
when sending a line number from gdb to the client."""
- global _lines_start_at_1
if not _lines_start_at_1:
# In gdb, lines start at 1, so we only need to change this if
# the client starts at 0.
@@ -717,7 +707,6 @@ def import_line(line: int) -> int:
"""Rewrite LINE according to client capability.
This applies the linesStartAt1 capability as needed,
when the client sends a line number to gdb."""
- global _lines_start_at_1
if not _lines_start_at_1:
# In gdb, lines start at 1, so we only need to change this if
# the client starts at 0.
diff --git a/gdb/python/lib/gdb/dap/sources.py b/gdb/python/lib/gdb/dap/sources.py
index 938c93a..efcd799 100644
--- a/gdb/python/lib/gdb/dap/sources.py
+++ b/gdb/python/lib/gdb/dap/sources.py
@@ -37,7 +37,6 @@ def make_source(fullname, filename=None):
FILENAME is the base name; if None (the default), then it is
computed from FULLNAME.
"""
- global _source_map
if fullname in _source_map:
result = _source_map[fullname]
else:
@@ -53,7 +52,6 @@ def make_source(fullname, filename=None):
global _next_source
result["sourceReference"] = _next_source
- global _id_map
_id_map[_next_source] = result
_next_source += 1
@@ -66,12 +64,11 @@ def decode_source(source):
"""Decode a Source object.
Finds and returns the filename of a given Source object."""
- if "path" in source:
- return source["path"]
- if "sourceReference" not in source:
+ if "sourceReference" not in source or source["sourceReference"] <= 0:
+ if "path" in source:
+ return source["path"]
raise DAPException("either 'path' or 'sourceReference' must appear in Source")
ref = source["sourceReference"]
- global _id_map
if ref not in _id_map:
raise DAPException("no sourceReference " + str(ref))
return _id_map[ref]["path"]
diff --git a/gdb/python/lib/gdb/dap/varref.py b/gdb/python/lib/gdb/dap/varref.py
index 42b5302..8a13c51 100644
--- a/gdb/python/lib/gdb/dap/varref.py
+++ b/gdb/python/lib/gdb/dap/varref.py
@@ -69,7 +69,6 @@ class BaseReference(ABC):
NAME is a string or None. None means this does not have a
name, e.g., the result of expression evaluation."""
- global all_variables
all_variables.append(self)
self._ref = len(all_variables)
self._name = name
@@ -267,7 +266,7 @@ class VariableReference(BaseReference):
@in_gdb_thread
def find_variable(ref):
"""Given a variable reference, return the corresponding variable object."""
- global all_variables
+
# Variable references are offset by 1.
ref = ref - 1
if ref < 0 or ref > len(all_variables):
diff --git a/gdb/python/lib/gdb/styling.py b/gdb/python/lib/gdb/styling.py
index e9387e3..60c470f 100644
--- a/gdb/python/lib/gdb/styling.py
+++ b/gdb/python/lib/gdb/styling.py
@@ -80,7 +80,6 @@ try:
# ignore.
pass
- global _asm_lexers
if lexer_type not in _asm_lexers:
_asm_lexers[lexer_type] = lexers.get_lexer_by_name(lexer_type)
_asm_lexers[lexer_type].add_filter(HandleNasmComments())
diff --git a/gdb/python/py-breakpoint.c b/gdb/python/py-breakpoint.c
index 861e9a3..58998f5 100644
--- a/gdb/python/py-breakpoint.c
+++ b/gdb/python/py-breakpoint.c
@@ -948,7 +948,7 @@ bppy_init (PyObject *self, PyObject *args, PyObject *kwargs)
else
{
PyErr_SetString (PyExc_RuntimeError,
- _("Line keyword should be an integer or a string. "));
+ _("Line keyword should be an integer or a string."));
return -1;
}
}
diff --git a/gdb/python/py-cmd.c b/gdb/python/py-cmd.c
index 5d98d03..c53138a 100644
--- a/gdb/python/py-cmd.c
+++ b/gdb/python/py-cmd.c
@@ -385,7 +385,7 @@ gdbpy_parse_command_name (const char *name,
prefix_text2 = prefix_text.c_str ();
elt = lookup_cmd_1 (&prefix_text2, *start_list, NULL, NULL, 1);
- if (elt == NULL || elt == CMD_LIST_AMBIGUOUS)
+ if (elt == nullptr || elt == CMD_LIST_AMBIGUOUS || *prefix_text2 != '\0')
{
PyErr_Format (PyExc_RuntimeError, _("Could not find command prefix %s."),
prefix_text.c_str ());
diff --git a/gdb/python/py-color.c b/gdb/python/py-color.c
index 91ad7ae..3bbd22d 100644
--- a/gdb/python/py-color.c
+++ b/gdb/python/py-color.c
@@ -21,6 +21,7 @@
#include "python-internal.h"
#include "py-color.h"
#include "cli/cli-decode.h"
+#include "cli/cli-style.h"
/* Colorspace constants and their values. */
static struct {
@@ -64,7 +65,8 @@ create_color_object (const ui_file_style::color &color)
bool
gdbpy_is_color (PyObject *obj)
{
- return PyObject_IsInstance (obj, (PyObject *) &colorpy_object_type);
+ gdb_assert (obj != nullptr);
+ return PyObject_TypeCheck (obj, &colorpy_object_type) != 0;
}
/* See py-color.h. */
@@ -80,33 +82,33 @@ gdbpy_get_color (PyObject *obj)
static PyObject *
get_attr (PyObject *obj, PyObject *attr_name)
{
- if (! PyUnicode_Check (attr_name))
+ if (!PyUnicode_Check (attr_name))
return PyObject_GenericGetAttr (obj, attr_name);
colorpy_object *self = (colorpy_object *) obj;
const ui_file_style::color &color = self->color;
- if (! PyUnicode_CompareWithASCIIString (attr_name, "colorspace"))
+ if (!PyUnicode_CompareWithASCIIString (attr_name, "colorspace"))
{
int value = static_cast<int> (color.colorspace ());
return gdb_py_object_from_longest (value).release ();
}
- if (! PyUnicode_CompareWithASCIIString (attr_name, "is_none"))
+ if (!PyUnicode_CompareWithASCIIString (attr_name, "is_none"))
return PyBool_FromLong (color.is_none ());
- if (! PyUnicode_CompareWithASCIIString (attr_name, "is_indexed"))
+ if (!PyUnicode_CompareWithASCIIString (attr_name, "is_indexed"))
return PyBool_FromLong (color.is_indexed ());
- if (! PyUnicode_CompareWithASCIIString (attr_name, "is_direct"))
+ if (!PyUnicode_CompareWithASCIIString (attr_name, "is_direct"))
return PyBool_FromLong (color.is_direct ());
if (color.is_indexed ()
- && ! PyUnicode_CompareWithASCIIString (attr_name, "index"))
+ && !PyUnicode_CompareWithASCIIString (attr_name, "index"))
return gdb_py_object_from_longest (color.get_value ()).release ();
if (color.is_direct ()
- && ! PyUnicode_CompareWithASCIIString (attr_name, "components"))
+ && !PyUnicode_CompareWithASCIIString (attr_name, "components"))
{
uint8_t rgb[3];
color.get_rgb (rgb);
@@ -135,25 +137,29 @@ get_attr (PyObject *obj, PyObject *attr_name)
/* Implementation of Color.escape_sequence (self, is_fg) -> str. */
static PyObject *
-colorpy_escape_sequence (PyObject *self, PyObject *is_fg_obj)
+colorpy_escape_sequence (PyObject *self, PyObject *args, PyObject *kwargs)
{
- if (!gdbpy_is_color (self))
- {
- PyErr_SetString (PyExc_RuntimeError,
- _("Object is not gdb.Color."));
- return nullptr;
- }
+ static const char *keywords[] = { "is_foreground", nullptr };
+ PyObject *is_fg_obj;
+
+ /* Parse method arguments. */
+ if (!gdb_PyArg_ParseTupleAndKeywords (args, kwargs, "O!", keywords,
+ &PyBool_Type, &is_fg_obj))
+ return nullptr;
- if (! PyBool_Check (is_fg_obj))
+ /* Python ensures the type of SELF. */
+ gdb_assert (gdbpy_is_color (self));
+
+ /* The argument parsing ensures we have a bool. */
+ gdb_assert (PyBool_Check (is_fg_obj));
+
+ std::string s;
+ if (term_cli_styling ())
{
- PyErr_SetString (PyExc_RuntimeError,
- _("A boolean argument is required."));
- return nullptr;
+ bool is_fg = is_fg_obj == Py_True;
+ s = gdbpy_get_color (self).to_ansi (is_fg);
}
- bool is_fg = is_fg_obj == Py_True;
- std::string s = gdbpy_get_color (self).to_ansi (is_fg);
-
return host_string_to_python_string (s.c_str ()).release ();
}
@@ -175,17 +181,20 @@ colorpy_init (PyObject *self, PyObject *args, PyObject *kwds)
PyObject *colorspace_obj = nullptr;
color_space colorspace = color_space::MONOCHROME;
- if (! PyArg_ParseTuple (args, "|OO", &value_obj, &colorspace_obj))
+ static const char *keywords[] = { "value", "color_space", nullptr };
+
+ if (!gdb_PyArg_ParseTupleAndKeywords (args, kwds, "|OO", keywords,
+ &value_obj, &colorspace_obj))
return -1;
try
{
- if (colorspace_obj)
+ if (colorspace_obj != nullptr)
{
if (PyLong_Check (colorspace_obj))
{
long colorspace_id = -1;
- if (! gdb_py_int_as_long (colorspace_obj, &colorspace_id))
+ if (!gdb_py_int_as_long (colorspace_obj, &colorspace_id))
return -1;
if (!color_space_safe_cast (&colorspace, colorspace_id))
error (_("colorspace %ld is out of range."), colorspace_id);
@@ -201,11 +210,11 @@ colorpy_init (PyObject *self, PyObject *args, PyObject *kwds)
else if (PyLong_Check (value_obj))
{
long value = -1;
- if (! gdb_py_int_as_long (value_obj, &value))
+ if (!gdb_py_int_as_long (value_obj, &value))
return -1;
if (value < 0 || value > INT_MAX)
error (_("value %ld is out of range."), value);
- if (colorspace_obj)
+ if (colorspace_obj != nullptr)
obj->color = ui_file_style::color (colorspace, value);
else
obj->color = ui_file_style::color (value);
@@ -256,7 +265,6 @@ colorpy_init (PyObject *self, PyObject *args, PyObject *kwds)
return gdbpy_handle_gdb_exception (-1, except);
}
- Py_INCREF (self);
return 0;
}
@@ -272,10 +280,10 @@ colorpy_str (PyObject *self)
static int
gdbpy_initialize_color (void)
{
- for (auto & pair : colorspace_constants)
- if (PyModule_AddIntConstant (gdb_module, pair.name,
- static_cast<long> (pair.value)) < 0)
- return -1;
+ for (auto &pair : colorspace_constants)
+ if (PyModule_AddIntConstant (gdb_module, pair.name,
+ static_cast<long> (pair.value)) < 0)
+ return -1;
colorpy_object_type.tp_new = PyType_GenericNew;
return gdbpy_type_ready (&colorpy_object_type, gdb_module);
@@ -285,7 +293,8 @@ gdbpy_initialize_color (void)
static PyMethodDef color_methods[] =
{
- { "escape_sequence", colorpy_escape_sequence, METH_O,
+ { "escape_sequence", (PyCFunction) colorpy_escape_sequence,
+ METH_VARARGS | METH_KEYWORDS,
"escape_sequence (is_foreground) -> str.\n\
Return the ANSI escape sequence for this color.\n\
IS_FOREGROUND indicates whether this is a foreground or background color."},
@@ -313,7 +322,7 @@ PyTypeObject colorpy_object_type =
get_attr, /*tp_getattro*/
0, /*tp_setattro*/
0, /*tp_as_buffer*/
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
+ Py_TPFLAGS_DEFAULT, /*tp_flags*/
"GDB color object", /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
diff --git a/gdb/python/py-disasm.c b/gdb/python/py-disasm.c
index 9ca8d22..17064dc 100644
--- a/gdb/python/py-disasm.c
+++ b/gdb/python/py-disasm.c
@@ -1311,12 +1311,13 @@ gdbpy_print_insn (struct gdbarch *gdbarch, CORE_ADDR memaddr,
return {};
}
- /* Check the result is a DisassemblerResult (or a sub-class). */
- if (!PyObject_IsInstance (result.get (),
- (PyObject *) &disasm_result_object_type))
+ /* Check the result is a DisassemblerResult. */
+ if (!PyObject_TypeCheck (result.get (), &disasm_result_object_type))
{
- PyErr_SetString (PyExc_TypeError,
- _("Result is not a DisassemblerResult."));
+ PyErr_Format
+ (PyExc_TypeError,
+ _("Result from Disassembler must be gdb.DisassemblerResult, not %s."),
+ Py_TYPE (result.get ())->tp_name);
gdbpy_print_stack ();
return std::optional<int> (-1);
}
diff --git a/gdb/python/py-param.c b/gdb/python/py-param.c
index 763680e..06237b6 100644
--- a/gdb/python/py-param.c
+++ b/gdb/python/py-param.c
@@ -495,7 +495,11 @@ get_doc_string (PyObject *object, enum doc_string_type doc_type,
}
}
- if (result == nullptr)
+ /* For the set/show docs, if these strings are empty then we set then to
+ a non-empty string. This ensures that the command has some sane
+ documentation for its 'help' text. */
+ if (result == nullptr
+ || (doc_type != doc_string_description && *result == '\0'))
{
if (doc_type == doc_string_description)
result.reset (xstrdup (_("This command is not documented.")));
@@ -904,6 +908,18 @@ parmpy_init (PyObject *self, PyObject *args, PyObject *kwds)
show_doc = get_doc_string (self, doc_string_show, name);
doc = get_doc_string (self, doc_string_description, cmd_name.get ());
+ /* The set/show docs should always be a non-empty string. */
+ gdb_assert (set_doc != nullptr && *set_doc != '\0');
+ gdb_assert (show_doc != nullptr && *show_doc != '\0');
+
+ /* For the DOC string only, if it is the empty string, then we convert it
+ to NULL. This means GDB will not even display a blank line for this
+ part of the help text, instead the set/show line is all the user will
+ get. */
+ gdb_assert (doc != nullptr);
+ if (*doc == '\0')
+ doc = nullptr;
+
Py_INCREF (self);
try
diff --git a/gdb/python/py-registers.c b/gdb/python/py-registers.c
index 78a806c..9be2e3c 100644
--- a/gdb/python/py-registers.c
+++ b/gdb/python/py-registers.c
@@ -403,8 +403,7 @@ gdbpy_parse_register_id (struct gdbarch *gdbarch, PyObject *pyo_reg_id,
PyErr_SetString (PyExc_ValueError, "Bad register");
}
/* The register could be a gdb.RegisterDescriptor object. */
- else if (PyObject_IsInstance (pyo_reg_id,
- (PyObject *) &register_descriptor_object_type))
+ else if (PyObject_TypeCheck (pyo_reg_id, &register_descriptor_object_type))
{
register_descriptor_object *reg
= (register_descriptor_object *) pyo_reg_id;
diff --git a/gdb/python/py-unwind.c b/gdb/python/py-unwind.c
index ab34971..d43d7e9 100644
--- a/gdb/python/py-unwind.c
+++ b/gdb/python/py-unwind.c
@@ -929,9 +929,9 @@ frame_unwind_python::sniff (const frame_info_ptr &this_frame,
/* Received UnwindInfo, cache data. */
PyObject *pyo_unwind_info = PyTuple_GET_ITEM (pyo_execute_ret.get (), 0);
- if (PyObject_IsInstance (pyo_unwind_info,
- (PyObject *) &unwind_info_object_type) <= 0)
- error (_("A Unwinder should return gdb.UnwindInfo instance."));
+ if (!PyObject_TypeCheck (pyo_unwind_info, &unwind_info_object_type))
+ error (_("an Unwinder should return gdb.UnwindInfo, not %s."),
+ Py_TYPE (pyo_unwind_info)->tp_name);
{
unwind_info_object *unwind_info =
diff --git a/gdb/python/python.c b/gdb/python/python.c
index 7e28eb6..24cb511 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -1612,16 +1612,19 @@ gdbpy_flush (PyObject *self, PyObject *args, PyObject *kw)
{
case 1:
{
- gdb_flush (gdb_stderr);
+ if (gdb_stderr != nullptr)
+ gdb_flush (gdb_stderr);
break;
}
case 2:
{
- gdb_flush (gdb_stdlog);
+ if (gdb_stdlog != nullptr)
+ gdb_flush (gdb_stdlog);
break;
}
default:
- gdb_flush (gdb_stdout);
+ if (gdb_stdout != nullptr)
+ gdb_flush (gdb_stdout);
}
Py_RETURN_NONE;