added a descriptor, to ignore unbound methods in Python 2

This commit is contained in:
David Halter
2012-08-22 01:11:54 +02:00
parent 528b978ca5
commit 9c09de6245
2 changed files with 24 additions and 10 deletions

View File

@@ -90,3 +90,14 @@ else:
return True return True
except AttributeError: except AttributeError:
return False 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)

View File

@@ -29,7 +29,7 @@ Ignored statements:
- exec (dangerous - not controllable) - exec (dangerous - not controllable)
""" """
from _compatibility import (next, literal_eval, tokenize_func, BytesIO, from _compatibility import (next, literal_eval, tokenize_func, BytesIO,
property, is_py3k) property, is_py3k, Python3Method)
import tokenize import tokenize
import re import re
@@ -164,6 +164,7 @@ class Scope(Simple):
string = indent_block(string, indention=indention) string = indent_block(string, indention=indention)
return string return string
@Python3Method
def get_set_vars(self): def get_set_vars(self):
""" """
Get all the names, that are active and accessible in the current Get all the names, that are active and accessible in the current
@@ -172,15 +173,6 @@ class Scope(Simple):
:return: list of Name :return: list of Name
:rtype: list :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 = [] n = []
for stmt in self.statements: for stmt in self.statements:
try: try:
@@ -207,6 +199,17 @@ class Scope(Simple):
""" """
return not (self.imports or self.subscopes or self.statements) 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): def __repr__(self):
try: try:
name = self.name name = self.name