Files
archived-camoufox/pythonlib/camoufox/_warnings.py
T
Jake WriterandClaude Opus 5.5 165e68f831 fix(python): warn whenever an identity falls back to a substitute value
Several draws swallowed their failure and used something else, so an
identity could ship with values the rest of it was not drawn to match and
nobody would hear about it:

- from_preset(): a failed font or voice draw used the preset's recorded
  list, or nothing, on any exception.
- generate_context_fingerprint(): a failed font, voice or WebGL draw was
  `except Exception: pass`, leaving the browser's launch-time values.
- _load_font_groups() / _load_font_bases(): an unreadable file became {},
  i.e. no font additions or no OS-version base.
- launch_options(): a failed font draw used every font in fonts.json, a
  failed voice draw used no voices, and a preset GPU missing from
  webgl_data.db was silently swapped for a drawn one (36 of the 397
  bundled presets).

Each site now catches only the errors its data can raise (OSError and
ValueError for an unreadable or corrupt file, KeyError for a manifest with
no entry for the OS, sqlite3.Error for the WebGL database) and emits a
FallbackWarning. The text names what failed and what the identity uses
instead, then gives a block to paste into an issue (camoufox, browser, OS
and Python versions, the error, and the identity's user agent or GPU),
asking the user to report it on GitHub. It shares LeakWarning's
caller-frame attribution and its template lives in warnings.yml.

The broad excepts had also been hiding a broken fixture:
test_launch_environment's font and voice stubs did not accept `seed`, so
every draw there raised and was swallowed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-25 20:34:17 -06:00

92 lines
2.9 KiB
Python

import inspect
import platform
import warnings
from importlib.metadata import PackageNotFoundError, version
from pathlib import Path
from typing import Optional, Type
from camoufox.pkgman import load_yaml
WARNINGS_DATA = load_yaml('warnings.yml')
def _warn_from_caller(message: str, category: Type[Warning]) -> None:
"""Attribute the warning to the first frame outside this package, so it
points at the user's call rather than at camoufox internals."""
current_module = Path(__file__).parent
frame = inspect.currentframe()
while frame:
if not Path(frame.f_code.co_filename).is_relative_to(current_module):
break
frame = frame.f_back
if frame:
warnings.warn_explicit(
message,
category=category,
filename=frame.f_code.co_filename,
lineno=frame.f_lineno,
)
return
warnings.warn(message, category=category)
class LeakWarning(RuntimeWarning):
"""
Raised when a the user has a setting enabled that can cause detection.
"""
@staticmethod
def warn(warning_key: str, i_know_what_im_doing: Optional[bool] = None) -> None:
"""
Warns the user if a passed parameter can cause leaks.
"""
warning = WARNINGS_DATA[warning_key]
if i_know_what_im_doing:
return
if i_know_what_im_doing is not None:
warning += '\nIf this is intentional, pass `i_know_what_im_doing=True`.'
_warn_from_caller(warning, LeakWarning)
def _browser_version() -> str:
from camoufox.exceptions import CamoufoxNotInstalled
from camoufox.pkgman import installed_verstr
try:
return installed_verstr()
except CamoufoxNotInstalled:
return 'not installed'
class FallbackWarning(RuntimeWarning):
"""
Raised when part of an identity could not be drawn and a substitute was used.
"""
@staticmethod
def warn(what: str, instead: str, error: Exception, identity: Optional[str] = None) -> None:
"""
Warns that `what` failed with `error` and the identity uses `instead`,
with a block of versions and the error for the user to paste into an issue.
"""
try:
camoufox_version = version('camoufox')
except PackageNotFoundError:
camoufox_version = 'source checkout'
lines = [
f'camoufox: {camoufox_version}',
f'browser: {_browser_version()}',
f'os: {platform.platform()}',
f'python: {platform.python_version()}',
f'error: {type(error).__name__}: {error}',
]
if identity:
lines.append(f'identity: {identity}')
report = '\n'.join(f' {line}' for line in lines)
_warn_from_caller(
WARNINGS_DATA['fallback'].format(what=what, instead=instead, report=report),
FallbackWarning,
)