summaryrefslogtreecommitdiffstats
path: root/utils.py
diff options
context:
space:
mode:
authorJan Pokorný <jpokorny@redhat.com>2014-03-28 19:57:57 +0100
committerJan Pokorný <jpokorny@redhat.com>2014-03-28 20:00:14 +0100
commitbb85ba3568a9bc166becd298ed649cd8c841f60f (patch)
tree75325f36884eaae2f49c383d98bae1f1693ec33e /utils.py
parent5c9999dff1fb385b79dcb2234a9318b7d560baa5 (diff)
downloadclufter-bb85ba3568a9bc166becd298ed649cd8c841f60f.tar.gz
clufter-bb85ba3568a9bc166becd298ed649cd8c841f60f.tar.xz
clufter-bb85ba3568a9bc166becd298ed649cd8c841f60f.zip
utils: fix implicit function argument when making a generator
...which happened with the previous commit re: magic @DITIT+ files Signed-off-by: Jan Pokorný <jpokorny@redhat.com>
Diffstat (limited to 'utils.py')
-rw-r--r--utils.py16
1 files changed, 12 insertions, 4 deletions
diff --git a/utils.py b/utils.py
index d8c6746..000c3fa 100644
--- a/utils.py
+++ b/utils.py
@@ -168,17 +168,25 @@ longopt_letters_reprio = \
# extrapolate optparse.make_option to specifically-encoded "plural"
make_options = lambda opt_decl: [make_option(*a, **kw) for a, kw in opt_decl]
-def func_defaults_varnames(func, skip=0):
+def func_defaults_varnames(func, skip=0, fix_generator_tail=True):
"""Using introspection, get arg defaults (dict) + all arg names (tuple)
Parameters:
- skipfirst how many initial arguments to skip
+ skip how many initial arguments to skip
+ fix_generator_tail see http://stackoverflow.com/questions/9631777
+ https://mail.python.org/pipermail/python-list/
+ 2013-November/661304.html
"""
# XXX assert len(func_varnames) - skip >= len(func.func_defaults)
func_varnames = func.func_code.co_varnames[skip:]
+ fix = None
+ for i in xrange(len(func_varnames) if fix_generator_tail else 0, 0, -1):
+ if func_varnames[i - 1][0] not in "_.":
+ break
+ fix -= 1
func_defaults = dict(zip(
- func_varnames[-len(func.func_defaults):],
- func.func_defaults
+ func_varnames[-len(func.func_defaults) + skip - 1:fix],
+ func.func_defaults[skip:fix]
))
return func_defaults, func_varnames