From 563fd2e74af39a75bbc4eee451d4570d20e0beec Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Wed, 7 Aug 2013 12:48:37 +0100 Subject: Fixed warnings if Python is run with -3. This also adds proper hashing and comparision support to undefined objects. This fixes #224 --- CHANGES | 3 +++ jinja2/nodes.py | 3 +++ jinja2/runtime.py | 12 +++++++++++- 3 files changed, 17 insertions(+), 1 deletion(-) 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 -- cgit v1.2.1