Skip navigation.
 
mlRe: Cocoa Python broken in Leopard??
FROM : Bill Bumgarner
DATE : Wed Nov 07 18:46:03 2007

On Nov 7, 2007, at 9:04 AM, Uliano Guerrini wrote:
> Il giorno 07/nov/07, alle ore 17:48, Bill Bumgarner ha scritto:

>> Import Quartz *after* the application's main event loop has been 
>> started, either in applicationDidFinishLaunching_() as I 
>> demonstrated in my first message:
>>

>>> class FoobarAppDelegate(NSObject):
>>>  def applicationDidFinishLaunching_(self, sender):
>>>      import Quartz

>
> this is useless as the scope of that import is limited to that 
> function in that file


The overhead for re-importing is not much.

>>> Or import it as the first line of the drawRect_() method of your 
>>> view.

>
> I found a better (much efficient, I hope) workaround:
>
> Quartz = None
>
> class MyView(NSView):
>    def awakeFromNib(self):
>        global Quartz
>        Quartz = __import__('Quartz')
>
>    def drawRect_(self, rect):
>        context = NSGraphicsContext.currentContext().graphicsPort()
>        Quartz.whatever()
>
> here the import happens to be called only once per class and Quartz 
> is global to the whole file. Still it is not elegant but I can live 
> with it


That isn't a workaround. That is one solution.  There isn't really a 
bug here.  This is just the way Python works -- some modules are 
designed such that their import has dependencies on certain other 
state pre-existing.  Quartz is one of them.

Alternatively, if MyView exists as a part of an NSDocument, you could 
import MyView as a part of the NSDocument document loading mechanism. 
Since the class will be created in the ObjC runtime and instantiated 
by the loading of the NSDocument's NIB, no need to jump through the 
global/__import__hoops as per above (which, btw, are just fine and 
I'll add 'em to my recipes).

b.bum

Related mailsAuthorDate
mlCocoa Python broken in Leopard?? Uliano Guerrini Nov 7, 08:49
mlRe: Cocoa Python broken in Leopard?? Bill Bumgarner Nov 7, 08:58
mlRe: Cocoa Python broken in Leopard?? Uliano Guerrini Nov 7, 09:46
mlRe: Cocoa Python broken in Leopard?? Bill Bumgarner Nov 7, 09:57
mlRe: Cocoa Python broken in Leopard?? Uliano Guerrini Nov 7, 10:41
mlRe: Cocoa Python broken in Leopard?? Bill Bumgarner Nov 7, 17:48
mlRe: Cocoa Python broken in Leopard?? Uliano Guerrini Nov 7, 18:04
mlRe: Cocoa Python broken in Leopard?? Bill Bumgarner Nov 7, 18:46
mlRe: Cocoa Python broken in Leopard?? Luc Heinrich Nov 8, 09:32
mlRe: Cocoa Python broken in Leopard?? Uliano Guerrini Nov 8, 13:00
mlRe: Cocoa Python broken in Leopard?? Luc Heinrich Nov 8, 14:10
mlRe: Cocoa Python broken in Leopard?? Uliano Guerrini Nov 8, 14:41