[issue9250] sys.modules changes size during iteration in regrtest module

Xavier de Gaye report at bugs.python.org
Tue Jul 13 18:36:13 CEST 2010


New submission from Xavier de Gaye <xdegaye at gmail.com>:

Python 2.7 - svn revision 82852

Bug description
===============

Test script foo.py
------------------

    #!/usr/bin/env python
    import distutils.core
    import test.regrtest


Exception when running foo.py
-----------------------------

Traceback (most recent call last):
  File "foo.py", line 3, in <module>
    import test.regrtest
  File "/usr/local/lib/python2.7/test/regrtest.py", line 184, in <module>
    for module in sys.modules.itervalues():
RuntimeError: dictionary changed size during iteration


Workaround
==========

Revese the order of the imports in foo.py.


Proposed patch
==============

Root cause: email modules are being imported inside the loop.

The following patch (svn revision 82852) forces the import of the
email modules before the main iteration. These modules have been
lazily imported in email/__init__.py and need to be effectively
imported before updating the __file__ and __path__ attributes.

The patch is also attached to this report.

--- release27-maint/Lib/test/regrtest.py       2010-07-13 18:11:32.000000000 +0200
+++ /usr/local/lib/python2.7/test/regrtest.py   2010-07-13 18:00:52.000000000 +0200
@@ -181,6 +181,12 @@
 # (site.py absolutize them), the __file__ and __path__ will be absolute too.
 # Therefore it is necessary to absolutize manually the __file__ and __path__ of
 # the packages to prevent later imports to fail when the CWD is different.
+
+# Email modules are imported lazily, force their import first.
+if 'email' in sys.modules:
+    [hasattr(sys.modules[name], '__foo__') for name in list(sys.modules)
+                                                if name.startswith('email.')]
+
 for module in sys.modules.itervalues():
     if hasattr(module, '__path__'):
         module.__path__ = [os.path.abspath(path) for path in module.__path__]

----------
components: Tests
files: sys.modules_regrtest.patch
keywords: patch
messages: 110211
nosy: xdegaye
priority: normal
severity: normal
status: open
title: sys.modules changes size during iteration in regrtest module
type: crash
versions: Python 2.7
Added file: http://bugs.python.org/file17988/sys.modules_regrtest.patch

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue9250>
_______________________________________


More information about the Python-bugs-list mailing list