<s> from io import BytesIO <EOL> import sys <EOL> from msgpack import Unpacker , packb , OutOfData , ExtType <EOL> from pytest import raises , mark <EOL> def test_unpack_array_header_from_file ( ) : <EOL> f = BytesIO ( packb ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] ) ) <EOL> unpacker = Unpacker ( f ) <EOL> assert unpacker . read_array_header ( ) == <NUM_LIT:4> <EOL> assert unpacker . unpack ( ) == <NUM_LIT:1> <EOL> assert unpacker . unpack ( ) == <NUM_LIT:2> <EOL> assert unpacker . unpack ( ) == <NUM_LIT:3> <EOL> assert unpacker . unpack ( ) == <NUM_LIT:4> <EOL> with raises ( OutOfData ) : <EOL> unpacker . unpack ( ) <EOL> @ mark . skipif ( "<STR_LIT>" , <EOL> reason = '<STR_LIT>' ) <EOL> def test_unpacker_hook_refcnt ( ) : <EOL> result = [ ] <EOL> def hook ( x ) : <EOL> result . append ( x ) <EOL> return x <EOL> basecnt = sys . getrefcount ( hook ) <EOL> up = Unpacker ( object_hook = hook , list_hook = hook ) <EOL> assert sys . getrefcount ( hook ) >= basecnt + <NUM_LIT:2> <EOL> up . feed ( packb ( [ { } ] ) ) <EOL> up . feed ( packb ( [ { } ] ) ) <EOL> assert up . unpack ( ) == [ { } ] <EOL> assert up . unpack ( ) == [ { } ] <EOL> assert result == [ { } , [ { } ] , { } , [ { } ] ] <EOL> del up <EOL> assert sys . getrefcount ( hook ) == basecnt <EOL> def test_unpacker_ext_hook ( ) : <EOL> class MyUnpacker ( Unpacker ) : <EOL> def __init__ ( self ) : <EOL> super ( MyUnpacker , self ) . __init__ ( ext_hook = self . _hook , <EOL> encoding = '<STR_LIT:utf-8>' ) <EOL> def _hook ( self , code , data ) : <EOL> if code == <NUM_LIT:1> : <EOL> return int ( data ) <EOL> else : <EOL> return ExtType ( code , data ) <EOL> unpacker = MyUnpacker ( ) <EOL> unpacker . feed ( packb ( { '<STR_LIT:a>' : <NUM_LIT:1> } , encoding = '<STR_LIT:utf-8>' ) ) <EOL> assert unpacker . unpack ( ) == { '<STR_LIT:a>' : <NUM_LIT:1> } <EOL> unpacker . feed ( packb ( { '<STR_LIT:a>' : ExtType ( <NUM_LIT:1> , b'<STR_LIT>' ) } , encoding = '<STR_LIT:utf-8>' ) ) <EOL> assert unpacker . unpack ( ) == { '<STR_LIT:a>' : <NUM_LIT> } <EOL> unpacker . feed ( packb ( { '<STR_LIT:a>' : ExtType ( <NUM_LIT:2> , b'<STR_LIT>' ) } , encoding = '<STR_LIT:utf-8>' ) ) <EOL> assert unpacker . unpack ( ) == { '<STR_LIT:a>' : ExtType ( <NUM_LIT:2> , b'<STR_LIT>' ) } <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> test_unpack_array_header_from_file ( ) <EOL> test_unpacker_hook_refcnt ( ) <EOL> test_unpacker_ext_hook ( ) </s>
