diff --git a/examples/contrib/run_swag.py b/examples/contrib/run_swag.py index 70dcca3b0b..bfa1cd1666 100644 --- a/examples/contrib/run_swag.py +++ b/examples/contrib/run_swag.py @@ -44,7 +44,7 @@ from transformers import ( try: from torch.utils.tensorboard import SummaryWriter -except: +except ImportError: from tensorboardX import SummaryWriter diff --git a/examples/distillation/distiller.py b/examples/distillation/distiller.py index a957b1a094..16d73ece3c 100644 --- a/examples/distillation/distiller.py +++ b/examples/distillation/distiller.py @@ -37,7 +37,7 @@ from utils import logger try: from torch.utils.tensorboard import SummaryWriter -except: +except ImportError: from tensorboardX import SummaryWriter diff --git a/examples/distillation/run_squad_w_distillation.py b/examples/distillation/run_squad_w_distillation.py index 11524e388e..ca73419689 100644 --- a/examples/distillation/run_squad_w_distillation.py +++ b/examples/distillation/run_squad_w_distillation.py @@ -67,7 +67,7 @@ from ..utils_squad_evaluate import main as evaluate_on_squad try: from torch.utils.tensorboard import SummaryWriter -except: +except ImportError: from tensorboardX import SummaryWriter diff --git a/examples/mm-imdb/run_mmimdb.py b/examples/mm-imdb/run_mmimdb.py index e87555f7da..24ad82190c 100644 --- a/examples/mm-imdb/run_mmimdb.py +++ b/examples/mm-imdb/run_mmimdb.py @@ -62,7 +62,7 @@ from utils_mmimdb import ImageEncoder, JsonlDataset, collate_fn, get_image_trans try: from torch.utils.tensorboard import SummaryWriter -except: +except ImportError: from tensorboardX import SummaryWriter diff --git a/examples/pplm/run_pplm.py b/examples/pplm/run_pplm.py index ec848323e9..8c405b56ad 100644 --- a/examples/pplm/run_pplm.py +++ b/examples/pplm/run_pplm.py @@ -697,8 +697,8 @@ def run_pplm_example( print("= Perturbed generated text {} =".format(i + 1)) print(pert_gen_text) print() - except: - pass + except Exception as exc: + print("Ignoring error while generating perturbed text:", exc) # keep the prefix, perturbed seq, original seq for each index generated_texts.append((tokenized_cond_text, pert_gen_tok_text, unpert_gen_tok_text)) diff --git a/examples/pplm/run_pplm_discrim_train.py b/examples/pplm/run_pplm_discrim_train.py index 287715e53b..1c21c56c86 100644 --- a/examples/pplm/run_pplm_discrim_train.py +++ b/examples/pplm/run_pplm_discrim_train.py @@ -285,7 +285,7 @@ def train_discriminator( for i, line in enumerate(f): try: data.append(eval(line)) - except: + except Exception: print("Error evaluating line {}: {}".format(i, line)) continue x = [] @@ -303,7 +303,7 @@ def train_discriminator( continue x.append(seq) y.append(d["label"]) - except: + except Exception: print("Error evaluating / tokenizing" " line {}, skipping it".format(i)) pass @@ -343,7 +343,7 @@ def train_discriminator( continue x.append(seq) y.append(int(np.sum(d["label"]) > 0)) - except: + except Exception: print("Error evaluating / tokenizing" " line {}, skipping it".format(i)) pass @@ -402,7 +402,7 @@ def train_discriminator( x.append(seq) y.append(class2idx[label]) - except: + except Exception: print("Error tokenizing line {}, skipping it".format(i)) pass diff --git a/examples/run_glue.py b/examples/run_glue.py index d70e20f330..f981900594 100644 --- a/examples/run_glue.py +++ b/examples/run_glue.py @@ -64,7 +64,7 @@ from transformers import glue_processors as processors try: from torch.utils.tensorboard import SummaryWriter -except: +except ImportError: from tensorboardX import SummaryWriter diff --git a/examples/run_lm_finetuning.py b/examples/run_lm_finetuning.py index f916897d00..ae3d68dada 100644 --- a/examples/run_lm_finetuning.py +++ b/examples/run_lm_finetuning.py @@ -63,7 +63,7 @@ from transformers import ( try: from torch.utils.tensorboard import SummaryWriter -except: +except ImportError: from tensorboardX import SummaryWriter diff --git a/examples/run_multiple_choice.py b/examples/run_multiple_choice.py index 27b2e51e6f..82f5a7ee4c 100644 --- a/examples/run_multiple_choice.py +++ b/examples/run_multiple_choice.py @@ -48,7 +48,7 @@ from utils_multiple_choice import convert_examples_to_features, processors try: from torch.utils.tensorboard import SummaryWriter -except: +except ImportError: from tensorboardX import SummaryWriter diff --git a/examples/run_squad.py b/examples/run_squad.py index 9dbc39cb7b..acf4d0eecd 100644 --- a/examples/run_squad.py +++ b/examples/run_squad.py @@ -64,7 +64,7 @@ from transformers.data.processors.squad import SquadResult, SquadV1Processor, Sq try: from torch.utils.tensorboard import SummaryWriter -except: +except ImportError: from tensorboardX import SummaryWriter diff --git a/examples/run_xnli.py b/examples/run_xnli.py index bc1789f1d0..f772bb5cbb 100644 --- a/examples/run_xnli.py +++ b/examples/run_xnli.py @@ -52,7 +52,7 @@ from transformers import xnli_processors as processors try: from torch.utils.tensorboard import SummaryWriter -except: +except ImportError: from tensorboardX import SummaryWriter diff --git a/templates/adding_a_new_example_script/run_xxx.py b/templates/adding_a_new_example_script/run_xxx.py index e7e95ede67..eec29b59b7 100644 --- a/templates/adding_a_new_example_script/run_xxx.py +++ b/templates/adding_a_new_example_script/run_xxx.py @@ -63,7 +63,7 @@ from utils_squad_evaluate import main as evaluate_on_squad try: from torch.utils.tensorboard import SummaryWriter -except: +except ImportError: from tensorboardX import SummaryWriter diff --git a/transformers/__init__.py b/transformers/__init__.py index 8e52771f2c..f5f961a9e0 100755 --- a/transformers/__init__.py +++ b/transformers/__init__.py @@ -6,12 +6,12 @@ __version__ = "2.3.0" # and: https://github.com/tensorflow/tensorflow/issues/26691#issuecomment-500369493 try: import absl.logging - +except ImportError: + pass +else: absl.logging.set_verbosity("info") absl.logging.set_stderrthreshold("info") absl.logging._warn_preinit_stderr = False -except: - pass import logging diff --git a/transformers/hf_api.py b/transformers/hf_api.py index 7380a784c7..b1c7650835 100644 --- a/transformers/hf_api.py +++ b/transformers/hf_api.py @@ -205,10 +205,8 @@ class HfFolder: try: with open(cls.path_token, "r") as f: return f.read() - except: - # this is too wide. When Py2 is dead use: - # `except FileNotFoundError:` instead - return None + except FileNotFoundError: + pass @classmethod def delete_token(cls): @@ -218,5 +216,5 @@ class HfFolder: """ try: os.remove(cls.path_token) - except: - return + except FileNotFoundError: + pass diff --git a/transformers/modeling_utils.py b/transformers/modeling_utils.py index 79b7dafc10..d09a4880b3 100644 --- a/transformers/modeling_utils.py +++ b/transformers/modeling_utils.py @@ -439,7 +439,7 @@ class PreTrainedModel(nn.Module): if state_dict is None and not from_tf: try: state_dict = torch.load(resolved_archive_file, map_location="cpu") - except: + except Exception: raise OSError( "Unable to load weights from pytorch checkpoint file. " "If you tried to load a PyTorch model from a TF 2.0 checkpoint, please set from_tf=True. " diff --git a/transformers/tests/modeling_tf_common_test.py b/transformers/tests/modeling_tf_common_test.py index 6f2d62cc94..ac43e7e875 100644 --- a/transformers/tests/modeling_tf_common_test.py +++ b/transformers/tests/modeling_tf_common_test.py @@ -333,13 +333,13 @@ class TFCommonTestCases: # We used to fall back to just synthetically creating a dummy tensor of ones: try: x = wte(input_ids, mode="embedding") - except: + except Exception: try: x = wte([input_ids], mode="embedding") - except: + except Exception: try: x = wte([input_ids, None, None, None], mode="embedding") - except: + except Exception: if hasattr(self.model_tester, "embedding_size"): x = tf.ones(input_ids.shape + [self.model_tester.embedding_size], dtype=tf.dtypes.float32) else: diff --git a/transformers/tokenization_ctrl.py b/transformers/tokenization_ctrl.py index 4c8d9c96bb..24036b422a 100644 --- a/transformers/tokenization_ctrl.py +++ b/transformers/tokenization_ctrl.py @@ -168,11 +168,12 @@ class CTRLTokenizer(PreTrainedTokenizer): while i < len(word): try: j = word.index(first, i) - new_word.extend(word[i:j]) - i = j - except: + except ValueError: new_word.extend(word[i:]) break + else: + new_word.extend(word[i:j]) + i = j if word[i] == first and i < len(word) - 1 and word[i + 1] == second: new_word.append(first + second) diff --git a/transformers/tokenization_gpt2.py b/transformers/tokenization_gpt2.py index 06da888506..c8f97f0526 100644 --- a/transformers/tokenization_gpt2.py +++ b/transformers/tokenization_gpt2.py @@ -178,11 +178,12 @@ class GPT2Tokenizer(PreTrainedTokenizer): while i < len(word): try: j = word.index(first, i) - new_word.extend(word[i:j]) - i = j - except: + except ValueError: new_word.extend(word[i:]) break + else: + new_word.extend(word[i:j]) + i = j if word[i] == first and i < len(word) - 1 and word[i + 1] == second: new_word.append(first + second) diff --git a/transformers/tokenization_openai.py b/transformers/tokenization_openai.py index 013d8cbc66..9c4c485483 100644 --- a/transformers/tokenization_openai.py +++ b/transformers/tokenization_openai.py @@ -136,11 +136,12 @@ class OpenAIGPTTokenizer(PreTrainedTokenizer): while i < len(word): try: j = word.index(first, i) - new_word.extend(word[i:j]) - i = j - except: + except ValueError: new_word.extend(word[i:]) break + else: + new_word.extend(word[i:j]) + i = j if word[i] == first and i < len(word) - 1 and word[i + 1] == second: new_word.append(first + second) diff --git a/transformers/tokenization_xlm.py b/transformers/tokenization_xlm.py index 4651629312..9d315f8801 100644 --- a/transformers/tokenization_xlm.py +++ b/transformers/tokenization_xlm.py @@ -683,11 +683,12 @@ class XLMTokenizer(PreTrainedTokenizer): while i < len(word): try: j = word.index(first, i) - new_word.extend(word[i:j]) - i = j - except: + except ValueError: new_word.extend(word[i:]) break + else: + new_word.extend(word[i:j]) + i = j if word[i] == first and i < len(word) - 1 and word[i + 1] == second: new_word.append(first + second)