Skip to content

The only downloader you need. 下载器的集大成者。

refactor(sysio, task): 优化Windows系统调用错误处理机制, 简化任务载荷应用逻辑

XiaoYouChRb8ea9333cc29
diff --git a/app/supports/sysio.py b/app/supports/sysio.py
index 8d870873..9e7c8101 100644
--- a/app/supports/sysio.py
+++ b/app/supports/sysio.py
@@ -4,10 +4,19 @@ from sys import platform
if platform == "win32":
import msvcrt
from ctypes import wintypes, Structure, Union, c_void_p, POINTER, windll, WinError, cast, c_char_p, byref, \
- c_longlong, get_last_error
+ c_longlong
INVALID_HANDLE_VALUE = -1
+ def _check_error(msg: str):
+ def _check(result, func, arg):
+ if not result:
+ err = WinError()
+ raise OSError(err.winerror, f"{msg}: {err.strerror}")
+ return result
+
+ return _check
+
class _dummy_s(Structure):
_fields_ = [("Offset", wintypes.DWORD), ("OffsetHigh", wintypes.DWORD)]
@@ -32,13 +41,7 @@ if platform == "win32":
POINTER(OVERLAPPED),
]
WriteFile.restype = wintypes.BOOL
-
- def check1(result, func, arg):
- if result == 0:
- raise WinError()
- return result
-
- WriteFile.errcheck = check1
+ WriteFile.errcheck = _check_error("WriteFile failed")
# _get_osfhandle = ctypes.windll.msvcrt._get_osfhandle
# _get_osfhandle.argtypes = [ctypes.c_int]
@@ -87,6 +90,7 @@ if platform == "win32":
wintypes.LPVOID,
]
DeviceIoControl.restype = wintypes.BOOL
+ DeviceIoControl.errcheck = _check_error("FSCTL_SET_SPARSE failed")
SetFilePointerEx = windll.kernel32.SetFilePointerEx
SetFilePointerEx.argtypes = [
@@ -96,15 +100,12 @@ if platform == "win32":
wintypes.DWORD,
]
SetFilePointerEx.restype = wintypes.BOOL
+ SetFilePointerEx.errcheck = _check_error("SetFilePointerEx failed")
SetEndOfFile = windll.kernel32.SetEndOfFile
SetEndOfFile.argtypes = [wintypes.HANDLE]
SetEndOfFile.restype = wintypes.BOOL
-
-
- def _raise_winerror(msg: str):
- err = get_last_error()
- raise OSError(err, f"{msg}, WinError={err}")
+ SetEndOfFile.errcheck = _check_error("SetEndOfFile failed")
def ftruncate(fd: typing.Union[int, wintypes.HANDLE], size: int) -> None:
if isinstance(fd, int):
@@ -115,7 +116,7 @@ if platform == "win32":
handle = fd
returned = wintypes.DWORD(0)
- ok = DeviceIoControl(
+ DeviceIoControl(
handle,
FSCTL_SET_SPARSE,
None,
@@ -125,17 +126,11 @@ if platform == "win32":
byref(returned),
None,
)
- if not ok:
- _raise_winerror("FSCTL_SET_SPARSE failed")
newPos = c_longlong()
- ok = SetFilePointerEx(handle, size, byref(newPos), FILE_BEGIN)
- if not ok:
- _raise_winerror("SetFilePointerEx failed")
+ SetFilePointerEx(handle, size, byref(newPos), FILE_BEGIN)
- ok = SetEndOfFile(handle)
- if not ok:
- _raise_winerror("SetEndOfFile failed")
+ SetEndOfFile(handle)
else:
# noinspection PyUnresolvedReferences
diff --git a/app/view/components/add_task_dialog.py b/app/view/components/add_task_dialog.py
index 4b58af6f..036899c7 100644
--- a/app/view/components/add_task_dialog.py
+++ b/app/view/components/add_task_dialog.py
@@ -130,9 +130,6 @@ class AddTaskDialog(MessageBoxBase):
def _applyCurrentPayloadToTask(self, task: Task):
payload = self.getCurrentPayload()
- taskUrl = getattr(task, "url", None)
- if isinstance(taskUrl, str) and taskUrl.strip():
- payload["url"] = taskUrl
task.applyPayloadToTask(payload)
def syncPayload(self):
diff --git a/app/view/components/cards.py b/app/view/components/cards.py
index c7342e9f..702171fd 100644
--- a/app/view/components/cards.py
+++ b/app/view/components/cards.py
@@ -92,7 +92,7 @@ class ParseSettingCard(QWidget):
class TaskCard(CardWidget):
""" Task card base class """
- deleted = Signal() # TODO Send Task ID, or lambda function?
+ deleted = Signal()
finished = Signal()
checkedChanged = Signal(bool)
diff --git a/features/http_pack/task.py b/features/http_pack/task.py
index 90ecea3d..08ceea59 100644
--- a/features/http_pack/task.py
+++ b/features/http_pack/task.py
@@ -61,11 +61,11 @@ class HttpTask(Task):
super().applyPayloadToTask(payload)
headers = payload.get("headers")
if isinstance(headers, dict):
- self.headers = headers.copy()
+ self.headers = headers
proxies = payload.get("proxies")
if isinstance(proxies, dict):
- self.proxies = proxies.copy()
+ self.proxies = proxies
blockNum = payload.get("preBlockNum")
if isinstance(blockNum, int):