Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
added support for creating abstract classes using a __clr_abstract__ …
…attribute.
  • Loading branch information
rmadsen-ks committed Oct 31, 2022
commit 2449bd210b76268e2986d1fd04122fd06369bd4d
1 change: 1 addition & 0 deletions src/python_tests_runner/PythonTestRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ static IEnumerable<string[]> PythonTestCases()
yield return new[] { "test_subclass", "test_class_with_attributes" };
yield return new[] { "test_subclass", "test_class_with_advanced_attribute" };
yield return new[] { "test_subclass", "test_more_subclasses" };
yield return new[] { "test_subclass", "abstract_test" };
}

/// <summary>
Expand Down
11 changes: 10 additions & 1 deletion src/runtime/Types/ClassDerived.cs
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,16 @@ internal static Type CreateDerivedType(string name,
baseClass = typeof(object);
}

bool isAbstract = false;
if (py_dict != null && Runtime.PyDict_Check(py_dict))
{
using var dict = new PyDict(py_dict);
if (dict.HasKey("__clr_abstract__"))
isAbstract = true;
}

TypeBuilder typeBuilder = moduleBuilder.DefineType(name,
TypeAttributes.Public | TypeAttributes.Class,
TypeAttributes.Public | TypeAttributes.Class | (isAbstract ? TypeAttributes.Abstract : 0),
baseClass,
interfaces.ToArray());

Expand Down Expand Up @@ -359,6 +367,7 @@ internal static Type CreateDerivedType(string name,
if (py_dict != null && Runtime.PyDict_Check(py_dict))
{
using var dict = new PyDict(py_dict);

if (dict.HasKey("__clr_attributes__"))
{
var attributes = new PyList(dict["__clr_attributes__"]);
Expand Down
10 changes: 9 additions & 1 deletion tests/test_subclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,15 @@ def IncrementThing(self):
obj = None
SimpleClass.Test2()


def abstract_test():
class abstractClass(SimpleClass):
__clr_abstract__ = True
failed = False
try:
abstractClass()
except:
failed = True
assert failed

def test_construction_from_clr():
import clr
Expand Down