예제 #1
0
        public static void PyCheck_Iter_PyObject_IsIterable_Test()
        {
            Runtime.Runtime.Py_Initialize();

            Runtime.Native.ABI.Initialize(Runtime.Runtime.PyVersion);

            // Tests that a python list is an iterable, but not an iterator
            using (var pyList = NewReference.DangerousFromPointer(Runtime.Runtime.PyList_New(0)))
            {
                Assert.IsFalse(Runtime.Runtime.PyIter_Check(pyList));
                Assert.IsTrue(Runtime.Runtime.PyObject_IsIterable(pyList));

                // Tests that a python list iterator is both an iterable and an iterator
                using var pyListIter = Runtime.Runtime.PyObject_GetIter(pyList);
                Assert.IsTrue(Runtime.Runtime.PyObject_IsIterable(pyListIter));
                Assert.IsTrue(Runtime.Runtime.PyIter_Check(pyListIter));
            }

            // Tests that a python float is neither an iterable nor an iterator
            using (var pyFloat = NewReference.DangerousFromPointer(Runtime.Runtime.PyFloat_FromDouble(2.73)))
            {
                Assert.IsFalse(Runtime.Runtime.PyObject_IsIterable(pyFloat));
                Assert.IsFalse(Runtime.Runtime.PyIter_Check(pyFloat));
            }

            Runtime.Runtime.Py_Finalize();
        }
예제 #2
0
        public static void PyCheck_Iter_PyObject_IsIterable_ThreadingLock_Test()
        {
            Runtime.Runtime.Py_Initialize();

            Runtime.Native.ABI.Initialize(Runtime.Runtime.PyVersion);

            try
            {
                // Create an instance of threading.Lock, which is one of the very few types that does not have the
                // TypeFlags.HaveIter set in Python 2. This tests a different code path in PyObject_IsIterable and PyIter_Check.
                using var threading = Runtime.Runtime.PyImport_ImportModule("threading");
                Exceptions.ErrorCheck(threading);
                var threadingDict = Runtime.Runtime.PyModule_GetDict(threading);
                Exceptions.ErrorCheck(threadingDict);
                var lockType = Runtime.Runtime.PyDict_GetItemString(threadingDict, "Lock");
                if (lockType.IsNull)
                {
                    throw PythonException.ThrowLastAsClrException();
                }

                using var args         = NewReference.DangerousFromPointer(Runtime.Runtime.PyTuple_New(0));
                using var lockInstance = Runtime.Runtime.PyObject_CallObject(lockType, args);
                Exceptions.ErrorCheck(lockInstance);

                Assert.IsFalse(Runtime.Runtime.PyObject_IsIterable(lockInstance));
                Assert.IsFalse(Runtime.Runtime.PyIter_Check(lockInstance));
            }
            finally
            {
                Runtime.Runtime.Py_Finalize();
            }
        }
예제 #3
0
            public override ValueType Execute(ValueType arg)
            {
                // handle refering a clr object created in previous domain,
                // it should had been deserialized and became callable agian.
                using var handle = NewReference.DangerousFromPointer((IntPtr)arg);
                try
                {
                    using (Py.GIL())
                    {
                        BorrowedReference tp       = Runtime.Runtime.PyObject_TYPE(handle.Borrow());
                        IntPtr            tp_clear = Util.ReadIntPtr(tp, TypeOffset.tp_clear);
                        Assert.That(tp_clear, Is.Not.Null);

                        using (PyObject obj = new PyObject(handle.Steal()))
                        {
                            obj.InvokeMethod("Method");
                            obj.InvokeMethod("StaticMethod");

                            using (var scope = Py.CreateScope())
                            {
                                scope.Set("obj", obj);
                                scope.Exec(@"
obj.Method()
obj.StaticMethod()
obj.Property += 1
obj.Field += 10
");
                            }
                            var clrObj = obj.As <Domain.MyClass>();
                            Assert.AreEqual(clrObj.Property, 2);
                            Assert.AreEqual(clrObj.Field, 20);
                        }
                    }
                }
                catch (Exception e)
                {
                    Debug.WriteLine(e);
                    throw;
                }
                return(0);
            }
예제 #4
0
        public static IntPtr CreateObjectType()
        {
            using var globals = NewReference.DangerousFromPointer(Runtime.PyDict_New());
            if (Runtime.PyDict_SetItemString(globals, "__builtins__", Runtime.PyEval_GetBuiltins()) != 0)
            {
                globals.Dispose();
                throw new PythonException();
            }
            const string code = "class A(object): pass";

            using var resRef = Runtime.PyRun_String(code, RunFlagType.File, globals, globals);
            if (resRef.IsNull())
            {
                globals.Dispose();
                throw new PythonException();
            }
            resRef.Dispose();
            BorrowedReference A = Runtime.PyDict_GetItemString(globals, "A");

            Debug.Assert(!A.IsNull);
            return(new NewReference(A).DangerousMoveToPointer());
        }