8000 - Expanded the way attributes amy be added · pythonnet/pythonnet@014ef2e · GitHub
[go: up one dir, main page]

Skip to content

Commit 014ef2e

Browse files
committed
- Expanded the way attributes amy be added
- Fixed search bug in AssocAttribute - Added testing
1 parent 28f4717 commit 014ef2e

File tree

3 files changed

+46
-15
lines changed

3 files changed

+46
-15
lines changed

src/runtime/Resources/clr.py

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,14 @@ def __set__(self, instance, value):
4949
if not self.fset:
5050
raise AttributeError("%s is read-only" % self.__name__)
5151
return self.fset.__get__(instance, None)(value)
52-
def add_attribute(self, attribute):
53-
self._clr_attributes_.append(attribute)
52+
def add_attribute(self, *args, **kwargs):
53+
lst = []
54+
if len(args) > 0:
55+
if isinstance(args[0], tuple):
56+
lst = args
57+
else:
58+
lst = [(args[0], args[1:], kwargs)]
59+
self._clr_attributes_.extend(lst)
5460
return self
5561

5662
class property(object):
@@ -68,9 +74,16 @@ def __get__(self, instance, owner):
6874
return v
6975
def __set__(self, instance, value):
7076
self.values[instance] = value
71-
def add_attribute(self, attribute):
72-
self._clr_attributes_.append(attribute)
77+
def add_attribute(self, *args, **kwargs):
78+
lst = []
79+
if len(args) > 0:
80+
if isinstance(args[0], tuple):
81+
lst = args
82+
else:
83+
lst = [(args[0], args[1:], kwargs)]
84+
self._clr_attributes_.extend(lst)
7385
return self
86+
7487
def __call__(self, type, default):
7588
self2 = self.__class__(self._clr_property_type_, type, default)
7689
self2._clr_attributes_ = self._clr_attributes_
@@ -118,22 +131,34 @@ def __call__(self, func):
118131
def __get__(self, instance, owner):
119132
return self.__func.__get__(instance, owner)
120133

121-
def clr_attribute(self, attribute):
122-
self._clr_attributes_.append(attribute)
134+
def add_attribute(self, *args, **kwargs):
135+
lst = []
136+
if len(args) > 0:
137+
if isinstance(args[0], tuple):
138+
lst = args
139+
else:
140+
lst = [(args[0], args[1:], kwargs)]
141+
self._clr_attributes_.extend(lst)
123142
return self
124143

125144
class attribute(object):
126145

127-
def __init__(self, attr, *args, **kwargs):
128-
self.attr = attr
146+
def __init__(self, *args, **kwargs):
147+
lst = []
148+
if len(args) > 0:
149+
if isinstance(args[0], tuple):
150+
lst = args
151+
else:
152+
lst = [(args[0], args[1:], kwargs)]
129153
import Python.Runtime
130154
#todo: ensure that attributes only are pushed when @ is used.
131-
#import inspect
132-
#Python.Runtime.PythonDerivedType.Test(inspect.stack()[1].code_context)
155+
self.attr = lst
156+
for item in lst:
157+
Python.Runtime.PythonDerivedType.PushAttribute(item)
133158

134-
Python.Runtime.PythonDerivedType.PushAttribute(attr)
135159
def __call__(self, x):
136160
import Python.Runtime
137-
if Python.Runtime.PythonDerivedType.AssocAttribute(self.attr, x):
138-
pass
161+
for item in self.attr:
162+
if Python.Runtime.PythonDerivedType.AssocAttribute(item, x):
163+
pass
139164
return x

src/runtime/Types/ClassDerived.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -872,7 +872,8 @@ public static bool AssocAttribute(PyObject obj, PyObject func)
872872
var tp = new PyTuple(obj);
873873
for (int i = 0; i < attributesStack.Count; i++)
874874
{
875-
if (Equals(tp, attributesStack[i]))
875+
876+
if (tp.BorrowNullable()== attributesStack[i].BorrowNullable())
876877
{
877878
attributesStack.RemoveAt(i);
878879
if (!methodAssoc.TryGetValue(func, out var lst))

tests/test_subclass.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from System.ComponentModel import (Browsable, BrowsableAttribute)
1313
import pytest
1414
from Python.Test import (IInterfaceTest, SubClassTest, EventArgsTest,
15-
FunctionsTest, GenericVirtualMethodTest, ISimpleInterface, SimpleClass, TestAttribute)
15+
FunctionsTest, GenericVirtualMethodTest, ISimpleInterface, SimpleClass, TestAttribute, TestAttributeAttribute)
1616
from System.Collections.Generic import List
1717

1818

@@ -339,7 +339,12 @@ def test_class_with_advanced_attribute():
339339
@clr.attribute(TestAttribute(1, 2, z = "A", W = "B"))
340340
class ClassWithAttributes2(ISimpleInterface, SimpleClass):
341341
pass
342+
@clr.attribute(TestAttributeAttribute, 1, 2, z = "A", W = "B")
343+
class ClassWithAttributes3(ISimpleInterface, SimpleClass):
344+
X = clr.property(Double, 1.0).add_attribute(TestAttributeAttribute, 1, 2)
345+
342346
c = ClassWithAttributes2()
347+
c2 = ClassWithAttributes3()
343348
def test_more_subclasses():
344349
import clr
345350
class SubClass1(SimpleClass):

0 commit comments

Comments
 (0)
0