replace try/except with context manager

This commit is contained in:
ahzam 2022-09-28 01:18:34 +05:00
parent 68d0ca6e7e
commit 7e95987bfd

View File

@ -6,7 +6,9 @@ on the response, such as JSON encoded data or HTML output.
REST framework also provides an HTML renderer that renders the browsable API. REST framework also provides an HTML renderer that renders the browsable API.
""" """
import base64 import base64
import contextlib
from collections import OrderedDict from collections import OrderedDict
from urllib import parse from urllib import parse
@ -72,11 +74,8 @@ class JSONRenderer(BaseRenderer):
# then pretty print the result. # then pretty print the result.
# Note that we coerce `indent=0` into `indent=None`. # Note that we coerce `indent=0` into `indent=None`.
base_media_type, params = parse_header_parameters(accepted_media_type) base_media_type, params = parse_header_parameters(accepted_media_type)
try: with contextlib.suppress(KeyError, ValueError, TypeError):
return zero_as_none(max(min(int(params['indent']), 8), 0)) return zero_as_none(max(min(int(params['indent']), 8), 0))
except (KeyError, ValueError, TypeError):
pass
# If 'indent' is provided in the context, then pretty print the result. # If 'indent' is provided in the context, then pretty print the result.
# E.g. If we're being called by the BrowsableAPIRenderer. # E.g. If we're being called by the BrowsableAPIRenderer.
return renderer_context.get('indent', None) return renderer_context.get('indent', None)