Limit try block to code expected to raise an exception

In the off chance that the additional code in decoder()/encoder() raise
a KeyError or AttributeError, a bug could be hidden.
This commit is contained in:
Jon Dufresne 2020-01-26 07:08:58 -08:00
parent a0a9b76ab5
commit bd71374cd2

View File

@ -401,15 +401,17 @@ def _getdecoder(mode, decoder_name, args, extra=()):
try:
decoder = DECODERS[decoder_name]
return decoder(mode, *args + extra)
except KeyError:
pass
else:
return decoder(mode, *args + extra)
try:
# get decoder
decoder = getattr(core, decoder_name + "_decoder")
return decoder(mode, *args + extra)
except AttributeError:
raise OSError("decoder %s not available" % decoder_name)
return decoder(mode, *args + extra)
def _getencoder(mode, encoder_name, args, extra=()):
@ -422,15 +424,17 @@ def _getencoder(mode, encoder_name, args, extra=()):
try:
encoder = ENCODERS[encoder_name]
return encoder(mode, *args + extra)
except KeyError:
pass
else:
return encoder(mode, *args + extra)
try:
# get encoder
encoder = getattr(core, encoder_name + "_encoder")
return encoder(mode, *args + extra)
except AttributeError:
raise OSError("encoder %s not available" % encoder_name)
return encoder(mode, *args + extra)
# --------------------------------------------------------------------