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

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Only set mp_subscript and mp_ass_subscript for indexable types
  • Loading branch information
danabr committed Oct 6, 2020
commit 12fdaab6a8f62123a253acd4ca2e173a9b2e096e
22 changes: 22 additions & 0 deletions src/runtime/typemanager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,28 @@ internal static IntPtr CreateType(ManagedType impl, Type clrType)
Marshal.WriteIntPtr(type, TypeOffset.tp_iter, IntPtr.Zero);
}


// Only set mp_subscript and mp_ass_subscript for types with indexers
if (impl is ClassBase cb)
{
if (!(impl is ArrayObject))
{
if (cb.indexer == null || !cb.indexer.CanGet)
{
Marshal.WriteIntPtr(type, TypeOffset.mp_subscript, IntPtr.Zero);
}
if (cb.indexer == null || !cb.indexer.CanSet)
{
Marshal.WriteIntPtr(type, TypeOffset.mp_ass_subscript, IntPtr.Zero);
}
}
}
else
{
Marshal.WriteIntPtr(type, TypeOffset.mp_subscript, IntPtr.Zero);
Marshal.WriteIntPtr(type, TypeOffset.mp_ass_subscript, IntPtr.Zero);
}

if (base_ != IntPtr.Zero)
{
Marshal.WriteIntPtr(type, TypeOffset.tp_base, base_);
Expand Down
12 changes: 5 additions & 7 deletions src/tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -1321,16 +1321,14 @@ def test_array_abuse():
with pytest.raises(TypeError):
Test.PublicArrayTest.__getitem__(0, 0)

with pytest.raises(TypeError):
with pytest.raises(AttributeError):
Test.PublicArrayTest.__setitem__(0, 0, 0)

with pytest.raises(TypeError):
desc = Test.PublicArrayTest.__dict__['__getitem__']
desc(0, 0)
with pytest.raises(KeyError):
Test.PublicArrayTest.__dict__['__getitem__']

with pytest.raises(TypeError):
desc = Test.PublicArrayTest.__dict__['__setitem__']
desc(0, 0, 0)
with pytest.raises(KeyError):
Test.PublicArrayTest.__dict__['__setitem__']


def test_iterator_to_array():
Expand Down
15 changes: 13 additions & 2 deletions src/tests/test_indexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def test_internal_indexer():
with pytest.raises(TypeError):
Test.InternalIndexerTest.__getitem__(ob, 0)

with pytest.raises(TypeError):
with pytest.raises(AttributeError):
ob.__getitem__(0)


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

with pytest.raises(TypeError):
with pytest.raises(AttributeError):
ob.__getitem__(0)


Expand Down Expand Up @@ -603,3 +603,14 @@ def test_indexer_accessed_through_interface():
d = IDictionary[str, str](Dictionary[str, str]())
d["one"] = "1"
assert d["one"] == "1"


def test_using_indexer_on_object_without_indexer():
"""Test using subscript syntax on an object an without indexer raises"""
from System import Object
o = Object()
with pytest.raises(TypeError):
o[0]

with pytest.raises(TypeError):
o[0] = 1