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

Class OpenerDirector

Lib/urllib/request.py:394–533  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

392 return list(hdrs.items())
393
394class OpenerDirector:
395 def __init__(self):
396 client_version = "Python-urllib/%s" % __version__
397 self.addheaders = [('User-agent', client_version)]
398 # self.handlers is retained only for backward compatibility
399 self.handlers = []
400 # manage the individual handlers
401 self.handle_open = {}
402 self.handle_error = {}
403 self.process_response = {}
404 self.process_request = {}
405
406 def add_handler(self, handler):
407 if not hasattr(handler, "add_parent"):
408 raise TypeError("expected BaseHandler instance, got %r" %
409 type(handler))
410
411 added = False
412 for meth in dir(handler):
413 if meth in ["redirect_request", "do_open", "proxy_open"]:
414 # oops, coincidental match
415 continue
416
417 i = meth.find("_")
418 if i < 1:
419 continue
420 protocol = meth[:i]
421 condition = meth[i+1:]
422
423 if condition.startswith("error"):
424 j = condition.find("_") + i + 1
425 kind = meth[j+1:]
426 try:
427 kind = int(kind)
428 except ValueError:
429 pass
430 lookup = self.handle_error.get(protocol, {})
431 self.handle_error[protocol] = lookup
432 elif condition == "open":
433 kind = protocol
434 lookup = self.handle_open
435 elif condition == "response":
436 kind = protocol
437 lookup = self.process_response
438 elif condition == "request":
439 kind = protocol
440 lookup = self.process_request
441 else:
442 continue
443
444 handlers = lookup.setdefault(kind, [])
445 if handlers:
446 bisect.insort(handlers, handler)
447 else:
448 handlers.append(handler)
449 added = True
450
451 if added:

Callers 15

build_test_openerFunction · 0.90
test_add_non_handlerMethod · 0.90
test_handledMethod · 0.90
test_handler_orderMethod · 0.90
test_raiseMethod · 0.90
test_http_errorMethod · 0.90
test_processorsMethod · 0.90

Calls

no outgoing calls

Tested by 15

build_test_openerFunction · 0.72
test_add_non_handlerMethod · 0.72
test_handledMethod · 0.72
test_handler_orderMethod · 0.72
test_raiseMethod · 0.72
test_http_errorMethod · 0.72
test_processorsMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…