mirror of
https://git.linux-kernel.at/oliver/ivatar.git
synced 2025-11-18 22:18:02 +00:00
Reduce occurrences of bare exceptions
* replace bare exceptions with specific ones, if possible
* use "exc" instead of "e" as exception variable
("exc" is the most popular choice in the standard library)
This commit is contained in:
@@ -107,7 +107,7 @@ DEFAULT_FROM_EMAIL = 'ivatar@mg.linux-kernel.at'
|
||||
|
||||
try:
|
||||
from ivatar.settings import DATABASES
|
||||
except Exception: # pragma: no cover
|
||||
except ImportError: # pragma: no cover
|
||||
DATABASES = [] # pragma: no cover
|
||||
|
||||
if 'default' not in DATABASES:
|
||||
|
||||
@@ -176,9 +176,9 @@ class Photo(BaseAccountModel):
|
||||
try:
|
||||
img = Image.open(BytesIO(self.data))
|
||||
# Testing? Ideas anyone?
|
||||
except Exception as e: # pylint: disable=invalid-name,broad-except
|
||||
except Exception as exc: # pylint: disable=broad-except
|
||||
# For debugging only
|
||||
print('Exception caught: %s' % e)
|
||||
print('Exception caught: %s' % exc)
|
||||
return False
|
||||
self.format = file_format(img.format)
|
||||
if not self.format:
|
||||
@@ -537,8 +537,7 @@ class DjangoOpenIDStore(OpenIDStore):
|
||||
try:
|
||||
# pylint: disable=no-member
|
||||
expires = association.getExpiresIn()
|
||||
# pylint: disable=invalid-name,broad-except,unused-variable
|
||||
except Exception as e:
|
||||
except AttributeError:
|
||||
expires = association.expiresIn
|
||||
if expires == 0:
|
||||
self.removeAssociation(server_url, assoc.handle)
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
Reading libravatar export
|
||||
'''
|
||||
|
||||
import binascii
|
||||
from io import BytesIO
|
||||
import gzip
|
||||
import xml.etree.ElementTree
|
||||
@@ -46,22 +47,23 @@ def read_gzdata(gzdata=None):
|
||||
if photo.tag == '{%s}photo' % SCHEMAROOT:
|
||||
try:
|
||||
data = base64.decodebytes(bytes(photo.text, 'utf-8'))
|
||||
except Exception as e: # pylint: disable=broad-except,invalid-name
|
||||
except binascii.Error as exc:
|
||||
print('Cannot decode photo; Encoding: %s, Format: %s: %s' % (
|
||||
photo.attrib['encoding'], photo.attrib['format'], e))
|
||||
photo.attrib['encoding'], photo.attrib['format'], exc))
|
||||
continue
|
||||
try:
|
||||
Image.open(BytesIO(data))
|
||||
except Exception as exc: # pylint: disable=broad-except
|
||||
print('Cannot decode photo; Encoding: %s, Format: %s: %s' % (
|
||||
photo.attrib['encoding'], photo.attrib['format'], exc))
|
||||
continue
|
||||
else:
|
||||
# If it is a working image, we can use it
|
||||
photo.text.replace('\n', '')
|
||||
photos.append({
|
||||
'data': photo.text,
|
||||
'format': photo.attrib['format'],
|
||||
})
|
||||
except Exception as e: # pylint: disable=broad-except,invalid-name
|
||||
print('Cannot decode photo; Encoding: %s, Format: %s: %s' % (
|
||||
photo.attrib['encoding'], photo.attrib['format'], e))
|
||||
continue
|
||||
|
||||
return {
|
||||
'emails': emails,
|
||||
|
||||
@@ -4,9 +4,11 @@ View classes for ivatar/ivataraccount/
|
||||
from io import BytesIO
|
||||
from urllib.request import urlopen
|
||||
import base64
|
||||
import binascii
|
||||
|
||||
from PIL import Image
|
||||
|
||||
from django.db.models import ProtectedError
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.contrib.auth.models import User
|
||||
from django.utils.decorators import method_decorator
|
||||
@@ -298,7 +300,7 @@ class ImportPhotoView(SuccessMessageMixin, TemplateView):
|
||||
if 'email_id' in kwargs:
|
||||
try:
|
||||
addr = ConfirmedEmail.objects.get(pk=kwargs['email_id']).email
|
||||
except Exception: # pylint: disable=broad-except
|
||||
except ConfirmedEmail.ObjectDoesNotExist:
|
||||
messages.error(
|
||||
self.request,
|
||||
_('Address does not exist'))
|
||||
@@ -316,10 +318,12 @@ class ImportPhotoView(SuccessMessageMixin, TemplateView):
|
||||
email=addr,
|
||||
default=404,
|
||||
)
|
||||
try:
|
||||
if libravatar_service_url:
|
||||
# if it doesn't work, it will be caught by except
|
||||
if libravatar_service_url:
|
||||
try:
|
||||
urlopen(libravatar_service_url)
|
||||
except OSError as exc:
|
||||
print('Exception caught during photo import: {}'.format(exc))
|
||||
else:
|
||||
context['photos'].append({
|
||||
'service_url': libravatar_service_url,
|
||||
'thumbnail_url': libravatar_service_url + '?s=80',
|
||||
@@ -328,9 +332,6 @@ class ImportPhotoView(SuccessMessageMixin, TemplateView):
|
||||
'height': 80,
|
||||
'service_name': 'Libravatar',
|
||||
})
|
||||
except Exception as e: # pylint: disable=broad-except,invalid-name
|
||||
print('Exception caught during photo import: %s' % e)
|
||||
pass
|
||||
|
||||
return context
|
||||
|
||||
@@ -423,7 +424,7 @@ class DeletePhotoView(SuccessMessageMixin, View):
|
||||
photo = self.model.objects.get( # pylint: disable=no-member
|
||||
pk=kwargs['pk'], user=request.user)
|
||||
photo.delete()
|
||||
except Exception: # pylint: disable=broad-except
|
||||
except (self.model.DoesNotExist, ProtectedError):
|
||||
messages.error(
|
||||
request,
|
||||
_('No such image or no permission to delete it'))
|
||||
@@ -558,12 +559,12 @@ class RedirectOpenIDView(View):
|
||||
|
||||
try:
|
||||
auth_request = openid_consumer.begin(user_url)
|
||||
except consumer.DiscoveryFailure as e: # pylint: disable=invalid-name
|
||||
messages.error(request, _('OpenID discovery failed: %s' % e))
|
||||
except consumer.DiscoveryFailure as exc:
|
||||
messages.error(request, _('OpenID discovery failed: %s' % exc))
|
||||
return HttpResponseRedirect(reverse_lazy('profile'))
|
||||
except UnicodeDecodeError as e: # pragma: no cover pylint: disable=invalid-name
|
||||
except UnicodeDecodeError as exc: # pragma: no cover
|
||||
msg = _('OpenID discovery failed (userid=%s) for %s: %s' %
|
||||
(request.user.id, user_url.encode('utf-8'), e))
|
||||
(request.user.id, user_url.encode('utf-8'), exc))
|
||||
print(msg)
|
||||
messages.error(request, msg)
|
||||
|
||||
@@ -684,14 +685,14 @@ class CropPhotoView(TemplateView):
|
||||
if 'email' in request.POST:
|
||||
try:
|
||||
email = ConfirmedEmail.objects.get(email=request.POST['email'])
|
||||
except Exception: # pylint: disable=broad-except
|
||||
except ConfirmedEmail.DoesNotExist:
|
||||
pass # Ignore automatic assignment
|
||||
|
||||
if 'openid' in request.POST:
|
||||
try:
|
||||
openid = ConfirmedOpenId.objects.get( # pylint: disable=no-member
|
||||
openid=request.POST['openid'])
|
||||
except Exception: # pylint: disable=broad-except
|
||||
except ConfirmedOpenId.DoesNotExist:
|
||||
pass # Ignore automatic assignment
|
||||
|
||||
return photo.perform_crop(request, dimensions, email, openid)
|
||||
@@ -747,15 +748,16 @@ class UploadLibravatarExportView(SuccessMessageMixin, FormView):
|
||||
email,
|
||||
_('address added successfully,\
|
||||
confirmation mail sent')))
|
||||
except Exception as e: # pylint: disable=broad-except,invalid-name
|
||||
except Exception as exc: # pylint: disable=broad-except
|
||||
# DEBUG
|
||||
print('Exception during adding mail address (%s): %s' % (email, e))
|
||||
print('Exception during adding mail address (%s): %s'
|
||||
% (email, exc))
|
||||
|
||||
if arg.startswith('photo'):
|
||||
try:
|
||||
data = base64.decodebytes(bytes(request.POST[arg], 'utf-8'))
|
||||
except Exception as e: # pylint: disable=broad-except,invalid-name
|
||||
print('Cannot decode photo: %s' % e)
|
||||
except binascii.Error as exc:
|
||||
print('Cannot decode photo: %s' % exc)
|
||||
continue
|
||||
try:
|
||||
pilobj = Image.open(BytesIO(data))
|
||||
@@ -768,8 +770,8 @@ class UploadLibravatarExportView(SuccessMessageMixin, FormView):
|
||||
photo.format = file_format(pilobj.format)
|
||||
photo.data = out.read()
|
||||
photo.save()
|
||||
except Exception as e: # pylint: disable=broad-except,invalid-name
|
||||
print('Exception during save: %s' % e)
|
||||
except Exception as exc: # pylint: disable=broad-except
|
||||
print('Exception during save: %s' % exc)
|
||||
continue
|
||||
|
||||
return HttpResponseRedirect(reverse_lazy('profile'))
|
||||
@@ -799,17 +801,18 @@ class ResendConfirmationMailView(View):
|
||||
try:
|
||||
email = self.model.objects.get( # pylint: disable=no-member
|
||||
user=request.user, id=kwargs['email_id'])
|
||||
except self.model.DoesNotExist: # pragma: no cover # pylint: disable=no-member
|
||||
messages.error(request, _('ID does not exist'))
|
||||
else:
|
||||
try:
|
||||
email.send_confirmation_mail(
|
||||
url=request.build_absolute_uri('/')[:-1])
|
||||
messages.success(
|
||||
request, '%s: %s' %
|
||||
(_('Confirmation mail sent to'), email.email))
|
||||
except Exception as e: # pylint: disable=broad-except,invalid-name
|
||||
except Exception as exc: # pylint: disable=broad-except
|
||||
messages.error(
|
||||
request, '%s %s: %s' %
|
||||
(_('Unable to send confirmation email for'),
|
||||
email.email, e))
|
||||
except self.model.DoesNotExist: # pragma: no cover # pylint: disable=no-member
|
||||
messages.error(request, _('ID does not exist'))
|
||||
email.email, exc))
|
||||
return HttpResponseRedirect(reverse_lazy('profile'))
|
||||
|
||||
Reference in New Issue
Block a user