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

Side by Side Diff: Lib/test/test_os.py

Issue 10639: reindent.py converts newlines to platform default
Patch Set: Created 8 years, 8 months ago
Left:
Right:
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 unified diff | Download patch
« no previous file with comments | « Lib/test/test_optparse.py ('k') | Lib/test/test_pkgimport.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # As a test suite for the os module, this is woefully inadequate, but this 1 # As a test suite for the os module, this is woefully inadequate, but this
2 # does add tests for a few functions which have been determined to be more 2 # does add tests for a few functions which have been determined to be more
3 # portable than they had been thought to be. 3 # portable than they had been thought to be.
4 4
5 import os 5 import os
6 import errno 6 import errno
7 import unittest 7 import unittest
8 import warnings 8 import warnings
9 import sys 9 import sys
10 import signal 10 import signal
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
107 'test specific to the Windows console') 107 'test specific to the Windows console')
108 def test_write_windows_console(self): 108 def test_write_windows_console(self):
109 # Issue #11395: the Windows console returns an error (12: not enough 109 # Issue #11395: the Windows console returns an error (12: not enough
110 # space error) on writing into stdout if stdout mode is binary and the 110 # space error) on writing into stdout if stdout mode is binary and the
111 # length is greater than 66,000 bytes (or less, depending on heap 111 # length is greater than 66,000 bytes (or less, depending on heap
112 # usage). 112 # usage).
113 code = "print('x' * 100000)" 113 code = "print('x' * 100000)"
114 self.write_windows_console(sys.executable, "-c", code) 114 self.write_windows_console(sys.executable, "-c", code)
115 self.write_windows_console(sys.executable, "-u", "-c", code) 115 self.write_windows_console(sys.executable, "-u", "-c", code)
116 116
117
118 class TemporaryFileTests(unittest.TestCase):
119 def setUp(self):
120 self.files = []
121 os.mkdir(support.TESTFN)
122
123 def tearDown(self):
124 for name in self.files:
125 os.unlink(name)
126 os.rmdir(support.TESTFN)
127
128 def check_tempfile(self, name):
129 # make sure it doesn't already exist:
130 self.assertFalse(os.path.exists(name),
131 "file already exists for temporary file")
132 # make sure we can create the file
133 open(name, "w")
134 self.files.append(name)
135
136 def test_tempnam(self):
137 if not hasattr(os, "tempnam"):
138 return
139 warnings.filterwarnings("ignore", "tempnam", RuntimeWarning,
140 r"test_os$")
141 self.check_tempfile(os.tempnam())
142
143 name = os.tempnam(support.TESTFN)
144 self.check_tempfile(name)
145
146 name = os.tempnam(support.TESTFN, "pfx")
147 self.assertTrue(os.path.basename(name)[:3] == "pfx")
148 self.check_tempfile(name)
149
150 def test_tmpfile(self):
151 if not hasattr(os, "tmpfile"):
152 return
153 # As with test_tmpnam() below, the Windows implementation of tmpfile()
154 # attempts to create a file in the root directory of the current drive.
155 # On Vista and Server 2008, this test will always fail for normal users
156 # as writing to the root directory requires elevated privileges. With
157 # XP and below, the semantics of tmpfile() are the same, but the user
158 # running the test is more likely to have administrative privileges on
159 # their account already. If that's the case, then os.tmpfile() should
160 # work. In order to make this test as useful as possible, rather than
161 # trying to detect Windows versions or whether or not the user has the
162 # right permissions, just try and create a file in the root directory
163 # and see if it raises a 'Permission denied' OSError. If it does, then
164 # test that a subsequent call to os.tmpfile() raises the same error. If
165 # it doesn't, assume we're on XP or below and the user running the test
166 # has administrative privileges, and proceed with the test as normal.
167 if sys.platform == 'win32':
168 name = '\\python_test_os_test_tmpfile.txt'
169 if os.path.exists(name):
170 os.remove(name)
171 try:
172 fp = open(name, 'w')
173 except IOError as first:
174 # open() failed, assert tmpfile() fails in the same way.
175 # Although open() raises an IOError and os.tmpfile() raises an
176 # OSError(), 'args' will be (13, 'Permission denied') in both
177 # cases.
178 try:
179 fp = os.tmpfile()
180 except OSError as second:
181 self.assertEqual(first.args, second.args)
182 else:
183 self.fail("expected os.tmpfile() to raise OSError")
184 return
185 else:
186 # open() worked, therefore, tmpfile() should work. Close our
187 # dummy file and proceed with the test as normal.
188 fp.close()
189 os.remove(name)
190
191 fp = os.tmpfile()
192 fp.write("foobar")
193 fp.seek(0,0)
194 s = fp.read()
195 fp.close()
196 self.assertTrue(s == "foobar")
197
198 def test_tmpnam(self):
199 if not hasattr(os, "tmpnam"):
200 return
201 warnings.filterwarnings("ignore", "tmpnam", RuntimeWarning,
202 r"test_os$")
203 name = os.tmpnam()
204 if sys.platform in ("win32",):
205 # The Windows tmpnam() seems useless. From the MS docs:
206 #
207 # The character string that tmpnam creates consists of
208 # the path prefix, defined by the entry P_tmpdir in the
209 # file STDIO.H, followed by a sequence consisting of the
210 # digit characters '0' through '9'; the numerical value
211 # of this string is in the range 1 - 65,535. Changing the
212 # definitions of L_tmpnam or P_tmpdir in STDIO.H does not
213 # change the operation of tmpnam.
214 #
215 # The really bizarre part is that, at least under MSVC6,
216 # P_tmpdir is "\\". That is, the path returned refers to
217 # the root of the current drive. That's a terrible place to
218 # put temp files, and, depending on privileges, the user
219 # may not even be able to open a file in the root directory.
220 self.assertFalse(os.path.exists(name),
221 "file already exists for temporary file")
222 else:
223 self.check_tempfile(name)
224
117 def fdopen_helper(self, *args): 225 def fdopen_helper(self, *args):
118 fd = os.open(support.TESTFN, os.O_RDONLY) 226 fd = os.open(support.TESTFN, os.O_RDONLY)
119 f = os.fdopen(fd, *args) 227 fp2 = os.fdopen(fd, *args)
120 f.close() 228 fp2.close()
121 229
122 def test_fdopen(self): 230 def test_fdopen(self):
123 fd = os.open(support.TESTFN, os.O_CREAT|os.O_RDWR)
124 os.close(fd)
125
126 self.fdopen_helper() 231 self.fdopen_helper()
127 self.fdopen_helper('r') 232 self.fdopen_helper('r')
128 self.fdopen_helper('r', 100) 233 self.fdopen_helper('r', 100)
129 234
130
131 # Test attributes on return values from os.*stat* family. 235 # Test attributes on return values from os.*stat* family.
132 class StatAttributeTests(unittest.TestCase): 236 class StatAttributeTests(unittest.TestCase):
133 def setUp(self): 237 def setUp(self):
134 os.mkdir(support.TESTFN) 238 os.mkdir(support.TESTFN)
135 self.fname = os.path.join(support.TESTFN, "f1") 239 self.fname = os.path.join(support.TESTFN, "f1")
136 f = open(self.fname, 'wb') 240 f = open(self.fname, 'wb')
137 f.write(b"ABC") 241 f.write(b"ABC")
138 f.close() 242 f.close()
139 243
140 def tearDown(self): 244 def tearDown(self):
(...skipping 442 matching lines...) Expand 10 before | Expand all | Expand 10 after
583 # If the tests failed, the bottom-most directory ('../dir6') 687 # If the tests failed, the bottom-most directory ('../dir6')
584 # may not have been created, so we look for the outermost directory 688 # may not have been created, so we look for the outermost directory
585 # that exists. 689 # that exists.
586 while not os.path.exists(path) and path != support.TESTFN: 690 while not os.path.exists(path) and path != support.TESTFN:
587 path = os.path.dirname(path) 691 path = os.path.dirname(path)
588 692
589 os.removedirs(path) 693 os.removedirs(path)
590 694
591 class DevNullTests(unittest.TestCase): 695 class DevNullTests(unittest.TestCase):
592 def test_devnull(self): 696 def test_devnull(self):
593 with open(os.devnull, 'wb') as f: 697 f = open(os.devnull, 'w')
594 f.write(b'hello') 698 f.write('hello')
595 f.close() 699 f.close()
596 with open(os.devnull, 'rb') as f: 700 f = open(os.devnull, 'r')
597 self.assertEqual(f.read(), b'') 701 self.assertEqual(f.read(), '')
702 f.close()
598 703
599 class URandomTests(unittest.TestCase): 704 class URandomTests(unittest.TestCase):
600 def test_urandom(self): 705 def test_urandom(self):
601 try: 706 try:
602 self.assertEqual(len(os.urandom(1)), 1) 707 self.assertEqual(len(os.urandom(1)), 1)
603 self.assertEqual(len(os.urandom(10)), 10) 708 self.assertEqual(len(os.urandom(10)), 10)
604 self.assertEqual(len(os.urandom(100)), 100) 709 self.assertEqual(len(os.urandom(100)), 100)
605 self.assertEqual(len(os.urandom(1000)), 1000) 710 self.assertEqual(len(os.urandom(1000)), 1000)
606 except NotImplementedError: 711 except NotImplementedError:
607 pass 712 pass
(...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after
917 add_filename(support.TESTFN_UNICODE) 1022 add_filename(support.TESTFN_UNICODE)
918 if support.TESTFN_UNENCODABLE: 1023 if support.TESTFN_UNENCODABLE:
919 add_filename(support.TESTFN_UNENCODABLE) 1024 add_filename(support.TESTFN_UNENCODABLE)
920 if not bytesfn: 1025 if not bytesfn:
921 self.skipTest("couldn't create any non-ascii filename") 1026 self.skipTest("couldn't create any non-ascii filename")
922 1027
923 self.unicodefn = set() 1028 self.unicodefn = set()
924 os.mkdir(self.dir) 1029 os.mkdir(self.dir)
925 try: 1030 try:
926 for fn in bytesfn: 1031 for fn in bytesfn:
927 support.create_empty_file(os.path.join(self.bdir, fn)) 1032 f = open(os.path.join(self.bdir, fn), "w")
1033 f.close()
928 fn = os.fsdecode(fn) 1034 fn = os.fsdecode(fn)
929 if fn in self.unicodefn: 1035 if fn in self.unicodefn:
930 raise ValueError("duplicate filename") 1036 raise ValueError("duplicate filename")
931 self.unicodefn.add(fn) 1037 self.unicodefn.add(fn)
932 except: 1038 except:
933 shutil.rmtree(self.dir) 1039 shutil.rmtree(self.dir)
934 raise 1040 raise
935 1041
936 def tearDown(self): 1042 def tearDown(self):
937 shutil.rmtree(self.dir) 1043 shutil.rmtree(self.dir)
938 1044
939 def test_listdir(self): 1045 def test_listdir(self):
940 expected = self.unicodefn 1046 expected = self.unicodefn
941 found = set(os.listdir(self.dir)) 1047 found = set(os.listdir(self.dir))
942 self.assertEqual(found, expected) 1048 self.assertEqual(found, expected)
943 1049
944 def test_open(self): 1050 def test_open(self):
945 for fn in self.unicodefn: 1051 for fn in self.unicodefn:
946 f = open(os.path.join(self.dir, fn), 'rb') 1052 f = open(os.path.join(self.dir, fn))
947 f.close() 1053 f.close()
948 1054
949 def test_stat(self): 1055 def test_stat(self):
950 for fn in self.unicodefn: 1056 for fn in self.unicodefn:
951 os.stat(os.path.join(self.dir, fn)) 1057 os.stat(os.path.join(self.dir, fn))
952 else: 1058 else:
953 class PosixUidGidTests(unittest.TestCase): 1059 class PosixUidGidTests(unittest.TestCase):
954 pass 1060 pass
955 class Pep383Tests(unittest.TestCase): 1061 class Pep383Tests(unittest.TestCase):
956 pass 1062 pass
(...skipping 542 matching lines...) Expand 10 before | Expand all | Expand 10 after
1499 if hasattr(os, "SF_NODISKIO"): 1605 if hasattr(os, "SF_NODISKIO"):
1500 def test_flags(self): 1606 def test_flags(self):
1501 try: 1607 try:
1502 os.sendfile(self.sockno, self.fileno, 0, 4096, 1608 os.sendfile(self.sockno, self.fileno, 0, 4096,
1503 flags=os.SF_NODISKIO) 1609 flags=os.SF_NODISKIO)
1504 except OSError as err: 1610 except OSError as err:
1505 if err.errno not in (errno.EBUSY, errno.EAGAIN): 1611 if err.errno not in (errno.EBUSY, errno.EAGAIN):
1506 raise 1612 raise
1507 1613
1508 1614
1509 @support.reap_threads
1510 def test_main(): 1615 def test_main():
1511 support.run_unittest( 1616 support.run_unittest(
1512 FileTests, 1617 FileTests,
1513 StatAttributeTests, 1618 StatAttributeTests,
1514 EnvironTests, 1619 EnvironTests,
1515 WalkTests, 1620 WalkTests,
1516 MakedirTests, 1621 MakedirTests,
1517 DevNullTests, 1622 DevNullTests,
1518 URandomTests, 1623 URandomTests,
1519 ExecTests, 1624 ExecTests,
1520 Win32ErrorTests, 1625 Win32ErrorTests,
1521 TestInvalidFD, 1626 TestInvalidFD,
1522 PosixUidGidTests, 1627 PosixUidGidTests,
1523 Pep383Tests, 1628 Pep383Tests,
1524 Win32KillTests, 1629 Win32KillTests,
1525 Win32SymlinkTests, 1630 Win32SymlinkTests,
1526 FSEncodingTests, 1631 FSEncodingTests,
1527 PidTests, 1632 PidTests,
1528 LoginTests, 1633 LoginTests,
1529 LinkTests, 1634 LinkTests,
1530 TestSendfile, 1635 TestSendfile,
1531 ProgramPriorityTests, 1636 ProgramPriorityTests,
1532 ) 1637 )
1533 1638
1534 if __name__ == "__main__": 1639 if __name__ == "__main__":
1535 test_main() 1640 test_main()
OLDNEW
« 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+