mirror of
https://github.com/django/django.git
synced 2025-11-07 07:15:35 +00:00
Fixed #27818 -- Replaced try/except/pass with contextlib.suppress().
This commit is contained in:
@@ -8,6 +8,7 @@ import itertools
|
||||
import os
|
||||
import re
|
||||
import uuid
|
||||
from contextlib import suppress
|
||||
from decimal import Decimal, DecimalException
|
||||
from io import BytesIO
|
||||
from urllib.parse import urlsplit, urlunsplit
|
||||
@@ -1086,7 +1087,7 @@ class FilePathField(ChoiceField):
|
||||
f = os.path.join(root, f)
|
||||
self.choices.append((f, f.replace(path, "", 1)))
|
||||
else:
|
||||
try:
|
||||
with suppress(OSError):
|
||||
for f in sorted(os.listdir(self.path)):
|
||||
if f == '__pycache__':
|
||||
continue
|
||||
@@ -1095,8 +1096,6 @@ class FilePathField(ChoiceField):
|
||||
(self.allow_folders and os.path.isdir(full_file))) and
|
||||
(self.match is None or self.match_re.search(f))):
|
||||
self.choices.append((full_file, f))
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
self.widget.choices = self.choices
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ Form classes
|
||||
|
||||
import copy
|
||||
from collections import OrderedDict
|
||||
from contextlib import suppress
|
||||
|
||||
from django.core.exceptions import NON_FIELD_ERRORS, ValidationError
|
||||
# BoundField is imported for backwards compatibility in Django 1.9
|
||||
@@ -125,10 +126,8 @@ class BaseForm:
|
||||
return
|
||||
fields = OrderedDict()
|
||||
for key in field_order:
|
||||
try:
|
||||
with suppress(KeyError): # ignore unknown fields
|
||||
fields[key] = self.fields.pop(key)
|
||||
except KeyError: # ignore unknown fields
|
||||
pass
|
||||
fields.update(self.fields) # add remaining fields in original order
|
||||
self.fields = fields
|
||||
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
from contextlib import suppress
|
||||
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.forms import Form
|
||||
from django.forms.fields import BooleanField, IntegerField
|
||||
@@ -160,10 +162,8 @@ class BaseFormSet:
|
||||
defaults['data'] = self.data
|
||||
defaults['files'] = self.files
|
||||
if self.initial and 'initial' not in kwargs:
|
||||
try:
|
||||
with suppress(IndexError):
|
||||
defaults['initial'] = self.initial[i]
|
||||
except IndexError:
|
||||
pass
|
||||
# Allow extra forms to be empty, unless they're part of
|
||||
# the minimum forms.
|
||||
if i >= self.initial_form_count() and i >= self.min_num:
|
||||
|
||||
@@ -4,6 +4,7 @@ and database field objects.
|
||||
"""
|
||||
|
||||
from collections import OrderedDict
|
||||
from contextlib import suppress
|
||||
from itertools import chain
|
||||
|
||||
from django.core.exceptions import (
|
||||
@@ -588,10 +589,8 @@ class BaseModelFormSet(BaseFormSet):
|
||||
kwargs['instance'] = self.get_queryset()[i]
|
||||
if i >= self.initial_form_count() and self.initial_extra:
|
||||
# Set initial values for extra forms
|
||||
try:
|
||||
with suppress(IndexError):
|
||||
kwargs['initial'] = self.initial_extra[i - self.initial_form_count()]
|
||||
except IndexError:
|
||||
pass
|
||||
return super()._construct_form(i, **kwargs)
|
||||
|
||||
def get_queryset(self):
|
||||
|
||||
@@ -5,6 +5,7 @@ HTML Widget classes
|
||||
import copy
|
||||
import datetime
|
||||
import re
|
||||
from contextlib import suppress
|
||||
from itertools import chain
|
||||
|
||||
from django.conf import settings
|
||||
@@ -618,10 +619,8 @@ class ChoiceWidget(Widget):
|
||||
def value_from_datadict(self, data, files, name):
|
||||
getter = data.get
|
||||
if self.allow_multiple_selected:
|
||||
try:
|
||||
with suppress(AttributeError):
|
||||
getter = data.getlist
|
||||
except AttributeError:
|
||||
pass
|
||||
return getter(name)
|
||||
|
||||
def format_value(self, value):
|
||||
@@ -977,12 +976,13 @@ class SelectDateWidget(Widget):
|
||||
year, month, day = value.year, value.month, value.day
|
||||
elif isinstance(value, str):
|
||||
if settings.USE_L10N:
|
||||
input_format = get_format('DATE_INPUT_FORMATS')[0]
|
||||
try:
|
||||
input_format = get_format('DATE_INPUT_FORMATS')[0]
|
||||
d = datetime.datetime.strptime(value, input_format)
|
||||
year, month, day = d.year, d.month, d.day
|
||||
except ValueError:
|
||||
pass
|
||||
else:
|
||||
year, month, day = d.year, d.month, d.day
|
||||
match = self.date_re.match(value)
|
||||
if match:
|
||||
year, month, day = [int(val) for val in match.groups()]
|
||||
|
||||
Reference in New Issue
Block a user