8000 Merge pull request #27874 from QuLogic/modernize-macosx · matplotlib/matplotlib@e16967f · GitHub
[go: up one dir, main page]

Skip to content

Commit e16967f

Browse files
authored
Merge pull request #27874 from QuLogic/modernize-macosx
Modernize macosx backend a bit
2 parents 3323ae8 + bffdc18 commit e16967f

File tree

1 file changed

+62
-47
lines changed

1 file changed

+62
-47
lines changed

src/_macosx.m

Lines changed: 62 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#include <Cocoa/Cocoa.h>
33
#include <ApplicationServices/ApplicationServices.h>
44
#include <Python.h>
5-
#include "mplutils.h"
65

76
/* Proper way to check for the OS X version we are compiling for, from
87
* https://developer.apple.com/library/archive/documentation/DeveloperTools/Conceptual/cross_development/Using/using.html
@@ -258,20 +257,21 @@ static CGFloat _get_device_scale(CGContextRef cr)
258257
return pixelSize.width;
259258
}
260259

261-
int mpl_check_modifier(
260+
bool
261+
mpl_check_modifier(
262262
NSUInteger modifiers, NSEventModifierFlags flag,
263263
PyObject* list, char const* name)
264264
{
265-
int status = 0;
265+
bool failed = false;
266266
if (modifiers & flag) {
267267
PyObject* py_name = NULL;
268268
if (!(py_name = PyUnicode_FromString(name))
269269
|| PyList_Append(list, py_name)) {
270-
status = -1; // failure
270+
failed = true;
271271
}
272272
Py_XDECREF(py_name);
273273
}
274-
return status;
274+
return failed;
275275
}
276276

277277
PyObject* mpl_modifiers(NSEvent* event)
@@ -498,14 +498,16 @@ int mpl_check_modifier(
498498

499499
static PyTypeObject FigureCanvasType = {
500500
PyVarObject_HEAD_INIT(NULL, 0)
501-
.tp_name = "_macosx.FigureCanvas",
501+
.tp_name = "matplotlib.backends._macosx.FigureCanvas",
502+
.tp_doc = PyDoc_STR("A FigureCanvas object wraps a Cocoa NSView object."),
502503
.tp_basicsize = sizeof(FigureCanvas),
503-
.tp_dealloc = (destructor)FigureCanvas_dealloc,
504-
.tp_repr = (reprfunc)FigureCanvas_repr,
505504
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
506-
.tp_init = (initproc)FigureCanvas_init,
505+
507506
.tp_new = (newfunc)FigureCanvas_new,
508-
.tp_doc = "A FigureCanvas object wraps a Cocoa NSView object.",
507+
.tp_init = (initproc)FigureCanvas_init,
508+
.tp_dealloc = (destructor)FigureCanvas_dealloc,
509+
.tp_repr = (reprfunc)FigureCanvas_repr,
510+
509511
.tp_methods = (PyMethodDef[]){
510512
{"update",
511513
(PyCFunction)FigureCanvas_update,
@@ -518,15 +520,15 @@ int mpl_check_modifier(
518520
{"set_cursor",
519521
(PyCFunction)FigureCanvas_set_cursor,
520522
METH_VARARGS,
521-
"Set the active cursor."},
523+
PyDoc_STR("Set the active cursor.")},
522524
{"set_rubberband",
523525
(PyCFunction)FigureCanvas_set_rubberband,
524526
METH_VARARGS,
525-
"Specify a new rubberband rectangle and invalidate it."},
527+
PyDoc_STR("Specify a new rubberband rectangle and invalidate it.")},
526528
{"remove_rubberband",
527529
(PyCFunction)FigureCanvas_remove_rubberband,
528530
METH_NOARGS,
529-
"Remove the current rubberband rectangle."},
531+
PyDoc_STR("Remove the current rubberband rectangle.")},
530532
{"_start_event_loop",
531533
(PyCFunction)FigureCanvas__start_event_loop,
532534
METH_KEYWORDS | METH_VARARGS,
@@ -745,14 +747,16 @@ int mpl_check_modifier(
745747

746748
static PyTypeObject FigureManagerType = {
747749
PyVarObject_HEAD_INIT(NULL, 0)
748-
.tp_name = "_macosx.FigureManager",
750+
.tp_name = "matplotlib.backends._macosx.FigureManager",
751+
.tp_doc = PyDoc_STR("A FigureManager object wraps a Cocoa NSWindow object."),
749752
.tp_basicsize = sizeof(FigureManager),
750-
.tp_dealloc = (destructor)FigureManager_dealloc,
751-
.tp_repr = (reprfunc)FigureManager_repr,
752753
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
753-
.tp_init = (initproc)FigureManager_init,
754+
754755
.tp_new = (newfunc)FigureManager_new,
755-
.tp_doc = "A FigureManager object wraps a Cocoa NSWindow object.",
756+
.tp_init = (initproc)FigureManager_init,
757+
.tp_dealloc = (destructor)FigureManager_dealloc,
758+
.tp_repr = (reprfunc)FigureManager_repr,
759+
756760
.tp_methods = (PyMethodDef[]){ // All docstrings are inherited.
757761
{"_show",
758762
(PyCFunction)FigureManager__show,
@@ -766,11 +770,11 @@ int mpl_check_modifier(
766770
{"_set_window_mode",
767771
(PyCFunction)FigureManager__set_window_mode,
768772
METH_VARARGS,
769-
"Set the window open mode (system, tab, window)"},
773+
PyDoc_STR("Set the window open mode (system, tab, window)")},
770774
{"set_icon",
771775
(PyCFunction)FigureManager_set_icon,
772776
METH_STATIC | METH_VARARGS,
773-
"Set application icon"},
777+
PyDoc_STR("Set application icon")},
774778
{"set_window_title",
775779
(PyCFunction)FigureManager_set_window_title,
776780
METH_VARARGS},
@@ -826,8 +830,7 @@ - (NavigationToolbar2Handler*)initWithToolbar:(PyObject*)theToolbar
826830

827831
- (void)installCallbacks:(SEL[7])actions forButtons:(NSButton*[7])buttons
828832
{
829-
int i;
830-
for (i = 0; i < 7; i++) {
833+
for (int i = 0; i < 7; i++) {
831834
SEL action = actions[i];
832835
NSButton* button = buttons[i];
833836
[button setTarget: self];
@@ -1036,14 +1039,16 @@ -(void)save_figure:(id)sender { gil_call_method(toolbar, "save_figure"); }
10361039

10371040
static PyTypeObject NavigationToolbar2Type = {
10381041
PyVarObject_HEAD_INIT(NULL, 0)
1039-
.tp_name = "_macosx.NavigationToolbar2",
1042+
.tp_name = "matplotlib.backends._macosx.NavigationToolbar2",
1043+
.tp_doc = PyDoc_STR("NavigationToolbar2"),
10401044
.tp_basicsize = sizeof(NavigationToolbar2),
1041-
.tp_dealloc = (destructor)NavigationToolbar2_dealloc,
1042-
.tp_repr = (reprfunc)NavigationToolbar2_repr,
10431045
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
1044-
.tp_init = (initproc)NavigationToolbar2_init,
1046+
10451047
.tp_new = (newfunc)NavigationToolbar2_new,
1046-
.tp_doc = "NavigationToolbar2",
1048+
.tp_init = (initproc)NavigationToolbar2_init,
1049+
.tp_dealloc = (destructor)NavigationToolbar2_dealloc,
1050+
.tp_repr = (reprfunc)NavigationToolbar2_repr,
1051+
10471052
.tp_methods = (PyMethodDef[]){ // All docstrings are inherited.
10481053
{"set_message",
10491054
(PyCFunction)NavigationToolbar2_set_message,
@@ -1782,13 +1787,16 @@ - (void)flagsChanged:(NSEvent *)event
17821787

17831788
static PyTypeObject TimerType = {
17841789
PyVarObject_HEAD_INIT(NULL, 0)
1785-
.tp_name = "_macosx.Timer",
1790+
.tp_name = "matplotlib.backends._macosx.Timer",
1791+
.tp_doc = PyDoc_STR("A Timer object that contains an NSTimer that gets added to "
1792+
"the event loop when started."),
17861793
.tp_basicsize = sizeof(Timer),
1787-
.tp_dealloc = (destructor)Timer_dealloc,
1788-
.tp_repr = (reprfunc)Timer_repr,
17891794
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
1795+
17901796
.tp_new = (newfunc)Timer_new,
1791-
.tp_doc = "A Timer object that contains an NSTimer that gets added to the event loop when started.",
1797+
.tp_dealloc = (destructor)Timer_dealloc,
1798+
.tp_repr = (reprfunc)Timer_repr,
1799+
17921800
.tp_methods = (PyMethodDef[]){ // All docstrings are inherited.
17931801
{"_timer_start",
17941802
(PyCFunction)Timer__timer_start,
@@ -1801,46 +1809,53 @@ - (void)flagsChanged:(NSEvent *)event
18011809
};
18021810

18031811
static struct PyModuleDef moduledef = {
1804-
PyModuleDef_HEAD_INIT, "_macosx", "Mac OS X native backend", -1,
1805-
(PyMethodDef[]){
1812+
.m_base = PyModuleDef_HEAD_INIT,
1813+
.m_name = "_macosx",
1814+
.m_doc = PyDoc_STR("Mac OS X native backend"),
1815+
.m_size = -1,
1816+
.m_methods = (PyMethodDef[]){
18061817
{"event_loop_is_running",
18071818
(PyCFunction)event_loop_is_running,
18081819
METH_NOARGS,
1809-
"Return whether the macosx backend has set up the NSApp main event loop."},
1820+
PyDoc_STR(
1821+
"Return whether the macosx backend has set up the NSApp main event loop.")},
18101822
{"wake_on_fd_write",
18111823
(PyCFunction)wake_on_fd_write,
18121824
METH_VARARGS,
1813-
"Arrange for Python to invoke its signal handlers when (any) data is\n"
1814-
"written on the file descriptor given as argument."},
1825+
PyDoc_STR(
1826+
"Arrange for Python to invoke its signal handlers when (any) data is\n"
1827+
"written on the file descriptor given as argument.")},
18151828
{"stop",
18161829
(PyCFunction)stop,
18171830
METH_NOARGS,
1818-
"Stop the NSApp."},
1831+
PyDoc_STR("Stop the NSApp.")},
18191832
{"show",
18201833
(PyCFunction)show,
18211834
METH_NOARGS,
1822-
"Show all the figures and enter the main loop.\n"
1823-
"\n"
1824-
"This function does not return until all Matplotlib windows are closed,\n"
1825-
"and is normally not needed in interactive sessions."},
1835+
PyDoc_STR(
1836+
"Show all the figures and enter the main loop.\n"
1837+
"\n"
1838+
"This function does not return until all Matplotlib windows are closed,\n"
1839+
"and is normally not needed in interactive sessions.")},
18261840
{"choose_save_file",
18271841
(PyCFunction)choose_save_file,
18281842
METH_VARARGS,
1829-
"Query the user for a location where to save a file."},
1843+
PyDoc_STR("Query the user for a location where to save a file.")},
18301844
{} /* Sentinel */
18311845
},
18321846
};
18331847

18341848
#pragma GCC visibility push(default)
18351849

1836-
PyObject* PyInit__macosx(void)
1850+
PyMODINIT_FUNC
1851+
PyInit__macosx(void)
18371852
{
18381853
PyObject *m;
18391854
if (!(m = PyModule_Create(&moduledef))
1840-
|| prepare_and_add_type(&FigureCanvasType, m)
1841-
|| prepare_and_add_type(&FigureManagerType, m)
1842-
|| prepare_and_add_type(&NavigationToolbar2Type, m)
1843-
|| prepare_and_add_type(&TimerType, m)) {
1855+
|| PyModule_AddType(m, &FigureCanvasType)
1856+
|| PyModule_AddType(m, &FigureManagerType)
1857+
|| PyModule_AddType(m, &NavigationToolbar2Type)
1858+
|| PyModule_AddType(m, &TimerType)) {
18441859
Py_XDECREF(m);
18451860
return NULL;
18461861
}

0 commit comments

Comments
 (0)
0