annotate Lib/test/test_os.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents dec65cb98128
children 8c2635afbfe1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
6f15e73b59f2 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 28917
diff changeset
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
60197
0ab89e8bdedc Implement #1220212. Add os.kill support for Windows.
Brian Curtin <brian.curtin@gmail.com>
parents: 59722
diff changeset
10 import signal
75100
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
11 import subprocess
60197
0ab89e8bdedc Implement #1220212. Add os.kill support for Windows.
Brian Curtin <brian.curtin@gmail.com>
parents: 59722
diff changeset
12 import time
75100
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
13
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28869
diff changeset
14 from test import test_support
65176
acdb32005c0a Merged revisions 85315 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 62663
diff changeset
15 import mmap
acdb32005c0a Merged revisions 85315 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 62663
diff changeset
16 import uuid
18678
a7bc339ee2f3 Start of a test suite for the "os" module -- this only tests the temporary
Fred Drake <fdrake@acm.org>
parents:
diff changeset
17
19429
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19403
diff changeset
18 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
19 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
20
38979
6975bd4884eb Correct arithmetic in access on Win32. Fixes #1513646.
Martin v. Löwis <martin@v.loewis.de>
parents: 38125
diff changeset
21 # Tests creating TESTFN
6975bd4884eb Correct arithmetic in access on Win32. Fixes #1513646.
Martin v. Löwis <martin@v.loewis.de>
parents: 38125
diff changeset
22 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
23 def setUp(self):
6975bd4884eb Correct arithmetic in access on Win32. Fixes #1513646.
Martin v. Löwis <martin@v.loewis.de>
parents: 38125
diff changeset
24 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
25 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
26 tearDown = setUp
6975bd4884eb Correct arithmetic in access on Win32. Fixes #1513646.
Martin v. Löwis <martin@v.loewis.de>
parents: 38125
diff changeset
27
6975bd4884eb Correct arithmetic in access on Win32. Fixes #1513646.
Martin v. Löwis <martin@v.loewis.de>
parents: 38125
diff changeset
28 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
29 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
30 os.close(f)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54117
diff changeset
31 self.assertTrue(os.access(test_support.TESTFN, os.W_OK))
38984
27973962fc9f Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 38979
diff changeset
32
44785
b520d33bee06 #1663329: add os.closerange() to close a range of fds,
Georg Brandl <georg@python.org>
parents: 43082
diff changeset
33 def test_closerange(self):
48267
7a038020c8dd backport r65723: strengthen test_os.test_closerange
Antoine Pitrou <solipsis@pitrou.net>
parents: 47071
diff changeset
34 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
35 # 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
36 # 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
37 # standard ones).
7a038020c8dd backport r65723: strengthen test_os.test_closerange
Antoine Pitrou <solipsis@pitrou.net>
parents: 47071
diff changeset
38 second = os.dup(first)
7a038020c8dd backport r65723: strengthen test_os.test_closerange
Antoine Pitrou <solipsis@pitrou.net>
parents: 47071
diff changeset
39 try:
7a038020c8dd backport r65723: strengthen test_os.test_closerange
Antoine Pitrou <solipsis@pitrou.net>
parents: 47071
diff changeset
40 retries = 0
7a038020c8dd backport r65723: strengthen test_os.test_closerange
Antoine Pitrou <solipsis@pitrou.net>
parents: 47071
diff changeset
41 while second != first + 1:
7a038020c8dd backport r65723: strengthen test_os.test_closerange
Antoine Pitrou <solipsis@pitrou.net>
parents: 47071
diff changeset
42 os.close(first)
7a038020c8dd backport r65723: strengthen test_os.test_closerange
Antoine Pitrou <solipsis@pitrou.net>
parents: 47071
diff changeset
43 retries += 1
7a038020c8dd backport r65723: strengthen test_os.test_closerange
Antoine Pitrou <solipsis@pitrou.net>
parents: 47071
diff changeset
44 if retries > 10:
7a038020c8dd backport r65723: strengthen test_os.test_closerange
Antoine Pitrou <solipsis@pitrou.net>
parents: 47071
diff changeset
45 # XXX test skipped
54117
1f8accd1f42a use skipTest()
Benjamin Peterson <benjamin@python.org>
parents: 52925
diff changeset
46 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
47 first, second = second, os.dup(second)
7a038020c8dd backport r65723: strengthen test_os.test_closerange
Antoine Pitrou <solipsis@pitrou.net>
parents: 47071
diff changeset
48 finally:
7a038020c8dd backport r65723: strengthen test_os.test_closerange
Antoine Pitrou <solipsis@pitrou.net>
parents: 47071
diff changeset
49 os.close(second)
44785
b520d33bee06 #1663329: add os.closerange() to close a range of fds,
Georg Brandl <georg@python.org>
parents: 43082
diff changeset
50 # 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
51 os.closerange(first, first + 2)
7a038020c8dd backport r65723: strengthen test_os.test_closerange
Antoine Pitrou <solipsis@pitrou.net>
parents: 47071
diff changeset
52 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
53
62657
e9c8a96584d4 mark test depending on ref counting
Benjamin Peterson <benjamin@python.org>
parents: 61966
diff changeset
54 @test_support.cpython_only
49096
86a42e9f73dc Issue #3804: Added test for Issue #2222.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 48802
diff changeset
55 def test_rename(self):
86a42e9f73dc Issue #3804: Added test for Issue #2222.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 48802
diff changeset
56 path = unicode(test_support.TESTFN)
86a42e9f73dc Issue #3804: Added test for Issue #2222.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 48802
diff changeset
57 old = sys.getrefcount(path)
86a42e9f73dc Issue #3804: Added test for Issue #2222.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 48802
diff changeset
58 self.assertRaises(TypeError, os.rename, path, 0)
86a42e9f73dc Issue #3804: Added test for Issue #2222.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 48802
diff changeset
59 new = sys.getrefcount(path)
86a42e9f73dc Issue #3804: Added test for Issue #2222.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 48802
diff changeset
60 self.assertEqual(old, new)
86a42e9f73dc Issue #3804: Added test for Issue #2222.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 48802
diff changeset
61
38979
6975bd4884eb Correct arithmetic in access on Win32. Fixes #1513646.
Martin v. Löwis <martin@v.loewis.de>
parents: 38125
diff changeset
62
18678
a7bc339ee2f3 Start of a test suite for the "os" module -- this only tests the temporary
Fred Drake <fdrake@acm.org>
parents:
diff changeset
63 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
64 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
65 self.files = []
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28869
diff changeset
66 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
67
a7bc339ee2f3 Start of a test suite for the "os" module -- this only tests the temporary
Fred Drake <fdrake@acm.org>
parents:
diff changeset
68 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
69 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
70 os.unlink(name)
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28869
diff changeset
71 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
72
a7bc339ee2f3 Start of a test suite for the "os" module -- this only tests the temporary
Fred Drake <fdrake@acm.org>
parents:
diff changeset
73 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
74 # make sure it doesn't already exist:
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54117
diff changeset
75 self.assertFalse(os.path.exists(name),
18678
a7bc339ee2f3 Start of a test suite for the "os" module -- this only tests the temporary
Fred Drake <fdrake@acm.org>
parents:
diff changeset
76 "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
77 # 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
78 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
79 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
80
a7bc339ee2f3 Start of a test suite for the "os" module -- this only tests the temporary
Fred Drake <fdrake@acm.org>
parents:
diff changeset
81 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
82 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
83 return
67263
9ea12e86d565 Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
Antoine Pitrou <solipsis@pitrou.net>
parents: 66348
diff changeset
84 with warnings.catch_warnings():
9ea12e86d565 Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
Antoine Pitrou <solipsis@pitrou.net>
parents: 66348
diff changeset
85 warnings.filterwarnings("ignore", "tempnam", RuntimeWarning,
9ea12e86d565 Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
Antoine Pitrou <solipsis@pitrou.net>
parents: 66348
diff changeset
86 r"test_os$")
9ea12e86d565 Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
Antoine Pitrou <solipsis@pitrou.net>
parents: 66348
diff changeset
87 warnings.filterwarnings("ignore", "tempnam", DeprecationWarning)
9ea12e86d565 Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
Antoine Pitrou <solipsis@pitrou.net>
parents: 66348
diff changeset
88 self.check_tempfile(os.tempnam())
18678
a7bc339ee2f3 Start of a test suite for the "os" module -- this only tests the temporary
Fred Drake <fdrake@acm.org>
parents:
diff changeset
89
67263
9ea12e86d565 Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
Antoine Pitrou <solipsis@pitrou.net>
parents: 66348
diff changeset
90 name = os.tempnam(test_support.TESTFN)
9ea12e86d565 Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
Antoine Pitrou <solipsis@pitrou.net>
parents: 66348
diff changeset
91 self.check_tempfile(name)
18678
a7bc339ee2f3 Start of a test suite for the "os" module -- this only tests the temporary
Fred Drake <fdrake@acm.org>
parents:
diff changeset
92
67263
9ea12e86d565 Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
Antoine Pitrou <solipsis@pitrou.net>
parents: 66348
diff changeset
93 name = os.tempnam(test_support.TESTFN, "pfx")
9ea12e86d565 Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
Antoine Pitrou <solipsis@pitrou.net>
parents: 66348
diff changeset
94 self.assertTrue(os.path.basename(name)[:3] == "pfx")
9ea12e86d565 Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
Antoine Pitrou <solipsis@pitrou.net>
parents: 66348
diff changeset
95 self.check_tempfile(name)
18678
a7bc339ee2f3 Start of a test suite for the "os" module -- this only tests the temporary
Fred Drake <fdrake@acm.org>
parents:
diff changeset
96
a7bc339ee2f3 Start of a test suite for the "os" module -- this only tests the temporary
Fred Drake <fdrake@acm.org>
parents:
diff changeset
97 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
98 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
99 return
45683
b8ef71b01446 Patch #2232: os.tmpfile might fail on Windows if the user has no
Martin v. Löwis <martin@v.loewis.de>
parents: 43081
diff changeset
100 # As with test_tmpnam() below, the Windows implementation of tmpfile()
b8ef71b01446 Patch #2232: os.tmpfile might fail on Windows if the user has no
Martin v. Löwis <martin@v.loewis.de>
parents: 43081
diff changeset
101 # attempts to create a file in the root directory of the current drive.
b8ef71b01446 Patch #2232: os.tmpfile might fail on Windows if the user has no
Martin v. Löwis <martin@v.loewis.de>
parents: 43081
diff changeset
102 # On Vista and Server 2008, this test will always fail for normal users
b8ef71b01446 Patch #2232: os.tmpfile might fail on Windows if the user has no
Martin v. Löwis <martin@v.loewis.de>
parents: 43081
diff changeset
103 # as writing to the root directory requires elevated privileges. With
b8ef71b01446 Patch #2232: os.tmpfile might fail on Windows if the user has no
Martin v. Löwis <martin@v.loewis.de>
parents: 43081
diff changeset
104 # XP and below, the semantics of tmpfile() are the same, but the user
b8ef71b01446 Patch #2232: os.tmpfile might fail on Windows if the user has no
Martin v. Löwis <martin@v.loewis.de>
parents: 43081
diff changeset
105 # running the test is more likely to have administrative privileges on
b8ef71b01446 Patch #2232: os.tmpfile might fail on Windows if the user has no
Martin v. Löwis <martin@v.loewis.de>
parents: 43081
diff changeset
106 # their account already. If that's the case, then os.tmpfile() should
b8ef71b01446 Patch #2232: os.tmpfile might fail on Windows if the user has no
Martin v. Löwis <martin@v.loewis.de>
parents: 43081
diff changeset
107 # work. In order to make this test as useful as possible, rather than
b8ef71b01446 Patch #2232: os.tmpfile might fail on Windows if the user has no
Martin v. Löwis <martin@v.loewis.de>
parents: 43081
diff changeset
108 # trying to detect Windows versions or whether or not the user has the
b8ef71b01446 Patch #2232: os.tmpfile might fail on Windows if the user has no
Martin v. Löwis <martin@v.loewis.de>
parents: 43081
diff changeset
109 # right permissions, just try and create a file in the root directory
b8ef71b01446 Patch #2232: os.tmpfile might fail on Windows if the user has no
Martin v. Löwis <martin@v.loewis.de>
parents: 43081
diff changeset
110 # and see if it raises a 'Permission denied' OSError. If it does, then
b8ef71b01446 Patch #2232: os.tmpfile might fail on Windows if the user has no
Martin v. Löwis <martin@v.loewis.de>
parents: 43081
diff changeset
111 # test that a subsequent call to os.tmpfile() raises the same error. If
b8ef71b01446 Patch #2232: os.tmpfile might fail on Windows if the user has no
Martin v. Löwis <martin@v.loewis.de>
parents: 43081
diff changeset
112 # it doesn't, assume we're on XP or below and the user running the test
b8ef71b01446 Patch #2232: os.tmpfile might fail on Windows if the user has no
Martin v. Löwis <martin@v.loewis.de>
parents: 43081
diff changeset
113 # has administrative privileges, and proceed with the test as normal.
67263
9ea12e86d565 Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
Antoine Pitrou <solipsis@pitrou.net>
parents: 66348
diff changeset
114 with warnings.catch_warnings():
9ea12e86d565 Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
Antoine Pitrou <solipsis@pitrou.net>
parents: 66348
diff changeset
115 warnings.filterwarnings("ignore", "tmpfile", DeprecationWarning)
9ea12e86d565 Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
Antoine Pitrou <solipsis@pitrou.net>
parents: 66348
diff changeset
116
9ea12e86d565 Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
Antoine Pitrou <solipsis@pitrou.net>
parents: 66348
diff changeset
117 if sys.platform == 'win32':
9ea12e86d565 Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
Antoine Pitrou <solipsis@pitrou.net>
parents: 66348
diff changeset
118 name = '\\python_test_os_test_tmpfile.txt'
9ea12e86d565 Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
Antoine Pitrou <solipsis@pitrou.net>
parents: 66348
diff changeset
119 if os.path.exists(name):
9ea12e86d565 Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
Antoine Pitrou <solipsis@pitrou.net>
parents: 66348
diff changeset
120 os.remove(name)
45683
b8ef71b01446 Patch #2232: os.tmpfile might fail on Windows if the user has no
Martin v. Löwis <martin@v.loewis.de>
parents: 43081
diff changeset
121 try:
67263
9ea12e86d565 Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
Antoine Pitrou <solipsis@pitrou.net>
parents: 66348
diff changeset
122 fp = open(name, 'w')
9ea12e86d565 Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
Antoine Pitrou <solipsis@pitrou.net>
parents: 66348
diff changeset
123 except IOError, first:
9ea12e86d565 Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
Antoine Pitrou <solipsis@pitrou.net>
parents: 66348
diff changeset
124 # open() failed, assert tmpfile() fails in the same way.
9ea12e86d565 Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
Antoine Pitrou <solipsis@pitrou.net>
parents: 66348
diff changeset
125 # Although open() raises an IOError and os.tmpfile() raises an
9ea12e86d565 Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
Antoine Pitrou <solipsis@pitrou.net>
parents: 66348
diff changeset
126 # OSError(), 'args' will be (13, 'Permission denied') in both
9ea12e86d565 Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
Antoine Pitrou <solipsis@pitrou.net>
parents: 66348
diff changeset
127 # cases.
9ea12e86d565 Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
Antoine Pitrou <solipsis@pitrou.net>
parents: 66348
diff changeset
128 try:
9ea12e86d565 Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
Antoine Pitrou <solipsis@pitrou.net>
parents: 66348
diff changeset
129 fp = os.tmpfile()
9ea12e86d565 Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
Antoine Pitrou <solipsis@pitrou.net>
parents: 66348
diff changeset
130 except OSError, second:
9ea12e86d565 Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
Antoine Pitrou <solipsis@pitrou.net>
parents: 66348
diff changeset
131 self.assertEqual(first.args, second.args)
9ea12e86d565 Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
Antoine Pitrou <solipsis@pitrou.net>
parents: 66348
diff changeset
132 else:
9ea12e86d565 Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
Antoine Pitrou <solipsis@pitrou.net>
parents: 66348
diff changeset
133 self.fail("expected os.tmpfile() to raise OSError")
9ea12e86d565 Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
Antoine Pitrou <solipsis@pitrou.net>
parents: 66348
diff changeset
134 return
45683
b8ef71b01446 Patch #2232: os.tmpfile might fail on Windows if the user has no
Martin v. Löwis <martin@v.loewis.de>
parents: 43081
diff changeset
135 else:
67263
9ea12e86d565 Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
Antoine Pitrou <solipsis@pitrou.net>
parents: 66348
diff changeset
136 # open() worked, therefore, tmpfile() should work. Close our
9ea12e86d565 Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
Antoine Pitrou <solipsis@pitrou.net>
parents: 66348
diff changeset
137 # dummy file and proceed with the test as normal.
9ea12e86d565 Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
Antoine Pitrou <solipsis@pitrou.net>
parents: 66348
diff changeset
138 fp.close()
9ea12e86d565 Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
Antoine Pitrou <solipsis@pitrou.net>
parents: 66348
diff changeset
139 os.remove(name)
45683
b8ef71b01446 Patch #2232: os.tmpfile might fail on Windows if the user has no
Martin v. Löwis <martin@v.loewis.de>
parents: 43081
diff changeset
140
67263
9ea12e86d565 Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
Antoine Pitrou <solipsis@pitrou.net>
parents: 66348
diff changeset
141 fp = os.tmpfile()
9ea12e86d565 Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
Antoine Pitrou <solipsis@pitrou.net>
parents: 66348
diff changeset
142 fp.write("foobar")
9ea12e86d565 Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
Antoine Pitrou <solipsis@pitrou.net>
parents: 66348
diff changeset
143 fp.seek(0,0)
9ea12e86d565 Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
Antoine Pitrou <solipsis@pitrou.net>
parents: 66348
diff changeset
144 s = fp.read()
9ea12e86d565 Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
Antoine Pitrou <solipsis@pitrou.net>
parents: 66348
diff changeset
145 fp.close()
9ea12e86d565 Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
Antoine Pitrou <solipsis@pitrou.net>
parents: 66348
diff changeset
146 self.assertTrue(s == "foobar")
18678
a7bc339ee2f3 Start of a test suite for the "os" module -- this only tests the temporary
Fred Drake <fdrake@acm.org>
parents:
diff changeset
147
a7bc339ee2f3 Start of a test suite for the "os" module -- this only tests the temporary
Fred Drake <fdrake@acm.org>
parents:
diff changeset
148 def test_tmpnam(self):
a7bc339ee2f3 Start of a test suite for the "os" module -- this only tests the temporary
Fred Drake <fdrake@acm.org>
parents:
diff changeset
149 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
150 return
67263
9ea12e86d565 Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
Antoine Pitrou <solipsis@pitrou.net>
parents: 66348
diff changeset
151 with warnings.catch_warnings():
9ea12e86d565 Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
Antoine Pitrou <solipsis@pitrou.net>
parents: 66348
diff changeset
152 warnings.filterwarnings("ignore", "tmpnam", RuntimeWarning,
9ea12e86d565 Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
Antoine Pitrou <solipsis@pitrou.net>
parents: 66348
diff changeset
153 r"test_os$")
9ea12e86d565 Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
Antoine Pitrou <solipsis@pitrou.net>
parents: 66348
diff changeset
154 warnings.filterwarnings("ignore", "tmpnam", DeprecationWarning)
9ea12e86d565 Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
Antoine Pitrou <solipsis@pitrou.net>
parents: 66348
diff changeset
155
9ea12e86d565 Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
Antoine Pitrou <solipsis@pitrou.net>
parents: 66348
diff changeset
156 name = os.tmpnam()
9ea12e86d565 Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
Antoine Pitrou <solipsis@pitrou.net>
parents: 66348
diff changeset
157 if sys.platform in ("win32",):
9ea12e86d565 Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
Antoine Pitrou <solipsis@pitrou.net>
parents: 66348
diff changeset
158 # The Windows tmpnam() seems useless. From the MS docs:
9ea12e86d565 Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
Antoine Pitrou <solipsis@pitrou.net>
parents: 66348
diff changeset
159 #
9ea12e86d565 Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
Antoine Pitrou <solipsis@pitrou.net>
parents: 66348
diff changeset
160 # The character string that tmpnam creates consists of
9ea12e86d565 Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
Antoine Pitrou <solipsis@pitrou.net>
parents: 66348
diff changeset
161 # the path prefix, defined by the entry P_tmpdir in the
9ea12e86d565 Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
Antoine Pitrou <solipsis@pitrou.net>
parents: 66348
diff changeset
162 # file STDIO.H, followed by a sequence consisting of the
9ea12e86d565 Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
Antoine Pitrou <solipsis@pitrou.net>
parents: 66348
diff changeset
163 # digit characters '0' through '9'; the numerical value
9ea12e86d565 Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
Antoine Pitrou <solipsis@pitrou.net>
parents: 66348
diff changeset
164 # of this string is in the range 1 - 65,535. Changing the
9ea12e86d565 Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
Antoine Pitrou <solipsis@pitrou.net>
parents: 66348
diff changeset
165 # definitions of L_tmpnam or P_tmpdir in STDIO.H does not
9ea12e86d565 Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
Antoine Pitrou <solipsis@pitrou.net>
parents: 66348
diff changeset
166 # change the operation of tmpnam.
9ea12e86d565 Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
Antoine Pitrou <solipsis@pitrou.net>
parents: 66348
diff changeset
167 #
9ea12e86d565 Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
Antoine Pitrou <solipsis@pitrou.net>
parents: 66348
diff changeset
168 # The really bizarre part is that, at least under MSVC6,
9ea12e86d565 Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
Antoine Pitrou <solipsis@pitrou.net>
parents: 66348
diff changeset
169 # P_tmpdir is "\\". That is, the path returned refers to
9ea12e86d565 Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
Antoine Pitrou <solipsis@pitrou.net>
parents: 66348
diff changeset
170 # the root of the current drive. That's a terrible place to
9ea12e86d565 Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
Antoine Pitrou <solipsis@pitrou.net>
parents: 66348
diff changeset
171 # put temp files, and, depending on privileges, the user
9ea12e86d565 Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
Antoine Pitrou <solipsis@pitrou.net>
parents: 66348
diff changeset
172 # may not even be able to open a file in the root directory.
9ea12e86d565 Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
Antoine Pitrou <solipsis@pitrou.net>
parents: 66348
diff changeset
173 self.assertFalse(os.path.exists(name),
9ea12e86d565 Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
Antoine Pitrou <solipsis@pitrou.net>
parents: 66348
diff changeset
174 "file already exists for temporary file")
9ea12e86d565 Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
Antoine Pitrou <solipsis@pitrou.net>
parents: 66348
diff changeset
175 else:
9ea12e86d565 Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
Antoine Pitrou <solipsis@pitrou.net>
parents: 66348
diff changeset
176 self.check_tempfile(name)
18787
f999d3609f74 Whitespace normalization, plus:
Tim Peters <tim.peters@gmail.com>
parents: 18682
diff changeset
177
20539
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20013
diff changeset
178 # 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
179 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
180 def setUp(self):
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28869
diff changeset
181 os.mkdir(test_support.TESTFN)
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28869
diff changeset
182 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
183 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
184 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
185 f.close()
20547
a12ccdbce8da Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 20545
diff changeset
186
20539
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20013
diff changeset
187 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
188 os.unlink(self.fname)
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28869
diff changeset
189 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
190
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20013
diff changeset
191 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
192 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
193 return
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20013
diff changeset
194
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20013
diff changeset
195 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
196 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
197
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20013
diff changeset
198 # Make sure direct access works
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65970
diff changeset
199 self.assertEqual(result[stat.ST_SIZE], 3)
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65970
diff changeset
200 self.assertEqual(result.st_size, 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
201
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20013
diff changeset
202 # 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
203 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
204 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
205 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
206 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
207 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
208 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
209 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
210 def trunc(x): return x
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65970
diff changeset
211 self.assertEqual(trunc(getattr(result, attr)),
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65970
diff changeset
212 result[getattr(stat, name)])
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58070
diff changeset
213 self.assertIn(attr, members)
20539
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20013
diff changeset
214
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20013
diff changeset
215 try:
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20013
diff changeset
216 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
217 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
218 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
219 pass
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20013
diff changeset
220
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20013
diff changeset
221 # 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
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.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
224 self.fail("No exception thrown")
62663
b04616be7f14 an AttributeError is perfectly acceptable here
Benjamin Peterson <benjamin@python.org>
parents: 62657
diff changeset
225 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
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 try:
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20013
diff changeset
229 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
230 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
231 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
232 pass
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20013
diff changeset
233
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20013
diff changeset
234 try:
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20013
diff changeset
235 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
236 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
237 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
238 pass
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20013
diff changeset
239
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20013
diff changeset
240 # 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
241 try:
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20013
diff changeset
242 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
243 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
244 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
245 pass
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20013
diff changeset
246
68503
ee5350f1d0c2 #11515: fix several typos. Patch by Piotr Kasprzyk.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68256
diff changeset
247 # Use the constructor with a too-long tuple.
20539
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20013
diff changeset
248 try:
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20013
diff changeset
249 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
250 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
251 pass
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20013
diff changeset
252
20547
a12ccdbce8da Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 20545
diff changeset
253
20539
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20013
diff changeset
254 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
255 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
256 return
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20013
diff changeset
257
23777
bec1b942e0bc Patch #488073: AtheOS port.
Martin v. Löwis <martin@v.loewis.de>
parents: 23073
diff changeset
258 try:
bec1b942e0bc Patch #488073: AtheOS port.
Martin v. Löwis <martin@v.loewis.de>
parents: 23073
diff changeset
259 result = os.statvfs(self.fname)
bec1b942e0bc Patch #488073: AtheOS port.
Martin v. Löwis <martin@v.loewis.de>
parents: 23073
diff changeset
260 except OSError, e:
bec1b942e0bc Patch #488073: AtheOS port.
Martin v. Löwis <martin@v.loewis.de>
parents: 23073
diff changeset
261 # On AtheOS, glibc always returns ENOSYS
bec1b942e0bc Patch #488073: AtheOS port.
Martin v. Löwis <martin@v.loewis.de>
parents: 23073
diff changeset
262 if e.errno == errno.ENOSYS:
bec1b942e0bc Patch #488073: AtheOS port.
Martin v. Löwis <martin@v.loewis.de>
parents: 23073
diff changeset
263 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
264
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20013
diff changeset
265 # Make sure direct access works
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65970
diff changeset
266 self.assertEqual(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
267
47071
8d8e06f4be1f Remove the last usage of statvfs in the stdlib.
Brett Cannon <bcannon@gmail.com>
parents: 45681
diff changeset
268 # 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
269 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
270 'ffree', 'favail', 'flag', 'namemax')
8d8e06f4be1f Remove the last usage of statvfs in the stdlib.
Brett Cannon <bcannon@gmail.com>
parents: 45681
diff changeset
271 for value, member in enumerate(members):
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65970
diff changeset
272 self.assertEqual(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
273
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20013
diff changeset
274 # 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
275 try:
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20013
diff changeset
276 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
277 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
278 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
279 pass
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20013
diff changeset
280
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20013
diff changeset
281 try:
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20013
diff changeset
282 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
283 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
284 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
285 pass
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20013
diff changeset
286
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20013
diff changeset
287 # 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
288 try:
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20013
diff changeset
289 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
290 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
291 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
292 pass
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20013
diff changeset
293
68503
ee5350f1d0c2 #11515: fix several typos. Patch by Piotr Kasprzyk.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68256
diff changeset
294 # Use the constructor with a too-long tuple.
20539
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20013
diff changeset
295 try:
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20013
diff changeset
296 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
297 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
298 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
299
40119
b94bcb7aed3d Patch #1576166: Support os.utime for directories on Windows NT+.
Martin v. Löwis <martin@v.loewis.de>
parents: 40076
diff changeset
300 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
301 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
302 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
303 # 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
304 # 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
305 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
306 st2 = os.stat(test_support.TESTFN)
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65970
diff changeset
307 self.assertEqual(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
308
40076
28db61101463 Bug #1565150: Fix subsecond processing for os.utime on Windows.
Martin v. Löwis <martin@v.loewis.de>
parents: 38984
diff changeset
309 # 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
310 # 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
311 if sys.platform == 'win32':
43081
ae90420d2635 Bug #1709599: Run test_1565150 only if the file system is NTFS.
Martin v. Löwis <martin@v.loewis.de>
parents: 41382
diff changeset
312 def get_file_system(path):
48319
a36b6c1cec56 Backport r65900: 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: 45683
diff changeset
313 root = os.path.splitdrive(os.path.abspath(path))[0] + '\\'
43081
ae90420d2635 Bug #1709599: Run test_1565150 only if the file system is NTFS.
Martin v. Löwis <martin@v.loewis.de>
parents: 41382
diff changeset
314 import ctypes
48802
8133fbb74587 Reverted r65902. See http://mail.python.org/pipermail/python-checkins/2008-August/073116.html
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 48319
diff changeset
315 kernel32 = ctypes.windll.kernel32
8133fbb74587 Reverted r65902. See http://mail.python.org/pipermail/python-checkins/2008-August/073116.html
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 48319
diff changeset
316 buf = ctypes.create_string_buffer("", 100)
8133fbb74587 Reverted r65902. See http://mail.python.org/pipermail/python-checkins/2008-August/073116.html
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 48319
diff changeset
317 if kernel32.GetVolumeInformationA(root, None, 0, None, None, None, buf, len(buf)):
43081
ae90420d2635 Bug #1709599: Run test_1565150 only if the file system is NTFS.
Martin v. Löwis <martin@v.loewis.de>
parents: 41382
diff changeset
318 return buf.value
ae90420d2635 Bug #1709599: Run test_1565150 only if the file system is NTFS.
Martin v. Löwis <martin@v.loewis.de>
parents: 41382
diff changeset
319
ae90420d2635 Bug #1709599: Run test_1565150 only if the file system is NTFS.
Martin v. Löwis <martin@v.loewis.de>
parents: 41382
diff changeset
320 if get_file_system(test_support.TESTFN) == "NTFS":
ae90420d2635 Bug #1709599: Run test_1565150 only if the file system is NTFS.
Martin v. Löwis <martin@v.loewis.de>
parents: 41382
diff changeset
321 def test_1565150(self):
ae90420d2635 Bug #1709599: Run test_1565150 only if the file system is NTFS.
Martin v. Löwis <martin@v.loewis.de>
parents: 41382
diff changeset
322 t1 = 1159195039.25
ae90420d2635 Bug #1709599: Run test_1565150 only if the file system is NTFS.
Martin v. Löwis <martin@v.loewis.de>
parents: 41382
diff changeset
323 os.utime(self.fname, (t1, t1))
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65970
diff changeset
324 self.assertEqual(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
325
67286
93b7c0008b08 Merged revisions 87666 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 67263
diff changeset
326 def test_large_time(self):
93b7c0008b08 Merged revisions 87666 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 67263
diff changeset
327 t1 = 5000000000 # some day in 2128
93b7c0008b08 Merged revisions 87666 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 67263
diff changeset
328 os.utime(self.fname, (t1, t1))
93b7c0008b08 Merged revisions 87666 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 67263
diff changeset
329 self.assertEqual(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
330
41382
b001b65478fa Bug #1686475: Support stat'ing open files on Windows again.
Martin v. Löwis <martin@v.loewis.de>
parents: 40076
diff changeset
331 def test_1686475(self):
b001b65478fa Bug #1686475: Support stat'ing open files on Windows again.
Martin v. Löwis <martin@v.loewis.de>
parents: 40076
diff changeset
332 # Verify that an open file can be stat'ed
b001b65478fa Bug #1686475: Support stat'ing open files on Windows again.
Martin v. Löwis <martin@v.loewis.de>
parents: 40076
diff changeset
333 try:
b001b65478fa Bug #1686475: Support stat'ing open files on Windows again.
Martin v. Löwis <martin@v.loewis.de>
parents: 40076
diff changeset
334 os.stat(r"c:\pagefile.sys")
b001b65478fa Bug #1686475: Support stat'ing open files on Windows again.
Martin v. Löwis <martin@v.loewis.de>
parents: 40076
diff changeset
335 except WindowsError, e:
48270
2bebc0a6d042 #3580: fix a failure in test_os
Antoine Pitrou <solipsis@pitrou.net>
parents: 48267
diff changeset
336 if e.errno == 2: # file does not exist; cannot run test
41382
b001b65478fa Bug #1686475: Support stat'ing open files on Windows again.
Martin v. Löwis <martin@v.loewis.de>
parents: 40076
diff changeset
337 return
b001b65478fa Bug #1686475: Support stat'ing open files on Windows again.
Martin v. Löwis <martin@v.loewis.de>
parents: 40076
diff changeset
338 self.fail("Could not stat pagefile.sys")
b001b65478fa Bug #1686475: Support stat'ing open files on Windows again.
Martin v. Löwis <martin@v.loewis.de>
parents: 40076
diff changeset
339
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
340 from test import mapping_tests
28240
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24956
diff changeset
341
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
342 class EnvironTests(mapping_tests.BasicTestMappingProtocol):
28240
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24956
diff changeset
343 """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
344 type2test = None
28240
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24956
diff changeset
345 def _reference(self):
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24956
diff changeset
346 return {"KEY1":"VALUE1", "KEY2":"VALUE2", "KEY3":"VALUE3"}
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24956
diff changeset
347 def _empty_mapping(self):
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24956
diff changeset
348 os.environ.clear()
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24956
diff changeset
349 return os.environ
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24956
diff changeset
350 def setUp(self):
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24956
diff changeset
351 self.__save = dict(os.environ)
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24956
diff changeset
352 os.environ.clear()
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24956
diff changeset
353 def tearDown(self):
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24956
diff changeset
354 os.environ.clear()
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24956
diff changeset
355 os.environ.update(self.__save)
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24956
diff changeset
356
34516
658512774468 Revert os.py 1.75, and directly implement update.
Martin v. Löwis <martin@v.loewis.de>
parents: 34480
diff changeset
357 # Bug 1110478
34607
4b1207cb01ea Avoid using items() in environ.update(). Fixes #1124513.
Martin v. Löwis <martin@v.loewis.de>
parents: 34516
diff changeset
358 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
359 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
360 os.environ.update(HELLO="World")
65771
e203ed071fca Merged revisions 85987 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65176
diff changeset
361 with os.popen("/bin/sh -c 'echo $HELLO'") as popen:
e203ed071fca Merged revisions 85987 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65176
diff changeset
362 value = popen.read().strip()
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65970
diff changeset
363 self.assertEqual(value, "World")
34516
658512774468 Revert os.py 1.75, and directly implement update.
Martin v. Löwis <martin@v.loewis.de>
parents: 34480
diff changeset
364
73771
99f5a0475ead Issue #13415: Skip test_os.test_unset_error on FreeBSD and OS X.
Charles-François Natali <neologix@free.fr>
parents: 73709
diff changeset
365 # On FreeBSD < 7 and OS X < 10.6, unsetenv() doesn't return a value (issue
99f5a0475ead Issue #13415: Skip test_os.test_unset_error on FreeBSD and OS X.
Charles-François Natali <neologix@free.fr>
parents: 73709
diff changeset
366 # #13415).
99f5a0475ead Issue #13415: Skip test_os.test_unset_error on FreeBSD and OS X.
Charles-François Natali <neologix@free.fr>
parents: 73709
diff changeset
367 @unittest.skipIf(sys.platform.startswith(('freebsd', 'darwin')),
99f5a0475ead Issue #13415: Skip test_os.test_unset_error on FreeBSD and OS X.
Charles-François Natali <neologix@free.fr>
parents: 73709
diff changeset
368 "due to known OS bug: see issue #13415")
73704
53cf6f9f374e Issue #13415: os.unsetenv() doesn't ignore errors anymore.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68503
diff changeset
369 def test_unset_error(self):
53cf6f9f374e Issue #13415: os.unsetenv() doesn't ignore errors anymore.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68503
diff changeset
370 if sys.platform == "win32":
53cf6f9f374e Issue #13415: os.unsetenv() doesn't ignore errors anymore.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68503
diff changeset
371 # an environment variable is limited to 32,767 characters
53cf6f9f374e Issue #13415: os.unsetenv() doesn't ignore errors anymore.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68503
diff changeset
372 key = 'x' * 50000
73709
e66ef9fa55de Issue #13436: Fix unsetenv() test on Windows
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73704
diff changeset
373 self.assertRaises(ValueError, os.environ.__delitem__, key)
73704
53cf6f9f374e Issue #13415: os.unsetenv() doesn't ignore errors anymore.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68503
diff changeset
374 else:
53cf6f9f374e Issue #13415: os.unsetenv() doesn't ignore errors anymore.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68503
diff changeset
375 # "=" is not allowed in a variable name
53cf6f9f374e Issue #13415: os.unsetenv() doesn't ignore errors anymore.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68503
diff changeset
376 key = 'key='
73709
e66ef9fa55de Issue #13436: Fix unsetenv() test on Windows
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73704
diff changeset
377 self.assertRaises(OSError, os.environ.__delitem__, key)
34516
658512774468 Revert os.py 1.75, and directly implement update.
Martin v. Löwis <martin@v.loewis.de>
parents: 34480
diff changeset
378
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
379 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
380 """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
381
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 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
383 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
384 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
385
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
386 # Build:
41532
4b9fa8092e82 Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41385
diff changeset
387 # TESTFN/
41258
1dc4c4ce2db9 New test for rev. 54407 which only uses directories under TESTFN.
Georg Brandl <georg@python.org>
parents: 41242
diff changeset
388 # 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
389 # 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
390 # 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
391 # tmp2
1dc4c4ce2db9 New test for rev. 54407 which only uses directories under TESTFN.
Georg Brandl <georg@python.org>
parents: 41242
diff changeset
392 # SUB11/ no kids
1dc4c4ce2db9 New test for rev. 54407 which only uses directories under TESTFN.
Georg Brandl <georg@python.org>
parents: 41242
diff changeset
393 # 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
394 # tmp3
1dc4c4ce2db9 New test for rev. 54407 which only uses directories under TESTFN.
Georg Brandl <georg@python.org>
parents: 41242
diff changeset
395 # 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
396 # TEST2/
1dc4c4ce2db9 New test for rev. 54407 which only uses directories under TESTFN.
Georg Brandl <georg@python.org>
parents: 41242
diff changeset
397 # 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
398 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
399 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
400 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
401 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
402 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
403 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
404 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
405 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
406 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
407 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
408
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 # 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
410 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
411 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
412 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
413 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
414 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
415 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
416 f.close()
41258
1dc4c4ce2db9 New test for rev. 54407 which only uses directories under TESTFN.
Georg Brandl <georg@python.org>
parents: 41242
diff changeset
417 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
418 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
419 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
420 else:
41385
512f56afa4a0 Fix WalkTests.test_traversal() on Windows. The cleanup in
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41381
diff changeset
421 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
422
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 # 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
424 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
425 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
426 # 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
427 # 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
428 # 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
429 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
430 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
431 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
432 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
433 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
434 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
435
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
436 # 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
437 all = []
41258
1dc4c4ce2db9 New test for rev. 54407 which only uses directories under TESTFN.
Georg Brandl <georg@python.org>
parents: 41242
diff changeset
438 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
439 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
440 # 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
441 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
442 # 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
443 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
444 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
445 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
446 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
447
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
448 # 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
449 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
450 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
451 # 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
452 # 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
453 # 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
454 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
455 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
456 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
457 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
458 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
459 self.assertEqual(all[2 - 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
460
41385
512f56afa4a0 Fix WalkTests.test_traversal() on Windows. The cleanup in
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41381
diff changeset
461 if hasattr(os, "symlink"):
512f56afa4a0 Fix WalkTests.test_traversal() on Windows. The cleanup in
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41381
diff changeset
462 # Walk, following symlinks.
512f56afa4a0 Fix WalkTests.test_traversal() on Windows. The cleanup in
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41381
diff changeset
463 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
464 if root == link_path:
512f56afa4a0 Fix WalkTests.test_traversal() on Windows. The cleanup in
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41381
diff changeset
465 self.assertEqual(dirs, [])
512f56afa4a0 Fix WalkTests.test_traversal() on Windows. The cleanup in
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41381
diff changeset
466 self.assertEqual(files, ["tmp4"])
512f56afa4a0 Fix WalkTests.test_traversal() on Windows. The cleanup in
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41381
diff changeset
467 break
512f56afa4a0 Fix WalkTests.test_traversal() on Windows. The cleanup in
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41381
diff changeset
468 else:
512f56afa4a0 Fix WalkTests.test_traversal() on Windows. The cleanup in
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41381
diff changeset
469 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
470
41385
512f56afa4a0 Fix WalkTests.test_traversal() on Windows. The cleanup in
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41381
diff changeset
471 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
472 # 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
473 # 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
474 # (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
475 # 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
476 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
477 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
478 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
479 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
480 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
481 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
482 os.rmdir(dirname)
1dc4c4ce2db9 New test for rev. 54407 which only uses directories under TESTFN.
Georg Brandl <georg@python.org>
parents: 41242
diff changeset
483 else:
1dc4c4ce2db9 New test for rev. 54407 which only uses directories under TESTFN.
Georg Brandl <georg@python.org>
parents: 41242
diff changeset
484 os.remove(dirname)
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28869
diff changeset
485 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
486
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
487 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
488 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
489 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
490
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
491 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
492 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
493 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
494 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
495 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
496 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
497
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
498 # Try paths with a '.' in them
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54117
diff changeset
499 self.assertRaises(OSError, os.makedirs, os.curdir)
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
500 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
501 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
502 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
503 '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
504 os.makedirs(path)
31389
aca1ce1b4b61 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 31254
diff changeset
505
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
506
31389
aca1ce1b4b61 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 31254
diff changeset
507
aca1ce1b4b61 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 31254
diff changeset
508
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
509 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
510 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
511 '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
512 # 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
513 # 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
514 # 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
515 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
516 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
517
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
518 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
519
32302
e196b823e6c5 Feature request #935915: Add os.path.devnull.
Martin v. Löwis <martin@v.loewis.de>
parents: 32204
diff changeset
520 class DevNullTests (unittest.TestCase):
e196b823e6c5 Feature request #935915: Add os.path.devnull.
Martin v. Löwis <martin@v.loewis.de>
parents: 32204
diff changeset
521 def test_devnull(self):
e196b823e6c5 Feature request #935915: Add os.path.devnull.
Martin v. Löwis <martin@v.loewis.de>
parents: 32204
diff changeset
522 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
523 f.write('hello')
e196b823e6c5 Feature request #935915: Add os.path.devnull.
Martin v. Löwis <martin@v.loewis.de>
parents: 32204
diff changeset
524 f.close()
e196b823e6c5 Feature request #935915: Add os.path.devnull.
Martin v. Löwis <martin@v.loewis.de>
parents: 32204
diff changeset
525 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
526 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
527 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
528
33317
cf0ea19869af Patch #934711: Expose platform-specific entropy.
Martin v. Löwis <martin@v.loewis.de>
parents: 32307
diff changeset
529 class URandomTests (unittest.TestCase):
75100
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
530
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
531 def test_urandom_length(self):
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
532 self.assertEqual(len(os.urandom(0)), 0)
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
533 self.assertEqual(len(os.urandom(1)), 1)
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
534 self.assertEqual(len(os.urandom(10)), 10)
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
535 self.assertEqual(len(os.urandom(100)), 100)
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
536 self.assertEqual(len(os.urandom(1000)), 1000)
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
537
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
538 def test_urandom_value(self):
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
539 data1 = os.urandom(16)
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
540 data2 = os.urandom(16)
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
541 self.assertNotEqual(data1, data2)
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
542
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
543 def get_urandom_subprocess(self, count):
75188
0f095a0f124c Back port from 2.7:
Barry Warsaw <barry@python.org>
parents: 75183
diff changeset
544 # We need to use repr() and eval() to avoid line ending conversions
0f095a0f124c Back port from 2.7:
Barry Warsaw <barry@python.org>
parents: 75183
diff changeset
545 # under Windows.
75100
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
546 code = '\n'.join((
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
547 'import os, sys',
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
548 'data = os.urandom(%s)' % count,
75188
0f095a0f124c Back port from 2.7:
Barry Warsaw <barry@python.org>
parents: 75183
diff changeset
549 'sys.stdout.write(repr(data))',
75183
bff3fd529e33 Backport from 2.7:
Barry Warsaw <barry@python.org>
parents: 75100
diff changeset
550 'sys.stdout.flush()',
bff3fd529e33 Backport from 2.7:
Barry Warsaw <barry@python.org>
parents: 75100
diff changeset
551 'print >> sys.stderr, (len(data), data)'))
75100
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
552 cmd_line = [sys.executable, '-c', code]
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
553 p = subprocess.Popen(cmd_line, stdin=subprocess.PIPE,
75183
bff3fd529e33 Backport from 2.7:
Barry Warsaw <barry@python.org>
parents: 75100
diff changeset
554 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
75100
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
555 out, err = p.communicate()
75183
bff3fd529e33 Backport from 2.7:
Barry Warsaw <barry@python.org>
parents: 75100
diff changeset
556 self.assertEqual(p.wait(), 0, (p.wait(), err))
75188
0f095a0f124c Back port from 2.7:
Barry Warsaw <barry@python.org>
parents: 75183
diff changeset
557 out = eval(out)
0f095a0f124c Back port from 2.7:
Barry Warsaw <barry@python.org>
parents: 75183
diff changeset
558 self.assertEqual(len(out), count, err)
75100
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
559 return out
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
560
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
561 def test_urandom_subprocess(self):
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
562 data1 = self.get_urandom_subprocess(16)
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
563 data2 = self.get_urandom_subprocess(16)
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
564 self.assertNotEqual(data1, data2)
33317
cf0ea19869af Patch #934711: Expose platform-specific entropy.
Martin v. Löwis <martin@v.loewis.de>
parents: 32307
diff changeset
565
59722
4b356c1b25f6 - Issue #1039, #8154: Fix os.execlp() crash with missing 2nd argument.
Matthias Klose <doko@ubuntu.com>
parents: 59392
diff changeset
566 def test_execvpe_with_bad_arglist(self):
4b356c1b25f6 - Issue #1039, #8154: Fix os.execlp() crash with missing 2nd argument.
Matthias Klose <doko@ubuntu.com>
parents: 59392
diff changeset
567 self.assertRaises(ValueError, os.execvpe, 'notepad', [], None)
33317
cf0ea19869af Patch #934711: Expose platform-specific entropy.
Martin v. Löwis <martin@v.loewis.de>
parents: 32307
diff changeset
568
38112
be49645e3984 Implement os.{chdir,rename,rmdir,remove} using Win32 directly.
Martin v. Löwis <martin@v.loewis.de>
parents: 34607
diff changeset
569 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
570 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
571 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
572
be49645e3984 Implement os.{chdir,rename,rmdir,remove} using Win32 directly.
Martin v. Löwis <martin@v.loewis.de>
parents: 34607
diff changeset
573 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
574 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
575
be49645e3984 Implement os.{chdir,rename,rmdir,remove} using Win32 directly.
Martin v. Löwis <martin@v.loewis.de>
parents: 34607
diff changeset
576 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
577 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
578
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
579 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
580 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
581 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
582 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
583 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
584 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
585 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
586
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
587 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
588 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
589
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
590 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
591 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
592
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
593 class TestInvalidFD(unittest.TestCase):
50897
18441b8f7000 Merged revisions 68610,68621-68622,68649 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50788
diff changeset
594 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
595 "fstatvfs", "fsync", "tcgetpgrp", "ttyname"]
50897
18441b8f7000 Merged revisions 68610,68621-68622,68649 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50788
diff changeset
596 #singles.append("close")
18441b8f7000 Merged revisions 68610,68621-68622,68649 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50788
diff changeset
597 #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
598 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
599 def helper(self):
50914
caa79d238917 Merged revisions 68779 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50897
diff changeset
600 if hasattr(os, f):
caa79d238917 Merged revisions 68779 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50897
diff changeset
601 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
602 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
603 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
604 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
605
50914
caa79d238917 Merged revisions 68779 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50897
diff changeset
606 def check(self, f, *args):
51198
4c9ea647eaf4 check the errno in bad fd cases
Benjamin Peterson <benjamin@python.org>
parents: 50916
diff changeset
607 try:
4c9ea647eaf4 check the errno in bad fd cases
Benjamin Peterson <benjamin@python.org>
parents: 50916
diff changeset
608 f(test_support.make_bad_fd(), *args)
4c9ea647eaf4 check the errno in bad fd cases
Benjamin Peterson <benjamin@python.org>
parents: 50916
diff changeset
609 except OSError as e:
4c9ea647eaf4 check the errno in bad fd cases
Benjamin Peterson <benjamin@python.org>
parents: 50916
diff changeset
610 self.assertEqual(e.errno, errno.EBADF)
4c9ea647eaf4 check the errno in bad fd cases
Benjamin Peterson <benjamin@python.org>
parents: 50916
diff changeset
611 else:
4c9ea647eaf4 check the errno in bad fd cases
Benjamin Peterson <benjamin@python.org>
parents: 50916
diff changeset
612 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
613 % f)
50914
caa79d238917 Merged revisions 68779 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50897
diff changeset
614
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
615 def test_isatty(self):
50897
18441b8f7000 Merged revisions 68610,68621-68622,68649 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50788
diff changeset
616 if hasattr(os, "isatty"):
50914
caa79d238917 Merged revisions 68779 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50897
diff changeset
617 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
618
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
619 def test_closerange(self):
50897
18441b8f7000 Merged revisions 68610,68621-68622,68649 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50788
diff changeset
620 if hasattr(os, "closerange"):
50914
caa79d238917 Merged revisions 68779 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50897
diff changeset
621 fd = test_support.make_bad_fd()
55416
a0eacb5f9d8a Merged revisions 74175 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 52930
diff changeset
622 # Make sure none of the descriptors we are about to close are
a0eacb5f9d8a Merged revisions 74175 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 52930
diff changeset
623 # currently valid (issue 6542).
a0eacb5f9d8a Merged revisions 74175 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 52930
diff changeset
624 for i in range(10):
a0eacb5f9d8a Merged revisions 74175 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 52930
diff changeset
625 try: os.fstat(fd+i)
a0eacb5f9d8a Merged revisions 74175 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 52930
diff changeset
626 except OSError:
a0eacb5f9d8a Merged revisions 74175 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 52930
diff changeset
627 pass
a0eacb5f9d8a Merged revisions 74175 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 52930
diff changeset
628 else:
a0eacb5f9d8a Merged revisions 74175 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 52930
diff changeset
629 break
a0eacb5f9d8a Merged revisions 74175 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 52930
diff changeset
630 if i < 2:
55415
94fc433c4373 Backport of fix for issue 6542: make sure
R. David Murray <rdmurray@bitdance.com>
parents: 55002
diff changeset
631 raise unittest.SkipTest(
94fc433c4373 Backport of fix for issue 6542: make sure
R. David Murray <rdmurray@bitdance.com>
parents: 55002
diff changeset
632 "Unable to acquire a range of invalid file descriptors")
55416
a0eacb5f9d8a Merged revisions 74175 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 52930
diff changeset
633 self.assertEqual(os.closerange(fd, fd + i-1), 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
634
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
635 def test_dup2(self):
50897
18441b8f7000 Merged revisions 68610,68621-68622,68649 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50788
diff changeset
636 if hasattr(os, "dup2"):
50914
caa79d238917 Merged revisions 68779 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50897
diff changeset
637 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
638
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
639 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
640 if hasattr(os, "fchmod"):
50914
caa79d238917 Merged revisions 68779 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50897
diff changeset
641 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
642
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
643 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
644 if hasattr(os, "fchown"):
50914
caa79d238917 Merged revisions 68779 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50897
diff changeset
645 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
646
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
647 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
648 if hasattr(os, "fpathconf"):
50914
caa79d238917 Merged revisions 68779 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50897
diff changeset
649 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
650
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
651 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
652 if hasattr(os, "ftruncate"):
50910
fa12c8e5ae8a make bad file descriptor tests more robust
Benjamin Peterson <benjamin@python.org>
parents: 50895
diff changeset
653 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
654
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
655 def test_lseek(self):
50897
18441b8f7000 Merged revisions 68610,68621-68622,68649 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50788
diff changeset
656 if hasattr(os, "lseek"):
50914
caa79d238917 Merged revisions 68779 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50897
diff changeset
657 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
658
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
659 def test_read(self):
50897
18441b8f7000 Merged revisions 68610,68621-68622,68649 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50788
diff changeset
660 if hasattr(os, "read"):
50914
caa79d238917 Merged revisions 68779 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50897
diff changeset
661 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
662
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
663 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
664 if hasattr(os, "tcsetpgrp"):
50914
caa79d238917 Merged revisions 68779 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50897
diff changeset
665 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
666
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
667 def test_write(self):
50897
18441b8f7000 Merged revisions 68610,68621-68622,68649 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50788
diff changeset
668 if hasattr(os, "write"):
50914
caa79d238917 Merged revisions 68779 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50897
diff changeset
669 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
670
38112
be49645e3984 Implement os.{chdir,rename,rmdir,remove} using Win32 directly.
Martin v. Löwis <martin@v.loewis.de>
parents: 34607
diff changeset
671 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
672 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
673 pass
be49645e3984 Implement os.{chdir,rename,rmdir,remove} using Win32 directly.
Martin v. Löwis <martin@v.loewis.de>
parents: 34607
diff changeset
674
52930
986201aca68b Merged revisions 71299 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 50914
diff changeset
675 class PosixUidGidTests(unittest.TestCase):
986201aca68b Merged revisions 71299 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 50914
diff changeset
676 if hasattr(os, 'setuid'):
986201aca68b Merged revisions 71299 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 50914
diff changeset
677 def test_setuid(self):
986201aca68b Merged revisions 71299 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 50914
diff changeset
678 if os.getuid() != 0:
986201aca68b Merged revisions 71299 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 50914
diff changeset
679 self.assertRaises(os.error, os.setuid, 0)
986201aca68b Merged revisions 71299 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 50914
diff changeset
680 self.assertRaises(OverflowError, os.setuid, 1<<32)
986201aca68b Merged revisions 71299 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 50914
diff changeset
681
986201aca68b Merged revisions 71299 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 50914
diff changeset
682 if hasattr(os, 'setgid'):
986201aca68b Merged revisions 71299 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 50914
diff changeset
683 def test_setgid(self):
986201aca68b Merged revisions 71299 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 50914
diff changeset
684 if os.getuid() != 0:
986201aca68b Merged revisions 71299 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 50914
diff changeset
685 self.assertRaises(os.error, os.setgid, 0)
986201aca68b Merged revisions 71299 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 50914
diff changeset
686 self.assertRaises(OverflowError, os.setgid, 1<<32)
986201aca68b Merged revisions 71299 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 50914
diff changeset
687
986201aca68b Merged revisions 71299 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 50914
diff changeset
688 if hasattr(os, 'seteuid'):
986201aca68b Merged revisions 71299 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 50914
diff changeset
689 def test_seteuid(self):
986201aca68b Merged revisions 71299 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 50914
diff changeset
690 if os.getuid() != 0:
986201aca68b Merged revisions 71299 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 50914
diff changeset
691 self.assertRaises(os.error, os.seteuid, 0)
986201aca68b Merged revisions 71299 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 50914
diff changeset
692 self.assertRaises(OverflowError, os.seteuid, 1<<32)
986201aca68b Merged revisions 71299 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 50914
diff changeset
693
986201aca68b Merged revisions 71299 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 50914
diff changeset
694 if hasattr(os, 'setegid'):
986201aca68b Merged revisions 71299 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 50914
diff changeset
695 def test_setegid(self):
986201aca68b Merged revisions 71299 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 50914
diff changeset
696 if os.getuid() != 0:
986201aca68b Merged revisions 71299 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 50914
diff changeset
697 self.assertRaises(os.error, os.setegid, 0)
986201aca68b Merged revisions 71299 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 50914
diff changeset
698 self.assertRaises(OverflowError, os.setegid, 1<<32)
986201aca68b Merged revisions 71299 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 50914
diff changeset
699
986201aca68b Merged revisions 71299 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 50914
diff changeset
700 if hasattr(os, 'setreuid'):
986201aca68b Merged revisions 71299 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 50914
diff changeset
701 def test_setreuid(self):
986201aca68b Merged revisions 71299 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 50914
diff changeset
702 if os.getuid() != 0:
986201aca68b Merged revisions 71299 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 50914
diff changeset
703 self.assertRaises(os.error, os.setreuid, 0, 0)
986201aca68b Merged revisions 71299 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 50914
diff changeset
704 self.assertRaises(OverflowError, os.setreuid, 1<<32, 0)
986201aca68b Merged revisions 71299 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 50914
diff changeset
705 self.assertRaises(OverflowError, os.setreuid, 0, 1<<32)
59428
4f02179493c2 Merged revisions 78718 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 59230
diff changeset
706
4f02179493c2 Merged revisions 78718 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 59230
diff changeset
707 def test_setreuid_neg1(self):
4f02179493c2 Merged revisions 78718 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 59230
diff changeset
708 # Needs to accept -1. We run this in a subprocess to avoid
4f02179493c2 Merged revisions 78718 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 59230
diff changeset
709 # altering the test runner's process state (issue8045).
4f02179493c2 Merged revisions 78718 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 59230
diff changeset
710 subprocess.check_call([
4f02179493c2 Merged revisions 78718 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 59230
diff changeset
711 sys.executable, '-c',
4f02179493c2 Merged revisions 78718 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 59230
diff changeset
712 'import os,sys;os.setreuid(-1,-1);sys.exit(0)'])
52930
986201aca68b Merged revisions 71299 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 50914
diff changeset
713
986201aca68b Merged revisions 71299 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 50914
diff changeset
714 if hasattr(os, 'setregid'):
986201aca68b Merged revisions 71299 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 50914
diff changeset
715 def test_setregid(self):
986201aca68b Merged revisions 71299 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 50914
diff changeset
716 if os.getuid() != 0:
986201aca68b Merged revisions 71299 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 50914
diff changeset
717 self.assertRaises(os.error, os.setregid, 0, 0)
986201aca68b Merged revisions 71299 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 50914
diff changeset
718 self.assertRaises(OverflowError, os.setregid, 1<<32, 0)
986201aca68b Merged revisions 71299 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 50914
diff changeset
719 self.assertRaises(OverflowError, os.setregid, 0, 1<<32)
59428
4f02179493c2 Merged revisions 78718 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 59230
diff changeset
720
4f02179493c2 Merged revisions 78718 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 59230
diff changeset
721 def test_setregid_neg1(self):
4f02179493c2 Merged revisions 78718 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 59230
diff changeset
722 # Needs to accept -1. We run this in a subprocess to avoid
4f02179493c2 Merged revisions 78718 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 59230
diff changeset
723 # altering the test runner's process state (issue8045).
4f02179493c2 Merged revisions 78718 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 59230
diff changeset
724 subprocess.check_call([
4f02179493c2 Merged revisions 78718 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 59230
diff changeset
725 sys.executable, '-c',
4f02179493c2 Merged revisions 78718 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 59230
diff changeset
726 'import os,sys;os.setregid(-1,-1);sys.exit(0)'])
52930
986201aca68b Merged revisions 71299 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 50914
diff changeset
727 else:
986201aca68b Merged revisions 71299 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 50914
diff changeset
728 class PosixUidGidTests(unittest.TestCase):
986201aca68b Merged revisions 71299 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 50914
diff changeset
729 pass
986201aca68b Merged revisions 71299 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 50914
diff changeset
730
60197
0ab89e8bdedc Implement #1220212. Add os.kill support for Windows.
Brian Curtin <brian.curtin@gmail.com>
parents: 59722
diff changeset
731 @unittest.skipUnless(sys.platform == "win32", "Win32 specific tests")
0ab89e8bdedc Implement #1220212. Add os.kill support for Windows.
Brian Curtin <brian.curtin@gmail.com>
parents: 59722
diff changeset
732 class Win32KillTests(unittest.TestCase):
60608
4229662ede87 Fix os.kill tests to be more robust and work with slower machines.
Brian Curtin <brian.curtin@gmail.com>
parents: 60592
diff changeset
733 def _kill(self, sig):
4229662ede87 Fix os.kill tests to be more robust and work with slower machines.
Brian Curtin <brian.curtin@gmail.com>
parents: 60592
diff changeset
734 # Start sys.executable as a subprocess and communicate from the
4229662ede87 Fix os.kill tests to be more robust and work with slower machines.
Brian Curtin <brian.curtin@gmail.com>
parents: 60592
diff changeset
735 # subprocess to the parent that the interpreter is ready. When it
4229662ede87 Fix os.kill tests to be more robust and work with slower machines.
Brian Curtin <brian.curtin@gmail.com>
parents: 60592
diff changeset
736 # becomes ready, send *sig* via os.kill to the subprocess and check
4229662ede87 Fix os.kill tests to be more robust and work with slower machines.
Brian Curtin <brian.curtin@gmail.com>
parents: 60592
diff changeset
737 # that the return code is equal to *sig*.
4229662ede87 Fix os.kill tests to be more robust and work with slower machines.
Brian Curtin <brian.curtin@gmail.com>
parents: 60592
diff changeset
738 import ctypes
4229662ede87 Fix os.kill tests to be more robust and work with slower machines.
Brian Curtin <brian.curtin@gmail.com>
parents: 60592
diff changeset
739 from ctypes import wintypes
4229662ede87 Fix os.kill tests to be more robust and work with slower machines.
Brian Curtin <brian.curtin@gmail.com>
parents: 60592
diff changeset
740 import msvcrt
4229662ede87 Fix os.kill tests to be more robust and work with slower machines.
Brian Curtin <brian.curtin@gmail.com>
parents: 60592
diff changeset
741
4229662ede87 Fix os.kill tests to be more robust and work with slower machines.
Brian Curtin <brian.curtin@gmail.com>
parents: 60592
diff changeset
742 # Since we can't access the contents of the process' stdout until the
4229662ede87 Fix os.kill tests to be more robust and work with slower machines.
Brian Curtin <brian.curtin@gmail.com>
parents: 60592
diff changeset
743 # process has exited, use PeekNamedPipe to see what's inside stdout
4229662ede87 Fix os.kill tests to be more robust and work with slower machines.
Brian Curtin <brian.curtin@gmail.com>
parents: 60592
diff changeset
744 # without waiting. This is done so we can tell that the interpreter
4229662ede87 Fix os.kill tests to be more robust and work with slower machines.
Brian Curtin <brian.curtin@gmail.com>
parents: 60592
diff changeset
745 # is started and running at a point where it could handle a signal.
4229662ede87 Fix os.kill tests to be more robust and work with slower machines.
Brian Curtin <brian.curtin@gmail.com>
parents: 60592
diff changeset
746 PeekNamedPipe = ctypes.windll.kernel32.PeekNamedPipe
4229662ede87 Fix os.kill tests to be more robust and work with slower machines.
Brian Curtin <brian.curtin@gmail.com>
parents: 60592
diff changeset
747 PeekNamedPipe.restype = wintypes.BOOL
4229662ede87 Fix os.kill tests to be more robust and work with slower machines.
Brian Curtin <brian.curtin@gmail.com>
parents: 60592
diff changeset
748 PeekNamedPipe.argtypes = (wintypes.HANDLE, # Pipe handle
4229662ede87 Fix os.kill tests to be more robust and work with slower machines.
Brian Curtin <brian.curtin@gmail.com>
parents: 60592
diff changeset
749 ctypes.POINTER(ctypes.c_char), # stdout buf
4229662ede87 Fix os.kill tests to be more robust and work with slower machines.
Brian Curtin <brian.curtin@gmail.com>
parents: 60592
diff changeset
750 wintypes.DWORD, # Buffer size
4229662ede87 Fix os.kill tests to be more robust and work with slower machines.
Brian Curtin <brian.curtin@gmail.com>
parents: 60592
diff changeset
751 ctypes.POINTER(wintypes.DWORD), # bytes read
4229662ede87 Fix os.kill tests to be more robust and work with slower machines.
Brian Curtin <brian.curtin@gmail.com>
parents: 60592
diff changeset
752 ctypes.POINTER(wintypes.DWORD), # bytes avail
4229662ede87 Fix os.kill tests to be more robust and work with slower machines.
Brian Curtin <brian.curtin@gmail.com>
parents: 60592
diff changeset
753 ctypes.POINTER(wintypes.DWORD)) # bytes left
4229662ede87 Fix os.kill tests to be more robust and work with slower machines.
Brian Curtin <brian.curtin@gmail.com>
parents: 60592
diff changeset
754 msg = "running"
4229662ede87 Fix os.kill tests to be more robust and work with slower machines.
Brian Curtin <brian.curtin@gmail.com>
parents: 60592
diff changeset
755 proc = subprocess.Popen([sys.executable, "-c",
4229662ede87 Fix os.kill tests to be more robust and work with slower machines.
Brian Curtin <brian.curtin@gmail.com>
parents: 60592
diff changeset
756 "import sys;"
4229662ede87 Fix os.kill tests to be more robust and work with slower machines.
Brian Curtin <brian.curtin@gmail.com>
parents: 60592
diff changeset
757 "sys.stdout.write('{}');"
4229662ede87 Fix os.kill tests to be more robust and work with slower machines.
Brian Curtin <brian.curtin@gmail.com>
parents: 60592
diff changeset
758 "sys.stdout.flush();"
4229662ede87 Fix os.kill tests to be more robust and work with slower machines.
Brian Curtin <brian.curtin@gmail.com>
parents: 60592
diff changeset
759 "input()".format(msg)],
4229662ede87 Fix os.kill tests to be more robust and work with slower machines.
Brian Curtin <brian.curtin@gmail.com>
parents: 60592
diff changeset
760 stdout=subprocess.PIPE,
4229662ede87 Fix os.kill tests to be more robust and work with slower machines.
Brian Curtin <brian.curtin@gmail.com>
parents: 60592
diff changeset
761 stderr=subprocess.PIPE,
4229662ede87 Fix os.kill tests to be more robust and work with slower machines.
Brian Curtin <brian.curtin@gmail.com>
parents: 60592
diff changeset
762 stdin=subprocess.PIPE)
65970
62085733f7df Merged revisions 86198 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65969
diff changeset
763 self.addCleanup(proc.stdout.close)
62085733f7df Merged revisions 86198 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65969
diff changeset
764 self.addCleanup(proc.stderr.close)
62085733f7df Merged revisions 86198 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65969
diff changeset
765 self.addCleanup(proc.stdin.close)
60608
4229662ede87 Fix os.kill tests to be more robust and work with slower machines.
Brian Curtin <brian.curtin@gmail.com>
parents: 60592
diff changeset
766
61966
b697e21d1e78 Fix #8405 for slow buildbots. Remove the sleep on startup and move the
Brian Curtin <brian.curtin@gmail.com>
parents: 60608
diff changeset
767 count, max = 0, 100
b697e21d1e78 Fix #8405 for slow buildbots. Remove the sleep on startup and move the
Brian Curtin <brian.curtin@gmail.com>
parents: 60608
diff changeset
768 while count < max and proc.poll() is None:
b697e21d1e78 Fix #8405 for slow buildbots. Remove the sleep on startup and move the
Brian Curtin <brian.curtin@gmail.com>
parents: 60608
diff changeset
769 # Create a string buffer to store the result of stdout from the pipe
b697e21d1e78 Fix #8405 for slow buildbots. Remove the sleep on startup and move the
Brian Curtin <brian.curtin@gmail.com>
parents: 60608
diff changeset
770 buf = ctypes.create_string_buffer(len(msg))
b697e21d1e78 Fix #8405 for slow buildbots. Remove the sleep on startup and move the
Brian Curtin <brian.curtin@gmail.com>
parents: 60608
diff changeset
771 # Obtain the text currently in proc.stdout
b697e21d1e78 Fix #8405 for slow buildbots. Remove the sleep on startup and move the
Brian Curtin <brian.curtin@gmail.com>
parents: 60608
diff changeset
772 # Bytes read/avail/left are left as NULL and unused
b697e21d1e78 Fix #8405 for slow buildbots. Remove the sleep on startup and move the
Brian Curtin <brian.curtin@gmail.com>
parents: 60608
diff changeset
773 rslt = PeekNamedPipe(msvcrt.get_osfhandle(proc.stdout.fileno()),
b697e21d1e78 Fix #8405 for slow buildbots. Remove the sleep on startup and move the
Brian Curtin <brian.curtin@gmail.com>
parents: 60608
diff changeset
774 buf, ctypes.sizeof(buf), None, None, None)
b697e21d1e78 Fix #8405 for slow buildbots. Remove the sleep on startup and move the
Brian Curtin <brian.curtin@gmail.com>
parents: 60608
diff changeset
775 self.assertNotEqual(rslt, 0, "PeekNamedPipe failed")
b697e21d1e78 Fix #8405 for slow buildbots. Remove the sleep on startup and move the
Brian Curtin <brian.curtin@gmail.com>
parents: 60608
diff changeset
776 if buf.value:
b697e21d1e78 Fix #8405 for slow buildbots. Remove the sleep on startup and move the
Brian Curtin <brian.curtin@gmail.com>
parents: 60608
diff changeset
777 self.assertEqual(msg, buf.value)
b697e21d1e78 Fix #8405 for slow buildbots. Remove the sleep on startup and move the
Brian Curtin <brian.curtin@gmail.com>
parents: 60608
diff changeset
778 break
b697e21d1e78 Fix #8405 for slow buildbots. Remove the sleep on startup and move the
Brian Curtin <brian.curtin@gmail.com>
parents: 60608
diff changeset
779 time.sleep(0.1)
b697e21d1e78 Fix #8405 for slow buildbots. Remove the sleep on startup and move the
Brian Curtin <brian.curtin@gmail.com>
parents: 60608
diff changeset
780 count += 1
b697e21d1e78 Fix #8405 for slow buildbots. Remove the sleep on startup and move the
Brian Curtin <brian.curtin@gmail.com>
parents: 60608
diff changeset
781 else:
b697e21d1e78 Fix #8405 for slow buildbots. Remove the sleep on startup and move the
Brian Curtin <brian.curtin@gmail.com>
parents: 60608
diff changeset
782 self.fail("Did not receive communication from the subprocess")
60608
4229662ede87 Fix os.kill tests to be more robust and work with slower machines.
Brian Curtin <brian.curtin@gmail.com>
parents: 60592
diff changeset
783
60197
0ab89e8bdedc Implement #1220212. Add os.kill support for Windows.
Brian Curtin <brian.curtin@gmail.com>
parents: 59722
diff changeset
784 os.kill(proc.pid, sig)
0ab89e8bdedc Implement #1220212. Add os.kill support for Windows.
Brian Curtin <brian.curtin@gmail.com>
parents: 59722
diff changeset
785 self.assertEqual(proc.wait(), sig)
0ab89e8bdedc Implement #1220212. Add os.kill support for Windows.
Brian Curtin <brian.curtin@gmail.com>
parents: 59722
diff changeset
786
0ab89e8bdedc Implement #1220212. Add os.kill support for Windows.
Brian Curtin <brian.curtin@gmail.com>
parents: 59722
diff changeset
787 def test_kill_sigterm(self):
0ab89e8bdedc Implement #1220212. Add os.kill support for Windows.
Brian Curtin <brian.curtin@gmail.com>
parents: 59722
diff changeset
788 # SIGTERM doesn't mean anything special, but make sure it works
60608
4229662ede87 Fix os.kill tests to be more robust and work with slower machines.
Brian Curtin <brian.curtin@gmail.com>
parents: 60592
diff changeset
789 self._kill(signal.SIGTERM)
60197
0ab89e8bdedc Implement #1220212. Add os.kill support for Windows.
Brian Curtin <brian.curtin@gmail.com>
parents: 59722
diff changeset
790
0ab89e8bdedc Implement #1220212. Add os.kill support for Windows.
Brian Curtin <brian.curtin@gmail.com>
parents: 59722
diff changeset
791 def test_kill_int(self):
0ab89e8bdedc Implement #1220212. Add os.kill support for Windows.
Brian Curtin <brian.curtin@gmail.com>
parents: 59722
diff changeset
792 # os.kill on Windows can take an int which gets set as the exit code
60608
4229662ede87 Fix os.kill tests to be more robust and work with slower machines.
Brian Curtin <brian.curtin@gmail.com>
parents: 60592
diff changeset
793 self._kill(100)
60197
0ab89e8bdedc Implement #1220212. Add os.kill support for Windows.
Brian Curtin <brian.curtin@gmail.com>
parents: 59722
diff changeset
794
0ab89e8bdedc Implement #1220212. Add os.kill support for Windows.
Brian Curtin <brian.curtin@gmail.com>
parents: 59722
diff changeset
795 def _kill_with_event(self, event, name):
65176
acdb32005c0a Merged revisions 85315 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 62663
diff changeset
796 tagname = "test_os_%s" % uuid.uuid1()
acdb32005c0a Merged revisions 85315 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 62663
diff changeset
797 m = mmap.mmap(-1, 1, tagname)
acdb32005c0a Merged revisions 85315 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 62663
diff changeset
798 m[0] = '0'
60197
0ab89e8bdedc Implement #1220212. Add os.kill support for Windows.
Brian Curtin <brian.curtin@gmail.com>
parents: 59722
diff changeset
799 # Run a script which has console control handling enabled.
0ab89e8bdedc Implement #1220212. Add os.kill support for Windows.
Brian Curtin <brian.curtin@gmail.com>
parents: 59722
diff changeset
800 proc = subprocess.Popen([sys.executable,
0ab89e8bdedc Implement #1220212. Add os.kill support for Windows.
Brian Curtin <brian.curtin@gmail.com>
parents: 59722
diff changeset
801 os.path.join(os.path.dirname(__file__),
65176
acdb32005c0a Merged revisions 85315 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 62663
diff changeset
802 "win_console_handler.py"), tagname],
60197
0ab89e8bdedc Implement #1220212. Add os.kill support for Windows.
Brian Curtin <brian.curtin@gmail.com>
parents: 59722
diff changeset
803 creationflags=subprocess.CREATE_NEW_PROCESS_GROUP)
0ab89e8bdedc Implement #1220212. Add os.kill support for Windows.
Brian Curtin <brian.curtin@gmail.com>
parents: 59722
diff changeset
804 # Let the interpreter startup before we send signals. See #3137.
65176
acdb32005c0a Merged revisions 85315 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 62663
diff changeset
805 count, max = 0, 20
acdb32005c0a Merged revisions 85315 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 62663
diff changeset
806 while count < max and proc.poll() is None:
65969
c500b4138f7b Backport r85525 from #10098.
Brian Curtin <brian.curtin@gmail.com>
parents: 65771
diff changeset
807 if m[0] == '1':
65176
acdb32005c0a Merged revisions 85315 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 62663
diff changeset
808 break
acdb32005c0a Merged revisions 85315 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 62663
diff changeset
809 time.sleep(0.5)
acdb32005c0a Merged revisions 85315 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 62663
diff changeset
810 count += 1
acdb32005c0a Merged revisions 85315 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 62663
diff changeset
811 else:
acdb32005c0a Merged revisions 85315 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 62663
diff changeset
812 self.fail("Subprocess didn't finish initialization")
60197
0ab89e8bdedc Implement #1220212. Add os.kill support for Windows.
Brian Curtin <brian.curtin@gmail.com>
parents: 59722
diff changeset
813 os.kill(proc.pid, event)
0ab89e8bdedc Implement #1220212. Add os.kill support for Windows.
Brian Curtin <brian.curtin@gmail.com>
parents: 59722
diff changeset
814 # proc.send_signal(event) could also be done here.
0ab89e8bdedc Implement #1220212. Add os.kill support for Windows.
Brian Curtin <brian.curtin@gmail.com>
parents: 59722
diff changeset
815 # Allow time for the signal to be passed and the process to exit.
60371
9fd8852ecdd4 Fix a failing test on an apparently slow Windows buildbot.
Brian Curtin <brian.curtin@gmail.com>
parents: 60197
diff changeset
816 time.sleep(0.5)
60197
0ab89e8bdedc Implement #1220212. Add os.kill support for Windows.
Brian Curtin <brian.curtin@gmail.com>
parents: 59722
diff changeset
817 if not proc.poll():
0ab89e8bdedc Implement #1220212. Add os.kill support for Windows.
Brian Curtin <brian.curtin@gmail.com>
parents: 59722
diff changeset
818 # Forcefully kill the process if we weren't able to signal it.
0ab89e8bdedc Implement #1220212. Add os.kill support for Windows.
Brian Curtin <brian.curtin@gmail.com>
parents: 59722
diff changeset
819 os.kill(proc.pid, signal.SIGINT)
0ab89e8bdedc Implement #1220212. Add os.kill support for Windows.
Brian Curtin <brian.curtin@gmail.com>
parents: 59722
diff changeset
820 self.fail("subprocess did not stop on {}".format(name))
0ab89e8bdedc Implement #1220212. Add os.kill support for Windows.
Brian Curtin <brian.curtin@gmail.com>
parents: 59722
diff changeset
821
0ab89e8bdedc Implement #1220212. Add os.kill support for Windows.
Brian Curtin <brian.curtin@gmail.com>
parents: 59722
diff changeset
822 @unittest.skip("subprocesses aren't inheriting CTRL+C property")
0ab89e8bdedc Implement #1220212. Add os.kill support for Windows.
Brian Curtin <brian.curtin@gmail.com>
parents: 59722
diff changeset
823 def test_CTRL_C_EVENT(self):
0ab89e8bdedc Implement #1220212. Add os.kill support for Windows.
Brian Curtin <brian.curtin@gmail.com>
parents: 59722
diff changeset
824 from ctypes import wintypes
0ab89e8bdedc Implement #1220212. Add os.kill support for Windows.
Brian Curtin <brian.curtin@gmail.com>
parents: 59722
diff changeset
825 import ctypes
0ab89e8bdedc Implement #1220212. Add os.kill support for Windows.
Brian Curtin <brian.curtin@gmail.com>
parents: 59722
diff changeset
826
0ab89e8bdedc Implement #1220212. Add os.kill support for Windows.
Brian Curtin <brian.curtin@gmail.com>
parents: 59722
diff changeset
827 # Make a NULL value by creating a pointer with no argument.
0ab89e8bdedc Implement #1220212. Add os.kill support for Windows.
Brian Curtin <brian.curtin@gmail.com>
parents: 59722
diff changeset
828 NULL = ctypes.POINTER(ctypes.c_int)()
0ab89e8bdedc Implement #1220212. Add os.kill support for Windows.
Brian Curtin <brian.curtin@gmail.com>
parents: 59722
diff changeset
829 SetConsoleCtrlHandler = ctypes.windll.kernel32.SetConsoleCtrlHandler
0ab89e8bdedc Implement #1220212. Add os.kill support for Windows.
Brian Curtin <brian.curtin@gmail.com>
parents: 59722
diff changeset
830 SetConsoleCtrlHandler.argtypes = (ctypes.POINTER(ctypes.c_int),
0ab89e8bdedc Implement #1220212. Add os.kill support for Windows.
Brian Curtin <brian.curtin@gmail.com>
parents: 59722
diff changeset
831 wintypes.BOOL)
0ab89e8bdedc Implement #1220212. Add os.kill support for Windows.
Brian Curtin <brian.curtin@gmail.com>
parents: 59722
diff changeset
832 SetConsoleCtrlHandler.restype = wintypes.BOOL
0ab89e8bdedc Implement #1220212. Add os.kill support for Windows.
Brian Curtin <brian.curtin@gmail.com>
parents: 59722
diff changeset
833
0ab89e8bdedc Implement #1220212. Add os.kill support for Windows.
Brian Curtin <brian.curtin@gmail.com>
parents: 59722
diff changeset
834 # Calling this with NULL and FALSE causes the calling process to
0ab89e8bdedc Implement #1220212. Add os.kill support for Windows.
Brian Curtin <brian.curtin@gmail.com>
parents: 59722
diff changeset
835 # handle CTRL+C, rather than ignore it. This property is inherited
0ab89e8bdedc Implement #1220212. Add os.kill support for Windows.
Brian Curtin <brian.curtin@gmail.com>
parents: 59722
diff changeset
836 # by subprocesses.
0ab89e8bdedc Implement #1220212. Add os.kill support for Windows.
Brian Curtin <brian.curtin@gmail.com>
parents: 59722
diff changeset
837 SetConsoleCtrlHandler(NULL, 0)
0ab89e8bdedc Implement #1220212. Add os.kill support for Windows.
Brian Curtin <brian.curtin@gmail.com>
parents: 59722
diff changeset
838
0ab89e8bdedc Implement #1220212. Add os.kill support for Windows.
Brian Curtin <brian.curtin@gmail.com>
parents: 59722
diff changeset
839 self._kill_with_event(signal.CTRL_C_EVENT, "CTRL_C_EVENT")
0ab89e8bdedc Implement #1220212. Add os.kill support for Windows.
Brian Curtin <brian.curtin@gmail.com>
parents: 59722
diff changeset
840
0ab89e8bdedc Implement #1220212. Add os.kill support for Windows.
Brian Curtin <brian.curtin@gmail.com>
parents: 59722
diff changeset
841 def test_CTRL_BREAK_EVENT(self):
0ab89e8bdedc Implement #1220212. Add os.kill support for Windows.
Brian Curtin <brian.curtin@gmail.com>
parents: 59722
diff changeset
842 self._kill_with_event(signal.CTRL_BREAK_EVENT, "CTRL_BREAK_EVENT")
0ab89e8bdedc Implement #1220212. Add os.kill support for Windows.
Brian Curtin <brian.curtin@gmail.com>
parents: 59722
diff changeset
843
0ab89e8bdedc Implement #1220212. Add os.kill support for Windows.
Brian Curtin <brian.curtin@gmail.com>
parents: 59722
diff changeset
844
20013
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 19429
diff changeset
845 def test_main():
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28869
diff changeset
846 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
847 FileTests,
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28869
diff changeset
848 TemporaryFileTests,
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28869
diff changeset
849 StatAttributeTests,
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28869
diff changeset
850 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
851 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
852 MakedirTests,
32302
e196b823e6c5 Feature request #935915: Add os.path.devnull.
Martin v. Löwis <martin@v.loewis.de>
parents: 32204
diff changeset
853 DevNullTests,
38112
be49645e3984 Implement os.{chdir,rename,rmdir,remove} using Win32 directly.
Martin v. Löwis <martin@v.loewis.de>
parents: 34607
diff changeset
854 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
855 Win32ErrorTests,
52930
986201aca68b Merged revisions 71299 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 50914
diff changeset
856 TestInvalidFD,
60197
0ab89e8bdedc Implement #1220212. Add os.kill support for Windows.
Brian Curtin <brian.curtin@gmail.com>
parents: 59722
diff changeset
857 PosixUidGidTests,
0ab89e8bdedc Implement #1220212. Add os.kill support for Windows.
Brian Curtin <brian.curtin@gmail.com>
parents: 59722
diff changeset
858 Win32KillTests
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28869
diff changeset
859 )
20013
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 19429
diff changeset
860
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 19429
diff changeset
861 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
862 test_main()