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

avoid unbound local ref on exc before first sample #1713

Merged
merged 1 commit into from Jan 26, 2017

Conversation

maedoc
Copy link
Contributor

@maedoc maedoc commented Jan 25, 2017

Fixes #1712; if sampling is interrupted before first sample is generated, strace is unbound.

This fix assigns strace a sentinel value (None) before the loop. In case of KeyboardInterrupt, strace is closed if required and the interrupt is reraised, avoiding a return MultiTrace([None]).

@ColCarroll
Copy link
Member

#1709 adds the pylint ignore line -- since you're fixing this, can you merge and get rid of the pylint comment?

Could you also catch the corner case at the end where sampling is an empty iterator, so strace is still None? In that case, I think returning MultiTrace([]) will provide the most informative error message!

@maedoc
Copy link
Contributor Author

maedoc commented Jan 25, 2017

This assumes sampling never returns None as a reasonable value. If it does, an object() can provide a unique object, e.g.

default_value = object()
strace = default_value
for strace in sampling:
    pass
if strace is default_value:
    pass

@ferrine
Copy link
Member

ferrine commented Jan 25, 2017

Why so difficult? Add context manager to BaseTrace class

def __enter__(self):
    return self

def __exit__(self, *args): # cannot recall exact signature
    self.close()

UPD: I see some refactoring will be needed though.

@maedoc
Copy link
Contributor Author

maedoc commented Jan 25, 2017

@ferrine that doesn't address the original issue that strace may be a referenced but unbound local variable if (1) the loop is KeyboardInterrupted or (2) sampling is an empty iterator.

@ferrine
Copy link
Member

ferrine commented Jan 25, 2017

@maedoc That is the usual way for handling cases when we need to do some ending action even when Exception occurred. Here it is closing the backend.

@maedoc
Copy link
Contributor Author

maedoc commented Jan 26, 2017

If adding context manager behavior makes sense for an strace object, then that refactor would be best handled in a separate PR by a core dev. This PR aims to address only #1712.

@ColCarroll ColCarroll merged commit 59f6c85 into pymc-devs:master Jan 26, 2017
@ColCarroll
Copy link
Member

I think this is good -- thanks a bunch!

@ferrine feel free to add a context manager if you'd like, but it does look tricky to me!

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.

Local variable referenced before assignment
3 participants