Re: [committed] libstdc++: Reformat Python code

2023-11-13 Thread Romain GEISSLER
> Le 13 nov. 2023 à 16:24, Jonathan Wakely  a écrit :
> 
> Yes, I'll do that backport (and most of the other Python improvements
> too, at least for gcc-13).
> 
> Thanks for raising it.
> 

Cool thanks ! ;)

In the meantime, in my own toolchains I have silenced (without fixing it) the 
warnings
with this simple patch (in case anyone else wants to quickly get rid of it, but 
it’s not
really a good long term workaround).

--- libstdc++-v3/python/libstdcxx/v6/__init__.py
+++ libstdc++-v3/python/libstdcxx/v6/__init__.py
@@ -1 +1,2 @@
-
+import warnings
+warnings.filterwarnings("ignore", category=SyntaxWarning)

Re: [committed] libstdc++: Reformat Python code

2023-11-13 Thread Jonathan Wakely
On Mon, 13 Nov 2023 at 14:41, Romain GEISSLER
 wrote:
>
> > Le 28 sept. 2023 à 22:21, Jonathan Wakely  a écrit :
> >
> > Tested x86_64-linux (GDB 13.2, Python 3.11). Pushed to trunk.
> >
> > -- >8 --
> >
> > Some of these changes were suggested by autopep8's --aggressive
> > option, others are for readability.
> >
> > Break long lines by splitting strings across multiple lines, or
> > introducing local variables to hold results.
> >
> > Use raw strings for regular expressions, so that backslashes don't need
> > to be escaped.
>
> Hi Jonathan,
>
> FYI, it seems that with python 3.12, the bits "Use raw strings for regular 
> expressions"
> seems to fix the following new Python warnings:
>
> /opt/1A/toolchain/x86_64-v23.0.19/lib/../share/gcc-13.2.1/python/libstdcxx/v6/printers.py:1273:
>  SyntaxWarning: invalid escape sequence '\d'
>   self.typename = re.sub('^std::experimental::fundamentals_v\d::', 
> 'std::experimental::', self.typename, 1)
> /opt/1A/toolchain/x86_64-v23.0.19/lib/../share/gcc-13.2.1/python/libstdcxx/v6/printers.py:1302:
>  SyntaxWarning: invalid escape sequence '\w'
>   x = re.sub("std::string(?!\w)", s, m.group(1))
>  … (snapped, there are a bit more than that in total).
>
> How ok would it be to backport to the branches still maintained the "raw 
> string" fix,
> in order to avoid deprecation warnings as soon as people use gdb with python 
> >= 3.12 ?

Yes, I'll do that backport (and most of the other Python improvements
too, at least for gcc-13).

Thanks for raising it.



Re: [committed] libstdc++: Reformat Python code

2023-11-13 Thread Romain GEISSLER
> Le 28 sept. 2023 à 22:21, Jonathan Wakely  a écrit :
> 
> Tested x86_64-linux (GDB 13.2, Python 3.11). Pushed to trunk.
> 
> -- >8 --
> 
> Some of these changes were suggested by autopep8's --aggressive
> option, others are for readability.
> 
> Break long lines by splitting strings across multiple lines, or
> introducing local variables to hold results.
> 
> Use raw strings for regular expressions, so that backslashes don't need
> to be escaped.

Hi Jonathan,

FYI, it seems that with python 3.12, the bits "Use raw strings for regular 
expressions"
seems to fix the following new Python warnings:

/opt/1A/toolchain/x86_64-v23.0.19/lib/../share/gcc-13.2.1/python/libstdcxx/v6/printers.py:1273:
 SyntaxWarning: invalid escape sequence '\d'
  self.typename = re.sub('^std::experimental::fundamentals_v\d::', 
'std::experimental::', self.typename, 1)
/opt/1A/toolchain/x86_64-v23.0.19/lib/../share/gcc-13.2.1/python/libstdcxx/v6/printers.py:1302:
 SyntaxWarning: invalid escape sequence '\w'
  x = re.sub("std::string(?!\w)", s, m.group(1))
 … (snapped, there are a bit more than that in total).

How ok would it be to backport to the branches still maintained the "raw 
string" fix,
in order to avoid deprecation warnings as soon as people use gdb with python >= 
3.12 ?

Thanks,
Romain

[committed] libstdc++: Reformat Python code

2023-09-28 Thread Jonathan Wakely
Tested x86_64-linux (GDB 13.2, Python 3.11). Pushed to trunk.

-- >8 --

Some of these changes were suggested by autopep8's --aggressive
option, others are for readability.

Break long lines by splitting strings across multiple lines, or
introducing local variables to hold results.

Use raw strings for regular expressions, so that backslashes don't need
to be escaped.

libstdc++-v3/ChangeLog:

