Skip to content

Handle the non-integer return of __index__ #97

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 1, 2019
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
Add tests for __index__ function
Add tests for __index__ in list,
tuple, string
  • Loading branch information
HyeockJinKim committed Sep 29, 2019
commit 313242b4d6dd99059ef2f6a73b606114555152d2
30 changes: 30 additions & 0 deletions py/tests/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,34 @@
assertRaises(TypeError, lambda: list.sort(s5, key=1))
assertRaises(TypeError, lambda: list.sort(1))

class Index:
def __index__(self):
return 1

a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
b = Index()
assert a[b] == 1
assert a[b:10] == a[1:10]
assert a[10:b:-1] == a[10:1:-1]

class NonIntegerIndex:
def __index__(self):
return 1.1

a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
b = NonIntegerIndex()
try:
a[b]
except TypeError:
pass
else:
assert False, "TypeError not raised"

try:
a[b:10]
except TypeError:
pass
else:
assert False, "TypeError not raised"

doc="finished"
31 changes: 31 additions & 0 deletions py/tests/string.py
Original file line number Diff line number Diff line change
Expand Up @@ -886,4 +886,35 @@ def index(s, i):
assert uni[7:7:2] == ''
assert uni[7:7:3] == ''

class Index:
def __index__(self):
return 1

a = '012345678910'
b = Index()
assert a[b] == '1'
assert a[b:10] == a[1:10]
assert a[10:b:-1] == a[10:1:-1]

class NonIntegerIndex:
def __index__(self):
return 1.1

a = '012345678910'
b = NonIntegerIndex()
try:
a[b]
except TypeError:
pass
else:
assert False, "TypeError not raised"

try:
a[b:10]
except TypeError:
pass
else:
assert False, "TypeError not raised"


doc="finished"
30 changes: 30 additions & 0 deletions py/tests/tuple.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,34 @@
assert a * 0 == ()
assert a * -1 == ()

class Index:
def __index__(self):
return 1

a = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
b = Index()
assert a[b] == 1
assert a[b:10] == a[1:10]
assert a[10:b:-1] == a[10:1:-1]

class NonIntegerIndex:
def __index__(self):
return 1.1

a = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
b = NonIntegerIndex()
try:
a[b]
except TypeError:
pass
else:
assert False, "TypeError not raised"

try:
a[b:10]
except TypeError:
pass
else:
assert False, "TypeError not raised"

doc="finished"