wlav / cppyy

Other
407 stars 42 forks source link

__repr__ Returning std::string Not Valid #48

Closed mkolin closed 2 years ago

mkolin commented 2 years ago

In cppyy 2.3.0 you can no longer have a __str__ or __repr__ that return a std::string. I'm not sure when this changed. Returning a const char* works.

import cppyy

cppyy.cppdef('''
struct Test
{
    // Works
    //const char* __repr__() const { return "Test()"; }
    // Doesn't Work
    std::string __repr__() const { return "Test()"; }
};
''')
from cppyy.gbl import Test

print(repr(Test()))
wlav commented 2 years ago

The change was with the choice of methods returning std::string to return the actual object and not convert them to Python str objects. The motivation is that for Python3, this requires a decision on which unicode encoding to use. In most cases, std::string is a drop-in replacement for str, but in __repr__ and __str__ Python actually does a type check, hence failure.

Now fixed in repo and tests added to prevent future breakage.

wlav commented 2 years ago

Released with 2.4.0 and its dependencies.