~landscape/zope3/ztk-1.1.3

« back to all changes in this revision

Viewing changes to src/twisted/internet/cfsupport/cfrunloop.pxi

  • Committer: Andreas Hasenack
  • Date: 2009-07-20 17:49:16 UTC
  • Revision ID: andreas@canonical.com-20090720174916-g2tn6qmietz2hn0u
Revert twisted removal, it breaks several dozen tests [trivial]

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import traceback
 
2
 
 
3
# possibly figure out how to subclass these or something
 
4
 
 
5
cdef class PyCFRunLoopTimer:
 
6
    cdef CFRunLoopTimerRef cf
 
7
    cdef public object callout
 
8
    cdef CFRunLoopTimerContext context
 
9
 
 
10
    def __new__(self, double fireDate, double interval, callout):
 
11
        self.callout = callout
 
12
        self.context.version = 0
 
13
        self.context.info = <void *>self
 
14
        self.context.retain = NULL
 
15
        self.context.release = NULL
 
16
        self.context.copyDescription = NULL
 
17
        self.cf = CFRunLoopTimerCreate(kCFAllocatorDefault, fireDate, interval, 0, 0, <CFRunLoopTimerCallBack>&gilRunLoopTimerCallBack, &self.context)
 
18
        if self.cf == NULL:
 
19
            raise ValueError("Invalid Socket")
 
20
        
 
21
    def getNextFireDate(self):
 
22
        return CFRunLoopTimerGetNextFireDate(self.cf)
 
23
 
 
24
    def setNextFireDate(self, double fireDate):
 
25
        CFRunLoopTimerSetNextFireDate(self.cf, fireDate)
 
26
 
 
27
    def invalidate(self):
 
28
        CFRunLoopTimerInvalidate(self.cf)
 
29
 
 
30
    def __dealloc__(self):
 
31
        if self.cf != NULL:
 
32
            CFRelease(self.cf)
 
33
 
 
34
cdef void runLoopTimerCallBack(CFRunLoopTimerRef timer, void *info):
 
35
    cdef PyCFRunLoopTimer obj
 
36
    obj = <PyCFRunLoopTimer>info
 
37
    try:
 
38
        if obj.callout:
 
39
            obj.callout()
 
40
    except:
 
41
        traceback.print_exc()
 
42
 
 
43
cdef void gilRunLoopTimerCallBack(CFRunLoopTimerRef timer, void *info):
 
44
    cdef PyGILState_STATE gil
 
45
    gil = PyGILState_Ensure()
 
46
    runLoopTimerCallBack(timer, info)
 
47
    PyGILState_Release(gil)
 
48
 
 
49
cdef class PyCFRunLoop:
 
50
    cdef public object cf
 
51
 
 
52
    def __new__(self, runLoop=None):
 
53
        cdef CFTypeRef _runLoop
 
54
        if runLoop is None:
 
55
            _runLoop = CFRunLoopGetCurrent()
 
56
        else:
 
57
            if CFObj_Convert(runLoop, &_runLoop) == 0:
 
58
                raise
 
59
                #return -1
 
60
        # this is going to leak a reference
 
61
        self.cf = CFObj_New(CFRetain(_runLoop))
 
62
 
 
63
    def run(self):
 
64
        CFRunLoopRun()
 
65
 
 
66
    def stop(self):
 
67
        cdef CFTypeRef _runLoop
 
68
        if CFObj_Convert(self.cf, &_runLoop) == 0:
 
69
            raise ValueError, "CFRunLoopReference is invalid"
 
70
        CFRunLoopStop(_runLoop)
 
71
 
 
72
    def currentMode(self):
 
73
        cdef CFTypeRef _currentMode
 
74
        cdef CFTypeRef _runLoop
 
75
        if CFObj_Convert(self.cf, &_runLoop) == 0:
 
76
            raise ValueError, "CFRunLoopReference is invalid"
 
77
        _currentMode = CFRunLoopCopyCurrentMode(_runLoop)
 
78
        if _currentMode == NULL:
 
79
            return None
 
80
        return CFObj_New(_currentMode)
 
81
        
 
82
    def addSocket(self, PyCFSocket socket not None):
 
83
        cdef CFTypeRef _runLoop
 
84
        if CFObj_Convert(self.cf, &_runLoop) == 0:
 
85
            raise ValueError, "CFRunLoopReference is invalid"
 
86
        CFRunLoopAddSource(_runLoop, socket.source, kCFRunLoopCommonModes)
 
87
 
 
88
    def removeSocket(self, PyCFSocket socket not None):
 
89
        cdef CFTypeRef _runLoop
 
90
        if CFObj_Convert(self.cf, &_runLoop) == 0:
 
91
            raise ValueError, "CFRunLoopReference is invalid"
 
92
        CFRunLoopRemoveSource(_runLoop, socket.source, kCFRunLoopCommonModes)
 
93
 
 
94
    def addTimer(self, PyCFRunLoopTimer timer not None):
 
95
        cdef CFTypeRef _runLoop
 
96
        if CFObj_Convert(self.cf, &_runLoop) == 0:
 
97
            raise ValueError, "CFRunLoopReference is invalid"
 
98
        CFRunLoopAddTimer(_runLoop, timer.cf, kCFRunLoopCommonModes)
 
99
 
 
100
    def removeTimer(self, PyCFRunLoopTimer timer not None):
 
101
        cdef CFTypeRef _runLoop
 
102
        if CFObj_Convert(self.cf, &_runLoop) == 0:
 
103
            raise ValueError, "CFRunLoopReference is invalid"
 
104
        CFRunLoopRemoveTimer(_runLoop, timer.cf, kCFRunLoopCommonModes)