Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 类内的Python C api异常类_Python 3.x_Python C Api - Fatal编程技术网

Python 3.x 类内的Python C api异常类

Python 3.x 类内的Python C api异常类,python-3.x,python-c-api,Python 3.x,Python C Api,我试图抛出异常,该异常是在用python C api创建的扩展模块的类中定义的 以下是我想要的python C api,但却是用python编写的: class Graph: class TooManyVerticesError( Exception ): pass def addVertex( self ): if self.__order == 16: raise Graph.TooManyVerticesError( "too ma

我试图抛出异常,该异常是在用python C api创建的扩展模块的类中定义的

以下是我想要的python C api,但却是用python编写的:

class Graph:
    class TooManyVerticesError( Exception ): pass

    def addVertex( self ):
        if self.__order == 16:
            raise Graph.TooManyVerticesError( "too many vertices" )
        for v in range( self.__order ):
            self.__matrix[v] += [False]
        self.__order += 1
        self.__matrix += [ [False] * self.__order ]
下面是我现在用python C api编写的内容:

#define ERROR return NULL;

PyObject *TooManyVerticesError;

typedef struct {
    PyObject_HEAD
    size_t __order;  /* the maximum number of elements in q_elements */
    std::vector<std::vector<int>> AdjacencyList;  
} ListaSasiedztwa;

static
PyObject* addVertex(ListaSasiedztwa* self, PyObject* args)
{
    int u, v;
    // Process arguments
    PyArg_ParseTuple(args, "ii",
    &u,
    &v);

    if (self->__order == 16)
    {
        PyErr_SetString(TooManyVerticesError, "too many vertices");
        ERROR
    }
    std::vector<int> vertexList;
    self->AdjacencyList.push_back(vertexList);
    return NULL;
}

PyMODINIT_FUNC PyInit_simple_graphs(void)
{
    if (PyType_Ready(&ListaSasiedztwaType) < 0) return NULL;
    PyObject* m = PyModule_Create(&cSimpleGraphsModule);
    if (m == NULL) return NULL;


    TooManyVerticesError = PyErr_NewException("simple_graphs.TooManyVerticesError", NULL, NULL);
    PyDict_SetItemString(ListaSasiedztwaType.tp_dict, "TooManyVerticesError", TooManyVerticesError);


    Py_INCREF(&ListaSasiedztwaType);
    PyModule_AddObject(m, "ListaSasiedztwa",
        (PyObject *)&ListaSasiedztwaType);

    return m;
}
#定义错误返回NULL;
PyObject*TooManyVerticesError;
类型定义结构{
皮尤头
size\u t\u顺序;/*q\u元素中的最大元素数*/
std::向量邻接列表;
}ListaSasiedztwa;
静止的
PyObject*addVertex(ListaSasiedztwa*self,PyObject*args)
{
INTU,v;
//过程参数
PyArg_语法元组(args,“ii”,
&u,,
&v) );
如果(自->顺序==16)
{
PyErr_SetString(TooManyVerticesError,“顶点太多”);
错误
}
向量顶点列表;
self->AdjacencyList.push_back(顶点列表);
返回NULL;
}
PyMODINIT_FUNC PyInit_简单图(void)
{
if(PyType_Ready(&ListaSasiedztwaType)<0)返回NULL;
PyObject*m=PyModule_Create(&cSimpleGraphsModule);
如果(m==NULL)返回NULL;
TooManyVerticesError=PyErr_NewException(“简单图.TooManyVerticesError”,NULL,NULL);
PyDict_SetItemString(listasaiedztwatype.tp_dict,“TooManyVerticesError”,TooManyVerticesError);
Py_增量(&listasaidztwatype);
PyModule_AddObject(m,“ListaSasiedztwa”,
(PyObject*)和listasaidztwatype);
返回m;
}
一切都正确构建,我可以创建ListAsaidzTwa类型的对象。但当抛出异常时,我有SystemError类型的异常,消息为:

类“simple_graphs.ListAsaidzTwa”返回了一个带有错误集的结果

我是否应该以某种方式将此异常添加为结构成员?
我不知道ListAsaidzTwaType是否与此问题相关,因此我还没有添加它。

我认为您实际遇到了
返回NULL在“无错误”情况下达到的函数末尾。这个
NULL
向Python指出,它们应该是一个异常集,但是由于没有异常集,因此您可以从Python获得常规
SystemError


我怀疑您试图返回Python
None
对象-请改用宏
Py\u return\u None

另一个方法调用了addVertex,该方法没有传递必须为NULL的返回值