4

My application embeds python. The application extends the embedded python by adding it's own function:

PyObject *ReadDPoint(PyObject *self, PyObject *args)

Then the application runs different python scripts(modules) which invoke the ReadDPoint extension function. The task is to determine the module name inside the invoked ReadDPoint function, name of the module from which the function is called. I expected that the solution is like one of these:

PyObject *module_name =  PyObject_GetAttrString(self, "__name__") 

or

char *module_name = PyModule_GetName(self) 

But I with a great surprise have found out that inside the extension function ReadDpoint self is equal to NULL, and referring to it leads to crash. Is there a way to determine the module name inside the C function? Thank you!

  • None of that code adds the function to the module. – Ignacio Vazquez-Abrams Oct 20 '14 at 02:50
  • look at [how the caller scope could be access in pure Python](http://stackoverflow.com/q/13312240/4279). – jfs Oct 20 '14 at 03:37
  • functions in Python belong to the module where they defined, not called. Your extension module must've already registered it if you are able to call it from Python code. `self` points to an object for methods i.e., [unless it is a bound method; it is NULL](http://stackoverflow.com/questions/53225/how-do-you-check-whether-a-python-method-is-bound-or-not) – jfs Oct 20 '14 at 03:40

1 Answers1

0

I've found a way to determine the name of the module from which extension C function is called:

PyFrameObject *pyframe;
PyObject *pydict;
pyframe = PyEval_GetFrame();
pydict = pyframe->f_globals;
PyObject *pyname;
pyname = PyDict_GetItemString(pydict, "__name__");
wxMessageBox(PyString_AsString(pyname));

Now pyname holds the seeked modulename