Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 0 additions & 18 deletions .basedpyright/baseline.json
Original file line number Diff line number Diff line change
Expand Up @@ -492,24 +492,6 @@
}
}
],
"./src/window_geometry.py": [
{
"code": "reportArgumentType",
"range": {
"startColumn": 34,
"endColumn": 55,
"lineCount": 1
}
},
{
"code": "reportArgumentType",
"range": {
"startColumn": 34,
"endColumn": 55,
"lineCount": 1
}
}
],
"./tests/_vd_loader.py": [
{
"code": "reportAttributeAccessIssue",
Expand Down
6 changes: 6 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,9 @@ updates:
groups:
python:
patterns: ["*"]
# The plugin hard-fails on a major mismatch with the vendored server script
# (vendor/bgutil-pot-provider/), so a major bump must move both together by hand
# (see "Updating the pin" in its README). Drop this ignore once that upgrade lands.
ignore:
- dependency-name: "bgutil-ytdlp-pot-provider"
update-types: ["version-update:semver-major"]
5 changes: 4 additions & 1 deletion meadowlark.spec
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ a = Analysis(
"yt_dlp_plugins.extractor.getpot_bgutil",
"yt_dlp_plugins.extractor.getpot_bgutil_http",
"yt_dlp_plugins.extractor.getpot_bgutil_script",
"wakepy._implementations",
# wakepy registers its keep-awake methods dynamically, so PyInstaller cannot trace them.
# Collect the whole package: 1.0 moved them from `_implementations` to `methods`, and a
# stale name only warns at build time while the frozen app silently stops keeping awake.
*collect_submodules("wakepy"),
],
hookspath=[],
runtime_hooks=[],
Expand Down
4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ ignore = [
"EM101", # String literal in exception — common pattern
"PLR2004", # Magic value used in comparison — too broad to enforce
"PLR0913", # Too many arguments in function definition
"PLR0917", # Too many positional arguments (positional sibling of PLR0913)
"CPY001", # Missing copyright notice (licensing lives in LICENSE, not per-file headers)
"PLR0911", # Too many return statements
"FBT003", # Boolean positional value in function call
"BLE001", # Blind `except Exception` — intentional defensive top-level catches
Expand All @@ -98,4 +100,4 @@ ignore = [
"scripts/**" = ["INP001"]

[tool.uv.sources]
genekit = { git = "https://github.com/TheGeneCode/genekit", subdirectory = "python", tag = "py-v0.4.0" }
genekit = { git = "https://github.com/TheGeneCode/genekit", subdirectory = "python", tag = "py-v0.5.0" }
24 changes: 21 additions & 3 deletions src/logging_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,21 @@
_debug_logger_lock = threading.Lock()


def _capture_handler(debug_logger: logging.Logger) -> logging.Handler | None:
"""
Return the capture file handler this module attached, if any.

Matched by name, not by ``bool(debug_logger.handlers)``: foreign handlers
can sit on this logger too (pytest >= 9.2 attaches ``LogCaptureHandler`` to
non-propagating loggers), and treating one as ours would skip opening the
debug log entirely.
"""
for handler in debug_logger.handlers:
if handler.get_name() == _YTDLP_DEBUG_LOGGER_NAME:
return handler
return None


def get_local_timestamp() -> str:
"""Return current local timestamp as formatted string 'YYYY-MM-DD HH:MM:SS'."""
return datetime.now().astimezone().strftime("%Y-%m-%d %H:%M:%S")
Expand All @@ -46,7 +61,9 @@ def log_exception(exc: Exception, context: str | None = None) -> None:
if not logging.getLogger().hasHandlers():
configure_logging("ERROR", log_file=ERROR_LOG_PATH, console="none")
msg = f"{context}: {exc}" if context else str(exc)
logger.exception(msg)
# Explicit exc_info: exception() reads sys.exc_info(), which is empty when a
# caller passes an exception it caught earlier and is no longer handling.
logger.error(msg, exc_info=exc)


def get_ytdlp_debug_logger() -> logging.Logger | None:
Expand All @@ -72,13 +89,13 @@ def get_ytdlp_debug_logger() -> logging.Logger | None:
return None

debug_logger = logging.getLogger(_YTDLP_DEBUG_LOGGER_NAME)
if debug_logger.handlers:
if _capture_handler(debug_logger) is not None:
return debug_logger

with _debug_logger_lock:
# Re-check inside the lock: another thread may have attached the
# handler while this one was waiting.
if debug_logger.handlers:
if _capture_handler(debug_logger) is not None:
return debug_logger

try:
Expand All @@ -94,6 +111,7 @@ def get_ytdlp_debug_logger() -> logging.Logger | None:
log_exception(exc, "Could not open the yt-dlp debug log")
return None

handler.set_name(_YTDLP_DEBUG_LOGGER_NAME)
handler.setFormatter(logging.Formatter("%(asctime)s %(levelname)-7s %(message)s"))
debug_logger.addHandler(handler)
debug_logger.setLevel(logging.DEBUG)
Expand Down
18 changes: 3 additions & 15 deletions src/qyt.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,20 +119,6 @@ def error(self, msg: str) -> None:
self._tee(logging.ERROR, msg)
self.message_changed.emit(msg)

def exception(self, msg: str) -> None:
"""
Log an exception message using the module logger and emit the message_changed signal.

Args:
msg (str): The exception message to log and emit.
"""
logger = logging.getLogger(__name__)
logger.exception(msg)
if self._debug_logger is not None:
# exception() rather than _tee: keep the traceback in the capture.
self._debug_logger.exception(msg)
self.message_changed.emit(msg)

# def download(self, urls, options):
# with YoutubeDL(options) as ydl:
# ydl.cache.remove()
Expand Down Expand Up @@ -583,7 +569,9 @@ def download(self, urls: list, options: dict) -> None:
self.message_changed.emit(error_message)
logger = options.get("logger")
if isinstance(logger, QLogger):
logger.exception(error_message)
# error(), not exception(): no exception is being handled here,
# so exception() would log a bogus "NoneType: None" traceback.
logger.error(error_message)
meta = options.get("qmeta") or {}
site = meta.get("site", "unknown")
dtype = meta.get("type", meta.get("source", "unknown"))
Expand Down
19 changes: 19 additions & 0 deletions tests/test_logging_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,22 @@ def test_log_exception_no_root_handlers_triggers_basicconfig(
handler.close()
logging.root.handlers = original_handlers
assert "test context: test error" in log_path.read_text(encoding="utf-8")

def test_log_exception_outside_except_block_records_passed_traceback(
self, caplog: pytest.LogCaptureFixture
) -> None:
"""A previously caught exception keeps its traceback after the handler exits."""
try:
int("not a number")
except ValueError as exc:
caught = exc
assert logging.getLogger().hasHandlers() # caplog handler: skip basicConfig path

with caplog.at_level(logging.ERROR):
log_exception(caught, "late")

record = next(r for r in caplog.records if "late: invalid literal" in r.getMessage())
assert record.exc_info is not None
assert record.exc_info[1] is caught
assert "ValueError: invalid literal" in caplog.text
assert "NoneType: None" not in caplog.text
31 changes: 31 additions & 0 deletions tests/test_release_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"""

import ast
import importlib.util
import re
from pathlib import Path

Expand Down Expand Up @@ -253,3 +254,33 @@ def test_spec_helper_detects_broader_resource_bundles(
fake.write_text(f"datas = [{bad_source}, ('resources/icons', 'resources/icons')]\n")
monkeypatch.setattr(f"{__name__}._SPEC", fake)
assert _spec_resource_sources() != ["resources/icons"]


def _spec_hidden_import_names() -> list[str]:
"""Return the spec's `hiddenimports=` names: literals and `collect_submodules` args."""
tree = ast.parse(_SPEC.read_text(encoding="utf-8"))
names: list[str] = []
for node in ast.walk(tree):
if not (isinstance(node, ast.keyword) and node.arg == "hiddenimports"):
continue
assert isinstance(node.value, ast.List), "hiddenimports is no longer a list literal"
for elt in node.value.elts:
target = elt.value if isinstance(elt, ast.Starred) else elt
if isinstance(target, ast.Call) and target.args:
target = target.args[0]
if isinstance(target, ast.Constant) and isinstance(target.value, str):
names.append(target.value)
return names


def test_every_spec_hidden_import_resolves() -> None:
"""
A stale hidden import only warns at build time, then the frozen app lacks the module.

wakepy 1.0 moved its dynamically-registered methods out of `_implementations`;
the old name would have shipped an exe that silently stopped keeping the PC awake.
"""
names = _spec_hidden_import_names()
assert "wakepy" in names, "spec parsing found no hidden imports; the guard is blind"
missing = [name for name in names if importlib.util.find_spec(name) is None]
assert not missing, f"hiddenimports name modules that do not exist: {missing}"
41 changes: 36 additions & 5 deletions tests/test_ytdlp_diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,23 @@
_JOIN_TIMEOUT_S = 5.0


def _own_handlers(lg: logging.Logger) -> list[logging.Handler]:
"""
Handlers this module attached, ignoring foreign ones.

pytest >= 9.2 adds its own ``LogCaptureHandler`` to non-propagating loggers,
so a raw ``len(lg.handlers)`` no longer counts only ours.
"""
return [h for h in lg.handlers if h.get_name() == logging_utils._YTDLP_DEBUG_LOGGER_NAME]


@pytest.fixture(autouse=True)
def _clean_debug_logger():
"""Detach handlers from the capture logger around every test."""
"""Detach the capture handler from the capture logger around every test."""

def _reset() -> None:
lg = logging.getLogger(logging_utils._YTDLP_DEBUG_LOGGER_NAME)
for handler in list(lg.handlers):
for handler in _own_handlers(lg):
handler.close()
lg.removeHandler(handler)

Expand Down Expand Up @@ -120,7 +130,28 @@ def test_repeated_calls_attach_one_handler(self, tmp_path: Path) -> None:
second = logging_utils.get_ytdlp_debug_logger()
assert first is second
assert first is not None
assert len(first.handlers) == 1
assert len(_own_handlers(first)) == 1

def test_foreign_handler_does_not_block_file_handler(self, tmp_path: Path) -> None:
"""An unnamed handler already on the logger must not read as 'configured'."""
target = tmp_path / "ytdlp_debug.log"
foreign = logging.NullHandler()
lg = logging.getLogger(logging_utils._YTDLP_DEBUG_LOGGER_NAME)
lg.addHandler(foreign)
try:
with (
patch.object(logging_utils, "YTDLP_VERBOSE", True),
patch.object(logging_utils, "YTDLP_DEBUG_LOG_PATH", target),
):
debug_logger = logging_utils.get_ytdlp_debug_logger()
assert debug_logger is not None
debug_logger.debug("pot=foreign-ok")
for handler in _own_handlers(debug_logger):
handler.flush()
assert len(_own_handlers(lg)) == 1
assert "pot=foreign-ok" in target.read_text(encoding="utf-8")
finally:
lg.removeHandler(foreign)

def test_does_not_propagate_to_root(self, tmp_path: Path) -> None:
"""The capture must never leak into error_log.txt."""
Expand Down Expand Up @@ -153,7 +184,7 @@ def test_concurrent_first_calls_attach_one_handler(self, tmp_path: Path) -> None
"""
Two QLogger constructions racing the cold path must not double-attach.

Both threads pass the unlocked ``if debug_logger.handlers:`` fast-path
Both threads pass the unlocked ``_capture_handler(...)`` fast-path
check as empty (neither has added one yet) before either reaches the
lock -- widened here by making lock acquisition itself wait on a
barrier, so both threads are guaranteed to arrive at
Expand Down Expand Up @@ -201,7 +232,7 @@ def _call(slot: int) -> None:

assert results[0] is not None
assert results[0] is results[1]
assert len(results[0].handlers) == 1
assert len(_own_handlers(results[0])) == 1


class TestVerboseOption:
Expand Down
Loading
Loading