Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,37 +1,41 @@
import inspect,dis
import sys
import dis

def expecting():
"""Return how many values the caller is expecting"""
f = inspect.currentframe()
f = sys._getframe(-1)
f = f.f_back.f_back
c = f.f_code
i = f.f_lasti
bytecode = c.co_code
instruction = ord(bytecode[i+3])
i = f.f_lasti + 2
if sys.version_info.major<3:
i += 1
bytecode = c.co_code.decode("latin-1")
instruction = ord(bytecode[i])
if instruction == dis.opmap['UNPACK_SEQUENCE']:
howmany = ord(bytecode[i+4])
howmany = ord(bytecode[i+1])
return howmany
elif instruction == dis.opmap['POP_TOP']:
return 0
return 1

def cleverfunc():
howmany = expecting()
if howmany == 0:
print "return value discarded"
if howmany == 2:
return 1,2
elif howmany == 3:
return 1,2,3
return 1
if __name__ == '__main__':
def cleverfunc():
howmany = expecting()
if howmany == 0:
print "return value discarded"
if howmany == 2:
return 1,2
elif howmany == 3:
return 1,2,3
return 1

def test():
cleverfunc()
x = cleverfunc()
print x
x,y = cleverfunc()
print x,y
x,y,z = cleverfunc()
print x,y,z
def test():
cleverfunc()
x = cleverfunc()
print(x)
x,y = cleverfunc()
print(x,y)
x,y,z = cleverfunc()
print(x,y,z)

test()
test()