Skip to content
Myr

Merge pull request #572 from slingamn/chardet_2

Before adoption: this commit came in with the history the project adopted, so no landing records it.

cleanup, redux

authored by Kenneth Reitz · committed by Kenneth Reitz · parents 373a34a, 853a071 · Browse files · 5 files+20 added−28 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

5 files+20 added−28 removed

  • tests:+7 added−7 removed
  • other:+13 added−21 removed

5 files+20 added−28 removed

modified:requests/models.py+8 added-14 removed
35 unmodified lines
3637# Import chardet if it is available.38try:39 import chardet40 # hush pyflakes41 chardet42except ImportError:41 pass43 chardet = None4445REDIRECT_STATI = (codes.moved, codes.found, codes.other, codes.temporary_moved)46CONTENT_CHUNK_SIZE = 10 * 102447
733 unmodified lines
781782 self._content_consumed = True783 return self._content784783 def _detected_encoding(self):784 try:785 detected = chardet.detect(self.content) or {}786 return detected.get('encoding')787788 # Trust that chardet isn't available or something went terribly wrong.789 except Exception:790 pass791785 @property786 def text(self):787 """Content of the response, in unicode.788
4 unmodified lines
793 # Try charset from content-type794 content = None795 encoding = self.encoding796804 # Fallback to auto-detected encoding if chardet is available.797 # Fallback to auto-detected encoding.798 if self.encoding is None:806 encoding = self._detected_encoding()799 if chardet is not None:800 encoding = chardet.detect(self.content)['encoding']801 else:802 raise ValueError("Can't detect page encoding.")803804 # Decode unicode from given encoding.805 try:806 content = str(self.content, encoding, errors='replace')
2 unmodified lines
809 # indicate a misspelling or similar mistake.810 #811 # So we try blindly encoding.812 content = str(self.content, errors='replace')817 except (UnicodeError, TypeError):818 pass813814 return content815816 def raise_for_status(self, allow_redirects=True):
19 unmodified lines
modified:tests/test_cookies.py+7 added-7 removed
36 unmodified lines
37 """Cookies should not be sent to domains they didn't originate from."""38 r = requests.get("http://github.com")39 c = r.cookies40 # github should send us cookies41 self.assertGreaterEqual(len(c), 1)41 self.assertTrue(len(c) >= 1)4243 # github cookies should not be sent to httpbin.org:44 r2 = requests.get(httpbin('cookies'), cookies=c)45 self.assertEqual(json.loads(r2.text)['cookies'], {})4647 # let's do this again using the session object48 s = requests.session()49 s.get("http://github.com")50 self.assertGreaterEqual(len(s.cookies), 1)50 self.assertTrue(len(s.cookies) >= 1)51 r = s.get(httpbin('cookies'))52 self.assertEqual(json.loads(r.text)['cookies'], {})53 # we can set a cookie and get exactly that same-domain cookie back:54 r = s.get(httpbin('cookies', 'set', 'myname', 'myvalue'))
23 unmodified lines
7879 r2 = requests.get(httpbin('cookies', 'set', 'very', 'well'), cookies=r.cookies)80 expected_cookies = {'redirects': 'work', 'very': 'well'}81 self.assertEqual(json.loads(r2.text)['cookies'], expected_cookies)82 self.assertIs(r.cookies, r2.cookies)82 self.assertTrue(r.cookies is r2.cookies)8384 def test_none_cookie(self):85 """Regression test: don't send a Cookie header with a string value of 'None'!"""86 page = json.loads(requests.get(httpbin('headers')).text)87 self.assertNotIn('Cookie', page['headers'])87 self.assertTrue('Cookie' not in page['headers'])8889class LWPCookieJarTest(TestBaseMixin, unittest.TestCase):90 """Check store/load of cookies to FileCookieJar's, specifically LWPCookieJar's."""91
49 unmodified lines
141142 # github sets a cookie143 requests.get("http://github.com", cookies=cookiejar)144 num_github_cookies = len(cookiejar)145 self.assertGreaterEqual(num_github_cookies, 1)145 self.assertTrue(num_github_cookies >= 1)146 # httpbin sets another147 requests.get(httpbin('cookies', 'set', 'key', 'value'), cookies=cookiejar)148 num_total_cookies = len(cookiejar)149 self.assertGreaterEqual(num_total_cookies, 2)150 self.assertGreater(num_total_cookies, num_github_cookies)149 self.assertTrue(num_total_cookies >= 2)150 self.assertTrue(num_total_cookies > num_github_cookies)151152 # save and load153 cookiejar.save(ignore_discard=True)154 cookiejar_2 = self.COOKIEJAR_CLASS(self.cookiejar_filename)
12 unmodified lines
modified:.travis.yml+1 added-1 removed
modified:Makefile+3 added-6 removed
11 unmodified lines
1213simple:14 nosetests tests/test_requests.py1516ci:17 find tests/ -name "*.py" ! -name "test_requests_ext.py" ! -name "test_requests_async.py" | xargs nosetests --with-xunit --xunit-file=junit-report.xml1819server:20 gunicorn httpbin:app --bind=0.0.0.0:7077 &2119ci: init20 nosetests tests/test_requests.py --with-xunit --xunit-file=junit-report.xml2122simpleci:23 nosetests tests/test_requests.py --with-xunit --xunit-file=junit-report.xml2422stats:23 pyflakes requests | awk -F\: '{printf "%s:%s: [E]%s\n", $1, $2, $3}' > violations.pyflakes.txt2425site:
13 unmodified lines
modified:setup.py+1 added
2 unmodified lines
3"""4distutils/setuptools install script. See inline comments for packaging documentation.5"""67import os8import sys910import requests11from requests.compat import is_py3
70 unmodified lines