mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-07-27 08:29:59 +03:00
replace try/except with context manager
This commit is contained in:
parent
e6f59e7260
commit
b78d218fa6
|
@ -1,3 +1,4 @@
|
||||||
|
import contextlib
|
||||||
import sys
|
import sys
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
from collections.abc import Mapping, MutableMapping
|
from collections.abc import Mapping, MutableMapping
|
||||||
|
@ -103,15 +104,13 @@ class JSONBoundField(BoundField):
|
||||||
# When HTML form input is used and the input is not valid
|
# When HTML form input is used and the input is not valid
|
||||||
# value will be a JSONString, rather than a JSON primitive.
|
# value will be a JSONString, rather than a JSON primitive.
|
||||||
if not getattr(value, 'is_json_string', False):
|
if not getattr(value, 'is_json_string', False):
|
||||||
try:
|
with contextlib.suppress(TypeError, ValueError):
|
||||||
value = json.dumps(
|
value = json.dumps(
|
||||||
self.value,
|
self.value,
|
||||||
sort_keys=True,
|
sort_keys=True,
|
||||||
indent=4,
|
indent=4,
|
||||||
separators=(',', ': '),
|
separators=(',', ': '),
|
||||||
)
|
)
|
||||||
except (TypeError, ValueError):
|
|
||||||
pass
|
|
||||||
return self.__class__(self._field, value, self.errors, self._prefix)
|
return self.__class__(self._field, value, self.errors, self._prefix)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user