diff options
author | shimizukawa <shimizukawa@gmail.com> | 2015-12-20 08:07:40 +0900 |
---|---|---|
committer | shimizukawa <shimizukawa@gmail.com> | 2015-12-20 08:13:02 +0900 |
commit | 91fe0b6ceb340ff7a7a83f0e195d5cbbadb830e6 (patch) | |
tree | e6671874e25e0a0d43804d3a00c5f7e2d85414df /tests/path.py | |
parent | 9669f2a48b3c91ba4fa9398a0a607a5f75f0b5ae (diff) | |
download | sphinx-git-91fe0b6ceb340ff7a7a83f0e195d5cbbadb830e6.tar.gz |
Fix #2185: Use with syntax to open files on testing.
Diffstat (limited to 'tests/path.py')
-rwxr-xr-x | tests/path.py | 23 |
1 files changed, 7 insertions, 16 deletions
diff --git a/tests/path.py b/tests/path.py index 9e6baa36e..68984e0ae 100755 --- a/tests/path.py +++ b/tests/path.py @@ -126,37 +126,31 @@ class path(text_type): def utime(self, arg): os.utime(self, arg) + def open(self, mode='r', **kwargs): + return open(self, mode, **kwargs) + def write_text(self, text, encoding='utf-8', **kwargs): """ Writes the given `text` to the file. """ if isinstance(text, bytes): text = text.decode(encoding) - f = open(self, 'w', encoding=encoding, **kwargs) - try: + with open(self, 'w', encoding=encoding, **kwargs) as f: f.write(text) - finally: - f.close() def text(self, encoding='utf-8', **kwargs): """ Returns the text in the file. """ - f = open(self, mode='U', encoding=encoding, **kwargs) - try: + with open(self, mode='U', encoding=encoding, **kwargs) as f: return f.read() - finally: - f.close() def bytes(self): """ Returns the bytes in the file. """ - f = open(self, mode='rb') - try: + with open(self, mode='rb') as f: return f.read() - finally: - f.close() def write_bytes(self, bytes, append=False): """ @@ -169,11 +163,8 @@ class path(text_type): mode = 'ab' else: mode = 'wb' - f = open(self, mode=mode) - try: + with open(self, mode=mode) as f: f.write(bytes) - finally: - f.close() def exists(self): """ |