Skip to content

Commit 3d23e5e

Browse files
committed
Fix bug with __index__ - failure seen on pypy3.8.
1 parent e83f3ca commit 3d23e5e

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

CHANGELOG.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ Changelog
1111
Python 2 specific handling will be removed at some point.
1212
* Linux wheels are now provided in `musllinux` and `manylinux2014` variants.
1313

14+
* Fixed ``__index__`` to fallback to ``int`` if the wrapped object doesn't have an ``__index__`` method.
15+
This prevents situations where code using a proxy would otherwise likely just call ``int`` had the object
16+
not have an ``__index__`` method.
17+
1418
1.6.0 (2021-03-22)
1519
------------------
1620

src/lazy_object_proxy/simple.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,13 @@ def __ror__(self, other):
233233
__float__ = make_proxy_method(float)
234234
__oct__ = make_proxy_method(oct)
235235
__hex__ = make_proxy_method(hex)
236-
__index__ = make_proxy_method(operator.index)
236+
237+
def __index__(self):
238+
if hasattr(self.__wrapped__, '__index__'):
239+
return operator.index(self.__wrapped__)
240+
else:
241+
return int(self.__wrapped__)
242+
237243
__len__ = make_proxy_method(len)
238244
__contains__ = make_proxy_method(operator.contains)
239245
__getitem__ = make_proxy_method(operator.getitem)

src/lazy_object_proxy/slots.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,10 @@ def __hex__(self):
392392
return hex(self.__wrapped__)
393393

394394
def __index__(self):
395-
return operator.index(self.__wrapped__)
395+
if hasattr(self.__wrapped__, '__index__'):
396+
return operator.index(self.__wrapped__)
397+
else:
398+
return int(self.__wrapped__)
396399

397400
def __len__(self):
398401
return len(self.__wrapped__)

0 commit comments

Comments
 (0)