-
Notifications
You must be signed in to change notification settings - Fork 19
/
objcblock.py
39 lines (38 loc) · 1.33 KB
/
objcblock.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# coding: utf-8
''' block factory
ObjCBlock(py_callback,restype, argtypes[])
'''
from ctypes import *
from objc_util import *
import pdb
NSBlock=ObjCClass('NSBlock')
class ObjCBlockDescriptor(Structure):
_fields_=[('reserved',c_ulong),
('size',c_ulong),
('signature', c_char_p)]
def __init__(self,block):
self.size=sizeof(block)
ENUMCALLBACK=CFUNCTYPE(None, c_void_p,c_void_p, c_uint, POINTER(c_bool))
def ObjCBlock(invoke,restype,argtypes):
BLOCK_FUNC = CFUNCTYPE(restype,c_void_p,*argtypes)
class Block(Structure):
_fields_= [('isa',c_void_p),
('flags',c_int32),
('reserved',c_int32),
('invoke',BLOCK_FUNC),
('descriptor',POINTER(ObjCBlockDescriptor))]
def __init__(self):
self._descriptor=ObjCBlockDescriptor(self)
self.descriptor=pointer(self._descriptor)
self.isa=NSBlock.ptr
self.invoke=BLOCK_FUNC(invoke)
self.descriptor[0].signature=c_char_p('') # not used
self.descriptor[0].size=sizeof(self)
return byref(Block())
if __name__=='__main__':
def invoke_py(self, obj, idx, stop):
print self, idx , obj
blk=ObjCBlock(invoke_py,None,[c_void_p,c_uint, POINTER(c_bool)])
A=ns(['a',1,3])
A.enumerateObjectsUsingBlock_(blk)
#A.enumerate