[C++-sig] Re: static method implementation

David Abrahams dave at boost-consulting.com
Tue Jan 14 17:37:47 CET 2003


Nikolay Mladenov <nickm at sitius.com> writes:

> I agree here, and wrote callable_check (PyCallable_Check wrapper)

OK, thanks!

>> Note that detail::[borrowed|new]_reference are implementation
>> details, but you're working on the implementation so it's OK!
>>
>> > Index: src/object/function.cpp
>> > ===================================================================
>> > RCS file: /cvsroot/boost/boost/libs/python/src/object/function.cpp,v
>> > retrieving revision 1.26
>> > diff -r1.26 function.cpp
>> > 264a265,273
>> >>             else if (existing->ob_type == &PyStaticMethod_Type){
>> >>                 //this can probably be made a lot better
>> >>                 ::PyErr_Format(
>> >>                     PyExc_RuntimeError
>> >>                     , "Boost.Python - All overloads of \"%s\" must be exported before calling
>> > \"class_.staticmethod\""
>> >>                     , name_
>> >>                     );
>> >>                 throw_error_already_set();
>>
>> I'm not sure this is technically neccessary, but I don't know if I
>> could improve it.  The behavior without it might just work anyway.
>
> I tried without this check and if one exports method with the same
> name after staticmethod call, than the static method is not visible
> from python.

Of course, you're right.

> I am not exactly sure how the things work here. As I understend Boost.Python is taking care of the
> overloading. Right?

Right.  Function objects with the same name get chained.  Overloading
just walks down the chain until it finds a match.

> If yes, than a better thing to do is to try extracting the
> Boost.Python.Function from the staticmethod and add_overload.
>
> If no, meaning that python is also taking part in the resolving of
> overloads, than python does not do very good job when mixing static
> and unbounded methods.
>
> And since I could not find a way to extract the function from the
> staticmethod I throw an exception.

I actually think your solution is best.  I don't think restricting the
order in which the user invokes staticmethod, or preventing
overloading of static/non-static methods is so terrible.

> Q: How can I get the name_space name?

What name_space are you referring to?

>> > Index: class.hpp
>> > ===================================================================
>> > RCS file: /cvsroot/boost/boost/boost/python/class.hpp,v
>> > retrieving revision 1.64
>> > diff -r1.64 class.hpp
>> > 354a355,359
>> >>     self& staticmethod(char const* name)
>> >>     {
>> >>         make_method_static(name);
>> >>         return *this;
>> >>     }
>> >
>> > Index: object/class.hpp
>> > ===================================================================
>> > RCS file: /cvsroot/boost/boost/boost/python/object/class.hpp,v
>> > retrieving revision 1.31
>> > diff -r1.31 class.hpp
>> > 50a51,52
>> >>
>> >>     void make_method_static(const char *method_name);
>>
>> Looks like a damned fine patch!
>> Tests and documentation are still needed, of course.
>
> I am using it already for exporting static methods, and so far looks
> fine.  I will check what I can do for documentation.
>
> How do I submit a patch? Posting diffs to the list?

The best thing you can do is post a cvs diff as an attachment.

    cvs diff -NR -c 


Thanks!
Dave
-- 
                       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