mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-07-27 00:19:53 +03:00
replace try/except with context manager
If the desire is to simply suppress an error, rather than perform some sort of branching logic, the Python standard library has a paradigm for that: contextlib.suppress()
This commit is contained in:
parent
73aeae3a0f
commit
da500e361a
|
@ -1157,20 +1157,14 @@ class DateTimeField(Field):
|
||||||
return self.enforce_timezone(value)
|
return self.enforce_timezone(value)
|
||||||
|
|
||||||
for input_format in input_formats:
|
for input_format in input_formats:
|
||||||
|
with contextlib.suppress(ValueError, TypeError):
|
||||||
if input_format.lower() == ISO_8601:
|
if input_format.lower() == ISO_8601:
|
||||||
try:
|
|
||||||
parsed = parse_datetime(value)
|
parsed = parse_datetime(value)
|
||||||
if parsed is not None:
|
if parsed is not None:
|
||||||
return self.enforce_timezone(parsed)
|
return self.enforce_timezone(parsed)
|
||||||
except (ValueError, TypeError):
|
|
||||||
pass
|
|
||||||
else:
|
else:
|
||||||
try:
|
|
||||||
parsed = self.datetime_parser(value, input_format)
|
parsed = self.datetime_parser(value, input_format)
|
||||||
return self.enforce_timezone(parsed)
|
return self.enforce_timezone(parsed)
|
||||||
except (ValueError, TypeError):
|
|
||||||
pass
|
|
||||||
|
|
||||||
humanized_format = humanize_datetime.datetime_formats(input_formats)
|
humanized_format = humanize_datetime.datetime_formats(input_formats)
|
||||||
self.fail('invalid', format=humanized_format)
|
self.fail('invalid', format=humanized_format)
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,8 @@
|
||||||
Pagination serializers determine the structure of the output that should
|
Pagination serializers determine the structure of the output that should
|
||||||
be used for paginated responses.
|
be used for paginated responses.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import contextlib
|
||||||
from base64 import b64decode, b64encode
|
from base64 import b64decode, b64encode
|
||||||
from collections import OrderedDict, namedtuple
|
from collections import OrderedDict, namedtuple
|
||||||
from urllib import parse
|
from urllib import parse
|
||||||
|
@ -257,15 +259,12 @@ class PageNumberPagination(BasePagination):
|
||||||
|
|
||||||
def get_page_size(self, request):
|
def get_page_size(self, request):
|
||||||
if self.page_size_query_param:
|
if self.page_size_query_param:
|
||||||
try:
|
with contextlib.suppress(KeyError, ValueError):
|
||||||
return _positive_int(
|
return _positive_int(
|
||||||
request.query_params[self.page_size_query_param],
|
request.query_params[self.page_size_query_param],
|
||||||
strict=True,
|
strict=True,
|
||||||
cutoff=self.max_page_size
|
cutoff=self.max_page_size
|
||||||
)
|
)
|
||||||
except (KeyError, ValueError):
|
|
||||||
pass
|
|
||||||
|
|
||||||
return self.page_size
|
return self.page_size
|
||||||
|
|
||||||
def get_next_link(self):
|
def get_next_link(self):
|
||||||
|
@ -430,15 +429,12 @@ class LimitOffsetPagination(BasePagination):
|
||||||
|
|
||||||
def get_limit(self, request):
|
def get_limit(self, request):
|
||||||
if self.limit_query_param:
|
if self.limit_query_param:
|
||||||
try:
|
with contextlib.suppress(KeyError, ValueError):
|
||||||
return _positive_int(
|
return _positive_int(
|
||||||
request.query_params[self.limit_query_param],
|
request.query_params[self.limit_query_param],
|
||||||
strict=True,
|
strict=True,
|
||||||
cutoff=self.max_limit
|
cutoff=self.max_limit
|
||||||
)
|
)
|
||||||
except (KeyError, ValueError):
|
|
||||||
pass
|
|
||||||
|
|
||||||
return self.default_limit
|
return self.default_limit
|
||||||
|
|
||||||
def get_offset(self, request):
|
def get_offset(self, request):
|
||||||
|
@ -680,15 +676,12 @@ class CursorPagination(BasePagination):
|
||||||
|
|
||||||
def get_page_size(self, request):
|
def get_page_size(self, request):
|
||||||
if self.page_size_query_param:
|
if self.page_size_query_param:
|
||||||
try:
|
with contextlib.suppress(KeyError, ValueError):
|
||||||
return _positive_int(
|
return _positive_int(
|
||||||
request.query_params[self.page_size_query_param],
|
request.query_params[self.page_size_query_param],
|
||||||
strict=True,
|
strict=True,
|
||||||
cutoff=self.max_page_size
|
cutoff=self.max_page_size
|
||||||
)
|
)
|
||||||
except (KeyError, ValueError):
|
|
||||||
pass
|
|
||||||
|
|
||||||
return self.page_size
|
return self.page_size
|
||||||
|
|
||||||
def get_next_link(self):
|
def get_next_link(self):
|
||||||
|
|
|
@ -4,8 +4,10 @@ Parsers are used to parse the content of incoming HTTP requests.
|
||||||
They give us a generic way of being able to handle various media types
|
They give us a generic way of being able to handle various media types
|
||||||
on the request, such as form content or json encoded data.
|
on the request, such as form content or json encoded data.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import codecs
|
import codecs
|
||||||
|
|
||||||
|
import contextlib
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.core.files.uploadhandler import StopFutureHandlers
|
from django.core.files.uploadhandler import StopFutureHandlers
|
||||||
from django.http import QueryDict
|
from django.http import QueryDict
|
||||||
|
@ -193,17 +195,9 @@ class FileUploadParser(BaseParser):
|
||||||
Detects the uploaded file name. First searches a 'filename' url kwarg.
|
Detects the uploaded file name. First searches a 'filename' url kwarg.
|
||||||
Then tries to parse Content-Disposition header.
|
Then tries to parse Content-Disposition header.
|
||||||
"""
|
"""
|
||||||
try:
|
with contextlib.suppress(KeyError):
|
||||||
return parser_context['kwargs']['filename']
|
return parser_context['kwargs']['filename']
|
||||||
except KeyError:
|
with contextlib.suppress(AttributeError, KeyError, ValueError):
|
||||||
pass
|
|
||||||
|
|
||||||
try:
|
|
||||||
meta = parser_context['request'].META
|
meta = parser_context['request'].META
|
||||||
disposition, params = parse_header_parameters(meta['HTTP_CONTENT_DISPOSITION'])
|
disposition, params = parse_header_parameters(meta['HTTP_CONTENT_DISPOSITION'])
|
||||||
if 'filename*' in params:
|
return params['filename*'] if 'filename*' in params else params['filename']
|
||||||
return params['filename*']
|
|
||||||
else:
|
|
||||||
return params['filename']
|
|
||||||
except (AttributeError, KeyError, ValueError):
|
|
||||||
pass
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user