From 19b866c254b54d904a942f34d662dfacd9005a77 Mon Sep 17 00:00:00 2001 From: Nick Pope Date: Tue, 20 Jul 2021 17:23:59 +0100 Subject: [PATCH] Refs #32948 -- Added Node.__copy__(). This allows the copy.copy() usage in the Q._combine() method to finish sooner, instead of having to fallback to using the __reduce_ex__(4) method. Thia also avoids having to fall into copy.copy() at in Q._combine(), when combining a Q() with another Q(). Co-authored-by: Keryn Knight --- django/db/models/sql/where.py | 3 --- django/utils/tree.py | 7 +++++++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/django/db/models/sql/where.py b/django/db/models/sql/where.py index 4b758e7adc..42a4b054a5 100644 --- a/django/db/models/sql/where.py +++ b/django/db/models/sql/where.py @@ -183,9 +183,6 @@ class WhereNode(tree.Node): clone.relabel_aliases(change_map) return clone - def copy(self): - return self.clone() - @classmethod def _contains_aggregate(cls, obj): if isinstance(obj, tree.Node): diff --git a/django/utils/tree.py b/django/utils/tree.py index 7ee5b01444..d897cbeacd 100644 --- a/django/utils/tree.py +++ b/django/utils/tree.py @@ -44,6 +44,13 @@ class Node: def __repr__(self): return "<%s: %s>" % (self.__class__.__name__, self) + def __copy__(self): + obj = self.create(connector=self.connector, negated=self.negated) + obj.children = self.children # Don't [:] as .__init__() via .create() does. + return obj + + copy = __copy__ + def __deepcopy__(self, memodict): obj = self.create(connector=self.connector, negated=self.negated) obj.children = copy.deepcopy(self.children, memodict)