This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author kernc
Recipients kernc
Date 2017-02-22.16:22:51
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
If any of the objects in sys.modules is a module-like object that performs some additional imports in its __getattribute__ (as appropriate) handler, the following simple unit test test case:

    import unittest
    import warnings

    ... # Ensure one of the imported modules is a module-like object as above

    class Case(unittest.TestCase):
        def test_assertWarns(self):
            with self.assertWarns(UserWarning):
                warnings.warn('Some warning')

fails with:

    ERROR: test_assertWarns (example.Case)
    Traceback (most recent call last):
      File "/tmp/", line 9, in test_assertWarns
        with self.assertWarns(UserWarning):
      File "/usr/lib/python3.4/unittest/", line 205, in __enter__
        for v in sys.modules.values():
    RuntimeError: dictionary changed size during iteration

The problem is in the iteration over sys.modules in and accessing every module's __warningregistry__ attribute. On this access, the module-like objects may perform arbitrary actions including importing of further modules which extends sys.modules.

The simple proposed fix with no foreseen side-effects is to wrap sys.modules.values() call as a tuple().
Date User Action Args
2017-02-22 16:22:51kerncsetrecipients: + kernc
2017-02-22 16:22:51kerncsetmessageid: <>
2017-02-22 16:22:51kernclinkissue29620 messages
2017-02-22 16:22:51kernccreate