FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

`SystemExit` not raised from argparse · Issue #12652 · ipython/ipython · GitHub

Repository navigation

SystemExit not raised from argparse #12652

Open

Description

Python 3.8.6 with IPython 7.18.1

Replicate with:

ipython3 -c 'from argparse import ArgumentParser; p=ArgumentParser();p.add_argument("-a");p.parse_args(["-a"])'

Output:

usage: ipython3 [-h] [-a A]
ipython3: error: argument -a: expected one argument
ERROR:root:Internal Python error in the inspect module.
Below is the traceback from this internal error.

Traceback (most recent call last):
  File "/usr/local/Cellar/python@3.8/3.8.6/Frameworks/Python.framework/Versions/3.8/lib/python3.8/argparse.py", line 1800, in parse_known_args
    namespace, args = self._parse_known_args(args, namespace)
  File "/usr/local/Cellar/python@3.8/3.8.6/Frameworks/Python.framework/Versions/3.8/lib/python3.8/argparse.py", line 2006, in _parse_known_args
    start_index = consume_optional(start_index)
  File "/usr/local/Cellar/python@3.8/3.8.6/Frameworks/Python.framework/Versions/3.8/lib/python3.8/argparse.py", line 1936, in consume_optional
    arg_count = match_argument(action, selected_patterns)
  File "/usr/local/Cellar/python@3.8/3.8.6/Frameworks/Python.framework/Versions/3.8/lib/python3.8/argparse.py", line 2101, in _match_argument
    raise ArgumentError(action, msg)
argparse.ArgumentError: argument -a: expected one argument

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3417, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-1-f6b8febebdd0>", line 1, in <module>
    from argparse import ArgumentParser; p = ArgumentParser(); p.add_argument("-a"); p.parse_args(["-a"])
  File "/usr/local/Cellar/python@3.8/3.8.6/Frameworks/Python.framework/Versions/3.8/lib/python3.8/argparse.py", line 1768, in parse_args
    args, argv = self.parse_known_args(args, namespace)
  File "/usr/local/Cellar/python@3.8/3.8.6/Frameworks/Python.framework/Versions/3.8/lib/python3.8/argparse.py", line 1807, in parse_known_args
    self.error(str(err))
  File "/usr/local/Cellar/python@3.8/3.8.6/Frameworks/Python.framework/Versions/3.8/lib/python3.8/argparse.py", line 2521, in error
    self.exit(2, _('%(prog)s: error: %(message)s\n') % args)
  File "/usr/local/Cellar/python@3.8/3.8.6/Frameworks/Python.framework/Versions/3.8/lib/python3.8/argparse.py", line 2508, in exit
    _sys.exit(status)
SystemExit: 2

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/IPython/core/ultratb.py", line 1169, in get_records
    return _fixed_getinnerframes(etb, number_of_lines_of_context, tb_offset)
  File "/usr/local/lib/python3.8/site-packages/IPython/core/ultratb.py", line 316, in wrapped
    return f(*args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/IPython/core/ultratb.py", line 350, in _fixed_getinnerframes
    records = fix_frame_records_filenames(inspect.getinnerframes(etb, context))
  File "/usr/local/Cellar/python@3.8/3.8.6/Frameworks/Python.framework/Versions/3.8/lib/python3.8/inspect.py", line 1503, in getinnerframes
    frameinfo = (tb.tb_frame,) + getframeinfo(tb, context)
AttributeError: 'tuple' object has no attribute 'tb_frame'
---------------------------------------------------------------------------
ArgumentError                             Traceback (most recent call last)
/usr/local/Cellar/python@3.8/3.8.6/Frameworks/Python.framework/Versions/3.8/lib/python3.8/argparse.py in parse_known_args(self, args, namespace)
   1799         try:
-> 1800             namespace, args = self._parse_known_args(args, namespace)
   1801             if hasattr(namespace, _UNRECOGNIZED_ARGS_ATTR):

/usr/local/Cellar/python@3.8/3.8.6/Frameworks/Python.framework/Versions/3.8/lib/python3.8/argparse.py in _parse_known_args(self, arg_strings, namespace)
   2005             # consume the next optional and any arguments for it
-> 2006             start_index = consume_optional(start_index)
   2007 

/usr/local/Cellar/python@3.8/3.8.6/Frameworks/Python.framework/Versions/3.8/lib/python3.8/argparse.py in consume_optional(start_index)
   1935                     selected_patterns = arg_strings_pattern[start:]
-> 1936                     arg_count = match_argument(action, selected_patterns)
   1937                     stop = start + arg_count

/usr/local/Cellar/python@3.8/3.8.6/Frameworks/Python.framework/Versions/3.8/lib/python3.8/argparse.py in _match_argument(self, action, arg_strings_pattern)
   2100                                action.nargs) % action.nargs
-> 2101             raise ArgumentError(action, msg)
   2102 

ArgumentError: argument -a: expected one argument

During handling of the above exception, another exception occurred:

SystemExit                                Traceback (most recent call last)
    [... skipping hidden 1 frame]

<ipython-input-1-f6b8febebdd0> in <module>
----> 1 from argparse import ArgumentParser; p = ArgumentParser(); p.add_argument("-a"); p.parse_args(["-a"])

/usr/local/Cellar/python@3.8/3.8.6/Frameworks/Python.framework/Versions/3.8/lib/python3.8/argparse.py in parse_args(self, args, namespace)
   1767     def parse_args(self, args=None, namespace=None):
-> 1768         args, argv = self.parse_known_args(args, namespace)
   1769         if argv:

