Debian Codegraph @

Lib/test/test_os.py

raw
1 # As a test suite for the os module, this is woefully inadequate, but this
2 # does add tests for a few functions which have been determined to be more
3 # portable than they had been thought to be.
4
5 import asyncio
6 import codecs
7 import contextlib
8 import decimal
9 import errno
10 import fnmatch
11 import fractions
12 import itertools
13 import locale
14 import os
15 import pickle
16 import select
17 import selectors
18 import shutil
19 import signal
20 import socket
21 import stat
22 import struct
23 import subprocess
24 import sys
25 import sysconfig
26 import tempfile
27 import textwrap
28 import time
29 import types
30 import unittest
31 import uuid
32 import warnings
33 from test import support
34 from test.support import import_helper
35 from test.support import os_helper
36 from test.support import socket_helper
37 from test.support import infinite_recursion
38 from test.support import warnings_helper
39 from platform import win32_is_iot
40
41 try:
42 import resource
43 except ImportError:
44 resource = None
45 try:
46 import fcntl
47 except ImportError:
48 fcntl = None
49 try:
50 import _winapi
51 except ImportError:
52 _winapi = None
53 try:
54 import pwd
55 all_users = [u.pw_uid for u in pwd.getpwall()]
56 except (ImportError, AttributeError):
57 all_users = []
58 try:
59 import _testcapi
60 from _testcapi import INT_MAX, PY_SSIZE_T_MAX
61 except ImportError:
62 _testcapi = None
63 INT_MAX = PY_SSIZE_T_MAX = sys.maxsize
64
65 try:
66 import mmap
67 except ImportError:
68 mmap = None
69
70 from test.support.script_helper import assert_python_ok
71 from test.support import unix_shell
72 from test.support.os_helper import FakePath
73
74
75 root_in_posix = False
76 if hasattr(os, 'geteuid'):
77 root_in_posix = (os.geteuid() == 0)
78
79 # Detect whether we're on a Linux system that uses the (now outdated
80 # and unmaintained) linuxthreads threading library. There's an issue
81 # when combining linuxthreads with a failed execv call: see
82 # http://bugs.python.org/issue4970.
83 if hasattr(sys, 'thread_info') and sys.thread_info.version:
84 USING_LINUXTHREADS = sys.thread_info.version.startswith("linuxthreads")
85 else:
86 USING_LINUXTHREADS = False
87
88 # Issue #14110: Some tests fail on FreeBSD if the user is in the wheel group.
89 HAVE_WHEEL_GROUP = sys.platform.startswith('freebsd') and os.getgid() == 0
90
91
92 def requires_os_func(name):
93 return unittest.skipUnless(hasattr(os, name), 'requires os.%s' % name)
94
95
96 def create_file(filename, content=b'content'):
97 with open(filename, "xb", 0) as fp:
98 fp.write(content)
99
100
101 # bpo-41625: On AIX, splice() only works with a socket, not with a pipe.
102 requires_splice_pipe = unittest.skipIf(sys.platform.startswith("aix"),
103 'on AIX, splice() only accepts sockets')
104
105
106 def tearDownModule():
107 asyncio.events._set_event_loop_policy(None)
108
109
110 class MiscTests(unittest.TestCase):
111 def test_getcwd(self):
112 cwd = os.getcwd()
113 self.assertIsInstance(cwd, str)
114
115 def test_getcwd_long_path(self):
116 # bpo-37412: On Linux, PATH_MAX is usually around 4096 bytes. On
117 # Windows, MAX_PATH is defined as 260 characters, but Windows supports
118 # longer path if longer paths support is enabled. Internally, the os
119 # module uses MAXPATHLEN which is at least 1024.
120 #
121 # Use a directory name of 200 characters to fit into Windows MAX_PATH
122 # limit.
123 #
124 # On Windows, the test can stop when trying to create a path longer
125 # than MAX_PATH if long paths support is disabled:
126 # see RtlAreLongPathsEnabled().
127 min_len = 2000 # characters
128 # On VxWorks, PATH_MAX is defined as 1024 bytes. Creating a path
129 # longer than PATH_MAX will fail.
130 if sys.platform == 'vxworks':
131 min_len = 1000
132 dirlen = 200 # characters
133 dirname = 'python_test_dir_'
134 dirname = dirname + ('a' * (dirlen - len(dirname)))
135
136 with tempfile.TemporaryDirectory() as tmpdir:
137 with os_helper.change_cwd(tmpdir) as path:
138 expected = path
139
140 while True:
141 cwd = os.getcwd()
142 self.assertEqual(cwd, expected)
143
144 need = min_len - (len(cwd) + len(os.path.sep))
145 if need <= 0:
146 break
147 if len(dirname) > need and need > 0:
148 dirname = dirname[:need]
149
150 path = os.path.join(path, dirname)
151 try:
152 os.mkdir(path)
153 # On Windows, chdir() can fail
154 # even if mkdir() succeeded
155 os.chdir(path)
156 except FileNotFoundError:
157 # On Windows, catch ERROR_PATH_NOT_FOUND (3) and
158 # ERROR_FILENAME_EXCED_RANGE (206) errors
159 # ("The filename or extension is too long")
160 break
161 except OSError as exc:
162 if exc.errno == errno.ENAMETOOLONG:
163 break
164 else:
165 raise
166
167 expected = path
168
169 if support.verbose:
170 print(f"Tested current directory length: {len(cwd)}")
171
172 def test_getcwdb(self):
173 cwd = os.getcwdb()
174 self.assertIsInstance(cwd, bytes)
175 self.assertEqual(os.fsdecode(cwd), os.getcwd())
176
177
178 # Tests creating TESTFN
179 class FileTests(unittest.TestCase):
180 def setUp(self):
181 if os.path.lexists(os_helper.TESTFN):
182 os.unlink(os_helper.TESTFN)
183 tearDown = setUp
184
185 def test_access(self):
186 f = os.open(os_helper.TESTFN, os.O_CREAT|os.O_RDWR)
187 os.close(f)
188 self.assertTrue(os.access(os_helper.TESTFN, os.W_OK))
189
190 @unittest.skipIf(
191 support.is_wasi, "WASI does not support dup."
192 )
193 def test_closerange(self):
194 first = os.open(os_helper.TESTFN, os.O_CREAT|os.O_RDWR)
195 # We must allocate two consecutive file descriptors, otherwise
196 # it will mess up other file descriptors (perhaps even the three
197 # standard ones).
198 second = os.dup(first)
199 try:
200 retries = 0
201 while second != first + 1:
202 os.close(first)
203 retries += 1
204 if retries > 10:
205 # XXX test skipped
206 self.skipTest("couldn't allocate two consecutive fds")
207 first, second = second, os.dup(second)
208 finally:
209 os.close(second)
210 # close a fd that is open, and one that isn't
211 os.closerange(first, first + 2)
212 self.assertRaises(OSError, os.write, first, b"a")
213
214 @support.cpython_only
215 def test_rename(self):
216 path = os_helper.TESTFN
217 old = sys.getrefcount(path)
218 self.assertRaises(TypeError, os.rename, path, 0)
219 new = sys.getrefcount(path)
220 self.assertEqual(old, new)
221
222 def test_read(self):
223 with open(os_helper.TESTFN, "w+b") as fobj:
224 fobj.write(b"spam")
225 fobj.flush()
226 fd = fobj.fileno()
227 os.lseek(fd, 0, 0)
228 s = os.read(fd, 4)
229 self.assertEqual(type(s), bytes)
230 self.assertEqual(s, b"spam")
231
232 def test_readinto(self):
233 with open(os_helper.TESTFN, "w+b") as fobj:
234 fobj.write(b"spam")
235 fobj.flush()
236 fd = fobj.fileno()
237 os.lseek(fd, 0, 0)
238 # Oversized so readinto without hitting end.
239 buffer = bytearray(7)
240 s = os.readinto(fd, buffer)
241 self.assertEqual(type(s), int)
242 self.assertEqual(s, 4)
243 # Should overwrite the first 4 bytes of the buffer.
244 self.assertEqual(buffer[:4], b"spam")
245
246 # Readinto at EOF should return 0 and not touch buffer.
247 buffer[:] = b"notspam"
248 s = os.readinto(fd, buffer)
249 self.assertEqual(type(s), int)
250 self.assertEqual(s, 0)
251 self.assertEqual(bytes(buffer), b"notspam")
252 s = os.readinto(fd, buffer)
253 self.assertEqual(s, 0)
254 self.assertEqual(bytes(buffer), b"notspam")
255
256 # Readinto a 0 length bytearray when at EOF should return 0
257 self.assertEqual(os.readinto(fd, bytearray()), 0)
258
259 # Readinto a 0 length bytearray with data available should return 0.
260 os.lseek(fd, 0, 0)
261 self.assertEqual(os.readinto(fd, bytearray()), 0)
262
263 @unittest.skipUnless(hasattr(os, 'get_blocking'),
264 'needs os.get_blocking() and os.set_blocking()')
265 @unittest.skipUnless(hasattr(os, "pipe"), "requires os.pipe()")
266 @unittest.skipIf(support.is_emscripten, "set_blocking does not work correctly")
267 def test_readinto_non_blocking(self):
268 # Verify behavior of a readinto which would block on a non-blocking fd.
269 r, w = os.pipe()
270 try:
271 os.set_blocking(r, False)
272 with self.assertRaises(BlockingIOError):
273 os.readinto(r, bytearray(5))
274
275 # Pass some data through
276 os.write(w, b"spam")
277 self.assertEqual(os.readinto(r, bytearray(4)), 4)
278
279 # Still don't block or return 0.
280 with self.assertRaises(BlockingIOError):
281 os.readinto(r, bytearray(5))
282
283 # At EOF should return size 0
284 os.close(w)
285 w = None
286 self.assertEqual(os.readinto(r, bytearray(5)), 0)
287 self.assertEqual(os.readinto(r, bytearray(5)), 0) # Still EOF
288
289 finally:
290 os.close(r)
291 if w is not None:
292 os.close(w)
293
294 def test_readinto_badarg(self):
295 with open(os_helper.TESTFN, "w+b") as fobj:
296 fobj.write(b"spam")
297 fobj.flush()
298 fd = fobj.fileno()
299 os.lseek(fd, 0, 0)
300
301 for bad_arg in ("test", bytes(), 14):
302 with self.subTest(f"bad buffer {type(bad_arg)}"):
303 with self.assertRaises(TypeError):
304 os.readinto(fd, bad_arg)
305
306 with self.subTest("doesn't work on file objects"):
307 with self.assertRaises(TypeError):
308 os.readinto(fobj, bytearray(5))
309
310 # takes two args
311 with self.assertRaises(TypeError):
312 os.readinto(fd)
313
314 # No data should have been read with the bad arguments.
315 buffer = bytearray(4)
316 s = os.readinto(fd, buffer)
317 self.assertEqual(s, 4)
318 self.assertEqual(buffer, b"spam")
319
320 @support.cpython_only
321 # Skip the test on 32-bit platforms: the number of bytes must fit in a
322 # Py_ssize_t type
323 @unittest.skipUnless(INT_MAX < PY_SSIZE_T_MAX,
324 "needs INT_MAX < PY_SSIZE_T_MAX")
325 @support.bigmemtest(size=INT_MAX + 10, memuse=1, dry_run=False)
326 def test_large_read(self, size):
327 self.addCleanup(os_helper.unlink, os_helper.TESTFN)
328 create_file(os_helper.TESTFN, b'test')
329
330 # Issue #21932: Make sure that os.read() does not raise an
331 # OverflowError for size larger than INT_MAX
332 with open(os_helper.TESTFN, "rb") as fp:
333 data = os.read(fp.fileno(), size)
334
335 # The test does not try to read more than 2 GiB at once because the
336 # operating system is free to return less bytes than requested.
337 self.assertEqual(data, b'test')
338
339
340 @support.cpython_only
341 # Skip the test on 32-bit platforms: the number of bytes must fit in a
342 # Py_ssize_t type
343 @unittest.skipUnless(INT_MAX < PY_SSIZE_T_MAX,
344 "needs INT_MAX < PY_SSIZE_T_MAX")
345 @support.bigmemtest(size=INT_MAX + 10, memuse=1, dry_run=False)
346 def test_large_readinto(self, size):
347 self.addCleanup(os_helper.unlink, os_helper.TESTFN)
348 create_file(os_helper.TESTFN, b'test')
349
350 # Issue #21932: For readinto the buffer contains the length rather than
351 # a length being passed explicitly to read, should still get capped to a
352 # valid size / not raise an OverflowError for sizes larger than INT_MAX.
353 buffer = bytearray(INT_MAX + 10)
354 with open(os_helper.TESTFN, "rb") as fp:
355 length = os.readinto(fp.fileno(), buffer)
356
357 # The test does not try to read more than 2 GiB at once because the
358 # operating system is free to return less bytes than requested.
359 self.assertEqual(length, 4)
360 self.assertEqual(buffer[:4], b'test')
361
362 def test_write(self):
363 # os.write() accepts bytes- and buffer-like objects but not strings
364 fd = os.open(os_helper.TESTFN, os.O_CREAT | os.O_WRONLY)
365 self.assertRaises(TypeError, os.write, fd, "beans")
366 os.write(fd, b"bacon\n")
367 os.write(fd, bytearray(b"eggs\n"))
368 os.write(fd, memoryview(b"spam\n"))
369 os.close(fd)
370 with open(os_helper.TESTFN, "rb") as fobj:
371 self.assertEqual(fobj.read().splitlines(),
372 [b"bacon", b"eggs", b"spam"])
373
374 def write_windows_console(self, *args):
375 retcode = subprocess.call(args,
376 # use a new console to not flood the test output
377 creationflags=subprocess.CREATE_NEW_CONSOLE,
378 # use a shell to hide the console window (SW_HIDE)
379 shell=True)
380 self.assertEqual(retcode, 0)
381
382 @unittest.skipUnless(sys.platform == 'win32',
383 'test specific to the Windows console')
384 def test_write_windows_console(self):
385 # Issue #11395: the Windows console returns an error (12: not enough
386 # space error) on writing into stdout if stdout mode is binary and the
387 # length is greater than 66,000 bytes (or less, depending on heap
388 # usage).
389 code = "print('x' * 100000)"
390 self.write_windows_console(sys.executable, "-c", code)
391 self.write_windows_console(sys.executable, "-u", "-c", code)
392
393 def fdopen_helper(self, *args):
394 fd = os.open(os_helper.TESTFN, os.O_RDONLY)
395 f = os.fdopen(fd, *args, encoding="utf-8")
396 f.close()
397
398 def test_fdopen(self):
399 fd = os.open(os_helper.TESTFN, os.O_CREAT|os.O_RDWR)
400 os.close(fd)
401
402 self.fdopen_helper()
403 self.fdopen_helper('r')
404 self.fdopen_helper('r', 100)
405
406 def test_replace(self):
407 TESTFN2 = os_helper.TESTFN + ".2"
408 self.addCleanup(os_helper.unlink, os_helper.TESTFN)
409 self.addCleanup(os_helper.unlink, TESTFN2)
410
411 create_file(os_helper.TESTFN, b"1")
412 create_file(TESTFN2, b"2")
413
414 os.replace(os_helper.TESTFN, TESTFN2)
415 self.assertRaises(FileNotFoundError, os.stat, os_helper.TESTFN)
416 with open(TESTFN2, 'r', encoding='utf-8') as f:
417 self.assertEqual(f.read(), "1")
418
419 def test_open_keywords(self):
420 f = os.open(path=__file__, flags=os.O_RDONLY, mode=0o777,
421 dir_fd=None)
422 os.close(f)
423
424 def test_symlink_keywords(self):
425 symlink = support.get_attribute(os, "symlink")
426 try:
427 symlink(src='target', dst=os_helper.TESTFN,
428 target_is_directory=False, dir_fd=None)
429 except (NotImplementedError, OSError):
430 pass # No OS support or unprivileged user
431
432 @unittest.skipUnless(hasattr(os, 'copy_file_range'), 'test needs os.copy_file_range()')
433 def test_copy_file_range_invalid_values(self):
434 with self.assertRaises(ValueError):
435 os.copy_file_range(0, 1, -10)
436
437 @unittest.skipUnless(hasattr(os, 'copy_file_range'), 'test needs os.copy_file_range()')
438 def test_copy_file_range(self):
439 TESTFN2 = os_helper.TESTFN + ".3"
440 data = b'0123456789'
441
442 create_file(os_helper.TESTFN, data)
443 self.addCleanup(os_helper.unlink, os_helper.TESTFN)
444
445 in_file = open(os_helper.TESTFN, 'rb')
446 self.addCleanup(in_file.close)
447 in_fd = in_file.fileno()
448
449 out_file = open(TESTFN2, 'w+b')
450 self.addCleanup(os_helper.unlink, TESTFN2)
451 self.addCleanup(out_file.close)
452 out_fd = out_file.fileno()
453
454 try:
455 i = os.copy_file_range(in_fd, out_fd, 5)
456 except OSError as e:
457 # Handle the case in which Python was compiled
458 # in a system with the syscall but without support
459 # in the kernel.
460 if e.errno != errno.ENOSYS:
461 raise
462 self.skipTest(e)
463 else:
464 # The number of copied bytes can be less than
465 # the number of bytes originally requested.
466 self.assertIn(i, range(0, 6));
467
468 with open(TESTFN2, 'rb') as in_file:
469 self.assertEqual(in_file.read(), data[:i])
470
471 @unittest.skipUnless(hasattr(os, 'copy_file_range'), 'test needs os.copy_file_range()')
472 def test_copy_file_range_offset(self):
473 TESTFN4 = os_helper.TESTFN + ".4"
474 data = b'0123456789'
475 bytes_to_copy = 6
476 in_skip = 3
477 out_seek = 5
478
479 create_file(os_helper.TESTFN, data)
480 self.addCleanup(os_helper.unlink, os_helper.TESTFN)
481
482 in_file = open(os_helper.TESTFN, 'rb')
483 self.addCleanup(in_file.close)
484 in_fd = in_file.fileno()
485
486 out_file = open(TESTFN4, 'w+b')
487 self.addCleanup(os_helper.unlink, TESTFN4)
488 self.addCleanup(out_file.close)
489 out_fd = out_file.fileno()
490
491 try:
492 i = os.copy_file_range(in_fd, out_fd, bytes_to_copy,
493 offset_src=in_skip,
494 offset_dst=out_seek)
495 except OSError as e:
496 # Handle the case in which Python was compiled
497 # in a system with the syscall but without support
498 # in the kernel.
499 if e.errno != errno.ENOSYS:
500 raise
501 self.skipTest(e)
502 else:
503 # The number of copied bytes can be less than
504 # the number of bytes originally requested.
505 self.assertIn(i, range(0, bytes_to_copy+1));
506
507 with open(TESTFN4, 'rb') as in_file:
508 read = in_file.read()
509 # seeked bytes (5) are zero'ed
510 self.assertEqual(read[:out_seek], b'\x00'*out_seek)
511 # 012 are skipped (in_skip)
512 # 345678 are copied in the file (in_skip + bytes_to_copy)
513 self.assertEqual(read[out_seek:],
514 data[in_skip:in_skip+i])
515
516 @unittest.skipUnless(hasattr(os, 'splice'), 'test needs os.splice()')
517 def test_splice_invalid_values(self):
518 with self.assertRaises(ValueError):
519 os.splice(0, 1, -10)
520
521 @unittest.skipUnless(hasattr(os, 'splice'), 'test needs os.splice()')
522 @requires_splice_pipe
523 def test_splice(self):
524 TESTFN2 = os_helper.TESTFN + ".3"
525 data = b'0123456789'
526
527 create_file(os_helper.TESTFN, data)
528 self.addCleanup(os_helper.unlink, os_helper.TESTFN)
529
530 in_file = open(os_helper.TESTFN, 'rb')
531 self.addCleanup(in_file.close)
532 in_fd = in_file.fileno()
533
534 read_fd, write_fd = os.pipe()
535 self.addCleanup(lambda: os.close(read_fd))
536 self.addCleanup(lambda: os.close(write_fd))
537
538 try:
539 i = os.splice(in_fd, write_fd, 5)
540 except OSError as e:
541 # Handle the case in which Python was compiled
542 # in a system with the syscall but without support
543 # in the kernel.
544 if e.errno != errno.ENOSYS:
545 raise
546 self.skipTest(e)
547 else:
548 # The number of copied bytes can be less than
549 # the number of bytes originally requested.
550 self.assertIn(i, range(0, 6));
551
552 self.assertEqual(os.read(read_fd, 100), data[:i])
553
554 @unittest.skipUnless(hasattr(os, 'splice'), 'test needs os.splice()')
555 @requires_splice_pipe
556 def test_splice_offset_in(self):
557 TESTFN4 = os_helper.TESTFN + ".4"
558 data = b'0123456789'
559 bytes_to_copy = 6
560 in_skip = 3
561
562 create_file(os_helper.TESTFN, data)
563 self.addCleanup(os_helper.unlink, os_helper.TESTFN)
564
565 in_file = open(os_helper.TESTFN, 'rb')
566 self.addCleanup(in_file.close)
567 in_fd = in_file.fileno()
568
569 read_fd, write_fd = os.pipe()
570 self.addCleanup(lambda: os.close(read_fd))
571 self.addCleanup(lambda: os.close(write_fd))
572
573 try:
574 i = os.splice(in_fd, write_fd, bytes_to_copy, offset_src=in_skip)
575 except OSError as e:
576 # Handle the case in which Python was compiled
577 # in a system with the syscall but without support
578 # in the kernel.
579 if e.errno != errno.ENOSYS:
580 raise
581 self.skipTest(e)
582 else:
583 # The number of copied bytes can be less than
584 # the number of bytes originally requested.
585 self.assertIn(i, range(0, bytes_to_copy+1));
586
587 read = os.read(read_fd, 100)
588 # 012 are skipped (in_skip)
589 # 345678 are copied in the file (in_skip + bytes_to_copy)
590 self.assertEqual(read, data[in_skip:in_skip+i])
591
592 @unittest.skipUnless(hasattr(os, 'splice'), 'test needs os.splice()')
593 @requires_splice_pipe
594 def test_splice_offset_out(self):
595 TESTFN4 = os_helper.TESTFN + ".4"
596 data = b'0123456789'
597 bytes_to_copy = 6
598 out_seek = 3
599
600 create_file(os_helper.TESTFN, data)
601 self.addCleanup(os_helper.unlink, os_helper.TESTFN)
602
603 read_fd, write_fd = os.pipe()
604 self.addCleanup(lambda: os.close(read_fd))
605 self.addCleanup(lambda: os.close(write_fd))
606 os.write(write_fd, data)
607
608 out_file = open(TESTFN4, 'w+b')
609 self.addCleanup(os_helper.unlink, TESTFN4)
610 self.addCleanup(out_file.close)
611 out_fd = out_file.fileno()
612
613 try:
614 i = os.splice(read_fd, out_fd, bytes_to_copy, offset_dst=out_seek)
615 except OSError as e:
616 # Handle the case in which Python was compiled
617 # in a system with the syscall but without support
618 # in the kernel.
619 if e.errno != errno.ENOSYS:
620 raise
621 self.skipTest(e)
622 else:
623 # The number of copied bytes can be less than
624 # the number of bytes originally requested.
625 self.assertIn(i, range(0, bytes_to_copy+1));
626
627 with open(TESTFN4, 'rb') as in_file:
628 read = in_file.read()
629 # seeked bytes (5) are zero'ed
630 self.assertEqual(read[:out_seek], b'\x00'*out_seek)
631 # 012 are skipped (in_skip)
632 # 345678 are copied in the file (in_skip + bytes_to_copy)
633 self.assertEqual(read[out_seek:], data[:i])
634
635
636 # Test attributes on return values from os.*stat* family.
637 class StatAttributeTests(unittest.TestCase):
638 def setUp(self):
639 self.fname = os_helper.TESTFN
640 self.addCleanup(os_helper.unlink, self.fname)
641 create_file(self.fname, b"ABC")
642
643 def check_stat_attributes(self, fname):
644 result = os.stat(fname)
645
646 # Make sure direct access works
647 self.assertEqual(result[stat.ST_SIZE], 3)
648 self.assertEqual(result.st_size, 3)
649
650 # Make sure all the attributes are there
651 members = dir(result)
652 for name in dir(stat):
653 if name[:3] == 'ST_':
654 attr = name.lower()
655 if name.endswith("TIME"):
656 def trunc(x): return int(x)
657 else:
658 def trunc(x): return x
659 self.assertEqual(trunc(getattr(result, attr)),
660 result[getattr(stat, name)])
661 self.assertIn(attr, members)
662
663 # Make sure that the st_?time and st_?time_ns fields roughly agree
664 # (they should always agree up to around tens-of-microseconds)
665 for name in 'st_atime st_mtime st_ctime'.split():
666 floaty = int(getattr(result, name) * 100000)
667 nanosecondy = getattr(result, name + "_ns") // 10000
668 self.assertAlmostEqual(floaty, nanosecondy, delta=2)
669
670 # Ensure both birthtime and birthtime_ns roughly agree, if present
671 try:
672 floaty = int(result.st_birthtime * 100000)
673 nanosecondy = result.st_birthtime_ns // 10000
674 except AttributeError:
675 pass
676 else:
677 self.assertAlmostEqual(floaty, nanosecondy, delta=2)
678
679 try:
680 result[200]
681 self.fail("No exception raised")
682 except IndexError:
683 pass
684
685 # Make sure that assignment fails
686 try:
687 result.st_mode = 1
688 self.fail("No exception raised")
689 except AttributeError:
690 pass
691
692 try:
693 result.st_rdev = 1
694 self.fail("No exception raised")
695 except (AttributeError, TypeError):
696 pass
697
698 try:
699 result.parrot = 1
700 self.fail("No exception raised")
701 except AttributeError:
702 pass
703
704 # Use the stat_result constructor with a too-short tuple.
705 try:
706 result2 = os.stat_result((10,))
707 self.fail("No exception raised")
708 except TypeError:
709 pass
710
711 # Use the constructor with a too-long tuple.
712 try:
713 result2 = os.stat_result((0,1,2,3,4,5,6,7,8,9,10,11,12,13,14))
714 except TypeError:
715 pass
716
717 def test_stat_attributes(self):
718 self.check_stat_attributes(self.fname)
719
720 def test_stat_attributes_bytes(self):
721 try:
722 fname = self.fname.encode(sys.getfilesystemencoding())
723 except UnicodeEncodeError:
724 self.skipTest("cannot encode %a for the filesystem" % self.fname)
725 self.check_stat_attributes(fname)
726
727 def test_stat_result_pickle(self):
728 result = os.stat(self.fname)
729 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
730 with self.subTest(f'protocol {proto}'):
731 p = pickle.dumps(result, proto)
732 self.assertIn(b'stat_result', p)
733 if proto < 4:
734 self.assertIn(b'cos\nstat_result\n', p)
735 unpickled = pickle.loads(p)
736 self.assertEqual(result, unpickled)
737
738 @unittest.skipUnless(hasattr(os, 'statvfs'), 'test needs os.statvfs()')
739 def test_statvfs_attributes(self):
740 result = os.statvfs(self.fname)
741
742 # Make sure direct access works
743 self.assertEqual(result.f_bfree, result[3])
744
745 # Make sure all the attributes are there.
746 members = ('bsize', 'frsize', 'blocks', 'bfree', 'bavail', 'files',
747 'ffree', 'favail', 'flag', 'namemax')
748 for value, member in enumerate(members):
749 self.assertEqual(getattr(result, 'f_' + member), result[value])
750
751 self.assertTrue(isinstance(result.f_fsid, int))
752
753 # Test that the size of the tuple doesn't change
754 self.assertEqual(len(result), 10)
755
756 # Make sure that assignment really fails
757 try:
758 result.f_bfree = 1
759 self.fail("No exception raised")
760 except AttributeError:
761 pass
762
763 try:
764 result.parrot = 1
765 self.fail("No exception raised")
766 except AttributeError:
767 pass
768
769 # Use the constructor with a too-short tuple.
770 try:
771 result2 = os.statvfs_result((10,))
772 self.fail("No exception raised")
773 except TypeError:
774 pass
775
776 # Use the constructor with a too-long tuple.
777 try:
778 result2 = os.statvfs_result((0,1,2,3,4,5,6,7,8,9,10,11,12,13,14))
779 except TypeError:
780 pass
781
782 @unittest.skipUnless(hasattr(os, 'statvfs'),
783 "need os.statvfs()")
784 def test_statvfs_result_pickle(self):
785 result = os.statvfs(self.fname)
786
787 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
788 p = pickle.dumps(result, proto)
789 self.assertIn(b'statvfs_result', p)
790 if proto < 4:
791 self.assertIn(b'cos\nstatvfs_result\n', p)
792 unpickled = pickle.loads(p)
793 self.assertEqual(result, unpickled)
794
795 @unittest.skipUnless(sys.platform == "win32", "Win32 specific tests")
796 def test_1686475(self):
797 # Verify that an open file can be stat'ed
798 try:
799 os.stat(r"c:\pagefile.sys")
800 except FileNotFoundError:
801 self.skipTest(r'c:\pagefile.sys does not exist')
802 except OSError as e:
803 self.fail("Could not stat pagefile.sys")
804
805 @unittest.skipUnless(sys.platform == "win32", "Win32 specific tests")
806 @unittest.skipUnless(hasattr(os, "pipe"), "requires os.pipe()")
807 def test_15261(self):
808 # Verify that stat'ing a closed fd does not cause crash
809 r, w = os.pipe()
810 try:
811 os.stat(r) # should not raise error
812 finally:
813 os.close(r)
814 os.close(w)
815 with self.assertRaises(OSError) as ctx:
816 os.stat(r)
817 self.assertEqual(ctx.exception.errno, errno.EBADF)
818
819 def check_file_attributes(self, result):
820 self.assertHasAttr(result, 'st_file_attributes')
821 self.assertTrue(isinstance(result.st_file_attributes, int))
822 self.assertTrue(0 <= result.st_file_attributes <= 0xFFFFFFFF)
823
824 @unittest.skipUnless(sys.platform == "win32",
825 "st_file_attributes is Win32 specific")
826 def test_file_attributes(self):
827 # test file st_file_attributes (FILE_ATTRIBUTE_DIRECTORY not set)
828 result = os.stat(self.fname)
829 self.check_file_attributes(result)
830 self.assertEqual(
831 result.st_file_attributes & stat.FILE_ATTRIBUTE_DIRECTORY,
832 0)
833
834 # test directory st_file_attributes (FILE_ATTRIBUTE_DIRECTORY set)
835 dirname = os_helper.TESTFN + "dir"
836 os.mkdir(dirname)
837 self.addCleanup(os.rmdir, dirname)
838
839 result = os.stat(dirname)
840 self.check_file_attributes(result)
841 self.assertEqual(
842 result.st_file_attributes & stat.FILE_ATTRIBUTE_DIRECTORY,
843 stat.FILE_ATTRIBUTE_DIRECTORY)
844
845 @unittest.skipUnless(sys.platform == "win32", "Win32 specific tests")
846 def test_access_denied(self):
847 # Default to FindFirstFile WIN32_FIND_DATA when access is
848 # denied. See issue 28075.
849 # os.environ['TEMP'] should be located on a volume that
850 # supports file ACLs.
851 fname = os.path.join(os.environ['TEMP'], self.fname + "_access")
852 self.addCleanup(os_helper.unlink, fname)
853 create_file(fname, b'ABC')
854 # Deny the right to [S]YNCHRONIZE on the file to
855 # force CreateFile to fail with ERROR_ACCESS_DENIED.
856 DETACHED_PROCESS = 8
857 subprocess.check_call(
858 # bpo-30584: Use security identifier *S-1-5-32-545 instead
859 # of localized "Users" to not depend on the locale.
860 ['icacls.exe', fname, '/deny', '*S-1-5-32-545:(S)'],
861 creationflags=DETACHED_PROCESS
862 )
863 result = os.stat(fname)
864 self.assertNotEqual(result.st_size, 0)
865 self.assertTrue(os.path.isfile(fname))
866
867 @unittest.skipUnless(sys.platform == "win32", "Win32 specific tests")
868 def test_stat_block_device(self):
869 # bpo-38030: os.stat fails for block devices
870 # Test a filename like "//./C:"
871 fname = "//./" + os.path.splitdrive(os.getcwd())[0]
872 result = os.stat(fname)
873 self.assertEqual(result.st_mode, stat.S_IFBLK)
874
875
876 class UtimeTests(unittest.TestCase):
877 def setUp(self):
878 self.dirname = os_helper.TESTFN
879 self.fname = os.path.join(self.dirname, "f1")
880
881 self.addCleanup(os_helper.rmtree, self.dirname)
882 os.mkdir(self.dirname)
883 create_file(self.fname)
884
885 def support_subsecond(self, filename):
886 # Heuristic to check if the filesystem supports timestamp with
887 # subsecond resolution: check if float and int timestamps are different
888 st = os.stat(filename)
889 return ((st.st_atime != st[7])
890 or (st.st_mtime != st[8])
891 or (st.st_ctime != st[9]))
892
893 def _test_utime(self, set_time, filename=None):
894 if not filename:
895 filename = self.fname
896
897 support_subsecond = self.support_subsecond(filename)
898 if support_subsecond:
899 # Timestamp with a resolution of 1 microsecond (10^-6).
900 #
901 # The resolution of the C internal function used by os.utime()
902 # depends on the platform: 1 sec, 1 us, 1 ns. Writing a portable
903 # test with a resolution of 1 ns requires more work:
904 # see the issue #15745.
905 atime_ns = 1002003000 # 1.002003 seconds
906 mtime_ns = 4005006000 # 4.005006 seconds
907 else:
908 # use a resolution of 1 second
909 atime_ns = 5 * 10**9
910 mtime_ns = 8 * 10**9
911
912 set_time(filename, (atime_ns, mtime_ns))
913 st = os.stat(filename)
914
915 if support.is_emscripten:
916 # Emscripten timestamps are roundtripped through a 53 bit integer of
917 # nanoseconds. If we want to represent ~50 years which is an 11
918 # digits number of seconds:
919 # 2*log10(60) + log10(24) + log10(365) + log10(60) + log10(50)
920 # is about 11. Because 53 * log10(2) is about 16, we only have 5
921 # digits worth of sub-second precision.
922 # Some day it would be good to fix this upstream.
923 delta=1e-5
924 self.assertAlmostEqual(st.st_atime, atime_ns * 1e-9, delta=1e-5)
925 self.assertAlmostEqual(st.st_mtime, mtime_ns * 1e-9, delta=1e-5)
926 self.assertAlmostEqual(st.st_atime_ns, atime_ns, delta=1e9 * 1e-5)
927 self.assertAlmostEqual(st.st_mtime_ns, mtime_ns, delta=1e9 * 1e-5)
928 else:
929 if support_subsecond:
930 self.assertAlmostEqual(st.st_atime, atime_ns * 1e-9, delta=1e-6)
931 self.assertAlmostEqual(st.st_mtime, mtime_ns * 1e-9, delta=1e-6)
932 else:
933 self.assertEqual(st.st_atime, atime_ns * 1e-9)
934 self.assertEqual(st.st_mtime, mtime_ns * 1e-9)
935 self.assertEqual(st.st_atime_ns, atime_ns)
936 self.assertEqual(st.st_mtime_ns, mtime_ns)
937
938 def test_utime(self):
939 def set_time(filename, ns):
940 # test the ns keyword parameter
941 os.utime(filename, ns=ns)
942 self._test_utime(set_time)
943
944 @staticmethod
945 def ns_to_sec(ns):
946 # Convert a number of nanosecond (int) to a number of seconds (float).
947 # Round towards infinity by adding 0.5 nanosecond to avoid rounding
948 # issue, os.utime() rounds towards minus infinity.
949 return (ns * 1e-9) + 0.5e-9
950
951 def test_utime_by_indexed(self):
952 # pass times as floating-point seconds as the second indexed parameter
953 def set_time(filename, ns):
954 atime_ns, mtime_ns = ns
955 atime = self.ns_to_sec(atime_ns)
956 mtime = self.ns_to_sec(mtime_ns)
957 # test utimensat(timespec), utimes(timeval), utime(utimbuf)
958 # or utime(time_t)
959 os.utime(filename, (atime, mtime))
960 self._test_utime(set_time)
961
962 def test_utime_by_times(self):
963 def set_time(filename, ns):
964 atime_ns, mtime_ns = ns
965 atime = self.ns_to_sec(atime_ns)
966 mtime = self.ns_to_sec(mtime_ns)
967 # test the times keyword parameter
968 os.utime(filename, times=(atime, mtime))
969 self._test_utime(set_time)
970
971 @unittest.skipUnless(os.utime in os.supports_follow_symlinks,
972 "follow_symlinks support for utime required "
973 "for this test.")
974 def test_utime_nofollow_symlinks(self):
975 def set_time(filename, ns):
976 # use follow_symlinks=False to test utimensat(timespec)
977 # or lutimes(timeval)
978 os.utime(filename, ns=ns, follow_symlinks=False)
979 self._test_utime(set_time)
980
981 @unittest.skipUnless(os.utime in os.supports_fd,
982 "fd support for utime required for this test.")
983 def test_utime_fd(self):
984 def set_time(filename, ns):
985 with open(filename, 'wb', 0) as fp:
986 # use a file descriptor to test futimens(timespec)
987 # or futimes(timeval)
988 os.utime(fp.fileno(), ns=ns)
989 self._test_utime(set_time)
990
991 @unittest.skipUnless(os.utime in os.supports_dir_fd,
992 "dir_fd support for utime required for this test.")
993 def test_utime_dir_fd(self):
994 def set_time(filename, ns):
995 dirname, name = os.path.split(filename)
996 with os_helper.open_dir_fd(dirname) as dirfd:
997 # pass dir_fd to test utimensat(timespec) or futimesat(timeval)
998 os.utime(name, dir_fd=dirfd, ns=ns)
999 self._test_utime(set_time)
1000
1001 def test_utime_directory(self):
1002 def set_time(filename, ns):
1003 # test calling os.utime() on a directory
1004 os.utime(filename, ns=ns)
1005 self._test_utime(set_time, filename=self.dirname)
1006
1007 def _test_utime_current(self, set_time):
1008 # Get the system clock
1009 current = time.time()
1010
1011 # Call os.utime() to set the timestamp to the current system clock
1012 set_time(self.fname)
1013
1014 if not self.support_subsecond(self.fname):
1015 delta = 1.0
1016 else:
1017 # On Windows, the usual resolution of time.time() is 15.6 ms.
1018 # bpo-30649: Tolerate 50 ms for slow Windows buildbots.
1019 #
1020 # x86 Gentoo Refleaks 3.x once failed with dt=20.2 ms. So use
1021 # also 50 ms on other platforms.
1022 delta = 0.050
1023 st = os.stat(self.fname)
1024 msg = ("st_time=%r, current=%r, dt=%r"
1025 % (st.st_mtime, current, st.st_mtime - current))
1026 self.assertAlmostEqual(st.st_mtime, current,
1027 delta=delta, msg=msg)
1028
1029 def test_utime_current(self):
1030 def set_time(filename):
1031 # Set to the current time in the new way
1032 os.utime(self.fname)
1033 self._test_utime_current(set_time)
1034
1035 def test_utime_current_old(self):
1036 def set_time(filename):
1037 # Set to the current time in the old explicit way.
1038 os.utime(self.fname, None)
1039 self._test_utime_current(set_time)
1040
1041 def test_utime_nonexistent(self):
1042 now = time.time()
1043 filename = 'nonexistent'
1044 with self.assertRaises(FileNotFoundError) as cm:
1045 os.utime(filename, (now, now))
1046 self.assertEqual(cm.exception.filename, filename)
1047
1048 def get_file_system(self, path):
1049 if sys.platform == 'win32':
1050 root = os.path.splitdrive(os.path.abspath(path))[0] + '\\'
1051 import ctypes
1052 kernel32 = ctypes.windll.kernel32
1053 buf = ctypes.create_unicode_buffer("", 100)
1054 ok = kernel32.GetVolumeInformationW(root, None, 0,
1055 None, None, None,
1056 buf, len(buf))
1057 if ok:
1058 return buf.value
1059 # return None if the filesystem is unknown
1060
1061 def test_large_time(self):
1062 # Many filesystems are limited to the year 2038. At least, the test
1063 # pass with NTFS filesystem.
1064 if self.get_file_system(self.dirname) != "NTFS":
1065 self.skipTest("requires NTFS")
1066
1067 large = 5000000000 # some day in 2128
1068 os.utime(self.fname, (large, large))
1069 self.assertEqual(os.stat(self.fname).st_mtime, large)
1070
1071 def test_utime_invalid_arguments(self):
1072 # seconds and nanoseconds parameters are mutually exclusive
1073 with self.assertRaises(ValueError):
1074 os.utime(self.fname, (5, 5), ns=(5, 5))
1075 with self.assertRaises(TypeError):
1076 os.utime(self.fname, [5, 5])
1077 with self.assertRaises(TypeError):
1078 os.utime(self.fname, (5,))
1079 with self.assertRaises(TypeError):
1080 os.utime(self.fname, (5, 5, 5))
1081 with self.assertRaises(TypeError):
1082 os.utime(self.fname, ns=[5, 5])
1083 with self.assertRaises(TypeError):
1084 os.utime(self.fname, ns=(5,))
1085 with self.assertRaises(TypeError):
1086 os.utime(self.fname, ns=(5, 5, 5))
1087
1088 if os.utime not in os.supports_follow_symlinks:
1089 with self.assertRaises(NotImplementedError):
1090 os.utime(self.fname, (5, 5), follow_symlinks=False)
1091 if os.utime not in os.supports_fd:
1092 with open(self.fname, 'wb', 0) as fp:
1093 with self.assertRaises(TypeError):
1094 os.utime(fp.fileno(), (5, 5))
1095 if os.utime not in os.supports_dir_fd:
1096 with self.assertRaises(NotImplementedError):
1097 os.utime(self.fname, (5, 5), dir_fd=0)
1098
1099 @support.cpython_only
1100 def test_issue31577(self):
1101 # The interpreter shouldn't crash in case utime() received a bad
1102 # ns argument.
1103 def get_bad_int(divmod_ret_val):
1104 class BadInt:
1105 def __divmod__(*args):
1106 return divmod_ret_val
1107 return BadInt()
1108 with self.assertRaises(TypeError):
1109 os.utime(self.fname, ns=(get_bad_int(42), 1))
1110 with self.assertRaises(TypeError):
1111 os.utime(self.fname, ns=(get_bad_int(()), 1))
1112 with self.assertRaises(TypeError):
1113 os.utime(self.fname, ns=(get_bad_int((1, 2, 3)), 1))
1114
1115
1116 from test import mapping_tests
1117
1118 class EnvironTests(mapping_tests.BasicTestMappingProtocol):
1119 """check that os.environ object conform to mapping protocol"""
1120 type2test = None
1121
1122 def setUp(self):
1123 self.__save = dict(os.environ)
1124 if os.supports_bytes_environ:
1125 self.__saveb = dict(os.environb)
1126 for key, value in self._reference().items():
1127 os.environ[key] = value
1128
1129 def tearDown(self):
1130 os.environ.clear()
1131 os.environ.update(self.__save)
1132 if os.supports_bytes_environ:
1133 os.environb.clear()
1134 os.environb.update(self.__saveb)
1135
1136 def _reference(self):
1137 return {"KEY1":"VALUE1", "KEY2":"VALUE2", "KEY3":"VALUE3"}
1138
1139 def _empty_mapping(self):
1140 os.environ.clear()
1141 return os.environ
1142
1143 # Bug 1110478
1144 @unittest.skipUnless(unix_shell and os.path.exists(unix_shell),
1145 'requires a shell')
1146 @unittest.skipUnless(hasattr(os, 'popen'), "needs os.popen()")
1147 @support.requires_subprocess()
1148 def test_update2(self):
1149 os.environ.clear()
1150 os.environ.update(HELLO="World")
1151 with os.popen("%s -c 'echo $HELLO'" % unix_shell) as popen:
1152 value = popen.read().strip()
1153 self.assertEqual(value, "World")
1154
1155 @unittest.skipUnless(unix_shell and os.path.exists(unix_shell),
1156 'requires a shell')
1157 @unittest.skipUnless(hasattr(os, 'popen'), "needs os.popen()")
1158 @support.requires_subprocess()
1159 def test_os_popen_iter(self):
1160 with os.popen("%s -c 'echo \"line1\nline2\nline3\"'"
1161 % unix_shell) as popen:
1162 it = iter(popen)
1163 self.assertEqual(next(it), "line1\n")
1164 self.assertEqual(next(it), "line2\n")
1165 self.assertEqual(next(it), "line3\n")
1166 self.assertRaises(StopIteration, next, it)
1167
1168 # Verify environ keys and values from the OS are of the
1169 # correct str type.
1170 def test_keyvalue_types(self):
1171 for key, val in os.environ.items():
1172 self.assertEqual(type(key), str)
1173 self.assertEqual(type(val), str)
1174
1175 def test_items(self):
1176 for key, value in self._reference().items():
1177 self.assertEqual(os.environ.get(key), value)
1178
1179 # Issue 7310
1180 def test___repr__(self):
1181 """Check that the repr() of os.environ looks like environ({...})."""
1182 env = os.environ
1183 formatted_items = ", ".join(
1184 f"{key!r}: {value!r}"
1185 for key, value in env.items()
1186 )
1187 self.assertEqual(repr(env), f"environ({{{formatted_items}}})")
1188
1189 def test_get_exec_path(self):
1190 defpath_list = os.defpath.split(os.pathsep)
1191 test_path = ['/monty', '/python', '', '/flying/circus']
1192 test_env = {'PATH': os.pathsep.join(test_path)}
1193
1194 saved_environ = os.environ
1195 try:
1196 os.environ = dict(test_env)
1197 # Test that defaulting to os.environ works.
1198 self.assertSequenceEqual(test_path, os.get_exec_path())
1199 self.assertSequenceEqual(test_path, os.get_exec_path(env=None))
1200 finally:
1201 os.environ = saved_environ
1202
1203 # No PATH environment variable
1204 self.assertSequenceEqual(defpath_list, os.get_exec_path({}))
1205 # Empty PATH environment variable
1206 self.assertSequenceEqual(('',), os.get_exec_path({'PATH':''}))
1207 # Supplied PATH environment variable
1208 self.assertSequenceEqual(test_path, os.get_exec_path(test_env))
1209
1210 if os.supports_bytes_environ:
1211 # env cannot contain 'PATH' and b'PATH' keys
1212 try:
1213 # ignore BytesWarning warning
1214 with warnings.catch_warnings(record=True):
1215 mixed_env = {'PATH': '1', b'PATH': b'2'}
1216 except BytesWarning:
1217 # mixed_env cannot be created with python -bb
1218 pass
1219 else:
1220 self.assertRaises(ValueError, os.get_exec_path, mixed_env)
1221
1222 # bytes key and/or value
1223 self.assertSequenceEqual(os.get_exec_path({b'PATH': b'abc'}),
1224 ['abc'])
1225 self.assertSequenceEqual(os.get_exec_path({b'PATH': 'abc'}),
1226 ['abc'])
1227 self.assertSequenceEqual(os.get_exec_path({'PATH': b'abc'}),
1228 ['abc'])
1229
1230 @unittest.skipUnless(os.supports_bytes_environ,
1231 "os.environb required for this test.")
1232 def test_environb(self):
1233 # os.environ -> os.environb
1234 value = 'euro\u20ac'
1235 try:
1236 value_bytes = value.encode(sys.getfilesystemencoding(),
1237 'surrogateescape')
1238 except UnicodeEncodeError:
1239 msg = "U+20AC character is not encodable to %s" % (
1240 sys.getfilesystemencoding(),)
1241 self.skipTest(msg)
1242 os.environ['unicode'] = value
1243 self.assertEqual(os.environ['unicode'], value)
1244 self.assertEqual(os.environb[b'unicode'], value_bytes)
1245
1246 # os.environb -> os.environ
1247 value = b'\xff'
1248 os.environb[b'bytes'] = value
1249 self.assertEqual(os.environb[b'bytes'], value)
1250 value_str = value.decode(sys.getfilesystemencoding(), 'surrogateescape')
1251 self.assertEqual(os.environ['bytes'], value_str)
1252
1253 @support.requires_subprocess()
1254 def test_putenv_unsetenv(self):
1255 name = "PYTHONTESTVAR"
1256 value = "testvalue"
1257 code = f'import os; print(repr(os.environ.get({name!r})))'
1258
1259 with os_helper.EnvironmentVarGuard() as env:
1260 env.pop(name, None)
1261
1262 os.putenv(name, value)
1263 proc = subprocess.run([sys.executable, '-c', code], check=True,
1264 stdout=subprocess.PIPE, text=True)
1265 self.assertEqual(proc.stdout.rstrip(), repr(value))
1266
1267 os.unsetenv(name)
1268 proc = subprocess.run([sys.executable, '-c', code], check=True,
1269 stdout=subprocess.PIPE, text=True)
1270 self.assertEqual(proc.stdout.rstrip(), repr(None))
1271
1272 # On OS X < 10.6, unsetenv() doesn't return a value (bpo-13415).
1273 @support.requires_mac_ver(10, 6)
1274 def test_putenv_unsetenv_error(self):
1275 # Empty variable name is invalid.
1276 # "=" and null character are not allowed in a variable name.
1277 for name in ('', '=name', 'na=me', 'name='):
1278 self.assertRaises((OSError, ValueError), os.putenv, name, "value")
1279 self.assertRaises((OSError, ValueError), os.unsetenv, name)
1280 for name in ('name\0', 'na\0me'):
1281 self.assertRaises(ValueError, os.putenv, name, "value")
1282 self.assertRaises(ValueError, os.unsetenv, name)
1283
1284 if sys.platform == "win32":
1285 # On Windows, an environment variable string ("name=value" string)
1286 # is limited to 32,767 characters
1287 longstr = 'x' * 32_768
1288 self.assertRaises(ValueError, os.putenv, longstr, "1")
1289 self.assertRaises(ValueError, os.putenv, "X", longstr)
1290 self.assertRaises(ValueError, os.unsetenv, longstr)
1291
1292 def test_key_type(self):
1293 missing = 'missingkey'
1294 self.assertNotIn(missing, os.environ)
1295
1296 with self.assertRaises(KeyError) as cm:
1297 os.environ[missing]
1298 self.assertIs(cm.exception.args[0], missing)
1299 self.assertTrue(cm.exception.__suppress_context__)
1300
1301 with self.assertRaises(KeyError) as cm:
1302 del os.environ[missing]
1303 self.assertIs(cm.exception.args[0], missing)
1304 self.assertTrue(cm.exception.__suppress_context__)
1305
1306 def _test_environ_iteration(self, collection):
1307 iterator = iter(collection)
1308 new_key = "__new_key__"
1309
1310 next(iterator) # start iteration over os.environ.items
1311
1312 # add a new key in os.environ mapping
1313 os.environ[new_key] = "test_environ_iteration"
1314
1315 try:
1316 next(iterator) # force iteration over modified mapping
1317 self.assertEqual(os.environ[new_key], "test_environ_iteration")
1318 finally:
1319 del os.environ[new_key]
1320
1321 def test_iter_error_when_changing_os_environ(self):
1322 self._test_environ_iteration(os.environ)
1323
1324 def test_iter_error_when_changing_os_environ_items(self):
1325 self._test_environ_iteration(os.environ.items())
1326
1327 def test_iter_error_when_changing_os_environ_values(self):
1328 self._test_environ_iteration(os.environ.values())
1329
1330 def _test_underlying_process_env(self, var, expected):
1331 if not (unix_shell and os.path.exists(unix_shell)):
1332 return
1333 elif not support.has_subprocess_support:
1334 return
1335
1336 with os.popen(f"{unix_shell} -c 'echo ${var}'") as popen:
1337 value = popen.read().strip()
1338
1339 self.assertEqual(expected, value)
1340
1341 def test_or_operator(self):
1342 overridden_key = '_TEST_VAR_'
1343 original_value = 'original_value'
1344 os.environ[overridden_key] = original_value
1345
1346 new_vars_dict = {'_A_': '1', '_B_': '2', overridden_key: '3'}
1347 expected = dict(os.environ)
1348 expected.update(new_vars_dict)
1349
1350 actual = os.environ | new_vars_dict
1351 self.assertDictEqual(expected, actual)
1352 self.assertEqual('3', actual[overridden_key])
1353
1354 new_vars_items = new_vars_dict.items()
1355 self.assertIs(NotImplemented, os.environ.__or__(new_vars_items))
1356
1357 self._test_underlying_process_env('_A_', '')
1358 self._test_underlying_process_env(overridden_key, original_value)
1359
1360 def test_ior_operator(self):
1361 overridden_key = '_TEST_VAR_'
1362 os.environ[overridden_key] = 'original_value'
1363
1364 new_vars_dict = {'_A_': '1', '_B_': '2', overridden_key: '3'}
1365 expected = dict(os.environ)
1366 expected.update(new_vars_dict)
1367
1368 os.environ |= new_vars_dict
1369 self.assertEqual(expected, os.environ)
1370 self.assertEqual('3', os.environ[overridden_key])
1371
1372 self._test_underlying_process_env('_A_', '1')
1373 self._test_underlying_process_env(overridden_key, '3')
1374
1375 def test_ior_operator_invalid_dicts(self):
1376 os_environ_copy = os.environ.copy()
1377 with self.assertRaises(TypeError):
1378 dict_with_bad_key = {1: '_A_'}
1379 os.environ |= dict_with_bad_key
1380
1381 with self.assertRaises(TypeError):
1382 dict_with_bad_val = {'_A_': 1}
1383 os.environ |= dict_with_bad_val
1384
1385 # Check nothing was added.
1386 self.assertEqual(os_environ_copy, os.environ)
1387
1388 def test_ior_operator_key_value_iterable(self):
1389 overridden_key = '_TEST_VAR_'
1390 os.environ[overridden_key] = 'original_value'
1391
1392 new_vars_items = (('_A_', '1'), ('_B_', '2'), (overridden_key, '3'))
1393 expected = dict(os.environ)
1394 expected.update(new_vars_items)
1395
1396 os.environ |= new_vars_items
1397 self.assertEqual(expected, os.environ)
1398 self.assertEqual('3', os.environ[overridden_key])
1399
1400 self._test_underlying_process_env('_A_', '1')
1401 self._test_underlying_process_env(overridden_key, '3')
1402
1403 def test_ror_operator(self):
1404 overridden_key = '_TEST_VAR_'
1405 original_value = 'original_value'
1406 os.environ[overridden_key] = original_value
1407
1408 new_vars_dict = {'_A_': '1', '_B_': '2', overridden_key: '3'}
1409 expected = dict(new_vars_dict)
1410 expected.update(os.environ)
1411
1412 actual = new_vars_dict | os.environ
1413 self.assertDictEqual(expected, actual)
1414 self.assertEqual(original_value, actual[overridden_key])
1415
1416 new_vars_items = new_vars_dict.items()
1417 self.assertIs(NotImplemented, os.environ.__ror__(new_vars_items))
1418
1419 self._test_underlying_process_env('_A_', '')
1420 self._test_underlying_process_env(overridden_key, original_value)
1421
1422 def test_reload_environ(self):
1423 # Test os.reload_environ()
1424 has_environb = hasattr(os, 'environb')
1425
1426 # Test with putenv() which doesn't update os.environ
1427 os.environ['test_env'] = 'python_value'
1428 os.putenv("test_env", "new_value")
1429 self.assertEqual(os.environ['test_env'], 'python_value')
1430 if has_environb:
1431 self.assertEqual(os.environb[b'test_env'], b'python_value')
1432
1433 os.reload_environ()
1434 self.assertEqual(os.environ['test_env'], 'new_value')
1435 if has_environb:
1436 self.assertEqual(os.environb[b'test_env'], b'new_value')
1437
1438 # Test with unsetenv() which doesn't update os.environ
1439 os.unsetenv('test_env')
1440 self.assertEqual(os.environ['test_env'], 'new_value')
1441 if has_environb:
1442 self.assertEqual(os.environb[b'test_env'], b'new_value')
1443
1444 os.reload_environ()
1445 self.assertNotIn('test_env', os.environ)
1446 if has_environb:
1447 self.assertNotIn(b'test_env', os.environb)
1448
1449 if has_environb:
1450 # test reload_environ() on os.environb with putenv()
1451 os.environb[b'test_env'] = b'python_value2'
1452 os.putenv("test_env", "new_value2")
1453 self.assertEqual(os.environb[b'test_env'], b'python_value2')
1454 self.assertEqual(os.environ['test_env'], 'python_value2')
1455
1456 os.reload_environ()
1457 self.assertEqual(os.environb[b'test_env'], b'new_value2')
1458 self.assertEqual(os.environ['test_env'], 'new_value2')
1459
1460 # test reload_environ() on os.environb with unsetenv()
1461 os.unsetenv('test_env')
1462 self.assertEqual(os.environb[b'test_env'], b'new_value2')
1463 self.assertEqual(os.environ['test_env'], 'new_value2')
1464
1465 os.reload_environ()
1466 self.assertNotIn(b'test_env', os.environb)
1467 self.assertNotIn('test_env', os.environ)
1468
1469 class WalkTests(unittest.TestCase):
1470 """Tests for os.walk()."""
1471 is_fwalk = False
1472
1473 # Wrapper to hide minor differences between os.walk and os.fwalk
1474 # to tests both functions with the same code base
1475 def walk(self, top, **kwargs):
1476 if 'follow_symlinks' in kwargs:
1477 kwargs['followlinks'] = kwargs.pop('follow_symlinks')
1478 return os.walk(top, **kwargs)
1479
1480 def setUp(self):
1481 join = os.path.join
1482 self.addCleanup(os_helper.rmtree, os_helper.TESTFN)
1483
1484 # Build:
1485 # TESTFN/
1486 # TEST1/ a file kid and two directory kids
1487 # tmp1
1488 # SUB1/ a file kid and a directory kid
1489 # tmp2
1490 # SUB11/ no kids
1491 # SUB2/ a file kid and a dirsymlink kid
1492 # tmp3
1493 # SUB21/ not readable
1494 # tmp5
1495 # link/ a symlink to TESTFN.2
1496 # broken_link
1497 # broken_link2
1498 # broken_link3
1499 # TEST2/
1500 # tmp4 a lone file
1501 self.walk_path = join(os_helper.TESTFN, "TEST1")
1502 self.sub1_path = join(self.walk_path, "SUB1")
1503 self.sub11_path = join(self.sub1_path, "SUB11")
1504 sub2_path = join(self.walk_path, "SUB2")
1505 sub21_path = join(sub2_path, "SUB21")
1506 self.tmp1_path = join(self.walk_path, "tmp1")
1507 tmp2_path = join(self.sub1_path, "tmp2")
1508 tmp3_path = join(sub2_path, "tmp3")
1509 tmp5_path = join(sub21_path, "tmp3")
1510 self.link_path = join(sub2_path, "link")
1511 t2_path = join(os_helper.TESTFN, "TEST2")
1512 tmp4_path = join(os_helper.TESTFN, "TEST2", "tmp4")
1513 self.broken_link_path = join(sub2_path, "broken_link")
1514 broken_link2_path = join(sub2_path, "broken_link2")
1515 broken_link3_path = join(sub2_path, "broken_link3")
1516
1517 # Create stuff.
1518 os.makedirs(self.sub11_path)
1519 os.makedirs(sub2_path)
1520 os.makedirs(sub21_path)
1521 os.makedirs(t2_path)
1522
1523 for path in self.tmp1_path, tmp2_path, tmp3_path, tmp4_path, tmp5_path:
1524 with open(path, "x", encoding='utf-8') as f:
1525 f.write("I'm " + path + " and proud of it. Blame test_os.\n")
1526
1527 if os_helper.can_symlink():
1528 os.symlink(os.path.abspath(t2_path), self.link_path)
1529 os.symlink('broken', self.broken_link_path, True)
1530 os.symlink(join('tmp3', 'broken'), broken_link2_path, True)
1531 os.symlink(join('SUB21', 'tmp5'), broken_link3_path, True)
1532 self.sub2_tree = (sub2_path, ["SUB21", "link"],
1533 ["broken_link", "broken_link2", "broken_link3",
1534 "tmp3"])
1535 else:
1536 self.sub2_tree = (sub2_path, ["SUB21"], ["tmp3"])
1537
1538 os.chmod(sub21_path, 0)
1539 try:
1540 os.listdir(sub21_path)
1541 except PermissionError:
1542 self.addCleanup(os.chmod, sub21_path, stat.S_IRWXU)
1543 else:
1544 os.chmod(sub21_path, stat.S_IRWXU)
1545 os.unlink(tmp5_path)
1546 os.rmdir(sub21_path)
1547 del self.sub2_tree[1][:1]
1548
1549 def test_walk_topdown(self):
1550 # Walk top-down.
1551 all = list(self.walk(self.walk_path))
1552
1553 self.assertEqual(len(all), 4)
1554 # We can't know which order SUB1 and SUB2 will appear in.
1555 # Not flipped: TESTFN, SUB1, SUB11, SUB2
1556 # flipped: TESTFN, SUB2, SUB1, SUB11
1557 flipped = all[0][1][0] != "SUB1"
1558 all[0][1].sort()
1559 all[3 - 2 * flipped][-1].sort()
1560 all[3 - 2 * flipped][1].sort()
1561 self.assertEqual(all[0], (self.walk_path, ["SUB1", "SUB2"], ["tmp1"]))
1562 self.assertEqual(all[1 + flipped], (self.sub1_path, ["SUB11"], ["tmp2"]))
1563 self.assertEqual(all[2 + flipped], (self.sub11_path, [], []))
1564 self.assertEqual(all[3 - 2 * flipped], self.sub2_tree)
1565
1566 def test_walk_prune(self, walk_path=None):
1567 if walk_path is None:
1568 walk_path = self.walk_path
1569 # Prune the search.
1570 all = []
1571 for root, dirs, files in self.walk(walk_path):
1572 all.append((root, dirs, files))
1573 # Don't descend into SUB1.
1574 if 'SUB1' in dirs:
1575 # Note that this also mutates the dirs we appended to all!
1576 dirs.remove('SUB1')
1577
1578 self.assertEqual(len(all), 2)
1579 self.assertEqual(all[0], (self.walk_path, ["SUB2"], ["tmp1"]))
1580
1581 all[1][-1].sort()
1582 all[1][1].sort()
1583 self.assertEqual(all[1], self.sub2_tree)
1584
1585 def test_file_like_path(self):
1586 self.test_walk_prune(FakePath(self.walk_path))
1587
1588 def test_walk_bottom_up(self):
1589 # Walk bottom-up.
1590 all = list(self.walk(self.walk_path, topdown=False))
1591
1592 self.assertEqual(len(all), 4, all)
1593 # We can't know which order SUB1 and SUB2 will appear in.
1594 # Not flipped: SUB11, SUB1, SUB2, TESTFN
1595 # flipped: SUB2, SUB11, SUB1, TESTFN
1596 flipped = all[3][1][0] != "SUB1"
1597 all[3][1].sort()
1598 all[2 - 2 * flipped][-1].sort()
1599 all[2 - 2 * flipped][1].sort()
1600 self.assertEqual(all[3],
1601 (self.walk_path, ["SUB1", "SUB2"], ["tmp1"]))
1602 self.assertEqual(all[flipped],
1603 (self.sub11_path, [], []))
1604 self.assertEqual(all[flipped + 1],
1605 (self.sub1_path, ["SUB11"], ["tmp2"]))
1606 self.assertEqual(all[2 - 2 * flipped],
1607 self.sub2_tree)
1608
1609 def test_walk_symlink(self):
1610 if not os_helper.can_symlink():
1611 self.skipTest("need symlink support")
1612
1613 # Walk, following symlinks.
1614 walk_it = self.walk(self.walk_path, follow_symlinks=True)
1615 for root, dirs, files in walk_it:
1616 if root == self.link_path:
1617 self.assertEqual(dirs, [])
1618 self.assertEqual(files, ["tmp4"])
1619 break
1620 else:
1621 self.fail("Didn't follow symlink with followlinks=True")
1622
1623 walk_it = self.walk(self.broken_link_path, follow_symlinks=True)
1624 if self.is_fwalk:
1625 self.assertRaises(FileNotFoundError, next, walk_it)
1626 self.assertRaises(StopIteration, next, walk_it)
1627
1628 def test_walk_bad_dir(self):
1629 # Walk top-down.
1630 errors = []
1631 walk_it = self.walk(self.walk_path, onerror=errors.append)
1632 root, dirs, files = next(walk_it)
1633 self.assertEqual(errors, [])
1634 dir1 = 'SUB1'
1635 path1 = os.path.join(root, dir1)
1636 path1new = os.path.join(root, dir1 + '.new')
1637 os.rename(path1, path1new)
1638 try:
1639 roots = [r for r, d, f in walk_it]
1640 self.assertTrue(errors)
1641 self.assertNotIn(path1, roots)
1642 self.assertNotIn(path1new, roots)
1643 for dir2 in dirs:
1644 if dir2 != dir1:
1645 self.assertIn(os.path.join(root, dir2), roots)
1646 finally:
1647 os.rename(path1new, path1)
1648
1649 def test_walk_bad_dir2(self):
1650 walk_it = self.walk('nonexisting')
1651 if self.is_fwalk:
1652 self.assertRaises(FileNotFoundError, next, walk_it)
1653 self.assertRaises(StopIteration, next, walk_it)
1654
1655 walk_it = self.walk('nonexisting', follow_symlinks=True)
1656 if self.is_fwalk:
1657 self.assertRaises(FileNotFoundError, next, walk_it)
1658 self.assertRaises(StopIteration, next, walk_it)
1659
1660 walk_it = self.walk(self.tmp1_path)
1661 self.assertRaises(StopIteration, next, walk_it)
1662
1663 walk_it = self.walk(self.tmp1_path, follow_symlinks=True)
1664 if self.is_fwalk:
1665 self.assertRaises(NotADirectoryError, next, walk_it)
1666 self.assertRaises(StopIteration, next, walk_it)
1667
1668 @unittest.skipUnless(hasattr(os, "mkfifo"), 'requires os.mkfifo()')
1669 @unittest.skipIf(sys.platform == "vxworks",
1670 "fifo requires special path on VxWorks")
1671 def test_walk_named_pipe(self):
1672 path = os_helper.TESTFN + '-pipe'
1673 os.mkfifo(path)
1674 self.addCleanup(os.unlink, path)
1675
1676 walk_it = self.walk(path)
1677 self.assertRaises(StopIteration, next, walk_it)
1678
1679 walk_it = self.walk(path, follow_symlinks=True)
1680 if self.is_fwalk:
1681 self.assertRaises(NotADirectoryError, next, walk_it)
1682 self.assertRaises(StopIteration, next, walk_it)
1683
1684 @unittest.skipUnless(hasattr(os, "mkfifo"), 'requires os.mkfifo()')
1685 @unittest.skipIf(sys.platform == "vxworks",
1686 "fifo requires special path on VxWorks")
1687 def test_walk_named_pipe2(self):
1688 path = os_helper.TESTFN + '-dir'
1689 os.mkdir(path)
1690 self.addCleanup(shutil.rmtree, path)
1691 os.mkfifo(os.path.join(path, 'mypipe'))
1692
1693 errors = []
1694 walk_it = self.walk(path, onerror=errors.append)
1695 next(walk_it)
1696 self.assertRaises(StopIteration, next, walk_it)
1697 self.assertEqual(errors, [])
1698
1699 errors = []
1700 walk_it = self.walk(path, onerror=errors.append)
1701 root, dirs, files = next(walk_it)
1702 self.assertEqual(root, path)
1703 self.assertEqual(dirs, [])
1704 self.assertEqual(files, ['mypipe'])
1705 dirs.extend(files)
1706 files.clear()
1707 if self.is_fwalk:
1708 self.assertRaises(NotADirectoryError, next, walk_it)
1709 self.assertRaises(StopIteration, next, walk_it)
1710 if self.is_fwalk:
1711 self.assertEqual(errors, [])
1712 else:
1713 self.assertEqual(len(errors), 1, errors)
1714 self.assertIsInstance(errors[0], NotADirectoryError)
1715
1716 def test_walk_many_open_files(self):
1717 depth = 30
1718 base = os.path.join(os_helper.TESTFN, 'deep')
1719 p = os.path.join(base, *(['d']*depth))
1720 os.makedirs(p)
1721
1722 iters = [self.walk(base, topdown=False) for j in range(100)]
1723 for i in range(depth + 1):
1724 expected = (p, ['d'] if i else [], [])
1725 for it in iters:
1726 self.assertEqual(next(it), expected)
1727 p = os.path.dirname(p)
1728
1729 iters = [self.walk(base, topdown=True) for j in range(100)]
1730 p = base
1731 for i in range(depth + 1):
1732 expected = (p, ['d'] if i < depth else [], [])
1733 for it in iters:
1734 self.assertEqual(next(it), expected)
1735 p = os.path.join(p, 'd')
1736
1737 def test_walk_above_recursion_limit(self):
1738 depth = 50
1739 os.makedirs(os.path.join(self.walk_path, *(['d'] * depth)))
1740 with infinite_recursion(depth - 5):
1741 all = list(self.walk(self.walk_path))
1742
1743 sub2_path = self.sub2_tree[0]
1744 for root, dirs, files in all:
1745 if root == sub2_path:
1746 dirs.sort()
1747 files.sort()
1748
1749 d_entries = []
1750 d_path = self.walk_path
1751 for _ in range(depth):
1752 d_path = os.path.join(d_path, "d")
1753 d_entries.append((d_path, ["d"], []))
1754 d_entries[-1][1].clear()
1755
1756 # Sub-sequences where the order is known
1757 sections = {
1758 "SUB1": [
1759 (self.sub1_path, ["SUB11"], ["tmp2"]),
1760 (self.sub11_path, [], []),
1761 ],
1762 "SUB2": [self.sub2_tree],
1763 "d": d_entries,
1764 }
1765
1766 # The ordering of sub-dirs is arbitrary but determines the order in
1767 # which sub-sequences appear
1768 dirs = all[0][1]
1769 expected = [(self.walk_path, dirs, ["tmp1"])]
1770 for d in dirs:
1771 expected.extend(sections[d])
1772
1773 self.assertEqual(len(all), depth + 4)
1774 self.assertEqual(sorted(dirs), ["SUB1", "SUB2", "d"])
1775 self.assertEqual(all, expected)
1776
1777
1778 @unittest.skipUnless(hasattr(os, 'fwalk'), "Test needs os.fwalk()")
1779 class FwalkTests(WalkTests):
1780 """Tests for os.fwalk()."""
1781 is_fwalk = True
1782
1783 def walk(self, top, **kwargs):
1784 for root, dirs, files, root_fd in self.fwalk(top, **kwargs):
1785 yield (root, dirs, files)
1786
1787 def fwalk(self, *args, **kwargs):
1788 return os.fwalk(*args, **kwargs)
1789
1790 def _compare_to_walk(self, walk_kwargs, fwalk_kwargs):
1791 """
1792 compare with walk() results.
1793 """
1794 walk_kwargs = walk_kwargs.copy()
1795 fwalk_kwargs = fwalk_kwargs.copy()
1796 for topdown, follow_symlinks in itertools.product((True, False), repeat=2):
1797 walk_kwargs.update(topdown=topdown, followlinks=follow_symlinks)
1798 fwalk_kwargs.update(topdown=topdown, follow_symlinks=follow_symlinks)
1799
1800 expected = {}
1801 for root, dirs, files in os.walk(**walk_kwargs):
1802 expected[root] = (set(dirs), set(files))
1803
1804 for root, dirs, files, rootfd in self.fwalk(**fwalk_kwargs):
1805 self.assertIn(root, expected)
1806 self.assertEqual(expected[root], (set(dirs), set(files)))
1807
1808 def test_compare_to_walk(self):
1809 kwargs = {'top': os_helper.TESTFN}
1810 self._compare_to_walk(kwargs, kwargs)
1811
1812 def test_dir_fd(self):
1813 try:
1814 fd = os.open(".", os.O_RDONLY)
1815 walk_kwargs = {'top': os_helper.TESTFN}
1816 fwalk_kwargs = walk_kwargs.copy()
1817 fwalk_kwargs['dir_fd'] = fd
1818 self._compare_to_walk(walk_kwargs, fwalk_kwargs)
1819 finally:
1820 os.close(fd)
1821
1822 def test_yields_correct_dir_fd(self):
1823 # check returned file descriptors
1824 for topdown, follow_symlinks in itertools.product((True, False), repeat=2):
1825 args = os_helper.TESTFN, topdown, None
1826 for root, dirs, files, rootfd in self.fwalk(*args, follow_symlinks=follow_symlinks):
1827 # check that the FD is valid
1828 os.fstat(rootfd)
1829 # redundant check
1830 os.stat(rootfd)
1831 # check that listdir() returns consistent information
1832 self.assertEqual(set(os.listdir(rootfd)), set(dirs) | set(files))
1833
1834 @unittest.skipIf(
1835 support.is_android, "dup return value is unpredictable on Android"
1836 )
1837 def test_fd_leak(self):
1838 # Since we're opening a lot of FDs, we must be careful to avoid leaks:
1839 # we both check that calling fwalk() a large number of times doesn't
1840 # yield EMFILE, and that the minimum allocated FD hasn't changed.
1841 minfd = os.dup(1)
1842 os.close(minfd)
1843 for i in range(256):
1844 for x in self.fwalk(os_helper.TESTFN):
1845 pass
1846 newfd = os.dup(1)
1847 self.addCleanup(os.close, newfd)
1848 self.assertEqual(newfd, minfd)
1849
1850 @unittest.skipIf(
1851 support.is_android, "dup return value is unpredictable on Android"
1852 )
1853 def test_fd_finalization(self):
1854 # Check that close()ing the fwalk() generator closes FDs
1855 def getfd():
1856 fd = os.dup(1)
1857 os.close(fd)
1858 return fd
1859 for topdown in (False, True):
1860 old_fd = getfd()
1861 it = self.fwalk(os_helper.TESTFN, topdown=topdown)
1862 self.assertEqual(getfd(), old_fd)
1863 next(it)
1864 self.assertGreater(getfd(), old_fd)
1865 it.close()
1866 self.assertEqual(getfd(), old_fd)
1867
1868 # fwalk() keeps file descriptors open
1869 test_walk_many_open_files = None
1870
1871
1872 class BytesWalkTests(WalkTests):
1873 """Tests for os.walk() with bytes."""
1874 def walk(self, top, **kwargs):
1875 if 'follow_symlinks' in kwargs:
1876 kwargs['followlinks'] = kwargs.pop('follow_symlinks')
1877 for broot, bdirs, bfiles in os.walk(os.fsencode(top), **kwargs):
1878 root = os.fsdecode(broot)
1879 dirs = list(map(os.fsdecode, bdirs))
1880 files = list(map(os.fsdecode, bfiles))
1881 yield (root, dirs, files)
1882 bdirs[:] = list(map(os.fsencode, dirs))
1883 bfiles[:] = list(map(os.fsencode, files))
1884
1885 @unittest.skipUnless(hasattr(os, 'fwalk'), "Test needs os.fwalk()")
1886 class BytesFwalkTests(FwalkTests):
1887 """Tests for os.walk() with bytes."""
1888 def fwalk(self, top='.', *args, **kwargs):
1889 for broot, bdirs, bfiles, topfd in os.fwalk(os.fsencode(top), *args, **kwargs):
1890 root = os.fsdecode(broot)
1891 dirs = list(map(os.fsdecode, bdirs))
1892 files = list(map(os.fsdecode, bfiles))
1893 yield (root, dirs, files, topfd)
1894 bdirs[:] = list(map(os.fsencode, dirs))
1895 bfiles[:] = list(map(os.fsencode, files))
1896
1897
1898 class MakedirTests(unittest.TestCase):
1899 def setUp(self):
1900 os.mkdir(os_helper.TESTFN)
1901
1902 def test_makedir(self):
1903 base = os_helper.TESTFN
1904 path = os.path.join(base, 'dir1', 'dir2', 'dir3')
1905 os.makedirs(path) # Should work
1906 path = os.path.join(base, 'dir1', 'dir2', 'dir3', 'dir4')
1907 os.makedirs(path)
1908
1909 # Try paths with a '.' in them
1910 self.assertRaises(OSError, os.makedirs, os.curdir)
1911 path = os.path.join(base, 'dir1', 'dir2', 'dir3', 'dir4', 'dir5', os.curdir)
1912 os.makedirs(path)
1913 path = os.path.join(base, 'dir1', os.curdir, 'dir2', 'dir3', 'dir4',
1914 'dir5', 'dir6')
1915 os.makedirs(path)
1916
1917 @unittest.skipIf(
1918 support.is_wasi,
1919 "WASI's umask is a stub."
1920 )
1921 def test_mode(self):
1922 # Note: in some cases, the umask might already be 2 in which case this
1923 # will pass even if os.umask is actually broken.
1924 with os_helper.temp_umask(0o002):
1925 base = os_helper.TESTFN
1926 parent = os.path.join(base, 'dir1')
1927 path = os.path.join(parent, 'dir2')
1928 os.makedirs(path, 0o555)
1929 self.assertTrue(os.path.exists(path))
1930 self.assertTrue(os.path.isdir(path))
1931 if os.name != 'nt':
1932 self.assertEqual(os.stat(path).st_mode & 0o777, 0o555)
1933 self.assertEqual(os.stat(parent).st_mode & 0o777, 0o775)
1934
1935 @unittest.skipIf(
1936 support.is_wasi,
1937 "WASI's umask is a stub."
1938 )
1939 def test_exist_ok_existing_directory(self):
1940 path = os.path.join(os_helper.TESTFN, 'dir1')
1941 mode = 0o777
1942 old_mask = os.umask(0o022)
1943 os.makedirs(path, mode)
1944 self.assertRaises(OSError, os.makedirs, path, mode)
1945 self.assertRaises(OSError, os.makedirs, path, mode, exist_ok=False)
1946 os.makedirs(path, 0o776, exist_ok=True)
1947 os.makedirs(path, mode=mode, exist_ok=True)
1948 os.umask(old_mask)
1949
1950 # Issue #25583: A drive root could raise PermissionError on Windows
1951 os.makedirs(os.path.abspath('/'), exist_ok=True)
1952
1953 @unittest.skipIf(
1954 support.is_wasi,
1955 "WASI's umask is a stub."
1956 )
1957 def test_exist_ok_s_isgid_directory(self):
1958 path = os.path.join(os_helper.TESTFN, 'dir1')
1959 S_ISGID = stat.S_ISGID
1960 mode = 0o777
1961 old_mask = os.umask(0o022)
1962 try:
1963 existing_testfn_mode = stat.S_IMODE(
1964 os.lstat(os_helper.TESTFN).st_mode)
1965 try:
1966 os.chmod(os_helper.TESTFN, existing_testfn_mode | S_ISGID)
1967 except PermissionError:
1968 raise unittest.SkipTest('Cannot set S_ISGID for dir.')
1969 if (os.lstat(os_helper.TESTFN).st_mode & S_ISGID != S_ISGID):
1970 raise unittest.SkipTest('No support for S_ISGID dir mode.')
1971 # The os should apply S_ISGID from the parent dir for us, but
1972 # this test need not depend on that behavior. Be explicit.
1973 os.makedirs(path, mode | S_ISGID)
1974 # http://bugs.python.org/issue14992
1975 # Should not fail when the bit is already set.
1976 os.makedirs(path, mode, exist_ok=True)
1977 # remove the bit.
1978 os.chmod(path, stat.S_IMODE(os.lstat(path).st_mode) & ~S_ISGID)
1979 # May work even when the bit is not already set when demanded.
1980 os.makedirs(path, mode | S_ISGID, exist_ok=True)
1981 finally:
1982 os.umask(old_mask)
1983
1984 def test_exist_ok_existing_regular_file(self):
1985 base = os_helper.TESTFN
1986 path = os.path.join(os_helper.TESTFN, 'dir1')
1987 with open(path, 'w', encoding='utf-8') as f:
1988 f.write('abc')
1989 self.assertRaises(OSError, os.makedirs, path)
1990 self.assertRaises(OSError, os.makedirs, path, exist_ok=False)
1991 self.assertRaises(OSError, os.makedirs, path, exist_ok=True)
1992 os.remove(path)
1993
1994 @unittest.skipUnless(os.name == 'nt', "requires Windows")
1995 def test_win32_mkdir_700(self):
1996 base = os_helper.TESTFN
1997 path = os.path.abspath(os.path.join(os_helper.TESTFN, 'dir'))
1998 os.mkdir(path, mode=0o700)
1999 out = subprocess.check_output(["cacls.exe", path, "/s"], encoding="oem")
2000 os.rmdir(path)
2001 out = out.strip().rsplit(" ", 1)[1]
2002 self.assertEqual(
2003 out,
2004 '"D:P(A;OICI;FA;;;SY)(A;OICI;FA;;;BA)(A;OICI;FA;;;OW)"',
2005 )
2006
2007 def tearDown(self):
2008 path = os.path.join(os_helper.TESTFN, 'dir1', 'dir2', 'dir3',
2009 'dir4', 'dir5', 'dir6')
2010 # If the tests failed, the bottom-most directory ('../dir6')
2011 # may not have been created, so we look for the outermost directory
2012 # that exists.
2013 while not os.path.exists(path) and path != os_helper.TESTFN:
2014 path = os.path.dirname(path)
2015
2016 os.removedirs(path)
2017
2018
2019 @unittest.skipUnless(hasattr(os, "chown"), "requires os.chown()")
2020 class ChownFileTests(unittest.TestCase):
2021
2022 @classmethod
2023 def setUpClass(cls):
2024 os.mkdir(os_helper.TESTFN)
2025
2026 def test_chown_uid_gid_arguments_must_be_index(self):
2027 stat = os.stat(os_helper.TESTFN)
2028 uid = stat.st_uid
2029 gid = stat.st_gid
2030 for value in (-1.0, -1j, decimal.Decimal(-1), fractions.Fraction(-2, 2)):
2031 self.assertRaises(TypeError, os.chown, os_helper.TESTFN, value, gid)
2032 self.assertRaises(TypeError, os.chown, os_helper.TESTFN, uid, value)
2033 self.assertIsNone(os.chown(os_helper.TESTFN, uid, gid))
2034 self.assertIsNone(os.chown(os_helper.TESTFN, -1, -1))
2035
2036 @unittest.skipUnless(hasattr(os, 'getgroups'), 'need os.getgroups')
2037 def test_chown_gid(self):
2038 groups = os.getgroups()
2039 if len(groups) < 2:
2040 self.skipTest("test needs at least 2 groups")
2041
2042 gid_1, gid_2 = groups[:2]
2043 uid = os.stat(os_helper.TESTFN).st_uid
2044
2045 os.chown(os_helper.TESTFN, uid, gid_1)
2046 gid = os.stat(os_helper.TESTFN).st_gid
2047 self.assertEqual(gid, gid_1)
2048
2049 os.chown(os_helper.TESTFN, uid, gid_2)
2050 gid = os.stat(os_helper.TESTFN).st_gid
2051 self.assertEqual(gid, gid_2)
2052
2053 @unittest.skipUnless(root_in_posix and len(all_users) > 1,
2054 "test needs root privilege and more than one user")
2055 def test_chown_with_root(self):
2056 uid_1, uid_2 = all_users[:2]
2057 gid = os.stat(os_helper.TESTFN).st_gid
2058 os.chown(os_helper.TESTFN, uid_1, gid)
2059 uid = os.stat(os_helper.TESTFN).st_uid
2060 self.assertEqual(uid, uid_1)
2061 os.chown(os_helper.TESTFN, uid_2, gid)
2062 uid = os.stat(os_helper.TESTFN).st_uid
2063 self.assertEqual(uid, uid_2)
2064
2065 @unittest.skipUnless(not root_in_posix and len(all_users) > 1,
2066 "test needs non-root account and more than one user")
2067 def test_chown_without_permission(self):
2068 uid_1, uid_2 = all_users[:2]
2069 gid = os.stat(os_helper.TESTFN).st_gid
2070 with self.assertRaises(PermissionError):
2071 os.chown(os_helper.TESTFN, uid_1, gid)
2072 os.chown(os_helper.TESTFN, uid_2, gid)
2073
2074 @classmethod
2075 def tearDownClass(cls):
2076 os.rmdir(os_helper.TESTFN)
2077
2078
2079 class RemoveDirsTests(unittest.TestCase):
2080 def setUp(self):
2081 os.makedirs(os_helper.TESTFN)
2082
2083 def tearDown(self):
2084 os_helper.rmtree(os_helper.TESTFN)
2085
2086 def test_remove_all(self):
2087 dira = os.path.join(os_helper.TESTFN, 'dira')
2088 os.mkdir(dira)
2089 dirb = os.path.join(dira, 'dirb')
2090 os.mkdir(dirb)
2091 os.removedirs(dirb)
2092 self.assertFalse(os.path.exists(dirb))
2093 self.assertFalse(os.path.exists(dira))
2094 self.assertFalse(os.path.exists(os_helper.TESTFN))
2095
2096 def test_remove_partial(self):
2097 dira = os.path.join(os_helper.TESTFN, 'dira')
2098 os.mkdir(dira)
2099 dirb = os.path.join(dira, 'dirb')
2100 os.mkdir(dirb)
2101 create_file(os.path.join(dira, 'file.txt'))
2102 os.removedirs(dirb)
2103 self.assertFalse(os.path.exists(dirb))
2104 self.assertTrue(os.path.exists(dira))
2105 self.assertTrue(os.path.exists(os_helper.TESTFN))
2106
2107 def test_remove_nothing(self):
2108 dira = os.path.join(os_helper.TESTFN, 'dira')
2109 os.mkdir(dira)
2110 dirb = os.path.join(dira, 'dirb')
2111 os.mkdir(dirb)
2112 create_file(os.path.join(dirb, 'file.txt'))
2113 with self.assertRaises(OSError):
2114 os.removedirs(dirb)
2115 self.assertTrue(os.path.exists(dirb))
2116 self.assertTrue(os.path.exists(dira))
2117 self.assertTrue(os.path.exists(os_helper.TESTFN))
2118
2119
2120 @unittest.skipIf(support.is_wasi, "WASI has no /dev/null")
2121 class DevNullTests(unittest.TestCase):
2122 def test_devnull(self):
2123 with open(os.devnull, 'wb', 0) as f:
2124 f.write(b'hello')
2125 f.close()
2126 with open(os.devnull, 'rb') as f:
2127 self.assertEqual(f.read(), b'')
2128
2129
2130 class URandomTests(unittest.TestCase):
2131 def test_urandom_length(self):
2132 self.assertEqual(len(os.urandom(0)), 0)
2133 self.assertEqual(len(os.urandom(1)), 1)
2134 self.assertEqual(len(os.urandom(10)), 10)
2135 self.assertEqual(len(os.urandom(100)), 100)
2136 self.assertEqual(len(os.urandom(1000)), 1000)
2137
2138 def test_urandom_value(self):
2139 data1 = os.urandom(16)
2140 self.assertIsInstance(data1, bytes)
2141 data2 = os.urandom(16)
2142 self.assertNotEqual(data1, data2)
2143
2144 def get_urandom_subprocess(self, count):
2145 code = '\n'.join((
2146 'import os, sys',
2147 'data = os.urandom(%s)' % count,
2148 'sys.stdout.buffer.write(data)',
2149 'sys.stdout.buffer.flush()'))
2150 out = assert_python_ok('-c', code)
2151 stdout = out[1]
2152 self.assertEqual(len(stdout), count)
2153 return stdout
2154
2155 def test_urandom_subprocess(self):
2156 data1 = self.get_urandom_subprocess(16)
2157 data2 = self.get_urandom_subprocess(16)
2158 self.assertNotEqual(data1, data2)
2159
2160
2161 @unittest.skipUnless(hasattr(os, 'getrandom'), 'need os.getrandom()')
2162 class GetRandomTests(unittest.TestCase):
2163 @classmethod
2164 def setUpClass(cls):
2165 try:
2166 os.getrandom(1)
2167 except OSError as exc:
2168 if exc.errno == errno.ENOSYS:
2169 # Python compiled on a more recent Linux version
2170 # than the current Linux kernel
2171 raise unittest.SkipTest("getrandom() syscall fails with ENOSYS")
2172 else:
2173 raise
2174
2175 def test_getrandom_type(self):
2176 data = os.getrandom(16)
2177 self.assertIsInstance(data, bytes)
2178 self.assertEqual(len(data), 16)
2179
2180 def test_getrandom0(self):
2181 empty = os.getrandom(0)
2182 self.assertEqual(empty, b'')
2183
2184 def test_getrandom_random(self):
2185 self.assertHasAttr(os, 'GRND_RANDOM')
2186
2187 # Don't test os.getrandom(1, os.GRND_RANDOM) to not consume the rare
2188 # resource /dev/random
2189
2190 def test_getrandom_nonblock(self):
2191 # The call must not fail. Check also that the flag exists
2192 try:
2193 os.getrandom(1, os.GRND_NONBLOCK)
2194 except BlockingIOError:
2195 # System urandom is not initialized yet
2196 pass
2197
2198 def test_getrandom_value(self):
2199 data1 = os.getrandom(16)
2200 data2 = os.getrandom(16)
2201 self.assertNotEqual(data1, data2)
2202
2203
2204 # os.urandom() doesn't use a file descriptor when it is implemented with the
2205 # getentropy() function, the getrandom() function or the getrandom() syscall
2206 OS_URANDOM_DONT_USE_FD = (
2207 sysconfig.get_config_var('HAVE_GETENTROPY') == 1
2208 or sysconfig.get_config_var('HAVE_GETRANDOM') == 1
2209 or sysconfig.get_config_var('HAVE_GETRANDOM_SYSCALL') == 1)
2210
2211 @unittest.skipIf(OS_URANDOM_DONT_USE_FD ,
2212 "os.random() does not use a file descriptor")
2213 @unittest.skipIf(sys.platform == "vxworks",
2214 "VxWorks can't set RLIMIT_NOFILE to 1")
2215 class URandomFDTests(unittest.TestCase):
2216 @unittest.skipUnless(resource, "test requires the resource module")
2217 def test_urandom_failure(self):
2218 # Check urandom() failing when it is not able to open /dev/random.
2219 # We spawn a new process to make the test more robust (if getrlimit()
2220 # failed to restore the file descriptor limit after this, the whole
2221 # test suite would crash; this actually happened on the OS X Tiger
2222 # buildbot).
2223 code = """if 1:
2224 import errno
2225 import os
2226 import resource
2227
2228 soft_limit, hard_limit = resource.getrlimit(resource.RLIMIT_NOFILE)
2229 resource.setrlimit(resource.RLIMIT_NOFILE, (1, hard_limit))
2230 try:
2231 os.urandom(16)
2232 except OSError as e:
2233 assert e.errno == errno.EMFILE, e.errno
2234 else:
2235 raise AssertionError("OSError not raised")
2236 """
2237 assert_python_ok('-c', code)
2238
2239 def test_urandom_fd_closed(self):
2240 # Issue #21207: urandom() should reopen its fd to /dev/urandom if
2241 # closed.
2242 code = """if 1:
2243 import os
2244 import sys
2245 import test.support
2246 os.urandom(4)
2247 with test.support.SuppressCrashReport():
2248 os.closerange(3, 256)
2249 sys.stdout.buffer.write(os.urandom(4))
2250 """
2251 rc, out, err = assert_python_ok('-Sc', code)
2252
2253 def test_urandom_fd_reopened(self):
2254 # Issue #21207: urandom() should detect its fd to /dev/urandom
2255 # changed to something else, and reopen it.
2256 self.addCleanup(os_helper.unlink, os_helper.TESTFN)
2257 create_file(os_helper.TESTFN, b"x" * 256)
2258
2259 code = """if 1:
2260 import os
2261 import sys
2262 import test.support
2263 os.urandom(4)
2264 with test.support.SuppressCrashReport():
2265 for fd in range(3, 256):
2266 try:
2267 os.close(fd)
2268 except OSError:
2269 pass
2270 else:
2271 # Found the urandom fd (XXX hopefully)
2272 break
2273 os.closerange(3, 256)
2274 with open({TESTFN!r}, 'rb') as f:
2275 new_fd = f.fileno()
2276 # Issue #26935: posix allows new_fd and fd to be equal but
2277 # some libc implementations have dup2 return an error in this
2278 # case.
2279 if new_fd != fd:
2280 os.dup2(new_fd, fd)
2281 sys.stdout.buffer.write(os.urandom(4))
2282 sys.stdout.buffer.write(os.urandom(4))
2283 """.format(TESTFN=os_helper.TESTFN)
2284 rc, out, err = assert_python_ok('-Sc', code)
2285 self.assertEqual(len(out), 8)
2286 self.assertNotEqual(out[0:4], out[4:8])
2287 rc, out2, err2 = assert_python_ok('-Sc', code)
2288 self.assertEqual(len(out2), 8)
2289 self.assertNotEqual(out2, out)
2290
2291
2292 @contextlib.contextmanager
2293 def _execvpe_mockup(defpath=None):
2294 """
2295 Stubs out execv and execve functions when used as context manager.
2296 Records exec calls. The mock execv and execve functions always raise an
2297 exception as they would normally never return.
2298 """
2299 # A list of tuples containing (function name, first arg, args)
2300 # of calls to execv or execve that have been made.
2301 calls = []
2302
2303 def mock_execv(name, *args):
2304 calls.append(('execv', name, args))
2305 raise RuntimeError("execv called")
2306
2307 def mock_execve(name, *args):
2308 calls.append(('execve', name, args))
2309 raise OSError(errno.ENOTDIR, "execve called")
2310
2311 try:
2312 orig_execv = os.execv
2313 orig_execve = os.execve
2314 orig_defpath = os.defpath
2315 os.execv = mock_execv
2316 os.execve = mock_execve
2317 if defpath is not None:
2318 os.defpath = defpath
2319 yield calls
2320 finally:
2321 os.execv = orig_execv
2322 os.execve = orig_execve
2323 os.defpath = orig_defpath
2324
2325 @unittest.skipUnless(hasattr(os, 'execv'),
2326 "need os.execv()")
2327 class ExecTests(unittest.TestCase):
2328 @unittest.skipIf(USING_LINUXTHREADS,
2329 "avoid triggering a linuxthreads bug: see issue #4970")
2330 def test_execvpe_with_bad_program(self):
2331 self.assertRaises(OSError, os.execvpe, 'no such app-',
2332 ['no such app-'], None)
2333
2334 def test_execv_with_bad_arglist(self):
2335 self.assertRaises(ValueError, os.execv, 'notepad', ())
2336 self.assertRaises(ValueError, os.execv, 'notepad', [])
2337 self.assertRaises(ValueError, os.execv, 'notepad', ('',))
2338 self.assertRaises(ValueError, os.execv, 'notepad', [''])
2339
2340 def test_execvpe_with_bad_arglist(self):
2341 self.assertRaises(ValueError, os.execvpe, 'notepad', [], None)
2342 self.assertRaises(ValueError, os.execvpe, 'notepad', [], {})
2343 self.assertRaises(ValueError, os.execvpe, 'notepad', [''], {})
2344
2345 @unittest.skipUnless(hasattr(os, '_execvpe'),
2346 "No internal os._execvpe function to test.")
2347 def _test_internal_execvpe(self, test_type):
2348 program_path = os.sep + 'absolutepath'
2349 if test_type is bytes:
2350 program = b'executable'
2351 fullpath = os.path.join(os.fsencode(program_path), program)
2352 native_fullpath = fullpath
2353 arguments = [b'progname', 'arg1', 'arg2']
2354 else:
2355 program = 'executable'
2356 arguments = ['progname', 'arg1', 'arg2']
2357 fullpath = os.path.join(program_path, program)
2358 if os.name != "nt":
2359 native_fullpath = os.fsencode(fullpath)
2360 else:
2361 native_fullpath = fullpath
2362 env = {'spam': 'beans'}
2363
2364 # test os._execvpe() with an absolute path
2365 with _execvpe_mockup() as calls:
2366 self.assertRaises(RuntimeError,
2367 os._execvpe, fullpath, arguments)
2368 self.assertEqual(len(calls), 1)
2369 self.assertEqual(calls[0], ('execv', fullpath, (arguments,)))
2370
2371 # test os._execvpe() with a relative path:
2372 # os.get_exec_path() returns defpath
2373 with _execvpe_mockup(defpath=program_path) as calls:
2374 self.assertRaises(OSError,
2375 os._execvpe, program, arguments, env=env)
2376 self.assertEqual(len(calls), 1)
2377 self.assertSequenceEqual(calls[0],
2378 ('execve', native_fullpath, (arguments, env)))
2379
2380 # test os._execvpe() with a relative path:
2381 # os.get_exec_path() reads the 'PATH' variable
2382 with _execvpe_mockup() as calls:
2383 env_path = env.copy()
2384 if test_type is bytes:
2385 env_path[b'PATH'] = program_path
2386 else:
2387 env_path['PATH'] = program_path
2388 self.assertRaises(OSError,
2389 os._execvpe, program, arguments, env=env_path)
2390 self.assertEqual(len(calls), 1)
2391 self.assertSequenceEqual(calls[0],
2392 ('execve', native_fullpath, (arguments, env_path)))
2393
2394 def test_internal_execvpe_str(self):
2395 self._test_internal_execvpe(str)
2396 if os.name != "nt":
2397 self._test_internal_execvpe(bytes)
2398
2399 def test_execve_invalid_env(self):
2400 args = [sys.executable, '-c', 'pass']
2401
2402 # null character in the environment variable name
2403 newenv = os.environ.copy()
2404 newenv["FRUIT\0VEGETABLE"] = "cabbage"
2405 with self.assertRaises(ValueError):
2406 os.execve(args[0], args, newenv)
2407
2408 # null character in the environment variable value
2409 newenv = os.environ.copy()
2410 newenv["FRUIT"] = "orange\0VEGETABLE=cabbage"
2411 with self.assertRaises(ValueError):
2412 os.execve(args[0], args, newenv)
2413
2414 # equal character in the environment variable name
2415 newenv = os.environ.copy()
2416 newenv["FRUIT=ORANGE"] = "lemon"
2417 with self.assertRaises(ValueError):
2418 os.execve(args[0], args, newenv)
2419
2420 # See https://github.com/python/cpython/issues/137934 and the other
2421 # related issues for the reason why we cannot test this on Windows.
2422 @unittest.skipIf(os.name == "nt", "POSIX-specific test")
2423 @unittest.skipUnless(unix_shell and os.path.exists(unix_shell),
2424 "requires a shell")
2425 def test_execve_env_concurrent_mutation_with_fspath_posix(self):
2426 # Prevent crash when mutating environment during parsing.
2427 # Regression test for https://github.com/python/cpython/issues/143309.
2428
2429 message = "hello from execve"
2430 code = """
2431 import os, sys
2432
2433 class MyPath:
2434 def __fspath__(self):
2435 mutated.clear()
2436 return b"pwn"
2437
2438 mutated = KEYS = VALUES = [MyPath()]
2439
2440 class MyEnv:
2441 def __getitem__(self): raise RuntimeError("must not be called")
2442 def __len__(self): return 1
2443 def keys(self): return KEYS
2444 def values(self): return VALUES
2445
2446 args = [{unix_shell!r}, '-c', 'echo \"{message!s}\"']
2447 os.execve(args[0], args, MyEnv())
2448 """.format(unix_shell=unix_shell, message=message)
2449
2450 # Make sure to forward "LD_*" variables so that assert_python_ok()
2451 # can run correctly.
2452 minimal = {k: v for k, v in os.environ.items() if k.startswith("LD_")}
2453 with os_helper.EnvironmentVarGuard() as env:
2454 env.clear()
2455 env.update(minimal)
2456 _, out, _ = assert_python_ok('-c', code, **env)
2457 self.assertIn(bytes(message, "ascii"), out)
2458
2459 @unittest.skipUnless(sys.platform == "win32", "Win32-specific test")
2460 def test_execve_with_empty_path(self):
2461 # bpo-32890: Check GetLastError() misuse
2462 try:
2463 os.execve('', ['arg'], {})
2464 except OSError as e:
2465 self.assertTrue(e.winerror is None or e.winerror != 0)
2466 else:
2467 self.fail('No OSError raised')
2468
2469
2470 @unittest.skipUnless(sys.platform == "win32", "Win32 specific tests")
2471 class Win32ErrorTests(unittest.TestCase):
2472 def setUp(self):
2473 try:
2474 os.stat(os_helper.TESTFN)
2475 except FileNotFoundError:
2476 exists = False
2477 except OSError as exc:
2478 exists = True
2479 self.fail("file %s must not exist; os.stat failed with %s"
2480 % (os_helper.TESTFN, exc))
2481 else:
2482 self.fail("file %s must not exist" % os_helper.TESTFN)
2483
2484 def test_rename(self):
2485 self.assertRaises(OSError, os.rename, os_helper.TESTFN, os_helper.TESTFN+".bak")
2486
2487 def test_remove(self):
2488 self.assertRaises(OSError, os.remove, os_helper.TESTFN)
2489
2490 def test_chdir(self):
2491 self.assertRaises(OSError, os.chdir, os_helper.TESTFN)
2492
2493 def test_mkdir(self):
2494 self.addCleanup(os_helper.unlink, os_helper.TESTFN)
2495
2496 with open(os_helper.TESTFN, "x") as f:
2497 self.assertRaises(OSError, os.mkdir, os_helper.TESTFN)
2498
2499 def test_utime(self):
2500 self.assertRaises(OSError, os.utime, os_helper.TESTFN, None)
2501
2502 def test_chmod(self):
2503 self.assertRaises(OSError, os.chmod, os_helper.TESTFN, 0)
2504
2505
2506 @unittest.skipIf(support.is_wasi, "Cannot create invalid FD on WASI.")
2507 class TestInvalidFD(unittest.TestCase):
2508 singles = ["fchdir", "dup", "fstat", "fstatvfs", "tcgetpgrp", "ttyname"]
2509 singles_fildes = {"fchdir"}
2510 # systemd-nspawn --suppress-sync=true does not verify fd passed
2511 # fdatasync() and fsync(), and always returns success
2512 if not support.in_systemd_nspawn_sync_suppressed():
2513 singles += ["fdatasync", "fsync"]
2514 singles_fildes |= {"fdatasync", "fsync"}
2515 #singles.append("close")
2516 #We omit close because it doesn't raise an exception on some platforms
2517 def get_single(f):
2518 def helper(self):
2519 if hasattr(os, f):
2520 self.check(getattr(os, f))
2521 if f in self.singles_fildes:
2522 self.check_bool(getattr(os, f))
2523 return helper
2524 for f in singles:
2525 locals()["test_"+f] = get_single(f)
2526
2527 def check(self, f, *args, **kwargs):
2528 try:
2529 f(os_helper.make_bad_fd(), *args, **kwargs)
2530 except OSError as e:
2531 self.assertEqual(e.errno, errno.EBADF)
2532 else:
2533 self.fail("%r didn't raise an OSError with a bad file descriptor"
2534 % f)
2535
2536 def check_bool(self, f, *args, **kwargs):
2537 with warnings.catch_warnings():
2538 warnings.simplefilter("error", RuntimeWarning)
2539 for fd in False, True:
2540 with self.assertRaises(RuntimeWarning):
2541 f(fd, *args, **kwargs)
2542
2543 def test_fdopen(self):
2544 self.check(os.fdopen, encoding="utf-8")
2545 self.check_bool(os.fdopen, encoding="utf-8")
2546
2547 @unittest.skipUnless(hasattr(os, 'isatty'), 'test needs os.isatty()')
2548 def test_isatty(self):
2549 self.assertEqual(os.isatty(os_helper.make_bad_fd()), False)
2550
2551 @unittest.skipUnless(hasattr(os, 'closerange'), 'test needs os.closerange()')
2552 def test_closerange(self):
2553 fd = os_helper.make_bad_fd()
2554 # Make sure none of the descriptors we are about to close are
2555 # currently valid (issue 6542).
2556 for i in range(10):
2557 try: os.fstat(fd+i)
2558 except OSError:
2559 pass
2560 else:
2561 break
2562 if i < 2:
2563 raise unittest.SkipTest(
2564 "Unable to acquire a range of invalid file descriptors")
2565 self.assertEqual(os.closerange(fd, fd + i-1), None)
2566
2567 @unittest.skipUnless(hasattr(os, 'dup2'), 'test needs os.dup2()')
2568 def test_dup2(self):
2569 self.check(os.dup2, 20)
2570
2571 @unittest.skipUnless(hasattr(os, 'dup2'), 'test needs os.dup2()')
2572 def test_dup2_negative_fd(self):
2573 valid_fd = os.open(__file__, os.O_RDONLY)
2574 self.addCleanup(os.close, valid_fd)
2575 fds = [
2576 valid_fd,
2577 -1,
2578 -2**31,
2579 ]
2580 for fd, fd2 in itertools.product(fds, repeat=2):
2581 if fd != fd2:
2582 with self.subTest(fd=fd, fd2=fd2):
2583 with self.assertRaises(OSError) as ctx:
2584 os.dup2(fd, fd2)
2585 self.assertEqual(ctx.exception.errno, errno.EBADF)
2586
2587 @unittest.skipUnless(hasattr(os, 'fchmod'), 'test needs os.fchmod()')
2588 def test_fchmod(self):
2589 self.check(os.fchmod, 0)
2590
2591 @unittest.skipUnless(hasattr(os, 'fchown'), 'test needs os.fchown()')
2592 def test_fchown(self):
2593 self.check(os.fchown, -1, -1)
2594
2595 @unittest.skipUnless(hasattr(os, 'fpathconf'), 'test needs os.fpathconf()')
2596 def test_fpathconf(self):
2597 self.assertIn("PC_NAME_MAX", os.pathconf_names)
2598 self.check_bool(os.pathconf, "PC_NAME_MAX")
2599 self.check_bool(os.fpathconf, "PC_NAME_MAX")
2600
2601 @unittest.skipUnless(hasattr(os, 'fpathconf'), 'test needs os.fpathconf()')
2602 @unittest.skipIf(
2603 support.linked_to_musl(),
2604 'musl pathconf ignores the file descriptor and returns a constant',
2605 )
2606 def test_fpathconf_bad_fd(self):
2607 self.check(os.pathconf, "PC_NAME_MAX")
2608 self.check(os.fpathconf, "PC_NAME_MAX")
2609
2610 @unittest.skipUnless(hasattr(os, 'pathconf'), 'test needs os.pathconf()')
2611 @unittest.skipIf(
2612 support.linked_to_musl(),
2613 'musl fpathconf ignores the file descriptor and returns a constant',
2614 )
2615 def test_pathconf_negative_fd_uses_fd_semantics(self):
2616 with self.assertRaises(OSError) as ctx:
2617 os.pathconf(-1, 1)
2618 self.assertEqual(ctx.exception.errno, errno.EBADF)
2619
2620 @unittest.skipUnless(hasattr(os, 'ftruncate'), 'test needs os.ftruncate()')
2621 def test_ftruncate(self):
2622 self.check(os.truncate, 0)
2623 self.check(os.ftruncate, 0)
2624 self.check_bool(os.truncate, 0)
2625
2626 @unittest.skipUnless(hasattr(os, 'lseek'), 'test needs os.lseek()')
2627 def test_lseek(self):
2628 self.check(os.lseek, 0, 0)
2629
2630 @unittest.skipUnless(hasattr(os, 'read'), 'test needs os.read()')
2631 def test_read(self):
2632 self.check(os.read, 1)
2633
2634 @unittest.skipUnless(hasattr(os, 'readinto'), 'test needs os.readinto()')
2635 def test_readinto(self):
2636 self.check(os.readinto, bytearray(5))
2637
2638 @unittest.skipUnless(hasattr(os, 'readv'), 'test needs os.readv()')
2639 def test_readv(self):
2640 buf = bytearray(10)
2641 self.check(os.readv, [buf])
2642
2643 @unittest.skipUnless(hasattr(os, 'tcsetpgrp'), 'test needs os.tcsetpgrp()')
2644 def test_tcsetpgrpt(self):
2645 self.check(os.tcsetpgrp, 0)
2646
2647 @unittest.skipUnless(hasattr(os, 'write'), 'test needs os.write()')
2648 def test_write(self):
2649 self.check(os.write, b" ")
2650
2651 @unittest.skipUnless(hasattr(os, 'writev'), 'test needs os.writev()')
2652 def test_writev(self):
2653 self.check(os.writev, [b'abc'])
2654
2655 @support.requires_subprocess()
2656 def test_inheritable(self):
2657 self.check(os.get_inheritable)
2658 self.check(os.set_inheritable, True)
2659
2660 @unittest.skipUnless(hasattr(os, 'get_blocking'),
2661 'needs os.get_blocking() and os.set_blocking()')
2662 def test_blocking(self):
2663 self.check(os.get_blocking)
2664 self.check(os.set_blocking, True)
2665
2666
2667 @unittest.skipUnless(hasattr(os, 'link'), 'requires os.link')
2668 class LinkTests(unittest.TestCase):
2669 def setUp(self):
2670 self.file1 = os_helper.TESTFN
2671 self.file2 = os.path.join(os_helper.TESTFN + "2")
2672
2673 def tearDown(self):
2674 for file in (self.file1, self.file2):
2675 if os.path.exists(file):
2676 os.unlink(file)
2677
2678 def _test_link(self, file1, file2):
2679 create_file(file1)
2680
2681 try:
2682 os.link(file1, file2)
2683 except PermissionError as e:
2684 self.skipTest('os.link(): %s' % e)
2685 with open(file1, "rb") as f1, open(file2, "rb") as f2:
2686 self.assertTrue(os.path.sameopenfile(f1.fileno(), f2.fileno()))
2687
2688 def test_link(self):
2689 self._test_link(self.file1, self.file2)
2690
2691 def test_link_bytes(self):
2692 self._test_link(bytes(self.file1, sys.getfilesystemencoding()),
2693 bytes(self.file2, sys.getfilesystemencoding()))
2694
2695 def test_unicode_name(self):
2696 try:
2697 os.fsencode("\xf1")
2698 except UnicodeError:
2699 raise unittest.SkipTest("Unable to encode for this platform.")
2700
2701 self.file1 += "\xf1"
2702 self.file2 = self.file1 + "2"
2703 self._test_link(self.file1, self.file2)
2704
2705 @unittest.skipIf(sys.platform == "win32", "Posix specific tests")
2706 class PosixUidGidTests(unittest.TestCase):
2707 # uid_t and gid_t are 32-bit unsigned integers on Linux
2708 UID_OVERFLOW = (1 << 32)
2709 GID_OVERFLOW = (1 << 32)
2710
2711 @unittest.skipUnless(hasattr(os, 'setuid'), 'test needs os.setuid()')
2712 def test_setuid(self):
2713 if os.getuid() != 0:
2714 self.assertRaises(OSError, os.setuid, 0)
2715 self.assertRaises(TypeError, os.setuid, 'not an int')
2716 self.assertRaises(OverflowError, os.setuid, self.UID_OVERFLOW)
2717
2718 @unittest.skipUnless(hasattr(os, 'setgid'), 'test needs os.setgid()')
2719 def test_setgid(self):
2720 if os.getuid() != 0 and not HAVE_WHEEL_GROUP:
2721 self.assertRaises(OSError, os.setgid, 0)
2722 self.assertRaises(TypeError, os.setgid, 'not an int')
2723 self.assertRaises(OverflowError, os.setgid, self.GID_OVERFLOW)
2724
2725 @unittest.skipUnless(hasattr(os, 'seteuid'), 'test needs os.seteuid()')
2726 def test_seteuid(self):
2727 if os.getuid() != 0:
2728 self.assertRaises(OSError, os.seteuid, 0)
2729 self.assertRaises(TypeError, os.setegid, 'not an int')
2730 self.assertRaises(OverflowError, os.seteuid, self.UID_OVERFLOW)
2731
2732 @unittest.skipUnless(hasattr(os, 'setegid'), 'test needs os.setegid()')
2733 def test_setegid(self):
2734 if os.getuid() != 0 and not HAVE_WHEEL_GROUP:
2735 self.assertRaises(OSError, os.setegid, 0)
2736 self.assertRaises(TypeError, os.setegid, 'not an int')
2737 self.assertRaises(OverflowError, os.setegid, self.GID_OVERFLOW)
2738
2739 @unittest.skipUnless(hasattr(os, 'setreuid'), 'test needs os.setreuid()')
2740 def test_setreuid(self):
2741 if os.getuid() != 0:
2742 self.assertRaises(OSError, os.setreuid, 0, 0)
2743 self.assertRaises(TypeError, os.setreuid, 'not an int', 0)
2744 self.assertRaises(TypeError, os.setreuid, 0, 'not an int')
2745 self.assertRaises(OverflowError, os.setreuid, self.UID_OVERFLOW, 0)
2746 self.assertRaises(OverflowError, os.setreuid, 0, self.UID_OVERFLOW)
2747
2748 @unittest.skipUnless(hasattr(os, 'setreuid'), 'test needs os.setreuid()')
2749 @support.requires_subprocess()
2750 def test_setreuid_neg1(self):
2751 # Needs to accept -1. We run this in a subprocess to avoid
2752 # altering the test runner's process state (issue8045).
2753 subprocess.check_call([
2754 sys.executable, '-c',
2755 'import os,sys;os.setreuid(-1,-1);sys.exit(0)'])
2756
2757 @unittest.skipUnless(hasattr(os, 'setregid'), 'test needs os.setregid()')
2758 @support.requires_subprocess()
2759 def test_setregid(self):
2760 if os.getuid() != 0 and not HAVE_WHEEL_GROUP:
2761 self.assertRaises(OSError, os.setregid, 0, 0)
2762 self.assertRaises(TypeError, os.setregid, 'not an int', 0)
2763 self.assertRaises(TypeError, os.setregid, 0, 'not an int')
2764 self.assertRaises(OverflowError, os.setregid, self.GID_OVERFLOW, 0)
2765 self.assertRaises(OverflowError, os.setregid, 0, self.GID_OVERFLOW)
2766
2767 @unittest.skipUnless(hasattr(os, 'setregid'), 'test needs os.setregid()')
2768 @support.requires_subprocess()
2769 def test_setregid_neg1(self):
2770 # Needs to accept -1. We run this in a subprocess to avoid
2771 # altering the test runner's process state (issue8045).
2772 subprocess.check_call([
2773 sys.executable, '-c',
2774 'import os,sys;os.setregid(-1,-1);sys.exit(0)'])
2775
2776 @unittest.skipIf(sys.platform == "win32", "Posix specific tests")
2777 class Pep383Tests(unittest.TestCase):
2778 def setUp(self):
2779 if os_helper.TESTFN_UNENCODABLE:
2780 self.dir = os_helper.TESTFN_UNENCODABLE
2781 elif os_helper.TESTFN_NONASCII:
2782 self.dir = os_helper.TESTFN_NONASCII
2783 else:
2784 self.dir = os_helper.TESTFN
2785 self.bdir = os.fsencode(self.dir)
2786
2787 bytesfn = []
2788 def add_filename(fn):
2789 try:
2790 fn = os.fsencode(fn)
2791 except UnicodeEncodeError:
2792 return
2793 bytesfn.append(fn)
2794 add_filename(os_helper.TESTFN_UNICODE)
2795 if os_helper.TESTFN_UNENCODABLE:
2796 add_filename(os_helper.TESTFN_UNENCODABLE)
2797 if os_helper.TESTFN_NONASCII:
2798 add_filename(os_helper.TESTFN_NONASCII)
2799 if not bytesfn:
2800 self.skipTest("couldn't create any non-ascii filename")
2801
2802 self.unicodefn = set()
2803 os.mkdir(self.dir)
2804 try:
2805 for fn in bytesfn:
2806 os_helper.create_empty_file(os.path.join(self.bdir, fn))
2807 fn = os.fsdecode(fn)
2808 if fn in self.unicodefn:
2809 raise ValueError("duplicate filename")
2810 self.unicodefn.add(fn)
2811 except:
2812 shutil.rmtree(self.dir)
2813 raise
2814
2815 def tearDown(self):
2816 shutil.rmtree(self.dir)
2817
2818 def test_listdir(self):
2819 expected = self.unicodefn
2820 found = set(os.listdir(self.dir))
2821 self.assertEqual(found, expected)
2822 # test listdir without arguments
2823 current_directory = os.getcwd()
2824 try:
2825 # The root directory is not readable on Android, so use a directory
2826 # we created ourselves.
2827 os.chdir(self.dir)
2828 self.assertEqual(set(os.listdir()), expected)
2829 finally:
2830 os.chdir(current_directory)
2831
2832 def test_open(self):
2833 for fn in self.unicodefn:
2834 f = open(os.path.join(self.dir, fn), 'rb')
2835 f.close()
2836
2837 @unittest.skipUnless(hasattr(os, 'statvfs'),
2838 "need os.statvfs()")
2839 def test_statvfs(self):
2840 # issue #9645
2841 for fn in self.unicodefn:
2842 # should not fail with file not found error
2843 fullname = os.path.join(self.dir, fn)
2844 os.statvfs(fullname)
2845
2846 def test_stat(self):
2847 for fn in self.unicodefn:
2848 os.stat(os.path.join(self.dir, fn))
2849
2850 @unittest.skipUnless(sys.platform == "win32", "Win32 specific tests")
2851 class Win32KillTests(unittest.TestCase):
2852 def _kill(self, sig):
2853 # Start sys.executable as a subprocess and communicate from the
2854 # subprocess to the parent that the interpreter is ready. When it
2855 # becomes ready, send *sig* via os.kill to the subprocess and check
2856 # that the return code is equal to *sig*.
2857 import ctypes
2858 from ctypes import wintypes
2859 import msvcrt
2860
2861 # Since we can't access the contents of the process' stdout until the
2862 # process has exited, use PeekNamedPipe to see what's inside stdout
2863 # without waiting. This is done so we can tell that the interpreter
2864 # is started and running at a point where it could handle a signal.
2865 PeekNamedPipe = ctypes.windll.kernel32.PeekNamedPipe
2866 PeekNamedPipe.restype = wintypes.BOOL
2867 PeekNamedPipe.argtypes = (wintypes.HANDLE, # Pipe handle
2868 ctypes.POINTER(ctypes.c_char), # stdout buf
2869 wintypes.DWORD, # Buffer size
2870 ctypes.POINTER(wintypes.DWORD), # bytes read
2871 ctypes.POINTER(wintypes.DWORD), # bytes avail
2872 ctypes.POINTER(wintypes.DWORD)) # bytes left
2873 msg = "running"
2874 proc = subprocess.Popen([sys.executable, "-c",
2875 "import sys;"
2876 "sys.stdout.write('{}');"
2877 "sys.stdout.flush();"
2878 "input()".format(msg)],
2879 stdout=subprocess.PIPE,
2880 stderr=subprocess.PIPE,
2881 stdin=subprocess.PIPE)
2882 self.addCleanup(proc.stdout.close)
2883 self.addCleanup(proc.stderr.close)
2884 self.addCleanup(proc.stdin.close)
2885
2886 count, max = 0, 100
2887 while count < max and proc.poll() is None:
2888 # Create a string buffer to store the result of stdout from the pipe
2889 buf = ctypes.create_string_buffer(len(msg))
2890 # Obtain the text currently in proc.stdout
2891 # Bytes read/avail/left are left as NULL and unused
2892 rslt = PeekNamedPipe(msvcrt.get_osfhandle(proc.stdout.fileno()),
2893 buf, ctypes.sizeof(buf), None, None, None)
2894 self.assertNotEqual(rslt, 0, "PeekNamedPipe failed")
2895 if buf.value:
2896 self.assertEqual(msg, buf.value.decode())
2897 break
2898 time.sleep(0.1)
2899 count += 1
2900 else:
2901 self.fail("Did not receive communication from the subprocess")
2902
2903 os.kill(proc.pid, sig)
2904 self.assertEqual(proc.wait(), sig)
2905
2906 def test_kill_sigterm(self):
2907 # SIGTERM doesn't mean anything special, but make sure it works
2908 self._kill(signal.SIGTERM)
2909
2910 def test_kill_int(self):
2911 # os.kill on Windows can take an int which gets set as the exit code
2912 self._kill(100)
2913
2914 @unittest.skipIf(mmap is None, "requires mmap")
2915 def _kill_with_event(self, event, name):
2916 tagname = "test_os_%s" % uuid.uuid1()
2917 m = mmap.mmap(-1, 1, tagname)
2918 m[0] = 0
2919
2920 # Run a script which has console control handling enabled.
2921 script = os.path.join(os.path.dirname(__file__),
2922 "win_console_handler.py")
2923 cmd = [sys.executable, script, tagname]
2924 proc = subprocess.Popen(cmd,
2925 creationflags=subprocess.CREATE_NEW_PROCESS_GROUP)
2926
2927 with proc:
2928 # Let the interpreter startup before we send signals. See #3137.
2929 for _ in support.sleeping_retry(support.SHORT_TIMEOUT):
2930 if proc.poll() is None:
2931 break
2932 else:
2933 # Forcefully kill the process if we weren't able to signal it.
2934 proc.kill()
2935 self.fail("Subprocess didn't finish initialization")
2936
2937 os.kill(proc.pid, event)
2938
2939 try:
2940 # proc.send_signal(event) could also be done here.
2941 # Allow time for the signal to be passed and the process to exit.
2942 proc.wait(timeout=support.SHORT_TIMEOUT)
2943 except subprocess.TimeoutExpired:
2944 # Forcefully kill the process if we weren't able to signal it.
2945 proc.kill()
2946 self.fail("subprocess did not stop on {}".format(name))
2947
2948 @unittest.skip("subprocesses aren't inheriting Ctrl+C property")
2949 @support.requires_subprocess()
2950 def test_CTRL_C_EVENT(self):
2951 from ctypes import wintypes
2952 import ctypes
2953
2954 # Make a NULL value by creating a pointer with no argument.
2955 NULL = ctypes.POINTER(ctypes.c_int)()
2956 SetConsoleCtrlHandler = ctypes.windll.kernel32.SetConsoleCtrlHandler
2957 SetConsoleCtrlHandler.argtypes = (ctypes.POINTER(ctypes.c_int),
2958 wintypes.BOOL)
2959 SetConsoleCtrlHandler.restype = wintypes.BOOL
2960
2961 # Calling this with NULL and FALSE causes the calling process to
2962 # handle Ctrl+C, rather than ignore it. This property is inherited
2963 # by subprocesses.
2964 SetConsoleCtrlHandler(NULL, 0)
2965
2966 self._kill_with_event(signal.CTRL_C_EVENT, "CTRL_C_EVENT")
2967
2968 @support.requires_subprocess()
2969 def test_CTRL_BREAK_EVENT(self):
2970 self._kill_with_event(signal.CTRL_BREAK_EVENT, "CTRL_BREAK_EVENT")
2971
2972
2973 @unittest.skipUnless(sys.platform == "win32", "Win32 specific tests")
2974 class Win32ListdirTests(unittest.TestCase):
2975 """Test listdir on Windows."""
2976
2977 def setUp(self):
2978 self.created_paths = []
2979 for i in range(2):
2980 dir_name = 'SUB%d' % i
2981 dir_path = os.path.join(os_helper.TESTFN, dir_name)
2982 file_name = 'FILE%d' % i
2983 file_path = os.path.join(os_helper.TESTFN, file_name)
2984 os.makedirs(dir_path)
2985 with open(file_path, 'w', encoding='utf-8') as f:
2986 f.write("I'm %s and proud of it. Blame test_os.\n" % file_path)
2987 self.created_paths.extend([dir_name, file_name])
2988 self.created_paths.sort()
2989
2990 def tearDown(self):
2991 shutil.rmtree(os_helper.TESTFN)
2992
2993 def test_listdir_no_extended_path(self):
2994 """Test when the path is not an "extended" path."""
2995 # unicode
2996 self.assertEqual(
2997 sorted(os.listdir(os_helper.TESTFN)),
2998 self.created_paths)
2999
3000 # bytes
3001 self.assertEqual(
3002 sorted(os.listdir(os.fsencode(os_helper.TESTFN))),
3003 [os.fsencode(path) for path in self.created_paths])
3004
3005 def test_listdir_extended_path(self):
3006 """Test when the path starts with '\\\\?\\'."""
3007 # See: http://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx#maxpath
3008 # unicode
3009 path = '\\\\?\\' + os.path.abspath(os_helper.TESTFN)
3010 self.assertEqual(
3011 sorted(os.listdir(path)),
3012 self.created_paths)
3013
3014 # bytes
3015 path = b'\\\\?\\' + os.fsencode(os.path.abspath(os_helper.TESTFN))
3016 self.assertEqual(
3017 sorted(os.listdir(path)),
3018 [os.fsencode(path) for path in self.created_paths])
3019
3020
3021 @unittest.skipUnless(os.name == "nt", "NT specific tests")
3022 class Win32ListdriveTests(unittest.TestCase):
3023 """Test listdrive, listmounts and listvolume on Windows."""
3024
3025 def setUp(self):
3026 # Get drives and volumes from fsutil
3027 out = subprocess.check_output(
3028 ["fsutil.exe", "volume", "list"],
3029 cwd=os.path.join(os.getenv("SystemRoot", "\\Windows"), "System32"),
3030 encoding="mbcs",
3031 errors="ignore",
3032 )
3033 lines = out.splitlines()
3034 self.known_volumes = {l for l in lines if l.startswith('\\\\?\\')}
3035 self.known_drives = {l for l in lines if l[1:] == ':\\'}
3036 self.known_mounts = {l for l in lines if l[1:3] == ':\\'}
3037
3038 def test_listdrives(self):
3039 drives = os.listdrives()
3040 self.assertIsInstance(drives, list)
3041 self.assertSetEqual(
3042 self.known_drives,
3043 self.known_drives & set(drives),
3044 )
3045
3046 def test_listvolumes(self):
3047 volumes = os.listvolumes()
3048 self.assertIsInstance(volumes, list)
3049 self.assertSetEqual(
3050 self.known_volumes,
3051 self.known_volumes & set(volumes),
3052 )
3053
3054 def test_listmounts(self):
3055 for volume in os.listvolumes():
3056 try:
3057 mounts = os.listmounts(volume)
3058 except OSError as ex:
3059 if support.verbose:
3060 print("Skipping", volume, "because of", ex)
3061 else:
3062 self.assertIsInstance(mounts, list)
3063 self.assertSetEqual(
3064 set(mounts),
3065 self.known_mounts & set(mounts),
3066 )
3067
3068
3069 @unittest.skipUnless(hasattr(os, 'readlink'), 'needs os.readlink()')
3070 class ReadlinkTests(unittest.TestCase):
3071 filelink = 'readlinktest'
3072 filelink_target = os.path.abspath(__file__)
3073 filelinkb = os.fsencode(filelink)
3074 filelinkb_target = os.fsencode(filelink_target)
3075
3076 def assertPathEqual(self, left, right):
3077 left = os.path.normcase(left)
3078 right = os.path.normcase(right)
3079 if sys.platform == 'win32':
3080 # Bad practice to blindly strip the prefix as it may be required to
3081 # correctly refer to the file, but we're only comparing paths here.
3082 has_prefix = lambda p: p.startswith(
3083 b'\\\\?\\' if isinstance(p, bytes) else '\\\\?\\')
3084 if has_prefix(left):
3085 left = left[4:]
3086 if has_prefix(right):
3087 right = right[4:]
3088 self.assertEqual(left, right)
3089
3090 def setUp(self):
3091 self.assertTrue(os.path.exists(self.filelink_target))
3092 self.assertTrue(os.path.exists(self.filelinkb_target))
3093 self.assertFalse(os.path.exists(self.filelink))
3094 self.assertFalse(os.path.exists(self.filelinkb))
3095
3096 def test_not_symlink(self):
3097 filelink_target = FakePath(self.filelink_target)
3098 self.assertRaises(OSError, os.readlink, self.filelink_target)
3099 self.assertRaises(OSError, os.readlink, filelink_target)
3100
3101 def test_missing_link(self):
3102 self.assertRaises(FileNotFoundError, os.readlink, 'missing-link')
3103 self.assertRaises(FileNotFoundError, os.readlink,
3104 FakePath('missing-link'))
3105
3106 @os_helper.skip_unless_symlink
3107 def test_pathlike(self):
3108 os.symlink(self.filelink_target, self.filelink)
3109 self.addCleanup(os_helper.unlink, self.filelink)
3110 filelink = FakePath(self.filelink)
3111 self.assertPathEqual(os.readlink(filelink), self.filelink_target)
3112
3113 @os_helper.skip_unless_symlink
3114 def test_pathlike_bytes(self):
3115 os.symlink(self.filelinkb_target, self.filelinkb)
3116 self.addCleanup(os_helper.unlink, self.filelinkb)
3117 path = os.readlink(FakePath(self.filelinkb))
3118 self.assertPathEqual(path, self.filelinkb_target)
3119 self.assertIsInstance(path, bytes)
3120
3121 @os_helper.skip_unless_symlink
3122 def test_bytes(self):
3123 os.symlink(self.filelinkb_target, self.filelinkb)
3124 self.addCleanup(os_helper.unlink, self.filelinkb)
3125 path = os.readlink(self.filelinkb)
3126 self.assertPathEqual(path, self.filelinkb_target)
3127 self.assertIsInstance(path, bytes)
3128
3129
3130 @unittest.skipUnless(sys.platform == "win32", "Win32 specific tests")
3131 @os_helper.skip_unless_symlink
3132 class Win32SymlinkTests(unittest.TestCase):
3133 filelink = 'filelinktest'
3134 filelink_target = os.path.abspath(__file__)
3135 dirlink = 'dirlinktest'
3136 dirlink_target = os.path.dirname(filelink_target)
3137 missing_link = 'missing link'
3138
3139 def setUp(self):
3140 assert os.path.exists(self.dirlink_target)
3141 assert os.path.exists(self.filelink_target)
3142 assert not os.path.exists(self.dirlink)
3143 assert not os.path.exists(self.filelink)
3144 assert not os.path.exists(self.missing_link)
3145
3146 def tearDown(self):
3147 if os.path.exists(self.filelink):
3148 os.remove(self.filelink)
3149 if os.path.exists(self.dirlink):
3150 os.rmdir(self.dirlink)
3151 if os.path.lexists(self.missing_link):
3152 os.remove(self.missing_link)
3153
3154 def test_directory_link(self):
3155 os.symlink(self.dirlink_target, self.dirlink)
3156 self.assertTrue(os.path.exists(self.dirlink))
3157 self.assertTrue(os.path.isdir(self.dirlink))
3158 self.assertTrue(os.path.islink(self.dirlink))
3159 self.check_stat(self.dirlink, self.dirlink_target)
3160
3161 def test_file_link(self):
3162 os.symlink(self.filelink_target, self.filelink)
3163 self.assertTrue(os.path.exists(self.filelink))
3164 self.assertTrue(os.path.isfile(self.filelink))
3165 self.assertTrue(os.path.islink(self.filelink))
3166 self.check_stat(self.filelink, self.filelink_target)
3167
3168 def _create_missing_dir_link(self):
3169 'Create a "directory" link to a non-existent target'
3170 linkname = self.missing_link
3171 if os.path.lexists(linkname):
3172 os.remove(linkname)
3173 target = r'c:\\target does not exist.29r3c740'
3174 assert not os.path.exists(target)
3175 target_is_dir = True
3176 os.symlink(target, linkname, target_is_dir)
3177
3178 def test_remove_directory_link_to_missing_target(self):
3179 self._create_missing_dir_link()
3180 # For compatibility with Unix, os.remove will check the
3181 # directory status and call RemoveDirectory if the symlink
3182 # was created with target_is_dir==True.
3183 os.remove(self.missing_link)
3184
3185 def test_isdir_on_directory_link_to_missing_target(self):
3186 self._create_missing_dir_link()
3187 self.assertFalse(os.path.isdir(self.missing_link))
3188
3189 def test_rmdir_on_directory_link_to_missing_target(self):
3190 self._create_missing_dir_link()
3191 os.rmdir(self.missing_link)
3192
3193 def check_stat(self, link, target):
3194 self.assertEqual(os.stat(link), os.stat(target))
3195 self.assertNotEqual(os.lstat(link), os.stat(link))
3196
3197 bytes_link = os.fsencode(link)
3198 self.assertEqual(os.stat(bytes_link), os.stat(target))
3199 self.assertNotEqual(os.lstat(bytes_link), os.stat(bytes_link))
3200
3201 def test_12084(self):
3202 level1 = os.path.abspath(os_helper.TESTFN)
3203 level2 = os.path.join(level1, "level2")
3204 level3 = os.path.join(level2, "level3")
3205 self.addCleanup(os_helper.rmtree, level1)
3206
3207 os.mkdir(level1)
3208 os.mkdir(level2)
3209 os.mkdir(level3)
3210
3211 file1 = os.path.abspath(os.path.join(level1, "file1"))
3212 create_file(file1)
3213
3214 orig_dir = os.getcwd()
3215 try:
3216 os.chdir(level2)
3217 link = os.path.join(level2, "link")
3218 os.symlink(os.path.relpath(file1), "link")
3219 self.assertIn("link", os.listdir(os.getcwd()))
3220
3221 # Check os.stat calls from the same dir as the link
3222 self.assertEqual(os.stat(file1), os.stat("link"))
3223
3224 # Check os.stat calls from a dir below the link
3225 os.chdir(level1)
3226 self.assertEqual(os.stat(file1),
3227 os.stat(os.path.relpath(link)))
3228
3229 # Check os.stat calls from a dir above the link
3230 os.chdir(level3)
3231 self.assertEqual(os.stat(file1),
3232 os.stat(os.path.relpath(link)))
3233 finally:
3234 os.chdir(orig_dir)
3235
3236 @unittest.skipUnless(os.path.lexists(r'C:\Users\All Users')
3237 and os.path.exists(r'C:\ProgramData'),
3238 'Test directories not found')
3239 def test_29248(self):
3240 # os.symlink() calls CreateSymbolicLink, which creates
3241 # the reparse data buffer with the print name stored
3242 # first, so the offset is always 0. CreateSymbolicLink
3243 # stores the "PrintName" DOS path (e.g. "C:\") first,
3244 # with an offset of 0, followed by the "SubstituteName"
3245 # NT path (e.g. "\??\C:\"). The "All Users" link, on
3246 # the other hand, seems to have been created manually
3247 # with an inverted order.
3248 target = os.readlink(r'C:\Users\All Users')
3249 self.assertTrue(os.path.samefile(target, r'C:\ProgramData'))
3250
3251 def test_buffer_overflow(self):
3252 # Older versions would have a buffer overflow when detecting
3253 # whether a link source was a directory. This test ensures we
3254 # no longer crash, but does not otherwise validate the behavior
3255 segment = 'X' * 27
3256 path = os.path.join(*[segment] * 10)
3257 test_cases = [
3258 # overflow with absolute src
3259 ('\\' + path, segment),
3260 # overflow dest with relative src
3261 (segment, path),
3262 # overflow when joining src
3263 (path[:180], path[:180]),
3264 ]
3265 for src, dest in test_cases:
3266 try:
3267 os.symlink(src, dest)
3268 except FileNotFoundError:
3269 pass
3270 else:
3271 try:
3272 os.remove(dest)
3273 except OSError:
3274 pass
3275 # Also test with bytes, since that is a separate code path.
3276 try:
3277 os.symlink(os.fsencode(src), os.fsencode(dest))
3278 except FileNotFoundError:
3279 pass
3280 else:
3281 try:
3282 os.remove(dest)
3283 except OSError:
3284 pass
3285
3286 def test_appexeclink(self):
3287 root = os.path.expandvars(r'%LOCALAPPDATA%\Microsoft\WindowsApps')
3288 if not os.path.isdir(root):
3289 self.skipTest("test requires a WindowsApps directory")
3290
3291 aliases = [os.path.join(root, a)
3292 for a in fnmatch.filter(os.listdir(root), '*.exe')]
3293
3294 for alias in aliases:
3295 if support.verbose:
3296 print()
3297 print("Testing with", alias)
3298 st = os.lstat(alias)
3299 self.assertEqual(st, os.stat(alias))
3300 self.assertFalse(stat.S_ISLNK(st.st_mode))
3301 self.assertEqual(st.st_reparse_tag, stat.IO_REPARSE_TAG_APPEXECLINK)
3302 self.assertTrue(os.path.isfile(alias))
3303 # testing the first one we see is sufficient
3304 break
3305 else:
3306 self.skipTest("test requires an app execution alias")
3307
3308 @unittest.skipUnless(sys.platform == "win32", "Win32 specific tests")
3309 class Win32JunctionTests(unittest.TestCase):
3310 junction = 'junctiontest'
3311 junction_target = os.path.dirname(os.path.abspath(__file__))
3312
3313 def setUp(self):
3314 assert os.path.exists(self.junction_target)
3315 assert not os.path.lexists(self.junction)
3316
3317 def tearDown(self):
3318 if os.path.lexists(self.junction):
3319 os.unlink(self.junction)
3320
3321 def test_create_junction(self):
3322 _winapi.CreateJunction(self.junction_target, self.junction)
3323 self.assertTrue(os.path.lexists(self.junction))
3324 self.assertTrue(os.path.exists(self.junction))
3325 self.assertTrue(os.path.isdir(self.junction))
3326 self.assertNotEqual(os.stat(self.junction), os.lstat(self.junction))
3327 self.assertEqual(os.stat(self.junction), os.stat(self.junction_target))
3328
3329 # bpo-37834: Junctions are not recognized as links.
3330 self.assertFalse(os.path.islink(self.junction))
3331 self.assertEqual(os.path.normcase("\\\\?\\" + self.junction_target),
3332 os.path.normcase(os.readlink(self.junction)))
3333
3334 def test_unlink_removes_junction(self):
3335 _winapi.CreateJunction(self.junction_target, self.junction)
3336 self.assertTrue(os.path.exists(self.junction))
3337 self.assertTrue(os.path.lexists(self.junction))
3338
3339 os.unlink(self.junction)
3340 self.assertFalse(os.path.exists(self.junction))
3341
3342 @unittest.skipUnless(sys.platform == "win32", "Win32 specific tests")
3343 class Win32NtTests(unittest.TestCase):
3344 def test_getfinalpathname_handles(self):
3345 nt = import_helper.import_module('nt')
3346 ctypes = import_helper.import_module('ctypes')
3347 # Ruff false positive -- it thinks we're redefining `ctypes` here
3348 import ctypes.wintypes # noqa: F811
3349
3350 kernel = ctypes.WinDLL('Kernel32.dll', use_last_error=True)
3351 kernel.GetCurrentProcess.restype = ctypes.wintypes.HANDLE
3352
3353 kernel.GetProcessHandleCount.restype = ctypes.wintypes.BOOL
3354 kernel.GetProcessHandleCount.argtypes = (ctypes.wintypes.HANDLE,
3355 ctypes.wintypes.LPDWORD)
3356
3357 # This is a pseudo-handle that doesn't need to be closed
3358 hproc = kernel.GetCurrentProcess()
3359
3360 handle_count = ctypes.wintypes.DWORD()
3361 ok = kernel.GetProcessHandleCount(hproc, ctypes.byref(handle_count))
3362 self.assertEqual(1, ok)
3363
3364 before_count = handle_count.value
3365
3366 # The first two test the error path, __file__ tests the success path
3367 filenames = [
3368 r'\\?\C:',
3369 r'\\?\NUL',
3370 r'\\?\CONIN',
3371 __file__,
3372 ]
3373
3374 for _ in range(10):
3375 for name in filenames:
3376 try:
3377 nt._getfinalpathname(name)
3378 except Exception:
3379 # Failure is expected
3380 pass
3381 try:
3382 os.stat(name)
3383 except Exception:
3384 pass
3385
3386 ok = kernel.GetProcessHandleCount(hproc, ctypes.byref(handle_count))
3387 self.assertEqual(1, ok)
3388
3389 handle_delta = handle_count.value - before_count
3390
3391 self.assertEqual(0, handle_delta)
3392
3393 @support.requires_subprocess()
3394 def test_stat_unlink_race(self):
3395 # bpo-46785: the implementation of os.stat() falls back to reading
3396 # the parent directory if CreateFileW() fails with a permission
3397 # error. If reading the parent directory fails because the file or
3398 # directory are subsequently unlinked, or because the volume or
3399 # share are no longer available, then the original permission error
3400 # should not be restored.
3401 filename = os_helper.TESTFN
3402 self.addCleanup(os_helper.unlink, filename)
3403 deadline = time.time() + 5
3404 command = textwrap.dedent("""\
3405 import os
3406 import sys
3407 import time
3408
3409 filename = sys.argv[1]
3410 deadline = float(sys.argv[2])
3411
3412 while time.time() < deadline:
3413 try:
3414 with open(filename, "w") as f:
3415 pass
3416 except OSError:
3417 pass
3418 try:
3419 os.remove(filename)
3420 except OSError:
3421 pass
3422 """)
3423
3424 with subprocess.Popen([sys.executable, '-c', command, filename, str(deadline)]) as proc:
3425 while time.time() < deadline:
3426 try:
3427 os.stat(filename)
3428 except FileNotFoundError as e:
3429 assert e.winerror == 2 # ERROR_FILE_NOT_FOUND
3430 try:
3431 proc.wait(1)
3432 except subprocess.TimeoutExpired:
3433 proc.terminate()
3434
3435 @support.requires_subprocess()
3436 def test_stat_inaccessible_file(self):
3437 filename = os_helper.TESTFN
3438 ICACLS = os.path.expandvars(r"%SystemRoot%\System32\icacls.exe")
3439
3440 with open(filename, "wb") as f:
3441 f.write(b'Test data')
3442
3443 stat1 = os.stat(filename)
3444
3445 try:
3446 # Remove all permissions from the file
3447 subprocess.check_output([ICACLS, filename, "/inheritance:r"],
3448 stderr=subprocess.STDOUT)
3449 except subprocess.CalledProcessError as ex:
3450 if support.verbose:
3451 print(ICACLS, filename, "/inheritance:r", "failed.")
3452 print(ex.stdout.decode("oem", "replace").rstrip())
3453 try:
3454 os.unlink(filename)
3455 except OSError:
3456 pass
3457 self.skipTest("Unable to create inaccessible file")
3458
3459 def cleanup():
3460 # Give delete permission to the owner (us)
3461 subprocess.check_output([ICACLS, filename, "/grant", "*WD:(D)"],
3462 stderr=subprocess.STDOUT)
3463 os.unlink(filename)
3464
3465 self.addCleanup(cleanup)
3466
3467 if support.verbose:
3468 print("File:", filename)
3469 print("stat with access:", stat1)
3470
3471 # First test - we shouldn't raise here, because we still have access to
3472 # the directory and can extract enough information from its metadata.
3473 stat2 = os.stat(filename)
3474
3475 if support.verbose:
3476 print(" without access:", stat2)
3477
3478 # We may not get st_dev/st_ino, so ensure those are 0 or match
3479 self.assertIn(stat2.st_dev, (0, stat1.st_dev))
3480 self.assertIn(stat2.st_ino, (0, stat1.st_ino))
3481
3482 # st_mode and st_size should match (for a normal file, at least)
3483 self.assertEqual(stat1.st_mode, stat2.st_mode)
3484 self.assertEqual(stat1.st_size, stat2.st_size)
3485
3486 # st_ctime and st_mtime should be the same
3487 self.assertEqual(stat1.st_ctime, stat2.st_ctime)
3488 self.assertEqual(stat1.st_mtime, stat2.st_mtime)
3489
3490 # st_atime should be the same or later
3491 self.assertGreaterEqual(stat1.st_atime, stat2.st_atime)
3492
3493
3494 @os_helper.skip_unless_symlink
3495 class NonLocalSymlinkTests(unittest.TestCase):
3496
3497 def setUp(self):
3498 r"""
3499 Create this structure:
3500
3501 base
3502 \___ some_dir
3503 """
3504 os.makedirs('base/some_dir')
3505
3506 def tearDown(self):
3507 shutil.rmtree('base')
3508
3509 def test_directory_link_nonlocal(self):
3510 """
3511 The symlink target should resolve relative to the link, not relative
3512 to the current directory.
3513
3514 Then, link base/some_link -> base/some_dir and ensure that some_link
3515 is resolved as a directory.
3516
3517 In issue13772, it was discovered that directory detection failed if
3518 the symlink target was not specified relative to the current
3519 directory, which was a defect in the implementation.
3520 """
3521 src = os.path.join('base', 'some_link')
3522 os.symlink('some_dir', src)
3523 assert os.path.isdir(src)
3524
3525
3526 class FSEncodingTests(unittest.TestCase):
3527 def test_nop(self):
3528 self.assertEqual(os.fsencode(b'abc\xff'), b'abc\xff')
3529 self.assertEqual(os.fsdecode('abc\u0141'), 'abc\u0141')
3530
3531 def test_identity(self):
3532 # assert fsdecode(fsencode(x)) == x
3533 for fn in ('unicode\u0141', 'latin\xe9', 'ascii'):
3534 try:
3535 bytesfn = os.fsencode(fn)
3536 except UnicodeEncodeError:
3537 continue
3538 self.assertEqual(os.fsdecode(bytesfn), fn)
3539
3540
3541
3542 class DeviceEncodingTests(unittest.TestCase):
3543
3544 def test_bad_fd(self):
3545 # Return None when an fd doesn't actually exist.
3546 self.assertIsNone(os.device_encoding(123456))
3547
3548 @unittest.skipUnless(os.isatty(0) and not win32_is_iot() and (sys.platform.startswith('win') or
3549 (hasattr(locale, 'nl_langinfo') and hasattr(locale, 'CODESET'))),
3550 'test requires a tty and either Windows or nl_langinfo(CODESET)')
3551 def test_device_encoding(self):
3552 encoding = os.device_encoding(0)
3553 self.assertIsNotNone(encoding)
3554 self.assertTrue(codecs.lookup(encoding))
3555
3556
3557 @support.requires_subprocess()
3558 class PidTests(unittest.TestCase):
3559 @unittest.skipUnless(hasattr(os, 'getppid'), "test needs os.getppid")
3560 def test_getppid(self):
3561 p = subprocess.Popen([sys._base_executable, '-c',
3562 'import os; print(os.getppid())'],
3563 stdout=subprocess.PIPE,
3564 stderr=subprocess.PIPE)
3565 stdout, error = p.communicate()
3566 # We are the parent of our subprocess
3567 self.assertEqual(error, b'')
3568 self.assertEqual(int(stdout), os.getpid())
3569
3570 def check_waitpid(self, code, exitcode, callback=None):
3571 if sys.platform == 'win32':
3572 # On Windows, os.spawnv() simply joins arguments with spaces:
3573 # arguments need to be quoted
3574 args = [f'"{sys.executable}"', '-c', f'"{code}"']
3575 else:
3576 args = [sys.executable, '-c', code]
3577 pid = os.spawnv(os.P_NOWAIT, sys.executable, args)
3578
3579 if callback is not None:
3580 callback(pid)
3581
3582 # don't use support.wait_process() to test directly os.waitpid()
3583 # and os.waitstatus_to_exitcode()
3584 pid2, status = os.waitpid(pid, 0)
3585 self.assertEqual(os.waitstatus_to_exitcode(status), exitcode)
3586 self.assertEqual(pid2, pid)
3587
3588 def test_waitpid(self):
3589 self.check_waitpid(code='pass', exitcode=0)
3590
3591 def test_waitstatus_to_exitcode(self):
3592 exitcode = 23
3593 code = f'import sys; sys.exit({exitcode})'
3594 self.check_waitpid(code, exitcode=exitcode)
3595
3596 with self.assertRaises(TypeError):
3597 os.waitstatus_to_exitcode(0.0)
3598
3599 @unittest.skipUnless(sys.platform == 'win32', 'win32-specific test')
3600 def test_waitpid_windows(self):
3601 # bpo-40138: test os.waitpid() and os.waitstatus_to_exitcode()
3602 # with exit code larger than INT_MAX.
3603 STATUS_CONTROL_C_EXIT = 0xC000013A
3604 code = f'import _winapi; _winapi.ExitProcess({STATUS_CONTROL_C_EXIT})'
3605 self.check_waitpid(code, exitcode=STATUS_CONTROL_C_EXIT)
3606
3607 @unittest.skipUnless(sys.platform == 'win32', 'win32-specific test')
3608 def test_waitstatus_to_exitcode_windows(self):
3609 max_exitcode = 2 ** 32 - 1
3610 for exitcode in (0, 1, 5, max_exitcode):
3611 self.assertEqual(os.waitstatus_to_exitcode(exitcode << 8),
3612 exitcode)
3613
3614 # invalid values
3615 with self.assertRaises(ValueError):
3616 os.waitstatus_to_exitcode((max_exitcode + 1) << 8)
3617 with self.assertRaises(OverflowError):
3618 os.waitstatus_to_exitcode(-1)
3619
3620 # Skip the test on Windows
3621 @unittest.skipUnless(hasattr(signal, 'SIGKILL'), 'need signal.SIGKILL')
3622 def test_waitstatus_to_exitcode_kill(self):
3623 code = f'import time; time.sleep({support.LONG_TIMEOUT})'
3624 signum = signal.SIGKILL
3625
3626 def kill_process(pid):
3627 os.kill(pid, signum)
3628
3629 self.check_waitpid(code, exitcode=-signum, callback=kill_process)
3630
3631
3632 @support.requires_subprocess()
3633 class SpawnTests(unittest.TestCase):
3634 @staticmethod
3635 def quote_args(args):
3636 # On Windows, os.spawn* simply joins arguments with spaces:
3637 # arguments need to be quoted
3638 if os.name != 'nt':
3639 return args
3640 return [f'"{arg}"' if " " in arg.strip() else arg for arg in args]
3641
3642 def create_args(self, *, with_env=False, use_bytes=False):
3643 self.exitcode = 17
3644
3645 filename = os_helper.TESTFN
3646 self.addCleanup(os_helper.unlink, filename)
3647
3648 if not with_env:
3649 code = 'import sys; sys.exit(%s)' % self.exitcode
3650 else:
3651 self.env = dict(os.environ)
3652 # create an unique key
3653 self.key = str(uuid.uuid4())
3654 self.env[self.key] = self.key
3655 # read the variable from os.environ to check that it exists
3656 code = ('import sys, os; magic = os.environ[%r]; sys.exit(%s)'
3657 % (self.key, self.exitcode))
3658
3659 with open(filename, "w", encoding="utf-8") as fp:
3660 fp.write(code)
3661
3662 program = sys.executable
3663 args = self.quote_args([program, filename])
3664 if use_bytes:
3665 program = os.fsencode(program)
3666 args = [os.fsencode(a) for a in args]
3667 self.env = {os.fsencode(k): os.fsencode(v)
3668 for k, v in self.env.items()}
3669
3670 return program, args
3671
3672 @requires_os_func('spawnl')
3673 def test_spawnl(self):
3674 program, args = self.create_args()
3675 exitcode = os.spawnl(os.P_WAIT, program, *args)
3676 self.assertEqual(exitcode, self.exitcode)
3677
3678 @requires_os_func('spawnle')
3679 def test_spawnle(self):
3680 program, args = self.create_args(with_env=True)
3681 exitcode = os.spawnle(os.P_WAIT, program, *args, self.env)
3682 self.assertEqual(exitcode, self.exitcode)
3683
3684 @requires_os_func('spawnlp')
3685 def test_spawnlp(self):
3686 program, args = self.create_args()
3687 exitcode = os.spawnlp(os.P_WAIT, program, *args)
3688 self.assertEqual(exitcode, self.exitcode)
3689
3690 @requires_os_func('spawnlpe')
3691 def test_spawnlpe(self):
3692 program, args = self.create_args(with_env=True)
3693 exitcode = os.spawnlpe(os.P_WAIT, program, *args, self.env)
3694 self.assertEqual(exitcode, self.exitcode)
3695
3696 @requires_os_func('spawnv')
3697 def test_spawnv(self):
3698 program, args = self.create_args()
3699 exitcode = os.spawnv(os.P_WAIT, program, args)
3700 self.assertEqual(exitcode, self.exitcode)
3701
3702 # Test for PyUnicode_FSConverter()
3703 exitcode = os.spawnv(os.P_WAIT, FakePath(program), args)
3704 self.assertEqual(exitcode, self.exitcode)
3705
3706 @requires_os_func('spawnve')
3707 def test_spawnve(self):
3708 program, args = self.create_args(with_env=True)
3709 exitcode = os.spawnve(os.P_WAIT, program, args, self.env)
3710 self.assertEqual(exitcode, self.exitcode)
3711
3712 @requires_os_func('spawnvp')
3713 def test_spawnvp(self):
3714 program, args = self.create_args()
3715 exitcode = os.spawnvp(os.P_WAIT, program, args)
3716 self.assertEqual(exitcode, self.exitcode)
3717
3718 @requires_os_func('spawnvpe')
3719 def test_spawnvpe(self):
3720 program, args = self.create_args(with_env=True)
3721 exitcode = os.spawnvpe(os.P_WAIT, program, args, self.env)
3722 self.assertEqual(exitcode, self.exitcode)
3723
3724 @requires_os_func('spawnv')
3725 def test_nowait(self):
3726 program, args = self.create_args()
3727 pid = os.spawnv(os.P_NOWAIT, program, args)
3728 support.wait_process(pid, exitcode=self.exitcode)
3729
3730 @requires_os_func('spawnve')
3731 def test_spawnve_bytes(self):
3732 # Test bytes handling in parse_arglist and parse_envlist (#28114)
3733 program, args = self.create_args(with_env=True, use_bytes=True)
3734 exitcode = os.spawnve(os.P_WAIT, program, args, self.env)
3735 self.assertEqual(exitcode, self.exitcode)
3736
3737 @requires_os_func('spawnl')
3738 def test_spawnl_noargs(self):
3739 program, __ = self.create_args()
3740 self.assertRaises(ValueError, os.spawnl, os.P_NOWAIT, program)
3741 self.assertRaises(ValueError, os.spawnl, os.P_NOWAIT, program, '')
3742
3743 @requires_os_func('spawnle')
3744 def test_spawnle_noargs(self):
3745 program, __ = self.create_args()
3746 self.assertRaises(ValueError, os.spawnle, os.P_NOWAIT, program, {})
3747 self.assertRaises(ValueError, os.spawnle, os.P_NOWAIT, program, '', {})
3748
3749 @requires_os_func('spawnv')
3750 def test_spawnv_noargs(self):
3751 program, __ = self.create_args()
3752 self.assertRaises(ValueError, os.spawnv, os.P_NOWAIT, program, ())
3753 self.assertRaises(ValueError, os.spawnv, os.P_NOWAIT, program, [])
3754 self.assertRaises(ValueError, os.spawnv, os.P_NOWAIT, program, ('',))
3755 self.assertRaises(ValueError, os.spawnv, os.P_NOWAIT, program, [''])
3756
3757 @requires_os_func('spawnve')
3758 def test_spawnve_noargs(self):
3759 program, __ = self.create_args()
3760 self.assertRaises(ValueError, os.spawnve, os.P_NOWAIT, program, (), {})
3761 self.assertRaises(ValueError, os.spawnve, os.P_NOWAIT, program, [], {})
3762 self.assertRaises(ValueError, os.spawnve, os.P_NOWAIT, program, ('',), {})
3763 self.assertRaises(ValueError, os.spawnve, os.P_NOWAIT, program, [''], {})
3764
3765 def _test_invalid_env(self, spawn):
3766 program = sys.executable
3767 args = self.quote_args([program, '-c', 'pass'])
3768
3769 # null character in the environment variable name
3770 newenv = os.environ.copy()
3771 newenv["FRUIT\0VEGETABLE"] = "cabbage"
3772 try:
3773 exitcode = spawn(os.P_WAIT, program, args, newenv)
3774 except ValueError:
3775 pass
3776 else:
3777 self.assertEqual(exitcode, 127)
3778
3779 # null character in the environment variable value
3780 newenv = os.environ.copy()
3781 newenv["FRUIT"] = "orange\0VEGETABLE=cabbage"
3782 try:
3783 exitcode = spawn(os.P_WAIT, program, args, newenv)
3784 except ValueError:
3785 pass
3786 else:
3787 self.assertEqual(exitcode, 127)
3788
3789 # equal character in the environment variable name
3790 newenv = os.environ.copy()
3791 newenv["FRUIT=ORANGE"] = "lemon"
3792 try:
3793 exitcode = spawn(os.P_WAIT, program, args, newenv)
3794 except ValueError:
3795 pass
3796 else:
3797 self.assertEqual(exitcode, 127)
3798
3799 # equal character in the environment variable value
3800 filename = os_helper.TESTFN
3801 self.addCleanup(os_helper.unlink, filename)
3802 with open(filename, "w", encoding="utf-8") as fp:
3803 fp.write('import sys, os\n'
3804 'if os.getenv("FRUIT") != "orange=lemon":\n'
3805 ' raise AssertionError')
3806
3807 args = self.quote_args([program, filename])
3808 newenv = os.environ.copy()
3809 newenv["FRUIT"] = "orange=lemon"
3810 exitcode = spawn(os.P_WAIT, program, args, newenv)
3811 self.assertEqual(exitcode, 0)
3812
3813 @requires_os_func('spawnve')
3814 def test_spawnve_invalid_env(self):
3815 self._test_invalid_env(os.spawnve)
3816
3817 @requires_os_func('spawnvpe')
3818 def test_spawnvpe_invalid_env(self):
3819 self._test_invalid_env(os.spawnvpe)
3820
3821
3822 # The introduction of this TestCase caused at least two different errors on
3823 # *nix buildbots. Temporarily skip this to let the buildbots move along.
3824 @unittest.skip("Skip due to platform/environment differences on *NIX buildbots")
3825 @unittest.skipUnless(hasattr(os, 'getlogin'), "test needs os.getlogin")
3826 class LoginTests(unittest.TestCase):
3827 def test_getlogin(self):
3828 user_name = os.getlogin()
3829 self.assertNotEqual(len(user_name), 0)
3830
3831
3832 @unittest.skipUnless(hasattr(os, 'getpriority') and hasattr(os, 'setpriority'),
3833 "needs os.getpriority and os.setpriority")
3834 class ProgramPriorityTests(unittest.TestCase):
3835 """Tests for os.getpriority() and os.setpriority()."""
3836
3837 def test_set_get_priority(self):
3838 base = os.getpriority(os.PRIO_PROCESS, os.getpid())
3839 code = f"""if 1:
3840 import os
3841 os.setpriority(os.PRIO_PROCESS, os.getpid(), {base} + 1)
3842 print(os.getpriority(os.PRIO_PROCESS, os.getpid()))
3843 """
3844
3845 # Subprocess inherits the current process' priority.
3846 _, out, _ = assert_python_ok("-c", code)
3847 new_prio = int(out)
3848 # nice value cap is 19 for linux and 20 for FreeBSD
3849 if base >= 19 and new_prio <= base:
3850 raise unittest.SkipTest("unable to reliably test setpriority "
3851 "at current nice level of %s" % base)
3852 else:
3853 self.assertEqual(new_prio, base + 1)
3854
3855
3856 @unittest.skipUnless(hasattr(os, 'sendfile'), "test needs os.sendfile()")
3857 class TestSendfile(unittest.IsolatedAsyncioTestCase):
3858
3859 DATA = b"12345abcde" * 16 * 1024 # 160 KiB
3860 SUPPORT_HEADERS_TRAILERS = (
3861 not sys.platform.startswith(("linux", "android", "solaris", "sunos")))
3862 requires_headers_trailers = unittest.skipUnless(SUPPORT_HEADERS_TRAILERS,
3863 'requires headers and trailers support')
3864 requires_32b = unittest.skipUnless(sys.maxsize < 2**32,
3865 'test is only meaningful on 32-bit builds')
3866
3867 @classmethod
3868 def setUpClass(cls):
3869 create_file(os_helper.TESTFN, cls.DATA)
3870
3871 @classmethod
3872 def tearDownClass(cls):
3873 os_helper.unlink(os_helper.TESTFN)
3874
3875 @staticmethod
3876 async def chunks(reader):
3877 while not reader.at_eof():
3878 yield await reader.read()
3879
3880 async def handle_new_client(self, reader, writer):
3881 self.server_buffer = b''.join([x async for x in self.chunks(reader)])
3882 writer.close()
3883 self.server.close() # The test server processes a single client only
3884
3885 async def asyncSetUp(self):
3886 self.server_buffer = b''
3887 self.server = await asyncio.start_server(self.handle_new_client,
3888 socket_helper.HOSTv4)
3889 server_name = self.server.sockets[0].getsockname()
3890 self.client = socket.socket()
3891 self.client.setblocking(False)
3892 await asyncio.get_running_loop().sock_connect(self.client, server_name)
3893 self.sockno = self.client.fileno()
3894 self.file = open(os_helper.TESTFN, 'rb')
3895 self.fileno = self.file.fileno()
3896
3897 async def asyncTearDown(self):
3898 self.file.close()
3899 self.client.close()
3900 await self.server.wait_closed()
3901
3902 # Use the test subject instead of asyncio.loop.sendfile
3903 @staticmethod
3904 async def async_sendfile(*args, **kwargs):
3905 return await asyncio.to_thread(os.sendfile, *args, **kwargs)
3906
3907 @staticmethod
3908 async def sendfile_wrapper(*args, **kwargs):
3909 """A higher level wrapper representing how an application is
3910 supposed to use sendfile().
3911 """
3912 while True:
3913 try:
3914 return await TestSendfile.async_sendfile(*args, **kwargs)
3915 except OSError as err:
3916 if err.errno == errno.ECONNRESET:
3917 # disconnected
3918 raise
3919 elif err.errno in (errno.EAGAIN, errno.EBUSY):
3920 # we have to retry send data
3921 continue
3922 else:
3923 raise
3924
3925 async def test_send_whole_file(self):
3926 # normal send
3927 total_sent = 0
3928 offset = 0
3929 nbytes = 4096
3930 while total_sent < len(self.DATA):
3931 sent = await self.sendfile_wrapper(self.sockno, self.fileno,
3932 offset, nbytes)
3933 if sent == 0:
3934 break
3935 offset += sent
3936 total_sent += sent
3937 self.assertTrue(sent <= nbytes)
3938 self.assertEqual(offset, total_sent)
3939
3940 self.assertEqual(total_sent, len(self.DATA))
3941 self.client.shutdown(socket.SHUT_RDWR)
3942 self.client.close()
3943 await self.server.wait_closed()
3944 self.assertEqual(len(self.server_buffer), len(self.DATA))
3945 self.assertEqual(self.server_buffer, self.DATA)
3946
3947 async def test_send_at_certain_offset(self):
3948 # start sending a file at a certain offset
3949 total_sent = 0
3950 offset = len(self.DATA) // 2
3951 must_send = len(self.DATA) - offset
3952 nbytes = 4096
3953 while total_sent < must_send:
3954 sent = await self.sendfile_wrapper(self.sockno, self.fileno,
3955 offset, nbytes)
3956 if sent == 0:
3957 break
3958 offset += sent
3959 total_sent += sent
3960 self.assertTrue(sent <= nbytes)
3961
3962 self.client.shutdown(socket.SHUT_RDWR)
3963 self.client.close()
3964 await self.server.wait_closed()
3965 expected = self.DATA[len(self.DATA) // 2:]
3966 self.assertEqual(total_sent, len(expected))
3967 self.assertEqual(len(self.server_buffer), len(expected))
3968 self.assertEqual(self.server_buffer, expected)
3969
3970 async def test_offset_overflow(self):
3971 # specify an offset > file size
3972 offset = len(self.DATA) + 4096
3973 try:
3974 sent = await self.async_sendfile(self.sockno, self.fileno,
3975 offset, 4096)
3976 except OSError as e:
3977 # Solaris can raise EINVAL if offset >= file length, ignore.
3978 if e.errno != errno.EINVAL:
3979 raise
3980 else:
3981 self.assertEqual(sent, 0)
3982 self.client.shutdown(socket.SHUT_RDWR)
3983 self.client.close()
3984 await self.server.wait_closed()
3985 self.assertEqual(self.server_buffer, b'')
3986
3987 async def test_invalid_offset(self):
3988 with self.assertRaises(OSError) as cm:
3989 await self.async_sendfile(self.sockno, self.fileno, -1, 4096)
3990 self.assertEqual(cm.exception.errno, errno.EINVAL)
3991
3992 async def test_keywords(self):
3993 # Keyword arguments should be supported
3994 await self.async_sendfile(out_fd=self.sockno, in_fd=self.fileno,
3995 offset=0, count=4096)
3996 if self.SUPPORT_HEADERS_TRAILERS:
3997 await self.async_sendfile(out_fd=self.sockno, in_fd=self.fileno,
3998 offset=0, count=4096,
3999 headers=(), trailers=(), flags=0)
4000
4001 # --- headers / trailers tests
4002
4003 @requires_headers_trailers
4004 async def test_headers(self):
4005 total_sent = 0
4006 expected_data = b"x" * 512 + b"y" * 256 + self.DATA[:-1]
4007 sent = await self.async_sendfile(self.sockno, self.fileno, 0, 4096,
4008 headers=[b"x" * 512, b"y" * 256])
4009 self.assertLessEqual(sent, 512 + 256 + 4096)
4010 total_sent += sent
4011 offset = 4096
4012 while total_sent < len(expected_data):
4013 nbytes = min(len(expected_data) - total_sent, 4096)
4014 sent = await self.sendfile_wrapper(self.sockno, self.fileno,
4015 offset, nbytes)
4016 if sent == 0:
4017 break
4018 self.assertLessEqual(sent, nbytes)
4019 total_sent += sent
4020 offset += sent
4021
4022 self.assertEqual(total_sent, len(expected_data))
4023 self.client.close()
4024 await self.server.wait_closed()
4025 self.assertEqual(hash(self.server_buffer), hash(expected_data))
4026
4027 @requires_headers_trailers
4028 async def test_trailers(self):
4029 TESTFN2 = os_helper.TESTFN + "2"
4030 file_data = b"abcdef"
4031
4032 self.addCleanup(os_helper.unlink, TESTFN2)
4033 create_file(TESTFN2, file_data)
4034
4035 with open(TESTFN2, 'rb') as f:
4036 await self.async_sendfile(self.sockno, f.fileno(), 0, 5,
4037 trailers=[b"123456", b"789"])
4038 self.client.close()
4039 await self.server.wait_closed()
4040 self.assertEqual(self.server_buffer, b"abcde123456789")
4041
4042 @requires_headers_trailers
4043 @requires_32b
4044 async def test_headers_overflow_32bits(self):
4045 with self.assertRaises(OSError) as cm:
4046 await self.async_sendfile(self.sockno, self.fileno, 0, 0,
4047 headers=[b"x" * 2**16] * 2**15)
4048 self.assertEqual(cm.exception.errno, errno.EINVAL)
4049
4050 @requires_headers_trailers
4051 @requires_32b
4052 async def test_trailers_overflow_32bits(self):
4053 with self.assertRaises(OSError) as cm:
4054 await self.async_sendfile(self.sockno, self.fileno, 0, 0,
4055 trailers=[b"x" * 2**16] * 2**15)
4056 self.assertEqual(cm.exception.errno, errno.EINVAL)
4057
4058 @requires_headers_trailers
4059 @unittest.skipUnless(hasattr(os, 'SF_NODISKIO'),
4060 'test needs os.SF_NODISKIO')
4061 async def test_flags(self):
4062 try:
4063 await self.async_sendfile(self.sockno, self.fileno, 0, 4096,
4064 flags=os.SF_NODISKIO)
4065 except OSError as err:
4066 if err.errno not in (errno.EBUSY, errno.EAGAIN):
4067 raise
4068
4069
4070 def supports_extended_attributes():
4071 if not hasattr(os, "setxattr"):
4072 return False
4073
4074 try:
4075 with open(os_helper.TESTFN, "xb", 0) as fp:
4076 try:
4077 os.setxattr(fp.fileno(), b"user.test", b"")
4078 except OSError:
4079 return False
4080 finally:
4081 os_helper.unlink(os_helper.TESTFN)
4082
4083 return True
4084
4085
4086 @unittest.skipUnless(supports_extended_attributes(),
4087 "no non-broken extended attribute support")
4088 # Kernels < 2.6.39 don't respect setxattr flags.
4089 @support.requires_linux_version(2, 6, 39)
4090 class ExtendedAttributeTests(unittest.TestCase):
4091
4092 def _check_xattrs_str(self, s, getxattr, setxattr, removexattr, listxattr, **kwargs):
4093 fn = os_helper.TESTFN
4094 self.addCleanup(os_helper.unlink, fn)
4095 create_file(fn)
4096
4097 with self.assertRaises(OSError) as cm:
4098 getxattr(fn, s("user.test"), **kwargs)
4099 self.assertEqual(cm.exception.errno, errno.ENODATA)
4100
4101 init_xattr = listxattr(fn)
4102 self.assertIsInstance(init_xattr, list)
4103
4104 setxattr(fn, s("user.test"), b"", **kwargs)
4105 xattr = set(init_xattr)
4106 xattr.add("user.test")
4107 self.assertEqual(set(listxattr(fn)), xattr)
4108 self.assertEqual(getxattr(fn, b"user.test", **kwargs), b"")
4109 setxattr(fn, s("user.test"), b"hello", os.XATTR_REPLACE, **kwargs)
4110 self.assertEqual(getxattr(fn, b"user.test", **kwargs), b"hello")
4111
4112 with self.assertRaises(OSError) as cm:
4113 setxattr(fn, s("user.test"), b"bye", os.XATTR_CREATE, **kwargs)
4114 self.assertEqual(cm.exception.errno, errno.EEXIST)
4115
4116 with self.assertRaises(OSError) as cm:
4117 setxattr(fn, s("user.test2"), b"bye", os.XATTR_REPLACE, **kwargs)
4118 self.assertEqual(cm.exception.errno, errno.ENODATA)
4119
4120 setxattr(fn, s("user.test2"), b"foo", os.XATTR_CREATE, **kwargs)
4121 xattr.add("user.test2")
4122 self.assertEqual(set(listxattr(fn)), xattr)
4123 removexattr(fn, s("user.test"), **kwargs)
4124
4125 with self.assertRaises(OSError) as cm:
4126 getxattr(fn, s("user.test"), **kwargs)
4127 self.assertEqual(cm.exception.errno, errno.ENODATA)
4128
4129 xattr.remove("user.test")
4130 self.assertEqual(set(listxattr(fn)), xattr)
4131 self.assertEqual(getxattr(fn, s("user.test2"), **kwargs), b"foo")
4132 setxattr(fn, s("user.test"), b"a"*256, **kwargs)
4133 self.assertEqual(getxattr(fn, s("user.test"), **kwargs), b"a"*256)
4134 removexattr(fn, s("user.test"), **kwargs)
4135 many = sorted("user.test{}".format(i) for i in range(32))
4136 for thing in many:
4137 setxattr(fn, thing, b"x", **kwargs)
4138 self.assertEqual(set(listxattr(fn)), set(init_xattr) | set(many))
4139
4140 def _check_xattrs(self, *args, **kwargs):
4141 self._check_xattrs_str(str, *args, **kwargs)
4142 os_helper.unlink(os_helper.TESTFN)
4143
4144 self._check_xattrs_str(os.fsencode, *args, **kwargs)
4145 os_helper.unlink(os_helper.TESTFN)
4146
4147 def test_simple(self):
4148 self._check_xattrs(os.getxattr, os.setxattr, os.removexattr,
4149 os.listxattr)
4150
4151 def test_lpath(self):
4152 self._check_xattrs(os.getxattr, os.setxattr, os.removexattr,
4153 os.listxattr, follow_symlinks=False)
4154
4155 def test_fds(self):
4156 def getxattr(path, *args):
4157 with open(path, "rb") as fp:
4158 return os.getxattr(fp.fileno(), *args)
4159 def setxattr(path, *args):
4160 with open(path, "wb", 0) as fp:
4161 os.setxattr(fp.fileno(), *args)
4162 def removexattr(path, *args):
4163 with open(path, "wb", 0) as fp:
4164 os.removexattr(fp.fileno(), *args)
4165 def listxattr(path, *args):
4166 with open(path, "rb") as fp:
4167 return os.listxattr(fp.fileno(), *args)
4168 self._check_xattrs(getxattr, setxattr, removexattr, listxattr)
4169
4170
4171 @unittest.skipUnless(hasattr(os, 'get_terminal_size'), "requires os.get_terminal_size")
4172 class TermsizeTests(unittest.TestCase):
4173 def test_does_not_crash(self):
4174 """Check if get_terminal_size() returns a meaningful value.
4175
4176 There's no easy portable way to actually check the size of the
4177 terminal, so let's check if it returns something sensible instead.
4178 """
4179 try:
4180 size = os.get_terminal_size()
4181 except OSError as e:
4182 known_errnos = [errno.EINVAL, errno.ENOTTY]
4183 if sys.platform == "android":
4184 # The Android testbed redirects the native stdout to a pipe,
4185 # which returns a different error code.
4186 known_errnos.append(errno.EACCES)
4187 if sys.platform == "win32" or e.errno in known_errnos:
4188 # Under win32 a generic OSError can be thrown if the
4189 # handle cannot be retrieved
4190 self.skipTest("failed to query terminal size")
4191 raise
4192
4193 self.assertGreaterEqual(size.columns, 0)
4194 self.assertGreaterEqual(size.lines, 0)
4195
4196 @support.requires_subprocess()
4197 def test_stty_match(self):
4198 """Check if stty returns the same results
4199
4200 stty actually tests stdin, so get_terminal_size is invoked on
4201 stdin explicitly. If stty succeeded, then get_terminal_size()
4202 should work too.
4203 """
4204 try:
4205 size = (
4206 subprocess.check_output(
4207 ["stty", "size"], stderr=subprocess.DEVNULL, text=True
4208 ).split()
4209 )
4210 except (FileNotFoundError, subprocess.CalledProcessError,
4211 PermissionError):
4212 self.skipTest("stty invocation failed")
4213 expected = (int(size[1]), int(size[0])) # reversed order
4214
4215 try:
4216 actual = os.get_terminal_size(sys.__stdin__.fileno())
4217 except OSError as e:
4218 if sys.platform == "win32" or e.errno in (errno.EINVAL, errno.ENOTTY):
4219 # Under win32 a generic OSError can be thrown if the
4220 # handle cannot be retrieved
4221 self.skipTest("failed to query terminal size")
4222 raise
4223 self.assertEqual(expected, actual)
4224
4225 @unittest.skipUnless(sys.platform == 'win32', 'Windows specific test')
4226 def test_windows_fd(self):
4227 """Check if get_terminal_size() returns a meaningful value in Windows"""
4228 try:
4229 conout = open('conout$', 'w')
4230 except OSError:
4231 self.skipTest('failed to open conout$')
4232 with conout:
4233 size = os.get_terminal_size(conout.fileno())
4234
4235 self.assertGreaterEqual(size.columns, 0)
4236 self.assertGreaterEqual(size.lines, 0)
4237
4238
4239 @unittest.skipUnless(hasattr(os, 'memfd_create'), 'requires os.memfd_create')
4240 @support.requires_linux_version(3, 17)
4241 class MemfdCreateTests(unittest.TestCase):
4242 def test_memfd_create(self):
4243 fd = os.memfd_create("Hi", os.MFD_CLOEXEC)
4244 self.assertNotEqual(fd, -1)
4245 self.addCleanup(os.close, fd)
4246 self.assertFalse(os.get_inheritable(fd))
4247 with open(fd, "wb", closefd=False) as f:
4248 f.write(b'memfd_create')
4249 self.assertEqual(f.tell(), 12)
4250
4251 fd2 = os.memfd_create("Hi")
4252 self.addCleanup(os.close, fd2)
4253 self.assertFalse(os.get_inheritable(fd2))
4254
4255
4256 @unittest.skipUnless(hasattr(os, 'eventfd'), 'requires os.eventfd')
4257 @support.requires_linux_version(2, 6, 30)
4258 class EventfdTests(unittest.TestCase):
4259 def test_eventfd_initval(self):
4260 def pack(value):
4261 """Pack as native uint64_t
4262 """
4263 return struct.pack("@Q", value)
4264 size = 8 # read/write 8 bytes
4265 initval = 42
4266 fd = os.eventfd(initval)
4267 self.assertNotEqual(fd, -1)
4268 self.addCleanup(os.close, fd)
4269 self.assertFalse(os.get_inheritable(fd))
4270
4271 # test with raw read/write
4272 res = os.read(fd, size)
4273 self.assertEqual(res, pack(initval))
4274
4275 os.write(fd, pack(23))
4276 res = os.read(fd, size)
4277 self.assertEqual(res, pack(23))
4278
4279 os.write(fd, pack(40))
4280 os.write(fd, pack(2))
4281 res = os.read(fd, size)
4282 self.assertEqual(res, pack(42))
4283
4284 # test with eventfd_read/eventfd_write
4285 os.eventfd_write(fd, 20)
4286 os.eventfd_write(fd, 3)
4287 res = os.eventfd_read(fd)
4288 self.assertEqual(res, 23)
4289
4290 def test_eventfd_semaphore(self):
4291 initval = 2
4292 flags = os.EFD_CLOEXEC | os.EFD_SEMAPHORE | os.EFD_NONBLOCK
4293 fd = os.eventfd(initval, flags)
4294 self.assertNotEqual(fd, -1)
4295 self.addCleanup(os.close, fd)
4296
4297 # semaphore starts has initval 2, two reads return '1'
4298 res = os.eventfd_read(fd)
4299 self.assertEqual(res, 1)
4300 res = os.eventfd_read(fd)
4301 self.assertEqual(res, 1)
4302 # third read would block
4303 with self.assertRaises(BlockingIOError):
4304 os.eventfd_read(fd)
4305 with self.assertRaises(BlockingIOError):
4306 os.read(fd, 8)
4307
4308 # increase semaphore counter, read one
4309 os.eventfd_write(fd, 1)
4310 res = os.eventfd_read(fd)
4311 self.assertEqual(res, 1)
4312 # next read would block, too
4313 with self.assertRaises(BlockingIOError):
4314 os.eventfd_read(fd)
4315
4316 def test_eventfd_select(self):
4317 flags = os.EFD_CLOEXEC | os.EFD_NONBLOCK
4318 fd = os.eventfd(0, flags)
4319 self.assertNotEqual(fd, -1)
4320 self.addCleanup(os.close, fd)
4321
4322 # counter is zero, only writeable
4323 rfd, wfd, xfd = select.select([fd], [fd], [fd], 0)
4324 self.assertEqual((rfd, wfd, xfd), ([], [fd], []))
4325
4326 # counter is non-zero, read and writeable
4327 os.eventfd_write(fd, 23)
4328 rfd, wfd, xfd = select.select([fd], [fd], [fd], 0)
4329 self.assertEqual((rfd, wfd, xfd), ([fd], [fd], []))
4330 self.assertEqual(os.eventfd_read(fd), 23)
4331
4332 # counter at max, only readable
4333 os.eventfd_write(fd, (2**64) - 2)
4334 rfd, wfd, xfd = select.select([fd], [fd], [fd], 0)
4335 self.assertEqual((rfd, wfd, xfd), ([fd], [], []))
4336 os.eventfd_read(fd)
4337
4338 @unittest.skipUnless(hasattr(os, 'timerfd_create'), 'requires os.timerfd_create')
4339 @unittest.skipIf(sys.platform == "android", "gh-124873: Test is flaky on Android")
4340 @support.requires_linux_version(2, 6, 30)
4341 class TimerfdTests(unittest.TestCase):
4342 # gh-126112: Use 10 ms to tolerate slow buildbots
4343 CLOCK_RES_PLACES = 2 # 10 ms
4344 CLOCK_RES = 10 ** -CLOCK_RES_PLACES
4345 CLOCK_RES_NS = 10 ** (9 - CLOCK_RES_PLACES)
4346
4347 def timerfd_create(self, *args, **kwargs):
4348 fd = os.timerfd_create(*args, **kwargs)
4349 self.assertGreaterEqual(fd, 0)
4350 self.assertFalse(os.get_inheritable(fd))
4351 self.addCleanup(os.close, fd)
4352 return fd
4353
4354 def read_count_signaled(self, fd):
4355 # read 8 bytes
4356 data = os.read(fd, 8)
4357 return int.from_bytes(data, byteorder=sys.byteorder)
4358
4359 def test_timerfd_initval(self):
4360 fd = self.timerfd_create(time.CLOCK_REALTIME)
4361
4362 initial_expiration = 0.25
4363 interval = 0.125
4364
4365 # 1st call
4366 next_expiration, interval2 = os.timerfd_settime(fd, initial=initial_expiration, interval=interval)
4367 self.assertAlmostEqual(interval2, 0.0, places=self.CLOCK_RES_PLACES)
4368 self.assertAlmostEqual(next_expiration, 0.0, places=self.CLOCK_RES_PLACES)
4369
4370 # 2nd call
4371 next_expiration, interval2 = os.timerfd_settime(fd, initial=initial_expiration, interval=interval)
4372 self.assertAlmostEqual(interval2, interval, places=self.CLOCK_RES_PLACES)
4373 self.assertAlmostEqual(next_expiration, initial_expiration, places=self.CLOCK_RES_PLACES)
4374
4375 # timerfd_gettime
4376 next_expiration, interval2 = os.timerfd_gettime(fd)
4377 self.assertAlmostEqual(interval2, interval, places=self.CLOCK_RES_PLACES)
4378 self.assertAlmostEqual(next_expiration, initial_expiration, places=self.CLOCK_RES_PLACES)
4379
4380 def test_timerfd_non_blocking(self):
4381 fd = self.timerfd_create(time.CLOCK_REALTIME, flags=os.TFD_NONBLOCK)
4382
4383 # 0.1 second later
4384 initial_expiration = 0.1
4385 os.timerfd_settime(fd, initial=initial_expiration, interval=0)
4386
4387 # read() raises OSError with errno is EAGAIN for non-blocking timer.
4388 with self.assertRaises(OSError) as ctx:
4389 self.read_count_signaled(fd)
4390 self.assertEqual(ctx.exception.errno, errno.EAGAIN)
4391
4392 # Wait more than 0.1 seconds
4393 time.sleep(initial_expiration + 0.1)
4394
4395 # confirm if timerfd is readable and read() returns 1 as bytes.
4396 self.assertEqual(self.read_count_signaled(fd), 1)
4397
4398 @unittest.skipIf(sys.platform.startswith('netbsd'),
4399 "gh-131263: Skip on NetBSD due to system freeze "
4400 "with negative timer values")
4401 def test_timerfd_negative(self):
4402 one_sec_in_nsec = 10**9
4403 fd = self.timerfd_create(time.CLOCK_REALTIME)
4404
4405 test_flags = [0, os.TFD_TIMER_ABSTIME]
4406 if hasattr(os, 'TFD_TIMER_CANCEL_ON_SET'):
4407 test_flags.append(os.TFD_TIMER_ABSTIME | os.TFD_TIMER_CANCEL_ON_SET)
4408
4409 # Any of 'initial' and 'interval' is negative value.
4410 for initial, interval in ( (-1, 0), (1, -1), (-1, -1), (-0.1, 0), (1, -0.1), (-0.1, -0.1)):
4411 for flags in test_flags:
4412 with self.subTest(flags=flags, initial=initial, interval=interval):
4413 with self.assertRaises(OSError) as context:
4414 os.timerfd_settime(fd, flags=flags, initial=initial, interval=interval)
4415 self.assertEqual(context.exception.errno, errno.EINVAL)
4416
4417 with self.assertRaises(OSError) as context:
4418 initial_ns = int( one_sec_in_nsec * initial )
4419 interval_ns = int( one_sec_in_nsec * interval )
4420 os.timerfd_settime_ns(fd, flags=flags, initial=initial_ns, interval=interval_ns)
4421 self.assertEqual(context.exception.errno, errno.EINVAL)
4422
4423 def test_timerfd_interval(self):
4424 fd = self.timerfd_create(time.CLOCK_REALTIME)
4425
4426 # 1 second
4427 initial_expiration = 1
4428 # 0.5 second
4429 interval = 0.5
4430
4431 os.timerfd_settime(fd, initial=initial_expiration, interval=interval)
4432
4433 # timerfd_gettime
4434 next_expiration, interval2 = os.timerfd_gettime(fd)
4435 self.assertAlmostEqual(interval2, interval, places=self.CLOCK_RES_PLACES)
4436 self.assertAlmostEqual(next_expiration, initial_expiration, places=self.CLOCK_RES_PLACES)
4437
4438 count = 3
4439 t = time.perf_counter()
4440 for _ in range(count):
4441 self.assertEqual(self.read_count_signaled(fd), 1)
4442 t = time.perf_counter() - t
4443
4444 total_time = initial_expiration + interval * (count - 1)
4445 self.assertGreater(t, total_time - self.CLOCK_RES)
4446
4447 # wait 3.5 time of interval
4448 time.sleep( (count+0.5) * interval)
4449 self.assertEqual(self.read_count_signaled(fd), count)
4450
4451 def test_timerfd_TFD_TIMER_ABSTIME(self):
4452 fd = self.timerfd_create(time.CLOCK_REALTIME)
4453
4454 now = time.clock_gettime(time.CLOCK_REALTIME)
4455
4456 # 1 second later from now.
4457 offset = 1
4458 initial_expiration = now + offset
4459 # not interval timer
4460 interval = 0
4461
4462 os.timerfd_settime(fd, flags=os.TFD_TIMER_ABSTIME, initial=initial_expiration, interval=interval)
4463
4464 # timerfd_gettime
4465 # Note: timerfd_gettime returns relative values even if TFD_TIMER_ABSTIME is specified.
4466 next_expiration, interval2 = os.timerfd_gettime(fd)
4467 self.assertAlmostEqual(interval2, interval, places=self.CLOCK_RES_PLACES)
4468 self.assertAlmostEqual(next_expiration, offset, places=self.CLOCK_RES_PLACES)
4469
4470 t = time.perf_counter()
4471 count_signaled = self.read_count_signaled(fd)
4472 t = time.perf_counter() - t
4473 self.assertEqual(count_signaled, 1)
4474
4475 self.assertGreater(t, offset - self.CLOCK_RES)
4476
4477 def test_timerfd_select(self):
4478 fd = self.timerfd_create(time.CLOCK_REALTIME, flags=os.TFD_NONBLOCK)
4479
4480 rfd, wfd, xfd = select.select([fd], [fd], [fd], 0)
4481 self.assertEqual((rfd, wfd, xfd), ([], [], []))
4482
4483 # 0.25 second
4484 initial_expiration = 0.25
4485 # every 0.125 second
4486 interval = 0.125
4487
4488 os.timerfd_settime(fd, initial=initial_expiration, interval=interval)
4489
4490 count = 3
4491 t = time.perf_counter()
4492 for _ in range(count):
4493 rfd, wfd, xfd = select.select([fd], [fd], [fd], initial_expiration + interval)
4494 self.assertEqual((rfd, wfd, xfd), ([fd], [], []))
4495 self.assertEqual(self.read_count_signaled(fd), 1)
4496 t = time.perf_counter() - t
4497
4498 total_time = initial_expiration + interval * (count - 1)
4499 self.assertGreater(t, total_time - self.CLOCK_RES)
4500
4501 def check_timerfd_poll(self, nanoseconds):
4502 fd = self.timerfd_create(time.CLOCK_REALTIME, flags=os.TFD_NONBLOCK)
4503
4504 selector = selectors.DefaultSelector()
4505 selector.register(fd, selectors.EVENT_READ)
4506 self.addCleanup(selector.close)
4507
4508 sec_to_nsec = 10 ** 9
4509 # 0.25 second
4510 initial_expiration_ns = sec_to_nsec // 4
4511 # every 0.125 second
4512 interval_ns = sec_to_nsec // 8
4513
4514 if nanoseconds:
4515 os.timerfd_settime_ns(fd,
4516 initial=initial_expiration_ns,
4517 interval=interval_ns)
4518 else:
4519 os.timerfd_settime(fd,
4520 initial=initial_expiration_ns / sec_to_nsec,
4521 interval=interval_ns / sec_to_nsec)
4522
4523 count = 3
4524 if nanoseconds:
4525 t = time.perf_counter_ns()
4526 else:
4527 t = time.perf_counter()
4528 for i in range(count):
4529 timeout_margin_ns = interval_ns
4530 if i == 0:
4531 timeout_ns = initial_expiration_ns + interval_ns + timeout_margin_ns
4532 else:
4533 timeout_ns = interval_ns + timeout_margin_ns
4534
4535 ready = selector.select(timeout_ns / sec_to_nsec)
4536 self.assertEqual(len(ready), 1, ready)
4537 event = ready[0][1]
4538 self.assertEqual(event, selectors.EVENT_READ)
4539
4540 self.assertEqual(self.read_count_signaled(fd), 1)
4541
4542 total_time = initial_expiration_ns + interval_ns * (count - 1)
4543 if nanoseconds:
4544 dt = time.perf_counter_ns() - t
4545 self.assertGreater(dt, total_time - self.CLOCK_RES_NS)
4546 else:
4547 dt = time.perf_counter() - t
4548 self.assertGreater(dt, total_time / sec_to_nsec - self.CLOCK_RES)
4549 selector.unregister(fd)
4550
4551 def test_timerfd_poll(self):
4552 self.check_timerfd_poll(False)
4553
4554 def test_timerfd_ns_poll(self):
4555 self.check_timerfd_poll(True)
4556
4557 def test_timerfd_ns_initval(self):
4558 one_sec_in_nsec = 10**9
4559 limit_error = one_sec_in_nsec // 10**3
4560 fd = self.timerfd_create(time.CLOCK_REALTIME)
4561
4562 # 1st call
4563 initial_expiration_ns = 0
4564 interval_ns = one_sec_in_nsec // 1000
4565 next_expiration_ns, interval_ns2 = os.timerfd_settime_ns(fd, initial=initial_expiration_ns, interval=interval_ns)
4566 self.assertEqual(interval_ns2, 0)
4567 self.assertEqual(next_expiration_ns, 0)
4568
4569 # 2nd call
4570 next_expiration_ns, interval_ns2 = os.timerfd_settime_ns(fd, initial=initial_expiration_ns, interval=interval_ns)
4571 self.assertEqual(interval_ns2, interval_ns)
4572 self.assertEqual(next_expiration_ns, initial_expiration_ns)
4573
4574 # timerfd_gettime
4575 next_expiration_ns, interval_ns2 = os.timerfd_gettime_ns(fd)
4576 self.assertEqual(interval_ns2, interval_ns)
4577 self.assertLessEqual(next_expiration_ns, initial_expiration_ns)
4578
4579 self.assertAlmostEqual(next_expiration_ns, initial_expiration_ns, delta=limit_error)
4580
4581 def test_timerfd_ns_interval(self):
4582 one_sec_in_nsec = 10**9
4583 limit_error = one_sec_in_nsec // 10**3
4584 fd = self.timerfd_create(time.CLOCK_REALTIME)
4585
4586 # 1 second
4587 initial_expiration_ns = one_sec_in_nsec
4588 # every 0.5 second
4589 interval_ns = one_sec_in_nsec // 2
4590
4591 os.timerfd_settime_ns(fd, initial=initial_expiration_ns, interval=interval_ns)
4592
4593 # timerfd_gettime
4594 next_expiration_ns, interval_ns2 = os.timerfd_gettime_ns(fd)
4595 self.assertEqual(interval_ns2, interval_ns)
4596 self.assertLessEqual(next_expiration_ns, initial_expiration_ns)
4597
4598 count = 3
4599 t = time.perf_counter_ns()
4600 for _ in range(count):
4601 self.assertEqual(self.read_count_signaled(fd), 1)
4602 t = time.perf_counter_ns() - t
4603
4604 total_time_ns = initial_expiration_ns + interval_ns * (count - 1)
4605 self.assertGreater(t, total_time_ns - self.CLOCK_RES_NS)
4606
4607 # wait 3.5 time of interval
4608 time.sleep( (count+0.5) * interval_ns / one_sec_in_nsec)
4609 self.assertEqual(self.read_count_signaled(fd), count)
4610
4611
4612 def test_timerfd_ns_TFD_TIMER_ABSTIME(self):
4613 one_sec_in_nsec = 10**9
4614 limit_error = one_sec_in_nsec // 10**3
4615 fd = self.timerfd_create(time.CLOCK_REALTIME)
4616
4617 now_ns = time.clock_gettime_ns(time.CLOCK_REALTIME)
4618
4619 # 1 second later from now.
4620 offset_ns = one_sec_in_nsec
4621 initial_expiration_ns = now_ns + offset_ns
4622 # not interval timer
4623 interval_ns = 0
4624
4625 os.timerfd_settime_ns(fd, flags=os.TFD_TIMER_ABSTIME, initial=initial_expiration_ns, interval=interval_ns)
4626
4627 # timerfd_gettime
4628 # Note: timerfd_gettime returns relative values even if TFD_TIMER_ABSTIME is specified.
4629 next_expiration_ns, interval_ns2 = os.timerfd_gettime_ns(fd)
4630 self.assertLess(abs(interval_ns2 - interval_ns), limit_error)
4631 self.assertLess(abs(next_expiration_ns - offset_ns), limit_error)
4632
4633 t = time.perf_counter_ns()
4634 count_signaled = self.read_count_signaled(fd)
4635 t = time.perf_counter_ns() - t
4636 self.assertEqual(count_signaled, 1)
4637
4638 self.assertGreater(t, offset_ns - self.CLOCK_RES_NS)
4639
4640 def test_timerfd_ns_select(self):
4641 one_sec_in_nsec = 10**9
4642
4643 fd = self.timerfd_create(time.CLOCK_REALTIME, flags=os.TFD_NONBLOCK)
4644
4645 rfd, wfd, xfd = select.select([fd], [fd], [fd], 0)
4646 self.assertEqual((rfd, wfd, xfd), ([], [], []))
4647
4648 # 0.25 second
4649 initial_expiration_ns = one_sec_in_nsec // 4
4650 # every 0.125 second
4651 interval_ns = one_sec_in_nsec // 8
4652
4653 os.timerfd_settime_ns(fd, initial=initial_expiration_ns, interval=interval_ns)
4654
4655 count = 3
4656 t = time.perf_counter_ns()
4657 for _ in range(count):
4658 rfd, wfd, xfd = select.select([fd], [fd], [fd], (initial_expiration_ns + interval_ns) / 1e9 )
4659 self.assertEqual((rfd, wfd, xfd), ([fd], [], []))
4660 self.assertEqual(self.read_count_signaled(fd), 1)
4661 t = time.perf_counter_ns() - t
4662
4663 total_time_ns = initial_expiration_ns + interval_ns * (count - 1)
4664 self.assertGreater(t, total_time_ns - self.CLOCK_RES_NS)
4665
4666 class OSErrorTests(unittest.TestCase):
4667 def setUp(self):
4668 class Str(str):
4669 pass
4670
4671 self.bytes_filenames = []
4672 self.unicode_filenames = []
4673 if os_helper.TESTFN_UNENCODABLE is not None:
4674 decoded = os_helper.TESTFN_UNENCODABLE
4675 else:
4676 decoded = os_helper.TESTFN
4677 self.unicode_filenames.append(decoded)
4678 self.unicode_filenames.append(Str(decoded))
4679 if os_helper.TESTFN_UNDECODABLE is not None:
4680 encoded = os_helper.TESTFN_UNDECODABLE
4681 else:
4682 encoded = os.fsencode(os_helper.TESTFN)
4683 self.bytes_filenames.append(encoded)
4684
4685 self.filenames = self.bytes_filenames + self.unicode_filenames
4686
4687 def test_oserror_filename(self):
4688 funcs = [
4689 (self.filenames, os.chdir,),
4690 (self.filenames, os.lstat,),
4691 (self.filenames, os.open, os.O_RDONLY),
4692 (self.filenames, os.rmdir,),
4693 (self.filenames, os.stat,),
4694 (self.filenames, os.unlink,),
4695 (self.filenames, os.listdir,),
4696 (self.filenames, os.rename, "dst"),
4697 (self.filenames, os.replace, "dst"),
4698 ]
4699 if os_helper.can_chmod():
4700 funcs.append((self.filenames, os.chmod, 0o777))
4701 if hasattr(os, "chown"):
4702 funcs.append((self.filenames, os.chown, 0, 0))
4703 if hasattr(os, "lchown"):
4704 funcs.append((self.filenames, os.lchown, 0, 0))
4705 if hasattr(os, "truncate"):
4706 funcs.append((self.filenames, os.truncate, 0))
4707 if hasattr(os, "chflags"):
4708 funcs.append((self.filenames, os.chflags, 0))
4709 if hasattr(os, "lchflags"):
4710 funcs.append((self.filenames, os.lchflags, 0))
4711 if hasattr(os, "chroot"):
4712 funcs.append((self.filenames, os.chroot,))
4713 if hasattr(os, "link"):
4714 funcs.append((self.filenames, os.link, "dst"))
4715 if hasattr(os, "listxattr"):
4716 funcs.extend((
4717 (self.filenames, os.listxattr,),
4718 (self.filenames, os.getxattr, "user.test"),
4719 (self.filenames, os.setxattr, "user.test", b'user'),
4720 (self.filenames, os.removexattr, "user.test"),
4721 ))
4722 if hasattr(os, "lchmod"):
4723 funcs.append((self.filenames, os.lchmod, 0o777))
4724 if hasattr(os, "readlink"):
4725 funcs.append((self.filenames, os.readlink,))
4726
4727 for filenames, func, *func_args in funcs:
4728 for name in filenames:
4729 try:
4730 func(name, *func_args)
4731 except OSError as err:
4732 self.assertIs(err.filename, name, str(func))
4733 except UnicodeDecodeError:
4734 pass
4735 else:
4736 self.fail(f"No exception thrown by {func}")
4737
4738 class CPUCountTests(unittest.TestCase):
4739 def check_cpu_count(self, cpus):
4740 if cpus is None:
4741 self.skipTest("Could not determine the number of CPUs")
4742
4743 self.assertIsInstance(cpus, int)
4744 self.assertGreater(cpus, 0)
4745
4746 def test_cpu_count(self):
4747 cpus = os.cpu_count()
4748 self.check_cpu_count(cpus)
4749
4750 def test_process_cpu_count(self):
4751 cpus = os.process_cpu_count()
4752 self.assertLessEqual(cpus, os.cpu_count())
4753 self.check_cpu_count(cpus)
4754
4755 @unittest.skipUnless(hasattr(os, 'sched_setaffinity'),
4756 "don't have sched affinity support")
4757 def test_process_cpu_count_affinity(self):
4758 affinity1 = os.process_cpu_count()
4759 if affinity1 is None:
4760 self.skipTest("Could not determine the number of CPUs")
4761
4762 # Disable one CPU
4763 mask = os.sched_getaffinity(0)
4764 if len(mask) <= 1:
4765 self.skipTest(f"sched_getaffinity() returns less than "
4766 f"2 CPUs: {sorted(mask)}")
4767 self.addCleanup(os.sched_setaffinity, 0, list(mask))
4768 mask.pop()
4769 os.sched_setaffinity(0, mask)
4770
4771 # test process_cpu_count()
4772 affinity2 = os.process_cpu_count()
4773 self.assertEqual(affinity2, affinity1 - 1)
4774
4775
4776 # FD inheritance check is only useful for systems with process support.
4777 @support.requires_subprocess()
4778 class FDInheritanceTests(unittest.TestCase):
4779 def test_get_set_inheritable(self):
4780 fd = os.open(__file__, os.O_RDONLY)
4781 self.addCleanup(os.close, fd)
4782 self.assertEqual(os.get_inheritable(fd), False)
4783
4784 os.set_inheritable(fd, True)
4785 self.assertEqual(os.get_inheritable(fd), True)
4786
4787 @unittest.skipIf(fcntl is None, "need fcntl")
4788 def test_get_inheritable_cloexec(self):
4789 fd = os.open(__file__, os.O_RDONLY)
4790 self.addCleanup(os.close, fd)
4791 self.assertEqual(os.get_inheritable(fd), False)
4792
4793 # clear FD_CLOEXEC flag
4794 flags = fcntl.fcntl(fd, fcntl.F_GETFD)
4795 flags &= ~fcntl.FD_CLOEXEC
4796 fcntl.fcntl(fd, fcntl.F_SETFD, flags)
4797
4798 self.assertEqual(os.get_inheritable(fd), True)
4799
4800 @unittest.skipIf(fcntl is None, "need fcntl")
4801 def test_set_inheritable_cloexec(self):
4802 fd = os.open(__file__, os.O_RDONLY)
4803 self.addCleanup(os.close, fd)
4804 self.assertEqual(fcntl.fcntl(fd, fcntl.F_GETFD) & fcntl.FD_CLOEXEC,
4805 fcntl.FD_CLOEXEC)
4806
4807 os.set_inheritable(fd, True)
4808 self.assertEqual(fcntl.fcntl(fd, fcntl.F_GETFD) & fcntl.FD_CLOEXEC,
4809 0)
4810
4811 @unittest.skipUnless(hasattr(os, 'O_PATH'), "need os.O_PATH")
4812 def test_get_set_inheritable_o_path(self):
4813 fd = os.open(__file__, os.O_PATH)
4814 self.addCleanup(os.close, fd)
4815 self.assertEqual(os.get_inheritable(fd), False)
4816
4817 os.set_inheritable(fd, True)
4818 self.assertEqual(os.get_inheritable(fd), True)
4819
4820 os.set_inheritable(fd, False)
4821 self.assertEqual(os.get_inheritable(fd), False)
4822
4823 def test_get_set_inheritable_badf(self):
4824 fd = os_helper.make_bad_fd()
4825
4826 with self.assertRaises(OSError) as ctx:
4827 os.get_inheritable(fd)
4828 self.assertEqual(ctx.exception.errno, errno.EBADF)
4829
4830 with self.assertRaises(OSError) as ctx:
4831 os.set_inheritable(fd, True)
4832 self.assertEqual(ctx.exception.errno, errno.EBADF)
4833
4834 with self.assertRaises(OSError) as ctx:
4835 os.set_inheritable(fd, False)
4836 self.assertEqual(ctx.exception.errno, errno.EBADF)
4837
4838 def test_open(self):
4839 fd = os.open(__file__, os.O_RDONLY)
4840 self.addCleanup(os.close, fd)
4841 self.assertEqual(os.get_inheritable(fd), False)
4842
4843 @unittest.skipUnless(hasattr(os, 'pipe'), "need os.pipe()")
4844 def test_pipe(self):
4845 rfd, wfd = os.pipe()
4846 self.addCleanup(os.close, rfd)
4847 self.addCleanup(os.close, wfd)
4848 self.assertEqual(os.get_inheritable(rfd), False)
4849 self.assertEqual(os.get_inheritable(wfd), False)
4850
4851 def test_dup(self):
4852 fd1 = os.open(__file__, os.O_RDONLY)
4853 self.addCleanup(os.close, fd1)
4854
4855 fd2 = os.dup(fd1)
4856 self.addCleanup(os.close, fd2)
4857 self.assertEqual(os.get_inheritable(fd2), False)
4858
4859 def test_dup_standard_stream(self):
4860 fd = os.dup(1)
4861 self.addCleanup(os.close, fd)
4862 self.assertGreater(fd, 0)
4863
4864 @unittest.skipUnless(sys.platform == 'win32', 'win32-specific test')
4865 def test_dup_nul(self):
4866 # os.dup() was creating inheritable fds for character files.
4867 fd1 = os.open('NUL', os.O_RDONLY)
4868 self.addCleanup(os.close, fd1)
4869 fd2 = os.dup(fd1)
4870 self.addCleanup(os.close, fd2)
4871 self.assertFalse(os.get_inheritable(fd2))
4872
4873 @unittest.skipUnless(hasattr(os, 'dup2'), "need os.dup2()")
4874 def test_dup2(self):
4875 fd = os.open(__file__, os.O_RDONLY)
4876 self.addCleanup(os.close, fd)
4877
4878 # inheritable by default
4879 fd2 = os.open(__file__, os.O_RDONLY)
4880 self.addCleanup(os.close, fd2)
4881 self.assertEqual(os.dup2(fd, fd2), fd2)
4882 self.assertTrue(os.get_inheritable(fd2))
4883
4884 # force non-inheritable
4885 fd3 = os.open(__file__, os.O_RDONLY)
4886 self.addCleanup(os.close, fd3)
4887 self.assertEqual(os.dup2(fd, fd3, inheritable=False), fd3)
4888 self.assertFalse(os.get_inheritable(fd3))
4889
4890 @unittest.skipUnless(hasattr(os, 'openpty'), "need os.openpty()")
4891 class PseudoterminalTests(unittest.TestCase):
4892 def open_pty(self):
4893 """Open a pty fd-pair, and schedule cleanup for it"""
4894 main_fd, second_fd = os.openpty()
4895 self.addCleanup(os.close, main_fd)
4896 self.addCleanup(os.close, second_fd)
4897 return main_fd, second_fd
4898
4899 def test_openpty(self):
4900 main_fd, second_fd = self.open_pty()
4901 self.assertEqual(os.get_inheritable(main_fd), False)
4902 self.assertEqual(os.get_inheritable(second_fd), False)
4903
4904 @unittest.skipUnless(hasattr(os, 'ptsname'), "need os.ptsname()")
4905 @unittest.skipUnless(hasattr(os, 'O_RDWR'), "need os.O_RDWR")
4906 @unittest.skipUnless(hasattr(os, 'O_NOCTTY'), "need os.O_NOCTTY")
4907 def test_open_via_ptsname(self):
4908 main_fd, second_fd = self.open_pty()
4909 second_path = os.ptsname(main_fd)
4910 reopened_second_fd = os.open(second_path, os.O_RDWR|os.O_NOCTTY)
4911 self.addCleanup(os.close, reopened_second_fd)
4912 os.write(reopened_second_fd, b'foo')
4913 self.assertEqual(os.read(main_fd, 3), b'foo')
4914
4915 @unittest.skipUnless(hasattr(os, 'posix_openpt'), "need os.posix_openpt()")
4916 @unittest.skipUnless(hasattr(os, 'grantpt'), "need os.grantpt()")
4917 @unittest.skipUnless(hasattr(os, 'unlockpt'), "need os.unlockpt()")
4918 @unittest.skipUnless(hasattr(os, 'ptsname'), "need os.ptsname()")
4919 @unittest.skipUnless(hasattr(os, 'O_RDWR'), "need os.O_RDWR")
4920 @unittest.skipUnless(hasattr(os, 'O_NOCTTY'), "need os.O_NOCTTY")
4921 def test_posix_pty_functions(self):
4922 mother_fd = os.posix_openpt(os.O_RDWR|os.O_NOCTTY)
4923 self.addCleanup(os.close, mother_fd)
4924 os.grantpt(mother_fd)
4925 os.unlockpt(mother_fd)
4926 son_path = os.ptsname(mother_fd)
4927 son_fd = os.open(son_path, os.O_RDWR|os.O_NOCTTY)
4928 self.addCleanup(os.close, son_fd)
4929 self.assertEqual(os.ptsname(mother_fd), os.ttyname(son_fd))
4930
4931 @unittest.skipUnless(hasattr(os, 'spawnl'), "need os.spawnl()")
4932 @support.requires_subprocess()
4933 def test_pipe_spawnl(self):
4934 # gh-77046: On Windows, os.pipe() file descriptors must be created with
4935 # _O_NOINHERIT to make them non-inheritable. UCRT has no public API to
4936 # get (_osfile(fd) & _O_NOINHERIT), so use a functional test.
4937 #
4938 # Make sure that fd is not inherited by a child process created by
4939 # os.spawnl(): get_osfhandle() and dup() must fail with EBADF.
4940
4941 fd, fd2 = os.pipe()
4942 self.addCleanup(os.close, fd)
4943 self.addCleanup(os.close, fd2)
4944
4945 code = textwrap.dedent(f"""
4946 import errno
4947 import os
4948 import test.support
4949 try:
4950 import msvcrt
4951 except ImportError:
4952 msvcrt = None
4953
4954 fd = {fd}
4955
4956 with test.support.SuppressCrashReport():
4957 if msvcrt is not None:
4958 try:
4959 handle = msvcrt.get_osfhandle(fd)
4960 except OSError as exc:
4961 if exc.errno != errno.EBADF:
4962 raise
4963 # get_osfhandle(fd) failed with EBADF as expected
4964 else:
4965 raise Exception("get_osfhandle() must fail")
4966
4967 try:
4968 fd3 = os.dup(fd)
4969 except OSError as exc:
4970 if exc.errno != errno.EBADF:
4971 raise
4972 # os.dup(fd) failed with EBADF as expected
4973 else:
4974 os.close(fd3)
4975 raise Exception("dup must fail")
4976 """)
4977
4978 filename = os_helper.TESTFN
4979 self.addCleanup(os_helper.unlink, os_helper.TESTFN)
4980 with open(filename, "w") as fp:
4981 print(code, file=fp, end="")
4982
4983 executable = sys.executable
4984 cmd = [executable, filename]
4985 if os.name == "nt" and " " in cmd[0]:
4986 cmd[0] = f'"{cmd[0]}"'
4987 exitcode = os.spawnl(os.P_WAIT, executable, *cmd)
4988 self.assertEqual(exitcode, 0)
4989
4990
4991 class PathTConverterTests(unittest.TestCase):
4992 # tuples of (function name, allows fd arguments, additional arguments to
4993 # function, cleanup function)
4994 functions = [
4995 ('stat', True, (), None),
4996 ('lstat', False, (), None),
4997 ('access', False, (os.F_OK,), None),
4998 ('chflags', False, (0,), None),
4999 ('lchflags', False, (0,), None),
5000 ('open', False, (os.O_RDONLY,), getattr(os, 'close', None)),
5001 ]
5002
5003 def test_path_t_converter(self):
5004 str_filename = os_helper.TESTFN
5005 if os.name == 'nt':
5006 bytes_fspath = bytes_filename = None
5007 else:
5008 bytes_filename = os.fsencode(os_helper.TESTFN)
5009 bytes_fspath = FakePath(bytes_filename)
5010 fd = os.open(FakePath(str_filename), os.O_WRONLY|os.O_CREAT)
5011 self.addCleanup(os_helper.unlink, os_helper.TESTFN)
5012 self.addCleanup(os.close, fd)
5013
5014 int_fspath = FakePath(fd)
5015 str_fspath = FakePath(str_filename)
5016
5017 for name, allow_fd, extra_args, cleanup_fn in self.functions:
5018 with self.subTest(name=name):
5019 try:
5020 fn = getattr(os, name)
5021 except AttributeError:
5022 continue
5023
5024 for path in (str_filename, bytes_filename, str_fspath,
5025 bytes_fspath):
5026 if path is None:
5027 continue
5028 with self.subTest(name=name, path=path):
5029 result = fn(path, *extra_args)
5030 if cleanup_fn is not None:
5031 cleanup_fn(result)
5032
5033 with self.assertRaisesRegex(
5034 TypeError, 'to return str or bytes'):
5035 fn(int_fspath, *extra_args)
5036
5037 if allow_fd:
5038 result = fn(fd, *extra_args) # should not fail
5039 if cleanup_fn is not None:
5040 cleanup_fn(result)
5041 else:
5042 with self.assertRaisesRegex(
5043 TypeError,
5044 'os.PathLike'):
5045 fn(fd, *extra_args)
5046
5047 def test_path_t_converter_and_custom_class(self):
5048 msg = r'__fspath__\(\) to return str or bytes, not %s'
5049 with self.assertRaisesRegex(TypeError, msg % r'int'):
5050 os.stat(FakePath(2))
5051 with self.assertRaisesRegex(TypeError, msg % r'float'):
5052 os.stat(FakePath(2.34))
5053 with self.assertRaisesRegex(TypeError, msg % r'object'):
5054 os.stat(FakePath(object()))
5055
5056
5057 @unittest.skipUnless(hasattr(os, 'get_blocking'),
5058 'needs os.get_blocking() and os.set_blocking()')
5059 @unittest.skipIf(support.is_emscripten, "Cannot unset blocking flag")
5060 @unittest.skipIf(sys.platform == 'win32', 'Windows only supports blocking on pipes')
5061 class BlockingTests(unittest.TestCase):
5062 def test_blocking(self):
5063 fd = os.open(__file__, os.O_RDONLY)
5064 self.addCleanup(os.close, fd)
5065 self.assertEqual(os.get_blocking(fd), True)
5066
5067 os.set_blocking(fd, False)
5068 self.assertEqual(os.get_blocking(fd), False)
5069
5070 os.set_blocking(fd, True)
5071 self.assertEqual(os.get_blocking(fd), True)
5072
5073
5074
5075 class ExportsTests(unittest.TestCase):
5076 def test_os_all(self):
5077 self.assertIn('open', os.__all__)
5078 self.assertIn('walk', os.__all__)
5079
5080
5081 class TestDirEntry(unittest.TestCase):
5082 def setUp(self):
5083 self.path = os.path.realpath(os_helper.TESTFN)
5084 self.addCleanup(os_helper.rmtree, self.path)
5085 os.mkdir(self.path)
5086
5087 def test_uninstantiable(self):
5088 self.assertRaises(TypeError, os.DirEntry)
5089
5090 def test_unpickable(self):
5091 filename = create_file(os.path.join(self.path, "file.txt"), b'python')
5092 entry = [entry for entry in os.scandir(self.path)].pop()
5093 self.assertIsInstance(entry, os.DirEntry)
5094 self.assertEqual(entry.name, "file.txt")
5095 import pickle
5096 self.assertRaises(TypeError, pickle.dumps, entry, filename)
5097
5098
5099 class TestScandir(unittest.TestCase):
5100 check_no_resource_warning = warnings_helper.check_no_resource_warning
5101
5102 def setUp(self):
5103 self.path = os.path.realpath(os_helper.TESTFN)
5104 self.bytes_path = os.fsencode(self.path)
5105 self.addCleanup(os_helper.rmtree, self.path)
5106 os.mkdir(self.path)
5107
5108 def create_file(self, name="file.txt"):
5109 path = self.bytes_path if isinstance(name, bytes) else self.path
5110 filename = os.path.join(path, name)
5111 create_file(filename, b'python')
5112 return filename
5113
5114 def get_entries(self, names):
5115 entries = dict((entry.name, entry)
5116 for entry in os.scandir(self.path))
5117 self.assertEqual(sorted(entries.keys()), names)
5118 return entries
5119
5120 def assert_stat_equal(self, stat1, stat2, skip_fields):
5121 if skip_fields:
5122 for attr in dir(stat1):
5123 if not attr.startswith("st_"):
5124 continue
5125 if attr in ("st_dev", "st_ino", "st_nlink", "st_ctime",
5126 "st_ctime_ns"):
5127 continue
5128 self.assertEqual(getattr(stat1, attr),
5129 getattr(stat2, attr),
5130 (stat1, stat2, attr))
5131 else:
5132 self.assertEqual(stat1, stat2)
5133
5134 def test_uninstantiable(self):
5135 scandir_iter = os.scandir(self.path)
5136 self.assertRaises(TypeError, type(scandir_iter))
5137 scandir_iter.close()
5138
5139 def test_unpickable(self):
5140 filename = self.create_file("file.txt")
5141 scandir_iter = os.scandir(self.path)
5142 import pickle
5143 self.assertRaises(TypeError, pickle.dumps, scandir_iter, filename)
5144 scandir_iter.close()
5145
5146 def check_entry(self, entry, name, is_dir, is_file, is_symlink):
5147 self.assertIsInstance(entry, os.DirEntry)
5148 self.assertEqual(entry.name, name)
5149 self.assertEqual(entry.path, os.path.join(self.path, name))
5150 self.assertEqual(entry.inode(),
5151 os.stat(entry.path, follow_symlinks=False).st_ino)
5152
5153 entry_stat = os.stat(entry.path)
5154 self.assertEqual(entry.is_dir(),
5155 stat.S_ISDIR(entry_stat.st_mode))
5156 self.assertEqual(entry.is_file(),
5157 stat.S_ISREG(entry_stat.st_mode))
5158 self.assertEqual(entry.is_symlink(),
5159 os.path.islink(entry.path))
5160
5161 entry_lstat = os.stat(entry.path, follow_symlinks=False)
5162 self.assertEqual(entry.is_dir(follow_symlinks=False),
5163 stat.S_ISDIR(entry_lstat.st_mode))
5164 self.assertEqual(entry.is_file(follow_symlinks=False),
5165 stat.S_ISREG(entry_lstat.st_mode))
5166
5167 self.assertEqual(entry.is_junction(), os.path.isjunction(entry.path))
5168
5169 self.assert_stat_equal(entry.stat(),
5170 entry_stat,
5171 os.name == 'nt' and not is_symlink)
5172 self.assert_stat_equal(entry.stat(follow_symlinks=False),
5173 entry_lstat,
5174 os.name == 'nt')
5175
5176 def test_attributes(self):
5177 link = os_helper.can_hardlink()
5178 symlink = os_helper.can_symlink()
5179
5180 dirname = os.path.join(self.path, "dir")
5181 os.mkdir(dirname)
5182 filename = self.create_file("file.txt")
5183 if link:
5184 try:
5185 os.link(filename, os.path.join(self.path, "link_file.txt"))
5186 except PermissionError as e:
5187 self.skipTest('os.link(): %s' % e)
5188 if symlink:
5189 os.symlink(dirname, os.path.join(self.path, "symlink_dir"),
5190 target_is_directory=True)
5191 os.symlink(filename, os.path.join(self.path, "symlink_file.txt"))
5192
5193 names = ['dir', 'file.txt']
5194 if link:
5195 names.append('link_file.txt')
5196 if symlink:
5197 names.extend(('symlink_dir', 'symlink_file.txt'))
5198 entries = self.get_entries(names)
5199
5200 entry = entries['dir']
5201 self.check_entry(entry, 'dir', True, False, False)
5202
5203 entry = entries['file.txt']
5204 self.check_entry(entry, 'file.txt', False, True, False)
5205
5206 if link:
5207 entry = entries['link_file.txt']
5208 self.check_entry(entry, 'link_file.txt', False, True, False)
5209
5210 if symlink:
5211 entry = entries['symlink_dir']
5212 self.check_entry(entry, 'symlink_dir', True, False, True)
5213
5214 entry = entries['symlink_file.txt']
5215 self.check_entry(entry, 'symlink_file.txt', False, True, True)
5216
5217 @unittest.skipIf(sys.platform != 'win32', "Can only test junctions with creation on win32.")
5218 def test_attributes_junctions(self):
5219 dirname = os.path.join(self.path, "tgtdir")
5220 os.mkdir(dirname)
5221
5222 import _winapi
5223 try:
5224 _winapi.CreateJunction(dirname, os.path.join(self.path, "srcjunc"))
5225 except OSError:
5226 raise unittest.SkipTest('creating the test junction failed')
5227
5228 entries = self.get_entries(['srcjunc', 'tgtdir'])
5229 self.assertEqual(entries['srcjunc'].is_junction(), True)
5230 self.assertEqual(entries['tgtdir'].is_junction(), False)
5231
5232 def get_entry(self, name):
5233 path = self.bytes_path if isinstance(name, bytes) else self.path
5234 entries = list(os.scandir(path))
5235 self.assertEqual(len(entries), 1)
5236
5237 entry = entries[0]
5238 self.assertEqual(entry.name, name)
5239 return entry
5240
5241 def create_file_entry(self, name='file.txt'):
5242 filename = self.create_file(name=name)
5243 return self.get_entry(os.path.basename(filename))
5244
5245 def test_current_directory(self):
5246 filename = self.create_file()
5247 old_dir = os.getcwd()
5248 try:
5249 os.chdir(self.path)
5250
5251 # call scandir() without parameter: it must list the content
5252 # of the current directory
5253 entries = dict((entry.name, entry) for entry in os.scandir())
5254 self.assertEqual(sorted(entries.keys()),
5255 [os.path.basename(filename)])
5256 finally:
5257 os.chdir(old_dir)
5258
5259 def test_repr(self):
5260 entry = self.create_file_entry()
5261 self.assertEqual(repr(entry), "<DirEntry 'file.txt'>")
5262
5263 def test_fspath_protocol(self):
5264 entry = self.create_file_entry()
5265 self.assertEqual(os.fspath(entry), os.path.join(self.path, 'file.txt'))
5266
5267 def test_fspath_protocol_bytes(self):
5268 bytes_filename = os.fsencode('bytesfile.txt')
5269 bytes_entry = self.create_file_entry(name=bytes_filename)
5270 fspath = os.fspath(bytes_entry)
5271 self.assertIsInstance(fspath, bytes)
5272 self.assertEqual(fspath,
5273 os.path.join(os.fsencode(self.path),bytes_filename))
5274
5275 def test_removed_dir(self):
5276 path = os.path.join(self.path, 'dir')
5277
5278 os.mkdir(path)
5279 entry = self.get_entry('dir')
5280 os.rmdir(path)
5281
5282 # On POSIX, is_dir() result depends if scandir() filled d_type or not
5283 if os.name == 'nt':
5284 self.assertTrue(entry.is_dir())
5285 self.assertFalse(entry.is_file())
5286 self.assertFalse(entry.is_symlink())
5287 if os.name == 'nt':
5288 self.assertRaises(FileNotFoundError, entry.inode)
5289 # don't fail
5290 entry.stat()
5291 entry.stat(follow_symlinks=False)
5292 else:
5293 self.assertGreater(entry.inode(), 0)
5294 self.assertRaises(FileNotFoundError, entry.stat)
5295 self.assertRaises(FileNotFoundError, entry.stat, follow_symlinks=False)
5296
5297 def test_removed_file(self):
5298 entry = self.create_file_entry()
5299 os.unlink(entry.path)
5300
5301 self.assertFalse(entry.is_dir())
5302 # On POSIX, is_dir() result depends if scandir() filled d_type or not
5303 if os.name == 'nt':
5304 self.assertTrue(entry.is_file())
5305 self.assertFalse(entry.is_symlink())
5306 if os.name == 'nt':
5307 self.assertRaises(FileNotFoundError, entry.inode)
5308 # don't fail
5309 entry.stat()
5310 entry.stat(follow_symlinks=False)
5311 else:
5312 self.assertGreater(entry.inode(), 0)
5313 self.assertRaises(FileNotFoundError, entry.stat)
5314 self.assertRaises(FileNotFoundError, entry.stat, follow_symlinks=False)
5315
5316 def test_broken_symlink(self):
5317 if not os_helper.can_symlink():
5318 return self.skipTest('cannot create symbolic link')
5319
5320 filename = self.create_file("file.txt")
5321 os.symlink(filename,
5322 os.path.join(self.path, "symlink.txt"))
5323 entries = self.get_entries(['file.txt', 'symlink.txt'])
5324 entry = entries['symlink.txt']
5325 os.unlink(filename)
5326
5327 self.assertGreater(entry.inode(), 0)
5328 self.assertFalse(entry.is_dir())
5329 self.assertFalse(entry.is_file()) # broken symlink returns False
5330 self.assertFalse(entry.is_dir(follow_symlinks=False))
5331 self.assertFalse(entry.is_file(follow_symlinks=False))
5332 self.assertTrue(entry.is_symlink())
5333 self.assertRaises(FileNotFoundError, entry.stat)
5334 # don't fail
5335 entry.stat(follow_symlinks=False)
5336
5337 def test_bytes(self):
5338 self.create_file("file.txt")
5339
5340 path_bytes = os.fsencode(self.path)
5341 entries = list(os.scandir(path_bytes))
5342 self.assertEqual(len(entries), 1, entries)
5343 entry = entries[0]
5344
5345 self.assertEqual(entry.name, b'file.txt')
5346 self.assertEqual(entry.path,
5347 os.fsencode(os.path.join(self.path, 'file.txt')))
5348
5349 def test_bytes_like(self):
5350 self.create_file("file.txt")
5351
5352 for cls in bytearray, memoryview:
5353 path_bytes = cls(os.fsencode(self.path))
5354 with self.assertRaises(TypeError):
5355 os.scandir(path_bytes)
5356
5357 @unittest.skipUnless(os.listdir in os.supports_fd,
5358 'fd support for listdir required for this test.')
5359 def test_fd(self):
5360 self.assertIn(os.scandir, os.supports_fd)
5361 self.create_file('file.txt')
5362 expected_names = ['file.txt']
5363 if os_helper.can_symlink():
5364 os.symlink('file.txt', os.path.join(self.path, 'link'))
5365 expected_names.append('link')
5366
5367 with os_helper.open_dir_fd(self.path) as fd:
5368 with os.scandir(fd) as it:
5369 entries = list(it)
5370 names = [entry.name for entry in entries]
5371 self.assertEqual(sorted(names), expected_names)
5372 self.assertEqual(names, os.listdir(fd))
5373 for entry in entries:
5374 self.assertEqual(entry.path, entry.name)
5375 self.assertEqual(os.fspath(entry), entry.name)
5376 self.assertEqual(entry.is_symlink(), entry.name == 'link')
5377 if os.stat in os.supports_dir_fd:
5378 st = os.stat(entry.name, dir_fd=fd)
5379 self.assertEqual(entry.stat(), st)
5380 st = os.stat(entry.name, dir_fd=fd, follow_symlinks=False)
5381 self.assertEqual(entry.stat(follow_symlinks=False), st)
5382
5383 @unittest.skipIf(support.is_wasi, "WASI maps '' to cwd")
5384 def test_empty_path(self):
5385 self.assertRaises(FileNotFoundError, os.scandir, '')
5386
5387 def test_consume_iterator_twice(self):
5388 self.create_file("file.txt")
5389 iterator = os.scandir(self.path)
5390
5391 entries = list(iterator)
5392 self.assertEqual(len(entries), 1, entries)
5393
5394 # check than consuming the iterator twice doesn't raise exception
5395 entries2 = list(iterator)
5396 self.assertEqual(len(entries2), 0, entries2)
5397
5398 def test_bad_path_type(self):
5399 for obj in [1.234, {}, []]:
5400 self.assertRaises(TypeError, os.scandir, obj)
5401
5402 def test_close(self):
5403 self.create_file("file.txt")
5404 self.create_file("file2.txt")
5405 iterator = os.scandir(self.path)
5406 next(iterator)
5407 iterator.close()
5408 # multiple closes
5409 iterator.close()
5410 with self.check_no_resource_warning():
5411 del iterator
5412
5413 def test_context_manager(self):
5414 self.create_file("file.txt")
5415 self.create_file("file2.txt")
5416 with os.scandir(self.path) as iterator:
5417 next(iterator)
5418 with self.check_no_resource_warning():
5419 del iterator
5420
5421 def test_context_manager_close(self):
5422 self.create_file("file.txt")
5423 self.create_file("file2.txt")
5424 with os.scandir(self.path) as iterator:
5425 next(iterator)
5426 iterator.close()
5427
5428 def test_context_manager_exception(self):
5429 self.create_file("file.txt")
5430 self.create_file("file2.txt")
5431 with self.assertRaises(ZeroDivisionError):
5432 with os.scandir(self.path) as iterator:
5433 next(iterator)
5434 1/0
5435 with self.check_no_resource_warning():
5436 del iterator
5437
5438 def test_resource_warning(self):
5439 self.create_file("file.txt")
5440 self.create_file("file2.txt")
5441 iterator = os.scandir(self.path)
5442 next(iterator)
5443 with self.assertWarns(ResourceWarning):
5444 del iterator
5445 support.gc_collect()
5446 # exhausted iterator
5447 iterator = os.scandir(self.path)
5448 list(iterator)
5449 with self.check_no_resource_warning():
5450 del iterator
5451
5452
5453 class TestPEP519(unittest.TestCase):
5454
5455 # Abstracted so it can be overridden to test pure Python implementation
5456 # if a C version is provided.
5457 fspath = staticmethod(os.fspath)
5458
5459 def test_return_bytes(self):
5460 for b in b'hello', b'goodbye', b'some/path/and/file':
5461 self.assertEqual(b, self.fspath(b))
5462
5463 def test_return_string(self):
5464 for s in 'hello', 'goodbye', 'some/path/and/file':
5465 self.assertEqual(s, self.fspath(s))
5466
5467 def test_fsencode_fsdecode(self):
5468 for p in "path/like/object", b"path/like/object":
5469 pathlike = FakePath(p)
5470
5471 self.assertEqual(p, self.fspath(pathlike))
5472 self.assertEqual(b"path/like/object", os.fsencode(pathlike))
5473 self.assertEqual("path/like/object", os.fsdecode(pathlike))
5474
5475 def test_pathlike(self):
5476 self.assertEqual('#feelthegil', self.fspath(FakePath('#feelthegil')))
5477 self.assertIsSubclass(FakePath, os.PathLike)
5478 self.assertIsInstance(FakePath('x'), os.PathLike)
5479
5480 def test_garbage_in_exception_out(self):
5481 vapor = type('blah', (), {})
5482 for o in int, type, os, vapor():
5483 self.assertRaises(TypeError, self.fspath, o)
5484
5485 def test_argument_required(self):
5486 self.assertRaises(TypeError, self.fspath)
5487
5488 def test_bad_pathlike(self):
5489 # __fspath__ returns a value other than str or bytes.
5490 self.assertRaises(TypeError, self.fspath, FakePath(42))
5491 # __fspath__ attribute that is not callable.
5492 c = type('foo', (), {})
5493 c.__fspath__ = 1
5494 self.assertRaises(TypeError, self.fspath, c())
5495 # __fspath__ raises an exception.
5496 self.assertRaises(ZeroDivisionError, self.fspath,
5497 FakePath(ZeroDivisionError()))
5498
5499 def test_pathlike_subclasshook(self):
5500 # bpo-38878: subclasshook causes subclass checks
5501 # true on abstract implementation.
5502 class A(os.PathLike):
5503 pass
5504 self.assertNotIsSubclass(FakePath, A)
5505 self.assertIsSubclass(FakePath, os.PathLike)
5506
5507 def test_pathlike_class_getitem(self):
5508 self.assertIsInstance(os.PathLike[bytes], types.GenericAlias)
5509
5510 def test_pathlike_subclass_slots(self):
5511 class A(os.PathLike):
5512 __slots__ = ()
5513 def __fspath__(self):
5514 return ''
5515 self.assertNotHasAttr(A(), '__dict__')
5516
5517 def test_fspath_set_to_None(self):
5518 class Foo:
5519 __fspath__ = None
5520
5521 class Bar:
5522 def __fspath__(self):
5523 return 'bar'
5524
5525 class Baz(Bar):
5526 __fspath__ = None
5527
5528 good_error_msg = (
5529 r"expected str, bytes or os.PathLike object, not {}".format
5530 )
5531
5532 with self.assertRaisesRegex(TypeError, good_error_msg("Foo")):
5533 self.fspath(Foo())
5534
5535 self.assertEqual(self.fspath(Bar()), 'bar')
5536
5537 with self.assertRaisesRegex(TypeError, good_error_msg("Baz")):
5538 self.fspath(Baz())
5539
5540 with self.assertRaisesRegex(TypeError, good_error_msg("Foo")):
5541 open(Foo())
5542
5543 with self.assertRaisesRegex(TypeError, good_error_msg("Baz")):
5544 open(Baz())
5545
5546 other_good_error_msg = (
5547 r"should be string, bytes or os.PathLike, not {}".format
5548 )
5549
5550 with self.assertRaisesRegex(TypeError, other_good_error_msg("Foo")):
5551 os.rename(Foo(), "foooo")
5552
5553 with self.assertRaisesRegex(TypeError, other_good_error_msg("Baz")):
5554 os.rename(Baz(), "bazzz")
5555
5556 class TimesTests(unittest.TestCase):
5557 def test_times(self):
5558 times = os.times()
5559 self.assertIsInstance(times, os.times_result)
5560
5561 for field in ('user', 'system', 'children_user', 'children_system',
5562 'elapsed'):
5563 value = getattr(times, field)
5564 self.assertIsInstance(value, float)
5565
5566 if os.name == 'nt':
5567 self.assertEqual(times.children_user, 0)
5568 self.assertEqual(times.children_system, 0)
5569 self.assertEqual(times.elapsed, 0)
5570
5571
5572 @support.requires_fork()
5573 class ForkTests(unittest.TestCase):
5574 def test_fork(self):
5575 # bpo-42540: ensure os.fork() with non-default memory allocator does
5576 # not crash on exit.
5577 code = """if 1:
5578 import os
5579 from test import support
5580 pid = os.fork()
5581 if pid != 0:
5582 support.wait_process(pid, exitcode=0)
5583 """
5584 assert_python_ok("-c", code)
5585 if support.Py_GIL_DISABLED:
5586 assert_python_ok("-c", code, PYTHONMALLOC="mimalloc_debug")
5587 else:
5588 assert_python_ok("-c", code, PYTHONMALLOC="malloc_debug")
5589
5590 @unittest.skipUnless(sys.platform in ("linux", "android", "darwin"),
5591 "Only Linux and macOS detect this today.")
5592 @unittest.skipIf(_testcapi is None, "requires _testcapi")
5593 def test_fork_warns_when_non_python_thread_exists(self):
5594 code = """if 1:
5595 import os, threading, warnings
5596 from _testcapi import _spawn_pthread_waiter, _end_spawned_pthread
5597 _spawn_pthread_waiter()
5598 try:
5599 with warnings.catch_warnings(record=True) as ws:
5600 warnings.filterwarnings(
5601 "always", category=DeprecationWarning)
5602 if os.fork() == 0:
5603 assert not ws, f"unexpected warnings in child: {ws}"
5604 os._exit(0) # child
5605 else:
5606 assert ws[0].category == DeprecationWarning, ws[0]
5607 assert 'fork' in str(ws[0].message), ws[0]
5608 # Waiting allows an error in the child to hit stderr.
5609 exitcode = os.wait()[1]
5610 assert exitcode == 0, f"child exited {exitcode}"
5611 assert threading.active_count() == 1, threading.enumerate()
5612 finally:
5613 _end_spawned_pthread()
5614 """
5615 _, out, err = assert_python_ok("-c", code, PYTHONOPTIMIZE='0')
5616 self.assertEqual(err.decode("utf-8"), "")
5617 self.assertEqual(out.decode("utf-8"), "")
5618
5619 def test_fork_at_finalization(self):
5620 code = """if 1:
5621 import atexit
5622 import os
5623
5624 class AtFinalization:
5625 def __del__(self):
5626 print("OK")
5627 pid = os.fork()
5628 if pid != 0:
5629 print("shouldn't be printed")
5630 at_finalization = AtFinalization()
5631 """
5632 _, out, err = assert_python_ok("-c", code)
5633 self.assertEqual(b"OK\n", out)
5634 self.assertIn(b"can't fork at interpreter shutdown", err)
5635
5636
5637 # Only test if the C version is provided, otherwise TestPEP519 already tested
5638 # the pure Python implementation.
5639 if hasattr(os, "_fspath"):
5640 class TestPEP519PurePython(TestPEP519):
5641
5642 """Explicitly test the pure Python implementation of os.fspath()."""
5643
5644 fspath = staticmethod(os._fspath)
5645
5646
5647 if __name__ == "__main__":
5648 unittest.main()