@@ -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 {
0 commit comments