<s> import wx <EOL> import wx . webkit <EOL> import sys <EOL> import traceback <EOL> import unittest <EOL> _hooked = False <EOL> _app = None <EOL> _current_main_loop_instance = <NUM_LIT:0> <EOL> _wx_frame = None <EOL> _pending_tasks_timer = None <EOL> _pending_tasks = [ ] <EOL> _unittests_running = False <EOL> _active_test_result = None <EOL> _active_test = None <EOL> _quit_handlers = [ ] <EOL> def init_main_loop ( ) : <EOL> global _hooked <EOL> if not _hooked : <EOL> _hooked = True <EOL> old_hook = sys . excepthook <EOL> def hook ( exc , value , tb ) : <EOL> if is_main_loop_running ( ) and _active_test : <EOL> if isinstance ( value , unittest . TestCase . failureException ) : <EOL> _active_test_result . addFailure ( _active_test , ( exc , value , tb ) ) <EOL> else : <EOL> if not str ( value ) . startswith ( "<STR_LIT>" ) : <EOL> print "<STR_LIT>" <EOL> _active_test_result . addError ( _active_test , ( exc , value , tb ) ) <EOL> quit_main_loop ( ) <EOL> return <EOL> else : <EOL> old_hook ( exc , value , tb ) <EOL> return <EOL> sys . excepthook = hook <EOL> global _app <EOL> if not _app : <EOL> _app = wx . App ( False ) <EOL> _app . SetAppName ( "<STR_LIT>" ) <EOL> global _wx_frame <EOL> _wx_frame = wx . Frame ( None , - <NUM_LIT:1> , "<STR_LIT>" ) ; <EOL> def _run_pending_tasks ( e ) : <EOL> pending = list ( _pending_tasks ) <EOL> del _pending_tasks [ : ] <EOL> for cb , args in pending : <EOL> cb ( * args ) <EOL> def post_task ( cb , * args ) : <EOL> init_main_loop ( ) <EOL> global _pending_tasks_timer <EOL> if not _pending_tasks_timer : <EOL> _pending_tasks_timer = wx . Timer ( None , - <NUM_LIT:1> ) <EOL> _pending_tasks_timer . Bind ( wx . EVT_TIMER , _run_pending_tasks , _pending_tasks_timer ) <EOL> _pending_tasks . append ( ( cb , args ) ) <EOL> if not _pending_tasks_timer . IsRunning ( ) : <EOL> _pending_tasks_timer . Start ( <NUM_LIT:11> , True ) <EOL> def post_delayed_task ( cb , delay , * args ) : <EOL> init_main_loop ( ) <EOL> timer = wx . Timer ( None , - <NUM_LIT:1> ) <EOL> main_loop_instance_at_post = _current_main_loop_instance <EOL> def on_run ( e ) : <EOL> try : <EOL> if _current_main_loop_instance == main_loop_instance_at_post : <EOL> cb ( * args ) <EOL> finally : <EOL> timer . Destroy ( ) <EOL> timer . Bind ( wx . EVT_TIMER , on_run , timer ) <EOL> timer . Start ( max ( <NUM_LIT:1> , int ( delay * <NUM_LIT:1000> ) ) , True ) <EOL> def add_quit_handler ( cb ) : <EOL> _quit_handlers . insert ( <NUM_LIT:0> , cb ) <EOL> def set_unittests_running ( running ) : <EOL> global _unittests_running <EOL> _unittests_running = running <EOL> def set_active_test ( test , result ) : <EOL> global _active_test <EOL> global _active_test_result <EOL> _active_test = test <EOL> _active_test_result = result <EOL> def is_main_loop_running ( ) : <EOL> if not _app : <EOL> return False <EOL> return _app . IsMainLoopRunning ( ) <EOL> def run_main_loop ( ) : <EOL> global _current_main_loop_instance <EOL> if _unittests_running and not _active_test : <EOL> _current_main_loop_instance += <NUM_LIT:1> <EOL> del _pending_tasks [ : ] <EOL> del _quit_handlers [ : ] <EOL> raise Exception ( "<STR_LIT>" ) <EOL> global _app <EOL> global _pending_tasks_timer <EOL> init_main_loop ( ) <EOL> assert not is_main_loop_running ( ) <EOL> try : <EOL> _app . MainLoop ( ) <EOL> except : <EOL> traceback . print_exc ( ) <EOL> finally : <EOL> _current_main_loop_instance += <NUM_LIT:1> <EOL> del _pending_tasks [ : ] <EOL> _app . Destroy ( ) <EOL> _app = None <EOL> global _quitting <EOL> _quitting = False <EOL> _quitting = False <EOL> def quit_main_loop ( ) : <EOL> global _current_main_loop_instance <EOL> _current_main_loop_instance += <NUM_LIT:1> <EOL> global _quitting <EOL> if _quitting : <EOL> return <EOL> _quitting = True <EOL> def do_quit ( ) : <EOL> global _wx_frame <EOL> if _wx_frame : <EOL> _wx_frame . Destroy ( ) <EOL> _wx_frame = None <EOL> global _pending_tasks_timer <EOL> if _pending_tasks_timer : <EOL> _pending_tasks_timer . Destroy ( ) <EOL> _pending_tasks_timer = None <EOL> for cb in _quit_handlers : <EOL> cb ( ) <EOL> del _quit_handlers [ : ] <EOL> _app . ExitMainLoop ( ) <EOL> post_task ( do_quit ) </s>
