[C++-sig] Re: v2 def_raw ?

David Abrahams dave at boost-consulting.com
Thu Jan 9 18:41:18 CET 2003


Jacek Generowicz <jacek.generowicz at cern.ch> writes:

> David Abrahams <dave at boost-consulting.com> writes:
>
>> What we /do/ have is the ability to specify optional arguments
>
>> and keyword expressions
>
>> Does that cover your needs?
>
> I don't think so. But it's possible that I have missed the point.
>
> I have a C++ function (which came in a v1 interface to a C++ library,
> which I am migrating to v2 - I hope to lose this function by the time
> I've finished, but I'm stuck with it for now):
>
>    return_type function(PyObject* tuple, PyObject* dict) {
>       // Igrone dict.
>       // Mess around with the elements of tuple.
>    }
>
> IOW, what I really want would be written in python as
>
>    def function( *args ):
>        # Mess arount with elements of args.
>
> I was wondering whether it might be done with a precall CallPolicy,
> but I'm finding the documentation of that area a little too cryptic
> and abstract.

CallPolicies are really not intended for that purpose; they're for
specifying how pointer/reference return types will be converted to
python, and for helping with lifetime management issues (so that
Python can't easily delete objects which your C++ code is holding
pointers to).

I looked a little bit at the library code a few days ago to see what
would be needed to support a "raw" function; I don't have a pretty
interface for you, but I can tell you how to make one with the
library's implementation details.  Be warned that these interfaces may
change, but hopefully we'll have an "official" interface for what you
want to do by then.

  python::objects::function_object(
      f                       // must be py_function compatible
      , 0, std::numeric_limits<unsigned>::max()  // arity range
      , python::detail::keyword_range());        // no keywords

Will create a Python callable object.

f is any function pointer, function reference, or function object
which can be invoked with two PyObject* arguments and returns
something convertible to a PyObject*.

You can add this to your module namespace with:

    scope().attr("name") = function_object(f, ... );

HTH,
-- 
                       David Abrahams
   dave at boost-consulting.com * http://www.boost-consulting.com
Boost support, enhancements, training, and commercial distribution





More information about the Cplusplus-sig mailing list