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: