PageRenderTime 51ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/python/helpers/pycharm_generator_utils/constants.py

http://github.com/JetBrains/intellij-community
Python | 794 lines | 732 code | 38 blank | 24 comment | 16 complexity | a3f4cfc8dd298ee11c4ef2d61cd0dcc4 MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0, MPL-2.0-no-copyleft-exception, MIT, EPL-1.0, AGPL-1.0
  1. import os
  2. import re
  3. import types
  4. import sys
  5. import string
  6. import time
  7. # !!! Don't forget to update VERSION and required_gen_version if necessary !!!
  8. VERSION = "1.143"
  9. OUT_ENCODING = 'utf-8'
  10. version = (
  11. (sys.hexversion & (0xff << 24)) >> 24,
  12. (sys.hexversion & (0xff << 16)) >> 16
  13. )
  14. if version[0] >= 3:
  15. #noinspection PyUnresolvedReferences
  16. import builtins as the_builtins
  17. string = "".__class__
  18. STR_TYPES = (getattr(the_builtins, "bytes"), str)
  19. NUM_TYPES = (int, float)
  20. SIMPLEST_TYPES = NUM_TYPES + STR_TYPES + (None.__class__,)
  21. EASY_TYPES = NUM_TYPES + STR_TYPES + (None.__class__, dict, tuple, list)
  22. def the_exec(source, context):
  23. exec (source, context)
  24. else: # < 3.0
  25. import __builtin__ as the_builtins
  26. STR_TYPES = (getattr(the_builtins, "unicode"), str)
  27. NUM_TYPES = (int, long, float)
  28. SIMPLEST_TYPES = NUM_TYPES + STR_TYPES + (types.NoneType,)
  29. EASY_TYPES = NUM_TYPES + STR_TYPES + (types.NoneType, dict, tuple, list)
  30. def the_exec(source, context):
  31. #noinspection PyRedundantParentheses
  32. exec (source) in context
  33. if version[0] == 2 and version[1] < 4:
  34. HAS_DECORATORS = False
  35. def lstrip(s, prefix):
  36. i = 0
  37. while s[i] == prefix:
  38. i += 1
  39. return s[i:]
  40. else:
  41. HAS_DECORATORS = True
  42. lstrip = string.lstrip
  43. # return type inference helper table
  44. INT_LIT = '0'
  45. FLOAT_LIT = '0.0'
  46. DICT_LIT = '{}'
  47. LIST_LIT = '[]'
  48. TUPLE_LIT = '()'
  49. BOOL_LIT = 'False'
  50. RET_TYPE = {# {'type_name': 'value_string'} lookup table
  51. # chaining
  52. "self": "self",
  53. "self.": "self",
  54. # int
  55. "int": INT_LIT,
  56. "Int": INT_LIT,
  57. "integer": INT_LIT,
  58. "Integer": INT_LIT,
  59. "short": INT_LIT,
  60. "long": INT_LIT,
  61. "number": INT_LIT,
  62. "Number": INT_LIT,
  63. # float
  64. "float": FLOAT_LIT,
  65. "Float": FLOAT_LIT,
  66. "double": FLOAT_LIT,
  67. "Double": FLOAT_LIT,
  68. "floating": FLOAT_LIT,
  69. # boolean
  70. "bool": BOOL_LIT,
  71. "boolean": BOOL_LIT,
  72. "Bool": BOOL_LIT,
  73. "Boolean": BOOL_LIT,
  74. "True": BOOL_LIT,
  75. "true": BOOL_LIT,
  76. "False": BOOL_LIT,
  77. "false": BOOL_LIT,
  78. # list
  79. 'list': LIST_LIT,
  80. 'List': LIST_LIT,
  81. '[]': LIST_LIT,
  82. # tuple
  83. "tuple": TUPLE_LIT,
  84. "sequence": TUPLE_LIT,
  85. "Sequence": TUPLE_LIT,
  86. # dict
  87. "dict": DICT_LIT,
  88. "Dict": DICT_LIT,
  89. "dictionary": DICT_LIT,
  90. "Dictionary": DICT_LIT,
  91. "map": DICT_LIT,
  92. "Map": DICT_LIT,
  93. "hashtable": DICT_LIT,
  94. "Hashtable": DICT_LIT,
  95. "{}": DICT_LIT,
  96. # "objects"
  97. "object": "object()",
  98. }
  99. if version[0] < 3:
  100. UNICODE_LIT = 'u""'
  101. BYTES_LIT = '""'
  102. RET_TYPE.update({
  103. 'string': BYTES_LIT,
  104. 'String': BYTES_LIT,
  105. 'str': BYTES_LIT,
  106. 'Str': BYTES_LIT,
  107. 'character': BYTES_LIT,
  108. 'char': BYTES_LIT,
  109. 'unicode': UNICODE_LIT,
  110. 'Unicode': UNICODE_LIT,
  111. 'bytes': BYTES_LIT,
  112. 'byte': BYTES_LIT,
  113. 'Bytes': BYTES_LIT,
  114. 'Byte': BYTES_LIT,
  115. })
  116. DEFAULT_STR_LIT = BYTES_LIT
  117. # also, files:
  118. RET_TYPE.update({
  119. 'file': "file('/dev/null')",
  120. })
  121. def ensureUnicode(data):
  122. if type(data) == str:
  123. return data.decode(OUT_ENCODING, 'replace')
  124. return unicode(data)
  125. else:
  126. UNICODE_LIT = '""'
  127. BYTES_LIT = 'b""'
  128. RET_TYPE.update({
  129. 'string': UNICODE_LIT,
  130. 'String': UNICODE_LIT,
  131. 'str': UNICODE_LIT,
  132. 'Str': UNICODE_LIT,
  133. 'character': UNICODE_LIT,
  134. 'char': UNICODE_LIT,
  135. 'unicode': UNICODE_LIT,
  136. 'Unicode': UNICODE_LIT,
  137. 'bytes': BYTES_LIT,
  138. 'byte': BYTES_LIT,
  139. 'Bytes': BYTES_LIT,
  140. 'Byte': BYTES_LIT,
  141. })
  142. DEFAULT_STR_LIT = UNICODE_LIT
  143. # also, files: we can't provide an easy expression on py3k
  144. RET_TYPE.update({
  145. 'file': None,
  146. })
  147. def ensureUnicode(data):
  148. if type(data) == bytes:
  149. return data.decode(OUT_ENCODING, 'replace')
  150. return str(data)
  151. if version[0] > 2:
  152. import io # in 3.0
  153. #noinspection PyArgumentList
  154. fopen = lambda name, mode: io.open(name, mode, encoding=OUT_ENCODING)
  155. else:
  156. fopen = open
  157. if sys.platform == 'cli':
  158. #noinspection PyUnresolvedReferences
  159. from System import DateTime
  160. class Timer(object):
  161. def __init__(self):
  162. self.started = DateTime.Now
  163. def elapsed(self):
  164. return (DateTime.Now - self.started).TotalMilliseconds
  165. else:
  166. class Timer(object):
  167. def __init__(self):
  168. self.started = time.time()
  169. def elapsed(self):
  170. return int((time.time() - self.started) * 1000)
  171. IS_JAVA = hasattr(os, "java")
  172. BUILTIN_MOD_NAME = the_builtins.__name__
  173. IDENT_PATTERN = "[A-Za-z_][0-9A-Za-z_]*" # re pattern for identifier
  174. NUM_IDENT_PATTERN = re.compile("([A-Za-z_]+)[0-9]?[A-Za-z_]*") # 'foo_123' -> $1 = 'foo_'
  175. STR_CHAR_PATTERN = "[0-9A-Za-z_.,\+\-&\*% ]"
  176. DOC_FUNC_RE = re.compile("(?:.*\.)?(\w+)\(([^\)]*)\).*") # $1 = function name, $2 = arglist
  177. SANE_REPR_RE = re.compile(IDENT_PATTERN + "(?:\(.*\))?") # identifier with possible (...), go catches
  178. IDENT_RE = re.compile("(" + IDENT_PATTERN + ")") # $1 = identifier
  179. STARS_IDENT_RE = re.compile("(\*?\*?" + IDENT_PATTERN + ")") # $1 = identifier, maybe with a * or **
  180. IDENT_EQ_RE = re.compile("(" + IDENT_PATTERN + "\s*=)") # $1 = identifier with a following '='
  181. SIMPLE_VALUE_RE = re.compile(
  182. "(\([+-]?[0-9](?:\s*,\s*[+-]?[0-9])*\))|" + # a numeric tuple, e.g. in pygame
  183. "([+-]?[0-9]+\.?[0-9]*(?:[Ee]?[+-]?[0-9]+\.?[0-9]*)?)|" + # number
  184. "('" + STR_CHAR_PATTERN + "*')|" + # single-quoted string
  185. '("' + STR_CHAR_PATTERN + '*")|' + # double-quoted string
  186. "(\[\])|" +
  187. "(\{\})|" +
  188. "(\(\))|" +
  189. "(True|False|None)"
  190. ) # $? = sane default value
  191. ########################### parsing ###########################################################
  192. if version[0] < 3:
  193. from pycharm_generator_utils.pyparsing import *
  194. else:
  195. #noinspection PyUnresolvedReferences
  196. from pycharm_generator_utils.pyparsing_py3 import *
  197. # grammar to parse parameter lists
  198. # // snatched from parsePythonValue.py, from pyparsing samples, copyright 2006 by Paul McGuire but under BSD license.
  199. # we don't suppress lots of punctuation because we want it back when we reconstruct the lists
  200. lparen, rparen, lbrack, rbrack, lbrace, rbrace, colon = map(Literal, "()[]{}:")
  201. integer = Combine(Optional(oneOf("+ -")) + Word(nums)).setName("integer")
  202. real = Combine(Optional(oneOf("+ -")) + Word(nums) + "." +
  203. Optional(Word(nums)) +
  204. Optional(oneOf("e E") + Optional(oneOf("+ -")) + Word(nums))).setName("real")
  205. tupleStr = Forward()
  206. listStr = Forward()
  207. dictStr = Forward()
  208. boolLiteral = oneOf("True False")
  209. noneLiteral = Literal("None")
  210. listItem = real | integer | quotedString | unicodeString | boolLiteral | noneLiteral | \
  211. Group(listStr) | tupleStr | dictStr
  212. tupleStr << ( Suppress("(") + Optional(delimitedList(listItem)) +
  213. Optional(Literal(",")) + Suppress(")") ).setResultsName("tuple")
  214. listStr << (lbrack + Optional(delimitedList(listItem) +
  215. Optional(Literal(","))) + rbrack).setResultsName("list")
  216. dictEntry = Group(listItem + colon + listItem)
  217. dictStr << (lbrace + Optional(delimitedList(dictEntry) + Optional(Literal(","))) + rbrace).setResultsName("dict")
  218. # \\ end of the snatched part
  219. # our output format is s-expressions:
  220. # (simple name optional_value) is name or name=value
  221. # (nested (simple ...) (simple ...)) is (name, name,...)
  222. # (opt ...) is [, ...] or suchlike.
  223. T_SIMPLE = 'Simple'
  224. T_NESTED = 'Nested'
  225. T_OPTIONAL = 'Opt'
  226. T_RETURN = "Ret"
  227. TRIPLE_DOT = '...'
  228. COMMA = Suppress(",")
  229. APOS = Suppress("'")
  230. QUOTE = Suppress('"')
  231. SP = Suppress(Optional(White()))
  232. ident = Word(alphas + "_", alphanums + "_-.").setName("ident") # we accept things like "foo-or-bar"
  233. decorated_ident = ident + Optional(Suppress(SP + Literal(":") + SP + ident)) # accept "foo: bar", ignore "bar"
  234. spaced_ident = Combine(decorated_ident + ZeroOrMore(Literal(' ') + decorated_ident)) # we accept 'list or tuple' or 'C struct'
  235. # allow quoted names, because __setattr__, etc docs use it
  236. paramname = spaced_ident | \
  237. APOS + spaced_ident + APOS | \
  238. QUOTE + spaced_ident + QUOTE
  239. parenthesized_tuple = ( Literal("(") + Optional(delimitedList(listItem, combine=True)) +
  240. Optional(Literal(",")) + Literal(")") ).setResultsName("(tuple)")
  241. initializer = (SP + Suppress("=") + SP + Combine(parenthesized_tuple | listItem | ident)).setName("=init") # accept foo=defaultfoo
  242. param = Group(Empty().setParseAction(replaceWith(T_SIMPLE)) + Combine(Optional(oneOf("* **")) + paramname) + Optional(initializer))
  243. ellipsis = Group(
  244. Empty().setParseAction(replaceWith(T_SIMPLE)) + \
  245. (Literal("..") +
  246. ZeroOrMore(Literal('.'))).setParseAction(replaceWith(TRIPLE_DOT)) # we want to accept both 'foo,..' and 'foo, ...'
  247. )
  248. paramSlot = Forward()
  249. simpleParamSeq = ZeroOrMore(paramSlot + COMMA) + Optional(paramSlot + Optional(COMMA))
  250. nestedParamSeq = Group(
  251. Suppress('(').setParseAction(replaceWith(T_NESTED)) + \
  252. simpleParamSeq + Optional(ellipsis + Optional(COMMA) + Optional(simpleParamSeq)) + \
  253. Suppress(')')
  254. ) # we accept "(a1, ... an)"
  255. paramSlot << (param | nestedParamSeq)
  256. optionalPart = Forward()
  257. paramSeq = simpleParamSeq + Optional(optionalPart) # this is our approximate target
  258. optionalPart << (
  259. Group(
  260. Suppress('[').setParseAction(replaceWith(T_OPTIONAL)) + Optional(COMMA) +
  261. paramSeq + Optional(ellipsis) +
  262. Suppress(']')
  263. )
  264. | ellipsis
  265. )
  266. return_type = Group(
  267. Empty().setParseAction(replaceWith(T_RETURN)) +
  268. Suppress(SP + (Literal("->") | (Literal(":") + SP + Literal("return"))) + SP) +
  269. ident
  270. )
  271. # this is our ideal target, with balancing paren and a multiline rest of doc.
  272. paramSeqAndRest = paramSeq + Suppress(')') + Optional(return_type) + Suppress(Optional(Regex(".*(?s)")))
  273. ############################################################################################
  274. # Some values are known to be of no use in source and needs to be suppressed.
  275. # Dict is keyed by module names, with "*" meaning "any module";
  276. # values are lists of names of members whose value must be pruned.
  277. SKIP_VALUE_IN_MODULE = {
  278. "sys": (
  279. "modules", "path_importer_cache", "argv", "builtins",
  280. "last_traceback", "last_type", "last_value", "builtin_module_names",
  281. ),
  282. "posix": (
  283. "environ",
  284. ),
  285. "zipimport": (
  286. "_zip_directory_cache",
  287. ),
  288. "*": (BUILTIN_MOD_NAME,)
  289. }
  290. # {"module": ("name",..)}: omit the names from the skeleton at all.
  291. OMIT_NAME_IN_MODULE = {}
  292. if version[0] >= 3:
  293. v = OMIT_NAME_IN_MODULE.get(BUILTIN_MOD_NAME, []) + ["True", "False", "None", "__debug__"]
  294. OMIT_NAME_IN_MODULE[BUILTIN_MOD_NAME] = v
  295. if IS_JAVA and version > (2, 4): # in 2.5.1 things are way weird!
  296. OMIT_NAME_IN_MODULE['_codecs'] = ['EncodingMap']
  297. OMIT_NAME_IN_MODULE['_hashlib'] = ['Hash']
  298. ADD_VALUE_IN_MODULE = {
  299. "sys": ("exc_value = Exception()", "exc_traceback=None"), # only present after an exception in current thread
  300. }
  301. # Some values are special and are better represented by hand-crafted constructs.
  302. # Dict is keyed by (module name, member name) and value is the replacement.
  303. REPLACE_MODULE_VALUES = {
  304. ("numpy.core.multiarray", "typeinfo"): "{}",
  305. ("psycopg2._psycopg", "string_types"): "{}", # badly mangled __eq__ breaks fmtValue
  306. ("PyQt5.QtWidgets", "qApp") : "QApplication()", # instead of None
  307. }
  308. if version[0] <= 2:
  309. REPLACE_MODULE_VALUES[(BUILTIN_MOD_NAME, "None")] = "object()"
  310. for std_file in ("stdin", "stdout", "stderr"):
  311. REPLACE_MODULE_VALUES[("sys", std_file)] = "open('')" #
  312. # Some functions and methods of some builtin classes have special signatures.
  313. # {("class", "method"): ("signature_string")}
  314. PREDEFINED_BUILTIN_SIGS = { #TODO: user-skeleton
  315. ("type", "__init__"): "(cls, what, bases=None, dict=None)", # two sigs squeezed into one
  316. ("object", "__init__"): "(self)",
  317. ("object", "__new__"): "(cls, *more)", # only for the sake of parameter names readability
  318. ("object", "__subclasshook__"): "(cls, subclass)", # trusting PY-1818 on sig
  319. ("int", "__init__"): "(self, x, base=10)", # overrides a fake
  320. ("list", "__init__"): "(self, seq=())",
  321. ("tuple", "__init__"): "(self, seq=())", # overrides a fake
  322. ("set", "__init__"): "(self, seq=())",
  323. ("dict", "__init__"): "(self, seq=None, **kwargs)",
  324. ("property", "__init__"): "(self, fget=None, fset=None, fdel=None, doc=None)",
  325. # TODO: infer, doc comments have it
  326. ("dict", "update"): "(self, E=None, **F)", # docstring nearly lies
  327. (None, "zip"): "(seq1, seq2, *more_seqs)",
  328. (None, "range"): "(start=None, stop=None, step=None)", # suboptimal: allows empty arglist
  329. (None, "filter"): "(function_or_none, sequence)",
  330. (None, "iter"): "(source, sentinel=None)",
  331. (None, "getattr"): "(object, name, default=None)",
  332. ('frozenset', "__init__"): "(self, seq=())",
  333. ("bytearray", "__init__"): "(self, source=None, encoding=None, errors='strict')",
  334. }
  335. if version[0] < 3:
  336. PREDEFINED_BUILTIN_SIGS[
  337. ("unicode", "__init__")] = "(self, string=u'', encoding=None, errors='strict')" # overrides a fake
  338. PREDEFINED_BUILTIN_SIGS[("super", "__init__")] = "(self, type1, type2=None)"
  339. PREDEFINED_BUILTIN_SIGS[
  340. (None, "min")] = "(*args, **kwargs)" # too permissive, but py2.x won't allow a better sig
  341. PREDEFINED_BUILTIN_SIGS[(None, "max")] = "(*args, **kwargs)"
  342. PREDEFINED_BUILTIN_SIGS[("str", "__init__")] = "(self, string='')" # overrides a fake
  343. PREDEFINED_BUILTIN_SIGS[(None, "print")] = "(*args, **kwargs)" # can't do better in 2.x
  344. else:
  345. PREDEFINED_BUILTIN_SIGS[("super", "__init__")] = "(self, type1=None, type2=None)"
  346. PREDEFINED_BUILTIN_SIGS[(None, "min")] = "(*args, key=None)"
  347. PREDEFINED_BUILTIN_SIGS[(None, "max")] = "(*args, key=None)"
  348. PREDEFINED_BUILTIN_SIGS[
  349. (None, "open")] = "(file, mode='r', buffering=None, encoding=None, errors=None, newline=None, closefd=True)"
  350. PREDEFINED_BUILTIN_SIGS[
  351. ("str", "__init__")] = "(self, value='', encoding=None, errors='strict')" # overrides a fake
  352. PREDEFINED_BUILTIN_SIGS[("str", "format")] = "(self, *args, **kwargs)"
  353. PREDEFINED_BUILTIN_SIGS[
  354. ("bytes", "__init__")] = "(self, value=b'', encoding=None, errors='strict')" # overrides a fake
  355. PREDEFINED_BUILTIN_SIGS[("bytes", "format")] = "(self, *args, **kwargs)"
  356. PREDEFINED_BUILTIN_SIGS[(None, "print")] = "(self, *args, sep=' ', end='\\n', file=None)" # proper signature
  357. if (2, 6) <= version < (3, 0):
  358. PREDEFINED_BUILTIN_SIGS[("unicode", "format")] = "(self, *args, **kwargs)"
  359. PREDEFINED_BUILTIN_SIGS[("str", "format")] = "(self, *args, **kwargs)"
  360. if version == (2, 5):
  361. PREDEFINED_BUILTIN_SIGS[("unicode", "splitlines")] = "(keepends=None)" # a typo in docstring there
  362. if version >= (2, 7):
  363. PREDEFINED_BUILTIN_SIGS[
  364. ("enumerate", "__init__")] = "(self, iterable, start=0)" # dosctring omits this completely.
  365. if version < (3, 3):
  366. datetime_mod = "datetime"
  367. else:
  368. datetime_mod = "_datetime"
  369. # NOTE: per-module signature data may be lazily imported
  370. # keyed by (module_name, class_name, method_name). PREDEFINED_BUILTIN_SIGS might be a layer of it.
  371. # value is ("signature", "return_literal")
  372. PREDEFINED_MOD_CLASS_SIGS = { #TODO: user-skeleton
  373. (BUILTIN_MOD_NAME, None, 'divmod'): ("(x, y)", "(0, 0)"),
  374. ("binascii", None, "hexlify"): ("(data)", BYTES_LIT),
  375. ("binascii", None, "unhexlify"): ("(hexstr)", BYTES_LIT),
  376. ("time", None, "ctime"): ("(seconds=None)", DEFAULT_STR_LIT),
  377. ("_struct", None, "pack"): ("(fmt, *args)", BYTES_LIT),
  378. ("_struct", None, "pack_into"): ("(fmt, buffer, offset, *args)", None),
  379. ("_struct", None, "unpack"): ("(fmt, string)", None),
  380. ("_struct", None, "unpack_from"): ("(fmt, buffer, offset=0)", None),
  381. ("_struct", None, "calcsize"): ("(fmt)", INT_LIT),
  382. ("_struct", "Struct", "__init__"): ("(self, fmt)", None),
  383. ("_struct", "Struct", "pack"): ("(self, *args)", BYTES_LIT),
  384. ("_struct", "Struct", "pack_into"): ("(self, buffer, offset, *args)", None),
  385. ("_struct", "Struct", "unpack"): ("(self, string)", None),
  386. ("_struct", "Struct", "unpack_from"): ("(self, buffer, offset=0)", None),
  387. (datetime_mod, "date", "__new__"): ("(cls, year=None, month=None, day=None)", None),
  388. (datetime_mod, "date", "fromordinal"): ("(cls, ordinal)", "date(1,1,1)"),
  389. (datetime_mod, "date", "fromtimestamp"): ("(cls, timestamp)", "date(1,1,1)"),
  390. (datetime_mod, "date", "isocalendar"): ("(self)", "(1, 1, 1)"),
  391. (datetime_mod, "date", "isoformat"): ("(self)", DEFAULT_STR_LIT),
  392. (datetime_mod, "date", "isoweekday"): ("(self)", INT_LIT),
  393. (datetime_mod, "date", "replace"): ("(self, year=None, month=None, day=None)", "date(1,1,1)"),
  394. (datetime_mod, "date", "strftime"): ("(self, format)", DEFAULT_STR_LIT),
  395. (datetime_mod, "date", "timetuple"): ("(self)", "(0, 0, 0, 0, 0, 0, 0, 0, 0)"),
  396. (datetime_mod, "date", "today"): ("(self)", "date(1, 1, 1)"),
  397. (datetime_mod, "date", "toordinal"): ("(self)", INT_LIT),
  398. (datetime_mod, "date", "weekday"): ("(self)", INT_LIT),
  399. (datetime_mod, "timedelta", "__new__"
  400. ): (
  401. "(cls, days=None, seconds=None, microseconds=None, milliseconds=None, minutes=None, hours=None, weeks=None)",
  402. None),
  403. (datetime_mod, "datetime", "__new__"
  404. ): (
  405. "(cls, year=None, month=None, day=None, hour=None, minute=None, second=None, microsecond=None, tzinfo=None)",
  406. None),
  407. (datetime_mod, "datetime", "astimezone"): ("(self, tz)", "datetime(1, 1, 1)"),
  408. (datetime_mod, "datetime", "combine"): ("(cls, date, time)", "datetime(1, 1, 1)"),
  409. (datetime_mod, "datetime", "date"): ("(self)", "datetime(1, 1, 1)"),
  410. (datetime_mod, "datetime", "fromtimestamp"): ("(cls, timestamp, tz=None)", "datetime(1, 1, 1)"),
  411. (datetime_mod, "datetime", "isoformat"): ("(self, sep='T')", DEFAULT_STR_LIT),
  412. (datetime_mod, "datetime", "now"): ("(cls, tz=None)", "datetime(1, 1, 1)"),
  413. (datetime_mod, "datetime", "strptime"): ("(cls, date_string, format)", DEFAULT_STR_LIT),
  414. (datetime_mod, "datetime", "replace" ):
  415. (
  416. "(self, year=None, month=None, day=None, hour=None, minute=None, second=None, microsecond=None, tzinfo=None)",
  417. "datetime(1, 1, 1)"),
  418. (datetime_mod, "datetime", "time"): ("(self)", "time(0, 0)"),
  419. (datetime_mod, "datetime", "timetuple"): ("(self)", "(0, 0, 0, 0, 0, 0, 0, 0, 0)"),
  420. (datetime_mod, "datetime", "timetz"): ("(self)", "time(0, 0)"),
  421. (datetime_mod, "datetime", "utcfromtimestamp"): ("(self, timestamp)", "datetime(1, 1, 1)"),
  422. (datetime_mod, "datetime", "utcnow"): ("(cls)", "datetime(1, 1, 1)"),
  423. (datetime_mod, "datetime", "utctimetuple"): ("(self)", "(0, 0, 0, 0, 0, 0, 0, 0, 0)"),
  424. (datetime_mod, "time", "__new__"): (
  425. "(cls, hour=None, minute=None, second=None, microsecond=None, tzinfo=None)", None),
  426. (datetime_mod, "time", "isoformat"): ("(self)", DEFAULT_STR_LIT),
  427. (datetime_mod, "time", "replace"): (
  428. "(self, hour=None, minute=None, second=None, microsecond=None, tzinfo=None)", "time(0, 0)"),
  429. (datetime_mod, "time", "strftime"): ("(self, format)", DEFAULT_STR_LIT),
  430. (datetime_mod, "tzinfo", "dst"): ("(self, date_time)", INT_LIT),
  431. (datetime_mod, "tzinfo", "fromutc"): ("(self, date_time)", "datetime(1, 1, 1)"),
  432. (datetime_mod, "tzinfo", "tzname"): ("(self, date_time)", DEFAULT_STR_LIT),
  433. (datetime_mod, "tzinfo", "utcoffset"): ("(self, date_time)", INT_LIT),
  434. ("_io", None, "open"): ("(name, mode=None, buffering=None)", "file('/dev/null')"),
  435. ("_io", "FileIO", "read"): ("(self, size=-1)", DEFAULT_STR_LIT),
  436. ("_fileio", "_FileIO", "read"): ("(self, size=-1)", DEFAULT_STR_LIT),
  437. ("thread", None, "start_new"): ("(function, args, kwargs=None)", INT_LIT),
  438. ("_thread", None, "start_new"): ("(function, args, kwargs=None)", INT_LIT),
  439. ("itertools", "groupby", "__init__"): ("(self, iterable, key=None)", None),
  440. ("itertools", None, "groupby"): ("(iterable, key=None)", LIST_LIT),
  441. ("cStringIO", "OutputType", "seek"): ("(self, position, mode=0)", None),
  442. ("cStringIO", "InputType", "seek"): ("(self, position, mode=0)", None),
  443. # NOTE: here we stand on shaky ground providing sigs for 3rd-party modules, though well-known
  444. ("numpy.core.multiarray", "ndarray", "__array__"): ("(self, dtype=None)", None),
  445. ("numpy.core.multiarray", None, "arange"): ("(start=None, stop=None, step=None, dtype=None)", None),
  446. # same as range()
  447. ("numpy.core.multiarray", None, "set_numeric_ops"): ("(**ops)", None),
  448. ("numpy.random.mtrand", None, "rand"): ("(*dn)", None),
  449. ("numpy.random.mtrand", None, "randn"): ("(*dn)", None),
  450. ("numpy.core.multiarray", "ndarray", "reshape"): ("(self, shape, *shapes, order='C')", None),
  451. ("numpy.core.multiarray", "ndarray", "resize"): ("(self, *new_shape, refcheck=True)", None),
  452. }
  453. bin_collections_names = ['collections', '_collections']
  454. for name in bin_collections_names:
  455. PREDEFINED_MOD_CLASS_SIGS[(name, "deque", "__init__")] = ("(self, iterable=(), maxlen=None)", None)
  456. PREDEFINED_MOD_CLASS_SIGS[(name, "defaultdict", "__init__")] = ("(self, default_factory=None, **kwargs)", None)
  457. if version[0] < 3:
  458. PREDEFINED_MOD_CLASS_SIGS[("exceptions", "BaseException", "__unicode__")] = ("(self)", UNICODE_LIT)
  459. PREDEFINED_MOD_CLASS_SIGS[("itertools", "product", "__init__")] = ("(self, *iterables, **kwargs)", LIST_LIT)
  460. else:
  461. PREDEFINED_MOD_CLASS_SIGS[("itertools", "product", "__init__")] = ("(self, *iterables, repeat=1)", LIST_LIT)
  462. if version[0] < 3:
  463. PREDEFINED_MOD_CLASS_SIGS[("PyQt4.QtCore", None, "pyqtSlot")] = (
  464. "(*types, **keywords)", None) # doc assumes py3k syntax
  465. # known properties of modules
  466. # {{"module": {"class", "property" : ("letters", ("getter", "type"))}},
  467. # where letters is any set of r,w,d (read, write, del) and "getter" is a source of typed getter.
  468. # if value is None, the property should be omitted.
  469. # read-only properties that return an object are not listed.
  470. G_OBJECT = ("lambda self: object()", None)
  471. G_TYPE = ("lambda self: type(object)", "type")
  472. G_DICT = ("lambda self: {}", "dict")
  473. G_STR = ("lambda self: ''", "string")
  474. G_TUPLE = ("lambda self: tuple()", "tuple")
  475. G_FLOAT = ("lambda self: 0.0", "float")
  476. G_INT = ("lambda self: 0", "int")
  477. G_BOOL = ("lambda self: True", "bool")
  478. KNOWN_PROPS = {
  479. BUILTIN_MOD_NAME: {
  480. ("object", '__class__'): ('r', G_TYPE),
  481. ('complex', 'real'): ('r', G_FLOAT),
  482. ('complex', 'imag'): ('r', G_FLOAT),
  483. ("file", 'softspace'): ('r', G_BOOL),
  484. ("file", 'name'): ('r', G_STR),
  485. ("file", 'encoding'): ('r', G_STR),
  486. ("file", 'mode'): ('r', G_STR),
  487. ("file", 'closed'): ('r', G_BOOL),
  488. ("file", 'newlines'): ('r', G_STR),
  489. ("slice", 'start'): ('r', G_INT),
  490. ("slice", 'step'): ('r', G_INT),
  491. ("slice", 'stop'): ('r', G_INT),
  492. ("super", '__thisclass__'): ('r', G_TYPE),
  493. ("super", '__self__'): ('r', G_TYPE),
  494. ("super", '__self_class__'): ('r', G_TYPE),
  495. ("type", '__basicsize__'): ('r', G_INT),
  496. ("type", '__itemsize__'): ('r', G_INT),
  497. ("type", '__base__'): ('r', G_TYPE),
  498. ("type", '__flags__'): ('r', G_INT),
  499. ("type", '__mro__'): ('r', G_TUPLE),
  500. ("type", '__bases__'): ('r', G_TUPLE),
  501. ("type", '__dictoffset__'): ('r', G_INT),
  502. ("type", '__dict__'): ('r', G_DICT),
  503. ("type", '__name__'): ('r', G_STR),
  504. ("type", '__weakrefoffset__'): ('r', G_INT),
  505. },
  506. "exceptions": {
  507. ("BaseException", '__dict__'): ('r', G_DICT),
  508. ("BaseException", 'message'): ('rwd', G_STR),
  509. ("BaseException", 'args'): ('r', G_TUPLE),
  510. ("EnvironmentError", 'errno'): ('rwd', G_INT),
  511. ("EnvironmentError", 'message'): ('rwd', G_STR),
  512. ("EnvironmentError", 'strerror'): ('rwd', G_INT),
  513. ("EnvironmentError", 'filename'): ('rwd', G_STR),
  514. ("SyntaxError", 'text'): ('rwd', G_STR),
  515. ("SyntaxError", 'print_file_and_line'): ('rwd', G_BOOL),
  516. ("SyntaxError", 'filename'): ('rwd', G_STR),
  517. ("SyntaxError", 'lineno'): ('rwd', G_INT),
  518. ("SyntaxError", 'offset'): ('rwd', G_INT),
  519. ("SyntaxError", 'msg'): ('rwd', G_STR),
  520. ("SyntaxError", 'message'): ('rwd', G_STR),
  521. ("SystemExit", 'message'): ('rwd', G_STR),
  522. ("SystemExit", 'code'): ('rwd', G_OBJECT),
  523. ("UnicodeDecodeError", '__basicsize__'): None,
  524. ("UnicodeDecodeError", '__itemsize__'): None,
  525. ("UnicodeDecodeError", '__base__'): None,
  526. ("UnicodeDecodeError", '__flags__'): ('rwd', G_INT),
  527. ("UnicodeDecodeError", '__mro__'): None,
  528. ("UnicodeDecodeError", '__bases__'): None,
  529. ("UnicodeDecodeError", '__dictoffset__'): None,
  530. ("UnicodeDecodeError", '__dict__'): None,
  531. ("UnicodeDecodeError", '__name__'): None,
  532. ("UnicodeDecodeError", '__weakrefoffset__'): None,
  533. ("UnicodeEncodeError", 'end'): ('rwd', G_INT),
  534. ("UnicodeEncodeError", 'encoding'): ('rwd', G_STR),
  535. ("UnicodeEncodeError", 'object'): ('rwd', G_OBJECT),
  536. ("UnicodeEncodeError", 'start'): ('rwd', G_INT),
  537. ("UnicodeEncodeError", 'reason'): ('rwd', G_STR),
  538. ("UnicodeEncodeError", 'message'): ('rwd', G_STR),
  539. ("UnicodeTranslateError", 'end'): ('rwd', G_INT),
  540. ("UnicodeTranslateError", 'encoding'): ('rwd', G_STR),
  541. ("UnicodeTranslateError", 'object'): ('rwd', G_OBJECT),
  542. ("UnicodeTranslateError", 'start'): ('rwd', G_INT),
  543. ("UnicodeTranslateError", 'reason'): ('rwd', G_STR),
  544. ("UnicodeTranslateError", 'message'): ('rwd', G_STR),
  545. },
  546. '_ast': {
  547. ("AST", '__dict__'): ('rd', G_DICT),
  548. },
  549. 'posix': {
  550. ("statvfs_result", 'f_flag'): ('r', G_INT),
  551. ("statvfs_result", 'f_bavail'): ('r', G_INT),
  552. ("statvfs_result", 'f_favail'): ('r', G_INT),
  553. ("statvfs_result", 'f_files'): ('r', G_INT),
  554. ("statvfs_result", 'f_frsize'): ('r', G_INT),
  555. ("statvfs_result", 'f_blocks'): ('r', G_INT),
  556. ("statvfs_result", 'f_ffree'): ('r', G_INT),
  557. ("statvfs_result", 'f_bfree'): ('r', G_INT),
  558. ("statvfs_result", 'f_namemax'): ('r', G_INT),
  559. ("statvfs_result", 'f_bsize'): ('r', G_INT),
  560. ("stat_result", 'st_ctime'): ('r', G_INT),
  561. ("stat_result", 'st_rdev'): ('r', G_INT),
  562. ("stat_result", 'st_mtime'): ('r', G_INT),
  563. ("stat_result", 'st_blocks'): ('r', G_INT),
  564. ("stat_result", 'st_gid'): ('r', G_INT),
  565. ("stat_result", 'st_nlink'): ('r', G_INT),
  566. ("stat_result", 'st_ino'): ('r', G_INT),
  567. ("stat_result", 'st_blksize'): ('r', G_INT),
  568. ("stat_result", 'st_dev'): ('r', G_INT),
  569. ("stat_result", 'st_size'): ('r', G_INT),
  570. ("stat_result", 'st_mode'): ('r', G_INT),
  571. ("stat_result", 'st_uid'): ('r', G_INT),
  572. ("stat_result", 'st_atime'): ('r', G_INT),
  573. },
  574. "pwd": {
  575. ("struct_pwent", 'pw_dir'): ('r', G_STR),
  576. ("struct_pwent", 'pw_gid'): ('r', G_INT),
  577. ("struct_pwent", 'pw_passwd'): ('r', G_STR),
  578. ("struct_pwent", 'pw_gecos'): ('r', G_STR),
  579. ("struct_pwent", 'pw_shell'): ('r', G_STR),
  580. ("struct_pwent", 'pw_name'): ('r', G_STR),
  581. ("struct_pwent", 'pw_uid'): ('r', G_INT),
  582. ("struct_passwd", 'pw_dir'): ('r', G_STR),
  583. ("struct_passwd", 'pw_gid'): ('r', G_INT),
  584. ("struct_passwd", 'pw_passwd'): ('r', G_STR),
  585. ("struct_passwd", 'pw_gecos'): ('r', G_STR),
  586. ("struct_passwd", 'pw_shell'): ('r', G_STR),
  587. ("struct_passwd", 'pw_name'): ('r', G_STR),
  588. ("struct_passwd", 'pw_uid'): ('r', G_INT),
  589. },
  590. "thread": {
  591. ("_local", '__dict__'): None
  592. },
  593. "xxsubtype": {
  594. ("spamdict", 'state'): ('r', G_INT),
  595. ("spamlist", 'state'): ('r', G_INT),
  596. },
  597. "zipimport": {
  598. ("zipimporter", 'prefix'): ('r', G_STR),
  599. ("zipimporter", 'archive'): ('r', G_STR),
  600. ("zipimporter", '_files'): ('r', G_DICT),
  601. },
  602. "_struct": {
  603. ("Struct", "size"): ('r', G_INT),
  604. ("Struct", "format"): ('r', G_STR),
  605. },
  606. datetime_mod: {
  607. ("datetime", "hour"): ('r', G_INT),
  608. ("datetime", "minute"): ('r', G_INT),
  609. ("datetime", "second"): ('r', G_INT),
  610. ("datetime", "microsecond"): ('r', G_INT),
  611. ("date", "day"): ('r', G_INT),
  612. ("date", "month"): ('r', G_INT),
  613. ("date", "year"): ('r', G_INT),
  614. ("time", "hour"): ('r', G_INT),
  615. ("time", "minute"): ('r', G_INT),
  616. ("time", "second"): ('r', G_INT),
  617. ("time", "microsecond"): ('r', G_INT),
  618. ("timedelta", "days"): ('r', G_INT),
  619. ("timedelta", "seconds"): ('r', G_INT),
  620. ("timedelta", "microseconds"): ('r', G_INT),
  621. },
  622. }
  623. # Sometimes module X defines item foo but foo.__module__ == 'Y' instead of 'X';
  624. # module Y just re-exports foo, and foo fakes being defined in Y.
  625. # We list all such Ys keyed by X, all fully-qualified names:
  626. # {"real_definer_module": ("fake_reexporter_module",..)}
  627. KNOWN_FAKE_REEXPORTERS = {
  628. "_collections": ('collections',),
  629. "_functools": ('functools',),
  630. "_socket": ('socket',), # .error, etc
  631. "pyexpat": ('xml.parsers.expat',),
  632. "_bsddb": ('bsddb.db',),
  633. "pysqlite2._sqlite": ('pysqlite2.dbapi2',), # errors
  634. "numpy.core.multiarray": ('numpy', 'numpy.core'),
  635. "numpy.core._dotblas": ('numpy', 'numpy.core'),
  636. "numpy.core.umath": ('numpy', 'numpy.core'),
  637. "gtk._gtk": ('gtk', 'gtk.gdk',),
  638. "gobject._gobject": ('gobject',),
  639. "gnomecanvas": ("gnome.canvas",),
  640. }
  641. KNOWN_FAKE_BASES = []
  642. # list of classes that pretend to be base classes but are mere wrappers, and their defining modules
  643. # [(class, module),...] -- real objects, not names
  644. #noinspection PyBroadException
  645. try:
  646. #noinspection PyUnresolvedReferences
  647. import sip as sip_module # Qt specifically likes it
  648. if hasattr(sip_module, 'wrapper'):
  649. KNOWN_FAKE_BASES.append((sip_module.wrapper, sip_module))
  650. if hasattr(sip_module, 'simplewrapper'):
  651. KNOWN_FAKE_BASES.append((sip_module.simplewrapper, sip_module))
  652. del sip_module
  653. except:
  654. pass
  655. # This is a list of builtin classes to use fake init
  656. FAKE_BUILTIN_INITS = (tuple, type, int, str)
  657. if version[0] < 3:
  658. FAKE_BUILTIN_INITS = FAKE_BUILTIN_INITS + (getattr(the_builtins, "unicode"),)
  659. else:
  660. FAKE_BUILTIN_INITS = FAKE_BUILTIN_INITS + (getattr(the_builtins, "str"), getattr(the_builtins, "bytes"))
  661. # Some builtin methods are decorated, but this is hard to detect.
  662. # {("class_name", "method_name"): "decorator"}
  663. KNOWN_DECORATORS = {
  664. ("dict", "fromkeys"): "staticmethod",
  665. ("object", "__subclasshook__"): "classmethod",
  666. ("bytearray", "fromhex"): "classmethod",
  667. ("bytes", "fromhex"): "classmethod",
  668. ("bytearray", "maketrans"): "staticmethod",
  669. ("bytes", "maketrans"): "staticmethod",
  670. ("int", "from_bytes"): "classmethod",
  671. }
  672. classobj_txt = ( #TODO: user-skeleton
  673. "class ___Classobj:" "\n"
  674. " '''A mock class representing the old style class base.'''" "\n"
  675. " __module__ = ''" "\n"
  676. " __class__ = None" "\n"
  677. "\n"
  678. " def __init__(self):" "\n"
  679. " pass" "\n"
  680. " __dict__ = {}" "\n"
  681. " __doc__ = ''" "\n"
  682. )
  683. MAC_STDLIB_PATTERN = re.compile("/System/Library/Frameworks/Python\\.framework/Versions/(.+)/lib/python\\1/(.+)")
  684. MAC_SKIP_MODULES = ["test", "ctypes/test", "distutils/tests", "email/test",
  685. "importlib/test", "json/tests", "lib2to3/tests",
  686. "bsddb/test",
  687. "sqlite3/test", "tkinter/test", "idlelib", "antigravity"]
  688. POSIX_SKIP_MODULES = ["vtemodule", "PAMmodule", "_snackmodule", "/quodlibet/_mmkeys"]
  689. BIN_MODULE_FNAME_PAT = re.compile(r'([a-zA-Z_][0-9a-zA-Z_]*)\.(?:pyc|pyo|(?:(?:[a-zA-Z_0-9\-]+\.)?(?:so|pyd)))$')
  690. # possible binary module filename: letter, alphanum architecture per PEP-3149
  691. TYPELIB_MODULE_FNAME_PAT = re.compile("([a-zA-Z_]+[0-9a-zA-Z]*)[0-9a-zA-Z-.]*\\.typelib")
  692. MODULES_INSPECT_DIR = ['gi.repository']