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
- Expanded the way attributes amy be added
- Fixed search bug in AssocAttribute
- Added testing
  • Loading branch information
rmadsen-ks committed Oct 31, 2022
commit 014ef2e298845a777f6e6c7a4b40dae1fa61408b
51 changes: 38 additions & 13 deletions src/runtime/Resources/clr.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,14 @@ def __set__(self, instance, value):
if not self.fset:
raise AttributeError("%s is read-only" % self.__name__)
return self.fset.__get__(instance, None)(value)
def add_attribute(self, attribute):
self._clr_attributes_.append(attribute)
def add_attribute(self, *args, **kwargs):
lst = []
if len(args) > 0:
if isinstance(args[0], tuple):
lst = args
else:
lst = [(args[0], args[1:], kwargs)]
self._clr_attributes_.extend(lst)
return self

class property(object):
Expand All @@ -68,9 +74,16 @@ def __get__(self, instance, owner):
return v
def __set__(self, instance, value):
self.values[instance] = value
def add_attribute(self, attribute):
self._clr_attributes_.append(attribute)
def add_attribute(self, *args, **kwargs):
lst = []
if len(args) > 0:
if isinstance(args[0], tuple):
lst = args
else:
lst = [(args[0], args[1:], kwargs)]
self._clr_attributes_.extend(lst)
return self

def __call__(self, type, default):
self2 = self.__class__(self._clr_property_type_, type, default)
self2._clr_attributes_ = self._clr_attributes_
Expand Down Expand Up @@ -118,22 +131,34 @@ def __call__(self, func):
def __get__(self, instance, owner):
return self.__func.__get__(instance, owner)

def clr_attribute(self, attribute):
self._clr_attributes_.append(attribute)
def add_attribute(self, *args, **kwargs):
lst = []
if len(args) > 0:
if isinstance(args[0], tuple):
lst = args
else:
lst = [(args[0], args[1:], kwargs)]
self._clr_attributes_.extend(lst)
return self

class attribute(object):

def __init__(self, attr, *args, **kwargs):
self.attr = attr
def __init__(self, *args, **kwargs):
lst = []
if len(args) > 0:
if isinstance(args[0], tuple):
lst = args
else:
lst = [(args[0], args[1:], kwargs)]
import Python.Runtime
#todo: ensure that attributes only are pushed when @ is used.
#import inspect
#Python.Runtime.PythonDerivedType.Test(inspect.stack()[1].code_context)
self.attr = lst
for item in lst:
Python.Runtime.PythonDerivedType.PushAttribute(item)

Python.Runtime.PythonDerivedType.PushAttribute(attr)
def __call__(self, x):
import Python.Runtime
if Python.Runtime.PythonDerivedType.AssocAttribute(self.attr, x):
pass
for item in self.attr:
if Python.Runtime.PythonDerivedType.AssocAttribute(item, x):
pass
return x
3 changes: 2 additions & 1 deletion src/runtime/Types/ClassDerived.cs
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,8 @@ public static bool AssocAttribute(PyObject obj, PyObject func)
var tp = new PyTuple(obj);
for (int i = 0; i < attributesStack.Count; i++)
{
if (Equals(tp, attributesStack[i]))

if (tp.BorrowNullable()== attributesStack[i].BorrowNullable())
{
attributesStack.RemoveAt(i);
if (!methodAssoc.TryGetValue(func, out var lst))
Expand Down
7 changes: 6 additions & 1 deletion tests/test_subclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from System.ComponentModel import (Browsable, BrowsableAttribute)
import pytest
from Python.Test import (IInterfaceTest, SubClassTest, EventArgsTest,
FunctionsTest, GenericVirtualMethodTest, ISimpleInterface, SimpleClass, TestAttribute)
FunctionsTest, GenericVirtualMethodTest, ISimpleInterface, SimpleClass, TestAttribute, TestAttributeAttribute)
from System.Collections.Generic import List


Expand Down Expand Up @@ -339,7 +339,12 @@ def test_class_with_advanced_attribute():
@clr.attribute(TestAttribute(1, 2, z = "A", W = "B"))
class ClassWithAttributes2(ISimpleInterface, SimpleClass):
pass
@clr.attribute(TestAttributeAttribute, 1, 2, z = "A", W = "B")
class ClassWithAttributes3(ISimpleInterface, SimpleClass):
X = clr.property(Double, 1.0).add_attribute(TestAttributeAttribute, 1, 2)

c = ClassWithAttributes2()
c2 = ClassWithAttributes3()
def test_more_subclasses():
import clr
class SubClass1(SimpleClass):
Expand Down