<s> import itertools <EOL> import weakref <EOL> import atexit <EOL> import threading <EOL> from multiprocessing . process import current_process , active_children <EOL> __all__ = [ <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:info>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' <EOL> ] <EOL> NOTSET = <NUM_LIT:0> <EOL> SUBDEBUG = <NUM_LIT:5> <EOL> DEBUG = <NUM_LIT:10> <EOL> INFO = <NUM_LIT:20> <EOL> SUBWARNING = <NUM_LIT> <EOL> LOGGER_NAME = '<STR_LIT>' <EOL> DEFAULT_LOGGING_FORMAT = '<STR_LIT>' <EOL> _logger = None <EOL> _log_to_stderr = False <EOL> def sub_debug ( msg , * args ) : <EOL> if _logger : <EOL> _logger . log ( SUBDEBUG , msg , * args ) <EOL> def debug ( msg , * args ) : <EOL> if _logger : <EOL> _logger . log ( DEBUG , msg , * args ) <EOL> def info ( msg , * args ) : <EOL> if _logger : <EOL> _logger . log ( INFO , msg , * args ) <EOL> def sub_warning ( msg , * args ) : <EOL> if _logger : <EOL> _logger . log ( SUBWARNING , msg , * args ) <EOL> def get_logger ( ) : <EOL> '''<STR_LIT>''' <EOL> global _logger <EOL> if not _logger : <EOL> import logging , atexit <EOL> if hasattr ( atexit , '<STR_LIT>' ) : <EOL> atexit . unregister ( _exit_function ) <EOL> atexit . register ( _exit_function ) <EOL> else : <EOL> atexit . _exithandlers . remove ( ( _exit_function , ( ) , { } ) ) <EOL> atexit . _exithandlers . append ( ( _exit_function , ( ) , { } ) ) <EOL> _check_logger_class ( ) <EOL> _logger = logging . getLogger ( LOGGER_NAME ) <EOL> return _logger <EOL> def _check_logger_class ( ) : <EOL> '''<STR_LIT>''' <EOL> import logging <EOL> if hasattr ( logging , '<STR_LIT>' ) : <EOL> return <EOL> logging . _acquireLock ( ) <EOL> try : <EOL> OldLoggerClass = logging . getLoggerClass ( ) <EOL> if not getattr ( OldLoggerClass , '<STR_LIT>' , False ) : <EOL> class ProcessAwareLogger ( OldLoggerClass ) : <EOL> _process_aware = True <EOL> def makeRecord ( self , * args , ** kwds ) : <EOL> record = OldLoggerClass . makeRecord ( self , * args , ** kwds ) <EOL> record . processName = current_process ( ) . _name <EOL> return record <EOL> logging . setLoggerClass ( ProcessAwareLogger ) <EOL> finally : <EOL> logging . _releaseLock ( ) <EOL> def log_to_stderr ( level = None ) : <EOL> '''<STR_LIT>''' <EOL> global _log_to_stderr <EOL> import logging <EOL> logger = get_logger ( ) <EOL> formatter = logging . Formatter ( DEFAULT_LOGGING_FORMAT ) <EOL> handler = logging . StreamHandler ( ) <EOL> handler . setFormatter ( formatter ) <EOL> logger . addHandler ( handler ) <EOL> if level is not None : <EOL> logger . setLevel ( level ) <EOL> _log_to_stderr = True <EOL> def get_temp_dir ( ) : <EOL> if current_process ( ) . _tempdir is None : <EOL> import shutil , tempfile <EOL> tempdir = tempfile . mkdtemp ( prefix = '<STR_LIT>' ) <EOL> info ( '<STR_LIT>' , tempdir ) <EOL> Finalize ( None , shutil . rmtree , args = [ tempdir ] , exitpriority = - <NUM_LIT:100> ) <EOL> current_process ( ) . _tempdir = tempdir <EOL> return current_process ( ) . _tempdir <EOL> _afterfork_registry = weakref . WeakValueDictionary ( ) <EOL> _afterfork_counter = itertools . count ( ) <EOL> def _run_after_forkers ( ) : <EOL> items = list ( _afterfork_registry . items ( ) ) <EOL> items . sort ( ) <EOL> for ( index , ident , func ) , obj in items : <EOL> try : <EOL> func ( obj ) <EOL> except Exception , e : <EOL> info ( '<STR_LIT>' , e ) <EOL> def register_after_fork ( obj , func ) : <EOL> _afterfork_registry [ ( _afterfork_counter . next ( ) , id ( obj ) , func ) ] = obj <EOL> _finalizer_registry = { } <EOL> _finalizer_counter = itertools . count ( ) <EOL> class Finalize ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , obj , callback , args = ( ) , kwargs = None , exitpriority = None ) : <EOL> assert exitpriority is None or type ( exitpriority ) is int <EOL> if obj is not None : <EOL> self . _weakref = weakref . ref ( obj , self ) <EOL> else : <EOL> assert exitpriority is not None <EOL> self . _callback = callback <EOL> self . _args = args <EOL> self . _kwargs = kwargs or { } <EOL> self . _key = ( exitpriority , _finalizer_counter . next ( ) ) <EOL> _finalizer_registry [ self . _key ] = self <EOL> def __call__ ( self , wr = None ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> del _finalizer_registry [ self . _key ] <EOL> except KeyError : <EOL> sub_debug ( '<STR_LIT>' ) <EOL> else : <EOL> sub_debug ( '<STR_LIT>' , <EOL> self . _callback , self . _args , self . _kwargs ) <EOL> res = self . _callback ( * self . _args , ** self . _kwargs ) <EOL> self . _weakref = self . _callback = self . _args = self . _kwargs = self . _key = None <EOL> return res <EOL> def cancel ( self ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> del _finalizer_registry [ self . _key ] <EOL> except KeyError : <EOL> pass <EOL> else : <EOL> self . _weakref = self . _callback = self . _args = self . _kwargs = self . _key = None <EOL> def still_active ( self ) : <EOL> '''<STR_LIT>''' <EOL> return self . _key in _finalizer_registry <EOL> def __repr__ ( self ) : <EOL> try : <EOL> obj = self . _weakref ( ) <EOL> except ( AttributeError , TypeError ) : <EOL> obj = None <EOL> if obj is None : <EOL> return '<STR_LIT>' <EOL> x = '<STR_LIT>' % getattr ( self . _callback , '<STR_LIT>' , self . _callback ) <EOL> if self . _args : <EOL> x += '<STR_LIT>' + str ( self . _args ) <EOL> if self . _kwargs : <EOL> x += '<STR_LIT>' + str ( self . _kwargs ) <EOL> if self . _key [ <NUM_LIT:0> ] is not None : <EOL> x += '<STR_LIT>' + str ( self . _key [ <NUM_LIT:0> ] ) <EOL> return x + '<STR_LIT:>>' <EOL> def _run_finalizers ( minpriority = None ) : <EOL> '''<STR_LIT>''' <EOL> if minpriority is None : <EOL> f = lambda p : p [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] is not None <EOL> else : <EOL> f = lambda p : p [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] is not None and p [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] >= minpriority <EOL> items = [ x for x in _finalizer_registry . items ( ) if f ( x ) ] <EOL> items . sort ( reverse = True ) <EOL> for key , finalizer in items : <EOL> sub_debug ( '<STR_LIT>' , finalizer ) <EOL> try : <EOL> finalizer ( ) <EOL> except Exception : <EOL> import traceback <EOL> traceback . print_exc ( ) <EOL> if minpriority is None : <EOL> _finalizer_registry . clear ( ) <EOL> def is_exiting ( ) : <EOL> '''<STR_LIT>''' <EOL> return _exiting or _exiting is None <EOL> _exiting = False <EOL> def _exit_function ( ) : <EOL> global _exiting <EOL> info ( '<STR_LIT>' ) <EOL> debug ( '<STR_LIT>' ) <EOL> _run_finalizers ( <NUM_LIT:0> ) <EOL> for p in active_children ( ) : <EOL> if p . _daemonic : <EOL> info ( '<STR_LIT>' , p . name ) <EOL> p . _popen . terminate ( ) <EOL> for p in active_children ( ) : <EOL> info ( '<STR_LIT>' , p . name ) <EOL> p . join ( ) <EOL> debug ( '<STR_LIT>' ) <EOL> _run_finalizers ( ) <EOL> atexit . register ( _exit_function ) <EOL> class ForkAwareThreadLock ( object ) : <EOL> def __init__ ( self ) : <EOL> self . _lock = threading . Lock ( ) <EOL> self . acquire = self . _lock . acquire <EOL> self . release = self . _lock . release <EOL> register_after_fork ( self , ForkAwareThreadLock . __init__ ) <EOL> class ForkAwareLocal ( threading . local ) : <EOL> def __init__ ( self ) : <EOL> register_after_fork ( self , lambda obj : obj . __dict__ . clear ( ) ) <EOL> def __reduce__ ( self ) : <EOL> return type ( self ) , ( ) </s>
