<s> """<STR_LIT>""" <EOL> from __future__ import print_function , division , absolute_import <EOL> import imp <EOL> import marshal <EOL> import sys <EOL> from types import FunctionType , ModuleType <EOL> from . import bytecode , compiler <EOL> def _rebuild_reduction ( cls , * args ) : <EOL> """<STR_LIT>""" <EOL> return cls . _rebuild ( * args ) <EOL> class _ModuleRef ( object ) : <EOL> def __init__ ( self , name ) : <EOL> self . name = name <EOL> def __reduce__ ( self ) : <EOL> return _rebuild_module , ( self . name , ) <EOL> def _rebuild_module ( name ) : <EOL> if name is None : <EOL> raise ImportError ( "<STR_LIT>" ) <EOL> __import__ ( name ) <EOL> return sys . modules [ name ] <EOL> def _get_function_globals_for_reduction ( func ) : <EOL> """<STR_LIT>""" <EOL> bc = bytecode . ByteCode ( func ) <EOL> globs = bc . get_used_globals ( ) <EOL> for k , v in globs . items ( ) : <EOL> if isinstance ( v , ModuleType ) : <EOL> globs [ k ] = _ModuleRef ( v . __name__ ) <EOL> globs [ '<STR_LIT>' ] = func . __module__ <EOL> return globs <EOL> def _reduce_function ( func , globs ) : <EOL> """<STR_LIT>""" <EOL> if func . __closure__ : <EOL> cells = [ cell . cell_contents for cell in func . __closure__ ] <EOL> else : <EOL> cells = None <EOL> return _reduce_code ( func . __code__ ) , globs , func . __name__ , cells <EOL> def _reduce_code ( code ) : <EOL> """<STR_LIT>""" <EOL> return marshal . version , imp . get_magic ( ) , marshal . dumps ( code ) <EOL> def _dummy_closure ( x ) : <EOL> """<STR_LIT>""" <EOL> return lambda : x <EOL> def _rebuild_function ( code_reduced , globals , name , cell_values ) : <EOL> """<STR_LIT>""" <EOL> if cell_values : <EOL> cells = tuple ( _dummy_closure ( v ) . __closure__ [ <NUM_LIT:0> ] for v in cell_values ) <EOL> else : <EOL> cells = ( ) <EOL> code = _rebuild_code ( * code_reduced ) <EOL> modname = globals [ '<STR_LIT>' ] <EOL> try : <EOL> _rebuild_module ( modname ) <EOL> except ImportError : <EOL> del globals [ '<STR_LIT>' ] <EOL> return FunctionType ( code , globals , name , ( ) , cells ) <EOL> def _rebuild_code ( marshal_version , bytecode_magic , marshalled ) : <EOL> """<STR_LIT>""" <EOL> if marshal . version != marshal_version : <EOL> raise RuntimeError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( marshal . version , marshal_version ) ) <EOL> if imp . get_magic ( ) != bytecode_magic : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> return marshal . loads ( marshalled ) </s>
