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 self.server.handler_instance.accumulate = False
4046 with self.assertRaises(OSError) as cm:
4047 await self.async_sendfile(self.sockno, self.fileno, 0, 0,
4048 headers=[b"x" * 2**16] * 2**15)
4049 self.assertEqual(cm.exception.errno, errno.EINVAL)
4050
4051 @requires_headers_trailers
4052 @requires_32b
4053 async def test_trailers_overflow_32bits(self):
4054 self.server.handler_instance.accumulate = False
4055 with self.assertRaises(OSError) as cm:
4056 await self.async_sendfile(self.sockno, self.fileno, 0, 0,
4057 trailers=[b"x" * 2**16] * 2**15)
4058 self.assertEqual(cm.exception.errno, errno.EINVAL)
4059
4060 @requires_headers_trailers
4061 @unittest.skipUnless(hasattr(os, 'SF_NODISKIO'),
4062 'test needs os.SF_NODISKIO')
4063 async def test_flags(self):
4064 try:
4065 await self.async_sendfile(self.sockno, self.fileno, 0, 4096,
4066 flags=os.SF_NODISKIO)
4067 except OSError as err:
4068 if err.errno not in (errno.EBUSY, errno.EAGAIN):
4069 raise
4070
4071
4072 def supports_extended_attributes():
4073 if not hasattr(os, "setxattr"):
4074 return False
4075
4076 try:
4077 with open(os_helper.TESTFN, "xb", 0) as fp:
4078 try:
4079 os.setxattr(fp.fileno(), b"user.test", b"")
4080 except OSError:
4081 return False
4082 finally:
4083 os_helper.unlink(os_helper.TESTFN)
4084
4085 return True
4086
4087
4088 @unittest.skipUnless(supports_extended_attributes(),
4089 "no non-broken extended attribute support")
4090 # Kernels < 2.6.39 don't respect setxattr flags.
4091 @support.requires_linux_version(2, 6, 39)
4092 class ExtendedAttributeTests(unittest.TestCase):
4093
4094 def _check_xattrs_str(self, s, getxattr, setxattr, removexattr, listxattr, **kwargs):
4095 fn = os_helper.TESTFN
4096 self.addCleanup(os_helper.unlink, fn)
4097 create_file(fn)
4098
4099 with self.assertRaises(OSError) as cm:
4100 getxattr(fn, s("user.test"), **kwargs)
4101 self.assertEqual(cm.exception.errno, errno.ENODATA)
4102
4103 init_xattr = listxattr(fn)
4104 self.assertIsInstance(init_xattr, list)
4105
4106 setxattr(fn, s("user.test"), b"", **kwargs)
4107 xattr = set(init_xattr)
4108 xattr.add("user.test")
4109 self.assertEqual(set(listxattr(fn)), xattr)
4110 self.assertEqual(getxattr(fn, b"user.test", **kwargs), b"")
4111 setxattr(fn, s("user.test"), b"hello", os.XATTR_REPLACE, **kwargs)
4112 self.assertEqual(getxattr(fn, b"user.test", **kwargs), b"hello")
4113
4114 with self.assertRaises(OSError) as cm:
4115 setxattr(fn, s("user.test"), b"bye", os.XATTR_CREATE, **kwargs)
4116 self.assertEqual(cm.exception.errno, errno.EEXIST)
4117
4118 with self.assertRaises(OSError) as cm:
4119 setxattr(fn, s("user.test2"), b"bye", os.XATTR_REPLACE, **kwargs)
4120 self.assertEqual(cm.exception.errno, errno.ENODATA)
4121
4122 setxattr(fn, s("user.test2"), b"foo", os.XATTR_CREATE, **kwargs)
4123 xattr.add("user.test2")
4124 self.assertEqual(set(listxattr(fn)), xattr)
4125 removexattr(fn, s("user.test"), **kwargs)
4126
4127 with self.assertRaises(OSError) as cm:
4128 getxattr(fn, s("user.test"), **kwargs)
4129 self.assertEqual(cm.exception.errno, errno.ENODATA)
4130
4131 xattr.remove("user.test")
4132 self.assertEqual(set(listxattr(fn)), xattr)
4133 self.assertEqual(getxattr(fn, s("user.test2"), **kwargs), b"foo")
4134 setxattr(fn, s("user.test"), b"a"*256, **kwargs)
4135 self.assertEqual(getxattr(fn, s("user.test"), **kwargs), b"a"*256)
4136 removexattr(fn, s("user.test"), **kwargs)
4137 many = sorted("user.test{}".format(i) for i in range(32))
4138 for thing in many:
4139 setxattr(fn, thing, b"x", **kwargs)
4140 self.assertEqual(set(listxattr(fn)), set(init_xattr) | set(many))
4141
4142 def _check_xattrs(self, *args, **kwargs):
4143 self._check_xattrs_str(str, *args, **kwargs)
4144 os_helper.unlink(os_helper.TESTFN)
4145
4146 self._check_xattrs_str(os.fsencode, *args, **kwargs)
4147 os_helper.unlink(os_helper.TESTFN)
4148
4149 def test_simple(self):
4150 self._check_xattrs(os.getxattr, os.setxattr, os.removexattr,
4151 os.listxattr)
4152
4153 def test_lpath(self):
4154 self._check_xattrs(os.getxattr, os.setxattr, os.removexattr,
4155 os.listxattr, follow_symlinks=False)
4156
4157 def test_fds(self):
4158 def getxattr(path, *args):
4159 with open(path, "rb") as fp:
4160 return os.getxattr(fp.fileno(), *args)
4161 def setxattr(path, *args):
4162 with open(path, "wb", 0) as fp:
4163 os.setxattr(fp.fileno(), *args)
4164 def removexattr(path, *args):
4165 with open(path, "wb", 0) as fp:
4166 os.removexattr(fp.fileno(), *args)
4167 def listxattr(path, *args):
4168 with open(path, "rb") as fp:
4169 return os.listxattr(fp.fileno(), *args)
4170 self._check_xattrs(getxattr, setxattr, removexattr, listxattr)
4171
4172
4173 @unittest.skipUnless(hasattr(os, 'get_terminal_size'), "requires os.get_terminal_size")
4174 class TermsizeTests(unittest.TestCase):
4175 def test_does_not_crash(self):
4176 """Check if get_terminal_size() returns a meaningful value.
4177
4178 There's no easy portable way to actually check the size of the
4179 terminal, so let's check if it returns something sensible instead.
4180 """
4181 try:
4182 size = os.get_terminal_size()
4183 except OSError as e:
4184 known_errnos = [errno.EINVAL, errno.ENOTTY]
4185 if sys.platform == "android":
4186 # The Android testbed redirects the native stdout to a pipe,
4187 # which returns a different error code.
4188 known_errnos.append(errno.EACCES)
4189 if sys.platform == "win32" or e.errno in known_errnos:
4190 # Under win32 a generic OSError can be thrown if the
4191 # handle cannot be retrieved
4192 self.skipTest("failed to query terminal size")
4193 raise
4194
4195 self.assertGreaterEqual(size.columns, 0)
4196 self.assertGreaterEqual(size.lines, 0)
4197
4198 @support.requires_subprocess()
4199 def test_stty_match(self):
4200 """Check if stty returns the same results
4201
4202 stty actually tests stdin, so get_terminal_size is invoked on
4203 stdin explicitly. If stty succeeded, then get_terminal_size()
4204 should work too.
4205 """
4206 try:
4207 size = (
4208 subprocess.check_output(
4209 ["stty", "size"], stderr=subprocess.DEVNULL, text=True
4210 ).split()
4211 )
4212 except (FileNotFoundError, subprocess.CalledProcessError,
4213 PermissionError):
4214 self.skipTest("stty invocation failed")
4215 expected = (int(size[1]), int(size[0])) # reversed order
4216
4217 try:
4218 actual = os.get_terminal_size(sys.__stdin__.fileno())
4219 except OSError as e:
4220 if sys.platform == "win32" or e.errno in (errno.EINVAL, errno.ENOTTY):
4221 # Under win32 a generic OSError can be thrown if the
4222 # handle cannot be retrieved
4223 self.skipTest("failed to query terminal size")
4224 raise
4225 self.assertEqual(expected, actual)
4226
4227 @unittest.skipUnless(sys.platform == 'win32', 'Windows specific test')
4228 def test_windows_fd(self):
4229 """Check if get_terminal_size() returns a meaningful value in Windows"""
4230 try:
4231 conout = open('conout$', 'w')
4232 except OSError:
4233 self.skipTest('failed to open conout$')
4234 with conout:
4235 size = os.get_terminal_size(conout.fileno())
4236
4237 self.assertGreaterEqual(size.columns, 0)
4238 self.assertGreaterEqual(size.lines, 0)
4239
4240
4241 @unittest.skipUnless(hasattr(os, 'memfd_create'), 'requires os.memfd_create')
4242 @support.requires_linux_version(3, 17)
4243 class MemfdCreateTests(unittest.TestCase):
4244 def test_memfd_create(self):
4245 fd = os.memfd_create("Hi", os.MFD_CLOEXEC)
4246 self.assertNotEqual(fd, -1)
4247 self.addCleanup(os.close, fd)
4248 self.assertFalse(os.get_inheritable(fd))
4249 with open(fd, "wb", closefd=False) as f:
4250 f.write(b'memfd_create')
4251 self.assertEqual(f.tell(), 12)
4252
4253 fd2 = os.memfd_create("Hi")
4254 self.addCleanup(os.close, fd2)
4255 self.assertFalse(os.get_inheritable(fd2))
4256
4257
4258 @unittest.skipUnless(hasattr(os, 'eventfd'), 'requires os.eventfd')
4259 @support.requires_linux_version(2, 6, 30)
4260 class EventfdTests(unittest.TestCase):
4261 def test_eventfd_initval(self):
4262 def pack(value):
4263 """Pack as native uint64_t
4264 """
4265 return struct.pack("@Q", value)
4266 size = 8 # read/write 8 bytes
4267 initval = 42
4268 fd = os.eventfd(initval)
4269 self.assertNotEqual(fd, -1)
4270 self.addCleanup(os.close, fd)
4271 self.assertFalse(os.get_inheritable(fd))
4272
4273 # test with raw read/write
4274 res = os.read(fd, size)
4275 self.assertEqual(res, pack(initval))
4276
4277 os.write(fd, pack(23))
4278 res = os.read(fd, size)
4279 self.assertEqual(res, pack(23))
4280
4281 os.write(fd, pack(40))
4282 os.write(fd, pack(2))
4283 res = os.read(fd, size)
4284 self.assertEqual(res, pack(42))
4285
4286 # test with eventfd_read/eventfd_write
4287 os.eventfd_write(fd, 20)
4288 os.eventfd_write(fd, 3)
4289 res = os.eventfd_read(fd)
4290 self.assertEqual(res, 23)
4291
4292 def test_eventfd_semaphore(self):
4293 initval = 2
4294 flags = os.EFD_CLOEXEC | os.EFD_SEMAPHORE | os.EFD_NONBLOCK
4295 fd = os.eventfd(initval, flags)
4296 self.assertNotEqual(fd, -1)
4297 self.addCleanup(os.close, fd)
4298
4299 # semaphore starts has initval 2, two reads return '1'
4300 res = os.eventfd_read(fd)
4301 self.assertEqual(res, 1)
4302 res = os.eventfd_read(fd)
4303 self.assertEqual(res, 1)
4304 # third read would block
4305 with self.assertRaises(BlockingIOError):
4306 os.eventfd_read(fd)
4307 with self.assertRaises(BlockingIOError):
4308 os.read(fd, 8)
4309
4310 # increase semaphore counter, read one
4311 os.eventfd_write(fd, 1)
4312 res = os.eventfd_read(fd)
4313 self.assertEqual(res, 1)
4314 # next read would block, too
4315 with self.assertRaises(BlockingIOError):
4316 os.eventfd_read(fd)
4317
4318 def test_eventfd_select(self):
4319 flags = os.EFD_CLOEXEC | os.EFD_NONBLOCK
4320 fd = os.eventfd(0, flags)
4321 self.assertNotEqual(fd, -1)
4322 self.addCleanup(os.close, fd)
4323
4324 # counter is zero, only writeable
4325 rfd, wfd, xfd = select.select([fd], [fd], [fd], 0)
4326 self.assertEqual((rfd, wfd, xfd), ([], [fd], []))
4327
4328 # counter is non-zero, read and writeable
4329 os.eventfd_write(fd, 23)
4330 rfd, wfd, xfd = select.select([fd], [fd], [fd], 0)
4331 self.assertEqual((rfd, wfd, xfd), ([fd], [fd], []))
4332 self.assertEqual(os.eventfd_read(fd), 23)
4333
4334 # counter at max, only readable
4335 os.eventfd_write(fd, (2**64) - 2)
4336 rfd, wfd, xfd = select.select([fd], [fd], [fd], 0)
4337 self.assertEqual((rfd, wfd, xfd), ([fd], [], []))
4338 os.eventfd_read(fd)
4339
4340 @unittest.skipUnless(hasattr(os, 'timerfd_create'), 'requires os.timerfd_create')
4341 @unittest.skipIf(sys.platform == "android", "gh-124873: Test is flaky on Android")
4342 @support.requires_linux_version(2, 6, 30)
4343 class TimerfdTests(unittest.TestCase):
4344 # gh-126112: Use 10 ms to tolerate slow buildbots
4345 CLOCK_RES_PLACES = 2 # 10 ms
4346 CLOCK_RES = 10 ** -CLOCK_RES_PLACES
4347 CLOCK_RES_NS = 10 ** (9 - CLOCK_RES_PLACES)
4348
4349 def timerfd_create(self, *args, **kwargs):
4350 fd = os.timerfd_create(*args, **kwargs)
4351 self.assertGreaterEqual(fd, 0)
4352 self.assertFalse(os.get_inheritable(fd))
4353 self.addCleanup(os.close, fd)
4354 return fd
4355
4356 def read_count_signaled(self, fd):
4357 # read 8 bytes
4358 data = os.read(fd, 8)
4359 return int.from_bytes(data, byteorder=sys.byteorder)
4360
4361 def test_timerfd_initval(self):
4362 fd = self.timerfd_create(time.CLOCK_REALTIME)
4363
4364 initial_expiration = 0.25
4365 interval = 0.125
4366
4367 # 1st call
4368 next_expiration, interval2 = os.timerfd_settime(fd, initial=initial_expiration, interval=interval)
4369 self.assertAlmostEqual(interval2, 0.0, places=self.CLOCK_RES_PLACES)
4370 self.assertAlmostEqual(next_expiration, 0.0, places=self.CLOCK_RES_PLACES)
4371
4372 # 2nd call
4373 next_expiration, interval2 = os.timerfd_settime(fd, initial=initial_expiration, interval=interval)
4374 self.assertAlmostEqual(interval2, interval, places=self.CLOCK_RES_PLACES)
4375 self.assertAlmostEqual(next_expiration, initial_expiration, places=self.CLOCK_RES_PLACES)
4376
4377 # timerfd_gettime
4378 next_expiration, interval2 = os.timerfd_gettime(fd)
4379 self.assertAlmostEqual(interval2, interval, places=self.CLOCK_RES_PLACES)
4380 self.assertAlmostEqual(next_expiration, initial_expiration, places=self.CLOCK_RES_PLACES)
4381
4382 def test_timerfd_non_blocking(self):
4383 fd = self.timerfd_create(time.CLOCK_REALTIME, flags=os.TFD_NONBLOCK)
4384
4385 # 0.1 second later
4386 initial_expiration = 0.1
4387 os.timerfd_settime(fd, initial=initial_expiration, interval=0)
4388
4389 # read() raises OSError with errno is EAGAIN for non-blocking timer.
4390 with self.assertRaises(OSError) as ctx:
4391 self.read_count_signaled(fd)
4392 self.assertEqual(ctx.exception.errno, errno.EAGAIN)
4393
4394 # Wait more than 0.1 seconds
4395 time.sleep(initial_expiration + 0.1)
4396
4397 # confirm if timerfd is readable and read() returns 1 as bytes.
4398 self.assertEqual(self.read_count_signaled(fd), 1)
4399
4400 @unittest.skipIf(sys.platform.startswith('netbsd'),
4401 "gh-131263: Skip on NetBSD due to system freeze "
4402 "with negative timer values")
4403 def test_timerfd_negative(self):
4404 one_sec_in_nsec = 10**9
4405 fd = self.timerfd_create(time.CLOCK_REALTIME)
4406
4407 test_flags = [0, os.TFD_TIMER_ABSTIME]
4408 if hasattr(os, 'TFD_TIMER_CANCEL_ON_SET'):
4409 test_flags.append(os.TFD_TIMER_ABSTIME | os.TFD_TIMER_CANCEL_ON_SET)
4410
4411 # Any of 'initial' and 'interval' is negative value.
4412 for initial, interval in ( (-1, 0), (1, -1), (-1, -1), (-0.1, 0), (1, -0.1), (-0.1, -0.1)):
4413 for flags in test_flags:
4414 with self.subTest(flags=flags, initial=initial, interval=interval):
4415 with self.assertRaises(OSError) as context:
4416 os.timerfd_settime(fd, flags=flags, initial=initial, interval=interval)
4417 self.assertEqual(context.exception.errno, errno.EINVAL)
4418
4419 with self.assertRaises(OSError) as context:
4420 initial_ns = int( one_sec_in_nsec * initial )
4421 interval_ns = int( one_sec_in_nsec * interval )
4422 os.timerfd_settime_ns(fd, flags=flags, initial=initial_ns, interval=interval_ns)
4423 self.assertEqual(context.exception.errno, errno.EINVAL)
4424
4425 def test_timerfd_interval(self):
4426 fd = self.timerfd_create(time.CLOCK_REALTIME)
4427
4428 # 1 second
4429 initial_expiration = 1
4430 # 0.5 second
4431 interval = 0.5
4432
4433 os.timerfd_settime(fd, initial=initial_expiration, interval=interval)
4434
4435 # timerfd_gettime
4436 next_expiration, interval2 = os.timerfd_gettime(fd)
4437 self.assertAlmostEqual(interval2, interval, places=self.CLOCK_RES_PLACES)
4438 self.assertAlmostEqual(next_expiration, initial_expiration, places=self.CLOCK_RES_PLACES)
4439
4440 count = 3
4441 t = time.perf_counter()
4442 for _ in range(count):
4443 self.assertEqual(self.read_count_signaled(fd), 1)
4444 t = time.perf_counter() - t
4445
4446 total_time = initial_expiration + interval * (count - 1)
4447 self.assertGreater(t, total_time - self.CLOCK_RES)
4448
4449 # wait 3.5 time of interval
4450 time.sleep( (count+0.5) * interval)
4451 self.assertEqual(self.read_count_signaled(fd), count)
4452
4453 def test_timerfd_TFD_TIMER_ABSTIME(self):
4454 fd = self.timerfd_create(time.CLOCK_REALTIME)
4455
4456 now = time.clock_gettime(time.CLOCK_REALTIME)
4457
4458 # 1 second later from now.
4459 offset = 1
4460 initial_expiration = now + offset
4461 # not interval timer
4462 interval = 0
4463
4464 os.timerfd_settime(fd, flags=os.TFD_TIMER_ABSTIME, initial=initial_expiration, interval=interval)
4465
4466 # timerfd_gettime
4467 # Note: timerfd_gettime returns relative values even if TFD_TIMER_ABSTIME is specified.
4468 next_expiration, interval2 = os.timerfd_gettime(fd)
4469 self.assertAlmostEqual(interval2, interval, places=self.CLOCK_RES_PLACES)
4470 self.assertAlmostEqual(next_expiration, offset, places=self.CLOCK_RES_PLACES)
4471
4472 t = time.perf_counter()
4473 count_signaled = self.read_count_signaled(fd)
4474 t = time.perf_counter() - t
4475 self.assertEqual(count_signaled, 1)
4476
4477 self.assertGreater(t, offset - self.CLOCK_RES)
4478
4479 def test_timerfd_select(self):
4480 fd = self.timerfd_create(time.CLOCK_REALTIME, flags=os.TFD_NONBLOCK)
4481
4482 rfd, wfd, xfd = select.select([fd], [fd], [fd], 0)
4483 self.assertEqual((rfd, wfd, xfd), ([], [], []))
4484
4485 # 0.25 second
4486 initial_expiration = 0.25
4487 # every 0.125 second
4488 interval = 0.125
4489
4490 os.timerfd_settime(fd, initial=initial_expiration, interval=interval)
4491
4492 count = 3
4493 t = time.perf_counter()
4494 for _ in range(count):
4495 rfd, wfd, xfd = select.select([fd], [fd], [fd], initial_expiration + interval)
4496 self.assertEqual((rfd, wfd, xfd), ([fd], [], []))
4497 self.assertEqual(self.read_count_signaled(fd), 1)
4498 t = time.perf_counter() - t
4499
4500 total_time = initial_expiration + interval * (count - 1)
4501 self.assertGreater(t, total_time - self.CLOCK_RES)
4502
4503 def check_timerfd_poll(self, nanoseconds):
4504 fd = self.timerfd_create(time.CLOCK_REALTIME, flags=os.TFD_NONBLOCK)
4505
4506 selector = selectors.DefaultSelector()
4507 selector.register(fd, selectors.EVENT_READ)
4508 self.addCleanup(selector.close)
4509
4510 sec_to_nsec = 10 ** 9
4511 # 0.25 second
4512 initial_expiration_ns = sec_to_nsec // 4
4513 # every 0.125 second
4514 interval_ns = sec_to_nsec // 8
4515
4516 if nanoseconds:
4517 os.timerfd_settime_ns(fd,
4518 initial=initial_expiration_ns,
4519 interval=interval_ns)
4520 else:
4521 os.timerfd_settime(fd,
4522 initial=initial_expiration_ns / sec_to_nsec,
4523 interval=interval_ns / sec_to_nsec)
4524
4525 count = 3
4526 if nanoseconds:
4527 t = time.perf_counter_ns()
4528 else:
4529 t = time.perf_counter()
4530 for i in range(count):
4531 timeout_margin_ns = interval_ns
4532 if i == 0:
4533 timeout_ns = initial_expiration_ns + interval_ns + timeout_margin_ns
4534 else:
4535 timeout_ns = interval_ns + timeout_margin_ns
4536
4537 ready = selector.select(timeout_ns / sec_to_nsec)
4538 self.assertEqual(len(ready), 1, ready)
4539 event = ready[0][1]
4540 self.assertEqual(event, selectors.EVENT_READ)
4541
4542 self.assertEqual(self.read_count_signaled(fd), 1)
4543
4544 total_time = initial_expiration_ns + interval_ns * (count - 1)
4545 if nanoseconds:
4546 dt = time.perf_counter_ns() - t
4547 self.assertGreater(dt, total_time - self.CLOCK_RES_NS)
4548 else:
4549 dt = time.perf_counter() - t
4550 self.assertGreater(dt, total_time / sec_to_nsec - self.CLOCK_RES)
4551 selector.unregister(fd)
4552
4553 def test_timerfd_poll(self):
4554 self.check_timerfd_poll(False)
4555
4556 def test_timerfd_ns_poll(self):
4557 self.check_timerfd_poll(True)
4558
4559 def test_timerfd_ns_initval(self):
4560 one_sec_in_nsec = 10**9
4561 limit_error = one_sec_in_nsec // 10**3
4562 fd = self.timerfd_create(time.CLOCK_REALTIME)
4563
4564 # 1st call
4565 initial_expiration_ns = 0
4566 interval_ns = one_sec_in_nsec // 1000
4567 next_expiration_ns, interval_ns2 = os.timerfd_settime_ns(fd, initial=initial_expiration_ns, interval=interval_ns)
4568 self.assertEqual(interval_ns2, 0)
4569 self.assertEqual(next_expiration_ns, 0)
4570
4571 # 2nd call
4572 next_expiration_ns, interval_ns2 = os.timerfd_settime_ns(fd, initial=initial_expiration_ns, interval=interval_ns)
4573 self.assertEqual(interval_ns2, interval_ns)
4574 self.assertEqual(next_expiration_ns, initial_expiration_ns)
4575
4576 # timerfd_gettime
4577 next_expiration_ns, interval_ns2 = os.timerfd_gettime_ns(fd)
4578 self.assertEqual(interval_ns2, interval_ns)
4579 self.assertLessEqual(next_expiration_ns, initial_expiration_ns)
4580
4581 self.assertAlmostEqual(next_expiration_ns, initial_expiration_ns, delta=limit_error)
4582
4583 def test_timerfd_ns_interval(self):
4584 one_sec_in_nsec = 10**9
4585 limit_error = one_sec_in_nsec // 10**3
4586 fd = self.timerfd_create(time.CLOCK_REALTIME)
4587
4588 # 1 second
4589 initial_expiration_ns = one_sec_in_nsec
4590 # every 0.5 second
4591 interval_ns = one_sec_in_nsec // 2
4592
4593 os.timerfd_settime_ns(fd, initial=initial_expiration_ns, interval=interval_ns)
4594
4595 # timerfd_gettime
4596 next_expiration_ns, interval_ns2 = os.timerfd_gettime_ns(fd)
4597 self.assertEqual(interval_ns2, interval_ns)
4598 self.assertLessEqual(next_expiration_ns, initial_expiration_ns)
4599
4600 count = 3
4601 t = time.perf_counter_ns()
4602 for _ in range(count):
4603 self.assertEqual(self.read_count_signaled(fd), 1)
4604 t = time.perf_counter_ns() - t
4605
4606 total_time_ns = initial_expiration_ns + interval_ns * (count - 1)
4607 self.assertGreater(t, total_time_ns - self.CLOCK_RES_NS)
4608
4609 # wait 3.5 time of interval
4610 time.sleep( (count+0.5) * interval_ns / one_sec_in_nsec)
4611 self.assertEqual(self.read_count_signaled(fd), count)
4612
4613
4614 def test_timerfd_ns_TFD_TIMER_ABSTIME(self):
4615 one_sec_in_nsec = 10**9
4616 limit_error = one_sec_in_nsec // 10**3
4617 fd = self.timerfd_create(time.CLOCK_REALTIME)
4618
4619 now_ns = time.clock_gettime_ns(time.CLOCK_REALTIME)
4620
4621 # 1 second later from now.
4622 offset_ns = one_sec_in_nsec
4623 initial_expiration_ns = now_ns + offset_ns
4624 # not interval timer
4625 interval_ns = 0
4626
4627 os.timerfd_settime_ns(fd, flags=os.TFD_TIMER_ABSTIME, initial=initial_expiration_ns, interval=interval_ns)
4628
4629 # timerfd_gettime
4630 # Note: timerfd_gettime returns relative values even if TFD_TIMER_ABSTIME is specified.
4631 next_expiration_ns, interval_ns2 = os.timerfd_gettime_ns(fd)
4632 self.assertLess(abs(interval_ns2 - interval_ns), limit_error)
4633 self.assertLess(abs(next_expiration_ns - offset_ns), limit_error)
4634
4635 t = time.perf_counter_ns()
4636 count_signaled = self.read_count_signaled(fd)
4637 t = time.perf_counter_ns() - t
4638 self.assertEqual(count_signaled, 1)
4639
4640 self.assertGreater(t, offset_ns - self.CLOCK_RES_NS)
4641
4642 def test_timerfd_ns_select(self):
4643 one_sec_in_nsec = 10**9
4644
4645 fd = self.timerfd_create(time.CLOCK_REALTIME, flags=os.TFD_NONBLOCK)
4646
4647 rfd, wfd, xfd = select.select([fd], [fd], [fd], 0)
4648 self.assertEqual((rfd, wfd, xfd), ([], [], []))
4649
4650 # 0.25 second
4651 initial_expiration_ns = one_sec_in_nsec // 4
4652 # every 0.125 second
4653 interval_ns = one_sec_in_nsec // 8
4654
4655 os.timerfd_settime_ns(fd, initial=initial_expiration_ns, interval=interval_ns)
4656
4657 count = 3
4658 t = time.perf_counter_ns()
4659 for _ in range(count):
4660 rfd, wfd, xfd = select.select([fd], [fd], [fd], (initial_expiration_ns + interval_ns) / 1e9 )
4661 self.assertEqual((rfd, wfd, xfd), ([fd], [], []))
4662 self.assertEqual(self.read_count_signaled(fd), 1)
4663 t = time.perf_counter_ns() - t
4664
4665 total_time_ns = initial_expiration_ns + interval_ns * (count - 1)
4666 self.assertGreater(t, total_time_ns - self.CLOCK_RES_NS)
4667
4668 class OSErrorTests(unittest.TestCase):
4669 def setUp(self):
4670 class Str(str):
4671 pass
4672
4673 self.bytes_filenames = []
4674 self.unicode_filenames = []
4675 if os_helper.TESTFN_UNENCODABLE is not None:
4676 decoded = os_helper.TESTFN_UNENCODABLE
4677 else:
4678 decoded = os_helper.TESTFN
4679 self.unicode_filenames.append(decoded)
4680 self.unicode_filenames.append(Str(decoded))
4681 if os_helper.TESTFN_UNDECODABLE is not None:
4682 encoded = os_helper.TESTFN_UNDECODABLE
4683 else:
4684 encoded = os.fsencode(os_helper.TESTFN)
4685 self.bytes_filenames.append(encoded)
4686
4687 self.filenames = self.bytes_filenames + self.unicode_filenames
4688
4689 def test_oserror_filename(self):
4690 funcs = [
4691 (self.filenames, os.chdir,),
4692 (self.filenames, os.lstat,),
4693 (self.filenames, os.open, os.O_RDONLY),
4694 (self.filenames, os.rmdir,),
4695 (self.filenames, os.stat,),
4696 (self.filenames, os.unlink,),
4697 (self.filenames, os.listdir,),
4698 (self.filenames, os.rename, "dst"),
4699 (self.filenames, os.replace, "dst"),
4700 ]
4701 if os_helper.can_chmod():
4702 funcs.append((self.filenames, os.chmod, 0o777))
4703 if hasattr(os, "chown"):
4704 funcs.append((self.filenames, os.chown, 0, 0))
4705 if hasattr(os, "lchown"):
4706 funcs.append((self.filenames, os.lchown, 0, 0))
4707 if hasattr(os, "truncate"):
4708 funcs.append((self.filenames, os.truncate, 0))
4709 if hasattr(os, "chflags"):
4710 funcs.append((self.filenames, os.chflags, 0))
4711 if hasattr(os, "lchflags"):
4712 funcs.append((self.filenames, os.lchflags, 0))
4713 if hasattr(os, "chroot"):
4714 funcs.append((self.filenames, os.chroot,))
4715 if hasattr(os, "link"):
4716 funcs.append((self.filenames, os.link, "dst"))
4717 if hasattr(os, "listxattr"):
4718 funcs.extend((
4719 (self.filenames, os.listxattr,),
4720 (self.filenames, os.getxattr, "user.test"),
4721 (self.filenames, os.setxattr, "user.test", b'user'),
4722 (self.filenames, os.removexattr, "user.test"),
4723 ))
4724 if hasattr(os, "lchmod"):
4725 funcs.append((self.filenames, os.lchmod, 0o777))
4726 if hasattr(os, "readlink"):
4727 funcs.append((self.filenames, os.readlink,))
4728
4729 for filenames, func, *func_args in funcs:
4730 for name in filenames:
4731 try:
4732 func(name, *func_args)
4733 except OSError as err:
4734 self.assertIs(err.filename, name, str(func))
4735 except UnicodeDecodeError:
4736 pass
4737 else:
4738 self.fail(f"No exception thrown by {func}")
4739
4740 class CPUCountTests(unittest.TestCase):
4741 def check_cpu_count(self, cpus):
4742 if cpus is None:
4743 self.skipTest("Could not determine the number of CPUs")
4744
4745 self.assertIsInstance(cpus, int)
4746 self.assertGreater(cpus, 0)
4747
4748 def test_cpu_count(self):
4749 cpus = os.cpu_count()
4750 self.check_cpu_count(cpus)
4751
4752 def test_process_cpu_count(self):
4753 cpus = os.process_cpu_count()
4754 self.assertLessEqual(cpus, os.cpu_count())
4755 self.check_cpu_count(cpus)
4756
4757 @unittest.skipUnless(hasattr(os, 'sched_setaffinity'),
4758 "don't have sched affinity support")
4759 def test_process_cpu_count_affinity(self):
4760 affinity1 = os.process_cpu_count()
4761 if affinity1 is None:
4762 self.skipTest("Could not determine the number of CPUs")
4763
4764 # Disable one CPU
4765 mask = os.sched_getaffinity(0)
4766 if len(mask) <= 1:
4767 self.skipTest(f"sched_getaffinity() returns less than "
4768 f"2 CPUs: {sorted(mask)}")
4769 self.addCleanup(os.sched_setaffinity, 0, list(mask))
4770 mask.pop()
4771 os.sched_setaffinity(0, mask)
4772
4773 # test process_cpu_count()
4774 affinity2 = os.process_cpu_count()
4775 self.assertEqual(affinity2, affinity1 - 1)
4776
4777
4778 # FD inheritance check is only useful for systems with process support.
4779 @support.requires_subprocess()
4780 class FDInheritanceTests(unittest.TestCase):
4781 def test_get_set_inheritable(self):
4782 fd = os.open(__file__, os.O_RDONLY)
4783 self.addCleanup(os.close, fd)
4784 self.assertEqual(os.get_inheritable(fd), False)
4785
4786 os.set_inheritable(fd, True)
4787 self.assertEqual(os.get_inheritable(fd), True)
4788
4789 @unittest.skipIf(fcntl is None, "need fcntl")
4790 def test_get_inheritable_cloexec(self):
4791 fd = os.open(__file__, os.O_RDONLY)
4792 self.addCleanup(os.close, fd)
4793 self.assertEqual(os.get_inheritable(fd), False)
4794
4795 # clear FD_CLOEXEC flag
4796 flags = fcntl.fcntl(fd, fcntl.F_GETFD)
4797 flags &= ~fcntl.FD_CLOEXEC
4798 fcntl.fcntl(fd, fcntl.F_SETFD, flags)
4799
4800 self.assertEqual(os.get_inheritable(fd), True)
4801
4802 @unittest.skipIf(fcntl is None, "need fcntl")
4803 def test_set_inheritable_cloexec(self):
4804 fd = os.open(__file__, os.O_RDONLY)
4805 self.addCleanup(os.close, fd)
4806 self.assertEqual(fcntl.fcntl(fd, fcntl.F_GETFD) & fcntl.FD_CLOEXEC,
4807 fcntl.FD_CLOEXEC)
4808
4809 os.set_inheritable(fd, True)
4810 self.assertEqual(fcntl.fcntl(fd, fcntl.F_GETFD) & fcntl.FD_CLOEXEC,
4811 0)
4812
4813 @unittest.skipUnless(hasattr(os, 'O_PATH'), "need os.O_PATH")
4814 def test_get_set_inheritable_o_path(self):
4815 fd = os.open(__file__, os.O_PATH)
4816 self.addCleanup(os.close, fd)
4817 self.assertEqual(os.get_inheritable(fd), False)
4818
4819 os.set_inheritable(fd, True)
4820 self.assertEqual(os.get_inheritable(fd), True)
4821
4822 os.set_inheritable(fd, False)
4823 self.assertEqual(os.get_inheritable(fd), False)
4824
4825 def test_get_set_inheritable_badf(self):
4826 fd = os_helper.make_bad_fd()
4827
4828 with self.assertRaises(OSError) as ctx:
4829 os.get_inheritable(fd)
4830 self.assertEqual(ctx.exception.errno, errno.EBADF)
4831
4832 with self.assertRaises(OSError) as ctx:
4833 os.set_inheritable(fd, True)
4834 self.assertEqual(ctx.exception.errno, errno.EBADF)
4835
4836 with self.assertRaises(OSError) as ctx:
4837 os.set_inheritable(fd, False)
4838 self.assertEqual(ctx.exception.errno, errno.EBADF)
4839
4840 def test_open(self):
4841 fd = os.open(__file__, os.O_RDONLY)
4842 self.addCleanup(os.close, fd)
4843 self.assertEqual(os.get_inheritable(fd), False)
4844
4845 @unittest.skipUnless(hasattr(os, 'pipe'), "need os.pipe()")
4846 def test_pipe(self):
4847 rfd, wfd = os.pipe()
4848 self.addCleanup(os.close, rfd)
4849 self.addCleanup(os.close, wfd)
4850 self.assertEqual(os.get_inheritable(rfd), False)
4851 self.assertEqual(os.get_inheritable(wfd), False)
4852
4853 def test_dup(self):
4854 fd1 = os.open(__file__, os.O_RDONLY)
4855 self.addCleanup(os.close, fd1)
4856
4857 fd2 = os.dup(fd1)
4858 self.addCleanup(os.close, fd2)
4859 self.assertEqual(os.get_inheritable(fd2), False)
4860
4861 def test_dup_standard_stream(self):
4862 fd = os.dup(1)
4863 self.addCleanup(os.close, fd)
4864 self.assertGreater(fd, 0)
4865
4866 @unittest.skipUnless(sys.platform == 'win32', 'win32-specific test')
4867 def test_dup_nul(self):
4868 # os.dup() was creating inheritable fds for character files.
4869 fd1 = os.open('NUL', os.O_RDONLY)
4870 self.addCleanup(os.close, fd1)
4871 fd2 = os.dup(fd1)
4872 self.addCleanup(os.close, fd2)
4873 self.assertFalse(os.get_inheritable(fd2))
4874
4875 @unittest.skipUnless(hasattr(os, 'dup2'), "need os.dup2()")
4876 def test_dup2(self):
4877 fd = os.open(__file__, os.O_RDONLY)
4878 self.addCleanup(os.close, fd)
4879
4880 # inheritable by default
4881 fd2 = os.open(__file__, os.O_RDONLY)
4882 self.addCleanup(os.close, fd2)
4883 self.assertEqual(os.dup2(fd, fd2), fd2)
4884 self.assertTrue(os.get_inheritable(fd2))
4885
4886 # force non-inheritable
4887 fd3 = os.open(__file__, os.O_RDONLY)
4888 self.addCleanup(os.close, fd3)
4889 self.assertEqual(os.dup2(fd, fd3, inheritable=False), fd3)
4890 self.assertFalse(os.get_inheritable(fd3))
4891
4892 @unittest.skipUnless(hasattr(os, 'openpty'), "need os.openpty()")
4893 class PseudoterminalTests(unittest.TestCase):
4894 def open_pty(self):
4895 """Open a pty fd-pair, and schedule cleanup for it"""
4896 main_fd, second_fd = os.openpty()
4897 self.addCleanup(os.close, main_fd)
4898 self.addCleanup(os.close, second_fd)
4899 return main_fd, second_fd
4900
4901 def test_openpty(self):
4902 main_fd, second_fd = self.open_pty()
4903 self.assertEqual(os.get_inheritable(main_fd), False)
4904 self.assertEqual(os.get_inheritable(second_fd), False)
4905
4906 @unittest.skipUnless(hasattr(os, 'ptsname'), "need os.ptsname()")
4907 @unittest.skipUnless(hasattr(os, 'O_RDWR'), "need os.O_RDWR")
4908 @unittest.skipUnless(hasattr(os, 'O_NOCTTY'), "need os.O_NOCTTY")
4909 def test_open_via_ptsname(self):
4910 main_fd, second_fd = self.open_pty()
4911 second_path = os.ptsname(main_fd)
4912 reopened_second_fd = os.open(second_path, os.O_RDWR|os.O_NOCTTY)
4913 self.addCleanup(os.close, reopened_second_fd)
4914 os.write(reopened_second_fd, b'foo')
4915 self.assertEqual(os.read(main_fd, 3), b'foo')
4916
4917 @unittest.skipUnless(hasattr(os, 'posix_openpt'), "need os.posix_openpt()")
4918 @unittest.skipUnless(hasattr(os, 'grantpt'), "need os.grantpt()")
4919 @unittest.skipUnless(hasattr(os, 'unlockpt'), "need os.unlockpt()")
4920 @unittest.skipUnless(hasattr(os, 'ptsname'), "need os.ptsname()")
4921 @unittest.skipUnless(hasattr(os, 'O_RDWR'), "need os.O_RDWR")
4922 @unittest.skipUnless(hasattr(os, 'O_NOCTTY'), "need os.O_NOCTTY")
4923 def test_posix_pty_functions(self):
4924 mother_fd = os.posix_openpt(os.O_RDWR|os.O_NOCTTY)
4925 self.addCleanup(os.close, mother_fd)
4926 os.grantpt(mother_fd)
4927 os.unlockpt(mother_fd)
4928 son_path = os.ptsname(mother_fd)
4929 son_fd = os.open(son_path, os.O_RDWR|os.O_NOCTTY)
4930 self.addCleanup(os.close, son_fd)
4931 self.assertEqual(os.ptsname(mother_fd), os.ttyname(son_fd))
4932
4933 @unittest.skipUnless(hasattr(os, 'spawnl'), "need os.spawnl()")
4934 @support.requires_subprocess()
4935 def test_pipe_spawnl(self):
4936 # gh-77046: On Windows, os.pipe() file descriptors must be created with
4937 # _O_NOINHERIT to make them non-inheritable. UCRT has no public API to
4938 # get (_osfile(fd) & _O_NOINHERIT), so use a functional test.
4939 #
4940 # Make sure that fd is not inherited by a child process created by
4941 # os.spawnl(): get_osfhandle() and dup() must fail with EBADF.
4942
4943 fd, fd2 = os.pipe()
4944 self.addCleanup(os.close, fd)
4945 self.addCleanup(os.close, fd2)
4946
4947 code = textwrap.dedent(f"""
4948 import errno
4949 import os
4950 import test.support
4951 try:
4952 import msvcrt
4953 except ImportError:
4954 msvcrt = None
4955
4956 fd = {fd}
4957
4958 with test.support.SuppressCrashReport():
4959 if msvcrt is not None:
4960 try:
4961 handle = msvcrt.get_osfhandle(fd)
4962 except OSError as exc:
4963 if exc.errno != errno.EBADF:
4964 raise
4965 # get_osfhandle(fd) failed with EBADF as expected
4966 else:
4967 raise Exception("get_osfhandle() must fail")
4968
4969 try:
4970 fd3 = os.dup(fd)
4971 except OSError as exc:
4972 if exc.errno != errno.EBADF:
4973 raise
4974 # os.dup(fd) failed with EBADF as expected
4975 else:
4976 os.close(fd3)
4977 raise Exception("dup must fail")
4978 """)
4979
4980 filename = os_helper.TESTFN
4981 self.addCleanup(os_helper.unlink, os_helper.TESTFN)
4982 with open(filename, "w") as fp:
4983 print(code, file=fp, end="")
4984
4985 executable = sys.executable
4986 cmd = [executable, filename]
4987 if os.name == "nt" and " " in cmd[0]:
4988 cmd[0] = f'"{cmd[0]}"'
4989 exitcode = os.spawnl(os.P_WAIT, executable, *cmd)
4990 self.assertEqual(exitcode, 0)
4991
4992
4993 class PathTConverterTests(unittest.TestCase):
4994 # tuples of (function name, allows fd arguments, additional arguments to
4995 # function, cleanup function)
4996 functions = [
4997 ('stat', True, (), None),
4998 ('lstat', False, (), None),
4999 ('access', False, (os.F_OK,), None),
5000 ('chflags', False, (0,), None),
5001 ('lchflags', False, (0,), None),
5002 ('open', False, (os.O_RDONLY,), getattr(os, 'close', None)),
5003 ]
5004
5005 def test_path_t_converter(self):
5006 str_filename = os_helper.TESTFN
5007 if os.name == 'nt':
5008 bytes_fspath = bytes_filename = None
5009 else:
5010 bytes_filename = os.fsencode(os_helper.TESTFN)
5011 bytes_fspath = FakePath(bytes_filename)
5012 fd = os.open(FakePath(str_filename), os.O_WRONLY|os.O_CREAT)
5013 self.addCleanup(os_helper.unlink, os_helper.TESTFN)
5014 self.addCleanup(os.close, fd)
5015
5016 int_fspath = FakePath(fd)
5017 str_fspath = FakePath(str_filename)
5018
5019 for name, allow_fd, extra_args, cleanup_fn in self.functions:
5020 with self.subTest(name=name):
5021 try:
5022 fn = getattr(os, name)
5023 except AttributeError:
5024 continue
5025
5026 for path in (str_filename, bytes_filename, str_fspath,
5027 bytes_fspath):
5028 if path is None:
5029 continue
5030 with self.subTest(name=name, path=path):
5031 result = fn(path, *extra_args)
5032 if cleanup_fn is not None:
5033 cleanup_fn(result)
5034
5035 with self.assertRaisesRegex(
5036 TypeError, 'to return str or bytes'):
5037 fn(int_fspath, *extra_args)
5038
5039 if allow_fd:
5040 result = fn(fd, *extra_args) # should not fail
5041 if cleanup_fn is not None:
5042 cleanup_fn(result)
5043 else:
5044 with self.assertRaisesRegex(
5045 TypeError,
5046 'os.PathLike'):
5047 fn(fd, *extra_args)
5048
5049 def test_path_t_converter_and_custom_class(self):
5050 msg = r'__fspath__\(\) to return str or bytes, not %s'
5051 with self.assertRaisesRegex(TypeError, msg % r'int'):
5052 os.stat(FakePath(2))
5053 with self.assertRaisesRegex(TypeError, msg % r'float'):
5054 os.stat(FakePath(2.34))
5055 with self.assertRaisesRegex(TypeError, msg % r'object'):
5056 os.stat(FakePath(object()))
5057
5058
5059 @unittest.skipUnless(hasattr(os, 'get_blocking'),
5060 'needs os.get_blocking() and os.set_blocking()')
5061 @unittest.skipIf(support.is_emscripten, "Cannot unset blocking flag")
5062 @unittest.skipIf(sys.platform == 'win32', 'Windows only supports blocking on pipes')
5063 class BlockingTests(unittest.TestCase):
5064 def test_blocking(self):
5065 fd = os.open(__file__, os.O_RDONLY)
5066 self.addCleanup(os.close, fd)
5067 self.assertEqual(os.get_blocking(fd), True)
5068
5069 os.set_blocking(fd, False)
5070 self.assertEqual(os.get_blocking(fd), False)
5071
5072 os.set_blocking(fd, True)
5073 self.assertEqual(os.get_blocking(fd), True)
5074
5075
5076
5077 class ExportsTests(unittest.TestCase):
5078 def test_os_all(self):
5079 self.assertIn('open', os.__all__)
5080 self.assertIn('walk', os.__all__)
5081
5082
5083 class TestDirEntry(unittest.TestCase):
5084 def setUp(self):
5085 self.path = os.path.realpath(os_helper.TESTFN)
5086 self.addCleanup(os_helper.rmtree, self.path)
5087 os.mkdir(self.path)
5088
5089 def test_uninstantiable(self):
5090 self.assertRaises(TypeError, os.DirEntry)
5091
5092 def test_unpickable(self):
5093 filename = create_file(os.path.join(self.path, "file.txt"), b'python')
5094 entry = [entry for entry in os.scandir(self.path)].pop()
5095 self.assertIsInstance(entry, os.DirEntry)
5096 self.assertEqual(entry.name, "file.txt")
5097 import pickle
5098 self.assertRaises(TypeError, pickle.dumps, entry, filename)
5099
5100
5101 class TestScandir(unittest.TestCase):
5102 check_no_resource_warning = warnings_helper.check_no_resource_warning
5103
5104 def setUp(self):
5105 self.path = os.path.realpath(os_helper.TESTFN)
5106 self.bytes_path = os.fsencode(self.path)
5107 self.addCleanup(os_helper.rmtree, self.path)
5108 os.mkdir(self.path)
5109
5110 def create_file(self, name="file.txt"):
5111 path = self.bytes_path if isinstance(name, bytes) else self.path
5112 filename = os.path.join(path, name)
5113 create_file(filename, b'python')
5114 return filename
5115
5116 def get_entries(self, names):
5117 entries = dict((entry.name, entry)
5118 for entry in os.scandir(self.path))
5119 self.assertEqual(sorted(entries.keys()), names)
5120 return entries
5121
5122 def assert_stat_equal(self, stat1, stat2, skip_fields):
5123 if skip_fields:
5124 for attr in dir(stat1):
5125 if not attr.startswith("st_"):
5126 continue
5127 if attr in ("st_dev", "st_ino", "st_nlink", "st_ctime",
5128 "st_ctime_ns"):
5129 continue
5130 self.assertEqual(getattr(stat1, attr),
5131 getattr(stat2, attr),
5132 (stat1, stat2, attr))
5133 else:
5134 self.assertEqual(stat1, stat2)
5135
5136 def test_uninstantiable(self):
5137 scandir_iter = os.scandir(self.path)
5138 self.assertRaises(TypeError, type(scandir_iter))
5139 scandir_iter.close()
5140
5141 def test_unpickable(self):
5142 filename = self.create_file("file.txt")
5143 scandir_iter = os.scandir(self.path)
5144 import pickle
5145 self.assertRaises(TypeError, pickle.dumps, scandir_iter, filename)
5146 scandir_iter.close()
5147
5148 def check_entry(self, entry, name, is_dir, is_file, is_symlink):
5149 self.assertIsInstance(entry, os.DirEntry)
5150 self.assertEqual(entry.name, name)
5151 self.assertEqual(entry.path, os.path.join(self.path, name))
5152 self.assertEqual(entry.inode(),
5153 os.stat(entry.path, follow_symlinks=False).st_ino)
5154
5155 entry_stat = os.stat(entry.path)
5156 self.assertEqual(entry.is_dir(),
5157 stat.S_ISDIR(entry_stat.st_mode))
5158 self.assertEqual(entry.is_file(),
5159 stat.S_ISREG(entry_stat.st_mode))
5160 self.assertEqual(entry.is_symlink(),
5161 os.path.islink(entry.path))
5162
5163 entry_lstat = os.stat(entry.path, follow_symlinks=False)
5164 self.assertEqual(entry.is_dir(follow_symlinks=False),
5165 stat.S_ISDIR(entry_lstat.st_mode))
5166 self.assertEqual(entry.is_file(follow_symlinks=False),
5167 stat.S_ISREG(entry_lstat.st_mode))
5168
5169 self.assertEqual(entry.is_junction(), os.path.isjunction(entry.path))
5170
5171 self.assert_stat_equal(entry.stat(),
5172 entry_stat,
5173 os.name == 'nt' and not is_symlink)
5174 self.assert_stat_equal(entry.stat(follow_symlinks=False),
5175 entry_lstat,
5176 os.name == 'nt')
5177
5178 def test_attributes(self):
5179 link = os_helper.can_hardlink()
5180 symlink = os_helper.can_symlink()
5181
5182 dirname = os.path.join(self.path, "dir")
5183 os.mkdir(dirname)
5184 filename = self.create_file("file.txt")
5185 if link:
5186 try:
5187 os.link(filename, os.path.join(self.path, "link_file.txt"))
5188 except PermissionError as e:
5189 self.skipTest('os.link(): %s' % e)
5190 if symlink:
5191 os.symlink(dirname, os.path.join(self.path, "symlink_dir"),
5192 target_is_directory=True)
5193 os.symlink(filename, os.path.join(self.path, "symlink_file.txt"))
5194
5195 names = ['dir', 'file.txt']
5196 if link:
5197 names.append('link_file.txt')
5198 if symlink:
5199 names.extend(('symlink_dir', 'symlink_file.txt'))
5200 entries = self.get_entries(names)
5201
5202 entry = entries['dir']
5203 self.check_entry(entry, 'dir', True, False, False)
5204
5205 entry = entries['file.txt']
5206 self.check_entry(entry, 'file.txt', False, True, False)
5207
5208 if link:
5209 entry = entries['link_file.txt']
5210 self.check_entry(entry, 'link_file.txt', False, True, False)
5211
5212 if symlink:
5213 entry = entries['symlink_dir']
5214 self.check_entry(entry, 'symlink_dir', True, False, True)
5215
5216 entry = entries['symlink_file.txt']
5217 self.check_entry(entry, 'symlink_file.txt', False, True, True)
5218
5219 @unittest.skipIf(sys.platform != 'win32', "Can only test junctions with creation on win32.")
5220 def test_attributes_junctions(self):
5221 dirname = os.path.join(self.path, "tgtdir")
5222 os.mkdir(dirname)
5223
5224 import _winapi
5225 try:
5226 _winapi.CreateJunction(dirname, os.path.join(self.path, "srcjunc"))
5227 except OSError:
5228 raise unittest.SkipTest('creating the test junction failed')
5229
5230 entries = self.get_entries(['srcjunc', 'tgtdir'])
5231 self.assertEqual(entries['srcjunc'].is_junction(), True)
5232 self.assertEqual(entries['tgtdir'].is_junction(), False)
5233
5234 def get_entry(self, name):
5235 path = self.bytes_path if isinstance(name, bytes) else self.path
5236 entries = list(os.scandir(path))
5237 self.assertEqual(len(entries), 1)
5238
5239 entry = entries[0]
5240 self.assertEqual(entry.name, name)
5241 return entry
5242
5243 def create_file_entry(self, name='file.txt'):
5244 filename = self.create_file(name=name)
5245 return self.get_entry(os.path.basename(filename))
5246
5247 def test_current_directory(self):
5248 filename = self.create_file()
5249 old_dir = os.getcwd()
5250 try:
5251 os.chdir(self.path)
5252
5253 # call scandir() without parameter: it must list the content
5254 # of the current directory
5255 entries = dict((entry.name, entry) for entry in os.scandir())
5256 self.assertEqual(sorted(entries.keys()),
5257 [os.path.basename(filename)])
5258 finally:
5259 os.chdir(old_dir)
5260
5261 def test_repr(self):
5262 entry = self.create_file_entry()
5263 self.assertEqual(repr(entry), "<DirEntry 'file.txt'>")
5264
5265 def test_fspath_protocol(self):
5266 entry = self.create_file_entry()
5267 self.assertEqual(os.fspath(entry), os.path.join(self.path, 'file.txt'))
5268
5269 def test_fspath_protocol_bytes(self):
5270 bytes_filename = os.fsencode('bytesfile.txt')
5271 bytes_entry = self.create_file_entry(name=bytes_filename)
5272 fspath = os.fspath(bytes_entry)
5273 self.assertIsInstance(fspath, bytes)
5274 self.assertEqual(fspath,
5275 os.path.join(os.fsencode(self.path),bytes_filename))
5276
5277 def test_removed_dir(self):
5278 path = os.path.join(self.path, 'dir')
5279
5280 os.mkdir(path)
5281 entry = self.get_entry('dir')
5282 os.rmdir(path)
5283
5284 # On POSIX, is_dir() result depends if scandir() filled d_type or not
5285 if os.name == 'nt':
5286 self.assertTrue(entry.is_dir())
5287 self.assertFalse(entry.is_file())
5288 self.assertFalse(entry.is_symlink())
5289 if os.name == 'nt':
5290 self.assertRaises(FileNotFoundError, entry.inode)
5291 # don't fail
5292 entry.stat()
5293 entry.stat(follow_symlinks=False)
5294 else:
5295 self.assertGreater(entry.inode(), 0)
5296 self.assertRaises(FileNotFoundError, entry.stat)
5297 self.assertRaises(FileNotFoundError, entry.stat, follow_symlinks=False)
5298
5299 def test_removed_file(self):
5300 entry = self.create_file_entry()
5301 os.unlink(entry.path)
5302
5303 self.assertFalse(entry.is_dir())
5304 # On POSIX, is_dir() result depends if scandir() filled d_type or not
5305 if os.name == 'nt':
5306 self.assertTrue(entry.is_file())
5307 self.assertFalse(entry.is_symlink())
5308 if os.name == 'nt':
5309 self.assertRaises(FileNotFoundError, entry.inode)
5310 # don't fail
5311 entry.stat()
5312 entry.stat(follow_symlinks=False)
5313 else:
5314 self.assertGreater(entry.inode(), 0)
5315 self.assertRaises(FileNotFoundError, entry.stat)
5316 self.assertRaises(FileNotFoundError, entry.stat, follow_symlinks=False)
5317
5318 def test_broken_symlink(self):
5319 if not os_helper.can_symlink():
5320 return self.skipTest('cannot create symbolic link')
5321
5322 filename = self.create_file("file.txt")
5323 os.symlink(filename,
5324 os.path.join(self.path, "symlink.txt"))
5325 entries = self.get_entries(['file.txt', 'symlink.txt'])
5326 entry = entries['symlink.txt']
5327 os.unlink(filename)
5328
5329 self.assertGreater(entry.inode(), 0)
5330 self.assertFalse(entry.is_dir())
5331 self.assertFalse(entry.is_file()) # broken symlink returns False
5332 self.assertFalse(entry.is_dir(follow_symlinks=False))
5333 self.assertFalse(entry.is_file(follow_symlinks=False))
5334 self.assertTrue(entry.is_symlink())
5335 self.assertRaises(FileNotFoundError, entry.stat)
5336 # don't fail
5337 entry.stat(follow_symlinks=False)
5338
5339 def test_bytes(self):
5340 self.create_file("file.txt")
5341
5342 path_bytes = os.fsencode(self.path)
5343 entries = list(os.scandir(path_bytes))
5344 self.assertEqual(len(entries), 1, entries)
5345 entry = entries[0]
5346
5347 self.assertEqual(entry.name, b'file.txt')
5348 self.assertEqual(entry.path,
5349 os.fsencode(os.path.join(self.path, 'file.txt')))
5350
5351 def test_bytes_like(self):
5352 self.create_file("file.txt")
5353
5354 for cls in bytearray, memoryview:
5355 path_bytes = cls(os.fsencode(self.path))
5356 with self.assertRaises(TypeError):
5357 os.scandir(path_bytes)
5358
5359 @unittest.skipUnless(os.listdir in os.supports_fd,
5360 'fd support for listdir required for this test.')
5361 def test_fd(self):
5362 self.assertIn(os.scandir, os.supports_fd)
5363 self.create_file('file.txt')
5364 expected_names = ['file.txt']
5365 if os_helper.can_symlink():
5366 os.symlink('file.txt', os.path.join(self.path, 'link'))
5367 expected_names.append('link')
5368
5369 with os_helper.open_dir_fd(self.path) as fd:
5370 with os.scandir(fd) as it:
5371 entries = list(it)
5372 names = [entry.name for entry in entries]
5373 self.assertEqual(sorted(names), expected_names)
5374 self.assertEqual(names, os.listdir(fd))
5375 for entry in entries:
5376 self.assertEqual(entry.path, entry.name)
5377 self.assertEqual(os.fspath(entry), entry.name)
5378 self.assertEqual(entry.is_symlink(), entry.name == 'link')
5379 if os.stat in os.supports_dir_fd:
5380 st = os.stat(entry.name, dir_fd=fd)
5381 self.assertEqual(entry.stat(), st)
5382 st = os.stat(entry.name, dir_fd=fd, follow_symlinks=False)
5383 self.assertEqual(entry.stat(follow_symlinks=False), st)
5384
5385 @unittest.skipIf(support.is_wasi, "WASI maps '' to cwd")
5386 def test_empty_path(self):
5387 self.assertRaises(FileNotFoundError, os.scandir, '')
5388
5389 def test_consume_iterator_twice(self):
5390 self.create_file("file.txt")
5391 iterator = os.scandir(self.path)
5392
5393 entries = list(iterator)
5394 self.assertEqual(len(entries), 1, entries)
5395
5396 # check than consuming the iterator twice doesn't raise exception
5397 entries2 = list(iterator)
5398 self.assertEqual(len(entries2), 0, entries2)
5399
5400 def test_bad_path_type(self):
5401 for obj in [1.234, {}, []]:
5402 self.assertRaises(TypeError, os.scandir, obj)
5403
5404 def test_close(self):
5405 self.create_file("file.txt")
5406 self.create_file("file2.txt")
5407 iterator = os.scandir(self.path)
5408 next(iterator)
5409 iterator.close()
5410 # multiple closes
5411 iterator.close()
5412 with self.check_no_resource_warning():
5413 del iterator
5414
5415 def test_context_manager(self):
5416 self.create_file("file.txt")
5417 self.create_file("file2.txt")
5418 with os.scandir(self.path) as iterator:
5419 next(iterator)
5420 with self.check_no_resource_warning():
5421 del iterator
5422
5423 def test_context_manager_close(self):
5424 self.create_file("file.txt")
5425 self.create_file("file2.txt")
5426 with os.scandir(self.path) as iterator:
5427 next(iterator)
5428 iterator.close()
5429
5430 def test_context_manager_exception(self):
5431 self.create_file("file.txt")
5432 self.create_file("file2.txt")
5433 with self.assertRaises(ZeroDivisionError):
5434 with os.scandir(self.path) as iterator:
5435 next(iterator)
5436 1/0
5437 with self.check_no_resource_warning():
5438 del iterator
5439
5440 def test_resource_warning(self):
5441 self.create_file("file.txt")
5442 self.create_file("file2.txt")
5443 iterator = os.scandir(self.path)
5444 next(iterator)
5445 with self.assertWarns(ResourceWarning):
5446 del iterator
5447 support.gc_collect()
5448 # exhausted iterator
5449 iterator = os.scandir(self.path)
5450 list(iterator)
5451 with self.check_no_resource_warning():
5452 del iterator
5453
5454
5455 class TestPEP519(unittest.TestCase):
5456
5457 # Abstracted so it can be overridden to test pure Python implementation
5458 # if a C version is provided.
5459 fspath = staticmethod(os.fspath)
5460
5461 def test_return_bytes(self):
5462 for b in b'hello', b'goodbye', b'some/path/and/file':
5463 self.assertEqual(b, self.fspath(b))
5464
5465 def test_return_string(self):
5466 for s in 'hello', 'goodbye', 'some/path/and/file':
5467 self.assertEqual(s, self.fspath(s))
5468
5469 def test_fsencode_fsdecode(self):
5470 for p in "path/like/object", b"path/like/object":
5471 pathlike = FakePath(p)
5472
5473 self.assertEqual(p, self.fspath(pathlike))
5474 self.assertEqual(b"path/like/object", os.fsencode(pathlike))
5475 self.assertEqual("path/like/object", os.fsdecode(pathlike))
5476
5477 def test_pathlike(self):
5478 self.assertEqual('#feelthegil', self.fspath(FakePath('#feelthegil')))
5479 self.assertIsSubclass(FakePath, os.PathLike)
5480 self.assertIsInstance(FakePath('x'), os.PathLike)
5481
5482 def test_garbage_in_exception_out(self):
5483 vapor = type('blah', (), {})
5484 for o in int, type, os, vapor():
5485 self.assertRaises(TypeError, self.fspath, o)
5486
5487 def test_argument_required(self):
5488 self.assertRaises(TypeError, self.fspath)
5489
5490 def test_bad_pathlike(self):
5491 # __fspath__ returns a value other than str or bytes.
5492 self.assertRaises(TypeError, self.fspath, FakePath(42))
5493 # __fspath__ attribute that is not callable.
5494 c = type('foo', (), {})
5495 c.__fspath__ = 1
5496 self.assertRaises(TypeError, self.fspath, c())
5497 # __fspath__ raises an exception.
5498 self.assertRaises(ZeroDivisionError, self.fspath,
5499 FakePath(ZeroDivisionError()))
5500
5501 def test_pathlike_subclasshook(self):
5502 # bpo-38878: subclasshook causes subclass checks
5503 # true on abstract implementation.
5504 class A(os.PathLike):
5505 pass
5506 self.assertNotIsSubclass(FakePath, A)
5507 self.assertIsSubclass(FakePath, os.PathLike)
5508
5509 def test_pathlike_class_getitem(self):
5510 self.assertIsInstance(os.PathLike[bytes], types.GenericAlias)
5511
5512 def test_pathlike_subclass_slots(self):
5513 class A(os.PathLike):
5514 __slots__ = ()
5515 def __fspath__(self):
5516 return ''
5517 self.assertNotHasAttr(A(), '__dict__')
5518
5519 def test_fspath_set_to_None(self):
5520 class Foo:
5521 __fspath__ = None
5522
5523 class Bar:
5524 def __fspath__(self):
5525 return 'bar'
5526
5527 class Baz(Bar):
5528 __fspath__ = None
5529
5530 good_error_msg = (
5531 r"expected str, bytes or os.PathLike object, not {}".format
5532 )
5533
5534 with self.assertRaisesRegex(TypeError, good_error_msg("Foo")):
5535 self.fspath(Foo())
5536
5537 self.assertEqual(self.fspath(Bar()), 'bar')
5538
5539 with self.assertRaisesRegex(TypeError, good_error_msg("Baz")):
5540 self.fspath(Baz())
5541
5542 with self.assertRaisesRegex(TypeError, good_error_msg("Foo")):
5543 open(Foo())
5544
5545 with self.assertRaisesRegex(TypeError, good_error_msg("Baz")):
5546 open(Baz())
5547
5548 other_good_error_msg = (
5549 r"should be string, bytes or os.PathLike, not {}".format
5550 )
5551
5552 with self.assertRaisesRegex(TypeError, other_good_error_msg("Foo")):
5553 os.rename(Foo(), "foooo")
5554
5555 with self.assertRaisesRegex(TypeError, other_good_error_msg("Baz")):
5556 os.rename(Baz(), "bazzz")
5557
5558 class TimesTests(unittest.TestCase):
5559 def test_times(self):
5560 times = os.times()
5561 self.assertIsInstance(times, os.times_result)
5562
5563 for field in ('user', 'system', 'children_user', 'children_system',
5564 'elapsed'):
5565 value = getattr(times, field)
5566 self.assertIsInstance(value, float)
5567
5568 if os.name == 'nt':
5569 self.assertEqual(times.children_user, 0)
5570 self.assertEqual(times.children_system, 0)
5571 self.assertEqual(times.elapsed, 0)
5572
5573
5574 @support.requires_fork()
5575 class ForkTests(unittest.TestCase):
5576 def test_fork(self):
5577 # bpo-42540: ensure os.fork() with non-default memory allocator does
5578 # not crash on exit.
5579 code = """if 1:
5580 import os
5581 from test import support
5582 pid = os.fork()
5583 if pid != 0:
5584 support.wait_process(pid, exitcode=0)
5585 """
5586 assert_python_ok("-c", code)
5587 if support.Py_GIL_DISABLED:
5588 assert_python_ok("-c", code, PYTHONMALLOC="mimalloc_debug")
5589 else:
5590 assert_python_ok("-c", code, PYTHONMALLOC="malloc_debug")
5591
5592 @unittest.skipUnless(sys.platform in ("linux", "android", "darwin"),
5593 "Only Linux and macOS detect this today.")
5594 @unittest.skipIf(_testcapi is None, "requires _testcapi")
5595 def test_fork_warns_when_non_python_thread_exists(self):
5596 code = """if 1:
5597 import os, threading, warnings
5598 from _testcapi import _spawn_pthread_waiter, _end_spawned_pthread
5599 _spawn_pthread_waiter()
5600 try:
5601 with warnings.catch_warnings(record=True) as ws:
5602 warnings.filterwarnings(
5603 "always", category=DeprecationWarning)
5604 if os.fork() == 0:
5605 assert not ws, f"unexpected warnings in child: {ws}"
5606 os._exit(0) # child
5607 else:
5608 assert ws[0].category == DeprecationWarning, ws[0]
5609 assert 'fork' in str(ws[0].message), ws[0]
5610 # Waiting allows an error in the child to hit stderr.
5611 exitcode = os.wait()[1]
5612 assert exitcode == 0, f"child exited {exitcode}"
5613 assert threading.active_count() == 1, threading.enumerate()
5614 finally:
5615 _end_spawned_pthread()
5616 """
5617 _, out, err = assert_python_ok("-c", code, PYTHONOPTIMIZE='0')
5618 self.assertEqual(err.decode("utf-8"), "")
5619 self.assertEqual(out.decode("utf-8"), "")
5620
5621 def test_fork_at_finalization(self):
5622 code = """if 1:
5623 import atexit
5624 import os
5625
5626 class AtFinalization:
5627 def __del__(self):
5628 print("OK")
5629 pid = os.fork()
5630 if pid != 0:
5631 print("shouldn't be printed")
5632 at_finalization = AtFinalization()
5633 """
5634 _, out, err = assert_python_ok("-c", code)
5635 self.assertEqual(b"OK\n", out)
5636 self.assertIn(b"can't fork at interpreter shutdown", err)
5637
5638
5639 # Only test if the C version is provided, otherwise TestPEP519 already tested
5640 # the pure Python implementation.
5641 if hasattr(os, "_fspath"):
5642 class TestPEP519PurePython(TestPEP519):
5643
5644 """Explicitly test the pure Python implementation of os.fspath()."""
5645
5646 fspath = staticmethod(os._fspath)
5647
5648
5649 if __name__ == "__main__":
5650 unittest.main()