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

Unexpected Behavior w/ prefix #736

matthewwithanm opened this Issue Sep 26, 2012 · 2 comments


None yet
2 participants

matthewwithanm commented Sep 26, 2012

The prefix context manager (and probably others that are using _setenv) don't behave as expected when not called immediately prior to __enter__(). This means that the context manager isn't usable with contextlib.nested. Here's an example:

from contextlib import nested
from fabric import prefix, state
with nested(prefix('echo 1'), prefix('echo 2')):
    print state.env.command_prefixes

Expected Output:

['echo 1', 'echo 2']

Actual Output:

['echo 2']

The reason is because the new values are being stored at the time prefix is called—not when the context is entered. So, in the example above, when prefix is called the first time, it defines the context manager as setting command_prefixes to the current value ([]) plus ['echo 1']. When it's called the second time, the current value of command_prefixes is still an empty list (because the first context hasn't been entered yet), so the context manager is defined as setting command_prefixes to ['echo 2']. Therefore, entering the second context manager clobbers any effect of entering the first.

The fix, I think, is to defer the getting of command_prefixes until the context is entered.

matthewwithanm added a commit to matthewwithanm/fabric that referenced this issue Sep 26, 2012

Create nested prefix test for fabric#736
This test initializes two prefix context managers before using them.
Since the context is not entered immediately after initialization, the
test will fail if the bug is present.

matthewwithanm added a commit to matthewwithanm/fabric that referenced this issue Sep 26, 2012


This comment has been minimized.

matthewwithanm commented Sep 26, 2012

Just to clarify, this bug doesn't just affect nested, but any situation in which the context managers aren't entered immediately after initialization. The test in 65647c6 contains a simple nested-less example.


This comment has been minimized.


bitprophet commented Oct 28, 2012

Rolling this guy into #738 -- thanks!

@bitprophet bitprophet closed this Oct 28, 2012

bitprophet added a commit that referenced this issue Oct 28, 2012

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