Show More
@@ -170,13 +170,13 def _safe_repr(obj): | |||
|
170 | 170 | return _failed_repr(obj, e) |
|
171 | 171 | |
|
172 | 172 | def _safe_getattr(obj, attr, default=None): |
|
173 |
""" |
|
|
173 | """Safe version of getattr. | |
|
174 | 174 | |
|
175 | catches errors other than AttributeError, | |
|
176 | unlike vanilla getattr | |
|
175 | Same as getattr, but will return ``default`` on any Exception, | |
|
176 | rather than raising. | |
|
177 | 177 | """ |
|
178 | 178 | try: |
|
179 | return getattr(obj, attr) | |
|
179 | return getattr(obj, attr, default) | |
|
180 | 180 | except Exception: |
|
181 | 181 | return default |
|
182 | 182 |
@@ -160,3 +160,25 def test_bad_repr(): | |||
|
160 | 160 | """Don't raise, even when repr fails""" |
|
161 | 161 | output = pretty.pretty(BadRepr()) |
|
162 | 162 | nt.assert_in("failed", output) |
|
163 | nt.assert_in("at 0x", output) | |
|
164 | nt.assert_in("test_pretty", output) | |
|
165 | ||
|
166 | class BadException(Exception): | |
|
167 | def __str__(self): | |
|
168 | return -1 | |
|
169 | ||
|
170 | class ReallyBadRepr(object): | |
|
171 | __module__ = 1 | |
|
172 | @property | |
|
173 | def __class__(self): | |
|
174 | raise ValueError("I am horrible") | |
|
175 | ||
|
176 | def __repr__(self): | |
|
177 | raise BadException() | |
|
178 | ||
|
179 | def test_really_bad_repr(): | |
|
180 | output = pretty.pretty(ReallyBadRepr()) | |
|
181 | nt.assert_in("failed", output) | |
|
182 | nt.assert_in("BadException: unknown", output) | |
|
183 | nt.assert_in("unknown type", output) | |
|
184 | No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now