Click here to Skip to main content
15,905,229 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C++
...
#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:
C++
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;
    }

    // init exception
    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
Python
hive = pphive.Hive(path)
hive.expand()

My expand function:
C++
static PyObject * expand(PyHive * self, PyObject * args)
{
    Py_INCREF(HiveError); // access violation
    return PyErr_Format(HiveError, ""); // throws SystemError: error return without exception set
};


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?
Posted
Comments
Richard MacCutchan 11-Mar-15 7:33am    
Are the expand function and the init function in the same source file? Global variables are generally declared as extern, see https://msdn.microsoft.com/en-us/library/061kwye0.aspx.
patrik polakovic 11-Mar-15 7:49am    
No, they are in separated files, but have common header file. I've put "extern PyObject * HiveError;" to each source file but the problem remains.
Richard MacCutchan 11-Mar-15 7:57am    
See below.

1 solution

Your header file has:
C++
static PyObject * HiveError;

but it should be:
C++
extern PyObject * HiveError;

Then in your main module (the one with the initialisation code), you should add the following statement at the beginning:
C++
PyObject * HiveError = NULL;

to create the actual pointer variable.
 
Share this answer
 
Comments
patrik polakovic 12-Mar-15 7:20am    
With your solution I got error LNK2001: unresolved external symbol HiveError in every source file using HiveError except the one, where HiveError is initializing.
:(
Richard MacCutchan 12-Mar-15 7:35am    
Then you must have something missing from the header, or the header is not being included in every source file. Please update your question with what you now have, including the full text of the error message.
patrik polakovic 12-Mar-15 7:49am    
1>Link:
1> Creating library <...>\pphive.lib and object <...>\pphive.exp
1>PyHive.obj : error LNK2001: unresolved external symbol HiveError
1><...>\pphive.pyd : fatal error LNK1120: 1 unresolved externals
Richard MacCutchan 12-Mar-15 7:47am    
I just tried it with my suggestions and it builds fine.
patrik polakovic 12-Mar-15 7:50am    
https://github.com/polakovicp/LuaCL/tree/master/pphive
It's for python3.2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900