[Checkins] SVN: zope.testbrowser/trunk/ Remove use of ``urllib2`` APIs in favor of ``mechanize`` equivalents.

Tres Seaver tseaver at palladion.com
Sat Apr 24 14:59:54 EDT 2010


Log message for revision 111389:
  Remove use of ``urllib2`` APIs in favor of ``mechanize`` equivalents.
  
  ``urllib2`` is now incompatible with ``mechanize 0.2.0``.
  
  Thanks to John J. Lee for the patch.
  

Changed:
  U   zope.testbrowser/trunk/CHANGES.txt
  U   zope.testbrowser/trunk/src/zope/testbrowser/README.txt
  U   zope.testbrowser/trunk/src/zope/testbrowser/browser.py
  U   zope.testbrowser/trunk/src/zope/testbrowser/testing.py
  U   zope.testbrowser/trunk/src/zope/testbrowser/tests.py

-=-
Modified: zope.testbrowser/trunk/CHANGES.txt
===================================================================
--- zope.testbrowser/trunk/CHANGES.txt	2010-04-24 18:30:53 UTC (rev 111388)
+++ zope.testbrowser/trunk/CHANGES.txt	2010-04-24 18:59:54 UTC (rev 111389)
@@ -6,7 +6,9 @@
 ------------------
 
 - LP #568806: Update dependency ``mechanize >= 0.2.0``, which now includes
-  the ``ClientForm`` APIs.
+  the ``ClientForm`` APIs.  Remove use of ``urllib2`` APIs (incompatible
+  with ``mechanize 0.2.0``) in favor of ``mechanize`` equivalents.
+  Thanks to John J. Lee for the patch.
 
 - Use stdlib ``doctest`` module, instead of ``zope.testing.doctest``.
 

Modified: zope.testbrowser/trunk/src/zope/testbrowser/README.txt
===================================================================
--- zope.testbrowser/trunk/src/zope/testbrowser/README.txt	2010-04-24 18:30:53 UTC (rev 111388)
+++ zope.testbrowser/trunk/src/zope/testbrowser/README.txt	2010-04-24 18:59:54 UTC (rev 111389)
@@ -1290,7 +1290,7 @@
     ...
     HTTPError: HTTP Error 404: Not Found
 
-Note that the above error was thrown by ``urllib2`` and not by the
+Note that the above error was thrown by ``mechanize`` and not by the
 publisher.  For debugging purposes, however, it can be very useful to see the
 original exception caused by the application.  In those cases you can set the
 ``handleErrors`` property of the browser to ``False``.  It is defaulted to

Modified: zope.testbrowser/trunk/src/zope/testbrowser/browser.py
===================================================================
--- zope.testbrowser/trunk/src/zope/testbrowser/browser.py	2010-04-24 18:30:53 UTC (rev 111388)
+++ zope.testbrowser/trunk/src/zope/testbrowser/browser.py	2010-04-24 18:59:54 UTC (rev 111389)
@@ -23,7 +23,6 @@
 import re
 import sys
 import time
-import urllib2
 
 import mechanize
 import zope.interface
@@ -239,7 +238,7 @@
                 except Exception, e:
                     fix_exception_name(e)
                     raise
-            except urllib2.HTTPError, e:
+            except mechanize.HTTPError, e:
                 if e.code >= 200 and e.code <= 299:
                     # 200s aren't really errors
                     pass
@@ -254,7 +253,7 @@
             code, msg = self.headers['Status'].split(' ', 1)
             code = int(code)
             if self.raiseHttpErrors and code >= 400:
-                raise urllib2.HTTPError(url, code, msg, self.headers, fp=None)
+                raise mechanize.HTTPError(url, code, msg, self.headers, fp=None)
 
     def post(self, url, data, content_type=None):
         if content_type is not None:
@@ -743,7 +742,7 @@
             if index is not None or coord != (1,1):
                 raise ValueError(
                     'May not use index or coord without a control')
-            request = self.mech_form._switch_click("request", urllib2.Request)
+            request = self.mech_form._switch_click("request", mechanize.Request)
             self.browser._start_timer()
             self.browser.mech_browser.open(request)
             self.browser._stop_timer()

Modified: zope.testbrowser/trunk/src/zope/testbrowser/testing.py
===================================================================
--- zope.testbrowser/trunk/src/zope/testbrowser/testing.py	2010-04-24 18:30:53 UTC (rev 111388)
+++ zope.testbrowser/trunk/src/zope/testbrowser/testing.py	2010-04-24 18:59:54 UTC (rev 111389)
@@ -20,12 +20,11 @@
 import mechanize
 import socket
 import sys
-import urllib2
 import zope.testbrowser.browser
 
 
 class PublisherConnection(object):
