Get __name__ in C extension module

MRAB python at mrabarnett.plus.com
Sun Oct 6 12:55:14 EDT 2019


On 2019-10-06 15:48, Ian Pilcher wrote:
> On 10/5/19 12:55 PM, Ian Pilcher wrote:
>> This is straightforward, except that I cannot figure out how to retrieve
>> the __name__.
> 
> Making progress.  I can get a __name__ value with:
> 
>     PyDict_GetItemString(PyEval_GetGlobals(), "__name__")
> 
> I say "a __name__ value" because the returned value is actually that of
> the calling module, not the name of my extension.
> 
> Is this normal?
> 
Don't you already have the module's name? You have to specify it in the 
PyModuleDef struct that you pass to PyModule_Create.
Calling PyModule_Create returns a reference to the module, and you can 
get its namespace dict with PyModule_GetDict(...).



More information about the Python-list mailing list