MCPcopy
hub / github.com/pytest-dev/pytest / safe_getattr

Function safe_getattr

src/_pytest/compat.py:236–250  ·  view source on GitHub ↗

Like getattr but return default upon any Exception or any OutcomeException. Attribute access can potentially fail for 'evil' Python objects. See issue #214. It catches OutcomeException because of #2490 (issue #580), new outcomes are derived from BaseException instead of Exception (f

(object: Any, name: str, default: Any)

Source from the content-addressed store, hash-verified

234
235
236def safe_getattr(object: Any, name: str, default: Any) -> Any:
237 """Like getattr but return default upon any Exception or any OutcomeException.
238
239 Attribute access can potentially fail for 'evil' Python objects.
240 See issue #214.
241 It catches OutcomeException because of #2490 (issue #580), new outcomes
242 are derived from BaseException instead of Exception (for more details
243 check #2707).
244 """
245 from _pytest.outcomes import TEST_OUTCOME
246
247 try:
248 return getattr(object, name, default)
249 except TEST_OUTCOME:
250 return default
251
252
253def safe_isclass(obj: object) -> bool:

Callers 6

_is_mockedFunction · 0.90
isnosetestMethod · 0.90
istestfunctionMethod · 0.90
collectMethod · 0.90
parsefactoriesMethod · 0.90
test_safe_getattrFunction · 0.90

Calls

no outgoing calls

Tested by 2

_is_mockedFunction · 0.72
test_safe_getattrFunction · 0.72