Skip to content

EZT should allow dictionary access if attribute access fails on an object #8

@GoogleCodeExporter

Description

@GoogleCodeExporter
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)):

Original issue reported on code.google.com by taki...@google.com on 30 Aug 2010 at 12:42

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions