forked from pythonnet/pythonnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpysequence.cs
More file actions
152 lines (130 loc) · 4.73 KB
/
pysequence.cs
File metadata and controls
152 lines (130 loc) · 4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#nullable enable
using System;
namespace Python.Runtime
{
/// <summary>
/// Represents a generic Python sequence. The methods of this class are
/// equivalent to the Python "abstract sequence API". See
/// PY2: https://docs.python.org/2/c-api/sequence.html
/// PY3: https://docs.python.org/3/c-api/sequence.html
/// for details.
/// </summary>
public class PySequence : PyIterable
{
internal PySequence(BorrowedReference reference) : base(reference) { }
internal PySequence(in StolenReference reference) : base(reference) { }
/// <summary>
/// Creates new instance from an existing object.
/// </summary>
/// <exception cref="ArgumentException"><paramref name="o"/> does not provide sequence protocol</exception>
public PySequence(PyObject o) : base(FromObject(o)) { }
static BorrowedReference FromObject(PyObject o)
{
if (o is null) throw new ArgumentNullException(nameof(o));
if (!IsSequenceType(o)) throw new ArgumentException("object is not a sequence");
return o.Reference;
}
/// <summary>
/// Returns <c>true</c> if the given object implements the sequence protocol.
/// </summary>
public static bool IsSequenceType(PyObject value)
{
if (value is null) throw new ArgumentNullException(nameof(value));
return Runtime.PySequence_Check(value.obj);
}
/// <summary>
/// Return the slice of the sequence with the given indices.
/// </summary>
public PyObject GetSlice(int i1, int i2)
{
IntPtr op = Runtime.PySequence_GetSlice(obj, i1, i2);
if (op == IntPtr.Zero)
{
throw PythonException.ThrowLastAsClrException();
}
return new PyObject(op);
}
/// <summary>
/// Sets the slice of the sequence with the given indices.
/// </summary>
public void SetSlice(int i1, int i2, PyObject v)
{
if (v is null) throw new ArgumentNullException(nameof(v));
int r = Runtime.PySequence_SetSlice(obj, i1, i2, v.obj);
if (r < 0)
{
throw PythonException.ThrowLastAsClrException();
}
}
/// <summary>
/// DelSlice Method
/// </summary>
/// <remarks>
/// Deletes the slice of the sequence with the given indices.
/// </remarks>
public void DelSlice(int i1, int i2)
{
int r = Runtime.PySequence_DelSlice(obj, i1, i2);
if (r < 0)
{
throw PythonException.ThrowLastAsClrException();
}
}
/// <summary>
/// Return the index of the given item in the sequence, or -1 if
/// the item does not appear in the sequence.
/// </summary>
public int Index(PyObject item)
{
if (item is null) throw new ArgumentNullException(nameof(item));
int r = Runtime.PySequence_Index(obj, item.obj);
if (r < 0)
{
Runtime.PyErr_Clear();
return -1;
}
return r;
}
/// <summary>
/// Return true if the sequence contains the given item. This method
/// throws a PythonException if an error occurs during the check.
/// </summary>
public bool Contains(PyObject item)
{
if (item is null) throw new ArgumentNullException(nameof(item));
int r = Runtime.PySequence_Contains(obj, item.obj);
if (r < 0)
{
throw PythonException.ThrowLastAsClrException();
}
return r != 0;
}
/// <summary>
/// Return the concatenation of the sequence object with the passed in
/// sequence object.
/// </summary>
public PyObject Concat(PyObject other)
{
if (other is null) throw new ArgumentNullException(nameof(other));
IntPtr op = Runtime.PySequence_Concat(obj, other.obj);
if (op == IntPtr.Zero)
{
throw PythonException.ThrowLastAsClrException();
}
return new PyObject(op);
}
/// <summary>
/// Return the sequence object repeated N times. This is equivalent
/// to the Python expression "object * count".
/// </summary>
public PyObject Repeat(int count)
{
IntPtr op = Runtime.PySequence_Repeat(obj, count);
if (op == IntPtr.Zero)
{
throw PythonException.ThrowLastAsClrException();
}
return new PyObject(op);
}
}
}