<s> """<STR_LIT>""" <EOL> import copy <EOL> import sys <EOL> from types import FunctionType <EOL> from ipython_genutils import py3compat <EOL> from ipython_genutils . importstring import import_item <EOL> from ipython_genutils . py3compat import string_types , iteritems , buffer_to_bytes , buffer_to_bytes_py2 <EOL> from . import codeutil <EOL> from traitlets . log import get_logger <EOL> if py3compat . PY3 : <EOL> buffer = memoryview <EOL> class_type = type <EOL> else : <EOL> from types import ClassType <EOL> class_type = ( type , ClassType ) <EOL> def _get_cell_type ( a = None ) : <EOL> """<STR_LIT>""" <EOL> def inner ( ) : <EOL> return a <EOL> return type ( py3compat . get_closure ( inner ) [ <NUM_LIT:0> ] ) <EOL> cell_type = _get_cell_type ( ) <EOL> def interactive ( f ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( f , FunctionType ) : <EOL> mainmod = __import__ ( '<STR_LIT:__main__>' ) <EOL> f = FunctionType ( f . __code__ , mainmod . __dict__ , <EOL> f . __name__ , f . __defaults__ , <EOL> ) <EOL> f . __module__ = '<STR_LIT:__main__>' <EOL> return f <EOL> def use_dill ( ) : <EOL> """<STR_LIT>""" <EOL> import dill <EOL> from . import serialize <EOL> serialize . pickle = dill <EOL> can_map . pop ( FunctionType , None ) <EOL> def use_cloudpickle ( ) : <EOL> """<STR_LIT>""" <EOL> import cloudpickle <EOL> from . import serialize <EOL> serialize . pickle = cloudpickle <EOL> can_map . pop ( FunctionType , None ) <EOL> def use_pickle ( ) : <EOL> """<STR_LIT>""" <EOL> from . import serialize <EOL> serialize . pickle = serialize . _stdlib_pickle <EOL> can_map [ FunctionType ] = _original_can_map [ FunctionType ] <EOL> class CannedObject ( object ) : <EOL> def __init__ ( self , obj , keys = [ ] , hook = None ) : <EOL> """<STR_LIT>""" <EOL> self . keys = keys <EOL> self . obj = copy . copy ( obj ) <EOL> self . hook = can ( hook ) <EOL> for key in keys : <EOL> setattr ( self . obj , key , can ( getattr ( obj , key ) ) ) <EOL> self . buffers = [ ] <EOL> def get_object ( self , g = None ) : <EOL> if g is None : <EOL> g = { } <EOL> obj = self . obj <EOL> for key in self . keys : <EOL> setattr ( obj , key , uncan ( getattr ( obj , key ) , g ) ) <EOL> if self . hook : <EOL> self . hook = uncan ( self . hook , g ) <EOL> self . hook ( obj , g ) <EOL> return self . obj <EOL> class Reference ( CannedObject ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name ) : <EOL> if not isinstance ( name , string_types ) : <EOL> raise TypeError ( "<STR_LIT>" % name ) <EOL> self . name = name <EOL> self . buffers = [ ] <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % self . name <EOL> def get_object ( self , g = None ) : <EOL> if g is None : <EOL> g = { } <EOL> return eval ( self . name , g ) <EOL> class CannedCell ( CannedObject ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , cell ) : <EOL> self . cell_contents = can ( cell . cell_contents ) <EOL> def get_object ( self , g = None ) : <EOL> cell_contents = uncan ( self . cell_contents , g ) <EOL> def inner ( ) : <EOL> return cell_contents <EOL> return py3compat . get_closure ( inner ) [ <NUM_LIT:0> ] <EOL> class CannedFunction ( CannedObject ) : <EOL> def __init__ ( self , f ) : <EOL> self . _check_type ( f ) <EOL> self . code = f . __code__ <EOL> if f . __defaults__ : <EOL> self . defaults = [ can ( fd ) for fd in f . __defaults__ ] <EOL> else : <EOL> self . defaults = None <EOL> closure = py3compat . get_closure ( f ) <EOL> if closure : <EOL> self . closure = tuple ( can ( cell ) for cell in closure ) <EOL> else : <EOL> self . closure = None <EOL> self . module = f . __module__ or '<STR_LIT:__main__>' <EOL> self . __name__ = f . __name__ <EOL> self . buffers = [ ] <EOL> def _check_type ( self , obj ) : <EOL> assert isinstance ( obj , FunctionType ) , "<STR_LIT>" <EOL> def get_object ( self , g = None ) : <EOL> if not self . module . startswith ( '<STR_LIT>' ) : <EOL> __import__ ( self . module ) <EOL> g = sys . modules [ self . module ] . __dict__ <EOL> if g is None : <EOL> g = { } <EOL> if self . defaults : <EOL> defaults = tuple ( uncan ( cfd , g ) for cfd in self . defaults ) <EOL> else : <EOL> defaults = None <EOL> if self . closure : <EOL> closure = tuple ( uncan ( cell , g ) for cell in self . closure ) <EOL> else : <EOL> closure = None <EOL> newFunc = FunctionType ( self . code , g , self . __name__ , defaults , closure ) <EOL> return newFunc <EOL> class CannedClass ( CannedObject ) : <EOL> def __init__ ( self , cls ) : <EOL> self . _check_type ( cls ) <EOL> self . name = cls . __name__ <EOL> self . old_style = not isinstance ( cls , type ) <EOL> self . _canned_dict = { } <EOL> for k , v in cls . __dict__ . items ( ) : <EOL> if k not in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> self . _canned_dict [ k ] = can ( v ) <EOL> if self . old_style : <EOL> mro = [ ] <EOL> else : <EOL> mro = cls . mro ( ) <EOL> self . parents = [ can ( c ) for c in mro [ <NUM_LIT:1> : ] ] <EOL> self . buffers = [ ] <EOL> def _check_type ( self , obj ) : <EOL> assert isinstance ( obj , class_type ) , "<STR_LIT>" <EOL> def get_object ( self , g = None ) : <EOL> parents = tuple ( uncan ( p , g ) for p in self . parents ) <EOL> return type ( self . name , parents , uncan_dict ( self . _canned_dict , g = g ) ) <EOL> class CannedArray ( CannedObject ) : <EOL> def __init__ ( self , obj ) : <EOL> from numpy import ascontiguousarray <EOL> self . shape = obj . shape <EOL> self . dtype = obj . dtype . descr if obj . dtype . fields else obj . dtype . str <EOL> self . pickled = False <EOL> if sum ( obj . shape ) == <NUM_LIT:0> : <EOL> self . pickled = True <EOL> elif obj . dtype == '<STR_LIT:O>' : <EOL> self . pickled = True <EOL> elif obj . dtype . fields and any ( dt == '<STR_LIT:O>' for dt , sz in obj . dtype . fields . values ( ) ) : <EOL> self . pickled = True <EOL> if self . pickled : <EOL> from . import serialize <EOL> self . buffers = [ serialize . pickle . dumps ( obj , serialize . PICKLE_PROTOCOL ) ] <EOL> else : <EOL> obj = ascontiguousarray ( obj , dtype = None ) <EOL> self . buffers = [ buffer ( obj ) ] <EOL> def get_object ( self , g = None ) : <EOL> from numpy import frombuffer <EOL> data = self . buffers [ <NUM_LIT:0> ] <EOL> if self . pickled : <EOL> from . import serialize <EOL> return serialize . pickle . loads ( buffer_to_bytes_py2 ( data ) ) <EOL> else : <EOL> if not py3compat . PY3 and isinstance ( data , memoryview ) : <EOL> data = buffer ( data . tobytes ( ) ) <EOL> return frombuffer ( data , dtype = self . dtype ) . reshape ( self . shape ) <EOL> class CannedBytes ( CannedObject ) : <EOL> wrap = staticmethod ( buffer_to_bytes ) <EOL> def __init__ ( self , obj ) : <EOL> self . buffers = [ obj ] <EOL> def get_object ( self , g = None ) : <EOL> data = self . buffers [ <NUM_LIT:0> ] <EOL> return self . wrap ( data ) <EOL> class CannedBuffer ( CannedBytes ) : <EOL> wrap = buffer <EOL> class CannedMemoryView ( CannedBytes ) : <EOL> wrap = memoryview <EOL> def _import_mapping ( mapping , original = None ) : <EOL> """<STR_LIT>""" <EOL> log = get_logger ( ) <EOL> log . debug ( "<STR_LIT>" ) <EOL> for key , value in list ( mapping . items ( ) ) : <EOL> if isinstance ( key , string_types ) : <EOL> try : <EOL> cls = import_item ( key ) <EOL> except Exception : <EOL> if original and key not in original : <EOL> log . error ( "<STR_LIT>" , key , exc_info = True ) <EOL> mapping . pop ( key ) <EOL> else : <EOL> mapping [ cls ] = mapping . pop ( key ) <EOL> def istype ( obj , check ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( check , tuple ) : <EOL> for cls in check : <EOL> if type ( obj ) is cls : <EOL> return True <EOL> return False <EOL> else : <EOL> return type ( obj ) is check <EOL> def can ( obj ) : <EOL> """<STR_LIT>""" <EOL> import_needed = False <EOL> for cls , canner in iteritems ( can_map ) : <EOL> if isinstance ( cls , string_types ) : <EOL> import_needed = True <EOL> break <EOL> elif istype ( obj , cls ) : <EOL> return canner ( obj ) <EOL> if import_needed : <EOL> _import_mapping ( can_map , _original_can_map ) <EOL> return can ( obj ) <EOL> return obj <EOL> def can_class ( obj ) : <EOL> if isinstance ( obj , class_type ) and obj . __module__ == '<STR_LIT:__main__>' : <EOL> return CannedClass ( obj ) <EOL> else : <EOL> return obj <EOL> def can_dict ( obj ) : <EOL> """<STR_LIT>""" <EOL> if istype ( obj , dict ) : <EOL> newobj = { } <EOL> for k , v in iteritems ( obj ) : <EOL> newobj [ k ] = can ( v ) <EOL> return newobj <EOL> else : <EOL> return obj <EOL> sequence_types = ( list , tuple , set ) <EOL> def can_sequence ( obj ) : <EOL> """<STR_LIT>""" <EOL> if istype ( obj , sequence_types ) : <EOL> t = type ( obj ) <EOL> return t ( [ can ( i ) for i in obj ] ) <EOL> else : <EOL> return obj <EOL> def uncan ( obj , g = None ) : <EOL> """<STR_LIT>""" <EOL> import_needed = False <EOL> for cls , uncanner in iteritems ( uncan_map ) : <EOL> if isinstance ( cls , string_types ) : <EOL> import_needed = True <EOL> break <EOL> elif isinstance ( obj , cls ) : <EOL> return uncanner ( obj , g ) <EOL> if import_needed : <EOL> _import_mapping ( uncan_map , _original_uncan_map ) <EOL> return uncan ( obj , g ) <EOL> return obj <EOL> def uncan_dict ( obj , g = None ) : <EOL> if istype ( obj , dict ) : <EOL> newobj = { } <EOL> for k , v in iteritems ( obj ) : <EOL> newobj [ k ] = uncan ( v , g ) <EOL> return newobj <EOL> else : <EOL> return obj <EOL> def uncan_sequence ( obj , g = None ) : <EOL> if istype ( obj , sequence_types ) : <EOL> t = type ( obj ) <EOL> return t ( [ uncan ( i , g ) for i in obj ] ) <EOL> else : <EOL> return obj <EOL> def _uncan_dependent_hook ( dep , g = None ) : <EOL> dep . check_dependency ( ) <EOL> def can_dependent ( obj ) : <EOL> return CannedObject ( obj , keys = ( '<STR_LIT:f>' , '<STR_LIT>' ) , hook = _uncan_dependent_hook ) <EOL> can_map = { <EOL> '<STR_LIT>' : CannedArray , <EOL> FunctionType : CannedFunction , <EOL> bytes : CannedBytes , <EOL> memoryview : CannedMemoryView , <EOL> cell_type : CannedCell , <EOL> class_type : can_class , <EOL> '<STR_LIT>' : can_dependent , <EOL> } <EOL> if buffer is not memoryview : <EOL> can_map [ buffer ] = CannedBuffer <EOL> uncan_map = { <EOL> CannedObject : lambda obj , g : obj . get_object ( g ) , <EOL> dict : uncan_dict , <EOL> } <EOL> _original_can_map = can_map . copy ( ) <EOL> _original_uncan_map = uncan_map . copy ( ) </s>
