Skip to content
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

Crash when trying to show globals() (__repr__ returned non-string) #14435

Closed
trag1c opened this issue May 11, 2024 · 3 comments
Closed

Crash when trying to show globals() (__repr__ returned non-string) #14435

trag1c opened this issue May 11, 2024 · 3 comments
Milestone

Comments

@trag1c
Copy link

trag1c commented May 11, 2024

Keywords searched: globals, __repr__

λ ~ :: ipython
Python 3.12.0 (main, Oct 20 2023, 00:04:04) [Clang 15.0.0 (clang-1500.0.40.1)]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.24.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: globals()
Out[1]: ---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
File ~/.pyenv/versions/3.12.0/lib/python3.12/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 ~/.pyenv/versions/3.12.0/lib/python3.12/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 ~/.pyenv/versions/3.12.0/lib/python3.12/site-packages/IPython/lib/pretty.py:693, in _dict_pprinter_factory.<locals>.inner(obj, p, cycle)
    691
    692     p.text(': ')
--> 693     p.pretty(obj[key])
    694 p.end_group(step, end)

File ~/.pyenv/versions/3.12.0/lib/python3.12/site-packages/IPython/lib/pretty.py:411, in RepresentationPrinter.pretty(self, obj)
    408                         return meth(obj, self, cycle)
    409                 if cls is not object \
    410                         and callable(cls.__dict__.get('__repr__')):
--> 411                     return _repr_pprint(obj, self, cycle)
    413     return _default_pprint(obj, self, cycle)
    414 finally:

File ~/.pyenv/versions/3.12.0/lib/python3.12/site-packages/IPython/lib/pretty.py:779, in _repr_pprint(obj, p, cycle)
    777 """A pprint that just redirects to the normal repr function."""
    778 # Find newlines and replace them with p.break_()
--> 779 output = repr(obj)
    780 lines = output.splitlines()
    781 with p.group():

TypeError: __repr__ returned non-string (type NoneType)
λ ~ ::
@Carreau
Copy link
Member

Carreau commented May 14, 2024

Do you have any custom configuration ?

I can't reproduce so it's hard to find which object returns a None repr:

$ ipython
Python 3.12.3 | packaged by conda-forge | (main, Apr 15 2024, 18:35:20) [Clang 16.0.6 ]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.24.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: globals()
Out[1]:
{'__name__': '__main__',
 '__doc__': 'Automatically created module for IPython interactive environment',
 '__package__': None,
 '__loader__': None,
 '__spec__': None,
 '__builtin__': <module 'builtins' (built-in)>,
 '__builtins__': <module 'builtins' (built-in)>,
 '_ih': ['', 'globals()'],
 '_oh': {},
 '_dh': [PosixPath('/.../ipython')],
 'In': ['', 'globals()'],
 'Out': {},
 'get_ipython': <bound method InteractiveShell.get_ipython of <IPython.terminal.interactiveshell.TerminalInteractiveShell object at 0x105733d70>>,
 'exit': <IPython.core.autocall.ExitAutocall at 0x10582f2f0>,
 'quit': <IPython.core.autocall.ExitAutocall at 0x10582f2f0>,
 'open': <function _io.open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)>,
 '_': '',
 '__': '',
 '___': '',
 'Module': ast.Module,
 'FunctionDef': ast.FunctionDef,
 'Pass': ast.Pass,
 'arguments': ast.arguments,
 'indent': <function textwrap.indent(text, prefix, predicate=None)>,
 'formatter': <IPython.core.formatters.PlainTextFormatter at 0x10582dfa0>,
 'repr_module': <function __main__.repr_module(module, p, cycle)>,
 'arg': <function __main__.arg(p, obj, name)>,
 'repr_FunctionDef': <function __main__.repr_FunctionDef(fun, p, cycle)>,
 'ast': <module 'ast' from '...'>,
 '_i': '',
 '_ii': '',
 '_iii': '',
 '_i1': 'globals()'}

In [2]:

@trag1c
Copy link
Author

trag1c commented May 14, 2024

Yup, found the culprit—it's exit and quit, caused by my startup file:

type(exit).__repr__ = lambda self: self()

This "backports" IPython's behavior of quitting with just exit instead of exit() to the built-in REPL. I didn't realize IPython was also using this startup file 😄 And I guess I thought this was an IPython issue since the default REPL doesn't exit since it doesn't include exit nor quit in its globals().

I guess that makes this issue invalid?

@Carreau
Copy link
Member

Carreau commented May 14, 2024

ok, all good then.

Yes I'm going to close as I don't think there is much actionable.

But thanks for opening the issue just in case.

@Carreau Carreau closed this as completed May 14, 2024
@Carreau Carreau added this to the no action milestone May 14, 2024
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

No branches or pull requests

2 participants