/client/bower_components/brython/www/src/Lib/test/test_os.py
Python | 1348 lines | 1085 code | 146 blank | 117 comment | 175 complexity | b4bf81172a2f22e2b8e993a3f6bdd98b MD5 | raw 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 os
- import errno
- import unittest
- import warnings
- import sys
- import signal
- import subprocess
- import time
- import shutil
- from test import support
- import contextlib
- import mmap
- import platform
- import re
- import uuid
- import asyncore
- import asynchat
- import socket
- import itertools
- import stat
- import locale
- import codecs
- try:
- import threading
- except ImportError:
- threading = None
- try:
- import resource
- except ImportError:
- resource = None
- from test.script_helper import assert_python_ok
- with warnings.catch_warnings():
- warnings.simplefilter("ignore", DeprecationWarning)
- os.stat_float_times(True)
- st = os.stat(__file__)
- stat_supports_subsecond = (
- # check if float and int timestamps are different
- (st.st_atime != st[7])
- or (st.st_mtime != st[8])
- or (st.st_ctime != st[9]))
- # 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
- # Tests creating TESTFN
- class FileTests(unittest.TestCase):
- def setUp(self):
- if os.path.exists(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")
- 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"
- with open(support.TESTFN, 'w') as f:
- f.write("1")
- with open(TESTFN2, 'w') as f:
- f.write("2")
- self.addCleanup(os.unlink, TESTFN2)
- os.replace(support.TESTFN, TESTFN2)
- self.assertRaises(FileNotFoundError, os.stat, support.TESTFN)
- with open(TESTFN2, 'r') as f:
- self.assertEqual(f.read(), "1")
- # Test attributes on return values from os.*stat* family.
- class StatAttributeTests(unittest.TestCase):
- def setUp(self):
- os.mkdir(support.TESTFN)
- self.fname = os.path.join(support.TESTFN, "f1")
- f = open(self.fname, 'wb')
- f.write(b"ABC")
- f.close()
- def tearDown(self):
- os.unlink(self.fname)
- os.rmdir(support.TESTFN)
- def check_stat_attributes(self, fname):
- if not hasattr(os, "stat"):
- return
- 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 warnings.catch_warnings():
- warnings.simplefilter("ignore", DeprecationWarning)
- self.check_stat_attributes(fname)
- def test_statvfs_attributes(self):
- if not hasattr(os, "statvfs"):
- return
- try:
- result = os.statvfs(self.fname)
- except OSError as e:
- # On AtheOS, glibc always returns ENOSYS
- if e.errno == errno.ENOSYS:
- return
- # 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
- def test_utime_dir(self):
- delta = 1000000
- st = os.stat(support.TESTFN)
- # round to int, because some systems may support sub-second
- # time stamps in stat, but not in utime.
- os.utime(support.TESTFN, (st.st_atime, int(st.st_mtime-delta)))
- st2 = os.stat(support.TESTFN)
- self.assertEqual(st2.st_mtime, int(st.st_mtime-delta))
- def _test_utime(self, filename, attr, utime, delta):
- # Issue #13327 removed the requirement to pass None as the
- # second argument. Check that the previous methods of passing
- # a time tuple or None work in addition to no argument.
- st0 = os.stat(filename)
- # Doesn't set anything new, but sets the time tuple way
- utime(filename, (attr(st0, "st_atime"), attr(st0, "st_mtime")))
- # Setting the time to the time you just read, then reading again,
- # should always return exactly the same times.
- st1 = os.stat(filename)
- self.assertEqual(attr(st0, "st_mtime"), attr(st1, "st_mtime"))
- self.assertEqual(attr(st0, "st_atime"), attr(st1, "st_atime"))
- # Set to the current time in the old explicit way.
- os.utime(filename, None)
- st2 = os.stat(support.TESTFN)
- # Set to the current time in the new way
- os.utime(filename)
- st3 = os.stat(filename)
- self.assertAlmostEqual(attr(st2, "st_mtime"), attr(st3, "st_mtime"), delta=delta)
- def test_utime(self):
- def utime(file, times):
- return os.utime(file, times)
- self._test_utime(self.fname, getattr, utime, 10)
- self._test_utime(support.TESTFN, getattr, utime, 10)
- def _test_utime_ns(self, set_times_ns, test_dir=True):
- def getattr_ns(o, attr):
- return getattr(o, attr + "_ns")
- ten_s = 10 * 1000 * 1000 * 1000
- self._test_utime(self.fname, getattr_ns, set_times_ns, ten_s)
- if test_dir:
- self._test_utime(support.TESTFN, getattr_ns, set_times_ns, ten_s)
- def test_utime_ns(self):
- def utime_ns(file, times):
- return os.utime(file, ns=times)
- self._test_utime_ns(utime_ns)
- requires_utime_dir_fd = unittest.skipUnless(
- os.utime in os.supports_dir_fd,
- "dir_fd support for utime required for this test.")
- requires_utime_fd = unittest.skipUnless(
- os.utime in os.supports_fd,
- "fd support for utime required for this test.")
- requires_utime_nofollow_symlinks = unittest.skipUnless(
- os.utime in os.supports_follow_symlinks,
- "follow_symlinks support for utime required for this test.")
- @requires_utime_nofollow_symlinks
- def test_lutimes_ns(self):
- def lutimes_ns(file, times):
- return os.utime(file, ns=times, follow_symlinks=False)
- self._test_utime_ns(lutimes_ns)
- @requires_utime_fd
- def test_futimes_ns(self):
- def futimes_ns(file, times):
- with open(file, "wb") as f:
- os.utime(f.fileno(), ns=times)
- self._test_utime_ns(futimes_ns, test_dir=False)
- def _utime_invalid_arguments(self, name, arg):
- with self.assertRaises(ValueError):
- getattr(os, name)(arg, (5, 5), ns=(5, 5))
- def test_utime_invalid_arguments(self):
- self._utime_invalid_arguments('utime', self.fname)
- @unittest.skipUnless(stat_supports_subsecond,
- "os.stat() doesn't has a subsecond resolution")
- def _test_utime_subsecond(self, set_time_func):
- asec, amsec = 1, 901
- atime = asec + amsec * 1e-3
- msec, mmsec = 2, 901
- mtime = msec + mmsec * 1e-3
- filename = self.fname
- os.utime(filename, (0, 0))
- set_time_func(filename, atime, mtime)
- with warnings.catch_warnings():
- warnings.simplefilter("ignore", DeprecationWarning)
- os.stat_float_times(True)
- st = os.stat(filename)
- self.assertAlmostEqual(st.st_atime, atime, places=3)
- self.assertAlmostEqual(st.st_mtime, mtime, places=3)
- def test_utime_subsecond(self):
- def set_time(filename, atime, mtime):
- os.utime(filename, (atime, mtime))
- self._test_utime_subsecond(set_time)
- @requires_utime_fd
- def test_futimes_subsecond(self):
- def set_time(filename, atime, mtime):
- with open(filename, "wb") as f:
- os.utime(f.fileno(), times=(atime, mtime))
- self._test_utime_subsecond(set_time)
- @requires_utime_fd
- def test_futimens_subsecond(self):
- def set_time(filename, atime, mtime):
- with open(filename, "wb") as f:
- os.utime(f.fileno(), times=(atime, mtime))
- self._test_utime_subsecond(set_time)
- @requires_utime_dir_fd
- def test_futimesat_subsecond(self):
- def set_time(filename, atime, mtime):
- dirname = os.path.dirname(filename)
- dirfd = os.open(dirname, os.O_RDONLY)
- try:
- os.utime(os.path.basename(filename), dir_fd=dirfd,
- times=(atime, mtime))
- finally:
- os.close(dirfd)
- self._test_utime_subsecond(set_time)
- @requires_utime_nofollow_symlinks
- def test_lutimes_subsecond(self):
- def set_time(filename, atime, mtime):
- os.utime(filename, (atime, mtime), follow_symlinks=False)
- self._test_utime_subsecond(set_time)
- @requires_utime_dir_fd
- def test_utimensat_subsecond(self):
- def set_time(filename, atime, mtime):
- dirname = os.path.dirname(filename)
- dirfd = os.open(dirname, os.O_RDONLY)
- try:
- os.utime(os.path.basename(filename), dir_fd=dirfd,
- times=(atime, mtime))
- finally:
- os.close(dirfd)
- self._test_utime_subsecond(set_time)
- # Restrict test to Win32, since there is no guarantee other
- # systems support centiseconds
- if sys.platform == 'win32':
- def get_file_system(path):
- root = os.path.splitdrive(os.path.abspath(path))[0] + '\\'
- import ctypes
- kernel32 = ctypes.windll.kernel32
- buf = ctypes.create_unicode_buffer("", 100)
- if kernel32.GetVolumeInformationW(root, None, 0, None, None, None, buf, len(buf)):
- return buf.value
- if get_file_system(support.TESTFN) == "NTFS":
- def test_1565150(self):
- t1 = 1159195039.25
- os.utime(self.fname, (t1, t1))
- self.assertEqual(os.stat(self.fname).st_mtime, t1)
- def test_large_time(self):
- t1 = 5000000000 # some day in 2128
- os.utime(self.fname, (t1, t1))
- self.assertEqual(os.stat(self.fname).st_mtime, t1)
- def test_1686475(self):
- # Verify that an open file can be stat'ed
- try:
- os.stat(r"c:\pagefile.sys")
- except WindowsError as e:
- if e.errno == 2: # file does not exist; cannot run test
- return
- self.fail("Could not stat pagefile.sys")
- @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)
- 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(os.path.exists('/bin/sh'), 'requires /bin/sh')
- def test_update2(self):
- os.environ.clear()
- os.environ.update(HELLO="World")
- with os.popen("/bin/sh -c 'echo $HELLO'") as popen:
- value = popen.read().strip()
- self.assertEqual(value, "World")
- @unittest.skipUnless(os.path.exists('/bin/sh'), 'requires /bin/sh')
- def test_os_popen_iter(self):
- with os.popen(
- "/bin/sh -c 'echo \"line1\nline2\nline3\"'") 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()."""
- def setUp(self):
- import os
- from os.path import join
- # 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
- walk_path = join(support.TESTFN, "TEST1")
- sub1_path = join(walk_path, "SUB1")
- sub11_path = join(sub1_path, "SUB11")
- sub2_path = join(walk_path, "SUB2")
- tmp1_path = join(walk_path, "tmp1")
- tmp2_path = join(sub1_path, "tmp2")
- tmp3_path = join(sub2_path, "tmp3")
- link_path = join(sub2_path, "link")
- t2_path = join(support.TESTFN, "TEST2")
- tmp4_path = join(support.TESTFN, "TEST2", "tmp4")
- link_path = join(sub2_path, "link")
- broken_link_path = join(sub2_path, "broken_link")
- # Create stuff.
- os.makedirs(sub11_path)
- os.makedirs(sub2_path)
- os.makedirs(t2_path)
- for path in tmp1_path, tmp2_path, tmp3_path, tmp4_path:
- f = open(path, "w")
- f.write("I'm " + path + " and proud of it. Blame test_os.\n")
- f.close()
- if support.can_symlink():
- os.symlink(os.path.abspath(t2_path), link_path)
- os.symlink('broken', broken_link_path, True)
- sub2_tree = (sub2_path, ["link"], ["broken_link", "tmp3"])
- else:
- sub2_tree = (sub2_path, [], ["tmp3"])
- # Walk top-down.
- all = list(os.walk(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], (walk_path, ["SUB1", "SUB2"], ["tmp1"]))
- self.assertEqual(all[1 + flipped], (sub1_path, ["SUB11"], ["tmp2"]))
- self.assertEqual(all[2 + flipped], (sub11_path, [], []))
- self.assertEqual(all[3 - 2 * flipped], sub2_tree)
- # Prune the search.
- all = []
- for root, dirs, files in os.walk(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], (walk_path, ["SUB2"], ["tmp1"]))
- all[1][-1].sort()
- self.assertEqual(all[1], sub2_tree)
- # Walk bottom-up.
- all = list(os.walk(walk_path, topdown=False))
- self.assertEqual(len(all), 4)
- # 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], (walk_path, ["SUB1", "SUB2"], ["tmp1"]))
- self.assertEqual(all[flipped], (sub11_path, [], []))
- self.assertEqual(all[flipped + 1], (sub1_path, ["SUB11"], ["tmp2"]))
- self.assertEqual(all[2 - 2 * flipped], sub2_tree)
- if support.can_symlink():
- # Walk, following symlinks.
- for root, dirs, files in os.walk(walk_path, followlinks=True):
- if root == link_path:
- self.assertEqual(dirs, [])
- self.assertEqual(files, ["tmp4"])
- break
- else:
- self.fail("Didn't follow symlink with followlinks=True")
- def tearDown(self):
- # Tear everything down. This is a decent use for bottom-up on
- # Windows, which doesn't have a recursive delete command. The
- # (not so) subtlety is that rmdir will fail unless the dir's
- # kids are removed first, so bottom up is essential.
- for root, dirs, files in os.walk(support.TESTFN, topdown=False):
- for name in files:
- os.remove(os.path.join(root, name))
- for name in dirs:
- dirname = os.path.join(root, name)
- if not os.path.islink(dirname):
- os.rmdir(dirname)
- else:
- os.remove(dirname)
- os.rmdir(support.TESTFN)
- @unittest.skipUnless(hasattr(os, 'fwalk'), "Test needs os.fwalk()")
- class FwalkTests(WalkTests):
- """Tests for os.fwalk()."""
- 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)
- def tearDown(self):
- # cleanup
- for root, dirs, files, rootfd in os.fwalk(support.TESTFN, topdown=False):
- for name in files:
- os.unlink(name, dir_fd=rootfd)
- for name in dirs:
- st = os.stat(name, dir_fd=rootfd, follow_symlinks=False)
- if stat.S_ISDIR(st.st_mode):
- os.rmdir(name, dir_fd=rootfd)
- else:
- os.unlink(name, dir_fd=rootfd)
- os.rmdir(support.TESTFN)
- 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)
- self.assertRaises(OSError, os.makedirs, path, 0o776, exist_ok=True)
- os.makedirs(path, mode=mode, exist_ok=True)
- os.umask(old_mask)
- 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)
- with self.assertRaises(OSError):
- # Should fail 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)
- 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)
- with open(os.path.join(dira, 'file.txt'), 'w') as f:
- f.write('text')
- 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)
- with open(os.path.join(dirb, 'file.txt'), 'w') as f:
- f.write('text')
- 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') 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)
- @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)
- @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 = mock_execv
- os.execve = mock_execve
- if defpath is not None:
- os.defpath = defpath
- yield calls
- finally:
- os.execv = orig_execv
- os.execve = orig_execve
- os.defpath = orig_defpath
- class ExecTests(unittest.TestCase):
- @unittest.skipIf(USING_LINUXTHREADS,
- "avoid triggering a linuxthreads bug: see issue #4970")
- def test_execvpe_with_bad_program(self):
- self.assertRaises(OSError, os.execvpe, 'no such app-',
- ['no such app-'], None)
- def test_execvpe_with_bad_arglist(self):
- self.assertRaises(ValueError, os.execvpe, 'notepad', [], None)
- @unittest.skipUnless(hasattr(os, '_execvpe'),
- "No internal os._execvpe function to test.")
- def _test_internal_execvpe(self, test_type):
- program_path = os.sep + 'absolutepath'
- if test_type is bytes:
- program = b'executable'
- fullpath = os.path.join(os.fsencode(program_path), program)
- native_fullpath = fullpath
- arguments = [b'progname', 'arg1', 'arg2']
- else:
- program = 'executable'
- arguments = ['progname', 'arg1', 'arg2']
- fullpath = os.path.join(program_path, program)
- if os.name != "nt":
- native_fullpath = os.fsencode(fullpath)
- else:
- native_fullpath = fullpath
- env = {'spam': 'beans'}
- # test os._execvpe() with an absolute path
- with _execvpe_mockup() as calls:
- self.assertRaises(RuntimeError,
- os._execvpe, fullpath, arguments)
- self.assertEqual(len(calls), 1)
- self.assertEqual(calls[0], ('execv', fullpath, (arguments,)))
- # test os._execvpe() with a relative path:
- # os.get_exec_path() returns defpath
- with _execvpe_mockup(defpath=program_path) as calls:
- self.assertRaises(OSError,
- os._execvpe, program, arguments, env=env)
- self.assertEqual(len(calls), 1)
- self.assertSequenceEqual(calls[0],
- ('execve', native_fullpath, (arguments, env)))
- # test os._execvpe() with a relative path:
- # os.get_exec_path() reads the 'PATH' variable
- with _execvpe_mockup() as calls:
- env_path = env.copy()
- if test_type is bytes:
- env_path[b'PATH'] = program_path
- else:
- env_path['PATH'] = program_path
- self.assertRaises(OSError,
- os._execvpe, program, arguments, env=env_path)
- self.assertEqual(len(calls), 1)
- self.assertSequenceEqual(calls[0],
- ('execve', native_fullpath, (arguments, env_path)))
- def test_internal_execvpe_str(self):
- self._test_internal_execvpe(str)
- if os.name != "nt":
- self._test_internal_execvpe(bytes)
- class Win32ErrorTests(unittest.TestCase):
- def test_rename(self):
- self.assertRaises(WindowsError, os.rename, support.TESTFN, support.TESTFN+".bak")
- def test_remove(self):
- self.assertRaises(WindowsError, os.remove, support.TESTFN)
- def test_chdir(self):
- self.assertRaises(WindowsError, os.chdir, support.TESTFN)
- def test_mkdir(self):
- f = open(support.TESTFN, "w")
- try:
- self.assertRaises(WindowsError, os.mkdir, support.TESTFN)
- finally:
- f.close()
- os.unlink(support.TESTFN)
- def test_utime(self):
- self.assertRaises(WindowsError, os.utime, support.TESTFN, None)
- def test_chmod(self):
- self.assertRaises(WindowsError, os.chmod, support.TESTFN, 0)
- class TestInvalidFD(unittest.TestCase):
- singles = ["fchdir", "dup", "fdopen", "fdatasync", "fstat",
- "fstatvfs", "fsync", "tcgetpgrp", "ttyname"]
- #singles.append("close")
- #We omit close because it doesn'r raise an exception on some platforms
- def get_single(f):
- def helper(self):
- if hasattr(os, f):
- self.check(getattr(os, f))
- return helper
- for f in singles:
- locals()["test_"+f] = get_single(f)
- def check(self, f, *args):
- try:
- f(support.make_bad_fd(), *args)
- except OSError as e:
- self.assertEqual(e.errno, errno.EBADF)
- else:
- self.fail("%r didn't raise a OSError with a bad file descriptor"
- % f)
- def test_isatty(self):
- if hasattr(os, "isatty"):
- self.assertEqual(os.isatty(support.make_bad_fd()), False)
- def test_closerange(self):
- if hasattr(os, "closerange"):
- fd = support.make_bad_fd()
- # Make sure none of the descriptors we are about to close are
- # currently valid (issue 6542).
- for i in range(10):
- try: os.fstat(fd+i)
- except OSError:
- pass
- else:
- break
- if i < 2:
- raise unittest.SkipTest(
- "Unable to acquire a range of invalid file descriptors")
- self.assertEqual(os.closerange(fd, fd + i-1), None)
- def test_dup2(self):
- if hasattr(os, "dup2"):
- self.check(os.dup2, 20)
- def test_fchmod(self):
- if hasattr(os, "fchmod"):
- self.check(os.fchmod, 0)
- def test_fchown(self):
- if hasattr(os, "fchown"):
- self.check(os.fchown, -1, -1)
- def test_fpathconf(self):
- if hasattr(os, "fpathconf"):
- self.check(os.pathconf, "PC_NAME_MAX")
- self.check(os.fpathconf, "PC_NAME_MAX")
- def test_ftruncate(self):
- if hasattr(os, "ftruncate"):
- self.check(os.truncate, 0)
- self.check(os.ftruncate, 0)
- def test_lseek(self):
- if hasattr(os, "lseek"):
- self.check(os.lseek, 0, 0)
- def test_read(self):
- if hasattr(os, "read"):
- self.check(os.read, 1)
- def test_tcsetpgrpt(self):
- if hasattr(os, "tcsetpgrp"):
- self.check(os.tcsetpgrp, 0)
- def test_write(self):
- if hasattr(os, "write"):
- self.check(os.write, b" ")
- class LinkTests(unittest.TestCase):
- def setUp(self):
- self.file1 = support.TESTFN
- self.file2 = os.path.join(support.TESTFN + "2")
- def tearDown(self):
- for file in (self.file1, self.file2):
- if os.path.exists(file):
- os.unlink(file)
- def _test_link(self, file1, file2):
- with open(file1, "w") as f1:
- f1.write("test")
- with warnings.catch_warnings():
- warnings.simplefilter("ignore", DeprecationWarning)
- os.link(file1, file2)
- with open(file1, "r") as f1, open(file2, "r") as f2:
- self.assertTrue(os.path.sameopenfile(f1.fileno(), f2.fileno()))
- def test_link(self):
- self._test_link(self.file1, self.file2)
- def test_link_bytes(self):
- self._test_link(bytes(self.file1, sys.getfilesystemencoding()),
- bytes(self.file2, sys.getfilesystemencoding()))
- def test_unicode_name(self):
- try:
- os.fsencode("\xf1")
- except UnicodeError:
- raise unittest.SkipTest("Unable to encode for this platform.")
- self.file1 += "\xf1"
- self.file2 = self.file1 + "2"
- self._test_link(self.file1, self.file2)
- if sys.platform != 'win32':
- class Win32ErrorTests(unittest.TestCase):
- pass
- class PosixUidGidTests(unittest.TestCase):
- if hasattr(os, 'setuid'):
- def test_setuid(self):
- if os.getuid() != 0:
- self.assertRaises(os.error, os.setuid, 0)
- self.assertRaises(OverflowError, os.setuid, 1<<32)
- if hasattr(os, 'setgid'):
- def test_setgid(self):
- if os.getuid() != 0 and not HAVE_WHEEL_GROUP:
- self.assertRaises(os.error, os.setgid, 0)
- self.assertRaises(OverflowError, os.setgid, 1<<32)
- if hasattr(os, 'seteuid'):
- def test_seteuid(self):
- if os.getuid() != 0:
- self.assertRaises(os.error, os.seteuid, 0)
- self.assertRaises(OverflowError, os.seteuid, 1<<32)
- if hasattr(os, 'setegid'):
- def test_setegid(self):
- if os.getuid() != 0 and not HAVE_WHEEL_GROUP:
- self.assertRaises(os.error, os.setegid, 0)
- self.assertRaises(OverflowError, os.setegid, 1<<32)
- if hasattr(os, 'setreuid'):
- def test_setreuid(self):
- if os.getuid() != 0:
- self.assertRaises(os.error, os.setreuid, 0, 0)
- self.assertRaises(OverflowError, os.setreuid, 1<<32, 0)
- self.assertRaises(OverflowError, os.setreuid, 0, 1<<32)
- def test_setreuid_neg1(self):
- # Needs to accept -1. We run this in a subprocess to avoid
- # altering the test runner's process state (issue8045).
- subprocess.check_call([
- sys.executable, '-c',
- 'import os,sys;os.setreuid(-1,-1);sys.exit(0)'])
- if hasattr(os, 'setregid'):
- def test_setregid(self):
- if os.getuid() != 0 and not HAVE_WHEEL_GROUP:
- self.assertRaises(os.error, os.setregid, 0, 0)
- self.assertRaises(OverflowError, os.setregid, 1<<32, 0)
- self.assertRaises(OverflowError, os.setregid, 0, 1<<32)
- def test_setregid_neg1(self):
- # Needs to accept -1. We run this in a subprocess to avoid
- # altering the test runner's process state (issue8045).
- subprocess.check_call([
- sys.executable, '-c',
- 'import os,sys;os.setregid(-1,-1);sys.exit(0)'])
- class Pep383Tests(unittest.TestCase):
- def setUp(self):
- if support.TESTFN_UNENCODABLE:
- self.dir = support.TESTFN_UNENCODABLE
- elif support.TESTFN_NONASCII:
- self.dir = support.TESTFN_NONASCII
- else:
- self.dir = support.TESTFN
- self.bdir = os.fsencode(self.dir)
- bytesfn = []
- def add_filename(fn):
- try:
- fn = os.fsencode(fn)
- except UnicodeEncodeError:
- return
- bytesfn.append(fn)
- add_filename(support.TESTFN_UNICODE)
- if support.TESTFN_UNENCODABLE:
- add_filename(support.TE