🌐 AI搜索 & 代理 主页
Skip to content

Commit cb16d01

Browse files
committed
Take the GIL in sequence and list wrappers
1 parent 788c010 commit cb16d01

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

src/runtime/CollectionWrappers/ListWrapper.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ public T this[int index]
1414
{
1515
get
1616
{
17+
using var _ = Py.GIL();
1718
var item = Runtime.PyList_GetItem(pyObject, index);
1819
var pyItem = new PyObject(item);
1920
return pyItem.As<T>()!;
2021
}
2122
set
2223
{
24+
using var _ = Py.GIL();
2325
var pyItem = value.ToPython();
2426
var result = Runtime.PyList_SetItem(pyObject, index, new NewReference(pyItem).Steal());
2527
if (result == -1)
@@ -37,6 +39,7 @@ public void Insert(int index, T item)
3739
if (IsReadOnly)
3840
throw new InvalidOperationException("Collection is read-only");
3941

42+
using var _ = Py.GIL();
4043
var pyItem = item.ToPython();
4144

4245
int result = Runtime.PyList_Insert(pyObject, index, pyItem);

src/runtime/CollectionWrappers/SequenceWrapper.cs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@ public int Count
1414
{
1515
get
1616
{
17-
var size = Runtime.PySequence_Size(pyObject.Reference);
18-
if (size == -1)
17+
nint size = -1;
1918
{
20-
Runtime.CheckExceptionOccurred();
19+
using var _ = Py.GIL();
20+
size = Runtime.PySequence_Size(pyObject.Reference);
21+
if (size == -1)
22+
{
23+
Runtime.CheckExceptionOccurred();
24+
}
2125
}
2226

2327
return checked((int)size);
@@ -38,6 +42,7 @@ public void Clear()
3842
{
3943
if (IsReadOnly)
4044
throw new NotImplementedException();
45+
using var _ = Py.GIL();
4146
int result = Runtime.PySequence_DelSlice(pyObject, 0, Count);
4247
if (result == -1)
4348
{
@@ -77,12 +82,16 @@ protected bool removeAt(int index)
7782
if (index >= Count || index < 0)
7883
return false;
7984

80-
int result = Runtime.PySequence_DelItem(pyObject, index);
8185

82-
if (result == 0)
83-
return true;
86+
{
87+
using var _ = Py.GIL();
88+
int result = Runtime.PySequence_DelItem(pyObject, index);
89+
90+
if (result == 0)
91+
return true;
8492

85-
Runtime.CheckExceptionOccurred();
93+
Runtime.CheckExceptionOccurred();
94+
}
8695
return false;
8796
}
8897

tests/test_codec.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ def test_iterable():
5959
assert 3 == ob.GetLength2(iterable2)
6060

6161

62-
@pytest.mark.skip
6362
def test_sequence():
6463
Python.Runtime.Codecs.SequenceDecoder.Register()
6564
ob = ListConversionTester()
@@ -71,7 +70,6 @@ def test_sequence():
7170
assert 3 == ob.GetLength(tup2)
7271

7372

74-
@pytest.mark.skip
7573
def test_list():
7674
Python.Runtime.Codecs.SequenceDecoder.Register()
7775
ob = ListConversionTester()

0 commit comments

Comments
 (0)