Skip to content

add .unwrap_and_destroy() method to remove reference to value/error when unwrapping outcome#49

Open
graingert wants to merge 7 commits intopython-trio:mainfrom
graingert:unwrap-and-destroy
Open

add .unwrap_and_destroy() method to remove reference to value/error when unwrapping outcome#49
graingert wants to merge 7 commits intopython-trio:mainfrom
graingert:unwrap-and-destroy

Conversation

@graingert
Copy link
Member

@graingert graingert commented Feb 6, 2026

Fixes #47

currently unwraping exceptions leaves a refcycle that keeps the entire traceback and any locals alive. This PR resolves that by clearing the error field when the Error is unwrap_and_destroy-ed.

For the previous behaviour of Value (eg in trio) if you receive a large bytes object in a Value that bytes object stays alive long after it's needed. This PR resolves that by clearing the value field when the Value is unwrap_and_destroy-ed.

this is an alternative approach to #45 that is fully backwards compatible and will only need a minor version bump, eg outcome==1.4.0

@graingert graingert changed the title add .unwrap_and_destroy() method add .unwrap_and_destroy() method to remove reference to value/error when unwrapping outcome Feb 6, 2026
Copy link
Member

@CoolCat467 CoolCat467 left a comment

Choose a reason for hiding this comment

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

As far as I can tell this looks like a good approach.

@A5rocks
Copy link
Contributor

A5rocks commented Feb 6, 2026

I like the idea of at some point making .unwrap() do this too, because this seems unintuitive. I would be happy with either this or the prior PR (disregarding comments), as long as we plan to make the breaking change at some point.

I'm not sure it helps to provide a .unwrap_and_destroy() if we're going to end up breaking things by updating .unwrap()? The only harm is that we gain an extra method, but I'm not sure if there's benefits to this.

(or does this footgun not really matter in most cases? then maybe breaking everything isn't a good idea... sorry, I don't really know the context for this change)

@graingert
Copy link
Member Author

@graingert
Copy link
Member Author

as long as we plan to make the breaking change at some point

when I make the change in trio to use the new method I'll change the dep on outcome to outcome ~=1.4

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.

leaving .error and .value alive after unwrap results in unnecessary refcycles or potentially large values remaining alive longer than needed

3 participants