<s> from ctypes import ( <EOL> POINTER , byref , c_byte , c_char , c_double , c_float , c_int , c_int64 , c_short , <EOL> c_ubyte , c_ushort , c_uint , c_uint64 , pointer , <EOL> ) <EOL> import datetime <EOL> import decimal <EOL> import sys <EOL> import unittest <EOL> from comtypes import IUnknown , GUID <EOL> from comtypes . automation import ( <EOL> VARIANT , DISPPARAMS , VT_NULL , VT_EMPTY , VT_ERROR , VT_I1 , VT_I2 , VT_I4 , <EOL> VT_I8 , VT_UI1 , VT_UI2 , VT_UI4 , VT_UI8 , VT_R4 , VT_R8 , VT_BYREF , VT_BSTR , <EOL> VT_DATE , VT_DECIMAL , VT_CY , ) <EOL> from comtypes . typeinfo import LoadRegTypeLib <EOL> from comtypes . test import get_numpy <EOL> from comtypes . test . find_memleak import find_memleak <EOL> def get_refcnt ( comptr ) : <EOL> if not comptr : <EOL> return <NUM_LIT:0> <EOL> comptr . AddRef ( ) <EOL> return comptr . Release ( ) <EOL> class VariantTestCase ( unittest . TestCase ) : <EOL> def test_constants ( self ) : <EOL> empty = VARIANT . empty <EOL> self . failUnlessEqual ( empty . vt , VT_EMPTY ) <EOL> self . failUnless ( empty . value is None ) <EOL> null = VARIANT . null <EOL> self . failUnlessEqual ( null . vt , VT_NULL ) <EOL> self . failUnless ( null . value is None ) <EOL> missing = VARIANT . missing <EOL> self . failUnlessEqual ( missing . vt , VT_ERROR ) <EOL> self . assertRaises ( NotImplementedError , lambda : missing . value ) <EOL> def test_com_refcounts ( self ) : <EOL> tlb = LoadRegTypeLib ( GUID ( "<STR_LIT>" ) , <NUM_LIT:2> , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> rc = get_refcnt ( tlb ) <EOL> p = tlb . QueryInterface ( IUnknown ) <EOL> self . failUnlessEqual ( get_refcnt ( tlb ) , rc + <NUM_LIT:1> ) <EOL> del p <EOL> self . failUnlessEqual ( get_refcnt ( tlb ) , rc ) <EOL> def test_com_pointers ( self ) : <EOL> tlb = LoadRegTypeLib ( GUID ( "<STR_LIT>" ) , <NUM_LIT:2> , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> rc = get_refcnt ( tlb ) <EOL> v = VARIANT ( tlb ) <EOL> self . failUnlessEqual ( get_refcnt ( tlb ) , rc + <NUM_LIT:1> ) <EOL> p = v . value <EOL> self . failUnlessEqual ( get_refcnt ( tlb ) , rc + <NUM_LIT:2> ) <EOL> del p <EOL> self . failUnlessEqual ( get_refcnt ( tlb ) , rc + <NUM_LIT:1> ) <EOL> v . value = None <EOL> self . failUnlessEqual ( get_refcnt ( tlb ) , rc ) <EOL> def test_null_com_pointers ( self ) : <EOL> p = POINTER ( IUnknown ) ( ) <EOL> self . failUnlessEqual ( get_refcnt ( p ) , <NUM_LIT:0> ) <EOL> VARIANT ( p ) <EOL> self . failUnlessEqual ( get_refcnt ( p ) , <NUM_LIT:0> ) <EOL> def test_dispparams ( self ) : <EOL> d = DISPPARAMS ( ) <EOL> d . rgvarg = ( VARIANT * <NUM_LIT:3> ) ( ) <EOL> values = [ <NUM_LIT:1> , <NUM_LIT:5> , <NUM_LIT:7> ] <EOL> for i , v in enumerate ( values ) : <EOL> d . rgvarg [ i ] . value = v <EOL> result = [ d . rgvarg [ i ] . value for i in range ( <NUM_LIT:3> ) ] <EOL> self . failUnlessEqual ( result , values ) <EOL> def test_pythonobjects ( self ) : <EOL> objects = [ None , <NUM_LIT> , <NUM_LIT> , True , False , "<STR_LIT:abc>" , u"<STR_LIT:abc>" , <NUM_LIT:7> L ] <EOL> for x in objects : <EOL> v = VARIANT ( x ) <EOL> self . failUnlessEqual ( x , v . value ) <EOL> def test_integers ( self ) : <EOL> v = VARIANT ( ) <EOL> if ( hasattr ( sys , "<STR_LIT>" ) ) : <EOL> v . value = sys . maxint <EOL> self . failUnlessEqual ( v . value , sys . maxint ) <EOL> self . failUnlessEqual ( type ( v . value ) , int ) <EOL> v . value += <NUM_LIT:1> <EOL> self . failUnlessEqual ( v . value , sys . maxint + <NUM_LIT:1> ) <EOL> self . failUnlessEqual ( type ( v . value ) , long ) <EOL> v . value = <NUM_LIT:1> L <EOL> self . failUnlessEqual ( v . value , <NUM_LIT:1> ) <EOL> self . failUnlessEqual ( type ( v . value ) , int ) <EOL> def test_datetime ( self ) : <EOL> now = datetime . datetime . now ( ) <EOL> v = VARIANT ( ) <EOL> v . value = now <EOL> self . failUnlessEqual ( v . vt , VT_DATE ) <EOL> self . failUnlessEqual ( v . value , now ) <EOL> def test_datetime64 ( self ) : <EOL> np = get_numpy ( ) <EOL> if np is None : <EOL> return <EOL> try : <EOL> np . datetime64 <EOL> except AttributeError : <EOL> return <EOL> dates = [ <EOL> np . datetime64 ( "<STR_LIT>" , "<STR_LIT:s>" ) , <EOL> np . datetime64 ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> np . datetime64 ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> ] <EOL> for date in dates : <EOL> v = VARIANT ( ) <EOL> v . value = date <EOL> self . failUnlessEqual ( v . vt , VT_DATE ) <EOL> self . failUnlessEqual ( v . value , date . astype ( datetime . datetime ) ) <EOL> def test_decimal_as_currency ( self ) : <EOL> value = decimal . Decimal ( '<STR_LIT>' ) <EOL> v = VARIANT ( ) <EOL> v . value = value <EOL> self . failUnlessEqual ( v . vt , VT_CY ) <EOL> self . failUnlessEqual ( v . value , value ) <EOL> def test_decimal_as_decimal ( self ) : <EOL> v = VARIANT ( ) <EOL> v . vt = VT_DECIMAL <EOL> v . decVal . Lo64 = <NUM_LIT> <EOL> v . decVal . scale = <NUM_LIT:3> <EOL> self . failUnlessEqual ( v . value , decimal . Decimal ( '<STR_LIT>' ) ) <EOL> v . decVal . sign = <NUM_LIT> <EOL> self . failUnlessEqual ( v . value , decimal . Decimal ( '<STR_LIT>' ) ) <EOL> v . decVal . scale = <NUM_LIT> <EOL> self . failUnlessEqual ( v . value , decimal . Decimal ( '<STR_LIT>' ) ) <EOL> v . decVal . scale = <NUM_LIT:12> <EOL> v . decVal . Hi32 = <NUM_LIT:100> <EOL> self . failUnlessEqual ( <EOL> v . value , decimal . Decimal ( '<STR_LIT>' ) ) <EOL> def test_BSTR ( self ) : <EOL> v = VARIANT ( ) <EOL> v . value = u"<STR_LIT>" <EOL> self . failUnlessEqual ( v . value , "<STR_LIT>" ) <EOL> v . value = None <EOL> v . _ . VT_I4 = <NUM_LIT:0> <EOL> v . vt = VT_BSTR <EOL> self . failUnless ( v . value in ( "<STR_LIT>" , None ) ) <EOL> def test_empty_BSTR ( self ) : <EOL> v = VARIANT ( ) <EOL> v . value = "<STR_LIT>" <EOL> self . assertEqual ( v . vt , VT_BSTR ) <EOL> def test_UDT ( self ) : <EOL> from comtypes . gen . TestComServerLib import MYCOLOR <EOL> v = VARIANT ( MYCOLOR ( red = <NUM_LIT:1.0> , green = <NUM_LIT> , blue = <NUM_LIT> ) ) <EOL> value = v . value <EOL> self . failUnlessEqual ( ( <NUM_LIT:1.0> , <NUM_LIT> , <NUM_LIT> ) , <EOL> ( value . red , value . green , value . blue ) ) <EOL> def func ( ) : <EOL> v = VARIANT ( MYCOLOR ( red = <NUM_LIT:1.0> , green = <NUM_LIT> , blue = <NUM_LIT> ) ) <EOL> return v . value <EOL> bytes = find_memleak ( func ) <EOL> self . failIf ( bytes , "<STR_LIT>" % bytes ) <EOL> def test_ctypes_in_variant ( self ) : <EOL> v = VARIANT ( ) <EOL> objs = [ ( c_ubyte ( <NUM_LIT:3> ) , VT_UI1 ) , <EOL> ( c_char ( "<STR_LIT:x>" ) , VT_UI1 ) , <EOL> ( c_byte ( <NUM_LIT:3> ) , VT_I1 ) , <EOL> ( c_ushort ( <NUM_LIT:3> ) , VT_UI2 ) , <EOL> ( c_short ( <NUM_LIT:3> ) , VT_I2 ) , <EOL> ( c_uint ( <NUM_LIT:3> ) , VT_UI4 ) , <EOL> ( c_uint64 ( <NUM_LIT:2> ** <NUM_LIT:64> ) , VT_UI8 ) , <EOL> ( c_int ( <NUM_LIT:3> ) , VT_I4 ) , <EOL> ( c_int64 ( <NUM_LIT:2> ** <NUM_LIT:32> ) , VT_I8 ) , <EOL> ( c_double ( <NUM_LIT> ) , VT_R8 ) , <EOL> ( c_float ( <NUM_LIT> ) , VT_R4 ) , <EOL> ] <EOL> for value , vt in objs : <EOL> v . value = value <EOL> self . failUnlessEqual ( v . vt , vt ) <EOL> def test_byref ( self ) : <EOL> variable = c_int ( <NUM_LIT> ) <EOL> v = VARIANT ( byref ( variable ) ) <EOL> self . failUnlessEqual ( v [ <NUM_LIT:0> ] , <NUM_LIT> ) <EOL> self . failUnlessEqual ( v . vt , VT_BYREF | VT_I4 ) <EOL> variable . value = <NUM_LIT> <EOL> self . failUnlessEqual ( v [ <NUM_LIT:0> ] , <NUM_LIT> ) <EOL> variable = c_int ( <NUM_LIT> ) <EOL> v = VARIANT ( pointer ( variable ) ) <EOL> self . failUnlessEqual ( v [ <NUM_LIT:0> ] , <NUM_LIT> ) <EOL> self . failUnlessEqual ( v . vt , VT_BYREF | VT_I4 ) <EOL> variable . value = <NUM_LIT> <EOL> self . failUnlessEqual ( v [ <NUM_LIT:0> ] , <NUM_LIT> ) <EOL> class NdArrayTest ( unittest . TestCase ) : <EOL> def test_double ( self ) : <EOL> np = get_numpy ( ) <EOL> if np is None : <EOL> return <EOL> for dtype in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> a = np . array ( [ <NUM_LIT:1.0> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , dtype = dtype ) <EOL> v = VARIANT ( ) <EOL> v . value = a <EOL> self . failUnless ( ( v . value == a ) . all ( ) ) <EOL> def test_int ( self ) : <EOL> np = get_numpy ( ) <EOL> if np is None : <EOL> return <EOL> for dtype in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> a = np . array ( ( <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ) , dtype = dtype ) <EOL> v = VARIANT ( ) <EOL> v . value = a <EOL> self . failUnless ( ( v . value == a ) . all ( ) ) <EOL> def test_mixed ( self ) : <EOL> np = get_numpy ( ) <EOL> if np is None : <EOL> return <EOL> now = datetime . datetime . now ( ) <EOL> a = np . array ( <EOL> [ <NUM_LIT:11> , "<STR_LIT>" , None , True , now , decimal . Decimal ( "<STR_LIT>" ) ] ) . reshape ( <NUM_LIT:2> , <NUM_LIT:3> ) <EOL> v = VARIANT ( ) <EOL> v . value = a <EOL> self . failUnless ( ( v . value == a ) . all ( ) ) <EOL> class ArrayTest ( unittest . TestCase ) : <EOL> def test_double ( self ) : <EOL> import array <EOL> for typecode in "<STR_LIT>" : <EOL> a = array . array ( typecode , ( <NUM_LIT:1.0> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) ) <EOL> v = VARIANT ( ) <EOL> v . value = a <EOL> self . failUnlessEqual ( v . value , ( <NUM_LIT:1.0> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) ) <EOL> def test_int ( self ) : <EOL> import array <EOL> for typecode in "<STR_LIT>" : <EOL> a = array . array ( typecode , ( <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ) ) <EOL> v = VARIANT ( ) <EOL> v . value = a <EOL> self . failUnlessEqual ( v . value , ( <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ) ) <EOL> def run_test ( rep , msg , func = None , previous = { } , results = { } ) : <EOL> if func is None : <EOL> locals = sys . _getframe ( <NUM_LIT:1> ) . f_locals <EOL> func = eval ( "<STR_LIT>" % msg , locals ) <EOL> items = xrange ( rep ) <EOL> from time import clock <EOL> start = clock ( ) <EOL> for i in items : <EOL> func ( ) ; func ( ) ; func ( ) ; func ( ) ; func ( ) <EOL> stop = clock ( ) <EOL> duration = ( stop - start ) * <NUM_LIT> / <NUM_LIT:5> / rep <EOL> try : <EOL> prev = previous [ msg ] <EOL> except KeyError : <EOL> print >> sys . stderr , "<STR_LIT>" % ( msg , duration ) <EOL> delta = <NUM_LIT:0.0> <EOL> else : <EOL> delta = duration / prev * <NUM_LIT> <EOL> print >> sys . stderr , "<STR_LIT>" % ( msg , duration , delta ) <EOL> results [ msg ] = duration <EOL> return delta <EOL> def check_perf ( rep = <NUM_LIT> ) : <EOL> from ctypes import c_int , byref <EOL> from comtypes . automation import VARIANT <EOL> import comtypes . automation <EOL> print comtypes . automation <EOL> variable = c_int ( ) <EOL> by_var = byref ( variable ) <EOL> ptr_var = pointer ( variable ) <EOL> import cPickle <EOL> try : <EOL> previous = cPickle . load ( open ( "<STR_LIT>" , "<STR_LIT:rb>" ) ) <EOL> except IOError : <EOL> previous = { } <EOL> results = { } <EOL> d = <NUM_LIT:0.0> <EOL> d += run_test ( rep , "<STR_LIT>" , previous = previous , results = results ) <EOL> d += run_test ( rep , "<STR_LIT>" , previous = previous , results = results ) <EOL> d += run_test ( rep , "<STR_LIT>" , previous = previous , results = results ) <EOL> d += run_test ( rep , "<STR_LIT>" , previous = previous , results = results ) <EOL> d += run_test ( rep , "<STR_LIT>" , previous = previous , results = results ) <EOL> d += run_test ( rep , "<STR_LIT>" , previous = previous , results = results ) <EOL> d += run_test ( rep , "<STR_LIT>" , previous = previous , results = results ) <EOL> d += run_test ( rep , "<STR_LIT>" , previous = previous , results = results ) <EOL> d += run_test ( rep , "<STR_LIT>" , previous = previous , results = results ) <EOL> d += run_test ( rep , "<STR_LIT>" , previous = previous , results = results ) <EOL> d += run_test ( rep , "<STR_LIT>" , previous = previous , results = results ) <EOL> d += run_test ( rep , "<STR_LIT>" , previous = previous , results = results ) <EOL> print "<STR_LIT>" % ( d / <NUM_LIT:10> ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> try : <EOL> unittest . main ( ) <EOL> except SystemExit : <EOL> pass <EOL> import comtypes <EOL> print "<STR_LIT>" % ( comtypes . __version__ , sys . version . split ( ) [ <NUM_LIT:0> ] , ) <EOL> check_perf ( ) </s>
