public async Task IsSubclass() { var context = await runProgram("class Foo:\n" + " def __init__(self):\n" + " self.a = 1\n" + "\n" + "class Bar(Foo):\n" + " def change_a(self, new_a):\n" + " self.a = self.a + new_a\n" + "\n" + "class Unrelated:\n" + " pass\n" + "\n" + "bar = Bar()\n" + "class_class = issubclass(Bar, Foo)\n" + "unrelated_class_class = issubclass(Unrelated, Foo)\n" + "obj_class = issubclass(type(bar), Foo)\n" + "unrelated_obj_class = issubclass(type(bar), Unrelated)\n", new Dictionary <string, object>(), 1); var variables = new VariableMultimap(context); var class_class = (bool)variables.Get("class_class"); var obj_class = (bool)variables.Get("obj_class"); var unrelated_class_class = (bool)variables.Get("unrelated_class_class"); var unrelated_obj_class = (bool)variables.Get("unrelated_obj_class"); Assert.That(class_class, Is.True); Assert.That(obj_class, Is.True); Assert.That(unrelated_class_class, Is.False); Assert.That(unrelated_obj_class, Is.False); }
public void AccessClassMethod() { //>>> def make_foo(): //... class Foo: //... def __init__(self): //... self.a = 1 //... def change_a(self, new_a): //... self.a = new_a //... //>>> dis(make_foo) // 2 0 LOAD_BUILD_CLASS // 2 LOAD_CONST 1 (<code object Foo at 0x0000021BD5908D20, file "<stdin>", line 2>) // 4 LOAD_CONST 2 ('Foo') // 6 MAKE_FUNCTION 0 // 8 LOAD_CONST 2 ('Foo') // 10 CALL_FUNCTION 2 // 12 STORE_FAST 0 (Foo) // 14 LOAD_CONST 0 (None) // 16 RETURN_VALUE var interpreter = runProgram("class Foo:\n" + " def __init__(self):\n" + " self.a = 1\n" + "\n" + " def change_a(self, new_a):\n" + " self.a = new_a\n" + "\n" + "bar = Foo()\n" + "bar.change_a(2)\n", new Dictionary <string, object>(), 1); var variables = new VariableMultimap(interpreter); var bar = (PyObject)variables.Get("bar"); Assert.That(bar.__dict__["a"], Is.EqualTo(PyInteger.Create(2))); }
public void IntCallsBaseMethods() { var interpreter = runProgram("f = 1\n" + "lt = f.__getattribute__('__lt__')\n", new Dictionary <string, object>(), 1); var variables = new VariableMultimap(interpreter); var testing = variables.Get("lt"); Assert.That(testing, Is.Not.Null); }
public async Task IntCallsBaseMethods() { var context = await runProgram("f = 1\n" + "lt = f.__getattribute__('__lt__')\n", new Dictionary <string, object>(), 1); var variables = new VariableMultimap(context); var testing = variables.Get("lt"); Assert.That(testing, Is.Not.Null); }
public void DeclareClassMember() { var interpreter = runProgram("class Foo:\n" + " def __init__(self):\n" + " self.a = 1\n" + "\n" + "bar = Foo()\n", new Dictionary <string, object>(), 1); var variables = new VariableMultimap(interpreter); var bar = (PyObject)variables.Get("bar"); Assert.That(bar.__dict__["a"], Is.EqualTo(PyInteger.Create(1))); }
public async Task Range() { FrameContext runContext = await runProgram( "test_range = range(0, 2, 1)\n" + "itr = test_range.__iter__()\n" + "raised_exception = False\n" + "i0 = itr.__next__()\n" + "i1 = itr.__next__()\n" + // Should raise StopIterationException on following __next__() "i2 = itr.__next__()\n", new Dictionary <string, object>(), 1, false); // TODO: [Escaped StopIteration] StopIteration (and other Python exceptions thrown in .NET should be caught as regular Python exceptions) Assert.NotNull(runContext.EscapedDotNetException); Assert.That(runContext.EscapedDotNetException.InnerException.GetType(), Is.EqualTo(typeof(StopIterationException))); var variables = new VariableMultimap(runContext); var i0 = (PyInteger)variables.Get("i0"); var i1 = (PyInteger)variables.Get("i1"); Assert.That(i0, Is.EqualTo(PyInteger.Create(0))); Assert.That(i1, Is.EqualTo(PyInteger.Create(1))); }
public void DeclareAndCreateClassDefaultConstructor() { var interpreter = runProgram("class Foo:\n" + " def __init__(self):\n" + " pass\n" + "bar = Foo()\n", new Dictionary <string, object>(), 1); var variables = new VariableMultimap(interpreter); Assert.That(variables.ContainsKey("bar")); var bar = variables.Get("bar", typeof(PyObject)); }
public void ObjectCallsBaseMethods() { var interpreter = runProgram("class Foo:\n" + " pass\n" + "\n" + "f = Foo()\n" + "testing = f.__eq__(f)\n", new Dictionary <string, object>(), 1); var variables = new VariableMultimap(interpreter); var testing = (PyBool)variables.Get("testing"); Assert.That(testing, Is.EqualTo(PyBool.True)); }
public void TryExceptAliasBasic() { var interpreter = runProgram( "a = 0\n" + "try:\n" + " raise Exception('Hello, World!')\n" + "except Exception as e:\n" + " a = 10\n", new Dictionary <string, object>(), 1); var variables = new VariableMultimap(interpreter); var a = (PyInteger)variables.Get("a"); Assert.That(a, Is.EqualTo(PyInteger.Create(10))); }
public void RaiseFromClass() { var interpreter = runProgram( "a = False\n" + "try:\n" + " raise Exception\n" + "except Exception:\n" + " a = True\n", new Dictionary <string, object>(), 1); var variables = new VariableMultimap(interpreter); var a = (bool)variables.Get("a"); Assert.That(a, Is.True); }
public void TryExceptAliasUseMessage() { var interpreter = runProgram( "a = 'Fail'\n" + "try:\n" + " raise Exception('Pass')\n" + "except Exception as e:\n" + " a = str(e)\n", new Dictionary <string, object>(), 1); var variables = new VariableMultimap(interpreter); var a = (PyInteger)variables.Get("a"); Assert.That(a, Is.EqualTo("Pass")); }
public async Task DeclareConstructorArgument() { var context = await runProgram("class Foo:\n" + " def __init__(self, new_a):\n" + " self.a = new_a\n" + "\n" + "bar = Foo(2)\n", new Dictionary <string, object>(), 1); var variables = new VariableMultimap(context); var bar = (PyObject)variables.Get("bar"); Assert.That(bar.__dict__["a"], Is.EqualTo(PyInteger.Create(2))); }
public async Task TryExceptTyped() { var context = await runProgram( "a = 0\n" + "try:\n" + " raise Exception('Hello, World!')\n" + "except Exception:\n" + " a = 10\n", new Dictionary <string, object>(), 1); var variables = new VariableMultimap(context); var a = (PyInteger)variables.Get("a"); Assert.That(a, Is.EqualTo(PyInteger.Create(10))); }
public async Task RaiseFromClass() { var context = await runProgram( "a = False\n" + "try:\n" + " raise Exception\n" + "except Exception:\n" + " a = True\n", new Dictionary <string, object>(), 1); var variables = new VariableMultimap(context); var a = (bool)variables.Get("a"); Assert.That(a, Is.True); }
public async Task SubclassBasic() { /* * 2 0 LOAD_BUILD_CLASS * 2 LOAD_CONST 1 (<code object Foo at 0x000001DBDF5511E0, file "<stdin>", line 2>) * 4 LOAD_CONST 2 ('Foo') * 6 MAKE_FUNCTION 0 * 8 LOAD_CONST 2 ('Foo') * 10 CALL_FUNCTION 2 * 12 STORE_FAST 0 (Foo) * * 5 14 LOAD_BUILD_CLASS * 16 LOAD_CONST 3 (<code object Bar at 0x000001DBDF568390, file "<stdin>", line 5>) * 18 LOAD_CONST 4 ('Bar') * 20 MAKE_FUNCTION 0 * 22 LOAD_CONST 4 ('Bar') * 24 LOAD_FAST 0 (Foo) * 26 CALL_FUNCTION 3 * 28 STORE_FAST 1 (Bar) * * 8 30 LOAD_FAST 1 (Bar) * 32 CALL_FUNCTION 0 * 34 STORE_FAST 2 (bar) * * 9 36 LOAD_FAST 2 (bar) * 38 LOAD_ATTR 0 (change_a) * 40 LOAD_CONST 5 (1) * 42 CALL_FUNCTION 1 * 44 POP_TOP * 46 LOAD_CONST 0 (None) * 48 RETURN_VALUE */ var context = await runProgram("class Foo:\n" + " def __init__(self):\n" + " self.a = 1\n" + "\n" + "class Bar(Foo):\n" + " def change_a(self, new_a):\n" + " self.a = self.a + new_a\n" + "\n" + "bar = Bar()\n" + "bar.change_a(1)\n", new Dictionary <string, object>(), 1); var variables = new VariableMultimap(context); var bar = (PyObject)variables.Get("bar"); Assert.That(bar.__dict__["a"], Is.EqualTo(PyInteger.Create(2))); }
public async Task AccessClassMember() { var context = await runProgram("class Foo:\n" + " def __init__(self):\n" + " self.a = 1\n" + "\n" + "bar = Foo()\n" + "b = bar.a\n" + "bar.a = 2\n", new Dictionary <string, object>(), 1); var variables = new VariableMultimap(context); var bar = (PyObject)variables.Get("bar"); Assert.That(bar.__dict__["a"], Is.EqualTo(PyInteger.Create(2))); }
public void TryExceptElse() { var interpreter = runProgram( "a = 0\n" + "try:\n" + " a = 1\n" + "except Exception as e:\n" + " a = a + 10\n" + "else:\n" + " a = a + 100\n", new Dictionary <string, object>(), 1); var variables = new VariableMultimap(interpreter); var a = (PyInteger)variables.Get("a"); Assert.That(a, Is.EqualTo(PyInteger.Create(101))); }
public void TryExceptAliasUseValue() { var interpreter = runProgram( "class MeowException(Exception):\n" + " def __init__(self, number):\n" + " self.number = number\n" + "a = 0\n" + "try:\n" + " raise MeowException(1)\n" + "except MeowException as e:\n" + " a = e.number\n", new Dictionary <string, object>(), 1); var variables = new VariableMultimap(interpreter); var a = (PyInteger)variables.Get("a"); Assert.That(a, Is.EqualTo(PyInteger.Create(1))); }
public async Task TryExceptElse() { var context = await runProgram( "a = 0\n" + "try:\n" + " a = 1\n" + "except Exception as e:\n" + " a = a + 10\n" + "else:\n" + " a = a + 100\n", new Dictionary <string, object>(), 1); var variables = new VariableMultimap(context); var a = (PyInteger)variables.Get("a"); Assert.That(a, Is.EqualTo(PyInteger.Create(101))); AssertNoDotNetExceptions(); }
public async Task TryUnhandledFinally() { FrameContext runContext = await runProgram( "a = 0\n" + "try:\n" + " raise Exception('Hello, World!')\n" + "finally:\n" + " a = 1\n", new Dictionary <string, object>(), 1, false); Assert.NotNull(runContext.CurrentException); Assert.That(runContext.CurrentException.__dict__["message"], Is.EqualTo("Hello, World!")); var variables = new VariableMultimap(runContext); var a = (PyInteger)variables.Get("a"); Assert.That(a, Is.EqualTo(PyInteger.Create(1))); }
public void SubclassSuperconstructor() { var interpreter = runProgram("class Foo:\n" + " def __init__(self):\n" + " self.a = 1\n" + "\n" + "class Bar(Foo):\n" + " def __init__(self):\n" + " super().__init__()\n" + " self.b = 2\n" + "\n" + "bar = Bar()\n", new Dictionary <string, object>(), 1); var variables = new VariableMultimap(interpreter); var bar = (PyObject)variables.Get("bar"); Assert.That(bar.__dict__["a"], Is.EqualTo(PyInteger.Create(1))); Assert.That(bar.__dict__["b"], Is.EqualTo(PyInteger.Create(2))); }
public void TryUnhandledFinally() { FrameContext runContext = null; Assert.Throws <EscapedPyException>( () => { runProgram( "a = 0\n" + "try:\n" + " raise Exception('Hello, World!')\n" + "finally:\n" + " a = 1\n", new Dictionary <string, object>(), 1, out runContext); }, "Hello, World!"); var variables = new VariableMultimap(runContext); var a = (PyInteger)variables.Get("a"); Assert.That(a, Is.EqualTo(PyInteger.Create(1))); }
public async Task TryExceptTwoExceptions() { var context = await runProgram( "class MeowException(Exception):\n" + " def __init__(self, number):\n" + " self.number = number\n" + "a = 0\n" + "try:\n" + " raise MeowException(1)\n" + "except Exception as ignored:\n" + " a = -1\n" + "except MeowException as e:\n" + " a = e.number\n", new Dictionary <string, object>(), 1); var variables = new VariableMultimap(context); var a = (PyInteger)variables.Get("a"); Assert.That(a, Is.EqualTo(PyInteger.Create(1))); }