diff options
author | Yilei Yang <yileiyang@google.com> | 2021-11-16 10:20:42 -0800 |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2021-11-16 10:21:15 -0800 |
commit | 737055c5e3c0b57d461265dba19130bc8795a382 (patch) | |
tree | 6031e02f82b209303d6b96f199cb782cd6861ab7 | |
parent | 19bf98da82d45fd7a43d58ad41f3b3bb743f3516 (diff) | |
download | absl-py-737055c5e3c0b57d461265dba19130bc8795a382.tar.gz |
Simplify this code to use a `with` block.
PiperOrigin-RevId: 410287536
Change-Id: I50b3999e8da21aefa65314a121fddf8e832d57b0
-rw-r--r-- | absl/testing/absltest.py | 14 |
1 files changed, 5 insertions, 9 deletions
diff --git a/absl/testing/absltest.py b/absl/testing/absltest.py index 4779663..ffad3da 100644 --- a/absl/testing/absltest.py +++ b/absl/testing/absltest.py @@ -2394,16 +2394,12 @@ def _setup_sharding(custom_loader=None): # to query whether a test binary implements the test sharding protocol. if 'TEST_SHARD_STATUS_FILE' in os.environ: try: - f = None - try: - f = open(os.environ['TEST_SHARD_STATUS_FILE'], 'w') + with open(os.environ['TEST_SHARD_STATUS_FILE'], 'w') as f: f.write('') - except IOError: - sys.stderr.write('Error opening TEST_SHARD_STATUS_FILE (%s). Exiting.' - % os.environ['TEST_SHARD_STATUS_FILE']) - sys.exit(1) - finally: - if f is not None: f.close() + except IOError: + sys.stderr.write('Error opening TEST_SHARD_STATUS_FILE (%s). Exiting.' + % os.environ['TEST_SHARD_STATUS_FILE']) + sys.exit(1) base_loader = custom_loader or TestLoader() if 'TEST_TOTAL_SHARDS' not in os.environ: |