Code

Ticket #18561: 18561.diff

File 18561.diff, 1.4 KB (added by claudep, 22 months ago)

Make tell() handle unicode content

Line 
1diff --git a/django/http/__init__.py b/django/http/__init__.py
2index 51e6ca2..8f97428 100644
3--- a/django/http/__init__.py
4+++ b/django/http/__init__.py
5@@ -694,7 +694,7 @@ class HttpResponse(object):
6     def tell(self):
7         if self._base_content_is_iter:
8             raise Exception("This %s instance cannot tell its position" % self.__class__)
9-        return sum([len(str(chunk)) for chunk in self._container])
10+        return sum([len(chunk) for chunk in self])
11 
12 class HttpResponseRedirect(HttpResponse):
13     status_code = 302
14diff --git a/tests/regressiontests/httpwrappers/tests.py b/tests/regressiontests/httpwrappers/tests.py
15index 870324b..d52adc4 100644
16--- a/tests/regressiontests/httpwrappers/tests.py
17+++ b/tests/regressiontests/httpwrappers/tests.py
18@@ -1,3 +1,4 @@
19+# -*- encoding: utf-8 -*-
20 from __future__ import unicode_literals
21 
22 import copy
23@@ -298,6 +299,17 @@ class HttpResponseTests(unittest.TestCase):
24         self.assertRaises(UnicodeEncodeError,
25                           getattr, r, 'content')
26 
27+    def test_file_interface(self):
28+        r = HttpResponse()
29+        r.write(b"hello")
30+        self.assertEqual(r.tell(), 5)
31+        r.write("привет")
32+        self.assertEqual(r.tell(), 17)
33+
34+        r = HttpResponse(['abc'])
35+        self.assertRaises(Exception, r.write, 'def')
36+
37+
38 class CookieTests(unittest.TestCase):
39     def test_encode(self):
40         """