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)
| 315 | |
| 316 | |
| 317 | def 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 | |
| 334 | def safe_isclass(obj: object) -> bool: |
no outgoing calls