Example #1
0
 def test_from_raw(self):
     serverlocal = Address.as_server('localhost', 9876)
     serverremote = Address.as_server('1.2.3.4', 9876)
     clientlocal = Address.as_client('localhost', 9876)
     clientremote = Address.as_client('1.2.3.4', 9876)
     default = Address(None, 1111)
     external = Address('', 1111)
     values = [
         (serverlocal, serverlocal),
         (serverremote, serverremote),
         (clientlocal, clientlocal),
         (clientremote, clientremote),
         (None, default),
         ('', external),
         ([], default),
         ({}, default),
         (9876, serverlocal),
         ('localhost:9876', clientlocal),
         ('1.2.3.4:9876', clientremote),
         ('*:9876', Address.as_server('', 9876)),
         ('*', external),
         (':9876', Address.as_server('', 9876)),
         ('localhost', Address('localhost', 1111)),
         (':', external),
         (dict(host='localhost'), Address('localhost', 1111)),
         (dict(port=9876), serverlocal),
         (dict(host=None, port=9876), serverlocal),
         (dict(host='localhost', port=9876), clientlocal),
         (dict(host='localhost', port='9876'), clientlocal),
     ]
     for value, expected in values:
         addr = Address.from_raw(value, defaultport=1111)
         assert addr == expected
Example #2
0
def run_file(address, filename, *extra, **kwargs):
    """Run pydevd for the given Python file."""
    addr = Address.from_raw(address)
    run = kwargs.pop('_run', _run)
    prog = kwargs.pop('_prog', sys.argv[0])
    argv = _run_argv(addr, filename, extra, _prog=prog)
    run(argv, addr, **kwargs)
    def start_client(self, addr):
        """Return ("socket", start_session) with a new client socket."""

        ptvsd.log.debug('Starting client daemon on {0!r}.', addr)

        addr = Address.from_raw(addr)
        self._singlesession = True
        with self.started():
            assert self.session is None
            client = create_client()
            connect(client, addr)
        sock = self._sock

        def start_session(**kwargs):
            self._check_ready_for_session()
            if self._server is not None:
                raise RuntimeError('running as server')
            if self._numsessions:
                raise RuntimeError('session stopped')

            try:
                self._bind_session(client)
                self._start_session_safely('ptvsd.Client', **kwargs)
                return self._session
            except Exception:
                self._stop_quietly()
                raise

        return sock, start_session
Example #4
0
def install(pydevd,
            address,
            start_server=start_server,
            start_client=start_client,
            **kwargs):
    """Configure pydevd to use our wrapper.

    This is a bit of a hack to allow us to run our VSC debug adapter
    in the same process as pydevd.  Note that, as with most hacks,
    this is somewhat fragile (since the monkeypatching sites may
    change).
    """
    addr = Address.from_raw(address)
    daemon = Daemon(**kwargs)

    _start_server = (lambda p: start_server(daemon, addr.host, p))
    _start_server.orig = start_server
    _start_client = (lambda h, p: start_client(daemon, h, p))
    _start_client.orig = start_client

    # These are the functions pydevd invokes to get a socket to the client.
    pydevd_comm.start_server = _start_server
    pydevd_comm.start_client = _start_client

    # Ensure that pydevd is using our functions.
    pydevd.start_server = _start_server
    pydevd.start_client = _start_client
    __main__ = sys.modules['__main__']
    if __main__ is not pydevd:
        if getattr(__main__, '__file__', None) == pydevd.__file__:
            __main__.start_server = _start_server
            __main__.start_client = _start_client
    return daemon
Example #5
0
def run_file(address, filename, *extra, **kwargs):
    """Run pydevd for the given Python file."""
    addr = Address.from_raw(address)
    if not addr.isserver:
        kwargs['singlesession'] = True
    run = kwargs.pop('_run', _run)
    prog = kwargs.pop('_prog', sys.argv[0])
    argv = _run_argv(addr, filename, extra, _prog=prog)
    run(argv, addr, **kwargs)
Example #6
0
def run_file(address, filename, *extra, **kwargs):
    """Run pydevd for the given Python file."""
    addr = Address.from_raw(address)
    if not addr.isserver:
        kwargs['singlesession'] = True
    run = kwargs.pop('_run', _run)
    prog = kwargs.pop('_prog', sys.argv[0])
    argv = _run_argv(addr, filename, extra, _prog=prog)
    run(argv, addr, **kwargs)
