1

尝试使用 swagger codegen 为我正在为工作做的项目自动生成一些客户端库。显然,swagger 本身没有 python 版本,但它们链接到社区支持的 swagger-py 代码生成。用 pip 安装它,尝试在我的项目 OpenAPI repo 上运行,我收到错误“RecursionError:instancecheck中超出最大递归深度”

这是堆栈跟踪:

Traceback (most recent call last):
  File "/home/user/.local/bin/swagger_py_codegen", line 8, in <module>
    sys.exit(generate())
  File "/home/user/.local/lib/python3.8/site-packages/click/core.py", line 722, in __call__
    return self.main(*args, **kwargs)
  File "/home/user/.local/lib/python3.8/site-packages/click/core.py", line 697, in main
    rv = self.invoke(ctx)
  File "/home/user/.local/lib/python3.8/site-packages/click/core.py", line 895, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/user/.local/lib/python3.8/site-packages/click/core.py", line 535, in invoke
    return callback(*args, **kwargs)
  File "/home/user/.local/lib/python3.8/site-packages/swagger_py_codegen/command.py", line 200, in generate
    for code in generator.generate():
  File "/home/user/.local/lib/python3.8/site-packages/swagger_py_codegen/base.py", line 44, in generate
    for code in g:
  File "/home/user/.local/lib/python3.8/site-packages/swagger_py_codegen/base.py", line 47, in generate
    for code in self._process():
  File "/home/user/.local/lib/python3.8/site-packages/swagger_py_codegen/jsonschema.py", line 110, in _process
    yield Schema(build_data(self.swagger))
  File "/home/user/.local/lib/python3.8/site-packages/swagger_py_codegen/jsonschema.py", line 51, in build_data
    for path, _ in swagger.search(['paths', '*']):
  File "/home/user/.local/lib/python3.8/site-packages/swagger_py_codegen/parser.py", line 118, in search
    for p, d in dpath.util.search(
  File "/home/user/.local/lib/python3.8/site-packages/dpath/util.py", line 222, in yielder
    for segments, found in dpath.segments.walk(obj):
  File "/home/user/.local/lib/python3.8/site-packages/dpath/segments.py", line 73, in walk
    for found in walk(v, location + (k,)):
  File "/home/user/.local/lib/python3.8/site-packages/dpath/segments.py", line 73, in walk
    for found in walk(v, location + (k,)):
  File "/home/user/.local/lib/python3.8/site-packages/dpath/segments.py", line 73, in walk
    for found in walk(v, location + (k,)):
  [Previous line repeated 981 more times]
  File "/home/user/.local/lib/python3.8/site-packages/dpath/segments.py", line 58, in walk
    if not leaf(obj):
  File "/home/user/.local/lib/python3.8/site-packages/dpath/segments.py", line 33, in leaf
    return isinstance(thing, leaves)
RecursionError: maximum recursion depth exceeded in __instancecheck__

如果只是这个 swagger codegen 的社区 python 实现,我会理解,但它发生在 isInstance 中。

看起来好像它到达了 dpath 站点包(不熟悉这个,也许 swagger_py_codegen 添加了它),它到达了段,然后有一个函数可以检查某物是否是叶子。

这是segments.py的相关部分:

def leaf(thing):
    '''
    Return True if thing is a leaf, otherwise False.

    leaf(thing) -> bool
    '''
    leaves = (bytes, str, int, float, bool, type(None))

    return isinstance(thing, leaves)

我很确定 isinstance 只是标准库的一部分,所以我不能调整它。但是从 dpath 中查看这个 leaf(thing) 方法,我看不出是什么导致了错误。

到目前为止我已经尝试过:

我尝试编辑segments.py 将最大递归数增加到10,000。这只会导致永远运行。我把它单独放置了 5 分钟,但它仍然没有完成。

所以它的某种无限递归循环,但假设 isinstance 是正确的,我无法从那个简单的叶子(事物)调用中找出导致问题的原因

4

0 回答 0