Skip to content

Commit

Permalink
Avoid runtime checks for missing "os.getxattr" / "os.setxattr"
Browse files Browse the repository at this point in the history
  • Loading branch information
Delgan committed Oct 26, 2019
1 parent 8bc1384 commit cb92f68
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
15 changes: 12 additions & 3 deletions loguru/_ctime_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,25 @@ def set_ctime(filepath, timestamp):
pass


else:
elif hasattr(os, "getxattr") and hasattr(os, "setxattr"):

def get_ctime(filepath):
try:
return float(os.getxattr(filepath, b"user.loguru_crtime"))
except (OSError, AttributeError):
except OSError:
return os.stat(filepath).st_mtime

def set_ctime(filepath, timestamp):
try:
os.setxattr(filepath, b"user.loguru_crtime", str(timestamp).encode("ascii"))
except (OSError, AttributeError):
except OSError:
pass


else:

def get_ctime(filepath):
return os.stat(filepath).st_mtime

def set_ctime(filepath, timestamp):
pass
7 changes: 2 additions & 5 deletions tests/test_filesink_rotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,20 +110,17 @@ def raising(*args, **kwargs):
raise OSError

monkeypatch.setattr(os, "name", "posix")
monkeypatch_filesystem(raising="st_birthtime", crtime="st_mtime")
monkeypatch.setattr(os, "setxattr", raising, raising=False)
monkeypatch.setattr(os, "getxattr", raising, raising=False)
monkeypatch_filesystem(raising="st_birthtime", crtime="st_mtime")


@pytest.fixture
def linux_xattr_attributeerror_filesystem(monkeypatch, monkeypatch_filesystem):
def raising(*args, **kwargs):
raise OSError

monkeypatch.setattr(os, "name", "posix")
monkeypatch_filesystem(raising="st_birthtime", crtime="st_mtime")
monkeypatch.delattr(os, "setxattr", raising=False)
monkeypatch.delattr(os, "getxattr", raising=False)
monkeypatch_filesystem(raising="st_birthtime", crtime="st_mtime")


def test_renaming(tmpdir):
Expand Down

0 comments on commit cb92f68

Please sign in to comment.