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

Skip to content

Commit 2ac952a

Browse files
committed
switched all PyObject derived classes to the new style references
1 parent 0241b38 commit 2ac952a

File tree

10 files changed

+53
-123
lines changed

10 files changed

+53
-123
lines changed

src/runtime/pydict.cs

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,7 @@ internal PyDict(in StolenReference reference) : base(reference) { }
1616
/// <summary>
1717
/// Creates a new Python dictionary object.
1818
/// </summary>
19-
public PyDict() : base(Runtime.PyDict_New())
20-
{
21-
if (obj == IntPtr.Zero)
22-
{
23-
throw PythonException.ThrowLastAsClrException();
24-
}
25-
}
26-
19+
public PyDict() : base(Runtime.PyDict_New().StealOrThrow()) { }
2720

2821
/// <summary>
2922
/// Wraps existing dictionary object.
@@ -106,12 +99,8 @@ public PyIterable Keys()
10699
/// </remarks>
107100
public PyIterable Values()
108101
{
109-
IntPtr items = Runtime.PyDict_Values(obj);
110-
if (items == IntPtr.Zero)
111-
{
112-
throw PythonException.ThrowLastAsClrException();
113-
}
114-
return new PyIterable(items);
102+
using var items = Runtime.PyDict_Values(obj);
103+
return new PyIterable(items.StealOrThrow());
115104
}
116105

117106

src/runtime/pyfloat.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public PyFloat(PyObject o) : base(FromObject(o))
3333
/// <remarks>
3434
/// Creates a new Python float from a double value.
3535
/// </remarks>
36-
public PyFloat(double value) : base(FromDouble(value).Steal())
36+
public PyFloat(double value) : base(Runtime.PyFloat_FromDouble(value).StealOrThrow())
3737
{
3838
}
3939

@@ -48,13 +48,6 @@ private static BorrowedReference FromObject(PyObject o)
4848
return o.Reference;
4949
}
5050

51-
private static NewReference FromDouble(double value)
52-
{
53-
IntPtr val = Runtime.PyFloat_FromDouble(value);
54-
PythonException.ThrowIfIsNull(val);
55-
return NewReference.DangerousFromPointer(val);
56-
}
57-
5851
private static StolenReference FromString(string value)
5952
{
6053
if (value is null) throw new ArgumentNullException(nameof(value));

src/runtime/pyint.cs

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,13 @@ private static BorrowedReference FromObject(PyObject o)
4242
return o.Reference;
4343
}
4444

45-
private static NewReference FromInt(int value)
46-
{
47-
IntPtr val = Runtime.PyInt_FromInt32(value);
48-
PythonException.ThrowIfIsNull(val);
49-
return NewReference.DangerousFromPointer(val);
50-
}
51-
5245
/// <summary>
5346
/// PyInt Constructor
5447
/// </summary>
5548
/// <remarks>
5649
/// Creates a new Python int from an int32 value.
5750
/// </remarks>
58-
public PyInt(int value) : base(FromInt(value).Steal())
51+
public PyInt(int value) : base(Runtime.PyInt_FromInt32(value).StealOrThrow())
5952
{
6053
}
6154

@@ -66,7 +59,7 @@ public PyInt(int value) : base(FromInt(value).Steal())
6659
/// <remarks>
6760
/// Creates a new Python int from a uint32 value.
6861
/// </remarks>
69-
public PyInt(uint value) : base(FromLong(value))
62+
public PyInt(uint value) : this((long)value)
7063
{
7164
}
7265

@@ -77,32 +70,17 @@ public PyInt(uint value) : base(FromLong(value))
7770
/// <remarks>
7871
/// Creates a new Python int from an int64 value.
7972
/// </remarks>
80-
public PyInt(long value) : base(FromLong(value))
73+
public PyInt(long value) : base(Runtime.PyInt_FromInt64(value).StealOrThrow())
8174
{
8275
}
8376

