完成 Ruff 全量清理
This commit is contained in:
@@ -17,6 +17,7 @@ from __future__ import annotations
|
||||
import itertools
|
||||
import json
|
||||
import math
|
||||
import random
|
||||
import urllib.parse
|
||||
from pathlib import Path
|
||||
|
||||
@@ -40,7 +41,7 @@ def _ic(x):
|
||||
try:
|
||||
f = float(x)
|
||||
return 0 if math.isnan(f) else int(f)
|
||||
except Exception:
|
||||
except Exception: # noqa: BLE001 外部接口与任务边界需要保留宽泛异常兜底
|
||||
return 0
|
||||
|
||||
|
||||
@@ -79,7 +80,7 @@ def js_typeof(v):
|
||||
return "number"
|
||||
if isinstance(v, str):
|
||||
return "string"
|
||||
if isinstance(v, JSFunction) or isinstance(v, HostFunction):
|
||||
if isinstance(v, (JSFunction, HostFunction)):
|
||||
return "function"
|
||||
return "object"
|
||||
|
||||
@@ -99,7 +100,7 @@ def js_truthy(v):
|
||||
def _nan_ok(x):
|
||||
try:
|
||||
return not (isinstance(x, float) and math.isnan(x))
|
||||
except Exception:
|
||||
except Exception: # noqa: BLE001 外部接口与任务边界需要保留宽泛异常兜底
|
||||
return True
|
||||
|
||||
|
||||
@@ -207,7 +208,7 @@ def js_num(v):
|
||||
return v
|
||||
try:
|
||||
return float(v)
|
||||
except Exception:
|
||||
except Exception: # noqa: BLE001 外部接口与任务边界需要保留宽泛异常兜底
|
||||
return float("nan")
|
||||
|
||||
|
||||
@@ -226,12 +227,12 @@ def js_eq(a, b):
|
||||
if isinstance(a, (int, float)) and isinstance(b, str):
|
||||
try:
|
||||
return a == float(b)
|
||||
except Exception:
|
||||
except Exception: # noqa: BLE001 外部接口与任务边界需要保留宽泛异常兜底
|
||||
return False
|
||||
if isinstance(b, (int, float)) and isinstance(a, str):
|
||||
try:
|
||||
return float(a) == b
|
||||
except Exception:
|
||||
except Exception: # noqa: BLE001 外部接口与任务边界需要保留宽泛异常兜底
|
||||
return False
|
||||
return a == b
|
||||
|
||||
@@ -355,9 +356,7 @@ def js_set(obj, key, val):
|
||||
pass
|
||||
if isinstance(obj, str):
|
||||
raise TypeError("Cannot assign to read only property")
|
||||
cur = getattr(
|
||||
__import__("pyvm.algorithm", fromlist=["x"]).VM, "_last_u", None
|
||||
)
|
||||
cur = getattr(__import__("pyvm.algorithm", fromlist=["x"]).VM, "_last_u", None)
|
||||
raise TypeError(
|
||||
f"invalid set target obj={type(obj).__name__} key={key!r} val={type(val).__name__} u={cur}"
|
||||
)
|
||||
@@ -736,7 +735,7 @@ def h_parsefloat(this, s):
|
||||
def h_isnan(this, x):
|
||||
try:
|
||||
return math.isnan(float(x))
|
||||
except Exception:
|
||||
except Exception: # noqa: BLE001 外部接口与任务边界需要保留宽泛异常兜底
|
||||
return True
|
||||
|
||||
|
||||
@@ -896,7 +895,7 @@ class VM:
|
||||
for x in v:
|
||||
try:
|
||||
parts.append("%02x" % (int(x) & 255))
|
||||
except Exception:
|
||||
except Exception: # noqa: BLE001 外部接口与任务边界需要保留宽泛异常兜底
|
||||
parts.append("??")
|
||||
self.out_hex = "".join(parts)
|
||||
# 仅供离线排查 webSave 输出缓冲何时变化,生产路径默认不启用。
|
||||
@@ -1527,9 +1526,7 @@ class VM:
|
||||
if not (
|
||||
isinstance(fn, (JSFunction, HostFunction)) or callable(fn)
|
||||
):
|
||||
raise RuntimeError(
|
||||
"op97 non-function at u=%s fn=%r" % (u, fn)
|
||||
)
|
||||
raise RuntimeError(f"op97 non-function at u={u} fn={fn!r}")
|
||||
js_set(C, dest, js_apply(fn, thisv, f))
|
||||
elif op == 98:
|
||||
a = o[u + 1]
|
||||
@@ -1647,10 +1644,10 @@ class VM:
|
||||
u += 1
|
||||
js_set(C, a, None)
|
||||
else:
|
||||
raise RuntimeError("unknown opcode %s at %s" % (op, u))
|
||||
raise RuntimeError(f"unknown opcode {op} at {u}")
|
||||
except _VMThrow as e:
|
||||
if not d:
|
||||
raise RuntimeError("VM uncaught throw: %s" % (e.value,))
|
||||
raise RuntimeError(f"VM uncaught throw: {e.value}")
|
||||
l = e.value
|
||||
u = d.pop()
|
||||
continue
|
||||
@@ -1658,9 +1655,8 @@ class VM:
|
||||
if not d:
|
||||
raise
|
||||
if not isinstance(d, list):
|
||||
raise RuntimeError(
|
||||
"d corrupted: %r (type %s) at trace %s"
|
||||
% (d, type(d).__name__, self._trace[-3:])
|
||||
raise TypeError(
|
||||
f"d corrupted: {d!r} (type {type(d).__name__}) at trace {self._trace[-3:]}"
|
||||
)
|
||||
l = e
|
||||
u = d.pop()
|
||||
@@ -1746,14 +1742,14 @@ def decode_d(v):
|
||||
vs = p[ci + 1 :]
|
||||
try:
|
||||
obj.set(key, decode_d(json.loads(vs)))
|
||||
except Exception:
|
||||
except Exception: # noqa: BLE001 外部接口与任务边界需要保留宽泛异常兜底
|
||||
obj.set(key, decode_d(vs))
|
||||
return obj
|
||||
if v.lstrip("-").isdigit():
|
||||
return int(v)
|
||||
try:
|
||||
return float(v)
|
||||
except Exception:
|
||||
except Exception: # noqa: BLE001 外部接口与任务边界需要保留宽泛异常兜底
|
||||
return v
|
||||
|
||||
|
||||
|
||||
@@ -72,9 +72,13 @@ class MallSession:
|
||||
f"mall xMidasOps 应为 59620(非 goods 59640),实际 {len(self.xmidas_ops)}"
|
||||
)
|
||||
mid = self.transform_input[10]
|
||||
if isinstance(mid, list) and mid and isinstance(mid[0], list):
|
||||
if len(mid[0]) != 624:
|
||||
raise ValueError(f"624B 中间态长度 != 624: {len(mid[0])}")
|
||||
if (
|
||||
isinstance(mid, list)
|
||||
and mid
|
||||
and isinstance(mid[0], list)
|
||||
and len(mid[0]) != 624
|
||||
):
|
||||
raise ValueError(f"624B 中间态长度 != 624: {len(mid[0])}")
|
||||
|
||||
@classmethod
|
||||
def from_capture_file(cls, frames_jsonl: str | Path) -> MallSession:
|
||||
|
||||
@@ -38,13 +38,13 @@ def get_official_orders(cookies: dict[str, str], count: int = 20) -> dict[str, A
|
||||
except ValueError as exc:
|
||||
raise RuntimeError("订单状态查询返回非 JSON") from exc
|
||||
if not isinstance(document, dict):
|
||||
raise RuntimeError("订单状态查询响应格式异常")
|
||||
raise TypeError("订单状态查询响应格式异常")
|
||||
if document.get("ret_code") not in (None, 0, "0"):
|
||||
raise RuntimeError(
|
||||
f"订单状态查询失败: {document.get('ret_code')} {document.get('ret_msg', '')}"
|
||||
)
|
||||
if not isinstance(document.get("list", []), list):
|
||||
raise RuntimeError("订单状态查询响应缺少 list")
|
||||
raise TypeError("订单状态查询响应缺少 list")
|
||||
return document
|
||||
|
||||
|
||||
|
||||
@@ -13,6 +13,8 @@ JS 语义辅助复用 algorithm.py(_ic/i32/js_add/js_index/JSObject/JSFunction
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import math
|
||||
from pathlib import Path
|
||||
|
||||
from .algorithm import (
|
||||
UNDEF,
|
||||
@@ -20,6 +22,7 @@ from .algorithm import (
|
||||
JSDate,
|
||||
JSFunction,
|
||||
JSObject,
|
||||
Window,
|
||||
_ic,
|
||||
h_decodeuri,
|
||||
h_decodeuricomponent,
|
||||
@@ -57,6 +60,8 @@ from .algorithm import (
|
||||
ushr,
|
||||
)
|
||||
|
||||
REPLAY = Path(__file__).resolve().parent.parent / "replay"
|
||||
|
||||
__all__ = ["REPLAY", "PagedooVM", "run_frame"]
|
||||
|
||||
|
||||
@@ -66,28 +71,28 @@ __all__ = ["REPLAY", "PagedooVM", "run_frame"]
|
||||
def _cmp_lt(a, b):
|
||||
try:
|
||||
return a < b
|
||||
except Exception:
|
||||
except Exception: # noqa: BLE001 外部接口与任务边界需要保留宽泛异常兜底
|
||||
return False
|
||||
|
||||
|
||||
def _cmp_le(a, b):
|
||||
try:
|
||||
return a <= b
|
||||
except Exception:
|
||||
except Exception: # noqa: BLE001 外部接口与任务边界需要保留宽泛异常兜底
|
||||
return False
|
||||
|
||||
|
||||
def _cmp_gt(a, b):
|
||||
try:
|
||||
return a > b
|
||||
except Exception:
|
||||
except Exception: # noqa: BLE001 外部接口与任务边界需要保留宽泛异常兜底
|
||||
return False
|
||||
|
||||
|
||||
def _cmp_ge(a, b):
|
||||
try:
|
||||
return a >= b
|
||||
except Exception:
|
||||
except Exception: # noqa: BLE001 外部接口与任务边界需要保留宽泛异常兜底
|
||||
return False
|
||||
|
||||
|
||||
@@ -122,8 +127,8 @@ def h_arr_slice(this, a=None, b=None):
|
||||
a = 0
|
||||
if b is None or b is UNDEF:
|
||||
b = n
|
||||
a = int(a) if a == a else 0
|
||||
b = int(b) if b == b else n
|
||||
a = int(a) if not isinstance(a, float) or not math.isnan(a) else 0
|
||||
b = int(b) if not isinstance(b, float) or not math.isnan(b) else n
|
||||
if a < 0:
|
||||
a = max(0, n + a)
|
||||
if b < 0:
|
||||
@@ -303,30 +308,29 @@ def _pg_index(obj, key):
|
||||
return HostFunction(
|
||||
lambda this: this.t if isinstance(this, JSDate) else obj.t, "valueOf"
|
||||
)
|
||||
if isinstance(obj, str):
|
||||
if isinstance(key, str):
|
||||
if key == "length":
|
||||
return len(obj)
|
||||
if key == "charCodeAt":
|
||||
return HostFunction(h_char_code_at, "charCodeAt")
|
||||
if key == "charAt":
|
||||
return HostFunction(h_char_at, "charAt")
|
||||
if key == "indexOf":
|
||||
return HostFunction(h_str_indexof, "indexOf")
|
||||
if key == "slice":
|
||||
return HostFunction(h_str_slice, "slice")
|
||||
if key == "split":
|
||||
return HostFunction(h_str_split, "split")
|
||||
if key == "toLowerCase":
|
||||
return HostFunction(h_str_tolower, "toLowerCase")
|
||||
if key == "toUpperCase":
|
||||
return HostFunction(h_str_toupper, "toUpperCase")
|
||||
if key == "toString":
|
||||
return HostFunction(h_str_tostring, "toString")
|
||||
if key == "substr":
|
||||
return HostFunction(h_str_substr, "substr")
|
||||
if key == "substring":
|
||||
return HostFunction(h_str_substring, "substring")
|
||||
if isinstance(obj, str) and isinstance(key, str):
|
||||
if key == "length":
|
||||
return len(obj)
|
||||
if key == "charCodeAt":
|
||||
return HostFunction(h_char_code_at, "charCodeAt")
|
||||
if key == "charAt":
|
||||
return HostFunction(h_char_at, "charAt")
|
||||
if key == "indexOf":
|
||||
return HostFunction(h_str_indexof, "indexOf")
|
||||
if key == "slice":
|
||||
return HostFunction(h_str_slice, "slice")
|
||||
if key == "split":
|
||||
return HostFunction(h_str_split, "split")
|
||||
if key == "toLowerCase":
|
||||
return HostFunction(h_str_tolower, "toLowerCase")
|
||||
if key == "toUpperCase":
|
||||
return HostFunction(h_str_toupper, "toUpperCase")
|
||||
if key == "toString":
|
||||
return HostFunction(h_str_tostring, "toString")
|
||||
if key == "substr":
|
||||
return HostFunction(h_str_substr, "substr")
|
||||
if key == "substring":
|
||||
return HostFunction(h_str_substring, "substring")
|
||||
return js_index(obj, key)
|
||||
|
||||
|
||||
@@ -486,15 +490,7 @@ class PagedooVM:
|
||||
and len(self._host_log) < 2000
|
||||
):
|
||||
# 记录调用目标(简化)
|
||||
_tgt = (
|
||||
o[u + 2]
|
||||
if op in (4, 11, 44, 50)
|
||||
else (
|
||||
o[u + 2]
|
||||
if op in (0, 18, 23, 26, 43, 48, 84, 107)
|
||||
else o[u + 2]
|
||||
)
|
||||
)
|
||||
_tgt = o[u + 2]
|
||||
try:
|
||||
_tv = C[_tgt] if 0 <= _tgt < len(C) else UNDEF
|
||||
_tr = (
|
||||
@@ -505,7 +501,7 @@ class PagedooVM:
|
||||
else repr(_tv)[:30]
|
||||
)
|
||||
self._host_log.append((op, u, _tr))
|
||||
except Exception:
|
||||
except Exception: # noqa: BLE001 外部接口与任务边界需要保留宽泛异常兜底
|
||||
self._host_log.append((op, u, "?"))
|
||||
if (
|
||||
getattr(self, "_trace", None) is not None
|
||||
|
||||
@@ -86,7 +86,7 @@ def validate_goods_materials(
|
||||
def validate_mall_materials(transform_fixed: dict[str, Any]) -> None:
|
||||
"""校验 mall 固定槽模板;动态槽仍由当前会话的 GetPayToken 填充。"""
|
||||
if not isinstance(transform_fixed, dict):
|
||||
raise ValueError("mall transform-fixed 必须是对象")
|
||||
raise TypeError("mall transform-fixed 必须是对象")
|
||||
required = {
|
||||
str(index) for index in (1, 2, 3, 4, 5, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17)
|
||||
}
|
||||
@@ -95,7 +95,7 @@ def validate_mall_materials(transform_fixed: dict[str, Any]) -> None:
|
||||
raise ValueError(f"mall transform-fixed 缺少槽: {', '.join(missing)}")
|
||||
for index in required:
|
||||
if not isinstance(transform_fixed[index], list):
|
||||
raise ValueError(f"mall transform-fixed 槽 {index} 不是数组")
|
||||
raise TypeError(f"mall transform-fixed 槽 {index} 不是数组")
|
||||
|
||||
|
||||
def goods_material_diagnostics(
|
||||
|
||||
Reference in New Issue
Block a user