Hello, I am working on python extension module. Currently I'm struggling with invalid
static global variable HiveError
. Here are code snippets:
My extension module header file:
...
#ifdef _DEBUG
#define _DEBUG_BUILD 1
#undef _DEBUG
#endif
#ifdef __cplusplus
extern "C"
{
#endif
#include <Python.h>
#include <structmember.h>
static PyObject * HiveError;
#ifdef __cplusplus
}
#endif
...
And this is
PyMODINIT_FUNC
function initializing
HiveError
exception:
PyMODINIT_FUNC PyInit_pphive(void)
{
PyObject * module;
if(!init_type(&PyHive_Type))
{
return NULL;
}
module = PyModule_Create(&PPHiveModule);
if (module == NULL ||
!add_type(module, &PyHive_Type, "Hive"))
{
return NULL;
}
HiveError = PyErr_NewException("pphive.HiveError", PyExc_Exception, NULL);
if(!HiveError)
return NULL;
Py_INCREF(HiveError);
if(PyModule_AddObject(module, "HiveError", HiveError) == -1)
return NULL;
return module;
}
In above function,
HiveError
is valid - can be thrown to python code... Then comes python code, which instantiates
Hive
class and call its method, where I want exception to be throw:
.py code
hive = pphive.Hive(path)
hive.expand()
My
expand
function:
static PyObject * expand(PyHive * self, PyObject * args)
{
Py_INCREF(HiveError); return PyErr_Format(HiveError, ""); };
HiveError
is NULL in my
expand
function, why? I've put basically whole code called between exception initializing and expand function.
Any tips, or suggestions where exception might be deleted?