0
我從dkfileutils
寫Path.touch測試:time.time()和file.getmtime()之間的關係?
class Path(str):
def touch(self, mode=0o666, exist_ok=True):
"""Create this file with the given access mode, if it doesn't exist.
(based on https://github.com/python/cpython/blob/master/Lib/pathlib.py)
"""
if exist_ok:
# First try to bump modification time
# Implementation note: GNU touch uses the UTIME_NOW option of
# the utimensat()/futimens() functions.
try:
os.utime(self, None)
except OSError:
# Avoid exception chaining
pass
else:
return
flags = os.O_CREAT | os.O_WRONLY
if not exist_ok:
flags |= os.O_EXCL
fd = os.open(self, flags, mode)
os.close(fd)
我曾嘗試以下的變化..
def test_touch():
before = time.time()
with open('a', 'w') as fp:
fp.write('a')
after = time.time()
assert before <= after
a = Path('a')
a_before_touch = a.getmtime()
assert before <= a_before_touch <= after
a.touch()
after_touch = time.time()
a_after_touch = a.getmtime()
assert a_before_touch <= a_after_touch
assert a_after_touch >= after
assert a_after_touch <= after_touch
但它不停地進行故障(https://travis-ci.org/datakortet/dkfileutils/builds),運行68:
> assert before <= a_before_touch <= after
E assert 1489532961.064958 <= 1489532961.064464
運行67
> assert a_after_touch > after
E assert 1489532462.412086 > 1489532462.412933
運行66
> assert a_after_touch > after
E assert 1489532286.6042855 > 1489532286.606766
我只是在假設了time.time()和file.getmtime傻()有關係嗎?