初始化器不是常量,错误C2099,有关为Python编译用c编写的模块 [英] initializer is not a constant, error C2099, on compiling a module written in c for python
问题描述
我尝试编译名为 distance 的python模块,其中c在Windows 10上使用msvc 2017的python setup.py install --with-c,我收到此错误,
i tried to compile a python module called distance, whith c "python setup.py install --with-c" using msvc 2017 on windows 10, i got this error ,
Cdistance/distance.c(647):错误C2099:初始化程序不是 恒定
Cdistance / distance.c (647): error C2099: initializer is not a constant
Cdistance/distance.c(689):错误C2099:初始化程序不是 恒定
Cdistance / distance.c (689): error C2099: initializer is not a constant
错误:命令'C:\ Program Files(x86)\ Microsoft Visual Studio \ 2017 \ BuildTools \ VC \工具\ MSVC \ 14.10.25017 \ bin \ HostX64 \ x64 \ cl .exe'失败,退出状态为2
Error: command 'C: \ Program Files (x86) \ Microsoft Visual Studio \ 2017 \ BuildTools \ VC \ Tools \ MSVC \ 14.10.25017 \ bin \ HostX64 \ x64 \ cl .exe 'failed with exit status 2
这是第647行上的代码
here is the code on line 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行中的
是另一个类似的结构
in the line 689 is another like strucure,
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 */
在同一页面中均被引用如下
both referenced as follow, in the same page
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);
谢谢
推荐答案
i have found the solution, by looking to the definition of the structure PyTypeObject PyTypeObject , i have changed yVarObject_HEAD_INIT(&PyType_Type, 0)
by PyVarObject_HEAD_INIT(NULL, 0)
and it compiles successfully, and i have tried some functions and it works, so the error is caused by &PyType_Type
which is a PyObject*
, i know that because IFastComp_Type
is a globale varailble it should be initialised by a constante, but i still don't know why the author of the module gave &PyType_Type
as argument , thank you all for your comments.
这篇关于初始化器不是常量,错误C2099,有关为Python编译用c编写的模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!