How to use the mechanize._response.upgrade_response function in mechanize

To help you get started, we’ve selected a few mechanize examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github python-mechanize / mechanize / test / test_response.py View on Github external
)
            for name in names:
                self.assertTrue(
                    hasattr(r, name), 'No attr named: {}'.format(name))
            self.assertEqual(r.get_data(), b"test data")

        from mechanize._response import upgrade_response, make_headers, make_response, closeable_response, seek_wrapper
        data = b"test data"
        url = "http://example.com/"
        code = 200
        msg = "OK"

        # Normal response (closeable_response wrapped with seek_wrapper): return a copy

        r1 = make_response(data, [], url, code, msg)
        r2 = upgrade_response(r1)
        is_response(r2)
        self.assertIsNot(r1, r2)
        self.assertIs(r1.wrapped, r2.wrapped)

        # closeable_response with no seek_wrapper: wrap with seek_wrapper

        r1 = closeable_response(
            BytesIO(data), make_headers([]), url, code, msg)
        self.assertRaises(AssertionError, is_response, r1)
        r2 = upgrade_response(r1)
        is_response(r2)
        self.assertIsNot(r1, r2)
        self.assertIs(r1, r2.wrapped)

        # addinfourl: extract .fp and wrap it with closeable_response and seek_wrapper
github python-mechanize / mechanize / test / test_response.py View on Github external
class MyHTTPError(mechanize.HTTPError):
            pass

        r1 = MyHTTPError(url, code, msg, hdrs, r1)
        self.assertRaises(AssertionError, is_response, r1)
        r2 = upgrade_response(r1)
        is_response(r2)
        self.assertIsInstance(r2, MyHTTPError)
        name = MyHTTPError.__module__ + '.' + MyHTTPError.__name__
        self.assertTrue(
            repr(r2).startswith(
                '
github python-mechanize / mechanize / test / test_response.py View on Github external
# addinfourl with code, msg

        r1 = addinfourl(BytesIO(data), make_headers([]), url)
        r1.code = 206
        r1.msg = "cool"
        r2 = upgrade_response(r1)
        is_response(r2)
        self.assertEqual(r2.code, r1.code)
        self.assertEqual(r2.msg, r1.msg)

        # addinfourl with seek wrapper: cached data is not lost

        r1 = addinfourl(BytesIO(data), make_headers([]), url)
        r1 = seek_wrapper(r1)
        self.assertEqual(r1.read(4), b'test')
        r2 = upgrade_response(r1)
        is_response(r2)

        # addinfourl wrapped with HTTPError -- remains an HTTPError of the same subclass (through horrible trickery)

        hdrs = make_headers([])
        r1 = addinfourl(BytesIO(data), hdrs, url)

        class MyHTTPError(mechanize.HTTPError):
            pass

        r1 = MyHTTPError(url, code, msg, hdrs, r1)
        self.assertRaises(AssertionError, is_response, r1)
        r2 = upgrade_response(r1)
        is_response(r2)
        self.assertIsInstance(r2, MyHTTPError)
        name = MyHTTPError.__module__ + '.' + MyHTTPError.__name__
github python-mechanize / mechanize / test / test_response.py View on Github external
# closeable_response with no seek_wrapper: wrap with seek_wrapper

        r1 = closeable_response(
            BytesIO(data), make_headers([]), url, code, msg)
        self.assertRaises(AssertionError, is_response, r1)
        r2 = upgrade_response(r1)
        is_response(r2)
        self.assertIsNot(r1, r2)
        self.assertIs(r1, r2.wrapped)

        # addinfourl: extract .fp and wrap it with closeable_response and seek_wrapper

        from mechanize.polyglot import addinfourl
        r1 = addinfourl(BytesIO(data), make_headers([]), url)
        self.assertRaises(AssertionError, is_response, r1)
        r2 = upgrade_response(r1)
        is_response(r2)
        self.assertIsNot(r1, r2)
        self.assertIsNot(r1, r2.wrapped)
        self.assertIs(r1.fp, r2.wrapped.fp)

        # addinfourl with code, msg

        r1 = addinfourl(BytesIO(data), make_headers([]), url)
        r1.code = 206
        r1.msg = "cool"
        r2 = upgrade_response(r1)
        is_response(r2)
        self.assertEqual(r2.code, r1.code)
        self.assertEqual(r2.msg, r1.msg)

        # addinfourl with seek wrapper: cached data is not lost
github python-mechanize / mechanize / mechanize / _mechanize.py View on Github external
def _set_response(self, response, close_current):
        # sanity check, necessary but far from sufficient
        if not (response is None or
                (hasattr(response, "info") and hasattr(response, "geturl") and
                 hasattr(response, "read"))):
            raise ValueError("not a response object")

        self.form = None
        if response is not None:
            response = _response.upgrade_response(response)
        if close_current and self._response is not None:
            self._response.close()
        self._response = response
        self._factory.set_response(response)
github python-mechanize / mechanize / mechanize / _mechanize.py View on Github external
#             Yes, urllib2 really does raise all these :-((
#             See test_urllib2.py for examples of socket.gaierror and OSError,
#             plus note that FTPHandler raises IOError.
#             XXX I don't seem to have an example of exactly socket.error being
#              raised, only socket.gaierror...
#             I don't want to start fixing these here, though, since this is a
#             subclass of OpenerDirector, and it would break old code.  Even in
#             Python core, a fix would need some backwards-compat. hack to be
#             acceptable.
#             raise

        if visit:
            self._set_response(response, False)
            response = copy.copy(self._response)
        elif response is not None:
            response = _response.upgrade_response(response)

        if not success:
            raise response
        return response
github Masood-M / yalih / mechanize / _mechanize.py View on Github external
def _set_response(self, response, close_current):
        # sanity check, necessary but far from sufficient
        if not (response is None or
                (hasattr(response, "info") and hasattr(response, "geturl") and
                 hasattr(response, "read"))):
            raise ValueError("not a response object")

        self.form = None
        if response is not None:
            response = _response.upgrade_response(response)
        if close_current and self._response is not None:
            self._response.close()
        self._response = response
        self._factory.set_response(response)
github Masood-M / yalih / mechanize / _mechanize.py View on Github external
#             Yes, urllib2 really does raise all these :-((
#             See test_urllib2.py for examples of socket.gaierror and OSError,
#             plus note that FTPHandler raises IOError.
#             XXX I don't seem to have an example of exactly socket.error being
#              raised, only socket.gaierror...
#             I don't want to start fixing these here, though, since this is a
#             subclass of OpenerDirector, and it would break old code.  Even in
#             Python core, a fix would need some backwards-compat. hack to be
#             acceptable.
#             raise

        if visit:
            self._set_response(response, False)
            response = copy.copy(self._response)
        elif response is not None:
            response = _response.upgrade_response(response)

        if not success:
            raise response
        return response