[C++-sig] optimizing away calls to the python runtime -- was [detecting if a function is written in python or C++]

David Abrahams dave at boost-consulting.com
Tue Jan 17 01:24:59 CET 2006


Mathieu Lacage <Mathieu.Lacage at sophia.inria.fr> writes:

> hi,
>
> I am sufficiently stubborn to try to figure this out myself so, I have
> put together this simple example of a solution to my problem:
> http://spoutnik.inria.fr/yans/releases/test-python.tar.gz
>
> This code defines:
>   1) a C++ Callback1 class
>   2) a C++ Real class which will called back into by the Callback1 class
>   3) a C++ store_and_invoke_callback which uses the c++ Callback1 class
> to call back.
>
> The test python code demonstrate how:
>   1) you can make store_and_invoke_callback call back into python
>   2) you can make store_and_invoke_callback call back into a c++ method
> bound in python without requiring any call into the python runtime

Which line of Python, in particular, demonstrates 2)?

Your example is much, much bigger than it needs to be.  Forget about
methods; just demonstrate it for regular functions.  Stop exporting
the "Real" class twice.  Stop doing needless dynamic allocation in
store_and_invoke_callback.

And stop using double underscores in C++ identifiers; that's reserved
to the compiler ;-)

If I sift out all the stuff in your code that seems extraneous, I
still can't tell what you're trying to accomplish, but maybe you just
want to use

      boost::function<void()>

inside which you can store any object invokable with zero arguments?

P.S. there's little reason to do anything special for bound methods,
since if

        >>> x.f()

is legal Python, you can always store x.f and invoke it later.

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com




More information about the Cplusplus-sig mailing list