<s> import os , sys , errno <EOL> import unittest <EOL> from test import test_support <EOL> threading = test_support . import_module ( "<STR_LIT>" ) <EOL> from platform import machine <EOL> test_support . import_module ( '<STR_LIT>' ) <EOL> from _winreg import * <EOL> try : <EOL> REMOTE_NAME = sys . argv [ sys . argv . index ( "<STR_LIT>" ) + <NUM_LIT:1> ] <EOL> except ( IndexError , ValueError ) : <EOL> REMOTE_NAME = None <EOL> WIN_VER = sys . getwindowsversion ( ) [ : <NUM_LIT:2> ] <EOL> WIN64_MACHINE = True if machine ( ) == "<STR_LIT>" else False <EOL> HAS_REFLECTION = True if WIN_VER < ( <NUM_LIT:6> , <NUM_LIT:1> ) else False <EOL> test_key_base = "<STR_LIT>" % ( os . getpid ( ) , ) <EOL> test_key_name = "<STR_LIT>" + test_key_base <EOL> test_reflect_key_name = "<STR_LIT>" + test_key_base <EOL> test_data = [ <EOL> ( "<STR_LIT>" , <NUM_LIT> , REG_DWORD ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" , REG_SZ ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" , REG_EXPAND_SZ ) , <EOL> ( "<STR_LIT>" , [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:string>" , "<STR_LIT>" ] , REG_MULTI_SZ ) , <EOL> ( "<STR_LIT>" , ( "<STR_LIT>" + chr ( <NUM_LIT:0> ) + "<STR_LIT:data>" ) , REG_BINARY ) , <EOL> ( "<STR_LIT>" , "<STR_LIT:x>" * ( <NUM_LIT:2> ** <NUM_LIT> - <NUM_LIT:1> ) , REG_SZ ) , <EOL> ( "<STR_LIT>" , "<STR_LIT:x>" * ( <NUM_LIT:2> ** <NUM_LIT> ) , REG_BINARY ) , <EOL> ] <EOL> if test_support . have_unicode : <EOL> test_data += [ <EOL> ( unicode ( "<STR_LIT>" ) , unicode ( "<STR_LIT>" ) , REG_SZ , ) , <EOL> ( "<STR_LIT>" , unicode ( "<STR_LIT>" ) , REG_EXPAND_SZ ) , <EOL> ( "<STR_LIT>" , [ unicode ( "<STR_LIT>" ) , unicode ( "<STR_LIT>" ) , unicode ( "<STR_LIT>" ) , <EOL> unicode ( "<STR_LIT>" ) ] , REG_MULTI_SZ ) , <EOL> ( "<STR_LIT>" , [ unicode ( "<STR_LIT>" ) , unicode ( "<STR_LIT>" ) , "<STR_LIT:string>" , <EOL> "<STR_LIT>" ] , REG_MULTI_SZ ) , <EOL> ] <EOL> class BaseWinregTests ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . delete_tree ( HKEY_CURRENT_USER , test_key_name ) <EOL> def delete_tree ( self , root , subkey ) : <EOL> try : <EOL> hkey = OpenKey ( root , subkey , KEY_ALL_ACCESS ) <EOL> except WindowsError : <EOL> return <EOL> while True : <EOL> try : <EOL> subsubkey = EnumKey ( hkey , <NUM_LIT:0> ) <EOL> except WindowsError : <EOL> break <EOL> self . delete_tree ( hkey , subsubkey ) <EOL> CloseKey ( hkey ) <EOL> DeleteKey ( root , subkey ) <EOL> def _write_test_data ( self , root_key , CreateKey = CreateKey ) : <EOL> SetValue ( root_key , test_key_name , REG_SZ , "<STR_LIT>" ) <EOL> key = CreateKey ( root_key , test_key_name ) <EOL> sub_key = CreateKey ( key , "<STR_LIT>" ) <EOL> for value_name , value_data , value_type in test_data : <EOL> SetValueEx ( sub_key , value_name , <NUM_LIT:0> , value_type , value_data ) <EOL> nkeys , nvalues , since_mod = QueryInfoKey ( key ) <EOL> self . assertEqual ( nkeys , <NUM_LIT:1> , "<STR_LIT>" ) <EOL> self . assertEqual ( nvalues , <NUM_LIT:1> , "<STR_LIT>" ) <EOL> nkeys , nvalues , since_mod = QueryInfoKey ( sub_key ) <EOL> self . assertEqual ( nkeys , <NUM_LIT:0> , "<STR_LIT>" ) <EOL> self . assertEqual ( nvalues , len ( test_data ) , <EOL> "<STR_LIT>" ) <EOL> int_sub_key = int ( sub_key ) <EOL> CloseKey ( sub_key ) <EOL> try : <EOL> QueryInfoKey ( int_sub_key ) <EOL> self . fail ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> except EnvironmentError : <EOL> pass <EOL> int_key = int ( key ) <EOL> key . Close ( ) <EOL> try : <EOL> QueryInfoKey ( int_key ) <EOL> self . fail ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> except EnvironmentError : <EOL> pass <EOL> def _read_test_data ( self , root_key , OpenKey = OpenKey ) : <EOL> val = QueryValue ( root_key , test_key_name ) <EOL> self . assertEqual ( val , "<STR_LIT>" , <EOL> "<STR_LIT>" ) <EOL> key = OpenKey ( root_key , test_key_name ) <EOL> with OpenKey ( key , "<STR_LIT>" ) as sub_key : <EOL> index = <NUM_LIT:0> <EOL> while <NUM_LIT:1> : <EOL> try : <EOL> data = EnumValue ( sub_key , index ) <EOL> except EnvironmentError : <EOL> break <EOL> self . assertIn ( data , test_data , <EOL> "<STR_LIT>" ) <EOL> index = index + <NUM_LIT:1> <EOL> self . assertEqual ( index , len ( test_data ) , <EOL> "<STR_LIT>" ) <EOL> for value_name , value_data , value_type in test_data : <EOL> read_val , read_typ = QueryValueEx ( sub_key , value_name ) <EOL> self . assertEqual ( read_val , value_data , <EOL> "<STR_LIT>" ) <EOL> self . assertEqual ( read_typ , value_type , <EOL> "<STR_LIT>" ) <EOL> sub_key . Close ( ) <EOL> read_val = EnumKey ( key , <NUM_LIT:0> ) <EOL> self . assertEqual ( read_val , "<STR_LIT>" , "<STR_LIT>" ) <EOL> try : <EOL> EnumKey ( key , <NUM_LIT:1> ) <EOL> self . fail ( "<STR_LIT>" ) <EOL> except EnvironmentError : <EOL> pass <EOL> key . Close ( ) <EOL> def _delete_test_data ( self , root_key ) : <EOL> key = OpenKey ( root_key , test_key_name , <NUM_LIT:0> , KEY_ALL_ACCESS ) <EOL> sub_key = OpenKey ( key , "<STR_LIT>" , <NUM_LIT:0> , KEY_ALL_ACCESS ) <EOL> for value_name , value_data , value_type in test_data : <EOL> DeleteValue ( sub_key , value_name ) <EOL> nkeys , nvalues , since_mod = QueryInfoKey ( sub_key ) <EOL> self . assertEqual ( nkeys , <NUM_LIT:0> , "<STR_LIT>" ) <EOL> self . assertEqual ( nvalues , <NUM_LIT:0> , "<STR_LIT>" ) <EOL> sub_key . Close ( ) <EOL> DeleteKey ( key , "<STR_LIT>" ) <EOL> try : <EOL> DeleteKey ( key , "<STR_LIT>" ) <EOL> self . fail ( "<STR_LIT>" ) <EOL> except EnvironmentError : <EOL> pass <EOL> key . Close ( ) <EOL> DeleteKey ( root_key , test_key_name ) <EOL> try : <EOL> key = OpenKey ( root_key , test_key_name ) <EOL> self . fail ( "<STR_LIT>" ) <EOL> except WindowsError : <EOL> pass <EOL> def _test_all ( self , root_key ) : <EOL> self . _write_test_data ( root_key ) <EOL> self . _read_test_data ( root_key ) <EOL> self . _delete_test_data ( root_key ) <EOL> class LocalWinregTests ( BaseWinregTests ) : <EOL> def test_registry_works ( self ) : <EOL> self . _test_all ( HKEY_CURRENT_USER ) <EOL> def test_registry_works_extended_functions ( self ) : <EOL> cke = lambda key , sub_key : CreateKeyEx ( key , sub_key , <NUM_LIT:0> , KEY_ALL_ACCESS ) <EOL> self . _write_test_data ( HKEY_CURRENT_USER , cke ) <EOL> oke = lambda key , sub_key : OpenKeyEx ( key , sub_key , <NUM_LIT:0> , KEY_READ ) <EOL> self . _read_test_data ( HKEY_CURRENT_USER , oke ) <EOL> self . _delete_test_data ( HKEY_CURRENT_USER ) <EOL> def test_connect_registry_to_local_machine_works ( self ) : <EOL> h = ConnectRegistry ( None , HKEY_LOCAL_MACHINE ) <EOL> self . assertNotEqual ( h . handle , <NUM_LIT:0> ) <EOL> h . Close ( ) <EOL> self . assertEqual ( h . handle , <NUM_LIT:0> ) <EOL> def test_inexistant_remote_registry ( self ) : <EOL> connect = lambda : ConnectRegistry ( "<STR_LIT>" , HKEY_CURRENT_USER ) <EOL> self . assertRaises ( WindowsError , connect ) <EOL> def test_expand_environment_strings ( self ) : <EOL> r = ExpandEnvironmentStrings ( u"<STR_LIT>" ) <EOL> self . assertEqual ( type ( r ) , unicode ) <EOL> self . assertEqual ( r , os . environ [ "<STR_LIT>" ] + "<STR_LIT>" ) <EOL> def test_context_manager ( self ) : <EOL> try : <EOL> with ConnectRegistry ( None , HKEY_LOCAL_MACHINE ) as h : <EOL> self . assertNotEqual ( h . handle , <NUM_LIT:0> ) <EOL> raise WindowsError <EOL> except WindowsError : <EOL> self . assertEqual ( h . handle , <NUM_LIT:0> ) <EOL> def test_changing_value ( self ) : <EOL> done = False <EOL> class VeryActiveThread ( threading . Thread ) : <EOL> def run ( self ) : <EOL> with CreateKey ( HKEY_CURRENT_USER , test_key_name ) as key : <EOL> use_short = True <EOL> long_string = '<STR_LIT:x>' * <NUM_LIT> <EOL> while not done : <EOL> s = '<STR_LIT:x>' if use_short else long_string <EOL> use_short = not use_short <EOL> SetValue ( key , '<STR_LIT>' , REG_SZ , s ) <EOL> thread = VeryActiveThread ( ) <EOL> thread . start ( ) <EOL> try : <EOL> with CreateKey ( HKEY_CURRENT_USER , <EOL> test_key_name + '<STR_LIT>' ) as key : <EOL> for _ in range ( <NUM_LIT:1000> ) : <EOL> num_subkeys , num_values , t = QueryInfoKey ( key ) <EOL> for i in range ( num_values ) : <EOL> name = EnumValue ( key , i ) <EOL> QueryValue ( key , name [ <NUM_LIT:0> ] ) <EOL> finally : <EOL> done = True <EOL> thread . join ( ) <EOL> with OpenKey ( HKEY_CURRENT_USER , test_key_name , <NUM_LIT:0> , KEY_ALL_ACCESS ) as key : <EOL> DeleteKey ( key , '<STR_LIT>' ) <EOL> DeleteKey ( HKEY_CURRENT_USER , test_key_name ) <EOL> def test_long_key ( self ) : <EOL> name = '<STR_LIT:x>' * <NUM_LIT> <EOL> try : <EOL> with CreateKey ( HKEY_CURRENT_USER , test_key_name ) as key : <EOL> SetValue ( key , name , REG_SZ , '<STR_LIT:x>' ) <EOL> num_subkeys , num_values , t = QueryInfoKey ( key ) <EOL> EnumKey ( key , <NUM_LIT:0> ) <EOL> finally : <EOL> with OpenKey ( HKEY_CURRENT_USER , test_key_name , <NUM_LIT:0> , KEY_ALL_ACCESS ) as key : <EOL> DeleteKey ( key , name ) <EOL> DeleteKey ( HKEY_CURRENT_USER , test_key_name ) <EOL> def test_dynamic_key ( self ) : <EOL> try : <EOL> EnumValue ( HKEY_PERFORMANCE_DATA , <NUM_LIT:0> ) <EOL> except OSError as e : <EOL> if e . errno in ( errno . EPERM , errno . EACCES ) : <EOL> self . skipTest ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> raise <EOL> QueryValueEx ( HKEY_PERFORMANCE_DATA , None ) <EOL> @ unittest . skipUnless ( WIN_VER < ( <NUM_LIT:5> , <NUM_LIT:2> ) , "<STR_LIT>" ) <EOL> def test_reflection_unsupported ( self ) : <EOL> try : <EOL> with CreateKey ( HKEY_CURRENT_USER , test_key_name ) as ck : <EOL> self . assertNotEqual ( ck . handle , <NUM_LIT:0> ) <EOL> key = OpenKey ( HKEY_CURRENT_USER , test_key_name ) <EOL> self . assertNotEqual ( key . handle , <NUM_LIT:0> ) <EOL> with self . assertRaises ( NotImplementedError ) : <EOL> DisableReflectionKey ( key ) <EOL> with self . assertRaises ( NotImplementedError ) : <EOL> EnableReflectionKey ( key ) <EOL> with self . assertRaises ( NotImplementedError ) : <EOL> QueryReflectionKey ( key ) <EOL> with self . assertRaises ( NotImplementedError ) : <EOL> DeleteKeyEx ( HKEY_CURRENT_USER , test_key_name ) <EOL> finally : <EOL> DeleteKey ( HKEY_CURRENT_USER , test_key_name ) <EOL> def test_setvalueex_value_range ( self ) : <EOL> try : <EOL> with CreateKey ( HKEY_CURRENT_USER , test_key_name ) as ck : <EOL> self . assertNotEqual ( ck . handle , <NUM_LIT:0> ) <EOL> SetValueEx ( ck , "<STR_LIT>" , None , REG_DWORD , <NUM_LIT> ) <EOL> finally : <EOL> DeleteKey ( HKEY_CURRENT_USER , test_key_name ) <EOL> def test_setvalueex_with_memoryview ( self ) : <EOL> try : <EOL> with CreateKey ( HKEY_CURRENT_USER , test_key_name ) as ck : <EOL> self . assertNotEqual ( ck . handle , <NUM_LIT:0> ) <EOL> with self . assertRaises ( TypeError ) : <EOL> SetValueEx ( ck , "<STR_LIT>" , None , REG_BINARY , memoryview ( '<STR_LIT>' ) ) <EOL> finally : <EOL> DeleteKey ( HKEY_CURRENT_USER , test_key_name ) <EOL> def test_queryvalueex_return_value ( self ) : <EOL> try : <EOL> with CreateKey ( HKEY_CURRENT_USER , test_key_name ) as ck : <EOL> self . assertNotEqual ( ck . handle , <NUM_LIT:0> ) <EOL> test_val = <NUM_LIT> <EOL> SetValueEx ( ck , "<STR_LIT>" , None , REG_DWORD , test_val ) <EOL> ret_val , ret_type = QueryValueEx ( ck , "<STR_LIT>" ) <EOL> self . assertEqual ( ret_type , REG_DWORD ) <EOL> self . assertEqual ( ret_val , test_val ) <EOL> finally : <EOL> DeleteKey ( HKEY_CURRENT_USER , test_key_name ) <EOL> @ unittest . skipUnless ( REMOTE_NAME , "<STR_LIT>" ) <EOL> class RemoteWinregTests ( BaseWinregTests ) : <EOL> def test_remote_registry_works ( self ) : <EOL> remote_key = ConnectRegistry ( REMOTE_NAME , HKEY_CURRENT_USER ) <EOL> self . _test_all ( remote_key ) <EOL> @ unittest . skipUnless ( WIN64_MACHINE , "<STR_LIT>" ) <EOL> class Win64WinregTests ( BaseWinregTests ) : <EOL> def test_reflection_functions ( self ) : <EOL> with OpenKey ( HKEY_LOCAL_MACHINE , "<STR_LIT>" ) as key : <EOL> self . assertTrue ( QueryReflectionKey ( key ) ) <EOL> self . assertEqual ( None , EnableReflectionKey ( key ) ) <EOL> self . assertEqual ( None , DisableReflectionKey ( key ) ) <EOL> self . assertTrue ( QueryReflectionKey ( key ) ) <EOL> @ unittest . skipUnless ( HAS_REFLECTION , "<STR_LIT>" ) <EOL> def test_reflection ( self ) : <EOL> try : <EOL> with CreateKeyEx ( HKEY_CURRENT_USER , test_reflect_key_name , <NUM_LIT:0> , <EOL> KEY_ALL_ACCESS | KEY_WOW64_32KEY ) as created_key : <EOL> self . assertNotEqual ( created_key . handle , <NUM_LIT:0> ) <EOL> with OpenKey ( HKEY_CURRENT_USER , test_reflect_key_name , <NUM_LIT:0> , <EOL> KEY_ALL_ACCESS | KEY_WOW64_32KEY ) as key : <EOL> self . assertNotEqual ( key . handle , <NUM_LIT:0> ) <EOL> SetValueEx ( created_key , "<STR_LIT>" , <NUM_LIT:0> , REG_SZ , "<STR_LIT>" ) <EOL> open_fail = lambda : OpenKey ( HKEY_CURRENT_USER , <EOL> test_reflect_key_name , <NUM_LIT:0> , <EOL> KEY_READ | KEY_WOW64_64KEY ) <EOL> self . assertRaises ( WindowsError , open_fail ) <EOL> with OpenKey ( HKEY_CURRENT_USER , test_reflect_key_name , <NUM_LIT:0> , <EOL> KEY_ALL_ACCESS | KEY_WOW64_64KEY ) as key : <EOL> self . assertNotEqual ( key . handle , <NUM_LIT:0> ) <EOL> self . assertEqual ( "<STR_LIT>" , QueryValue ( key , "<STR_LIT>" ) ) <EOL> SetValueEx ( key , "<STR_LIT>" , <NUM_LIT:0> , REG_SZ , "<STR_LIT>" ) <EOL> with OpenKey ( HKEY_CURRENT_USER , test_reflect_key_name , <NUM_LIT:0> , <EOL> KEY_READ | KEY_WOW64_32KEY ) as key : <EOL> self . assertEqual ( "<STR_LIT>" , QueryValue ( key , "<STR_LIT>" ) ) <EOL> finally : <EOL> DeleteKeyEx ( HKEY_CURRENT_USER , test_reflect_key_name , <EOL> KEY_WOW64_32KEY , <NUM_LIT:0> ) <EOL> @ unittest . skipUnless ( HAS_REFLECTION , "<STR_LIT>" ) <EOL> def test_disable_reflection ( self ) : <EOL> try : <EOL> with CreateKeyEx ( HKEY_CURRENT_USER , test_reflect_key_name , <NUM_LIT:0> , <EOL> KEY_ALL_ACCESS | KEY_WOW64_32KEY ) as created_key : <EOL> disabled = QueryReflectionKey ( created_key ) <EOL> self . assertEqual ( type ( disabled ) , bool ) <EOL> self . assertFalse ( disabled ) <EOL> DisableReflectionKey ( created_key ) <EOL> self . assertTrue ( QueryReflectionKey ( created_key ) ) <EOL> open_fail = lambda : OpenKeyEx ( HKEY_CURRENT_USER , <EOL> test_reflect_key_name , <NUM_LIT:0> , <EOL> KEY_READ | KEY_WOW64_64KEY ) <EOL> self . assertRaises ( WindowsError , open_fail ) <EOL> with OpenKeyEx ( HKEY_CURRENT_USER , test_reflect_key_name , <NUM_LIT:0> , <EOL> KEY_READ | KEY_WOW64_32KEY ) as key : <EOL> self . assertNotEqual ( key . handle , <NUM_LIT:0> ) <EOL> finally : <EOL> DeleteKeyEx ( HKEY_CURRENT_USER , test_reflect_key_name , <EOL> KEY_WOW64_32KEY , <NUM_LIT:0> ) <EOL> def test_exception_numbers ( self ) : <EOL> with self . assertRaises ( WindowsError ) as ctx : <EOL> QueryValue ( HKEY_CLASSES_ROOT , '<STR_LIT>' ) <EOL> self . assertEqual ( ctx . exception . errno , <NUM_LIT:2> ) <EOL> def test_main ( ) : <EOL> test_support . run_unittest ( LocalWinregTests , RemoteWinregTests , <EOL> Win64WinregTests ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> if not REMOTE_NAME : <EOL> print "<STR_LIT>" , <EOL> print "<STR_LIT>" <EOL> test_main ( ) </s>
