def test_carray_ushort(self): ushort_array = short_array _instance.setUShortValues_(ushort_array) returned_ushorts = dereference(_instance.getUShortValues(), of_type=CArray, return_count=10) returned_ushorts_WithCount = dereference(_instance.getUShortValuesWithCount_(CArrayCount), of_type=CArray) self.assertEquals(returned_ushorts, ushort_array) self.assertEquals(returned_ushorts_WithCount, ushort_array)
def test_carray_ulonglong(self): ulonglong_array = ulong_array _instance.setULongLongValues_(ulonglong_array) returned_ulonglongs = dereference(_instance.getULongLongValues(), of_type=CArray, return_count=10) returned_ulonglongs_WithCount = dereference(_instance.getULongLongValuesWithCount_(CArrayCount), of_type=CArray) self.assertEquals(returned_ulonglongs, ulonglong_array) self.assertEquals(returned_ulonglongs_WithCount, ulonglong_array)
def test_carray_uint(self): uint_array = num_list _instance.setUIntValues_(uint_array) returned_uints = dereference(_instance.getUIntValues(), of_type=CArray, return_count=10) returned_uints_WithCount = dereference(_instance.getUIntValuesWithCount_(CArrayCount), of_type=CArray) self.assertEquals(returned_uints, uint_array) self.assertEquals(returned_uints_WithCount, uint_array)
def test_carray_double(self): _instance.setDoubleValues_(float_array) returned_doubles = dereference(_instance.getDoubleValues(), of_type=CArray, return_count=10) returned_doubles_WithCount = dereference( _instance.getDoubleValuesWithCount_(CArrayCount), of_type=CArray) self.assertEquals(returned_doubles, float_array) self.assertEquals(returned_doubles_WithCount, float_array)
def test_carray_longlong(self): _instance.setLongLongValues_(long_array) returned_longlongs = dereference(_instance.getLongLongValues(), of_type=CArray, return_count=10) returned_longlongs_WithCount = dereference( _instance.getLongLongValuesWithCount_(CArrayCount), of_type=CArray) self.assertEquals(returned_longlongs, long_array) self.assertEquals(returned_longlongs_WithCount, long_array)
def test_carray_short(self): _instance.setShortValues_(short_array) returned_shorts = dereference(_instance.getShortValues(), of_type=CArray, return_count=10) returned_shorts_WithCount = dereference( _instance.getShortValuesWithCount_(CArrayCount), of_type=CArray) self.assertEquals(returned_shorts, short_array) self.assertEquals(returned_shorts_WithCount, short_array)
def test_carray_int(self): _instance.setIntValues_(num_list) returned_PyList = dereference(_instance.getIntValues(), of_type=CArray, return_count=10) returned_PyList_withCount = dereference( _instance.getIntValuesWithCount_(CArrayCount), of_type=CArray) self.assertEquals(returned_PyList, num_list) self.assertEquals(returned_PyList_withCount, num_list)
def test_carray_bool(self): _instance.setBoolValues_(bool_array) returned_bools = dereference(_instance.getBoolValues(), of_type=CArray, return_count=10) returned_bools_WithCount = dereference( _instance.getBoolValuesWithCount_(CArrayCount), of_type=CArray) self.assertEquals(returned_bools, bool_array) self.assertEquals(returned_bools_WithCount, bool_array)
def test_carray_uint(self): uint_array = num_list _instance.setUIntValues_(uint_array) returned_uints = dereference(_instance.getUIntValues(), of_type=CArray, return_count=10) returned_uints_WithCount = dereference( _instance.getUIntValuesWithCount_(CArrayCount), of_type=CArray) self.assertEquals(returned_uints, uint_array) self.assertEquals(returned_uints_WithCount, uint_array)
def test_carray_long(self): count = ctypes.c_uint32(0) _instance.setLongValues_(long_array) returned_longs = dereference( _instance.getLongValues(), of_type=CArray, return_count=10) returned_longs_WithCount = dereference( _instance.getLongValuesWithCount_(count), of_type=CArray, return_count=count) self.assertEqual(returned_longs, long_array) self.assertEqual(returned_longs_WithCount, long_array)
def test_carray_class(self): NSNumber = autoclass("NSNumber") nsnumber_class = NSNumber.oclass() nsnumber_class_array = [nsnumber_class for i in xrange(0, 10)] _instance.setClassValues_(nsnumber_class_array) returned_classes = dereference(_instance.getClassValues(), of_type=CArray, return_count=10) returned_classes_WithCount = dereference(_instance.getClassValuesWithCount_(CArrayCount), of_type=CArray) flag = True for i in xrange(len(returned_classes_WithCount)): if NSNumber.isKindOfClass_(returned_classes_WithCount[i]) == False: flag = False self.assertEquals(flag, True)
def test_carray_ushort(self): count = ctypes.c_uint32(0) ushort_array = short_array _instance.setUShortValues_(ushort_array) returned_ushorts = dereference( _instance.getUShortValues(), of_type=CArray, return_count=10) returned_ushorts_WithCount = dereference( _instance.getUShortValuesWithCount_(count), of_type=CArray, return_count=count) self.assertEqual(returned_ushorts, ushort_array) self.assertEqual(returned_ushorts_WithCount, ushort_array)
def test_carray_class(self): NSNumber = autoclass("NSNumber") nsnumber_class = NSNumber.oclass() nsnumber_class_array = [nsnumber_class for i in xrange(0, 10)] _instance.setClassValues_(nsnumber_class_array) returned_classes = dereference(_instance.getClassValues(), of_type=CArray, return_count=10) returned_classes_WithCount = dereference( _instance.getClassValuesWithCount_(CArrayCount), of_type=CArray) flag = True for i in xrange(len(returned_classes_WithCount)): if NSNumber.isKindOfClass_(returned_classes_WithCount[i]) == False: flag = False self.assertEquals(flag, True)
def test_carray_float(self): count = ctypes.c_uint32(0) _instance.setFloatValues_(float_array) returned_floats = dereference( _instance.getFloatValues(), of_type=CArray, return_count=10) returned_floats_WithCount = dereference( _instance.getFloatValuesWithCount_(count), of_type=CArray, return_count=count) for x in range(10): self.assertTrue(returned_floats[x], pytest.approx( float_array[x])) self.assertAlmostEqual( returned_floats_WithCount[x], pytest.approx( float_array[x]))
def test_custom_getter_properties(self): car.propIntCst = 5678 self.assertEquals(car.propIntCst, 5678) # this is int* car.propCstInt = 3456 self.assertEquals(dereference(car.propCstInt), 3456)
def test_valueWithRangePointer(self): range = opy.NSRange(10, 20) range_ptr = NSValue.valueWithPointer_(range) range_val_ptr = range_ptr.pointerValue() range_deref = dereference(range_val_ptr, type=opy.NSRange) self.assertEquals(range_deref.location, 10) self.assertEquals(range_deref.length, 20)
def test_valueWithRangePointer(self): range = opy.NSRange(10, 20) range_ptr = NSValue.valueWithPointer_(range) range_val_ptr = range_ptr.pointerValue() range_deref = dereference(range_val_ptr, of_type=opy.NSRange) self.assertEqual(range_deref.location, 10) self.assertEqual(range_deref.length, 20)
def test_valueWithRectPointer(self): rect = opy.NSRect(opy.NSPoint(3, 5), opy.NSSize(320, 480)) rct_ptr = NSValue.valueWithPointer_(rect) rect_val_ptr = rct_ptr.pointerValue() rect_deref = dereference(rect_val_ptr, type=opy.NSRect) self.assertEquals(rect_deref.origin.x, 3) self.assertEquals(rect_deref.origin.y, 5) self.assertEquals(rect_deref.size.width, 320) self.assertEquals(rect_deref.size.height, 480)
def test_carray_object(self): NSNumber = autoclass("NSNumber") ns_number_array = list() for i in xrange(0, 10): ns_number_array.append(NSNumber.alloc().initWithInt_(i)) py_ints = [i for i in xrange(0,10)] _instance.setNSNumberValues_(ns_number_array) nsnumber_ptr_array = _instance.getNSNumberValues() returned_nsnumbers = dereference(nsnumber_ptr_array, of_type=CArray, return_count=10) returned_ints = list() for i in xrange(len(returned_nsnumbers)): returned_ints.append(returned_nsnumbers[i].intValue()) returned_nsnumbers_WithCount = dereference(_instance.getNSNumberValuesWithCount_(CArrayCount), of_type=CArray) returned_ints_WithCount = list() for i in xrange(len(returned_nsnumbers_WithCount)): returned_ints_WithCount.append(returned_nsnumbers_WithCount[i].intValue()) self.assertEquals(returned_ints, py_ints) self.assertEquals(returned_ints_WithCount, py_ints)
def test_valueWithRectPointer(self): rect = opy.NSRect(opy.NSPoint(3, 5), opy.NSSize(320, 480)) rct_ptr = NSValue.valueWithPointer_(rect) rect_val_ptr = rct_ptr.pointerValue() rect_deref = dereference(rect_val_ptr, of_type=opy.NSRect) self.assertEqual(rect_deref.origin.x, 3) self.assertEqual(rect_deref.origin.y, 5) self.assertEqual(rect_deref.size.width, 320) self.assertEqual(rect_deref.size.height, 480)
def test_carray_struct(self): struct_array = [NSRect(NSPoint(300 + i, 500 + i), NSSize(320, 480)) for i in xrange(1, 11)] _instance.setNSRectValues_(struct_array) returned_struct_array = dereference(_instance.getNSRectValues(), of_type=CArray, return_count=10) vals = [(300 + i , 500 + i) for i in xrange(1, 11)] ret_vals = list() for item in returned_struct_array: ret_vals.append((item.origin.x, item.origin.y)) self.assertEquals(ret_vals, vals)
def test_carray_struct(self): struct_array = [NSRect(NSPoint(300 + i, 500 + i), NSSize(320, 480)) for i in range(1, 11)] _instance.setNSRectValues_(struct_array) returned_struct_array = dereference(_instance.getNSRectValues(), of_type=CArray, return_count=10) vals = [(300 + i , 500 + i) for i in range(1, 11)] ret_vals = list() for item in returned_struct_array: ret_vals.append((item.origin.x, item.origin.y)) self.assertEqual(ret_vals, vals)
def test_returning_values(self): car = Car.alloc().init() union_ptr = car.makeUnionPtr() union = dereference(union_ptr) self.assertEqual(union.rect.origin.x, 10) self.assertEqual(union.rect.origin.y, 30) union = car.makeUnion() self.assertEqual(union.rect.origin.x, 20) self.assertEqual(union.rect.origin.y, 40)
def test_basic_properties(self): car.propInt = 12345 self.assertEquals(car.propInt, 12345) car.propDouble = 3456.2345 self.assertEquals(car.propDouble, 3456.2345) car.prop_double_ptr = 333.444 self.assertEquals(dereference(car.prop_double_ptr), 333.444) car.propNSString = autoclass('NSString').stringWithUTF8String_('string for test') self.assertEquals(car.propNSString.UTF8String(), 'string for test')
def test_basic_properties(self): car.propInt = 12345 self.assertEquals(car.propInt, 12345) car.propDouble = 3456.2345 self.assertEquals(car.propDouble, 3456.2345) car.prop_double_ptr = 333.444 self.assertEquals(dereference(car.prop_double_ptr), 333.444) car.propNSString = autoclass('NSString').stringWithUTF8String_( 'string for test') self.assertEquals(car.propNSString.UTF8String(), 'string for test')
def test_carray_object(self): NSNumber = autoclass("NSNumber") ns_number_array = list() for i in xrange(0, 10): ns_number_array.append(NSNumber.alloc().initWithInt_(i)) py_ints = [i for i in xrange(0, 10)] _instance.setNSNumberValues_(ns_number_array) nsnumber_ptr_array = _instance.getNSNumberValues() returned_nsnumbers = dereference(nsnumber_ptr_array, of_type=CArray, return_count=10) returned_ints = list() for i in xrange(len(returned_nsnumbers)): returned_ints.append(returned_nsnumbers[i].intValue()) returned_nsnumbers_WithCount = dereference( _instance.getNSNumberValuesWithCount_(CArrayCount), of_type=CArray) returned_ints_WithCount = list() for i in xrange(len(returned_nsnumbers_WithCount)): returned_ints_WithCount.append( returned_nsnumbers_WithCount[i].intValue()) self.assertEquals(returned_ints, py_ints) self.assertEquals(returned_ints_WithCount, py_ints)
def test_carray_int(self): _instance.setIntValues_(num_list) returned_PyList = dereference(_instance.getIntValues(), of_type=CArray, return_count=10) returned_PyList_withCount = dereference(_instance.getIntValuesWithCount_(CArrayCount), of_type=CArray) self.assertEquals(returned_PyList, num_list) self.assertEquals(returned_PyList_withCount, num_list)
def test_carray_double(self): _instance.setDoubleValues_(float_array) returned_doubles = dereference(_instance.getDoubleValues(), of_type=CArray, return_count=10) returned_doubles_WithCount = dereference(_instance.getDoubleValuesWithCount_(CArrayCount), of_type=CArray) self.assertEquals(returned_doubles, float_array) self.assertEquals(returned_doubles_WithCount, float_array)
def test_dereference(self): car = Car.alloc().init() union_ptr = car.makeUnionPtr() union = dereference(union_ptr) self.assertEqual(union.rect.origin.x, 10) self.assertEqual(union.rect.origin.y, 30)
# NSRect rect = NSMakeRect(10, 30, 50, 60); # un.rect = rect; # return un; # } union = car.makeUnion() print union.rect.origin.x, union.rect.origin.y # Also there is ability to call function which returns pointer to some unoin type # - (test_un_*) makeUnionPtr { # test_un_ *un = malloc(sizeof(test_un_)); # NSRect rect = NSMakeRect(10, 30, 50, 60); # un->rect = rect; # return un; # } union_ptr = car.makeUnionPtr() union_val = dereference(union_ptr) print union_val.rect.origin.x, union_val.rect.origin.y # BUT, currently passing unions by value to some function isn't supported, # because pyobjus use libffi, which doesn't support that feature # - (void) useUnion:(test_un_)un { # // THIS WILL RAISE EXCEPTION IN PYOBJUS # } # this would cause ObjcException error! --> c.useUnion_(union) # FORTUNATELY, passing unions as pointes to some function IS SUPPORTED in pyobjus # - (void) useUnionPtr:(test_un_*)un_p { # test_un_ *un_ = (test_un_*)un_p; # test_un_ un = un_[0]; # printf("values --> %f %f\n", un.rect.origin.x, un.rect.origin.y); # }
def test_carray_bool(self): _instance.setBoolValues_(bool_array) returned_bools = dereference(_instance.getBoolValues(), of_type=CArray, return_count=10) returned_bools_WithCount = dereference(_instance.getBoolValuesWithCount_(CArrayCount), of_type=CArray) self.assertEquals(returned_bools, bool_array) self.assertEquals(returned_bools_WithCount, bool_array)
def test_dereference_basic(self): rng_ptr = car.makeRangePtr() rng = dereference(rng_ptr) self.assertEqual(rng.location, 567) self.assertEqual(rng.length, 123)
# So, after assigning you can see actual value of property: print car.propInt # If you have property which is pointer to some type, pyobjus also can deal with those properties # @property (assign) double *propDoublePtr; # . # . # . # @synthesize propDoublePtr; car.prop_double_ptr = 345.543 # As you can see, you don't need to worry about dereferencing pointer when you asigning value, # pyobjus will do it for you :) # After this you can get actual value on which pointer points print dereference(car.prop_double_ptr) # But can pyobjus deal with @dynamic properties? Yes, it can # @property (assign) NSString *propNsstringDyn; # . # . # . # @dynamic propNsstringDyn; car.propNsstringDyn = autoclass("NSString").stringWithUTF8String_("test from python") # And now, let we get value: print car.propNsstringDyn.UTF8String() # And what if I don't have default getter/setters for some property? # Pyobjus also can deal with that # @property (nonatomic, assign, getter = getPropIntGtr, setter = customSetPropInt:) int propIntCst;
_instance = CArrayTestlib.alloc() ################################# INT ARRAY #################################################### # Objective-C method signatures: # - (void) setIntValues: (int[10]) val_arr; # - (int*) getIntValues; # - (void) printIntValues; # - (int*) getIntValuesWithCount: (unsigned int*) n; nums = [0, 2, 1, 5, 4, 3, 6, 7, 8, 9] # we can set values with Python List(nums) or CArray style(array) array = (ctypes.c_int * 10)(*nums) # this is optional, another way of passing carray to pyobjus _instance.setIntValues_(array) #Do not forget for _ to signify no. of arguments #_instance.printIntValues() returned_PyList = dereference(_instance.getIntValues(), of_type=CArray, return_count=10) print returned_PyList # If method returns values/ArrayCount over reference and you don't provide CArrayCount # on the right position in the method signature, you will get "IndexError: tuple index out of range" # or segmentation fault, so don't forget to provide CArrayCount on the right position returned_PyList_withCount = dereference(_instance.getIntValuesWithCount_(CArrayCount), of_type=CArray) print returned_PyList_withCount ################################################################################################# ################################## CHAR ARRAY ################################################### # Objective-C method signatures: # - (void) setCharValues: (char[10]) val_arr; # - (char*) getCharValues; # - (char*) getCharValuesWithCount: (unsigned int*) n;
# - (void) useSelectorPtr:(SEL*)sel_ptr { # SEL sel = sel_ptr[0]; # [self performSelector:sel]; # } car.useSelectorPtr_(sel_ptr) # but what if we have selector value (not reference) and we want to call method which accepts reference, not value? # Luckily pyobjus is smart enought to figure out that method which we call accepts reference, # and also knows are we passing reference or value to some method. # So if we pass a value, he will by himself convert it to reference to some type which method accepts, # and with this user is freed of thinking is method accepting value, or reference to some type car.useSelectorPtr_(sel) # As we knows, in native Objective C we can dereference pointer to get actual value on which pointer points # Pyobjus also have mechanism of dereferencing some pointer. He is also smart enought to know on which type # he need to cast value on which pointer points. sel = dereference(sel_ptr) print(sel) # There is one limitation of dereference function in cases when we have function which returns void pointer (void*) # But pyobjus can also deal with this situation, user only need to tell pyobjus in which type to convert # - (void*) makeSelectorVoidPtr { # SEL *s = malloc(sizeof(SEL)); # SEL sel = @selector(print); # *s = sel; # return (void*)s; # } sel_void_ptr = car.makeSelectorVoidPtr() sel = dereference(sel_void_ptr, of_type=ObjcSelector) print(sel)
def test_carray_multidimensional(self): _instance.set2DIntValues_(twoD_array) returned_2d_list = dereference(_instance.get2DIntValues(), of_type=CArray, partition=[10, 10]) self.assertEquals(returned_2d_list, twoD_array)
def test_dereference_with_type(self): int_ptr = car.makeIntVoidPtr() int_val = dereference(int_ptr, of_type=ObjcInt) self.assertEquals(int_val, 12345)
def test_carray_multidimensional(self): _instance.set2DIntValues_(twoD_array) returned_2d_list = dereference(_instance.get2DIntValues(), of_type=CArray, partition=[10,10]) self.assertEquals(returned_2d_list, twoD_array)
# So, after assigning you can see actual value of property: print(car.propInt) # If you have property which is pointer to some type, pyobjus also can deal with those properties # @property (assign) double *propDoublePtr; # . # . # . # @synthesize propDoublePtr; car.prop_double_ptr = 345.543 # As you can see, you don't need to worry about dereferencing pointer when you asigning value, # pyobjus will do it for you :) # After this you can get actual value on which pointer points print(dereference(car.prop_double_ptr)) # But can pyobjus deal with @dynamic properties? Yes, it can # @property (assign) NSString *propNsstringDyn; # . # . # . # @dynamic propNsstringDyn; car.propNsstringDyn = autoclass('NSString').stringWithUTF8String_( 'test from python') # And now, let we get value: print(car.propNsstringDyn.UTF8String()) # And what if I don't have default getter/setters for some property? # Pyobjus also can deal with that
# - (void) useSelectorPtr:(SEL*)sel_ptr { # SEL sel = sel_ptr[0]; # [self performSelector:sel]; # } car.useSelectorPtr_(sel_ptr) # but what if we have selector value (not reference) and we want to call method which accepts reference, not value? # Luckily pyobjus is smart enought to figure out that method which we call accepts reference, # and also knows are we passing reference or value to some method. # So if we pass a value, he will by himself convert it to reference to some type which method accepts, # and with this user is freed of thinking is method accepting value, or reference to some type car.useSelectorPtr_(sel) # As we knows, in native Objective C we can dereference pointer to get actual value on which pointer points # Pyobjus also have mechanism of dereferencing some pointer. He is also smart enought to know on which type # he need to cast value on which pointer points. sel = dereference(sel_ptr) print sel # There is one limitation of dereference function in cases when we have function which returns void pointer (void*) # But pyobjus can also deal with this situation, user only need to tell pyobjus in which type to convert # - (void*) makeSelectorVoidPtr { # SEL *s = malloc(sizeof(SEL)); # SEL sel = @selector(print); # *s = sel; # return (void*)s; # } sel_void_ptr = car.makeSelectorVoidPtr() sel = dereference(sel_void_ptr, of_type=ObjcSelector) print sel
def test_dereference_with_type(self): int_ptr = car.makeIntVoidPtr() int_val = dereference(int_ptr, of_type=ObjcInt) self.assertEqual(int_val, 12345)
def test_dereference_basic(self): rng_ptr = car.makeRangePtr() rng = dereference(rng_ptr) self.assertEquals(rng.location, 567) self.assertEquals(rng.length, 123)
################################# INT ARRAY #################################################### # Objective-C method signatures: # - (void) setIntValues: (int[10]) val_arr; # - (int*) getIntValues; # - (void) printIntValues; # - (int*) getIntValuesWithCount: (unsigned int*) n; nums = [0, 2, 1, 5, 4, 3, 6, 7, 8, 9] # we can set values with Python List(nums) or CArray style(array) array = (ctypes.c_int * 10)( *nums) # this is optional, another way of passing carray to pyobjus _instance.setIntValues_( array) #Do not forget for _ to signify no. of arguments #_instance.printIntValues() returned_PyList = dereference(_instance.getIntValues(), of_type=CArray, return_count=10) print returned_PyList # If method returns values/ArrayCount over reference and you don't provide CArrayCount # on the right position in the method signature, you will get "IndexError: tuple index out of range" # or segmentation fault, so don't forget to provide CArrayCount on the right position returned_PyList_withCount = dereference( _instance.getIntValuesWithCount_(CArrayCount), of_type=CArray) print returned_PyList_withCount ################################################################################################# ################################## CHAR ARRAY ################################################### # Objective-C method signatures: # - (void) setCharValues: (char[10]) val_arr; # - (char*) getCharValues;