コード例 #1
0
        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);
        }
コード例 #2
0
        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)));
        }
コード例 #3
0
        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);
        }
コード例 #4
0
ファイル: ObjectTests.cs プロジェクト: rockobonaparte/cloaca
        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);
        }
コード例 #5
0
        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)));
        }
コード例 #6
0
        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)));
        }
コード例 #7
0
        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));
        }
コード例 #8
0
        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));
        }
コード例 #9
0
ファイル: ExceptionTests.cs プロジェクト: bamyazi/cloaca
        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)));
        }
コード例 #10
0
ファイル: ExceptionTests.cs プロジェクト: bamyazi/cloaca
        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);
        }
コード例 #11
0
ファイル: ExceptionTests.cs プロジェクト: bamyazi/cloaca
        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"));
        }
コード例 #12
0
ファイル: ObjectTests.cs プロジェクト: rockobonaparte/cloaca
        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)));
        }
コード例 #13
0
        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)));
        }
コード例 #14
0
        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);
        }
コード例 #15
0
ファイル: ObjectTests.cs プロジェクト: rockobonaparte/cloaca
        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)));
        }
コード例 #16
0
ファイル: ObjectTests.cs プロジェクト: rockobonaparte/cloaca
        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)));
        }
コード例 #17
0
ファイル: ExceptionTests.cs プロジェクト: bamyazi/cloaca
        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)));
        }
コード例 #18
0
ファイル: ExceptionTests.cs プロジェクト: bamyazi/cloaca
        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)));
        }
コード例 #19
0
        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();
        }
コード例 #20
0
        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)));
        }
コード例 #21
0
        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)));
        }
コード例 #22
0
ファイル: ExceptionTests.cs プロジェクト: bamyazi/cloaca
        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)));
        }
コード例 #23
0
        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)));
        }