Skip to content

[Py3.11] RuntimeError: dictionary changed size during iteration #569

@AdityaGupta030697

Description

@AdityaGupta030697

Hi team,

I'm getting this error intermittently:

Traceback (most recent call last):
  File <SNIP>
    with freezegun.freeze_time(lambda: datetime(today.year,
  File " <SNIP>/python3.11/site-packages/freezegun/api.py", line 633, in __enter__
    return self.start()
           ^^^^^^^^^^^^
  File "<SNIP>/python3.11/site-packages/freezegun/api.py", line 714, in start
    for mod_name, module in list(sys.modules.items()):
                            ^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: dictionary changed size during iteration

Unfortunately, I don't have a reproducer yet. Would something like this work here?

mod_names = list(sys.modules.keys())
for mod_name in mod_names:
    module =  sys.modules[mod_name]

    if mod_name is None or module is None or mod_name == __name__:
        continue
    elif mod_name.startswith(self.ignore) or mod_name.endswith('.six.moves'):
        continue
    elif (not hasattr(module, "__name__") or module.__name__ in ('datetime', 'time')):
        continue

    module_attrs = _get_cached_module_attributes(module)
    for attribute_name, attribute_value in module_attrs:
        fake = fakes.get(id(attribute_value))
        if fake:
            setattr(module, attribute_name, fake)
            add_change((module, attribute_name, attribute_value))

Please let me know if I'm missing something.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions