MCPcopy Create free account
hub / github.com/pytest-dev/pytest / safe_getattr

Function safe_getattr

src/_pytest/compat.py:317–331  ·  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

315
316
317def safe_getattr(object: Any, name: str, default: Any) -> Any:
318 """Like getattr but return default upon any Exception or any OutcomeException.
319
320 Attribute access can potentially fail for 'evil' Python objects.
321 See issue #214.
322 It catches OutcomeException because of #2490 (issue #580), new outcomes
323 are derived from BaseException instead of Exception (for more details
324 check #2707).
325 """
326 from _pytest.outcomes import TEST_OUTCOME
327
328 try:
329 return getattr(object, name, default)
330 except TEST_OUTCOME:
331 return default
332
333
334def 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