<s> """<STR_LIT>""" <EOL> import __builtin__ <EOL> import sys <EOL> import cPickle <EOL> import pickle <EOL> import unittest <EOL> from StringIO import StringIO <EOL> from test import test_support <EOL> class MyClass ( object ) : <EOL> pass <EOL> class CPickleTestCase ( unittest . TestCase ) : <EOL> def test_zero_long ( self ) : <EOL> self . assertEqual ( cPickle . loads ( cPickle . dumps ( <NUM_LIT:0> L , <NUM_LIT:2> ) ) , <NUM_LIT:0> L ) <EOL> self . assertEqual ( cPickle . dumps ( <NUM_LIT:0> L , <NUM_LIT:2> ) , pickle . dumps ( <NUM_LIT:0> L , <NUM_LIT:2> ) ) <EOL> def test_cyclic_memoize ( self ) : <EOL> m = MyClass ( ) <EOL> m2 = MyClass ( ) <EOL> s = set ( [ m ] ) <EOL> m . foo = set ( [ m2 ] ) <EOL> m2 . foo = s <EOL> s2 = cPickle . loads ( cPickle . dumps ( s ) ) <EOL> self . assertEqual ( len ( s2 ) , <NUM_LIT:1> ) <EOL> m3 = iter ( s2 ) . next ( ) <EOL> self . assertEqual ( len ( m3 . foo ) , <NUM_LIT:1> ) <EOL> m4 = iter ( m3 . foo ) . next ( ) <EOL> self . assertEqual ( m4 . foo , s2 ) <EOL> def test_find_global ( self ) : <EOL> class A ( object ) : <EOL> def __init__ ( self , x , y ) : <EOL> self . x = x <EOL> self . y = y <EOL> def __eq__ ( self , other ) : <EOL> if isinstance ( other , A ) and self . x == other . x and self . y == other . y : <EOL> return True <EOL> return False <EOL> class B ( object ) : <EOL> def __init__ ( self , x , y ) : <EOL> self . x = x <EOL> self . y = y <EOL> def restrictive_find_global ( module , clsname ) : <EOL> if clsname == '<STR_LIT:A>' : <EOL> return A <EOL> else : <EOL> raise pickle . UnpicklingError ( "<STR_LIT>" , module , clsname ) <EOL> a = A ( "<STR_LIT>" , "<STR_LIT:C>" ) <EOL> a_pickled = cPickle . dumps ( a , <NUM_LIT:2> ) <EOL> a_unpickler = cPickle . Unpickler ( StringIO ( a_pickled ) ) <EOL> a_unpickler . find_global = restrictive_find_global <EOL> self . assertEqual ( a_unpickler . load ( ) , a ) <EOL> b_pickled = cPickle . dumps ( B ( "<STR_LIT>" , "<STR_LIT>" ) , <NUM_LIT:2> ) <EOL> b_unpickler = cPickle . Unpickler ( StringIO ( b_pickled ) ) <EOL> b_unpickler . find_global = restrictive_find_global <EOL> self . assertRaises ( pickle . UnpicklingError , b_unpickler . load ) <EOL> def testWithUserDefinedImport ( self ) : <EOL> """<STR_LIT>""" <EOL> original_import = __builtin__ . __import__ <EOL> def import_hook ( name , _globals = None , locals = None , fromlist = None , level = - <NUM_LIT:1> ) : <EOL> return original_import ( name , _globals , locals , fromlist , level ) <EOL> __builtin__ . __import__ = import_hook <EOL> try : <EOL> if "<STR_LIT>" in sys . modules : <EOL> del sys . modules [ "<STR_LIT>" ] <EOL> self . assertRaises ( ImportError , cPickle . loads , pickle . GLOBAL + "<STR_LIT>" + "<STR_LIT>" ) <EOL> finally : <EOL> __builtin__ . __import__ = original_import <EOL> def test_main ( ) : <EOL> test_support . run_unittest ( CPickleTestCase ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> test_main ( ) </s>
