【发布时间】:2017-06-11 05:59:21
【问题描述】:
所以我需要使用子流程模块的代码来添加一些我需要的功能。当我试图编译 _subprocess.c 文件时,它给出了以下错误消息:
Error 1 error C2086: 'PyTypeObject sp_handle_type' : redefinition
这是与_subprocess.c 文件相关的代码部分:
typedef struct {
PyObject_HEAD
HANDLE handle;
} sp_handle_object;
staticforward PyTypeObject sp_handle_type;
static PyObject*
sp_handle_new(HANDLE handle)
{
sp_handle_object* self;
self = PyObject_NEW(sp_handle_object, &sp_handle_type);
if (self == NULL)
return NULL;
self->handle = handle;
return (PyObject*)self;
}
#if defined(MS_WIN32) && !defined(MS_WIN64)
#define HANDLE_TO_PYNUM(handle) PyInt_FromLong((long) handle)
#define PY_HANDLE_PARAM "l"
#else
#define HANDLE_TO_PYNUM(handle) PyLong_FromLongLong((long long) handle)
#define PY_HANDLE_PARAM "L"
#endif
static PyObject*
sp_handle_detach(sp_handle_object* self, PyObject* args)
{
HANDLE handle;
if (!PyArg_ParseTuple(args, ":Detach"))
return NULL;
handle = self->handle;
self->handle = INVALID_HANDLE_VALUE;
/* note: return the current handle, as an integer */
return HANDLE_TO_PYNUM(handle);
}
static PyObject*
sp_handle_close(sp_handle_object* self, PyObject* args)
{
if (!PyArg_ParseTuple(args, ":Close"))
return NULL;
if (self->handle != INVALID_HANDLE_VALUE) {
CloseHandle(self->handle);
self->handle = INVALID_HANDLE_VALUE;
}
Py_INCREF(Py_None);
return Py_None;
}
static void
sp_handle_dealloc(sp_handle_object* self)
{
if (self->handle != INVALID_HANDLE_VALUE)
CloseHandle(self->handle);
PyObject_FREE(self);
}
static PyMethodDef sp_handle_methods[] = {
{ "Detach", (PyCFunction)sp_handle_detach, METH_VARARGS },
{ "Close", (PyCFunction)sp_handle_close, METH_VARARGS },
{ NULL, NULL }
};
static PyObject*
sp_handle_getattr(sp_handle_object* self, char* name)
{
return Py_FindMethod(sp_handle_methods, (PyObject*)self, name);
}
static PyObject*
sp_handle_as_int(sp_handle_object* self)
{
return HANDLE_TO_PYNUM(self->handle);
}
static PyNumberMethods sp_handle_as_number;
statichere PyTypeObject sp_handle_type = {
PyObject_HEAD_INIT(NULL)
0, /*ob_size*/
"_subprocess_handle", sizeof(sp_handle_object), 0,
(destructor)sp_handle_dealloc, /*tp_dealloc*/
0, /*tp_print*/
(getattrfunc)sp_handle_getattr,/*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_compare*/
0, /*tp_repr*/
&sp_handle_as_number, /*tp_as_number */
0, /*tp_as_sequence */
0, /*tp_as_mapping */
0 /*tp_hash*/
};`
我还发现:
#define staticforward static
#define statichere static
我不明白我做错了什么。任何帮助,将不胜感激。 顺便说一句(我不确定它是否相关),我正在使用 Visual Studio Professional 2013 来编译这个文件。
【问题讨论】:
标签: python c forward-declaration python-extensions