Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6fa2004

Browse files
committed
switched tests to match the new reference changes
1 parent c4909d4 commit 6fa2004

12 files changed

+51
-71
lines changed

src/embed_tests/CodecGroups.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ public void Encodes()
4949
};
5050

5151
var uri = group.TryEncode(new Uri("data:"));
52-
var clrObject = (CLRObject)ManagedType.GetManagedObject(uri.Handle);
52+
var clrObject = (CLRObject)ManagedType.GetManagedObject(uri);
5353
Assert.AreSame(encoder1, clrObject.inst);
5454
Assert.AreNotSame(encoder2, clrObject.inst);
5555

5656
var tuple = group.TryEncode(Tuple.Create(1));
57-
clrObject = (CLRObject)ManagedType.GetManagedObject(tuple.Handle);
57+
clrObject = (CLRObject)ManagedType.GetManagedObject(tuple);
5858
Assert.AreSame(encoder0, clrObject.inst);
5959
}
6060

src/embed_tests/Inheritance.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ public int XProp
171171
{
172172
return scope.Eval<int>($"super(this.__class__, this).{nameof(XProp)}");
173173
}
174-
catch (PythonException ex) when (ex.Type.Handle == Exceptions.AttributeError)
174+
catch (PythonException ex) when (PythonReferenceComparer.Instance.Equals(ex.Type, Exceptions.AttributeError))
175175
{
176176
if (this.extras.TryGetValue(nameof(this.XProp), out object value))
177177
return (int)value;

src/embed_tests/References.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,9 @@ public void MoveToPyObject_SetsNull()
3939
public void CanBorrowFromNewReference()
4040
{
4141
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));
5145
}
5246
}
5347
}

src/embed_tests/TestCustomMarshal.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ public static void GetManagedStringTwice()
2323
{
2424
const string expected = "FooBar";
2525

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());
2929

30-
Assert.AreEqual(1, Runtime.Runtime.Refcount(op));
30+
Assert.AreEqual(1, Runtime.Runtime.Refcount(op.Borrow()));
3131
Assert.AreEqual(expected, s1);
3232
Assert.AreEqual(expected, s2);
3333
}

src/embed_tests/TestDomainReload.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ from Python.EmbeddingTest.Domain import MyClass
107107
{
108108
Debug.Assert(obj.AsManagedObject(type).GetType() == type);
109109
// We only needs its Python handle
110-
PyRuntime.XIncref(obj.Handle);
110+
PyRuntime.XIncref(obj);
111111
return obj.Handle;
112112
}
113113
}
@@ -127,16 +127,16 @@ public override ValueType Execute(ValueType arg)
127127
{
128128
// handle refering a clr object created in previous domain,
129129
// it should had been deserialized and became callable agian.
130-
IntPtr handle = (IntPtr)arg;
130+
using var handle = NewReference.DangerousFromPointer((IntPtr)arg);
131131
try
132132
{
133133
using (Py.GIL())
134134
{
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);
137137
Assert.That(tp_clear, Is.Not.Null);
138138

139-
using (PyObject obj = new PyObject(handle))
139+
using (PyObject obj = new PyObject(handle.Steal()))
140140
{
141141
obj.InvokeMethod("Method");
142142
obj.InvokeMethod("StaticMethod");

src/embed_tests/TestFinalizer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ public void ValidateRefCount()
212212
Assert.AreEqual(ptr, e.Handle);
213213
Assert.AreEqual(2, e.ImpactedObjects.Count);
214214
// Fix for this test, don't do this on general environment
215-
Runtime.Runtime.XIncref(e.Handle);
215+
Runtime.Runtime.XIncref(e.Reference);
216216
return false;
217217
};
218218
Finalizer.Instance.IncorrectRefCntResolver += handler;

src/embed_tests/TestPyInt.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ public void TestCtorSByte()
8686
public void TestCtorPyObject()
8787
{
8888
var i = new PyInt(5);
89-
Runtime.Runtime.XIncref(i.Handle);
9089
var a = new PyInt(i);
9190
Assert.AreEqual(5, a.ToInt32());
9291
}

src/embed_tests/TestPyObject.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public void GetAttrDefault_IgnoresAttributeErrorOnly()
9898

9999
public class PyObjectTestMethods
100100
{
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);
103103
}
104104
}