Example #7
0
def run_module(address, modname, *extra, **kwargs):
    """Run pydevd for the given module."""
    addr = Address.from_raw(address)
    run = kwargs.pop('_run', _run)
    prog = kwargs.pop('_prog', sys.argv[0])
    filename = modname + ':'
    argv = _run_argv(addr, filename, extra, _prog=prog)
    argv.insert(argv.index('--file'), '--module')
    run(argv, addr, **kwargs)
Example #8
0
def run_module(address, modname, *extra, **kwargs):
    """Run pydevd for the given module."""
    addr = Address.from_raw(address)
    if not addr.isserver:
        kwargs['singlesession'] = True
    run = kwargs.pop('_run', _run)
    prog = kwargs.pop('_prog', sys.argv[0])
    filename = modname + ':'
    argv = _run_argv(addr, filename, extra, _prog=prog)
    argv.insert(argv.index('--file'), '--module')
    run(argv, addr, **kwargs)
    def start_server(self, addr, hidebadsessions=True):
        """Return ("socket", next_session) with a new server socket."""

        ptvsd.log.debug('Starting server daemon on {0!r}.', addr)

        addr = Address.from_raw(addr)
        with self.started():
            assert self._sessionlock is None
            assert self.session is None
            self._server = create_server(addr.host, addr.port)
            host, port = self._server.getsockname()
            ptvsd.log.debug('Server socket created on {0!r}', addr)
            self._sessionlock = threading.Lock()
        sock = self._sock

        def check_ready(**kwargs):
            self._check_ready_for_session(**kwargs)
            if self._server is None:
                raise DaemonStoppedError()

        def next_session(timeout=None, **kwargs):
            server = self._server
            sessionlock = self._sessionlock
            check_ready(checksession=False)

            ptvsd.log.debug('Getting next session...')
            sessionlock.acquire()  # Released in _finish_session().
            ptvsd.log.debug('Session lock acquired.')
            # It may have closed or stopped while we waited.
            check_ready()

            timeout = kwargs.pop('timeout', None)
            try:
                ptvsd.log.debug('Getting session socket...')
                client = connect(server, None, **kwargs)
                self._bind_session(client)
                ptvsd.log.debug('Starting session...')
                self._start_session_safely('ptvsd.Server', timeout=timeout)
                ptvsd.log.debug('Session started.')
                return self._session
            except Exception:
                ptvsd.log.exception(category=('D' if hidebadsessions else 'E'))
                with ignore_errors():
                    self._finish_session()
                if hidebadsessions:
                    ptvsd.log.debug('Hiding bad session')
                    return None
                self._stop_quietly()
                raise

        if options.subprocess_notify:
            multiproc.notify_root(port)

        return sock, next_session
Example #10
0
    def start_server(self, addr, hidebadsessions=True):
        """Return ("socket", next_session) with a new server socket."""
        addr = Address.from_raw(addr)
        with self.started():
            assert self._sessionlock is None
            assert self.session is None
            self._server = create_server(addr.host, addr.port)
            host, port = self._server.getsockname()
            debug('server socket created on %r:%r' % (host, port))
            self._sessionlock = threading.Lock()
        sock = self._sock

        def check_ready(**kwargs):
            self._check_ready_for_session(**kwargs)
            if self._server is None:
                raise DaemonStoppedError()

        def next_session(timeout=None, **kwargs):
            server = self._server
            sessionlock = self._sessionlock
            check_ready(checksession=False)

            debug('getting next session')
            sessionlock.acquire()  # Released in _finish_session().
            debug('session lock acquired')
            # It may have closed or stopped while we waited.
            check_ready()

            timeout = kwargs.pop('timeout', None)
            try:
                debug('getting session socket')
                client = connect(server, None, **kwargs)
                self._bind_session(client)
                debug('starting session')
                self._start_session_safely('ptvsd.Server', timeout=timeout)
                debug('session started')
                return self._session
            except Exception as exc:
                debug('session exc:', exc, tb=True)
                with ignore_errors():
                    self._finish_session()
                if hidebadsessions:
                    debug('hiding bad session')
                    # TODO: Log the error?
                    return None
                self._stop_quietly()
                raise

        if options.subprocess_notify:
            multiproc.notify_root(port)

        return sock, next_session
Example #11
0
def install(pydevd_module,
            address,
            start_server=start_server,
            start_client=start_client,
            **kwargs):
    """Configure pydevd to use our wrapper.

    This is a bit of a hack to allow us to run our VSC debug adapter
    in the same process as pydevd.  Note that, as with most hacks,
    this is somewhat fragile (since the monkeypatching sites may
    change).
    """

    ptvsd.log.debug('Installing pydevd hooks.')

    addr = Address.from_raw(address)
    daemon = Daemon(**kwargs)

    def _start_server(p):
        ptvsd.log.debug('ptvsd: install._start_server.')
        return start_server(daemon, addr.host, p)

    def _start_client(h, p):
        ptvsd.log.debug('ptvsd: install._start_client.')
        return start_client(daemon, h, p)

    _start_server.orig = start_server
    _start_client.orig = start_client

    # These are the functions pydevd invokes to get a socket to the client.
    pydevd_comm.start_server = _start_server
    pydevd_comm.start_client = _start_client

    # This is invoked when a child process is spawned with multiproc debugging enabled.
    pydev_monkey.patch_args = multiproc.patch_and_quote_args
    if not options.multiprocess and not options.no_debug:
        # This means '--multiprocess' flag was not passed via command line args. Patch the
        # new process functions here to handle multiprocess being enabled via debug options.
        ptvsd.log.debug('Monkey-patching multiprocess functions.')
        pydev_monkey.patch_new_process_functions()

    # Ensure that pydevd is using our functions.
    pydevd_module.start_server = _start_server
    pydevd_module.start_client = _start_client
    __main__ = sys.modules['__main__']
    if __main__ is not pydevd:
        if getattr(__main__, '__file__', None) == pydevd.__file__:
            __main__.start_server = _start_server
            __main__.start_client = _start_client

    return daemon
Example #12
0
    def __init__(
            self,
            addr=None,
            port=8888,
            breakpoints=None,
            connecttimeout=1.0,
    ):
        super(_LifecycleClient, self).__init__()
        self._addr = Address.from_raw(addr, defaultport=port)
        self._connecttimeout = connecttimeout
        self._adapter = None
        self._session = None

        self._breakpoints = breakpoints
Example #13
0
def install(pydevd_module, address,
            start_server=start_server, start_client=start_client,
            **kwargs):
    """Configure pydevd to use our wrapper.

    This is a bit of a hack to allow us to run our VSC debug adapter
    in the same process as pydevd.  Note that, as with most hacks,
    this is somewhat fragile (since the monkeypatching sites may
    change).
    """

    ptvsd.log.debug('Installing pydevd hooks.')

    addr = Address.from_raw(address)
    daemon = Daemon(**kwargs)

    _start_server = (lambda p: start_server(daemon, addr.host, p))
    _start_server.orig = start_server
    _start_client = (lambda h, p: start_client(daemon, h, p))
    _start_client.orig = start_client

    # These are the functions pydevd invokes to get a socket to the client.
    pydevd_comm.start_server = _start_server
    pydevd_comm.start_client = _start_client

    # This is invoked when a child process is spawned with multiproc debugging enabled.
    pydev_monkey.patch_args = multiproc.patch_and_quote_args
    if not options.multiprocess and not options.no_debug:
        # This means '--multiprocess' flag was not passed via command line args. Patch the
        # new process functions here to handle multiprocess being enabled via debug options.
        ptvsd.log.debug('Monkey-patching multiprocess functions.')
        pydev_monkey.patch_new_process_functions()

    # Ensure that pydevd is using our functions.
    pydevd_module.start_server = _start_server
    pydevd_module.start_client = _start_client
    __main__ = sys.modules['__main__']
    if __main__ is not pydevd:
        if getattr(__main__, '__file__', None) == pydevd.__file__:
            __main__.start_server = _start_server
            __main__.start_client = _start_client

    return daemon
Example #14
0
 def _start(cls, new_proc, argv, addr=None, **kwargs):
     addr = Address.from_raw(addr, defaultport=cls.PORT)
     proc = new_proc(argv, addr, **kwargs)
     return cls(proc, addr, owned=True)
Example #15
0
def run_main(address, name, kind, *extra, **kwargs):
    addr = Address.from_raw(address)
    sys.argv[:] = _run_main_argv(name, extra)
    runner = kwargs.pop('_runner', no_debug_runner)
    runner(addr, name, kind == 'module', *extra, **kwargs)
Example #16
0
def run_main(address, name, kind, *extra, **kwargs):
    addr = Address.from_raw(address)
    sys.argv[:] = _run_main_argv(name, extra)
    runner = kwargs.pop('_runner', no_debug_runner)
    runner(addr, name, kind == 'module', *extra, **kwargs)