Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d2f87e8

Browse files
authored
Merge pull request #23060 from greglucas/macosx-ARC
MNT: Change objective C code to Automatic Reference Counting (ARC)
2 parents 3eadeac + ec0a325 commit d2f87e8

File tree

2 files changed

+15
-50
lines changed

2 files changed

+15
-50
lines changed

setupext.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,7 @@ def get_extensions(self):
762762
'matplotlib.backends._macosx', [
763763
'src/_macosx.m'
764764
])
765-
ext.extra_compile_args.extend(['-Werror'])
765+
ext.extra_compile_args.extend(['-Werror', '-fobjc-arc'])
766766
ext.extra_link_args.extend(['-framework', 'Cocoa'])
767767
if platform.python_implementation().lower() == 'pypy':
768768
ext.extra_compile_args.append('-DPYPY=1')

src/_macosx.m

Lines changed: 14 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
#if ! __has_feature(objc_arc)
2+
#error This file must be compiled with ARC. Use -fobjc-arc flag (or convert project to ARC).
3+
#endif
4+
15
#define PY_SSIZE_T_CLEAN
26
#include <Cocoa/Cocoa.h>
37
#include <ApplicationServices/ApplicationServices.h>
@@ -333,15 +337,14 @@ static CGFloat _get_device_scale(CGContextRef cr)
333337
FigureCanvas_dealloc(FigureCanvas* self)
334338
{
335339
[self->view setCanvas: NULL];
336-
[self->view release];
337340
Py_TYPE(self)->tp_free((PyObject*)self);
338341
}
339342

340343
static PyObject*
341344
FigureCanvas_repr(FigureCanvas* self)
342345
{
343346
return PyUnicode_FromFormat("FigureCanvas object %p wrapping NSView %p",
344-
(void*)self, (void*)(self->view));
347+
(void*)self, (__bridge void*)(self->view));
345348
}
346349

347350
static PyObject*
@@ -545,7 +548,6 @@ static CGFloat _get_device_scale(CGContextRef cr)
545548
if (!window) { return NULL; }
546549
FigureManager *self = (FigureManager*)type->tp_alloc(type, 0);
547550
if (!self) {
548-
[window release];
549551
return NULL;
550552
}
551553
self->window = window;
@@ -598,7 +600,7 @@ static CGFloat _get_device_scale(CGContextRef cr)
598600
FigureManager_repr(FigureManager* self)
599601
{
600602
return PyUnicode_FromFormat("FigureManager object %p wrapping NSWindow %p",
601-
(void*) self, (void*)(self->window));
603+
(void*) self, (__bridge void*)(self->window));
602604
}
603605

604606
static void
@@ -648,7 +650,7 @@ static CGFloat _get_device_scale(CGContextRef cr)
648650
PyErr_SetString(PyExc_RuntimeError, "Could not convert to NSString*");
649651
return NULL;
650652
}
651-
NSImage* image = [[[NSImage alloc] initByReferencingFile: ns_icon_path] autorelease];
653+
NSImage* image = [[NSImage alloc] initByReferencingFile: ns_icon_path];
652654
if (!image) {
653655
PyErr_SetString(PyExc_RuntimeError, "Could not create NSImage*");
654656
return NULL;
@@ -677,9 +679,9 @@ static CGFloat _get_device_scale(CGContextRef cr)
677679
if (!PyArg_ParseTuple(args, "s", &title)) {
678680
return NULL;
679681
}
680-
NSString* ns_title = [[[NSString alloc]
681-
initWithCString: title
682-
encoding: NSUTF8StringEncoding] autorelease];
682+
NSString* ns_title = [[NSString alloc]
683+
initWithCString: title
684+
encoding: NSUTF8StringEncoding];
683685
[self->window setTitle: ns_title];
684686
Py_RETURN_NONE;
685687
}
@@ -766,7 +768,7 @@ @interface NavigationToolbar2Handler : NSObject
766768
NSButton* zoombutton;
767769
}
768770
- (NavigationToolbar2Handler*)initWithToolbar:(PyObject*)toolbar;
769-
- (void)installCallbacks:(SEL[7])actions forButtons:(NSButton*[7])buttons;
771+
- (void)installCallbacks:(SEL[7])actions forButtons:(NSButton*__strong [7])buttons;
770772
- (void)home:(id)sender;
771773
- (void)back:(id)sender;
772774
- (void)forward:(id)sender;
@@ -787,12 +789,12 @@ - (void)save_figure:(id)sender;
787789
@implementation NavigationToolbar2Handler
788790
- (NavigationToolbar2Handler*)initWithToolbar:(PyObject*)theToolbar
789791
{
790-
[self init];
792+
self = [self init];
791793
toolbar = theToolbar;
792794
return self;
793795
}
794796

