Skip to content

Commit 1086de7

Browse files
committed
FIX: macosx framework check
1 parent 7dc526e commit 1086de7

File tree

1 file changed

+42
-41
lines changed

1 file changed

+42
-41
lines changed

src/_macosx.m

+42-41
Original file line numberDiff line numberDiff line change
@@ -269,9 +269,41 @@ - (int)index;
269269

270270
static bool backend_inited = false;
271271

272-
static void lazy_init(void) {
272+
static bool verify_framework(void)
273+
{
274+
#ifdef COMPILING_FOR_10_6
275+
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
276+
NSRunningApplication* app = [NSRunningApplication currentApplication];
277+
NSApplicationActivationPolicy activationPolicy = [app activationPolicy];
278+
[pool release];
279+
switch (activationPolicy) {
280+
case NSApplicationActivationPolicyRegular:
281+
case NSApplicationActivationPolicyAccessory:
282+
return true;
283+
case NSApplicationActivationPolicyProhibited:
284+
break;
285+
}
286+
#else
287+
ProcessSerialNumber psn;
288+
if (CGMainDisplayID()!=0
289+
&& GetCurrentProcess(&psn)==noErr
290+
&& SetFrontProcess(&psn)==noErr) return true;
291+
#endif
292+
PyErr_SetString(PyExc_ImportError,
293+
"Python is not installed as a framework. The Mac OS X backend will "
294+
"not be able to function correctly if Python is not installed as a "
295+
"framework. See the Python documentation for more information on "
296+
"installing Python as a framework on Mac OS X. Please either reinstall "
297+
"Python as a framework, or try one of the other backends. If you are "
298+
"using (Ana)Conda please install python.app and replace the use of "
299+
"'python' with 'pythonw'. See 'Working with Matplotlib on OSX' in the "
300+
"Matplotlib FAQ for more information.");
301+
return false;
302+
}
303+
304+
static bool lazy_init(void) {
273305
if (backend_inited) {
274-
return;
306+
return true;
275307
}
276308
backend_inited = true;
277309

@@ -288,6 +320,10 @@ static void lazy_init(void) {
288320
name: NSWorkspaceDidLaunchApplicationNotification
289321
object: nil];
290322
[pool release];
323+
324+
if (!verify_framework()) return false;
325+
326+
return true;
291327
}
292328

293329
static PyObject*
@@ -314,7 +350,7 @@ static CGFloat _get_device_scale(CGContextRef cr)
314350
static PyObject*
315351
FigureCanvas_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
316352
{
317-
lazy_init();
353+
if (!lazy_init()) return NULL;
318354
FigureCanvas *self = (FigureCanvas*)type->tp_alloc(type, 0);
319355
if (!self) return NULL;
320356
self->view = [View alloc];
@@ -675,7 +711,7 @@ static CGFloat _get_device_scale(CGContextRef cr)
675711
static PyObject*
676712
FigureManager_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
677713
{
678-
lazy_init();
714+
if (!lazy_init()) return NULL;
679715
Window* window = [Window alloc];
680716
if (!window) return NULL;
681717
FigureManager *self = (FigureManager*)type->tp_alloc(type, 0);
@@ -1111,7 +1147,7 @@ -(void)save_figure:(id)sender
11111147
static PyObject*
11121148
NavigationToolbar2_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
11131149
{
1114-
lazy_init();
1150+
if (!lazy_init()) return NULL;
11151151
NavigationToolbar2Handler* handler = [NavigationToolbar2Handler alloc];
11161152
if (!handler) return NULL;
11171153
NavigationToolbar2 *self = (NavigationToolbar2*)type->tp_alloc(type, 0);
@@ -2372,7 +2408,7 @@ - (int)index
23722408
static PyObject*
23732409
Timer_new(PyTypeObject* type, PyObject *args, PyObject *kwds)
23742410
{
2375-
lazy_init();
2411+
if (!lazy_init()) return NULL;
23762412
Timer* self = (Timer*)type->tp_alloc(type, 0);
23772413
if (!self) return NULL;
23782414
self->timer = NULL;
@@ -2563,38 +2599,6 @@ static void context_cleanup(const void* info)
25632599
Timer_new, /* tp_new */
25642600
};
25652601

2566-
static bool verify_framework(void)
2567-
{
2568-
#ifdef COMPILING_FOR_10_6
2569-
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
2570-
NSRunningApplication* app = [NSRunningApplication currentApplication];
2571-
NSApplicationActivationPolicy activationPolicy = [app activationPolicy];
2572-
[pool release];
2573-
switch (activationPolicy) {
2574-
case NSApplicationActivationPolicyRegular:
2575-
case NSApplicationActivationPolicyAccessory:
2576-
return true;
2577-
case NSApplicationActivationPolicyProhibited:
2578-
break;
2579-
}
2580-
#else
2581-
ProcessSerialNumber psn;
2582-
if (CGMainDisplayID()!=0
2583-
&& GetCurrentProcess(&psn)==noErr
2584-
&& SetFrontProcess(&psn)==noErr) return true;
2585-
#endif
2586-
PyErr_SetString(PyExc_ImportError,
2587-
"Python is not installed as a framework. The Mac OS X backend will "
2588-
"not be able to function correctly if Python is not installed as a "
2589-
"framework. See the Python documentation for more information on "
2590-
"installing Python as a framework on Mac OS X. Please either reinstall "
2591-
"Python as a framework, or try one of the other backends. If you are "
2592-
"using (Ana)Conda please install python.app and replace the use of "
2593-
"'python' with 'pythonw'. See 'Working with Matplotlib on OSX' in the "
2594-
"Matplotlib FAQ for more information.");
2595-
return false;
2596-
}
2597-
25982602
static struct PyMethodDef methods[] = {
25992603
{"event_loop_is_running",
26002604
(PyCFunction)event_loop_is_running,
@@ -2644,9 +2648,6 @@ static bool verify_framework(void)
26442648
|| PyType_Ready(&TimerType) < 0)
26452649
return NULL;
26462650

2647-
if (!verify_framework())
2648-
return NULL;
2649-
26502651
module = PyModule_Create(&moduledef);
26512652
if (!module)
26522653
return NULL;

0 commit comments

Comments
 (0)