pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/pytest-dev/pytest/pull/14209

et" href="https://github.githubassets.com/assets/actions-109fb3a41bacb1c2.css" /> Fix monkeypatch.setattr() teardown crash on failed setattr by worksbyfriday · Pull Request #14209 · pytest-dev/pytest · GitHub
Skip to content

Fix monkeypatch.setattr() teardown crash on failed setattr#14209

Open
worksbyfriday wants to merge 1 commit intopytest-dev:mainfrom
worksbyfriday:fix-monkeypatch-setattr-undo
Open

Fix monkeypatch.setattr() teardown crash on failed setattr#14209
worksbyfriday wants to merge 1 commit intopytest-dev:mainfrom
worksbyfriday:fix-monkeypatch-setattr-undo

Conversation

@worksbyfriday
Copy link

Fixes #14161.

When monkeypatch.setattr(target, name, value, raising=False) is called on a target that doesn't support attribute setting (e.g. None or other immutable objects), the underlying setattr() raises AttributeError. But the undo stack entry was appended before the setattr() call, leaving a stale (target, name, NOTSET) entry. During teardown, undo() then tries delattr(target, name), causing a second AttributeError crash.

Fix: Move the _setattr.append() call after the setattr() call, so only successful attribute modifications get undo entries.

# Before: crashes at teardown
something_or_none = None
monkeypatch.setattr(something_or_none, 'do_something', fake, raising=False)
# → setattr fails, but undo stack has stale entry
# → teardown: delattr(None, 'do_something') → AttributeError

# After: teardown is clean
# → setattr fails, undo stack is empty
# → teardown: nothing to undo

…v#14161)

When setattr() fails (e.g. because the target is immutable like None),
the undo stack already had the entry appended before the setattr call.
This left a stale (target, name, NOTSET) entry that would crash during
teardown with AttributeError trying to delattr on the target.

Fix by moving the _setattr.append() after the setattr() call, so
failed setattr operations don't pollute the undo stack.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@psf-chronographer psf-chronographer bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Feb 18, 2026
Copy link
Member

@RonnyPfannschmidt RonnyPfannschmidt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good, thanks

if you like you can use

with monkeypatch.context() as ctx:
       with pytest.raises(...):
          ctx.setattr(...)

to pin down the monkey-patch used to something very constrained

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

Labels

bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Monkeypatch raising having no effect at tear down

2 participants

pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy