Iterate over copy of sys.modules keys in Python2/3

Iterate over a copy of sys.modules keys in both Python 2.x and
Python 3.x.  In Python 3.x, keys() is not a copy, and therefore
items can't be popped from it while iterating.

Change-Id: I98c3d7695bbfe3a6a4f23990af45a07dc147f22f
Closes-Bug: #1463503
This commit is contained in:
Corey Bryant
2015-06-09 13:42:53 -04:00
parent c0046d7d01
commit d99c56fa53

View File

@@ -179,7 +179,7 @@ class DisableModuleFixture(fixtures.Fixture):
def clear_module(self):
cleared_modules = {}
for fullname in sys.modules.keys():
for fullname in list(sys.modules):
if (fullname == self.module or
fullname.startswith(self.module + '.')):
cleared_modules[fullname] = sys.modules.pop(fullname)