Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Fixed accessing partially overriden properties from Python#1650

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
lostmsu merged 1 commit intopythonnet:masterfromlosttech:bugs/OverridenProperty
Jan 4, 2022
Merged
Show file tree
Hide file tree
Changes fromall commits
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
21 changes: 21 additions & 0 deletionssrc/embed_tests/Inheritance.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -119,6 +119,15 @@ public void BaseClearIsCalled()
scope.Set("exn",null);
Assert.AreEqual(1,msg.Refcount);
}

// https://github.com/pythonnet/pythonnet/issues/1455
[Test]
publicvoidPropertyAccessorOverridden()
{
usingvarderived=newPropertyAccessorDerived().ToPython();
derived.SetAttr(nameof(PropertyAccessorDerived.VirtualProp),"hi".ToPython());
Assert.AreEqual("HI",derived.GetAttr(nameof(PropertyAccessorDerived.VirtualProp)).As<string>());
}
}

classExtraBaseTypeProvider:IPythonBaseTypeProvider
Expand DownExpand Up@@ -192,6 +201,18 @@ public int XProp
}
}

publicclassPropertyAccessorBase
{
publicvirtualstringVirtualProp{get;set;}
}

publicclassPropertyAccessorIntermediate:PropertyAccessorBase{}

publicclassPropertyAccessorDerived:PropertyAccessorIntermediate
{
publicoverridestringVirtualProp{set=>base.VirtualProp=value.ToUpperInvariant();}
}

publicclassContainerClass
{
publicvoidBaseMethod(){}
Expand Down
56 changes: 56 additions & 0 deletionssrc/runtime/ReflectionUtil.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
namespacePython.Runtime;

usingSystem;
usingSystem.Reflection;

staticclassReflectionUtil
{
publicstaticMethodInfo?GetBaseGetMethod(thisPropertyInfoproperty,boolnonPublic)
{
if(propertyisnull)thrownewArgumentNullException(nameof(property));

TypebaseType=property.DeclaringType.BaseType;
BindingFlagsbindingFlags=property.GetBindingFlags();

while(baseTypeis notnull)
{
varbaseProperty=baseType.GetProperty(property.Name,bindingFlags|BindingFlags.DeclaredOnly);
varaccessor=baseProperty?.GetGetMethod(nonPublic);
if(accessoris notnull)
returnaccessor;

baseType=baseType.BaseType;
}

returnnull;
}

publicstaticMethodInfo?GetBaseSetMethod(thisPropertyInfoproperty,boolnonPublic)
{
if(propertyisnull)thrownewArgumentNullException(nameof(property));

TypebaseType=property.DeclaringType.BaseType;
BindingFlagsbindingFlags=property.GetBindingFlags();

while(baseTypeis notnull)
{
varbaseProperty=baseType.GetProperty(property.Name,bindingFlags|BindingFlags.DeclaredOnly);
varaccessor=baseProperty?.GetSetMethod(nonPublic);
if(accessoris notnull)
returnaccessor;

baseType=baseType.BaseType;
}

returnnull;
}

publicstaticBindingFlagsGetBindingFlags(thisPropertyInfoproperty)
{
varaccessor=property.GetMethod??property.SetMethod;
BindingFlagsflags=default;
flags|=accessor.IsStatic?BindingFlags.Static:BindingFlags.Instance;
flags|=accessor.IsPublic?BindingFlags.Public:BindingFlags.NonPublic;
returnflags;
}
}
35 changes: 26 additions & 9 deletionssrc/runtime/propertyobject.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
usingSystem;
usingSystem.Reflection;
usingSystem.Runtime.Serialization;

namespacePython.Runtime
{
Expand All@@ -8,17 +9,25 @@ namespace Python.Runtime
/// Implements a Python descriptor type that manages CLR properties.
/// </summary>
[Serializable]
internalclassPropertyObject:ExtensionType
internalclassPropertyObject:ExtensionType,IDeserializationCallback
{
internalMaybeMemberInfo<PropertyInfo>info;
privateMaybeMethodInfogetter;
privateMaybeMethodInfosetter;
[NonSerialized]
privateMethodInfo?getter;
[NonSerialized]
privateMethodInfo?setter;

publicPropertyObject(PropertyInfomd)
{
getter=md.GetGetMethod(true);
setter=md.GetSetMethod(true);
info=newMaybeMemberInfo<PropertyInfo>(md);
CacheAccessors();
}

voidCacheAccessors()
{
PropertyInfomd=info.Value;
getter=md.GetGetMethod(true)??md.GetBaseGetMethod(true);
setter=md.GetSetMethod(true)??md.GetBaseSetMethod(true);
}


Expand All@@ -35,7 +44,7 @@ public static NewReference tp_descr_get(BorrowedReference ds, BorrowedReference
returnExceptions.RaiseTypeError(self.info.DeletedMessage);
}
varinfo=self.info.Value;
MethodInfogetter=self.getter.UnsafeValue;
MethodInfo?getter=self.getter;
objectresult;


Expand DownExpand Up@@ -70,7 +79,7 @@ public static NewReference tp_descr_get(BorrowedReference ds, BorrowedReference

try
{
result=info.GetValue(co.inst,null);
result=getter.Invoke(co.inst,Array.Empty<object>());
returnConverter.ToPython(result,info.PropertyType);
}
catch(Exceptione)
Expand DownExpand Up@@ -100,7 +109,7 @@ public static int tp_descr_set(BorrowedReference ds, BorrowedReference ob, Borro
}
varinfo=self.info.Value;

MethodInfosetter=self.setter.UnsafeValue;
MethodInfo?setter=self.setter;

if(val==null)
{
Expand DownExpand Up@@ -141,7 +150,7 @@ public static int tp_descr_set(BorrowedReference ds, BorrowedReference ob, Borro
Exceptions.RaiseTypeError("invalid target");
return-1;
}
info.SetValue(co.inst,newval,null);
setter.Invoke(co.inst,newobject?[]{newval});
}
else
{
Expand DownExpand Up@@ -169,5 +178,13 @@ public static NewReference tp_repr(BorrowedReference ob)
varself=(PropertyObject)GetManagedObject(ob)!;
returnRuntime.PyString_FromString($"<property '{self.info}'>");
}

voidIDeserializationCallback.OnDeserialization(objectsender)
{
if(info.Valid)
{
CacheAccessors();
}
}
}
}

[8]ページ先頭

©2009-2025 Movatter.jp