@@ -638,16 +638,20 @@ cdef class _ItemsIter:
638638 cdef _Impl _impl
639639 cdef int _current
640640 cdef int _len
641+ cdef unsigned long long _version
641642
642643 def __cinit__ (self , _Impl impl ):
643644 self ._impl = impl
644645 self ._current = 0
645- self ._len = len (self ._impl._items)
646+ self ._version = impl._version
647+ self ._len = len (impl._items)
646648
647649 def __iter__ (self ):
648650 return self
649651
650652 def __next__ (self ):
653+ if self ._version != self ._impl._version:
654+ raise RuntimeError (" Dictionary changed during iteration" )
651655 if self ._current == self ._len:
652656 raise StopIteration
653657 item = < _Pair> self ._impl._items[self ._current]
@@ -700,16 +704,20 @@ cdef class _ValuesIter:
700704 cdef _Impl _impl
701705 cdef int _current
702706 cdef int _len
707+ cdef unsigned long long _version
703708
704709 def __cinit__ (self , _Impl impl ):
705710 self ._impl = impl
706711 self ._current = 0
707- self ._len = len (self ._impl._items)
712+ self ._len = len (impl._items)
713+ self ._version = impl._version
708714
709715 def __iter__ (self ):
710716 return self
711717
712718 def __next__ (self ):
719+ if self ._version != self ._impl._version:
720+ raise RuntimeError (" Dictionary changed during iteration" )
713721 if self ._current == self ._len:
714722 raise StopIteration
715723 item = < _Pair> self ._impl._items[self ._current]
@@ -747,16 +755,20 @@ cdef class _KeysIter:
747755 cdef _Impl _impl
748756 cdef int _current
749757 cdef int _len
758+ cdef unsigned long long _version
750759
751760 def __cinit__ (self , _Impl impl ):
752761 self ._impl = impl
753762 self ._current = 0
754763 self ._len = len (self ._impl._items)
764+ self ._version = impl._version
755765
756766 def __iter__ (self ):
757767 return self
758768
759769 def __next__ (self ):
770+ if self ._version != self ._impl._version:
771+ raise RuntimeError (" Dictionary changed during iteration" )
760772 if self ._current == self ._len:
761773 raise StopIteration
762774 item = < _Pair> self ._impl._items[self ._current]
0 commit comments