English 中文(简体)
使C延伸到沙捞越,还需要再延期。
原标题:Making a C extension to Python that requires another extension
  • 时间:2009-10-17 20:01:35
  •  标签:

我有两.斯功能,我利用这些功能,使与Pyega的游戏发展更容易。 我在一份称为“助手”的档案中看到他们。 在我的Sharmapath,因此,我可以从我的任何游戏中进口。 我认为,作为了解沙捞越情况的一种做法,将这一模块改成C。 我的第一个问题是,我需要利用Pyega的职能,我不敢肯定这样做。 Pyega安装了一些头盔档案,但似乎没有C版的灰色功能。 Maybe 我没有东西。

我如何解决这一问题? 作为一项工作,该职能目前接受一个职能参数,要求它不是理想的解决办法。

采用Windows XP、Alfredo 2.6和Pyega 1.9.1。

最佳回答
/* get the sys.modules dictionary */
PyObject* sysmodules PyImport_GetModuleDict();
PyObject* pygame_module;
if(PyMapping_HasKeyString(sysmodules, "pygame")) {
    pygame_module = PyMapping_GetItemString(sysmodules, "pygame");
} else {
    PyObject* initresult;
    pygame_module = PyImport_ImportModule("pygame");
    if(!pygame_module) {
      /* insert error handling here! and exit this function */
    }
    initresult = PyObject_CallMethod(pygame_module, "init", NULL);
    if(!initresult) {
      /* more error handling &c */
    }
    Py_DECREF(initresult);
}
/* use PyObject_CallMethod(pygame_module, ...) to your heart s contents */
/* and lastly, when done, don t forget, before you exit, to: */
Py_DECREF(pygame_module);
问题回答

你可以从C码中进口on素模块,把像你那样用平典法界定的物品称作。 这是一个远大的节奏,但完全有可能。

当我想研究如何做像我这样的事情时,请参阅 进口模块将有所帮助。 你们还需要读一下,如何读写的是特性、称呼功能等等。

然而,我怀疑你真的想要做的是:underlying Library sdl from C. 这是一个Clibrary,从C.真正容易使用。

这里有一些样本代码,以进口C版的假装模块,该模块根据工作守则的参数进行调整。

PyObject *module = 0;
PyObject *result = 0;
PyObject *module_dict = 0;
PyObject *func = 0;

module = PyImport_ImportModule((char *)"pygame"); /* new ref */
if (module == 0)
{
    PyErr_Print();
    log("Couldn t find python module pygame");
    goto out;
}
module_dict = PyModule_GetDict(module); /* borrowed */
if (module_dict == 0)
{
    PyErr_Print();
    log("Couldn t find read python module pygame");
    goto out;
}
func = PyDict_GetItemString(module_dict, "pygame_function"); /* borrowed */
if (func == 0)
{
    PyErr_Print();
    log("Couldn t find pygame.pygame_function");
    goto out;
}
result = PyEval_CallObject(func, NULL); /* new ref */
if (result == 0)
{
    PyErr_Print();
    log("Couldn t run pygame.pygame_function");
    goto out;
}
/* do stuff with result */
out:;
Py_XDECREF(result);
Py_XDECREF(module);

大部分职能载于<代码>pyega。 模块仅围绕SDL职能进行总结,即需要寻找其职能的C版。 <代码>pyega.h 界定了一系列<编码>import_pyega_*(功能。 电话:import_pyega_base() 和在推广模块初始化时的其他人,以获得所需部分的丙型六氯环己烷模块(在每台头盔档案中界定)。 http://www.google.com/codesearch?q=import_pyega_base” rel=“nofollow noreferer”>。





相关问题
热门标签