[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Get __name__ in C extension module

> On 5 Oct 2019, at 18:55, Ian Pilcher <arequipeno at gmail.com> wrote:
> On 10/4/19 4:30 PM, Ian Pilcher wrote:
>> Ideally, I would pass my existing Logging.logger object into my C
>> function and use PyObject_CallMethod to call the appropriate method on
>> it (info, debug, etc.).
> As I've researched this further, I've realized that this isn't the
> correct approach.
> My extension should be doing the C equivalent of:
>    logger = logging.getLogger(__name__)

The way I build extension modules is to split the work between python code and
C++ code. You only need the C++ for the difficult stuff.

For the "foo" module I would create a foo.py that imports a _foo extension.

Then the answer to your question is simple. Do it in python and passt logger into the
C++ module.

Next I would never code directly against the C API. Its a pain to use and get right, get
the ref counts wrong and you get memory leaks of worse crash python.

There are lots of good alternative. Personal I use PyCXX, but then I'm the maintainer :-)
https://sourceforge.net/projects/cxx/ <https://sourceforge.net/projects/cxx/>


> This is straightforward, except that I cannot figure out how to retrieve
> the __name__.  I can get it from the module object with
> PyModule_GetName, but that requires that I have a reference to the
> module object in order to do so.
> I would have thought that this would be easy for a module function to
> access, but I haven't been able to find any API which does this.
> (Module functions get NULL as their 'self' argument.)
> Any pointers appreciated.  Thanks!
> -- 
> ========================================================================
> Ian Pilcher                                         arequipeno at gmail.com
> -------- "I grew up before Mark Zuckerberg invented friendship" --------
> ========================================================================
> -- 
> https://mail.python.org/mailman/listinfo/python-list