summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenjamin Peterson <benjamin@python.org>2014-06-08 20:52:33 -0700
committerBenjamin Peterson <benjamin@python.org>2014-06-08 20:52:33 -0700
commite69f24b859d0a7867fd5939ed0cc2ec667a693b4 (patch)
treeb5f1af9ba7950113ce0cc3019c897531c47d0b75
parent48c724c9dcafbf5d36ec9beb3afeedad49814669 (diff)
downloadsix-e69f24b859d0a7867fd5939ed0cc2ec667a693b4.tar.gz
remove other instances of the six meta path importer if six is reloaded (fixes #71)
-rw-r--r--CHANGES6
-rw-r--r--six.py17
2 files changed, 21 insertions, 2 deletions
diff --git a/CHANGES b/CHANGES
index 7b026ac..b5fabfa 100644
--- a/CHANGES
+++ b/CHANGES
@@ -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
-----
diff --git a/six.py b/six.py
index 78e065a..416fee0 100644
--- a/six.py
+++ b/six.py
@@ -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)