8000 Re-fix exception caching in dviread. by anntzer · Pull Request #28906 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Re-fix exception caching in dviread. #28906

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
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions lib/matplotlib/cbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,29 @@
from matplotlib import _api, _c_internal_utils


class _ExceptionInfo:
"""
A class to carry exception information around.

This is used to store and later raise exceptions. It's an alternative to
directly storing Exception instances that circumvents traceback-related
issues: caching tracebacks can keep user's objects in local namespaces
alive indefinitely, which can lead to very surprising memory issues for
users and result in incorrect tracebacks.
"""

def __init__(self, cls, *args):
self._cls = cls
self._args = args

@classmethod
def from_exception(cls, exc):
return cls(type(exc), *exc.args)

Check warning on line 52 in lib/matplotlib/cbook.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/cbook.py#L52

Added line #L52 was not covered by tests

def to_exception(self):
return self._cls(*self._args)


def _get_running_interactive_framework():
"""
Return the interactive framework whose event loop is currently running, if
Expand Down
10 changes: 5 additions & 5 deletions lib/matplotlib/dviread.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@
byte = self.file.read(1)[0]
self._dtable[byte](self, byte)
if self._missing_font:
raise self._missing_font
raise self._missing_font.to_exception()

Check warning on line 343 in lib/matplotlib/dviread.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/dviread.py#L343

Added line #L343 was not covered by tests
name = self._dtable[byte].__name__
if name == "_push":
down_stack.append(down_stack[-1])
Expand Down Expand Up @@ -368,14 +368,14 @@
@_dispatch(min=0, max=127, state=_dvistate.inpage)
def _set_char_immediate(self, char):
self._put_char_real(char)
if isinstance(self.fonts[self.f], FileNotFoundError):
if isinstance(self.fonts[self.f], cbook._ExceptionInfo):
return
self.h += self.fonts[self.f]._width_of(char)

@_dispatch(min=128, max=131, state=_dvistate.inpage, args=('olen1',))
def _set_char(self, char):
self._put_char_real(char)
if isinstance(self.fonts[self.f], FileNotFoundError):
if isinstance(self.fonts[self.f], cbook._ExceptionInfo):
return
self.h += self.fonts[self.f]._width_of(char)

Expand All @@ -390,7 +390,7 @@

def _put_char_real(self, char):
font = self.fonts[self.f]
if isinstance(font, FileNotFoundError):
if isinstance(font, cbook._ExceptionInfo):
self._missing_font = font
elif font._vf is None:
self.text.append(Text(self.h, self.v, font, char,
Expand Down Expand Up @@ -504,7 +504,7 @@
# and throw that error in Dvi._read. For Vf, _finalize_packet
# checks whether a missing glyph has been used, and in that case
# skips the glyph definition.
self.fonts[k] = exc.with_traceback(None)
self.fonts[k] = cbook._ExceptionInfo.from_exception(exc)

Check warning on line 507 in lib/matplotlib/dviread.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/dviread.py#L507

Added line #L507 was not covered by tests
return
if c != 0 and tfm.checksum != 0 and c != tfm.checksum:
raise ValueError(f'tfm checksum mismatch: {n}')
Expand Down
11 changes: 4 additions & 7 deletions lib/matplotlib/font_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
from __future__ import annotations

from base64 import b64encode
from collections import namedtuple
import copy
import dataclasses
from functools import lru_cache
Expand Down Expand Up @@ -133,8 +132,6 @@
'sans',
}

_ExceptionProxy = namedtuple('_ExceptionProxy', ['klass', 'message'])

# OS Font paths
try:
_HOME = Path.home()
Expand Down Expand Up @@ -1355,8 +1352,8 @@
ret = self._findfont_cached(
prop, fontext, directory, fallback_to_default, rebuild_if_missing,
rc_params)
if isinstance(ret, _ExceptionProxy):
raise ret.klass(ret.message)
if isinstance(ret, cbook._ExceptionInfo):
raise ret.to_exception()
return ret

def get_font_names(self):
Expand Down Expand Up @@ -1509,7 +1506,7 @@
# This return instead of raise is intentional, as we wish to
# cache that it was not found, which will not occur if it was
# actually raised.
return _ExceptionProxy(
return cbook._ExceptionInfo(
ValueError,
f"Failed to find font {prop}, and fallback to the default font was "
f"disabled"
Expand All @@ -1535,7 +1532,7 @@
# This return instead of raise is intentional, as we wish to
# cache that it was not found, which will not occur if it was
# actually raised.
return _ExceptionProxy(ValueError, "No valid font could be found")
return cbook._ExceptionInfo(ValueError, "No valid font could be found")

Check warning on line 1535 in lib/matplotlib/font_manager.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/font_manager.py#L1535

Added line #L1535 was not covered by tests

return _cached_realpath(result)

Expand Down
Loading
0