Skip to content

Commit f808166

Browse files
danabrlostmsu
authored andcommitted
Only set mp_subscript and mp_ass_subscript for indexable types
1 parent 00c22a5 commit f808166

File tree

3 files changed

+40
-9
lines changed

3 files changed

+40
-9
lines changed

src/runtime/typemanager.cs

+22
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,28 @@ internal static IntPtr CreateType(ManagedType impl, Type clrType)
171171
Marshal.WriteIntPtr(type, TypeOffset.tp_iter, IntPtr.Zero);
172172
}
173173

174+
175+
// Only set mp_subscript and mp_ass_subscript for types with indexers
176+
if (impl is ClassBase cb)
177+
{
178+
if (!(impl is ArrayObject))
179+
{
180+
if (cb.indexer == null || !cb.indexer.CanGet)
181+
{
182+
Marshal.WriteIntPtr(type, TypeOffset.mp_subscript, IntPtr.Zero);
183+
}
184+
if (cb.indexer == null || !cb.indexer.CanSet)
185+
{
186+
Marshal.WriteIntPtr(type, TypeOffset.mp_ass_subscript, IntPtr.Zero);
187+
}
188+
}
189+
}
190+
else
191+
{
192+
Marshal.WriteIntPtr(type, TypeOffset.mp_subscript, IntPtr.Zero);
193+
Marshal.WriteIntPtr(type, TypeOffset.mp_ass_subscript, IntPtr.Zero);
194+
}
195+
174196
if (base_ != IntPtr.Zero)
175197
{
176198
Marshal.WriteIntPtr(type, TypeOffset.tp_base, base_);

src/tests/test_array.py

+5-7
Original file line numberDiff line numberDiff line change
@@ -1321,16 +1321,14 @@ def test_array_abuse():
13211321
with pytest.raises(TypeError):
13221322
Test.PublicArrayTest.__getitem__(0, 0)
13231323

1324-
with pytest.raises(TypeError):
1324+
with pytest.raises(AttributeError):
13251325
Test.PublicArrayTest.__setitem__(0, 0, 0)
13261326

1327-
with pytest.raises(TypeError):
1328-
desc = Test.PublicArrayTest.__dict__['__getitem__']
1329-
desc(0, 0)
1327+
with pytest.raises(KeyError):
1328+
Test.PublicArrayTest.__dict__['__getitem__']
13301329

1331-
with pytest.raises(TypeError):
1332-
desc = Test.PublicArrayTest.__dict__['__setitem__']
1333-
desc(0, 0, 0)
1330+
with pytest.raises(KeyError):
1331+
Test.PublicArrayTest.__dict__['__setitem__']
13341332

13351333

13361334
def test_iterator_to_array():

src/tests/test_indexer.py

+13-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def test_internal_indexer():
4242
with pytest.raises(TypeError):
4343
Test.InternalIndexerTest.__getitem__(ob, 0)
4444

45-
with pytest.raises(TypeError):
45+
with pytest.raises(AttributeError):
4646
ob.__getitem__(0)
4747

4848

@@ -56,7 +56,7 @@ def test_private_indexer():
5656
with pytest.raises(TypeError):
5757
Test.PrivateIndexerTest.__getitem__(ob, 0)
5858

59-
with pytest.raises(TypeError):
59+
with pytest.raises(AttributeError):
6060
ob.__getitem__(0)
6161

6262

@@ -603,3 +603,14 @@ def test_indexer_accessed_through_interface():
603603
d = IDictionary[str, str](Dictionary[str, str]())
604604
d["one"] = "1"
605605
assert d["one"] == "1"
606+
607+
608+
def test_using_indexer_on_object_without_indexer():
609+
"""Test using subscript syntax on an object an without indexer raises"""
610+
from System import Object
611+
o = Object()
612+
with pytest.raises(TypeError):
613+
o[0]
614+
615+
with pytest.raises(TypeError):
616+
o[0] = 1

0 commit comments

Comments
 (0)