Re: [pygccxml-development] Proper overload resolution

Roman Yakovenko wrote:
> On Mon, May 17, 2010 at 8:31 PM, Benoit Leveau
> <benoit-l@...> wrote:
>> I already took a look at that page and:
>> - solution 1 doesn't work if you want to bind all the functions to one
>> unique alias: __setitem__
>
> Ok.
>
>> - solution 2 & 3 don't change the fact that only one of the two functions
>> will be called (as bool and int can be converted to each other, it's always
>> the last one exposed that will be called)
>
> You are right.
>
>> - solution 4 is what I was mentionning in my message, but it doesn't seem
>> py++ can generate that code yet. Or does it?
>
> No, it doesn't. I guess you have no choice, but to implement that
> last method by yourself :-(
Ok, that's what I thought!
And I guess there is no way to "register" some Python code to be
executed when the module is loaded, it must be done in the __init__.py
file, right?
> The only help, you get from Py++, is warning "W1010". You can use
> "x.readme()" functionality to check whether set of functions have such
> problem or not. You can use that functionality as assert for the
> "solution 4".
Good, I didn't know a warning was issued in that case.
>>From my experience patching boost, creates a huge headache. This is
> exactly the reason, why I migrated indexing suite v2 to be header only
> library.
Thanks!

Thread view

Hello,
Consider the following class:
struct MyClass
{
void setBool(bool a);
void setInt(int a);
};
Because of the way the overloading resolution works currently in boost
python, if you're trying to bind these two functions to __setitem__,
only one of these functions (the last one bound actually) will always be
called, no matter if you call __setitem__ with a bool or an int (for
more information of the why, please take a look at the cplusplus-sig
thread mentioned below).
The only way I could find to bind these two methods to __setitem__ and
have them called "correctly" is to bind them as two separate functions,
and then define a __setitem__ function in python at runtime, by adding
something like that in the __init__.py file:
def custom_set_item(a):
if isinstanceof(a, bool):
...
...
MyClass.__setitem__ = custom_set_item
Maybe there's a way to do that by binding a setitem which would take a
bp::object and test the type in C++, I haven't tried.
It looks like Troy D. Straszheim came with a better implementation of
the overloading mechanism (which is much closer to what we expect):
http://mail.python.org/pipermail/cplusplus-sig/2009-December/015047.html
It is apparently considered for addition in boost, but looks like
they're waiting for the new langbinding integration:
http://www.boost.org/doc/libs/1_43_0/libs/python/todo.html
Any chance one of you have tried Troy's modification, or have any
suggestion for this problem?
Thanks!
Benoit

On Mon, May 17, 2010 at 6:23 PM, Benoit Leveau
<benoit-l@...> wrote:
> Hello,
>
> Consider the following class:
>
> struct MyClass
> {
> void setBool(bool a);
> void setInt(int a);
> };
>
> Because of the way the overloading resolution works currently in boost
> python, if you're trying to bind these two functions to __setitem__,
> only one of these functions (the last one bound actually) will always be
> called, no matter if you call __setitem__ with a bool or an int (for
> more information of the why, please take a look at the cplusplus-sig
> thread mentioned below).
>
> The only way I could find to bind these two methods to __setitem__ and
> have them called "correctly" is to bind them as two separate functions,
> and then define a __setitem__ function in python at runtime, by adding
> something like that in the __init__.py file:
>
> def custom_set_item(a):
> if isinstanceof(a, bool):
> ...
> ...
>
> MyClass.__setitem__ = custom_set_item
>
> Maybe there's a way to do that by binding a setitem which would take a
> bp::object and test the type in C++, I haven't tried.
You can do this in C++ too, but why bother? You can generate that code
from Py++ :-)
> It looks like Troy D. Straszheim came with a better implementation of
> the overloading mechanism (which is much closer to what we expect):
> http://mail.python.org/pipermail/cplusplus-sig/2009-December/015047.html
>
> It is apparently considered for addition in boost, but looks like
> they're waiting for the new langbinding integration:
> http://www.boost.org/doc/libs/1_43_0/libs/python/todo.html
>
> Any chance one of you have tried Troy's modification, or have any
> suggestion for this problem?
Yes, I do. Please take a look on the following document(
http://language-binding.net/pyplusplus/documentation/functions/registration_order.html
) and let me know whether it solved your problem or not.
--
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/

