1
1
using System ;
2
+ using System . Linq ;
2
3
3
4
namespace Python . Runtime
4
5
{
@@ -50,33 +51,32 @@ public PyTuple(PyObject o) : base(FromObject(o))
50
51
/// <remarks>
51
52
/// Creates a new empty PyTuple.
52
53
/// </remarks>
53
- public PyTuple ( ) : base ( NewEmtpy ( ) . Steal ( ) ) { }
54
+ public PyTuple ( ) : base ( NewEmtpy ( ) ) { }
54
55
55
- private static NewReference NewEmtpy ( )
56
+ private static StolenReference NewEmtpy ( )
56
57
{
57
- IntPtr ptr = Runtime . PyTuple_New ( 0 ) ;
58
- PythonException . ThrowIfIsNull ( ptr ) ;
59
- return NewReference . DangerousFromPointer ( ptr ) ;
58
+ var ptr = Runtime . PyTuple_New ( 0 ) ;
59
+ return ptr . StealOrThrow ( ) ;
60
60
}
61
61
62
- private static NewReference FromArray ( PyObject [ ] items )
62
+ private static StolenReference FromArray ( PyObject [ ] items )
63
63
{
64
64
if ( items is null ) throw new ArgumentNullException ( nameof ( items ) ) ;
65
+ if ( items . Any ( item => item is null ) )
66
+ throw new ArgumentException ( message : Util . UseNone , paramName : nameof ( items ) ) ;
65
67
66
68
int count = items . Length ;
67
- IntPtr val = Runtime . PyTuple_New ( count ) ;
69
+ var val = Runtime . PyTuple_New ( count ) ;
68
70
for ( var i = 0 ; i < count ; i ++ )
69
71
{
70
- IntPtr ptr = items [ i ] . obj ;
71
- Runtime . XIncref ( ptr ) ;
72
- int res = Runtime . PyTuple_SetItem ( val , i , ptr ) ;
72
+ int res = Runtime . PyTuple_SetItem ( val . Borrow ( ) , i , items [ i ] ) ;
73
73
if ( res != 0 )
74
74
{
75
- Runtime . Py_DecRef ( val ) ;
75
+ val . Dispose ( ) ;
76
76
throw PythonException . ThrowLastAsClrException ( ) ;
77
77
}
78
78
}
79
- return NewReference . DangerousFromPointer ( val ) ;
79
+ return val . Steal ( ) ;
80
80
}
81
81
82
82
/// <summary>
@@ -88,7 +88,7 @@ private static NewReference FromArray(PyObject[] items)
88
88
/// See caveats about PyTuple_SetItem:
89
89
/// https://www.coursehero.com/file/p4j2ogg/important-exceptions-to-this-rule-PyTupleSetItem-and-PyListSetItem-These/
90
90
/// </remarks>
91
- public PyTuple ( PyObject [ ] items ) : base ( FromArray ( items ) . Steal ( ) )
91
+ public PyTuple ( PyObject [ ] items ) : base ( FromArray ( items ) )
92
92
{
93
93
}
94
94
0 commit comments