MCPcopy Index your code
hub / github.com/python/cpython / TransientResource

Class TransientResource

Lib/test/test_urllib2net.py:41–64  ·  view source on GitHub ↗

Raise ResourceDenied if an exception is raised while the context manager is in effect that matches the specified exception and attributes.

Source from the content-addressed store, hash-verified

39
40
41class TransientResource(object):
42
43 """Raise ResourceDenied if an exception is raised while the context manager
44 is in effect that matches the specified exception and attributes."""
45
46 def __init__(self, exc, **kwargs):
47 self.exc = exc
48 self.attrs = kwargs
49
50 def __enter__(self):
51 return self
52
53 def __exit__(self, type_=None, value=None, traceback=None):
54 """If type_ is a subclass of self.exc and value has attributes matching
55 self.attrs, raise ResourceDenied. Otherwise let the exception
56 propagate (if any)."""
57 if type_ is not None and issubclass(self.exc, type_):
58 for attr, attr_value in self.attrs.items():
59 if not hasattr(value, attr):
60 break
61 if getattr(value, attr) != attr_value:
62 break
63 else:
64 raise ResourceDenied("an optional resource is not available")
65
66# Context managers that raise ResourceDenied when various issues
67# with the internet connection manifest themselves as exceptions.

Callers 1

test_urllib2net.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…