summaryrefslogtreecommitdiff
path: root/gdb/python/lib/gdb/printing.py
diff options
context:
space:
mode:
authorSimon Marchi <simon.marchi@polymtl.ca>2021-05-07 10:56:20 -0400
committerSimon Marchi <simon.marchi@polymtl.ca>2021-05-07 10:56:20 -0400
commit13123da89a2c7e06a5312ca6b4b24c68ba1c6c2d (patch)
treeec33a85ccb58f44445608d0cab68020fa588cb74 /gdb/python/lib/gdb/printing.py
parenta9b49cbcd5935a713da5715799ea3b24e0a52851 (diff)
downloadbinutils-gdb-13123da89a2c7e06a5312ca6b4b24c68ba1c6c2d.tar.gz
gdb: re-format Python files using black 21.4b0
Re-format all Python files using black [1] version 21.4b0. The goal is that from now on, we keep all Python files formatted using black. And that we never have to discuss formatting during review (for these files at least) ever again. One change is needed in gdb.python/py-prettyprint.exp, because it matches the string representation of an exception, which shows source code. So the change in formatting must be replicated in the expected regexp. To document our usage of black I plan on adding this to the "GDB Python Coding Standards" wiki page [2]: --8<-- All Python source files under the `gdb/` directory must be formatted using black version 21.4b0. This specific version can be installed using: $ pip3 install 'black == 21.4b0' All you need to do to re-format files is run `black <file/directory>`, and black will re-format any Python file it finds in there. It runs quite fast, so the simplest is to do: $ black gdb/ from the top-level. If you notice that black produces changes unrelated to your patch, it's probably because someone forgot to run it before you. In this case, don't include unrelated hunks in your patch. Push an obvious patch fixing the formatting and rebase your work on top of that. -->8-- Once this is merged, I plan on setting a up an `ignoreRevsFile` config so that git-blame ignores this commit, as described here: https://github.com/psf/black#migrating-your-code-style-without-ruining-git-blame I also plan on working on a git commit hook (checked in the repo) to automatically check the formatting of the Python files on commit. [1] https://pypi.org/project/black/ [2] https://sourceware.org/gdb/wiki/Internals%20GDB-Python-Coding-Standards gdb/ChangeLog: * Re-format all Python files using black. gdb/testsuite/ChangeLog: * Re-format all Python files using black. * gdb.python/py-prettyprint.exp (run_lang_tests): Adjust. Change-Id: I28588a22c2406afd6bc2703774ddfff47cd61919
Diffstat (limited to 'gdb/python/lib/gdb/printing.py')
-rw-r--r--gdb/python/lib/gdb/printing.py27
1 files changed, 16 insertions, 11 deletions
diff --git a/gdb/python/lib/gdb/printing.py b/gdb/python/lib/gdb/printing.py
index 0fecb3d538f..aaa0115224f 100644
--- a/gdb/python/lib/gdb/printing.py
+++ b/gdb/python/lib/gdb/printing.py
@@ -26,6 +26,7 @@ if sys.version_info[0] > 2:
basestring = str
long = int
+
class PrettyPrinter(object):
"""A basic pretty-printer.
@@ -110,22 +111,23 @@ def register_pretty_printer(obj, printer, replace=False):
if not hasattr(printer, "__name__") and not hasattr(printer, "name"):
raise TypeError("printer missing attribute: name")
if hasattr(printer, "name") and not hasattr(printer, "enabled"):
- raise TypeError("printer missing attribute: enabled")
+ raise TypeError("printer missing attribute: enabled")
if not hasattr(printer, "__call__"):
raise TypeError("printer missing attribute: __call__")
if hasattr(printer, "name"):
- name = printer.name
+ name = printer.name
else:
- name = printer.__name__
+ name = printer.__name__
if obj is None or obj is gdb:
if gdb.parameter("verbose"):
gdb.write("Registering global %s pretty-printer ...\n" % name)
obj = gdb
else:
if gdb.parameter("verbose"):
- gdb.write("Registering %s pretty-printer for %s ...\n" % (
- name, obj.filename))
+ gdb.write(
+ "Registering %s pretty-printer for %s ...\n" % (name, obj.filename)
+ )
# Printers implemented as functions are old-style. In order to not risk
# breaking anything we do not check __name__ here.
@@ -148,8 +150,9 @@ def register_pretty_printer(obj, printer, replace=False):
del obj.pretty_printers[i]
break
else:
- raise RuntimeError("pretty-printer already registered: %s" %
- printer.name)
+ raise RuntimeError(
+ "pretty-printer already registered: %s" % printer.name
+ )
i = i + 1
obj.pretty_printers.insert(0, printer)
@@ -197,8 +200,7 @@ class RegexpCollectionPrettyPrinter(PrettyPrinter):
# cumbersome to make a regexp of a regexp). So now the name is a
# separate parameter.
- self.subprinters.append(self.RegexpSubprinter(name, regexp,
- gen_printer))
+ self.subprinters.append(self.RegexpSubprinter(name, regexp, gen_printer))
def __call__(self, val):
"""Lookup the pretty-printer for the provided value."""
@@ -220,6 +222,7 @@ class RegexpCollectionPrettyPrinter(PrettyPrinter):
# Cannot find a pretty printer. Return None.
return None
+
# A helper class for printing enum types. This class is instantiated
# with a list of enumerators to print a particular Value.
class _EnumInstance:
@@ -238,9 +241,10 @@ class _EnumInstance:
any_found = True
if not any_found or v != 0:
# Leftover value.
- flag_list.append('<unknown: 0x%x>' % v)
+ flag_list.append("<unknown: 0x%x>" % v)
return "0x%x [%s]" % (int(self.val), " | ".join(flag_list))
+
class FlagEnumerationPrinter(PrettyPrinter):
"""A pretty-printer which can be used to print a flag-style enumeration.
A flag-style enumeration is one where the enumerators are or'd
@@ -263,7 +267,7 @@ class FlagEnumerationPrinter(PrettyPrinter):
self.enumerators.append((field.name, field.enumval))
# Sorting the enumerators by value usually does the right
# thing.
- self.enumerators.sort(key = lambda x: x[1])
+ self.enumerators.sort(key=lambda x: x[1])
if self.enabled:
return _EnumInstance(self.enumerators, val)
@@ -281,5 +285,6 @@ register_pretty_printer(None, _builtin_pretty_printers)
# Add a builtin pretty-printer.
+
def add_builtin_pretty_printer(name, regexp, printer):
_builtin_pretty_printers.add_printer(name, regexp, printer)