[Python-3000-checkins] r56168 - python/branches/py3k-struni/Lib/urllib2.py

guido.van.rossum python-3000-checkins at python.org
Tue Jul 3 23:07:17 CEST 2007


Author: guido.van.rossum
Date: Tue Jul  3 23:07:17 2007
New Revision: 56168

Modified:
   python/branches/py3k-struni/Lib/urllib2.py
Log:
Two necessary fixes (but not enough to make tests pass):
- Use os.urandom() as the only source of random bytes.
- Don't reference socket._fileobject; use io.BufferedReader instead.


Modified: python/branches/py3k-struni/Lib/urllib2.py
==============================================================================
--- python/branches/py3k-struni/Lib/urllib2.py	(original)
+++ python/branches/py3k-struni/Lib/urllib2.py	Tue Jul  3 23:07:17 2007
@@ -90,6 +90,7 @@
 import base64
 import hashlib
 import httplib
+import io
 import mimetools
 import os
 import posixpath
@@ -832,17 +833,7 @@
 
 def randombytes(n):
     """Return n random bytes."""
-    # Use /dev/urandom if it is available.  Fall back to random module
-    # if not.  It might be worthwhile to extend this function to use
-    # other platform-specific mechanisms for getting random bytes.
-    if os.path.exists("/dev/urandom"):
-        f = open("/dev/urandom")
-        s = f.read(n)
-        f.close()
-        return s
-    else:
-        L = [chr(random.randrange(0, 256)) for i in range(n)]
-        return "".join(L)
+    return str(os.urandom(n), "latin-1")
 
 class AbstractDigestAuthHandler:
     # Digest authentication is specified in RFC 2617.
@@ -1077,14 +1068,10 @@
         # Pick apart the HTTPResponse object to get the addinfourl
         # object initialized properly.
 
-        # Wrap the HTTPResponse object in socket's file object adapter
-        # for Windows.  That adapter calls recv(), so delegate recv()
-        # to read().  This weird wrapping allows the returned object to
-        # have readline() and readlines() methods.
-
-        r.recv = r.read
-        # XXX socket._fileobject is gone; use some class from io.py instead
-        fp = socket._fileobject(r, close=True)
+        # Add some fake methods to the reader to satisfy BufferedReader.
+        r.readable = lambda: True
+        r.writable = r.seekable = lambda: False
+        fp = io.BufferedReader(r)
 
         resp = addinfourl(fp, r.msg, req.get_full_url())
         resp.code = r.status


More information about the Python-3000-checkins mailing list