summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_reprlib.py
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@haypocalc.com>2011-06-30 21:25:47 (GMT)
committerVictor Stinner <victor.stinner@haypocalc.com>2011-06-30 21:25:47 (GMT)
commitbf816223dfe8f1d36a020b4bc02060b8d1ce7d26 (patch)
tree3651ee82210cb2110b6808a2545471cac321a22f /Lib/test/test_reprlib.py
parent61600cb0c357fcdca048cee586865a26417dbd70 (diff)
downloadcpython-bf816223dfe8f1d36a020b4bc02060b8d1ce7d26.zip
cpython-bf816223dfe8f1d36a020b4bc02060b8d1ce7d26.tar.gz
cpython-bf816223dfe8f1d36a020b4bc02060b8d1ce7d26.tar.bz2
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).
Diffstat (limited to 'Lib/test/test_reprlib.py')
-rw-r--r--Lib/test/test_reprlib.py23
1 files changed, 11 insertions, 12 deletions
diff --git a/Lib/test/test_reprlib.py b/Lib/test/test_reprlib.py
index e476941..439fa33 100644
--- a/Lib/test/test_reprlib.py
+++ b/Lib/test/test_reprlib.py
@@ -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
''')