Skip to content

Fix macosx verify later #12557

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

Closed
wants to merge 6 commits into from
Closed
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
18 changes: 17 additions & 1 deletion lib/matplotlib/pyplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,6 @@ def switch_backend(newbackend):
The name of the backend to use.
"""
close("all")

if newbackend is rcsetup._auto_backend_sentinel:
for candidate in ["macosx", "qt5agg", "qt4agg", "gtk3agg", "gtk3cairo",
"tkagg", "wxagg", "agg", "cairo"]:
Expand Down Expand Up @@ -2349,6 +2348,23 @@ def _autogen_docstring(base):
# Set up the backend.
switch_backend(rcParams["backend"])

# We need an extra check here for macosx framework. If we are using MacOSX
# and not using the framework build we throw an ImportError (for back compat).
if dict.__getitem__(rcParams, "backend") == 'MacOSX':
from matplotlib.backends import _macosx
if not _macosx.verify_framework():
raise ImportError(
"Python is not installed as a framework. The Mac OS X backend "
"will not be able to function correctly if Python is not "
"installed as a framework. See the Python documentation for more "
"information on installing Python as a framework on Mac OS X. "
"Please either reinstall Python as a framework, or try one of "
"the other backends. If you are using (Ana)Conda please install "
"python.app and replace the use of 'python' with 'pythonw'. See "
"'Working with Matplotlib on OSX' in the Matplotlib FAQ for "
"more information.")


# Just to be safe. Interactive mode can be turned on without
# calling `plt.ion()` so register it again here.
# This is safe because multiple calls to `install_repl_displayhook`
Expand Down
46 changes: 31 additions & 15 deletions src/_macosx.m
Original file line number Diff line number Diff line change
Expand Up @@ -2575,24 +2575,37 @@ static void context_cleanup(const void* info)
Timer_new, /* tp_new */
};

static bool verify_framework(void)
PyObject*
verify_framework(void)
{
ProcessSerialNumber oldpsn;
GetFrontProcess(&oldpsn);

#ifdef COMPILING_FOR_10_6
NSApp = [NSApplication sharedApplication];
NSApplicationActivationPolicy activationPolicy = [NSApp activationPolicy];
switch (activationPolicy) {
case NSApplicationActivationPolicyRegular:
case NSApplicationActivationPolicyAccessory:
{ SetFrontProcess(&oldpsn);
return Py_True;
}
case NSApplicationActivationPolicyProhibited:
break;
}
#else
ProcessSerialNumber psn;
/* These methods are deprecated, but they don't require the app to
have started */
if (CGMainDisplayID()!=0
&& GetCurrentProcess(&psn)==noErr
&& SetFrontProcess(&psn)==noErr) return true;
PyErr_SetString(PyExc_ImportError,
"Python is not installed as a framework. The Mac OS X backend will "
"not be able to function correctly if Python is not installed as a "
"framework. See the Python documentation for more information on "
"installing Python as a framework on Mac OS X. Please either reinstall "
"Python as a framework, or try one of the other backends. If you are "
"using (Ana)Conda please install python.app and replace the use of "
"'python' with 'pythonw'. See 'Working with Matplotlib on OSX' in the "
"Matplotlib FAQ for more information.");
return false;
&& SetFrontProcess(&psn)==noErr){
SetFrontProcess(&oldpsn);
return Py_True;
}
#endif
SetFrontProcess(&oldpsn);
return Py_False;
}

static struct PyMethodDef methods[] = {
Expand All @@ -2619,7 +2632,12 @@ static bool verify_framework(void)
METH_VARARGS,
"Sets the active cursor."
},
{NULL, NULL, 0, NULL} /* sentinel */
{"verify_framework",
(PyCFunction)verify_framework,
METH_NOARGS,
"Verifies that the framework build is being used."
},
{NULL, NULL, 0, NULL, NULL} /* sentinel */
};

static struct PyModuleDef moduledef = {
Expand All @@ -2644,8 +2662,6 @@ static bool verify_framework(void)
|| PyType_Ready(&TimerType) < 0)
return NULL;

if (!verify_framework())
return NULL;

module = PyModule_Create(&moduledef);
if (!module)
Expand Down