Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(8)

Unified Diff: Lib/test/test_os.py

Issue 10639: reindent.py converts newlines to platform default
Patch Set: Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « Lib/test/test_optparse.py ('k') | Lib/test/test_pkgimport.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
--- a/Lib/test/test_os.py Tue Jul 26 09:37:46 2011 +0300
+++ b/Lib/test/test_os.py Mon Jul 25 09:47:18 2011 -0400
@@ -114,20 +114,124 @@
self.write_windows_console(sys.executable, "-c", code)
self.write_windows_console(sys.executable, "-u", "-c", code)
+
+class TemporaryFileTests(unittest.TestCase):
+ def setUp(self):
+ self.files = []
+ os.mkdir(support.TESTFN)
+
+ def tearDown(self):
+ for name in self.files:
+ os.unlink(name)
+ os.rmdir(support.TESTFN)
+
+ def check_tempfile(self, name):
+ # make sure it doesn't already exist:
+ self.assertFalse(os.path.exists(name),
+ "file already exists for temporary file")
+ # make sure we can create the file
+ open(name, "w")
+ self.files.append(name)
+
+ def test_tempnam(self):
+ if not hasattr(os, "tempnam"):
+ return
+ warnings.filterwarnings("ignore", "tempnam", RuntimeWarning,
+ r"test_os$")
+ self.check_tempfile(os.tempnam())
+
+ name = os.tempnam(support.TESTFN)
+ self.check_tempfile(name)
+
+ name = os.tempnam(support.TESTFN, "pfx")
+ self.assertTrue(os.path.basename(name)[:3] == "pfx")
+ self.check_tempfile(name)
+
+ def test_tmpfile(self):
+ if not hasattr(os, "tmpfile"):
+ return
+ # As with test_tmpnam() below, the Windows implementation of tmpfile()
+ # attempts to create a file in the root directory of the current drive.
+ # On Vista and Server 2008, this test will always fail for normal users
+ # as writing to the root directory requires elevated privileges. With
+ # XP and below, the semantics of tmpfile() are the same, but the user
+ # running the test is more likely to have administrative privileges on
+ # their account already. If that's the case, then os.tmpfile() should
+ # work. In order to make this test as useful as possible, rather than
+ # trying to detect Windows versions or whether or not the user has the
+ # right permissions, just try and create a file in the root directory
+ # and see if it raises a 'Permission denied' OSError. If it does, then
+ # test that a subsequent call to os.tmpfile() raises the same error. If
+ # it doesn't, assume we're on XP or below and the user running the test
+ # has administrative privileges, and proceed with the test as normal.
+ if sys.platform == 'win32':
+ name = '\\python_test_os_test_tmpfile.txt'
+ if os.path.exists(name):
+ os.remove(name)
+ try:
+ fp = open(name, 'w')
+ except IOError as first:
+ # open() failed, assert tmpfile() fails in the same way.
+ # Although open() raises an IOError and os.tmpfile() raises an
+ # OSError(), 'args' will be (13, 'Permission denied') in both
+ # cases.
+ try:
+ fp = os.tmpfile()
+ except OSError as second:
+ self.assertEqual(first.args, second.args)
+ else:
+ self.fail("expected os.tmpfile() to raise OSError")
+ return
+ else:
+ # open() worked, therefore, tmpfile() should work. Close our
+ # dummy file and proceed with the test as normal.
+ fp.close()
+ os.remove(name)
+
+ fp = os.tmpfile()
+ fp.write("foobar")
+ fp.seek(0,0)
+ s = fp.read()
+ fp.close()
+ self.assertTrue(s == "foobar")
+
+ def test_tmpnam(self):
+ if not hasattr(os, "tmpnam"):
+ return
+ warnings.filterwarnings("ignore", "tmpnam", RuntimeWarning,
+ r"test_os$")
+ name = os.tmpnam()
+ if sys.platform in ("win32",):
+ # The Windows tmpnam() seems useless. From the MS docs:
+ #
+ # The character string that tmpnam creates consists of
+ # the path prefix, defined by the entry P_tmpdir in the
+ # file STDIO.H, followed by a sequence consisting of the
+ # digit characters '0' through '9'; the numerical value
+ # of this string is in the range 1 - 65,535. Changing the
+ # definitions of L_tmpnam or P_tmpdir in STDIO.H does not
+ # change the operation of tmpnam.
+ #
+ # The really bizarre part is that, at least under MSVC6,
+ # P_tmpdir is "\\". That is, the path returned refers to
+ # the root of the current drive. That's a terrible place to
+ # put temp files, and, depending on privileges, the user
+ # may not even be able to open a file in the root directory.
+ self.assertFalse(os.path.exists(name),
+ "file already exists for temporary file")
+ else:
+ self.check_tempfile(name)
+
def fdopen_helper(self, *args):
fd = os.open(support.TESTFN, os.O_RDONLY)
- f = os.fdopen(fd, *args)
- f.close()
+ fp2 = os.fdopen(fd, *args)
+ fp2.close()
def test_fdopen(self):
- fd = os.open(support.TESTFN, os.O_CREAT|os.O_RDWR)
- os.close(fd)
-
self.fdopen_helper()
self.fdopen_helper('r')
self.fdopen_helper('r', 100)
-
# Test attributes on return values from os.*stat* family.
class StatAttributeTests(unittest.TestCase):
def setUp(self):
@@ -590,11 +694,12 @@
class DevNullTests(unittest.TestCase):
def test_devnull(self):
- with open(os.devnull, 'wb') as f:
- f.write(b'hello')
- f.close()
- with open(os.devnull, 'rb') as f:
- self.assertEqual(f.read(), b'')
+ f = open(os.devnull, 'w')
+ f.write('hello')
+ f.close()
+ f = open(os.devnull, 'r')
+ self.assertEqual(f.read(), '')
+ f.close()
class URandomTests(unittest.TestCase):
def test_urandom(self):
@@ -924,7 +1029,8 @@
os.mkdir(self.dir)
try:
for fn in bytesfn:
- support.create_empty_file(os.path.join(self.bdir, fn))
+ f = open(os.path.join(self.bdir, fn), "w")
+ f.close()
fn = os.fsdecode(fn)
if fn in self.unicodefn:
raise ValueError("duplicate filename")
@@ -943,7 +1049,7 @@
def test_open(self):
for fn in self.unicodefn:
- f = open(os.path.join(self.dir, fn), 'rb')
+ f = open(os.path.join(self.dir, fn))
f.close()
def test_stat(self):
@@ -1506,7 +1612,6 @@
raise
-@support.reap_threads
def test_main():
support.run_unittest(
FileTests,
« no previous file with comments | « Lib/test/test_optparse.py ('k') | Lib/test/test_pkgimport.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+