diff --git a/src/core/IronPython/Runtime/Operations/CustomTypeDescHelpers.cs b/src/core/IronPython/Runtime/Operations/CustomTypeDescHelpers.cs index 7a2b711c7..2b9bf3ae5 100644 --- a/src/core/IronPython/Runtime/Operations/CustomTypeDescHelpers.cs +++ b/src/core/IronPython/Runtime/Operations/CustomTypeDescHelpers.cs @@ -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())); } } @@ -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) { diff --git a/tests/IronPython.Tests/CustomTypeDescriptorTest.cs b/tests/IronPython.Tests/CustomTypeDescriptorTest.cs new file mode 100644 index 000000000..a973644b7 --- /dev/null +++ b/tests/IronPython.Tests/CustomTypeDescriptorTest.cs @@ -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()); + } + + [Test] + public void GetPropertiesLetsKeyboardInterruptPropagate() { + object instance = CreateInstance("WithInterrupt"); + + Assert.That(() => TypeDescriptor.GetProperties(instance), Throws.InstanceOf()); + } + } +}