55#include " numpy/arrayobject.h"
66#include " path_cleanup.h"
77
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+
819/* Proper way to check for the OS X version we are compiling for, from
920 http://developer.apple.com/documentation/DeveloperTools/Conceptual/cross_development */
1021#if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1050
@@ -488,14 +499,18 @@ static int _get_snap(GraphicsContext* self, enum e_snap_mode* mode)
488499 ngc--;
489500 if (ngc==0 ) _dealloc_atsui ();
490501
491- self-> ob_type ->tp_free ((PyObject*)self);
502+ Py_TYPE ( self) ->tp_free ((PyObject*)self);
492503}
493504#endif
494505
495506static PyObject*
496507GraphicsContext_repr (GraphicsContext* self)
497508{
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
498512 return PyString_FromFormat (" GraphicsContext object %p wrapping the Quartz 2D graphics context %p " , (void *)self, (void *)(self->cr ));
513+ #endif
499514}
500515
501516static PyObject*
@@ -2236,6 +2251,9 @@ static CGRect _find_enclosing_rect(CGPoint points[3])
22362251#else
22372252 ATSFontRef font = 0 ;
22382253#endif
2254+ #if PY_MAJOR_VERSION >= 3
2255+ PyObject* ascii = NULL ;
2256+ #endif
22392257
22402258 const int k = (strcmp (italic, " italic" ) ? 0 : 2 )
22412259 + (strcmp (weight, " bold" ) ? 0 : 1 );
@@ -2416,8 +2434,14 @@ static CGRect _find_enclosing_rect(CGPoint points[3])
24162434 for (i = 0 ; i < n; i++)
24172435 {
24182436 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
24192442 if (!PyString_Check (item)) return 0 ;
24202443 temp = PyString_AS_STRING (item);
2444+ #endif
24212445 for (j = 0 ; j < NMAP; j++)
24222446 { if (!strcmp (map[j].name , temp))
24232447 { temp = psnames[map[j].index][k];
@@ -2444,6 +2468,10 @@ static CGRect _find_enclosing_rect(CGPoint points[3])
24442468 name = temp;
24452469 break ;
24462470 }
2471+ #if PY_MAJOR_VERSION >= 3
2472+ Py_DECREF (ascii);
2473+ ascii = NULL ;
2474+ #endif
24472475 }
24482476 if (!font)
24492477 { string = CFStringCreateWithCString (kCFAllocatorDefault ,
@@ -2458,6 +2486,9 @@ static CGRect _find_enclosing_rect(CGPoint points[3])
24582486 }
24592487#ifndef COMPILING_FOR_10_5
24602488 CGContextSelectFont (cr, name, size, kCGEncodingMacRoman );
2489+ #endif
2490+ #if PY_MAJOR_VERSION >= 3
2491+ Py_XDECREF (ascii);
24612492#endif
24622493 return font;
24632494}
@@ -2958,11 +2989,19 @@ static void _data_provider_release(void* info, const void* data, size_t size)
29582989 CGDataProviderRef provider;
29592990 double rect[4 ] = {0.0 , 0.0 , self->size .width , self->size .height };
29602991
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
29612999 if (!PyString_Check (image))
29623000 {
29633001 PyErr_SetString (PyExc_RuntimeError, " image is not a string" );
29643002 return NULL ;
29653003 }
3004+ #endif
29663005
29673006 const size_t bytesPerComponent = 1 ;
29683007 const size_t bitsPerComponent = 8 * bytesPerComponent;
@@ -2978,8 +3017,13 @@ static void _data_provider_release(void* info, const void* data, size_t size)
29783017 }
29793018
29803019 Py_INCREF (image);
3020+ #if PY_MAJOR_VERSION >= 3
3021+ n = PyByteArray_GET_SIZE (image);
3022+ data = PyByteArray_AS_STRING (image);
3023+ #else
29813024 n = PyString_GET_SIZE (image);
29823025 data = PyString_AsString (image);
3026+ #endif
29833027
29843028 provider = CGDataProviderCreateWithData (image,
29853029 data,
@@ -3161,8 +3205,7 @@ static void _data_provider_release(void* info, const void* data, size_t size)
31613205" set_joinstyle, etc.).\n " ;
31623206
31633207static PyTypeObject GraphicsContextType = {
3164- PyObject_HEAD_INIT (NULL )
3165- 0 , /* ob_size*/
3208+ PyVarObject_HEAD_INIT (NULL , 0 )
31663209 " _macosx.GraphicsContext" , /* tp_name*/
31673210 sizeof (GraphicsContext), /* tp_basicsize*/
31683211 0 , /* tp_itemsize*/
@@ -3247,14 +3290,19 @@ static void _data_provider_release(void* info, const void* data, size_t size)
32473290 [self ->view setCanvas: NULL ];
32483291 [self ->view release ];
32493292 }
3250- self-> ob_type ->tp_free ((PyObject*)self);
3293+ Py_TYPE ( self) ->tp_free ((PyObject*)self);
32513294}
32523295
32533296static PyObject*
32543297FigureCanvas_repr (FigureCanvas* self)
32553298{
3299+ #if PY_MAJOR_VERSION >= 3
3300+ return PyUnicode_FromFormat (" FigureCanvas object %p wrapping NSView %p " ,
3301+ (void *)self, (void *)(self->view ));
3302+ #else
32563303 return PyString_FromFormat (" FigureCanvas object %p wrapping NSView %p " ,
32573304 (void *)self, (void *)(self->view ));
3305+ #endif
32583306}
32593307
32603308static PyObject*
@@ -3588,8 +3636,7 @@ static void _data_provider_release(void* info, const void* data, size_t size)
35883636" A FigureCanvas object wraps a Cocoa NSView object.\n " ;
35893637
35903638static PyTypeObject FigureCanvasType = {
3591- PyObject_HEAD_INIT (NULL )
3592- 0 , /* ob_size*/
3639+ PyVarObject_HEAD_INIT (NULL , 0 )
35933640 " _macosx.FigureCanvas" , /* tp_name*/
35943641 sizeof (FigureCanvas), /* tp_basicsize*/
35953642 0 , /* tp_itemsize*/
@@ -3720,8 +3767,13 @@ static void _data_provider_release(void* info, const void* data, size_t size)
37203767static PyObject*
37213768FigureManager_repr (FigureManager* self)
37223769{
3770+ #if PY_MAJOR_VERSION >= 3
3771+ return PyUnicode_FromFormat (" FigureManager object %p wrapping NSWindow %p " ,
3772+ (void *) self, (void *)(self->window ));
3773+ #else
37233774 return PyString_FromFormat (" FigureManager object %p wrapping NSWindow %p " ,
37243775 (void *) self, (void *)(self->window ));
3776+ #endif
37253777}
37263778
37273779static void
@@ -3734,7 +3786,7 @@ static void _data_provider_release(void* info, const void* data, size_t size)
37343786 [window close ];
37353787 [pool release ];
37363788 }
3737- self-> ob_type ->tp_free ((PyObject*)self);
3789+ Py_TYPE ( self) ->tp_free ((PyObject*)self);
37383790}
37393791
37403792static PyObject*
@@ -3765,8 +3817,7 @@ static void _data_provider_release(void* info, const void* data, size_t size)
37653817" A FigureManager object wraps a Cocoa NSWindow object.\n " ;
37663818
37673819static PyTypeObject FigureManagerType = {
3768- PyObject_HEAD_INIT (NULL )
3769- 0 , /* ob_size*/
3820+ PyVarObject_HEAD_INIT (NULL , 0 )
37703821 " _macosx.FigureManager" , /* tp_name*/
37713822 sizeof (FigureManager), /* tp_basicsize*/
37723823 0 , /* tp_itemsize*/
@@ -4101,13 +4152,17 @@ -(void)save_figure:(id)sender
41014152NavigationToolbar_dealloc (NavigationToolbar *self)
41024153{
41034154 [self ->handler release ];
4104- self-> ob_type ->tp_free ((PyObject*)self);
4155+ Py_TYPE ( self) ->tp_free ((PyObject*)self);
41054156}
41064157
41074158static PyObject*
41084159NavigationToolbar_repr (NavigationToolbar* self)
41094160{
4161+ #if PY_MAJOR_VERSION >= 3
4162+ return PyUnicode_FromFormat (" NavigationToolbar object %p " , (void *)self);
4163+ #else
41104164 return PyString_FromFormat (" NavigationToolbar object %p " , (void *)self);
4165+ #endif
41114166}
41124167
41134168static char NavigationToolbar_doc[] =
@@ -4214,7 +4269,11 @@ -(void)save_figure:(id)sender
42144269 {
42154270 if (states[i]==1 )
42164271 {
4272+ #if PY_MAJOR_VERSION >= 3
4273+ PyList_SET_ITEM (list, j, PyLong_FromLong (i));
4274+ #else
42174275 PyList_SET_ITEM (list, j, PyInt_FromLong (i));
4276+ #endif
42184277 j++;
42194278 }
42204279 }
@@ -4237,8 +4296,7 @@ -(void)save_figure:(id)sender
42374296};
42384297
42394298static PyTypeObject NavigationToolbarType = {
4240- PyObject_HEAD_INIT (NULL )
4241- 0 , /* ob_size*/
4299+ PyVarObject_HEAD_INIT (NULL , 0 )
42424300 " _macosx.NavigationToolbar" , /* tp_name*/
42434301 sizeof (NavigationToolbar), /* tp_basicsize*/
42444302 0 , /* tp_itemsize*/
@@ -4623,13 +4681,17 @@ -(void)save_figure:(id)sender
46234681NavigationToolbar2_dealloc (NavigationToolbar2 *self)
46244682{
46254683 [self ->handler release ];
4626- self-> ob_type ->tp_free ((PyObject*)self);
4684+ Py_TYPE ( self) ->tp_free ((PyObject*)self);
46274685}
46284686
46294687static PyObject*
46304688NavigationToolbar2_repr (NavigationToolbar2* self)
46314689{
4690+ #if PY_MAJOR_VERSION >= 3
4691+ return PyUnicode_FromFormat (" NavigationToolbar2 object %p " , (void *)self);
4692+ #else
46324693 return PyString_FromFormat (" NavigationToolbar2 object %p " , (void *)self);
4694+ #endif
46334695}
46344696
46354697static char NavigationToolbar2_doc[] =
@@ -4662,8 +4724,7 @@ -(void)save_figure:(id)sender
46624724};
46634725
46644726static PyTypeObject NavigationToolbar2Type = {
4665- PyObject_HEAD_INIT (NULL )
4666- 0 , /* ob_size*/
4727+ PyVarObject_HEAD_INIT (NULL , 0 )
46674728 " _macosx.NavigationToolbar2" , /* tp_name*/
46684729 sizeof (NavigationToolbar2), /* tp_basicsize*/
46694730 0 , /* tp_itemsize*/
@@ -5539,14 +5600,19 @@ - (int)index
55395600 CFRelease (self->timer );
55405601 self->timer = NULL ;
55415602 }
5542- self-> ob_type ->tp_free ((PyObject*)self);
5603+ Py_TYPE ( self) ->tp_free ((PyObject*)self);
55435604}
55445605
55455606static PyObject*
55465607Timer_repr (Timer* self)
55475608{
5609+ #if PY_MAJOR_VERSION >= 3
5610+ return PyUnicode_FromFormat (" Timer object %p wrapping CFRunLoopTimerRef %p " ,
5611+ (void *) self, (void *)(self->timer ));
5612+ #else
55485613 return PyString_FromFormat (" Timer object %p wrapping CFRunLoopTimerRef %p " ,
55495614 (void *) self, (void *)(self->timer ));
5615+ #endif
55505616}
55515617
55525618static char Timer_doc[] =
@@ -5657,8 +5723,7 @@ static void timer_callback(CFRunLoopTimerRef timer, void* info)
56575723};
56585724
56595725static PyTypeObject TimerType = {
5660- PyObject_HEAD_INIT (NULL )
5661- 0 , /* ob_size*/
5726+ PyVarObject_HEAD_INIT (NULL , 0 )
56625727 " _macosx.Timer" , /* tp_name*/
56635728 sizeof (Timer), /* tp_basicsize*/
56645729 0 , /* tp_itemsize*/
@@ -5722,36 +5787,65 @@ static void timer_callback(CFRunLoopTimerRef timer, void* info)
57225787 {NULL , NULL , 0 , NULL }/* sentinel */
57235788};
57245789
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+
57255808void init_macosx (void )
5726- { PyObject *m;
5809+ #endif
5810+ { PyObject *module;
57275811
57285812 import_array ();
57295813
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
57365825
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
57425836
57435837 Py_INCREF (&GraphicsContextType);
57445838 Py_INCREF (&FigureCanvasType);
57455839 Py_INCREF (&FigureManagerType);
57465840 Py_INCREF (&NavigationToolbarType);
57475841 Py_INCREF (&NavigationToolbar2Type);
57485842 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);
57555849
57565850 PyOS_InputHook = wait_for_stdin;
57575851}
0 commit comments