Skip to content

Commit 4fd9ea5

Browse files
author
jsbain
committed
added block test
1 parent 6e42990 commit 4fd9ea5

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

blocktest.py

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# coding: utf-8
2+
'''
3+
experiment with blocks.
4+
5+
6+
[theArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop){
7+
NSLog(@"The object at index %d is %@",idx,obj);
8+
}];
9+
'''
10+
from ctypes import *
11+
from objc_util import *
12+
import pdb
13+
14+
NSBlock=ObjCClass('NSBlock')
15+
from ctypes import *
16+
17+
ENUMCALLBACK=CFUNCTYPE(None, c_void_p,c_void_p, c_uint, POINTER(c_bool))
18+
19+
20+
class GenericBlockDescriptor(Structure):
21+
_fields_=[('reserved',c_ulong),
22+
('size',c_ulong),
23+
('signature', c_char_p)]
24+
def __init__(self,block):
25+
self.size=sizeof(block)
26+
27+
class EnumerationBlock(Structure):
28+
'''
29+
struct Block_literal_1 {
30+
void *isa; // initialized to &_NSConcreteStackBlock or &_NSConcreteGlobalBlock
31+
int flags;
32+
int reserved;
33+
void (*invoke)(void *, ...);
34+
struct Block_descriptor_1 {
35+
unsigned long int reserved; // NULL
36+
unsigned long int size; // sizeof(struct Block_literal_1)
37+
// optional helper functions
38+
void (*copy_helper)(void *dst, void *src); // IFF (1<<25)
39+
void (*dispose_helper)(void *src); // IFF (1<<25)
40+
// required ABI.2010.3.16
41+
const char *signature; // IFF (1<<30)
42+
} *descriptor;
43+
// imported variables
44+
};'''
45+
_fields_= [('isa',c_void_p),
46+
('flags',c_int32),
47+
('reserved',c_int32),
48+
('invoke',ENUMCALLBACK),
49+
('descriptor',POINTER(GenericBlockDescriptor))]
50+
def __init__(self,invoke):
51+
self._descriptor=GenericBlockDescriptor(self)
52+
self.descriptor=pointer(self._descriptor)
53+
self.isa=NSBlock.ptr
54+
self.invoke=ENUMCALLBACK(invoke)
55+
self.descriptor[0].signature=c_char_p('') # not used
56+
self.descriptor[0].size=sizeof(self)
57+
58+
59+
def invoke_py(self, obj, ind, stop):
60+
print 'The item at index',ind,'is', ObjCInstance(obj)
61+
if ObjCInstance(obj).intValue()==-1:
62+
print 'stopping here'
63+
stop[0]=True
64+
65+
blk=EnumerationBlock(invoke_py)
66+
A=ns([4,3,7,9,-1, 5])
67+
A.enumerateObjectsUsingBlock_(byref(blk))

0 commit comments

Comments
 (0)