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

Skip to content

Commit 2f47c6a

Browse files
committed
Lazy-init the OSX event loop.
1 parent 77ba47c commit 2f47c6a

File tree

1 file changed

+29
-14
lines changed

1 file changed

+29
-14
lines changed

src/_macosx.m

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,28 @@ - (int)index;
267267

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

270+
static void lazy_init(void) {
271+
static bool inited;
272+
if (inited) {
273+
return;
274+
}
275+
inited = true;
276+
277+
NSApp = [NSApplication sharedApplication];
278+
279+
PyOS_InputHook = wait_for_stdin;
280+
281+
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
282+
WindowServerConnectionManager* connectionManager = [WindowServerConnectionManager sharedManager];
283+
NSWorkspace* workspace = [NSWorkspace sharedWorkspace];
284+
NSNotificationCenter* notificationCenter = [workspace notificationCenter];
285+
[notificationCenter addObserver: connectionManager
286+
selector: @selector(launch:)
287+
name: NSWorkspaceDidLaunchApplicationNotification
288+
object: nil];
289+
[pool release];
290+
}
291+
270292
static CGFloat _get_device_scale(CGContextRef cr)
271293
{
272294
CGSize pixelSize = CGContextConvertSizeToDeviceSpace(cr, CGSizeMake(1, 1));
@@ -281,6 +303,7 @@ static CGFloat _get_device_scale(CGContextRef cr)
281303
static PyObject*
282304
FigureCanvas_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
283305
{
306+
lazy_init();
284307
FigureCanvas *self = (FigureCanvas*)type->tp_alloc(type, 0);
285308
if (!self) return NULL;
286309
self->view = [View alloc];
@@ -641,6 +664,7 @@ static CGFloat _get_device_scale(CGContextRef cr)
641664
static PyObject*
642665
FigureManager_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
643666
{
667+
lazy_init();
644668
Window* window = [Window alloc];
645669
if (!window) return NULL;
646670
FigureManager *self = (FigureManager*)type->tp_alloc(type, 0);
@@ -1016,6 +1040,7 @@ -(void)save_figure:(id)sender
10161040
static PyObject*
10171041
NavigationToolbar_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
10181042
{
1043+
lazy_init();
10191044
NavigationToolbarHandler* handler = [NavigationToolbarHandler alloc];
10201045
if (!handler) return NULL;
10211046
NavigationToolbar *self = (NavigationToolbar*)type->tp_alloc(type, 0);
@@ -1555,6 +1580,7 @@ -(void)save_figure:(id)sender
15551580
static PyObject*
15561581
NavigationToolbar2_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
15571582
{
1583+
lazy_init();
15581584
NavigationToolbar2Handler* handler = [NavigationToolbar2Handler alloc];
15591585
if (!handler) return NULL;
15601586
NavigationToolbar2 *self = (NavigationToolbar2*)type->tp_alloc(type, 0);
@@ -2825,6 +2851,7 @@ - (int)index
28252851
static PyObject*
28262852
Timer_new(PyTypeObject* type, PyObject *args, PyObject *kwds)
28272853
{
2854+
lazy_init();
28282855
Timer* self = (Timer*)type->tp_alloc(type, 0);
28292856
if (!self) return NULL;
28302857
self->timer = NULL;
@@ -3097,13 +3124,12 @@ static bool verify_framework(void)
30973124
|| PyType_Ready(&TimerType) < 0)
30983125
return NULL;
30993126

3100-
NSApp = [NSApplication sharedApplication];
3101-
31023127
if (!verify_framework())
31033128
return NULL;
31043129

31053130
module = PyModule_Create(&moduledef);
3106-
if (module==NULL) return NULL;
3131+
if (!module)
3132+
return NULL;
31073133

31083134
Py_INCREF(&FigureCanvasType);
31093135
Py_INCREF(&FigureManagerType);
@@ -3116,16 +3142,5 @@ static bool verify_framework(void)
31163142
PyModule_AddObject(module, "NavigationToolbar2", (PyObject*) &NavigationToolbar2Type);
31173143
PyModule_AddObject(module, "Timer", (PyObject*) &TimerType);
31183144

3119-
PyOS_InputHook = wait_for_stdin;
3120-
3121-
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
3122-
WindowServerConnectionManager* connectionManager = [WindowServerConnectionManager sharedManager];
3123-
NSWorkspace* workspace = [NSWorkspace sharedWorkspace];
3124-
NSNotificationCenter* notificationCenter = [workspace notificationCenter];
3125-
[notificationCenter addObserver: connectionManager
3126-
selector: @selector(launch:)
3127-
name: NSWorkspaceDidLaunchApplicationNotification
3128-
object: nil];
3129-
[pool release];
31303145
return module;
31313146
}

0 commit comments

Comments
 (0)