diff --git a/_compatibility.py b/_compatibility.py index cf41b37b..d06f7674 100644 --- a/_compatibility.py +++ b/_compatibility.py @@ -112,3 +112,37 @@ def use_metaclass(meta, *bases): if not bases: bases = (object,) return meta("HackClass", bases, {}) + +try: + from inspect import cleandoc +except ImportError: + # python 2.5 doesn't have this method + import string + def cleandoc(doc): + """Clean up indentation from docstrings. + + Any whitespace that can be uniformly removed from the second line + onwards is removed.""" + try: + lines = string.split(string.expandtabs(doc), '\n') + except UnicodeError: + return None + else: + # Find minimum indentation of any non-blank lines after first line. + margin = sys.maxint + for line in lines[1:]: + content = len(string.lstrip(line)) + if content: + indent = len(line) - content + margin = min(margin, indent) + # Remove indentation. + if lines: + lines[0] = lines[0].lstrip() + if margin < sys.maxint: + for i in range(1, len(lines)): lines[i] = lines[i][margin:] + # Remove any trailing or leading blank lines. + while lines and not lines[-1]: + lines.pop() + while lines and not lines[0]: + lines.pop(0) + return string.join(lines, '\n') diff --git a/parsing.py b/parsing.py index aa24cc4c..075db832 100644 --- a/parsing.py +++ b/parsing.py @@ -29,7 +29,7 @@ as input and ignores just all the non-python stuff. Basically you could feed it a perl script, and it should still work (which means throw no error. """ from _compatibility import (next, literal_eval, tokenize_func, BytesIO, - property, is_py3k, Python3Method) + property, is_py3k, cleandoc, Python3Method) import tokenize import re @@ -37,7 +37,6 @@ import keyword import weakref import debug -from inspect import cleandoc class ParserError(Exception):