From cd5c3f63646a8b950ea02e07b148878a54a37a5c Mon Sep 17 00:00:00 2001 From: Matt Ward Date: Sat, 14 Feb 2009 12:50:44 +0000 Subject: [PATCH] Catch InvalidCastException in IronPython parser when parsing invalid code. git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/3.0@3807 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61 --- .../PythonBinding/Project/Src/PythonParser.cs | 12 ++-- .../InvalidCastInPythonParserTestFixture.cs | 61 +++++++++++++++++++ .../Test/PythonBinding.Tests.csproj | 1 + 3 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 src/AddIns/BackendBindings/Python/PythonBinding/Test/Parsing/InvalidCastInPythonParserTestFixture.cs diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonParser.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonParser.cs index 4b2b4015ca..96df0dafd6 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonParser.cs +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonParser.cs @@ -98,10 +98,14 @@ namespace ICSharpCode.PythonBinding public ICompilationUnit Parse(IProjectContent projectContent, string fileName, string fileContent) { if (fileContent != null) { - PythonAst ast = CreateAst(fileName, fileContent); - PythonAstWalker walker = new PythonAstWalker(projectContent, fileName); - walker.Walk(ast); - return walker.CompilationUnit; + try { + PythonAst ast = CreateAst(fileName, fileContent); + PythonAstWalker walker = new PythonAstWalker(projectContent, fileName); + walker.Walk(ast); + return walker.CompilationUnit; + } catch (InvalidCastException) { + // Ignore. + } } DefaultCompilationUnit compilationUnit = new DefaultCompilationUnit(projectContent); diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Parsing/InvalidCastInPythonParserTestFixture.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Parsing/InvalidCastInPythonParserTestFixture.cs new file mode 100644 index 0000000000..fe86402cda --- /dev/null +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Parsing/InvalidCastInPythonParserTestFixture.cs @@ -0,0 +1,61 @@ +// +// +// +// +// $Revision$ +// + +using System; +using ICSharpCode.PythonBinding; +using ICSharpCode.SharpDevelop.Dom; +using IronPython.Compiler.Ast; +using NUnit.Framework; +using PythonBinding.Tests; + +namespace PythonBinding.Tests.Parsing +{ + /// + /// The IronPython parser will throw an invalid cast exception for the following code: + /// + /// class Project(id): + /// def __init__ Project_ID(): + /// #i + /// + /// System.InvalidCastException: Unable to cast object of type 'IronPython.Compiler.Ast.ErrorExpression' to type 'IronPython.Compiler.Ast.NameExpression'. + /// at IronPython.Compiler.Parser.ParseParameter(Int32 position, Dictionary`2 names) + /// at IronPython.Compiler.Parser.ParseVarArgsList(TokenKind terminator) + /// at IronPython.Compiler.Parser.ParseFuncDef() + /// at IronPython.Compiler.Parser.ParseStmt() + /// at IronPython.Compiler.Parser.ParseSuite() + /// at IronPython.Compiler.Parser.ParseClassDef() + /// at IronPython.Compiler.Parser.ParseStmt() + /// at IronPython.Compiler.Parser.ParseFile(Boolean makeModule) + /// at ICSharpCode.PythonBinding.PythonParser.CreateAst(String fileName, String fileContent) + /// + [TestFixture] + public class InvalidCastInPythonParserTestFixture + { + string code = "class Project(id): \r\n" + + " def __init__ Project_ID(): \r\n" + + " #i\r\n"; + + /// + /// Check that IronPython bug still exists. + /// + [Test] + [ExpectedException(typeof(InvalidCastException))] + public void CreateAstShouldThrowInvalidCastException() + { + PythonParser parser = new PythonParser(); + PythonAst ast = parser.CreateAst(@"d:\projects\test\test.py", code); + } + + [Test] + public void ParseShouldNotThrowInvalidCastException() + { + PythonParser parser = new PythonParser(); + ICompilationUnit unit = parser.Parse(new DefaultProjectContent(), @"d:\projects\test\test.py", code); + Assert.AreEqual(0, unit.Classes.Count); + } + } +} diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj b/src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj index 0de917d37a..b81c053acb 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj @@ -174,6 +174,7 @@ +