Issue #12451: Add support.create_empty_file()

We don't need to create a temporary buffered binary or text file object just to
create an empty file.

Replace also os.fdopen(handle).close() by os.close(handle).
This commit is contained in:
Victor Stinner 2011-06-30 23:25:47 +02:00
parent 61600cb0c3
commit bf816223df
17 changed files with 50 additions and 53 deletions

View file

@ -8,7 +8,7 @@ import os
import shutil
import unittest
from test.support import run_unittest
from test.support import run_unittest, create_empty_file
from reprlib import repr as r # Don't shadow builtin repr
from reprlib import Repr
from reprlib import recursive_repr
@ -193,10 +193,9 @@ class ReprTests(unittest.TestCase):
r(y)
r(z)
def touch(path, text=''):
fp = open(path, 'w')
fp.write(text)
fp.close()
def write_file(path, text):
with open(path, 'w', encoding='ASCII') as fp:
fp.write(text)
class LongReprTest(unittest.TestCase):
def setUp(self):
@ -206,10 +205,10 @@ class LongReprTest(unittest.TestCase):
# Make the package and subpackage
shutil.rmtree(self.pkgname, ignore_errors=True)
os.mkdir(self.pkgname)
touch(os.path.join(self.pkgname, '__init__.py'))
create_empty_file(os.path.join(self.pkgname, '__init__.py'))
shutil.rmtree(self.subpkgname, ignore_errors=True)
os.mkdir(self.subpkgname)
touch(os.path.join(self.subpkgname, '__init__.py'))
create_empty_file(os.path.join(self.subpkgname, '__init__.py'))
# Remember where we are
self.here = os.getcwd()
sys.path.insert(0, self.here)
@ -231,7 +230,7 @@ class LongReprTest(unittest.TestCase):
def test_module(self):
eq = self.assertEqual
touch(os.path.join(self.subpkgname, self.pkgname + '.py'))
create_empty_file(os.path.join(self.subpkgname, self.pkgname + '.py'))
from areallylongpackageandmodulenametotestreprtruncation.areallylongpackageandmodulenametotestreprtruncation import areallylongpackageandmodulenametotestreprtruncation
eq(repr(areallylongpackageandmodulenametotestreprtruncation),
"<module %r from %r>" % (areallylongpackageandmodulenametotestreprtruncation.__name__, areallylongpackageandmodulenametotestreprtruncation.__file__))
@ -239,7 +238,7 @@ class LongReprTest(unittest.TestCase):
def test_type(self):
eq = self.assertEqual
touch(os.path.join(self.subpkgname, 'foo.py'), '''\
write_file(os.path.join(self.subpkgname, 'foo.py'), '''\
class foo(object):
pass
''')
@ -253,7 +252,7 @@ class foo(object):
pass
def test_class(self):
touch(os.path.join(self.subpkgname, 'bar.py'), '''\
write_file(os.path.join(self.subpkgname, 'bar.py'), '''\
class bar:
pass
''')
@ -262,7 +261,7 @@ class bar:
self.assertEqual(repr(bar.bar), "<class '%s.bar'>" % bar.__name__)
def test_instance(self):
touch(os.path.join(self.subpkgname, 'baz.py'), '''\
write_file(os.path.join(self.subpkgname, 'baz.py'), '''\
class baz:
pass
''')
@ -273,7 +272,7 @@ class baz:
def test_method(self):
eq = self.assertEqual
touch(os.path.join(self.subpkgname, 'qux.py'), '''\
write_file(os.path.join(self.subpkgname, 'qux.py'), '''\
class aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:
def amethod(self): pass
''')