Skip to content
Myr

Merge https://github.com/rickmak/requests into feature/unicode

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

Conflicts: requests/models.py

authored by Kenneth Reitz · committed by Kenneth Reitz · parents 8bfa909, d26f333 · Browse files · 2 files+119 added−1 removed
This commit

State

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

Diff stats

2 files+119 added−1 removed

  • other:+119 added−1 removed

2 files+119 added−1 removed

modified:requests/models.py+53 added-1 removed
8 unmodified lines
9import urllib10import urllib211import socket12import zlib13import cgi14import re1516from urllib2 import HTTPError17from urlparse import urlparse, urlunparse, urljoin18from datetime import datetime
414 unmodified lines
433 try:434 self._content = zlib.decompress(self._content, 16+zlib.MAX_WBITS)435 except zlib.error:436 pass435 return self._content437 return self.unicode_content(self._content)438439 440 else:441 raise AttributeError442 443444 def get_content_type(self):445 content_type = self.headers.get("content-type")446 content_type, params = cgi.parse_header(content_type)447 return content_type, params448449 def get_encoding_from_content_type(self):450 content_type, params = self.get_content_type()451 if "charset" in params:452 return params["charset"].strip("'\"")453454 def get_encodings_from_content(self, content):455 if self._charset_re is None:456 self._charset_re = re.compile(457 r'<meta.*?charset=["\']*(.+?)["\'>]', flags=re.I458 )459 return self._charset_re.findall(content)460 461 def unicode_content(self, content):462 """463 Returns the requested content back in unicode.464 Tried:465 1. charset from content-type466 2. every encodings from <meta ... charset=XXX>467 3. fall back and replace all unicode characters468 """469 tried_encodings = []470 # Try charset from content-type471 encoding = self.get_encoding_from_content_type()472 if encoding:473 try:474 return unicode(content, encoding)475 except UnicodeError:476 tried_encodings.append(encoding)477478 # Try every encodings from <meta ... charset=XXX>479 encodings = self.get_encodings_from_content(content)480 for encoding in encodings:481 if encoding in tried_encodings:482 continue483 try:484 return unicode(content, encoding)485 except UnicodeError:486 tried_encodings.append(encoding)487488 # Fall back:489 return unicode(content, encoding, errors="replace")490491 def raise_for_status(self):492 """Raises stored :class:`HTTPError` or :class:`URLError`, if one occured."""493 if self.error:494 raise self.error
181 unmodified lines
added:test_unicode.py+66 added
1#!/usr/bin/env python2# -*- coding: utf-8 -*-34from __future__ import with_statement56import unittest7import cookielib89try:10 import omnijson as json11except ImportError:12 import json1314import requests15161718HTTPBIN_URL = 'http://httpbin.org/'19HTTPSBIN_URL = 'https://httpbin.ep.io/'2021# HTTPBIN_URL = 'http://staging.httpbin.org/'22# HTTPSBIN_URL = 'https://httpbin-staging.ep.io/'232425def httpbin(*suffix):26 """Returns url for HTTPBIN resource."""2728 return HTTPBIN_URL + '/'.join(suffix)293031def httpsbin(*suffix):32 """Returns url for HTTPSBIN resource."""3334 return HTTPSBIN_URL + '/'.join(suffix)353637SERVICES = (httpbin, httpsbin)38394041class RequestsTestSuite(unittest.TestCase):42 """Requests test cases."""434445 def setUp(self):46 pass474849 def tearDown(self):50 """Teardown."""51 pass525354 def test_HTTP_200_OK_GET_ON_ISO88591(self):55 r = requests.get("http://www.qypedeals.de/Verzehrgutschein+für+Jellyfish")56 self.assertEqual(r.status_code, 200)57 self.assertIsInstance(r.content, unicode)58 59 def test_HTTP_200_OK_GET_ON_BIG5(self):60 r = requests.get("http://google.com.hk/")61 self.assertEqual(r.status_code, 200)62 self.assertIsInstance(r.content, unicode)636465if __name__ == '__main__':66 unittest.main()