From 6be3977fa76596b7235310aa4b26c424630ec69a Mon Sep 17 00:00:00 2001 From: mdipierro Date: Fri, 29 Jul 2016 08:50:28 -0500 Subject: [PATCH] fixed issue #1409, py2 vs py3 --- gluon/sqlhtml.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/gluon/sqlhtml.py b/gluon/sqlhtml.py index 701b9872..a0f19d64 100644 --- a/gluon/sqlhtml.py +++ b/gluon/sqlhtml.py @@ -54,17 +54,27 @@ REGEX_ALIAS_MATCH = re.compile('^(.*) AS (.*)$') def add_class(a, b): return a + ' ' + b if a else b +def count_expected_args(f): + if hasattr(f,'func_code'): + # python 2 + n = f.func_code.co_argcount - len(f.func_defaults or []) + if getattr(f, 'im_self', None): + n -= 1 + elif hasattr(f, '__code__'): + # python 3 + n = f.__code__.co_argcount - len(f.__defaults__ or []) + if getattr(f, '__self__', None): + n -= 1 + else: + # doh! + n = 1 + return n def represent(field, value, record): f = field.represent if not callable(f): return str(value) - if hasattr(f,'func_code'): - n = f.__code__.co_argcount - len(f.__defaults__ or []) - if getattr(f, 'im_self', None): - n -= 1 - else: - n = 1 + n = count_expected_args(f) if n == 1: return f(value) elif n == 2: