Fix #2185: Use with syntax to open files on testing.

This commit is contained in:
shimizukawa
2015-12-20 08:13:02 +09:00
parent 9669f2a48b
commit 91fe0b6ceb
2 changed files with 15 additions and 48 deletions
+7 -16
View File
@@ -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):
"""