Skip to content

MNT: Change objective C code to Automatic Reference Counting (ARC) #23060

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion setupext.py
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,7 @@ def get_extensions(self):
'matplotlib.backends._macosx', [
'src/_macosx.m'
])
ext.extra_compile_args.extend(['-Werror'])
ext.extra_compile_args.extend(['-Werror', '-fobjc-arc'])
ext.extra_link_args.extend(['-framework', 'Cocoa'])
if platform.python_implementation().lower() == 'pypy':
ext.extra_compile_args.append('-DPYPY=1')
Expand Down
63 changes: 14 additions & 49 deletions src/_macosx.m
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#if ! __has_feature(objc_arc)
#error This file must be compiled with ARC. Use -fobjc-arc flag (or convert project to ARC).
#endif

#define PY_SSIZE_T_CLEAN
#include <Cocoa/Cocoa.h>
#include <ApplicationServices/ApplicationServices.h>
Expand Down Expand Up @@ -333,15 +337,14 @@ static CGFloat _get_device_scale(CGContextRef cr)
FigureCanvas_dealloc(FigureCanvas* self)
{
[self->view setCanvas: NULL];
[self->view release];
Py_TYPE(self)->tp_free((PyObject*)self);
}

static PyObject*
FigureCanvas_repr(FigureCanvas* self)
{
return PyUnicode_FromFormat("FigureCanvas object %p wrapping NSView %p",
(void*)self, (void*)(self->view));
(void*)self, (__bridge void*)(self->view));
}

static PyObject*
Expand Down Expand Up @@ -545,7 +548,6 @@ static CGFloat _get_device_scale(CGContextRef cr)
if (!window) { return NULL; }
FigureManager *self = (FigureManager*)type->tp_alloc(type, 0);
if (!self) {
[window release];
return NULL;
}
self->window = window;
Expand Down Expand Up @@ -598,7 +600,7 @@ static CGFloat _get_device_scale(CGContextRef cr)
FigureManager_repr(FigureManager* self)
{
return PyUnicode_FromFormat("FigureManager object %p wrapping NSWindow %p",
(void*) self, (void*)(self->window));
(void*) self, (__bridge void*)(self->window));
}

static void
Expand Down Expand Up @@ -648,7 +650,7 @@ static CGFloat _get_device_scale(CGContextRef cr)
PyErr_SetString(PyExc_RuntimeError, "Could not convert to NSString*");
return NULL;
}
NSImage* image = [[[NSImage alloc] initByReferencingFile: ns_icon_path] autorelease];
NSImage* image = [[NSImage alloc] initByReferencingFile: ns_icon_path];
if (!image) {
PyErr_SetString(PyExc_RuntimeError, "Could not create NSImage*");
return NULL;
Expand Down Expand Up @@ -677,9 +679,9 @@ static CGFloat _get_device_scale(CGContextRef cr)
if (!PyArg_ParseTuple(args, "s", &title)) {
return NULL;
}
NSString* ns_title = [[[NSString alloc]
initWithCString: title
encoding: NSUTF8StringEncoding] autorelease];
NSString* ns_title = [[NSString alloc]
initWithCString: title
encoding: NSUTF8StringEncoding];
[self->window setTitle: ns_title];
Py_RETURN_NONE;
}
Expand Down Expand Up @@ -766,7 +768,7 @@ @interface NavigationToolbar2Handler : NSObject
NSButton* zoombutton;
}
- (NavigationToolbar2Handler*)initWithToolbar:(PyObject*)toolbar;
- (void)installCallbacks:(SEL[7])actions forButtons:(NSButton*[7])buttons;
- (void)installCallbacks:(SEL[7])actions forButtons:(NSButton*__strong [7])buttons;
- (void)home:(id)sender;
- (void)back:(id)sender;
- (void)forward:(id)sender;
Expand All @@ -787,12 +789,12 @@ - (void)save_figure:(id)sender;
@implementation NavigationToolbar2Handler
- (NavigationToolbar2Handler*)initWithToolbar:(PyObject*)theToolbar
{
[self init];
self = [self init];
toolbar = theToolbar;
return self;
}

