Skip to content

Commit 1d80162

Browse files
committed
switched pytuple.cs to the new style references
1 parent 43a862a commit 1d80162

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

src/runtime/Util.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ internal static class Util
1919

2020
internal const string UseOverloadWithReferenceTypes =
2121
"This API is unsafe, and will be removed in the future. Use overloads working with *Reference types";
22+
internal const string UseNone =
23+
$"null is not supported in this context. Use {nameof(PyObject)}.{nameof(PyObject.None)}";
2224

2325
internal const string BadStr = "bad __str__";
2426

src/runtime/pytuple.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Linq;
23

34
namespace Python.Runtime
45
{
@@ -50,33 +51,32 @@ public PyTuple(PyObject o) : base(FromObject(o))
5051
/// <remarks>
5152
/// Creates a new empty PyTuple.
5253
/// </remarks>
53-
public PyTuple() : base(NewEmtpy().Steal()) { }
54+
public PyTuple() : base(NewEmtpy()) { }
5455

55-
private static NewReference NewEmtpy()
56+
private static StolenReference NewEmtpy()
5657
{
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();
6060
}
6161

62-
private static NewReference FromArray(PyObject[] items)
62+
private static StolenReference FromArray(PyObject[] items)
6363
{
6464
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));
6567

6668
int count = items.Length;
67-
IntPtr val = Runtime.PyTuple_New(count);
69+
var val = Runtime.PyTuple_New(count);
6870
for (var i = 0; i < count; i++)
6971
{
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]);
7373
if (res != 0)
7474
{
75-
Runtime.Py_DecRef(val);
75+
val.Dispose();
7676
throw PythonException.ThrowLastAsClrException();
7777
}
7878
}
79-
return NewReference.DangerousFromPointer(val);
79+
return val.Steal();
8080
}
8181

8282
/// <summary>
@@ -88,7 +88,7 @@ private static NewReference FromArray(PyObject[] items)
8888
/// See caveats about PyTuple_SetItem:
8989
/// https://www.coursehero.com/file/p4j2ogg/important-exceptions-to-this-rule-PyTupleSetItem-and-PyListSetItem-These/
9090
/// </remarks>
91-
public PyTuple(PyObject[] items) : base(FromArray(items).Steal())
91+
public PyTuple(PyObject[] items) : base(FromArray(items))
9292
{
9393
}
9494

0 commit comments

Comments
 (0)