【问题标题】:Load module into embedded Python runtime using PyCXX使用 PyCXX 将模块加载到嵌入式 Python 运行时
【发布时间】:2014-10-12 12:34:05
【问题描述】:

我正在使用 PyCXX 围绕嵌入式 Python 运行时创建 C++ 包装器。

PyCXX 似乎没有可执行文件的示例,所以我正在尝试调整现有的示例代码。

我可以轻松启动并运行 Python 解释器:

extern "C" int Py_Main(int argc, PY_CHAR** argv);

int main(int argc, const char * argv[])
{
    Py_Initialize();
    PyRun_SimpleString( "print('hello world') \n" );
    Py_Finalize();
}

这会在我的 Xcode 调试/输出窗口中放置一个功能齐全的 Python 提示符。

接下来,我公开了一个测试 C++ 类,以便它在 Python 中可见。为此目的编写了一个range 类:

extern "C" int Py_Main(int argc, PY_CHAR** argv);

int main(int argc, const char * argv[])
{
    Py_Initialize();
    range::init_type();

    //test_extension_object();  <-- test-suite for 'range'

    Py_Main(argc, (PY_CHAR**)argv); // wrong but works(!)

    /*
     This will launch a Python prompt in Xcode's output window
     You can type:
         >>> x = range(1,20,3)
         >>> i = [a for a in x]
         >>> i
         [1, 4, 7, 10, 13, 16, 19]
         >>> quit()
         Program ended with exit code: 0
     */
    Py_Finalize();

}

好的,这样也可以。

但现在我正在尝试加载一个模块。

有一个名为“simple.cxx”的简单演示模块,其中包含:

extern "C" EXPORT_SYMBOL PyObject *PyInit_simple()
{
    static simple_module* simple = new simple_module;
    return simple->module().ptr();
}

simple_module 类派生自PyCXX 的ExtensionModule 类,该类派生自具有初始化程序的ExtensionModuleBase 类:

void ExtensionModuleBase::initialize( const char *module_doc )
{
    memset( &m_module_def, 0, sizeof( m_module_def ) );

    m_module_def.m_name = const_cast<char *>( m_module_name.c_str() );
    m_module_def.m_doc = const_cast<char *>( module_doc );
    m_module_def.m_methods = m_method_table.table();
    // where does module_ptr get passed in?

    m_module = PyModule_Create( &m_module_def );
}

如果我理解正确,预期用途是应该将此 .cxx 编译成库(OS X 上的 .so),并将其放在 Python 的搜索路径中的某个位置。

但应该可以在无需编译单独的库的情况下使其工作。这就是我想要做的。

extern "C"
{
    int Py_Main(int argc, PY_CHAR** argv);
    PyObject *PyInit_example();
}

int main(int argc, const char * argv[])
{
    PyImport_AppendInittab("spam", &PyInit_example);

    Py_Initialize();
    Py_Main(argc, (PY_CHAR**)argv); // wrong but works(!)
    Py_Finalize();
}

我在这里使用文档:https://docs.python.org/3.4/extending/embedding.html,它告诉我使用PyImport_AppendInittab

现在我应该可以从提示中看到这个模块了。它被称为simple

>>> import simple
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named 'simple'

>>> import sys
>>> sys.modules.keys()
dict_keys(['_weakrefset', 'copyreg', 'posix', '_io', 'encodings.aliases', '__main__', '_frozen_importlib', '_sysconfigdata', 'sys', 'encodings.utf_8', '_osx_support', 'marshal', 'builtins', 'encodings.ascii', 'abc', 'stat', '_weakref', 'atexit', '_bootlocale', 'rlcompleter', '_collections_abc', 're', 'readline', '_thread', 'zipimport', 'sre_constants', '_sitebuiltins', 'encodings.latin_1', '_sre', 'codecs', '_codecs', 'sysconfig', '_locale', 'posixpath', '_stat', 'encodings', 'genericpath', 'os.path', 'site', 'sitecustomize', 'sre_parse', 'io', 'os', 'errno', '_warnings', 'signal', 'sre_compile', '_imp'])

好像没用。

我错过了什么?

【问题讨论】:

    标签: python c++ embedding pycxx


    【解决方案1】:

    我是一只黑猩猩。 “spam”需要替换为“simple”,它可以工作。

    我将留下问题而不是删除它,因为它包含对任何未来 PyCXX 浏览器有用的踏脚石。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 2020-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-23
      • 1970-01-01
      相关资源
      最近更新 更多