[Pythonmac-SIG] Quartz, twisted.web and CGBitmapContextCreate

Erik van Blokland erik at letterror.com
Thu Nov 26 09:58:54 CET 2009


Ronald, Orestis,

Here's a small script that reproduces the crash. Stock Python 2.6.1 (r261:67515, Jul  7 2009, 23:51:51), current twistd.

Background: an earlier version of my app uses CoreGraphics rather than Quartz and has been very reliable in twisted, python 2.3.5, OSX 10.4.
CoreGraphics in 10.6 seems to have picked up some bugs (SImilar to:
http://lists.apple.com/archives/quartz-dev/2009/Oct/msg00070.html )
I really want to move to Quartz, hence this attempt. 

Cheers, Erik



#!/usr/bin/python

"""

    This should test the interaction between a OSX Quartz call
    and twisted.web.
    
    To run, call the twistd binary with this script. 
    On my machine, it lives here:
    
    /usr/bin/twistd -y twisted.quartz.test.py --pidfile quartztestpid.txt --logfile quartztest.log
    
    Then open the local host
    http:127.0.0.1:8080
    
    Then wait for the crash report to pop up.
    

"""
from twisted.web import resource, server
from twisted.internet import reactor
from twisted.application import service, internet

from Quartz import *

import time

rasterColorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB)



def _makeContext(size):
    width, height = size
    data = None
    bitsPerComponent = 8
    numComponents = 4
    bestByteAlignment = 16
    bytesPerRow = width * (bitsPerComponent / 8) * numComponents
    bytesPerRow = ((bytesPerRow + (bestByteAlignment - 1)) & ~(bestByteAlignment - 1))
    bitmapInfo = kCGImageAlphaPremultipliedLast
    
    # documentation says None should work
    data = None
    
    # some samples use a numpy object
    #data = numpy.zeros( ( height, width, 8), numpy.uint8)
    
    # other samples use a array.array object
    #data = array.array('B', (0 for i in xrange(width*height*8)))
    
    # here it goes
    context = CGBitmapContextCreate(data, width, height, 8, bytesPerRow, rasterColorSpace, bitmapInfo)
    return context

#   Just to show the context does work when used outside of twistd. 
#   This returns something like: <CGContext 0x103713ca0>
#   Obviously, printing in this place won't work in twistd.
#print "_makeContext all by itself", _makeContext((100,100))


class RootResource(resource.Resource):
    def render_GET(self, request):
        if not hasattr(self, 'count'):
            self.count = 0
        self.count += 1
        ctx = _makeContext((100,100))
        return "OK %d %s"%(self.count, ctx)

    def getChild(self, name, request):
        return self


port = 8080
site = server.Site(RootResource())
application = service.Application('QuartzTestApp')
sc = service.IServiceCollection(application)
i = internet.TCPServer(port, site)
i.setServiceParent(sc)



More information about the Pythonmac-SIG mailing list