Issue 8 in ezt: EZT should allow dictionary access if attribute access fails on an object

8 views
Skip to first unread message

e...@googlecode.com

unread,
Aug 29, 2010, 8:43:07 PM8/29/10
to ezt-d...@googlegroups.com
Status: New
Owner: ----
Labels: Type-Defect Priority-Medium

New issue 8 by taki...@google.com: EZT should allow dictionary access if
attribute access fails on an object
http://code.google.com/p/ezt/issues/detail?id=8

Django supports accessing dictionary entries when attribute accesses fail,
which is a really handy feature in my experience. It'd be nice if EZT
supported this as well.

What steps will reproduce the problem?

1. Reference an entry in a dictionary in your template somewhere,
e.g. <h1>[mydict.one]</h1>
2. Pass in a dictionary for the corresponding data variable to generate,
e.g. generate(fd, {'mydict': {'one': 1, 'two': 2}})

What is the expected output? What do you see instead?

In Django, the attribute access would fail, but the system would check that
the object is a dictionary, and try a dictionary access, which would then
return 1. In EZT, this simply fails with an UnknownReference exception

What version of the product are you using? On what operating system?

I'm not entirely sure what version I'm using, but the _get_value function
looks no different when compared with the trunk version in this repository.

Please provide any additional information below.

Something like this seems to work for me:

@@ -672,7 +672,11 @@ def _get_value((refname, start, rest), ctx, filename,
line_number):
try:
ob = getattr(ob, attr)
except AttributeError:
- raise UnknownReference(refname, filename, line_number)
+ if (hasattr(ob, 'has_key') and hasattr(ob, '__getitem__')
+ and ob.has_key(attr)):
+ ob = ob[attr]
+ else:
+ raise UnknownReference(refname, filename, line_number)

# make sure we return a string instead of some various Python types
if isinstance(ob, (IntType, FloatType, LongType)):


Reply all
Reply to author
Forward
0 new messages