Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

ExceptCommand, first tests

  • Loading branch information...
commit 972fca4f35b25e92b496d731e768a000b7005992 1 parent c5f3db3
@ajlopez authored
View
54 Src/PythonSharp.Tests/Commands/ExceptCommandTests.cs
@@ -0,0 +1,54 @@
+namespace PythonSharp.Tests.Commands
+{
+ using System;
+ using System.Text;
+ using System.Collections.Generic;
+ using System.Linq;
+ using Microsoft.VisualStudio.TestTools.UnitTesting;
+ using PythonSharp.Commands;
+ using PythonSharp.Expressions;
+ using PythonSharp.Exceptions;
+
+ [TestClass]
+ public class ExceptCommandTests
+ {
+ [TestMethod]
+ public void CatchValueError()
+ {
+ ExceptCommand command = new ExceptCommand(new ConstantExpression(typeof(ValueError)), null);
+
+ Assert.IsNotNull(command.Expression);
+ Assert.IsNull(command.Command);
+ Assert.IsTrue(command.CatchException(null, new ValueError(null)));
+ Assert.IsFalse(command.CatchException(null, new SyntaxError(null)));
+ }
+
+ [TestMethod]
+ public void CatchBaseException()
+ {
+ ExceptCommand command = new ExceptCommand(new ConstantExpression(typeof(Exception)), null);
+
+ Assert.IsNotNull(command.Expression);
+ Assert.IsNull(command.Command);
+ Assert.IsTrue(command.CatchException(null, new ValueError(null)));
+ Assert.IsTrue(command.CatchException(null, new SyntaxError(null)));
+ }
+
+ [TestMethod]
+ public void RaiseIfExpressionIsNotAnException()
+ {
+ ExceptCommand command = new ExceptCommand(new ConstantExpression(1), null);
+
+ try
+ {
+ command.CatchException(null, new ValueError(null));
+ Assert.Fail("Exception expected");
+ }
+ catch (Exception ex)
+ {
+ Assert.IsInstanceOfType(ex, typeof(TypeError));
+ Assert.AreEqual("catching classes that do not inherit from BaseException is not allowed", ex.Message);
+ }
+ }
+ }
+}
View
17 Src/PythonSharp.Tests/Compiler/ParserTest.cs
@@ -1831,6 +1831,23 @@ public void CompileSimpleTryWithFinally()
Assert.IsNull(parser.CompileCommand());
}
+ [TestMethod]
+ public void RaiseWhenTwoFinallyCommands()
+ {
+ Parser parser = new Parser("try:\r\n pass\r\nfinally:\r\n pass\r\nfinally:\r\n pass");
+
+ try
+ {
+ parser.CompileCommand();
+ Assert.Fail("Exception expected");
+ }
+ catch (Exception ex)
+ {
+ Assert.IsInstanceOfType(ex, typeof(SyntaxError));
+ Assert.AreEqual("invalid syntax", ex.Message);
+ }
+ }
+
private static object CompileAndEvaluateExpression(string text)
{
Machine machine = new Machine();
View
1  Src/PythonSharp.Tests/PythonSharp.Tests.csproj
@@ -45,6 +45,7 @@
<Compile Include="Classes\Person.cs" />
<Compile Include="Commands\ClassCommandTests.cs" />
<Compile Include="Commands\CompositeCommandTests.cs" />
+ <Compile Include="Commands\ExceptCommandTests.cs" />
<Compile Include="Commands\ForCommandTests.cs" />
<Compile Include="Commands\ImportCommandTest.cs" />
<Compile Include="Commands\DefCommandTests.cs" />
View
43 Src/PythonSharp/Commands/ExceptCommand.cs
@@ -0,0 +1,43 @@
+namespace PythonSharp.Commands
+{
+ using System;
+ using System.Collections.Generic;
+ using System.Linq;
+ using System.Text;
+ using PythonSharp.Exceptions;
+ using PythonSharp.Expressions;
+ using PythonSharp.Language;
+
+ public class ExceptCommand : ICommand
+ {
+ private IExpression expression;
+ private ICommand command;
+
+ public ExceptCommand(IExpression expression, ICommand command)
+ {
+ this.expression = expression;
+ this.command = command;
+ }
+
+ public ICommand Command { get { return this.command; } }
+
+ public IExpression Expression { get { return this.expression; } }
+
+ public bool CatchException(IContext context, Exception exception)
+ {
+ var value = this.expression.Evaluate(context);
+
+ if (!(value is Type) || !((Type)value == typeof(Exception) || ((Type)value).IsSubclassOf(typeof(Exception))))
+ throw new TypeError("catching classes that do not inherit from BaseException is not allowed");
+
+ var type = (Type)value;
+
+ return exception.GetType() == type || exception.GetType().IsSubclassOf(type);
+ }
+
+ public void Execute(IContext context)
+ {
+ this.command.Execute(context);
+ }
+ }
+}
View
10 Src/PythonSharp/Compiler/Parser.cs
@@ -509,14 +509,20 @@ private ICommand CompileTryCommand()
int indent = this.lexer.NextIndent();
- if (indent == this.indent)
+ while (indent == this.indent)
{
if (this.TryCompile(TokenType.Name, "finally"))
{
+ if (trycommand.Finally != null)
+ throw new SyntaxError("invalid syntax");
+
ICommand finallycommand = this.CompileSuite();
trycommand.SetFinally(finallycommand);
- return trycommand;
}
+ else
+ break;
+
+ indent = this.lexer.NextIndent();
}
this.lexer.PushIndent(indent);
View
1  Src/PythonSharp/PythonSharp.csproj
@@ -50,6 +50,7 @@
<Compile Include="Commands\CompositeCommand.cs" />
<Compile Include="Commands\ContinueCommand.cs" />
<Compile Include="Commands\BreakCommand.cs" />
+ <Compile Include="Commands\ExceptCommand.cs" />
<Compile Include="Commands\TryCommand.cs" />
<Compile Include="Commands\SetIndexCommand.cs" />
<Compile Include="Commands\ForCommand.cs" />
View
2  Src/Todo.txt
@@ -135,6 +135,8 @@ Import
+ break
+ continue
- try/except/finally
+ see http://stackoverflow.com/questions/730764/try-catch-in-python
+ difference btw exceptions.Exception exceptions.BaseException
- raise
+ Real number
Please sign in to comment.
Something went wrong with that request. Please try again.