fix: plot_format leaks its settings when the wrapped block raises - #176
WatchTree-19 wants to merge 1 commit into
Conversation
plot_format mutates module-level state and restores it after the yield with no
try/finally:
old = get_plot_format()
set_plot_format(plot_format, dpi)
yield
set_plot_format(*old)
If the wrapped code raises, which rendering readily does, the restore never
runs and _PLOT_FORMAT, _PLOT_MIME_TYPE and _PLOT_DPI stay overridden for the
rest of the process. Nothing warns; every later block silently renders at the
wrong format and DPI. Against released 1.4.3, a block that raises inside
plot_format("svg", 300) leaves the globals at ('svg', 300).
Wrap the yield in try/finally.
Separately, set_plot_format assigns _PLOT_FORMAT before looking up the mime
type, so an unsupported format raises KeyError with _PLOT_FORMAT already
changed and _PLOT_MIME_TYPE still holding the old value. On 1.4.3,
set_plot_format("jpeg") leaves format 'jpeg' paired with mime 'png'. Look the
mime type up first so a bad format changes nothing.
Adds two tests alongside the existing test_plot_format_ctx_manager, which only
covers the success path.
Signed-Off By Sandeep Singh Rai sandeeprai_dsp@hotmail.com
|
CI is red here, but as far as I can tell none of it comes from this change. Flagging what I found in case it is useful.
This PR only touches Both look like pandas 3 turning existing deprecations into errors. On clean I could not install pandas 3 in my environment to confirm Glad to send the pandas 3 compatibility work as a separate PR if that would help. |
|
Following up on my last comment, I said the pandas 3 part was inference. I have now confirmed it. I built a Python 3.12 environment with pandas 3.0.5 and ran clean So it is pre-existing and unrelated to this PR. I have opened #177 with the fix, which is independent of this one.
|
|
The three red checks here are all pre-existing rather than caused by this change, and two of them are fixed by #177. This branch touches
These are pandas 3 removals — #177 is the fix for exactly those, and it's green on 3.11 and 3.12. So merging #177 first should clear these; I'll rebase this onto
Nothing here needs changing on my side until #177 lands — happy to be told otherwise. |
What does this fix?
1.
plot_formatleaks its settings when the wrapped block raises.There is no
try/finally, so if the wrapped code raises, the restore never runs and_PLOT_FORMAT,_PLOT_MIME_TYPEand_PLOT_DPIstay overridden for the rest of the process. Nothing warns, so every subsequent block silently renders at the wrong format and DPI. Against released 1.4.3:2.
set_plot_formathalf-applies an unsupported format.The assignment happens before the lookup, so a format not in
_MIME_TYPESraisesKeyErrorwith_PLOT_FORMATalready changed and_PLOT_MIME_TYPEstill on the old value:The change
try/finallyaround theyield, and the mime lookup moved ahead of both assignments so a bad format changes nothing.Tests
Two added next to the existing
test_plot_format_ctx_manager, which only covers the success path. Both fail on master and pass with the change; the fulltest_image_unit.pymodule passes (9 passed).One thing I left alone
set_plot_formatwith a bad format still raises a bareKeyError: 'jpeg', which is not a great message for what is really an invalid argument. I kept the exception type as-is to avoid changing behaviour anyone might be catching, but happy to turn it into aValueErrornaming the supported formats if you would prefer that.