From 11808d8fa33fb6c61d6c88456e81edc88c1ece36 Mon Sep 17 00:00:00 2001 From: David Halter Date: Tue, 7 Aug 2012 10:09:59 +0200 Subject: [PATCH] catched some errors of invalid code, that was caused by imbalanced closing brackets --- functions.py | 6 ++++ modules.py | 57 +++++++++++++++++++++----------------- test/completion/invalid.py | 7 +++-- 3 files changed, 43 insertions(+), 27 deletions(-) diff --git a/functions.py b/functions.py index d4fa36ba..6eae67f1 100644 --- a/functions.py +++ b/functions.py @@ -205,6 +205,12 @@ def prepare_goto(source, position, source_path, module, goto_path, debug.dbg('start: %s in %s' % (goto_path, scope)) user_stmt = module.parser.user_stmt + if not user_stmt and len(goto_path.split('\n')) > 1: + # If the user_stmt is not defined and the goto_path is multi line, + # something's strange. Most probably the backwards tokenizer matched to + # much. + return [] + if isinstance(user_stmt, parsing.Import): scopes = [imports.ImportPath(user_stmt, is_like_search)] else: diff --git a/modules.py b/modules.py index 507be14c..5eb5b4d4 100644 --- a/modules.py +++ b/modules.py @@ -1,9 +1,11 @@ from __future__ import with_statement import re import tokenize +import sys import parsing import builtin +import debug files = {} load_module_cb = None @@ -63,7 +65,7 @@ class ModuleWithCursor(Module): else: line = line + '\n' # add lines with a backslash at the end - while self._line_temp > 1: + while 1: self._line_temp -= 1 last_line = self.get_line(self._line_temp) if last_line and last_line[-1] == '\\': @@ -81,34 +83,37 @@ class ModuleWithCursor(Module): gen = tokenize.generate_tokens(fetch_line) string = '' level = 0 - for token_type, tok, start, end, line in gen: - #print token_type, tok, force_point - if level > 0: - if tok in close_brackets: - level += 1 - if tok in open_brackets: - level -= 1 - elif tok == '.': - force_point = False - elif force_point: - # it is reversed, therefore a number is getting recognized - # as a floating point number - if token_type == tokenize.NUMBER and tok[0] == '.': + try: + for token_type, tok, start, end, line in gen: + #print token_type, tok, force_point + if level > 0: + if tok in close_brackets: + level += 1 + if tok in open_brackets: + level -= 1 + elif tok == '.': force_point = False + elif force_point: + # it is reversed, therefore a number is getting recognized + # as a floating point number + if token_type == tokenize.NUMBER and tok[0] == '.': + force_point = False + else: + #print 'break2', token_type, tok + break + elif tok in close_brackets: + level += 1 + elif token_type in [tokenize.NAME, tokenize.STRING]: + force_point = True + elif token_type == tokenize.NUMBER: + pass else: - #print 'break2', token_type, tok + #print 'break', token_type, tok break - elif tok in close_brackets: - level += 1 - elif token_type in [tokenize.NAME, tokenize.STRING]: - force_point = True - elif token_type == tokenize.NUMBER: - pass - else: - #print 'break', token_type, tok - break - string += tok + string += tok + except tokenize.TokenError: + debug.warning("Tokenize couldn't finish", sys.exc_info) return string[::-1] @@ -125,6 +130,8 @@ class ModuleWithCursor(Module): if not self._line_cache: self._line_cache = self.source.split('\n') + if line < 1: + raise StopIteration() try: return self._line_cache[line - 1] except IndexError: diff --git a/test/completion/invalid.py b/test/completion/invalid.py index 042cee92..b6e6b2e0 100644 --- a/test/completion/invalid.py +++ b/test/completion/invalid.py @@ -1,3 +1,6 @@ +#? ['message'] +AttributeError()).message + from a import (b def blub(): return 0 @@ -53,5 +56,5 @@ try: #? str() "" -#? ['lala'] -isinstance()) +#? [] +int()).