84-
private static StolenReference FromLong(long value)
85-
{
86-
var val = Runtime.PyInt_FromInt64(value);
87-
PythonException.ThrowIfIsNull(val);
88-
return val.Steal();
89-
}
90-
9177
/// <summary>
9278
/// Creates a new Python int from a <see cref="UInt64"/> value.
9379
/// </summary>
94-
public PyInt(ulong value) : base(FromUInt64(value))
95-
{
96-
}
97-
98-
private static StolenReference FromUInt64(ulong value)
80+
public PyInt(ulong value) : base(Runtime.PyLong_FromUnsignedLongLong(value).StealOrThrow())
9981
{
100-
var val = Runtime.PyLong_FromUnsignedLongLong(value);
101-
PythonException.ThrowIfIsNull(val);
102-
return val.Steal();
10382
}
10483

105-
10684
/// <summary>
10785
/// PyInt Constructor
10886
/// </summary>
@@ -146,21 +124,13 @@ public PyInt(sbyte value) : this((int)value)
146124
{
147125
}
148126

149-
150-
private static StolenReference FromString(string value)
151-
{
152-
NewReference val = Runtime.PyLong_FromString(value, 0);
153-
PythonException.ThrowIfIsNull(val);
154-
return val.Steal();
155-
}
156-
157127
/// <summary>
158128
/// PyInt Constructor
159129
/// </summary>
160130
/// <remarks>
161131
/// Creates a new Python int from a string value.
162132
/// </remarks>
163-
public PyInt(string value) : base(FromString(value))
133+
public PyInt(string value) : base(Runtime.PyLong_FromString(value, 0).StealOrThrow())
164134
{
165135
}
166136

src/runtime/pyiter.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace Python.Runtime
1111
/// </summary>
1212
public class PyIter : PyObject, IEnumerator<PyObject>
1313
{
14-
private PyObject _current;
14+
private PyObject? _current;
1515

1616
/// <summary>
1717
/// PyIter Constructor
@@ -87,7 +87,7 @@ public void Reset()
8787
throw new NotSupportedException();
8888
}
8989

90-
public PyObject Current => _current;
91-
object System.Collections.IEnumerator.Current => _current;
90+
public PyObject Current => _current ?? throw new InvalidOperationException();
91+
object System.Collections.IEnumerator.Current => Current;
9292
}
9393
}

