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
Provenance
- Commit
a3e5563- Tree
db6aa20- Author
Kenneth Reitz <me@kennethreitz.com>- Committer
Kenneth Reitz <me@kennethreitz.com>- Parent 1
373a34a- Parent 2
853a071
5 files changed+20 added−28 removed
modified:requests/models.py+8 added-14 removed
35 unmodified lines36 37 # Import chardet if it is available.38 try:39 import chardet40+ # hush pyflakes41+ chardet42 except ImportError:41- pass43+ chardet = None44 45 REDIRECT_STATI = (codes.moved, codes.found, codes.other, codes.temporary_moved)46 CONTENT_CHUNK_SIZE = 10 * 102447 733 unmodified lines781 782 self._content_consumed = True783 return self._content784 783- def _detected_encoding(self):784- try:785- detected = chardet.detect(self.content) or {}786- return detected.get('encoding')787-788- # Trust that chardet isn't available or something went terribly wrong.789- except Exception:790- pass791-785 @property786 def text(self):787 """Content of the response, in unicode.788 4 unmodified lines793 # Try charset from content-type794 content = None795 encoding = self.encoding796 804- # 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.")803 804 # Decode unicode from given encoding.805 try:806 content = str(self.content, encoding, errors='replace')2 unmodified lines809 # 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- pass813 814 return content815 816 def raise_for_status(self, allow_redirects=True):19 unmodified linesmodified:tests/test_cookies.py+7 added-7 removed
36 unmodified lines37 """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)42 43 # 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'], {})46 47 # 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 lines78 79 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)83 84 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'])88 89 class LWPCookieJarTest(TestBaseMixin, unittest.TestCase):90 """Check store/load of cookies to FileCookieJar's, specifically LWPCookieJar's."""91 49 unmodified lines141 142 # 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)151 152 # save and load153 cookiejar.save(ignore_discard=True)154 cookiejar_2 = self.COOKIEJAR_CLASS(self.cookiejar_filename)12 unmodified linesmodified:.travis.yml+1 added-1 removed
2 unmodified lines34567789102 unmodified lines - 2.6 - 2.7 - 3.2env: HTTPBIN_URL=http://httpbin.org/script: make simpleciscript: make ciinstall: - pip install nose - pip install . --use-mirrorsmodified:Makefile+3 added-6 removed
11 unmodified lines12 13 simple:14 nosetests tests/test_requests.py15 16+ci:17+ find tests/ -name "*.py" ! -name "test_requests_ext.py" ! -name "test_requests_async.py" | xargs nosetests --with-xunit --xunit-file=junit-report.xml18+19 server:20 gunicorn httpbin:app --bind=0.0.0.0:7077 &21 19-ci: init20- nosetests tests/test_requests.py --with-xunit --xunit-file=junit-report.xml21-22-simpleci:23- nosetests tests/test_requests.py --with-xunit --xunit-file=junit-report.xml24-22 stats:23 pyflakes requests | awk -F\: '{printf "%s:%s: [E]%s\n", $1, $2, $3}' > violations.pyflakes.txt24 25 site:13 unmodified linesmodified:setup.py+1 added
2 unmodified lines3 """4 distutils/setuptools install script. See inline comments for packaging documentation.5 """6 7+import os8 import sys9 10 import requests11 from requests.compat import is_py370 unmodified lines