0000002a
  A   ˆl–Ým_J
j"TýÆß¸ÛJbÑ¿�þD'eY²€­‰á<çÊƒyæCÉ_ŠI|¥�+Wé3ž¯ÉÈ "    # Copyright (c) 2009-2014, gevent contributors
# Based on eventlet.backdoor Copyright (c) 2005-2006, Bob Ippolito
"""
Interactive greenlet-based network console that can be used in any process.

The :class:`BackdoorServer` provides a REPL inside a running process. As
long as the process is monkey-patched, the ``BackdoorServer`` can coexist
with other elements of the process.

.. seealso:: :class:`code.InteractiveConsole`
"""
from __future__ import print_function, absolute_import
import sys
import socket
from code import InteractiveConsole

from gevent.greenlet import Greenlet
from gevent.hub import getcurrent
from gevent.server import StreamServer
from gevent.pool import Pool
from gevent._compat import PY36
from gevent._compat import exc_clear

__all__ = [
    'BackdoorServer',
]

try:
    sys.ps1
except AttributeError:
    sys.ps1 = '>>> '
try:
    sys.ps2
except AttributeError:
    sys.ps2 = '... '

class _Greenlet_stdreplace(Greenlet):
    # A greenlet that replaces sys.std[in/out/err] while running.

    __slots__ = (
        'stdin',
        'stdout',
        'prev_stdin',
        'prev_stdout',
        'prev_stderr',
    )

    def __init__(self, *args, **kwargs):
        Greenlet.__init__(self, *args, **kwargs)
        self.stdin = None
        self.stdout = None
        self.prev_stdin = None
        self.prev_stdout = None
        self.prev_stderr = None

    def switch(self, *args, **kw):
        if self.stdin is not None:
            self.switch_in()
        Greenlet.switch(self, *args, **kw)

    def switch_in(self):
        self.prev_stdin = sys.stdin
        self.prev_stdout = sys.stdout
        self.prev_stderr = sys.stderr

        sys.stdin = self.stdin
        sys.stdout = self.stdout
        sys.stderr = self.stdout

    def switch_out(self):
        sys.stdin = self.prev_stdin
        sys.stdout = self.prev_stdout
        sys.stderr = self.prev_stderr

        self.prev_stdin = self.prev_stdout = self.prev_stderr = None

    def throw(self, *args, **kwargs):
        # pylint:disable=arguments-differ
        if self.prev_stdin is None and self.stdin is not None:
            self.switch_in()
        Greenlet.throw(self, *args, **kwargs)

    def run(self):
        try:
            return Greenlet.run(self)
        finally:
            # Make sure to restore the originals.
            self.switch_out()


class BackdoorServer(StreamServer):
    """
    Provide a backdoor to a program for debugging purposes.

    .. warning:: This backdoor provides no authentication and makes no
          attempt to limit what remote users can do. Anyone that
          can access the server can take any action that the running
          python process can. Thus, while you may bind to any interface, for
          security purposes it is recommended that you bind to one
          only accessible to the local machine, e.g.,
          127.0.0.1/localhost.

    Basic usage::

        from gevent.backdoor import BackdoorServer
        server = BackdoorServer(('127.0.0.1', 5001),
                                banner="Hello from gevent backdoor!",
                                locals={'foo': "From defined scope!"})
        server.serve_forever()

    In a another terminal, connect with...::

        $ telnet 127.0.0.1 5001
        Trying 127.0.0.1...
        Connected to 127.0.0.1.
        Escape character is '^]'.
        Hello from gevent backdoor!
        >> print(foo)
        From defined scope!

    .. versionchanged:: 1.2a1
       Spawned greenlets are now tracked in a pool and killed when the server
       is stopped.
    """

    def __init__(self, listener, locals=None, banner=None, **server_args):
        """
        :keyword locals: If given, a dictionary of "builtin" values that will be available
            at the top-level.
        :keyword banner: If geven, a string that will be printed to each connecting user.
        """
        group = Pool(greenlet_class=_Greenlet_stdreplace) # no limit on number
        StreamServer.