diff options
author | Benjamin Peterson <benjamin@python.org> | 2014-06-08 20:52:33 -0700 |
---|---|---|
committer | Benjamin Peterson <benjamin@python.org> | 2014-06-08 20:52:33 -0700 |
commit | e69f24b859d0a7867fd5939ed0cc2ec667a693b4 (patch) | |
tree | b5f1af9ba7950113ce0cc3019c897531c47d0b75 | |
parent | 48c724c9dcafbf5d36ec9beb3afeedad49814669 (diff) | |
download | six-e69f24b859d0a7867fd5939ed0cc2ec667a693b4.tar.gz |
remove other instances of the six meta path importer if six is reloaded (fixes #71)
-rw-r--r-- | CHANGES | 6 | ||||
-rw-r--r-- | six.py | 17 |
2 files changed, 21 insertions, 2 deletions
@@ -3,6 +3,12 @@ Changelog for six This file lists the changes in each six version. +1.7.1 +----- + +- Issue #71: Make the six.moves meta path importer handle reloading of the six + module gracefully. + 1.7.0 ----- @@ -731,12 +731,25 @@ def add_metaclass(metaclass): # Complete the moves implementation. # This code is at the end of this module to speed up module loading. -# 1. turn this module into a package. +# Turn this module into a package. __path__ = [] # required for PEP 302 and PEP 451 __package__ = __name__ # see PEP 366 @ReservedAssignment try: __spec__.submodule_search_locations = [] # PEP 451 @UndefinedVariable except NameError: pass -# 2. add the importer to the meta path import hook. +# Remove other six meta path importers, since they cause problems. This can +# happen if six is removed from sys.modules and then reloaded. (Setuptools does +# this for some reason.) +for i, importer in enumerate(sys.meta_path): + # Here's some real nastiness: Another "instance" of the six module might be + # floating around. Therefore, we can't use isinstance() to check for the six + # meta path importer, since the other six instance will have inserted an + # importer with different class. + if (type(importer).__name__ == "_SixMetaPathImporter" and + importer.name == __name__): + del sys.meta_path[i] + break +del i, importer +# Finally, add the importer to the meta path import hook. sys.meta_path.append(_importer) |