Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_os.py @ 54294:dba767bc3155 legacy-trunk
remove mention of old ctypes version
| author | Benjamin Peterson <benjamin@python.org> |
|---|---|
| date | Sat, 23 May 2009 20:59:09 +0000 |
| parents | 1f8accd1f42a |
| children | 6768b2350c57 |
| rev | line source |
|---|---|
|
18678
a7bc339ee2f3
Start of a test suite for the "os" module -- this only tests the temporary
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
1 # As a test suite for the os module, this is woefully inadequate, but this |
|
a7bc339ee2f3
Start of a test suite for the "os" module -- this only tests the temporary
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
2 # does add tests for a few functions which have been determined to be more |
| 30723 | 3 # portable than they had been thought to be. |
|
18678
a7bc339ee2f3
Start of a test suite for the "os" module -- this only tests the temporary
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
4 |
|
a7bc339ee2f3
Start of a test suite for the "os" module -- this only tests the temporary
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
5 import os |
|
51198
4c9ea647eaf4
check the errno in bad fd cases
Benjamin Peterson <benjamin@python.org>
parents:
50916
diff
changeset
|
6 import errno |
|
18678
a7bc339ee2f3
Start of a test suite for the "os" module -- this only tests the temporary
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
7 import unittest |
|
19403
ac6bfa55fceb
Silence warnings during test_os
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
18787
diff
changeset
|
8 import warnings |
|
38112
be49645e3984
Implement os.{chdir,rename,rmdir,remove} using Win32 directly.
Martin v. Löwis <martin@v.loewis.de>
parents:
34607
diff
changeset
|
9 import sys |
|
28917
1741af48fd9f
Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents:
28869
diff
changeset
|
10 from test import test_support |
|
18678
a7bc339ee2f3
Start of a test suite for the "os" module -- this only tests the temporary
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
11 |
|
19429
9cf669581110
Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents:
19403
diff
changeset
|
12 warnings.filterwarnings("ignore", "tempnam", RuntimeWarning, __name__) |
|
9cf669581110
Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents:
19403
diff
changeset
|
13 warnings.filterwarnings("ignore", "tmpnam", RuntimeWarning, __name__) |
|
9cf669581110
Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents:
19403
diff
changeset
|
14 |
|
38979
6975bd4884eb
Correct arithmetic in access on Win32. Fixes #1513646.
Martin v. Löwis <martin@v.loewis.de>
parents:
38125
diff
changeset
|
15 # Tests creating TESTFN |
|
6975bd4884eb
Correct arithmetic in access on Win32. Fixes #1513646.
Martin v. Löwis <martin@v.loewis.de>
parents:
38125
diff
changeset
|
16 class FileTests(unittest.TestCase): |
|
6975bd4884eb
Correct arithmetic in access on Win32. Fixes #1513646.
Martin v. Löwis <martin@v.loewis.de>
parents:
38125
diff
changeset
|
17 def setUp(self): |
|
6975bd4884eb
Correct arithmetic in access on Win32. Fixes #1513646.
Martin v. Löwis <martin@v.loewis.de>
parents:
38125
diff
changeset
|
18 if os.path.exists(test_support.TESTFN): |
|
6975bd4884eb
Correct arithmetic in access on Win32. Fixes #1513646.
Martin v. Löwis <martin@v.loewis.de>
parents:
38125
diff
changeset
|
19 os.unlink(test_support.TESTFN) |
|
6975bd4884eb
Correct arithmetic in access on Win32. Fixes #1513646.
Martin v. Löwis <martin@v.loewis.de>
parents:
38125
diff
changeset
|
20 tearDown = setUp |
|
6975bd4884eb
Correct arithmetic in access on Win32. Fixes #1513646.
Martin v. Löwis <martin@v.loewis.de>
parents:
38125
diff
changeset
|
21 |
|
6975bd4884eb
Correct arithmetic in access on Win32. Fixes #1513646.
Martin v. Löwis <martin@v.loewis.de>
parents:
38125
diff
changeset
|
22 def test_access(self): |
|
6975bd4884eb
Correct arithmetic in access on Win32. Fixes #1513646.
Martin v. Löwis <martin@v.loewis.de>
parents:
38125
diff
changeset
|
23 f = os.open(test_support.TESTFN, os.O_CREAT|os.O_RDWR) |
|
6975bd4884eb
Correct arithmetic in access on Win32. Fixes #1513646.
Martin v. Löwis <martin@v.loewis.de>
parents:
38125
diff
changeset
|
24 os.close(f) |
|
6975bd4884eb
Correct arithmetic in access on Win32. Fixes #1513646.
Martin v. Löwis <martin@v.loewis.de>
parents:
38125
diff
changeset
|
25 self.assert_(os.access(test_support.TESTFN, os.W_OK)) |
|
38984
27973962fc9f
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
38979
diff
changeset
|
26 |
|
44785
b520d33bee06
#1663329: add os.closerange() to close a range of fds,
Georg Brandl <georg@python.org>
parents:
43082
diff
changeset
|
27 def test_closerange(self): |
|
48267
7a038020c8dd
backport r65723: strengthen test_os.test_closerange
Antoine Pitrou <solipsis@pitrou.net>
parents:
47071
diff
changeset
|
28 first = os.open(test_support.TESTFN, os.O_CREAT|os.O_RDWR) |
|
7a038020c8dd
backport r65723: strengthen test_os.test_closerange
Antoine Pitrou <solipsis@pitrou.net>
parents:
47071
diff
changeset
|
29 # We must allocate two consecutive file descriptors, otherwise |
|
7a038020c8dd
backport r65723: strengthen test_os.test_closerange
Antoine Pitrou <solipsis@pitrou.net>
parents:
47071
diff
changeset
|
30 # it will mess up other file descriptors (perhaps even the three |
|
7a038020c8dd
backport r65723: strengthen test_os.test_closerange
Antoine Pitrou <solipsis@pitrou.net>
parents:
47071
diff
changeset
|
31 # standard ones). |
|
7a038020c8dd
backport r65723: strengthen test_os.test_closerange
Antoine Pitrou <solipsis@pitrou.net>
parents:
47071
diff
changeset
|
32 second = os.dup(first) |
|
7a038020c8dd
backport r65723: strengthen test_os.test_closerange
Antoine Pitrou <solipsis@pitrou.net>
parents:
47071
diff
changeset
|
33 try: |
|
7a038020c8dd
backport r65723: strengthen test_os.test_closerange
Antoine Pitrou <solipsis@pitrou.net>
parents:
47071
diff
changeset
|
34 retries = 0 |
|
7a038020c8dd
backport r65723: strengthen test_os.test_closerange
Antoine Pitrou <solipsis@pitrou.net>
parents:
47071
diff
changeset
|
35 while second != first + 1: |
|
7a038020c8dd
backport r65723: strengthen test_os.test_closerange
Antoine Pitrou <solipsis@pitrou.net>
parents:
47071
diff
changeset
|
36 os.close(first) |
|
7a038020c8dd
backport r65723: strengthen test_os.test_closerange
Antoine Pitrou <solipsis@pitrou.net>
parents:
47071
diff
changeset
|
37 retries += 1 |
|
7a038020c8dd
backport r65723: strengthen test_os.test_closerange
Antoine Pitrou <solipsis@pitrou.net>
parents:
47071
diff
changeset
|
38 if retries > 10: |
|
7a038020c8dd
backport r65723: strengthen test_os.test_closerange
Antoine Pitrou <solipsis@pitrou.net>
parents:
47071
diff
changeset
|
39 # XXX test skipped |
| 54117 | 40 self.skipTest("couldn't allocate two consecutive fds") |
|
48267
7a038020c8dd
backport r65723: strengthen test_os.test_closerange
Antoine Pitrou <solipsis@pitrou.net>
parents:
47071
diff
changeset
|
41 first, second = second, os.dup(second) |
|
7a038020c8dd
backport r65723: strengthen test_os.test_closerange
Antoine Pitrou <solipsis@pitrou.net>
parents:
47071
diff
changeset
|
42 finally: |
|
7a038020c8dd
backport r65723: strengthen test_os.test_closerange
Antoine Pitrou <solipsis@pitrou.net>
parents:
47071
diff
changeset
|
43 os.close(second) |
|
44785
b520d33bee06
#1663329: add os.closerange() to close a range of fds,
Georg Brandl <georg@python.org>
parents:
43082
diff
changeset
|
44 # close a fd that is open, and one that isn't |
|
48267
7a038020c8dd
backport r65723: strengthen test_os.test_closerange
Antoine Pitrou <solipsis@pitrou.net>
parents:
47071
diff
changeset
|
45 os.closerange(first, first + 2) |
|
7a038020c8dd
backport r65723: strengthen test_os.test_closerange
Antoine Pitrou <solipsis@pitrou.net>
parents:
47071
diff
changeset
|
46 self.assertRaises(OSError, os.write, first, "a") |
|
44785
b520d33bee06
#1663329: add os.closerange() to close a range of fds,
Georg Brandl <georg@python.org>
parents:
43082
diff
changeset
|
47 |
|
49094
973bdb2db6ac
Issue #3804: Added test for Issue #2222.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents:
48955
diff
changeset
|
48 def test_rename(self): |
|
973bdb2db6ac
Issue #3804: Added test for Issue #2222.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents:
48955
diff
changeset
|
49 path = unicode(test_support.TESTFN) |
|
973bdb2db6ac
Issue #3804: Added test for Issue #2222.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents:
48955
diff
changeset
|
50 old = sys.getrefcount(path) |
|
973bdb2db6ac
Issue #3804: Added test for Issue #2222.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents:
48955
diff
changeset
|
51 self.assertRaises(TypeError, os.rename, path, 0) |
|
973bdb2db6ac
Issue #3804: Added test for Issue #2222.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents:
48955
diff
changeset
|
52 new = sys.getrefcount(path) |
|
973bdb2db6ac
Issue #3804: Added test for Issue #2222.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents:
48955
diff
changeset
|
53 self.assertEqual(old, new) |
|
973bdb2db6ac
Issue #3804: Added test for Issue #2222.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents:
48955
diff
changeset
|
54 |
|
38979
6975bd4884eb
Correct arithmetic in access on Win32. Fixes #1513646.
Martin v. Löwis <martin@v.loewis.de>
parents:
38125
diff
changeset
|
55 |
|
18678
a7bc339ee2f3
Start of a test suite for the "os" module -- this only tests the temporary
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
56 class TemporaryFileTests(unittest.TestCase): |
|
a7bc339ee2f3
Start of a test suite for the "os" module -- this only tests the temporary
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
57 def setUp(self): |
|
a7bc339ee2f3
Start of a test suite for the "os" module -- this only tests the temporary
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
58 self.files = [] |
|
28917
1741af48fd9f
Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents:
28869
diff
changeset
|
59 os.mkdir(test_support.TESTFN) |
|
18678
a7bc339ee2f3
Start of a test suite for the "os" module -- this only tests the temporary
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
60 |
|
a7bc339ee2f3
Start of a test suite for the "os" module -- this only tests the temporary
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
61 def tearDown(self): |
|
a7bc339ee2f3
Start of a test suite for the "os" module -- this only tests the temporary
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
62 for name in self.files: |
|
a7bc339ee2f3
Start of a test suite for the "os" module -- this only tests the temporary
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
63 os.unlink(name) |
|
28917
1741af48fd9f
Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents:
28869
diff
changeset
|
64 os.rmdir(test_support.TESTFN) |
|
18678
a7bc339ee2f3
Start of a test suite for the "os" module -- this only tests the temporary
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
65 |
|
a7bc339ee2f3
Start of a test suite for the "os" module -- this only tests the temporary
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
66 def check_tempfile(self, name): |
|
a7bc339ee2f3
Start of a test suite for the "os" module -- this only tests the temporary
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
67 # make sure it doesn't already exist: |
|
a7bc339ee2f3
Start of a test suite for the "os" module -- this only tests the temporary
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
68 self.failIf(os.path.exists(name), |
|
a7bc339ee2f3
Start of a test suite for the "os" module -- this only tests the temporary
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
69 "file already exists for temporary file") |
|
a7bc339ee2f3
Start of a test suite for the "os" module -- this only tests the temporary
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
70 # make sure we can create the file |
|
a7bc339ee2f3
Start of a test suite for the "os" module -- this only tests the temporary
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
71 open(name, "w") |
|
a7bc339ee2f3
Start of a test suite for the "os" module -- this only tests the temporary
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
72 self.files.append(name) |
|
a7bc339ee2f3
Start of a test suite for the "os" module -- this only tests the temporary
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
73 |
|
a7bc339ee2f3
Start of a test suite for the "os" module -- this only tests the temporary
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
74 def test_tempnam(self): |
|
a7bc339ee2f3
Start of a test suite for the "os" module -- this only tests the temporary
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
75 if not hasattr(os, "tempnam"): |
|
a7bc339ee2f3
Start of a test suite for the "os" module -- this only tests the temporary
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
76 return |
|
19403
ac6bfa55fceb
Silence warnings during test_os
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
18787
diff
changeset
|
77 warnings.filterwarnings("ignore", "tempnam", RuntimeWarning, |
|
23073
21d252028404
Tighten up some warning filters, and break some dependencies on the
Tim Peters <tim.peters@gmail.com>
parents:
20547
diff
changeset
|
78 r"test_os$") |
|
18678
a7bc339ee2f3
Start of a test suite for the "os" module -- this only tests the temporary
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
79 self.check_tempfile(os.tempnam()) |
|
a7bc339ee2f3
Start of a test suite for the "os" module -- this only tests the temporary
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
80 |
|
28917
1741af48fd9f
Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents:
28869
diff
changeset
|
81 name = os.tempnam(test_support.TESTFN) |
|
18678
a7bc339ee2f3
Start of a test suite for the "os" module -- this only tests the temporary
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
82 self.check_tempfile(name) |
|
a7bc339ee2f3
Start of a test suite for the "os" module -- this only tests the temporary
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
83 |
|
28917
1741af48fd9f
Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents:
28869
diff
changeset
|
84 name = os.tempnam(test_support.TESTFN, "pfx") |
|
18678
a7bc339ee2f3
Start of a test suite for the "os" module -- this only tests the temporary
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
85 self.assert_(os.path.basename(name)[:3] == "pfx") |
|
a7bc339ee2f3
Start of a test suite for the "os" module -- this only tests the temporary
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
86 self.check_tempfile(name) |
|
a7bc339ee2f3
Start of a test suite for the "os" module -- this only tests the temporary
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
87 |
|
a7bc339ee2f3
Start of a test suite for the "os" module -- this only tests the temporary
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
88 def test_tmpfile(self): |
|
a7bc339ee2f3
Start of a test suite for the "os" module -- this only tests the temporary
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
89 if not hasattr(os, "tmpfile"): |
|
a7bc339ee2f3
Start of a test suite for the "os" module -- this only tests the temporary
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
90 return |
|
45681
61d22701a5e5
Patch #2232: os.tmpfile might fail on Windows if the user has no
Martin v. Löwis <martin@v.loewis.de>
parents:
44785
diff
changeset
|
91 # As with test_tmpnam() below, the Windows implementation of tmpfile() |
|
61d22701a5e5
Patch #2232: os.tmpfile might fail on Windows if the user has no
Martin v. Löwis <martin@v.loewis.de>
parents:
44785
diff
changeset
|
92 # attempts to create a file in the root directory of the current drive. |
|
61d22701a5e5
Patch #2232: os.tmpfile might fail on Windows if the user has no
Martin v. Löwis <martin@v.loewis.de>
parents:
44785
diff
changeset
|
93 # On Vista and Server 2008, this test will always fail for normal users |
|
61d22701a5e5
Patch #2232: os.tmpfile might fail on Windows if the user has no
Martin v. Löwis <martin@v.loewis.de>
parents:
44785
diff
changeset
|
94 # as writing to the root directory requires elevated privileges. With |
|
61d22701a5e5
Patch #2232: os.tmpfile might fail on Windows if the user has no
Martin v. Löwis <martin@v.loewis.de>
parents:
44785
diff
changeset
|
95 # XP and below, the semantics of tmpfile() are the same, but the user |
|
61d22701a5e5
Patch #2232: os.tmpfile might fail on Windows if the user has no
Martin v. Löwis <martin@v.loewis.de>
parents:
44785
diff
changeset
|
96 # running the test is more likely to have administrative privileges on |
|
61d22701a5e5
Patch #2232: os.tmpfile might fail on Windows if the user has no
Martin v. Löwis <martin@v.loewis.de>
parents:
44785
diff
changeset
|
97 # their account already. If that's the case, then os.tmpfile() should |
|
61d22701a5e5
Patch #2232: os.tmpfile might fail on Windows if the user has no
Martin v. Löwis <martin@v.loewis.de>
parents:
44785
diff
changeset
|
98 # work. In order to make this test as useful as possible, rather than |
|
61d22701a5e5
Patch #2232: os.tmpfile might fail on Windows if the user has no
Martin v. Löwis <martin@v.loewis.de>
parents:
44785
diff
changeset
|
99 # trying to detect Windows versions or whether or not the user has the |
|
61d22701a5e5
Patch #2232: os.tmpfile might fail on Windows if the user has no
Martin v. Löwis <martin@v.loewis.de>
parents:
44785
diff
changeset
|
100 # right permissions, just try and create a file in the root directory |
|
61d22701a5e5
Patch #2232: os.tmpfile might fail on Windows if the user has no
Martin v. Löwis <martin@v.loewis.de>
parents:
44785
diff
changeset
|
101 # and see if it raises a 'Permission denied' OSError. If it does, then |
|
61d22701a5e5
Patch #2232: os.tmpfile might fail on Windows if the user has no
Martin v. Löwis <martin@v.loewis.de>
parents:
44785
diff
changeset
|
102 # test that a subsequent call to os.tmpfile() raises the same error. If |
|
61d22701a5e5
Patch #2232: os.tmpfile might fail on Windows if the user has no
Martin v. Löwis <martin@v.loewis.de>
parents:
44785
diff
changeset
|
103 # it doesn't, assume we're on XP or below and the user running the test |
|
61d22701a5e5
Patch #2232: os.tmpfile might fail on Windows if the user has no
Martin v. Löwis <martin@v.loewis.de>
parents:
44785
diff
changeset
|
104 # has administrative privileges, and proceed with the test as normal. |
|
61d22701a5e5
Patch #2232: os.tmpfile might fail on Windows if the user has no
Martin v. Löwis <martin@v.loewis.de>
parents:
44785
diff
changeset
|
105 if sys.platform == 'win32': |
|
61d22701a5e5
Patch #2232: os.tmpfile might fail on Windows if the user has no
Martin v. Löwis <martin@v.loewis.de>
parents:
44785
diff
changeset
|
106 name = '\\python_test_os_test_tmpfile.txt' |
|
61d22701a5e5
Patch #2232: os.tmpfile might fail on Windows if the user has no
Martin v. Löwis <martin@v.loewis.de>
parents:
44785
diff
changeset
|
107 if os.path.exists(name): |
|
61d22701a5e5
Patch #2232: os.tmpfile might fail on Windows if the user has no
Martin v. Löwis <martin@v.loewis.de>
parents:
44785
diff
changeset
|
108 os.remove(name) |
|
61d22701a5e5
Patch #2232: os.tmpfile might fail on Windows if the user has no
Martin v. Löwis <martin@v.loewis.de>
parents:
44785
diff
changeset
|
109 try: |
|
61d22701a5e5
Patch #2232: os.tmpfile might fail on Windows if the user has no
Martin v. Löwis <martin@v.loewis.de>
parents:
44785
diff
changeset
|
110 fp = open(name, 'w') |
|
61d22701a5e5
Patch #2232: os.tmpfile might fail on Windows if the user has no
Martin v. Löwis <martin@v.loewis.de>
parents:
44785
diff
changeset
|
111 except IOError, first: |
|
61d22701a5e5
Patch #2232: os.tmpfile might fail on Windows if the user has no
Martin v. Löwis <martin@v.loewis.de>
parents:
44785
diff
changeset
|
112 # open() failed, assert tmpfile() fails in the same way. |
|
61d22701a5e5
Patch #2232: os.tmpfile might fail on Windows if the user has no
Martin v. Löwis <martin@v.loewis.de>
parents:
44785
diff
changeset
|
113 # Although open() raises an IOError and os.tmpfile() raises an |
|
61d22701a5e5
Patch #2232: os.tmpfile might fail on Windows if the user has no
Martin v. Löwis <martin@v.loewis.de>
parents:
44785
diff
changeset
|
114 # OSError(), 'args' will be (13, 'Permission denied') in both |
|
61d22701a5e5
Patch #2232: os.tmpfile might fail on Windows if the user has no
Martin v. Löwis <martin@v.loewis.de>
parents:
44785
diff
changeset
|
115 # cases. |
|
61d22701a5e5
Patch #2232: os.tmpfile might fail on Windows if the user has no
Martin v. Löwis <martin@v.loewis.de>
parents:
44785
diff
changeset
|
116 try: |
|
61d22701a5e5
Patch #2232: os.tmpfile might fail on Windows if the user has no
Martin v. Löwis <martin@v.loewis.de>
parents:
44785
diff
changeset
|
117 fp = os.tmpfile() |
|
61d22701a5e5
Patch #2232: os.tmpfile might fail on Windows if the user has no
Martin v. Löwis <martin@v.loewis.de>
parents:
44785
diff
changeset
|
118 except OSError, second: |
|
61d22701a5e5
Patch #2232: os.tmpfile might fail on Windows if the user has no
Martin v. Löwis <martin@v.loewis.de>
parents:
44785
diff
changeset
|
119 self.assertEqual(first.args, second.args) |
|
61d22701a5e5
Patch #2232: os.tmpfile might fail on Windows if the user has no
Martin v. Löwis <martin@v.loewis.de>
parents:
44785
diff
changeset
|
120 else: |
|
61d22701a5e5
Patch #2232: os.tmpfile might fail on Windows if the user has no
Martin v. Löwis <martin@v.loewis.de>
parents:
44785
diff
changeset
|
121 self.fail("expected os.tmpfile() to raise OSError") |
|
61d22701a5e5
Patch #2232: os.tmpfile might fail on Windows if the user has no
Martin v. Löwis <martin@v.loewis.de>
parents:
44785
diff
changeset
|
122 return |
|
61d22701a5e5
Patch #2232: os.tmpfile might fail on Windows if the user has no
Martin v. Löwis <martin@v.loewis.de>
parents:
44785
diff
changeset
|
123 else: |
|
61d22701a5e5
Patch #2232: os.tmpfile might fail on Windows if the user has no
Martin v. Löwis <martin@v.loewis.de>
parents:
44785
diff
changeset
|
124 # open() worked, therefore, tmpfile() should work. Close our |
|
61d22701a5e5
Patch #2232: os.tmpfile might fail on Windows if the user has no
Martin v. Löwis <martin@v.loewis.de>
parents:
44785
diff
changeset
|
125 # dummy file and proceed with the test as normal. |
|
61d22701a5e5
Patch #2232: os.tmpfile might fail on Windows if the user has no
Martin v. Löwis <martin@v.loewis.de>
parents:
44785
diff
changeset
|
126 fp.close() |
|
61d22701a5e5
Patch #2232: os.tmpfile might fail on Windows if the user has no
Martin v. Löwis <martin@v.loewis.de>
parents:
44785
diff
changeset
|
127 os.remove(name) |
|
61d22701a5e5
Patch #2232: os.tmpfile might fail on Windows if the user has no
Martin v. Löwis <martin@v.loewis.de>
parents:
44785
diff
changeset
|
128 |
|
18678
a7bc339ee2f3
Start of a test suite for the "os" module -- this only tests the temporary
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
129 fp = os.tmpfile() |
|
a7bc339ee2f3
Start of a test suite for the "os" module -- this only tests the temporary
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
130 fp.write("foobar") |
|
a7bc339ee2f3
Start of a test suite for the "os" module -- this only tests the temporary
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
131 fp.seek(0,0) |
|
a7bc339ee2f3
Start of a test suite for the "os" module -- this only tests the temporary
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
132 s = fp.read() |
|
a7bc339ee2f3
Start of a test suite for the "os" module -- this only tests the temporary
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
133 fp.close() |
|
a7bc339ee2f3
Start of a test suite for the "os" module -- this only tests the temporary
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
134 self.assert_(s == "foobar") |
|
a7bc339ee2f3
Start of a test suite for the "os" module -- this only tests the temporary
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
135 |
|
a7bc339ee2f3
Start of a test suite for the "os" module -- this only tests the temporary
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
136 def test_tmpnam(self): |
|
28869
a53529095e95
SF bug 728097: tmpnam problems on windows 2.3b, breaks test.test_os.
Tim Peters <tim.peters@gmail.com>
parents:
28863
diff
changeset
|
137 import sys |
|
18678
a7bc339ee2f3
Start of a test suite for the "os" module -- this only tests the temporary
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
138 if not hasattr(os, "tmpnam"): |
|
a7bc339ee2f3
Start of a test suite for the "os" module -- this only tests the temporary
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
139 return |
|
19403
ac6bfa55fceb
Silence warnings during test_os
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
18787
diff
changeset
|
140 warnings.filterwarnings("ignore", "tmpnam", RuntimeWarning, |
|
23073
21d252028404
Tighten up some warning filters, and break some dependencies on the
Tim Peters <tim.peters@gmail.com>
parents:
20547
diff
changeset
|
141 r"test_os$") |
|
28869
a53529095e95
SF bug 728097: tmpnam problems on windows 2.3b, breaks test.test_os.
Tim Peters <tim.peters@gmail.com>
parents:
28863
diff
changeset
|
142 name = os.tmpnam() |
|
a53529095e95
SF bug 728097: tmpnam problems on windows 2.3b, breaks test.test_os.
Tim Peters <tim.peters@gmail.com>
parents:
28863
diff
changeset
|
143 if sys.platform in ("win32",): |
|
a53529095e95
SF bug 728097: tmpnam problems on windows 2.3b, breaks test.test_os.
Tim Peters <tim.peters@gmail.com>
parents:
28863
diff
changeset
|
144 # The Windows tmpnam() seems useless. From the MS docs: |
|
a53529095e95
SF bug 728097: tmpnam problems on windows 2.3b, breaks test.test_os.
Tim Peters <tim.peters@gmail.com>
parents:
28863
diff
changeset
|
145 # |
|
a53529095e95
SF bug 728097: tmpnam problems on windows 2.3b, breaks test.test_os.
Tim Peters <tim.peters@gmail.com>
parents:
28863
diff
changeset
|
146 # The character string that tmpnam creates consists of |
|
a53529095e95
SF bug 728097: tmpnam problems on windows 2.3b, breaks test.test_os.
Tim Peters <tim.peters@gmail.com>
parents:
28863
diff
changeset
|
147 # the path prefix, defined by the entry P_tmpdir in the |
|
a53529095e95
SF bug 728097: tmpnam problems on windows 2.3b, breaks test.test_os.
Tim Peters <tim.peters@gmail.com>
parents:
28863
diff
changeset
|
148 # file STDIO.H, followed by a sequence consisting of the |
|
a53529095e95
SF bug 728097: tmpnam problems on windows 2.3b, breaks test.test_os.
Tim Peters <tim.peters@gmail.com>
parents:
28863
diff
changeset
|
149 # digit characters '0' through '9'; the numerical value |
|
a53529095e95
SF bug 728097: tmpnam problems on windows 2.3b, breaks test.test_os.
Tim Peters <tim.peters@gmail.com>
parents:
28863
diff
changeset
|
150 # of this string is in the range 1 - 65,535. Changing the |
|
a53529095e95
SF bug 728097: tmpnam problems on windows 2.3b, breaks test.test_os.
Tim Peters <tim.peters@gmail.com>
parents:
28863
diff
changeset
|
151 # definitions of L_tmpnam or P_tmpdir in STDIO.H does not |
|
a53529095e95
SF bug 728097: tmpnam problems on windows 2.3b, breaks test.test_os.
Tim Peters <tim.peters@gmail.com>
parents:
28863
diff
changeset
|
152 # change the operation of tmpnam. |
|
a53529095e95
SF bug 728097: tmpnam problems on windows 2.3b, breaks test.test_os.
Tim Peters <tim.peters@gmail.com>
parents:
28863
diff
changeset
|
153 # |
|
a53529095e95
SF bug 728097: tmpnam problems on windows 2.3b, breaks test.test_os.
Tim Peters <tim.peters@gmail.com>
parents:
28863
diff
changeset
|
154 # The really bizarre part is that, at least under MSVC6, |
|
a53529095e95
SF bug 728097: tmpnam problems on windows 2.3b, breaks test.test_os.
Tim Peters <tim.peters@gmail.com>
parents:
28863
diff
changeset
|
155 # P_tmpdir is "\\". That is, the path returned refers to |
|
a53529095e95
SF bug 728097: tmpnam problems on windows 2.3b, breaks test.test_os.
Tim Peters <tim.peters@gmail.com>
parents:
28863
diff
changeset
|
156 # the root of the current drive. That's a terrible place to |
|
a53529095e95
SF bug 728097: tmpnam problems on windows 2.3b, breaks test.test_os.
Tim Peters <tim.peters@gmail.com>
parents:
28863
diff
changeset
|
157 # put temp files, and, depending on privileges, the user |
|
a53529095e95
SF bug 728097: tmpnam problems on windows 2.3b, breaks test.test_os.
Tim Peters <tim.peters@gmail.com>
parents:
28863
diff
changeset
|
158 # may not even be able to open a file in the root directory. |
|
a53529095e95
SF bug 728097: tmpnam problems on windows 2.3b, breaks test.test_os.
Tim Peters <tim.peters@gmail.com>
parents:
28863
diff
changeset
|
159 self.failIf(os.path.exists(name), |
|
a53529095e95
SF bug 728097: tmpnam problems on windows 2.3b, breaks test.test_os.
Tim Peters <tim.peters@gmail.com>
parents:
28863
diff
changeset
|
160 "file already exists for temporary file") |
|
a53529095e95
SF bug 728097: tmpnam problems on windows 2.3b, breaks test.test_os.
Tim Peters <tim.peters@gmail.com>
parents:
28863
diff
changeset
|
161 else: |
|
a53529095e95
SF bug 728097: tmpnam problems on windows 2.3b, breaks test.test_os.
Tim Peters <tim.peters@gmail.com>
parents:
28863
diff
changeset
|
162 self.check_tempfile(name) |
|
18787
f999d3609f74
Whitespace normalization, plus:
Tim Peters <tim.peters@gmail.com>
parents:
18682
diff
changeset
|
163 |
|
20539
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
164 # Test attributes on return values from os.*stat* family. |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
165 class StatAttributeTests(unittest.TestCase): |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
166 def setUp(self): |
|
28917
1741af48fd9f
Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents:
28869
diff
changeset
|
167 os.mkdir(test_support.TESTFN) |
|
1741af48fd9f
Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents:
28869
diff
changeset
|
168 self.fname = os.path.join(test_support.TESTFN, "f1") |
|
20539
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
169 f = open(self.fname, 'wb') |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
170 f.write("ABC") |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
171 f.close() |
|
20547
a12ccdbce8da
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
20545
diff
changeset
|
172 |
|
20539
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
173 def tearDown(self): |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
174 os.unlink(self.fname) |
|
28917
1741af48fd9f
Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents:
28869
diff
changeset
|
175 os.rmdir(test_support.TESTFN) |
|
20539
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
176 |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
177 def test_stat_attributes(self): |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
178 if not hasattr(os, "stat"): |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
179 return |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
180 |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
181 import stat |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
182 result = os.stat(self.fname) |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
183 |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
184 # Make sure direct access works |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
185 self.assertEquals(result[stat.ST_SIZE], 3) |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
186 self.assertEquals(result.st_size, 3) |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
187 |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
188 import sys |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
189 |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
190 # Make sure all the attributes are there |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
191 members = dir(result) |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
192 for name in dir(stat): |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
193 if name[:3] == 'ST_': |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
194 attr = name.lower() |
|
34480
36abdd478e07
Truncate st_?time before comparing it with ST_?TIME in the tests.
Martin v. Löwis <martin@v.loewis.de>
parents:
33323
diff
changeset
|
195 if name.endswith("TIME"): |
|
36abdd478e07
Truncate st_?time before comparing it with ST_?TIME in the tests.
Martin v. Löwis <martin@v.loewis.de>
parents:
33323
diff
changeset
|
196 def trunc(x): return int(x) |
|
36abdd478e07
Truncate st_?time before comparing it with ST_?TIME in the tests.
Martin v. Löwis <martin@v.loewis.de>
parents:
33323
diff
changeset
|
197 else: |
|
36abdd478e07
Truncate st_?time before comparing it with ST_?TIME in the tests.
Martin v. Löwis <martin@v.loewis.de>
parents:
33323
diff
changeset
|
198 def trunc(x): return x |
|
36abdd478e07
Truncate st_?time before comparing it with ST_?TIME in the tests.
Martin v. Löwis <martin@v.loewis.de>
parents:
33323
diff
changeset
|
199 self.assertEquals(trunc(getattr(result, attr)), |
|
20539
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
200 result[getattr(stat, name)]) |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
201 self.assert_(attr in members) |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
202 |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
203 try: |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
204 result[200] |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
205 self.fail("No exception thrown") |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
206 except IndexError: |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
207 pass |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
208 |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
209 # Make sure that assignment fails |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
210 try: |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
211 result.st_mode = 1 |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
212 self.fail("No exception thrown") |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
213 except TypeError: |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
214 pass |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
215 |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
216 try: |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
217 result.st_rdev = 1 |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
218 self.fail("No exception thrown") |
|
20545
36714b98fe51
The assignment to result.st_rdev can raise AttributeError as well as
Guido van Rossum <guido@python.org>
parents:
20539
diff
changeset
|
219 except (AttributeError, TypeError): |
|
20539
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
220 pass |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
221 |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
222 try: |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
223 result.parrot = 1 |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
224 self.fail("No exception thrown") |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
225 except AttributeError: |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
226 pass |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
227 |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
228 # Use the stat_result constructor with a too-short tuple. |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
229 try: |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
230 result2 = os.stat_result((10,)) |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
231 self.fail("No exception thrown") |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
232 except TypeError: |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
233 pass |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
234 |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
235 # Use the constructr with a too-long tuple. |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
236 try: |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
237 result2 = os.stat_result((0,1,2,3,4,5,6,7,8,9,10,11,12,13,14)) |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
238 except TypeError: |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
239 pass |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
240 |
|
20547
a12ccdbce8da
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
20545
diff
changeset
|
241 |
|
20539
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
242 def test_statvfs_attributes(self): |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
243 if not hasattr(os, "statvfs"): |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
244 return |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
245 |
|
23777
bec1b942e0bc
Patch #488073: AtheOS port.
Martin v. Löwis <martin@v.loewis.de>
parents:
23073
diff
changeset
|
246 try: |
|
bec1b942e0bc
Patch #488073: AtheOS port.
Martin v. Löwis <martin@v.loewis.de>
parents:
23073
diff
changeset
|
247 result = os.statvfs(self.fname) |
|
bec1b942e0bc
Patch #488073: AtheOS port.
Martin v. Löwis <martin@v.loewis.de>
parents:
23073
diff
changeset
|
248 except OSError, e: |
|
bec1b942e0bc
Patch #488073: AtheOS port.
Martin v. Löwis <martin@v.loewis.de>
parents:
23073
diff
changeset
|
249 # On AtheOS, glibc always returns ENOSYS |
|
bec1b942e0bc
Patch #488073: AtheOS port.
Martin v. Löwis <martin@v.loewis.de>
parents:
23073
diff
changeset
|
250 if e.errno == errno.ENOSYS: |
|
bec1b942e0bc
Patch #488073: AtheOS port.
Martin v. Löwis <martin@v.loewis.de>
parents:
23073
diff
changeset
|
251 return |
|
20539
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
252 |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
253 # Make sure direct access works |
|
47071
8d8e06f4be1f
Remove the last usage of statvfs in the stdlib.
Brett Cannon <bcannon@gmail.com>
parents:
45681
diff
changeset
|
254 self.assertEquals(result.f_bfree, result[3]) |
|
20539
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
255 |
|
47071
8d8e06f4be1f
Remove the last usage of statvfs in the stdlib.
Brett Cannon <bcannon@gmail.com>
parents:
45681
diff
changeset
|
256 # Make sure all the attributes are there. |
|
8d8e06f4be1f
Remove the last usage of statvfs in the stdlib.
Brett Cannon <bcannon@gmail.com>
parents:
45681
diff
changeset
|
257 members = ('bsize', 'frsize', 'blocks', 'bfree', 'bavail', 'files', |
|
8d8e06f4be1f
Remove the last usage of statvfs in the stdlib.
Brett Cannon <bcannon@gmail.com>
parents:
45681
diff
changeset
|
258 'ffree', 'favail', 'flag', 'namemax') |
|
8d8e06f4be1f
Remove the last usage of statvfs in the stdlib.
Brett Cannon <bcannon@gmail.com>
parents:
45681
diff
changeset
|
259 for value, member in enumerate(members): |
|
8d8e06f4be1f
Remove the last usage of statvfs in the stdlib.
Brett Cannon <bcannon@gmail.com>
parents:
45681
diff
changeset
|
260 self.assertEquals(getattr(result, 'f_' + member), result[value]) |
|
20539
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
261 |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
262 # Make sure that assignment really fails |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
263 try: |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
264 result.f_bfree = 1 |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
265 self.fail("No exception thrown") |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
266 except TypeError: |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
267 pass |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
268 |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
269 try: |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
270 result.parrot = 1 |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
271 self.fail("No exception thrown") |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
272 except AttributeError: |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
273 pass |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
274 |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
275 # Use the constructor with a too-short tuple. |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
276 try: |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
277 result2 = os.statvfs_result((10,)) |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
278 self.fail("No exception thrown") |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
279 except TypeError: |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
280 pass |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
281 |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
282 # Use the constructr with a too-long tuple. |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
283 try: |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
284 result2 = os.statvfs_result((0,1,2,3,4,5,6,7,8,9,10,11,12,13,14)) |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
285 except TypeError: |
|
2d599a36b67a
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents:
20013
diff
changeset
|
286 pass |
|
18678
a7bc339ee2f3
Start of a test suite for the "os" module -- this only tests the temporary
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
287 |
|
40119
b94bcb7aed3d
Patch #1576166: Support os.utime for directories on Windows NT+.
Martin v. Löwis <martin@v.loewis.de>
parents:
40076
diff
changeset
|
288 def test_utime_dir(self): |
|
b94bcb7aed3d
Patch #1576166: Support os.utime for directories on Windows NT+.
Martin v. Löwis <martin@v.loewis.de>
parents:
40076
diff
changeset
|
289 delta = 1000000 |
|
b94bcb7aed3d
Patch #1576166: Support os.utime for directories on Windows NT+.
Martin v. Löwis <martin@v.loewis.de>
parents:
40076
diff
changeset
|
290 st = os.stat(test_support.TESTFN) |
|
40125
c006d227948d
Round to int, because some systems support sub-second time stamps in stat, but not in utime.
Martin v. Löwis <martin@v.loewis.de>
parents:
40122
diff
changeset
|
291 # round to int, because some systems may support sub-second |
|
c006d227948d
Round to int, because some systems support sub-second time stamps in stat, but not in utime.
Martin v. Löwis <martin@v.loewis.de>
parents:
40122
diff
changeset
|
292 # time stamps in stat, but not in utime. |
|
c006d227948d
Round to int, because some systems support sub-second time stamps in stat, but not in utime.
Martin v. Löwis <martin@v.loewis.de>
parents:
40122
diff
changeset
|
293 os.utime(test_support.TESTFN, (st.st_atime, int(st.st_mtime-delta))) |
|
40119
b94bcb7aed3d
Patch #1576166: Support os.utime for directories on Windows NT+.
Martin v. Löwis <martin@v.loewis.de>
parents:
40076
diff
changeset
|
294 st2 = os.stat(test_support.TESTFN) |
|
40125
c006d227948d
Round to int, because some systems support sub-second time stamps in stat, but not in utime.
Martin v. Löwis <martin@v.loewis.de>
parents:
40122
diff
changeset
|
295 self.assertEquals(st2.st_mtime, int(st.st_mtime-delta)) |
|
40119
b94bcb7aed3d
Patch #1576166: Support os.utime for directories on Windows NT+.
Martin v. Löwis <martin@v.loewis.de>
parents:
40076
diff
changeset
|
296 |
|
40076
28db61101463
Bug #1565150: Fix subsecond processing for os.utime on Windows.
Martin v. Löwis <martin@v.loewis.de>
parents:
38984
diff
changeset
|
297 # Restrict test to Win32, since there is no guarantee other |
|
28db61101463
Bug #1565150: Fix subsecond processing for os.utime on Windows.
Martin v. Löwis <martin@v.loewis.de>
parents:
38984
diff
changeset
|
298 # systems support centiseconds |
|
28db61101463
Bug #1565150: Fix subsecond processing for os.utime on Windows.
Martin v. Löwis <martin@v.loewis.de>
parents:
38984
diff
changeset
|
299 if sys.platform == 'win32': |
|
43082
68f8b2393243
Bug #1709599: Run test_1565150 only if the file system is NTFS.
Martin v. Löwis <martin@v.loewis.de>
parents:
41532
diff
changeset
|
300 def get_file_system(path): |
|
48318
d42629185382
fixed get_file_system in test_os.py ('path' is unicode on py3k and ansi on trunk)
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents:
48270
diff
changeset
|
301 root = os.path.splitdrive(os.path.abspath(path))[0] + '\\' |
|
43082
68f8b2393243
Bug #1709599: Run test_1565150 only if the file system is NTFS.
Martin v. Löwis <martin@v.loewis.de>
parents:
41532
diff
changeset
|
302 import ctypes |
|
48803
b199f3419e65
Reverted r65900. See http://mail.python.org/pipermail/python-checkins/2008-August/073116.html
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents:
48318
diff
changeset
|
303 kernel32 = ctypes.windll.kernel32 |
|
b199f3419e65
Reverted r65900. See http://mail.python.org/pipermail/python-checkins/2008-August/073116.html
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents:
48318
diff
changeset
|
304 buf = ctypes.create_string_buffer("", 100) |
|
b199f3419e65
Reverted r65900. See http://mail.python.org/pipermail/python-checkins/2008-August/073116.html
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents:
48318
diff
changeset
|
305 if kernel32.GetVolumeInformationA(root, None, 0, None, None, None, buf, len(buf)): |
|
43082
68f8b2393243
Bug #1709599: Run test_1565150 only if the file system is NTFS.
Martin v. Löwis <martin@v.loewis.de>
parents:
41532
diff
changeset
|
306 return buf.value |
|
68f8b2393243
Bug #1709599: Run test_1565150 only if the file system is NTFS.
Martin v. Löwis <martin@v.loewis.de>
parents:
41532
diff
changeset
|
307 |
|
68f8b2393243
Bug #1709599: Run test_1565150 only if the file system is NTFS.
Martin v. Löwis <martin@v.loewis.de>
parents:
41532
diff
changeset
|
308 if get_file_system(test_support.TESTFN) == "NTFS": |
|
68f8b2393243
Bug #1709599: Run test_1565150 only if the file system is NTFS.
Martin v. Löwis <martin@v.loewis.de>
parents:
41532
diff
changeset
|
309 def test_1565150(self): |
|
68f8b2393243
Bug #1709599: Run test_1565150 only if the file system is NTFS.
Martin v. Löwis <martin@v.loewis.de>
parents:
41532
diff
changeset
|
310 t1 = 1159195039.25 |
|
68f8b2393243
Bug #1709599: Run test_1565150 only if the file system is NTFS.
Martin v. Löwis <martin@v.loewis.de>
parents:
41532
diff
changeset
|
311 os.utime(self.fname, (t1, t1)) |
|
68f8b2393243
Bug #1709599: Run test_1565150 only if the file system is NTFS.
Martin v. Löwis <martin@v.loewis.de>
parents:
41532
diff
changeset
|
312 self.assertEquals(os.stat(self.fname).st_mtime, t1) |
|
40076
28db61101463
Bug #1565150: Fix subsecond processing for os.utime on Windows.
Martin v. Löwis <martin@v.loewis.de>
parents:
38984
diff
changeset
|
313 |
|
41381
d8cd88f93098
Bug #1686475: Support stat'ing open files on Windows again.
Martin v. Löwis <martin@v.loewis.de>
parents:
41345
diff
changeset
|
314 def test_1686475(self): |
|
d8cd88f93098
Bug #1686475: Support stat'ing open files on Windows again.
Martin v. Löwis <martin@v.loewis.de>
parents:
41345
diff
changeset
|
315 # Verify that an open file can be stat'ed |
|
d8cd88f93098
Bug #1686475: Support stat'ing open files on Windows again.
Martin v. Löwis <martin@v.loewis.de>
parents:
41345
diff
changeset
|
316 try: |
|
d8cd88f93098
Bug #1686475: Support stat'ing open files on Windows again.
Martin v. Löwis <martin@v.loewis.de>
parents:
41345
diff
changeset
|
317 os.stat(r"c:\pagefile.sys") |
|
d8cd88f93098
Bug #1686475: Support stat'ing open files on Windows again.
Martin v. Löwis <martin@v.loewis.de>
parents:
41345
diff
changeset
|
318 except WindowsError, e: |
|
48270
2bebc0a6d042
#3580: fix a failure in test_os
Antoine Pitrou <solipsis@pitrou.net>
parents:
48267
diff
changeset
|
319 if e.errno == 2: # file does not exist; cannot run test |
|
41381
d8cd88f93098
Bug #1686475: Support stat'ing open files on Windows again.
Martin v. Löwis <martin@v.loewis.de>
parents:
41345
diff
changeset
|
320 return |
|
d8cd88f93098
Bug #1686475: Support stat'ing open files on Windows again.
Martin v. Löwis <martin@v.loewis.de>
parents:
41345
diff
changeset
|
321 self.fail("Could not stat pagefile.sys") |
|
d8cd88f93098
Bug #1686475: Support stat'ing open files on Windows again.
Martin v. Löwis <martin@v.loewis.de>
parents:
41345
diff
changeset
|
322 |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
31389
diff
changeset
|
323 from test import mapping_tests |
|
28240
ea70032a24b1
SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents:
24956
diff
changeset
|
324 |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
31389
diff
changeset
|
325 class EnvironTests(mapping_tests.BasicTestMappingProtocol): |
|
28240
ea70032a24b1
SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents:
24956
diff
changeset
|
326 """check that os.environ object conform to mapping protocol""" |
|
32204
9a3901bf6222
Rename class attribute containing the class to be tested, so the name is the
Walter Dörwald <walter@livinglogic.de>
parents:
32163
diff
changeset
|
327 type2test = None |
|
28240
ea70032a24b1
SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents:
24956
diff
changeset
|
328 def _reference(self): |
|
ea70032a24b1
SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents:
24956
diff
changeset
|
329 return {"KEY1":"VALUE1", "KEY2":"VALUE2", "KEY3":"VALUE3"} |
|
ea70032a24b1
SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents:
24956
diff
changeset
|
330 def _empty_mapping(self): |
|
ea70032a24b1
SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents:
24956
diff
changeset
|
331 os.environ.clear() |
|
ea70032a24b1
SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents:
24956
diff
changeset
|
332 return os.environ |
|
ea70032a24b1
SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents:
24956
diff
changeset
|
333 def setUp(self): |
|
ea70032a24b1
SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents:
24956
diff
changeset
|
334 self.__save = dict(os.environ) |
|
ea70032a24b1
SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents:
24956
diff
changeset
|
335 os.environ.clear() |
|
ea70032a24b1
SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents:
24956
diff
changeset
|
336 def tearDown(self): |
|
ea70032a24b1
SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents:
24956
diff
changeset
|
337 os.environ.clear() |
|
ea70032a24b1
SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents:
24956
diff
changeset
|
338 os.environ.update(self.__save) |
|
ea70032a24b1
SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents:
24956
diff
changeset
|
339 |
|
34516
658512774468
Revert os.py 1.75, and directly implement update.
Martin v. Löwis <martin@v.loewis.de>
parents:
34480
diff
changeset
|
340 # Bug 1110478 |
|
34607
4b1207cb01ea
Avoid using items() in environ.update(). Fixes #1124513.
Martin v. Löwis <martin@v.loewis.de>
parents:
34516
diff
changeset
|
341 def test_update2(self): |
|
34516
658512774468
Revert os.py 1.75, and directly implement update.
Martin v. Löwis <martin@v.loewis.de>
parents:
34480
diff
changeset
|
342 if os.path.exists("/bin/sh"): |
|
658512774468
Revert os.py 1.75, and directly implement update.
Martin v. Löwis <martin@v.loewis.de>
parents:
34480
diff
changeset
|
343 os.environ.update(HELLO="World") |
|
658512774468
Revert os.py 1.75, and directly implement update.
Martin v. Löwis <martin@v.loewis.de>
parents:
34480
diff
changeset
|
344 value = os.popen("/bin/sh -c 'echo $HELLO'").read().strip() |
|
658512774468
Revert os.py 1.75, and directly implement update.
Martin v. Löwis <martin@v.loewis.de>
parents:
34480
diff
changeset
|
345 self.assertEquals(value, "World") |
|
658512774468
Revert os.py 1.75, and directly implement update.
Martin v. Löwis <martin@v.loewis.de>
parents:
34480
diff
changeset
|
346 |
|
28820
60e722384d62
New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents:
28240
diff
changeset
|
347 class WalkTests(unittest.TestCase): |
|
60e722384d62
New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents:
28240
diff
changeset
|
348 """Tests for os.walk().""" |
|
60e722384d62
New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents:
28240
diff
changeset
|
349 |
|
60e722384d62
New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents:
28240
diff
changeset
|
350 def test_traversal(self): |
|
60e722384d62
New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents:
28240
diff
changeset
|
351 import os |
|
60e722384d62
New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents:
28240
diff
changeset
|
352 from os.path import join |
|
60e722384d62
New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents:
28240
diff
changeset
|
353 |
|
60e722384d62
New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents:
28240
diff
changeset
|
354 # Build: |
|
41532
4b9fa8092e82
Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents:
41385
diff
changeset
|
355 # TESTFN/ |
|
41258
1dc4c4ce2db9
New test for rev. 54407 which only uses directories under TESTFN.
Georg Brandl <georg@python.org>
parents:
41242
diff
changeset
|
356 # TEST1/ a file kid and two directory kids |
|
28820
60e722384d62
New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents:
28240
diff
changeset
|
357 # tmp1 |
|
60e722384d62
New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents:
28240
diff
changeset
|
358 # SUB1/ a file kid and a directory kid |
|
41258
1dc4c4ce2db9
New test for rev. 54407 which only uses directories under TESTFN.
Georg Brandl <georg@python.org>
parents:
41242
diff
changeset
|
359 # tmp2 |
|
1dc4c4ce2db9
New test for rev. 54407 which only uses directories under TESTFN.
Georg Brandl <georg@python.org>
parents:
41242
diff
changeset
|
360 # SUB11/ no kids |
|
1dc4c4ce2db9
New test for rev. 54407 which only uses directories under TESTFN.
Georg Brandl <georg@python.org>
parents:
41242
diff
changeset
|
361 # SUB2/ a file kid and a dirsymlink kid |
|
1dc4c4ce2db9
New test for rev. 54407 which only uses directories under TESTFN.
Georg Brandl <georg@python.org>
parents:
41242
diff
changeset
|
362 # tmp3 |
|
1dc4c4ce2db9
New test for rev. 54407 which only uses directories under TESTFN.
Georg Brandl <georg@python.org>
parents:
41242
diff
changeset
|
363 # link/ a symlink to TESTFN.2 |
|
1dc4c4ce2db9
New test for rev. 54407 which only uses directories under TESTFN.
Georg Brandl <georg@python.org>
parents:
41242
diff
changeset
|
364 # TEST2/ |
|
1dc4c4ce2db9
New test for rev. 54407 which only uses directories under TESTFN.
Georg Brandl <georg@python.org>
parents:
41242
diff
changeset
|
365 # tmp4 a lone file |
|
1dc4c4ce2db9
New test for rev. 54407 which only uses directories under TESTFN.
Georg Brandl <georg@python.org>
parents:
41242
diff
changeset
|
366 walk_path = join(test_support.TESTFN, "TEST1") |
|
1dc4c4ce2db9
New test for rev. 54407 which only uses directories under TESTFN.
Georg Brandl <georg@python.org>
parents:
41242
diff
changeset
|
367 sub1_path = join(walk_path, "SUB1") |
|
28820
60e722384d62
New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents:
28240
diff
changeset
|
368 sub11_path = join(sub1_path, "SUB11") |
|
41258
1dc4c4ce2db9
New test for rev. 54407 which only uses directories under TESTFN.
Georg Brandl <georg@python.org>
parents:
41242
diff
changeset
|
369 sub2_path = join(walk_path, "SUB2") |
|
1dc4c4ce2db9
New test for rev. 54407 which only uses directories under TESTFN.
Georg Brandl <georg@python.org>
parents:
41242
diff
changeset
|
370 tmp1_path = join(walk_path, "tmp1") |
|
28820
60e722384d62
New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents:
28240
diff
changeset
|
371 tmp2_path = join(sub1_path, "tmp2") |
|
60e722384d62
New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents:
28240
diff
changeset
|
372 tmp3_path = join(sub2_path, "tmp3") |
|
41258
1dc4c4ce2db9
New test for rev. 54407 which only uses directories under TESTFN.
Georg Brandl <georg@python.org>
parents:
41242
diff
changeset
|
373 link_path = join(sub2_path, "link") |
|
1dc4c4ce2db9
New test for rev. 54407 which only uses directories under TESTFN.
Georg Brandl <georg@python.org>
parents:
41242
diff
changeset
|
374 t2_path = join(test_support.TESTFN, "TEST2") |
|
1dc4c4ce2db9
New test for rev. 54407 which only uses directories under TESTFN.
Georg Brandl <georg@python.org>
parents:
41242
diff
changeset
|
375 tmp4_path = join(test_support.TESTFN, "TEST2", "tmp4") |
|
28820
60e722384d62
New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents:
28240
diff
changeset
|
376 |
|
60e722384d62
New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents:
28240
diff
changeset
|
377 # Create stuff. |
|
60e722384d62
New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents:
28240
diff
changeset
|
378 os.makedirs(sub11_path) |
|
60e722384d62
New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents:
28240
diff
changeset
|
379 os.makedirs(sub2_path) |
|
41258
1dc4c4ce2db9
New test for rev. 54407 which only uses directories under TESTFN.
Georg Brandl <georg@python.org>
parents:
41242
diff
changeset
|
380 os.makedirs(t2_path) |
|
1dc4c4ce2db9
New test for rev. 54407 which only uses directories under TESTFN.
Georg Brandl <georg@python.org>
parents:
41242
diff
changeset
|
381 for path in tmp1_path, tmp2_path, tmp3_path, tmp4_path: |
|
28820
60e722384d62
New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents:
28240
diff
changeset
|
382 f = file(path, "w") |
|
60e722384d62
New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents:
28240
diff
changeset
|
383 f.write("I'm " + path + " and proud of it. Blame test_os.\n") |
|
60e722384d62
New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents:
28240
diff
changeset
|
384 f.close() |
|
41258
1dc4c4ce2db9
New test for rev. 54407 which only uses directories under TESTFN.
Georg Brandl <georg@python.org>
parents:
41242
diff
changeset
|
385 if hasattr(os, "symlink"): |
|
1dc4c4ce2db9
New test for rev. 54407 which only uses directories under TESTFN.
Georg Brandl <georg@python.org>
parents:
41242
diff
changeset
|
386 os.symlink(os.path.abspath(t2_path), link_path) |
|
41385
512f56afa4a0
Fix WalkTests.test_traversal() on Windows. The cleanup in
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents:
41381
diff
changeset
|
387 sub2_tree = (sub2_path, ["link"], ["tmp3"]) |
|
41258
1dc4c4ce2db9
New test for rev. 54407 which only uses directories under TESTFN.
Georg Brandl <georg@python.org>
parents:
41242
diff
changeset
|
388 else: |
|
41385
512f56afa4a0
Fix WalkTests.test_traversal() on Windows. The cleanup in
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents:
41381
diff
changeset
|
389 sub2_tree = (sub2_path, [], ["tmp3"]) |
|
28820
60e722384d62
New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents:
28240
diff
changeset
|
390 |
|
60e722384d62
New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents:
28240
diff
changeset
|
391 # Walk top-down. |
|
41258
1dc4c4ce2db9
New test for rev. 54407 which only uses directories under TESTFN.
Georg Brandl <georg@python.org>
parents:
41242
diff
changeset
|
392 all = list(os.walk(walk_path)) |
|
28820
60e722384d62
New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents:
28240
diff
changeset
|
393 self.assertEqual(len(all), 4) |
|
60e722384d62
New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents:
28240
diff
changeset
|
394 # We can't know which order SUB1 and SUB2 will appear in. |
|
60e722384d62
New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents:
28240
diff
changeset
|
395 # Not flipped: TESTFN, SUB1, SUB11, SUB2 |
|
60e722384d62
New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents:
28240
diff
changeset
|
396 # flipped: TESTFN, SUB2, SUB1, SUB11 |
|
60e722384d62
New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents:
28240
diff
changeset
|
397 flipped = all[0][1][0] != "SUB1" |
|
60e722384d62
New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents:
28240
diff
changeset
|
398 all[0][1].sort() |
|
41258
1dc4c4ce2db9
New test for rev. 54407 which only uses directories under TESTFN.
Georg Brandl <georg@python.org>
parents:
41242
diff
changeset
|
399 self.assertEqual(all[0], (walk_path, ["SUB1", "SUB2"], ["tmp1"])) |
|
28820
60e722384d62
New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents:
28240
diff
changeset
|
400 self.assertEqual(all[1 + flipped], (sub1_path, ["SUB11"], ["tmp2"])) |
|
60e722384d62
New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents:
28240
diff
changeset
|
401 self.assertEqual(all[2 + flipped], (sub11_path, [], [])) |
|
41385
512f56afa4a0
Fix WalkTests.test_traversal() on Windows. The cleanup in
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents:
41381
diff
changeset
|
402 self.assertEqual(all[3 - 2 * flipped], sub2_tree) |
|
28820
60e722384d62
New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents:
28240
diff
changeset
|
403 |
|
60e722384d62
New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents:
28240
diff
changeset
|
404 # Prune the search. |
|
60e722384d62
New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents:
28240
diff
changeset
|
405 all = [] |
|
41258
1dc4c4ce2db9
New test for rev. 54407 which only uses directories under TESTFN.
Georg Brandl <georg@python.org>
parents:
41242
diff
changeset
|
406 for root, dirs, files in os.walk(walk_path): |
|
28820
60e722384d62
New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents:
28240
diff
changeset
|
407 all.append((root, dirs, files)) |
|
60e722384d62
New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents:
28240
diff
changeset
|
408 # Don't descend into SUB1. |
|
60e722384d62
New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents:
28240
diff
changeset
|
409 if 'SUB1' in dirs: |
|
60e722384d62
New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents:
28240
diff
changeset
|
410 # Note that this also mutates the dirs we appended to all! |
|
60e722384d62
New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents:
28240
diff
changeset
|
411 dirs.remove('SUB1') |
|
60e722384d62
New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents:
28240
diff
changeset
|
412 self.assertEqual(len(all), 2) |
|
41258
1dc4c4ce2db9
New test for rev. 54407 which only uses directories under TESTFN.
Georg Brandl <georg@python.org>
parents:
41242
diff
changeset
|
413 self.assertEqual(all[0], (walk_path, ["SUB2"], ["tmp1"])) |
|
41385
512f56afa4a0
Fix WalkTests.test_traversal() on Windows. The cleanup in
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents:
41381
diff
changeset
|
414 self.assertEqual(all[1], sub2_tree) |
|
28820
60e722384d62
New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents:
28240
diff
changeset
|
415 |
|
60e722384d62
New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents:
28240
diff
changeset
|
416 # Walk bottom-up. |
|
41258
1dc4c4ce2db9
New test for rev. 54407 which only uses directories under TESTFN.
Georg Brandl <georg@python.org>
parents:
41242
diff
changeset
|
417 all = list(os.walk(walk_path, topdown=False)) |
|
28820
60e722384d62
New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents:
28240
diff
changeset
|
418 self.assertEqual(len(all), 4) |
|
60e722384d62
New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents:
28240
diff
changeset
|
419 # We can't know which order SUB1 and SUB2 will appear in. |
|
60e722384d62
New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents:
28240
diff
changeset
|
420 # Not flipped: SUB11, SUB1, SUB2, TESTFN |
|
60e722384d62
New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents:
28240
diff
changeset
|
421 # flipped: SUB2, SUB11, SUB1, TESTFN |
|
60e722384d62
New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents:
28240
diff
changeset
|
422 flipped = all[3][1][0] != "SUB1" |
|
60e722384d62
New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents:
28240
diff
changeset
|
423 all[3][1].sort() |
|
41258
1dc4c4ce2db9
New test for rev. 54407 which only uses directories under TESTFN.
Georg Brandl <georg@python.org>
parents:
41242
diff
changeset
|
424 self.assertEqual(all[3], (walk_path, ["SUB1", "SUB2"], ["tmp1"])) |
|
28820
60e722384d62
New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents:
28240
diff
changeset
|
425 self.assertEqual(all[flipped], (sub11_path, [], [])) |
|
60e722384d62
New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents:
28240
diff
changeset
|
426 self.assertEqual(all[flipped + 1], (sub1_path, ["SUB11"], ["tmp2"])) |
|
41385
512f56afa4a0
Fix WalkTests.test_traversal() on Windows. The cleanup in
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents:
41381
diff
changeset
|
427 self.assertEqual(all[2 - 2 * flipped], sub2_tree) |
|
41258
1dc4c4ce2db9
New test for rev. 54407 which only uses directories under TESTFN.
Georg Brandl <georg@python.org>
parents:
41242
diff
changeset
|
428 |
|
41385
512f56afa4a0
Fix WalkTests.test_traversal() on Windows. The cleanup in
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents:
41381
diff
changeset
|
429 if hasattr(os, "symlink"): |
|
512f56afa4a0
Fix WalkTests.test_traversal() on Windows. The cleanup in
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents:
41381
diff
changeset
|
430 # Walk, following symlinks. |
|
512f56afa4a0
Fix WalkTests.test_traversal() on Windows. The cleanup in
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents:
41381
diff
changeset
|
431 for root, dirs, files in os.walk(walk_path, followlinks=True): |
|
512f56afa4a0
Fix WalkTests.test_traversal() on Windows. The cleanup in
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents:
41381
diff
changeset
|
432 if root == link_path: |
|
512f56afa4a0
Fix WalkTests.test_traversal() on Windows. The cleanup in
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents:
41381
diff
changeset
|
433 self.assertEqual(dirs, []) |
|
512f56afa4a0
Fix WalkTests.test_traversal() on Windows. The cleanup in
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents:
41381
diff
changeset
|
434 self.assertEqual(files, ["tmp4"]) |
|
512f56afa4a0
Fix WalkTests.test_traversal() on Windows. The cleanup in
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents:
41381
diff
changeset
|
435 break |
|
512f56afa4a0
Fix WalkTests.test_traversal() on Windows. The cleanup in
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents:
41381
diff
changeset
|
436 else: |
|
512f56afa4a0
Fix WalkTests.test_traversal() on Windows. The cleanup in
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents:
41381
diff
changeset
|
437 self.fail("Didn't follow symlink with followlinks=True") |
|
28820
60e722384d62
New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents:
28240
diff
changeset
|
438 |
|
41385
512f56afa4a0
Fix WalkTests.test_traversal() on Windows. The cleanup in
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents:
41381
diff
changeset
|
439 def tearDown(self): |
|
28820
60e722384d62
New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents:
28240
diff
changeset
|
440 # Tear everything down. This is a decent use for bottom-up on |
|
60e722384d62
New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents:
28240
diff
changeset
|
441 # Windows, which doesn't have a recursive delete command. The |
|
60e722384d62
New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents:
28240
diff
changeset
|
442 # (not so) subtlety is that rmdir will fail unless the dir's |
|
60e722384d62
New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents:
28240
diff
changeset
|
443 # kids are removed first, so bottom up is essential. |
|
28917
1741af48fd9f
Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents:
28869
diff
changeset
|
444 for root, dirs, files in os.walk(test_support.TESTFN, topdown=False): |
|
28820
60e722384d62
New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents:
28240
diff
changeset
|
445 for name in files: |
|
41385
512f56afa4a0
Fix WalkTests.test_traversal() on Windows. The cleanup in
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents:
41381
diff
changeset
|
446 os.remove(os.path.join(root, name)) |
|
28820
60e722384d62
New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents:
28240
diff
changeset
|
447 for name in dirs: |
|
41385
512f56afa4a0
Fix WalkTests.test_traversal() on Windows. The cleanup in
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents:
41381
diff
changeset
|
448 dirname = os.path.join(root, name) |
|
41258
1dc4c4ce2db9
New test for rev. 54407 which only uses directories under TESTFN.
Georg Brandl <georg@python.org>
parents:
41242
diff
changeset
|
449 if not os.path.islink(dirname): |
|
1dc4c4ce2db9
New test for rev. 54407 which only uses directories under TESTFN.
Georg Brandl <georg@python.org>
parents:
41242
diff
changeset
|
450 os.rmdir(dirname) |
|
1dc4c4ce2db9
New test for rev. 54407 which only uses directories under TESTFN.
Georg Brandl <georg@python.org>
parents:
41242
diff
changeset
|
451 else: |
|
1dc4c4ce2db9
New test for rev. 54407 which only uses directories under TESTFN.
Georg Brandl <georg@python.org>
parents:
41242
diff
changeset
|
452 os.remove(dirname) |
|
28917
1741af48fd9f
Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents:
28869
diff
changeset
|
453 os.rmdir(test_support.TESTFN) |
|
28820
60e722384d62
New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents:
28240
diff
changeset
|
454 |
|
31254
f6064f8c1460
As part of fixing bug #829532, add a test case that exercises os.makedirs
Andrew M. Kuchling <amk@amk.ca>
parents:
30723
diff
changeset
|
455 class MakedirTests (unittest.TestCase): |
|
f6064f8c1460
As part of fixing bug #829532, add a test case that exercises os.makedirs
Andrew M. Kuchling <amk@amk.ca>
parents:
30723
diff
changeset
|
456 def setUp(self): |
|
f6064f8c1460
As part of fixing bug #829532, add a test case that exercises os.makedirs
Andrew M. Kuchling <amk@amk.ca>
parents:
30723
diff
changeset
|
457 os.mkdir(test_support.TESTFN) |
|
f6064f8c1460
As part of fixing bug #829532, add a test case that exercises os.makedirs
Andrew M. Kuchling <amk@amk.ca>
parents:
30723
diff
changeset
|
458 |
|
f6064f8c1460
As part of fixing bug #829532, add a test case that exercises os.makedirs
Andrew M. Kuchling <amk@amk.ca>
parents:
30723
diff
changeset
|
459 def test_makedir(self): |
|
f6064f8c1460
As part of fixing bug #829532, add a test case that exercises os.makedirs
Andrew M. Kuchling <amk@amk.ca>
parents:
30723
diff
changeset
|
460 base = test_support.TESTFN |
|
f6064f8c1460
As part of fixing bug #829532, add a test case that exercises os.makedirs
Andrew M. Kuchling <amk@amk.ca>
parents:
30723
diff
changeset
|
461 path = os.path.join(base, 'dir1', 'dir2', 'dir3') |
|
f6064f8c1460
As part of fixing bug #829532, add a test case that exercises os.makedirs
Andrew M. Kuchling <amk@amk.ca>
parents:
30723
diff
changeset
|
462 os.makedirs(path) # Should work |
|
f6064f8c1460
As part of fixing bug #829532, add a test case that exercises os.makedirs
Andrew M. Kuchling <amk@amk.ca>
parents:
30723
diff
changeset
|
463 path = os.path.join(base, 'dir1', 'dir2', 'dir3', 'dir4') |
|
f6064f8c1460
As part of fixing bug #829532, add a test case that exercises os.makedirs
Andrew M. Kuchling <amk@amk.ca>
parents:
30723
diff
changeset
|
464 os.makedirs(path) |
|
f6064f8c1460
As part of fixing bug #829532, add a test case that exercises os.makedirs
Andrew M. Kuchling <amk@amk.ca>
parents:
30723
diff
changeset
|
465 |
|
f6064f8c1460
As part of fixing bug #829532, add a test case that exercises os.makedirs
Andrew M. Kuchling <amk@amk.ca>
parents:
30723
diff
changeset
|
466 # Try paths with a '.' in them |
|
f6064f8c1460
As part of fixing bug #829532, add a test case that exercises os.makedirs
Andrew M. Kuchling <amk@amk.ca>
parents:
30723
diff
changeset
|
467 self.failUnlessRaises(OSError, os.makedirs, os.curdir) |
|
f6064f8c1460
As part of fixing bug #829532, add a test case that exercises os.makedirs
Andrew M. Kuchling <amk@amk.ca>
parents:
30723
diff
changeset
|
468 path = os.path.join(base, 'dir1', 'dir2', 'dir3', 'dir4', 'dir5', os.curdir) |
|
f6064f8c1460
As part of fixing bug #829532, add a test case that exercises os.makedirs
Andrew M. Kuchling <amk@amk.ca>
parents:
30723
diff
changeset
|
469 os.makedirs(path) |
|
f6064f8c1460
As part of fixing bug #829532, add a test case that exercises os.makedirs
Andrew M. Kuchling <amk@amk.ca>
parents:
30723
diff
changeset
|
470 path = os.path.join(base, 'dir1', os.curdir, 'dir2', 'dir3', 'dir4', |
|
f6064f8c1460
As part of fixing bug #829532, add a test case that exercises os.makedirs
Andrew M. Kuchling <amk@amk.ca>
parents:
30723
diff
changeset
|
471 'dir5', 'dir6') |
|
f6064f8c1460
As part of fixing bug #829532, add a test case that exercises os.makedirs
Andrew M. Kuchling <amk@amk.ca>
parents:
30723
diff
changeset
|
472 os.makedirs(path) |
|
31389
aca1ce1b4b61
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
31254
diff
changeset
|
473 |
|
31254
f6064f8c1460
As part of fixing bug #829532, add a test case that exercises os.makedirs
Andrew M. Kuchling <amk@amk.ca>
parents:
30723
diff
changeset
|
474 |
|
31389
aca1ce1b4b61
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
31254
diff
changeset
|
475 |
|
aca1ce1b4b61
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
31254
diff
changeset
|
476 |
|
31254
f6064f8c1460
As part of fixing bug #829532, add a test case that exercises os.makedirs
Andrew M. Kuchling <amk@amk.ca>
parents:
30723
diff
changeset
|
477 def tearDown(self): |
|
f6064f8c1460
As part of fixing bug #829532, add a test case that exercises os.makedirs
Andrew M. Kuchling <amk@amk.ca>
parents:
30723
diff
changeset
|
478 path = os.path.join(test_support.TESTFN, 'dir1', 'dir2', 'dir3', |
|
f6064f8c1460
As part of fixing bug #829532, add a test case that exercises os.makedirs
Andrew M. Kuchling <amk@amk.ca>
parents:
30723
diff
changeset
|
479 'dir4', 'dir5', 'dir6') |
|
f6064f8c1460
As part of fixing bug #829532, add a test case that exercises os.makedirs
Andrew M. Kuchling <amk@amk.ca>
parents:
30723
diff
changeset
|
480 # If the tests failed, the bottom-most directory ('../dir6') |
|
f6064f8c1460
As part of fixing bug #829532, add a test case that exercises os.makedirs
Andrew M. Kuchling <amk@amk.ca>
parents:
30723
diff
changeset
|
481 # may not have been created, so we look for the outermost directory |
|
f6064f8c1460
As part of fixing bug #829532, add a test case that exercises os.makedirs
Andrew M. Kuchling <amk@amk.ca>
parents:
30723
diff
changeset
|
482 # that exists. |
|
f6064f8c1460
As part of fixing bug #829532, add a test case that exercises os.makedirs
Andrew M. Kuchling <amk@amk.ca>
parents:
30723
diff
changeset
|
483 while not os.path.exists(path) and path != test_support.TESTFN: |
|
f6064f8c1460
As part of fixing bug #829532, add a test case that exercises os.makedirs
Andrew M. Kuchling <amk@amk.ca>
parents:
30723
diff
changeset
|
484 path = os.path.dirname(path) |
|
f6064f8c1460
As part of fixing bug #829532, add a test case that exercises os.makedirs
Andrew M. Kuchling <amk@amk.ca>
parents:
30723
diff
changeset
|
485 |
|
f6064f8c1460
As part of fixing bug #829532, add a test case that exercises os.makedirs
Andrew M. Kuchling <amk@amk.ca>
parents:
30723
diff
changeset
|
486 os.removedirs(path) |
|
f6064f8c1460
As part of fixing bug #829532, add a test case that exercises os.makedirs
Andrew M. Kuchling <amk@amk.ca>
parents:
30723
diff
changeset
|
487 |
|
32302
e196b823e6c5
Feature request #935915: Add os.path.devnull.
Martin v. Löwis <martin@v.loewis.de>
parents:
32204
diff
changeset
|
488 class DevNullTests (unittest.TestCase): |
|
e196b823e6c5
Feature request #935915: Add os.path.devnull.
Martin v. Löwis <martin@v.loewis.de>
parents:
32204
diff
changeset
|
489 def test_devnull(self): |
|
e196b823e6c5
Feature request #935915: Add os.path.devnull.
Martin v. Löwis <martin@v.loewis.de>
parents:
32204
diff
changeset
|
490 f = file(os.devnull, 'w') |
|
e196b823e6c5
Feature request #935915: Add os.path.devnull.
Martin v. Löwis <martin@v.loewis.de>
parents:
32204
diff
changeset
|
491 f.write('hello') |
|
e196b823e6c5
Feature request #935915: Add os.path.devnull.
Martin v. Löwis <martin@v.loewis.de>
parents:
32204
diff
changeset
|
492 f.close() |
|
e196b823e6c5
Feature request #935915: Add os.path.devnull.
Martin v. Löwis <martin@v.loewis.de>
parents:
32204
diff
changeset
|
493 f = file(os.devnull, 'r') |
|
32307
ec88f278a1ad
test_devnull(): Use assertEqual() instead of an assert stmt.
Tim Peters <tim.peters@gmail.com>
parents:
32302
diff
changeset
|
494 self.assertEqual(f.read(), '') |
|
32302
e196b823e6c5
Feature request #935915: Add os.path.devnull.
Martin v. Löwis <martin@v.loewis.de>
parents:
32204
diff
changeset
|
495 f.close() |
|
31254
f6064f8c1460
As part of fixing bug #829532, add a test case that exercises os.makedirs
Andrew M. Kuchling <amk@amk.ca>
parents:
30723
diff
changeset
|
496 |
|
33317
cf0ea19869af
Patch #934711: Expose platform-specific entropy.
Martin v. Löwis <martin@v.loewis.de>
parents:
32307
diff
changeset
|
497 class URandomTests (unittest.TestCase): |
|
cf0ea19869af
Patch #934711: Expose platform-specific entropy.
Martin v. Löwis <martin@v.loewis.de>
parents:
32307
diff
changeset
|
498 def test_urandom(self): |
|
cf0ea19869af
Patch #934711: Expose platform-specific entropy.
Martin v. Löwis <martin@v.loewis.de>
parents:
32307
diff
changeset
|
499 try: |
|
cf0ea19869af
Patch #934711: Expose platform-specific entropy.
Martin v. Löwis <martin@v.loewis.de>
parents:
32307
diff
changeset
|
500 self.assertEqual(len(os.urandom(1)), 1) |
|
cf0ea19869af
Patch #934711: Expose platform-specific entropy.
Martin v. Löwis <martin@v.loewis.de>
parents:
32307
diff
changeset
|
501 self.assertEqual(len(os.urandom(10)), 10) |
|
cf0ea19869af
Patch #934711: Expose platform-specific entropy.
Martin v. Löwis <martin@v.loewis.de>
parents:
32307
diff
changeset
|
502 self.assertEqual(len(os.urandom(100)), 100) |
|
cf0ea19869af
Patch #934711: Expose platform-specific entropy.
Martin v. Löwis <martin@v.loewis.de>
parents:
32307
diff
changeset
|
503 self.assertEqual(len(os.urandom(1000)), 1000) |
|
48955
99ee3b77bc0d
Issue #3708: os.urandom no longer goes into an infinite loop when passed a
Gregory P. Smith <greg@mad-scientist.com>
parents:
48803
diff
changeset
|
504 # see http://bugs.python.org/issue3708 |
|
50916
c3a0ee51d867
I'm sick of these deprecations warnings in test_os
Benjamin Peterson <benjamin@python.org>
parents:
50910
diff
changeset
|
505 with test_support.check_warnings(): |
|
c3a0ee51d867
I'm sick of these deprecations warnings in test_os
Benjamin Peterson <benjamin@python.org>
parents:
50910
diff
changeset
|
506 # silence deprecation warnings about float arguments |
|
c3a0ee51d867
I'm sick of these deprecations warnings in test_os
Benjamin Peterson <benjamin@python.org>
parents:
50910
diff
changeset
|
507 self.assertEqual(len(os.urandom(0.9)), 0) |
|
c3a0ee51d867
I'm sick of these deprecations warnings in test_os
Benjamin Peterson <benjamin@python.org>
parents:
50910
diff
changeset
|
508 self.assertEqual(len(os.urandom(1.1)), 1) |
|
c3a0ee51d867
I'm sick of these deprecations warnings in test_os
Benjamin Peterson <benjamin@python.org>
parents:
50910
diff
changeset
|
509 self.assertEqual(len(os.urandom(2.0)), 2) |
|
33317
cf0ea19869af
Patch #934711: Expose platform-specific entropy.
Martin v. Löwis <martin@v.loewis.de>
parents:
32307
diff
changeset
|
510 except NotImplementedError: |
|
cf0ea19869af
Patch #934711: Expose platform-specific entropy.
Martin v. Löwis <martin@v.loewis.de>
parents:
32307
diff
changeset
|
511 pass |
|
cf0ea19869af
Patch #934711: Expose platform-specific entropy.
Martin v. Löwis <martin@v.loewis.de>
parents:
32307
diff
changeset
|
512 |
|
38112
be49645e3984
Implement os.{chdir,rename,rmdir,remove} using Win32 directly.
Martin v. Löwis <martin@v.loewis.de>
parents:
34607
diff
changeset
|
513 class Win32ErrorTests(unittest.TestCase): |
|
be49645e3984
Implement os.{chdir,rename,rmdir,remove} using Win32 directly.
Martin v. Löwis <martin@v.loewis.de>
parents:
34607
diff
changeset
|
514 def test_rename(self): |
|
be49645e3984
Implement os.{chdir,rename,rmdir,remove} using Win32 directly.
Martin v. Löwis <martin@v.loewis.de>
parents:
34607
diff
changeset
|
515 self.assertRaises(WindowsError, os.rename, test_support.TESTFN, test_support.TESTFN+".bak") |
|
be49645e3984
Implement os.{chdir,rename,rmdir,remove} using Win32 directly.
Martin v. Löwis <martin@v.loewis.de>
parents:
34607
diff
changeset
|
516 |
|
be49645e3984
Implement os.{chdir,rename,rmdir,remove} using Win32 directly.
Martin v. Löwis <martin@v.loewis.de>
parents:
34607
diff
changeset
|
517 def test_remove(self): |
|
be49645e3984
Implement os.{chdir,rename,rmdir,remove} using Win32 directly.
Martin v. Löwis <martin@v.loewis.de>
parents:
34607
diff
changeset
|
518 self.assertRaises(WindowsError, os.remove, test_support.TESTFN) |
|
be49645e3984
Implement os.{chdir,rename,rmdir,remove} using Win32 directly.
Martin v. Löwis <martin@v.loewis.de>
parents:
34607
diff
changeset
|
519 |
|
be49645e3984
Implement os.{chdir,rename,rmdir,remove} using Win32 directly.
Martin v. Löwis <martin@v.loewis.de>
parents:
34607
diff
changeset
|
520 def test_chdir(self): |
|
be49645e3984
Implement os.{chdir,rename,rmdir,remove} using Win32 directly.
Martin v. Löwis <martin@v.loewis.de>
parents:
34607
diff
changeset
|
521 self.assertRaises(WindowsError, os.chdir, test_support.TESTFN) |
|
be49645e3984
Implement os.{chdir,rename,rmdir,remove} using Win32 directly.
Martin v. Löwis <martin@v.loewis.de>
parents:
34607
diff
changeset
|
522 |
|
38125
0d879bff25f6
Port access, chmod, parts of getcwdu, mkdir, and utime to direct Win32 API.
Martin v. Löwis <martin@v.loewis.de>
parents:
38112
diff
changeset
|
523 def test_mkdir(self): |
|
51395
4327dfa68118
Fix a number of Win32ErrorTests error cases. chmod wasn't being tested. 'access' never raises an error.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
51198
diff
changeset
|
524 f = open(test_support.TESTFN, "w") |
|
4327dfa68118
Fix a number of Win32ErrorTests error cases. chmod wasn't being tested. 'access' never raises an error.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
51198
diff
changeset
|
525 try: |
|
4327dfa68118
Fix a number of Win32ErrorTests error cases. chmod wasn't being tested. 'access' never raises an error.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
51198
diff
changeset
|
526 self.assertRaises(WindowsError, os.mkdir, test_support.TESTFN) |
|
4327dfa68118
Fix a number of Win32ErrorTests error cases. chmod wasn't being tested. 'access' never raises an error.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
51198
diff
changeset
|
527 finally: |
|
4327dfa68118
Fix a number of Win32ErrorTests error cases. chmod wasn't being tested. 'access' never raises an error.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
51198
diff
changeset
|
528 f.close() |
|
4327dfa68118
Fix a number of Win32ErrorTests error cases. chmod wasn't being tested. 'access' never raises an error.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
51198
diff
changeset
|
529 os.unlink(test_support.TESTFN) |
|
38125
0d879bff25f6
Port access, chmod, parts of getcwdu, mkdir, and utime to direct Win32 API.
Martin v. Löwis <martin@v.loewis.de>
parents:
38112
diff
changeset
|
530 |
|
0d879bff25f6
Port access, chmod, parts of getcwdu, mkdir, and utime to direct Win32 API.
Martin v. Löwis <martin@v.loewis.de>
parents:
38112
diff
changeset
|
531 def test_utime(self): |
|
0d879bff25f6
Port access, chmod, parts of getcwdu, mkdir, and utime to direct Win32 API.
Martin v. Löwis <martin@v.loewis.de>
parents:
38112
diff
changeset
|
532 self.assertRaises(WindowsError, os.utime, test_support.TESTFN, None) |
|
0d879bff25f6
Port access, chmod, parts of getcwdu, mkdir, and utime to direct Win32 API.
Martin v. Löwis <martin@v.loewis.de>
parents:
38112
diff
changeset
|
533 |
|
0d879bff25f6
Port access, chmod, parts of getcwdu, mkdir, and utime to direct Win32 API.
Martin v. Löwis <martin@v.loewis.de>
parents:
38112
diff
changeset
|
534 def test_chmod(self): |
|
51395
4327dfa68118
Fix a number of Win32ErrorTests error cases. chmod wasn't being tested. 'access' never raises an error.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
51198
diff
changeset
|
535 self.assertRaises(WindowsError, os.chmod, test_support.TESTFN, 0) |
|
38125
0d879bff25f6
Port access, chmod, parts of getcwdu, mkdir, and utime to direct Win32 API.
Martin v. Löwis <martin@v.loewis.de>
parents:
38112
diff
changeset
|
536 |
|
50788
e11c99e1e6e7
Merged revisions 68521,68527,68534-68536,68540,68547,68552,68563,68570,68572,68575,68579-68580,68584 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49094
diff
changeset
|
537 class TestInvalidFD(unittest.TestCase): |
|
50805
6dde0df3bb4e
Fix two test cases in test_os. ftruncate raises IOError unlike all the others which raise OSError. And close() on some platforms doesn't complain when given an invalid file descriptor.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
50795
diff
changeset
|
538 singles = ["fchdir", "fdopen", "dup", "fdatasync", "fstat", |
|
50788
e11c99e1e6e7
Merged revisions 68521,68527,68534-68536,68540,68547,68552,68563,68570,68572,68575,68579-68580,68584 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49094
diff
changeset
|
539 "fstatvfs", "fsync", "tcgetpgrp", "ttyname"] |
|
50805
6dde0df3bb4e
Fix two test cases in test_os. ftruncate raises IOError unlike all the others which raise OSError. And close() on some platforms doesn't complain when given an invalid file descriptor.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
50795
diff
changeset
|
540 #singles.append("close") |
|
6dde0df3bb4e
Fix two test cases in test_os. ftruncate raises IOError unlike all the others which raise OSError. And close() on some platforms doesn't complain when given an invalid file descriptor.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
50795
diff
changeset
|
541 #We omit close because it doesn'r raise an exception on some platforms |
|
50788
e11c99e1e6e7
Merged revisions 68521,68527,68534-68536,68540,68547,68552,68563,68570,68572,68575,68579-68580,68584 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49094
diff
changeset
|
542 def get_single(f): |
|
e11c99e1e6e7
Merged revisions 68521,68527,68534-68536,68540,68547,68552,68563,68570,68572,68575,68579-68580,68584 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49094
diff
changeset
|
543 def helper(self): |
|
50910
fa12c8e5ae8a
make bad file descriptor tests more robust
Benjamin Peterson <benjamin@python.org>
parents:
50895
diff
changeset
|
544 if hasattr(os, f): |
|
fa12c8e5ae8a
make bad file descriptor tests more robust
Benjamin Peterson <benjamin@python.org>
parents:
50895
diff
changeset
|
545 self.check(getattr(os, f)) |
|
50788
e11c99e1e6e7
Merged revisions 68521,68527,68534-68536,68540,68547,68552,68563,68570,68572,68575,68579-68580,68584 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49094
diff
changeset
|
546 return helper |
|
e11c99e1e6e7
Merged revisions 68521,68527,68534-68536,68540,68547,68552,68563,68570,68572,68575,68579-68580,68584 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49094
diff
changeset
|
547 for f in singles: |
|
e11c99e1e6e7
Merged revisions 68521,68527,68534-68536,68540,68547,68552,68563,68570,68572,68575,68579-68580,68584 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49094
diff
changeset
|
548 locals()["test_"+f] = get_single(f) |
|
e11c99e1e6e7
Merged revisions 68521,68527,68534-68536,68540,68547,68552,68563,68570,68572,68575,68579-68580,68584 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49094
diff
changeset
|
549 |
|
50910
fa12c8e5ae8a
make bad file descriptor tests more robust
Benjamin Peterson <benjamin@python.org>
parents:
50895
diff
changeset
|
550 def check(self, f, *args): |
|
51198
4c9ea647eaf4
check the errno in bad fd cases
Benjamin Peterson <benjamin@python.org>
parents:
50916
diff
changeset
|
551 try: |
|
4c9ea647eaf4
check the errno in bad fd cases
Benjamin Peterson <benjamin@python.org>
parents:
50916
diff
changeset
|
552 f(test_support.make_bad_fd(), *args) |
|
4c9ea647eaf4
check the errno in bad fd cases
Benjamin Peterson <benjamin@python.org>
parents:
50916
diff
changeset
|
553 except OSError as e: |
|
4c9ea647eaf4
check the errno in bad fd cases
Benjamin Peterson <benjamin@python.org>
parents:
50916
diff
changeset
|
554 self.assertEqual(e.errno, errno.EBADF) |
|
4c9ea647eaf4
check the errno in bad fd cases
Benjamin Peterson <benjamin@python.org>
parents:
50916
diff
changeset
|
555 else: |
|
4c9ea647eaf4
check the errno in bad fd cases
Benjamin Peterson <benjamin@python.org>
parents:
50916
diff
changeset
|
556 self.fail("%r didn't raise a OSError with a bad file descriptor" |
|
4c9ea647eaf4
check the errno in bad fd cases
Benjamin Peterson <benjamin@python.org>
parents:
50916
diff
changeset
|
557 % f) |
|
50910
fa12c8e5ae8a
make bad file descriptor tests more robust
Benjamin Peterson <benjamin@python.org>
parents:
50895
diff
changeset
|
558 |
|
50788
e11c99e1e6e7
Merged revisions 68521,68527,68534-68536,68540,68547,68552,68563,68570,68572,68575,68579-68580,68584 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49094
diff
changeset
|
559 def test_isatty(self): |
|
50806
65bebe4a1aa3
Make all the invalid fd tests for os subject to the function being available.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
50805
diff
changeset
|
560 if hasattr(os, "isatty"): |
|
50910
fa12c8e5ae8a
make bad file descriptor tests more robust
Benjamin Peterson <benjamin@python.org>
parents:
50895
diff
changeset
|
561 self.assertEqual(os.isatty(test_support.make_bad_fd()), False) |
|
50788
e11c99e1e6e7
Merged revisions 68521,68527,68534-68536,68540,68547,68552,68563,68570,68572,68575,68579-68580,68584 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49094
diff
changeset
|
562 |
|
e11c99e1e6e7
Merged revisions 68521,68527,68534-68536,68540,68547,68552,68563,68570,68572,68575,68579-68580,68584 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49094
diff
changeset
|
563 def test_closerange(self): |
|
50806
65bebe4a1aa3
Make all the invalid fd tests for os subject to the function being available.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
50805
diff
changeset
|
564 if hasattr(os, "closerange"): |
|
50910
fa12c8e5ae8a
make bad file descriptor tests more robust
Benjamin Peterson <benjamin@python.org>
parents:
50895
diff
changeset
|
565 fd = test_support.make_bad_fd() |
|
fa12c8e5ae8a
make bad file descriptor tests more robust
Benjamin Peterson <benjamin@python.org>
parents:
50895
diff
changeset
|
566 self.assertEqual(os.closerange(fd, fd + 10), None) |
|
50788
e11c99e1e6e7
Merged revisions 68521,68527,68534-68536,68540,68547,68552,68563,68570,68572,68575,68579-68580,68584 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49094
diff
changeset
|
567 |
|
e11c99e1e6e7
Merged revisions 68521,68527,68534-68536,68540,68547,68552,68563,68570,68572,68575,68579-68580,68584 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49094
diff
changeset
|
568 def test_dup2(self): |
|
50806
65bebe4a1aa3
Make all the invalid fd tests for os subject to the function being available.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
50805
diff
changeset
|
569 if hasattr(os, "dup2"): |
|
50910
fa12c8e5ae8a
make bad file descriptor tests more robust
Benjamin Peterson <benjamin@python.org>
parents:
50895
diff
changeset
|
570 self.check(os.dup2, 20) |
|
50788
e11c99e1e6e7
Merged revisions 68521,68527,68534-68536,68540,68547,68552,68563,68570,68572,68575,68579-68580,68584 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49094
diff
changeset
|
571 |
|
e11c99e1e6e7
Merged revisions 68521,68527,68534-68536,68540,68547,68552,68563,68570,68572,68575,68579-68580,68584 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49094
diff
changeset
|
572 def test_fchmod(self): |
|
e11c99e1e6e7
Merged revisions 68521,68527,68534-68536,68540,68547,68552,68563,68570,68572,68575,68579-68580,68584 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49094
diff
changeset
|
573 if hasattr(os, "fchmod"): |
|
50910
fa12c8e5ae8a
make bad file descriptor tests more robust
Benjamin Peterson <benjamin@python.org>
parents:
50895
diff
changeset
|
574 self.check(os.fchmod, 0) |
|
50788
e11c99e1e6e7
Merged revisions 68521,68527,68534-68536,68540,68547,68552,68563,68570,68572,68575,68579-68580,68584 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49094
diff
changeset
|
575 |
|
e11c99e1e6e7
Merged revisions 68521,68527,68534-68536,68540,68547,68552,68563,68570,68572,68575,68579-68580,68584 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49094
diff
changeset
|
576 def test_fchown(self): |
|
e11c99e1e6e7
Merged revisions 68521,68527,68534-68536,68540,68547,68552,68563,68570,68572,68575,68579-68580,68584 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49094
diff
changeset
|
577 if hasattr(os, "fchown"): |
|
50910
fa12c8e5ae8a
make bad file descriptor tests more robust
Benjamin Peterson <benjamin@python.org>
parents:
50895
diff
changeset
|
578 self.check(os.fchown, -1, -1) |
|
50788
e11c99e1e6e7
Merged revisions 68521,68527,68534-68536,68540,68547,68552,68563,68570,68572,68575,68579-68580,68584 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49094
diff
changeset
|
579 |
|
e11c99e1e6e7
Merged revisions 68521,68527,68534-68536,68540,68547,68552,68563,68570,68572,68575,68579-68580,68584 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49094
diff
changeset
|
580 def test_fpathconf(self): |
|
e11c99e1e6e7
Merged revisions 68521,68527,68534-68536,68540,68547,68552,68563,68570,68572,68575,68579-68580,68584 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49094
diff
changeset
|
581 if hasattr(os, "fpathconf"): |
|
50910
fa12c8e5ae8a
make bad file descriptor tests more robust
Benjamin Peterson <benjamin@python.org>
parents:
50895
diff
changeset
|
582 self.check(os.fpathconf, "PC_NAME_MAX") |
|
50788
e11c99e1e6e7
Merged revisions 68521,68527,68534-68536,68540,68547,68552,68563,68570,68572,68575,68579-68580,68584 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49094
diff
changeset
|
583 |
|
e11c99e1e6e7
Merged revisions 68521,68527,68534-68536,68540,68547,68552,68563,68570,68572,68575,68579-68580,68584 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49094
diff
changeset
|
584 def test_ftruncate(self): |
|
e11c99e1e6e7
Merged revisions 68521,68527,68534-68536,68540,68547,68552,68563,68570,68572,68575,68579-68580,68584 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49094
diff
changeset
|
585 if hasattr(os, "ftruncate"): |
|
50910
fa12c8e5ae8a
make bad file descriptor tests more robust
Benjamin Peterson <benjamin@python.org>
parents:
50895
diff
changeset
|
586 self.check(os.ftruncate, 0) |
|
50788
e11c99e1e6e7
Merged revisions 68521,68527,68534-68536,68540,68547,68552,68563,68570,68572,68575,68579-68580,68584 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49094
diff
changeset
|
587 |
|
e11c99e1e6e7
Merged revisions 68521,68527,68534-68536,68540,68547,68552,68563,68570,68572,68575,68579-68580,68584 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49094
diff
changeset
|
588 def test_lseek(self): |
|
50806
65bebe4a1aa3
Make all the invalid fd tests for os subject to the function being available.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
50805
diff
changeset
|
589 if hasattr(os, "lseek"): |
|
50910
fa12c8e5ae8a
make bad file descriptor tests more robust
Benjamin Peterson <benjamin@python.org>
parents:
50895
diff
changeset
|
590 self.check(os.lseek, 0, 0) |
|
50788
e11c99e1e6e7
Merged revisions 68521,68527,68534-68536,68540,68547,68552,68563,68570,68572,68575,68579-68580,68584 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49094
diff
changeset
|
591 |
|
e11c99e1e6e7
Merged revisions 68521,68527,68534-68536,68540,68547,68552,68563,68570,68572,68575,68579-68580,68584 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49094
diff
changeset
|
592 def test_read(self): |
|
50806
65bebe4a1aa3
Make all the invalid fd tests for os subject to the function being available.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
50805
diff
changeset
|
593 if hasattr(os, "read"): |
|
50910
fa12c8e5ae8a
make bad file descriptor tests more robust
Benjamin Peterson <benjamin@python.org>
parents:
50895
diff
changeset
|
594 self.check(os.read, 1) |
|
50788
e11c99e1e6e7
Merged revisions 68521,68527,68534-68536,68540,68547,68552,68563,68570,68572,68575,68579-68580,68584 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49094
diff
changeset
|
595 |
|
e11c99e1e6e7
Merged revisions 68521,68527,68534-68536,68540,68547,68552,68563,68570,68572,68575,68579-68580,68584 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49094
diff
changeset
|
596 def test_tcsetpgrpt(self): |
|
e11c99e1e6e7
Merged revisions 68521,68527,68534-68536,68540,68547,68552,68563,68570,68572,68575,68579-68580,68584 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49094
diff
changeset
|
597 if hasattr(os, "tcsetpgrp"): |
|
50910
fa12c8e5ae8a
make bad file descriptor tests more robust
Benjamin Peterson <benjamin@python.org>
parents:
50895
diff
changeset
|
598 self.check(os.tcsetpgrp, 0) |
|
50788
e11c99e1e6e7
Merged revisions 68521,68527,68534-68536,68540,68547,68552,68563,68570,68572,68575,68579-68580,68584 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49094
diff
changeset
|
599 |
|
e11c99e1e6e7
Merged revisions 68521,68527,68534-68536,68540,68547,68552,68563,68570,68572,68575,68579-68580,68584 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49094
diff
changeset
|
600 def test_write(self): |
|
50806
65bebe4a1aa3
Make all the invalid fd tests for os subject to the function being available.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
50805
diff
changeset
|
601 if hasattr(os, "write"): |
|
50910
fa12c8e5ae8a
make bad file descriptor tests more robust
Benjamin Peterson <benjamin@python.org>
parents:
50895
diff
changeset
|
602 self.check(os.write, " ") |
|
50788
e11c99e1e6e7
Merged revisions 68521,68527,68534-68536,68540,68547,68552,68563,68570,68572,68575,68579-68580,68584 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49094
diff
changeset
|
603 |
|
38112
be49645e3984
Implement os.{chdir,rename,rmdir,remove} using Win32 directly.
Martin v. Löwis <martin@v.loewis.de>
parents:
34607
diff
changeset
|
604 if sys.platform != 'win32': |
|
be49645e3984
Implement os.{chdir,rename,rmdir,remove} using Win32 directly.
Martin v. Löwis <martin@v.loewis.de>
parents:
34607
diff
changeset
|
605 class Win32ErrorTests(unittest.TestCase): |
|
be49645e3984
Implement os.{chdir,rename,rmdir,remove} using Win32 directly.
Martin v. Löwis <martin@v.loewis.de>
parents:
34607
diff
changeset
|
606 pass |
|
be49645e3984
Implement os.{chdir,rename,rmdir,remove} using Win32 directly.
Martin v. Löwis <martin@v.loewis.de>
parents:
34607
diff
changeset
|
607 |
|
52925
d36a4f920378
Fixes issue5705: os.setuid() and friends did not accept the same range of
Gregory P. Smith <greg@mad-scientist.com>
parents:
51395
diff
changeset
|
608 class PosixUidGidTests(unittest.TestCase): |
|
d36a4f920378
Fixes issue5705: os.setuid() and friends did not accept the same range of
Gregory P. Smith <greg@mad-scientist.com>
parents:
51395
diff
changeset
|
609 if hasattr(os, 'setuid'): |
|
d36a4f920378
Fixes issue5705: os.setuid() and friends did not accept the same range of
Gregory P. Smith <greg@mad-scientist.com>
parents:
51395
diff
changeset
|
610 def test_setuid(self): |
|
d36a4f920378
Fixes issue5705: os.setuid() and friends did not accept the same range of
Gregory P. Smith <greg@mad-scientist.com>
parents:
51395
diff
changeset
|
611 if os.getuid() != 0: |
|
d36a4f920378
Fixes issue5705: os.setuid() and friends did not accept the same range of
Gregory P. Smith <greg@mad-scientist.com>
parents:
51395
diff
changeset
|
612 self.assertRaises(os.error, os.setuid, 0) |
|
d36a4f920378
Fixes issue5705: os.setuid() and friends did not accept the same range of
Gregory P. Smith <greg@mad-scientist.com>
parents:
51395
diff
changeset
|
613 self.assertRaises(OverflowError, os.setuid, 1<<32) |
|
d36a4f920378
Fixes issue5705: os.setuid() and friends did not accept the same range of
Gregory P. Smith <greg@mad-scientist.com>
parents:
51395
diff
changeset
|
614 |
|
d36a4f920378
Fixes issue5705: os.setuid() and friends did not accept the same range of
Gregory P. Smith <greg@mad-scientist.com>
parents:
51395
diff
changeset
|
615 if hasattr(os, 'setgid'): |
|
d36a4f920378
Fixes issue5705: os.setuid() and friends did not accept the same range of
Gregory P. Smith <greg@mad-scientist.com>
parents:
51395
diff
changeset
|
616 def test_setgid(self): |
|
d36a4f920378
Fixes issue5705: os.setuid() and friends did not accept the same range of
Gregory P. Smith <greg@mad-scientist.com>
parents:
51395
diff
changeset
|
617 if os.getuid() != 0: |
|
d36a4f920378
Fixes issue5705: os.setuid() and friends did not accept the same range of
Gregory P. Smith <greg@mad-scientist.com>
parents:
51395
diff
changeset
|
618 self.assertRaises(os.error, os.setgid, 0) |
|
d36a4f920378
Fixes issue5705: os.setuid() and friends did not accept the same range of
Gregory P. Smith <greg@mad-scientist.com>
parents:
51395
diff
changeset
|
619 self.assertRaises(OverflowError, os.setgid, 1<<32) |
|
d36a4f920378
Fixes issue5705: os.setuid() and friends did not accept the same range of
Gregory P. Smith <greg@mad-scientist.com>
parents:
51395
diff
changeset
|
620 |
|
d36a4f920378
Fixes issue5705: os.setuid() and friends did not accept the same range of
Gregory P. Smith <greg@mad-scientist.com>
parents:
51395
diff
changeset
|
621 if hasattr(os, 'seteuid'): |
|
d36a4f920378
Fixes issue5705: os.setuid() and friends did not accept the same range of
Gregory P. Smith <greg@mad-scientist.com>
parents:
51395
diff
changeset
|
622 def test_seteuid(self): |
|
d36a4f920378
Fixes issue5705: os.setuid() and friends did not accept the same range of
Gregory P. Smith <greg@mad-scientist.com>
parents:
51395
diff
changeset
|
623 if os.getuid() != 0: |
|
d36a4f920378
Fixes issue5705: os.setuid() and friends did not accept the same range of
Gregory P. Smith <greg@mad-scientist.com>
parents:
51395
diff
changeset
|
624 self.assertRaises(os.error, os.seteuid, 0) |
|
d36a4f920378
Fixes issue5705: os.setuid() and friends did not accept the same range of
Gregory P. Smith <greg@mad-scientist.com>
parents:
51395
diff
changeset
|
625 self.assertRaises(OverflowError, os.seteuid, 1<<32) |
|
d36a4f920378
Fixes issue5705: os.setuid() and friends did not accept the same range of
Gregory P. Smith <greg@mad-scientist.com>
parents:
51395
diff
changeset
|
626 |
|
d36a4f920378
Fixes issue5705: os.setuid() and friends did not accept the same range of
Gregory P. Smith <greg@mad-scientist.com>
parents:
51395
diff
changeset
|
627 if hasattr(os, 'setegid'): |
|
d36a4f920378
Fixes issue5705: os.setuid() and friends did not accept the same range of
Gregory P. Smith <greg@mad-scientist.com>
parents:
51395
diff
changeset
|
628 def test_setegid(self): |
|
d36a4f920378
Fixes issue5705: os.setuid() and friends did not accept the same range of
Gregory P. Smith <greg@mad-scientist.com>
parents:
51395
diff
changeset
|
629 if os.getuid() != 0: |
|
d36a4f920378
Fixes issue5705: os.setuid() and friends did not accept the same range of
Gregory P. Smith <greg@mad-scientist.com>
parents:
51395
diff
changeset
|
630 self.assertRaises(os.error, os.setegid, 0) |
|
d36a4f920378
Fixes issue5705: os.setuid() and friends did not accept the same range of
Gregory P. Smith <greg@mad-scientist.com>
parents:
51395
diff
changeset
|
631 self.assertRaises(OverflowError, os.setegid, 1<<32) |
|
d36a4f920378
Fixes issue5705: os.setuid() and friends did not accept the same range of
Gregory P. Smith <greg@mad-scientist.com>
parents:
51395
diff
changeset
|
632 |
|
d36a4f920378
Fixes issue5705: os.setuid() and friends did not accept the same range of
Gregory P. Smith <greg@mad-scientist.com>
parents:
51395
diff
changeset
|
633 if hasattr(os, 'setreuid'): |
|
d36a4f920378
Fixes issue5705: os.setuid() and friends did not accept the same range of
Gregory P. Smith <greg@mad-scientist.com>
parents:
51395
diff
changeset
|
634 def test_setreuid(self): |
|
d36a4f920378
Fixes issue5705: os.setuid() and friends did not accept the same range of
Gregory P. Smith <greg@mad-scientist.com>
parents:
51395
diff
changeset
|
635 if os.getuid() != 0: |
|
d36a4f920378
Fixes issue5705: os.setuid() and friends did not accept the same range of
Gregory P. Smith <greg@mad-scientist.com>
parents:
51395
diff
changeset
|
636 self.assertRaises(os.error, os.setreuid, 0, 0) |
|
d36a4f920378
Fixes issue5705: os.setuid() and friends did not accept the same range of
Gregory P. Smith <greg@mad-scientist.com>
parents:
51395
diff
changeset
|
637 self.assertRaises(OverflowError, os.setreuid, 1<<32, 0) |
|
d36a4f920378
Fixes issue5705: os.setuid() and friends did not accept the same range of
Gregory P. Smith <greg@mad-scientist.com>
parents:
51395
diff
changeset
|
638 self.assertRaises(OverflowError, os.setreuid, 0, 1<<32) |
|
d36a4f920378
Fixes issue5705: os.setuid() and friends did not accept the same range of
Gregory P. Smith <greg@mad-scientist.com>
parents:
51395
diff
changeset
|
639 |
|
d36a4f920378
Fixes issue5705: os.setuid() and friends did not accept the same range of
Gregory P. Smith <greg@mad-scientist.com>
parents:
51395
diff
changeset
|
640 if hasattr(os, 'setregid'): |
|
d36a4f920378
Fixes issue5705: os.setuid() and friends did not accept the same range of
Gregory P. Smith <greg@mad-scientist.com>
parents:
51395
diff
changeset
|
641 def test_setregid(self): |
|
d36a4f920378
Fixes issue5705: os.setuid() and friends did not accept the same range of
Gregory P. Smith <greg@mad-scientist.com>
parents:
51395
diff
changeset
|
642 if os.getuid() != 0: |
|
d36a4f920378
Fixes issue5705: os.setuid() and friends did not accept the same range of
Gregory P. Smith <greg@mad-scientist.com>
parents:
51395
diff
changeset
|
643 self.assertRaises(os.error, os.setregid, 0, 0) |
|
d36a4f920378
Fixes issue5705: os.setuid() and friends did not accept the same range of
Gregory P. Smith <greg@mad-scientist.com>
parents:
51395
diff
changeset
|
644 self.assertRaises(OverflowError, os.setregid, 1<<32, 0) |
|
d36a4f920378
Fixes issue5705: os.setuid() and friends did not accept the same range of
Gregory P. Smith <greg@mad-scientist.com>
parents:
51395
diff
changeset
|
645 self.assertRaises(OverflowError, os.setregid, 0, 1<<32) |
|
d36a4f920378
Fixes issue5705: os.setuid() and friends did not accept the same range of
Gregory P. Smith <greg@mad-scientist.com>
parents:
51395
diff
changeset
|
646 else: |
|
d36a4f920378
Fixes issue5705: os.setuid() and friends did not accept the same range of
Gregory P. Smith <greg@mad-scientist.com>
parents:
51395
diff
changeset
|
647 class PosixUidGidTests(unittest.TestCase): |
|
d36a4f920378
Fixes issue5705: os.setuid() and friends did not accept the same range of
Gregory P. Smith <greg@mad-scientist.com>
parents:
51395
diff
changeset
|
648 pass |
|
d36a4f920378
Fixes issue5705: os.setuid() and friends did not accept the same range of
Gregory P. Smith <greg@mad-scientist.com>
parents:
51395
diff
changeset
|
649 |
|
20013
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
19429
diff
changeset
|
650 def test_main(): |
|
28917
1741af48fd9f
Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents:
28869
diff
changeset
|
651 test_support.run_unittest( |
|
38979
6975bd4884eb
Correct arithmetic in access on Win32. Fixes #1513646.
Martin v. Löwis <martin@v.loewis.de>
parents:
38125
diff
changeset
|
652 FileTests, |
|
28917
1741af48fd9f
Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents:
28869
diff
changeset
|
653 TemporaryFileTests, |
|
1741af48fd9f
Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents:
28869
diff
changeset
|
654 StatAttributeTests, |
|
1741af48fd9f
Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents:
28869
diff
changeset
|
655 EnvironTests, |
|
31254
f6064f8c1460
As part of fixing bug #829532, add a test case that exercises os.makedirs
Andrew M. Kuchling <amk@amk.ca>
parents:
30723
diff
changeset
|
656 WalkTests, |
|
f6064f8c1460
As part of fixing bug #829532, add a test case that exercises os.makedirs
Andrew M. Kuchling <amk@amk.ca>
parents:
30723
diff
changeset
|
657 MakedirTests, |
|
32302
e196b823e6c5
Feature request #935915: Add os.path.devnull.
Martin v. Löwis <martin@v.loewis.de>
parents:
32204
diff
changeset
|
658 DevNullTests, |
|
38112
be49645e3984
Implement os.{chdir,rename,rmdir,remove} using Win32 directly.
Martin v. Löwis <martin@v.loewis.de>
parents:
34607
diff
changeset
|
659 URandomTests, |
|
50788
e11c99e1e6e7
Merged revisions 68521,68527,68534-68536,68540,68547,68552,68563,68570,68572,68575,68579-68580,68584 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49094
diff
changeset
|
660 Win32ErrorTests, |
|
52925
d36a4f920378
Fixes issue5705: os.setuid() and friends did not accept the same range of
Gregory P. Smith <greg@mad-scientist.com>
parents:
51395
diff
changeset
|
661 TestInvalidFD, |
|
d36a4f920378
Fixes issue5705: os.setuid() and friends did not accept the same range of
Gregory P. Smith <greg@mad-scientist.com>
parents:
51395
diff
changeset
|
662 PosixUidGidTests |
|
28917
1741af48fd9f
Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents:
28869
diff
changeset
|
663 ) |
|
20013
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
19429
diff
changeset
|
664 |
|
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
19429
diff
changeset
|
665 if __name__ == "__main__": |
|
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
19429
diff
changeset
|
666 test_main() |
