Skip to content

RecursionError when printing argparse Namespace #983

@kmvanbrunt

Description

@kmvanbrunt

If I call print(args) in a do_* command function, I get the following stack trace.

Traceback (most recent call last):
File "/home/user/PycharmProjects/cmd2/cmd2/cmd2.py", line 2021, in onecmd_plus_hooks
stop = self.onecmd(statement, add_to_history=add_to_history)
File "/home/user/PycharmProjects/cmd2/cmd2/cmd2.py", line 2451, in onecmd
stop = func(statement)
File "/home/user/PycharmProjects/cmd2/cmd2/decorators.py", line 309, in cmd_wrapper
return func(*args_list, **kwargs)
File "/home/user/PycharmProjects/cmd2/cmd2/cmd2.py", line 2686, in do_alias
print(args)
File "/home/user/miniconda3/envs/cmd2/lib/python3.7/argparse.py", line 122, in repr
arg_strings.append('%s=%r' % (name, value))
File "/home/user/miniconda3/envs/cmd2/lib/python3.7/argparse.py", line 122, in repr
arg_strings.append('%s=%r' % (name, value))
File "/home/user/miniconda3/envs/cmd2/lib/python3.7/argparse.py", line 122, in repr
arg_strings.append('%s=%r' % (name, value))
[Previous line repeated 993 more times]
File "/home/user/miniconda3/envs/cmd2/lib/python3.7/argparse.py", line 120, in repr
for name, value in self._get_kwargs():
File "/home/user/miniconda3/envs/cmd2/lib/python3.7/argparse.py", line 130, in _get_kwargs
return sorted(self.dict.items())
RecursionError: maximum recursion depth exceeded while calling a Python object
EXCEPTION of type 'RecursionError' occurred with message: 'maximum recursion depth exceeded while calling a Python object'

I've narrowed down the cause to this line in decorators.py

setattr(ns, 'get_handler', types.MethodType(get_handler, ns))

This change fixes it

setattr(ns, 'get_handler', functools.partial(get_handler, ns))

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions