From 23f94f0741100233862922a8e77a3ac9dc1833e9 Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Tue, 17 Jul 2012 21:58:18 +0200 Subject: [PATCH] Fixed #18561 -- Made HttpResponse.tell() support non-ascii chars --- django/http/__init__.py | 2 +- tests/regressiontests/httpwrappers/tests.py | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/django/http/__init__.py b/django/http/__init__.py index 7ded554665..4b8f70fb4b 100644 --- a/django/http/__init__.py +++ b/django/http/__init__.py @@ -683,7 +683,7 @@ class HttpResponse(object): def tell(self): if self._base_content_is_iter: raise Exception("This %s instance cannot tell its position" % self.__class__) - return sum([len(str(chunk)) for chunk in self._container]) + return sum([len(chunk) for chunk in self]) class HttpResponseRedirect(HttpResponse): status_code = 302 diff --git a/tests/regressiontests/httpwrappers/tests.py b/tests/regressiontests/httpwrappers/tests.py index cc55057d31..9b599db6d0 100644 --- a/tests/regressiontests/httpwrappers/tests.py +++ b/tests/regressiontests/httpwrappers/tests.py @@ -1,3 +1,4 @@ +# -*- encoding: utf-8 -*- from __future__ import unicode_literals import copy @@ -298,6 +299,17 @@ class HttpResponseTests(unittest.TestCase): self.assertRaises(UnicodeEncodeError, getattr, r, 'content') + def test_file_interface(self): + r = HttpResponse() + r.write(b"hello") + self.assertEqual(r.tell(), 5) + r.write("привет") + self.assertEqual(r.tell(), 17) + + r = HttpResponse(['abc']) + self.assertRaises(Exception, r.write, 'def') + + class CookieTests(unittest.TestCase): def test_encode(self): """