| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -49,12 +49,12 @@ public void Encodes() | |||
| 49 | 49 | }; | |
| 50 | 50 | ||
| 51 | 51 | var uri = group.TryEncode(new Uri("data:")); | |
| 52 | - var clrObject = (CLRObject)ManagedType.GetManagedObject(uri.Handle); | ||
| 52 | + var clrObject = (CLRObject)ManagedType.GetManagedObject(uri); | ||
| 53 | 53 | Assert.AreSame(encoder1, clrObject.inst); | |
| 54 | 54 | Assert.AreNotSame(encoder2, clrObject.inst); | |
| 55 | 55 | ||
| 56 | 56 | var tuple = group.TryEncode(Tuple.Create(1)); | |
| 57 | - clrObject = (CLRObject)ManagedType.GetManagedObject(tuple.Handle); | ||
| 57 | + clrObject = (CLRObject)ManagedType.GetManagedObject(tuple); | ||
| 58 | 58 | Assert.AreSame(encoder0, clrObject.inst); | |
| 59 | 59 | } | |
| 60 | 60 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -171,7 +171,7 @@ public int XProp | |||
| 171 | 171 | { | |
| 172 | 172 | return scope.Eval<int>($"super(this.__class__, this).{nameof(XProp)}"); | |
| 173 | 173 | } | |
| 174 | - catch (PythonException ex) when (ex.Type.Handle == Exceptions.AttributeError) | ||
| 174 | + catch (PythonException ex) when (PythonReferenceComparer.Instance.Equals(ex.Type, Exceptions.AttributeError)) | ||
| 175 | 175 | { | |
| 176 | 176 | if (this.extras.TryGetValue(nameof(this.XProp), out object value)) | |
| 177 | 177 | return (int)value; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -39,15 +39,9 @@ public void MoveToPyObject_SetsNull() | |||
| 39 | 39 | public void CanBorrowFromNewReference() | |
| 40 | 40 | { | |
| 41 | 41 | var dict = new PyDict(); | |
| 42 | - NewReference reference = Runtime.PyDict_Items(dict.Reference); | ||
| 43 | - try | ||
| 44 | - { | ||
| 45 | - PythonException.ThrowIfIsNotZero(Runtime.PyList_Reverse(reference)); | ||
| 46 | - } | ||
| 47 | - finally | ||
| 48 | - { | ||
| 49 | - reference.Dispose(); | ||
| 50 | - } | ||
| 42 | + using NewReference reference = Runtime.PyDict_Items(dict.Reference); | ||
| 43 | + BorrowedReference borrowed = reference.BorrowOrThrow(); | ||
| 44 | + PythonException.ThrowIfIsNotZero(Runtime.PyList_Reverse(borrowed)); | ||
| 51 | 45 | } | |
| 52 | 46 | } | |
| 53 | 47 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -23,11 +23,11 @@ public static void GetManagedStringTwice() | |||
| 23 | 23 | { | |
| 24 | 24 | const string expected = "FooBar"; | |
| 25 | 25 | ||
| 26 | - IntPtr op = Runtime.Runtime.PyString_FromString(expected); | ||
| 27 | - string s1 = Runtime.Runtime.GetManagedString(op); | ||
| 28 | - string s2 = Runtime.Runtime.GetManagedString(op); | ||
| 26 | + using var op = Runtime.Runtime.PyString_FromString(expected); | ||
| 27 | + string s1 = Runtime.Runtime.GetManagedString(op.BorrowOrThrow()); | ||
| 28 | + string s2 = Runtime.Runtime.GetManagedString(op.Borrow()); | ||
| 29 | 29 | ||
| 30 | - Assert.AreEqual(1, Runtime.Runtime.Refcount(op)); | ||
| 30 | + Assert.AreEqual(1, Runtime.Runtime.Refcount(op.Borrow())); | ||
| 31 | 31 | Assert.AreEqual(expected, s1); | |
| 32 | 32 | Assert.AreEqual(expected, s2); | |
| 33 | 33 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -107,7 +107,7 @@ from Python.EmbeddingTest.Domain import MyClass | |||
| 107 | 107 | { | |
| 108 | 108 | Debug.Assert(obj.AsManagedObject(type).GetType() == type); | |
| 109 | 109 | // We only needs its Python handle | |
| 110 | - PyRuntime.XIncref(obj.Handle); | ||
| 110 | + PyRuntime.XIncref(obj); | ||
| 111 | 111 | return obj.Handle; | |
| 112 | 112 | } | |
| 113 | 113 | } | |
@@ -127,16 +127,16 @@ public override ValueType Execute(ValueType arg) | |||
| 127 | 127 | { | |
| 128 | 128 | // handle refering a clr object created in previous domain, | |
| 129 | 129 | // it should had been deserialized and became callable agian. | |
| 130 | - IntPtr handle = (IntPtr)arg; | ||
| 130 | + using var handle = NewReference.DangerousFromPointer((IntPtr)arg); | ||
| 131 | 131 | try | |
| 132 | 132 | { | |
| 133 | 133 | using (Py.GIL()) | |
| 134 | 134 | { | |
| 135 | - IntPtr tp = Runtime.Runtime.PyObject_TYPE(handle); | ||
| 136 | - IntPtr tp_clear = Marshal.ReadIntPtr(tp, TypeOffset.tp_clear); | ||
| 135 | + BorrowedReference tp = Runtime.Runtime.PyObject_TYPE(handle.Borrow()); | ||
| 136 | + IntPtr tp_clear = Util.ReadIntPtr(tp, TypeOffset.tp_clear); | ||
| 137 | 137 | Assert.That(tp_clear, Is.Not.Null); | |
| 138 | 138 | ||
| 139 | - using (PyObject obj = new PyObject(handle)) | ||
| 139 | + using (PyObject obj = new PyObject(handle.Steal())) | ||
| 140 | 140 | { | |
| 141 | 141 | obj.InvokeMethod("Method"); | |
| 142 | 142 | obj.InvokeMethod("StaticMethod"); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -212,7 +212,7 @@ public void ValidateRefCount() | |||
| 212 | 212 | Assert.AreEqual(ptr, e.Handle); | |
| 213 | 213 | Assert.AreEqual(2, e.ImpactedObjects.Count); | |
| 214 | 214 | // Fix for this test, don't do this on general environment | |
| 215 | - Runtime.Runtime.XIncref(e.Handle); | ||
| 215 | + Runtime.Runtime.XIncref(e.Reference); | ||
| 216 | 216 | return false; | |
| 217 | 217 | }; | |
| 218 | 218 | Finalizer.Instance.IncorrectRefCntResolver += handler; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -86,7 +86,6 @@ public void TestCtorSByte() | |||
| 86 | 86 | public void TestCtorPyObject() | |
| 87 | 87 | { | |
| 88 | 88 | var i = new PyInt(5); | |
| 89 | - Runtime.Runtime.XIncref(i.Handle); | ||
| 90 | 89 | var a = new PyInt(i); | |
| 91 | 90 | Assert.AreEqual(5, a.ToInt32()); | |
| 92 | 91 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -98,7 +98,7 @@ public void GetAttrDefault_IgnoresAttributeErrorOnly() | |||
| 98 | 98 | ||
| 99 | 99 | public class PyObjectTestMethods | |
| 100 | 100 | { | |
| 101 | - public string RaisesAttributeError => throw new PythonException(new PyType(new BorrowedReference(Exceptions.AttributeError)), value: null, traceback: null); | ||
| 102 | - public string RaisesTypeError => throw new PythonException(new PyType(new BorrowedReference(Exceptions.TypeError)), value: null, traceback: null); | ||
| 101 | + public string RaisesAttributeError => throw new PythonException(new PyType(Exceptions.AttributeError), value: null, traceback: null); | ||
| 102 | + public string RaisesTypeError => throw new PythonException(new PyType(Exceptions.TypeError), value: null, traceback: null); | ||
| 103 | 103 | } | |
| 104 | 104 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -36,29 +36,31 @@ public static void Py_IsInitializedValue() | |||
| 36 | 36 | public static void RefCountTest() | |
| 37 | 37 | { | |
| 38 | 38 | Runtime.Runtime.Py_Initialize(); | |
| 39 | - IntPtr op = Runtime.Runtime.PyString_FromString("FooBar"); | ||
| 39 | + using var op = Runtime.Runtime.PyString_FromString("FooBar"); | ||
| 40 | 40 | ||
| 41 | 41 | // New object RefCount should be one | |
| 42 | - Assert.AreEqual(1, Runtime.Runtime.Refcount(op)); | ||
| 42 | + Assert.AreEqual(1, Runtime.Runtime.Refcount(op.BorrowOrThrow())); | ||
| 43 | 43 | ||
| 44 | 44 | // Checking refcount didn't change refcount | |
| 45 | - Assert.AreEqual(1, Runtime.Runtime.Refcount(op)); | ||
| 45 | + Assert.AreEqual(1, Runtime.Runtime.Refcount(op.Borrow())); | ||
| 46 | 46 | ||
| 47 | - // New reference doesn't increase refcount | ||
| 48 | - IntPtr p = op; | ||
| 47 | + // Borrowing a reference doesn't increase refcount | ||
| 48 | + BorrowedReference p = op.Borrow(); | ||
| 49 | 49 | Assert.AreEqual(1, Runtime.Runtime.Refcount(p)); | |
| 50 | 50 | ||
| 51 | 51 | // Py_IncRef/Py_DecRef increase and decrease RefCount | |
| 52 | - Runtime.Runtime.Py_IncRef(op); | ||
| 53 | - Assert.AreEqual(2, Runtime.Runtime.Refcount(op)); | ||
| 54 | - Runtime.Runtime.Py_DecRef(op); | ||
| 55 | - Assert.AreEqual(1, Runtime.Runtime.Refcount(op)); | ||
| 52 | + Runtime.Runtime.Py_IncRef(op.Borrow()); | ||
| 53 | + Assert.AreEqual(2, Runtime.Runtime.Refcount(p)); | ||
| 54 | + Runtime.Runtime.Py_DecRef(StolenReference.DangerousFromPointer(op.DangerousGetAddress())); | ||
| 55 | + Assert.AreEqual(1, Runtime.Runtime.Refcount(p)); | ||
| 56 | 56 | ||
| 57 | 57 | // XIncref/XDecref increase and decrease RefCount | |
| 58 | - Runtime.Runtime.XIncref(op); | ||
| 59 | - Assert.AreEqual(2, Runtime.Runtime.Refcount(op)); | ||
| 60 | - Runtime.Runtime.XDecref(op); | ||
| 61 | - Assert.AreEqual(1, Runtime.Runtime.Refcount(op)); | ||
| 58 | + Runtime.Runtime.XIncref(p); | ||
| 59 | + Assert.AreEqual(2, Runtime.Runtime.Refcount(p)); | ||
| 60 | + Runtime.Runtime.XDecref(p); | ||
| 61 | + Assert.AreEqual(1, Runtime.Runtime.Refcount(p)); | ||
| 62 | + | ||
| 63 | + op.Dispose(); | ||
| 62 | 64 | ||
| 63 | 65 | Runtime.Runtime.Py_Finalize(); | |
| 64 | 66 | } | |
@@ -71,22 +73,23 @@ public static void PyCheck_Iter_PyObject_IsIterable_Test() | |||
| 71 | 73 | Runtime.Native.ABI.Initialize(Runtime.Runtime.PyVersion); | |
| 72 | 74 | ||
| 73 | 75 | // Tests that a python list is an iterable, but not an iterator | |
| 74 | - using (var pyList = NewReference.DangerousFromPointer(Runtime.Runtime.PyList_New(0))) | ||
| 76 | + using (var pyListNew = Runtime.Runtime.PyList_New(0)) | ||
| 75 | 77 | { | |
| 78 | + BorrowedReference pyList = pyListNew.BorrowOrThrow(); | ||
| 76 | 79 | Assert.IsFalse(Runtime.Runtime.PyIter_Check(pyList)); | |
| 77 | 80 | Assert.IsTrue(Runtime.Runtime.PyObject_IsIterable(pyList)); | |
| 78 | 81 | ||
| 79 | 82 | // Tests that a python list iterator is both an iterable and an iterator | |
| 80 | 83 | using var pyListIter = Runtime.Runtime.PyObject_GetIter(pyList); | |
| 81 | - Assert.IsTrue(Runtime.Runtime.PyObject_IsIterable(pyListIter)); | ||
| 82 | - Assert.IsTrue(Runtime.Runtime.PyIter_Check(pyListIter)); | ||
| 84 | + Assert.IsTrue(Runtime.Runtime.PyObject_IsIterable(pyListIter.BorrowOrThrow())); | ||
| 85 | + Assert.IsTrue(Runtime.Runtime.PyIter_Check(pyListIter.Borrow())); | ||
| 83 | 86 | } | |
| 84 | 87 | ||
| 85 | 88 | // Tests that a python float is neither an iterable nor an iterator | |
| 86 | - using (var pyFloat = NewReference.DangerousFromPointer(Runtime.Runtime.PyFloat_FromDouble(2.73))) | ||
| 89 | + using (var pyFloat = Runtime.Runtime.PyFloat_FromDouble(2.73)) | ||
| 87 | 90 | { | |
| 88 | - Assert.IsFalse(Runtime.Runtime.PyObject_IsIterable(pyFloat)); | ||
| 89 | - Assert.IsFalse(Runtime.Runtime.PyIter_Check(pyFloat)); | ||
| 91 | + Assert.IsFalse(Runtime.Runtime.PyObject_IsIterable(pyFloat.BorrowOrThrow())); | ||
| 92 | + Assert.IsFalse(Runtime.Runtime.PyIter_Check(pyFloat.Borrow())); | ||
| 90 | 93 | } | |
| 91 | 94 | ||
| 92 | 95 | Runtime.Runtime.Py_Finalize(); | |
@@ -104,19 +107,17 @@ public static void PyCheck_Iter_PyObject_IsIterable_ThreadingLock_Test() | |||
| 104 | 107 | // Create an instance of threading.Lock, which is one of the very few types that does not have the | |
| 105 | 108 | // TypeFlags.HaveIter set in Python 2. This tests a different code path in PyObject_IsIterable and PyIter_Check. | |
| 106 | 109 | using var threading = Runtime.Runtime.PyImport_ImportModule("threading"); | |
| 107 | - Exceptions.ErrorCheck(threading); | ||
| 108 | - var threadingDict = Runtime.Runtime.PyModule_GetDict(threading); | ||
| 110 | + BorrowedReference threadingDict = Runtime.Runtime.PyModule_GetDict(threading.BorrowOrThrow()); | ||
| 109 | 111 | Exceptions.ErrorCheck(threadingDict); | |
| 110 | - var lockType = Runtime.Runtime.PyDict_GetItemString(threadingDict, "Lock"); | ||
| 112 | + BorrowedReference lockType = Runtime.Runtime.PyDict_GetItemString(threadingDict, "Lock"); | ||
| 111 | 113 | if (lockType.IsNull) | |
| 112 | 114 | throw PythonException.ThrowLastAsClrException(); | |
| 113 | 115 | ||
| 114 | - using var args = NewReference.DangerousFromPointer(Runtime.Runtime.PyTuple_New(0)); | ||
| 115 | - using var lockInstance = Runtime.Runtime.PyObject_CallObject(lockType, args); | ||
| 116 | - Exceptions.ErrorCheck(lockInstance); | ||
| 116 | + using var args = Runtime.Runtime.PyTuple_New(0); | ||
| 117 | + using var lockInstance = Runtime.Runtime.PyObject_CallObject(lockType, args.Borrow()); | ||
| 117 | 118 | ||
| 118 | - Assert.IsFalse(Runtime.Runtime.PyObject_IsIterable(lockInstance)); | ||
| 119 | - Assert.IsFalse(Runtime.Runtime.PyIter_Check(lockInstance)); | ||
| 119 | + Assert.IsFalse(Runtime.Runtime.PyObject_IsIterable(lockInstance.BorrowOrThrow())); | ||
| 120 | + Assert.IsFalse(Runtime.Runtime.PyIter_Check(lockInstance.Borrow())); | ||
| 120 | 121 | } | |
| 121 | 122 | finally | |
| 122 | 123 | { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -181,12 +181,14 @@ internal static void SetArgsAndCause(BorrowedReference ob, Exception e) | |||
| 181 | 181 | /// Shortcut for (pointer == NULL) -> throw PythonException | |
| 182 | 182 | /// </summary> | |
| 183 | 183 | /// <param name="pointer">Pointer to a Python object</param> | |
| 184 | - internal static void ErrorCheck(BorrowedReference pointer) | ||
| 184 | + internal static BorrowedReference ErrorCheck(BorrowedReference pointer) | ||
| 185 | 185 | { | |
| 186 | 186 | if (pointer.IsNull) | |
| 187 | 187 | { | |
| 188 | 188 | throw PythonException.ThrowLastAsClrException(); | |
| 189 | 189 | } | |
| 190 | + | ||
| 191 | + return pointer; | ||
| 190 | 192 | } | |
| 191 | 193 | ||
| 192 | 194 | internal static void ErrorCheck(IntPtr pointer) => ErrorCheck(new BorrowedReference(pointer)); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments