mirror of
https://github.com/tiennm99/serena.git
synced 2026-10-13 14:16:21 +00:00
Add file existence check and improve encoding handling in read_file method
This commit is contained in:
1 parent
5cb291f096
commit
1a63192082
1 file changed
+7
-1
@@ -117,7 +117,10 @@ class FileUtils:
|
||||
"""
|
||||
Reads the file at the given path and returns the contents as a string.
|
||||
"""
|
||||
encodings = ["utf-8-sig", "utf-16"]
|
||||
if not os.path.exists(file_path):
|
||||
logger.log(f"File read '{file_path}' failed: File does not exist.", logging.ERROR)
|
||||
raise MultilspyException(f"File read '{file_path}' failed: File does not exist.")
|
||||
encodings = ["utf-8-sig", "utf-16", "utf-8", "latin-1"]
|
||||
try:
|
||||
for encoding in encodings:
|
||||
try:
|
||||
@@ -125,6 +128,9 @@ class FileUtils:
|
||||
return inp_file.read()
|
||||
except UnicodeError:
|
||||
continue
|
||||
# Try system default encoding as a last resort
|
||||
with open(file_path, "r") as inp_file:
|
||||
return inp_file.read()
|
||||
except Exception as exc:
|
||||
logger.log(f"File read '{file_path}' failed: {exc}", logging.ERROR)
|
||||
raise MultilspyException("File read failed.") from None
|
||||
|
||||
Reference in new issue
Block a user