[otConverters] must also implement _LazyList.__radd__
We can't set, e.g. `__radd__ = NotImplemented` as it's not a callable. NotImplemented is what is returned from a rich comparison method when self doesn't know how to compare with the other object. _LazyList object may also occur on the right hand side of a `+` operator, when the left operand is a list. Also in this case we want to first unpack the _LazyList and return a new list containing elements from both.
This commit is contained in:
parent
6ef48bd26f
commit
1aef1683df
@ -70,15 +70,18 @@ def buildConverters(tableSpec, tableNamespace):
|
|||||||
class _MissingItem(tuple):
|
class _MissingItem(tuple):
|
||||||
__slots__ = ()
|
__slots__ = ()
|
||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from collections import UserList
|
from collections import UserList
|
||||||
except:
|
except ImportError:
|
||||||
from UserList import UserList
|
from UserList import UserList
|
||||||
|
|
||||||
|
|
||||||
class _LazyList(UserList):
|
class _LazyList(UserList):
|
||||||
|
|
||||||
def __getslice__(self, i, j):
|
def __getslice__(self, i, j):
|
||||||
return self.__getitem__(slice(i, j))
|
return self.__getitem__(slice(i, j))
|
||||||
|
|
||||||
def __getitem__(self, k):
|
def __getitem__(self, k):
|
||||||
if isinstance(k, slice):
|
if isinstance(k, slice):
|
||||||
indices = range(*k.indices(len(self)))
|
indices = range(*k.indices(len(self)))
|
||||||
@ -89,13 +92,21 @@ class _LazyList(UserList):
|
|||||||
item = self.conv.read(self.reader, self.font, {})
|
item = self.conv.read(self.reader, self.font, {})
|
||||||
self.data[k] = item
|
self.data[k] = item
|
||||||
return item
|
return item
|
||||||
|
|
||||||
def __add__(self, other):
|
def __add__(self, other):
|
||||||
if isinstance(other, UserList):
|
if isinstance(other, _LazyList):
|
||||||
other = other[:]
|
other = list(other)
|
||||||
return self[:] + other
|
elif isinstance(other, list):
|
||||||
# Not needed currently
|
pass
|
||||||
__radd__ = NotImplemented
|
else:
|
||||||
__iadd__ = NotImplemented
|
return NotImplemented
|
||||||
|
return list(self) + other
|
||||||
|
|
||||||
|
def __radd__(self, other):
|
||||||
|
if not isinstance(other, list):
|
||||||
|
return NotImplemented
|
||||||
|
return other + list(self)
|
||||||
|
|
||||||
|
|
||||||
class BaseConverter(object):
|
class BaseConverter(object):
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user