/vim/bundle/YouCompleteMe/third_party/ycmd/third_party/jedi/jedi/evaluate/stdlib.py

https://bitbucket.org/tetonedge/linux
Python | 131 lines | 109 code | 0 blank | 22 comment | 1 complexity | 8fca85cabe298dc4d18da07571cf44e2 MD5 | raw file
  1. """
  2. Implementations of standard library functions, because it's not possible to
  3. understand them with Jedi.
  4. """
  5. from jedi._compatibility import unicode
  6. from jedi.evaluate import compiled
  7. from jedi.evaluate import representation as er
  8. from jedi.evaluate import iterable
  9. from jedi.evaluate.helpers import FakeArray, FakeStatement
  10. from jedi.parser import representation as pr
  11. from jedi import debug
  12. class NotInStdLib(LookupError):
  13. pass
  14. def execute(evaluator, obj, params):
  15. try:
  16. obj_name = str(obj.name)
  17. except AttributeError:
  18. pass
  19. else:
  20. if obj.parent == compiled.builtin:
  21. module_name = 'builtins'
  22. elif isinstance(obj.parent, pr.Module):
  23. module_name = str(obj.parent.name)
  24. else:
  25. module_name = ''
  26. # for now we just support builtin functions.
  27. try:
  28. return _implemented[module_name][obj_name](evaluator, obj, params)
  29. except KeyError:
  30. pass
  31. raise NotInStdLib()
  32. def _follow_param(evaluator, params, index):
  33. try:
  34. stmt = params[index]
  35. except IndexError:
  36. return []
  37. else:
  38. if isinstance(stmt, pr.Statement):
  39. return evaluator.eval_statement(stmt)
  40. else:
  41. return [stmt] # just some arbitrary object
  42. def builtins_getattr(evaluator, obj, params):
  43. stmts = []
  44. # follow the first param
  45. objects = _follow_param(evaluator, params, 0)
  46. names = _follow_param(evaluator, params, 1)
  47. for obj in objects:
  48. if not isinstance(obj, (er.Instance, er.Class, pr.Module, compiled.CompiledObject)):
  49. debug.warning('getattr called without instance')
  50. continue
  51. for name in names:
  52. s = unicode, str
  53. if isinstance(name, compiled.CompiledObject) and isinstance(name.obj, s):
  54. stmts += evaluator.follow_path(iter([name.obj]), [obj], obj)
  55. else:
  56. debug.warning('getattr called without str')
  57. continue
  58. return stmts
  59. def builtins_type(evaluator, obj, params):
  60. if len(params) == 1:
  61. # otherwise it would be a metaclass... maybe someday...
  62. objects = _follow_param(evaluator, params, 0)
  63. return [o.base for o in objects if isinstance(o, er.Instance)]
  64. return []
  65. def builtins_super(evaluator, obj, params):
  66. # TODO make this able to detect multiple inheritance super
  67. accept = (pr.Function,)
  68. func = params.get_parent_until(accept)
  69. if func.isinstance(*accept):
  70. cls = func.get_parent_until(accept + (pr.Class,),
  71. include_current=False)
  72. if isinstance(cls, pr.Class):
  73. cls = er.Class(evaluator, cls)
  74. su = cls.get_super_classes()
  75. if su:
  76. return evaluator.execute(su[0])
  77. return []
  78. def builtins_reversed(evaluator, obj, params):
  79. objects = tuple(_follow_param(evaluator, params, 0))
  80. if objects:
  81. # unpack the iterator values
  82. objects = tuple(iterable.get_iterator_types(objects))
  83. if objects:
  84. rev = reversed(objects)
  85. # Repack iterator values and then run it the normal way. This is
  86. # necessary, because `reversed` is a function and autocompletion
  87. # would fail in certain cases like `reversed(x).__iter__` if we
  88. # just returned the result directly.
  89. stmts = [FakeStatement([r]) for r in rev]
  90. objects = (iterable.Array(evaluator, FakeArray(stmts, objects[0].parent)),)
  91. return [er.Instance(evaluator, obj, objects)]
  92. def _return_first_param(evaluator, obj, params):
  93. if len(params) == 1:
  94. return _follow_param(evaluator, params, 0)
  95. return []
  96. _implemented = {
  97. 'builtins': {
  98. 'getattr': builtins_getattr,
  99. 'type': builtins_type,
  100. 'super': builtins_super,
  101. 'reversed': builtins_reversed,
  102. },
  103. 'copy': {
  104. 'copy': _return_first_param,
  105. 'deepcopy': _return_first_param,
  106. },
  107. 'json': {
  108. 'load': lambda *args: [],
  109. 'loads': lambda *args: [],
  110. },
  111. }