From 41e0c5bea59f35e4014f0f42dbc2d24ecc058ad3 Mon Sep 17 00:00:00 2001 From: Matt Ward Date: Sat, 24 Aug 2013 11:42:28 +0100 Subject: [PATCH] Fix Python forms designer. The Python forms designer was walking code after the InitializeComponents and if this code tried to create an instance of the form it would fail. Now the forms designer only walks the code inside the InitializeComponents. --- .../Project/Src/PythonComponentWalker.cs | 27 ++++---- .../Designer/AssignmentAfterFormClassTests.cs | 62 +++++++++++++++++++ .../Test/PythonBinding.Tests.csproj | 1 + 3 files changed, 76 insertions(+), 14 deletions(-) create mode 100644 src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/AssignmentAfterFormClassTests.cs diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonComponentWalker.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonComponentWalker.cs index 56fea8db0d..38ba2f5129 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonComponentWalker.cs +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonComponentWalker.cs @@ -31,6 +31,7 @@ namespace ICSharpCode.PythonBinding string componentName = String.Empty; PythonCodeDeserializer deserializer; ClassDefinition classDefinition; + bool walkingInitializeComponentMethod; public PythonComponentWalker(IComponentCreator componentCreator) { @@ -42,13 +43,13 @@ namespace ICSharpCode.PythonBinding /// Creates a control either a UserControl or Form from the python code. /// public IComponent CreateComponent(string pythonCode) - { + { PythonParser parser = new PythonParser(); PythonAst ast = parser.CreateAst(@"Control.py", new StringTextBuffer(pythonCode)); ast.Walk(this); // Did we find the InitializeComponent method? - if (!FoundInitializeComponentMethod) { + if (component == null) { throw new PythonComponentWalkerException("Unable to find InitializeComponents method."); } return component; @@ -88,14 +89,16 @@ namespace ICSharpCode.PythonBinding if (reader != null) { reader.Dispose(); } + walkingInitializeComponentMethod = true; node.Body.Walk(this); + walkingInitializeComponentMethod = false; } return false; } public override bool Walk(AssignmentStatement node) - { - if (!FoundInitializeComponentMethod) { + { + if (!walkingInitializeComponentMethod) { return false; } @@ -120,7 +123,7 @@ namespace ICSharpCode.PythonBinding public override bool Walk(ConstantExpression node) { - if (!FoundInitializeComponentMethod) { + if (!walkingInitializeComponentMethod) { return false; } @@ -129,8 +132,8 @@ namespace ICSharpCode.PythonBinding } public override bool Walk(CallExpression node) - { - if (!FoundInitializeComponentMethod) { + { + if (!walkingInitializeComponentMethod) { return false; } @@ -144,7 +147,7 @@ namespace ICSharpCode.PythonBinding public override bool Walk(NameExpression node) { - if (!FoundInitializeComponentMethod) { + if (!walkingInitializeComponentMethod) { return false; } @@ -159,7 +162,7 @@ namespace ICSharpCode.PythonBinding /// public override bool Walk(AugmentedAssignStatement node) { - if (!FoundInitializeComponentMethod) { + if (!walkingInitializeComponentMethod) { return false; } @@ -176,7 +179,7 @@ namespace ICSharpCode.PythonBinding PropertyDescriptor propertyDescriptor = componentCreator.GetEventProperty(eventDescriptor); propertyDescriptor.SetValue(currentComponent, eventHandlerName); return false; - } + } /// /// Walks the binary expression which is the right hand side of an assignment statement. @@ -357,10 +360,6 @@ namespace ICSharpCode.PythonBinding return null; } - bool FoundInitializeComponentMethod { - get { return component != null; } - } - /// /// Returns true if the expression is of the form: /// diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/AssignmentAfterFormClassTests.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/AssignmentAfterFormClassTests.cs new file mode 100644 index 0000000000..9b706477e3 --- /dev/null +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/AssignmentAfterFormClassTests.cs @@ -0,0 +1,62 @@ +// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) +// This code is distributed under the GNU LGPL (for details please see \doc\license.txt) + +using System; +using System.Resources; +using ICSharpCode.Core; +using NUnit.Framework; + +namespace PythonBinding.Tests.Designer +{ + /// + /// Tests that only the InitializeComponents method is processed + /// when walking the AST. + /// + [TestFixture] + public class AssignmentAfterFormClassTests : LoadFormTestFixtureBase + { + public override void BeforeSetUpFixture() + { + var rm = new ResourceManager("PythonBinding.Tests.Strings", GetType().Assembly); + ResourceService.RegisterNeutralStrings(rm); + } + + public override string PythonCode { + get { + return + "import clr\r\n" + + "clr.AddReference('System.Windows.Forms')\r\n" + + "clr.AddReference('System.Drawing')\r\n" + + "\r\n" + + "import System.Drawing\r\n" + + "import System.Windows.Forms\r\n" + + "\r\n" + + "from System.Drawing import *\r\n" + + "from System.Windows.Forms import *\r\n" + + "\r\n" + + "class MainForm(System.Windows.Forms.Form):\r\n" + + " def __init__(self):\r\n" + + " self.InitializeComponent()\r\n" + + " \r\n" + + " def InitializeComponent(self):\r\n" + + " self.SuspendLayout()\r\n" + + " # \r\n" + + " # MainForm\r\n" + + " # \r\n" + + " self.ClientSize = System.Drawing.Size(300, 400)\r\n" + + " self.Name = \"MainForm\"\r\n" + + " self.ResumeLayout(False)\r\n" + + "\r\n" + + "test = MainForm()\r\n" + + "Application.Run(test)\r\n" + + "\r\n"; + } + } + + [Test] + public void MainFormName() + { + Assert.AreEqual("MainForm", Form.Name); + } + } +} diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj b/src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj index 7e52957187..2a94163f96 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj @@ -183,6 +183,7 @@ +