diff -r f2cb733c9a37 Lib/test/test_shutil.py --- a/Lib/test/test_shutil.py Tue Aug 23 00:58:21 2011 +0200 +++ b/Lib/test/test_shutil.py Tue Aug 23 10:28:25 2011 +0200 @@ -65,11 +65,14 @@ If *path* is a tuple instead of a string, os.path.join will be used to make a path. If *binary* is true, the file will be opened in binary mode. + + Returns the path it wrote to. """ if isinstance(path, tuple): path = os.path.join(*path) with open(path, 'wb' if binary else 'w') as fp: fp.write(content) + return path def read_file(path, binary=False): """Return contents from a file located at *path*. @@ -262,15 +265,12 @@ return # bug 851123. os.mkdir(TESTFN) - src = os.path.join(TESTFN, 'cheese') dst = os.path.join(TESTFN, 'shop') try: - with open(src, 'w') as f: - f.write('cheddar') + src = write_file((TESTFN, 'cheese'), '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) @@ -279,18 +279,15 @@ def test_dont_copy_file_onto_symlink_to_itself(self): # bug 851123. os.mkdir(TESTFN) - src = os.path.join(TESTFN, 'cheese') dst = os.path.join(TESTFN, 'shop') try: - with open(src, 'w') as f: - f.write('cheddar') + src = write_file((TESTFN, 'cheese'), '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) @@ -778,10 +775,8 @@ filename = "foo" self.src_dir = tempfile.mkdtemp() self.dst_dir = tempfile.mkdtemp() - self.src_file = os.path.join(self.src_dir, filename) + self.src_file = write_file((self.src_dir, filename), b'spam', True) self.dst_file = os.path.join(self.dst_dir, filename) - with open(self.src_file, "wb") as f: - f.write(b"spam") def tearDown(self): for d in (self.src_dir, self.dst_dir):