示例#1
0
        public void CanBorrowFromNewReference()
        {
            var dict = new PyDict();

            using NewReference reference = Runtime.PyDict_Items(dict.Reference);
            BorrowedReference borrowed = reference.BorrowOrThrow();

            PythonException.ThrowIfIsNotZero(Runtime.PyList_Reverse(borrowed));
        }
示例#2
0
            public override ValueType Execute(ValueType arg)
            {
                const string code = @"
from Python.EmbeddingTest.Domain import MyClass

def test_obj_call():
    obj = MyClass()
    obj.Method()
    obj.StaticMethod()
    obj.Property = 1
    obj.Field = 10

test_obj_call()
";
                const string name = "test_domain_reload_mod";

                using (Py.GIL())
                {
                    // Create a new module
                    IntPtr module = PyRuntime.PyModule_New(name);
                    Assert.That(module != IntPtr.Zero);
                    IntPtr globals = PyRuntime.PyObject_GetAttrString(module, "__dict__");
                    Assert.That(globals != IntPtr.Zero);
                    try
                    {
                        // import builtins
                        // module.__dict__[__builtins__] = builtins
                        int res = PyRuntime.PyDict_SetItemString(globals, "__builtins__",
                                                                 PyRuntime.PyEval_GetBuiltins());
                        PythonException.ThrowIfIsNotZero(res);

                        // Execute the code in the module's scope
                        PythonEngine.Exec(code, globals);
                        // import sys
                        // modules = sys.modules
                        IntPtr modules = PyRuntime.PyImport_GetModuleDict();
                        // modules[name] = module
                        res = PyRuntime.PyDict_SetItemString(modules, name, module);
                        PythonException.ThrowIfIsNotZero(res);
                    }
                    catch
                    {
                        PyRuntime.XDecref(module);
                        throw;
                    }
                    finally
                    {
                        PyRuntime.XDecref(globals);
                    }
                    return(module);
                }
            }
        public void CanBorrowFromNewReference()
        {
            var          dict      = new PyDict();
            NewReference reference = Runtime.PyDict_Items(dict.Handle);

            try
            {
                PythonException.ThrowIfIsNotZero(Runtime.PyList_Reverse(reference));
            }
            finally
            {
                reference.Dispose();
            }
        }