- (void)installCallbacks:(SEL[7])actions forButtons:(NSButton*[7])buttons
- (void)installCallbacks:(SEL[7])actions forButtons:(NSButton*__strong [7])buttons
{
int i;
for (i = 0; i < 7; i++) {
Expand Down Expand Up @@ -833,7 +835,6 @@ -(void)save_figure:(id)sender { gil_call_method(toolbar, "save_figure"); }
if (!handler) { return NULL; }
NavigationToolbar2 *self = (NavigationToolbar2*)type->tp_alloc(type, 0);
if (!self) {
[handler release];
return NULL;
}
self->handler = handler;
Expand Down Expand Up @@ -925,8 +926,6 @@ -(void)save_figure:(id)sender { gil_call_method(toolbar, "save_figure"); }
[buttons[i] setImagePosition: NSImageOnly];
[buttons[i] setToolTip: tooltip];
[[window contentView] addSubview: buttons[i]];
[buttons[i] release];
[image release];
rect.origin.x += rect.size.width + gap;
}

Expand All @@ -939,7 +938,7 @@ -(void)save_figure:(id)sender { gil_call_method(toolbar, "save_figure"); }
// Make it a zero-width box if we don't have enough room
rect.size.width = fmax(bounds.size.width - rect.origin.x, 0);
rect.origin.x = bounds.size.width - rect.size.width;
NSTextView* messagebox = [[[NSTextView alloc] initWithFrame: rect] autorelease];
NSTextView* messagebox = [[NSTextView alloc] initWithFrame: rect];
messagebox.textContainer.maximumNumberOfLines = 2;
messagebox.textContainer.lineBreakMode = NSLineBreakByTruncatingTail;
messagebox.alignment = NSTextAlignmentRight;
Expand All @@ -949,7 +948,6 @@ -(void)save_figure:(id)sender { gil_call_method(toolbar, "save_figure"); }
/* if selectable, the messagebox can become first responder,
* which is not supposed to happen */
[[window contentView] addSubview: messagebox];
[messagebox release];
[[window contentView] display];

self->messagebox = messagebox;
Expand All @@ -959,7 +957,6 @@ -(void)save_figure:(id)sender { gil_call_method(toolbar, "save_figure"); }
static void
NavigationToolbar2_dealloc(NavigationToolbar2 *self)
{
[self->handler release];
Py_TYPE(self)->tp_free((PyObject*)self);
}

Expand Down Expand Up @@ -1060,36 +1057,6 @@ + (WindowServerConnectionManager *)sharedManager
return sharedWindowServerConnectionManager;
}

+ (id)allocWithZone:(NSZone *)zone
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://developer.apple.com/documentation/objectivec/nsobject/1571945-allocwithzone

Do not override allocWithZone: to include any initialization code. Instead, class-specific versions of init... methods.

OK, good thing we are deleting it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is also not clear we used this as the actual call site just above uses [[super allocWithZone:Null] init] ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This all came in via 7bacdc5 with the commit message

This patch allows interrupts to be delivered once Python is fixed.

{
return [[self sharedManager] retain];
}

+ (id)copyWithZone:(NSZone *)zone
{
return self;
}

+ (id)retain
{
return self;
}

- (NSUInteger)retainCount
{
return NSUIntegerMax; //denotes an object that cannot be released
}

- (oneway void)release
{
// Don't release a singleton object
}

- (id)autorelease
{
return self;
}

- (void)launch:(NSNotification*)notification
{
CFRunLoopRef runloop;
Expand Down Expand Up @@ -1167,7 +1134,6 @@ - (void)dealloc
* content view of this window was increased during the call to addSubview,
* and is decreased during the call to [super dealloc].
*/
[super dealloc];
}
@end

Expand All @@ -1189,7 +1155,6 @@ - (void)dealloc
{
FigureCanvas* fc = (FigureCanvas*)canvas;
if (fc) { fc->view = NULL; }
[super dealloc];
}

- (void)setCanvas: (PyObject*)newCanvas
Expand Down