From 8cfde70d8c2b96e80c2de4a7646c26998bd6ca49 Mon Sep 17 00:00:00 2001 From: ale-rt Date: Tue, 30 Apr 2024 09:22:09 +0200 Subject: [PATCH] Avoid casting the body to a string when it is already a string --- CHANGES.rst | 1 + src/ZPublisher/HTTPResponse.py | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGES.rst b/CHANGES.rst index edac84affb..bf71854c4b 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -38,6 +38,7 @@ https://github.com/zopefoundation/Zope/blob/4.x/CHANGES.rst For details, see `#1202 `_. +- Small optimization when encoding the response body. 5.9 (2023-11-24) ---------------- diff --git a/src/ZPublisher/HTTPResponse.py b/src/ZPublisher/HTTPResponse.py index 264a488a03..9967cd9b8f 100644 --- a/src/ZPublisher/HTTPResponse.py +++ b/src/ZPublisher/HTTPResponse.py @@ -552,7 +552,9 @@ def setBody(self, body, title='', is_error=False, lock=None): except (TypeError, UnicodeError): pass if not isinstance(body, bytes): - body = self._encode_unicode(str(body)) + if not isinstance(body, str): + body = str(body) + body = self._encode_unicode(body) # At this point body is always binary b_len = len(body)