Fixed bug in DefaultFrom where impleied keys were using entire func_code.co_varnames instead of an approprate slice

This commit is contained in:
Jason Gerard DeRose 2008-10-20 16:45:32 -06:00
parent d615e4dafb
commit bb978e591b
2 changed files with 12 additions and 1 deletions

View File

@ -121,7 +121,8 @@ class DefaultFrom(plugable.ReadOnly):
raise TypeError('callback must be callable; got %r' % callback)
self.callback = callback
if len(keys) == 0:
self.keys = callback.func_code.co_varnames
fc = callback.func_code
self.keys = fc.co_varnames[:fc.co_argcount]
else:
self.keys = keys
for key in self.keys:

View File

@ -115,11 +115,21 @@ class test_DefaultFrom(ClassChecker):
kw_copy = dict(kw)
del kw_copy[key]
assert o(**kw_copy) is None
# Test using implied keys:
o = self.cls(lambda first, last: first[0] + last)
assert o(first='john', last='doe') == 'jdoe'
assert o(first='', last='doe') is None
assert o(one='john', two='doe') is None
# Test that co_varnames slice is used:
def callback2(first, last):
letter = first[0]
return letter + last
o = self.cls(callback2)
assert o.keys == ('first', 'last')
assert o(first='john', last='doe') == 'jdoe'
def test_parse_param_spec():
"""