(original) (raw)

diff -r 1b4fae183da3 Lib/test/test_shutil.py --- a/Lib/test/test_shutil.py Tue Aug 09 18:48:02 2011 -0500 +++ b/Lib/test/test_shutil.py Wed Aug 10 12:07:49 2011 +0200 @@ -59,6 +59,31 @@ os.rename = builtin_rename return wrap +def write_file(path, content='xxx', binary=False): + """Writes a file in the given `path`. + + `path` can be a string or a sequence. + + If optional argument `binary` is set, the file is opened in binary mode. + """ + if isinstance(path, (list, tuple)): + path = os.path.join(*path) + with open(path, 'w' if not binary else 'wb') as f: + f.write(content) + +def read_file(path, binary=False): + """Reads a file from the given path. + + `path` can be a string or a sequence. + + If optional argument `binary` is set, the file is opened in binary mode. + """ + if isinstance(path, (list, tuple)): + path = os.path.join(*path) + with open(path, 'r' if not binary else 'rb') as f: + return f.read() + + class TestShutil(unittest.TestCase): def setUp(self): @@ -71,19 +96,6 @@ d = self.tempdirs.pop() shutil.rmtree(d, os.name in ('nt', 'cygwin')) - def write_file(self, path, content='xxx'): - """Writes a file in the given path. - - - path can be a string or a sequence. - """ - if isinstance(path, (list, tuple)): - path = os.path.join(*path) - f = open(path, 'w') - try: - f.write(content) - finally: - f.close() def mkdtemp(self): """Create a temporary directory that will be cleaned up. @@ -159,24 +171,12 @@ self.assertRaises(OSError, shutil.rmtree, path) os.remove(path) - def _write_data(self, path, data): - f = open(path, "w") - f.write(data) - f.close() - def test_copytree_simple(self): - - def read_data(path): - f = open(path) - data = f.read() - f.close() - return data - src_dir = tempfile.mkdtemp() dst_dir = os.path.join(tempfile.mkdtemp(), 'destination') - self._write_data(os.path.join(src_dir, 'test.txt'), '123') + write_file([src_dir, 'test.txt'], '123') os.mkdir(os.path.join(src_dir, 'test_dir')) - self._write_data(os.path.join(src_dir, 'test_dir', 'test.txt'), '456') + write_file([src_dir, 'test_dir', 'test.txt'], '456') try: shutil.copytree(src_dir, dst_dir) @@ -184,9 +184,9 @@ self.assertTrue(os.path.isdir(os.path.join(dst_dir, 'test_dir'))) self.assertTrue(os.path.isfile(os.path.join(dst_dir, 'test_dir', 'test.txt'))) - actual = read_data(os.path.join(dst_dir, 'test.txt')) + actual = read_file([dst_dir, 'test.txt']) self.assertEqual(actual, '123') - actual = read_data(os.path.join(dst_dir, 'test_dir', 'test.txt')) + actual = read_file([dst_dir, 'test_dir', 'test.txt']) self.assertEqual(actual, '456') finally: for path in ( @@ -204,30 +204,23 @@ shutil.rmtree(path) def test_copytree_with_exclude(self): - - def read_data(path): - f = open(path) - data = f.read() - f.close() - return data - # creating data join = os.path.join exists = os.path.exists src_dir = tempfile.mkdtemp() try: dst_dir = join(tempfile.mkdtemp(), 'destination') - self._write_data(join(src_dir, 'test.txt'), '123') - self._write_data(join(src_dir, 'test.tmp'), '123') + write_file([src_dir, 'test.txt'], '123') + write_file([src_dir, 'test.tmp'], '123') os.mkdir(join(src_dir, 'test_dir')) - self._write_data(join(src_dir, 'test_dir', 'test.txt'), '456') + write_file([src_dir, 'test_dir', 'test.txt'], '456') os.mkdir(join(src_dir, 'test_dir2')) - self._write_data(join(src_dir, 'test_dir2', 'test.txt'), '456') + write_file([src_dir, 'test_dir2', 'test.txt'], '456') os.mkdir(join(src_dir, 'test_dir2', 'subdir')) os.mkdir(join(src_dir, 'test_dir2', 'subdir2')) - self._write_data(join(src_dir, 'test_dir2', 'subdir', 'test.txt'), + write_file([src_dir, 'test_dir2', 'subdir', 'test.txt'], '456') - self._write_data(join(src_dir, 'test_dir2', 'subdir2', 'test.py'), + write_file([src_dir, 'test_dir2', 'subdir2', 'test.py'], '456') @@ -291,12 +284,10 @@ src = os.path.join(TESTFN, 'cheese') dst = os.path.join(TESTFN, 'shop') try: - with open(src, 'w') as f: - f.write('cheddar') + write_file(src, 'cheddar') os.link(src, dst) self.assertRaises(shutil.Error, shutil.copyfile, src, dst) - with open(src, 'r') as f: - self.assertEqual(f.read(), 'cheddar') + self.assertEqual(read_file(src), 'cheddar') os.remove(dst) finally: shutil.rmtree(TESTFN, ignore_errors=True) @@ -308,15 +299,13 @@ src = os.path.join(TESTFN, 'cheese') dst = os.path.join(TESTFN, 'shop') try: - with open(src, 'w') as f: - f.write('cheddar') + write_file(src, 'cheddar') # Using `src` here would mean we end up with a symlink pointing # to TESTFN/TESTFN/cheese, while it should point at # TESTFN/cheese. os.symlink('cheese', dst) self.assertRaises(shutil.Error, shutil.copyfile, src, dst) - with open(src, 'r') as f: - self.assertEqual(f.read(), 'cheddar') + self.assertEqual(read_file(src), 'cheddar') os.remove(dst) finally: shutil.rmtree(TESTFN, ignore_errors=True) @@ -371,9 +360,9 @@ src_dir = self.mkdtemp() dst_dir = os.path.join(self.mkdtemp(), 'destination') - self._write_data(os.path.join(src_dir, 'test.txt'), '123') + write_file([src_dir, 'test.txt'], '123') os.mkdir(os.path.join(src_dir, 'test_dir')) - self._write_data(os.path.join(src_dir, 'test_dir', 'test.txt'), '456') + write_file([src_dir, 'test_dir', 'test.txt'], '456') copied = [] def _copy(src, dst): @@ -390,7 +379,7 @@ dst_dir = os.path.join(self.mkdtemp(), 'destination') os.symlink('IDONTEXIST', os.path.join(src_dir, 'test.txt')) os.mkdir(os.path.join(src_dir, 'test_dir')) - self._write_data(os.path.join(src_dir, 'test_dir', 'test.txt'), '456') + write_file([src_dir, 'test_dir', 'test.txt'], '456') self.assertRaises(Error, shutil.copytree, src_dir, dst_dir) # a dangling symlink is ignored with the proper flag @@ -406,7 +395,7 @@ def _copy_file(self, method): fname = 'test.txt' tmpdir = self.mkdtemp() - self.write_file([tmpdir, fname]) + write_file([tmpdir, fname]) file1 = os.path.join(tmpdir, fname) tmpdir2 = self.mkdtemp() method(file1, tmpdir2) @@ -442,10 +431,10 @@ def test_make_tarball(self): # creating something to tar tmpdir = self.mkdtemp() - self.write_file([tmpdir, 'file1'], 'xxx') - self.write_file([tmpdir, 'file2'], 'xxx') + write_file([tmpdir, 'file1'], 'xxx') + write_file([tmpdir, 'file2'], 'xxx') os.mkdir(os.path.join(tmpdir, 'sub')) - self.write_file([tmpdir, 'sub', 'file3'], 'xxx') + write_file([tmpdir, 'sub', 'file3'], 'xxx') tmpdir2 = self.mkdtemp() # force shutil to create the directory @@ -492,10 +481,10 @@ tmpdir = self.mkdtemp() dist = os.path.join(tmpdir, 'dist') os.mkdir(dist) - self.write_file([dist, 'file1'], 'xxx') - self.write_file([dist, 'file2'], 'xxx') + write_file([dist, 'file1'], 'xxx') + write_file([dist, 'file2'], 'xxx') os.mkdir(os.path.join(dist, 'sub')) - self.write_file([dist, 'sub', 'file3'], 'xxx') + write_file([dist, 'sub', 'file3'], 'xxx') os.mkdir(os.path.join(dist, 'sub2')) tmpdir2 = self.mkdtemp() base_name = os.path.join(tmpdir2, 'archive') @@ -561,8 +550,8 @@ def test_make_zipfile(self): # creating something to tar tmpdir = self.mkdtemp() - self.write_file([tmpdir, 'file1'], 'xxx') - self.write_file([tmpdir, 'file2'], 'xxx') + write_file([tmpdir, 'file1'], 'xxx') + write_file([tmpdir, 'file2'], 'xxx') tmpdir2 = self.mkdtemp() # force shutil to create the directory @@ -747,8 +736,7 @@ self.dst_dir = tempfile.mkdtemp() self.src_file = os.path.join(self.src_dir, filename) self.dst_file = os.path.join(self.dst_dir, filename) - with open(self.src_file, "wb") as f: - f.write(b"spam") + write_file(self.src_file, b'spam', binary=True) def tearDown(self): for d in (self.src_dir, self.dst_dir): @@ -759,11 +747,9 @@ pass def _check_move_file(self, src, dst, real_dst): - with open(src, "rb") as f: - contents = f.read() + contents = read_file(src, binary=True) shutil.move(src, dst) - with open(real_dst, "rb") as f: - self.assertEqual(contents, f.read()) + self.assertEqual(contents, read_file(real_dst, binary=True)) self.assertFalse(os.path.exists(src)) def _check_move_dir(self, src, dst, real_dst): diff -r 1b4fae183da3 Misc/ACKS --- a/Misc/ACKS Tue Aug 09 18:48:02 2011 -0500 +++ b/Misc/ACKS Wed Aug 10 12:07:49 2011 +0200 @@ -838,6 +838,7 @@ Andreas Schawo Neil Schemenauer David Scherer +Hynek Schlawack Bob Schmertz Gregor Schmid Ralf Schmitt