From a3afc53c6070d00de85a54147ffd30a66dc74e0b Mon Sep 17 00:00:00 2001 From: Matt Ward Date: Sat, 8 Aug 2009 17:17:27 +0000 Subject: [PATCH] Fixed null reference exception in the python forms designer when generating code for properties that have a null property value. git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/3.0@4625 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61 --- .../Project/Src/PythonDesignerComponent.cs | 10 ++++- .../AppendNullPropertyValueTestFixture.cs | 43 +++++++++++++++++++ .../Test/PythonBinding.Tests.csproj | 1 + 3 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/AppendNullPropertyValueTestFixture.cs diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonDesignerComponent.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonDesignerComponent.cs index 9bc6f1a591..a2c85c2172 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonDesignerComponent.cs +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonDesignerComponent.cs @@ -543,7 +543,7 @@ namespace ICSharpCode.PythonBinding } } else if (IsResourcePropertyValue(propertyValue)) { AppendResourceProperty(codeBuilder, propertyName, propertyValue); - } else if (propertyValue.GetType().IsArray) { + } else if (IsArray(propertyValue)) { codeBuilder.AppendIndented(propertyName + " = "); AppendSystemArray(codeBuilder, GetArrayType(propertyValue).FullName, propertyValue as ICollection, false); codeBuilder.AppendLine(); @@ -891,5 +891,13 @@ namespace ICSharpCode.PythonBinding Type type = obj.GetType(); return obj.GetType().GetElementType(); } + + static bool IsArray(object obj) + { + if (obj != null) { + return obj.GetType().IsArray; + } + return false; + } } } diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/AppendNullPropertyValueTestFixture.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/AppendNullPropertyValueTestFixture.cs new file mode 100644 index 0000000000..3ec311e371 --- /dev/null +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/AppendNullPropertyValueTestFixture.cs @@ -0,0 +1,43 @@ +// +// +// +// +// $Revision$ +// + +using System; +using System.ComponentModel; +using ICSharpCode.PythonBinding; +using NUnit.Framework; + +namespace PythonBinding.Tests.Designer +{ + /// + /// Tests that a null property value does not cause a NullReferenceException in the + /// PythonControl's AppendProperty method. + /// + [TestFixture] + public class AppendNullPropertyValueTestFixture + { + string fooBar; + + public string FooBar { + get { return fooBar; } + set { fooBar = value; } + } + + [Test] + public void GeneratedCode() + { + string expectedCode = "self._myObject.FooBar = None\r\n"; + PropertyDescriptor propertyDescriptor = TypeDescriptor.GetProperties(this).Find("FooBar", false); + + PythonDesignerComponent designerComponent = new PythonDesignerComponent(null); + PythonCodeBuilder codeBuilder = new PythonCodeBuilder(); + designerComponent.AppendProperty(codeBuilder, "self._myObject", this, propertyDescriptor); + string generatedCode = codeBuilder.ToString(); + + Assert.AreEqual(expectedCode, generatedCode, generatedCode); + } + } +} diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj b/src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj index fe0314fe14..1339946002 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj @@ -161,6 +161,7 @@ +