Ticket #17778: #17778-RequestContext_pollutes_namespace.patch

File #17778-RequestContext_pollutes_namespace.patch, 2.2 KB (added by regebro, 12 years ago)

One solution

  • django/template/base.py

    diff --git a/django/template/base.py b/django/template/base.py
    index f43194d..7cdee56 100644
    a b from functools import partial  
    55from inspect import getargspec
    66
    77from django.conf import settings
    8 from django.template.context import (Context, RequestContext,
     8from django.template.context import (BaseContext, Context, RequestContext,
    99    ContextPopException)
    1010from django.utils.importlib import import_module
    1111from django.utils.itercompat import is_iterable
    class Variable(object):  
    765765                    current = current[bit]
    766766                except (TypeError, AttributeError, KeyError, ValueError):
    767767                    try:  # attribute lookup
     768                        # Don't return class attributes  if the class is the context:
     769                        if (isinstance(current, BaseContext) and
     770                            getattr(type(current), bit)):
     771                            raise AttributeError
    768772                        current = getattr(current, bit)
    769773                    except (TypeError, AttributeError):
    770774                        try:  # list-index lookup
  • tests/regressiontests/templates/context.py

    diff --git a/tests/regressiontests/templates/context.py b/tests/regressiontests/templates/context.py
    index 05c1dd5..25692fe 100644
    a b  
    11# coding: utf-8
    2 from django.template import Context
     2from django.template import Context, Variable, VariableDoesNotExist
    33from django.utils.unittest import TestCase
    44
    55
    66class ContextTests(TestCase):
     7
    78    def test_context(self):
    89        c = Context({"a": 1, "b": "xyzzy"})
    910        self.assertEqual(c["a"], 1)
    class ContextTests(TestCase):  
    1415        self.assertEqual(c.pop(), {"a": 2})
    1516        self.assertEqual(c["a"], 1)
    1617        self.assertEqual(c.get("foo", 42), 42)
     18
     19    def test_resolve_on_context_method(self):
     20        # Regression test for #17778
     21        empty_context = Context()
     22        self.assertRaises(VariableDoesNotExist,
     23                Variable('no_such_variable').resolve, empty_context)
     24        self.assertRaises(VariableDoesNotExist,
     25                Variable('new').resolve, empty_context)
     26
Back to Top