mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-07-25 23:50:01 +03:00
replace try/except with context manager
This commit is contained in:
parent
712317fb55
commit
e6f59e7260
|
@ -1,6 +1,8 @@
|
|||
"""
|
||||
Helper classes for parsers.
|
||||
"""
|
||||
|
||||
import contextlib
|
||||
import datetime
|
||||
import decimal
|
||||
import json # noqa
|
||||
|
@ -58,10 +60,8 @@ class JSONEncoder(json.JSONEncoder):
|
|||
)
|
||||
elif hasattr(obj, '__getitem__'):
|
||||
cls = (list if isinstance(obj, (list, tuple)) else dict)
|
||||
try:
|
||||
with contextlib.suppress(Exception):
|
||||
return cls(obj)
|
||||
except Exception:
|
||||
pass
|
||||
elif hasattr(obj, '__iter__'):
|
||||
return tuple(item for item in obj)
|
||||
return super().default(obj)
|
||||
|
|
Loading…
Reference in New Issue
Block a user