summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArmin Ronacher <armin.ronacher@active-4.com>2013-08-07 12:48:37 +0100
committerArmin Ronacher <armin.ronacher@active-4.com>2013-08-07 12:48:37 +0100
commit563fd2e74af39a75bbc4eee451d4570d20e0beec (patch)
treed92fa3b2caf0fb9bb6cbe088b8f209712cc21725
parentad09fe32dd02b6334d3b818c11b4a142866e7a87 (diff)
downloadjinja2-563fd2e74af39a75bbc4eee451d4570d20e0beec.tar.gz
Fixed warnings if Python is run with -3.
This also adds proper hashing and comparision support to undefined objects. This fixes #224
-rw-r--r--CHANGES3
-rw-r--r--jinja2/nodes.py3
-rw-r--r--jinja2/runtime.py12
3 files changed, 17 insertions, 1 deletions
diff --git a/CHANGES b/CHANGES
index 1c7caf4..01afc4e 100644
--- a/CHANGES
+++ b/CHANGES
@@ -10,6 +10,9 @@ Version 2.7.1
- Fixed lack of Python 3 support for bytecode caches.
- Reverted support for defining blocks in included templates as this
broke existing templates for users.
+- Fixed some warnings with hashing of undefineds and nodes if Python
+ is run with warnings for Python 3.
+- Added support for properly hashing undefined objects.
Version 2.7
-----------
diff --git a/jinja2/nodes.py b/jinja2/nodes.py
index 81fafb8..c5697e6 100644
--- a/jinja2/nodes.py
+++ b/jinja2/nodes.py
@@ -232,6 +232,9 @@ class Node(with_metaclass(NodeType, object)):
def __ne__(self, other):
return not self.__eq__(other)
+ # Restore Python 2 hashing behavior on Python 3
+ __hash__ = object.__hash__
+
def __repr__(self):
return '%s(%s)' % (
self.__class__.__name__,
diff --git a/jinja2/runtime.py b/jinja2/runtime.py
index d27ca53..7791c64 100644
--- a/jinja2/runtime.py
+++ b/jinja2/runtime.py
@@ -497,6 +497,15 @@ class Undefined(object):
__float__ = __complex__ = __pow__ = __rpow__ = \
_fail_with_undefined_error
+ def __eq__(self, other):
+ return type(self) is type(other)
+
+ def __ne__(self, other):
+ return not self.__eq__(other)
+
+ def __hash__(self):
+ return id(type(self))
+
def __str__(self):
return u''
@@ -563,7 +572,8 @@ class StrictUndefined(Undefined):
"""
__slots__ = ()
__iter__ = __str__ = __len__ = __nonzero__ = __eq__ = \
- __ne__ = __bool__ = Undefined._fail_with_undefined_error
+ __ne__ = __bool__ = __hash__ = \
+ Undefined._fail_with_undefined_error
# remove remaining slots attributes, after the metaclass did the magic they