/usr/local/Cellar/python@3.8/3.8.6/Frameworks/Python.framework/Versions/3.8/lib/python3.8/argparse.py in parse_known_args(self, args, namespace)
   1806             err = _sys.exc_info()[1]
-> 1807             self.error(str(err))
   1808 

/usr/local/Cellar/python@3.8/3.8.6/Frameworks/Python.framework/Versions/3.8/lib/python3.8/argparse.py in error(self, message)
   2520         args = {'prog': self.prog, 'message': message}
-> 2521         self.exit(2, _('%(prog)s: error: %(message)s\n') % args)

/usr/local/Cellar/python@3.8/3.8.6/Frameworks/Python.framework/Versions/3.8/lib/python3.8/argparse.py in exit(self, status, message)
   2507             self._print_message(message, _sys.stderr)
-> 2508         _sys.exit(status)
   2509 

SystemExit: 2

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
    [... skipping hidden 1 frame]

/usr/local/lib/python3.8/site-packages/IPython/core/interactiveshell.py in showtraceback(self, exc_tuple, filename, tb_offset, exception_only, running_compiled_code)
   2035                     stb = ['An exception has occurred, use %tb to see '
   2036                            'the full traceback.\n']
-> 2037                     stb.extend(self.InteractiveTB.get_exception_only(etype,
   2038                                                                      value))
   2039                 else:

/usr/local/lib/python3.8/site-packages/IPython/core/ultratb.py in get_exception_only(self, etype, value)
    821         value : exception value
    822         """
--> 823         return ListTB.structured_traceback(self, etype, value)
    824 
    825     def show_exception_only(self, etype, evalue):

/usr/local/lib/python3.8/site-packages/IPython/core/ultratb.py in structured_traceback(self, etype, evalue, etb, tb_offset, context)
    696             chained_exceptions_tb_offset = 0
    697             out_list = (
--> 698                 self.structured_traceback(
    699                     etype, evalue, (etb, chained_exc_ids),
    700                     chained_exceptions_tb_offset, context)

/usr/local/lib/python3.8/site-packages/IPython/core/ultratb.py in structured_traceback(self, etype, value, tb, tb_offset, number_of_lines_of_context)
   1433         else:
   1434             self.tb = tb
-> 1435         return FormattedTB.structured_traceback(
   1436             self, etype, value, tb, tb_offset, number_of_lines_of_context)
   1437 

/usr/local/lib/python3.8/site-packages/IPython/core/ultratb.py in structured_traceback(self, etype, value, tb, tb_offset, number_of_lines_of_context)
   1333         if mode in self.verbose_modes:
   1334             # Verbose modes need a full traceback
-> 1335             return VerboseTB.structured_traceback(
   1336                 self, etype, value, tb, tb_offset, number_of_lines_of_context
   1337             )

/usr/local/lib/python3.8/site-packages/IPython/core/ultratb.py in structured_traceback(self, etype, evalue, etb, tb_offset, number_of_lines_of_context)
   1190         """Return a nice text document describing the traceback."""
   1191 
-> 1192         formatted_exception = self.format_exception_as_a_whole(etype, evalue, etb, number_of_lines_of_context,
   1193                                                                tb_offset)
   1194 

/usr/local/lib/python3.8/site-packages/IPython/core/ultratb.py in format_exception_as_a_whole(self, etype, evalue, etb, number_of_lines_of_context, tb_offset)
   1148 
   1149 
-> 1150         last_unique, recursion_repeat = find_recursion(orig_etype, evalue, records)
   1151 
   1152         frames = self.format_records(records, last_unique, recursion_repeat)

/usr/local/lib/python3.8/site-packages/IPython/core/ultratb.py in find_recursion(etype, value, records)
    449     # first frame (from in to out) that looks different.
    450     if not is_recursion_error(etype, value, records):
--> 451         return len(records), 0
    452 
    453     # Select filename, lineno, func_name to track frames with

TypeError: object of type 'NoneType' has no len()

Solution:
Edit /usr/local/lib/python3.8/site-packages/IPython/core/ultratb.py from:

    if not is_recursion_error(etype, value, records):
        return len(records), 0

To:

    if not is_recursion_error(etype, value, records):
        return None if records is None else len(records), 0

Not sure if you want a PR, seeing as the new version fails for a different reason (I've send a PR to master for that #12652)

Activity

  1. anntzer commented on Aug 21, 2022

    Contributor

    I can repro the issue as of master (also in a different context, specifically when a cppimported module (https://github.com/tbenthompson/cppimport) fails to compile. The following simpler patch seems to fix the issue, too:

    diff --git i/IPython/core/ultratb.py w/IPython/core/ultratb.py
    index 856979194..61c26400e 100644
    --- i/IPython/core/ultratb.py
    +++ w/IPython/core/ultratb.py
    @@ -1111,17 +1111,17 @@ def structured_traceback(self, etype=None, value=None, tb=None,
             if etype is None:
                 etype, value, tb = sys.exc_info()
             if isinstance(tb, tuple):
                 # tb is a tuple if this is a chained exception.
                 self.tb = tb[0]
             else:
                 self.tb = tb
             return FormattedTB.structured_traceback(
    -            self, etype, value, tb, tb_offset, number_of_lines_of_context)
    +            self, etype, value, self.tb, tb_offset, number_of_lines_of_context)
     
     
     #---------------------------------------------------------------------------
     
     # A simple class to preserve Nathan's original functionality.
     class ColorTB(FormattedTB):
         """Shorthand to initialize a FormattedTB in Linux colors mode."""
     

    and makes sense from a typing PoV too (tb needs to be normalized to a traceback and not a tuple, which is what the 4 lines just above do).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL