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

Delta Between Two Patch Sets: Lib/test/test_posix.py

Issue 16510: Using appropriate checks in tests
Left Patch Set: Created 6 years, 9 months ago
Right Patch Set: Created 5 years, 6 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/test/test_platform.py ('k') | Lib/test/test_property.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 "Test posix functions" 1 "Test posix functions"
2 2
3 from test import support 3 from test import support
4 4
5 # Skip these tests if there is no posix module. 5 # Skip these tests if there is no posix module.
6 posix = support.import_module('posix') 6 posix = support.import_module('posix')
7 7
8 import errno 8 import errno
9 import sys 9 import sys
10 import time 10 import time
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
46 "getegid", "geteuid", "getgid", "getgroups", 46 "getegid", "geteuid", "getgid", "getgroups",
47 "getpid", "getpgrp", "getppid", "getuid", "sync", 47 "getpid", "getpgrp", "getppid", "getuid", "sync",
48 ] 48 ]
49 49
50 for name in NO_ARG_FUNCTIONS: 50 for name in NO_ARG_FUNCTIONS:
51 posix_func = getattr(posix, name, None) 51 posix_func = getattr(posix, name, None)
52 if posix_func is not None: 52 if posix_func is not None:
53 posix_func() 53 posix_func()
54 self.assertRaises(TypeError, posix_func, 1) 54 self.assertRaises(TypeError, posix_func, 1)
55 55
56 if hasattr(posix, 'getresuid'): 56 @unittest.skipUnless(hasattr(posix, 'getresuid'),
57 def test_getresuid(self): 57 'test needs posix.getresuid()')
58 user_ids = posix.getresuid() 58 def test_getresuid(self):
59 self.assertEqual(len(user_ids), 3) 59 user_ids = posix.getresuid()
60 for val in user_ids: 60 self.assertEqual(len(user_ids), 3)
61 self.assertGreaterEqual(val, 0) 61 for val in user_ids:
62 62 self.assertGreaterEqual(val, 0)
63 if hasattr(posix, 'getresgid'): 63
64 def test_getresgid(self): 64 @unittest.skipUnless(hasattr(posix, 'getresgid'),
65 group_ids = posix.getresgid() 65 'test needs posix.getresgid()')
66 self.assertEqual(len(group_ids), 3) 66 def test_getresgid(self):
67 for val in group_ids: 67 group_ids = posix.getresgid()
68 self.assertGreaterEqual(val, 0) 68 self.assertEqual(len(group_ids), 3)
69 69 for val in group_ids:
70 if hasattr(posix, 'setresuid'): 70 self.assertGreaterEqual(val, 0)
71 def test_setresuid(self): 71
72 current_user_ids = posix.getresuid() 72 @unittest.skipUnless(hasattr(posix, 'setresuid'),
73 self.assertIsNone(posix.setresuid(*current_user_ids)) 73 'test needs posix.setresuid()')
74 # -1 means don't change that value. 74 def test_setresuid(self):
75 self.assertIsNone(posix.setresuid(-1, -1, -1)) 75 current_user_ids = posix.getresuid()
76 76 self.assertIsNone(posix.setresuid(*current_user_ids))
77 def test_setresuid_exception(self): 77 # -1 means don't change that value.
78 # Don't do this test if someone is silly enough to run us as root. 78 self.assertIsNone(posix.setresuid(-1, -1, -1))
79 current_user_ids = posix.getresuid() 79
80 if 0 not in current_user_ids: 80 @unittest.skipUnless(hasattr(posix, 'setresuid'),
81 new_user_ids = (current_user_ids[0]+1, -1, -1) 81 'test needs posix.setresuid()')
82 self.assertRaises(OSError, posix.setresuid, *new_user_ids) 82 def test_setresuid_exception(self):
83 83 # Don't do this test if someone is silly enough to run us as root.
84 if hasattr(posix, 'setresgid'): 84 current_user_ids = posix.getresuid()
85 def test_setresgid(self): 85 if 0 not in current_user_ids:
86 current_group_ids = posix.getresgid() 86 new_user_ids = (current_user_ids[0]+1, -1, -1)
87 self.assertIsNone(posix.setresgid(*current_group_ids)) 87 self.assertRaises(OSError, posix.setresuid, *new_user_ids)
88 # -1 means don't change that value. 88
89 self.assertIsNone(posix.setresgid(-1, -1, -1)) 89 @unittest.skipUnless(hasattr(posix, 'setresgid'),
90 90 'test needs posix.setresgid()')
91 def test_setresgid_exception(self): 91 def test_setresgid(self):
92 # Don't do this test if someone is silly enough to run us as root. 92 current_group_ids = posix.getresgid()
93 current_group_ids = posix.getresgid() 93 self.assertIsNone(posix.setresgid(*current_group_ids))
94 if 0 not in current_group_ids: 94 # -1 means don't change that value.
95 new_group_ids = (current_group_ids[0]+1, -1, -1) 95 self.assertIsNone(posix.setresgid(-1, -1, -1))
96 self.assertRaises(OSError, posix.setresgid, *new_group_ids) 96
97 @unittest.skipUnless(hasattr(posix, 'setresgid'),
98 'test needs posix.setresgid()')
99 def test_setresgid_exception(self):
100 # Don't do this test if someone is silly enough to run us as root.
101 current_group_ids = posix.getresgid()
102 if 0 not in current_group_ids:
103 new_group_ids = (current_group_ids[0]+1, -1, -1)
104 self.assertRaises(OSError, posix.setresgid, *new_group_ids)
97 105
98 @unittest.skipUnless(hasattr(posix, 'initgroups'), 106 @unittest.skipUnless(hasattr(posix, 'initgroups'),
99 "test needs os.initgroups()") 107 "test needs os.initgroups()")
100 def test_initgroups(self): 108 def test_initgroups(self):
101 # It takes a string and an integer; check that it raises a TypeError 109 # It takes a string and an integer; check that it raises a TypeError
102 # for other argument lists. 110 # for other argument lists.
103 self.assertRaises(TypeError, posix.initgroups) 111 self.assertRaises(TypeError, posix.initgroups)
104 self.assertRaises(TypeError, posix.initgroups, None) 112 self.assertRaises(TypeError, posix.initgroups, None)
105 self.assertRaises(TypeError, posix.initgroups, 3, "foo") 113 self.assertRaises(TypeError, posix.initgroups, 3, "foo")
106 self.assertRaises(TypeError, posix.initgroups, "foo", 3, object()) 114 self.assertRaises(TypeError, posix.initgroups, "foo", 3, object())
107 115
108 # If a non-privileged user invokes it, it should fail with OSError 116 # If a non-privileged user invokes it, it should fail with OSError
109 # EPERM. 117 # EPERM.
110 if os.getuid() != 0: 118 if os.getuid() != 0:
111 try: 119 try:
112 name = pwd.getpwuid(posix.getuid()).pw_name 120 name = pwd.getpwuid(posix.getuid()).pw_name
113 except KeyError: 121 except KeyError:
114 # the current UID may not have a pwd entry 122 # the current UID may not have a pwd entry
115 raise unittest.SkipTest("need a pwd entry") 123 raise unittest.SkipTest("need a pwd entry")
116 try: 124 try:
117 posix.initgroups(name, 13) 125 posix.initgroups(name, 13)
118 except OSError as e: 126 except OSError as e:
119 self.assertEqual(e.errno, errno.EPERM) 127 self.assertEqual(e.errno, errno.EPERM)
120 else: 128 else:
121 self.fail("Expected OSError to be raised by initgroups") 129 self.fail("Expected OSError to be raised by initgroups")
122 130
131 @unittest.skipUnless(hasattr(posix, 'statvfs'),
132 'test needs posix.statvfs()')
123 def test_statvfs(self): 133 def test_statvfs(self):
124 if hasattr(posix, 'statvfs'): 134 self.assertTrue(posix.statvfs(os.curdir))
125 self.assertTrue(posix.statvfs(os.curdir)) 135
126 136 @unittest.skipUnless(hasattr(posix, 'fstatvfs'),
137 'test needs posix.fstatvfs()')
127 def test_fstatvfs(self): 138 def test_fstatvfs(self):
128 if hasattr(posix, 'fstatvfs'): 139 fp = open(support.TESTFN)
129 fp = open(support.TESTFN) 140 try:
130 try: 141 self.assertTrue(posix.fstatvfs(fp.fileno()))
131 self.assertTrue(posix.fstatvfs(fp.fileno())) 142 self.assertTrue(posix.statvfs(fp.fileno()))
132 self.assertTrue(posix.statvfs(fp.fileno())) 143 finally:
133 finally: 144 fp.close()
134 fp.close() 145
135 146 @unittest.skipUnless(hasattr(posix, 'ftruncate'),
147 'test needs posix.ftruncate()')
136 def test_ftruncate(self): 148 def test_ftruncate(self):
137 if hasattr(posix, 'ftruncate'): 149 fp = open(support.TESTFN, 'w+')
138 fp = open(support.TESTFN, 'w+') 150 try:
139 try: 151 # we need to have some data to truncate
140 # we need to have some data to truncate 152 fp.write('test')
141 fp.write('test') 153 fp.flush()
142 fp.flush() 154 posix.ftruncate(fp.fileno(), 0)
143 posix.ftruncate(fp.fileno(), 0) 155 finally:
144 finally: 156 fp.close()
145 fp.close()
146 157
147 @unittest.skipUnless(hasattr(posix, 'truncate'), "test needs posix.truncate( )") 158 @unittest.skipUnless(hasattr(posix, 'truncate'), "test needs posix.truncate( )")
148 def test_truncate(self): 159 def test_truncate(self):
149 with open(support.TESTFN, 'w') as fp: 160 with open(support.TESTFN, 'w') as fp:
150 fp.write('test') 161 fp.write('test')
151 fp.flush() 162 fp.flush()
152 posix.truncate(support.TESTFN, 0) 163 posix.truncate(support.TESTFN, 0)
153 164
154 @unittest.skipUnless(getattr(os, 'execve', None) in os.supports_fd, "test ne eds execve() to support the fd parameter") 165 @unittest.skipUnless(getattr(os, 'execve', None) in os.supports_fd, "test ne eds execve() to support the fd parameter")
155 @unittest.skipUnless(hasattr(os, 'fork'), "test needs os.fork()") 166 @unittest.skipUnless(hasattr(os, 'fork'), "test needs os.fork()")
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
264 self.assertRaises(TypeError, posix.utime, support.TESTFN, (now, None), f ollow_symlinks=False) 275 self.assertRaises(TypeError, posix.utime, support.TESTFN, (now, None), f ollow_symlinks=False)
265 self.assertRaises(TypeError, posix.utime, support.TESTFN, (None, now), f ollow_symlinks=False) 276 self.assertRaises(TypeError, posix.utime, support.TESTFN, (None, now), f ollow_symlinks=False)
266 posix.utime(support.TESTFN, (int(now), int(now)), follow_symlinks=False) 277 posix.utime(support.TESTFN, (int(now), int(now)), follow_symlinks=False)
267 posix.utime(support.TESTFN, (now, now), follow_symlinks=False) 278 posix.utime(support.TESTFN, (now, now), follow_symlinks=False)
268 posix.utime(support.TESTFN, follow_symlinks=False) 279 posix.utime(support.TESTFN, follow_symlinks=False)
269 280
270 @unittest.skipUnless(hasattr(posix, 'writev'), "test needs posix.writev()") 281 @unittest.skipUnless(hasattr(posix, 'writev'), "test needs posix.writev()")
271 def test_writev(self): 282 def test_writev(self):
272 fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT) 283 fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT)
273 try: 284 try:
274 os.writev(fd, (b'test1', b'tt2', b't3')) 285 n = os.writev(fd, (b'test1', b'tt2', b't3'))
286 self.assertEqual(n, 10)
287
275 os.lseek(fd, 0, os.SEEK_SET) 288 os.lseek(fd, 0, os.SEEK_SET)
276 self.assertEqual(b'test1tt2t3', posix.read(fd, 10)) 289 self.assertEqual(b'test1tt2t3', posix.read(fd, 10))
290
291 # Issue #20113: empty list of buffers should not crash
292 try:
293 size = posix.writev(fd, [])
294 except OSError:
295 # writev(fd, []) raises OSError(22, "Invalid argument")
296 # on OpenIndiana
297 pass
298 else:
299 self.assertEqual(size, 0)
277 finally: 300 finally:
278 os.close(fd) 301 os.close(fd)
279 302
280 @unittest.skipUnless(hasattr(posix, 'readv'), "test needs posix.readv()") 303 @unittest.skipUnless(hasattr(posix, 'readv'), "test needs posix.readv()")
281 def test_readv(self): 304 def test_readv(self):
282 fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT) 305 fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT)
283 try: 306 try:
284 os.write(fd, b'test1tt2t3') 307 os.write(fd, b'test1tt2t3')
285 os.lseek(fd, 0, os.SEEK_SET) 308 os.lseek(fd, 0, os.SEEK_SET)
286 buf = [bytearray(i) for i in [5, 3, 2]] 309 buf = [bytearray(i) for i in [5, 3, 2]]
287 self.assertEqual(posix.readv(fd, buf), 10) 310 self.assertEqual(posix.readv(fd, buf), 10)
288 self.assertEqual([b'test1', b'tt2', b't3'], [bytes(i) for i in buf]) 311 self.assertEqual([b'test1', b'tt2', b't3'], [bytes(i) for i in buf])
312
313 # Issue #20113: empty list of buffers should not crash
314 try:
315 size = posix.readv(fd, [])
316 except OSError:
317 # readv(fd, []) raises OSError(22, "Invalid argument")
318 # on OpenIndiana
319 pass
320 else:
321 self.assertEqual(size, 0)
289 finally: 322 finally:
290 os.close(fd) 323 os.close(fd)
291 324
325 @unittest.skipUnless(hasattr(posix, 'dup'),
326 'test needs posix.dup()')
292 def test_dup(self): 327 def test_dup(self):
293 if hasattr(posix, 'dup'): 328 fp = open(support.TESTFN)
294 fp = open(support.TESTFN) 329 try:
295 try: 330 fd = posix.dup(fp.fileno())
296 fd = posix.dup(fp.fileno()) 331 self.assertIsInstance(fd, int)
297 self.assertIsInstance(fd, int) 332 os.close(fd)
298 os.close(fd) 333 finally:
299 finally: 334 fp.close()
300 fp.close() 335
301 336 @unittest.skipUnless(hasattr(posix, 'confstr'),
337 'test needs posix.confstr()')
302 def test_confstr(self): 338 def test_confstr(self):
303 if hasattr(posix, 'confstr'): 339 self.assertRaises(ValueError, posix.confstr, "CS_garbage")
304 self.assertRaises(ValueError, posix.confstr, "CS_garbage") 340 self.assertEqual(len(posix.confstr("CS_PATH")) > 0, True)
305 self.assertEqual(len(posix.confstr("CS_PATH")) > 0, True) 341
306 342 @unittest.skipUnless(hasattr(posix, 'dup2'),
343 'test needs posix.dup2()')
307 def test_dup2(self): 344 def test_dup2(self):
308 if hasattr(posix, 'dup2'): 345 fp1 = open(support.TESTFN)
309 fp1 = open(support.TESTFN) 346 fp2 = open(support.TESTFN)
310 fp2 = open(support.TESTFN) 347 try:
311 try: 348 posix.dup2(fp1.fileno(), fp2.fileno())
312 posix.dup2(fp1.fileno(), fp2.fileno()) 349 finally:
313 finally: 350 fp1.close()
314 fp1.close() 351 fp2.close()
315 fp2.close()
316 352
317 @unittest.skipUnless(hasattr(os, 'O_CLOEXEC'), "needs os.O_CLOEXEC") 353 @unittest.skipUnless(hasattr(os, 'O_CLOEXEC'), "needs os.O_CLOEXEC")
318 @support.requires_linux_version(2, 6, 23) 354 @support.requires_linux_version(2, 6, 23)
319 def test_oscloexec(self): 355 def test_oscloexec(self):
320 fd = os.open(support.TESTFN, os.O_RDONLY|os.O_CLOEXEC) 356 fd = os.open(support.TESTFN, os.O_RDONLY|os.O_CLOEXEC)
321 self.addCleanup(os.close, fd) 357 self.addCleanup(os.close, fd)
322 self.assertTrue(fcntl.fcntl(fd, fcntl.F_GETFD) & fcntl.FD_CLOEXEC) 358 self.assertTrue(fcntl.fcntl(fd, fcntl.F_GETFD) & fcntl.FD_CLOEXEC)
323 359
360 @unittest.skipUnless(hasattr(posix, 'O_EXLOCK'),
361 'test needs posix.O_EXLOCK')
324 def test_osexlock(self): 362 def test_osexlock(self):
325 if hasattr(posix, "O_EXLOCK"): 363 fd = os.open(support.TESTFN,
364 os.O_WRONLY|os.O_EXLOCK|os.O_CREAT)
365 self.assertRaises(OSError, os.open, support.TESTFN,
366 os.O_WRONLY|os.O_EXLOCK|os.O_NONBLOCK)
367 os.close(fd)
368
369 if hasattr(posix, "O_SHLOCK"):
326 fd = os.open(support.TESTFN, 370 fd = os.open(support.TESTFN,
327 os.O_WRONLY|os.O_EXLOCK|os.O_CREAT) 371 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
328 self.assertRaises(OSError, os.open, support.TESTFN, 372 self.assertRaises(OSError, os.open, support.TESTFN,
329 os.O_WRONLY|os.O_EXLOCK|os.O_NONBLOCK) 373 os.O_WRONLY|os.O_EXLOCK|os.O_NONBLOCK)
330 os.close(fd) 374 os.close(fd)
331 375
332 if hasattr(posix, "O_SHLOCK"): 376 @unittest.skipUnless(hasattr(posix, 'O_SHLOCK'),
333 fd = os.open(support.TESTFN, 377 'test needs posix.O_SHLOCK')
334 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
335 self.assertRaises(OSError, os.open, support.TESTFN,
336 os.O_WRONLY|os.O_EXLOCK|os.O_NONBLOCK)
337 os.close(fd)
338
339 def test_osshlock(self): 378 def test_osshlock(self):
340 if hasattr(posix, "O_SHLOCK"): 379 fd1 = os.open(support.TESTFN,
341 fd1 = os.open(support.TESTFN, 380 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
381 fd2 = os.open(support.TESTFN,
382 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
383 os.close(fd2)
384 os.close(fd1)
385
386 if hasattr(posix, "O_EXLOCK"):
387 fd = os.open(support.TESTFN,
342 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT) 388 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
343 fd2 = os.open(support.TESTFN, 389 self.assertRaises(OSError, os.open, support.TESTFN,
344 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT) 390 os.O_RDONLY|os.O_EXLOCK|os.O_NONBLOCK)
345 os.close(fd2) 391 os.close(fd)
346 os.close(fd1) 392
347 393 @unittest.skipUnless(hasattr(posix, 'fstat'),
348 if hasattr(posix, "O_EXLOCK"): 394 'test needs posix.fstat()')
349 fd = os.open(support.TESTFN,
350 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
351 self.assertRaises(OSError, os.open, support.TESTFN,
352 os.O_RDONLY|os.O_EXLOCK|os.O_NONBLOCK)
353 os.close(fd)
354
355 def test_fstat(self): 395 def test_fstat(self):
356 if hasattr(posix, 'fstat'): 396 fp = open(support.TESTFN)
357 fp = open(support.TESTFN) 397 try:
358 try: 398 self.assertTrue(posix.fstat(fp.fileno()))
359 self.assertTrue(posix.fstat(fp.fileno())) 399 self.assertTrue(posix.stat(fp.fileno()))
360 self.assertTrue(posix.stat(fp.fileno())) 400
361 finally: 401 self.assertRaisesRegex(TypeError,
362 fp.close() 402 'should be string, bytes or integer, not',
363 403 posix.stat, float(fp.fileno()))
404 finally:
405 fp.close()
406
407 @unittest.skipUnless(hasattr(posix, 'stat'),
408 'test needs posix.stat()')
364 def test_stat(self): 409 def test_stat(self):
365 if hasattr(posix, 'stat'): 410 self.assertTrue(posix.stat(support.TESTFN))
366 self.assertTrue(posix.stat(support.TESTFN)) 411 self.assertTrue(posix.stat(os.fsencode(support.TESTFN)))
412 self.assertTrue(posix.stat(bytearray(os.fsencode(support.TESTFN))))
413
414 self.assertRaisesRegex(TypeError,
415 'can\'t specify None for path argument',
416 posix.stat, None)
417 self.assertRaisesRegex(TypeError,
418 'should be string, bytes or integer, not',
419 posix.stat, list(support.TESTFN))
420 self.assertRaisesRegex(TypeError,
421 'should be string, bytes or integer, not',
422 posix.stat, list(os.fsencode(support.TESTFN)))
367 423
368 @unittest.skipUnless(hasattr(posix, 'mkfifo'), "don't have mkfifo()") 424 @unittest.skipUnless(hasattr(posix, 'mkfifo'), "don't have mkfifo()")
369 def test_mkfifo(self): 425 def test_mkfifo(self):
370 support.unlink(support.TESTFN) 426 support.unlink(support.TESTFN)
371 posix.mkfifo(support.TESTFN, stat.S_IRUSR | stat.S_IWUSR) 427 posix.mkfifo(support.TESTFN, stat.S_IRUSR | stat.S_IWUSR)
372 self.assertTrue(stat.S_ISFIFO(posix.stat(support.TESTFN).st_mode)) 428 self.assertTrue(stat.S_ISFIFO(posix.stat(support.TESTFN).st_mode))
373 429
374 @unittest.skipUnless(hasattr(posix, 'mknod') and hasattr(stat, 'S_IFIFO'), 430 @unittest.skipUnless(hasattr(posix, 'mknod') and hasattr(stat, 'S_IFIFO'),
375 "don't have mknod()/S_IFIFO") 431 "don't have mknod()/S_IFIFO")
376 def test_mknod(self): 432 def test_mknod(self):
377 # Test using mknod() to create a FIFO (the only use specified 433 # Test using mknod() to create a FIFO (the only use specified
378 # by POSIX). 434 # by POSIX).
379 support.unlink(support.TESTFN) 435 support.unlink(support.TESTFN)
380 mode = stat.S_IFIFO | stat.S_IRUSR | stat.S_IWUSR 436 mode = stat.S_IFIFO | stat.S_IRUSR | stat.S_IWUSR
381 try: 437 try:
382 posix.mknod(support.TESTFN, mode, 0) 438 posix.mknod(support.TESTFN, mode, 0)
383 except OSError as e: 439 except OSError as e:
384 # Some old systems don't allow unprivileged users to use 440 # Some old systems don't allow unprivileged users to use
385 # mknod(), or only support creating device nodes. 441 # mknod(), or only support creating device nodes.
386 self.assertIn(e.errno, (errno.EPERM, errno.EINVAL)) 442 self.assertIn(e.errno, (errno.EPERM, errno.EINVAL))
387 else: 443 else:
388 self.assertTrue(stat.S_ISFIFO(posix.stat(support.TESTFN).st_mode)) 444 self.assertTrue(stat.S_ISFIFO(posix.stat(support.TESTFN).st_mode))
389 445
390 def _test_all_chown_common(self, chown_func, first_param): 446 def _test_all_chown_common(self, chown_func, first_param, stat_func):
391 """Common code for chown, fchown and lchown tests.""" 447 """Common code for chown, fchown and lchown tests."""
448 def check_stat(uid, gid):
449 if stat_func is not None:
450 stat = stat_func(first_param)
451 self.assertEqual(stat.st_uid, uid)
452 self.assertEqual(stat.st_gid, gid)
453 uid = os.getuid()
454 gid = os.getgid()
392 # test a successful chown call 455 # test a successful chown call
393 chown_func(first_param, os.getuid(), os.getgid()) 456 chown_func(first_param, uid, gid)
394 457 check_stat(uid, gid)
395 if os.getuid() == 0: 458 chown_func(first_param, -1, gid)
396 try: 459 check_stat(uid, gid)
397 # Many linux distros have a nfsnobody user as MAX_UID-2 460 chown_func(first_param, uid, -1)
398 # that makes a good test case for signedness issues. 461 check_stat(uid, gid)
399 # http://bugs.python.org/issue1747858 462
400 # This part of the test only runs when run as root. 463 if uid == 0:
401 # Only scary people run their tests as root. 464 # Try an amusingly large uid/gid to make sure we handle
402 ent = pwd.getpwnam('nfsnobody') 465 # large unsigned values. (chown lets you use any
403 chown_func(first_param, ent.pw_uid, ent.pw_gid) 466 # uid/gid you like, even if they aren't defined.)
404 except KeyError: 467 #
405 pass 468 # This problem keeps coming up:
469 # http://bugs.python.org/issue1747858
470 # http://bugs.python.org/issue4591
471 # http://bugs.python.org/issue15301
472 # Hopefully the fix in 4591 fixes it for good!
473 #
474 # This part of the test only runs when run as root.
475 # Only scary people run their tests as root.
476
477 big_value = 2**31
478 chown_func(first_param, big_value, big_value)
479 check_stat(big_value, big_value)
480 chown_func(first_param, -1, -1)
481 check_stat(big_value, big_value)
482 chown_func(first_param, uid, gid)
483 check_stat(uid, gid)
406 elif platform.system() in ('HP-UX', 'SunOS'): 484 elif platform.system() in ('HP-UX', 'SunOS'):
407 # HP-UX and Solaris can allow a non-root user to chown() to root 485 # HP-UX and Solaris can allow a non-root user to chown() to root
408 # (issue #5113) 486 # (issue #5113)
409 raise unittest.SkipTest("Skipping because of non-standard chown() " 487 raise unittest.SkipTest("Skipping because of non-standard chown() "
410 "behavior") 488 "behavior")
411 else: 489 else:
412 # non-root cannot chown to root, raises OSError 490 # non-root cannot chown to root, raises OSError
413 self.assertRaises(OSError, chown_func, 491 self.assertRaises(OSError, chown_func, first_param, 0, 0)
414 first_param, 0, 0) 492 check_stat(uid, gid)
493 self.assertRaises(OSError, chown_func, first_param, 0, -1)
494 check_stat(uid, gid)
495 if 0 not in os.getgroups():
496 self.assertRaises(OSError, chown_func, first_param, -1, 0)
497 check_stat(uid, gid)
498 # test illegal types
499 for t in str, float:
500 self.assertRaises(TypeError, chown_func, first_param, t(uid), gid)
501 check_stat(uid, gid)
502 self.assertRaises(TypeError, chown_func, first_param, uid, t(gid))
503 check_stat(uid, gid)
415 504
416 @unittest.skipUnless(hasattr(posix, 'chown'), "test needs os.chown()") 505 @unittest.skipUnless(hasattr(posix, 'chown'), "test needs os.chown()")
417 def test_chown(self): 506 def test_chown(self):
418 # raise an OSError if the file does not exist 507 # raise an OSError if the file does not exist
419 os.unlink(support.TESTFN) 508 os.unlink(support.TESTFN)
420 self.assertRaises(OSError, posix.chown, support.TESTFN, -1, -1) 509 self.assertRaises(OSError, posix.chown, support.TESTFN, -1, -1)
421 510
422 # re-create the file 511 # re-create the file
423 support.create_empty_file(support.TESTFN) 512 support.create_empty_file(support.TESTFN)
424 self._test_all_chown_common(posix.chown, support.TESTFN) 513 self._test_all_chown_common(posix.chown, support.TESTFN,
514 getattr(posix, 'stat', None))
425 515
426 @unittest.skipUnless(hasattr(posix, 'fchown'), "test needs os.fchown()") 516 @unittest.skipUnless(hasattr(posix, 'fchown'), "test needs os.fchown()")
427 def test_fchown(self): 517 def test_fchown(self):
428 os.unlink(support.TESTFN) 518 os.unlink(support.TESTFN)
429 519
430 # re-create the file 520 # re-create the file
431 test_file = open(support.TESTFN, 'w') 521 test_file = open(support.TESTFN, 'w')
432 try: 522 try:
433 fd = test_file.fileno() 523 fd = test_file.fileno()
434 self._test_all_chown_common(posix.fchown, fd) 524 self._test_all_chown_common(posix.fchown, fd,
525 getattr(posix, 'fstat', None))
435 finally: 526 finally:
436 test_file.close() 527 test_file.close()
437 528
438 @unittest.skipUnless(hasattr(posix, 'lchown'), "test needs os.lchown()") 529 @unittest.skipUnless(hasattr(posix, 'lchown'), "test needs os.lchown()")
439 def test_lchown(self): 530 def test_lchown(self):
440 os.unlink(support.TESTFN) 531 os.unlink(support.TESTFN)
441 # create a symlink 532 # create a symlink
442 os.symlink(_DUMMY_SYMLINK, support.TESTFN) 533 os.symlink(_DUMMY_SYMLINK, support.TESTFN)
443 self._test_all_chown_common(posix.lchown, support.TESTFN) 534 self._test_all_chown_common(posix.lchown, support.TESTFN,
444 535 getattr(posix, 'lstat', None))
536
537 @unittest.skipUnless(hasattr(posix, 'chdir'), 'test needs posix.chdir()')
445 def test_chdir(self): 538 def test_chdir(self):
446 if hasattr(posix, 'chdir'): 539 posix.chdir(os.curdir)
447 posix.chdir(os.curdir) 540 self.assertRaises(OSError, posix.chdir, support.TESTFN)
448 self.assertRaises(OSError, posix.chdir, support.TESTFN)
449 541
450 def test_listdir(self): 542 def test_listdir(self):
451 self.assertIn(support.TESTFN, posix.listdir(os.curdir)) 543 self.assertIn(support.TESTFN, posix.listdir(os.curdir))
452 544
453 def test_listdir_default(self): 545 def test_listdir_default(self):
454 # When listdir is called without argument, 546 # When listdir is called without argument,
455 # it's the same as listdir(os.curdir). 547 # it's the same as listdir(os.curdir).
456 self.assertIn(support.TESTFN, posix.listdir()) 548 self.assertIn(support.TESTFN, posix.listdir())
457 549
458 def test_listdir_bytes(self): 550 def test_listdir_bytes(self):
459 # When listdir is called with a bytes object, 551 # When listdir is called with a bytes object,
460 # the returned strings are of type bytes. 552 # the returned strings are of type bytes.
461 self.assertIn(os.fsencode(support.TESTFN), posix.listdir(b'.')) 553 self.assertIn(os.fsencode(support.TESTFN), posix.listdir(b'.'))
462 554
463 @unittest.skipUnless(posix.listdir in os.supports_fd, 555 @unittest.skipUnless(posix.listdir in os.supports_fd,
464 "test needs fd support for posix.listdir()") 556 "test needs fd support for posix.listdir()")
465 def test_listdir_fd(self): 557 def test_listdir_fd(self):
466 f = posix.open(posix.getcwd(), posix.O_RDONLY) 558 f = posix.open(posix.getcwd(), posix.O_RDONLY)
467 self.addCleanup(posix.close, f) 559 self.addCleanup(posix.close, f)
468 self.assertEqual( 560 self.assertEqual(
469 sorted(posix.listdir('.')), 561 sorted(posix.listdir('.')),
470 sorted(posix.listdir(f)) 562 sorted(posix.listdir(f))
471 ) 563 )
472 # Check that the fd offset was reset (issue #13739) 564 # Check that the fd offset was reset (issue #13739)
473 self.assertEqual( 565 self.assertEqual(
474 sorted(posix.listdir('.')), 566 sorted(posix.listdir('.')),
475 sorted(posix.listdir(f)) 567 sorted(posix.listdir(f))
476 ) 568 )
477 569
570 @unittest.skipUnless(hasattr(posix, 'access'), 'test needs posix.access()')
478 def test_access(self): 571 def test_access(self):
479 if hasattr(posix, 'access'): 572 self.assertTrue(posix.access(support.TESTFN, os.R_OK))
480 self.assertTrue(posix.access(support.TESTFN, os.R_OK)) 573
481 574 @unittest.skipUnless(hasattr(posix, 'umask'), 'test needs posix.umask()')
482 def test_umask(self): 575 def test_umask(self):
483 if hasattr(posix, 'umask'): 576 old_mask = posix.umask(0)
484 old_mask = posix.umask(0) 577 self.assertIsInstance(old_mask, int)
485 self.assertIsInstance(old_mask, int) 578 posix.umask(old_mask)
486 posix.umask(old_mask) 579
487 580 @unittest.skipUnless(hasattr(posix, 'strerror'),
581 'test needs posix.strerror()')
488 def test_strerror(self): 582 def test_strerror(self):
489 if hasattr(posix, 'strerror'): 583 self.assertTrue(posix.strerror(0))
490 self.assertTrue(posix.strerror(0)) 584
491 585 @unittest.skipUnless(hasattr(posix, 'pipe'), 'test needs posix.pipe()')
492 def test_pipe(self): 586 def test_pipe(self):
493 if hasattr(posix, 'pipe'): 587 reader, writer = posix.pipe()
494 reader, writer = posix.pipe() 588 os.close(reader)
495 os.close(reader) 589 os.close(writer)
496 os.close(writer)
497 590
498 @unittest.skipUnless(hasattr(os, 'pipe2'), "test needs os.pipe2()") 591 @unittest.skipUnless(hasattr(os, 'pipe2'), "test needs os.pipe2()")
499 @support.requires_linux_version(2, 6, 27) 592 @support.requires_linux_version(2, 6, 27)
500 def test_pipe2(self): 593 def test_pipe2(self):
501 self.assertRaises(TypeError, os.pipe2, 'DEADBEEF') 594 self.assertRaises(TypeError, os.pipe2, 'DEADBEEF')
502 self.assertRaises(TypeError, os.pipe2, 0, 0) 595 self.assertRaises(TypeError, os.pipe2, 0, 0)
503 596
504 # try calling with flags = 0, like os.pipe() 597 # try calling with flags = 0, like os.pipe()
505 r, w = os.pipe2(0) 598 r, w = os.pipe2(0)
506 os.close(r) 599 os.close(r)
507 os.close(w) 600 os.close(w)
508 601
509 # test flags 602 # test flags
510 r, w = os.pipe2(os.O_CLOEXEC|os.O_NONBLOCK) 603 r, w = os.pipe2(os.O_CLOEXEC|os.O_NONBLOCK)
511 self.addCleanup(os.close, r) 604 self.addCleanup(os.close, r)
512 self.addCleanup(os.close, w) 605 self.addCleanup(os.close, w)
513 self.assertTrue(fcntl.fcntl(r, fcntl.F_GETFD) & fcntl.FD_CLOEXEC) 606 self.assertFalse(os.get_inheritable(r))
514 self.assertTrue(fcntl.fcntl(w, fcntl.F_GETFD) & fcntl.FD_CLOEXEC) 607 self.assertFalse(os.get_inheritable(w))
608 self.assertTrue(fcntl.fcntl(r, fcntl.F_GETFL) & os.O_NONBLOCK)
609 self.assertTrue(fcntl.fcntl(w, fcntl.F_GETFL) & os.O_NONBLOCK)
515 # try reading from an empty pipe: this should fail, not block 610 # try reading from an empty pipe: this should fail, not block
516 self.assertRaises(OSError, os.read, r, 1) 611 self.assertRaises(OSError, os.read, r, 1)
517 # try a write big enough to fill-up the pipe: this should either 612 # try a write big enough to fill-up the pipe: this should either
518 # fail or perform a partial write, not block 613 # fail or perform a partial write, not block
519 try: 614 try:
520 os.write(w, b'x' * support.PIPE_MAX_SIZE) 615 os.write(w, b'x' * support.PIPE_MAX_SIZE)
521 except OSError: 616 except OSError:
522 pass 617 pass
523 618
619 @support.cpython_only
620 @unittest.skipUnless(hasattr(os, 'pipe2'), "test needs os.pipe2()")
621 @support.requires_linux_version(2, 6, 27)
622 def test_pipe2_c_limits(self):
623 # Issue 15989
624 import _testcapi
625 self.assertRaises(OverflowError, os.pipe2, _testcapi.INT_MAX + 1)
626 self.assertRaises(OverflowError, os.pipe2, _testcapi.UINT_MAX + 1)
627
628 @unittest.skipUnless(hasattr(posix, 'utime'), 'test needs posix.utime()')
524 def test_utime(self): 629 def test_utime(self):
525 if hasattr(posix, 'utime'): 630 now = time.time()
526 now = time.time() 631 posix.utime(support.TESTFN, None)
527 posix.utime(support.TESTFN, None) 632 self.assertRaises(TypeError, posix.utime, support.TESTFN, (None, None))
528 self.assertRaises(TypeError, posix.utime, support.TESTFN, (None, Non e)) 633 self.assertRaises(TypeError, posix.utime, support.TESTFN, (now, None))
529 self.assertRaises(TypeError, posix.utime, support.TESTFN, (now, None )) 634 self.assertRaises(TypeError, posix.utime, support.TESTFN, (None, now))
530 self.assertRaises(TypeError, posix.utime, support.TESTFN, (None, now )) 635 posix.utime(support.TESTFN, (int(now), int(now)))
531 posix.utime(support.TESTFN, (int(now), int(now))) 636 posix.utime(support.TESTFN, (now, now))
532 posix.utime(support.TESTFN, (now, now))
533 637
534 def _test_chflags_regular_file(self, chflags_func, target_file, **kwargs): 638 def _test_chflags_regular_file(self, chflags_func, target_file, **kwargs):
535 st = os.stat(target_file) 639 st = os.stat(target_file)
536 self.assertTrue(hasattr(st, 'st_flags')) 640 self.assertTrue(hasattr(st, 'st_flags'))
537 641
538 # ZFS returns EOPNOTSUPP when attempting to set flag UF_IMMUTABLE. 642 # ZFS returns EOPNOTSUPP when attempting to set flag UF_IMMUTABLE.
539 flags = st.st_flags | stat.UF_IMMUTABLE 643 flags = st.st_flags | stat.UF_IMMUTABLE
540 try: 644 try:
541 chflags_func(target_file, flags, **kwargs) 645 chflags_func(target_file, flags, **kwargs)
542 except OSError as err: 646 except OSError as err:
543 if err.errno != errno.EOPNOTSUPP: 647 if err.errno != errno.EOPNOTSUPP:
544 raise 648 raise
545 msg = 'chflag UF_IMMUTABLE not supported by underlying fs' 649 msg = 'chflag UF_IMMUTABLE not supported by underlying fs'
546 self.skipTest(msg) 650 self.skipTest(msg)
547 651
548 try: 652 try:
549 new_st = os.stat(target_file) 653 new_st = os.stat(target_file)
550 self.assertEqual(st.st_flags | stat.UF_IMMUTABLE, new_st.st_flags) 654 self.assertEqual(st.st_flags | stat.UF_IMMUTABLE, new_st.st_flags)
551 try: 655 try:
552 fd = open(target_file, 'w+') 656 fd = open(target_file, 'w+')
553 except IOError as e: 657 except OSError as e:
554 self.assertEqual(e.errno, errno.EPERM) 658 self.assertEqual(e.errno, errno.EPERM)
555 finally: 659 finally:
556 posix.chflags(target_file, st.st_flags) 660 posix.chflags(target_file, st.st_flags)
557 661
558 @unittest.skipUnless(hasattr(posix, 'chflags'), 'test needs os.chflags()') 662 @unittest.skipUnless(hasattr(posix, 'chflags'), 'test needs os.chflags()')
559 def test_chflags(self): 663 def test_chflags(self):
560 self._test_chflags_regular_file(posix.chflags, support.TESTFN) 664 self._test_chflags_regular_file(posix.chflags, support.TESTFN)
561 665
562 @unittest.skipUnless(hasattr(posix, 'lchflags'), 'test needs os.lchflags()') 666 @unittest.skipUnless(hasattr(posix, 'lchflags'), 'test needs os.lchflags()')
563 def test_lchflags_regular_file(self): 667 def test_lchflags_regular_file(self):
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
599 703
600 def test_environ(self): 704 def test_environ(self):
601 if os.name == "nt": 705 if os.name == "nt":
602 item_type = str 706 item_type = str
603 else: 707 else:
604 item_type = bytes 708 item_type = bytes
605 for k, v in posix.environ.items(): 709 for k, v in posix.environ.items():
606 self.assertEqual(type(k), item_type) 710 self.assertEqual(type(k), item_type)
607 self.assertEqual(type(v), item_type) 711 self.assertEqual(type(v), item_type)
608 712
713 @unittest.skipUnless(hasattr(posix, 'getcwd'), 'test needs posix.getcwd()')
609 def test_getcwd_long_pathnames(self): 714 def test_getcwd_long_pathnames(self):
610 if hasattr(posix, 'getcwd'): 715 dirname = 'getcwd-test-directory-0123456789abcdef-01234567890abcdef'
611 dirname = 'getcwd-test-directory-0123456789abcdef-01234567890abcdef' 716 curdir = os.getcwd()
612 curdir = os.getcwd() 717 base_path = os.path.abspath(support.TESTFN) + '.getcwd'
613 base_path = os.path.abspath(support.TESTFN) + '.getcwd' 718
614 719 try:
615 try: 720 os.mkdir(base_path)
616 os.mkdir(base_path) 721 os.chdir(base_path)
617 os.chdir(base_path) 722 except:
618 except: 723 # Just returning nothing instead of the SkipTest exception, because
619 # Just returning nothing instead of the SkipTest exception, 724 # the test results in Error in that case. Is that ok?
620 # because the test results in Error in that case. 725 # raise unittest.SkipTest("cannot create directory for testing")
621 # Is that ok? 726 return
622 # raise unittest.SkipTest("cannot create directory for testing") 727
623 return 728 def _create_and_do_getcwd(dirname, current_path_length = 0):
624 729 try:
625 def _create_and_do_getcwd(dirname, current_path_length = 0): 730 os.mkdir(dirname)
626 try: 731 except:
627 os.mkdir(dirname) 732 raise unittest.SkipTest("mkdir cannot create directory suffi ciently deep for getcwd test")
628 except: 733
629 raise unittest.SkipTest("mkdir cannot create directory s ufficiently deep for getcwd test") 734 os.chdir(dirname)
630 735 try:
631 os.chdir(dirname) 736 os.getcwd()
632 try: 737 if current_path_length < 1027:
633 os.getcwd() 738 _create_and_do_getcwd(dirname, current_path_length + len (dirname) + 1)
634 if current_path_length < 1027: 739 finally:
635 _create_and_do_getcwd(dirname, current_path_length + len(dirname) + 1) 740 os.chdir('..')
636 finally: 741 os.rmdir(dirname)
637 os.chdir('..') 742
638 os.rmdir(dirname) 743 _create_and_do_getcwd(dirname)
639 744
640 _create_and_do_getcwd(dirname) 745 finally:
641 746 os.chdir(curdir)
642 finally: 747 support.rmtree(base_path)
643 os.chdir(curdir)
644 support.rmtree(base_path)
645 748
646 @unittest.skipUnless(hasattr(posix, 'getgrouplist'), "test needs posix.getgr ouplist()") 749 @unittest.skipUnless(hasattr(posix, 'getgrouplist'), "test needs posix.getgr ouplist()")
647 @unittest.skipUnless(hasattr(pwd, 'getpwuid'), "test needs pwd.getpwuid()") 750 @unittest.skipUnless(hasattr(pwd, 'getpwuid'), "test needs pwd.getpwuid()")
648 @unittest.skipUnless(hasattr(os, 'getuid'), "test needs os.getuid()") 751 @unittest.skipUnless(hasattr(os, 'getuid'), "test needs os.getuid()")
649 def test_getgrouplist(self): 752 def test_getgrouplist(self):
650 with os.popen('id -G') as idg: 753 user = pwd.getpwuid(os.getuid())[0]
651 groups = idg.read().strip() 754 group = pwd.getpwuid(os.getuid())[3]
652 ret = idg.close() 755 self.assertIn(group, posix.getgrouplist(user, group))
653 756
654 if ret is not None or not groups:
655 raise unittest.SkipTest("need working 'id -G'")
656
657 self.assertEqual(
658 set([int(x) for x in groups.split()]),
659 set(posix.getgrouplist(pwd.getpwuid(os.getuid())[0],
660 pwd.getpwuid(os.getuid())[3])))
661 757
662 @unittest.skipUnless(hasattr(os, 'getegid'), "test needs os.getegid()") 758 @unittest.skipUnless(hasattr(os, 'getegid'), "test needs os.getegid()")
663 def test_getgroups(self): 759 def test_getgroups(self):
664 with os.popen('id -G') as idg: 760 with os.popen('id -G') as idg:
665 groups = idg.read().strip() 761 groups = idg.read().strip()
666 ret = idg.close() 762 ret = idg.close()
667 763
668 if ret is not None or not groups: 764 if ret is not None or not groups:
669 raise unittest.SkipTest("need working 'id -G'") 765 raise unittest.SkipTest("need working 'id -G'")
766
767 # Issues 16698: OS X ABIs prior to 10.6 have limits on getgroups()
768 if sys.platform == 'darwin':
769 import sysconfig
770 dt = sysconfig.get_config_var('MACOSX_DEPLOYMENT_TARGET') or '10.0'
771 if float(dt) < 10.6:
772 raise unittest.SkipTest("getgroups(2) is broken prior to 10.6")
670 773
671 # 'id -G' and 'os.getgroups()' should return the same 774 # 'id -G' and 'os.getgroups()' should return the same
672 # groups, ignoring order and duplicates. 775 # groups, ignoring order and duplicates.
673 # #10822 - it is implementation defined whether posix.getgroups() 776 # #10822 - it is implementation defined whether posix.getgroups()
674 # includes the effective gid so we include it anyway, since id -G does 777 # includes the effective gid so we include it anyway, since id -G does
675 self.assertEqual( 778 self.assertEqual(
676 set([int(x) for x in groups.split()]), 779 set([int(x) for x in groups.split()]),
677 set(posix.getgroups() + [posix.getegid()])) 780 set(posix.getgroups() + [posix.getegid()]))
678 781
679 # tests for the posix *at functions follow 782 # tests for the posix *at functions follow
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
714 def test_stat_dir_fd(self): 817 def test_stat_dir_fd(self):
715 support.unlink(support.TESTFN) 818 support.unlink(support.TESTFN)
716 with open(support.TESTFN, 'w') as outfile: 819 with open(support.TESTFN, 'w') as outfile:
717 outfile.write("testline\n") 820 outfile.write("testline\n")
718 821
719 f = posix.open(posix.getcwd(), posix.O_RDONLY) 822 f = posix.open(posix.getcwd(), posix.O_RDONLY)
720 try: 823 try:
721 s1 = posix.stat(support.TESTFN) 824 s1 = posix.stat(support.TESTFN)
722 s2 = posix.stat(support.TESTFN, dir_fd=f) 825 s2 = posix.stat(support.TESTFN, dir_fd=f)
723 self.assertEqual(s1, s2) 826 self.assertEqual(s1, s2)
827 s2 = posix.stat(support.TESTFN, dir_fd=None)
828 self.assertEqual(s1, s2)
829 self.assertRaisesRegex(TypeError, 'should be integer, not',
830 posix.stat, support.TESTFN, dir_fd=posix.getcwd())
831 self.assertRaisesRegex(TypeError, 'should be integer, not',
832 posix.stat, support.TESTFN, dir_fd=float(f))
833 self.assertRaises(OverflowError,
834 posix.stat, support.TESTFN, dir_fd=10**20)
724 finally: 835 finally:
725 posix.close(f) 836 posix.close(f)
726 837
727 @unittest.skipUnless(os.utime in os.supports_dir_fd, "test needs dir_fd supp ort in os.utime()") 838 @unittest.skipUnless(os.utime in os.supports_dir_fd, "test needs dir_fd supp ort in os.utime()")
728 def test_utime_dir_fd(self): 839 def test_utime_dir_fd(self):
729 f = posix.open(posix.getcwd(), posix.O_RDONLY) 840 f = posix.open(posix.getcwd(), posix.O_RDONLY)
730 try: 841 try:
731 now = time.time() 842 now = time.time()
732 posix.utime(support.TESTFN, None, dir_fd=f) 843 posix.utime(support.TESTFN, None, dir_fd=f)
733 posix.utime(support.TESTFN, dir_fd=f) 844 posix.utime(support.TESTFN, dir_fd=f)
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
824 def test_rename_dir_fd(self): 935 def test_rename_dir_fd(self):
825 support.unlink(support.TESTFN) 936 support.unlink(support.TESTFN)
826 support.create_empty_file(support.TESTFN + 'ren') 937 support.create_empty_file(support.TESTFN + 'ren')
827 f = posix.open(posix.getcwd(), posix.O_RDONLY) 938 f = posix.open(posix.getcwd(), posix.O_RDONLY)
828 try: 939 try:
829 posix.rename(support.TESTFN + 'ren', support.TESTFN, src_dir_fd=f, d st_dir_fd=f) 940 posix.rename(support.TESTFN + 'ren', support.TESTFN, src_dir_fd=f, d st_dir_fd=f)
830 except: 941 except:
831 posix.rename(support.TESTFN + 'ren', support.TESTFN) 942 posix.rename(support.TESTFN + 'ren', support.TESTFN)
832 raise 943 raise
833 else: 944 else:
834 posix.stat(support.TESTFN) # should not throw exception 945 posix.stat(support.TESTFN) # should not raise exception
835 finally: 946 finally:
836 posix.close(f) 947 posix.close(f)
837 948
838 @unittest.skipUnless(os.symlink in os.supports_dir_fd, "test needs dir_fd su pport in os.symlink()") 949 @unittest.skipUnless(os.symlink in os.supports_dir_fd, "test needs dir_fd su pport in os.symlink()")
839 def test_symlink_dir_fd(self): 950 def test_symlink_dir_fd(self):
840 f = posix.open(posix.getcwd(), posix.O_RDONLY) 951 f = posix.open(posix.getcwd(), posix.O_RDONLY)
841 try: 952 try:
842 posix.symlink(support.TESTFN, support.TESTFN + 'link', dir_fd=f) 953 posix.symlink(support.TESTFN, support.TESTFN + 'link', dir_fd=f)
843 self.assertEqual(posix.readlink(support.TESTFN + 'link'), support.TE STFN) 954 self.assertEqual(posix.readlink(support.TESTFN + 'link'), support.TE STFN)
844 finally: 955 finally:
845 posix.close(f) 956 posix.close(f)
846 support.unlink(support.TESTFN + 'link') 957 support.unlink(support.TESTFN + 'link')
847 958
848 @unittest.skipUnless(os.unlink in os.supports_dir_fd, "test needs dir_fd sup port in os.unlink()") 959 @unittest.skipUnless(os.unlink in os.supports_dir_fd, "test needs dir_fd sup port in os.unlink()")
849 def test_unlink_dir_fd(self): 960 def test_unlink_dir_fd(self):
850 f = posix.open(posix.getcwd(), posix.O_RDONLY) 961 f = posix.open(posix.getcwd(), posix.O_RDONLY)
851 support.create_empty_file(support.TESTFN + 'del') 962 support.create_empty_file(support.TESTFN + 'del')
852 posix.stat(support.TESTFN + 'del') # should not throw exception 963 posix.stat(support.TESTFN + 'del') # should not raise exception
853 try: 964 try:
854 posix.unlink(support.TESTFN + 'del', dir_fd=f) 965 posix.unlink(support.TESTFN + 'del', dir_fd=f)
855 except: 966 except:
856 support.unlink(support.TESTFN + 'del') 967 support.unlink(support.TESTFN + 'del')
857 raise 968 raise
858 else: 969 else:
859 self.assertRaises(OSError, posix.stat, support.TESTFN + 'link') 970 self.assertRaises(OSError, posix.stat, support.TESTFN + 'link')
860 finally: 971 finally:
861 posix.close(f) 972 posix.close(f)
862 973
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
906 parent = posix.sched_getscheduler(os.getppid()) 1017 parent = posix.sched_getscheduler(os.getppid())
907 except OSError as e: 1018 except OSError as e:
908 if e.errno != errno.EPERM: 1019 if e.errno != errno.EPERM:
909 raise 1020 raise
910 else: 1021 else:
911 self.assertIn(parent, possible_schedulers) 1022 self.assertIn(parent, possible_schedulers)
912 self.assertRaises(OSError, posix.sched_getscheduler, -1) 1023 self.assertRaises(OSError, posix.sched_getscheduler, -1)
913 self.assertRaises(OSError, posix.sched_getparam, -1) 1024 self.assertRaises(OSError, posix.sched_getparam, -1)
914 param = posix.sched_getparam(0) 1025 param = posix.sched_getparam(0)
915 self.assertIsInstance(param.sched_priority, int) 1026 self.assertIsInstance(param.sched_priority, int)
916 try: 1027
917 posix.sched_setscheduler(0, mine, param) 1028 # POSIX states that calling sched_setparam() or sched_setscheduler() on
918 except OSError as e: 1029 # a process with a scheduling policy other than SCHED_FIFO or SCHED_RR
919 if e.errno != errno.EPERM: 1030 # is implementation-defined: NetBSD and FreeBSD can return EINVAL.
920 raise 1031 if not sys.platform.startswith(('freebsd', 'netbsd')):
921 1032 try:
922 # POSIX states that calling sched_setparam() on a process with a 1033 posix.sched_setscheduler(0, mine, param)
923 # scheduling policy other than SCHED_FIFO or SCHED_RR is 1034 posix.sched_setparam(0, param)
924 # implementation-defined: FreeBSD returns EINVAL. 1035 except OSError as e:
925 if not sys.platform.startswith('freebsd'): 1036 if e.errno != errno.EPERM:
926 posix.sched_setparam(0, param) 1037 raise
927 self.assertRaises(OSError, posix.sched_setparam, -1, param) 1038 self.assertRaises(OSError, posix.sched_setparam, -1, param)
928 1039
929 self.assertRaises(OSError, posix.sched_setscheduler, -1, mine, param) 1040 self.assertRaises(OSError, posix.sched_setscheduler, -1, mine, param)
930 self.assertRaises(TypeError, posix.sched_setscheduler, 0, mine, None) 1041 self.assertRaises(TypeError, posix.sched_setscheduler, 0, mine, None)
931 self.assertRaises(TypeError, posix.sched_setparam, 0, 43) 1042 self.assertRaises(TypeError, posix.sched_setparam, 0, 43)
932 param = posix.sched_param(None) 1043 param = posix.sched_param(None)
933 self.assertRaises(TypeError, posix.sched_setparam, 0, param) 1044 self.assertRaises(TypeError, posix.sched_setparam, 0, param)
934 large = 214748364700 1045 large = 214748364700
935 param = posix.sched_param(large) 1046 param = posix.sched_param(large)
936 self.assertRaises(OverflowError, posix.sched_setparam, 0, param) 1047 self.assertRaises(OverflowError, posix.sched_setparam, 0, param)
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
1046 self.assertListEqual(groups, posix.getgroups()) 1157 self.assertListEqual(groups, posix.getgroups())
1047 1158
1048 def test_main(): 1159 def test_main():
1049 try: 1160 try:
1050 support.run_unittest(PosixTester, PosixGroupsTester) 1161 support.run_unittest(PosixTester, PosixGroupsTester)
1051 finally: 1162 finally:
1052 support.reap_children() 1163 support.reap_children()
1053 1164
1054 if __name__ == '__main__': 1165 if __name__ == '__main__':
1055 test_main() 1166 test_main()
LEFTRIGHT

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