Skip to content

Commit fa71877

Browse files
spenczarfischor
authored andcommitted
Use context manager form of tempfiles to manage closure
run black
1 parent 8545b81 commit fa71877

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

protogen/test.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,25 +56,25 @@ def run_plugin(
5656
# Open stdin and stdout for the plugin.
5757
# We need SpoledTemporaryFile(mode="w+t")._file because its a TextIOWrapper
5858
# which sys.stdin and sys.stdout also are.
59-
fake_stdin = tempfile.SpooledTemporaryFile(mode="w+t")._file
60-
fake_stdin.buffer.write(req.SerializeToString())
61-
fake_stdin.flush()
62-
fake_stdin.seek(0)
63-
fake_stdout = tempfile.SpooledTemporaryFile(mode="w+t")._file
59+
with tempfile.SpooledTemporaryFile(mode="w+t") as tmp_stdin:
60+
with tempfile.SpooledTemporaryFile(mode="w+t") as tmp_stdout:
61+
fake_stdin = tmp_stdin._file
62+
fake_stdin.buffer.write(req.SerializeToString())
63+
fake_stdin.flush()
64+
fake_stdin.seek(0)
6465

65-
_stdin, sys.stdin = sys.stdin, fake_stdin
66-
_stdout, sys.stdout = sys.stdout, fake_stdout
66+
fake_stdout = tmp_stdout._file
6767

68-
# Call the plugin under test.
69-
import_module(plugin)
68+
_stdin, sys.stdin = sys.stdin, fake_stdin
69+
_stdout, sys.stdout = sys.stdout, fake_stdout
7070

71-
fake_stdout.seek(0)
72-
resp = google.protobuf.compiler.plugin_pb2.CodeGeneratorResponse.FromString(
73-
fake_stdout.buffer.read()
74-
)
71+
# Call the plugin under test.
72+
import_module(plugin)
7573

76-
fake_stdin.close() # will remove tmp files
77-
fake_stdout.close()
74+
fake_stdout.seek(0)
75+
resp = google.protobuf.compiler.plugin_pb2.CodeGeneratorResponse.FromString(
76+
fake_stdout.buffer.read()
77+
)
7878

7979
# Reset stdin and stdout.
8080
sys.stdin = _stdin

0 commit comments

Comments
 (0)