Commit e542a896 by Chris Jerdonek

Merge 'issue_11' into development: closing issue #11 (callables executing incorrectly)

Added a unit test and doctest.
parents e2305c75 59283fcb
......@@ -125,6 +125,34 @@ class Context(object):
(3) If there is no attribute with the same name as the key, then
the key is considered not found in the item.
*Caution*:
Callables resulting from a call to __getitem__ (as in (1)
above) are handled differently from callables that are merely
attributes (as in (2) above).
The former are returned as-is, while the latter are first called
and that value returned. Here is an example:
>>> def greet():
... return "Hi Bob!"
>>>
>>> class Greeter(object):
... greet = None
>>>
>>> obj = Greeter()
>>> obj.greet = greet
>>> dct = {'greet': greet}
>>>
>>> obj.greet is dct['greet']
True
>>> Context(obj).get('greet')
'Hi Bob!'
>>> Context(dct).get('greet') #doctest: +ELLIPSIS
<function greet at 0x...>
TODO: explain the rationale for this difference in treatment.
"""
for obj in reversed(self._stack):
val = _get_item(obj, key)
......
......@@ -69,6 +69,18 @@ class GetItemTestCase(TestCase):
obj = {"foo": "bar"}
self.assertEquals(_get_item(obj, "foo"), "bar")
def test_dictionary__callable_not_called(self):
"""
Test that callable values are returned as-is (and in particular not called).
"""
def foo_callable(self):
return "bar"
obj = {"foo": foo_callable}
self.assertNotEquals(_get_item(obj, "foo"), "bar")
self.assertTrue(_get_item(obj, "foo") is foo_callable)
def test_dictionary__key_missing(self):
"""
Test getting a missing key from a dictionary.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment