mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-07 22:44:27 +08:00
basic python 3 compatibility
This commit is contained in:
@@ -2,6 +2,8 @@
|
|||||||
This is a compatibility module, to make it possible to use jedi also with older
|
This is a compatibility module, to make it possible to use jedi also with older
|
||||||
python versions.
|
python versions.
|
||||||
"""
|
"""
|
||||||
|
import sys
|
||||||
|
|
||||||
# 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:
|
||||||
@@ -31,8 +33,6 @@ except ImportError:
|
|||||||
try:
|
try:
|
||||||
property.setter
|
property.setter
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
import sys
|
|
||||||
|
|
||||||
class property(property):
|
class property(property):
|
||||||
def __init__(self, fget, *args, **kwargs):
|
def __init__(self, fget, *args, **kwargs):
|
||||||
self.__doc__ = fget.__doc__
|
self.__doc__ = fget.__doc__
|
||||||
@@ -49,3 +49,11 @@ except AttributeError:
|
|||||||
return cls_ns[propname]
|
return cls_ns[propname]
|
||||||
else:
|
else:
|
||||||
property = property
|
property = property
|
||||||
|
|
||||||
|
# Borrowed from Ned Batchelder
|
||||||
|
if sys.hexversion > 0x03000000:
|
||||||
|
def exec_function(source, global_map):
|
||||||
|
exec(source, global_map)
|
||||||
|
else:
|
||||||
|
eval(compile("""def exec_function(source, global_map):
|
||||||
|
exec source in global_map """, 'blub', 'exec'))
|
||||||
|
|||||||
22
builtin.py
22
builtin.py
@@ -5,6 +5,8 @@ import os
|
|||||||
import debug
|
import debug
|
||||||
import parsing
|
import parsing
|
||||||
|
|
||||||
|
from _compatibility import exec_function
|
||||||
|
|
||||||
module_find_path = sys.path[1:]
|
module_find_path = sys.path[1:]
|
||||||
|
|
||||||
|
|
||||||
@@ -89,7 +91,8 @@ class Parser(CachedModule):
|
|||||||
temp, sys.path = sys.path, self.sys_path
|
temp, sys.path = sys.path, self.sys_path
|
||||||
# TODO reenable and check (stackoverflow question - pylab builtins)
|
# TODO reenable and check (stackoverflow question - pylab builtins)
|
||||||
#print 'sypa', sys.path
|
#print 'sypa', sys.path
|
||||||
exec 'import %s as module' % self.name in self._content
|
exec_function('import %s as module' % self.name, self._content)
|
||||||
|
|
||||||
self.sys_path, sys.path = sys.path, temp
|
self.sys_path, sys.path = sys.path, temp
|
||||||
|
|
||||||
self.sys_path.pop(0)
|
self.sys_path.pop(0)
|
||||||
@@ -171,7 +174,7 @@ class Parser(CachedModule):
|
|||||||
classes, funcs, stmts, members = get_types(names)
|
classes, funcs, stmts, members = get_types(names)
|
||||||
|
|
||||||
# classes
|
# classes
|
||||||
for name, cl in classes.iteritems():
|
for name, cl in classes.items():
|
||||||
bases = (c.__name__ for c in cl.__bases__)
|
bases = (c.__name__ for c in cl.__bases__)
|
||||||
code += 'class %s(%s):\n' % (name, ','.join(bases))
|
code += 'class %s(%s):\n' % (name, ','.join(bases))
|
||||||
if depth == 0:
|
if depth == 0:
|
||||||
@@ -180,7 +183,7 @@ class Parser(CachedModule):
|
|||||||
code += '\n'
|
code += '\n'
|
||||||
|
|
||||||
# functions
|
# functions
|
||||||
for name, func in funcs.iteritems():
|
for name, func in funcs.items():
|
||||||
params, ret = parse_function_doc(func)
|
params, ret = parse_function_doc(func)
|
||||||
doc_str = parsing.indent_block('"""\n%s\n"""\n' % func.__doc__)
|
doc_str = parsing.indent_block('"""\n%s\n"""\n' % func.__doc__)
|
||||||
try:
|
try:
|
||||||
@@ -201,7 +204,7 @@ class Parser(CachedModule):
|
|||||||
code += mixin[:pos] + doc_str + mixin[pos:]
|
code += mixin[:pos] + doc_str + mixin[pos:]
|
||||||
|
|
||||||
# class members (functions)
|
# class members (functions)
|
||||||
for name, func in members.iteritems():
|
for name, func in members.items():
|
||||||
ret = 'pass'
|
ret = 'pass'
|
||||||
code += '@property\ndef %s(self):\n' % (name)
|
code += '@property\ndef %s(self):\n' % (name)
|
||||||
block = '"""\n%s\n"""\n' % func.__doc__
|
block = '"""\n%s\n"""\n' % func.__doc__
|
||||||
@@ -209,8 +212,8 @@ class Parser(CachedModule):
|
|||||||
code += parsing.indent_block(block)
|
code += parsing.indent_block(block)
|
||||||
|
|
||||||
# variables
|
# variables
|
||||||
for name, value in stmts.iteritems():
|
for name, value in stmts.items():
|
||||||
if type(value) == file:
|
if type(value).__name__ == 'file':
|
||||||
value = 'file'
|
value = 'file'
|
||||||
elif type(value).__name__ in ['int', 'bool', 'float',
|
elif type(value).__name__ in ['int', 'bool', 'float',
|
||||||
'dict', 'list', 'tuple']:
|
'dict', 'list', 'tuple']:
|
||||||
@@ -290,7 +293,12 @@ def parse_function_doc(func):
|
|||||||
|
|
||||||
|
|
||||||
class _Builtin(object):
|
class _Builtin(object):
|
||||||
_builtins = Parser(name='__builtin__')
|
# Python 3 compatibility
|
||||||
|
if sys.hexversion > 0x03000000:
|
||||||
|
name = 'builtins'
|
||||||
|
else:
|
||||||
|
name='__builtin__'
|
||||||
|
_builtins = Parser(name=name)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def scope(self):
|
def scope(self):
|
||||||
|
|||||||
6
ftest.py
6
ftest.py
@@ -3,7 +3,7 @@
|
|||||||
import functions
|
import functions
|
||||||
|
|
||||||
functions.debug.debug_function = functions.debug.print_to_stdout
|
functions.debug.debug_function = functions.debug.print_to_stdout
|
||||||
functions.debug.ignored_modules = ['parsing', 'builtin']
|
#functions.debug.ignored_modules = ['parsing', 'builtin']
|
||||||
#functions.debug.ignored_modules = ['parsing', 'builtin', 'evaluate', 'modules']
|
#functions.debug.ignored_modules = ['parsing', 'builtin', 'evaluate', 'modules']
|
||||||
functions.modules.builtin.module_find_path.insert(0, '.')
|
functions.modules.builtin.module_find_path.insert(0, '.')
|
||||||
|
|
||||||
@@ -19,7 +19,7 @@ for i in range(1):
|
|||||||
#completions = functions.get_definitions(code, 181, 2, path)
|
#completions = functions.get_definitions(code, 181, 2, path)
|
||||||
#completions = functions.complete(code, 42, 200, path)
|
#completions = functions.complete(code, 42, 200, path)
|
||||||
|
|
||||||
print '\n', ', '.join(sorted(str(c) for c in completions))
|
print('\n', ', '.join(sorted(str(c) for c in completions)))
|
||||||
#print [n.name for n in completions]
|
#print [n.name for n in completions]
|
||||||
#print [n.name.get_parent_until() for n in completions]
|
#print [n.name.get_parent_until() for n in completions]
|
||||||
print '#', len(completions)
|
print('#', len(completions))
|
||||||
|
|||||||
10
modules.py
10
modules.py
@@ -1,3 +1,4 @@
|
|||||||
|
from __future__ import with_statement
|
||||||
import re
|
import re
|
||||||
import tokenize
|
import tokenize
|
||||||
import imp
|
import imp
|
||||||
@@ -60,6 +61,9 @@ class ModuleWithCursor(Module):
|
|||||||
self._row_temp = None
|
self._row_temp = None
|
||||||
self._relevant_temp = None
|
self._relevant_temp = None
|
||||||
|
|
||||||
|
# Call the parser already here, because it will be used anyways.
|
||||||
|
# Also, the position is here important (which will not be used by
|
||||||
|
# default), therefore fill the cache here.
|
||||||
self._parser = parsing.PyFuzzyParser(source, path, row)
|
self._parser = parsing.PyFuzzyParser(source, path, row)
|
||||||
|
|
||||||
def get_path_until_cursor(self, column):
|
def get_path_until_cursor(self, column):
|
||||||
@@ -192,10 +196,8 @@ def find_module(current_module, point_path):
|
|||||||
# is a directory module
|
# is a directory module
|
||||||
if is_package_directory:
|
if is_package_directory:
|
||||||
path += '/__init__.py'
|
path += '/__init__.py'
|
||||||
# python2.5 cannot cope with the `with` statement
|
with open(path) as f:
|
||||||
#with open(path) as f:
|
source = f.read()
|
||||||
# source = f.read()
|
|
||||||
source = open(path).read()
|
|
||||||
else:
|
else:
|
||||||
source = current_namespace[0].read()
|
source = current_namespace[0].read()
|
||||||
if path.endswith('.py'):
|
if path.endswith('.py'):
|
||||||
|
|||||||
@@ -132,8 +132,8 @@ class c1():
|
|||||||
self.acp = athefirst; self.bcp = 3
|
self.acp = athefirst; self.bcp = 3
|
||||||
def c3(self, daeparam):
|
def c3(self, daeparam):
|
||||||
import os as c4 #from parsing import Scope as c4
|
import os as c4 #from parsing import Scope as c4
|
||||||
c5 = 1
|
c5 = 1
|
||||||
c5 = c4(
|
c5 = c4()
|
||||||
if 1:
|
if 1:
|
||||||
print 1
|
print 1
|
||||||
#return c5+'asdf'
|
#return c5+'asdf'
|
||||||
@@ -178,3 +178,4 @@ a = next(gen_exe)
|
|||||||
def ret():
|
def ret():
|
||||||
r = []
|
r = []
|
||||||
return r.
|
return r.
|
||||||
|
|
||||||
|
|||||||
15
parsing.py
15
parsing.py
@@ -34,7 +34,7 @@ TODO check meta classes
|
|||||||
from _compatibility import next, literal_eval
|
from _compatibility import next, literal_eval
|
||||||
|
|
||||||
import tokenize
|
import tokenize
|
||||||
import cStringIO
|
from io import BytesIO
|
||||||
import re
|
import re
|
||||||
|
|
||||||
import debug
|
import debug
|
||||||
@@ -229,7 +229,7 @@ class Module(Scope):
|
|||||||
The top scope, which is always a module.
|
The top scope, which is always a module.
|
||||||
"""
|
"""
|
||||||
def __init__(self, path, docstr=''):
|
def __init__(self, path, docstr=''):
|
||||||
super(Module, self).__init__(path, docstr)
|
super(Module, self).__init__(0, 0, docstr)
|
||||||
self.path = path
|
self.path = path
|
||||||
self.global_vars = []
|
self.global_vars = []
|
||||||
|
|
||||||
@@ -1245,8 +1245,14 @@ class PyFuzzyParser(object):
|
|||||||
|
|
||||||
:raises: IndentationError
|
:raises: IndentationError
|
||||||
"""
|
"""
|
||||||
buf = cStringIO.StringIO(self.code)
|
buf = BytesIO(self.code.encode())
|
||||||
self.gen = tokenize.generate_tokens(buf.readline)
|
#print(self.code.encode())
|
||||||
|
#print(list(tokenize.tokenize(BytesIO(self.code.encode()).readline))[:9])
|
||||||
|
import sys
|
||||||
|
if sys.hexversion > 0x03000000:
|
||||||
|
self.gen = tokenize.tokenize(buf.readline)
|
||||||
|
else:
|
||||||
|
self.gen = tokenize.generate_tokens(buf.readline)
|
||||||
self.currentscope = self.scope
|
self.currentscope = self.scope
|
||||||
|
|
||||||
extended_flow = ['else', 'elif', 'except', 'finally']
|
extended_flow = ['else', 'elif', 'except', 'finally']
|
||||||
@@ -1270,6 +1276,7 @@ class PyFuzzyParser(object):
|
|||||||
# check again for unindented stuff. this is true for syntax
|
# check again for unindented stuff. this is true for syntax
|
||||||
# errors. only check for names, because thats relevant here. If
|
# errors. only check for names, because thats relevant here. If
|
||||||
# some docstrings are not indented, I don't care.
|
# some docstrings are not indented, I don't care.
|
||||||
|
print(self.scope.indent, self.scope)
|
||||||
while indent <= self.scope.indent \
|
while indent <= self.scope.indent \
|
||||||
and (token_type == tokenize.NAME or tok in ['(', '['])\
|
and (token_type == tokenize.NAME or tok in ['(', '['])\
|
||||||
and self.scope != self.top:
|
and self.scope != self.top:
|
||||||
|
|||||||
Reference in New Issue
Block a user