简体   繁体   English

使用命名空间从C ++调用Python函数

[英]Calling Python functions from C++ with namespace

I'm trying to execute Python code from c++ that will define a Python function and pass it back to c++ so it can be called back from there. 我正在尝试从c ++执行Python代码,它将定义Python函数并将其传递回c ++,以便可以从那里调用它。 That works fine but the problem is I can't provide the Python function with the namespace it had when it was originally defined. 这工作正常,但问题是我不能为Python函数提供它最初定义时的命名空间。

struct MyClass {
    void log(const std::string & s)
    {
        cout << s << endl;
    }
    void callFnct(PyObject * fnct)
    {
        bp::call<void>(fnct);
        bp::call<void>(fnct);
    }
};

bp::class_<MyClass, boost::noncopyable> plugin("Plugin", bp::no_init);
plugin.def("callFnct", &MyClass::callFnct);

std::unique_ptr<MyClass> cls(new MyClass());

bp::object main_module = bp::import("__main__");
bp::object main_namespace = main_module.attr("__dict__");
bp::dict locals;
locals["plugin"] = bp::object(bp::ptr(cls.get()));

std::string scriptSource = 
                       "a=5\n"
                       "def my_func():\n"
                       "    a+=1\n"
                       "    plugin.log('won't work %d' % a)\n"
                       "plugin.log('this works')\n"
                       "plugin.callFnct(my_func)";
bp::object obj = bp::exec(bp::str(scriptSource), main_namespace, locals);

The initial call to plugin.log() works but once we call the python function in callFnct() , the namespace is gone so it can't see the variable a or the plugin module. plugin.log()的初始调用有效但是一旦我们在callFnct()调用python函数,命名空间就消失了,所以它看不到变量aplugin模块。

Does anyone know how to do bp::call<void>(fnct) by preserving the namespace and keep the variable a in scope? 有谁知道怎么做bp::call<void>(fnct)通过保留的命名空间,并保持变量a范围?

That is because variables in non-local scopes cannot be rebound. 那是因为非局部范围内的变量无法反弹。 It won't work even without calling to C++: 即使没有调用C ++它也不会工作:

a = 5
def my_func():
    a += 5
    print(a)
my_func()

UnboundLocalError: local variable 'a' referenced before assignment

You need to import it first: 您需要先导入它:

a = 5
def my_func():
    global a
    a += 5
    print(a)
my_func()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM