Skip to content

Commit 6219b4f

Browse files
committed
Shorten PyObjectType defs in macosx.m.
Follow the same pattern as other extension modules, and just initialize whatever fields are needed. The PyTypeObject doesn't need to be memset() to zero as globals are guaranteed to be zero-inited. It also doesn't need to be incref'd (this is consistent with other extension modules), matching the docs for PyModule_AddObject.
1 parent 728f0e4 commit 6219b4f

File tree

1 file changed

+50
-173
lines changed

1 file changed

+50
-173
lines changed

src/_macosx.m

Lines changed: 50 additions & 173 deletions
Original file line numberDiff line numberDiff line change
@@ -567,46 +567,7 @@ static CGFloat _get_device_scale(CGContextRef cr)
567567
static char FigureCanvas_doc[] =
568568
"A FigureCanvas object wraps a Cocoa NSView object.\n";
569569

570-
static PyTypeObject FigureCanvasType = {
571-
PyVarObject_HEAD_INIT(NULL, 0)
572-
"_macosx.FigureCanvas", /*tp_name*/
573-
sizeof(FigureCanvas), /*tp_basicsize*/
574-
0, /*tp_itemsize*/
575-
(destructor)FigureCanvas_dealloc, /*tp_dealloc*/
576-
0, /*tp_print*/
577-
0, /*tp_getattr*/
578-
0, /*tp_setattr*/
579-
0, /*tp_compare*/
580-
(reprfunc)FigureCanvas_repr, /*tp_repr*/
581-
0, /*tp_as_number*/
582-
0, /*tp_as_sequence*/
583-
0, /*tp_as_mapping*/
584-
0, /*tp_hash */
585-
0, /*tp_call*/
586-
0, /*tp_str*/
587-
0, /*tp_getattro*/
588-
0, /*tp_setattro*/
589-
0, /*tp_as_buffer*/
590-
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
591-
FigureCanvas_doc, /* tp_doc */
592-
0, /* tp_traverse */
593-
0, /* tp_clear */
594-
0, /* tp_richcompare */
595-
0, /* tp_weaklistoffset */
596-
0, /* tp_iter */
597-
0, /* tp_iternext */
598-
FigureCanvas_methods, /* tp_methods */
599-
0, /* tp_members */
600-
0, /* tp_getset */
601-
0, /* tp_base */
602-
0, /* tp_dict */
603-
0, /* tp_descr_get */
604-
0, /* tp_descr_set */
605-
0, /* tp_dictoffset */
606-
(initproc)FigureCanvas_init, /* tp_init */
607-
0, /* tp_alloc */
608-
FigureCanvas_new, /* tp_new */
609-
};
570+
static PyTypeObject FigureCanvasType;
610571

