如何将 Python 解释器重置为“安全”状态状态?
我有一个嵌入 Python 解释器的 C++ 应用程序。代码中的某些点解释器可能会被中断,我需要确保解释器处于“安全”状态才能执行新代码。我只需调用 Py_Finalize 并重新初始化所有内容,除非我有一堆需要保持有效的 PyObject * 引用。是否有一个函数可以做到这一点或者是否有必要?当我在上面提到解释器被打断时,我指的是段。我的应用程序尝试从中恢复的故障或访问冲突。
I have a C++ app that embeds the Python interpreter. There are points in the code where the interpreter may get interrupted and I need to make sure the interpreter is in a 'safe' state to execute new code. I would just call Py_Finalize and re-initialize everything except I have a bunch of PyObject * references that I need to stay valid. Is there a function to do this or is it even necessary? When I mentioned the interpreter being interrupted above, I meant by a seg. fault or access violation which my app tries to recover from.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
呃,尝试从段错误或访问冲突中“恢复”是相当危险的。首先你得到这些是有原因的,那就是你的程序试图做一些它不应该尝试做的事情;因此它遇到了错误或不可预见的情况。
Python 解释器中没有提供在上述情况下回滚到“安全点”的规定。即使最终确定并重新初始化解释器,仍然可能使一些静态数据处于不一致的状态。
如果您告诉我们为什么要尝试这样做,我们也许可以提出替代方案。
Er, trying to "recover" from a segfault or access violation is quite dangerous. There is a reason you get these in the first place, and it's that your program has tried to do something which it shouldn't have tried to do; therefore it has hit a bug or an unforeseen condition.
There is no provision in the Python interpreter to roll back to a "safe point" in cases such as those mentioned. Even finalizing and reinitializing the interpreter might still leave some static data in a inconsistent state.
If you told us why you are trying to do this we might be able to suggest an alternative.