Browse Source

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
shortcuts
Matt Ward 17 years ago
parent
commit
cd5c3f6364
  1. 12
      src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonParser.cs
  2. 61
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Parsing/InvalidCastInPythonParserTestFixture.cs
  3. 1
      src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj

12
src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonParser.cs

@ -98,10 +98,14 @@ namespace ICSharpCode.PythonBinding @@ -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);

61
src/AddIns/BackendBindings/Python/PythonBinding/Test/Parsing/InvalidCastInPythonParserTestFixture.cs

@ -0,0 +1,61 @@ @@ -0,0 +1,61 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/>
// <version>$Revision$</version>
// </file>
using System;
using ICSharpCode.PythonBinding;
using ICSharpCode.SharpDevelop.Dom;
using IronPython.Compiler.Ast;
using NUnit.Framework;
using PythonBinding.Tests;
namespace PythonBinding.Tests.Parsing
{
/// <summary>
/// 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)
/// </summary>
[TestFixture]
public class InvalidCastInPythonParserTestFixture
{
string code = "class Project(id): \r\n" +
" def __init__ Project_ID(): \r\n" +
" #i\r\n";
/// <summary>
/// Check that IronPython bug still exists.
/// </summary>
[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);
}
}
}

1
src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj

@ -174,6 +174,7 @@ @@ -174,6 +174,7 @@
<Compile Include="ImportCompletionTestFixture.cs" />
<Compile Include="LanguageBindingTestFixture.cs" />
<Compile Include="Parsing\ClassWithBaseClassTestFixture.cs" />
<Compile Include="Parsing\InvalidCastInPythonParserTestFixture.cs" />
<Compile Include="Parsing\MethodWithParametersTestFixture.cs" />
<Compile Include="Parsing\MissingLastParameterFromMethodTestFixture.cs" />
<Compile Include="Parsing\ParseClassWithCtorTestFixture.cs" />

Loading…
Cancel
Save