初始化程序不是常量,错误C2099,编译用c编写的用于python的模块

时间:2017-07-30 16:13:18

标签: python c visual-c++ python-c-api

我尝试编译一个名为distance的python模块,在Windows 10上使用msvc 2017进行c“python setup.py install --with-c”,我收到此错误,

  

Cdistance / distance.c(647):错误C2099:初始化程序不是a   恒定

     

Cdistance / distance.c(689):错误C2099:初始化程序不是a   恒定

     

错误:命令'C:\ Program Files(x86)\ Microsoft Visual Studio \   2017 \ BuildTools \ VC \ Tools \ MSVC \ 14.10.25017 \ bin \ HostX64 \   x64 \ cl .exe'因退出状态2而失败

这是第647行的代码

   646 PyTypeObject IFastComp_Type = {
   647     PyVarObject_HEAD_INIT(&PyType_Type, 0)
   648  "distance.ifast_comp", /* tp_name */
   649  sizeof(ItorState), /* tp_basicsize */
   650  0, /* tp_itemsize */
        (destructor)itor_dealloc, /* tp_dealloc */
        0, /* tp_print */
        0, /* tp_getattr */
        0, /* tp_setattr */
        0, /* tp_reserved */
        0, /* tp_repr */
        0, /* tp_as_number */
        0, /* tp_as_sequence */
        0, /* tp_as_mapping */
        0, /* tp_hash */
        0, /* tp_call */
        0, /* tp_str */
        0, /* tp_getattro */
        0, /* tp_setattro */
        0, /* tp_as_buffer */
        Py_TPFLAGS_DEFAULT, /* tp_flags */
        ifast_comp_doc, /* tp_doc */
        0, /* tp_traverse */
        0, /* tp_clear */
        0, /* tp_richcompare */
        0, /* tp_weaklistoffset */
        PyObject_SelfIter, /* tp_iter */
        (iternextfunc)ifastcomp_next, /* tp_iternext */
        0, /* tp_methods */
        0, /* tp_members */
        0, /* tp_getset */
        0, /* tp_base */
        0, /* tp_dict */
        0, /* tp_descr_get */
        0, /* tp_descr_set */
        0, /* tp_dictoffset */
        0, /* tp_init */
        PyType_GenericAlloc, /* tp_alloc */
        ifastcomp_new, /* tp_new */
    };
第689行中的

是另一种类似结构,

688  PyTypeObject ILevenshtein_Type = {
689     PyVarObject_HEAD_INIT(&PyType_Type, 0)
        "distance.ilevenshtein", /* tp_name */
        sizeof(ItorState), /* tp_basicsize */
        0, /* tp_itemsize */
        (destructor)itor_dealloc, /* tp_dealloc */
        0, /* tp_print */
        0, /* tp_getattr */
        0, /* tp_setattr */
        0, /* tp_reserved */
        0, /* tp_repr */

在同一页面中引用如下

762 if (PyType_Ready(&IFastComp_Type) != 0 || PyType_Ready(&ILevenshtein_Type)!= 0)
763 #if PY_MAJOR_VERSION >= 3
        return NULL;
    #else
        return;
    #endif

    Py_INCREF((PyObject *)&IFastComp_Type);
    Py_INCREF((PyObject *)&ILevenshtein_Type);

感谢

2 个答案:

答案 0 :(得分:2)

我找到了解决方案,通过查看结构PyTypeObject PyTypeObject 的定义,我已经yVarObject_HEAD_INIT(&PyType_Type, 0)更改了PyVarObject_HEAD_INIT(NULL, 0)并且它已成功编译,我尝试了一些函数和它有效,所以错误是由&PyType_Type PyObject*造成的,我知道因为IFastComp_Type是一个全局变量,它应该由一个constante初始化,但我仍然没有知道为什么模块的作者给出&PyType_Type作为参数,谢谢大家的意见。

答案 1 :(得分:1)

请参阅the documentation for "defining new types"

PyVarObject_HEAD_INIT(&PyType_Type, 0)
  

这条线有点疣;我们想写的是:

     

PyType_Ready()

     

因为类型对象的类型是“类型”,但这并不严格符合C并且一些编译器抱怨。幸运的是,<?php $doc->addScript(JURI::root(true).'/templates/nfhop/js/sorttable.js' ) ?> <?php require_once JPATH_SITE.'/includes/dbAudio.php'; ?> <?php $query = $handler->query('SELECT * from audio ORDER BY subject ASC'); ?> <article class="left"> <div class="formwrap"> <table class="members-audiolist table-striped sortable" border="0" align="left"> <thead> <tr class="audio_header"> <td><p class="house-red">Subject</p></td> <td><p class="house-red">Title</p></td> <td><p class="house-red">Author</p></td> </tr> </thead> <?php while($r = $query->fetch(PDO::FETCH_OBJ)) { ?> <tbody> <tr> <td width="25%"><?php echo $r->subject; ?></td> <td><a href="index.php/audioplayer-pdo?recordID=<?php echo $r->id; ?>"> <?php echo $r->title; ?></a></td> <td><?php echo $r->author; ?></td> </tr> <?php } ?> </tbody> </table> <div class="clearfix"></div> </div> <div style="margin-left:18px"> <p style="margin-top: 8px; font-size: 1em">To listen: click on title</p> <p style="font-size: 1em">To download: right click on title and select save link as...</p> <p style="font-size: 1em">You can sort by subject, title or author, just click on heading at top of column</p> </div> </article> 将为我们填写此成员。

我认为Visual C是抱怨的编译器,模块是用GCC编写和测试的......