Description
Bug summary
On macOS, default backend (Cocoa?), with Python embedded into an application, closing the last open Matplotlib window causes an uncatchable exit. It does not throw a system exit exception to call Python's system exit, but rather a C level exit which causes the entire application to close. This is fine if you are running Matplotlib from an interactive console, it is not fine when Python is embedded as a scripting language within an application.
Code for reproduction
# Embed Python into a minimal application, run application
# from the embedded python scripting, run the script.
# Such as running through PyRun_SimpleString("code"); (not an interactive shell)
import matplotlib.pyplot as plt
plt.clf() # make sure we are starting with a clean plot
plt.plot([1, 2, 3, 4])
plt.ylabel('some numbers')
plt.show()
# close the window - application exits in an uncatchable manner.
Actual outcome
Application shutdown that is not catchable.
Expected outcome
Window closes and script stops running and control is returned to interpreter, but Application with Python interpreter continues running. Backend returns to a state waiting for another plot / window to be opened.
Additional information
Use Matplotlib from an application that embeds Python as a scripting language. Close last Matplotlib window. Matplotlib forces application to close ungracefully, no way to catch close, no way to save / close opened files no way for an orderly shutdown and a C level exit is called.
Matplotlib always forces exit for every version tried.
Why: A call to exit() or exit_() at the C level of the code, not a Python SystemExit.
Fix: Don't terminate the application with an uncatchable exit.
Operating system
macOS
Matplotlib Version
3.8.0 and prior
Matplotlib Backend
MacOSX
Python version
3.11.5 ( and earlier all the way back to 3.5)
Jupyter version
No response
Installation
pip