@@ -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 );
@@ -1016,6 +1051,7 @@ -(void)save_figure:(id)sender
1016
1051
static PyObject*
1017
1052
NavigationToolbar_new (PyTypeObject *type, PyObject *args, PyObject *kwds)
1018
1053
{
1054
+ lazy_init ();
1019
1055
NavigationToolbarHandler* handler = [NavigationToolbarHandler alloc ];
1020
1056
if (!handler) return NULL ;
1021
1057
NavigationToolbar *self = (NavigationToolbar*)type->tp_alloc (type, 0 );
@@ -1555,6 +1591,7 @@ -(void)save_figure:(id)sender
1555
1591
static PyObject*
1556
1592
NavigationToolbar2_new (PyTypeObject *type, PyObject *args, PyObject *kwds)
1557
1593
{
1594
+ lazy_init ();
1558
1595
NavigationToolbar2Handler* handler = [NavigationToolbar2Handler alloc ];
1559
1596
if (!handler) return NULL ;
1560
1597
NavigationToolbar2 *self = (NavigationToolbar2*)type->tp_alloc (type, 0 );
@@ -2789,16 +2826,6 @@ - (int)index
2789
2826
}
2790
2827
@end
2791
2828
2792
- static PyObject*
2793
- event_loop_is_running (PyObject* self)
2794
- {
2795
- if ([NSApp isRunning ]) {
2796
- Py_RETURN_TRUE;
2797
- } else {
2798
- Py_RETURN_FALSE;
2799
- }
2800
- }
2801
-
2802
2829
static PyObject*
2803
2830
show (PyObject* self)
2804
2831
{
@@ -2825,6 +2852,7 @@ - (int)index
2825
2852
static PyObject*
2826
2853
Timer_new (PyTypeObject* type, PyObject *args, PyObject *kwds)
2827
2854
{
2855
+ lazy_init ();
2828
2856
Timer* self = (Timer*)type->tp_alloc (type, 0 );
2829
2857
if (!self) return NULL ;
2830
2858
self->timer = NULL ;
@@ -3051,7 +3079,7 @@ static bool verify_framework(void)
3051
3079
{" event_loop_is_running" ,
3052
3080
(PyCFunction)event_loop_is_running,
3053
3081
METH_NOARGS,
3054
- " Return whether the NSApp main event loop is currently running ."
3082
+ " Return whether the OSX backend has set up the NSApp main event loop."
3055
3083
},
3056
3084
{" show" ,
3057
3085
(PyCFunction)show,
@@ -3097,13 +3125,12 @@ static bool verify_framework(void)
3097
3125
|| PyType_Ready (&TimerType) < 0 )
3098
3126
return NULL ;
3099
3127
3100
- NSApp = [NSApplication sharedApplication ];
3101
-
3102
3128
if (!verify_framework ())
3103
3129
return NULL ;
3104
3130
3105
3131
module = PyModule_Create (&moduledef);
3106
- if (module==NULL ) return NULL ;
3132
+ if (!module)
3133
+ return NULL ;
3107
3134
3108
3135
Py_INCREF (&FigureCanvasType);
3109
3136
Py_INCREF (&FigureManagerType);
@@ -3116,16 +3143,5 @@ static bool verify_framework(void)
3116
3143
PyModule_AddObject (module, " NavigationToolbar2" , (PyObject*) &NavigationToolbar2Type);
3117
3144
PyModule_AddObject (module, " Timer" , (PyObject*) &TimerType);
3118
3145
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 ];
3130
3146
return module;
3131
3147
}
0 commit comments