From c97fb65a16dca879b9d215cb6ed1ffdc93c8ef20 Mon Sep 17 00:00:00 2001 From: Marc Abramowitz Date: Sat, 7 Sep 2013 10:19:05 -0700 Subject: Put six.moves modules in sys.modules so that they're importable (fixes #19) --- six.py | 2 ++ test_six.py | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/six.py b/six.py index 85898ec..f622c20 100644 --- a/six.py +++ b/six.py @@ -193,6 +193,8 @@ _moved_attributes = [ ] for attr in _moved_attributes: setattr(_MovedItems, attr.name, attr) + if isinstance(attr, MovedModule): + sys.modules[__name__ + ".moves." + attr.name] = attr del attr moves = sys.modules[__name__ + ".moves"] = _MovedItems(__name__ + ".moves") diff --git a/test_six.py b/test_six.py index 81d1776..b2ffe84 100644 --- a/test_six.py +++ b/test_six.py @@ -89,7 +89,9 @@ else: def test_move_items(item_name): """Ensure that everything loads correctly.""" try: - getattr(six.moves, item_name) + item = getattr(six.moves, item_name) + if isinstance(item, types.ModuleType): + __import__("six.moves." + item_name) except AttributeError: if item_name == "zip_longest" and sys.version_info < (2, 6): py.test.skip("zip_longest only available on 2.6+") -- cgit v1.2.1 From b1c37a84835a29375d721080b6b28990b5464c74 Mon Sep 17 00:00:00 2001 From: Marc Abramowitz Date: Sun, 8 Dec 2013 22:09:13 -0800 Subject: Add tests for `from six.moves.queue import Queue` and `from six.moves.configparser import ConfigParser` --- test_six.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test_six.py b/test_six.py index b4d1d0e..6ca57ed 100644 --- a/test_six.py +++ b/test_six.py @@ -142,6 +142,16 @@ def test_move_items_urllib_robotparser(item_name): getattr(six.moves.urllib.robotparser, item_name) +def test_from_six_moves_queue_import_Queue(): + from six.moves.queue import Queue + assert isinstance(Queue, types.ClassType) + + +def test_from_six_moves_configparser_import_ConfigParser(): + from six.moves.configparser import ConfigParser + assert isinstance(ConfigParser, types.ClassType) + + def test_filter(): from six.moves import filter f = filter(lambda x: x % 2, range(10)) -- cgit v1.2.1 From afe609858f6c616bb738713b054fc4e397ddc70a Mon Sep 17 00:00:00 2001 From: Marc Abramowitz Date: Sun, 8 Dec 2013 22:11:16 -0800 Subject: Add MovedModule.__getattr__; makes `from six.moves.queue import Queue` and such work. --- six.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/six.py b/six.py index 41733dd..b70ff75 100644 --- a/six.py +++ b/six.py @@ -104,6 +104,10 @@ class MovedModule(_LazyDescr): def _resolve(self): return _import_module(self.mod) + def __getattr__(self, attr): + _module = self._resolve() + return getattr(_module, attr) + class MovedAttribute(_LazyDescr): -- cgit v1.2.1 From f293ede1501e852dd16922eb33f6ede9d69edcc6 Mon Sep 17 00:00:00 2001 From: Marc Abramowitz Date: Sat, 4 Jan 2014 15:32:08 -0800 Subject: MovedModule.__getattr__: Set attribute on the lazy module, so __getattr__ isn't invoked multiple times. --- six.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/six.py b/six.py index 9a8a9a3..4e53304 100644 --- a/six.py +++ b/six.py @@ -106,7 +106,9 @@ class MovedModule(_LazyDescr): def __getattr__(self, attr): _module = self._resolve() - return getattr(_module, attr) + value = getattr(_module, attr) + setattr(self, attr, value) + return value class _LazyModule(types.ModuleType): -- cgit v1.2.1