Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
13 changes: 10 additions & 3 deletions src/core/IronPython/Runtime/Operations/CustomTypeDescHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,10 @@ private static PropertyDescriptor[] GetPropertiesImpl(object self, Attribute[] a

PythonType dt = DynamicHelpers.GetPythonType(self);
dt.TryResolveSlot(DefaultContext.DefaultCLS, s, out PythonTypeSlot attrSlot);
object attrVal = ObjectOps.__getattribute__(DefaultContext.DefaultCLS, self, s);

Type attrType = (attrVal == null) ? typeof(NoneTypeOps) : attrVal.GetType();

if ((attrSlot != null && ShouldIncludeProperty(attrSlot, attributes)) ||
(attrSlot == null && ShouldIncludeInstanceMember(s, attributes))) {
Type attrType = GetAttributeType(self, s);
descrs.Add(new SuperDynamicObjectPropertyDescriptor(s, attrType, self.GetType()));
}
}
Expand All @@ -102,6 +100,15 @@ private static PropertyDescriptor[] GetPropertiesImpl(object self, Attribute[] a
return descrs.ToArray();
}

private static Type GetAttributeType(object self, string name) {
try {
object attrVal = ObjectOps.__getattribute__(DefaultContext.DefaultCLS, self, name);
return (attrVal == null) ? typeof(NoneTypeOps) : attrVal.GetType();
} catch (Exception ex) when (!(ex is OutOfMemoryException || ex is System.Threading.ThreadAbortException || ex is IronPython.Runtime.Exceptions.SystemExitException || ex is Microsoft.Scripting.KeyboardInterruptException)) {
return typeof(object);
}
}

private static bool ShouldIncludeInstanceMember(string memberName, Attribute[] attributes) {
bool include = true;
foreach (Attribute attr in attributes) {
Expand Down
115 changes: 115 additions & 0 deletions tests/IronPython.Tests/CustomTypeDescriptorTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.

using System;
using System.ComponentModel;

using IronPython.Hosting;
using IronPython.Runtime.Exceptions;

using Microsoft.Scripting.Hosting;

using NUnit.Framework;

namespace IronPythonTest {
public class CustomTypeDescriptorTest {
private const string Source = @"
class Model(object):
plain = 5

@property
def good(self):
return 7

@property
def raises(self):
raise ValueError('getter failed')

@property
def exits(self):
raise SystemExit(3)

@property
def interrupts(self):
raise KeyboardInterrupt()

class WithRaises(Model):
@property
def exits(self):
return 1

@property
def interrupts(self):
return 2

class WithExit(Model):
@property
def raises(self):
return 1

@property
def interrupts(self):
return 2

class WithInterrupt(Model):
@property
def raises(self):
return 1

@property
def exits(self):
return 2
";

private static object CreateInstance(string className) {
ScriptEngine engine = Python.CreateEngine();
ScriptScope scope = engine.CreateScope();
engine.Execute(Source, scope);
return engine.Execute(className + "()", scope);
}

[Test]
public void GetPropertiesTypesAGetterThatRaisesAsObject() {
object instance = CreateInstance("WithRaises");

PropertyDescriptorCollection properties = null;
Assert.DoesNotThrow(() => properties = TypeDescriptor.GetProperties(instance));

Assert.That(properties["raises"].PropertyType, Is.EqualTo(typeof(object)));
}

[Test]
public void GetPropertiesKeepsTheTypesOfTheOtherProperties() {
object instance = CreateInstance("WithRaises");

PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(instance);

Assert.That(properties["good"].PropertyType, Is.EqualTo(typeof(int)));
Assert.That(properties["plain"].PropertyType, Is.EqualTo(typeof(int)));
}

[Test]
public void ReadingTheValueOfAGetterThatRaisesStillRaises() {
object instance = CreateInstance("WithRaises");

PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(instance);

Assert.That(() => properties["raises"].GetValue(instance), Throws.Exception);
}

[Test]
public void GetPropertiesLetsSystemExitPropagate() {
object instance = CreateInstance("WithExit");

Assert.That(() => TypeDescriptor.GetProperties(instance), Throws.InstanceOf<SystemExitException>());
}

[Test]
public void GetPropertiesLetsKeyboardInterruptPropagate() {
object instance = CreateInstance("WithInterrupt");

Assert.That(() => TypeDescriptor.GetProperties(instance), Throws.InstanceOf<Exception>());
}
}
}
Loading