611572
typedef struct {
612573
PyObject_HEAD
@@ -810,46 +771,7 @@ static CGFloat _get_device_scale(CGContextRef cr)
810771
static char FigureManager_doc[] =
811772
"A FigureManager object wraps a Cocoa NSWindow object.\n";
812773

813-
static PyTypeObject FigureManagerType = {
814-
PyVarObject_HEAD_INIT(NULL, 0)
815-
"_macosx.FigureManager", /*tp_name*/
816-
sizeof(FigureManager), /*tp_basicsize*/
817-
0, /*tp_itemsize*/
818-
(destructor)FigureManager_dealloc, /*tp_dealloc*/
819-
0, /*tp_print*/
820-
0, /*tp_getattr*/
821-
0, /*tp_setattr*/
822-
0, /*tp_compare*/
823-
(reprfunc)FigureManager_repr, /*tp_repr*/
824-
0, /*tp_as_number*/
825-
0, /*tp_as_sequence*/
826-
0, /*tp_as_mapping*/
827-
0, /*tp_hash */
828-
0, /*tp_call*/
829-
0, /*tp_str*/
830-
0, /*tp_getattro*/
831-
0, /*tp_setattro*/
832-
0, /*tp_as_buffer*/
833-
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
834-
FigureManager_doc, /* tp_doc */
835-
0, /* tp_traverse */
836-
0, /* tp_clear */
837-
0, /* tp_richcompare */
838-
0, /* tp_weaklistoffset */
839-
0, /* tp_iter */
840-
0, /* tp_iternext */
841-
FigureManager_methods, /* tp_methods */
842-
0, /* tp_members */
843-
0, /* tp_getset */
844-
0, /* tp_base */
845-
0, /* tp_dict */
846-
0, /* tp_descr_get */
847-
0, /* tp_descr_set */
848-
0, /* tp_dictoffset */
849-
(initproc)FigureManager_init, /* tp_init */
850-
0, /* tp_alloc */
851-
FigureManager_new, /* tp_new */
852-
};
774+
static PyTypeObject FigureManagerType;
853775

854776
@interface NavigationToolbar2Handler : NSObject {
855777
PyObject* toolbar;
@@ -1061,9 +983,6 @@ -(void)save_figure:(id)sender { gil_call_method(toolbar, "save_figure"); }
1061983
return PyUnicode_FromFormat("NavigationToolbar2 object %p", (void*)self);
1062984
}
1063985

1064-
static char NavigationToolbar2_doc[] =
1065-
"NavigationToolbar2\n";
1066-
1067986
static PyObject*
1068987
NavigationToolbar2_set_message(NavigationToolbar2 *self, PyObject* args)
1069988
{
@@ -1103,46 +1022,10 @@ -(void)save_figure:(id)sender { gil_call_method(toolbar, "save_figure"); }
11031022
{NULL} /* Sentinel */
11041023
};
11051024

1106-
static PyTypeObject NavigationToolbar2Type = {
1107-
PyVarObject_HEAD_INIT(NULL, 0)
1108-
"_macosx.NavigationToolbar2", /*tp_name*/
1109-
sizeof(NavigationToolbar2), /*tp_basicsize*/
1110-
0, /*tp_itemsize*/
1111-
(destructor)NavigationToolbar2_dealloc, /*tp_dealloc*/
1112-
0, /*tp_print*/
1113-
0, /*tp_getattr*/
1114-
0, /*tp_setattr*/
1115-
0, /*tp_compare*/
1116-
(reprfunc)NavigationToolbar2_repr, /*tp_repr*/
1117-
0, /*tp_as_number*/
1118-
0, /*tp_as_sequence*/
1119-
0, /*tp_as_mapping*/
1120-
0, /*tp_hash */
1121-
0, /*tp_call*/
1122-
0, /*tp_str*/
1123-
0, /*tp_getattro*/
1124-
0, /*tp_setattro*/
1125-
0, /*tp_as_buffer*/
1126-
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
1127-
NavigationToolbar2_doc, /* tp_doc */
1128-
0, /* tp_traverse */
1129-
0, /* tp_clear */
1130-
0, /* tp_richcompare */
1131-
0, /* tp_weaklistoffset */
1132-
0, /* tp_iter */
1133-
0, /* tp_iternext */
1134-
NavigationToolbar2_methods, /* tp_methods */
1135-
0, /* tp_members */
1136-
0, /* tp_getset */
1137-
0, /* tp_base */
1138-
0, /* tp_dict */
1139-
0, /* tp_descr_get */
1140-
0, /* tp_descr_set */
1141-
0, /* tp_dictoffset */
1142-
(initproc)NavigationToolbar2_init, /* tp_init */
1143-
0, /* tp_alloc */
1144-
NavigationToolbar2_new, /* tp_new */
1145-
};
1025+
static char NavigationToolbar2_doc[] =
1026+
"NavigationToolbar2\n";
1027+
1028+
static PyTypeObject NavigationToolbar2Type;
11461029

11471030
static PyObject*
11481031
choose_save_file(PyObject* unused, PyObject* args)
@@ -1888,9 +1771,6 @@ - (void)flagsChanged:(NSEvent*)event
18881771
(void*) self, (void*)(self->timer));
18891772
}
18901773

1891-
static char Timer_doc[] =
1892-
"A Timer object wraps a CFRunLoopTimerRef and can add it to the event loop.\n";
1893-
18941774
static void timer_callback(CFRunLoopTimerRef timer, void* info)
18951775
{
18961776
gil_call_method(info, "_on_timer");
@@ -1987,6 +1867,9 @@ static void context_cleanup(const void* info)
19871867
Py_TYPE(self)->tp_free((PyObject*)self);
19881868
}
19891869

