Skip to content
Myr

Implement settings context manager

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

authored by Johannes · committed by Johannes · parent 39e9424 · Browse files · 2 files+44 added−14 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+44 added−14 removed

  • other:+44 added−14 removed

2 files+44 added−14 removed

modified:requests/__init__.py+25 added
1# -*- coding: utf-8 -*-23import inspect45import packages6from core import *78from core import __version__910timeout = None1112class settings:13 """Context manager for settings."""14 15 cache = {}16 17 def __init__(self, timeout):18 self.module = inspect.getmodule(self)19 20 # Cache settings21 self.cache['timeout'] = self.module.timeout22 23 self.module.timeout = timeout24 25 def __enter__(self):26 pass27 28 def __exit__(self, type, value, traceback):29 # Restore settings 30 for key in self.cache:31 setattr(self.module, key, self.cache[key])
modified:requests/core.py+19 added-14 removed
10 unmodified lines
11"""1213from __future__ import absolute_import1415import requests16import urllib17import urllib218import socket19import zlib
4 unmodified lines
24from .packages.poster.encode import multipart_encode25from .packages.poster.streaminghttp import register_openers, get_handlers26272728__title__ = 'requests'29__version__ = '0.3.2'30__build__ = 0x00030231__author__ = 'Kenneth Reitz'
6 unmodified lines
38 'URLRequired', 'InvalidMethod', 'HTTPError'39]40414242class _Request(urllib2.Request):43 """Hidden wrapper around the urllib2.Request object. Allows for manual44 setting of HTTP methods.45 """
398 unmodified lines
444 :param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.445 :param cookies: (optional) CookieJar object to send with the :class:`Request`.446 :param files: (optional) Dictionary of 'filename': file-like-objects for multipart encoding upload.447 :param auth: (optional) AuthObject to enable Basic HTTP Auth.448 :param timeout: (optional) Float describing the timeout of the request.449 """450 data = kwargs.pop('data', dict()) or kwargs.pop('params', dict())451451 452 r = Request(method=method, url=url, data=data, headers=kwargs.pop('headers', {}),453 cookiejar=kwargs.pop('cookies', None), files=kwargs.pop('files', None),454 auth=kwargs.pop('auth', auth_manager.get_auth(url)),455 timeout=kwargs.pop('timeout', None))455 timeout=kwargs.pop('timeout', requests.timeout))456 r.send()457458 return r.response459460461def get(url, params={}, headers={}, cookies=None, auth=None, timeout=None):461def get(url, params={}, headers={}, cookies=None, auth=None, **kwargs):462 """Sends a GET request. Returns :class:`Response` object.463464 :param url: URL for the new :class:`Request` object.465 :param params: (optional) Dictionary of GET Parameters to send with the :class:`Request`.466 :param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.467 :param cookies: (optional) CookieJar object to send with the :class:`Request`.468 :param auth: (optional) AuthObject to enable Basic HTTP Auth.469 :param timeout: (optional) Float describing the timeout of the request.470 """471471 return request('GET', url, params=params, headers=headers, cookies=cookies, auth=auth, timeout=timeout)472 return request('GET', url, params=params, headers=headers, cookies=cookies, auth=auth, **kwargs)473474474def head(url, params={}, headers={}, cookies=None, auth=None, timeout=None):475def head(url, params={}, headers={}, cookies=None, auth=None, **kwargs):476 """Sends a HEAD request. Returns :class:`Response` object.477478 :param url: URL for the new :class:`Request` object.479 :param params: (optional) Dictionary of GET Parameters to send with the :class:`Request`.480 :param headers: (optional) Dictionary of HTTP Headers to sent with the :class:`Request`.481 :param cookies: (optional) CookieJar object to send with the :class:`Request`.482 :param auth: (optional) AuthObject to enable Basic HTTP Auth.483 :param timeout: (optional) Float describing the timeout of the request.484 """485484 return request('HEAD', url, params=params, headers=headers, cookies=cookies, auth=auth, timeout=timeout)486 return request('HEAD', url, params=params, headers=headers, cookies=cookies, auth=auth, **kwargs)487488487def post(url, data={}, headers={}, files=None, cookies=None, auth=None, timeout=None):489def post(url, data={}, headers={}, files=None, cookies=None, auth=None, **kwargs):490 """Sends a POST request. Returns :class:`Response` object.491492 :param url: URL for the new :class:`Request` object.493 :param data: (optional) Dictionary of POST data to send with the :class:`Request`.494 :param headers: (optional) Dictionary of HTTP Headers to sent with the :class:`Request`.495 :param files: (optional) Dictionary of 'filename': file-like-objects for multipart encoding upload.496 :param cookies: (optional) CookieJar object to send with the :class:`Request`.497 :param auth: (optional) AuthObject to enable Basic HTTP Auth.498 :param timeout: (optional) Float describing the timeout of the request.499 """500498 return request('POST', url, data=data, headers=headers, files=files, cookies=cookies, auth=auth, timeout=timeout)501 return request('POST', url, data=data, headers=headers, files=files, cookies=cookies, auth=auth, **kwargs)502503501def put(url, data='', headers={}, files={}, cookies=None, auth=None, timeout=None):504def put(url, data='', headers={}, files={}, cookies=None, auth=None, **kwargs):505 """Sends a PUT request. Returns :class:`Response` object.506507 :param url: URL for the new :class:`Request` object.508 :param params: (optional) Bytes of PUT Data to send with the :class:`Request`.509 :param headers: (optional) Dictionary of HTTP Headers to sent with the :class:`Request`.510 :param files: (optional) Dictionary of 'filename': file-like-objects for multipart encoding upload.511 :param cookies: (optional) CookieJar object to send with the :class:`Request`.512 :param auth: (optional) AuthObject to enable Basic HTTP Auth.513 :param timeout: (optional) Float describing the timeout of the request.514 """515512 return request('PUT', url, data=data, headers=headers, files=files, cookies=cookies, auth=auth, timeout=timeout)516 return request('PUT', url, data=data, headers=headers, files=files, cookies=cookies, auth=auth, **kwargs)517518515def delete(url, params={}, headers={}, cookies=None, auth=None, timeout=None):519def delete(url, params={}, headers={}, cookies=None, auth=None, **kwargs):520 """Sends a DELETE request. Returns :class:`Response` object.521522 :param url: URL for the new :class:`Request` object.523 :param params: (optional) Dictionary of DELETE Parameters to send with the :class:`Request`.524 :param headers: (optional) Dictionary of HTTP Headers to sent with the :class:`Request`.525 :param cookies: (optional) CookieJar object to send with the :class:`Request`.526 :param auth: (optional) AuthObject to enable Basic HTTP Auth.527 :param timeout: (optional) Float describing the timeout of the request.528 """529525 return request('DELETE', url, params=params, headers=headers, cookies=cookies, auth=auth, timeout=timeout)530 return request('DELETE', url, params=params, headers=headers, cookies=cookies, auth=auth, **kwargs)531532533534class RequestException(Exception):
11 unmodified lines