如何重新初始化一个嵌入式Python间preTER? [英] How to reinitialise an embedded Python interpreter?

查看:222
本文介绍了如何重新初始化一个嵌入式Python间preTER?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作在我们的测试套件应用程序中嵌入了Python。其目的是使用Python来运行多个测试脚本来收集数据并进行测试的报告。一测试运行多个测试脚本可以创建全局变量并且可以在下一脚本中使用的功能。

I'm working on embedding Python in our test suite application. The purpose is to use Python to run several tests scripts to collect data and make a report of tests. Multiple test scripts for one test run can create global variables and functions that can be used in the next script.

该应用程序还提供了在进口的嵌入式间preTER,并用来与应用程序交换一些数据扩展模块。

The application also provides extension modules that are imported in the embedded interpreter, and are used to exchange some data with the application.

但是用户也可以使多个测试运行。我不希望共享这些全局,进口和多个测试运行之间的数据交换。我必须确保我重新开始一个真正的状态来控制测试环境,并得到了相同的结果。

But the user can also make multiple test runs. I don't want to share those globals, imports and the exchanged data between multiple test runs. I have to be sure I restart in a genuine state to control the test environment and get the same results.

我应该如何重新初始化间preTER?

How should I reinitialise the interpreter?

我用Py_Initialize()和Py_Finalize(),但初始化第二次扩展模块我提供给跨preTER时,你得到的第二次运行异常。
和文档警告不要使用它不止一次的。

I used Py_Initialize() and Py_Finalize(), but get an exception on the second run when initialising a second time the extension modules I provide to the interpreter. And the documentation warns against using it more than once.

使用子间preters似乎能与扩展模块的初始化相同的注意事项。

Using sub-interpreters seems to have the same caveats with extension modules initialization.

我怀疑是我做错了什么与我的扩展模块的初始化,但我担心同样的问题与第三方扩展模块发生。

I suspect that I'm doing something wrong with the initialisation of my extension modules, but I fear that the same problem happens with 3rd party extension modules.

也许有可能得到它通过启动在它自己的进程间preTER工作,以便确保所有的内存被释放。

Maybe it's possible to get it to work by launching the interpreter in it's own process, so as to be sure that all the memory is released.

顺便说一句,我使用升压蟒蛇呢,也警告不要使用Py_Finalize!

By the way, I'm using boost-python for it, that also warns AGAINST using Py_Finalize!

任何建议?

感谢

推荐答案

下面是另一种方式,我发现达到我想要的,开始在国米preTER一个干净的石板。

Here is another way I found to achieve what I want, start with a clean slate in the interpreter.

我可以控制全局和局部名字空间我用执行code:

I can control the global and local namespaces I use to execute the code:

// get the dictionary from the main module
// Get pointer to main module of python script
object main_module = import("__main__");
// Get dictionary of main module (contains all variables and stuff)
object main_namespace = main_module.attr("__dict__");

// define the dictionaries to use in the interpreter
dict global_namespace;
dict local_namespace;

// add the builtins
global_namespace["__builtins__"] = main_namespace["__builtins__"];

我就可以使用使用命名空间包含在 code PY code 执行

exec( pyCode, global_namespace, lobaca_namespace );

我可以通过清洗字典清洁命名空间时,我想我跑测试的新实例,

I can clean the namespaces when I want to run a new instance of my test, by cleaning the dictionaries:

// empty the interpreters namespaces
global_namespace.clear();
local_namespace.clear();        

// Copy builtins to new global namespace
global_namespace["__builtins__"] = main_namespace["__builtins__"];

根据在什么水平我要执行,我可以使用全局=本地

Depending at what level I want the execution, I can use global = local

这篇关于如何重新初始化一个嵌入式Python间preTER?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