795-
- (void)installCallbacks:(SEL[7])actions forButtons:(NSButton*[7])buttons
797+
- (void)installCallbacks:(SEL[7])actions forButtons:(NSButton*__strong [7])buttons
796798
{
797799
int i;
798800
for (i = 0; i < 7; i++) {
@@ -833,7 +835,6 @@ -(void)save_figure:(id)sender { gil_call_method(toolbar, "save_figure"); }
833835
if (!handler) { return NULL; }
834836
NavigationToolbar2 *self = (NavigationToolbar2*)type->tp_alloc(type, 0);
835837
if (!self) {
836-
[handler release];
837838
return NULL;
838839
}
839840
self->handler = handler;
@@ -925,8 +926,6 @@ -(void)save_figure:(id)sender { gil_call_method(toolbar, "save_figure"); }
925926
[buttons[i] setImagePosition: NSImageOnly];
926927
[buttons[i] setToolTip: tooltip];
927928
[[window contentView] addSubview: buttons[i]];
928-
[buttons[i] release];
929-
[image release];
930929
rect.origin.x += rect.size.width + gap;
931930
}
932931

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

955953
self->messagebox = messagebox;
@@ -959,7 +957,6 @@ -(void)save_figure:(id)sender { gil_call_method(toolbar, "save_figure"); }
959957
static void
960958
NavigationToolbar2_dealloc(NavigationToolbar2 *self)
961959
{
962-
[self->handler release];
963960
Py_TYPE(self)->tp_free((PyObject*)self);
964961
}
965962

@@ -1060,36 +1057,6 @@ + (WindowServerConnectionManager *)sharedManager
10601057
return sharedWindowServerConnectionManager;
10611058
}
10621059

1063-
+ (id)allocWithZone:(NSZone *)zone
1064-
{
1065-
return [[self sharedManager] retain];
1066-
}
1067-
1068-
+ (id)copyWithZone:(NSZone *)zone
1069-
{
1070-
return self;
1071-
}
1072-
1073-
+ (id)retain
1074-
{
1075-
return self;
1076-
}
1077-
1078-
- (NSUInteger)retainCount
1079-
{
1080-
return NSUIntegerMax; //denotes an object that cannot be released
1081-
}
1082-
1083-
- (oneway void)release
1084-
{
1085-
// Don't release a singleton object
1086-
}
1087-
1088-
- (id)autorelease
1089-
{
1090-
return self;
1091-
}
1092-
10931060
- (void)launch:(NSNotification*)notification
10941061
{
10951062
CFRunLoopRef runloop;
@@ -1167,7 +1134,6 @@ - (void)dealloc
11671134
* content view of this window was increased during the call to addSubview,
11681135
* and is decreased during the call to [super dealloc].
11691136
*/
1170-
[super dealloc];
11711137
}
11721138
@end
11731139

@@ -1189,7 +1155,6 @@ - (void)dealloc
11891155
{
11901156
FigureCanvas* fc = (FigureCanvas*)canvas;
11911157
if (fc) { fc->view = NULL; }
1192-
[super dealloc];
11931158
}
11941159

11951160
- (void)setCanvas: (PyObject*)newCanvas

0 commit comments

Comments
 (0)