Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure that post actions are performed in some context managers #1289

Merged
merged 1 commit into from
Oct 3, 2018

Conversation

jenshnielsen
Copy link
Collaborator

even if an exception is thrown.

The code after a yield statement in a context manager (the __exit__ code) is not run if an exception is thrown unless wrapped in a try finally construct. In these cases I think it makes sense to always run the __exit__ code @WilliamHPNielsen

Copy link
Contributor

@WilliamHPNielsen WilliamHPNielsen left a comment

Choose a reason for hiding this comment

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

Thank you! I'll try to get the kitchen to take idiot sandwich off the menu now...

@jenshnielsen jenshnielsen changed the title Ensure that postactions are performed in some context managers Ensure that post actions are performed in some context managers Oct 3, 2018
Copy link
Contributor

@astafan8 astafan8 left a comment

Choose a reason for hiding this comment

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

Thanks! Didn't know that :(

@codecov
Copy link

codecov bot commented Oct 3, 2018

Codecov Report

Merging #1289 into master will decrease coverage by <.01%.
The diff coverage is 100%.

@@            Coverage Diff             @@
##           master    #1289      +/-   ##
==========================================
- Coverage   70.92%   70.91%   -0.01%     
==========================================
  Files          74       74              
  Lines        8351     8352       +1     
==========================================
  Hits         5923     5923              
- Misses       2428     2429       +1

@astafan8
Copy link
Contributor

astafan8 commented Oct 3, 2018

Indeed, this is from the docstring:

"""@contextmanager decorator.

    Typical usage:

        @contextmanager
        def some_generator(<arguments>):
            <setup>
            try:
                yield <value>
            finally:
                <cleanup>

    This makes this:

        with some_generator(<arguments>) as <variable>:
            <body>

    equivalent to this:

        <setup>
        try:
            <variable> = <value>
            <body>
        finally:
            <cleanup>

    """

@jenshnielsen jenshnielsen merged commit b54131a into microsoft:master Oct 3, 2018
@jenshnielsen jenshnielsen deleted the fix_contextmanagers branch October 3, 2018 08:52
giulioungaretti pushed a commit that referenced this pull request Oct 3, 2018
Merge: 65c94e9 c01b2ac
Author: Jens Hedegaard Nielsen <[email protected]>

    Merge pull request #1289 from jenshnielsen/fix_contextmanagers
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.

3 participants