Skip to content

Commit

Permalink
workaround in PyCXX classes due to removed support of old-style classes
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmayer committed Jun 25, 2019
1 parent 76953d9 commit e0db5b6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/Base/GeometryPyCXX.cpp
Expand Up @@ -131,14 +131,23 @@ Py::Object Vector2dPy::repr()

Py::Object Vector2dPy::getattro(const Py::String &name_)
{
// For Py3 either handle __dict__ or implement __dir__ as shown here:
// https://stackoverflow.com/questions/48609111/how-is-dir-implemented-exactly-and-how-should-i-know-it
//
std::string name( name_.as_std_string( "utf-8" ) );

if (name == "__members__") {
if (name == "__members__") { // Py2
Py::List attr;
attr.append(Py::String("x"));
attr.append(Py::String("y"));
return attr;
}
else if (name == "__dict__") { // Py3
Py::Dict attr;
attr.setItem(Py::String("x"), Py::Float(v.x));
attr.setItem(Py::String("y"), Py::Float(v.y));
return attr;
}
else if (name == "x") {
return Py::Float(v.x);
}
Expand Down
12 changes: 12 additions & 0 deletions src/CXX/Python3/ExtensionOldType.hxx
Expand Up @@ -157,6 +157,18 @@ namespace Py
EXPLICIT_TYPENAME method_map_t::const_iterator i = mm.find( name );
if( i == mm.end() )
{
if( name == "__dict__" ) // __methods__ is not supported in Py3 any more, use __dict__ instead
{
Dict methods;

i = mm.begin();
EXPLICIT_TYPENAME method_map_t::const_iterator i_end = mm.end();

for( ; i != i_end; ++i )
methods.setItem( String( (*i).first ), String( "" ) );

return methods;
}
if( name == "__methods__" )
{
List methods;
Expand Down

0 comments on commit e0db5b6

Please sign in to comment.