* python/libstdcxx/v6/printers.py: Break long lines. Use raw
strings for regular expressions. Add whitespace around
operators.
(is_member_of_namespace): Use isinstance to check type.
(is_specialization_of): Likewise. Adjust template_name
for versioned namespace instead of duplicating the re.match
call.
(StdExpAnyPrinter._string_types): New static method.
(StdExpAnyPrinter.to_string): Use _string_types.
---
 libstdc++-v3/python/libstdcxx/v6/printers.py | 122 ---
 1 file changed, 75 insertions(+), 47 deletions(-)

diff --git a/libstdc++-v3/python/libstdcxx/v6/printers.py 
b/libstdc++-v3/python/libstdcxx/v6/printers.py
index 7889235ce1c..3f22ba23452 100644
--- a/libstdc++-v3/python/libstdcxx/v6/printers.py
+++ b/libstdc++-v3/python/libstdcxx/v6/printers.py
@@ -145,7 +145,6 @@ def lookup_templ_spec(templ, *args):
 
 # Use this to find container node types instead of find_type,
 # see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91997 for details.
-
 def lookup_node_type(nodename, containertype):
 """
 Lookup specialization of template nodename corresponding to containertype.
@@ -188,7 +187,7 @@ def is_member_of_namespace(typ, *namespaces):
 Test whether a type is a member of one of the specified namespaces.
 The type can be specified as a string or a gdb.Type object.
 """
-if type(typ) is gdb.Type:
+if isinstance(typ, gdb.Type):
 typ = str(typ)
 typ = strip_versioned_namespace(typ)
 for namespace in namespaces:
@@ -205,10 +204,10 @@ def is_specialization_of(x, template_name):
 without any 'std' qualification.
 """
 global _versioned_namespace
-if type(x) is gdb.Type:
+if isinstance(x, gdb.Type):
 x = x.tag
 if _versioned_namespace:
-return re.match('^std::(%s)?%s<.*>$' % (_versioned_namespace, 
template_name), x) is not None
+template_name = '(%s)?%s' % (_versioned_namespace, template_name)
 return re.match('^std::%s<.*>$' % template_name, x) is not None
 
 
@@ -225,9 +224,9 @@ def strip_inline_namespaces(type_str):
 type_str = type_str.replace('std::__cxx11::', 'std::')
 expt_ns = 'std::experimental::'
 for lfts_ns in ('fundamentals_v1', 'fundamentals_v2'):
-type_str = type_str.replace(expt_ns+lfts_ns+'::', expt_ns)
+type_str = type_str.replace(expt_ns + lfts_ns + '::', expt_ns)
 fs_ns = expt_ns + 'filesystem::'
-type_str = type_str.replace(fs_ns+'v1::', fs_ns)
+type_str = type_str.replace(fs_ns + 'v1::', fs_ns)
 return type_str
 
 
@@ -365,7 +364,8 @@ class UniquePointerPrinter:
 return SmartPtrIterator(unique_ptr_get(self.val))
 
 def to_string(self):
-return ('std::unique_ptr<%s>' % 
(str(self.val.type.template_argument(0
+t = self.val.type.template_argument(0)
+return 'std::unique_ptr<{}>'.format(str(t))
 
 
 def get_value_from_aligned_membuf(buf, valtype):
@@ -597,7 +597,8 @@ class StdBitIteratorPrinter:
 def to_string(self):
 if not self.val['_M_p']:
 return 'non-dereferenceable iterator for std::vector'
-return bool(self.val['_M_p'].dereference() & (1 << 
self.val['_M_offset']))
+return bool(self.val['_M_p'].dereference()
+& (1 << self.val['_M_offset']))
 
 
 class StdBitReferencePrinter:
@@ -1087,9 +1088,10 @@ class StdStringStreamPrinter:
 self.val = val
 self.typename = typename
 
-# Check if the stream was redirected:
-# This is essentially: val['_M_streambuf'] == 
val['_M_stringbuf'].address
-# However, GDB can't resolve the virtual inheritance, so we do that 
manually
+# Check if the stream was redirected. This is essentially:
+# val['_M_streambuf'] != val['_M_stringbuf'].address
+# However, GDB can't resolve the virtual inheritance, so we do that
+# manually.
 basetype = [f.type for f in val.type.fields() if f.is_base_class][0]
 gdb.set_convenience_variable('__stream', val.cast(basetype).address)
 self.streambuf = gdb.parse_and_eval('$__stream->rdbuf()')
@@ -1097,7 +1099,8 @@ class StdStringStreamPrinter:
 
 def to_string(self):
 if self.was_redirected:
-return "%s redirected to %s" % (self.typename, 
self.streambuf.dereference())
+return "%s redirected to %s" % (
+self.typename, self.streambuf.dereference())
 return self.val['_M_stringbuf']
 
 def display_hint(self):
@@ -1309,8 +1312,9 @@ class