Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Don't abort on FT2Font weakref.#9074

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
tacaswell merged 1 commit intomatplotlib:masterfromanntzer:ft2font-weakref
Feb 11, 2018

Conversation

anntzer
Copy link
Contributor

Currently,

import weakref, matplotlib.ft2fontweakref.ref(matplotlib.ft2font.FT2Font("/usr/share/fonts/TTF/DejaVuSans.ttf"))

aborts Py3 with

terminate called after throwing an instance of 'char const*'Fatal Python error: Aborted

whereas on Py2 a normalTypeError: cannot create weak reference to 'matplotlib.ft2font.FT2Font' object is raised.

This is because the following happens:

  • Py3 sets the TypeError for failure to create a weakref.
  • The FT2Font object gets GC'd as nothing is referring to it (yes,
    creating a weakref to a temporary is a bit silly).
  • The FT2Font destructor calls close_file_callback, which calls
    mpl_PyFile_DupClose, which calls PyObject_AsFileDescriptor... which,
    on Py3, calls the fileno() method on the file object.
  • PyObject_AsFileDescriptor fails because the originally set TypeError
    has not been cleared, so mpl_PyFile_DupClose likewise fails, causing
    close_file_callback to throw a C++ exception.

Instead, carefully stash and restore the current exception state, both
in mpl_PyFile_DupClose, and likewise below, in mpl_PyFile_CloseFile (the
latter is needed because otherwise PyObject_CallMethod will clear the
exception, cf comment in CPython's Objects/abstract.c:

/* PyObject_Call() must not be called with an exception set,because it may clear it (directly or indirectly) and so thecaller loses its exception */

Note that if an exceptionactually gets raised by mpl_PyFile_DupClose
or mpl_PyFile_CloseFile, it will overwrite the TypeError. Strictly
speaking, on Py3, it may be preferrable to chain the exceptions, but
this seems a bit overkill (mostly, I just want to avoid aborting the
whole Python process).

PR Summary

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

Copy link
Member

@QuLogicQuLogic left a comment

Choose a reason for hiding this comment

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

A minor comment, but this works for me.

@@ -126,6 +126,9 @@ static NPY_INLINE FILE *mpl_PyFile_Dup(PyObject *file, char *mode, mpl_off_t *or
*/
static NPY_INLINE int mpl_PyFile_DupClose(PyObject *file, FILE *handle, mpl_off_t orig_pos)
{
PyObject *type = NULL, *value = NULL, *tb = NULL;
Copy link
Member

Choose a reason for hiding this comment

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

These should probably get some differentiating name, like anorig_ prefix or something.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

grepping throughhttps://github.com/python/cpython/blob/f320be77ffb73e3b9e7fc98c37b8df3975d84b40/Modules/_io/fileio.c shows that cpython tends to stash the current exception (if any) in *exc, *val, *tb (but not without a prefix). We're not shadowing anything anyways...

Copy link
Member

Choose a reason for hiding this comment

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

Sure, but those are generally around one other call, whereas this one is around almost an entire function body.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

added exc_ prefix.

@@ -200,14 +211,23 @@ static NPY_INLINE PyObject *mpl_PyFile_OpenFile(PyObject *filename, const char *

static NPY_INLINE int mpl_PyFile_CloseFile(PyObject *file)
{
PyObject *type, *value, *tb;
Copy link
Member

Choose a reason for hiding this comment

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

... and here too.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

as above

Currently,    import weakref, matplotlib.ft2font    weakref.ref(matplotlib.ft2font.FT2Font("/usr/share/fonts/TTF/DejaVuSans.ttf"))aborts Py3 with    terminate called after throwing an instance of 'char const*'    Fatal Python error: Abortedwhereas on Py2 a normal `TypeError: cannot create weak reference to'matplotlib.ft2font.FT2Font' object` is raised.This is because the following happens:- Py3 sets the TypeError for failure to create a weakref.- The FT2Font object gets GC'd as nothing is referring to it (yes,  creating a weakref to a temporary is a bit silly).- The FT2Font destructor calls close_file_callback, which calls  mpl_PyFile_DupClose, which calls PyObject_AsFileDescriptor... which,  on Py3, calls the fileno() method on the file object.- PyObject_AsFileDescriptor fails because the originally set TypeError  has not been cleared, so mpl_PyFile_DupClose likewise fails, causing  close_file_callback to throw a C++ exception.Instead, carefully stash and restore the current exception state, bothin mpl_PyFile_DupClose, and likewise below, in mpl_PyFile_CloseFile (thelatter is needed because otherwise PyObject_CallMethod will clear theexception, cf comment in CPython's Objects/abstract.c:    /* PyObject_Call() must not be called with an exception set,    because it may clear it (directly or indirectly) and so the    caller loses its exception */Note that if an exception *actually* gets raised by mpl_PyFile_DupCloseor mpl_PyFile_CloseFile, it will overwrite the TypeError.  Strictlyspeaking, on Py3, it may be preferrable to chain the exceptions, butthis seems a bit overkill (mostly, I just want to avoid aborting thewhole Python process).
@tacaswelltacaswell merged commit33bd5ed intomatplotlib:masterFeb 11, 2018
@anntzeranntzer deleted the ft2font-weakref branchFebruary 11, 2018 22:41
@QuLogicQuLogic modified the milestones:needs sorting,v2.2.0Feb 12, 2018
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@QuLogicQuLogicQuLogic approved these changes

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
v2.2.0
Development

Successfully merging this pull request may close these issues.

4 participants
@anntzer@QuLogic@tacaswell@dstansby

[8]ページ先頭

©2009-2025 Movatter.jp