1870+
static char Timer_doc[] =
1871+
"A Timer object wraps a CFRunLoopTimerRef and can add it to the event loop.\n";
1872+
19901873
static PyMethodDef Timer_methods[] = {
19911874
{"_timer_start",
19921875
(PyCFunction)Timer__timer_start,
@@ -2001,48 +1884,9 @@ static void context_cleanup(const void* info)
20011884
{NULL} /* Sentinel */
20021885
};
20031886

2004-
static PyTypeObject TimerType = {
2005-
PyVarObject_HEAD_INIT(NULL, 0)
2006-
"_macosx.Timer", /*tp_name*/
2007-
sizeof(Timer), /*tp_basicsize*/
2008-
0, /*tp_itemsize*/
2009-
(destructor)Timer_dealloc, /*tp_dealloc*/
2010-
0, /*tp_print*/
2011-
0, /*tp_getattr*/
2012-
0, /*tp_setattr*/
2013-
0, /*tp_compare*/
2014-
(reprfunc)Timer_repr, /*tp_repr*/
2015-
0, /*tp_as_number*/
2016-
0, /*tp_as_sequence*/
2017-
0, /*tp_as_mapping*/
2018-
0, /*tp_hash */
2019-
0, /*tp_call*/
2020-
0, /*tp_str*/
2021-
0, /*tp_getattro*/
2022-
0, /*tp_setattro*/
2023-
0, /*tp_as_buffer*/
2024-
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
2025-
Timer_doc, /* tp_doc */
2026-
0, /* tp_traverse */
2027-
0, /* tp_clear */
2028-
0, /* tp_richcompare */
2029-
0, /* tp_weaklistoffset */
2030-
0, /* tp_iter */
2031-
0, /* tp_iternext */
2032-
Timer_methods, /* tp_methods */
2033-
0, /* tp_members */
2034-
0, /* tp_getset */
2035-
0, /* tp_base */
2036-
0, /* tp_dict */
2037-
0, /* tp_descr_get */
2038-
0, /* tp_descr_set */
2039-
0, /* tp_dictoffset */
2040-
0, /* tp_init */
2041-
0, /* tp_alloc */
2042-
Timer_new, /* tp_new */
2043-
};
1887+
static PyTypeObject TimerType;
20441888

2045-
static struct PyMethodDef methods[] = {
1889+
static PyMethodDef methods[] = {
20461890
{"event_loop_is_running",
20471891
(PyCFunction)event_loop_is_running,
20481892
METH_NOARGS,
@@ -2077,23 +1921,56 @@ static void context_cleanup(const void* info)
20771921

20781922
PyObject* PyInit__macosx(void)
20791923
{
2080-
PyObject *module;
1924+
FigureCanvasType.tp_name = "_macosx.FigureCanvas";
1925+
FigureCanvasType.tp_basicsize = sizeof(FigureCanvas);
1926+
FigureCanvasType.tp_dealloc = FigureCanvas_dealloc;
1927+
FigureCanvasType.tp_repr = FigureCanvas_repr;
1928+
FigureCanvasType.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
1929+
FigureCanvasType.tp_doc = FigureCanvas_doc;
1930+
FigureCanvasType.tp_methods = FigureCanvas_methods;
1931+
FigureCanvasType.tp_init = FigureCanvas_init;
1932+
FigureCanvasType.tp_new = FigureCanvas_new;
1933+
1934+
FigureManagerType.tp_name = "_macosx.FigureManager";
1935+
FigureManagerType.tp_basicsize = sizeof(FigureManager);
1936+
FigureManagerType.tp_dealloc = FigureManager_dealloc;
1937+
FigureManagerType.tp_repr = FigureManager_repr;
1938+
FigureManagerType.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
1939+
FigureManagerType.tp_doc = FigureManager_doc;
1940+
FigureManagerType.tp_methods = FigureManager_methods;
1941+
FigureManagerType.tp_init = FigureManager_init;
1942+
FigureManagerType.tp_new = FigureManager_new;
1943+
1944+
NavigationToolbar2Type.tp_name = "_macosx.NavigationToolbar2";
1945+
NavigationToolbar2Type.tp_basicsize = sizeof(NavigationToolbar2);
1946+
NavigationToolbar2Type.tp_dealloc = NavigationToolbar2_dealloc;
1947+
NavigationToolbar2Type.tp_repr = NavigationToolbar2_repr;
1948+
NavigationToolbar2Type.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
1949+
NavigationToolbar2Type.tp_doc = NavigationToolbar2_doc;
1950+
NavigationToolbar2Type.tp_methods = NavigationToolbar2_methods;
1951+
NavigationToolbar2Type.tp_init = NavigationToolbar2_init;
1952+
NavigationToolbar2Type.tp_new = NavigationToolbar2_new;
1953+
1954+
TimerType.tp_name = "_macosx.Timer";
1955+
TimerType.tp_basicsize = sizeof(Timer);
1956+
TimerType.tp_dealloc = Timer_dealloc;
1957+
TimerType.tp_repr = Timer_repr;
1958+
TimerType.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
1959+
TimerType.tp_doc = Timer_doc;
1960+
TimerType.tp_methods = Timer_methods;
1961+
TimerType.tp_new = Timer_new;
20811962

20821963
if (PyType_Ready(&FigureCanvasType) < 0
20831964
|| PyType_Ready(&FigureManagerType) < 0
20841965
|| PyType_Ready(&NavigationToolbar2Type) < 0
20851966
|| PyType_Ready(&TimerType) < 0)
20861967
return NULL;
20871968

2088-
module = PyModule_Create(&moduledef);
1969+
PyObject *module = PyModule_Create(&moduledef);
20891970
if (!module) {
20901971
return NULL;
20911972
}
20921973

2093-
Py_INCREF(&FigureCanvasType);
2094-
Py_INCREF(&FigureManagerType);
2095-
Py_INCREF(&NavigationToolbar2Type);
2096-
Py_INCREF(&TimerType);
20971974
PyModule_AddObject(module, "FigureCanvas", (PyObject*) &FigureCanvasType);
20981975
PyModule_AddObject(module, "FigureManager", (PyObject*) &FigureManagerType);
20991976
PyModule_AddObject(module, "NavigationToolbar2", (PyObject*) &NavigationToolbar2Type);

0 commit comments

Comments
 (0)