pathlib refactor platform.base

This commit is contained in:
Thomas Waldmann 2025-06-10 12:19:43 +02:00
parent a0f73d7a61
commit 4ff49a6e91
No known key found for this signature in database
GPG key ID: 243ACFA951F78E01

View file

@ -2,6 +2,7 @@ import errno
import os
import socket
import uuid
from pathlib import Path
from ..helpers import safe_unlink
from ..platformflags import is_win32
@ -102,7 +103,7 @@ def sync_dir(path):
# Opening directories is not supported on windows.
# TODO: do we need to handle this in some other way?
return
fd = os.open(path, os.O_RDONLY)
fd = os.open(str(path), os.O_RDONLY)
try:
os.fsync(fd)
except OSError as os_error:
@ -164,7 +165,7 @@ class SyncFile:
mode = "xb" if binary else "x" # x -> raise FileExists exception in open() if file exists already
self.path = path
if fd is None:
self.f = open(path, mode=mode) # python file object
self.f = open(str(path), mode=mode) # python file object
else:
self.f = os.fdopen(fd, mode=mode)
self.fd = self.f.fileno() # OS-level fd
@ -197,7 +198,7 @@ class SyncFile:
dirname = None
try:
dirname = os.path.dirname(self.path)
dirname = Path(self.path).parent
self.sync()
finally:
self.f.close()
@ -225,8 +226,9 @@ class SaveFile:
def __init__(self, path, binary=False):
self.binary = binary
self.path = path
self.dir = os.path.dirname(path)
self.tmp_prefix = os.path.basename(path) + "-"
path_obj = Path(path)
self.dir = str(path_obj.parent)
self.tmp_prefix = path_obj.name + "-"
self.tmp_fd = None # OS-level fd
self.tmp_fname = None # full path/filename corresponding to self.tmp_fd
self.f = None # python-file-like SyncFile