-
Notifications
You must be signed in to change notification settings - Fork 774
Expand file tree
/
Copy pathListWrapper.cs
More file actions
60 lines (51 loc) · 1.66 KB
/
ListWrapper.cs
File metadata and controls
60 lines (51 loc) · 1.66 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
using System;
using System.Collections.Generic;
namespace Python.Runtime.CollectionWrappers
{
internal class ListWrapper<T> : SequenceWrapper<T>, IList<T>
{
public ListWrapper(PyObject pyObj) : base(pyObj)
{
}
public T this[int index]
{
get
{
using var _ = Py.GIL();
var item = Runtime.PyList_GetItem(pyObject, index);
var pyItem = new PyObject(item);
return pyItem.As<T>()!;
}
set
{
using var _ = Py.GIL();
var pyItem = value.ToPython();
var result = Runtime.PyList_SetItem(pyObject, index, new NewReference(pyItem).Steal());
if (result == -1)
Runtime.CheckExceptionOccurred();
}
}
public int IndexOf(T item)
{
return indexOf(item);
}
public void Insert(int index, T item)
{
if (IsReadOnly)
throw new InvalidOperationException("Collection is read-only");
using var _ = Py.GIL();
var pyItem = item.ToPython();
int result = Runtime.PyList_Insert(pyObject, index, pyItem);
if (result == -1)
Runtime.CheckExceptionOccurred();
}
public void RemoveAt(int index)
{
var result = removeAt(index);
//PySequence_DelItem will set an error if it fails. throw it here
//since RemoveAt does not have a bool return value.
if (result == false)
Runtime.CheckExceptionOccurred();
}
}
}