src/embed_tests/TestRuntime.cs

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -36,29 +36,31 @@ public static void Py_IsInitializedValue()
3636
public static void RefCountTest()
3737
{
3838
Runtime.Runtime.Py_Initialize();
39-
IntPtr op = Runtime.Runtime.PyString_FromString("FooBar");
39+
using var op = Runtime.Runtime.PyString_FromString("FooBar");
4040

4141
// New object RefCount should be one
42-
Assert.AreEqual(1, Runtime.Runtime.Refcount(op));
42+
Assert.AreEqual(1, Runtime.Runtime.Refcount(op.BorrowOrThrow()));
4343

4444
// Checking refcount didn't change refcount
45-
Assert.AreEqual(1, Runtime.Runtime.Refcount(op));
45+
Assert.AreEqual(1, Runtime.Runtime.Refcount(op.Borrow()));
4646

47-
// New reference doesn't increase refcount
48-
IntPtr p = op;
47+
// Borrowing a reference doesn't increase refcount
48+
BorrowedReference p = op.Borrow();
4949
Assert.AreEqual(1, Runtime.Runtime.Refcount(p));
5050

5151
// 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));
5656

5757
// 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();
6264

6365
Runtime.Runtime.Py_Finalize();
6466
}
@@ -71,22 +73,23 @@ public static void PyCheck_Iter_PyObject_IsIterable_Test()
7173
Runtime.Native.ABI.Initialize(Runtime.Runtime.PyVersion);
7274

7375
// 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))
7577
{
78+
BorrowedReference pyList = pyListNew.BorrowOrThrow();
7679
Assert.IsFalse(Runtime.Runtime.PyIter_Check(pyList));
7780
Assert.IsTrue(Runtime.Runtime.PyObject_IsIterable(pyList));
7881

7982
// Tests that a python list iterator is both an iterable and an iterator
8083
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()));
8386
}
8487

8588
// 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))
8790
{
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()));
9093
}
9194

9295
Runtime.Runtime.Py_Finalize();
@@ -104,19 +107,17 @@ public static void PyCheck_Iter_PyObject_IsIterable_ThreadingLock_Test()
104107
// Create an instance of threading.Lock, which is one of the very few types that does not have the
105108
// TypeFlags.HaveIter set in Python 2. This tests a different code path in PyObject_IsIterable and PyIter_Check.
106109
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());
109111
Exceptions.ErrorCheck(threadingDict);
110-
var lockType = Runtime.Runtime.PyDict_GetItemString(threadingDict, "Lock");
112+
BorrowedReference lockType = Runtime.Runtime.PyDict_GetItemString(threadingDict, "Lock");
111113
if (lockType.IsNull)
112114
throw PythonException.ThrowLastAsClrException();
113115

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());
117118

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()));
120121
}
121122
finally
122123
{

src/runtime/exceptions.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,14 @@ internal static void SetArgsAndCause(BorrowedReference ob, Exception e)
181181
/// Shortcut for (pointer == NULL) -&gt; throw PythonException
182182
/// </summary>
183183
/// <param name="pointer">Pointer to a Python object</param>
184-
internal static void ErrorCheck(BorrowedReference pointer)
184+
internal static BorrowedReference ErrorCheck(BorrowedReference pointer)
185185
{
186186
if (pointer.IsNull)
187187
{
188188
throw PythonException.ThrowLastAsClrException();
189189
}
190+
191+
return pointer;
190192
}
191193

192194
internal static void ErrorCheck(IntPtr pointer) => ErrorCheck(new BorrowedReference(pointer));

src/runtime/finalizer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public IncorrectFinalizeArgs(IntPtr handle, IReadOnlyCollection<IntPtr> imacted)
5757
ImpactedObjects = imacted;
5858
}
5959
public IntPtr Handle { get; }
60+
public BorrowedReference Reference => new(Handle);
6061
public IReadOnlyCollection<IntPtr> ImpactedObjects { get; }
6162
}
6263

src/runtime/pythonexception.cs

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -424,24 +424,7 @@ internal static void ThrowIfIsNull(in NewReference ob)
424424
}
425425
}
426426
internal static BorrowedReference ThrowIfIsNull(BorrowedReference ob)
427-
{
428-
if (ob == null)
429-
{
430-
throw ThrowLastAsClrException();
431-
}
432-
433-
return ob;
434-
}
435-
436-
internal static IntPtr ThrowIfIsNull(IntPtr ob)
437-
{
438-
if (ob == IntPtr.Zero)
439-
{
440-
throw ThrowLastAsClrException();
441-
}
442-
443-
return ob;
444-
}
427+
=> Exceptions.ErrorCheck(ob);
445428

446429
internal static void ThrowIfIsNotZero(int value)
447430
{

0 commit comments

Comments
 (0)