Permalink
Browse files

Merge pull request #187 from Astrac/python-3.3

Python 3.3
  • Loading branch information...
2 parents 6c99806 + 124595d commit 3c73a744c3d2b702e52be5621b7472ec2933d455 @davidhalter committed Mar 27, 2013
Showing with 83 additions and 24 deletions.
  1. +1 −0 .travis.yml
  2. +59 −1 jedi/_compatibility.py
  3. +3 −2 jedi/cache.py
  4. +16 −10 jedi/imports.py
  5. +3 −1 jedi/parsing_representation.py
  6. +0 −9 test/completion/std.py
  7. +1 −1 tox.ini
View
@@ -4,6 +4,7 @@ env:
- TOXENV=py26
- TOXENV=py27
- TOXENV=py32
+ - TOXENV=py33
install:
- pip install --quiet --use-mirrors tox
script:
View
@@ -7,11 +7,69 @@
will be dropped, we'll get rid of most code.
"""
import sys
+import imp
+import os
+try:
+ import importlib
+except:
+ pass
is_py3k = sys.hexversion >= 0x03000000
-
+is_py33 = sys.hexversion >= 0x03030000
is_py25 = sys.hexversion < 0x02060000
+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)
+
+ if loader is None:
+ raise ImportError
+
+ try:
+ if (loader.is_package(string)):
+ mod_info = (None, os.path.dirname(loader.path), True)
+ 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)
+ except AttributeError:
+ mod_info = (None, loader.load_module(string).__name__, False)
+
+ return mod_info
+
+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)
+
+def find_module(string, path=None):
+ """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)
+
# next was defined in python 2.6, in python 3 obj.next won't be possible
# anymore
try:
View
@@ -21,6 +21,7 @@
import time
import os
import sys
+import hashlib
try:
import cPickle as pickle
except:
@@ -221,7 +222,7 @@ def save_module(path, name, parser, pickling=True):
class _ModulePickling(object):
- version = 1
+ version = 2
"""
Version number (integer) for file system cache.
@@ -312,7 +313,7 @@ def delete_cache(self):
shutil.rmtree(self._cache_directory())
def _get_hashed_path(self, path):
- return self._get_path('%s.pkl' % hash(path))
+ return self._get_path('%s.pkl' % hashlib.md5(path.encode("utf-8")).hexdigest())
def _get_path(self, file):
dir = self._cache_directory()
View
@@ -5,9 +5,8 @@
filesystem. This can be quite tricky sometimes, because Python imports are not
always that simple.
-Currently the import process uses ``imp`` to find modules. In the future, it's
-a goal to use ``importlib`` for this purpose. There's a `pull request
-<https://github.com/davidhalter/jedi/pull/109>`_ for that.
+This module uses imp for python up to 3.2 and importlib for python 3.3 on; the
+correct implementation is delegated to _compatibility.
This module also supports import autocompletion, which means to complete
statements like ``from datetim`` (curser at the end would return ``datetime``).
@@ -17,10 +16,10 @@
import os
import pkgutil
-import imp
import sys
import itertools
+from jedi._compatibility import find_module
from jedi import modules
from jedi import debug
from jedi import parsing_representation as pr
@@ -238,27 +237,32 @@ def follow_str(ns_path, string):
global imports_processed
imports_processed += 1
+ importing = None
if path is not None:
- return imp.find_module(string, [path])
+ importing = find_module(string, [path])
else:
debug.dbg('search_module', string, self.file_path)
# Override the sys.path. It works only good that way.
# Injecting the path directly into `find_module` did not work.
sys.path, temp = sys_path_mod, sys.path
try:
- i = imp.find_module(string)
+ importing = find_module(string)
except ImportError:
sys.path = temp
raise
sys.path = temp
- return i
+
+ return importing
if self.file_path:
sys_path_mod = list(self.sys_path_with_modifications())
sys_path_mod.insert(0, self.file_path)
else:
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)
# now execute those paths
rest = []
@@ -277,12 +281,14 @@ def follow_str(ns_path, string):
if current_namespace[1]:
rest = self.import_path[i:]
else:
- raise ModuleNotFound(
- 'The module you searched has not been found')
+ module_not_found()
+
+ if current_namespace == (None, None, False):
+ module_not_found()
sys_path_mod.pop(0) # TODO why is this here?
path = current_namespace[1]
- is_package_directory = current_namespace[2][2] == imp.PKG_DIRECTORY
+ is_package_directory = current_namespace[2]
f = None
if is_package_directory or current_namespace[0]:
@@ -560,8 +560,10 @@ def parent(self):
@parent.setter
def parent(self, value):
self._parent = value
- if self.next:
+ try:
self.next.parent = value
+ except AttributeError:
+ return
def get_code(self, first_indent=False, indention=' '):
stmts = []
View
@@ -89,12 +89,3 @@ def huhu(db):
"""
#? sqlite3.Connection()
db
-
-# -----------------
-# various regression tests
-# -----------------
-
-#62
-import threading
-#? ['_Verbose', '_VERBOSE']
-threading._Verbose
View
@@ -1,5 +1,5 @@
[tox]
-envlist = py25, py26, py27, py32
+envlist = py25, py26, py27, py32, py33
[testenv]
deps =
pytest

0 comments on commit 3c73a74

Please sign in to comment.