diff --git a/_compatibility.py b/_compatibility.py index 27102c85..807ea3d6 100644 --- a/_compatibility.py +++ b/_compatibility.py @@ -90,3 +90,14 @@ else: return True except AttributeError: return False + +class Python3Method(object): + def __init__(self, func): + self.func = func + + def __get__(self, obj, objtype): + if obj is None: + return lambda *args, **kwargs: self.func(*args, **kwargs) + else: + return lambda *args, **kwargs: self.func(obj, *args, **kwargs) + diff --git a/parsing.py b/parsing.py index 984eca0e..b37e8c1f 100644 --- a/parsing.py +++ b/parsing.py @@ -29,7 +29,7 @@ Ignored statements: - exec (dangerous - not controllable) """ from _compatibility import (next, literal_eval, tokenize_func, BytesIO, - property, is_py3k) + property, is_py3k, Python3Method) import tokenize import re @@ -164,6 +164,7 @@ class Scope(Simple): string = indent_block(string, indention=indention) return string + @Python3Method def get_set_vars(self): """ Get all the names, that are active and accessible in the current @@ -172,15 +173,6 @@ class Scope(Simple): :return: list of Name :rtype: list """ - return self._get_set_vars(self) - - @staticmethod - def _get_set_vars(self): - """ - This is a hack, because Python 2 has another understanding of methods, - than Python 3. In Python 2 it is not possible to use a method without - the `self` being an instance of the class. - """ n = [] for stmt in self.statements: try: @@ -207,6 +199,17 @@ class Scope(Simple): """ return not (self.imports or self.subscopes or self.statements) + @Python3Method + def get_statement_for_position(self, pos): + for s in self.statements: + if s.start_pos <= pos < self.end_pos: + return s + + for s in self.subscopes: + p = s.get_statement_for_position(pos) + if p: + return p + def __repr__(self): try: name = self.name