def test_array(self): np = get_numpy() if np is None: return t = _midlSAFEARRAY(c_double) pat = pointer(t()) pat[0] = np.zeros(32, dtype=np.float) arr = get_array(pat[0]) self.failUnless(isinstance(arr, np.ndarray)) self.failUnlessEqual(np.dtype(np.double), arr.dtype) self.failUnless((arr == (0.0,) * 32).all()) data = ((1.0, 2.0, 3.0), (4.0, 5.0, 6.0), (7.0, 8.0, 9.0)) a = np.array(data, dtype=np.double) pat[0] = a arr = get_array(pat[0]) self.failUnless(isinstance(arr, np.ndarray)) self.failUnlessEqual(np.dtype(np.double), arr.dtype) self.failUnless((arr == data).all()) data = ((1.0, 2.0), (3.0, 4.0), (5.0, 6.0)) a = np.array(data, dtype=np.double, order="F") pat[0] = a arr = get_array(pat[0]) self.failUnless(isinstance(arr, np.ndarray)) self.failUnlessEqual(np.dtype(np.double), arr.dtype) self.failUnlessEqual(pat[0][0], data)
def test_array(self): np = get_numpy() if np is None: return t = _midlSAFEARRAY(c_double) pat = pointer(t()) pat[0] = np.zeros(32, dtype=np.float) arr = get_array(pat[0]) self.failUnless(isinstance(arr, np.ndarray)) self.failUnlessEqual(np.dtype(np.double), arr.dtype) self.failUnless((arr == (0.0, ) * 32).all()) data = ((1.0, 2.0, 3.0), (4.0, 5.0, 6.0), (7.0, 8.0, 9.0)) a = np.array(data, dtype=np.double) pat[0] = a arr = get_array(pat[0]) self.failUnless(isinstance(arr, np.ndarray)) self.failUnlessEqual(np.dtype(np.double), arr.dtype) self.failUnless((arr == data).all()) data = ((1.0, 2.0), (3.0, 4.0), (5.0, 6.0)) a = np.array(data, dtype=np.double, order="F") pat[0] = a arr = get_array(pat[0]) self.failUnless(isinstance(arr, np.ndarray)) self.failUnlessEqual(np.dtype(np.double), arr.dtype) self.failUnlessEqual(pat[0][0], data)
def test_UDT_ndarray(self): np = get_numpy() if np is None: return from comtypes.gen.TestComServerLib import MYCOLOR t = _midlSAFEARRAY(MYCOLOR) self.failUnless(t is _midlSAFEARRAY(MYCOLOR)) sa = t.from_param([MYCOLOR(0, 0, 0), MYCOLOR(1, 2, 3)]) arr = get_array(sa) self.failUnless(isinstance(arr, np.ndarray)) # The conversion code allows numpy to choose the dtype of # structured data. This dtype is structured under numpy 1.5, 1.7 and # 1.8, and object in 1.6. Instead of assuming either of these, check # the array contents based on the chosen type. if arr.dtype is np.dtype(object): data = [(x.red, x.green, x.blue) for x in arr] else: float_dtype = np.dtype('float64') self.assertIs(arr.dtype[0], float_dtype) self.assertIs(arr.dtype[1], float_dtype) self.assertIs(arr.dtype[2], float_dtype) data = [tuple(x) for x in arr] self.failUnlessEqual(data, [(0.0, 0.0, 0.0), (1.0, 2.0, 3.0)])
def test_VT_UNKNOWN_multi_ndarray(self): np = get_numpy() if np is None: return a = _midlSAFEARRAY(POINTER(IUnknown)) t = _midlSAFEARRAY(POINTER(IUnknown)) self.failUnless(a is t) def com_refcnt(o): "Return the COM refcount of an interface pointer" import gc gc.collect() gc.collect() o.AddRef() return o.Release() from comtypes.typeinfo import CreateTypeLib, ICreateTypeLib punk = CreateTypeLib("spam").QueryInterface( IUnknown) # will never be saved to disk # initial refcount initial = com_refcnt(punk) # This should increase the refcount by 4 sa = t.from_param((punk, ) * 4) self.failUnlessEqual(initial + 4, com_refcnt(punk)) # Unpacking the array must not change the refcount, and must # return an equal object. Creating an ndarray may change the # refcount. arr = get_array(sa) self.failUnless(isinstance(arr, np.ndarray)) self.failUnlessEqual(np.dtype(object), arr.dtype) self.failUnless((arr == (punk, ) * 4).all()) self.failUnlessEqual(initial + 8, com_refcnt(punk)) del arr self.failUnlessEqual(initial + 4, com_refcnt(punk)) del sa self.failUnlessEqual(initial, com_refcnt(punk)) # This should increase the refcount by 2 sa = t.from_param((punk, None, punk, None)) self.failUnlessEqual(initial + 2, com_refcnt(punk)) null = POINTER(IUnknown)() arr = get_array(sa) self.failUnless(isinstance(arr, np.ndarray)) self.failUnlessEqual(np.dtype(object), arr.dtype) self.failUnless((arr == (punk, null, punk, null)).all()) del sa del arr self.failUnlessEqual(initial, com_refcnt(punk))
def test_int(self): np = get_numpy() if np is None: return for dtype in ('int8', 'int16', 'int32', 'int64', 'uint8', 'uint16', 'uint32', 'uint64'): a = np.array((1, 1, 1, 1), dtype=dtype) v = VARIANT() v.value = a self.failUnless((v.value == a).all())
def test_double(self): np = get_numpy() if np is None: return for dtype in ('float32', 'float64'): # because of FLOAT rounding errors, whi will only work for # certain values! a = np.array([1.0, 2.0, 3.0, 4.5], dtype=dtype) v = VARIANT() v.value = a self.failUnless((v.value == a).all())
def test_mixed(self): np = get_numpy() if np is None: return now = datetime.datetime.now() a = np.array( [11, "22", None, True, now, decimal.Decimal("3.14")]).reshape(2,3) v = VARIANT() v.value = a self.failUnless((v.value == a).all())
def test_VT_UNKNOWN_multi_ndarray(self): np = get_numpy() if np is None: return a = _midlSAFEARRAY(POINTER(IUnknown)) t = _midlSAFEARRAY(POINTER(IUnknown)) self.failUnless(a is t) def com_refcnt(o): "Return the COM refcount of an interface pointer" import gc; gc.collect(); gc.collect() o.AddRef() return o.Release() from comtypes.typeinfo import CreateTypeLib, ICreateTypeLib punk = CreateTypeLib("spam").QueryInterface(IUnknown) # will never be saved to disk # initial refcount initial = com_refcnt(punk) # This should increase the refcount by 4 sa = t.from_param((punk,) * 4) self.failUnlessEqual(initial + 4, com_refcnt(punk)) # Unpacking the array must not change the refcount, and must # return an equal object. Creating an ndarray may change the # refcount. arr = get_array(sa) self.failUnless(isinstance(arr, np.ndarray)) self.failUnlessEqual(np.dtype(object), arr.dtype) self.failUnless((arr == (punk,)*4).all()) self.failUnlessEqual(initial + 8, com_refcnt(punk)) del arr self.failUnlessEqual(initial + 4, com_refcnt(punk)) del sa self.failUnlessEqual(initial, com_refcnt(punk)) # This should increase the refcount by 2 sa = t.from_param((punk, None, punk, None)) self.failUnlessEqual(initial + 2, com_refcnt(punk)) null = POINTER(IUnknown)() arr = get_array(sa) self.failUnless(isinstance(arr, np.ndarray)) self.failUnlessEqual(np.dtype(object), arr.dtype) self.failUnless((arr == (punk, null, punk, null)).all()) del sa del arr self.failUnlessEqual(initial, com_refcnt(punk))
def test_VT_BOOL_ndarray(self): np = get_numpy() if np is None: return t = _midlSAFEARRAY(VARIANT_BOOL) sa = t.from_param([True, False, True, False]) arr = get_array(sa) self.failUnlessEqual(np.dtype(np.bool_), arr.dtype) self.failUnless(isinstance(arr, np.ndarray)) self.failUnless((arr == (True, False, True, False)).all())
def test_VT_BSTR_ndarray(self): np = get_numpy() if np is None: return t = _midlSAFEARRAY(BSTR) sa = t.from_param(["a", "b", "c"]) arr = get_array(sa) self.failUnless(isinstance(arr, np.ndarray)) self.failUnlessEqual(np.dtype('<U1'), arr.dtype) self.failUnless((arr == ("a", "b", "c")).all()) self.failUnlessEqual(SafeArrayGetVartype(sa), VT_BSTR)
def test_VT_VARIANT_ndarray(self): np = get_numpy() if np is None: return t = _midlSAFEARRAY(VARIANT) now = datetime.datetime.now() sa = t.from_param([11, "22", None, True, now, Decimal("3.14")]) arr = get_array(sa) self.failUnlessEqual(np.dtype(object), arr.dtype) self.failUnless(isinstance(arr, np.ndarray)) self.failUnless((arr == (11, "22", None, True, now, Decimal("3.14"))).all()) self.failUnlessEqual(SafeArrayGetVartype(sa), VT_VARIANT)
def test_VT_I4_ndarray(self): np = get_numpy() if np is None: return t = _midlSAFEARRAY(c_long) sa = t.from_param([11, 22, 33]) arr = get_array(sa) self.failUnless(isinstance(arr, np.ndarray)) self.failUnlessEqual(np.dtype(np.int), arr.dtype) self.failUnless((arr == (11, 22, 33)).all()) self.failUnlessEqual(SafeArrayGetVartype(sa), VT_I4)
def test_VT_VARIANT_ndarray(self): np = get_numpy() if np is None: return t = _midlSAFEARRAY(VARIANT) now = datetime.datetime.now() sa = t.from_param([11, "22", None, True, now, Decimal("3.14")]) arr = get_array(sa) self.failUnlessEqual(np.dtype(object), arr.dtype) self.failUnless(isinstance(arr, np.ndarray)) self.failUnless( (arr == (11, "22", None, True, now, Decimal("3.14"))).all()) self.failUnlessEqual(SafeArrayGetVartype(sa), VT_VARIANT)
def test_VT_I4_ndarray(self): np = get_numpy() if np is None: return t = _midlSAFEARRAY(c_long) inarr = np.array([11, 22, 33]) sa = t.from_param(inarr) arr = get_array(sa) self.assertTrue(isinstance(arr, np.ndarray)) self.assertEqual(np.dtype(np.int), arr.dtype) self.assertTrue((arr == inarr).all()) self.assertEqual(SafeArrayGetVartype(sa), VT_I4)
def test_UDT_ndarray(self): np = get_numpy() if np is None: return from comtypes.gen.TestComServerLib import MYCOLOR t = _midlSAFEARRAY(MYCOLOR) self.failUnless(t is _midlSAFEARRAY(MYCOLOR)) sa = t.from_param([MYCOLOR(0, 0, 0), MYCOLOR(1, 2, 3)]) arr = get_array(sa) self.failUnless(isinstance(arr, np.ndarray)) self.failUnlessEqual(np.dtype(object), arr.dtype) self.failUnlessEqual([(x.red, x.green, x.blue) for x in arr], [(0.0, 0.0, 0.0), (1.0, 2.0, 3.0)])
def test_VT_VARIANT_ndarray(self): np = get_numpy() if np is None: return t = _midlSAFEARRAY(VARIANT) now = datetime.datetime.now() inarr = np.array( [11, "22", "33", 44.0, None, True, now, Decimal("3.14")]).reshape(2, 4) sa = t.from_param(inarr) arr = get_array(sa) self.assertEqual(np.dtype(object), arr.dtype) self.assertTrue(isinstance(arr, np.ndarray)) self.assertTrue((arr == inarr).all()) self.assertEqual(SafeArrayGetVartype(sa), VT_VARIANT)
def test_datetime64_ndarray(self): np = get_numpy() if np is None: return try: np.datetime64 except AttributeError: return dates = np.array([ np.datetime64("2000-01-01T05:30:00", "s"), np.datetime64("1800-01-01T05:30:00", "ms"), np.datetime64("2014-03-07T00:12:56", "us"), np.datetime64("2000-01-01T12:34:56", "ns"), ]) t = _midlSAFEARRAY(VARIANT) sa = t.from_param(dates) arr = get_array(sa).astype(dates.dtype) self.failUnless((dates == arr).all())
def test_datetime64(self): np = get_numpy() if np is None: return try: np.datetime64 except AttributeError: return dates = [ np.datetime64("2000-01-01T05:30:00", "s"), np.datetime64("1800-01-01T05:30:00", "ms"), np.datetime64("2000-01-01T12:34:56", "us") ] for date in dates: v = VARIANT() v.value = date self.failUnlessEqual(v.vt, VT_DATE) self.failUnlessEqual(v.value, date.astype(datetime.datetime))
def test_nested_contexts(self): np = get_numpy() if np is None: return t = _midlSAFEARRAY(BSTR) sa = t.from_param(["a", "b", "c"]) first = sa[0] with safearray_as_ndarray: second = sa[0] with safearray_as_ndarray: third = sa[0] fourth = sa[0] fifth = sa[0] self.failUnless(isinstance(first, tuple)) self.failUnless(isinstance(second, np.ndarray)) self.failUnless(isinstance(third, np.ndarray)) self.failUnless(isinstance(fourth, np.ndarray)) self.failUnless(isinstance(fifth, tuple))