Skip to content

Fix re pattern pretty print #14558

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed

Fix re pattern pretty print #14558

wants to merge 1 commit into from

Conversation

wimglenn
Copy link
Contributor

@wimglenn wimglenn commented Oct 24, 2024

re.TEMPLATE is gone in Python 3.13 resulting in a crash in the pattern pretty printer

Reproducer:

>>> import re
>>> re.compile("")
Click me to see the traceback!
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
File ~/.local/lib/python3.13/site-packages/IPython/core/formatters.py:711, in PlainTextFormatter.__call__(self, obj)
    704 stream = StringIO()
    705 printer = pretty.RepresentationPrinter(stream, self.verbose,
    706     self.max_width, self.newline,
    707     max_seq_length=self.max_seq_length,
    708     singleton_pprinters=self.singleton_printers,
    709     type_pprinters=self.type_printers,
    710     deferred_pprinters=self.deferred_printers)
--> 711 printer.pretty(obj)
    712 printer.flush()
    713 return stream.getvalue()

File ~/.local/lib/python3.13/site-packages/IPython/lib/pretty.py:394, in RepresentationPrinter.pretty(self, obj)
    391 for cls in _get_mro(obj_class):
    392     if cls in self.type_pprinters:
    393         # printer registered in self.type_pprinters
--> 394         return self.type_pprinters[cls](obj, self, cycle)
    395     else:
    396         # deferred printer
    397         printer = self._in_deferred_types(cls)

File ~/.local/lib/python3.13/site-packages/IPython/lib/pretty.py:740, in _re_pattern_pprint(obj, p, cycle)
    738 re_compile = CallExpression.factory('re.compile')
    739 if obj.flags:
--> 740     p.pretty(re_compile(RawStringLiteral(obj.pattern), _ReFlags(obj.flags)))
    741 else:
    742     p.pretty(re_compile(RawStringLiteral(obj.pattern)))

File ~/.local/lib/python3.13/site-packages/IPython/lib/pretty.py:408, in RepresentationPrinter.pretty(self, obj)
    406     meth = cls._repr_pretty_
    407     if callable(meth):
--> 408         return meth(obj, self, cycle)
    409 if (
    410     cls is not object
    411     # check if cls defines __repr__
   (...)
    417     and callable(_safe_getattr(cls, "__repr__", None))
    418 ):
    419     return _repr_pprint(obj, self, cycle)

File ~/.local/lib/python3.13/site-packages/IPython/lib/pretty.py:573, in CallExpression._repr_pretty_(self, p, cycle)
    571 for arg in self.args:
    572     new_item()
--> 573     p.pretty(arg)
    574 for arg_name, arg in self.kwargs.items():
    575     new_item()

File ~/.local/lib/python3.13/site-packages/IPython/lib/pretty.py:408, in RepresentationPrinter.pretty(self, obj)
    406     meth = cls._repr_pretty_
    407     if callable(meth):
--> 408         return meth(obj, self, cycle)
    409 if (
    410     cls is not object
    411     # check if cls defines __repr__
   (...)
    417     and callable(_safe_getattr(cls, "__repr__", None))
    418 ):
    419     return _repr_pprint(obj, self, cycle)

File ~/.local/lib/python3.13/site-packages/IPython/lib/pretty.py:729, in _ReFlags._repr_pretty_(self, p, cycle)
    726 done_one = False
    727 for flag in ('TEMPLATE', 'IGNORECASE', 'LOCALE', 'MULTILINE', 'DOTALL',
    728     'UNICODE', 'VERBOSE', 'DEBUG'):
--> 729     if self.value & getattr(re, flag):
    730         if done_one:
    731             p.text('|')

AttributeError: module 're' has no attribute 'TEMPLATE'

Apparently it was "undocumented and never working" (#32300). That was reverted and then usage deprecated immediately in #93161.

Status of existing re.TEMPLATE attribute in currently supported Python versions for IPython:

Since 3.11, using it in the first place causes warnings:

$ python3.11 -Wall
Python 3.11.10 (main, Oct 16 2024, 10:51:55) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> re.compile("", flags=re.TEMPLATE)
.../Python-3.11.10/lib/python3.11/re/__init__.py:289: DeprecationWarning: The re.TEMPLATE/re.T flag is deprecated as it is an undocumented flag without an obvious purpose. Don't use it.
  warnings.warn("The re.TEMPLATE/re.T flag is deprecated "
re.compile('', re.TEMPLATE)

#14559 offers an alternative approach, but it will change the pretty-print result for 3.10-3.12, I'm not sure if that would be considered a backwards-compatibility breaking change.

wimglenn added a commit to wimglenn/ipython that referenced this pull request Oct 24, 2024
Carreau added a commit that referenced this pull request Oct 25, 2024
@Carreau
Copy link
Member

Carreau commented Oct 25, 2024

closing as I merged your #14559 alternative. Thanks.

@Carreau Carreau closed this Oct 25, 2024
@Carreau Carreau added this to the 8.29 milestone Oct 25, 2024
@wimglenn wimglenn deleted the patch-1 branch October 25, 2024 17:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants