mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2026-09-03 20:52:55 -05:00
Fix #2185: Use with syntax to open files on testing.
This commit is contained in:
+7
-16
@@ -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):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user