Skip to content

narrow marshal.load except in _read_pyc to expected exceptions#14642

Closed
HrachShah wants to merge 1 commit into
pytest-dev:mainfrom
HrachShah:fix/read-pyc-marshal-narrow-except
Closed

narrow marshal.load except in _read_pyc to expected exceptions#14642
HrachShah wants to merge 1 commit into
pytest-dev:mainfrom
HrachShah:fix/read-pyc-marshal-narrow-except

Conversation

@HrachShah

Copy link
Copy Markdown

Summary

Tighten the bare except Exception around marshal.load in _read_pyc to the narrow tuple (EOFError, ValueError, TypeError, OSError).

What changes

  • src/_pytest/assertion/rewrite.py: _read_pyc previously wrapped marshal.load(fp) in except Exception. Per the Python docs, marshal.load only raises EOFError (truncated code), ValueError (unmarshalable object), TypeError (unsupported type code), or OSError (read failure on the file). The bare except was also catching KeyboardInterrupt and SystemExit, masking Ctrl-C during a slow import and silently converting AttributeError / MemoryError from a future bug in the marshal call chain into a benign "return None" that masked the real problem.
  • Added test covering a truncated marshal payload (EOFError) and a bad marshal type code (ValueError).

Tests

python -m pytest testing/test_assertrewrite.py -k read_pyc -v
# 4 passed

@RonnyPfannschmidt

Copy link
Copy Markdown
Member

closing as unattended ai slop based on the pr content, timeline and user profile

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants