Get __name__ in C extension module

Barry Scott barry at barrys-emacs.org
Tue Oct 8 13:36:56 EDT 2019



> On 7 Oct 2019, at 00:44, Ian Pilcher <arequipeno at gmail.com> wrote:
> 
> On 10/6/19 12:55 PM, Barry Scott wrote:
>> Then the answer to your question is simple. Do it in python and passt
>> logger into the C++ module.
> 
> Funny thing, that's exactly where I started this journey.  I couldn't
> figure out how to get the logging.Logger type object, so that I could
> use a "O!" format string unit.  This led me to read a bit more about
> the logging framework, which led me to the advice to get loggers by
> name, rather than passing them around, etc., etc.

In PyCXX I never need to use the "O!" stuff.

I write something like this (untested):

// function in foo module
Py::Object function( const Py::Tuple &args, const Py::Dict &kws )
{
	Py::Object logger( args[0] );

	Py::Tuple log_args( 1 );
	args[0] = Py::String( "log message" );

	logger.callMemberFunction( "info", log_args );

	return Py::None();
}

I'd call from Python something like:

import foo
import logging

logger = logging.getLogger(__name__)

foo.function( logger )


> 
>> 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.
> 
> Well, I like driving cars with manual transmissions, so ...

Understood.

Barry

> 
> -- 
> ========================================================================
> Ian Pilcher                                         arequipeno at gmail.com
> -------- "I grew up before Mark Zuckerberg invented friendship" --------
> ========================================================================
> 




More information about the Python-list mailing list