<s> from opcode import * <EOL> import sys <EOL> CALL_FUNCTION = opmap [ '<STR_LIT>' ] <EOL> UNPACK_SEQUENCE = opmap [ '<STR_LIT>' ] <EOL> STORE_FAST = opmap [ '<STR_LIT>' ] <EOL> STORE_NAME = opmap [ '<STR_LIT>' ] <EOL> STORE_GLOBAL = opmap [ '<STR_LIT>' ] <EOL> STORE_ATTR = opmap [ '<STR_LIT>' ] <EOL> STORE_SUBSCR = opmap [ '<STR_LIT>' ] <EOL> STORE_SLICE = opmap [ '<STR_LIT>' ] <EOL> def unpack ( x ) : <EOL> try : <EOL> <NUM_LIT:1> / <NUM_LIT:0> <EOL> except : <EOL> typ , value , traceback = sys . exc_info ( ) <EOL> f = traceback . tb_frame . f_back <EOL> co = f . f_code <EOL> i = f . f_lasti <EOL> code = co . co_code <EOL> if ord ( code [ i ] ) == CALL_FUNCTION and ord ( code [ i + <NUM_LIT:3> ] ) == UNPACK_SEQUENCE : <EOL> i += <NUM_LIT:3> <EOL> n = ord ( code [ i + <NUM_LIT:1> ] ) + ord ( code [ i + <NUM_LIT:2> ] ) * <NUM_LIT> <EOL> i += <NUM_LIT:3> <EOL> names = [ ] <EOL> while len ( names ) < n and i < len ( code ) : <EOL> op = ord ( code [ i ] ) <EOL> i += <NUM_LIT:1> <EOL> if op >= HAVE_ARGUMENT : <EOL> oparg = ord ( code [ i ] ) + ord ( code [ i + <NUM_LIT:1> ] ) * <NUM_LIT> <EOL> i += <NUM_LIT:2> <EOL> if op == STORE_FAST : <EOL> names . append ( co . co_varnames [ oparg ] ) <EOL> elif op in ( STORE_NAME , STORE_ATTR , STORE_GLOBAL ) : <EOL> names . append ( co . co_names [ oparg ] ) <EOL> if op == STORE_SUBSCR or STORE_SLICE <= op <= STORE_SLICE + <NUM_LIT:3> : <EOL> break <EOL> if len ( names ) == n : <EOL> r = [ ] <EOL> for name in names : <EOL> try : <EOL> v = getattr ( x , name ) <EOL> except AttributeError : <EOL> v = x [ name ] <EOL> r . append ( v ) <EOL> return r <EOL> raise SyntaxError <EOL> def test_unpack ( ) : <EOL> class C : <EOL> a = <NUM_LIT:1> <EOL> b = <NUM_LIT:3> <EOL> c = <NUM_LIT:4> <EOL> y = C ( ) <EOL> a , b , c = unpack ( y ) <EOL> x = [ a , b , c ] <EOL> class D : <EOL> pass <EOL> D . a , c , D . b = unpack ( y ) <EOL> x . extend ( [ D . a , c , D . b ] ) <EOL> l = [ None ] <EOL> try : <EOL> l [ <NUM_LIT:0> ] , c , b = unpack ( y ) <EOL> except SyntaxError : <EOL> pass <EOL> else : <EOL> raise RuntimeError <EOL> l = [ None ] <EOL> try : <EOL> l [ <NUM_LIT:1> : <NUM_LIT:2> ] , c , b = unpack ( y ) <EOL> except SyntaxError : <EOL> pass <EOL> else : <EOL> raise RuntimeError <EOL> y = [ ] <EOL> y = { '<STR_LIT:a>' : '<STR_LIT:A>' , '<STR_LIT:b>' : '<STR_LIT:B>' } <EOL> a , b = unpack ( y ) <EOL> x . extend ( [ a , b ] ) <EOL> global g <EOL> y [ '<STR_LIT:g>' ] = '<STR_LIT>' <EOL> g , b = unpack ( y ) <EOL> x . extend ( [ g , b ] ) <EOL> if x != [ <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:1> , <NUM_LIT:4> , <NUM_LIT:3> , '<STR_LIT:A>' , '<STR_LIT:B>' , '<STR_LIT>' , '<STR_LIT:B>' ] : <EOL> raise RuntimeError <EOL> __all__ = '<STR_LIT>' <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> test_unpack ( ) </s>
