replace try/except with context manager

This commit is contained in:
ahzam 2022-09-28 01:32:43 +05:00
parent e6f59e7260
commit b78d218fa6

View File

@ -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)