@@ -267,6 +267,28 @@ - (int)index;
267
267
268
268
/* ---------------------------- Python classes ---------------------------- */
269
269
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
+
270
292
static CGFloat _get_device_scale (CGContextRef cr)
271
293
{
272
294
CGSize pixelSize = CGContextConvertSizeToDeviceSpace (cr, CGSizeMake (1 , 1 ));
@@ -281,6 +303,7 @@ static CGFloat _get_device_scale(CGContextRef cr)
281
303
static PyObject*
282
304
FigureCanvas_new (PyTypeObject *type, PyObject *args, PyObject *kwds)
283
305
{
306
+ lazy_init ();
284
307
FigureCanvas *self = (FigureCanvas*)type->tp_alloc (type, 0 );
285
308
if (!self) return NULL ;
286
309
self->view = [View alloc ];
@@ -641,6 +664,7 @@ static CGFloat _get_device_scale(CGContextRef cr)
641
664
static PyObject*
642
665
FigureManager_new (PyTypeObject *type, PyObject *args, PyObject *kwds)
643
666
{
667
+ lazy_init ();
644
668
Window* window = [Window alloc ];
645
669
if (!window) return NULL ;
646
670
FigureManager *self = (FigureManager*)type->tp_alloc (type, 0 );
@@ -1016,6 +1040,7 @@ -(void)save_figure:(id)sender
1016
1040
static PyObject*
1017
1041
NavigationToolbar_new (PyTypeObject *type, PyObject *args, PyObject *kwds)
1018
1042
{
1043
+ lazy_init ();
1019
1044
NavigationToolbarHandler* handler = [NavigationToolbarHandler alloc ];
1020
1045
if (!handler) return NULL ;
1021
1046
NavigationToolbar *self = (NavigationToolbar*)type->tp_alloc (type, 0 );
@@ -1555,6 +1580,7 @@ -(void)save_figure:(id)sender
1555
1580
static PyObject*
1556
1581
NavigationToolbar2_new (PyTypeObject *type, PyObject *args, PyObject *kwds)
1557
1582
{
1583
+ lazy_init ();
1558
1584
NavigationToolbar2Handler* handler = [NavigationToolbar2Handler alloc ];
1559
1585
if (!handler) return NULL ;
1560
1586
NavigationToolbar2 *self = (NavigationToolbar2*)type->tp_alloc (type, 0 );
@@ -2825,6 +2851,7 @@ - (int)index
2825
2851
static PyObject*
2826
2852
Timer_new (PyTypeObject* type, PyObject *args, PyObject *kwds)
2827
2853
{
2854
+ lazy_init ();
2828
2855
Timer* self = (Timer*)type->tp_alloc (type, 0 );
2829
2856
if (!self) return NULL ;
2830
2857
self->timer = NULL ;
@@ -3097,13 +3124,12 @@ static bool verify_framework(void)
3097
3124
|| PyType_Ready (&TimerType) < 0 )
3098
3125
return NULL ;
3099
3126
3100
- NSApp = [NSApplication sharedApplication ];
3101
-
3102
3127
if (!verify_framework ())
3103
3128
return NULL ;
3104
3129
3105
3130
module = PyModule_Create (&moduledef);
3106
- if (module==NULL ) return NULL ;
3131
+ if (!module)
3132
+ return NULL ;
3107
3133
3108
3134
Py_INCREF (&FigureCanvasType);
3109
3135
Py_INCREF (&FigureManagerType);
@@ -3116,16 +3142,5 @@ static bool verify_framework(void)
3116
3142
PyModule_AddObject (module, " NavigationToolbar2" , (PyObject*) &NavigationToolbar2Type);
3117
3143
PyModule_AddObject (module, " Timer" , (PyObject*) &TimerType);
3118
3144
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
3145
return module;
3131
3146
}
0 commit comments