Merge pull request #1851 from bagerard/fix_IndexError_iter_while_modif_list

fix BaseList.__iter__ operator + minor improvements
This commit is contained in:
erdenezul 2018-08-26 08:26:43 +08:00 committed by GitHub
commit de74273108
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 5 deletions

View File

@ -246,3 +246,4 @@ that much better:
* Renjianxin (https://github.com/Davidrjx)
* Erdenezul Batmunkh (https://github.com/erdenezul)
* Andy Yankovsky (https://github.com/werat)
* Bastien Gérard (https://github.com/bagerard)

View File

@ -128,8 +128,8 @@ class BaseList(list):
return value
def __iter__(self):
for i in six.moves.range(self.__len__()):
yield self[i]
for v in super(BaseList, self).__iter__():
yield v
def __setitem__(self, key, value, *args, **kwargs):
if isinstance(key, slice):
@ -138,7 +138,7 @@ class BaseList(list):
self._mark_as_changed(key)
return super(BaseList, self).__setitem__(key, value)
def __delitem__(self, key, *args, **kwargs):
def __delitem__(self, key):
self._mark_as_changed()
return super(BaseList, self).__delitem__(key)
@ -187,7 +187,7 @@ class BaseList(list):
self._mark_as_changed()
return super(BaseList, self).remove(*args, **kwargs)
def reverse(self, *args, **kwargs):
def reverse(self):
self._mark_as_changed()
return super(BaseList, self).reverse()

View File

@ -1,6 +1,21 @@
import unittest
from mongoengine.base.datastructures import StrictDict
from mongoengine.base.datastructures import StrictDict, BaseList
class TestBaseList(unittest.TestCase):
def test_iter_simple(self):
values = [True, False, True, False]
base_list = BaseList(values, instance=None, name='my_name')
self.assertEqual(values, list(base_list))
def test_iter_allow_modification_while_iterating_withou_error(self):
# regular list allows for this, thus this subclass must comply to that
base_list = BaseList([True, False, True, False], instance=None, name='my_name')
for idx, val in enumerate(base_list):
if val:
base_list.pop(idx)
class TestStrictDict(unittest.TestCase):