8000 added support for creating abstract classes using a __clr_abstract__ … · pythonnet/pythonnet@2449bd2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2449bd2

Browse files
committed
added support for creating abstract classes using a __clr_abstract__ attribute.
1 parent 014ef2e commit 2449bd2

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

src/python_tests_runner/PythonTestRunner.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ static IEnumerable<string[]> PythonTestCases()
4242
yield return new[] { "test_subclass", "test_class_with_attributes" };
4343
yield return new[] { "test_subclass", "test_class_with_advanced_attribute" };
4444
yield return new[] { "test_subclass", "test_more_subclasses" };
45+
yield return new[] { "test_subclass", "abstract_test" };< 8000 /div>
4546
}
4647

4748
/// <summary>

src/runtime/Types/ClassDerived.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,16 @@ internal static Type CreateDerivedType(string name,
280280
baseClass = typeof(object);
281281
}
282282

283+
bool isAbstract = false;
284+
if (py_dict != null && Runtime.PyDict_Check(py_dict))
285+
{
286+
using var dict = new PyDict(py_dict);
287+
if (dict.HasKey("__clr_abstract__"))
288+
isAbstract = true;
289+
}
290+
283291
TypeBuilder typeBuilder = moduleBuilder.DefineType(name,
284-
TypeAttributes.Public | TypeAttributes.Class,
292+
TypeAttributes.Public | TypeAttributes.Class | (isAbstract ? TypeAttributes.Abstract : 0),
285293
baseClass,
286294
interfaces.ToArray());
287295

@@ -359,6 +367,7 @@ internal static Type CreateDerivedType(string name,
359367
if (py_dict != null && Runtime.PyDict_Check(py_dict))
360368
{
361369
using var dict = new PyDict(py_dict);
370+
362371
if (dict.HasKey("__clr_attributes__"))
363372
{
364373
var attributes = new PyList(dict["__clr_attributes__"]);

tests/test_subclass.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,15 @@ def IncrementThing(self):
376376
obj = None
377377
SimpleClass.Test2()
378378

379-
379+
def abstract_test():
380+
class abstractClass(SimpleClass):
381+
__clr_abstract__ = True
382+
failed = False
383+
try:
384+
abstractClass()
385+
except:
386+
failed = True
387+
assert failed
380388

381389
def test_construction_from_clr():
382390
import clr

0 commit comments

Comments
 (0)
0