/Lib/test/test_os.py
Python | 3191 lines | 2791 code | 223 blank | 177 comment | 217 complexity | 56001d5d1ca67eeff86a0af818a15624 MD5 | raw file
Possible License(s): Unlicense, 0BSD, BSD-3-Clause
Large files files are truncated, but you can click here to view the full file
- # As a test suite for the os module, this is woefully inadequate, but this
- # does add tests for a few functions which have been determined to be more
- # portable than they had been thought to be.
- import asynchat
- import asyncore
- import codecs
- import contextlib
- import decimal
- import errno
- import fractions
- import getpass
- import itertools
- import locale
- import mmap
- import os
- import pickle
- import re
- import shutil
- import signal
- import socket
- import stat
- import subprocess
- import sys
- import sysconfig
- import time
- import unittest
- import uuid
- import warnings
- from test import support
- try:
- import threading
- except ImportError:
- threading = None
- try:
- import resource
- except ImportError:
- resource = None
- try:
- import fcntl
- except ImportError:
- fcntl = None
- try:
- import _winapi
- except ImportError:
- _winapi = None
- try:
- import grp
- groups = [g.gr_gid for g in grp.getgrall() if getpass.getuser() in g.gr_mem]
- if hasattr(os, 'getgid'):
- process_gid = os.getgid()
- if process_gid not in groups:
- groups.append(process_gid)
- except ImportError:
- groups = []
- try:
- import pwd
- all_users = [u.pw_uid for u in pwd.getpwall()]
- except ImportError:
- all_users = []
- try:
- from _testcapi import INT_MAX, PY_SSIZE_T_MAX
- except ImportError:
- INT_MAX = PY_SSIZE_T_MAX = sys.maxsize
- from test.support.script_helper import assert_python_ok
- from test.support import unix_shell
- root_in_posix = False
- if hasattr(os, 'geteuid'):
- root_in_posix = (os.geteuid() == 0)
- # Detect whether we're on a Linux system that uses the (now outdated
- # and unmaintained) linuxthreads threading library. There's an issue
- # when combining linuxthreads with a failed execv call: see
- # http://bugs.python.org/issue4970.
- if hasattr(sys, 'thread_info') and sys.thread_info.version:
- USING_LINUXTHREADS = sys.thread_info.version.startswith("linuxthreads")
- else:
- USING_LINUXTHREADS = False
- # Issue #14110: Some tests fail on FreeBSD if the user is in the wheel group.
- HAVE_WHEEL_GROUP = sys.platform.startswith('freebsd') and os.getgid() == 0
- @contextlib.contextmanager
- def ignore_deprecation_warnings(msg_regex, quiet=False):
- with support.check_warnings((msg_regex, DeprecationWarning), quiet=quiet):
- yield
- @contextlib.contextmanager
- def bytes_filename_warn(expected):
- msg = 'The Windows bytes API has been deprecated'
- if os.name == 'nt':
- with ignore_deprecation_warnings(msg, quiet=not expected):
- yield
- else:
- yield
- def create_file(filename, content=b'content'):
- with open(filename, "xb", 0) as fp:
- fp.write(content)
- # Tests creating TESTFN
- class FileTests(unittest.TestCase):
- def setUp(self):
- if os.path.lexists(support.TESTFN):
- os.unlink(support.TESTFN)
- tearDown = setUp
- def test_access(self):
- f = os.open(support.TESTFN, os.O_CREAT|os.O_RDWR)
- os.close(f)
- self.assertTrue(os.access(support.TESTFN, os.W_OK))
- def test_closerange(self):
- first = os.open(support.TESTFN, os.O_CREAT|os.O_RDWR)
- # We must allocate two consecutive file descriptors, otherwise
- # it will mess up other file descriptors (perhaps even the three
- # standard ones).
- second = os.dup(first)
- try:
- retries = 0
- while second != first + 1:
- os.close(first)
- retries += 1
- if retries > 10:
- # XXX test skipped
- self.skipTest("couldn't allocate two consecutive fds")
- first, second = second, os.dup(second)
- finally:
- os.close(second)
- # close a fd that is open, and one that isn't
- os.closerange(first, first + 2)
- self.assertRaises(OSError, os.write, first, b"a")
- @support.cpython_only
- def test_rename(self):
- path = support.TESTFN
- old = sys.getrefcount(path)
- self.assertRaises(TypeError, os.rename, path, 0)
- new = sys.getrefcount(path)
- self.assertEqual(old, new)
- def test_read(self):
- with open(support.TESTFN, "w+b") as fobj:
- fobj.write(b"spam")
- fobj.flush()
- fd = fobj.fileno()
- os.lseek(fd, 0, 0)
- s = os.read(fd, 4)
- self.assertEqual(type(s), bytes)
- self.assertEqual(s, b"spam")
- @support.cpython_only
- # Skip the test on 32-bit platforms: the number of bytes must fit in a
- # Py_ssize_t type
- @unittest.skipUnless(INT_MAX < PY_SSIZE_T_MAX,
- "needs INT_MAX < PY_SSIZE_T_MAX")
- @support.bigmemtest(size=INT_MAX + 10, memuse=1, dry_run=False)
- def test_large_read(self, size):
- self.addCleanup(support.unlink, support.TESTFN)
- create_file(support.TESTFN, b'test')
- # Issue #21932: Make sure that os.read() does not raise an
- # OverflowError for size larger than INT_MAX
- with open(support.TESTFN, "rb") as fp:
- data = os.read(fp.fileno(), size)
- # The test does not try to read more than 2 GB at once because the
- # operating system is free to return less bytes than requested.
- self.assertEqual(data, b'test')
- def test_write(self):
- # os.write() accepts bytes- and buffer-like objects but not strings
- fd = os.open(support.TESTFN, os.O_CREAT | os.O_WRONLY)
- self.assertRaises(TypeError, os.write, fd, "beans")
- os.write(fd, b"bacon\n")
- os.write(fd, bytearray(b"eggs\n"))
- os.write(fd, memoryview(b"spam\n"))
- os.close(fd)
- with open(support.TESTFN, "rb") as fobj:
- self.assertEqual(fobj.read().splitlines(),
- [b"bacon", b"eggs", b"spam"])
- def write_windows_console(self, *args):
- retcode = subprocess.call(args,
- # use a new console to not flood the test output
- creationflags=subprocess.CREATE_NEW_CONSOLE,
- # use a shell to hide the console window (SW_HIDE)
- shell=True)
- self.assertEqual(retcode, 0)
- @unittest.skipUnless(sys.platform == 'win32',
- 'test specific to the Windows console')
- def test_write_windows_console(self):
- # Issue #11395: the Windows console returns an error (12: not enough
- # space error) on writing into stdout if stdout mode is binary and the
- # length is greater than 66,000 bytes (or less, depending on heap
- # usage).
- code = "print('x' * 100000)"
- self.write_windows_console(sys.executable, "-c", code)
- self.write_windows_console(sys.executable, "-u", "-c", code)
- def fdopen_helper(self, *args):
- fd = os.open(support.TESTFN, os.O_RDONLY)
- f = os.fdopen(fd, *args)
- f.close()
- def test_fdopen(self):
- fd = os.open(support.TESTFN, os.O_CREAT|os.O_RDWR)
- os.close(fd)
- self.fdopen_helper()
- self.fdopen_helper('r')
- self.fdopen_helper('r', 100)
- def test_replace(self):
- TESTFN2 = support.TESTFN + ".2"
- self.addCleanup(support.unlink, support.TESTFN)
- self.addCleanup(support.unlink, TESTFN2)
- create_file(support.TESTFN, b"1")
- create_file(TESTFN2, b"2")
- os.replace(support.TESTFN, TESTFN2)
- self.assertRaises(FileNotFoundError, os.stat, support.TESTFN)
- with open(TESTFN2, 'r') as f:
- self.assertEqual(f.read(), "1")
- def test_open_keywords(self):
- f = os.open(path=__file__, flags=os.O_RDONLY, mode=0o777,
- dir_fd=None)
- os.close(f)
- def test_symlink_keywords(self):
- symlink = support.get_attribute(os, "symlink")
- try:
- symlink(src='target', dst=support.TESTFN,
- target_is_directory=False, dir_fd=None)
- except (NotImplementedError, OSError):
- pass # No OS support or unprivileged user
- # Test attributes on return values from os.*stat* family.
- class StatAttributeTests(unittest.TestCase):
- def setUp(self):
- self.fname = support.TESTFN
- self.addCleanup(support.unlink, self.fname)
- create_file(self.fname, b"ABC")
- @unittest.skipUnless(hasattr(os, 'stat'), 'test needs os.stat()')
- def check_stat_attributes(self, fname):
- result = os.stat(fname)
- # Make sure direct access works
- self.assertEqual(result[stat.ST_SIZE], 3)
- self.assertEqual(result.st_size, 3)
- # Make sure all the attributes are there
- members = dir(result)
- for name in dir(stat):
- if name[:3] == 'ST_':
- attr = name.lower()
- if name.endswith("TIME"):
- def trunc(x): return int(x)
- else:
- def trunc(x): return x
- self.assertEqual(trunc(getattr(result, attr)),
- result[getattr(stat, name)])
- self.assertIn(attr, members)
- # Make sure that the st_?time and st_?time_ns fields roughly agree
- # (they should always agree up to around tens-of-microseconds)
- for name in 'st_atime st_mtime st_ctime'.split():
- floaty = int(getattr(result, name) * 100000)
- nanosecondy = getattr(result, name + "_ns") // 10000
- self.assertAlmostEqual(floaty, nanosecondy, delta=2)
- try:
- result[200]
- self.fail("No exception raised")
- except IndexError:
- pass
- # Make sure that assignment fails
- try:
- result.st_mode = 1
- self.fail("No exception raised")
- except AttributeError:
- pass
- try:
- result.st_rdev = 1
- self.fail("No exception raised")
- except (AttributeError, TypeError):
- pass
- try:
- result.parrot = 1
- self.fail("No exception raised")
- except AttributeError:
- pass
- # Use the stat_result constructor with a too-short tuple.
- try:
- result2 = os.stat_result((10,))
- self.fail("No exception raised")
- except TypeError:
- pass
- # Use the constructor with a too-long tuple.
- try:
- result2 = os.stat_result((0,1,2,3,4,5,6,7,8,9,10,11,12,13,14))
- except TypeError:
- pass
- def test_stat_attributes(self):
- self.check_stat_attributes(self.fname)
- def test_stat_attributes_bytes(self):
- try:
- fname = self.fname.encode(sys.getfilesystemencoding())
- except UnicodeEncodeError:
- self.skipTest("cannot encode %a for the filesystem" % self.fname)
- with bytes_filename_warn(True):
- self.check_stat_attributes(fname)
- def test_stat_result_pickle(self):
- result = os.stat(self.fname)
- for proto in range(pickle.HIGHEST_PROTOCOL + 1):
- p = pickle.dumps(result, proto)
- self.assertIn(b'stat_result', p)
- if proto < 4:
- self.assertIn(b'cos\nstat_result\n', p)
- unpickled = pickle.loads(p)
- self.assertEqual(result, unpickled)
- @unittest.skipUnless(hasattr(os, 'statvfs'), 'test needs os.statvfs()')
- def test_statvfs_attributes(self):
- try:
- result = os.statvfs(self.fname)
- except OSError as e:
- # On AtheOS, glibc always returns ENOSYS
- if e.errno == errno.ENOSYS:
- self.skipTest('os.statvfs() failed with ENOSYS')
- # Make sure direct access works
- self.assertEqual(result.f_bfree, result[3])
- # Make sure all the attributes are there.
- members = ('bsize', 'frsize', 'blocks', 'bfree', 'bavail', 'files',
- 'ffree', 'favail', 'flag', 'namemax')
- for value, member in enumerate(members):
- self.assertEqual(getattr(result, 'f_' + member), result[value])
- # Make sure that assignment really fails
- try:
- result.f_bfree = 1
- self.fail("No exception raised")
- except AttributeError:
- pass
- try:
- result.parrot = 1
- self.fail("No exception raised")
- except AttributeError:
- pass
- # Use the constructor with a too-short tuple.
- try:
- result2 = os.statvfs_result((10,))
- self.fail("No exception raised")
- except TypeError:
- pass
- # Use the constructor with a too-long tuple.
- try:
- result2 = os.statvfs_result((0,1,2,3,4,5,6,7,8,9,10,11,12,13,14))
- except TypeError:
- pass
- @unittest.skipUnless(hasattr(os, 'statvfs'),
- "need os.statvfs()")
- def test_statvfs_result_pickle(self):
- try:
- result = os.statvfs(self.fname)
- except OSError as e:
- # On AtheOS, glibc always returns ENOSYS
- if e.errno == errno.ENOSYS:
- self.skipTest('os.statvfs() failed with ENOSYS')
- for proto in range(pickle.HIGHEST_PROTOCOL + 1):
- p = pickle.dumps(result, proto)
- self.assertIn(b'statvfs_result', p)
- if proto < 4:
- self.assertIn(b'cos\nstatvfs_result\n', p)
- unpickled = pickle.loads(p)
- self.assertEqual(result, unpickled)
- @unittest.skipUnless(sys.platform == "win32", "Win32 specific tests")
- def test_1686475(self):
- # Verify that an open file can be stat'ed
- try:
- os.stat(r"c:\pagefile.sys")
- except FileNotFoundError:
- self.skipTest(r'c:\pagefile.sys does not exist')
- except OSError as e:
- self.fail("Could not stat pagefile.sys")
- @unittest.skipUnless(sys.platform == "win32", "Win32 specific tests")
- @unittest.skipUnless(hasattr(os, "pipe"), "requires os.pipe()")
- def test_15261(self):
- # Verify that stat'ing a closed fd does not cause crash
- r, w = os.pipe()
- try:
- os.stat(r) # should not raise error
- finally:
- os.close(r)
- os.close(w)
- with self.assertRaises(OSError) as ctx:
- os.stat(r)
- self.assertEqual(ctx.exception.errno, errno.EBADF)
- def check_file_attributes(self, result):
- self.assertTrue(hasattr(result, 'st_file_attributes'))
- self.assertTrue(isinstance(result.st_file_attributes, int))
- self.assertTrue(0 <= result.st_file_attributes <= 0xFFFFFFFF)
- @unittest.skipUnless(sys.platform == "win32",
- "st_file_attributes is Win32 specific")
- def test_file_attributes(self):
- # test file st_file_attributes (FILE_ATTRIBUTE_DIRECTORY not set)
- result = os.stat(self.fname)
- self.check_file_attributes(result)
- self.assertEqual(
- result.st_file_attributes & stat.FILE_ATTRIBUTE_DIRECTORY,
- 0)
- # test directory st_file_attributes (FILE_ATTRIBUTE_DIRECTORY set)
- dirname = support.TESTFN + "dir"
- os.mkdir(dirname)
- self.addCleanup(os.rmdir, dirname)
- result = os.stat(dirname)
- self.check_file_attributes(result)
- self.assertEqual(
- result.st_file_attributes & stat.FILE_ATTRIBUTE_DIRECTORY,
- stat.FILE_ATTRIBUTE_DIRECTORY)
- class UtimeTests(unittest.TestCase):
- def setUp(self):
- self.dirname = support.TESTFN
- self.fname = os.path.join(self.dirname, "f1")
- self.addCleanup(support.rmtree, self.dirname)
- os.mkdir(self.dirname)
- create_file(self.fname)
- def restore_float_times(state):
- with ignore_deprecation_warnings('stat_float_times'):
- os.stat_float_times(state)
- # ensure that st_atime and st_mtime are float
- with ignore_deprecation_warnings('stat_float_times'):
- old_float_times = os.stat_float_times(-1)
- self.addCleanup(restore_float_times, old_float_times)
- os.stat_float_times(True)
- def support_subsecond(self, filename):
- # Heuristic to check if the filesystem supports timestamp with
- # subsecond resolution: check if float and int timestamps are different
- st = os.stat(filename)
- return ((st.st_atime != st[7])
- or (st.st_mtime != st[8])
- or (st.st_ctime != st[9]))
- def _test_utime(self, set_time, filename=None):
- if not filename:
- filename = self.fname
- support_subsecond = self.support_subsecond(filename)
- if support_subsecond:
- # Timestamp with a resolution of 1 microsecond (10^-6).
- #
- # The resolution of the C internal function used by os.utime()
- # depends on the platform: 1 sec, 1 us, 1 ns. Writing a portable
- # test with a resolution of 1 ns requires more work:
- # see the issue #15745.
- atime_ns = 1002003000 # 1.002003 seconds
- mtime_ns = 4005006000 # 4.005006 seconds
- else:
- # use a resolution of 1 second
- atime_ns = 5 * 10**9
- mtime_ns = 8 * 10**9
- set_time(filename, (atime_ns, mtime_ns))
- st = os.stat(filename)
- if support_subsecond:
- self.assertAlmostEqual(st.st_atime, atime_ns * 1e-9, delta=1e-6)
- self.assertAlmostEqual(st.st_mtime, mtime_ns * 1e-9, delta=1e-6)
- else:
- self.assertEqual(st.st_atime, atime_ns * 1e-9)
- self.assertEqual(st.st_mtime, mtime_ns * 1e-9)
- self.assertEqual(st.st_atime_ns, atime_ns)
- self.assertEqual(st.st_mtime_ns, mtime_ns)
- def test_utime(self):
- def set_time(filename, ns):
- # test the ns keyword parameter
- os.utime(filename, ns=ns)
- self._test_utime(set_time)
- @staticmethod
- def ns_to_sec(ns):
- # Convert a number of nanosecond (int) to a number of seconds (float).
- # Round towards infinity by adding 0.5 nanosecond to avoid rounding
- # issue, os.utime() rounds towards minus infinity.
- return (ns * 1e-9) + 0.5e-9
- def test_utime_by_indexed(self):
- # pass times as floating point seconds as the second indexed parameter
- def set_time(filename, ns):
- atime_ns, mtime_ns = ns
- atime = self.ns_to_sec(atime_ns)
- mtime = self.ns_to_sec(mtime_ns)
- # test utimensat(timespec), utimes(timeval), utime(utimbuf)
- # or utime(time_t)
- os.utime(filename, (atime, mtime))
- self._test_utime(set_time)
- def test_utime_by_times(self):
- def set_time(filename, ns):
- atime_ns, mtime_ns = ns
- atime = self.ns_to_sec(atime_ns)
- mtime = self.ns_to_sec(mtime_ns)
- # test the times keyword parameter
- os.utime(filename, times=(atime, mtime))
- self._test_utime(set_time)
- @unittest.skipUnless(os.utime in os.supports_follow_symlinks,
- "follow_symlinks support for utime required "
- "for this test.")
- def test_utime_nofollow_symlinks(self):
- def set_time(filename, ns):
- # use follow_symlinks=False to test utimensat(timespec)
- # or lutimes(timeval)
- os.utime(filename, ns=ns, follow_symlinks=False)
- self._test_utime(set_time)
- @unittest.skipUnless(os.utime in os.supports_fd,
- "fd support for utime required for this test.")
- def test_utime_fd(self):
- def set_time(filename, ns):
- with open(filename, 'wb', 0) as fp:
- # use a file descriptor to test futimens(timespec)
- # or futimes(timeval)
- os.utime(fp.fileno(), ns=ns)
- self._test_utime(set_time)
- @unittest.skipUnless(os.utime in os.supports_dir_fd,
- "dir_fd support for utime required for this test.")
- def test_utime_dir_fd(self):
- def set_time(filename, ns):
- dirname, name = os.path.split(filename)
- dirfd = os.open(dirname, os.O_RDONLY)
- try:
- # pass dir_fd to test utimensat(timespec) or futimesat(timeval)
- os.utime(name, dir_fd=dirfd, ns=ns)
- finally:
- os.close(dirfd)
- self._test_utime(set_time)
- def test_utime_directory(self):
- def set_time(filename, ns):
- # test calling os.utime() on a directory
- os.utime(filename, ns=ns)
- self._test_utime(set_time, filename=self.dirname)
- def _test_utime_current(self, set_time):
- # Get the system clock
- current = time.time()
- # Call os.utime() to set the timestamp to the current system clock
- set_time(self.fname)
- if not self.support_subsecond(self.fname):
- delta = 1.0
- else:
- # On Windows, the usual resolution of time.time() is 15.6 ms
- delta = 0.020
- st = os.stat(self.fname)
- msg = ("st_time=%r, current=%r, dt=%r"
- % (st.st_mtime, current, st.st_mtime - current))
- self.assertAlmostEqual(st.st_mtime, current,
- delta=delta, msg=msg)
- def test_utime_current(self):
- def set_time(filename):
- # Set to the current time in the new way
- os.utime(self.fname)
- self._test_utime_current(set_time)
- def test_utime_current_old(self):
- def set_time(filename):
- # Set to the current time in the old explicit way.
- os.utime(self.fname, None)
- self._test_utime_current(set_time)
- def get_file_system(self, path):
- if sys.platform == 'win32':
- root = os.path.splitdrive(os.path.abspath(path))[0] + '\\'
- import ctypes
- kernel32 = ctypes.windll.kernel32
- buf = ctypes.create_unicode_buffer("", 100)
- ok = kernel32.GetVolumeInformationW(root, None, 0,
- None, None, None,
- buf, len(buf))
- if ok:
- return buf.value
- # return None if the filesystem is unknown
- def test_large_time(self):
- # Many filesystems are limited to the year 2038. At least, the test
- # pass with NTFS filesystem.
- if self.get_file_system(self.dirname) != "NTFS":
- self.skipTest("requires NTFS")
- large = 5000000000 # some day in 2128
- os.utime(self.fname, (large, large))
- self.assertEqual(os.stat(self.fname).st_mtime, large)
- def test_utime_invalid_arguments(self):
- # seconds and nanoseconds parameters are mutually exclusive
- with self.assertRaises(ValueError):
- os.utime(self.fname, (5, 5), ns=(5, 5))
- from test import mapping_tests
- class EnvironTests(mapping_tests.BasicTestMappingProtocol):
- """check that os.environ object conform to mapping protocol"""
- type2test = None
- def setUp(self):
- self.__save = dict(os.environ)
- if os.supports_bytes_environ:
- self.__saveb = dict(os.environb)
- for key, value in self._reference().items():
- os.environ[key] = value
- def tearDown(self):
- os.environ.clear()
- os.environ.update(self.__save)
- if os.supports_bytes_environ:
- os.environb.clear()
- os.environb.update(self.__saveb)
- def _reference(self):
- return {"KEY1":"VALUE1", "KEY2":"VALUE2", "KEY3":"VALUE3"}
- def _empty_mapping(self):
- os.environ.clear()
- return os.environ
- # Bug 1110478
- @unittest.skipUnless(unix_shell and os.path.exists(unix_shell),
- 'requires a shell')
- def test_update2(self):
- os.environ.clear()
- os.environ.update(HELLO="World")
- with os.popen("%s -c 'echo $HELLO'" % unix_shell) as popen:
- value = popen.read().strip()
- self.assertEqual(value, "World")
- @unittest.skipUnless(unix_shell and os.path.exists(unix_shell),
- 'requires a shell')
- def test_os_popen_iter(self):
- with os.popen("%s -c 'echo \"line1\nline2\nline3\"'"
- % unix_shell) as popen:
- it = iter(popen)
- self.assertEqual(next(it), "line1\n")
- self.assertEqual(next(it), "line2\n")
- self.assertEqual(next(it), "line3\n")
- self.assertRaises(StopIteration, next, it)
- # Verify environ keys and values from the OS are of the
- # correct str type.
- def test_keyvalue_types(self):
- for key, val in os.environ.items():
- self.assertEqual(type(key), str)
- self.assertEqual(type(val), str)
- def test_items(self):
- for key, value in self._reference().items():
- self.assertEqual(os.environ.get(key), value)
- # Issue 7310
- def test___repr__(self):
- """Check that the repr() of os.environ looks like environ({...})."""
- env = os.environ
- self.assertEqual(repr(env), 'environ({{{}}})'.format(', '.join(
- '{!r}: {!r}'.format(key, value)
- for key, value in env.items())))
- def test_get_exec_path(self):
- defpath_list = os.defpath.split(os.pathsep)
- test_path = ['/monty', '/python', '', '/flying/circus']
- test_env = {'PATH': os.pathsep.join(test_path)}
- saved_environ = os.environ
- try:
- os.environ = dict(test_env)
- # Test that defaulting to os.environ works.
- self.assertSequenceEqual(test_path, os.get_exec_path())
- self.assertSequenceEqual(test_path, os.get_exec_path(env=None))
- finally:
- os.environ = saved_environ
- # No PATH environment variable
- self.assertSequenceEqual(defpath_list, os.get_exec_path({}))
- # Empty PATH environment variable
- self.assertSequenceEqual(('',), os.get_exec_path({'PATH':''}))
- # Supplied PATH environment variable
- self.assertSequenceEqual(test_path, os.get_exec_path(test_env))
- if os.supports_bytes_environ:
- # env cannot contain 'PATH' and b'PATH' keys
- try:
- # ignore BytesWarning warning
- with warnings.catch_warnings(record=True):
- mixed_env = {'PATH': '1', b'PATH': b'2'}
- except BytesWarning:
- # mixed_env cannot be created with python -bb
- pass
- else:
- self.assertRaises(ValueError, os.get_exec_path, mixed_env)
- # bytes key and/or value
- self.assertSequenceEqual(os.get_exec_path({b'PATH': b'abc'}),
- ['abc'])
- self.assertSequenceEqual(os.get_exec_path({b'PATH': 'abc'}),
- ['abc'])
- self.assertSequenceEqual(os.get_exec_path({'PATH': b'abc'}),
- ['abc'])
- @unittest.skipUnless(os.supports_bytes_environ,
- "os.environb required for this test.")
- def test_environb(self):
- # os.environ -> os.environb
- value = 'euro\u20ac'
- try:
- value_bytes = value.encode(sys.getfilesystemencoding(),
- 'surrogateescape')
- except UnicodeEncodeError:
- msg = "U+20AC character is not encodable to %s" % (
- sys.getfilesystemencoding(),)
- self.skipTest(msg)
- os.environ['unicode'] = value
- self.assertEqual(os.environ['unicode'], value)
- self.assertEqual(os.environb[b'unicode'], value_bytes)
- # os.environb -> os.environ
- value = b'\xff'
- os.environb[b'bytes'] = value
- self.assertEqual(os.environb[b'bytes'], value)
- value_str = value.decode(sys.getfilesystemencoding(), 'surrogateescape')
- self.assertEqual(os.environ['bytes'], value_str)
- # On FreeBSD < 7 and OS X < 10.6, unsetenv() doesn't return a value (issue
- # #13415).
- @support.requires_freebsd_version(7)
- @support.requires_mac_ver(10, 6)
- def test_unset_error(self):
- if sys.platform == "win32":
- # an environment variable is limited to 32,767 characters
- key = 'x' * 50000
- self.assertRaises(ValueError, os.environ.__delitem__, key)
- else:
- # "=" is not allowed in a variable name
- key = 'key='
- self.assertRaises(OSError, os.environ.__delitem__, key)
- def test_key_type(self):
- missing = 'missingkey'
- self.assertNotIn(missing, os.environ)
- with self.assertRaises(KeyError) as cm:
- os.environ[missing]
- self.assertIs(cm.exception.args[0], missing)
- self.assertTrue(cm.exception.__suppress_context__)
- with self.assertRaises(KeyError) as cm:
- del os.environ[missing]
- self.assertIs(cm.exception.args[0], missing)
- self.assertTrue(cm.exception.__suppress_context__)
- class WalkTests(unittest.TestCase):
- """Tests for os.walk()."""
- # Wrapper to hide minor differences between os.walk and os.fwalk
- # to tests both functions with the same code base
- def walk(self, top, **kwargs):
- if 'follow_symlinks' in kwargs:
- kwargs['followlinks'] = kwargs.pop('follow_symlinks')
- return os.walk(top, **kwargs)
- def setUp(self):
- join = os.path.join
- self.addCleanup(support.rmtree, support.TESTFN)
- # Build:
- # TESTFN/
- # TEST1/ a file kid and two directory kids
- # tmp1
- # SUB1/ a file kid and a directory kid
- # tmp2
- # SUB11/ no kids
- # SUB2/ a file kid and a dirsymlink kid
- # tmp3
- # link/ a symlink to TESTFN.2
- # broken_link
- # TEST2/
- # tmp4 a lone file
- self.walk_path = join(support.TESTFN, "TEST1")
- self.sub1_path = join(self.walk_path, "SUB1")
- self.sub11_path = join(self.sub1_path, "SUB11")
- sub2_path = join(self.walk_path, "SUB2")
- tmp1_path = join(self.walk_path, "tmp1")
- tmp2_path = join(self.sub1_path, "tmp2")
- tmp3_path = join(sub2_path, "tmp3")
- self.link_path = join(sub2_path, "link")
- t2_path = join(support.TESTFN, "TEST2")
- tmp4_path = join(support.TESTFN, "TEST2", "tmp4")
- broken_link_path = join(sub2_path, "broken_link")
- # Create stuff.
- os.makedirs(self.sub11_path)
- os.makedirs(sub2_path)
- os.makedirs(t2_path)
- for path in tmp1_path, tmp2_path, tmp3_path, tmp4_path:
- with open(path, "x") as f:
- f.write("I'm " + path + " and proud of it. Blame test_os.\n")
- if support.can_symlink():
- os.symlink(os.path.abspath(t2_path), self.link_path)
- os.symlink('broken', broken_link_path, True)
- self.sub2_tree = (sub2_path, ["link"], ["broken_link", "tmp3"])
- else:
- self.sub2_tree = (sub2_path, [], ["tmp3"])
- def test_walk_topdown(self):
- # Walk top-down.
- all = list(self.walk(self.walk_path))
- self.assertEqual(len(all), 4)
- # We can't know which order SUB1 and SUB2 will appear in.
- # Not flipped: TESTFN, SUB1, SUB11, SUB2
- # flipped: TESTFN, SUB2, SUB1, SUB11
- flipped = all[0][1][0] != "SUB1"
- all[0][1].sort()
- all[3 - 2 * flipped][-1].sort()
- self.assertEqual(all[0], (self.walk_path, ["SUB1", "SUB2"], ["tmp1"]))
- self.assertEqual(all[1 + flipped], (self.sub1_path, ["SUB11"], ["tmp2"]))
- self.assertEqual(all[2 + flipped], (self.sub11_path, [], []))
- self.assertEqual(all[3 - 2 * flipped], self.sub2_tree)
- def test_walk_prune(self):
- # Prune the search.
- all = []
- for root, dirs, files in self.walk(self.walk_path):
- all.append((root, dirs, files))
- # Don't descend into SUB1.
- if 'SUB1' in dirs:
- # Note that this also mutates the dirs we appended to all!
- dirs.remove('SUB1')
- self.assertEqual(len(all), 2)
- self.assertEqual(all[0],
- (self.walk_path, ["SUB2"], ["tmp1"]))
- all[1][-1].sort()
- self.assertEqual(all[1], self.sub2_tree)
- def test_walk_bottom_up(self):
- # Walk bottom-up.
- all = list(self.walk(self.walk_path, topdown=False))
- self.assertEqual(len(all), 4, all)
- # We can't know which order SUB1 and SUB2 will appear in.
- # Not flipped: SUB11, SUB1, SUB2, TESTFN
- # flipped: SUB2, SUB11, SUB1, TESTFN
- flipped = all[3][1][0] != "SUB1"
- all[3][1].sort()
- all[2 - 2 * flipped][-1].sort()
- self.assertEqual(all[3],
- (self.walk_path, ["SUB1", "SUB2"], ["tmp1"]))
- self.assertEqual(all[flipped],
- (self.sub11_path, [], []))
- self.assertEqual(all[flipped + 1],
- (self.sub1_path, ["SUB11"], ["tmp2"]))
- self.assertEqual(all[2 - 2 * flipped],
- self.sub2_tree)
- def test_walk_symlink(self):
- if not support.can_symlink():
- self.skipTest("need symlink support")
- # Walk, following symlinks.
- walk_it = self.walk(self.walk_path, follow_symlinks=True)
- for root, dirs, files in walk_it:
- if root == self.link_path:
- self.assertEqual(dirs, [])
- self.assertEqual(files, ["tmp4"])
- break
- else:
- self.fail("Didn't follow symlink with followlinks=True")
- def test_walk_bad_dir(self):
- # Walk top-down.
- errors = []
- walk_it = self.walk(self.walk_path, onerror=errors.append)
- root, dirs, files = next(walk_it)
- self.assertFalse(errors)
- dir1 = dirs[0]
- dir1new = dir1 + '.new'
- os.rename(os.path.join(root, dir1), os.path.join(root, dir1new))
- roots = [r for r, d, f in walk_it]
- self.assertTrue(errors)
- self.assertNotIn(os.path.join(root, dir1), roots)
- self.assertNotIn(os.path.join(root, dir1new), roots)
- for dir2 in dirs[1:]:
- self.assertIn(os.path.join(root, dir2), roots)
- @unittest.skipUnless(hasattr(os, 'fwalk'), "Test needs os.fwalk()")
- class FwalkTests(WalkTests):
- """Tests for os.fwalk()."""
- def walk(self, top, **kwargs):
- for root, dirs, files, root_fd in os.fwalk(top, **kwargs):
- yield (root, dirs, files)
- def _compare_to_walk(self, walk_kwargs, fwalk_kwargs):
- """
- compare with walk() results.
- """
- walk_kwargs = walk_kwargs.copy()
- fwalk_kwargs = fwalk_kwargs.copy()
- for topdown, follow_symlinks in itertools.product((True, False), repeat=2):
- walk_kwargs.update(topdown=topdown, followlinks=follow_symlinks)
- fwalk_kwargs.update(topdown=topdown, follow_symlinks=follow_symlinks)
- expected = {}
- for root, dirs, files in os.walk(**walk_kwargs):
- expected[root] = (set(dirs), set(files))
- for root, dirs, files, rootfd in os.fwalk(**fwalk_kwargs):
- self.assertIn(root, expected)
- self.assertEqual(expected[root], (set(dirs), set(files)))
- def test_compare_to_walk(self):
- kwargs = {'top': support.TESTFN}
- self._compare_to_walk(kwargs, kwargs)
- def test_dir_fd(self):
- try:
- fd = os.open(".", os.O_RDONLY)
- walk_kwargs = {'top': support.TESTFN}
- fwalk_kwargs = walk_kwargs.copy()
- fwalk_kwargs['dir_fd'] = fd
- self._compare_to_walk(walk_kwargs, fwalk_kwargs)
- finally:
- os.close(fd)
- def test_yields_correct_dir_fd(self):
- # check returned file descriptors
- for topdown, follow_symlinks in itertools.product((True, False), repeat=2):
- args = support.TESTFN, topdown, None
- for root, dirs, files, rootfd in os.fwalk(*args, follow_symlinks=follow_symlinks):
- # check that the FD is valid
- os.fstat(rootfd)
- # redundant check
- os.stat(rootfd)
- # check that listdir() returns consistent information
- self.assertEqual(set(os.listdir(rootfd)), set(dirs) | set(files))
- def test_fd_leak(self):
- # Since we're opening a lot of FDs, we must be careful to avoid leaks:
- # we both check that calling fwalk() a large number of times doesn't
- # yield EMFILE, and that the minimum allocated FD hasn't changed.
- minfd = os.dup(1)
- os.close(minfd)
- for i in range(256):
- for x in os.fwalk(support.TESTFN):
- pass
- newfd = os.dup(1)
- self.addCleanup(os.close, newfd)
- self.assertEqual(newfd, minfd)
- class BytesWalkTests(WalkTests):
- """Tests for os.walk() with bytes."""
- def setUp(self):
- super().setUp()
- self.stack = contextlib.ExitStack()
- if os.name == 'nt':
- self.stack.enter_context(bytes_filename_warn(False))
- def tearDown(self):
- self.stack.close()
- super().tearDown()
- def walk(self, top, **kwargs):
- if 'follow_symlinks' in kwargs:
- kwargs['followlinks'] = kwargs.pop('follow_symlinks')
- for broot, bdirs, bfiles in os.walk(os.fsencode(top), **kwargs):
- root = os.fsdecode(broot)
- dirs = list(map(os.fsdecode, bdirs))
- files = list(map(os.fsdecode, bfiles))
- yield (root, dirs, files)
- bdirs[:] = list(map(os.fsencode, dirs))
- bfiles[:] = list(map(os.fsencode, files))
- class MakedirTests(unittest.TestCase):
- def setUp(self):
- os.mkdir(support.TESTFN)
- def test_makedir(self):
- base = support.TESTFN
- path = os.path.join(base, 'dir1', 'dir2', 'dir3')
- os.makedirs(path) # Should work
- path = os.path.join(base, 'dir1', 'dir2', 'dir3', 'dir4')
- os.makedirs(path)
- # Try paths with a '.' in them
- self.assertRaises(OSError, os.makedirs, os.curdir)
- path = os.path.join(base, 'dir1', 'dir2', 'dir3', 'dir4', 'dir5', os.curdir)
- os.makedirs(path)
- path = os.path.join(base, 'dir1', os.curdir, 'dir2', 'dir3', 'dir4',
- 'dir5', 'dir6')
- os.makedirs(path)
- def test_exist_ok_existing_directory(self):
- path = os.path.join(support.TESTFN, 'dir1')
- mode = 0o777
- old_mask = os.umask(0o022)
- os.makedirs(path, mode)
- self.assertRaises(OSError, os.makedirs, path, mode)
- self.assertRaises(OSError, os.makedirs, path, mode, exist_ok=False)
- os.makedirs(path, 0o776, exist_ok=True)
- os.makedirs(path, mode=mode, exist_ok=True)
- os.umask(old_mask)
- # Issue #25583: A drive root could raise PermissionError on Windows
- os.makedirs(os.path.abspath('/'), exist_ok=True)
- def test_exist_ok_s_isgid_directory(self):
- path = os.path.join(support.TESTFN, 'dir1')
- S_ISGID = stat.S_ISGID
- mode = 0o777
- old_mask = os.umask(0o022)
- try:
- existing_testfn_mode = stat.S_IMODE(
- os.lstat(support.TESTFN).st_mode)
- try:
- os.chmod(support.TESTFN, existing_testfn_mode | S_ISGID)
- except PermissionError:
- raise unittest.SkipTest('Cannot set S_ISGID for dir.')
- if (os.lstat(support.TESTFN).st_mode & S_ISGID != S_ISGID):
- raise unittest.SkipTest('No support for S_ISGID dir mode.')
- # The os should apply S_ISGID from the parent dir for us, but
- # this test need not depend on that behavior. Be explicit.
- os.makedirs(path, mode | S_ISGID)
- # http://bugs.python.org/issue14992
- # Should not fail when the bit is already set.
- os.makedirs(path, mode, exist_ok=True)
- # remove the bit.
- os.chmod(path, stat.S_IMODE(os.lstat(path).st_mode) & ~S_ISGID)
- # May work even when the bit is not already set when demanded.
- os.makedirs(path, mode | S_ISGID, exist_ok=True)
- finally:
- os.umask(old_mask)
- def test_exist_ok_existing_regular_file(self):
- base = support.TESTFN
- path = os.path.join(support.TESTFN, 'dir1')
- f = open(path, 'w')
- f.write('abc')
- f.close()
- self.assertRaises(OSError, os.makedirs, path)
- self.assertRaises(OSError, os.makedirs, path, exist_ok=False)
- self.assertRaises(OSError, os.makedirs, path, exist_ok=True)
- os.remove(path)
- def tearDown(self):
- path = os.path.join(support.TESTFN, 'dir1', 'dir2', 'dir3',
- 'dir4', 'dir5', 'dir6')
- # If the tests failed, the bottom-most directory ('../dir6')
- # may not have been created, so we look for the outermost directory
- # that exists.
- while not os.path.exists(path) and path != support.TESTFN:
- path = os.path.dirname(path)
- os.removedirs(path)
- @unittest.skipUnless(hasattr(os, 'chown'), "Test needs chown")
- class ChownFileTests(unittest.TestCase):
- @classmethod
- def setUpClass(cls):
- os.mkdir(support.TESTFN)
- def test_chown_uid_gid_arguments_must_be_index(self):
- stat = os.stat(support.TESTFN)
- uid = stat.st_uid
- gid = stat.st_gid
- for value in (-1.0, -1j, decimal.Decimal(-1), fractions.Fraction(-2, 2)):
- self.assertRaises(TypeError, os.chown, support.TESTFN, value, gid)
- self.assertRaises(TypeError, os.chown, support.TESTFN, uid, value)
- self.assertIsNone(os.chown(support.TESTFN, uid, gid))
- self.assertIsNone(os.chown(support.TESTFN, -1, -1))
- @unittest.skipUnless(len(groups) > 1, "test needs more than one group")
- def test_chown(self):
- gid_1, gid_2 = groups[:2]
- uid = os.stat(support.TESTFN).st_uid
- os.chown(support.TESTFN, uid, gid_1)
- gid = os.stat(support.TESTFN).st_gid
- self.assertEqual(gid, gid_1)
- os.chown(support.TESTFN, uid, gid_2)
- gid = os.stat(support.TESTFN).st_gid
- self.assertEqual(gid, gid_2)
- @unittest.skipUnless(root_in_posix and len(all_users) > 1,
- "test needs root privilege and more than one user")
- def test_chown_with_root(self):
- uid_1, uid_2 = all_users[:2]
- gid = os.stat(support.TESTFN).st_gid
- os.chown(support.TESTFN, uid_1, gid)
- uid = os.stat(support.TESTFN).st_uid
- self.assertEqual(uid, uid_1)
- os.chown(support.TESTFN, uid_2, gid)
- uid = os.stat(support.TESTFN).st_uid
- self.assertEqual(uid, uid_2)
- @unittest.skipUnless(not root_in_posix and len(all_users) > 1,
- "test needs non-root account and more than one user")
- def test_chown_without_permission(self):
- uid_1, uid_2 = all_users[:2]
- gid = os.stat(support.TESTFN).st_gid
- with self.assertRaises(PermissionError):
- os.chown(support.TESTFN, uid_1, gid)
- os.chown(support.TESTFN, uid_2, gid)
- @classmethod
- def tearDownClass(cls):
- os.rmdir(support.TESTFN)
- class RemoveDirsTests(unittest.TestCase):
- def setUp(self):
- os.makedirs(support.TESTFN)
- def tearDown(self):
- support.rmtree(support.TESTFN)
- def test_remove_all(self):
- dira = os.path.join(support.TESTFN, 'dira')
- os.mkdir(dira)
- dirb = os.path.join(dira, 'dirb')
- os.mkdir(dirb)
- os.removedirs(dirb)
- self.assertFalse(os.path.exists(dirb))
- self.assertFalse(os.path.exists(dira))
- self.assertFalse(os.path.exists(support.TESTFN))
- def test_remove_partial(self):
- dira = os.path.join(support.TESTFN, 'dira')
- os.mkdir(dira)
- dirb = os.path.join(dira, 'dirb')
- os.mkdir(dirb)
- create_file(os.path.join(dira, 'file.txt'))
- os.removedirs(dirb)
- self.assertFalse(os.path.exists(dirb))
- self.assertTrue(os.path.exists(dira))
- self.assertTrue(os.path.exists(support.TESTFN))
- def test_remove_nothing(self):
- dira = os.path.join(support.TESTFN, 'dira')
- os.mkdir(dira)
- dirb = os.path.join(dira, 'dirb')
- os.mkdir(dirb)
- create_file(os.path.join(dirb, 'file.txt'))
- with self.assertRaises(OSError):
- os.removedirs(dirb)
- self.assertTrue(os.path.exists(dirb))
- self.assertTrue(os.path.exists(dira))
- self.assertTrue(os.path.exists(support.TESTFN))
- class DevNullTests(unittest.TestCase):
- def test_devnull(self):
- with open(os.devnull, 'wb', 0) as f:
- f.write(b'hello')
- f.close()
- with open(os.devnull, 'rb') as f:
- self.assertEqual(f.read(), b'')
- class URandomTests(unittest.TestCase):
- def test_urandom_length(self):
- self.assertEqual(len(os.urandom(0)), 0)
- self.assertEqual(len(os.urandom(1)), 1)
- self.assertEqual(len(os.urandom(10)), 10)
- self.assertEqual(len(os.urandom(100)), 100)
- self.assertEqual(len(os.urandom(1000)), 1000)
- def test_urandom_value(self):
- data1 = os.urandom(16)
- data2 = os.urandom(16)
- self.assertNotEqual(data1, data2)
- def get_urandom_subprocess(self, count):
- code = '\n'.join((
- 'import os, sys',
- 'data = os.urandom(%s)' % count,
- 'sys.stdout.buffer.write(data)',
- 'sys.stdout.buffer.flush()'))
- out = assert_python_ok('-c', code)
- stdout = out[1]
- self.assertEqual(len(stdout), 16)
- return stdout
- def test_urandom_subprocess(self):
- data1 = self.get_urandom_subprocess(16)
- data2 = self.get_urandom_subprocess(16)
- self.assertNotEqual(data1, data2)
- # os.urandom() doesn't use a file descriptor when it is implemented with the
- # getentropy() function, the getrandom() function or the getrandom() syscall
- OS_URANDOM_DONT_USE_FD = (
- sysconfig.get_config_var('HAVE_GETENTROPY') == 1
- or sysconfig.get_config_var('HAVE_GETRANDOM') == 1
- or sysconfig.get_config_var('HAVE_GETRANDOM_SYSCALL') == 1)
- @unittest.skipIf(OS_URANDOM_DONT_USE_FD ,
- "os.random() does not use a file descriptor")
- class URandomFDTests(unittest.TestCase):
- @unittest.skipUnless(resource, "test requires the resource module")
- def test_urandom_failure(self):
- # Check urandom() failing when it is not able to open /dev/random.
- # We spawn a new process to make the test more robust (if getrlimit()
- # failed to restore the file descriptor limit after this, the whole
- # test suite would crash; this actually happened on the OS X Tiger
- # buildbot).
- code = """if 1:
- import errno
- import os
- import resource
- soft_limit, hard_limit = resource.getrlimit(resource.RLIMIT_NOFILE)
- resource.setrlimit(resource.RLIMIT_NOFILE, (1, hard_limit))
- try:
- os.urandom(16)
- except OSError as e:
- assert e.errno == errno.EMFILE, e.errno
- else:
- raise AssertionError("OSError not raised")
- """
- assert_python_ok('-c', code)
- def test_urandom_fd_closed(self):
- # Issue #21207: urandom() should reopen its fd to /dev/urandom if
- # closed.
- code = """if 1:
- import os
- import sys
- import test.support
- os.urandom(4)
- with test.support.SuppressCrashReport():
- os.closerange(3, 256)
- sys.stdout.buffer.write(os.urandom(4))
- """
- rc, out, err = assert_python_ok('-Sc', code)
- def test_urandom_fd_reopened(self):
- # Issue #21207: urandom() should detect its fd to /dev/urandom
- # changed to something else, and reopen it.
- self.addCleanup(support.unlink, support.TESTFN)
- create_file(support.TESTFN, b"x" * 256)
- code = """if 1:
- import os
- import sys
- import test.support
- os.urandom(4)
- with test.support.SuppressCrashReport():
- for fd in range(3, 256):
- try:
- os.close(fd)
- except OSError:
- pass
- else:
- # Found the urandom fd (XXX hopefully)
- break
- os.closerange(3, 256)
- with open({TESTFN!r}, 'rb') as f:
- os.dup2(f.fileno(), fd)
- sys.stdout.buffer.write(os.urandom(4))
- sys.stdout.buffer.write(os.urandom(4))
- """.format(TESTFN=support.TESTFN)
- rc, out, err = assert_python_ok('-Sc', code)
- self.assertEqual(len(out), 8)
- self.assertNotEqual(out[0:4], out[4:8])
- rc, out2, err2 = assert_python_ok('-Sc', code)
- self.assertEqual(len(out2), 8)
- self.assertNotEqual(out2, out)
- @contextlib.contextmanager
- def _execvpe_mockup(defpath=None):
- """
- Stubs out execv and execve functions when used as context manager.
- Records exec calls. The mock execv and execve functions always raise an
- exception as they would normally never return.
- """
- # A list of tuples containing (function name, first arg, args)
- # of calls to execv or execve that have been made.
- calls = []
- def mock_execv(name, *args):
- calls.append(('execv', name, args))
- raise RuntimeError("execv called")
- def mock_execve(name, *args):
- calls.append(('execve', name, args))
- raise OSError(errno.ENOTDIR, "execve called")
- try:
- orig_execv = os.execv
- orig_execve = os.execve
- orig_defpath = os.defpath
- os.execv …
Large files files are truncated, but you can click here to view the full file