Skip to content

Commit

Permalink
Merge branch 'pr/tested/1-newtypes_no-pure-interface' into pr/12-hasa…
Browse files Browse the repository at this point in the history
…ll-betterbinder
  • Loading branch information
eirannejad committed Mar 15, 2024
2 parents 1637d62 + 7e480f9 commit 1d48694
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
31 changes: 25 additions & 6 deletions src/runtime/Types/InterfaceObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,34 @@ public static NewReference tp_getattro(BorrowedReference ob, BorrowedReference k
}

string? name = Runtime.GetManagedString(key);
if (name == "__implementation__")
if (name != null)
{
return Converter.ToPython(clrObj.inst);
}
else if (name == "__raw_implementation__")
{
return CLRObject.GetReference(clrObj.inst);
if (name == "__implementation__")
{
return Converter.ToPython(clrObj.inst);
}
else if (name == "__raw_implementation__")
{
return CLRObject.GetReference(clrObj.inst);
}
else
{
// try get attr from pure interface wrapper
var value = Runtime.PyObject_GenericGetAttr(ob, key);
if (Exceptions.ErrorOccurred())
{
// if that didn't work, clear errors
// and try get from wrapped object
Exceptions.Clear();

using var pyObj = Converter.ToPython(clrObj.inst);
return Runtime.PyObject_GenericGetAttr(pyObj.Borrow(), key);
}
return value;
}
}


return Runtime.PyObject_GenericGetAttr(ob, key);
}

Expand Down
4 changes: 2 additions & 2 deletions tests/test_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ def test_explicit_cast_to_interface():
assert type(i1).__name__ == 'ISayHello1'
assert hasattr(i1, 'SayHello')
assert i1.SayHello() == 'hello 1'
assert not hasattr(i1, 'HelloProperty')
assert hasattr(i1, 'HelloProperty')
assert i1.__implementation__ == ob
assert i1.__raw_implementation__ == ob

i2 = Test.ISayHello2(ob)
assert type(i2).__name__ == 'ISayHello2'
assert i2.SayHello() == 'hello 2'
assert hasattr(i2, 'SayHello')
assert not hasattr(i2, 'HelloProperty')
assert hasattr(i2, 'HelloProperty')


def test_interface_object_returned_through_method():
Expand Down

0 comments on commit 1d48694

Please sign in to comment.