Esempio n. 1
0
        public static void RefCountTest()
        {
            Runtime.Runtime.Py_Initialize();
            using var op = Runtime.Runtime.PyString_FromString("FooBar");

            // New object RefCount should be one
            Assert.AreEqual(1, Runtime.Runtime.Refcount32(op.BorrowOrThrow()));

            // Checking refcount didn't change refcount
            Assert.AreEqual(1, Runtime.Runtime.Refcount32(op.Borrow()));

            // Borrowing a reference doesn't increase refcount
            BorrowedReference p = op.Borrow();

            Assert.AreEqual(1, Runtime.Runtime.Refcount32(p));

            // Py_IncRef/Py_DecRef increase and decrease RefCount
            Runtime.Runtime.Py_IncRef(op.Borrow());
            Assert.AreEqual(2, Runtime.Runtime.Refcount32(p));
            Runtime.Runtime.Py_DecRef(StolenReference.DangerousFromPointer(op.DangerousGetAddress()));
            Assert.AreEqual(1, Runtime.Runtime.Refcount32(p));

            // XIncref/XDecref increase and decrease RefCount
#pragma warning disable CS0618 // Type or member is obsolete. We are testing corresponding members
            Runtime.Runtime.XIncref(p);
            Assert.AreEqual(2, Runtime.Runtime.Refcount32(p));
            Runtime.Runtime.XDecref(op.Steal());
            Assert.AreEqual(1, Runtime.Runtime.Refcount32(p));
#pragma warning restore CS0618 // Type or member is obsolete

            op.Dispose();

            Runtime.Runtime.Py_Finalize();
        }
Esempio n. 2
0
        [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)] // ensure lack of references to s1 and s2
        private static IntPtr CreateStringGarbage()
        {
            PyString s1 = new PyString("test_string");
            // s2 steal a reference from s1
            PyString s2 = new PyString(StolenReference.DangerousFromPointer(s1.Handle));

            return(s1.Handle);
        }
Esempio n. 3
0
        [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)] // ensure lack of references to s1 and s2
        private static IntPtr CreateStringGarbage()
        {
            PyString s1 = new PyString("test_string");
            // s2 steal a reference from s1
            IntPtr   address = s1.Reference.DangerousGetAddress();
            PyString s2      = new (StolenReference.DangerousFromPointer(address));

            return(address);
        }
Esempio n. 4
0
        public PyObject TryEncode(object value)
        {
            if (value == null)
            {
                return(null);
            }

            var tupleType = value.GetType();

            if (tupleType == typeof(object))
            {
                return(null);
            }
            if (!this.CanEncode(tupleType))
            {
                return(null);
            }
            if (tupleType == typeof(TTuple))
            {
                return(new PyTuple());
            }

            long fieldCount = tupleType.GetGenericArguments().Length;
            var  tuple      = Runtime.PyTuple_New(fieldCount);

            Exceptions.ErrorCheck(tuple);
            int fieldIndex = 0;

            foreach (FieldInfo field in tupleType.GetFields())
            {
                var    item   = field.GetValue(value);
                IntPtr pyItem = Converter.ToPython(item, field.FieldType);
                Runtime.PyTuple_SetItem(tuple, fieldIndex, pyItem);
                fieldIndex++;
            }
            return(new PyTuple(StolenReference.DangerousFromPointer(tuple)));
        }