Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 131ad82

Browse files
anntzertacaswell
authored andcommitted
Lazy-init the OSX event loop.
1 parent 0fbd85f commit 131ad82

File tree

2 files changed

+40
-28
lines changed

2 files changed

+40
-28
lines changed

lib/matplotlib/backends/__init__.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,6 @@ def _get_running_interactive_framework():
5656
except ImportError:
5757
pass
5858
else:
59-
# Note that the NSApp event loop is also running when a non-native
60-
# toolkit (e.g. Qt5) is active, but in that case we want to report the
61-
# other toolkit; thus, this check comes after the other toolkits.
6259
if _macosx.event_loop_is_running():
6360
return "macosx"
6461
if sys.platform.startswith("linux") and not os.environ.get("DISPLAY"):

src/_macosx.m

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,39 @@ - (int)index;
267267

268268
/* ---------------------------- Python classes ---------------------------- */
269269

270+
static bool backend_inited = false;
271+
272+
static void lazy_init(void) {
273+
if (backend_inited) {
274+
return;
275+
}
276+
backend_inited = true;
277+
278+
NSApp = [NSApplication sharedApplication];
279+
280+
PyOS_InputHook = wait_for_stdin;
281+
282+
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
283+
WindowServerConnectionManager* connectionManager = [WindowServerConnectionManager sharedManager];
284+
NSWorkspace* workspace = [NSWorkspace sharedWorkspace];
285+
NSNotificationCenter* notificationCenter = [workspace notificationCenter];
286+
[notificationCenter addObserver: connectionManager
287+
selector: @selector(launch:)
288+
name: NSWorkspaceDidLaunchApplicationNotification
289+
object: nil];
290+
[pool release];
291+
}
292+
293+
static PyObject*
294+
event_loop_is_running(PyObject* self)
295+
{
296+
if (backend_inited) {
297+
Py_RETURN_TRUE;
298+
} else {
299+
Py_RETURN_FALSE;
300+
}
301+
}
302+
270303
static CGFloat _get_device_scale(CGContextRef cr)
271304
{
272305
CGSize pixelSize = CGContextConvertSizeToDeviceSpace(cr, CGSizeMake(1, 1));
@@ -281,6 +314,7 @@ static CGFloat _get_device_scale(CGContextRef cr)
281314
static PyObject*
282315
FigureCanvas_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
283316
{
317+
lazy_init();
284318
FigureCanvas *self = (FigureCanvas*)type->tp_alloc(type, 0);
285319
if (!self) return NULL;
286320
self->view = [View alloc];
@@ -641,6 +675,7 @@ static CGFloat _get_device_scale(CGContextRef cr)
641675
static PyObject*
642676
FigureManager_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
643677
{
678+
lazy_init();
644679
Window* window = [Window alloc];
645680
if (!window) return NULL;
646681
FigureManager *self = (FigureManager*)type->tp_alloc(type, 0);
@@ -1076,6 +1111,7 @@ -(void)save_figure:(id)sender
10761111
static PyObject*
10771112
NavigationToolbar2_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
10781113
{
1114+
lazy_init();
10791115
NavigationToolbar2Handler* handler = [NavigationToolbar2Handler alloc];
10801116
if (!handler) return NULL;
10811117
NavigationToolbar2 *self = (NavigationToolbar2*)type->tp_alloc(type, 0);
@@ -2310,16 +2346,6 @@ - (int)index
23102346
}
23112347
@end
23122348

2313-
static PyObject*
2314-
event_loop_is_running(PyObject* self)
2315-
{
2316-
if ([NSApp isRunning]) {
2317-
Py_RETURN_TRUE;
2318-
} else {
2319-
Py_RETURN_FALSE;
2320-
}
2321-
}
2322-
23232349
static PyObject*
23242350
show(PyObject* self)
23252351
{
@@ -2346,6 +2372,7 @@ - (int)index
23462372
static PyObject*
23472373
Timer_new(PyTypeObject* type, PyObject *args, PyObject *kwds)
23482374
{
2375+
lazy_init();
23492376
Timer* self = (Timer*)type->tp_alloc(type, 0);
23502377
if (!self) return NULL;
23512378
self->timer = NULL;
@@ -2572,7 +2599,7 @@ static bool verify_framework(void)
25722599
{"event_loop_is_running",
25732600
(PyCFunction)event_loop_is_running,
25742601
METH_NOARGS,
2575-
"Return whether the NSApp main event loop is currently running."
2602+
"Return whether the OSX backend has set up the NSApp main event loop."
25762603
},
25772604
{"show",
25782605
(PyCFunction)show,
@@ -2617,13 +2644,12 @@ static bool verify_framework(void)
26172644
|| PyType_Ready(&TimerType) < 0)
26182645
return NULL;
26192646

2620-
NSApp = [NSApplication sharedApplication];
2621-
26222647
if (!verify_framework())
26232648
return NULL;
26242649

26252650
module = PyModule_Create(&moduledef);
2626-
if (module==NULL) return NULL;
2651+
if (!module)
2652+
return NULL;
26272653

26282654
Py_INCREF(&FigureCanvasType);
26292655
Py_INCREF(&FigureManagerType);
@@ -2634,16 +2660,5 @@ static bool verify_framework(void)
26342660
PyModule_AddObject(module, "NavigationToolbar2", (PyObject*) &NavigationToolbar2Type);
26352661
PyModule_AddObject(module, "Timer", (PyObject*) &TimerType);
26362662

2637-
PyOS_InputHook = wait_for_stdin;
2638-
2639-
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
2640-
WindowServerConnectionManager* connectionManager = [WindowServerConnectionManager sharedManager];
2641-
NSWorkspace* workspace = [NSWorkspace sharedWorkspace];
2642-
NSNotificationCenter* notificationCenter = [workspace notificationCenter];
2643-
[notificationCenter addObserver: connectionManager
2644-
selector: @selector(launch:)
2645-
name: NSWorkspaceDidLaunchApplicationNotification
2646-
object: nil];
2647-
[pool release];
26482663
return module;
26492664
}

0 commit comments

Comments
 (0)