Skip to content

Stealth Chromium that passes every bot detection test. Drop-in Playwright replacement with source-level fingerprint patches. 50+ tests passed.

fix(humanize): accept a compiled Playwright in the internals check (#583)

berce9dd63a27f01a
diff --git a/cloakbrowser/human/patch.py b/cloakbrowser/human/patch.py
index e1ff4a8..8646d5f 100644
--- a/cloakbrowser/human/patch.py
+++ b/cloakbrowser/human/patch.py
@@ -20,6 +20,7 @@ from __future__ import annotations
import datetime
import functools
import inspect
+import types
from typing import Any, Awaitable, Callable, Dict, List, Optional, Tuple
from .config import HumanConfig, rand
@@ -61,6 +62,17 @@ def _human_for(kind: str, wrapper: Any) -> Optional[Human]:
MIN_PLAYWRIGHT = (1, 53)
+def _assigns_attr(func: Any, name: str) -> bool:
+ """Whether ``func`` stores ``self.<name>``, read from its bytecode."""
+ # Only a plain Python function has bytecode to read. A compiled Playwright
+ # (e.g. built with Nuitka) exposes a placeholder ``__code__`` whose ``co_names``
+ # says nothing about the source, so it is left to the version check instead
+ # of being reported as missing the attribute.
+ if type(func) is not types.FunctionType:
+ return True
+ return name in func.__code__.co_names
+
+
def _check_playwright() -> None:
"""Fail loudly on a Playwright the engine cannot drive, instead of letting a
humanized page fall back to Playwright's stock actions."""
@@ -75,7 +87,7 @@ def _check_playwright() -> None:
from playwright._impl._frame import Frame
missing = [n for n, ok in (("Frame._timeout", hasattr(Frame, "_timeout")),
- ("ElementHandle._frame", "_frame" in ElementHandle.__init__.__code__.co_names))
+ ("ElementHandle._frame", _assigns_attr(ElementHandle.__init__, "_frame")))
if not ok]
if (len(parts) == 2 and parts < MIN_PLAYWRIGHT) or missing:
raise RuntimeError(
diff --git a/tests/test_humanize_unit.py b/tests/test_humanize_unit.py
index 7e9cd7d..ab3e230 100644
--- a/tests/test_humanize_unit.py
+++ b/tests/test_humanize_unit.py
@@ -852,3 +852,28 @@ def test_error_classes_stay_importable():
for name in ("UnsupportedHumanizeSelectorError", "StealthWorldUnavailableError",
"StealthEvaluationError"):
assert issubclass(getattr(h, name), h.StealthDomError)
+
+
+def test_check_playwright_accepts_a_compiled_playwright(monkeypatch):
+ """A Nuitka build swaps function bytecode for a placeholder, so the
+ internals probe must not report a supported Playwright as broken."""
+ from playwright._impl._element_handle import ElementHandle
+ from cloakbrowser.human import patch
+
+ class _CompiledFunction: # stands in for Nuitka's compiled_function
+ __code__ = (lambda: None).__code__
+
+ monkeypatch.setattr(ElementHandle, "__init__", _CompiledFunction())
+ patch._check_playwright()
+
+
+def test_check_playwright_still_flags_missing_internals(monkeypatch):
+ from playwright._impl._element_handle import ElementHandle
+ from cloakbrowser.human import patch
+
+ def __init__(self, parent, type, guid, initializer):
+ pass
+
+ monkeypatch.setattr(ElementHandle, "__init__", __init__)
+ with pytest.raises(RuntimeError, match="missing internals: ElementHandle._frame"):
+ patch._check_playwright()