]> git.ipfire.org Git - thirdparty/jinja.git/commitdiff
Use object.__hash__ for Node.__hash__ 1522/head
authorKristian Klette <klette@otovo.com>
Wed, 27 Oct 2021 18:35:20 +0000 (20:35 +0200)
committerDavid Lord <davidism@gmail.com>
Tue, 9 Nov 2021 17:12:22 +0000 (09:12 -0800)
This fixes a regression in commit 60293416db69782fd048a7820667afa4ae7c423b that
changed the `__hash__` implementation of Node from the default pointer
hash, to a hash based on the node fields.

Since these fields contains list objects, they are not hashable, making
every call to `Node.__hash__` fail.

This breaks some third-party usage such as in `django-compressor`
(See: https://github.com/django-compressor/django-compressor/issues/1060)

This changed reverts the hash method back to using `object.__hash__` as
the hash implementation.

CHANGES.rst
src/jinja2/nodes.py
tests/test_nodes.py [new file with mode: 0644]

index ebd23b6a93ad615ea35ac9f319da8f8403e0586a..191d9008de0759f233513c08632e1423bc1fee5d 100644 (file)
@@ -11,6 +11,8 @@ Unreleased
     when parsing values on Python 3.10. :pr:`1537`
 -   Improve async performance by avoiding checks for common types.
     :issue:`1514`
+-   Revert change to ``hash(Node)`` behavior. Nodes are hashed by id
+    again :issue:`1521`
 
 
 Version 3.0.2
index 226e729c003fc6d259f3dc2f38cf67dced63ed8e..8feb2698d037816d716afef0737c007af4e69d1c 100644 (file)
@@ -241,8 +241,7 @@ class Node(metaclass=NodeType):
 
         return tuple(self.iter_fields()) == tuple(other.iter_fields())
 
-    def __hash__(self) -> int:
-        return hash(tuple(self.iter_fields()))
+    __hash__ = object.__hash__
 
     def __repr__(self) -> str:
         args_str = ", ".join(f"{a}={getattr(self, a, None)!r}" for a in self.fields)
diff --git a/tests/test_nodes.py b/tests/test_nodes.py
new file mode 100644 (file)
index 0000000..e910998
--- /dev/null
@@ -0,0 +1,3 @@
+def test_template_hash(env):
+    template = env.parse("hash test")
+    hash(template)