Skip to content
This repository was archived by the owner on Jan 23, 2024. It is now read-only.

Commit e01407f

Browse files
xinghuadou-googleXinghua Dou
authored andcommitted
Call list() on dict.items() in Python 3 to avoid 'dict changed size during
iteration'. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=191133041
1 parent 9b0c8d1 commit e01407f

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/googleclouddebugger/capture_collector.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import copy
2020
import datetime
2121
import inspect
22+
import itertools
2223
import logging
2324
import os
2425
import re
@@ -566,7 +567,14 @@ def CaptureVariableInternal(self, value, depth, limits, can_enqueue=True):
566567
return {'value': r}
567568

568569
# Add an additional depth for the object itself
569-
members = self.CaptureVariablesList(value.__dict__.items(), depth + 2,
570+
items = value.__dict__.items()
571+
if six.PY3:
572+
# Make a list of the iterator in Python 3, to avoid 'dict changed size
573+
# during iteration' errors from GC happening in the middle.
574+
# Only limits.max_list_items + 1 items are copied, anything past that will
575+
# get ignored by CaptureVariablesList().
576+
items = list(itertools.islice(items, limits.max_list_items + 1))
577+
members = self.CaptureVariablesList(items, depth + 2,
570578
OBJECT_HAS_NO_FIELDS, limits)
571579
v = {'members': members}
572580

0 commit comments

Comments
 (0)