From cab74d5f5128c7e7345f8081682265b5fea54a12 Mon Sep 17 00:00:00 2001 From: David Halter Date: Thu, 13 Dec 2012 16:38:15 +0100 Subject: [PATCH] move parsing.PushBackIterator and parsing.indent_block to common --- jedi/api.py | 3 ++- jedi/builtin.py | 9 +++++---- jedi/common.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ jedi/evaluate.py | 3 ++- jedi/helpers.py | 9 --------- jedi/modules.py | 1 - jedi/parsing.py | 39 ++++----------------------------------- 7 files changed, 60 insertions(+), 51 deletions(-) create mode 100644 jedi/common.py diff --git a/jedi/api.py b/jedi/api.py index 6fb7a0d7..07aa6e3d 100644 --- a/jedi/api.py +++ b/jedi/api.py @@ -37,6 +37,7 @@ import debug import settings import keywords import helpers +import common import builtin import api_classes @@ -401,7 +402,7 @@ class Script(object): return None debug.speed('func_call user_stmt') - with helpers.scale_speed_settings(settings.scale_get_in_function_call): + with common.scale_speed_settings(settings.scale_get_in_function_call): origins = evaluate.follow_call(call) debug.speed('func_call followed') diff --git a/jedi/builtin.py b/jedi/builtin.py index da4fc563..bb7ac96f 100644 --- a/jedi/builtin.py +++ b/jedi/builtin.py @@ -9,6 +9,7 @@ if is_py3k: import types import inspect +import common import debug import parsing import imports @@ -222,7 +223,7 @@ def _generate_code(scope, mixin_funcs={}, depth=0): if doc: doc = ('r"""\n%s\n"""\n' % doc) if indent: - doc = parsing.indent_block(doc) + doc = common.indent_block(doc) return doc return '' @@ -303,7 +304,7 @@ def _generate_code(scope, mixin_funcs={}, depth=0): except KeyError: mixin = {} cl_code = _generate_code(cl, mixin, depth + 1) - code += parsing.indent_block(cl_code) + code += common.indent_block(cl_code) code += '\n' # functions @@ -318,7 +319,7 @@ def _generate_code(scope, mixin_funcs={}, depth=0): # normal code generation code += 'def %s(%s):\n' % (name, params) code += doc_str - code += parsing.indent_block('%s\n\n' % ret) + code += common.indent_block('%s\n\n' % ret) else: # generation of code with mixins # the parser only supports basic functions with a newline after @@ -336,7 +337,7 @@ def _generate_code(scope, mixin_funcs={}, depth=0): continue ret = 'pass' code += '@property\ndef %s(self):\n' % (name) - code += parsing.indent_block(get_doc(func) + '%s\n\n' % ret) + code += common.indent_block(get_doc(func) + '%s\n\n' % ret) # variables for name, value in stmts.items(): diff --git a/jedi/common.py b/jedi/common.py new file mode 100644 index 00000000..00e77d6b --- /dev/null +++ b/jedi/common.py @@ -0,0 +1,47 @@ +""" A universal module with functions / classes without dependencies. """ +import contextlib + +import settings + + +class PushBackIterator(object): + def __init__(self, iterator): + self.pushes = [] + self.iterator = iterator + + def push_back(self, value): + self.pushes.append(value) + + def __iter__(self): + return self + + def next(self): + """ Python 2 Compatibility """ + return self.__next__() + + def __next__(self): + if self.pushes: + return self.pushes.pop() + else: + return next(self.iterator) + + +@contextlib.contextmanager +def scale_speed_settings(factor): + a = settings.max_executions + b = settings.max_until_execution_unique + settings.max_executions *= factor + settings.max_until_execution_unique *= factor + yield + settings.max_executions = a + settings.max_until_execution_unique = b + + +def indent_block(text, indention=' '): + """ This function indents a text block with a default of four spaces """ + temp = '' + while text and text[-1] == '\n': + temp += text[-1] + text = text[:-1] + lines = text.split('\n') + return '\n'.join(map(lambda s: indention + s, lines)) + temp diff --git a/jedi/evaluate.py b/jedi/evaluate.py index e243e260..3bba6ebf 100644 --- a/jedi/evaluate.py +++ b/jedi/evaluate.py @@ -18,6 +18,7 @@ import sys import itertools import copy +import common import cache import parsing import debug @@ -643,7 +644,7 @@ class Execution(Executable): else: yield None, var_arg - return iter(parsing.PushBackIterator(iterate())) + return iter(common.PushBackIterator(iterate())) def get_set_vars(self): return self.get_defined_names() diff --git a/jedi/helpers.py b/jedi/helpers.py index 111a8e40..1d46b150 100644 --- a/jedi/helpers.py +++ b/jedi/helpers.py @@ -254,12 +254,3 @@ def scan_array_for_pos(arr, pos): return call, check_arr_index(), stop -@contextlib.contextmanager -def scale_speed_settings(factor): - a = settings.max_executions - b = settings.max_until_execution_unique - settings.max_executions *= factor - settings.max_until_execution_unique *= factor - yield - settings.max_executions = a - settings.max_until_execution_unique = b diff --git a/jedi/modules.py b/jedi/modules.py index 71e300ab..af0aa7b8 100644 --- a/jedi/modules.py +++ b/jedi/modules.py @@ -12,7 +12,6 @@ import cache import parsing import builtin import debug -import evaluate import settings import imports diff --git a/jedi/parsing.py b/jedi/parsing.py index fe30f475..dec8ffef 100644 --- a/jedi/parsing.py +++ b/jedi/parsing.py @@ -37,44 +37,13 @@ import keyword import os import debug +import common class ParserError(Exception): pass -def indent_block(text, indention=' '): - """ This function indents a text block with a default of four spaces """ - temp = '' - while text and text[-1] == '\n': - temp += text[-1] - text = text[:-1] - lines = text.split('\n') - return '\n'.join(map(lambda s: indention + s, lines)) + temp - - -class PushBackIterator(object): - def __init__(self, iterator): - self.pushes = [] - self.iterator = iterator - - def push_back(self, value): - self.pushes.append(value) - - def __iter__(self): - return self - - def next(self): - """ Python 2 Compatibility """ - return self.__next__() - - def __next__(self): - if self.pushes: - return self.pushes.pop() - else: - return next(self.iterator) - - class Base(object): """ This is just here to have an isinstance check, which is also used on @@ -191,7 +160,7 @@ class Scope(Simple): string += stmt.get_code() if first_indent: - string = indent_block(string, indention=indention) + string = common.indent_block(string, indention=indention) return string @Python3Method @@ -1632,7 +1601,7 @@ class PyFuzzyParser(object): debug.warning('indentation error on line %s, ignoring it' % (self.start_pos[0])) self._line_of_tokenize_restart = self.start_pos[0] + 1 - self.gen = PushBackIterator(tokenize.generate_tokens( + self.gen = common.PushBackIterator(tokenize.generate_tokens( self.buf.readline)) return self.next() except StopIteration: @@ -1667,7 +1636,7 @@ class PyFuzzyParser(object): :raises: IndentationError """ self.buf = StringIO(self.code) - self.gen = PushBackIterator(tokenize.generate_tokens( + self.gen = common.PushBackIterator(tokenize.generate_tokens( self.buf.readline)) extended_flow = ['else', 'elif', 'except', 'finally']