-    """A ``urllib2`` compatible connection obejct."""
+    """A ``mechanize`` compatible connection object."""
 
     def __init__(self, host, timeout=None):
         from zope.app.testing.functional import HTTPCaller
@@ -77,10 +76,10 @@
         self.response = self.caller(request_string, handle_errors)
 
     def getresponse(self):
-        """Return a ``urllib2`` compatible response.
+        """Return a ``mechanize`` compatible response.
 
         The goal of ths method is to convert the Zope Publisher's reseponse to
-        a ``urllib2`` compatible response, which is also understood by
+        a ``mechanize`` compatible response, which is also understood by
         mechanize.
         """
         real_response = self.response._response
@@ -96,7 +95,7 @@
 
 
 class PublisherResponse(object):
-    """``urllib2`` compatible response object."""
+    """``mechanize`` compatible response object."""
 
     def __init__(self, content, headers, status, reason):
         self.content = content
@@ -109,11 +108,11 @@
         return self.content_as_file.read(amt)
 
     def close(self):
-        """To overcome changes in urllib2 and socket in python2.5"""
+        """To overcome changes in mechanize and socket in python2.5"""
         pass
 
 
-class PublisherHTTPHandler(urllib2.HTTPHandler):
+class PublisherHTTPHandler(mechanize.HTTPHandler):
     """Special HTTP handler to use the Zope Publisher."""
 
     def http_request(self, req):
@@ -124,12 +123,12 @@
                 req.add_data(data['body'])
                 req.add_unredirected_header('Content-type',
                                             data['content-type'])
-        return urllib2.AbstractHTTPHandler.do_request_(self, req)
+        return mechanize.HTTPHandler.do_request_(self, req)
 
     https_request = http_request
 
     def http_open(self, req):
-        """Open an HTTP connection having a ``urllib2`` request."""
+        """Open an HTTP connection having a ``mechanize`` request."""
         # Here we connect to the publisher.
         if sys.version_info > (2, 6) and not hasattr(req, 'timeout'):
             # Workaround mechanize incompatibility with Python

Modified: zope.testbrowser/trunk/src/zope/testbrowser/tests.py
===================================================================
--- zope.testbrowser/trunk/src/zope/testbrowser/tests.py	2010-04-24 18:30:53 UTC (rev 111388)
+++ zope.testbrowser/trunk/src/zope/testbrowser/tests.py	2010-04-24 18:59:54 UTC (rev 111389)
@@ -25,7 +25,6 @@
 import re
 import socket
 import sys
-import urllib2
 
 from zope.app.testing.functional import FunctionalDocFileSuite
 import zope.app.testing.functional
@@ -50,7 +49,7 @@
 
 
 class FauxConnection(object):
-    """A ``urllib2`` compatible connection object."""
+    """A ``mechanize`` compatible connection object."""
 
     def __init__(self, host, timeout=None):
         pass
@@ -89,10 +88,10 @@
         print request_string.replace('\r', '')
 
     def getresponse(self):
-        """Return a ``urllib2`` compatible response.
+        """Return a ``mechanize`` compatible response.
 
         The goal of this method is to convert the Zope Publisher's response to
-        a ``urllib2`` compatible response, which is also understood by
+        a ``mechanize`` compatible response, which is also understood by
         mechanize.
         """
         return FauxResponse(next_response_body,
@@ -115,16 +114,16 @@
         return self.content_as_file.read(amt)
 
     def close(self):
-        """To overcome changes in urllib2 and socket in python2.5"""
+        """To overcome changes in mechanize and socket in python2.5"""
         pass
 
 
-class FauxHTTPHandler(urllib2.HTTPHandler):
+class FauxHTTPHandler(mechanize.HTTPHandler):
 
-    http_request = urllib2.AbstractHTTPHandler.do_request_
+    http_request = mechanize.HTTPHandler.do_request_
 
     def http_open(self, req):
-        """Open an HTTP connection having a ``urllib2`` request."""
+        """Open an HTTP connection having a ``mechanize`` request."""
         # Here we connect to the publisher.
 
         if sys.version_info > (2, 6) and not hasattr(req, 'timeout'):
@@ -141,10 +140,10 @@
         "http": FauxHTTPHandler,
 
         "_http_error": mechanize.HTTPErrorProcessor,
-        "_http_default_error": urllib2.HTTPDefaultErrorHandler,
+        "_http_default_error": mechanize.HTTPDefaultErrorHandler,
 
         # feature handlers
-        "_authen": urllib2.HTTPBasicAuthHandler,
+        "_authen": mechanize.HTTPBasicAuthHandler,
         "_redirect": mechanize.HTTPRedirectHandler,
         "_cookies": mechanize.HTTPCookieProcessor,
         "_refresh": mechanize.HTTPRefreshProcessor,



More information about the checkins mailing list