Skip to content

Fix deprecated PyErr_Fetch/PyErr_Restore in close_file_callback#31610

Open
kx7m2qd wants to merge 1 commit intomatplotlib:mainfrom
kx7m2qd:fix/pyerr-fetch-restore-deprecated
Open

Fix deprecated PyErr_Fetch/PyErr_Restore in close_file_callback#31610
kx7m2qd wants to merge 1 commit intomatplotlib:mainfrom
kx7m2qd:fix/pyerr-fetch-restore-deprecated

Conversation

@kx7m2qd
Copy link
Copy Markdown

@kx7m2qd kx7m2qd commented May 4, 2026

Replace deprecated PyErr_Fetch/PyErr_Restore in close_file_callback

PyErr_Fetch and PyErr_Restore were deprecated in Python 3.12 in favour of
the unified PyErr_GetRaisedException/PyErr_SetRaisedException API.

This PR updates close_file_callback in src/ft2font_wrapper.cpp to use the
new API on Python 3.12+, while keeping the old API for older versions via a
#if PY_VERSION_HEX >= 0x030c0000 guard.

Closes part of #31424

PR summary

PyErr_Fetch/PyErr_Restore were deprecated in Python 3.12 (see
https://docs.python.org/3/whatsnew/3.12.html#deprecated). The replacement is
the simpler single-object API: PyErr_GetRaisedException/PyErr_SetRaisedException.

The change is in close_file_callback in src/ft2font_wrapper.cpp (lines ~394
and ~402 as noted in #31424). A version guard ensures backward compatibility
with Python < 3.12.

No behavior change — this is a pure maintenance fix to silence deprecation
warnings and future-proof the code for Python 3.13+.

AI Disclosure

Used Claude (Anthropic) to help identify the correct replacement API and version
guard approach. The fix, testing, and final review were done manually.

PR checklist

  • "closes [Bug]: Umbrella issue: matplotlib C/C++ Extension Analysis Report #31424" is in the body of the PR description
  • new and changed code is tested — 67/69 tests pass in lib/matplotlib/tests/test_ft2font.py; 2 skips are pre-existing and unrelated to this change
  • [N/A] Plotting related features are demonstrated in an example
  • [N/A] New Features and API Changes are noted with a directive and release note
  • [N/A] Documentation complies with general and docstring guidelines

Comment thread src/ft2font_wrapper.cpp Outdated
@kx7m2qd kx7m2qd force-pushed the fix/pyerr-fetch-restore-deprecated branch from 9876779 to 94738e5 Compare May 5, 2026 06:31
Replace deprecated PyErr_Fetch/PyErr_Restore (deprecated in Python 3.12)
with PyErr_GetRaisedException/PyErr_SetRaisedException on Python 3.12+,
while keeping backward compatibility with older versions via #if guard.

Closes part of matplotlib#31424
@kx7m2qd kx7m2qd force-pushed the fix/pyerr-fetch-restore-deprecated branch from 94738e5 to c1232d8 Compare May 5, 2026 08:53
Comment thread src/ft2font_wrapper.cpp
close();
}

// FIX: Guard against NULL family_name pointers before streaming.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Both these comments are not really necessary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Umbrella issue: matplotlib C/C++ Extension Analysis Report

3 participants