summaryrefslogtreecommitdiff
path: root/django/utils
diff options
context:
space:
mode:
authorMatthias Kestenholz <mk@feinheit.ch>2024-08-09 17:18:42 +0200
committernessita <124304+nessita@users.noreply.github.com>2024-08-12 14:25:05 -0300
commitd84200e4eb4d20116080130c612ba157a4718977 (patch)
tree34f59ee7d1860b2ccbd83ef913f0ca2fe449f076 /django/utils
parentb5c048f5ecbf1c718102f19c3018b1fb62d66b3d (diff)
Fixed #35648 -- Raised NotImplementedError in SafeString.__add__ for non-string RHS.
This change ensures SafeString addition operations handle non-string RHS properly, allowing them to implement __radd__ for better compatibility.
Diffstat (limited to 'django/utils')
-rw-r--r--django/utils/safestring.py14
1 files changed, 10 insertions, 4 deletions
diff --git a/django/utils/safestring.py b/django/utils/safestring.py
index 4eb0207a66..1ac9b877e9 100644
--- a/django/utils/safestring.py
+++ b/django/utils/safestring.py
@@ -35,10 +35,16 @@ class SafeString(str, SafeData):
Concatenating a safe string with another safe bytestring or
safe string is safe. Otherwise, the result is no longer safe.
"""
- t = super().__add__(rhs)
- if isinstance(rhs, SafeData):
- return SafeString(t)
- return t
+ if isinstance(rhs, str):
+ t = super().__add__(rhs)
+ if isinstance(rhs, SafeData):
+ t = SafeString(t)
+ return t
+
+ # Give the rhs object a chance to handle the addition, for example if
+ # the rhs object's class implements `__radd__`. More details:
+ # https://docs.python.org/3/reference/datamodel.html#object.__radd__
+ return NotImplemented
def __str__(self):
return self