cpython: 4335d898be59 (original) (raw)
--- a/Lib/test/test_os.py +++ b/Lib/test/test_os.py @@ -2,33 +2,34 @@
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 asynchat +import asyncore +import codecs +import contextlib +import decimal import errno +import fractions import getpass -import unittest -import warnings -import sys -import signal -import subprocess -import time -import shutil -from test import support -import contextlib +import itertools +import locale +import math import mmap +import os +import pickle import platform import re -import uuid -import asyncore -import asynchat +import shutil +import signal import socket -import itertools import stat -import locale -import codecs -import decimal -import fractions -import pickle +import subprocess +import sys import sysconfig +import time +import unittest +import uuid +import warnings +from test import support try: import threading except ImportError: @@ -70,16 +71,6 @@ root_in_posix = False if hasattr(os, 'geteuid'): root_in_posix = (os.geteuid() == 0) -with warnings.catch_warnings():
-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
@@ -223,15 +214,10 @@ class FileTests(unittest.TestCase):
Test attributes on return values from os.stat family.
class StatAttributeTests(unittest.TestCase): def setUp(self):
os.mkdir(support.TESTFN)[](#l1.78)
self.fname = os.path.join(support.TESTFN, "f1")[](#l1.79)
f = open(self.fname, 'wb')[](#l1.80)
f.write(b"ABC")[](#l1.81)
f.close()[](#l1.82)
self.fname = support.TESTFN[](#l1.87)
self.addCleanup(support.unlink, self.fname)[](#l1.88)
with open(self.fname, 'wb') as fp:[](#l1.89)
fp.write(b"ABC")[](#l1.90)
@unittest.skipUnless(hasattr(os, 'stat'), 'test needs os.stat()') def check_stat_attributes(self, fname): @@ -383,179 +369,6 @@ class StatAttributeTests(unittest.TestCa unpickled = pickle.loads(p) self.assertEqual(result, unpickled)
- def test_utime_dir(self):
delta = 1000000[](#l1.99)
st = os.stat(support.TESTFN)[](#l1.100)
# round to int, because some systems may support sub-second[](#l1.101)
# time stamps in stat, but not in utime.[](#l1.102)
os.utime(support.TESTFN, (st.st_atime, int(st.st_mtime-delta)))[](#l1.103)
st2 = os.stat(support.TESTFN)[](#l1.104)
self.assertEqual(st2.st_mtime, int(st.st_mtime-delta))[](#l1.105)
- def _test_utime(self, filename, attr, utime, delta):
# Issue #13327 removed the requirement to pass None as the[](#l1.108)
# second argument. Check that the previous methods of passing[](#l1.109)
# a time tuple or None work in addition to no argument.[](#l1.110)
st0 = os.stat(filename)[](#l1.111)
# Doesn't set anything new, but sets the time tuple way[](#l1.112)
utime(filename, (attr(st0, "st_atime"), attr(st0, "st_mtime")))[](#l1.113)
# Setting the time to the time you just read, then reading again,[](#l1.114)
# should always return exactly the same times.[](#l1.115)
st1 = os.stat(filename)[](#l1.116)
self.assertEqual(attr(st0, "st_mtime"), attr(st1, "st_mtime"))[](#l1.117)
self.assertEqual(attr(st0, "st_atime"), attr(st1, "st_atime"))[](#l1.118)
# Set to the current time in the old explicit way.[](#l1.119)
os.utime(filename, None)[](#l1.120)
st2 = os.stat(support.TESTFN)[](#l1.121)
# Set to the current time in the new way[](#l1.122)
os.utime(filename)[](#l1.123)
st3 = os.stat(filename)[](#l1.124)
self.assertAlmostEqual(attr(st2, "st_mtime"), attr(st3, "st_mtime"), delta=delta)[](#l1.125)
- def test_utime(self):
def utime(file, times):[](#l1.128)
return os.utime(file, times)[](#l1.129)
self._test_utime(self.fname, getattr, utime, 10)[](#l1.130)
self._test_utime(support.TESTFN, getattr, utime, 10)[](#l1.131)
- def _test_utime_ns(self, set_times_ns, test_dir=True):
def getattr_ns(o, attr):[](#l1.135)
return getattr(o, attr + "_ns")[](#l1.136)
ten_s = 10 * 1000 * 1000 * 1000[](#l1.137)
self._test_utime(self.fname, getattr_ns, set_times_ns, ten_s)[](#l1.138)
if test_dir:[](#l1.139)
self._test_utime(support.TESTFN, getattr_ns, set_times_ns, ten_s)[](#l1.140)
- def test_utime_ns(self):
def utime_ns(file, times):[](#l1.143)
return os.utime(file, ns=times)[](#l1.144)
self._test_utime_ns(utime_ns)[](#l1.145)
- requires_utime_dir_fd = unittest.skipUnless(
os.utime in os.supports_dir_fd,[](#l1.148)
"dir_fd support for utime required for this test.")[](#l1.149)
- requires_utime_fd = unittest.skipUnless(
os.utime in os.supports_fd,[](#l1.151)
"fd support for utime required for this test.")[](#l1.152)
- requires_utime_nofollow_symlinks = unittest.skipUnless(
os.utime in os.supports_follow_symlinks,[](#l1.154)
"follow_symlinks support for utime required for this test.")[](#l1.155)
- @requires_utime_nofollow_symlinks
- def test_lutimes_ns(self):
def lutimes_ns(file, times):[](#l1.159)
return os.utime(file, ns=times, follow_symlinks=False)[](#l1.160)
self._test_utime_ns(lutimes_ns)[](#l1.161)
- @requires_utime_fd
- def test_futimes_ns(self):
def futimes_ns(file, times):[](#l1.165)
with open(file, "wb") as f:[](#l1.166)
os.utime(f.fileno(), ns=times)[](#l1.167)
self._test_utime_ns(futimes_ns, test_dir=False)[](#l1.168)
- def _utime_invalid_arguments(self, name, arg):
with self.assertRaises(ValueError):[](#l1.171)
getattr(os, name)(arg, (5, 5), ns=(5, 5))[](#l1.172)
- def test_utime_invalid_arguments(self):
self._utime_invalid_arguments('utime', self.fname)[](#l1.175)
- @unittest.skipUnless(stat_supports_subsecond,
"os.stat() doesn't has a subsecond resolution")[](#l1.179)
- def _test_utime_subsecond(self, set_time_func):
asec, amsec = 1, 901[](#l1.181)
atime = asec + amsec * 1e-3[](#l1.182)
msec, mmsec = 2, 901[](#l1.183)
mtime = msec + mmsec * 1e-3[](#l1.184)
filename = self.fname[](#l1.185)
os.utime(filename, (0, 0))[](#l1.186)
set_time_func(filename, atime, mtime)[](#l1.187)
with warnings.catch_warnings():[](#l1.188)
warnings.simplefilter("ignore", DeprecationWarning)[](#l1.189)
os.stat_float_times(True)[](#l1.190)
st = os.stat(filename)[](#l1.191)
self.assertAlmostEqual(st.st_atime, atime, places=3)[](#l1.192)
self.assertAlmostEqual(st.st_mtime, mtime, places=3)[](#l1.193)
- def test_utime_subsecond(self):
def set_time(filename, atime, mtime):[](#l1.196)
os.utime(filename, (atime, mtime))[](#l1.197)
self._test_utime_subsecond(set_time)[](#l1.198)
- @requires_utime_fd
- def test_futimes_subsecond(self):
def set_time(filename, atime, mtime):[](#l1.202)
with open(filename, "wb") as f:[](#l1.203)
os.utime(f.fileno(), times=(atime, mtime))[](#l1.204)
self._test_utime_subsecond(set_time)[](#l1.205)
- @requires_utime_fd
- def test_futimens_subsecond(self):
def set_time(filename, atime, mtime):[](#l1.209)
with open(filename, "wb") as f:[](#l1.210)
os.utime(f.fileno(), times=(atime, mtime))[](#l1.211)
self._test_utime_subsecond(set_time)[](#l1.212)
- @requires_utime_dir_fd
- def test_futimesat_subsecond(self):
def set_time(filename, atime, mtime):[](#l1.216)
dirname = os.path.dirname(filename)[](#l1.217)
dirfd = os.open(dirname, os.O_RDONLY)[](#l1.218)
try:[](#l1.219)
os.utime(os.path.basename(filename), dir_fd=dirfd,[](#l1.220)
times=(atime, mtime))[](#l1.221)
finally:[](#l1.222)
os.close(dirfd)[](#l1.223)
self._test_utime_subsecond(set_time)[](#l1.224)
- @requires_utime_nofollow_symlinks
- def test_lutimes_subsecond(self):
def set_time(filename, atime, mtime):[](#l1.228)
os.utime(filename, (atime, mtime), follow_symlinks=False)[](#l1.229)
self._test_utime_subsecond(set_time)[](#l1.230)
- @requires_utime_dir_fd
- def test_utimensat_subsecond(self):
def set_time(filename, atime, mtime):[](#l1.234)
dirname = os.path.dirname(filename)[](#l1.235)
dirfd = os.open(dirname, os.O_RDONLY)[](#l1.236)
try:[](#l1.237)
os.utime(os.path.basename(filename), dir_fd=dirfd,[](#l1.238)
times=(atime, mtime))[](#l1.239)
finally:[](#l1.240)
os.close(dirfd)[](#l1.241)
self._test_utime_subsecond(set_time)[](#l1.242)
Restrict tests to Win32, since there is no guarantee other
systems support centiseconds
- def get_file_system(path):
if sys.platform == 'win32':[](#l1.247)
root = os.path.splitdrive(os.path.abspath(path))[0] + '\\'[](#l1.248)
import ctypes[](#l1.249)
kernel32 = ctypes.windll.kernel32[](#l1.250)
buf = ctypes.create_unicode_buffer("", 100)[](#l1.251)
if kernel32.GetVolumeInformationW(root, None, 0, None, None, None, buf, len(buf)):[](#l1.252)
return buf.value[](#l1.253)
- @unittest.skipUnless(sys.platform == "win32", "Win32 specific tests")
- @unittest.skipUnless(get_file_system(support.TESTFN) == "NTFS",
"requires NTFS")[](#l1.257)
- def test_1565150(self):
t1 = 1159195039.25[](#l1.259)
os.utime(self.fname, (t1, t1))[](#l1.260)
self.assertEqual(os.stat(self.fname).st_mtime, t1)[](#l1.261)
- @unittest.skipUnless(sys.platform == "win32", "Win32 specific tests")
- @unittest.skipUnless(get_file_system(support.TESTFN) == "NTFS",
"requires NTFS")[](#l1.265)
- def test_large_time(self):
t1 = 5000000000 # some day in 2128[](#l1.267)
os.utime(self.fname, (t1, t1))[](#l1.268)
self.assertEqual(os.stat(self.fname).st_mtime, t1)[](#l1.269)
- @unittest.skipUnless(sys.platform == "win32", "Win32 specific tests") def test_1686475(self): # Verify that an open file can be stat'ed @@ -596,12 +409,206 @@ class StatAttributeTests(unittest.TestCa 0) # test directory st_file_attributes (FILE_ATTRIBUTE_DIRECTORY set)
result = os.stat(support.TESTFN)[](#l1.278)
dirname = support.TESTFN + "dir"[](#l1.279)
os.mkdir(dirname)[](#l1.280)
self.addCleanup(os.rmdir, dirname)[](#l1.281)
result = os.stat(dirname)[](#l1.283) self.check_file_attributes(result)[](#l1.284) self.assertEqual([](#l1.285) result.st_file_attributes & stat.FILE_ATTRIBUTE_DIRECTORY,[](#l1.286) stat.FILE_ATTRIBUTE_DIRECTORY)[](#l1.287)
+ +class UtimeTests(unittest.TestCase):
- def setUp(self):
self.dirname = support.TESTFN[](#l1.292)
self.fname = os.path.join(self.dirname, "f1")[](#l1.293)
self.addCleanup(support.rmtree, self.dirname)[](#l1.295)
os.mkdir(self.dirname)[](#l1.296)
with open(self.fname, 'wb') as fp:[](#l1.297)
fp.write(b"ABC")[](#l1.298)
# ensure that st_atime and st_mtime are float[](#l1.300)
with warnings.catch_warnings():[](#l1.301)
warnings.simplefilter("ignore", DeprecationWarning)[](#l1.302)
old_state = os.stat_float_times(-1)[](#l1.304)
self.addCleanup(os.stat_float_times, old_state)[](#l1.305)
os.stat_float_times(True)[](#l1.307)
- def support_subsecond(self, filename):
# Heuristic to check if the filesystem supports timestamp with[](#l1.310)
# subsecond resolution: check if float and int timestamps are different[](#l1.311)
st = os.stat(filename)[](#l1.312)
return ((st.st_atime != st[7])[](#l1.313)
or (st.st_mtime != st[8])[](#l1.314)
or (st.st_ctime != st[9]))[](#l1.315)
- def _test_utime(self, set_time, filename=None):
if not filename:[](#l1.318)
filename = self.fname[](#l1.319)
support_subsecond = self.support_subsecond(filename)[](#l1.321)
if support_subsecond:[](#l1.322)
# Timestamp with a resolution of 1 microsecond (10^-6).[](#l1.323)
#[](#l1.324)
# The resolution of the C internal function used by os.utime()[](#l1.325)
# depends on the platform: 1 sec, 1 us, 1 ns. Writing a portable[](#l1.326)
# test with a resolution of 1 ns requires more work:[](#l1.327)
# see the issue #15745.[](#l1.328)
atime_ns = 1002003000 # 1.002003 seconds[](#l1.329)
mtime_ns = 4005006000 # 4.005006 seconds[](#l1.330)
else:[](#l1.331)
# use a resolution of 1 second[](#l1.332)
atime_ns = 5 * 10**9[](#l1.333)
mtime_ns = 8 * 10**9[](#l1.334)
set_time(filename, (atime_ns, mtime_ns))[](#l1.336)
st = os.stat(filename)[](#l1.337)
if support_subsecond:[](#l1.339)
self.assertAlmostEqual(st.st_atime, atime_ns * 1e-9, delta=1e-6)[](#l1.340)
self.assertAlmostEqual(st.st_mtime, mtime_ns * 1e-9, delta=1e-6)[](#l1.341)
else:[](#l1.342)
self.assertEqual(st.st_atime, atime_ns * 1e-9)[](#l1.343)
self.assertEqual(st.st_mtime, mtime_ns * 1e-9)[](#l1.344)
self.assertEqual(st.st_atime_ns, atime_ns)[](#l1.345)
self.assertEqual(st.st_mtime_ns, mtime_ns)[](#l1.346)
- def test_utime(self):
def set_time(filename, ns):[](#l1.349)
# test the ns keyword parameter[](#l1.350)
os.utime(filename, ns=ns)[](#l1.351)
self._test_utime(set_time)[](#l1.352)
- @staticmethod
- def ns_to_sec(ns):
# Convert a number of nanosecond (int) to a number of seconds (float).[](#l1.356)
# Round towards infinity by adding 0.5 nanosecond to avoid rounding[](#l1.357)
# issue, os.utime() rounds towards minus infinity.[](#l1.358)
return (ns * 1e-9) + 0.5e-9[](#l1.359)
- def test_utime_by_indexed(self):
# pass times as floating point seconds as the second indexed parameter[](#l1.362)
def set_time(filename, ns):[](#l1.363)
atime_ns, mtime_ns = ns[](#l1.364)
atime = self.ns_to_sec(atime_ns)[](#l1.365)
mtime = self.ns_to_sec(mtime_ns)[](#l1.366)
# test utimensat(timespec), utimes(timeval), utime(utimbuf)[](#l1.367)
# or utime(time_t)[](#l1.368)
os.utime(filename, (atime, mtime))[](#l1.369)
self._test_utime(set_time)[](#l1.370)
- def test_utime_by_times(self):
def set_time(filename, ns):[](#l1.373)
atime_ns, mtime_ns = ns[](#l1.374)
atime = self.ns_to_sec(atime_ns)[](#l1.375)
mtime = self.ns_to_sec(mtime_ns)[](#l1.376)
# test the times keyword parameter[](#l1.377)
os.utime(filename, times=(atime, mtime))[](#l1.378)
self._test_utime(set_time)[](#l1.379)
- @unittest.skipUnless(os.utime in os.supports_follow_symlinks,
"follow_symlinks support for utime required "[](#l1.382)
"for this test.")[](#l1.383)
- def test_utime_nofollow_symlinks(self):
def set_time(filename, ns):[](#l1.385)
# use follow_symlinks=False to test utimensat(timespec)[](#l1.386)
# or lutimes(timeval)[](#l1.387)
os.utime(filename, ns=ns, follow_symlinks=False)[](#l1.388)
self._test_utime(set_time)[](#l1.389)
- @unittest.skipUnless(os.utime in os.supports_fd,
"fd support for utime required for this test.")[](#l1.392)
- def test_utime_fd(self):
def set_time(filename, ns):[](#l1.394)
with open(filename, 'wb') as fp:[](#l1.395)
# use a file descriptor to test futimens(timespec)[](#l1.396)
# or futimes(timeval)[](#l1.397)
os.utime(fp.fileno(), ns=ns)[](#l1.398)
self._test_utime(set_time)[](#l1.399)
- @unittest.skipUnless(os.utime in os.supports_dir_fd,
"dir_fd support for utime required for this test.")[](#l1.402)
- def test_utime_dir_fd(self):
def set_time(filename, ns):[](#l1.404)
dirname, name = os.path.split(filename)[](#l1.405)
dirfd = os.open(dirname, os.O_RDONLY)[](#l1.406)
try:[](#l1.407)
# pass dir_fd to test utimensat(timespec) or futimesat(timeval)[](#l1.408)
os.utime(name, dir_fd=dirfd, ns=ns)[](#l1.409)
finally:[](#l1.410)
os.close(dirfd)[](#l1.411)
self._test_utime(set_time)[](#l1.412)
- def test_utime_directory(self):
def set_time(filename, ns):[](#l1.415)
# test calling os.utime() on a directory[](#l1.416)
os.utime(filename, ns=ns)[](#l1.417)
self._test_utime(set_time, filename=self.dirname)[](#l1.418)
- def _test_utime_current(self, set_time):
# Get the system clock[](#l1.421)
current = time.time()[](#l1.422)
# Call os.utime() to set the timestamp to the current system clock[](#l1.424)
set_time(self.fname)[](#l1.425)
if not self.support_subsecond(self.fname):[](#l1.427)
delta = 1.0[](#l1.428)
else:[](#l1.429)
# On Windows, the usual resolution of time.time() is 15.6 ms[](#l1.430)
delta = 0.020[](#l1.431)
st = os.stat(self.fname)[](#l1.432)
msg = ("st_time=%r, current=%r, dt=%r"[](#l1.433)
% (st.st_mtime, current, st.st_mtime - current))[](#l1.434)
self.assertAlmostEqual(st.st_mtime, current,[](#l1.435)
delta=delta, msg=msg)[](#l1.436)
- def test_utime_current(self):
def set_time(filename):[](#l1.439)
# Set to the current time in the new way[](#l1.440)
os.utime(self.fname)[](#l1.441)
self._test_utime_current(set_time)[](#l1.442)
- def test_utime_current_old(self):
def set_time(filename):[](#l1.445)
# Set to the current time in the old explicit way.[](#l1.446)
os.utime(self.fname, None)[](#l1.447)
self._test_utime_current(set_time)[](#l1.448)
- def get_file_system(self, path):
if sys.platform == 'win32':[](#l1.451)
root = os.path.splitdrive(os.path.abspath(path))[0] + '\\'[](#l1.452)
import ctypes[](#l1.453)
kernel32 = ctypes.windll.kernel32[](#l1.454)
buf = ctypes.create_unicode_buffer("", 100)[](#l1.455)
ok = kernel32.GetVolumeInformationW(root, None, 0,[](#l1.456)
None, None, None,[](#l1.457)
buf, len(buf))[](#l1.458)
if ok:[](#l1.459)
return buf.value[](#l1.460)
# return None if the filesystem is unknown[](#l1.461)
- def test_large_time(self):
# Many filesystems are limited to the year 2038. At least, the test[](#l1.464)
# pass with NTFS filesystem.[](#l1.465)
if self.get_file_system(self.dirname) != "NTFS":[](#l1.466)
self.skipTest("requires NTFS")[](#l1.467)
large = 5000000000 # some day in 2128[](#l1.469)
os.utime(self.fname, (large, large))[](#l1.470)
self.assertEqual(os.stat(self.fname).st_mtime, large)[](#l1.471)
- def test_utime_invalid_arguments(self):
# seconds and nanoseconds parameters are mutually exclusive[](#l1.474)
with self.assertRaises(ValueError):[](#l1.475)
os.utime(self.fname, (5, 5), ns=(5, 5))[](#l1.476)
+ + from test import mapping_tests class EnvironTests(mapping_tests.BasicTestMappingProtocol):