1
0
forked from VimPlug/jedi

Created find_module helper to handle compatibility with python 3.3

Moved package checking logic in follow_str function

Created find_module compatibility helper method

Conditional implementation of load_module for python 3.3
This commit is contained in:
Aldo Stracquadanio
2013-03-22 13:39:07 +00:00
parent 6c998067e8
commit d481a7aae4
2 changed files with 56 additions and 7 deletions

View File

@@ -7,11 +7,52 @@ Most of the code here is necessary to support Python 2.5. Once this dependency
will be dropped, we'll get rid of most code. will be dropped, we'll get rid of most code.
""" """
import sys import sys
import imp
import os
import io
try:
import importlib
except:
pass
is_py3k = sys.hexversion >= 0x03000000 is_py3k = sys.hexversion >= 0x03000000
is_py33 = sys.hexversion >= 0x03030000
is_py25 = sys.hexversion < 0x02060000 is_py25 = sys.hexversion < 0x02060000
if is_py33:
def find_module(string, path=None):
importing = None
if path is not None:
importing = importlib.find_loader(string, path)
else:
importing = importlib.find_loader(string)
returning = (None, None, None)
try:
filename = importing.get_filename(string)
if filename and os.path.exists(filename):
returning = (open(filename, 'U'), filename, False)
else:
returning = (None, filename, False)
except AttributeError:
if importing is None:
returning = (None, None, False)
else:
returning = (None, importing.load_module(string).__name__, False)
return returning
else:
def find_module(string, path=None):
importing = None
if path is None:
importing = imp.find_module(string)
else:
importing = imp.find_module(string, path)
returning = (importing[0], importing[1], importing[2][2] == imp.PKG_DIRECTORY)
return returning
# next was defined in python 2.6, in python 3 obj.next won't be possible # next was defined in python 2.6, in python 3 obj.next won't be possible
# anymore # anymore
try: try:

View File

@@ -21,6 +21,7 @@ import imp
import sys import sys
import itertools import itertools
from jedi._compatibility import find_module
from jedi import modules from jedi import modules
from jedi import debug from jedi import debug
from jedi import parsing_representation as pr from jedi import parsing_representation as pr
@@ -238,20 +239,22 @@ class ImportPath(pr.Base):
global imports_processed global imports_processed
imports_processed += 1 imports_processed += 1
importing = None
if path is not None: if path is not None:
return imp.find_module(string, [path]) importing = find_module(string, [path])
else: else:
debug.dbg('search_module', string, self.file_path) debug.dbg('search_module', string, self.file_path)
# Override the sys.path. It works only good that way. # Override the sys.path. It works only good that way.
# Injecting the path directly into `find_module` did not work. # Injecting the path directly into `find_module` did not work.
sys.path, temp = sys_path_mod, sys.path sys.path, temp = sys_path_mod, sys.path
try: try:
i = imp.find_module(string) importing = find_module(string)
except ImportError: except ImportError:
sys.path = temp sys.path = temp
raise raise
sys.path = temp sys.path = temp
return i
return importing
if self.file_path: if self.file_path:
sys_path_mod = list(self.sys_path_with_modifications()) sys_path_mod = list(self.sys_path_with_modifications())
@@ -259,6 +262,9 @@ class ImportPath(pr.Base):
else: else:
sys_path_mod = list(modules.get_sys_path()) sys_path_mod = list(modules.get_sys_path())
def module_not_found():
raise ModuleNotFound('The module you searched has not been found')
current_namespace = (None, None, None) current_namespace = (None, None, None)
# now execute those paths # now execute those paths
rest = [] rest = []
@@ -277,12 +283,14 @@ class ImportPath(pr.Base):
if current_namespace[1]: if current_namespace[1]:
rest = self.import_path[i:] rest = self.import_path[i:]
else: else:
raise ModuleNotFound( module_not_found()
'The module you searched has not been found')
if current_namespace == (None, None, False):
module_not_found()
sys_path_mod.pop(0) # TODO why is this here? sys_path_mod.pop(0) # TODO why is this here?
path = current_namespace[1] path = current_namespace[1]
is_package_directory = current_namespace[2][2] == imp.PKG_DIRECTORY is_package_directory = current_namespace[2]
f = None f = None
if is_package_directory or current_namespace[0]: if is_package_directory or current_namespace[0]: