mirror of
				https://salsa.debian.org/freeipa-team/freeipa.git
				synced 2025-02-25 18:55:28 -06:00 
			
		
		
		
	348: If no keys are passed to DefaultFrom.__init__(), the keys from callback.func_code.co_varnames are used; updated DefaultFrom unit tests to test this usage
This commit is contained in:
		@@ -61,12 +61,16 @@ class DefaultFrom(plugable.ReadOnly):
 | 
			
		||||
        :param callback: The callable to call when all ``keys`` are present.
 | 
			
		||||
        :param keys: The keys used to map from keyword to position arguments.
 | 
			
		||||
        """
 | 
			
		||||
        assert callable(callback), 'not a callable: %r' % callback
 | 
			
		||||
        assert len(keys) > 0, 'must have at least one key'
 | 
			
		||||
        for key in keys:
 | 
			
		||||
            assert type(key) is str, 'not an str: %r' % key
 | 
			
		||||
        if not callable(callback):
 | 
			
		||||
            raise TypeError('callback must be callable; got %r' % callback)
 | 
			
		||||
        self.callback = callback
 | 
			
		||||
        self.keys = keys
 | 
			
		||||
        if len(keys) == 0:
 | 
			
		||||
            self.keys = callback.func_code.co_varnames
 | 
			
		||||
        else:
 | 
			
		||||
            self.keys = keys
 | 
			
		||||
        for key in self.keys:
 | 
			
		||||
            if type(key) is not str:
 | 
			
		||||
                raise_TypeError(key, str, 'keys')
 | 
			
		||||
        lock(self)
 | 
			
		||||
 | 
			
		||||
    def __call__(self, **kw):
 | 
			
		||||
@@ -77,11 +81,11 @@ class DefaultFrom(plugable.ReadOnly):
 | 
			
		||||
        """
 | 
			
		||||
        vals = tuple(kw.get(k, None) for k in self.keys)
 | 
			
		||||
        if None in vals:
 | 
			
		||||
            return None
 | 
			
		||||
            return
 | 
			
		||||
        try:
 | 
			
		||||
            return self.callback(*vals)
 | 
			
		||||
        except Exception:
 | 
			
		||||
            return None
 | 
			
		||||
        except StandardError:
 | 
			
		||||
            pass
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def parse_param_spec(spec):
 | 
			
		||||
 
 | 
			
		||||
@@ -89,6 +89,9 @@ class test_DefaultFrom(ClassChecker):
 | 
			
		||||
        o = self.cls(callback, *keys)
 | 
			
		||||
        assert read_only(o, 'callback') is callback
 | 
			
		||||
        assert read_only(o, 'keys') == keys
 | 
			
		||||
        lam = lambda first, last: first[0] + last
 | 
			
		||||
        o = self.cls(lam)
 | 
			
		||||
        assert read_only(o, 'keys') == ('first', 'last')
 | 
			
		||||
 | 
			
		||||
    def test_call(self):
 | 
			
		||||
        """
 | 
			
		||||
@@ -109,6 +112,10 @@ class test_DefaultFrom(ClassChecker):
 | 
			
		||||
            kw_copy = dict(kw)
 | 
			
		||||
            del kw_copy[key]
 | 
			
		||||
            assert o(**kw_copy) is None
 | 
			
		||||
        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
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_parse_param_spec():
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user