Argparse+cmd2递归错误,即使在简单的程序上也是如此



示例代码:

import argparse
import cmd2
import sys

def create_subparser(a_parser = None):
if a_parser is None:
new_parser = argparse.ArgumentParser()
else:
new_parser = a_parser.add_parser("single")
new_parser.add_argument("--single-param", "-sp")
return new_parser

def single_print(some_param):
print("single operation " + str(some_param))

def other_print():
print("other operation")

class TestCmd(cmd2.Cmd):
single_parser = create_subparser()
@cmd2.with_argparser(single_parser)
def do_single(self, opts):
print(opts)
single_print(opts.single_param)
def do_other(self, opts):
other_print()

if __name__ == "__main__":
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(help="commands", dest="mode")
create_subparser(subparsers)
cmd_parser = subparsers.add_parser("cmd", help="shell")
args = parser.parse_args()
if args.mode == "single":
single_print(args.single_param)
else:
sys.argv = [sys.argv[0]]  # cmd2 complains about unknown parameters without
cmd = TestCmd()
cmd.cmdloop()

在这个例子中,我试图制作一个可以在单模(program.py single -sp test(或cmd/提示模式(program.py cmd(下运行的程序,然后它将调用单模或其他操作(在shell中,写single -sp testother。无论如何,如果我试图获取cmd2类内do_single中使用的argparse的所有值,我就会得到EXCEPTION of type 'RecursionError' occurred with message: 'maximum recursion depth exceeded in comparison'。使用cmd2的调试,它会变成:

Traceback (most recent call last):
File "/home/.../site-packages/cmd2/cmd2.py", line 2011, in onecmd_plus_hooks
stop = self.onecmd(statement, add_to_history=add_to_history)
File "/home/.../site-packages/cmd2/cmd2.py", line 2441, in onecmd
stop = func(statement)
File "/home/.../site-packages/cmd2/decorators.py", line 308, in cmd_wrapper
return func(*args_list, **kwargs)
File "test.py", line 28, in do_single
print(opts)
File "/usr/lib/python3.7/argparse.py", line 131, in __repr__
arg_strings.append('%s=%r' % (name, value))
File "/usr/lib/python3.7/argparse.py", line 131, in __repr__
arg_strings.append('%s=%r' % (name, value))
File "/usr/lib/python3.7/argparse.py", line 131, in __repr__
arg_strings.append('%s=%r' % (name, value))
[Previous line repeated 326 more times]
File "/usr/lib/python3.7/argparse.py", line 129, in __repr__
for name, value in self._get_kwargs():
File "/usr/lib/python3.7/argparse.py", line 139, in _get_kwargs
return sorted(self.__dict__.items())
RecursionError: maximum recursion depth exceeded in comparison

我之所以使用create_subparser函数,是因为我正在创建cmd2.Cmd子类要使用的子解析器,但当从主shell调用时,我希望对模式使用相同的解析器。这基本上是一个最低限度的工作示例,我的代码有更多的标志等等。

还有一件事,这个问题似乎是因为print(opts)而发生的。它似乎试图执行vars(opts),当它试图枚举所有变量时,由于某个地方的递归,它会耗尽内存。如果我不尝试列出对象内的所有变量(例如,如果我只使用opts.single_param(,它就会起作用。这似乎与我试图在cmd2内部和主程序中使用相同的argparser有关,但我不明白为什么会发生这种情况,以及是否可以修复。

如果没有cmd2,我就无法运行您的代码。但我可以创建一个命名空间来展示您的问题。

In [429]: ns = argparse.Namespace(foo='bar', baz=12)                                                 
In [430]: ns.ns = ns                                                                                 
In [431]: print(ns)                                                                                  
---------------------------------------------------------------------------
RecursionError                            Traceback (most recent call last)
<ipython-input-431-0769451eb86e> in <module>
----> 1 print(ns)
/usr/lib/python3.6/argparse.py in __repr__(self)
133         for name, value in self._get_kwargs():
134             if name.isidentifier():
--> 135                 arg_strings.append('%s=%r' % (name, value))
136             else:
137                 star_args[name] = value
... last 1 frames repeated, from the frame below ...
/usr/lib/python3.6/argparse.py in __repr__(self)
133         for name, value in self._get_kwargs():
134             if name.isidentifier():
--> 135                 arg_strings.append('%s=%r' % (name, value))
136             else:
137                 star_args[name] = value
RecursionError: maximum recursion depth exceeded while calling a Python object

你可以测试各个属性,直到找到问题所在:

In [432]: list(ns.__dict__.keys())                                                                   
Out[432]: ['foo', 'baz', 'ns']

最新更新