629 | | try: # dictionary lookup |
630 | | current = current[bits[0]] |
631 | | except (TypeError, AttributeError, KeyError): |
632 | | try: # attribute lookup |
633 | | current = getattr(current, bits[0]) |
634 | | if callable(current): |
635 | | if getattr(current, 'alters_data', False): |
636 | | current = settings.TEMPLATE_STRING_IF_INVALID |
637 | | else: |
638 | | try: # method call (assuming no args required) |
639 | | current = current() |
640 | | except TypeError: # arguments *were* required |
641 | | # GOTCHA: This will also catch any TypeError |
642 | | # raised in the function itself. |
643 | | current = settings.TEMPLATE_STRING_IF_INVALID # invalid method call |
644 | | except Exception, e: |
645 | | if getattr(e, 'silent_variable_failure', False): |
646 | | current = settings.TEMPLATE_STRING_IF_INVALID |
647 | | else: |
648 | | raise |
649 | | except (TypeError, AttributeError): |
650 | | try: # list-index lookup |
651 | | current = current[int(bits[0])] |
652 | | except (IndexError, ValueError, KeyError): |
653 | | raise VariableDoesNotExist, "Failed lookup for key [%s] in %r" % (bits[0], current) # missing attribute |
654 | | except Exception, e: |
655 | | if getattr(e, 'silent_variable_failure', False): |
| 630 | bit = bits.pop(0) |
| 631 | if 'has_key' in dir(current) and current.has_key(bit): # dictionary lookup |
| 632 | current = current[bit] |
| 633 | elif bit in dir(current): # attribute lookup |
| 634 | current = getattr(current, bit) |
| 635 | if callable(current): # method call |
| 636 | if getattr(current, 'alters_data', False): |
658 | | raise |
659 | | del bits[0] |
| 639 | try: |
| 640 | current = current() |
| 641 | except TypeError: # arguments *were* required |
| 642 | current = settings.TEMPLATE_STRING_IF_INVALID # invalid method call |
| 643 | except Exception, e: |
| 644 | if getattr(e, 'silent_variable_failure', False): |
| 645 | current = settings.TEMPLATE_STRING_IF_INVALID |
| 646 | else: |
| 647 | raise |
| 648 | elif type(current) == list or type(current) == tuple and bit.isdigit() and int(bit) < len(current): |
| 649 | # list lookup |
| 650 | current = current[int(bit)] |
| 651 | else: |
| 652 | if ObjectDoesNotExist.silent_variable_failure: |
| 653 | current = settings.TEMPLATE_STRING_IF_INVALID |
| 654 | else: |
| 655 | raise VariableDoesNotExist, "Failed lookup for key [%s] in %r" % (bit, current) # missing attribute |