Skip to content

Commit 2d0ffc3

Browse files
authored
Merge pull request #479 from atollk/issue_477
Some setinfo fixes
2 parents ac8a91a + b1511d2 commit 2d0ffc3

File tree

5 files changed

+26
-15
lines changed

5 files changed

+26
-15
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1616
### Fixed
1717

1818
- Fixed performance bugs in `fs.copy.copy_dir_if_newer`. Test cases were adapted to catch those bugs in the future.
19+
- Fixed precision bug for timestamps in `fs.OSFS.setinfo`.
1920

2021

2122
## [2.4.13] - 2021-03-27

fs/info.py

+3
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ def is_writeable(self, namespace, key):
135135
When creating an `Info` object, you can add a ``_write`` key to
136136
each raw namespace that lists which keys are writable or not.
137137
138+
In general, this means they are compatible with the `setinfo`
139+
function of filesystem objects.
140+
138141
Arguments:
139142
namespace (str): A namespace identifier.
140143
key (str): A key within the namespace.

fs/osfs.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -665,10 +665,10 @@ def setinfo(self, path, info):
665665
if "details" in info:
666666
details = info["details"]
667667
if "accessed" in details or "modified" in details:
668-
_accessed = typing.cast(int, details.get("accessed"))
669-
_modified = typing.cast(int, details.get("modified", _accessed))
670-
accessed = int(_modified if _accessed is None else _accessed)
671-
modified = int(_modified)
668+
_accessed = typing.cast(float, details.get("accessed"))
669+
_modified = typing.cast(float, details.get("modified", _accessed))
670+
accessed = float(_modified if _accessed is None else _accessed)
671+
modified = float(_modified)
672672
if accessed is not None or modified is not None:
673673
with convert_os_errors("setinfo", path):
674674
os.utime(sys_path, (accessed, modified))

fs/test.py

+16-10
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import io
1313
import itertools
1414
import json
15-
import math
1615
import os
1716
import time
1817
import unittest
@@ -1171,15 +1170,21 @@ def test_removetree_root(self):
11711170

11721171
def test_setinfo(self):
11731172
self.fs.create("birthday.txt")
1174-
now = math.floor(time.time())
1173+
now = time.time()
11751174

11761175
change_info = {"details": {"accessed": now + 60, "modified": now + 60 * 60}}
11771176
self.fs.setinfo("birthday.txt", change_info)
1178-
new_info = self.fs.getinfo("birthday.txt", namespaces=["details"]).raw
1179-
if "accessed" in new_info.get("_write", []):
1180-
self.assertEqual(new_info["details"]["accessed"], now + 60)
1181-
if "modified" in new_info.get("_write", []):
1182-
self.assertEqual(new_info["details"]["modified"], now + 60 * 60)
1177+
new_info = self.fs.getinfo("birthday.txt", namespaces=["details"])
1178+
can_write_acccess = new_info.is_writeable("details", "accessed")
1179+
can_write_modified = new_info.is_writeable("details", "modified")
1180+
if can_write_acccess:
1181+
self.assertAlmostEqual(
1182+
new_info.get("details", "accessed"), now + 60, places=4
1183+
)
1184+
if can_write_modified:
1185+
self.assertAlmostEqual(
1186+
new_info.get("details", "modified"), now + 60 * 60, places=4
1187+
)
11831188

11841189
with self.assertRaises(errors.ResourceNotFound):
11851190
self.fs.setinfo("nothing", {})
@@ -1188,10 +1193,11 @@ def test_settimes(self):
11881193
self.fs.create("birthday.txt")
11891194
self.fs.settimes("birthday.txt", accessed=datetime(2016, 7, 5))
11901195
info = self.fs.getinfo("birthday.txt", namespaces=["details"])
1191-
writeable = info.get("details", "_write", [])
1192-
if "accessed" in writeable:
1196+
can_write_acccess = info.is_writeable("details", "accessed")
1197+
can_write_modified = info.is_writeable("details", "modified")
1198+
if can_write_acccess:
11931199
self.assertEqual(info.accessed, datetime(2016, 7, 5, tzinfo=pytz.UTC))
1194-
if "modified" in writeable:
1200+
if can_write_modified:
11951201
self.assertEqual(info.modified, datetime(2016, 7, 5, tzinfo=pytz.UTC))
11961202

11971203
def test_touch(self):

tests/test_osfs.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ def test_copy_preserve_time(self):
111111
self.assertTrue(self.fs.exists("bar/file.txt"))
112112

113113
dst_info = self.fs.getinfo("bar/file.txt", namespaces)
114-
self.assertEqual(dst_info.modified, src_info.modified)
114+
delta = dst_info.modified - src_info.modified
115+
self.assertAlmostEqual(delta.total_seconds(), 0, places=2)
115116

116117
@unittest.skipUnless(osfs.sendfile, "sendfile not supported")
117118
@unittest.skipIf(

0 commit comments

Comments
 (0)