getattr_static.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. """
  2. A static version of getattr.
  3. This is a backport of the Python 3 code with a little bit of additional
  4. information returned to enable Jedi to make decisions.
  5. """
  6. import types
  7. from jedi import debug
  8. _sentinel = object()
  9. def _check_instance(obj, attr):
  10. instance_dict = {}
  11. try:
  12. instance_dict = object.__getattribute__(obj, "__dict__")
  13. except AttributeError:
  14. pass
  15. return dict.get(instance_dict, attr, _sentinel)
  16. def _check_class(klass, attr):
  17. for entry in _static_getmro(klass):
  18. if _shadowed_dict(type(entry)) is _sentinel:
  19. try:
  20. return entry.__dict__[attr]
  21. except KeyError:
  22. pass
  23. return _sentinel
  24. def _is_type(obj):
  25. try:
  26. _static_getmro(obj)
  27. except TypeError:
  28. return False
  29. return True
  30. def _shadowed_dict(klass):
  31. dict_attr = type.__dict__["__dict__"]
  32. for entry in _static_getmro(klass):
  33. try:
  34. class_dict = dict_attr.__get__(entry)["__dict__"]
  35. except KeyError:
  36. pass
  37. else:
  38. if not (type(class_dict) is types.GetSetDescriptorType
  39. and class_dict.__name__ == "__dict__"
  40. and class_dict.__objclass__ is entry):
  41. return class_dict
  42. return _sentinel
  43. def _static_getmro(klass):
  44. mro = type.__dict__['__mro__'].__get__(klass)
  45. if not isinstance(mro, (tuple, list)):
  46. # There are unfortunately no tests for this, I was not able to
  47. # reproduce this in pure Python. However should still solve the issue
  48. # raised in GH #1517.
  49. debug.warning('mro of %s returned %s, should be a tuple' % (klass, mro))
  50. return ()
  51. return mro
  52. def _safe_hasattr(obj, name):
  53. return _check_class(type(obj), name) is not _sentinel
  54. def _safe_is_data_descriptor(obj):
  55. return _safe_hasattr(obj, '__set__') or _safe_hasattr(obj, '__delete__')
  56. def getattr_static(obj, attr, default=_sentinel):
  57. """Retrieve attributes without triggering dynamic lookup via the
  58. descriptor protocol, __getattr__ or __getattribute__.
  59. Note: this function may not be able to retrieve all attributes
  60. that getattr can fetch (like dynamically created attributes)
  61. and may find attributes that getattr can't (like descriptors
  62. that raise AttributeError). It can also return descriptor objects
  63. instead of instance members in some cases. See the
  64. documentation for details.
  65. Returns a tuple `(attr, is_get_descriptor)`. is_get_descripter means that
  66. the attribute is a descriptor that has a `__get__` attribute.
  67. """
  68. instance_result = _sentinel
  69. if not _is_type(obj):
  70. klass = type(obj)
  71. dict_attr = _shadowed_dict(klass)
  72. if (dict_attr is _sentinel or type(dict_attr) is types.MemberDescriptorType):
  73. instance_result = _check_instance(obj, attr)
  74. else:
  75. klass = obj
  76. klass_result = _check_class(klass, attr)
  77. if instance_result is not _sentinel and klass_result is not _sentinel:
  78. if _safe_hasattr(klass_result, '__get__') \
  79. and _safe_is_data_descriptor(klass_result):
  80. # A get/set descriptor has priority over everything.
  81. return klass_result, True
  82. if instance_result is not _sentinel:
  83. return instance_result, False
  84. if klass_result is not _sentinel:
  85. return klass_result, _safe_hasattr(klass_result, '__get__')
  86. if obj is klass:
  87. # for types we check the metaclass too
  88. for entry in _static_getmro(type(klass)):
  89. if _shadowed_dict(type(entry)) is _sentinel:
  90. try:
  91. return entry.__dict__[attr], False
  92. except KeyError:
  93. pass
  94. if default is not _sentinel:
  95. return default, False
  96. raise AttributeError(attr)