<s> """<STR_LIT>""" <EOL> import sys <EOL> import signal <EOL> import traceback <EOL> import threading <EOL> from pdb import Pdb <EOL> from ants . utils . engine import format_engine_status <EOL> from ants . utils . trackref import format_live_refs <EOL> from ants . utils import log <EOL> class StackTraceDump ( object ) : <EOL> def __init__ ( self , crawler = None ) : <EOL> self . crawler = crawler <EOL> try : <EOL> signal . signal ( signal . SIGUSR2 , self . dump_stacktrace ) <EOL> signal . signal ( signal . SIGQUIT , self . dump_stacktrace ) <EOL> except AttributeError : <EOL> pass <EOL> @ classmethod <EOL> def from_crawler ( cls , crawler ) : <EOL> return cls ( crawler ) <EOL> def dump_stacktrace ( self , signum , frame ) : <EOL> stackdumps = self . _thread_stacks ( ) <EOL> enginestatus = format_engine_status ( self . crawler . engine ) <EOL> liverefs = format_live_refs ( ) <EOL> msg = "<STR_LIT>" "<STR_LIT>" . format ( enginestatus , liverefs , stackdumps ) <EOL> log . spider_log ( msg ) <EOL> def _thread_stacks ( self ) : <EOL> id2name = dict ( ( th . ident , th . name ) for th in threading . enumerate ( ) ) <EOL> dumps = '<STR_LIT>' <EOL> for id_ , frame in sys . _current_frames ( ) . items ( ) : <EOL> name = id2name . get ( id_ , '<STR_LIT>' ) <EOL> dump = '<STR_LIT>' . join ( traceback . format_stack ( frame ) ) <EOL> dumps += "<STR_LIT>" . format ( name , id_ , dump ) <EOL> return dumps <EOL> class Debugger ( object ) : <EOL> def __init__ ( self ) : <EOL> try : <EOL> signal . signal ( signal . SIGUSR2 , self . _enter_debugger ) <EOL> except AttributeError : <EOL> pass <EOL> def _enter_debugger ( self , signum , frame ) : <EOL> Pdb ( ) . set_trace ( frame . f_back ) </s>
