loguru icon indicating copy to clipboard operation
loguru copied to clipboard

ValueError when converting os.getxattr result to float in get_ctime_linux

Open JingofXin opened this issue 1 year ago • 4 comments

Actual Behavior: A ValueError is raised when attempting to convert the result of os.getxattr to a float. The output from os.getxattr appears to be corrupted or in an incorrect format.

Error Traceback:

Traceback (most recent call last):
  File "loguru/_ctime_functions.py", line 36, in get_ctime_linux
    return float(os.getxattr(filepath, b"user.loguru_crtime"))
ValueError: could not convert string to float: b'1721180095.264594\xdc:\xee|Q\x7f?WA\xf8\x8bdC\xe9V\xbdW\x19q\x80'

image

JingofXin avatar Nov 28 '24 09:11 JingofXin

Hi @JingofXin.

What is your OS version please?

Delgan avatar Nov 28 '24 09:11 Delgan

Hi @JingofXin.

What is your OS version please?

Ubuntu 22.04.3 LTS

JingofXin avatar Nov 28 '24 09:11 JingofXin

I’ve modified the code to handle the exception, which now bypasses the issue as follows:

def get_ctime_linux(filepath):
    try:
        return float(os.getxattr(filepath, b"user.loguru_crtime"))
    except Exception:
        return os.stat(filepath).st_mtime

JingofXin avatar Nov 28 '24 09:11 JingofXin

It's a real mystery how you end up with such weird value after the timestamp.

>>> print("\xdc:\xee|Q\x7f?WA\xf8\x8bdC\xe9V\xbdW\x19q\x80")
Ü:î|Q?WAødCéV½Wq

I don't think this is a bug in Loguru. Could it be that a program in your system is modifying the extended Linux attributes?

Do you have more context about how the problem occurs? Does it happen at the beginning or during execution? Is it systematic? Have you tried another log file?

What about the output of the following script:

import os

with open("random_file.txt", "w") as f:
    f.write("Hello, World!")

os.setxattr("random_file.txt", b"user.loguru_crtime", "1234567890.123456".encode("ascii"))

res = os.getxattr("random_file.txt", b"user.loguru_crtime")

print(res)

Delgan avatar Nov 28 '24 10:11 Delgan