@@ -267,6 +267,39 @@ - (int)index;
267
267
268
268
/* ---------------------------- Python classes ---------------------------- */
269
269
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
+
270
303
static CGFloat _get_device_scale (CGContextRef cr)
271
304
{
272
305
CGSize pixelSize = CGContextConvertSizeToDeviceSpace (cr, CGSizeMake (1 , 1 ));
@@ -281,6 +314,7 @@ static CGFloat _get_device_scale(CGContextRef cr)
281
314
static PyObject*
282
315
FigureCanvas_new (PyTypeObject *type, PyObject *args, PyObject *kwds)
283
316
{
317
+ lazy_init ();
284
318
FigureCanvas *self = (FigureCanvas*)type->tp_alloc (type, 0 );
285
319
if (!self) return NULL ;
286
320
self->view = [View alloc ];
@@ -641,6 +675,7 @@ static CGFloat _get_device_scale(CGContextRef cr)
641
675
static PyObject*
642
676
FigureManager_new (PyTypeObject *type, PyObject *args, PyObject *kwds)
643
677
{
678
+ lazy_init ();
644
679
Window* window = [Window alloc ];
645
680
if (!window) return NULL ;
646
681
FigureManager *self = (FigureManager*)type->tp_alloc (type, 0 );
@@ -1076,6 +1111,7 @@ -(void)save_figure:(id)sender
1076
1111
static PyObject*
1077
1112
NavigationToolbar2_new (PyTypeObject *type, PyObject *args, PyObject *kwds)
1078
1113
{
1114
+ lazy_init ();
1079
1115
NavigationToolbar2Handler* handler = [NavigationToolbar2Handler alloc ];
1080
1116
if (!handler) return NULL ;
1081
1117
NavigationToolbar2 *self = (NavigationToolbar2*)type->tp_alloc (type, 0 );
@@ -2310,16 +2346,6 @@ - (int)index
2310
2346
}
2311
2347
@end
2312
2348
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
-
2323
2349
static PyObject*
2324
2350
show (PyObject* self)
2325
2351
{
@@ -2346,6 +2372,7 @@ - (int)index
2346
2372
static PyObject*
2347
2373
Timer_new (PyTypeObject* type, PyObject *args, PyObject *kwds)
2348
2374
{
2375
+ lazy_init ();
2349
2376
Timer* self = (Timer*)type->tp_alloc (type, 0 );
2350
2377
if (!self) return NULL ;
2351
2378
self->timer = NULL ;
@@ -2572,7 +2599,7 @@ static bool verify_framework(void)
2572
2599
{" event_loop_is_running" ,
2573
2600
(PyCFunction)event_loop_is_running,
2574
2601
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."
2576
2603
},
2577
2604
{" show" ,
2578
2605
(PyCFunction)show,
@@ -2617,13 +2644,12 @@ static bool verify_framework(void)
2617
2644
|| PyType_Ready (&TimerType) < 0 )
2618
2645
return NULL ;
2619
2646
2620
- NSApp = [NSApplication sharedApplication ];
2621
-
2622
2647
if (!verify_framework ())
2623
2648
return NULL ;
2624
2649
2625
2650
module = PyModule_Create (&moduledef);
2626
- if (module==NULL ) return NULL ;
2651
+ if (!module)
2652
+ return NULL ;
2627
2653
2628
2654
Py_INCREF (&FigureCanvasType);
2629
2655
Py_INCREF (&FigureManagerType);
@@ -2634,16 +2660,5 @@ static bool verify_framework(void)
2634
2660
PyModule_AddObject (module, " NavigationToolbar2" , (PyObject*) &NavigationToolbar2Type);
2635
2661
PyModule_AddObject (module, " Timer" , (PyObject*) &TimerType);
2636
2662
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 ];
2648
2663
return module;
2649
2664
}
0 commit comments