src/runtime/pyiterable.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@ namespace Python.Runtime
66
{
77
public class PyIterable : PyObject, IEnumerable<PyObject>
88
{
9-
internal PyIterable(IntPtr ptr) : base(ptr)
10-
{
11-
}
12-
139
internal PyIterable(BorrowedReference reference) : base(reference) { }
1410
internal PyIterable(in StolenReference reference) : base(reference) { }
1511

src/runtime/pylist.cs

Lines changed: 11 additions & 17 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
{
@@ -42,41 +43,34 @@ public PyList(PyObject o) : base(FromObject(o))
4243
/// <summary>
4344
/// Creates a new empty Python list object.
4445
/// </summary>
45-
public PyList() : base(NewEmtpy().Steal())
46+
public PyList() : base(Runtime.PyList_New(0).StealOrThrow())
4647
{
4748
}
4849

49-
private static NewReference NewEmtpy()
50-
{
51-
IntPtr ptr = Runtime.PyList_New(0);
52-
PythonException.ThrowIfIsNull(ptr);
53-
return NewReference.DangerousFromPointer(ptr);
54-
}
55-
56-
private static NewReference FromArray(PyObject[] items)
50+
private static StolenReference FromArray(PyObject[] items)
5751
{
5852
if (items is null) throw new ArgumentNullException(nameof(items));
53+
if (items.Any(item => item is null))
54+
throw new ArgumentException(message: Util.UseNone, paramName: nameof(items));
5955

6056
int count = items.Length;
61-
IntPtr val = Runtime.PyList_New(count);
57+
using var val = Runtime.PyList_New(count);
6258
for (var i = 0; i < count; i++)
6359
{
64-
IntPtr ptr = items[i].obj;
65-
Runtime.XIncref(ptr);
66-
int r = Runtime.PyList_SetItem(val, i, ptr);
60+
int r = Runtime.PyList_SetItem(val.Borrow(), i, new NewReference(items[i]).Steal());
6761
if (r < 0)
6862
{
69-
Runtime.Py_DecRef(val);
63+
val.Dispose();
7064
throw PythonException.ThrowLastAsClrException();
7165
}
7266
}
73-
return NewReference.DangerousFromPointer(val);
67+
return val.Steal();
7468
}
7569

7670
/// <summary>
7771
/// Creates a new Python list object from an array of objects.
7872
/// </summary>
79-
public PyList(PyObject[] items) : base(FromArray(items).Steal())
73+
public PyList(PyObject[] items) : base(FromArray(items))
8074
{
8175
}
8276

@@ -130,7 +124,7 @@ public void Insert(int index, PyObject item)
130124
{
131125
if (item is null) throw new ArgumentNullException(nameof(item));
132126

133-
int r = Runtime.PyList_Insert(this.Reference, index, item.obj);
127+
int r = Runtime.PyList_Insert(this, index, item);
134128
if (r < 0)
135129
{
136130
throw PythonException.ThrowLastAsClrException();

src/runtime/pysequence.cs

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,9 @@ public static bool IsSequenceType(PyObject value)
4242
/// </summary>
4343
public PyObject GetSlice(int i1, int i2)
4444
{
45-
IntPtr op = Runtime.PySequence_GetSlice(obj, i1, i2);
46-
if (op == IntPtr.Zero)
47-
{
48-
throw PythonException.ThrowLastAsClrException();
49-
}
50-
return new PyObject(op);
45+
using var op = Runtime.PySequence_GetSlice(obj, i1, i2);
46+
PythonException.ThrowIfIsNull(op);
47+
return op.MoveToPyObject();
5148
}
5249

5350

@@ -86,11 +83,11 @@ public void DelSlice(int i1, int i2)
8683
/// Return the index of the given item in the sequence, or -1 if
8784
/// the item does not appear in the sequence.
8885
/// </summary>
89-
public int Index(PyObject item)
86+
public nint Index(PyObject item)
9087
{
9188
if (item is null) throw new ArgumentNullException(nameof(item));
9289

93-
int r = Runtime.PySequence_Index(obj, item.obj);
90+
nint r = Runtime.PySequence_Index(obj, item.obj);
9491
if (r < 0)
9592
{
9693
Runtime.PyErr_Clear();
@@ -99,6 +96,17 @@ public int Index(PyObject item)
9996
return r;
10097
}
10198

99+
/// <summary>
100+
/// Return the index of the given item in the sequence, or -1 if
101+
/// the item does not appear in the sequence.
102+
/// </summary>
103+
public int Index32(PyObject item) => checked((int)Index(item));
104+
/// <summary>
105+
/// Return the index of the given item in the sequence, or -1 if
106+
/// the item does not appear in the sequence.
107+
/// </summary>
108+
public long Index64(PyObject item) => Index(item);
109+
102110

103111
/// <summary>
104112
/// Return true if the sequence contains the given item. This method
@@ -125,12 +133,9 @@ public PyObject Concat(PyObject other)
125133
{
126134
if (other is null) throw new ArgumentNullException(nameof(other));
127135

128-
IntPtr op = Runtime.PySequence_Concat(obj, other.obj);
129-
if (op == IntPtr.Zero)
130-
{
131-
throw PythonException.ThrowLastAsClrException();
132-
}
133-
return new PyObject(op);
136+
using var op = Runtime.PySequence_Concat(obj, other.obj);
137+
PythonException.ThrowIfIsNull(op);
138+
return op.MoveToPyObject();
134139
}
135140

136141

@@ -140,12 +145,9 @@ public PyObject Concat(PyObject other)
140145
/// </summary>
141146
public PyObject Repeat(int count)
142147
{
143-
IntPtr op = Runtime.PySequence_Repeat(obj, count);
144-
if (op == IntPtr.Zero)
145-
{
146-
throw PythonException.ThrowLastAsClrException();
147-
}
148-
return new PyObject(op);
148+
using var op = Runtime.PySequence_Repeat(obj, count);
149+
PythonException.ThrowIfIsNull(op);
150+
return op.MoveToPyObject();
149151
}
150152
}
151153
}

src/runtime/pystring.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,13 @@ public PyString(PyObject o) : base(FromObject(o))
3939
{
4040
}
4141

42-
43-
private static NewReference FromString(string s)
44-
{
45-
IntPtr val = Runtime.PyString_FromString(s);
46-
PythonException.ThrowIfIsNull(val);
47-
return NewReference.DangerousFromPointer(val);
48-
}
4942
/// <summary>
5043
/// PyString Constructor
5144
/// </summary>
5245
/// <remarks>
5346
/// Creates a Python string from a managed string.
5447
/// </remarks>
55-
public PyString(string s) : base(FromString(s).Steal())
48+
public PyString(string s) : base(Runtime.PyString_FromString(s).StealOrThrow())
5649
{
5750
}
5851

src/runtime/pytuple.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ private static StolenReference FromArray(PyObject[] items)
6666
throw new ArgumentException(message: Util.UseNone, paramName: nameof(items));
6767

6868
int count = items.Length;
69-
var val = Runtime.PyTuple_New(count);
69+
using var val = Runtime.PyTuple_New(count);
7070
for (var i = 0; i < count; i++)
7171
{
7272
int res = Runtime.PyTuple_SetItem(val.Borrow(), i, items[i]);

src/runtime/runtime.cs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,15 +1307,15 @@ internal static bool PyFloat_Check(BorrowedReference ob)
13071307
internal static bool PySequence_Check(BorrowedReference pointer) => Delegates.PySequence_Check(pointer);
13081308

13091309
internal static NewReference PySequence_GetItem(BorrowedReference pointer, nint index) => Delegates.PySequence_GetItem(pointer, index);
1310-
private static int PySequence_SetItem(BorrowedReference pointer, nint index, BorrowedReference value) => Delegates.PySequence_SetItem(pointer, index, value);
1310+
internal static int PySequence_SetItem(BorrowedReference pointer, nint index, BorrowedReference value) => Delegates.PySequence_SetItem(pointer, index, value);
13111311

13121312
internal static int PySequence_DelItem(BorrowedReference pointer, nint index) => Delegates.PySequence_DelItem(pointer, index);
13131313

1314-
private static NewReference PySequence_GetSlice(BorrowedReference pointer, nint i1, nint i2) => Delegates.PySequence_GetSlice(pointer, i1, i2);
1314+
internal static NewReference PySequence_GetSlice(BorrowedReference pointer, nint i1, nint i2) => Delegates.PySequence_GetSlice(pointer, i1, i2);
13151315

13161316
internal static int PySequence_SetSlice(BorrowedReference pointer, nint i1, nint i2, BorrowedReference v) => Delegates.PySequence_SetSlice(pointer, i1, i2, v);
13171317

1318-
private static int PySequence_DelSlice(BorrowedReference pointer, nint i1, nint i2) => Delegates.PySequence_DelSlice(pointer, i1, i2);
1318+
internal static int PySequence_DelSlice(BorrowedReference pointer, nint i1, nint i2) => Delegates.PySequence_DelSlice(pointer, i1, i2);
13191319

13201320
internal static nint PySequence_Size(BorrowedReference pointer) => Delegates.PySequence_Size(pointer);
13211321

@@ -1534,20 +1534,13 @@ internal static bool PyList_Check(BorrowedReference ob)
15341534
return PyObject_TYPE(ob) == PyListType;
15351535
}
15361536

1537-
private static NewReference PyList_New(nint size) => Delegates.PyList_New(size);
1538-
1539-
1540-
internal static BorrowedReference PyList_GetItem(BorrowedReference pointer, long index)
1541-
{
1542-
return PyList_GetItem(pointer, new IntPtr(index));
1543-
}
1544-
1537+
internal static NewReference PyList_New(nint size) => Delegates.PyList_New(size);
15451538

15461539
private static BorrowedReference PyList_GetItem(BorrowedReference pointer, IntPtr index) => Delegates.PyList_GetItem(pointer, index);
15471540

1548-
private static int PyList_SetItem(BorrowedReference pointer, nint index, StolenReference value) => Delegates.PyList_SetItem(pointer, index, value);
1541+
internal static int PyList_SetItem(BorrowedReference pointer, nint index, StolenReference value) => Delegates.PyList_SetItem(pointer, index, value);
15491542

1550-
private static int PyList_Insert(BorrowedReference pointer, nint index, BorrowedReference value) => Delegates.PyList_Insert(pointer, index, value);
1543+
internal static int PyList_Insert(BorrowedReference pointer, nint index, BorrowedReference value) => Delegates.PyList_Insert(pointer, index, value);
15511544

15521545

15531546
internal static int PyList_Append(BorrowedReference pointer, BorrowedReference value) => Delegates.PyList_Append(pointer, value);

0 commit comments

Comments
 (0)