Coverage for requests.packages.urllib3._collections : 58%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
# urllib3/_collections.py # Copyright 2008-2013 Andrey Petrov and contributors (see CONTRIBUTORS.txt) # # This module is part of urllib3 and is released under # the MIT License: http://www.opensource.org/licenses/mit-license.php
except ImportError: # Platform-specific: No threads available class RLock: def __enter__(self): pass
def __exit__(self, exc_type, exc_value, traceback): pass
except ImportError: from .packages.ordered_dict import OrderedDict
""" Provides a thread-safe dict-like container which maintains up to ``maxsize`` keys while throwing away the least-recently-used keys beyond ``maxsize``.
:param maxsize: Maximum number of recent elements to retain.
:param dispose_func: Every time an item is evicted from the container, ``dispose_func(value)`` is called. Callback which will get called """
# Re-insert the item, moving it to the end of the eviction line. self._container[key] = item return item
# Possibly evict the existing value of 'key'
# If we didn't evict an existing value, we might have to evict the # least recently used item from the beginning of the container. _key, evicted_value = self._container.popitem(last=False)
self.dispose_func(evicted_value)
with self.lock: value = self._container.pop(key)
if self.dispose_func: self.dispose_func(value)
with self.lock: return len(self._container)
raise NotImplementedError('Iteration over this class is unlikely to be threadsafe.')
with self.lock: # Copy pointers to all values, then wipe the mapping # under Python 2, this copies the list of values twice :-| values = list(self._container.values()) self._container.clear()
if self.dispose_func: for value in values: self.dispose_func(value)
with self.lock: return self._container.keys()
""" :param headers: An iterable of field-value pairs. Must not contain multiple field names when compared case-insensitively.
:param kwargs: Additional field-value pairs to pass in to ``dict.update``.
A ``dict`` like container for storing HTTP Headers.
Field names are stored and compared case-insensitively in compliance with RFC 2616. Iteration provides the first case-sensitive key seen for each case-insensitive pair.
Using ``__setitem__`` syntax overwrites fields that compare equal case-insensitively in order to maintain ``dict``'s api. For fields that compare equal, instead create a new ``HTTPHeaderDict`` and use ``.add`` in a loop.
If multiple fields that are equal case-insensitively are passed to the constructor or ``.update``, the behavior is undefined and some will be lost.
>>> headers = HTTPHeaderDict() >>> headers.add('Set-Cookie', 'foo=bar') >>> headers.add('set-cookie', 'baz=quxx') >>> headers['content-length'] = '7' >>> headers['SET-cookie'] 'foo=bar, baz=quxx' >>> headers['Content-Length'] '7'
If you want to access the raw headers with their original casing for debugging purposes you can access the private ``._data`` attribute which is a normal python ``dict`` that maps the case-insensitive key to a list of tuples stored as (case-sensitive-original-name, value). Using the structure from above as our example:
>>> headers._data {'set-cookie': [('Set-Cookie', 'foo=bar'), ('set-cookie', 'baz=quxx')], 'content-length': [('content-length', '7')]} """
"""Adds a (name, value) pair, doesn't overwrite the value if it already exists.
>>> headers = HTTPHeaderDict(foo='bar') >>> headers.add('Foo', 'baz') >>> headers['foo'] 'bar, baz' """
"""Returns a list of all the values for the named field. Returns an empty list if the key doesn't exist.""" return self[key].split(', ') if key in self else []
h = HTTPHeaderDict() for key in self._data: for rawkey, value in self._data[key]: h.add(rawkey, value) return h
if not isinstance(other, Mapping): return False other = HTTPHeaderDict(other) return dict((k1, self[k1]) for k1 in self._data) == \ dict((k2, other[k2]) for k2 in other._data)
del self._data[key.lower()]
return '%s(%r)' % (self.__class__.__name__, dict(self.items())) |