@@ -18,7 +18,7 @@ public bool CanEncode(Type type)
1818 && type . Name . StartsWith ( typeof ( TTuple ) . Name + '`' ) ;
1919 }
2020
21- public PyObject TryEncode ( object value )
21+ public PyObject ? TryEncode ( object value )
2222 {
2323 if ( value == null ) return null ;
2424
@@ -27,45 +27,45 @@ public PyObject TryEncode(object value)
2727 if ( ! this . CanEncode ( tupleType ) ) return null ;
2828 if ( tupleType == typeof ( TTuple ) ) return new PyTuple ( ) ;
2929
30- long fieldCount = tupleType . GetGenericArguments ( ) . Length ;
31- var tuple = Runtime . PyTuple_New ( fieldCount ) ;
32- Exceptions . ErrorCheck ( tuple ) ;
30+ nint fieldCount = tupleType . GetGenericArguments ( ) . Length ;
31+ using var tuple = Runtime . PyTuple_New ( fieldCount ) ;
32+ PythonException . ThrowIfIsNull ( tuple ) ;
3333 int fieldIndex = 0 ;
3434 foreach ( FieldInfo field in tupleType . GetFields ( ) )
3535 {
3636 var item = field . GetValue ( value ) ;
37- IntPtr pyItem = Converter . ToPython ( item , field . FieldType ) ;
38- Runtime . PyTuple_SetItem ( tuple , fieldIndex , pyItem ) ;
37+ using var pyItem = Converter . ToPython ( item , field . FieldType ) ;
38+ Runtime . PyTuple_SetItem ( tuple . Borrow ( ) , fieldIndex , pyItem . Steal ( ) ) ;
3939 fieldIndex ++ ;
4040 }
41- return new PyTuple ( StolenReference . DangerousFromPointer ( tuple ) ) ;
41+ return new PyTuple ( tuple . Steal ( ) ) ;
4242 }
4343
4444 public bool CanDecode ( PyType objectType , Type targetType )
45- => objectType . Handle == Runtime . PyTupleType && this . CanEncode ( targetType ) ;
45+ => objectType == Runtime . PyTupleType && this . CanEncode ( targetType ) ;
4646
47- public bool TryDecode < T > ( PyObject pyObj , out T value )
47+ public bool TryDecode < T > ( PyObject pyObj , out T ? value )
4848 {
4949 if ( pyObj == null ) throw new ArgumentNullException ( nameof ( pyObj ) ) ;
5050
5151 value = default ;
5252
53- if ( ! Runtime . PyTuple_Check ( pyObj . Handle ) ) return false ;
53+ if ( ! Runtime . PyTuple_Check ( pyObj ) ) return false ;
5454
5555 if ( typeof ( T ) == typeof ( object ) )
5656 {
57- bool converted = Decode ( pyObj , out object result ) ;
57+ bool converted = Decode ( pyObj , out object ? result ) ;
5858 if ( converted )
5959 {
60- value = ( T ) result ;
60+ value = ( T ? ) result ;
6161 return true ;
6262 }
6363
6464 return false ;
6565 }
6666
6767 var itemTypes = typeof ( T ) . GetGenericArguments ( ) ;
68- long itemCount = Runtime . PyTuple_Size ( pyObj . Handle ) ;
68+ nint itemCount = Runtime . PyTuple_Size ( pyObj ) ;
6969 if ( itemTypes . Length != itemCount ) return false ;
7070
7171 if ( itemCount == 0 )
@@ -74,10 +74,10 @@ public bool TryDecode<T>(PyObject pyObj, out T value)
7474 return true ;
7575 }
7676
77- var elements = new object [ itemCount ] ;
77+ var elements = new object ? [ itemCount ] ;
7878 for ( int itemIndex = 0 ; itemIndex < itemTypes . Length ; itemIndex ++ )
7979 {
80- IntPtr pyItem = Runtime . PyTuple_GetItem ( pyObj . Handle , itemIndex ) ;
80+ BorrowedReference pyItem = Runtime . PyTuple_GetItem ( pyObj , itemIndex ) ;
8181 if ( ! Converter . ToManaged ( pyItem , itemTypes [ itemIndex ] , out elements [ itemIndex ] , setError : false ) )
8282 {
8383 Exceptions . Clear ( ) ;
@@ -89,20 +89,20 @@ public bool TryDecode<T>(PyObject pyObj, out T value)
8989 return true ;
9090 }
9191
92- static bool Decode ( PyObject tuple , out object value )
92+ static bool Decode ( PyObject tuple , out object ? value )
9393 {
94- long itemCount = Runtime . PyTuple_Size ( tuple . Handle ) ;
94+ long itemCount = Runtime . PyTuple_Size ( tuple ) ;
9595 if ( itemCount == 0 )
9696 {
9797 value = EmptyTuple ;
9898 return true ;
9999 }
100- var elements = new object [ itemCount ] ;
100+ var elements = new object ? [ itemCount ] ;
101101 var itemTypes = new Type [ itemCount ] ;
102102 value = null ;
103103 for ( int itemIndex = 0 ; itemIndex < elements . Length ; itemIndex ++ )
104104 {
105- var pyItem = Runtime . PyTuple_GetItem ( tuple . Handle , itemIndex ) ;
105+ var pyItem = Runtime . PyTuple_GetItem ( tuple , itemIndex ) ;
106106 if ( ! Converter . ToManaged ( pyItem , typeof ( object ) , out elements [ itemIndex ] , setError : false ) )
107107 {
108108 Exceptions . Clear ( ) ;
0 commit comments