5
5
#include " numpy/arrayobject.h"
6
6
#include " path_cleanup.h"
7
7
8
+ /* Must define Py_TYPE for Python 2.5 or older */
9
+ #ifndef Py_TYPE
10
+ # define Py_TYPE (o ) ((o)->ob_type)
11
+ #endif
12
+
13
+ /* Must define PyVarObject_HEAD_INIT for Python 2.5 or older */
14
+ #ifndef PyVarObject_HEAD_INIT
15
+ #define PyVarObject_HEAD_INIT (type, size ) \
16
+ PyObject_HEAD_INIT (type) size,
17
+ #endif
18
+
8
19
/* Proper way to check for the OS X version we are compiling for, from
9
20
http://developer.apple.com/documentation/DeveloperTools/Conceptual/cross_development */
10
21
#if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1050
@@ -488,14 +499,18 @@ static int _get_snap(GraphicsContext* self, enum e_snap_mode* mode)
488
499
ngc--;
489
500
if (ngc==0 ) _dealloc_atsui ();
490
501
491
- self-> ob_type ->tp_free ((PyObject*)self);
502
+ Py_TYPE ( self) ->tp_free ((PyObject*)self);
492
503
}
493
504
#endif
494
505
495
506
static PyObject*
496
507
GraphicsContext_repr (GraphicsContext* self)
497
508
{
509
+ #if PY_MAJOR_VERSION >= 3
510
+ return PyUnicode_FromFormat (" GraphicsContext object %p wrapping the Quartz 2D graphics context %p " , (void *)self, (void *)(self->cr ));
511
+ #else
498
512
return PyString_FromFormat (" GraphicsContext object %p wrapping the Quartz 2D graphics context %p " , (void *)self, (void *)(self->cr ));
513
+ #endif
499
514
}
500
515
501
516
static PyObject*
@@ -2236,6 +2251,9 @@ static CGRect _find_enclosing_rect(CGPoint points[3])
2236
2251
#else
2237
2252
ATSFontRef font = 0 ;
2238
2253
#endif
2254
+ #if PY_MAJOR_VERSION >= 3
2255
+ PyObject* ascii = NULL ;
2256
+ #endif
2239
2257
2240
2258
const int k = (strcmp (italic , " italic" ) ? 0 : 2 )
2241
2259
+ (strcmp (weight, " bold" ) ? 0 : 1 );
@@ -2416,8 +2434,14 @@ static CGRect _find_enclosing_rect(CGPoint points[3])
2416
2434
for (i = 0 ; i < n; i++)
2417
2435
{
2418
2436
PyObject* item = PyList_GET_ITEM (family, i);
2437
+ #if PY_MAJOR_VERSION >= 3
2438
+ ascii = PyUnicode_AsASCIIString (item);
2439
+ if (!ascii) return 0 ;
2440
+ temp = PyBytes_AS_STRING (ascii);
2441
+ #else
2419
2442
if (!PyString_Check (item)) return 0 ;
2420
2443
temp = PyString_AS_STRING (item);
2444
+ #endif
2421
2445
for (j = 0 ; j < NMAP; j++)
2422
2446
{ if (!strcmp (map[j].name , temp))
2423
2447
{ temp = psnames[map[j].index ][k];
@@ -2444,6 +2468,10 @@ static CGRect _find_enclosing_rect(CGPoint points[3])
2444
2468
name = temp;
2445
2469
break ;
2446
2470
}
2471
+ #if PY_MAJOR_VERSION >= 3
2472
+ Py_DECREF (ascii);
2473
+ ascii = NULL ;
2474
+ #endif
2447
2475
}
2448
2476
if (!font)
2449
2477
{ string = CFStringCreateWithCString (kCFAllocatorDefault ,
@@ -2458,6 +2486,9 @@ static CGRect _find_enclosing_rect(CGPoint points[3])
2458
2486
}
2459
2487
#ifndef COMPILING_FOR_10_5
2460
2488
CGContextSelectFont (cr, name, size, kCGEncodingMacRoman );
2489
+ #endif
2490
+ #if PY_MAJOR_VERSION >= 3
2491
+ Py_XDECREF (ascii);
2461
2492
#endif
2462
2493
return font;
2463
2494
}
@@ -2958,11 +2989,19 @@ static void _data_provider_release(void* info, const void* data, size_t size)
2958
2989
CGDataProviderRef provider;
2959
2990
double rect[4 ] = {0.0 , 0.0 , self->size .width , self->size .height };
2960
2991
2992
+ #if PY_MAJOR_VERSION >= 3
2993
+ if (!PyBytes_Check (image))
2994
+ {
2995
+ PyErr_SetString (PyExc_RuntimeError, " image is not a byte array" );
2996
+ return NULL ;
2997
+ }
2998
+ #else
2961
2999
if (!PyString_Check (image))
2962
3000
{
2963
3001
PyErr_SetString (PyExc_RuntimeError, " image is not a string" );
2964
3002
return NULL ;
2965
3003
}
3004
+ #endif
2966
3005
2967
3006
const size_t bytesPerComponent = 1 ;
2968
3007
const size_t bitsPerComponent = 8 * bytesPerComponent;
@@ -2978,8 +3017,13 @@ static void _data_provider_release(void* info, const void* data, size_t size)
2978
3017
}
2979
3018
2980
3019
Py_INCREF (image);
3020
+ #if PY_MAJOR_VERSION >= 3
3021
+ n = PyByteArray_GET_SIZE (image);
3022
+ data = PyByteArray_AS_STRING (image);
3023
+ #else
2981
3024
n = PyString_GET_SIZE (image);
2982
3025
data = PyString_AsString (image);
3026
+ #endif
2983
3027
2984
3028
provider = CGDataProviderCreateWithData (image,
2985
3029
data,
@@ -3161,8 +3205,7 @@ static void _data_provider_release(void* info, const void* data, size_t size)
3161
3205
" set_joinstyle, etc.).\n " ;
3162
3206
3163
3207
static PyTypeObject GraphicsContextType = {
3164
- PyObject_HEAD_INIT (NULL )
3165
- 0 , /* ob_size*/
3208
+ PyVarObject_HEAD_INIT (NULL , 0 )
3166
3209
" _macosx.GraphicsContext" , /* tp_name*/
3167
3210
sizeof (GraphicsContext), /* tp_basicsize*/
3168
3211
0 , /* tp_itemsize*/
@@ -3247,14 +3290,19 @@ static void _data_provider_release(void* info, const void* data, size_t size)
3247
3290
[self ->view setCanvas: NULL ];
3248
3291
[self ->view release ];
3249
3292
}
3250
- self-> ob_type ->tp_free ((PyObject*)self);
3293
+ Py_TYPE ( self) ->tp_free ((PyObject*)self);
3251
3294
}
3252
3295
3253
3296
static PyObject*
3254
3297
FigureCanvas_repr (FigureCanvas* self)
3255
3298
{
3299
+ #if PY_MAJOR_VERSION >= 3
3300
+ return PyUnicode_FromFormat (" FigureCanvas object %p wrapping NSView %p " ,
3301
+ (void *)self, (void *)(self->view ));
3302
+ #else
3256
3303
return PyString_FromFormat (" FigureCanvas object %p wrapping NSView %p " ,
3257
3304
(void *)self, (void *)(self->view ));
3305
+ #endif
3258
3306
}
3259
3307
3260
3308
static PyObject*
@@ -3588,8 +3636,7 @@ static void _data_provider_release(void* info, const void* data, size_t size)
3588
3636
" A FigureCanvas object wraps a Cocoa NSView object.\n " ;
3589
3637
3590
3638
static PyTypeObject FigureCanvasType = {
3591
- PyObject_HEAD_INIT (NULL )
3592
- 0 , /* ob_size*/
3639
+ PyVarObject_HEAD_INIT (NULL , 0 )
3593
3640
" _macosx.FigureCanvas" , /* tp_name*/
3594
3641
sizeof (FigureCanvas), /* tp_basicsize*/
3595
3642
0 , /* tp_itemsize*/
@@ -3720,8 +3767,13 @@ static void _data_provider_release(void* info, const void* data, size_t size)
3720
3767
static PyObject*
3721
3768
FigureManager_repr (FigureManager* self)
3722
3769
{
3770
+ #if PY_MAJOR_VERSION >= 3
3771
+ return PyUnicode_FromFormat (" FigureManager object %p wrapping NSWindow %p " ,
3772
+ (void *) self, (void *)(self->window ));
3773
+ #else
3723
3774
return PyString_FromFormat (" FigureManager object %p wrapping NSWindow %p " ,
3724
3775
(void *) self, (void *)(self->window ));
3776
+ #endif
3725
3777
}
3726
3778
3727
3779
static void
@@ -3734,7 +3786,7 @@ static void _data_provider_release(void* info, const void* data, size_t size)
3734
3786
[window close ];
3735
3787
[pool release ];
3736
3788
}
3737
- self-> ob_type ->tp_free ((PyObject*)self);
3789
+ Py_TYPE ( self) ->tp_free ((PyObject*)self);
3738
3790
}
3739
3791
3740
3792
static PyObject*
@@ -3765,8 +3817,7 @@ static void _data_provider_release(void* info, const void* data, size_t size)
3765
3817
" A FigureManager object wraps a Cocoa NSWindow object.\n " ;
3766
3818
3767
3819
static PyTypeObject FigureManagerType = {
3768
- PyObject_HEAD_INIT (NULL )
3769
- 0 , /* ob_size*/
3820
+ PyVarObject_HEAD_INIT (NULL , 0 )
3770
3821
" _macosx.FigureManager" , /* tp_name*/
3771
3822
sizeof (FigureManager), /* tp_basicsize*/
3772
3823
0 , /* tp_itemsize*/
@@ -4101,13 +4152,17 @@ -(void)save_figure:(id)sender
4101
4152
NavigationToolbar_dealloc (NavigationToolbar *self)
4102
4153
{
4103
4154
[self ->handler release ];
4104
- self-> ob_type ->tp_free ((PyObject*)self);
4155
+ Py_TYPE ( self) ->tp_free ((PyObject*)self);
4105
4156
}
4106
4157
4107
4158
static PyObject*
4108
4159
NavigationToolbar_repr (NavigationToolbar* self)
4109
4160
{
4161
+ #if PY_MAJOR_VERSION >= 3
4162
+ return PyUnicode_FromFormat (" NavigationToolbar object %p " , (void *)self);
4163
+ #else
4110
4164
return PyString_FromFormat (" NavigationToolbar object %p " , (void *)self);
4165
+ #endif
4111
4166
}
4112
4167
4113
4168
static char NavigationToolbar_doc[] =
@@ -4214,7 +4269,11 @@ -(void)save_figure:(id)sender
4214
4269
{
4215
4270
if (states[i]==1 )
4216
4271
{
4272
+ #if PY_MAJOR_VERSION >= 3
4273
+ PyList_SET_ITEM (list, j, PyLong_FromLong (i));
4274
+ #else
4217
4275
PyList_SET_ITEM (list, j, PyInt_FromLong (i));
4276
+ #endif
4218
4277
j++;
4219
4278
}
4220
4279
}
@@ -4237,8 +4296,7 @@ -(void)save_figure:(id)sender
4237
4296
};
4238
4297
4239
4298
static PyTypeObject NavigationToolbarType = {
4240
- PyObject_HEAD_INIT (NULL )
4241
- 0 , /* ob_size*/
4299
+ PyVarObject_HEAD_INIT (NULL , 0 )
4242
4300
" _macosx.NavigationToolbar" , /* tp_name*/
4243
4301
sizeof (NavigationToolbar), /* tp_basicsize*/
4244
4302
0 , /* tp_itemsize*/
@@ -4623,13 +4681,17 @@ -(void)save_figure:(id)sender
4623
4681
NavigationToolbar2_dealloc (NavigationToolbar2 *self)
4624
4682
{
4625
4683
[self ->handler release ];
4626
- self-> ob_type ->tp_free ((PyObject*)self);
4684
+ Py_TYPE ( self) ->tp_free ((PyObject*)self);
4627
4685
}
4628
4686
4629
4687
static PyObject*
4630
4688
NavigationToolbar2_repr (NavigationToolbar2* self)
4631
4689
{
4690
+ #if PY_MAJOR_VERSION >= 3
4691
+ return PyUnicode_FromFormat (" NavigationToolbar2 object %p " , (void *)self);
4692
+ #else
4632
4693
return PyString_FromFormat (" NavigationToolbar2 object %p " , (void *)self);
4694
+ #endif
4633
4695
}
4634
4696
4635
4697
static char NavigationToolbar2_doc[] =
@@ -4662,8 +4724,7 @@ -(void)save_figure:(id)sender
4662
4724
};
4663
4725
4664
4726
static PyTypeObject NavigationToolbar2Type = {
4665
- PyObject_HEAD_INIT (NULL )
4666
- 0 , /* ob_size*/
4727
+ PyVarObject_HEAD_INIT (NULL , 0 )
4667
4728
" _macosx.NavigationToolbar2" , /* tp_name*/
4668
4729
sizeof (NavigationToolbar2), /* tp_basicsize*/
4669
4730
0 , /* tp_itemsize*/
@@ -5539,14 +5600,19 @@ - (int)index
5539
5600
CFRelease (self->timer );
5540
5601
self->timer = NULL ;
5541
5602
}
5542
- self-> ob_type ->tp_free ((PyObject*)self);
5603
+ Py_TYPE ( self) ->tp_free ((PyObject*)self);
5543
5604
}
5544
5605
5545
5606
static PyObject*
5546
5607
Timer_repr (Timer* self)
5547
5608
{
5609
+ #if PY_MAJOR_VERSION >= 3
5610
+ return PyUnicode_FromFormat (" Timer object %p wrapping CFRunLoopTimerRef %p " ,
5611
+ (void *) self, (void *)(self->timer ));
5612
+ #else
5548
5613
return PyString_FromFormat (" Timer object %p wrapping CFRunLoopTimerRef %p " ,
5549
5614
(void *) self, (void *)(self->timer ));
5615
+ #endif
5550
5616
}
5551
5617
5552
5618
static char Timer_doc[] =
@@ -5657,8 +5723,7 @@ static void timer_callback(CFRunLoopTimerRef timer, void* info)
5657
5723
};
5658
5724
5659
5725
static PyTypeObject TimerType = {
5660
- PyObject_HEAD_INIT (NULL )
5661
- 0 , /* ob_size*/
5726
+ PyVarObject_HEAD_INIT (NULL , 0 )
5662
5727
" _macosx.Timer" , /* tp_name*/
5663
5728
sizeof (Timer), /* tp_basicsize*/
5664
5729
0 , /* tp_itemsize*/
@@ -5722,36 +5787,65 @@ static void timer_callback(CFRunLoopTimerRef timer, void* info)
5722
5787
{NULL , NULL , 0 , NULL }/* sentinel */
5723
5788
};
5724
5789
5790
+ #if PY_MAJOR_VERSION >= 3
5791
+
5792
+ static struct PyModuleDef moduledef = {
5793
+ PyModuleDef_HEAD_INIT,
5794
+ " _macosx" ,
5795
+ " Mac OS X native backend" ,
5796
+ -1 ,
5797
+ methods,
5798
+ NULL ,
5799
+ NULL ,
5800
+ NULL ,
5801
+ NULL
5802
+ };
5803
+
5804
+ PyObject* PyInit__macosx (void )
5805
+
5806
+ #else
5807
+
5725
5808
void init_macosx (void )
5726
- { PyObject *m;
5809
+ #endif
5810
+ { PyObject *module;
5727
5811
5728
5812
import_array ();
5729
5813
5730
- if (PyType_Ready (&GraphicsContextType) < 0 ) return ;
5731
- if (PyType_Ready (&FigureCanvasType) < 0 ) return ;
5732
- if (PyType_Ready (&FigureManagerType) < 0 ) return ;
5733
- if (PyType_Ready (&NavigationToolbarType) < 0 ) return ;
5734
- if (PyType_Ready (&NavigationToolbar2Type) < 0 ) return ;
5735
- if (PyType_Ready (&TimerType) < 0 ) return ;
5814
+ if (PyType_Ready (&GraphicsContextType) < 0
5815
+ || PyType_Ready (&FigureCanvasType) < 0
5816
+ || PyType_Ready (&FigureManagerType) < 0
5817
+ || PyType_Ready (&NavigationToolbarType) < 0
5818
+ || PyType_Ready (&NavigationToolbar2Type) < 0
5819
+ || PyType_Ready (&TimerType) < 0 )
5820
+ #if PY_MAJOR_VERSION >= 3
5821
+ return NULL ;
5822
+ #else
5823
+ return ;
5824
+ #endif
5736
5825
5737
- m = Py_InitModule4 (" _macosx" ,
5738
- methods,
5739
- " Mac OS X native backend" ,
5740
- NULL ,
5741
- PYTHON_API_VERSION);
5826
+ #if PY_MAJOR_VERSION >= 3
5827
+ module = PyModule_Create (&moduledef);
5828
+ if (module==NULL ) return NULL ;
5829
+ #else
5830
+ module = Py_InitModule4 (" _macosx" ,
5831
+ methods,
5832
+ " Mac OS X native backend" ,
5833
+ NULL ,
5834
+ PYTHON_API_VERSION);
5835
+ #endif
5742
5836
5743
5837
Py_INCREF (&GraphicsContextType);
5744
5838
Py_INCREF (&FigureCanvasType);
5745
5839
Py_INCREF (&FigureManagerType);
5746
5840
Py_INCREF (&NavigationToolbarType);
5747
5841
Py_INCREF (&NavigationToolbar2Type);
5748
5842
Py_INCREF (&TimerType);
5749
- PyModule_AddObject (m , " GraphicsContext" , (PyObject*) &GraphicsContextType);
5750
- PyModule_AddObject (m , " FigureCanvas" , (PyObject*) &FigureCanvasType);
5751
- PyModule_AddObject (m , " FigureManager" , (PyObject*) &FigureManagerType);
5752
- PyModule_AddObject (m , " NavigationToolbar" , (PyObject*) &NavigationToolbarType);
5753
- PyModule_AddObject (m , " NavigationToolbar2" , (PyObject*) &NavigationToolbar2Type);
5754
- PyModule_AddObject (m , " Timer" , (PyObject*) &TimerType);
5843
+ PyModule_AddObject (module , " GraphicsContext" , (PyObject*) &GraphicsContextType);
5844
+ PyModule_AddObject (module , " FigureCanvas" , (PyObject*) &FigureCanvasType);
5845
+ PyModule_AddObject (module , " FigureManager" , (PyObject*) &FigureManagerType);
5846
+ PyModule_AddObject (module , " NavigationToolbar" , (PyObject*) &NavigationToolbarType);
5847
+ PyModule_AddObject (module , " NavigationToolbar2" , (PyObject*) &NavigationToolbar2Type);
5848
+ PyModule_AddObject (module , " Timer" , (PyObject*) &TimerType);
5755
5849
5756
5850
PyOS_InputHook = wait_for_stdin;
5757
5851
}
0 commit comments