Changeset View
Changeset View
Standalone View
Standalone View
swh/storage/tests/storage_data.py
# Copyright (C) 2015-2019 The Software Heritage developers | # Copyright (C) 2015-2019 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import datetime | import datetime | ||||
from swh.model.hashutil import hash_to_bytes | from swh.model.hashutil import hash_to_bytes | ||||
from swh.model import from_disk | from swh.model import from_disk | ||||
class StorageData: | class StorageData: | ||||
def __getattr__(self, key): | def __getattr__(self, key): | ||||
try: | |||||
v = globals()[key] | v = globals()[key] | ||||
except KeyError as e: | |||||
ardumont: Why do we need this?
(maybe add a comment about it) | |||||
Done Inline ActionsIt's how __getattr__ works: "This method should either return the (computed) attribute value or raise an AttributeError exception." https://docs.python.org/3/reference/datamodel.html#object.__getattr__ Else, getattr() and hasattr() on the object fail, eg. /usr/lib/python3.7/doctest.py:932: in find self._find(tests, obj, name, module, source_lines, globs, {}) .tox/py3/lib/python3.7/site-packages/_pytest/doctest.py:445: in _find self, tests, obj, name, module, source_lines, globs, seen /usr/lib/python3.7/doctest.py:991: in _find if ((inspect.isroutine(inspect.unwrap(val)) .tox/py3/lib/python3.7/site-packages/_pytest/doctest.py:408: in _mock_aware_unwrap return real_unwrap(obj, stop=_is_mocked) /usr/lib/python3.7/inspect.py:511: in unwrap while _is_wrapper(func): /usr/lib/python3.7/inspect.py:505: in _is_wrapper return hasattr(f, '__wrapped__') and not stop(f) .tox/py3/lib/python3.7/site-packages/swh/storage/tests/storage_data.py:14: in __getattr__ v = globals()[key] E KeyError: '__wrapped__' vlorentz: It's how `__getattr__` works: "This method should either return the (computed) attribute value… | |||||
raise AttributeError(e.args[0]) | |||||
if hasattr(v, 'copy'): | if hasattr(v, 'copy'): | ||||
return v.copy() | return v.copy() | ||||
return v | return v | ||||
data = StorageData() | data = StorageData() | ||||
▲ Show 20 Lines • Show All 501 Lines • Show Last 20 Lines |
Why do we need this?
(maybe add a comment about it)