# For use with WSGI and Apache
# See https://modwsgi.readthedocs.io/en/develop/user-guides/debugging-techniques.html#python-interactive-debugger
# NOTE: not compatible with a) multi-threaded Apache or b) wsgi running in daemon mode.

class Debugger:

  def __init__(self, object):
    self.__object = object

  def __call__(self, *args, **kwargs):
    import pdb, sys
    debugger = pdb.Pdb()
    debugger.use_rawinput = 0
    debugger.reset()
    sys.settrace(debugger.trace_dispatch)
    try:
      return self.__object(*args, **kwargs)
    finally:
      debugger.quitting = 1
      sys.settrace(None)


# Example usage:
  """
  def application(environ, start_response):
    status = '200 OK'
    output = b'Hello World!\n'
    response_headers = [('Content-type', 'text/plain'), ('Content-Length', str(len(output)))]
    start_response(status, response_headers)
    return [output]


  application = Debugger(application)
  """

