extracted constants, util methods from generator, forced PEP8 style

This commit is contained in:
Ekaterina Tuzova
2013-10-24 13:57:12 +04:00
parent e27a3c18f5
commit 102647e3b8
8 changed files with 2475 additions and 2505 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1 @@
__author__ = 'ktisha'

View File

@@ -0,0 +1,790 @@
import os
import re
import types
import sys
import string
import time
VERSION = "1.131"
OUT_ENCODING = 'utf-8'
version = (
(sys.hexversion & (0xff << 24)) >> 24,
(sys.hexversion & (0xff << 16)) >> 16
)
if version[0] >= 3:
#noinspection PyUnresolvedReferences
import builtins as the_builtins
string = "".__class__
STR_TYPES = (getattr(the_builtins, "bytes"), str)
NUM_TYPES = (int, float)
SIMPLEST_TYPES = NUM_TYPES + STR_TYPES + (None.__class__,)
EASY_TYPES = NUM_TYPES + STR_TYPES + (None.__class__, dict, tuple, list)
def the_exec(source, context):
exec (source, context)
else: # < 3.0
import __builtin__ as the_builtins
STR_TYPES = (getattr(the_builtins, "unicode"), str)
NUM_TYPES = (int, long, float)
SIMPLEST_TYPES = NUM_TYPES + STR_TYPES + (types.NoneType,)
EASY_TYPES = NUM_TYPES + STR_TYPES + (types.NoneType, dict, tuple, list)
def the_exec(source, context):
#noinspection PyRedundantParentheses
exec (source) in context
if version[0] == 2 and version[1] < 4:
HAS_DECORATORS = False
def lstrip(s, prefix):
i = 0
while s[i] == prefix:
i += 1
return s[i:]
else:
HAS_DECORATORS = True
lstrip = string.lstrip
# return type inference helper table
INT_LIT = '0'
FLOAT_LIT = '0.0'
DICT_LIT = '{}'
LIST_LIT = '[]'
TUPLE_LIT = '()'
BOOL_LIT = 'False'
RET_TYPE = {# {'type_name': 'value_string'} lookup table
# chaining
"self": "self",
"self.": "self",
# int
"int": INT_LIT,
"Int": INT_LIT,
"integer": INT_LIT,
"Integer": INT_LIT,
"short": INT_LIT,
"long": INT_LIT,
"number": INT_LIT,
"Number": INT_LIT,
# float
"float": FLOAT_LIT,
"Float": FLOAT_LIT,
"double": FLOAT_LIT,
"Double": FLOAT_LIT,
"floating": FLOAT_LIT,
# boolean
"bool": BOOL_LIT,
"boolean": BOOL_LIT,
"Bool": BOOL_LIT,
"Boolean": BOOL_LIT,
"True": BOOL_LIT,
"true": BOOL_LIT,
"False": BOOL_LIT,
"false": BOOL_LIT,
# list
'list': LIST_LIT,
'List': LIST_LIT,
'[]': LIST_LIT,
# tuple
"tuple": TUPLE_LIT,
"sequence": TUPLE_LIT,
"Sequence": TUPLE_LIT,
# dict
"dict": DICT_LIT,
"Dict": DICT_LIT,
"dictionary": DICT_LIT,
"Dictionary": DICT_LIT,
"map": DICT_LIT,
"Map": DICT_LIT,
"hashtable": DICT_LIT,
"Hashtable": DICT_LIT,
"{}": DICT_LIT,
# "objects"
"object": "object()",
}
if version[0] < 3:
UNICODE_LIT = 'u""'
BYTES_LIT = '""'
RET_TYPE.update({
'string': BYTES_LIT,
'String': BYTES_LIT,
'str': BYTES_LIT,
'Str': BYTES_LIT,
'character': BYTES_LIT,
'char': BYTES_LIT,
'unicode': UNICODE_LIT,
'Unicode': UNICODE_LIT,
'bytes': BYTES_LIT,
'byte': BYTES_LIT,
'Bytes': BYTES_LIT,
'Byte': BYTES_LIT,
})
DEFAULT_STR_LIT = BYTES_LIT
# also, files:
RET_TYPE.update({
'file': "file('/dev/null')",
})
def ensureUnicode(data):
if type(data) == str:
return data.decode(OUT_ENCODING, 'replace')
return unicode(data)
else:
UNICODE_LIT = '""'
BYTES_LIT = 'b""'
RET_TYPE.update({
'string': UNICODE_LIT,
'String': UNICODE_LIT,
'str': UNICODE_LIT,
'Str': UNICODE_LIT,
'character': UNICODE_LIT,
'char': UNICODE_LIT,
'unicode': UNICODE_LIT,
'Unicode': UNICODE_LIT,
'bytes': BYTES_LIT,
'byte': BYTES_LIT,
'Bytes': BYTES_LIT,
'Byte': BYTES_LIT,
})
DEFAULT_STR_LIT = UNICODE_LIT
# also, files: we can't provide an easy expression on py3k
RET_TYPE.update({
'file': None,
})
def ensureUnicode(data):
if type(data) == bytes:
return data.decode(OUT_ENCODING, 'replace')
return str(data)
if version[0] > 2:
import io # in 3.0
#noinspection PyArgumentList
fopen = lambda name, mode: io.open(name, mode, encoding=OUT_ENCODING)
else:
fopen = open
if sys.platform == 'cli':
#noinspection PyUnresolvedReferences
from System import DateTime
class Timer(object):
def __init__(self):
self.started = DateTime.Now
def elapsed(self):
return (DateTime.Now - self.started).TotalMilliseconds
else:
class Timer(object):
def __init__(self):
self.started = time.time()
def elapsed(self):
return int((time.time() - self.started) * 1000)
IS_JAVA = hasattr(os, "java")
BUILTIN_MOD_NAME = the_builtins.__name__
IDENT_PATTERN = "[A-Za-z_][0-9A-Za-z_]*" # re pattern for identifier
NUM_IDENT_PATTERN = re.compile("([A-Za-z_]+)[0-9]?[A-Za-z_]*") # 'foo_123' -> $1 = 'foo_'
STR_CHAR_PATTERN = "[0-9A-Za-z_.,\+\-&\*% ]"
DOC_FUNC_RE = re.compile("(?:.*\.)?(\w+)\(([^\)]*)\).*") # $1 = function name, $2 = arglist
SANE_REPR_RE = re.compile(IDENT_PATTERN + "(?:\(.*\))?") # identifier with possible (...), go catches
IDENT_RE = re.compile("(" + IDENT_PATTERN + ")") # $1 = identifier
STARS_IDENT_RE = re.compile("(\*?\*?" + IDENT_PATTERN + ")") # $1 = identifier, maybe with a * or **
IDENT_EQ_RE = re.compile("(" + IDENT_PATTERN + "\s*=)") # $1 = identifier with a following '='
SIMPLE_VALUE_RE = re.compile(
"(\([+-]?[0-9](?:\s*,\s*[+-]?[0-9])*\))|" + # a numeric tuple, e.g. in pygame
"([+-]?[0-9]+\.?[0-9]*(?:[Ee]?[+-]?[0-9]+\.?[0-9]*)?)|" + # number
"('" + STR_CHAR_PATTERN + "*')|" + # single-quoted string
'("' + STR_CHAR_PATTERN + '*")|' + # double-quoted string
"(\[\])|" +
"(\{\})|" +
"(\(\))|" +
"(True|False|None)"
) # $? = sane default value
########################### parsing ###########################################################
if version[0] < 3:
from pyparsing import *
else:
#noinspection PyUnresolvedReferences
from pyparsing_py3 import *
# grammar to parse parameter lists
# // snatched from parsePythonValue.py, from pyparsing samples, copyright 2006 by Paul McGuire but under BSD license.
# we don't suppress lots of punctuation because we want it back when we reconstruct the lists
lparen, rparen, lbrack, rbrack, lbrace, rbrace, colon = map(Literal, "()[]{}:")
integer = Combine(Optional(oneOf("+ -")) + Word(nums)).setName("integer")
real = Combine(Optional(oneOf("+ -")) + Word(nums) + "." +
Optional(Word(nums)) +
Optional(oneOf("e E") + Optional(oneOf("+ -")) + Word(nums))).setName("real")
tupleStr = Forward()
listStr = Forward()
dictStr = Forward()
boolLiteral = oneOf("True False")
noneLiteral = Literal("None")
listItem = real | integer | quotedString | unicodeString | boolLiteral | noneLiteral | \
Group(listStr) | tupleStr | dictStr
tupleStr << ( Suppress("(") + Optional(delimitedList(listItem)) +
Optional(Literal(",")) + Suppress(")") ).setResultsName("tuple")
listStr << (lbrack + Optional(delimitedList(listItem) +
Optional(Literal(","))) + rbrack).setResultsName("list")
dictEntry = Group(listItem + colon + listItem)
dictStr << (lbrace + Optional(delimitedList(dictEntry) + Optional(Literal(","))) + rbrace).setResultsName("dict")
# \\ end of the snatched part
# our output format is s-expressions:
# (simple name optional_value) is name or name=value
# (nested (simple ...) (simple ...)) is (name, name,...)
# (opt ...) is [, ...] or suchlike.
T_SIMPLE = 'Simple'
T_NESTED = 'Nested'
T_OPTIONAL = 'Opt'
T_RETURN = "Ret"
TRIPLE_DOT = '...'
COMMA = Suppress(",")
APOS = Suppress("'")
QUOTE = Suppress('"')
SP = Suppress(Optional(White()))
ident = Word(alphas + "_", alphanums + "_-.").setName("ident") # we accept things like "foo-or-bar"
decorated_ident = ident + Optional(Suppress(SP + Literal(":") + SP + ident)) # accept "foo: bar", ignore "bar"
spaced_ident = Combine(decorated_ident + ZeroOrMore(Literal(' ') + decorated_ident)) # we accept 'list or tuple' or 'C struct'
# allow quoted names, because __setattr__, etc docs use it
paramname = spaced_ident | \
APOS + spaced_ident + APOS | \
QUOTE + spaced_ident + QUOTE
parenthesized_tuple = ( Literal("(") + Optional(delimitedList(listItem, combine=True)) +
Optional(Literal(",")) + Literal(")") ).setResultsName("(tuple)")
initializer = (SP + Suppress("=") + SP + Combine(parenthesized_tuple | listItem | ident)).setName("=init") # accept foo=defaultfoo
param = Group(Empty().setParseAction(replaceWith(T_SIMPLE)) + Combine(Optional(oneOf("* **")) + paramname) + Optional(initializer))
ellipsis = Group(
Empty().setParseAction(replaceWith(T_SIMPLE)) + \
(Literal("..") +
ZeroOrMore(Literal('.'))).setParseAction(replaceWith(TRIPLE_DOT)) # we want to accept both 'foo,..' and 'foo, ...'
)
paramSlot = Forward()
simpleParamSeq = ZeroOrMore(paramSlot + COMMA) + Optional(paramSlot + Optional(COMMA))
nestedParamSeq = Group(
Suppress('(').setParseAction(replaceWith(T_NESTED)) + \
simpleParamSeq + Optional(ellipsis + Optional(COMMA) + Optional(simpleParamSeq)) + \
Suppress(')')
) # we accept "(a1, ... an)"
paramSlot << (param | nestedParamSeq)
optionalPart = Forward()
paramSeq = simpleParamSeq + Optional(optionalPart) # this is our approximate target
optionalPart << (
Group(
Suppress('[').setParseAction(replaceWith(T_OPTIONAL)) + Optional(COMMA) +
paramSeq + Optional(ellipsis) +
Suppress(']')
)
| ellipsis
)
return_type = Group(
Empty().setParseAction(replaceWith(T_RETURN)) +
Suppress(SP + (Literal("->") | (Literal(":") + SP + Literal("return"))) + SP) +
ident
)
# this is our ideal target, with balancing paren and a multiline rest of doc.
paramSeqAndRest = paramSeq + Suppress(')') + Optional(return_type) + Suppress(Optional(Regex(".*(?s)")))
############################################################################################
# Some values are known to be of no use in source and needs to be suppressed.
# Dict is keyed by module names, with "*" meaning "any module";
# values are lists of names of members whose value must be pruned.
SKIP_VALUE_IN_MODULE = {
"sys": (
"modules", "path_importer_cache", "argv", "builtins",
"last_traceback", "last_type", "last_value", "builtin_module_names",
),
"posix": (
"environ",
),
"zipimport": (
"_zip_directory_cache",
),
"*": (BUILTIN_MOD_NAME,)
}
# {"module": ("name",..)}: omit the names from the skeleton at all.
OMIT_NAME_IN_MODULE = {}
if version[0] >= 3:
v = OMIT_NAME_IN_MODULE.get(BUILTIN_MOD_NAME, []) + ["True", "False", "None", "__debug__"]
OMIT_NAME_IN_MODULE[BUILTIN_MOD_NAME] = v
if IS_JAVA and version > (2, 4): # in 2.5.1 things are way weird!
OMIT_NAME_IN_MODULE['_codecs'] = ['EncodingMap']
OMIT_NAME_IN_MODULE['_hashlib'] = ['Hash']
ADD_VALUE_IN_MODULE = {
"sys": ("exc_value = Exception()", "exc_traceback=None"), # only present after an exception in current thread
}
# Some values are special and are better represented by hand-crafted constructs.
# Dict is keyed by (module name, member name) and value is the replacement.
REPLACE_MODULE_VALUES = {
("numpy.core.multiarray", "typeinfo"): "{}",
("psycopg2._psycopg", "string_types"): "{}", # badly mangled __eq__ breaks fmtValue
("PyQt5.QtWidgets", "qApp") : "QApplication()", # instead of None
}
if version[0] <= 2:
REPLACE_MODULE_VALUES[(BUILTIN_MOD_NAME, "None")] = "object()"
for std_file in ("stdin", "stdout", "stderr"):
REPLACE_MODULE_VALUES[("sys", std_file)] = "open('')" #
# Some functions and methods of some builtin classes have special signatures.
# {("class", "method"): ("signature_string")}
PREDEFINED_BUILTIN_SIGS = { #TODO: user-skeleton
("type", "__init__"): "(cls, what, bases=None, dict=None)", # two sigs squeezed into one
("object", "__init__"): "(self)",
("object", "__new__"): "(cls, *more)", # only for the sake of parameter names readability
("object", "__subclasshook__"): "(cls, subclass)", # trusting PY-1818 on sig
("int", "__init__"): "(self, x, base=10)", # overrides a fake
("list", "__init__"): "(self, seq=())",
("tuple", "__init__"): "(self, seq=())", # overrides a fake
("set", "__init__"): "(self, seq=())",
("dict", "__init__"): "(self, seq=None, **kwargs)",
("property", "__init__"): "(self, fget=None, fset=None, fdel=None, doc=None)",
# TODO: infer, doc comments have it
("dict", "update"): "(self, E=None, **F)", # docstring nearly lies
(None, "zip"): "(seq1, seq2, *more_seqs)",
(None, "range"): "(start=None, stop=None, step=None)", # suboptimal: allows empty arglist
(None, "filter"): "(function_or_none, sequence)",
(None, "iter"): "(source, sentinel=None)",
(None, "getattr"): "(object, name, default=None)",
('frozenset', "__init__"): "(self, seq=())",
("bytearray", "__init__"): "(self, source=None, encoding=None, errors='strict')",
}
if version[0] < 3:
PREDEFINED_BUILTIN_SIGS[
("unicode", "__init__")] = "(self, string=u'', encoding=None, errors='strict')" # overrides a fake
PREDEFINED_BUILTIN_SIGS[("super", "__init__")] = "(self, type1, type2=None)"
PREDEFINED_BUILTIN_SIGS[
(None, "min")] = "(*args, **kwargs)" # too permissive, but py2.x won't allow a better sig
PREDEFINED_BUILTIN_SIGS[(None, "max")] = "(*args, **kwargs)"
PREDEFINED_BUILTIN_SIGS[("str", "__init__")] = "(self, string='')" # overrides a fake
PREDEFINED_BUILTIN_SIGS[(None, "print")] = "(*args, **kwargs)" # can't do better in 2.x
else:
PREDEFINED_BUILTIN_SIGS[("super", "__init__")] = "(self, type1=None, type2=None)"
PREDEFINED_BUILTIN_SIGS[(None, "min")] = "(*args, key=None)"
PREDEFINED_BUILTIN_SIGS[(None, "max")] = "(*args, key=None)"
PREDEFINED_BUILTIN_SIGS[
(None, "open")] = "(file, mode='r', buffering=None, encoding=None, errors=None, newline=None, closefd=True)"
PREDEFINED_BUILTIN_SIGS[
("str", "__init__")] = "(self, value='', encoding=None, errors='strict')" # overrides a fake
PREDEFINED_BUILTIN_SIGS[("str", "format")] = "(*args, **kwargs)"
PREDEFINED_BUILTIN_SIGS[
("bytes", "__init__")] = "(self, value=b'', encoding=None, errors='strict')" # overrides a fake
PREDEFINED_BUILTIN_SIGS[("bytes", "format")] = "(*args, **kwargs)"
PREDEFINED_BUILTIN_SIGS[(None, "print")] = "(*args, sep=' ', end='\\n', file=None)" # proper signature
if (2, 6) <= version < (3, 0):
PREDEFINED_BUILTIN_SIGS[("unicode", "format")] = "(*args, **kwargs)"
PREDEFINED_BUILTIN_SIGS[("str", "format")] = "(*args, **kwargs)"
if version == (2, 5):
PREDEFINED_BUILTIN_SIGS[("unicode", "splitlines")] = "(keepends=None)" # a typo in docstring there
if version >= (2, 7):
PREDEFINED_BUILTIN_SIGS[
("enumerate", "__init__")] = "(self, iterable, start=0)" # dosctring omits this completely.
if version < (3, 3):
datetime_mod = "datetime"
else:
datetime_mod = "_datetime"
# NOTE: per-module signature data may be lazily imported
# keyed by (module_name, class_name, method_name). PREDEFINED_BUILTIN_SIGS might be a layer of it.
# value is ("signature", "return_literal")
PREDEFINED_MOD_CLASS_SIGS = { #TODO: user-skeleton
(BUILTIN_MOD_NAME, None, 'divmod'): ("(x, y)", "(0, 0)"),
("binascii", None, "hexlify"): ("(data)", BYTES_LIT),
("binascii", None, "unhexlify"): ("(hexstr)", BYTES_LIT),
("time", None, "ctime"): ("(seconds=None)", DEFAULT_STR_LIT),
("_struct", None, "pack"): ("(fmt, *args)", BYTES_LIT),
("_struct", None, "pack_into"): ("(fmt, buffer, offset, *args)", None),
("_struct", None, "unpack"): ("(fmt, string)", None),
("_struct", None, "unpack_from"): ("(fmt, buffer, offset=0)", None),
("_struct", None, "calcsize"): ("(fmt)", INT_LIT),
("_struct", "Struct", "__init__"): ("(self, fmt)", None),
("_struct", "Struct", "pack"): ("(self, *args)", BYTES_LIT),
("_struct", "Struct", "pack_into"): ("(self, buffer, offset, *args)", None),
("_struct", "Struct", "unpack"): ("(self, string)", None),
("_struct", "Struct", "unpack_from"): ("(self, buffer, offset=0)", None),
(datetime_mod, "date", "__new__"): ("(cls, year=None, month=None, day=None)", None),
(datetime_mod, "date", "fromordinal"): ("(cls, ordinal)", "date(1,1,1)"),
(datetime_mod, "date", "fromtimestamp"): ("(cls, timestamp)", "date(1,1,1)"),
(datetime_mod, "date", "isocalendar"): ("(self)", "(1, 1, 1)"),
(datetime_mod, "date", "isoformat"): ("(self)", DEFAULT_STR_LIT),
(datetime_mod, "date", "isoweekday"): ("(self)", INT_LIT),
(datetime_mod, "date", "replace"): ("(self, year=None, month=None, day=None)", "date(1,1,1)"),
(datetime_mod, "date", "strftime"): ("(self, format)", DEFAULT_STR_LIT),
(datetime_mod, "date", "timetuple"): ("(self)", "(0, 0, 0, 0, 0, 0, 0, 0, 0)"),
(datetime_mod, "date", "today"): ("(self)", "date(1, 1, 1)"),
(datetime_mod, "date", "toordinal"): ("(self)", INT_LIT),
(datetime_mod, "date", "weekday"): ("(self)", INT_LIT),
(datetime_mod, "timedelta", "__new__"
): (
"(cls, days=None, seconds=None, microseconds=None, milliseconds=None, minutes=None, hours=None, weeks=None)",
None),
(datetime_mod, "datetime", "__new__"
): (
"(cls, year=None, month=None, day=None, hour=None, minute=None, second=None, microsecond=None, tzinfo=None)",
None),
(datetime_mod, "datetime", "astimezone"): ("(self, tz)", "datetime(1, 1, 1)"),
(datetime_mod, "datetime", "combine"): ("(cls, date, time)", "datetime(1, 1, 1)"),
(datetime_mod, "datetime", "date"): ("(self)", "datetime(1, 1, 1)"),
(datetime_mod, "datetime", "fromtimestamp"): ("(cls, timestamp, tz=None)", "datetime(1, 1, 1)"),
(datetime_mod, "datetime", "isoformat"): ("(self, sep='T')", DEFAULT_STR_LIT),
(datetime_mod, "datetime", "now"): ("(cls, tz=None)", "datetime(1, 1, 1)"),
(datetime_mod, "datetime", "strptime"): ("(cls, date_string, format)", DEFAULT_STR_LIT),
(datetime_mod, "datetime", "replace" ):
(
"(self, year=None, month=None, day=None, hour=None, minute=None, second=None, microsecond=None, tzinfo=None)",
"datetime(1, 1, 1)"),
(datetime_mod, "datetime", "time"): ("(self)", "time(0, 0)"),
(datetime_mod, "datetime", "timetuple"): ("(self)", "(0, 0, 0, 0, 0, 0, 0, 0, 0)"),
(datetime_mod, "datetime", "timetz"): ("(self)", "time(0, 0)"),
(datetime_mod, "datetime", "utcfromtimestamp"): ("(self, timestamp)", "datetime(1, 1, 1)"),
(datetime_mod, "datetime", "utcnow"): ("(cls)", "datetime(1, 1, 1)"),
(datetime_mod, "datetime", "utctimetuple"): ("(self)", "(0, 0, 0, 0, 0, 0, 0, 0, 0)"),
(datetime_mod, "time", "__new__"): (
"(cls, hour=None, minute=None, second=None, microsecond=None, tzinfo=None)", None),
(datetime_mod, "time", "isoformat"): ("(self)", DEFAULT_STR_LIT),
(datetime_mod, "time", "replace"): (
"(self, hour=None, minute=None, second=None, microsecond=None, tzinfo=None)", "time(0, 0)"),
(datetime_mod, "time", "strftime"): ("(self, format)", DEFAULT_STR_LIT),
(datetime_mod, "tzinfo", "dst"): ("(self, date_time)", INT_LIT),
(datetime_mod, "tzinfo", "fromutc"): ("(self, date_time)", "datetime(1, 1, 1)"),
(datetime_mod, "tzinfo", "tzname"): ("(self, date_time)", DEFAULT_STR_LIT),
(datetime_mod, "tzinfo", "utcoffset"): ("(self, date_time)", INT_LIT),
("_io", None, "open"): ("(name, mode=None, buffering=None)", "file('/dev/null')"),
("_io", "FileIO", "read"): ("(self, size=-1)", DEFAULT_STR_LIT),
("_fileio", "_FileIO", "read"): ("(self, size=-1)", DEFAULT_STR_LIT),
("thread", None, "start_new"): ("(function, args, kwargs=None)", INT_LIT),
("_thread", None, "start_new"): ("(function, args, kwargs=None)", INT_LIT),
("itertools", "groupby", "__init__"): ("(self, iterable, key=None)", None),
("itertools", None, "groupby"): ("(iterable, key=None)", LIST_LIT),
("cStringIO", "OutputType", "seek"): ("(self, position, mode=0)", None),
("cStringIO", "InputType", "seek"): ("(self, position, mode=0)", None),
# NOTE: here we stand on shaky ground providing sigs for 3rd-party modules, though well-known
("numpy.core.multiarray", "ndarray", "__array__"): ("(self, dtype=None)", None),
("numpy.core.multiarray", None, "arange"): ("(start=None, stop=None, step=None, dtype=None)", None),
# same as range()
("numpy.core.multiarray", None, "set_numeric_ops"): ("(**ops)", None),
}
bin_collections_names = ['collections', '_collections']
for name in bin_collections_names:
PREDEFINED_MOD_CLASS_SIGS[(name, "deque", "__init__")] = ("(self, iterable=(), maxlen=None)", None)
PREDEFINED_MOD_CLASS_SIGS[(name, "defaultdict", "__init__")] = ("(self, default_factory=None, **kwargs)", None)
if version[0] < 3:
PREDEFINED_MOD_CLASS_SIGS[("exceptions", "BaseException", "__unicode__")] = ("(self)", UNICODE_LIT)
PREDEFINED_MOD_CLASS_SIGS[("itertools", "product", "__init__")] = ("(self, *iterables, **kwargs)", LIST_LIT)
else:
PREDEFINED_MOD_CLASS_SIGS[("itertools", "product", "__init__")] = ("(self, *iterables, repeat=1)", LIST_LIT)
if version[0] < 3:
PREDEFINED_MOD_CLASS_SIGS[("PyQt4.QtCore", None, "pyqtSlot")] = (
"(*types, **keywords)", None) # doc assumes py3k syntax
# known properties of modules
# {{"module": {"class", "property" : ("letters", ("getter", "type"))}},
# where letters is any set of r,w,d (read, write, del) and "getter" is a source of typed getter.
# if value is None, the property should be omitted.
# read-only properties that return an object are not listed.
G_OBJECT = ("lambda self: object()", None)
G_TYPE = ("lambda self: type(object)", "type")
G_DICT = ("lambda self: {}", "dict")
G_STR = ("lambda self: ''", "string")
G_TUPLE = ("lambda self: tuple()", "tuple")
G_FLOAT = ("lambda self: 0.0", "float")
G_INT = ("lambda self: 0", "int")
G_BOOL = ("lambda self: True", "bool")
KNOWN_PROPS = {
BUILTIN_MOD_NAME: {
("object", '__class__'): ('r', G_TYPE),
('complex', 'real'): ('r', G_FLOAT),
('complex', 'imag'): ('r', G_FLOAT),
("file", 'softspace'): ('r', G_BOOL),
("file", 'name'): ('r', G_STR),
("file", 'encoding'): ('r', G_STR),
("file", 'mode'): ('r', G_STR),
("file", 'closed'): ('r', G_BOOL),
("file", 'newlines'): ('r', G_STR),
("slice", 'start'): ('r', G_INT),
("slice", 'step'): ('r', G_INT),
("slice", 'stop'): ('r', G_INT),
("super", '__thisclass__'): ('r', G_TYPE),
("super", '__self__'): ('r', G_TYPE),
("super", '__self_class__'): ('r', G_TYPE),
("type", '__basicsize__'): ('r', G_INT),
("type", '__itemsize__'): ('r', G_INT),
("type", '__base__'): ('r', G_TYPE),
("type", '__flags__'): ('r', G_INT),
("type", '__mro__'): ('r', G_TUPLE),
("type", '__bases__'): ('r', G_TUPLE),
("type", '__dictoffset__'): ('r', G_INT),
("type", '__dict__'): ('r', G_DICT),
("type", '__name__'): ('r', G_STR),
("type", '__weakrefoffset__'): ('r', G_INT),
},
"exceptions": {
("BaseException", '__dict__'): ('r', G_DICT),
("BaseException", 'message'): ('rwd', G_STR),
("BaseException", 'args'): ('r', G_TUPLE),
("EnvironmentError", 'errno'): ('rwd', G_INT),
("EnvironmentError", 'message'): ('rwd', G_STR),
("EnvironmentError", 'strerror'): ('rwd', G_INT),
("EnvironmentError", 'filename'): ('rwd', G_STR),
("SyntaxError", 'text'): ('rwd', G_STR),
("SyntaxError", 'print_file_and_line'): ('rwd', G_BOOL),
("SyntaxError", 'filename'): ('rwd', G_STR),
("SyntaxError", 'lineno'): ('rwd', G_INT),
("SyntaxError", 'offset'): ('rwd', G_INT),
("SyntaxError", 'msg'): ('rwd', G_STR),
("SyntaxError", 'message'): ('rwd', G_STR),
("SystemExit", 'message'): ('rwd', G_STR),
("SystemExit", 'code'): ('rwd', G_OBJECT),
("UnicodeDecodeError", '__basicsize__'): None,
("UnicodeDecodeError", '__itemsize__'): None,
("UnicodeDecodeError", '__base__'): None,
("UnicodeDecodeError", '__flags__'): ('rwd', G_INT),
("UnicodeDecodeError", '__mro__'): None,
("UnicodeDecodeError", '__bases__'): None,
("UnicodeDecodeError", '__dictoffset__'): None,
("UnicodeDecodeError", '__dict__'): None,
("UnicodeDecodeError", '__name__'): None,
("UnicodeDecodeError", '__weakrefoffset__'): None,
("UnicodeEncodeError", 'end'): ('rwd', G_INT),
("UnicodeEncodeError", 'encoding'): ('rwd', G_STR),
("UnicodeEncodeError", 'object'): ('rwd', G_OBJECT),
("UnicodeEncodeError", 'start'): ('rwd', G_INT),
("UnicodeEncodeError", 'reason'): ('rwd', G_STR),
("UnicodeEncodeError", 'message'): ('rwd', G_STR),
("UnicodeTranslateError", 'end'): ('rwd', G_INT),
("UnicodeTranslateError", 'encoding'): ('rwd', G_STR),
("UnicodeTranslateError", 'object'): ('rwd', G_OBJECT),
("UnicodeTranslateError", 'start'): ('rwd', G_INT),
("UnicodeTranslateError", 'reason'): ('rwd', G_STR),
("UnicodeTranslateError", 'message'): ('rwd', G_STR),
},
'_ast': {
("AST", '__dict__'): ('rd', G_DICT),
},
'posix': {
("statvfs_result", 'f_flag'): ('r', G_INT),
("statvfs_result", 'f_bavail'): ('r', G_INT),
("statvfs_result", 'f_favail'): ('r', G_INT),
("statvfs_result", 'f_files'): ('r', G_INT),
("statvfs_result", 'f_frsize'): ('r', G_INT),
("statvfs_result", 'f_blocks'): ('r', G_INT),
("statvfs_result", 'f_ffree'): ('r', G_INT),
("statvfs_result", 'f_bfree'): ('r', G_INT),
("statvfs_result", 'f_namemax'): ('r', G_INT),
("statvfs_result", 'f_bsize'): ('r', G_INT),
("stat_result", 'st_ctime'): ('r', G_INT),
("stat_result", 'st_rdev'): ('r', G_INT),
("stat_result", 'st_mtime'): ('r', G_INT),
("stat_result", 'st_blocks'): ('r', G_INT),
("stat_result", 'st_gid'): ('r', G_INT),
("stat_result", 'st_nlink'): ('r', G_INT),
("stat_result", 'st_ino'): ('r', G_INT),
("stat_result", 'st_blksize'): ('r', G_INT),
("stat_result", 'st_dev'): ('r', G_INT),
("stat_result", 'st_size'): ('r', G_INT),
("stat_result", 'st_mode'): ('r', G_INT),
("stat_result", 'st_uid'): ('r', G_INT),
("stat_result", 'st_atime'): ('r', G_INT),
},
"pwd": {
("struct_pwent", 'pw_dir'): ('r', G_STR),
("struct_pwent", 'pw_gid'): ('r', G_INT),
("struct_pwent", 'pw_passwd'): ('r', G_STR),
("struct_pwent", 'pw_gecos'): ('r', G_STR),
("struct_pwent", 'pw_shell'): ('r', G_STR),
("struct_pwent", 'pw_name'): ('r', G_STR),
("struct_pwent", 'pw_uid'): ('r', G_INT),
("struct_passwd", 'pw_dir'): ('r', G_STR),
("struct_passwd", 'pw_gid'): ('r', G_INT),
("struct_passwd", 'pw_passwd'): ('r', G_STR),
("struct_passwd", 'pw_gecos'): ('r', G_STR),
("struct_passwd", 'pw_shell'): ('r', G_STR),
("struct_passwd", 'pw_name'): ('r', G_STR),
("struct_passwd", 'pw_uid'): ('r', G_INT),
},
"thread": {
("_local", '__dict__'): None
},
"xxsubtype": {
("spamdict", 'state'): ('r', G_INT),
("spamlist", 'state'): ('r', G_INT),
},
"zipimport": {
("zipimporter", 'prefix'): ('r', G_STR),
("zipimporter", 'archive'): ('r', G_STR),
("zipimporter", '_files'): ('r', G_DICT),
},
"_struct": {
("Struct", "size"): ('r', G_INT),
("Struct", "format"): ('r', G_STR),
},
datetime_mod: {
("datetime", "hour"): ('r', G_INT),
("datetime", "minute"): ('r', G_INT),
("datetime", "second"): ('r', G_INT),
("datetime", "microsecond"): ('r', G_INT),
("date", "day"): ('r', G_INT),
("date", "month"): ('r', G_INT),
("date", "year"): ('r', G_INT),
("time", "hour"): ('r', G_INT),
("time", "minute"): ('r', G_INT),
("time", "second"): ('r', G_INT),
("time", "microsecond"): ('r', G_INT),
("timedelta", "days"): ('r', G_INT),
("timedelta", "seconds"): ('r', G_INT),
("timedelta", "microseconds"): ('r', G_INT),
},
}
# Sometimes module X defines item foo but foo.__module__ == 'Y' instead of 'X';
# module Y just re-exports foo, and foo fakes being defined in Y.
# We list all such Ys keyed by X, all fully-qualified names:
# {"real_definer_module": ("fake_reexporter_module",..)}
KNOWN_FAKE_REEXPORTERS = {
"_collections": ('collections',),
"_functools": ('functools',),
"_socket": ('socket',), # .error, etc
"pyexpat": ('xml.parsers.expat',),
"_bsddb": ('bsddb.db',),
"pysqlite2._sqlite": ('pysqlite2.dbapi2',), # errors
"numpy.core.multiarray": ('numpy', 'numpy.core'),
"numpy.core._dotblas": ('numpy', 'numpy.core'),
"numpy.core.umath": ('numpy', 'numpy.core'),
"gtk._gtk": ('gtk', 'gtk.gdk',),
"gobject._gobject": ('gobject',),
"gnomecanvas": ("gnome.canvas",),
}
KNOWN_FAKE_BASES = []
# list of classes that pretend to be base classes but are mere wrappers, and their defining modules
# [(class, module),...] -- real objects, not names
#noinspection PyBroadException
try:
#noinspection PyUnresolvedReferences
import sip as sip_module # Qt specifically likes it
if hasattr(sip_module, 'wrapper'):
KNOWN_FAKE_BASES.append((sip_module.wrapper, sip_module))
if hasattr(sip_module, 'simplewrapper'):
KNOWN_FAKE_BASES.append((sip_module.simplewrapper, sip_module))
del sip_module
except:
pass
# This is a list of builtin classes to use fake init
FAKE_BUILTIN_INITS = (tuple, type, int, str)
if version[0] < 3:
FAKE_BUILTIN_INITS = FAKE_BUILTIN_INITS + (getattr(the_builtins, "unicode"),)
else:
FAKE_BUILTIN_INITS = FAKE_BUILTIN_INITS + (getattr(the_builtins, "str"), getattr(the_builtins, "bytes"))
# Some builtin methods are decorated, but this is hard to detect.
# {("class_name", "method_name"): "decorator"}
KNOWN_DECORATORS = {
("dict", "fromkeys"): "staticmethod",
("object", "__subclasshook__"): "classmethod",
("bytearray", "fromhex"): "classmethod",
("bytes", "fromhex"): "classmethod",
("bytearray", "maketrans"): "staticmethod",
("bytes", "maketrans"): "staticmethod",
("int", "from_bytes"): "classmethod",
}
classobj_txt = ( #TODO: user-skeleton
"class ___Classobj:" "\n"
" '''A mock class representing the old style class base.'''" "\n"
" __module__ = ''" "\n"
" __class__ = None" "\n"
"\n"
" def __init__(self):" "\n"
" pass" "\n"
" __dict__ = {}" "\n"
" __doc__ = ''" "\n"
)
MAC_STDLIB_PATTERN = re.compile("/System/Library/Frameworks/Python\\.framework/Versions/(.+)/lib/python\\1/(.+)")
MAC_SKIP_MODULES = ["test", "ctypes/test", "distutils/tests", "email/test",
"importlib/test", "json/tests", "lib2to3/tests",
"bsddb/test",
"sqlite3/test", "tkinter/test", "idlelib", "antigravity"]
POSIX_SKIP_MODULES = ["vtemodule", "PAMmodule", "_snackmodule", "/quodlibet/_mmkeys"]
BIN_MODULE_FNAME_PAT = re.compile('([a-zA-Z_]+[0-9a-zA-Z]*)\\.(?:pyc|pyo|(?:[a-zA-Z_]+-\\d\\d[a-zA-Z]*\\.|.+-linux-gnu\\.)?(?:so|pyd))')
# possible binary module filename: letter, alphanum architecture per PEP-3149
TYPELIB_MODULE_FNAME_PAT = re.compile("([a-zA-Z_]+[0-9a-zA-Z]*)[0-9a-zA-Z-.]*\\.typelib")
MODULES_INSPECT_DIR = ['gi.repository']

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,544 @@
from pycharm_generator_utils.constants import *
try:
import inspect
except ImportError:
inspect = None
def create_named_tuple(): #TODO: user-skeleton
return """
class __namedtuple(tuple):
'''A mock base class for named tuples.'''
__slots__ = ()
_fields = ()
def __new__(cls, *args, **kwargs):
'Create a new instance of the named tuple.'
return tuple.__new__(cls, *args)
@classmethod
def _make(cls, iterable, new=tuple.__new__, len=len):
'Make a new named tuple object from a sequence or iterable.'
return new(cls, iterable)
def __repr__(self):
return ''
def _asdict(self):
'Return a new dict which maps field types to their values.'
return {}
def _replace(self, **kwargs):
'Return a new named tuple object replacing specified fields with new values.'
return self
def __getnewargs__(self):
return tuple(self)
"""
def create_generator():
# Fake <type 'generator'>
if version[0] < 3:
next_name = "next"
else:
next_name = "__next__"
txt = """
class __generator(object):
'''A mock class representing the generator function type.'''
def __init__(self):
self.gi_code = None
self.gi_frame = None
self.gi_running = 0
def __iter__(self):
'''Defined to support iteration over container.'''
pass
def %s(self):
'''Return the next item from the container.'''
pass
""" % (next_name,)
if version[0] >= 3 or (version[0] == 2 and version[1] >= 5):
txt += """
def close(self):
'''Raises new GeneratorExit exception inside the generator to terminate the iteration.'''
pass
def send(self, value):
'''Resumes the generator and "sends" a value that becomes the result of the current yield-expression.'''
pass
def throw(self, type, value=None, traceback=None):
'''Used to raise an exception inside the generator.'''
pass
"""
return txt
def _searchbases(cls, accum):
# logic copied from inspect.py
if cls not in accum:
accum.append(cls)
for x in cls.__bases__:
_searchbases(x, accum)
def get_mro(a_class):
# logic copied from inspect.py
"""Returns a tuple of MRO classes."""
if hasattr(a_class, "__mro__"):
return a_class.__mro__
elif hasattr(a_class, "__bases__"):
bases = []
_searchbases(a_class, bases)
return tuple(bases)
else:
return tuple()
def get_bases(a_class): # TODO: test for classes that don't fit this scheme
"""Returns a sequence of class's bases."""
if hasattr(a_class, "__bases__"):
return a_class.__bases__
else:
return ()
def is_callable(x):
return hasattr(x, '__call__')
def sorted_no_case(p_array):
"""Sort an array case insensitevely, returns a sorted copy"""
p_array = list(p_array)
p_array = sorted(p_array, key=lambda x: x.upper())
return p_array
def cleanup(value):
result = []
prev = i = 0
length = len(value)
last_ascii = chr(127)
while i < length:
char = value[i]
replacement = None
if char == '\n':
replacement = '\\n'
elif char == '\r':
replacement = '\\r'
elif char < ' ' or char > last_ascii:
replacement = '?' # NOTE: such chars are rare; long swaths could be precessed differently
if replacement:
result.append(value[prev:i])
result.append(replacement)
i += 1
return "".join(result)
_prop_types = [type(property())]
#noinspection PyBroadException
try:
_prop_types.append(types.GetSetDescriptorType)
except:
pass
#noinspection PyBroadException
try:
_prop_types.append(types.MemberDescriptorType)
except:
pass
_prop_types = tuple(_prop_types)
def is_property(x):
return isinstance(x, _prop_types)
def sanitize_ident(x, is_clr=False):
"""Takes an identifier and returns it sanitized"""
if x in ("class", "object", "def", "list", "tuple", "int", "float", "str", "unicode" "None"):
return "p_" + x
else:
if is_clr:
# it tends to have names like "int x", turn it to just x
xs = x.split(" ")
if len(xs) == 2:
return sanitize_ident(xs[1])
return x.replace("-", "_").replace(" ", "_").replace(".", "_") # for things like "list-or-tuple" or "list or tuple"
def reliable_repr(value):
# some subclasses of built-in types (see PyGtk) may provide invalid __repr__ implementations,
# so we need to sanitize the output
if type(bool) == type and isinstance(value, bool):
return repr(bool(value))
for num_type in NUM_TYPES:
if isinstance(value, num_type):
return repr(num_type(value))
return repr(value)
def sanitize_value(p_value):
"""Returns p_value or its part if it represents a sane simple value, else returns 'None'"""
if isinstance(p_value, STR_TYPES):
match = SIMPLE_VALUE_RE.match(p_value)
if match:
return match.groups()[match.lastindex - 1]
else:
return 'None'
elif isinstance(p_value, NUM_TYPES):
return reliable_repr(p_value)
elif p_value is None:
return 'None'
else:
if hasattr(p_value, "__name__") and hasattr(p_value, "__module__") and p_value.__module__ == BUILTIN_MOD_NAME:
return p_value.__name__ # float -> "float"
else:
return repr(repr(p_value)) # function -> "<function ...>", etc
def extract_alpha_prefix(p_string, default_prefix="some"):
"""Returns 'foo' for things like 'foo1' or 'foo2'; if prefix cannot be found, the default is returned"""
match = NUM_IDENT_PATTERN.match(p_string)
prefix = match and match.groups()[match.lastindex - 1] or None
return prefix or default_prefix
def report(msg, *data):
"""Say something at error level (stderr)"""
sys.stderr.write(msg % data)
sys.stderr.write("\n")
def say(msg, *data):
"""Say something at info level (stdout)"""
sys.stdout.write(msg % data)
sys.stdout.write("\n")
def transform_seq(results, toplevel=True):
"""Transforms a tree of ParseResults into a param spec string."""
is_clr = sys.platform == "cli"
ret = [] # add here token to join
for token in results:
token_type = token[0]
if token_type is T_SIMPLE:
token_name = token[1]
if len(token) == 3: # name with value
if toplevel:
ret.append(sanitize_ident(token_name, is_clr) + "=" + sanitize_value(token[2]))
else:
# smth like "a, (b1=1, b2=2)", make it "a, p_b"
return ["p_" + results[0][1]] # NOTE: for each item of tuple, return the same name of its 1st item.
elif token_name == TRIPLE_DOT:
if toplevel and not has_item_starting_with(ret, "*"):
ret.append("*more")
else:
# we're in a "foo, (bar1, bar2, ...)"; make it "foo, bar_tuple"
return extract_alpha_prefix(results[0][1]) + "_tuple"
else: # just name
ret.append(sanitize_ident(token_name, is_clr))
elif token_type is T_NESTED:
inner = transform_seq(token[1:], False)
if len(inner) != 1:
ret.append(inner)
else:
ret.append(inner[0]) # [foo] -> foo
elif token_type is T_OPTIONAL:
ret.extend(transform_optional_seq(token))
elif token_type is T_RETURN:
pass # this is handled elsewhere
else:
raise Exception("This cannot be a token type: " + repr(token_type))
return ret
def transform_optional_seq(results):
"""
Produces a string that describes the optional part of parameters.
@param results must start from T_OPTIONAL.
"""
assert results[0] is T_OPTIONAL, "transform_optional_seq expects a T_OPTIONAL node, sees " + \
repr(results[0])
is_clr = sys.platform == "cli"
ret = []
for token in results[1:]:
token_type = token[0]
if token_type is T_SIMPLE:
token_name = token[1]
if len(token) == 3: # name with value; little sense, but can happen in a deeply nested optional
ret.append(sanitize_ident(token_name, is_clr) + "=" + sanitize_value(token[2]))
elif token_name == '...':
# we're in a "foo, [bar, ...]"; make it "foo, *bar"
return ["*" + extract_alpha_prefix(
results[1][1])] # we must return a seq; [1] is first simple, [1][1] is its name
else: # just name
ret.append(sanitize_ident(token_name, is_clr) + "=None")
elif token_type is T_OPTIONAL:
ret.extend(transform_optional_seq(token))
# maybe handle T_NESTED if such cases ever occur in real life
# it can't be nested in a sane case, really
return ret
def flatten(seq):
"""Transforms tree lists like ['a', ['b', 'c'], 'd'] to strings like '(a, (b, c), d)', enclosing each tree level in parens."""
ret = []
for one in seq:
if type(one) is list:
ret.append(flatten(one))
else:
ret.append(one)
return "(" + ", ".join(ret) + ")"
def make_names_unique(seq, name_map=None):
"""
Returns a copy of tree list seq where all clashing names are modified by numeric suffixes:
['a', 'b', 'a', 'b'] becomes ['a', 'b', 'a_1', 'b_1'].
Each repeating name has its own counter in the name_map.
"""
ret = []
if not name_map:
name_map = {}
for one in seq:
if type(one) is list:
ret.append(make_names_unique(one, name_map))
else:
one_key = lstrip(one, "*") # starred parameters are unique sans stars
if one_key in name_map:
old_one = one_key
one = one + "_" + str(name_map[old_one])
name_map[old_one] += 1
else:
name_map[one_key] = 1
ret.append(one)
return ret
def has_item_starting_with(p_seq, p_start):
for item in p_seq:
if isinstance(item, STR_TYPES) and item.startswith(p_start):
return True
return False
def out_docstring(out_func, docstring, indent):
if not isinstance(docstring, str): return
lines = docstring.strip().split("\n")
if lines:
if len(lines) == 1:
out_func(indent, '""" ' + lines[0] + ' """')
else:
out_func(indent, '"""')
for line in lines:
try:
out_func(indent, line)
except UnicodeEncodeError:
continue
out_func(indent, '"""')
def out_doc_attr(out_func, p_object, indent, p_class=None):
the_doc = getattr(p_object, "__doc__", None)
if the_doc:
if p_class and the_doc == object.__init__.__doc__ and p_object is not object.__init__ and p_class.__doc__:
the_doc = str(p_class.__doc__) # replace stock init's doc with class's; make it a certain string.
the_doc += "\n# (copied from class doc)"
out_docstring(out_func, the_doc, indent)
else:
out_func(indent, "# no doc")
def is_skipped_in_module(p_module, p_value):
"""
Returns True if p_value's value must be skipped for module p_module.
"""
skip_list = SKIP_VALUE_IN_MODULE.get(p_module, [])
if p_value in skip_list:
return True
skip_list = SKIP_VALUE_IN_MODULE.get("*", [])
if p_value in skip_list:
return True
return False
def restore_predefined_builtin(class_name, func_name):
spec = func_name + PREDEFINED_BUILTIN_SIGS[(class_name, func_name)]
note = "known special case of " + (class_name and class_name + "." or "") + func_name
return (spec, note)
def restore_by_inspect(p_func):
"""
Returns paramlist restored by inspect.
"""
args, varg, kwarg, defaults = inspect.getargspec(p_func)
spec = []
if defaults:
dcnt = len(defaults) - 1
else:
dcnt = -1
args = args or []
args.reverse() # backwards, for easier defaults handling
for arg in args:
if dcnt >= 0:
arg += "=" + sanitize_value(defaults[dcnt])
dcnt -= 1
spec.insert(0, arg)
if varg:
spec.append("*" + varg)
if kwarg:
spec.append("**" + kwarg)
return flatten(spec)
def restore_parameters_for_overloads(parameter_lists):
param_index = 0
star_args = False
optional = False
params = []
while True:
parameter_lists_copy = [pl for pl in parameter_lists]
for pl in parameter_lists_copy:
if param_index >= len(pl):
parameter_lists.remove(pl)
optional = True
if not parameter_lists:
break
name = parameter_lists[0][param_index]
for pl in parameter_lists[1:]:
if pl[param_index] != name:
star_args = True
break
if star_args: break
if optional and not '=' in name:
params.append(name + '=None')
else:
params.append(name)
param_index += 1
if star_args:
params.append("*__args")
return params
def build_signature(p_name, params):
return p_name + '(' + ', '.join(params) + ')'
def propose_first_param(deco):
"""@return: name of missing first paramater, considering a decorator"""
if deco is None:
return "self"
if deco == "classmethod":
return "cls"
# if deco == "staticmethod":
return None
def qualifier_of(cls, qualifiers_to_skip):
m = getattr(cls, "__module__", None)
if m in qualifiers_to_skip:
return ""
return m
def handle_error_func(item_name, out):
exctype, value = sys.exc_info()[:2]
msg = "Error generating skeleton for function %s: %s"
args = item_name, value
report(msg, *args)
out(0, "# " + msg % args)
out(0, "")
def format_accessors(accessor_line, getter, setter, deleter):
"""Nicely format accessors, like 'getter, fdel=deleter'"""
ret = []
consecutive = True
for key, arg, par in (('r', 'fget', getter), ('w', 'fset', setter), ('d', 'fdel', deleter)):
if key in accessor_line:
if consecutive:
ret.append(par)
else:
ret.append(arg + "=" + par)
else:
consecutive = False
return ", ".join(ret)
def has_regular_python_ext(file_name):
"""Does name end with .py?"""
return file_name.endswith(".py")
# Note that the standard library on MacOS X 10.6 is shipped only as .pyc files, so we need to
# have them processed by the generator in order to have any code insight for the standard library.
def detect_constructor(p_class):
# try to inspect the thing
constr = getattr(p_class, "__init__")
if constr and inspect and inspect.isfunction(constr):
args, _, _, _ = inspect.getargspec(constr)
return ", ".join(args)
else:
return None
############## notes, actions #################################################################
_is_verbose = False # controlled by -v
CURRENT_ACTION = "nothing yet"
def action(msg, *data):
global CURRENT_ACTION
CURRENT_ACTION = msg % data
note(msg, *data)
def note(msg, *data):
"""Say something at debug info level (stderr)"""
global _is_verbose
if _is_verbose:
sys.stderr.write(msg % data)
sys.stderr.write("\n")
############## plaform-specific methods #######################################################
import sys
if sys.platform == 'cli':
#noinspection PyUnresolvedReferences
import clr
# http://blogs.msdn.com/curth/archive/2009/03/29/an-ironpython-profiler.aspx
def print_profile():
data = []
data.extend(clr.GetProfilerData())
data.sort(lambda x, y: -cmp(x.ExclusiveTime, y.ExclusiveTime))
for pd in data:
say('%s\t%d\t%d\t%d', pd.Name, pd.InclusiveTime, pd.ExclusiveTime, pd.Calls)
def is_clr_type(clr_type):
if not clr_type: return False
try:
clr.GetClrType(clr_type)
return True
except TypeError:
return False
def restore_clr(p_name, p_class):
"""
Restore the function signature by the CLR type signature
"""
clr_type = clr.GetClrType(p_class)
if p_name == '__new__':
methods = [c for c in clr_type.GetConstructors()]
if not methods:
return p_name + '(*args)', 'cannot find CLR constructor'
else:
methods = [m for m in clr_type.GetMethods() if m.Name == p_name]
if not methods:
bases = p_class.__bases__
if len(bases) == 1 and p_name in dir(bases[0]):
# skip inherited methods
return None, None
return p_name + '(*args)', 'cannot find CLR method'
parameter_lists = []
for m in methods:
parameter_lists.append([p.Name for p in m.GetParameters()])
params = restore_parameters_for_overloads(parameter_lists)
if not methods[0].IsStatic:
params = ['self'] + params
return build_signature(p_name, params), None

