Skip to content

utils.py: explicitly convert module keys to a list

Matthew Pitkin requested to merge matthew-pitkin/bilby:loadedmodules into master

For some reason when I locally run my test suite for CWInPy it fails when using the loaded_modules_dict function in utils.py. The error given is:

cwinpy/hierarchical.py:1428: in sample
    self._result = bilby.run_sampler(
../../miniconda3/envs/cwinpy/lib/python3.8/site-packages/bilby/core/sampler/__init__.py:145: in run_sampler
    meta_data["loaded_modules"] = loaded_modules_dict()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

    def loaded_modules_dict():
        module_names = sys.modules.keys()
        vdict = {}
        print(module_names)
>       for key in module_names:
E       RuntimeError: dictionary changed size during iteration

../../miniconda3/envs/cwinpy/lib/python3.8/site-packages/bilby/core/utils.py:1277: RuntimeError

so it's complaining about a dictionary size changing during the loop.

I seem to be able to fix this by explicitly setting:

module_names = list(sys.modules.keys())

I'm not sure why I'm seeing the error, but this fix seems to be otherwise harmless.

Merge request reports