From e201f89a77ef3798c03f3c22853bf88121d264df Mon Sep 17 00:00:00 2001 From: Laurens Van Houtven <_@lvh.cc> Date: Thu, 20 Jun 2013 20:09:46 +0200 Subject: [PATCH 1/6] Remove double reduce import --- jedi/_compatibility.py | 7 ------- 1 file changed, 7 deletions(-) diff --git a/jedi/_compatibility.py b/jedi/_compatibility.py index e0252a7a..6a411b0b 100644 --- a/jedi/_compatibility.py +++ b/jedi/_compatibility.py @@ -157,13 +157,6 @@ class Python3Method(object): else: return lambda *args, **kwargs: self.func(obj, *args, **kwargs) -try: - # the python3 way - from functools import reduce -except ImportError: - reduce = reduce - - def use_metaclass(meta, *bases): """ Create a class with a metaclass. """ if not bases: From 68f9cdd884da1c0b47c1ebf9422cae78faf0e1b2 Mon Sep 17 00:00:00 2001 From: Laurens Van Houtven <_@lvh.cc> Date: Thu, 20 Jun 2013 20:08:36 +0200 Subject: [PATCH 2/6] Pick find_module implementation statically --- jedi/_compatibility.py | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/jedi/_compatibility.py b/jedi/_compatibility.py index 6a411b0b..72d6a4d1 100644 --- a/jedi/_compatibility.py +++ b/jedi/_compatibility.py @@ -56,18 +56,16 @@ def find_module_pre_py33(string, path=None): return (mod_info[0], mod_info[1], mod_info[2][2] == imp.PKG_DIRECTORY) -def find_module(string, path=None): - """Provides information about a module. +find_module = find_module_py33 if is_py33 else find_module_pre_py33 +find_module.__doc__ = """ +Provides information about a module. - This function isolates the differences in importing libraries introduced with - python 3.3 on; it gets a module name and optionally a path. It will return a - tuple containin an open file for the module (if not builtin), the filename - or the name of the module if it is a builtin one and a boolean indicating - if the module is contained in a package.""" - if is_py33: - return find_module_py33(string, path) - else: - return find_module_pre_py33(string, path) +This function isolates the differences in importing libraries introduced with +python 3.3 on; it gets a module name and optionally a path. It will return a +tuple containin an open file for the module (if not builtin), the filename +or the name of the module if it is a builtin one and a boolean indicating +if the module is contained in a package. +""" # next was defined in python 2.6, in python 3 obj.next won't be possible # anymore From 44b1390d8084d6fb83d7d610a2c85d36f5f64ce5 Mon Sep 17 00:00:00 2001 From: Laurens Van Houtven <_@lvh.cc> Date: Thu, 20 Jun 2013 21:35:56 +0200 Subject: [PATCH 3/6] Clean up find_module_pre_py33 --- jedi/_compatibility.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/jedi/_compatibility.py b/jedi/_compatibility.py index 72d6a4d1..9a3cb1d8 100644 --- a/jedi/_compatibility.py +++ b/jedi/_compatibility.py @@ -47,13 +47,9 @@ def find_module_py33(string, path=None): def find_module_pre_py33(string, path=None): - mod_info = None - if path is None: - mod_info = imp.find_module(string) - else: - mod_info = imp.find_module(string, path) - - return (mod_info[0], mod_info[1], mod_info[2][2] == imp.PKG_DIRECTORY) + module_file, module_path, description = imp.find_module(string, path) + module_type = description[2] + return module_file, module_path, module_type is imp.PKG_DIRECTORY find_module = find_module_py33 if is_py33 else find_module_pre_py33 From f171617766fee6aabd3d0dc6f7aea29b55eb58a9 Mon Sep 17 00:00:00 2001 From: Laurens Van Houtven <_@lvh.cc> Date: Thu, 20 Jun 2013 21:40:37 +0200 Subject: [PATCH 4/6] Raise a nicer exception instance instead of raising an exception class --- jedi/_compatibility.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jedi/_compatibility.py b/jedi/_compatibility.py index 9a3cb1d8..2e181e98 100644 --- a/jedi/_compatibility.py +++ b/jedi/_compatibility.py @@ -29,7 +29,7 @@ def find_module_py33(string, path=None): loader = importlib.find_loader(string) if loader is None: - raise ImportError + raise ImportError("Couldn't find a loader for {0}".format(string)) try: if (loader.is_package(string)): From 1b0822743cd4dae5b2ce7b0a8447ce11fc0247b3 Mon Sep 17 00:00:00 2001 From: Laurens Van Houtven <_@lvh.cc> Date: Thu, 20 Jun 2013 22:05:08 +0200 Subject: [PATCH 5/6] Clean up find_module_py33 loader finding --- jedi/_compatibility.py | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/jedi/_compatibility.py b/jedi/_compatibility.py index 2e181e98..cccc78b4 100644 --- a/jedi/_compatibility.py +++ b/jedi/_compatibility.py @@ -16,17 +16,10 @@ is_py33 = sys.hexversion >= 0x03030000 def find_module_py33(string, path=None): - mod_info = (None, None, None) - loader = None - if path is not None: - # Check for the module in the specidied path - loader = importlib.machinery.PathFinder.find_module(string, path) - else: - # Check for the module in sys.path - loader = importlib.machinery.PathFinder.find_module(string, sys.path) - if loader is None: - # Fallback to find builtins - loader = importlib.find_loader(string) + loader = importlib.machinery.PathFinder.find_module(string, path) + + if loader is None and path is None: # Fallback to find builtins + loader = importlib.find_loader(string) if loader is None: raise ImportError("Couldn't find a loader for {0}".format(string)) From fd87e8af2a391ae8b4a40f1c30f3f1ce20c65162 Mon Sep 17 00:00:00 2001 From: Laurens Van Houtven <_@lvh.cc> Date: Thu, 20 Jun 2013 22:36:16 +0200 Subject: [PATCH 6/6] Refactor the way module_path, module_file and is_package are computed to be a bit more legible --- jedi/_compatibility.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/jedi/_compatibility.py b/jedi/_compatibility.py index cccc78b4..fd33a60c 100644 --- a/jedi/_compatibility.py +++ b/jedi/_compatibility.py @@ -25,18 +25,18 @@ def find_module_py33(string, path=None): raise ImportError("Couldn't find a loader for {0}".format(string)) try: - if (loader.is_package(string)): - mod_info = (None, os.path.dirname(loader.path), True) + is_package = loader.is_package(string) + if is_package: + module_path = os.path.dirname(loader.path) + module_file = None else: - filename = loader.get_filename(string) - if filename and os.path.exists(filename): - mod_info = (open(filename, 'U'), filename, False) - else: - mod_info = (None, filename, False) + module_path = loader.get_filename(string) + module_file = open(module_path) except AttributeError: - mod_info = (None, loader.load_module(string).__name__, False) + module_path = loader.load_module(string).__name__ + module_file = None - return mod_info + return module_file, module_path, is_package def find_module_pre_py33(string, path=None):