From f25444cb223b1211082ac0b9882f4972db5c1f1c Mon Sep 17 00:00:00 2001 From: Sudharsan S T Date: Wed, 14 Apr 2021 20:01:04 +0530 Subject: [PATCH] Close open files to suppress ResourceWarning (#11240) Co-authored-by: Sudharsan Thirumalai --- examples/legacy/seq2seq/run_distributed_eval.py | 3 ++- examples/research_projects/seq2seq-distillation/run_eval.py | 3 ++- src/transformers/convert_slow_tokenizer.py | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/examples/legacy/seq2seq/run_distributed_eval.py b/examples/legacy/seq2seq/run_distributed_eval.py index 90a348078f..655807ba17 100755 --- a/examples/legacy/seq2seq/run_distributed_eval.py +++ b/examples/legacy/seq2seq/run_distributed_eval.py @@ -204,7 +204,8 @@ def run_generate(): save_json(preds, save_path) return tgt_file = Path(args.data_dir).joinpath(args.type_path + ".target") - labels = [x.rstrip() for x in open(tgt_file).readlines()][: len(preds)] + with open(tgt_file) as f: + labels = [x.rstrip() for x in f.readlines()][: len(preds)] # Calculate metrics, save metrics, and save _generations.txt calc_bleu = "translation" in args.task diff --git a/examples/research_projects/seq2seq-distillation/run_eval.py b/examples/research_projects/seq2seq-distillation/run_eval.py index 910d430bdd..de752c7df1 100755 --- a/examples/research_projects/seq2seq-distillation/run_eval.py +++ b/examples/research_projects/seq2seq-distillation/run_eval.py @@ -115,7 +115,8 @@ def run_generate(verbose=True): parsed_args = parse_numeric_n_bool_cl_kwargs(rest) if parsed_args and verbose: print(f"parsed the following generate kwargs: {parsed_args}") - examples = [" " + x.rstrip() if "t5" in args.model_name else x.rstrip() for x in open(args.input_path).readlines()] + with open(args.input_path) as f: + examples = [" " + x.rstrip() if "t5" in args.model_name else x.rstrip() for x in f.readlines()] if args.n_obs > 0: examples = examples[: args.n_obs] Path(args.save_path).parent.mkdir(exist_ok=True) diff --git a/src/transformers/convert_slow_tokenizer.py b/src/transformers/convert_slow_tokenizer.py index 680f910d37..be9e6fe891 100644 --- a/src/transformers/convert_slow_tokenizer.py +++ b/src/transformers/convert_slow_tokenizer.py @@ -305,7 +305,8 @@ class SpmConverter(Converter): from .utils import sentencepiece_model_pb2 as model_pb2 m = model_pb2.ModelProto() - m.ParseFromString(open(self.original_tokenizer.vocab_file, "rb").read()) + with open(self.original_tokenizer.vocab_file, "rb") as f: + m.ParseFromString(f.read()) self.proto = m def vocab(self, proto):