Fix on incorrect rejection by the regex of the size_str

String like '1. MB' should be accepted
This commit is contained in:
KnugiHK
2025-06-01 12:17:21 +08:00
parent f89f53cf2d
commit 99213503c4

View File

@@ -118,10 +118,10 @@ def readable_to_bytes(size_str: str) -> int:
'YB': 1024**8
}
size_str = size_str.upper().strip()
match = re.fullmatch(r'^(\d+(\.\d+)?)\s*([KMGTPEZY]?B)?$', size_str)
if size_str.isnumeric():
# If the string is purely numeric, assume it's in bytes
return int(size_str)
match = re.fullmatch(r'^(\d+(\.\d*)?)\s*([KMGTPEZY]?B)?$', size_str)
if not match:
raise ValueError("Invalid size format for size_str. Expected format like '10MB', '1024GB', or '512'.")
unit = ''.join(filter(str.isalpha, size_str)).strip()