Skip to content

setattr context manager. #10314

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

Merged
merged 1 commit into from
Mar 26, 2018
Merged

setattr context manager. #10314

merged 1 commit into from
Mar 26, 2018

Conversation

anntzer
Copy link
Contributor

@anntzer anntzer commented Jan 24, 2018

PR Summary

The setattr-contextmanager that was proposed in #10292 (comment). Just quickly grepped for all the finallys in the codebase.

I also thought about doing something similar for set_prop() / get_prop() but that only appears twice throughout the codebase afaict so it's not really worth it.

PR Checklist

  • Has Pytest style unit tests
  • Code is PEP 8 compliant
  • New features are documented, with examples if plot related
  • Documentation is sphinx and numpydoc compliant
  • Added an entry to doc/users/next_whats_new/ if major new feature (follow instructions in README.rst there)
  • Documented in doc/api/api_changes.rst if API changed in a backward-incompatible way

@QuLogic
Copy link
Member

QuLogic commented Jan 24, 2018

Could this be done with unittest.mock.patch or something similar?

@anntzer
Copy link
Contributor Author

anntzer commented Jan 24, 2018

Indeed, once we switch to Py3-only (or if we decide to add mock as a dependency) it'll be as simple as replacing cbook._setattr_cm by unittest.mock.patch.multiple...
So perhaps we may as well just wait for mpl3.

@dopplershift
Copy link
Contributor

I'd still be 👍 to merge this. It improves things, reduces the lines of code. I'm hesitant to wait for releases that are 6 months out.

with open(fpath, 'rb') as fh:
try:
font = afm.AFM(fh)
except RuntimeError:
Copy link
Member

Choose a reason for hiding this comment

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

Couldn't you simplify this by leaving out the inner try and catching RuntimeError in the outer try?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I guess open() cannot raise RuntimeError, so yes...

@@ -146,11 +146,6 @@ def context(style, after_reset=False):
mpl.rcdefaults()
try:
use(style)
except:
# Restore original settings before raising errors during the update.
mpl.rcParams.update(initial_settings)
Copy link
Member

Choose a reason for hiding this comment

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

Why is this removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

because it's already restored in the finally: just below...

Copy link
Member

Choose a reason for hiding this comment

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

Ok, learned something new today: The finally is executed before the raise. Then, it‘s ok.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

even

try: return 1
finally: return 2

will return 2...
https://docs.python.org/3/reference/compound_stmts.html#the-try-statement

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, the finally is executed no matter what else happens.

@anntzer anntzer force-pushed the setattr-cm branch 4 times, most recently from dbd5db6 to 34c7ce4 Compare January 27, 2018 07:32
@anntzer anntzer force-pushed the setattr-cm branch 3 times, most recently from 619460d to 3fa657a Compare February 15, 2018 22:33
@jklymak
Copy link
Member

jklymak commented Mar 6, 2018

Looks like this will be in MPL3.0 Did you want to make the change suggested above?

@jklymak jklymak added this to the v3.0 milestone Mar 6, 2018
@anntzer
Copy link
Contributor Author

anntzer commented Mar 6, 2018

Actually unittest.mock.patch.multiple does not work for previously non-existing attributes (where a delattr has to be done at the end), which we use for mpl_image_comparison_parameters. It is also nearly twice slower.
So I'm happy with keeping this implementation.

@jklymak jklymak merged commit 1c5eaf6 into matplotlib:master Mar 26, 2018
@anntzer anntzer deleted the setattr-cm branch March 26, 2018 21:40
tacaswell pushed a commit to tacaswell/matplotlib that referenced this pull request Aug 5, 2018
This context manager was added to master in
690b213 via matplotlib#10314.  We do not want
to backport that entire commit, however the backport of matplotlib#11407
requires this context manger.

It is private and self contained to low-risk to backport
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.

5 participants