使用 python 调试器 (pdb) 时调试 cython 代码 (.pyx) - 最佳实践

Debug cython code (.pyx) when using the python debugger (pdb) - Best Practice

我已阅读 Cython debugging, put a break point, and https://groups.google.com/forum/#!topic/apam-python-users/6rsRwcCAms4 并想知道调试从 python 代码调用的 cython 代码时最好的工作流程是什么?

理想情况下,我想在从我的 IDE (pycharm) 启动的 python 调试会话期间进入 .pyx 文件,但似乎这是不可能的。难道pyx文件被cython化后不能编译出调试信息让调试器介入吗?

如果无法实现,有什么替代方案(除了不使用 cython!)?

由于这个问题专门问如何单步进入cython代码,虽然和Cython & Python Project Test Driven Development and .pyx file structure advice类似,但又不一样

似乎 official way is your best option. It would be great if there were an easy alternative but from the link you included here it seems like there isn't. This wiki 文档似乎有一些官方文档缺少的额外提示。

如果您使用 Cython 只是为了提高速度(即不包装 C 库),您可以使用 pure Python mode 它可以让您在单独的 .pxd 文件(与您的代码一起存在于.py 文件),或使用装饰器。

这种模式的优点是您的代码可以 运行(并被调试)在普通的 Python 下。然后你会留下(希望很小)class 的错误,这些错误是由于 Cython 静态类型而不是你的代码。缺点是: 1) 运行在 plain Python 下使用你的代码会比较慢; 2) 语法比标准的 Cython 语法有点混乱; 3) 您不能像这样访问外部 C 代码,这是 Cython 的主要用例之一。

如果你最好的选择是传统的 "lots of print statements"。 print locals() 在这里很有用!不过也不是完全令人满意。