Roman Yakovenko wrote:
> On Mon, May 17, 2010 at 6:23 PM, Benoit Leveau
> <benoit-l@...> wrote:
>> Hello,
>>
>> Consider the following class:
>>
>> struct MyClass
>> {
>> void setBool(bool a);
>> void setInt(int a);
>> };
>>
>> Because of the way the overloading resolution works currently in boost
>> python, if you're trying to bind these two functions to __setitem__,
>> only one of these functions (the last one bound actually) will always be
>> called, no matter if you call __setitem__ with a bool or an int (for
>> more information of the why, please take a look at the cplusplus-sig
>> thread mentioned below).
>>
>> The only way I could find to bind these two methods to __setitem__ and
>> have them called "correctly" is to bind them as two separate functions,
>> and then define a __setitem__ function in python at runtime, by adding
>> something like that in the __init__.py file:
>>
>> def custom_set_item(a):
>> if isinstanceof(a, bool):
>> ...
>> ...
>>
>> MyClass.__setitem__ = custom_set_item
>>
>> Maybe there's a way to do that by binding a setitem which would take a
>> bp::object and test the type in C++, I haven't tried.
>
> You can do this in C++ too, but why bother? You can generate that code
> from Py++ :-)
>
>> It looks like Troy D. Straszheim came with a better implementation of
>> the overloading mechanism (which is much closer to what we expect):
>> http://mail.python.org/pipermail/cplusplus-sig/2009-December/015047.html
>>
>> It is apparently considered for addition in boost, but looks like
>> they're waiting for the new langbinding integration:
>> http://www.boost.org/doc/libs/1_43_0/libs/python/todo.html
>>
>> Any chance one of you have tried Troy's modification, or have any
>> suggestion for this problem?
>
> Yes, I do. Please take a look on the following document(
> http://language-binding.net/pyplusplus/documentation/functions/registration_order.html
> ) and let me know whether it solved your problem or not.
>
I already took a look at that page and:
- solution 1 doesn't work if you want to bind all the functions to one
unique alias: __setitem__
- solution 2 & 3 don't change the fact that only one of the two
functions will be called (as bool and int can be converted to each
other, it's always the last one exposed that will be called)
- solution 4 is what I was mentionning in my message, but it doesn't
seem py++ can generate that code yet. Or does it?

On Mon, May 17, 2010 at 8:31 PM, Benoit Leveau
<benoit-l@...> wrote:
> I already took a look at that page and:
> - solution 1 doesn't work if you want to bind all the functions to one
> unique alias: __setitem__
Ok.
> - solution 2 & 3 don't change the fact that only one of the two functions
> will be called (as bool and int can be converted to each other, it's always
> the last one exposed that will be called)
You are right.
> - solution 4 is what I was mentionning in my message, but it doesn't seem
> py++ can generate that code yet. Or does it?
No, it doesn't. I guess you have no choice, but to implement that
last method by yourself :-(
The only help, you get from Py++, is warning "W1010". You can use
"x.readme()" functionality to check whether set of functions have such
problem or not. You can use that functionality as assert for the
"solution 4".
>From my experience patching boost, creates a huge headache. This is
exactly the reason, why I migrated indexing suite v2 to be header only
library.
HTH
--
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/

Roman Yakovenko wrote:
> On Mon, May 17, 2010 at 8:31 PM, Benoit Leveau
> <benoit-l@...> wrote:
>> I already took a look at that page and:
>> - solution 1 doesn't work if you want to bind all the functions to one
>> unique alias: __setitem__
>
> Ok.
>
>> - solution 2 & 3 don't change the fact that only one of the two functions
>> will be called (as bool and int can be converted to each other, it's always
>> the last one exposed that will be called)
>
> You are right.
>
>> - solution 4 is what I was mentionning in my message, but it doesn't seem
>> py++ can generate that code yet. Or does it?
>
> No, it doesn't. I guess you have no choice, but to implement that
> last method by yourself :-(
Ok, that's what I thought!
And I guess there is no way to "register" some Python code to be
executed when the module is loaded, it must be done in the __init__.py
file, right?
> The only help, you get from Py++, is warning "W1010". You can use
> "x.readme()" functionality to check whether set of functions have such
> problem or not. You can use that functionality as assert for the
> "solution 4".
Good, I didn't know a warning was issued in that case.
>>From my experience patching boost, creates a huge headache. This is
> exactly the reason, why I migrated indexing suite v2 to be header only
> library.
Thanks!

On Mon, May 17, 2010 at 9:09 PM, Benoit Leveau
<benoit-l@...> wrote:
> Ok, that's what I thought!
> And I guess there is no way to "register" some Python code to be executed
> when the module is loaded, it must be done in the __init__.py file, right?
No:
http://www.boost.org/doc/libs/1_43_0/libs/python/doc/tutorial/doc/html/python/embedding.html
In any case, I suggest you to wrap your module in a Python package.
This will save you a lot of time later. Even now, you will not have to
learn how to "embed" Python code via Boost.Python. I am sure, this is
a nice topic to learn, but after all we have to deliver a product and
get some money :-) ?
>> The only help, you get from Py++, is warning "W1010". You can use
>> "x.readme()" functionality to check whether set of functions have such
>> problem or not. You can use that functionality as assert for the
>> "solution 4".
>
> Good, I didn't know a warning was issued in that case.
>
>>> From my experience patching boost, creates a huge headache. This is
>>
>> exactly the reason, why I migrated indexing suite v2 to be header only
>> library.
>
> Thanks!
You are welcome.
--
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/

2010/5/17 Roman Yakovenko <roman.yakovenko@...>:
> On Mon, May 17, 2010 at 9:09 PM, Benoit Leveau
> <benoit-l@...> wrote:
>> Ok, that's what I thought!
>> And I guess there is no way to "register" some Python code to be executed
>> when the module is loaded, it must be done in the __init__.py file, right?
>
> No:
> http://www.boost.org/doc/libs/1_43_0/libs/python/doc/tutorial/doc/html/python/embedding.html
>
> In any case, I suggest you to wrap your module in a Python package.
> This will save you a lot of time later. Even now, you will not have to
> learn how to "embed" Python code via Boost.Python. I am sure, this is
> a nice topic to learn, but after all we have to deliver a product and
> get some money :-) ?
This is already a Python package actually ;)
Thanks for the link.
>>> The only help, you get from Py++, is warning "W1010". You can use
>>> "x.readme()" functionality to check whether set of functions have such
>>> problem or not. You can use that functionality as assert for the
>>> "solution 4".
>>
>> Good, I didn't know a warning was issued in that case.
>>
>>>> From my experience patching boost, creates a huge headache. This is
>>>
>>> exactly the reason, why I migrated indexing suite v2 to be header only
>>> library.
>>
>> Thanks!
>
> You are welcome.
>
>
>
> --
> Roman Yakovenko
> C++ Python language binding
> http://www.language-binding.net/
Thanks again!
Benoit