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

Fix context manager for LocalCommand.popen() #495

Merged
merged 2 commits into from
Feb 3, 2020
Merged

Fix context manager for LocalCommand.popen() #495

merged 2 commits into from
Feb 3, 2020

Conversation

yayachiken
Copy link
Contributor

This fixes a regression introduced in 108342a.

It appears that this commit tried to implement a transparent proxy object for subprocess.Popen. Unfortunately, special method calls (e.g. of context manager methods by the with statement) generally circumvent the __getattribute__ machinery and perform direct lookup.

This leads to an exception when calling LocalCommand.popen() within a with statement:

>>> from plumbum import local
>>> ls = local['ls']
>>> with ls.popen():
...     print("yay")
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: __enter__

This broke some code of mine. I have not found any documentation regarding this change, so I assume dropping the context manager functionality was an accident.

This PR restores the context manager functionality for local[...].popen() calls.

Copy link
Collaborator

@henryiii henryiii 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 if CI passes. Thank you!

@coveralls
Copy link

coveralls commented Feb 3, 2020

Coverage Status

Coverage increased (+0.2%) to 82.842% when pulling 52ad039 on yayachiken:master into aed69a4 on tomerfiliba:master.

@@ -23,6 +23,16 @@
SDIR = os.path.dirname(os.path.abspath(__file__))


class TestLocalPopen:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you stick a Python 3 guard here, please?

Also, this can just be a function, rather than a class in a function (up to you).

Copy link
Collaborator

Choose a reason for hiding this comment

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

Sorry I have to ask for Python 3 guards in 2020, but plumbum still supports Python 2.6. :/ But hey, we might be able to drop Python 2.6 after November 2020 (RHEL 6)!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It it okay like this? Or do you want me to put a run-time check on the actual methods?

(I don't know what would be appropriate behaviour then, I guess just raising NotImplemented instead of AttributeError?)

Copy link
Collaborator

Choose a reason for hiding this comment

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

This is fine, adding features to Python 2 is not needed, it was an error before too, I believe.

@henryiii henryiii merged commit 794475a into tomerfiliba:master Feb 3, 2020
@henryiii
Copy link
Collaborator

henryiii commented Feb 3, 2020

Thanks!

@yayachiken
Copy link
Contributor Author

Thank you, that was quick!

@henryiii
Copy link
Collaborator

henryiii commented Feb 3, 2020

Adding tests speeds up the process considerably. ;)

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