From 5a468b4c085900ba28b7f8dfa2cb0b50d7699aa6 Mon Sep 17 00:00:00 2001 From: Illia Volochii Date: Thu, 17 Jun 2021 19:36:44 +0300 Subject: [PATCH] Fixed #32859 -- Simplified compress_string() by using gzip.compress(). --- django/utils/text.py | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/django/utils/text.py b/django/utils/text.py index 7f3368b16a..3e4199a1d9 100644 --- a/django/utils/text.py +++ b/django/utils/text.py @@ -1,7 +1,7 @@ import html.entities import re import unicodedata -from gzip import GzipFile +from gzip import GzipFile, compress as gzip_compress from io import BytesIO from django.core.exceptions import SuspiciousFileOperation @@ -280,13 +280,8 @@ def phone2numeric(phone): return ''.join(char2number.get(c, c) for c in phone.lower()) -# From http://www.xhaus.com/alan/python/httpcomp.html#gzip -# Used with permission. def compress_string(s): - zbuf = BytesIO() - with GzipFile(mode='wb', compresslevel=6, fileobj=zbuf, mtime=0) as zfile: - zfile.write(s) - return zbuf.getvalue() + return gzip_compress(s, compresslevel=6, mtime=0) class StreamingBuffer(BytesIO):