workaround in PyCXX classes due to removed support of old-style classes

This commit is contained in:
wmayer
2019-06-25 18:15:10 +02:00
parent 76953d933c
commit e0db5b62be
2 changed files with 22 additions and 1 deletions

View File

@@ -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);
}