oh snap
authorKenneth Reitz <me@kennethreitz.com>
Mon, 23 Jan 2012 06:32:09 +0000 (01:32 -0500)
committerKenneth Reitz <me@kennethreitz.com>
Mon, 23 Jan 2012 06:32:09 +0000 (01:32 -0500)
requests/auth.py
requests/compat.py
requests/models.py
requests/packages/urllib3/connectionpool.py
requests/utils.py
test_requests.py

index 974e8bb..183731b 100644 (file)
@@ -13,9 +13,7 @@ import time
 import hashlib
 
 from base64 import b64encode
-# from urlparse import urlparse
-from urllib.parse import urlparse
-
+from .compat import urlparse, str, bytes
 from .utils import randombytes, parse_dict_header
 
 
index 2d1deb9..224bfd0 100644 (file)
@@ -79,11 +79,15 @@ is_solaris = ('solar==' in str(sys.platform).lower())   # Complete guess.
 
 
 if is_py2:
-    from urllib import quote, unquote
+    from urllib import quote, unquote, urlencode
     from urlparse import urlparse, urlunparse, urljoin, urlsplit
     from urllib2 import parse_http_list
     import cookielib
     from .packages.oreos.monkeys import SimpleCookie
+    from StringIO import StringIO
+
+    str = unicode
+    bytes = str
 
 
 elif is_py3:
@@ -91,4 +95,8 @@ elif is_py3:
     from urllib.request import parse_http_list
     from http import cookiejar as cookielib
     from http.cookies import SimpleCookie
+    from io import StringIO
+
+    str = str
+    bytes = bytes
 
index 365adca..c954026 100644 (file)
@@ -28,7 +28,7 @@ from .utils import (
     get_encoding_from_headers, stream_decode_response_unicode,
     stream_decompress, guess_filename, requote_path, dict_from_string)
 
-from .compat import urlparse, urlunparse, urljoin, urlsplit, urlencode, quote, unquote
+from .compat import urlparse, urlunparse, urljoin, urlsplit, urlencode, quote, unquote, str, bytes, SimpleCookie, is_py3, is_py2
 
 # Import chardet if it is available.
 try:
@@ -320,10 +320,11 @@ class Request(object):
 
         netloc = netloc.encode('idna').decode('utf-8')
 
-        # if isinstance(path, str):
-        #     path = path.encode('utf-8')
+        if is_py2:
+            if isinstance(path, str):
+                path = path.encode('utf-8')
 
-        # path = requote_path(path)
+            path = requote_path(path)
 
         # print([ scheme, netloc, path, params, query, fragment ])
         # print('---------------------')
@@ -354,7 +355,9 @@ class Request(object):
         if not path:
             path = '/'
 
+        # if is_py3:
         path = quote(path.encode('utf-8'))
+
         url.append(path)
 
         query = p.query
@@ -496,8 +499,6 @@ class Request(object):
                 if 'cookie' not in self.headers:
 
                     # Simple cookie with our dict.
-                    # c = oreos.monkeys.SimpleCookie()
-                    from http.cookies import SimpleCookie
                     c = SimpleCookie()
                     for (k, v) in list(self.cookies.items()):
                         c[k] = v
@@ -620,6 +621,10 @@ class Response(object):
         """Returns true if :attr:`status_code` is 'OK'."""
         return self.ok
 
+    def __nonzero__(self):
+        """Returns true if :attr:`status_code` is 'OK'."""
+        return self.ok
+
     @property
     def ok(self):
         try:
@@ -778,8 +783,8 @@ class Response(object):
             except (UnicodeError, TypeError):
                 pass
 
-
-
+        if not content:
+            content = str(content, encoding, errors='replace')
         return content
 
 
index ea77c8f..52b1802 100644 (file)
@@ -7,10 +7,6 @@
 import logging
 import socket
 
-from httplib import (HTTPConnection, HTTPSConnection, HTTPException,
-                     HTTP_PORT, HTTPS_PORT)
-
-from Queue import Queue, Empty, Full
 from socket import error as SocketError, timeout as SocketTimeout
 
 try:
index 2fa49f7..0e0f69e 100644 (file)
@@ -17,7 +17,7 @@ import re
 import zlib
 
 from .compat import parse_http_list as _parse_list_header
-from .compat import quote, unquote, cookielib, SimpleCookie
+from .compat import quote, unquote, cookielib, SimpleCookie, is_py2
 
 
 def dict_from_string(s):
@@ -178,7 +178,10 @@ def header_expand(headers):
 
 def randombytes(n):
     """Return n random bytes."""
-    L = [chr(random.randrange(0, 256)).encode('utf-8') for i in range(n)]
+    if is_py2:
+        L = [chr(random.randrange(0, 256)) for i in range(n)]
+    else:
+        L = [chr(random.randrange(0, 256)).encode('utf-8') for i in range(n)]
     return b"".join(L)
 
 
index 8d3923b..08ef517 100755 (executable)
@@ -3,23 +3,20 @@
 
 
 
-
-
 import io
+import json
 import time
 import os
 import sys
 import unittest
 
 import requests
+from requests.compat import str, bytes, StringIO
 # import envoy
 from requests import HTTPError
 from requests.auth import HTTPBasicAuth, HTTPDigestAuth
 
-try:
-    import omnijson as json
-except ImportError:
-    import json
+
 
 if (sys.platform == 'win32') and ('HTTPBIN_URL' not in os.environ):
     os.environ['HTTPBIN_URL'] = 'http://httpbin.org/'
@@ -344,7 +341,8 @@ class RequestsTestSuite(unittest.TestCase):
             rbody = json.loads(r.text)
             # Body wasn't valid url encoded data, so the server returns None as
             # "form" and the raw body as "data".
-            self.assertEqual(rbody.get('form'), {})
+
+            assert rbody.get('form') in (None, {})
             self.assertEqual(rbody.get('data'), 'fooaowpeuf')
 
 
@@ -392,7 +390,7 @@ class RequestsTestSuite(unittest.TestCase):
 
             rbody = json.loads(r.text)
 
-            self.assertEqual(rbody.get('form'), {})
+            assert rbody.get('form') in (None, {})
             self.assertEqual(rbody.get('data'), 'foobar')
 
 
@@ -686,7 +684,7 @@ class RequestsTestSuite(unittest.TestCase):
 
         # Make a request and monkey-patch its contents
         r = requests.get(httpbin('get'))
-        r.raw = io.StringIO(quote)
+        r.raw = StringIO(quote)
 
         # Make sure iter_lines doesn't chop the trailing bit
         lines = '\n'.join(r.iter_lines())