View File

@@ -23,154 +23,154 @@ class TestRestoreFuncByDocComment(unittest.TestCase):
self.m = ModuleRedeclarator(None, None, '/dev/null')
def testTrivial(self):
result, ret_sig, note = self.m.parseFuncDoc("blah f(a, b, c) ololo", "f", "f", None)
result, ret_sig, note = self.m.parse_func_doc("blah f(a, b, c) ololo", "f", "f", None)
self.assertEquals(result, "f(a, b, c)")
self.assertEquals(note, M.SIG_DOC_NOTE)
def testTrivialNested(self):
result, ret_sig, note = self.m.parseFuncDoc("blah f(a, (b, c), d) ololo", "f", "f", None)
result, ret_sig, note = self.m.parse_func_doc("blah f(a, (b, c), d) ololo", "f", "f", None)
self.assertEquals(result, "f(a, (b, c), d)")
self.assertEquals(note, M.SIG_DOC_NOTE)
def testWithDefault(self):
result, ret_sig, note = self.m.parseFuncDoc("blah f(a, b, c=1) ololo", "f", "f", None)
result, ret_sig, note = self.m.parse_func_doc("blah f(a, b, c=1) ololo", "f", "f", None)
self.assertEquals(result, "f(a, b, c=1)")
self.assertEquals(note, M.SIG_DOC_NOTE)
def testNestedWithDefault(self):
result, ret_sig, note = self.m.parseFuncDoc("blah f(a, (b1, b2), c=1) ololo", "f", "f", None)
result, ret_sig, note = self.m.parse_func_doc("blah f(a, (b1, b2), c=1) ololo", "f", "f", None)
self.assertEquals(result, "f(a, (b1, b2), c=1)")
self.assertEquals(note, M.SIG_DOC_NOTE)
def testAbstractDefault(self):
# like new(S, ...)
result, ret_sig, note = self.m.parseFuncDoc('blah f(a, b=obscuredefault) ololo', "f", "f", None)
result, ret_sig, note = self.m.parse_func_doc('blah f(a, b=obscuredefault) ololo', "f", "f", None)
self.assertEquals(result, "f(a, b=None)")
self.assertEquals(note, M.SIG_DOC_NOTE)
def testWithReserved(self):
result, ret_sig, note = self.m.parseFuncDoc("blah f(class, object, def) ololo", "f", "f", None)
result, ret_sig, note = self.m.parse_func_doc("blah f(class, object, def) ololo", "f", "f", None)
self.assertEquals(result, "f(p_class, p_object, p_def)")
self.assertEquals(note, M.SIG_DOC_NOTE)
def testWithReservedOpt(self):
result, ret_sig, note = self.m.parseFuncDoc("blah f(foo, bar[, def]) ololo", "f", "f", None)
result, ret_sig, note = self.m.parse_func_doc("blah f(foo, bar[, def]) ololo", "f", "f", None)
self.assertEquals(result, "f(foo, bar, p_def=None)")
self.assertEquals(note, M.SIG_DOC_NOTE)
def testPseudoNested(self):
result, ret_sig, note = self.m.parseFuncDoc("blah f(a, (b1, b2, ...)) ololo", "f", "f", None)
result, ret_sig, note = self.m.parse_func_doc("blah f(a, (b1, b2, ...)) ololo", "f", "f", None)
self.assertEquals(result, "f(a, b_tuple)")
self.assertEquals(note, M.SIG_DOC_NOTE)
def testImportLike(self):
# __import__
result, ret_sig, note = self.m.parseFuncDoc("blah f(name, globals={}, locals={}, fromlist=[], level=-1) ololo",
"f", "f", None)
result, ret_sig, note = self.m.parse_func_doc("blah f(name, globals={}, locals={}, fromlist=[], level=-1) ololo",
"f", "f", None)
self.assertEquals(result, "f(name, globals={}, locals={}, fromlist=[], level=-1)")
self.assertEquals(note, M.SIG_DOC_NOTE)
def testOptionalBracket(self):
# reduce
result, ret_sig, note = self.m.parseFuncDoc("blah f(function, sequence[, initial]) ololo", "f", "f", None)
result, ret_sig, note = self.m.parse_func_doc("blah f(function, sequence[, initial]) ololo", "f", "f", None)
self.assertEquals(result, "f(function, sequence, initial=None)")
self.assertEquals(note, M.SIG_DOC_NOTE)
def testWithMore(self):
result, ret_sig, note = self.m.parseFuncDoc("blah f(foo [, bar1, bar2, ...]) ololo", "f", "f", None)
result, ret_sig, note = self.m.parse_func_doc("blah f(foo [, bar1, bar2, ...]) ololo", "f", "f", None)
self.assertEquals(result, "f(foo, *bar)")
self.assertEquals(note, M.SIG_DOC_NOTE)
def testNestedOptionals(self):
result, ret_sig, note = self.m.parseFuncDoc("blah f(foo [, bar1 [, bar2]]) ololo", "f", "f", None)
result, ret_sig, note = self.m.parse_func_doc("blah f(foo [, bar1 [, bar2]]) ololo", "f", "f", None)
self.assertEquals(result, "f(foo, bar1=None, bar2=None)")
self.assertEquals(note, M.SIG_DOC_NOTE)
def testInnerTuple(self):
result, ret_sig, note = self.m.parseFuncDoc("blah load_module(name, file, filename, (suffix, mode, type)) ololo"
, "load_module", "load_module", None)
result, ret_sig, note = self.m.parse_func_doc("blah load_module(name, file, filename, (suffix, mode, type)) ololo"
, "load_module", "load_module", None)
self.assertEquals(result, "load_module(name, file, filename, (suffix, mode, type))")
self.assertEquals(note, M.SIG_DOC_NOTE)
def testIncorrectInnerTuple(self):
result, ret_sig, note = self.m.parseFuncDoc("blah f(a, (b=1, c=2)) ololo", "f", "f", None)
result, ret_sig, note = self.m.parse_func_doc("blah f(a, (b=1, c=2)) ololo", "f", "f", None)
self.assertEquals(result, "f(a, p_b)")
self.assertEquals(note, M.SIG_DOC_NOTE)
def testNestedOnly(self):
result, ret_sig, note = self.m.parseFuncDoc("blah f((foo, bar, baz)) ololo", "f", "f", None)
result, ret_sig, note = self.m.parse_func_doc("blah f((foo, bar, baz)) ololo", "f", "f", None)
self.assertEquals(result, "f((foo, bar, baz))")
self.assertEquals(note, M.SIG_DOC_NOTE)
def testTwoPseudoNested(self):
result, ret_sig, note = self.m.parseFuncDoc("blah f((a1, a2, ...), (b1, b2,..)) ololo", "f", "f", None)
result, ret_sig, note = self.m.parse_func_doc("blah f((a1, a2, ...), (b1, b2,..)) ololo", "f", "f", None)
self.assertEquals(result, "f(a_tuple, b_tuple)")
self.assertEquals(note, M.SIG_DOC_NOTE)
def testTwoPseudoNestedWithLead(self):
result, ret_sig, note = self.m.parseFuncDoc("blah f(x, (a1, a2, ...), (b1, b2,..)) ololo", "f", "f", None)
result, ret_sig, note = self.m.parse_func_doc("blah f(x, (a1, a2, ...), (b1, b2,..)) ololo", "f", "f", None)
self.assertEquals(result, "f(x, a_tuple, b_tuple)")
self.assertEquals(note, M.SIG_DOC_NOTE)
def testPseudoNestedRange(self):
result, ret_sig, note = self.m.parseFuncDoc("blah f((a1, ..., an), b) ololo", "f", "f", None)
result, ret_sig, note = self.m.parse_func_doc("blah f((a1, ..., an), b) ololo", "f", "f", None)
self.assertEquals(result, "f(a_tuple, b)")
self.assertEquals(note, M.SIG_DOC_NOTE)
def testIncorrectList(self):
result, ret_sig, note = self.m.parseFuncDoc("blah f(x, y, 3, $) ololo", "f", "f", None)
result, ret_sig, note = self.m.parse_func_doc("blah f(x, y, 3, $) ololo", "f", "f", None)
self.assertEquals(result, "f(x, y, *args, **kwargs)")
self.assertEquals(note, M.SIG_DOC_UNRELIABLY)
def testIncorrectStarredList(self):
result, ret_sig, note = self.m.parseFuncDoc("blah f(x, *y, 3, $) ololo", "f", "f", None)
result, ret_sig, note = self.m.parse_func_doc("blah f(x, *y, 3, $) ololo", "f", "f", None)
self.assertEquals(result, "f(x, *y, **kwargs)")
self.assertEquals(note, M.SIG_DOC_UNRELIABLY)
def testClashingNames(self):
result, ret_sig, note = self.m.parseFuncDoc("blah f(x, y, (x, y), z) ololo", "f", "f", None)
result, ret_sig, note = self.m.parse_func_doc("blah f(x, y, (x, y), z) ololo", "f", "f", None)
self.assertEquals(result, "f(x, y, (x_1, y_1), z)")
self.assertEquals(note, M.SIG_DOC_NOTE)
def testQuotedParam(self):
# like __delattr__
result, ret_sig, note = self.m.parseFuncDoc("blah getattr('name') ololo", "getattr", "getattr", None)
result, ret_sig, note = self.m.parse_func_doc("blah getattr('name') ololo", "getattr", "getattr", None)
self.assertEquals(result, "getattr(name)")
self.assertEquals(note, M.SIG_DOC_NOTE)
def testQuotedParam2(self):
# like __delattr__, too
result, ret_sig, note = self.m.parseFuncDoc('blah getattr("name") ololo', "getattr", "getattr", None)
result, ret_sig, note = self.m.parse_func_doc('blah getattr("name") ololo', "getattr", "getattr", None)
self.assertEquals(result, "getattr(name)")
self.assertEquals(note, M.SIG_DOC_NOTE)
def testOptionalTripleDot(self):
# like new(S, ...)
result, ret_sig, note = self.m.parseFuncDoc('blah f(foo, ...) ololo', "f", "f", None)
result, ret_sig, note = self.m.parse_func_doc('blah f(foo, ...) ololo', "f", "f", None)
self.assertEquals(result, "f(foo, *more)")
self.assertEquals(note, M.SIG_DOC_NOTE)
def testUnderscoredName(self):
# like new(S, ...)
result, ret_sig, note = self.m.parseFuncDoc('blah f(foo_one, _bar_two) ololo', "f", "f", None)
result, ret_sig, note = self.m.parse_func_doc('blah f(foo_one, _bar_two) ololo', "f", "f", None)
self.assertEquals(result, "f(foo_one, _bar_two)")
self.assertEquals(note, M.SIG_DOC_NOTE)
def testDashedName(self):
# like new(S, ...)
result, ret_sig, note = self.m.parseFuncDoc('blah f(something-else, for-a-change) ololo', "f", "f", None)
result, ret_sig, note = self.m.parse_func_doc('blah f(something-else, for-a-change) ololo', "f", "f", None)
self.assertEquals(result, "f(something_else, for_a_change)")
self.assertEquals(note, M.SIG_DOC_NOTE)
def testSpacedDefault(self):
# like new(S, ...)
result, ret_sig, note = self.m.parseFuncDoc('blah f(a, b = 1) ololo', "f", "f", None)
result, ret_sig, note = self.m.parse_func_doc('blah f(a, b = 1) ololo', "f", "f", None)
self.assertEquals(result, "f(a, b=1)")
self.assertEquals(note, M.SIG_DOC_NOTE)
def testSpacedName(self):
# like new(S, ...)
result, ret_sig, note = self.m.parseFuncDoc('blah femme(skirt or pants) ololo', "femme", "femme", None)
result, ret_sig, note = self.m.parse_func_doc('blah femme(skirt or pants) ololo', "femme", "femme", None)
self.assertEquals(result, "femme(skirt_or_pants)")
self.assertEquals(note, M.SIG_DOC_NOTE)
@@ -184,12 +184,12 @@ class TestRestoreMethodByDocComment(unittest.TestCase):
self.m = ModuleRedeclarator(None, None, '/dev/null')
def testPlainMethod(self):
result, ret_sig, note = self.m.parseFuncDoc("blah f(self, foo, bar) ololo", "f", "f", "SomeClass")
result, ret_sig, note = self.m.parse_func_doc("blah f(self, foo, bar) ololo", "f", "f", "SomeClass")
self.assertEquals(result, "f(self, foo, bar)")
self.assertEquals(note, M.SIG_DOC_NOTE)
def testInsertSelf(self):
result, ret_sig, note = self.m.parseFuncDoc("blah f(foo, bar) ololo", "f", "f", "SomeClass")
result, ret_sig, note = self.m.parse_func_doc("blah f(foo, bar) ololo", "f", "f", "SomeClass")
self.assertEquals(result, "f(self, foo, bar)")
self.assertEquals(note, M.SIG_DOC_NOTE)
@@ -203,17 +203,17 @@ class TestAnnotatedParameters(unittest.TestCase):
self.m = ModuleRedeclarator(None, None, '/dev/null')
def testMixed(self):
result, ret_sig, note = self.m.parseFuncDoc('blah f(i: int, foo) ololo', "f", "f", None)
result, ret_sig, note = self.m.parse_func_doc('blah f(i: int, foo) ololo', "f", "f", None)
self.assertEquals(result, "f(i, foo)")
self.assertEquals(note, M.SIG_DOC_NOTE)
def testNested(self):
result, ret_sig, note = self.m.parseFuncDoc('blah f(i: int, (foo: bar, boo: Decimal)) ololo', "f", "f", None)
result, ret_sig, note = self.m.parse_func_doc('blah f(i: int, (foo: bar, boo: Decimal)) ololo', "f", "f", None)
self.assertEquals(result, "f(i, (foo, boo))")
self.assertEquals(note, M.SIG_DOC_NOTE)
def testSpaced(self):
result, ret_sig, note = self.m.parseFuncDoc('blah f(i: int, j :int, k : int) ololo', "f", "f", None)
result, ret_sig, note = self.m.parse_func_doc('blah f(i: int, j :int, k : int) ololo', "f", "f", None)
self.assertEquals(result, "f(i, j, k)")
self.assertEquals(note, M.SIG_DOC_NOTE)
@@ -231,20 +231,20 @@ if not IS_CLI and VERSION < (3, 0):
def target(a, b, c=1, *d, **e):
return a, b, c, d, e
result = self.m.restoreByInspect(target)
result = restore_by_inspect(target)
self.assertEquals(result, "(a, b, c=1, *d, **e)")
def testNested(self):
# NOTE: Py3k can't handle nested tuple args, thus we compile it conditionally
code = (
"def target(a, (b, c), d, e=1):\n"
" return a, b, c, d, e"
"def target(a, (b, c), d, e=1):\n"
" return a, b, c, d, e"
)
namespace = {}
eval(compile(code, "__main__", "single"), namespace)
target = namespace['target']
result = self.m.restoreByInspect(target)
result = restore_by_inspect(target)
self.assertEquals(result, "(a, (b, c), d, e=1)")
class _DiffPrintingTestCase(unittest.TestCase):
@@ -255,9 +255,9 @@ class _DiffPrintingTestCase(unittest.TestCase):
ei = iter(etalon.split("\n"))
si = iter(specimen.split("\n"))
if VERSION < (3, 0):
si_next = si.next
si_next = si.next
else:
si_next = si.__next__
si_next = si.__next__
for el in ei:
try: sl = si_next()
except StopIteration: break # I wish the exception would just work as break
@@ -266,7 +266,7 @@ class _DiffPrintingTestCase(unittest.TestCase):
print("?%s" % sl)
else:
print(">%s" % sl)
# one of the iters might not end yet
# one of the iters might not end yet
for el in ei:
print("!%s" % el)
for sl in si:
@@ -296,8 +296,8 @@ class TestSpecialCases(unittest.TestCase):
def _testBuiltinFuncName(self, func_name, expected):
class_name = None
self.assertTrue(self.m.isPredefinedBuiltin(self.builtins_name, class_name, func_name))
result, note = self.m.restorePredefinedBuiltin(class_name, func_name)
self.assertTrue(self.m.is_predefined_builtin(self.builtins_name, class_name, func_name))
result, note = restore_predefined_builtin(class_name, func_name)
self.assertEquals(result, func_name + expected)
self.assertEquals(note, "known special case of " + func_name)
@@ -310,7 +310,7 @@ class TestSpecialCases(unittest.TestCase):
def testFilter(self):
self._testBuiltinFuncName("filter", "(function_or_none, sequence)")
# we caould want to test a calss without __dict__, but it takes a C extension to really create one,
# we caould want to test a calss without __dict__, but it takes a C extension to really create one,
class TestDataOutput(_DiffPrintingTestCase):
"""
@@ -322,7 +322,7 @@ class TestDataOutput(_DiffPrintingTestCase):
def checkFmtValue(self, data, expected):
buf = Buf(self.m)
self.m.fmtValue(buf.out, data, 0)
self.m.fmt_value(buf.out, data, 0)
result = "".join(buf.data).strip()
self.assertEquals(expected, result)
@@ -366,7 +366,7 @@ if not IS_CLI:
self.m = ModuleRedeclarator(None, None, 4)
def checkRestoreFunction(self, doc, expected):
spec, ret_literal, note = self.m.parseFuncDoc(doc, "foo", "foo", None)
spec, ret_literal, note = self.m.parse_func_doc(doc, "foo", "foo", None)
self.assertEqual(expected, ret_literal, "%r != %r; spec=%r, note=%r" % (expected, ret_literal, spec, note))
pass
@@ -393,7 +393,7 @@ if not IS_CLI:
def testSimplePrefixObject(self):
doc = "Makes an instance: object foo(bar)"
self.checkRestoreFunction(doc, "object()")
if VERSION < (3, 0):
# TODO: we only support it in 2.x; must update when we do it in 3.x, too
def testSimpleArrowFile(self):