@@ -23,43 +23,30 @@ class Image:
23
23
24
24
def __init__ (self , interface , path = None , data = None , raw = None ):
25
25
self .interface = interface
26
- self ._needs_release = False
27
26
28
- try :
29
- # We *should* be able to do a direct NSImage.alloc.init...(), but if the
30
- # image file is invalid, the init fails, returns NULL, and releases the
31
- # Objective-C object. Since we've created an ObjC instance, when the object
32
- # passes out of scope, Rubicon tries to free it, which segfaults.
33
- # To avoid this, we retain result of the alloc() (overriding the default
34
- # Rubicon behavior of alloc), then release that reference once we're done.
35
- # If the image was created successfully, we temporarily have a reference
36
- # count that is 1 higher than it needs to be; if it fails, we don't end up
37
- # with a stray release.
38
- image = NSImage .alloc ().retain ()
39
- if path :
40
- self .native = image .initWithContentsOfFile (str (path ))
41
- if self .native is None :
42
- raise ValueError (f"Unable to load image from { path } " )
43
- else :
44
- self ._needs_release = True
45
- elif data :
46
- nsdata = NSData .dataWithBytes (data , length = len (data ))
47
- self .native = image .initWithData (nsdata )
48
- if self .native is None :
49
- raise ValueError ("Unable to load image from data" )
50
- else :
51
- self ._needs_release = True
52
- else :
53
- self .native = raw
54
- finally :
55
- # Calling `release` here disabled Rubicon's "release on delete" automation.
56
- # We therefore add an explicit `release` call in __del__ if the NSImage was
57
- # initialized successfully.
58
- image .release ()
27
+ # We *should* be able to do a direct NSImage.alloc.init...(), but if the
28
+ # image file is invalid, the init fails, returns NULL, and releases the
29
+ # Objective-C object. Since we've created an ObjC instance, when the object
30
+ # passes out of scope, Rubicon tries to free it, which segfaults.
31
+ # To avoid this, we retain result of the alloc() (overriding the default
32
+ # Rubicon behavior of alloc), then release that reference once we're done.
33
+ # If the image was created successfully, we temporarily have a reference
34
+ # count that is 1 higher than it needs to be; if it fails, we don't end up
35
+ # with a stray release.
36
+ image = NSImage .alloc ().retain ()
37
+ if path :
38
+ self .native = image .initWithContentsOfFile (str (path ))
39
+ if self .native is None :
40
+ raise ValueError (f"Unable to load image from { path } " )
41
+ elif data :
42
+ nsdata = NSData .dataWithBytes (data , length = len (data ))
43
+ self .native = image .initWithData (nsdata )
44
+ if self .native is None :
45
+ raise ValueError ("Unable to load image from data" )
46
+ else :
47
+ self .native = raw
59
48
60
- def __del__ (self ):
61
- if self ._needs_release :
62
- self .native .release ()
49
+ image .release ()
63
50
64
51
def get_width (self ):
65
52
return self .native .size .width
0 commit comments