From c074f35fcbbae71cf5e798c9b3bae2c7380ab310 Mon Sep 17 00:00:00 2001 From: SpahrK1 Date: Mon, 1 Jul 2019 12:47:27 -0400 Subject: [PATCH] Add Python3 support --- .../recipe-284742.py | 52 ++++++++++--------- 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/recipes/Python/284742_Finding_out_number_values_caller/recipe-284742.py b/recipes/Python/284742_Finding_out_number_values_caller/recipe-284742.py index 8363a1346..e35aa4563 100644 --- a/recipes/Python/284742_Finding_out_number_values_caller/recipe-284742.py +++ b/recipes/Python/284742_Finding_out_number_values_caller/recipe-284742.py @@ -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()