Example #1
0
def test_loading_unicode_files_with_bad_global_charset(monkeypatch, tmpdir):
    dirname = str(tmpdir.mkdir('jedi-test'))
    filename1 = os.path.join(dirname, 'test1.py')
    filename2 = os.path.join(dirname, 'test2.py')
    if sys.version_info < (3, 0):
        data = "# coding: latin-1\nfoo = 'm\xf6p'\n"
    else:
        data = "# coding: latin-1\nfoo = 'm\xf6p'\n".encode("latin-1")

    with open(filename1, "wb") as f:
        f.write(data)
    s = Script("from test1 import foo\nfoo.", line=2, column=4, path=filename2)
    s.complete()
Example #2
0
def test_loading_unicode_files_with_bad_global_charset(monkeypatch, tmpdir):
    dirname = str(tmpdir.mkdir('jedi-test'))
    filename1 = os.path.join(dirname, 'test1.py')
    filename2 = os.path.join(dirname, 'test2.py')
    if sys.version_info < (3, 0):
        data = "# coding: latin-1\nfoo = 'm\xf6p'\n"
    else:
        data = "# coding: latin-1\nfoo = 'm\xf6p'\n".encode("latin-1")

    with open(filename1, "wb") as f:
        f.write(data)
    s = Script("from test1 import foo\nfoo.",
               line=2, column=4, path=filename2)
    s.complete()
Example #3
0
class Service:
    def __init__(self, *, project_path=None):

        self.project = (Project(project_path) if project_path
                        and os.path.exists(project_path) else None)
        self._source = ""
        self.script = None

    def change_workspace(self, project_path):
        self.project = (Project(project_path) if project_path
                        and os.path.exists(project_path) else None)
        self._source = ""
        self.script = None

    def complete(self, source, row, col) -> List[JediCompletion]:
        if not self._source.startswith(source):
            self._source = source
            self.script = Script(self._source, project=self.project)

        return self.script.complete(row, col)

    def hover(self, source, row, col) -> List[JediName]:
        if not self._source.startswith(source):
            self._source = source
            self.script = Script(self._source, project=self.project)

        return self.script.help(row, col)
Example #4
0
    def complete(self, column, line, script):
        column = int(column)
        line = int(line)

        completer = Script(script, line + 1, column + 1, '')
        completes = completer.complete()

        def transform(complete):
            return complete.word

        return json.dumps(map(transform, completes))
Example #5
0
    def complete(
        source: str, *, line: int, column: int, project: Project = None
    ) -> Completions:
        """complete script at following pos(line, column)

        Raises:
            ValueError: column > len(line_content)
        """

        script = Script(code=source, project=project)
        results = script.complete(line=line, column=column)
        return Completions(results)
Example #6
0
  def ComputeCandidates( self, unused_query, unused_start_column ):
    filename = vim.current.buffer.name
    line, column = vimsupport.CurrentLineAndColumn()
    # Jedi expects lines to start at 1, not 0
    line += 1
    contents = '\n'.join( vim.current.buffer )
    script = Script( contents, line, column, filename )

    return [ { 'word': str( completion.word ),
               'menu': str( completion.description ),
               'info': str( completion.doc ) }
             for completion in script.complete() ]
Example #7
0
    def ComputeCandidates(self, unused_query, unused_start_column):
        filename = vim.current.buffer.name
        line, column = vimsupport.CurrentLineAndColumn()
        # Jedi expects lines to start at 1, not 0
        line += 1
        contents = '\n'.join(vim.current.buffer)
        script = Script(contents, line, column, filename)

        return [{
            'word': str(completion.word),
            'menu': str(completion.description),
            'info': str(completion.doc)
        } for completion in script.complete()]
Example #8
0
  def SetCandidates( self ):
    while True:
      try:
        WaitAndClear( self._query_ready )

        filename = vim.current.buffer.name
        line, column = vimsupport.CurrentLineAndColumn()
        # Jedi expects lines to start at 1, not 0
        line += 1
        contents = '\n'.join( vim.current.buffer )
        script = Script( contents, line, column, filename )

        self._candidates = [ { 'word': str( completion.word ),
                               'menu': str( completion.description ),
                               'info': str( completion.doc ) }
                            for completion in script.complete() ]
      except:
        self._query_ready.clear()
        self._candidates = []
      self._candidates_ready.set()
Example #9
0
  def SetCandidates( self ):
    while True:
      try:
        WaitAndClear( self._query_ready )

        filename = vim.current.buffer.name
        line, column = vimsupport.CurrentLineAndColumn()
        # Jedi expects lines to start at 1, not 0
        line += 1
        contents = '\n'.join( vim.current.buffer )
        script = Script( contents, line, column, filename )

        self._candidates = [ { 'word': str( completion.word ),
                               'menu': str( completion.description ),
                               'info': str( completion.doc ) }
                            for completion in script.complete() ]
      except:
        self._query_ready.clear()
        self._candidates = []
      self._candidates_ready.set()
Example #10
0
File: ui.py Project: TFenby/wdb
 def do_complete(self, data):
     file_ = self.db.get_file(self.current_file)
     file_ = to_unicode(file_)
     lines = file_.splitlines()
     lno = self.current['lno']
     line_before = ''
     if len(lines) >= lno:
         line_before = lines[lno - 1]
     indent = len(line_before) - len(line_before.lstrip())
     segments = data.splitlines()
     for segment in reversed(segments):
         line = u(' ') * indent + segment
         lines.insert(lno - 1, line)
     script = Script(
         u('\n').join(lines), lno - 1 + len(segments),
         len(segments[-1]) + indent, '')
     try:
         completions = script.complete()
     except:
         log.info('Completion failed', exc_info=True)
         self.db.send('Log|%s' % dump({
             'message': 'Completion failed for %s' %
             '\n'.join(reversed(segments))
         }))
     else:
         fun = script.get_in_function_call()
         self.db.send('Suggest|%s' % dump({
             'params': {
                 'params': [p.get_code().replace('\n', '')
                            for p in fun.params],
                 'index': fun.index,
                 'module': fun.module.path,
                 'call_name': fun.call_name} if fun else None,
             'completions': [{
                 'base': comp.word[
                     :len(comp.word) - len(comp.complete)],
                 'complete': comp.complete,
                 'description': comp.description
             } for comp in completions if comp.word.endswith(
                 comp.complete)]
         }))
Example #11
0
 def update(self, source_code, line, col, filename, encoding):
     # complete with jedi
     try:
         script = Script(source_code, line, col, filename, encoding)
         completions = script.complete()
         # clean suggestion list
         self._suggestions[:] = []
         for completion in completions:
             # get type from description
             desc = completion.description
             suggestionType = desc.split(":")[0]
             # get the associated icon if any
             icon = None
             if suggestionType in Icons:
                 icon = Icons[suggestionType]
             else:
                 print "PCEF WARNING: Unimplemented suggestion type: %s" % suggestionType
             # add the suggestion to the list
             self._suggestions.append(Suggestion(completion.word, icon=icon, description=desc.split(":")[1]))
     except:
         pass
Example #12
0
File: ui.py Project: seletz/wdb
 def do_complete(self, data):
     file_ = self.db.get_file(self.current_file)
     file_ = to_unicode(file_)
     lines = file_.split(u'\n')
     lno = self.current['lno']
     line_before = lines[lno - 1]
     indent = len(line_before) - len(line_before.lstrip())
     segments = data.split(u'\n')
     for segment in reversed(segments):
         line = u' ' * indent + segment
         lines.insert(lno - 1, line)
     script = Script(u'\n'.join(lines), lno - 1 + len(segments),
                     len(segments[-1]) + indent, '')
     try:
         completions = script.complete()
     except:
         log.exception('Completion failed')
         self.db.send('Log|%s' % dump({
             'message':
             'Completion failed for %s' % '\n'.join(reversed(segments))
         }))
     else:
         fun = script.get_in_function_call()
         self.db.send('Suggest|%s' % dump({
             'params': {
                 'params':
                 [p.get_code().replace('\n', '') for p in fun.params],
                 'index': fun.index,
                 'module': fun.module.path,
                 'call_name': fun.call_name
             } if fun else None,
             'completions': [{
                 'base':
                 comp.word[:len(comp.word) - len(comp.complete)],
                 'complete':
                 comp.complete,
                 'description':
                 comp.description
             } for comp in completions if comp.word.endswith(comp.complete)]
         }))
Example #13
0
    def _interaction(
            self, frame, tb,
            exception, exception_description):

        log.debug('Interaction for %r %r %r %r' % (
            frame, tb, exception, exception_description))
        stack, trace, current_index = self.get_trace(frame, tb)
        current = trace[current_index]
        locals_ = map(lambda x: x[0].f_locals, stack)

        if self.begun:
            self.send('Trace|%s' % dump({
                'trace': trace,
                'cwd': os.getcwd()
            }))
            current_file = current['file']
            self.send('Check|%s' % dump({
                'name': current_file,
                'sha512': sha512(self.get_file(current_file)).hexdigest()
            }))
        else:
            self.begun = True

        while True:
            try:
                message = self.receive()
                if '|' in message:
                    pipe = message.index('|')
                    cmd = message[:pipe]
                    data = message[pipe + 1:]
                else:
                    cmd = message
                    data = ''

                def fail(title=None, message=None):
                    if message is None:
                        message = self.handle_exc()
                    else:
                        message = escape(message)
                    self.send('Echo|%s' % dump({
                        'for': escape(title or '%s failed' % cmd),
                        'val': message
                    }))

                log.debug('Cmd %s #Data %d' % (cmd, len(data)))
                if cmd == 'Start':
                    self.send('Init|%s' % dump({
                        'cwd': os.getcwd()
                    }))
                    self.send('Title|%s' % dump({
                        'title': exception,
                        'subtitle': exception_description
                    }))
                    self.send('Trace|%s' % dump({
                        'trace': trace
                    }))
                    current_file = current['file']
                    self.send('Check|%s' % dump({
                        'name': current_file,
                        'sha512': sha512(
                            self.get_file(current_file)).hexdigest()
                    }))

                elif cmd == 'Select':
                    current_index = int(data)
                    current = trace[current_index]
                    current_file = current['file']
                    self.send('Check|%s' % dump({
                        'name': current_file,
                        'sha512': sha512(
                            self.get_file(current_file)).hexdigest()
                    }))

                elif cmd == 'File':
                    current_file = current['file']
                    self.send('Select|%s' % dump({
                        'frame': current,
                        'breaks': self.get_file_breaks(current_file),
                        'file': self.get_file(current_file),
                        'name': current_file,
                        'sha512': sha512(
                            self.get_file(current_file)).hexdigest()
                    }))

                elif cmd == 'NoFile':
                    self.send('Select|%s' % dump({
                        'frame': current,
                        'breaks': self.get_file_breaks(current['file'])
                    }))

                elif cmd == 'Inspect':
                    try:
                        thing = self.obj_cache.get(int(data))
                    except Exception:
                        fail()
                        continue
                    self.send('Dump|%s' % dump({
                        'for': escape(repr(thing)),
                        'val': self.dmp(thing)}))

                elif cmd == 'Dump':
                    globals_ = dict(stack[current_index][0].f_globals)
                    try:
                        thing = eval(data, globals_, locals_[current_index])
                    except Exception:
                        fail()
                        continue
                    else:
                        self.send('Dump|%s' % dump({
                            'for': escape(u'%s ⟶ %s ' % (data, repr(thing))),
                            'val': self.dmp(thing)}))

                elif cmd == 'Trace':
                    self.send('Trace|%s' % dump({
                        'trace': trace
                    }))

                elif cmd == 'Eval':
                    redir = None
                    raw_data = data = data.strip()
                    if '!>' in data:
                        data, redir = data.split('!>')
                        data = data.strip()
                        redir = redir.strip()
                    elif data.startswith('!<'):
                        filename = data[2:].strip()
                        try:
                            with open(filename, 'r') as f:
                                data = f.read()
                        except Exception:
                            fail('Unable to read from file %s' % filename)
                            continue
                    globals_ = dict(stack[current_index][0].f_globals)
                    # Hack for function scope eval
                    globals_.update(locals_[current_index])
                    globals_.setdefault('_pprint', pprint)
                    with self.capture_output(
                            with_hook=redir is None) as (out, err):
                        try:
                            compiled_code = compile(data, '<stdin>', 'single')
                            l = locals_[current_index]
                            exec compiled_code in globals_, l
                        except Exception:
                            self.hooked = self.handle_exc()
                    if redir:
                        try:
                            with open(redir, 'w') as f:
                                f.write('\n'.join(out) + '\n'.join(err) + '\n')
                        except Exception:
                            fail('Unable to write to file %s' % redir)
                            continue
                        self.send('Print|%s' % dump({
                            'for': escape(raw_data),
                            'result': escape('Written to file %s' % redir)
                        }))
                    else:
                        self.send('Print|%s' % dump({
                            'for': escape(raw_data),
                            'result': self.hooked + escape(
                                '\n'.join(out) + '\n'.join(err))
                        }))

                elif cmd == 'Ping':
                    self.send('Pong')

                elif cmd == 'Step':
                    if hasattr(self, 'botframe'):
                        self.set_step()
                    break

                elif cmd == 'Next':
                    if hasattr(self, 'botframe'):
                        self.set_next(stack[current_index][0])
                    break

                elif cmd == 'Continue':
                    if hasattr(self, 'botframe'):
                        self.set_continue()
                    break

                elif cmd == 'Return':
                    if hasattr(self, 'botframe'):
                        self.set_return(stack[current_index][0])
                    break

                elif cmd == 'Until':
                    if hasattr(self, 'botframe'):
                        self.set_until(stack[current_index][0])
                    break

                elif cmd in ('TBreak', 'Break'):
                    break_fail = lambda x: fail(
                        'Break on %s failed' % data, message=x)
                    if ':' in data:
                        fn, lno = data.split(':')
                    else:
                        fn, lno = current['file'], data
                    cond = None
                    if ',' in lno:
                        lno, cond = lno.split(',')
                        cond = cond.lstrip()

                    try:
                        lno = int(lno)
                    except:
                        break_fail(
                            'Wrong breakpoint format must be '
                            '[file:]lno[,cond].')
                        continue

                    line = getline(
                        fn, lno, stack[current_index][0].f_globals)
                    if not line:
                        break_fail('Line does not exist')
                        continue
                    line = line.strip()
                    if ((not line or (line[0] == '#') or
                         (line[:3] == '"""') or
                         line[:3] == "'''")):
                        break_fail('Blank line or comment')
                        continue

                    first_rv = rv = self.set_break(
                        fn, lno, int(cmd == 'TBreak'), cond)
                    if rv is not None:
                        for path in sys.path:
                            rv = self.set_break(
                                os.path.join(path, fn),
                                lno, int(cmd == 'TBreak'), cond)
                            if rv is None:
                                break
                    if rv is None:
                        log.info('Break set at %s:%d [%s]' % (fn, lno, rv))
                        if fn == current['file']:
                            self.send('BreakSet|%s' % dump({
                                'lno': lno, 'cond': cond
                            }))
                        else:
                            self.send('BreakSet|%s' % dump({}))
                    else:
                        break_fail(first_rv)

                elif cmd == 'Unbreak':
                    lno = int(data)
                    current_file = current['file']
                    log.info('Break unset at %s:%d' % (current_file, lno))
                    self.clear_break(current_file, lno)
                    self.send('BreakUnset|%s' % dump({'lno': lno}))

                elif cmd == 'Jump':
                    lno = int(data)
                    if current_index != len(trace) - 1:
                        log.error('Must be at bottom frame')
                        continue

                    try:
                        stack[current_index][0].f_lineno = lno
                    except ValueError:
                        fail()
                        continue

                    trace[current_index]['lno'] = lno
                    self.send('Trace|%s' % dump({
                        'trace': trace
                    }))
                    self.send('Select|%s' % dump({
                        'frame': current,
                        'breaks': self.get_file_breaks(current['file'])
                    }))

                elif cmd == 'Complete':
                    current_file = current['file']
                    file_ = self.get_file(current_file, False).decode('utf-8')
                    lines = file_.split(u'\n')
                    lno = trace[current_index]['lno']
                    line_before = lines[lno - 1]
                    indent = len(line_before) - len(line_before.lstrip())
                    segments = data.split(u'\n')
                    for segment in reversed(segments):
                        line = u' ' * indent + segment
                        lines.insert(lno - 1, line)
                    script = Script(
                        u'\n'.join(lines), lno - 1 + len(segments),
                        len(segments[-1]) + indent, '')
                    try:
                        completions = script.complete()
                    except:
                        log.exception('Completion failed')
                        self.send('Log|%s' % dump({
                            'message': 'Completion failed for %s' %
                            '\n'.join(reversed(segments))
                        }))
                    else:
                        fun = script.get_in_function_call()
                        self.send('Suggest|%s' % dump({
                            'params': {
                                'params': [p.get_code().replace('\n', '')
                                           for p in fun.params],
                                'index': fun.index,
                                'module': fun.module.path,
                                'call_name': fun.call_name} if fun else None,
                            'completions': [{
                                'base': comp.word[
                                    :len(comp.word) - len(comp.complete)],
                                'complete': comp.complete,
                                'description': comp.description
                            } for comp in completions if comp.word.endswith(
                                comp.complete)]
                        }))

                elif cmd == 'Quit':
                    if hasattr(self, 'botframe'):
                        self.set_continue()
                        raise BdbQuit()
                    break

                else:
                    log.warn('Unknown command %s' % cmd)

            except BdbQuit:
                raise
            except Exception:
                try:
                    exc = self.handle_exc()
                    type_, value = exc_info()[:2]
                    link = ('<a href="https://github.com/Kozea/wdb/issues/new?'
                            'title=%s&body=%s&labels=defect" class="nogood">'
                            'Please click here to report it on Github</a>') % (
                                quote('%s: %s' % (type_.__name__, str(value))),
                                quote('```\n%s\n```\n' %
                                      traceback.format_exc()))
                    self.send('Echo|%s' % dump({
                        'for': escape('Error in Wdb, this is bad'),
                        'val': exc + '<br>' + link
                    }))
                except:
                    self.send('Echo|%s' % dump({
                        'for': escape('Too many errors'),
                        'val': escape("Don't really know what to say. "
                                      "Maybe it will work tomorrow.")
                    }))
                    continue
Example #14
0
    def _interaction(self, frame, tb, exception, exception_description):

        log.debug("Interaction for %r %r %r %r" % (frame, tb, exception, exception_description))
        stack, trace, current_index = self.get_trace(frame, tb)
        current = trace[current_index]
        locals_ = map(lambda x: x[0].f_locals, stack)

        if self.begun:
            self.send("Trace|%s" % dump({"trace": trace, "cwd": os.getcwd()}))
            current_file = current["file"]
            self.send(
                "Check|%s" % dump({"name": current_file, "sha512": sha512(self.get_file(current_file)).hexdigest()})
            )
        else:
            self.begun = True

        while True:
            try:
                message = self.receive()
                if "|" in message:
                    pipe = message.index("|")
                    cmd = message[:pipe]
                    data = message[pipe + 1 :]
                else:
                    cmd = message
                    data = ""

                def fail(title=None, message=None):
                    if message is None:
                        message = self.handle_exc()
                    else:
                        message = escape(message)
                    self.send("Echo|%s" % dump({"for": escape(title or "%s failed" % cmd), "val": message}))

                log.debug("Cmd %s #Data %d" % (cmd, len(data)))
                if cmd == "Start":
                    self.send("Init|%s" % dump({"cwd": os.getcwd()}))
                    self.send("Title|%s" % dump({"title": exception, "subtitle": exception_description}))
                    self.send("Trace|%s" % dump({"trace": trace}))
                    current_file = current["file"]
                    self.send(
                        "Check|%s"
                        % dump({"name": current_file, "sha512": sha512(self.get_file(current_file)).hexdigest()})
                    )

                elif cmd == "Select":
                    current_index = int(data)
                    current = trace[current_index]
                    current_file = current["file"]
                    self.send(
                        "Check|%s"
                        % dump({"name": current_file, "sha512": sha512(self.get_file(current_file)).hexdigest()})
                    )

                elif cmd == "File":
                    current_file = current["file"]
                    self.send(
                        "Select|%s"
                        % dump(
                            {
                                "frame": current,
                                "breaks": self.get_file_breaks(current_file),
                                "file": self.get_file(current_file),
                                "name": current_file,
                                "sha512": sha512(self.get_file(current_file)).hexdigest(),
                            }
                        )
                    )

                elif cmd == "NoFile":
                    self.send("Select|%s" % dump({"frame": current, "breaks": self.get_file_breaks(current["file"])}))

                elif cmd == "Inspect":
                    try:
                        thing = self.obj_cache.get(int(data))
                    except Exception:
                        fail()
                        continue
                    self.send("Dump|%s" % dump({"for": escape(repr(thing)), "val": self.dmp(thing)}))

                elif cmd == "Dump":
                    globals_ = dict(stack[current_index][0].f_globals)
                    try:
                        thing = eval(data, globals_, locals_[current_index])
                    except Exception:
                        fail()
                        continue
                    else:
                        self.send(
                            "Dump|%s" % dump({"for": escape(u"%s ⟶ %s " % (data, repr(thing))), "val": self.dmp(thing)})
                        )

                elif cmd == "Trace":
                    self.send("Trace|%s" % dump({"trace": trace}))

                elif cmd == "Eval":
                    redir = None
                    raw_data = data = data.strip()
                    if "!>" in data:
                        data, redir = data.split("!>")
                        data = data.strip()
                        redir = redir.strip()
                    elif data.startswith("!<"):
                        filename = data[2:].strip()
                        try:
                            with open(filename, "r") as f:
                                data = f.read()
                        except Exception:
                            fail("Unable to read from file %s" % filename)
                            continue
                    globals_ = dict(stack[current_index][0].f_globals)
                    # Hack for function scope eval
                    globals_.update(locals_[current_index])
                    globals_.setdefault("_pprint", pprint)
                    with self.capture_output(with_hook=redir is None) as (out, err):
                        try:
                            compiled_code = compile(data, "<stdin>", "single")
                            l = locals_[current_index]
                            exec compiled_code in globals_, l
                        except Exception:
                            self.hooked = self.handle_exc()
                    if redir:
                        try:
                            with open(redir, "w") as f:
                                f.write("\n".join(out) + "\n".join(err) + "\n")
                        except Exception:
                            fail("Unable to write to file %s" % redir)
                            continue
                        self.send(
                            "Print|%s" % dump({"for": escape(raw_data), "result": escape("Written to file %s" % redir)})
                        )
                    else:
                        self.send(
                            "Print|%s"
                            % dump(
                                {
                                    "for": escape(raw_data),
                                    "result": self.hooked + escape("\n".join(out) + "\n".join(err)),
                                }
                            )
                        )

                elif cmd == "Ping":
                    self.send("Pong")

                elif cmd == "Step":
                    if hasattr(self, "botframe"):
                        self.set_step()
                    break

                elif cmd == "Next":
                    if hasattr(self, "botframe"):
                        self.set_next(stack[current_index][0])
                    break

                elif cmd == "Continue":
                    if hasattr(self, "botframe"):
                        self.set_continue()
                    break

                elif cmd == "Return":
                    if hasattr(self, "botframe"):
                        self.set_return(stack[current_index][0])
                    break

                elif cmd == "Until":
                    if hasattr(self, "botframe"):
                        self.set_until(stack[current_index][0])
                    break

                elif cmd in ("TBreak", "Break"):
                    break_fail = lambda x: fail("Break on %s failed" % data, message=x)
                    if ":" in data:
                        fn, lno = data.split(":")
                    else:
                        fn, lno = current["file"], data
                    cond = None
                    if "," in lno:
                        lno, cond = lno.split(",")
                        cond = cond.lstrip()

                    try:
                        lno = int(lno)
                    except:
                        break_fail("Wrong breakpoint format must be " "[file:]lno[,cond].")
                        continue

                    line = getline(fn, lno, stack[current_index][0].f_globals)
                    if not line:
                        break_fail("Line does not exist")
                        continue
                    line = line.strip()
                    if not line or (line[0] == "#") or (line[:3] == '"""') or line[:3] == "'''":
                        break_fail("Blank line or comment")
                        continue

                    first_rv = rv = self.set_break(fn, lno, int(cmd == "TBreak"), cond)
                    if rv is not None:
                        for path in sys.path:
                            rv = self.set_break(os.path.join(path, fn), lno, int(cmd == "TBreak"), cond)
                            if rv is None:
                                break
                    if rv is None:
                        log.info("Break set at %s:%d [%s]" % (fn, lno, rv))
                        if fn == current["file"]:
                            self.send("BreakSet|%s" % dump({"lno": lno, "cond": cond}))
                        else:
                            self.send("BreakSet|%s" % dump({}))
                    else:
                        break_fail(first_rv)

                elif cmd == "Unbreak":
                    lno = int(data)
                    current_file = current["file"]
                    log.info("Break unset at %s:%d" % (current_file, lno))
                    self.clear_break(current_file, lno)
                    self.send("BreakUnset|%s" % dump({"lno": lno}))

                elif cmd == "Jump":
                    lno = int(data)
                    if current_index != len(trace) - 1:
                        log.error("Must be at bottom frame")
                        continue

                    try:
                        stack[current_index][0].f_lineno = lno
                    except ValueError:
                        fail()
                        continue

                    trace[current_index]["lno"] = lno
                    self.send("Trace|%s" % dump({"trace": trace}))
                    self.send("Select|%s" % dump({"frame": current, "breaks": self.get_file_breaks(current["file"])}))

                elif cmd == "Complete":
                    current_file = current["file"]
                    file_ = self.get_file(current_file, False).decode("utf-8")
                    lines = file_.split(u"\n")
                    lno = trace[current_index]["lno"]
                    line_before = lines[lno - 1]
                    indent = len(line_before) - len(line_before.lstrip())
                    segments = data.split(u"\n")
                    for segment in reversed(segments):
                        line = u" " * indent + segment
                        lines.insert(lno - 1, line)
                    script = Script(u"\n".join(lines), lno - 1 + len(segments), len(segments[-1]) + indent, "")
                    try:
                        completions = script.complete()
                    except:
                        log.exception("Completion failed")
                        self.send(
                            "Log|%s" % dump({"message": "Completion failed for %s" % "\n".join(reversed(segments))})
                        )
                    else:
                        fun = script.get_in_function_call()
                        self.send(
                            "Suggest|%s"
                            % dump(
                                {
                                    "params": {
                                        "params": [p.get_code().replace("\n", "") for p in fun.params],
                                        "index": fun.index,
                                        "module": fun.module.path,
                                        "call_name": fun.call_name,
                                    }
                                    if fun
                                    else None,
                                    "completions": [
                                        {
                                            "base": comp.word[: len(comp.word) - len(comp.complete)],
                                            "complete": comp.complete,
                                            "description": comp.description,
                                        }
                                        for comp in completions
                                        if comp.word.endswith(comp.complete)
                                    ],
                                }
                            )
                        )

                elif cmd == "Quit":
                    if hasattr(self, "botframe"):
                        self.set_continue()
                        raise BdbQuit()
                    break

                else:
                    log.warn("Unknown command %s" % cmd)

            except BdbQuit:
                raise
            except Exception:
                try:
                    exc = self.handle_exc()
                    type_, value = exc_info()[:2]
                    link = (
                        '<a href="https://github.com/Kozea/wdb/issues/new?'
                        'title=%s&body=%s&labels=defect" class="nogood">'
                        "Please click here to report it on Github</a>"
                    ) % (
                        quote("%s: %s" % (type_.__name__, str(value))),
                        quote("```\n%s\n```\n" % traceback.format_exc()),
                    )
                    self.send(
                        "Echo|%s" % dump({"for": escape("Error in Wdb, this is bad"), "val": exc + "<br>" + link})
                    )
                except:
                    self.send(
                        "Echo|%s"
                        % dump(
                            {
                                "for": escape("Too many errors"),
                                "val": escape("Don't really know what to say. " "Maybe it will work tomorrow."),
                            }
                        )
                    )
                    continue
Example #15
0
from operator import abs

from jedi import Script

s = Script(code="""def mula(muusa:int, puusa:str) -> str:
    x = "dadocstring"
    return puusa
musi = 3
suur = 4
mula(mu""")
comps = s.complete(line=6, column=7, fuzzy=True)

ii = 3

ss = "asdf"


def kala(x: int):
    return x


def showsig(x):
    if not x:
        return x
    return x[0].to_string() + "(%s, %s)" % (x[0].index, x[0].bracket_start)


for comp in comps:
    print(comp,
          comp.type,
          showsig(comp.get_signatures()),
Example #16
0
    def _interaction(
            self, frame, tb,
            exception, exception_description):
        log.debug('Interaction for %r %r %r %r' % (
            frame, tb, exception, exception_description))
        stack, trace, current_index = self.get_trace(frame, tb)
        current = trace[current_index]
        locals_ = map(lambda x: x[0].f_locals, stack)

        if self.begun:
            self.send('Trace|%s' % dump({
                'trace': trace,
                'cwd': os.getcwd()
            }))
            current_file = current['file']
            self.send('Check|%s' % dump({
                'name': current_file,
                'sha512': sha512(self.get_file(current_file)).hexdigest()
            }))
        else:
            self.begun = True

        while True:
            message = self.receive()
            if '|' in message:
                pipe = message.index('|')
                cmd = message[:pipe]
                data = message[pipe + 1:]
            else:
                cmd = message
                data = ''

            log.debug('Cmd %s #Data %d' % (cmd, len(data)))
            if cmd == 'Start':
                self.send('Init|%s' % dump({
                    'cwd': os.getcwd()
                }))
                self.send('Title|%s' % dump({
                    'title': exception,
                    'subtitle': exception_description
                }))
                self.send('Trace|%s' % dump({
                    'trace': trace
                }))
                current_file = current['file']
                self.send('Check|%s' % dump({
                    'name': current_file,
                    'sha512': sha512(self.get_file(current_file)).hexdigest()
                }))

            elif cmd == 'Select':
                current_index = int(data)
                current = trace[current_index]
                current_file = current['file']
                self.send('Check|%s' % dump({
                    'name': current_file,
                    'sha512': sha512(self.get_file(current_file)).hexdigest()
                }))

            elif cmd == 'File':
                current_file = current['file']
                self.send('Select|%s' % dump({
                    'frame': current,
                    'breaks': self.get_file_breaks(current_file),
                    'file': self.get_file(current_file),
                    'name': current_file,
                    'sha512': sha512(self.get_file(current_file)).hexdigest()
                }))

            elif cmd == 'NoFile':
                self.send('Select|%s' % dump({
                    'frame': current,
                    'breaks': self.get_file_breaks(current['file'])
                }))

            elif cmd == 'Inspect':
                try:
                    thing = reverse_id(int(data))
                except:
                    continue
                self.send('Dump|%s' % dump({
                    'for': escape(repr(thing)),
                    'val': dmp(thing)}))

            elif cmd == 'Dump':
                globals_ = dict(stack[current_index][0].f_globals)
                try:
                    thing = eval(data, globals_, locals_[current_index])
                except:
                    continue

                self.send('Dump|%s' % dump({
                    'for': escape(repr(thing)),
                    'val': dmp(thing)}))

            elif cmd == 'Trace':
                self.send('Trace|%s' % dump({
                    'trace': trace
                }))

            elif cmd == 'Eval':
                globals_ = dict(stack[current_index][0].f_globals)
                # Hack for function scope eval
                globals_.update(locals_[current_index])
                globals_.setdefault('_pprint', pprint)
                globals_.setdefault('_dump', dmp)
                with capture_output() as (out, err):
                    try:
                        compiled_code = compile(data, '<stdin>', 'single')
                        exec compiled_code in globals_, locals_[current_index]
                    except Exception:
                        type_, value, tb = exc_info()
                        print '%s: %s' % (type_.__name__, str(value))
                self.send('Print|%s' % dump({
                    'result': escape('\n'.join(out) + '\n'.join(err))
                }))

            elif cmd == 'Ping':
                self.send('Pong')

            elif cmd == 'Step':
                if hasattr(self, 'botframe'):
                    self.set_step()
                break

            elif cmd == 'Next':
                if hasattr(self, 'botframe'):
                    self.set_next(stack[current_index][0])
                break

            elif cmd == 'Continue':
                if hasattr(self, 'botframe'):
                    self.set_continue()
                break

            elif cmd == 'Return':
                if hasattr(self, 'botframe'):
                    self.set_return(stack[current_index][0])
                break

            elif cmd == 'Until':
                if hasattr(self, 'botframe'):
                    self.set_until(stack[current_index][0])
                break

            elif cmd in ('TBreak', 'Break'):
                if ':' in data:
                    fn, lno = data.split(':')
                else:
                    fn, lno = current['file'], data
                cond = None
                if ',' in lno:
                    lno, cond = lno.split(',')
                    cond = cond.lstrip()

                lno = int(lno)
                rv = self.set_break(fn, lno, int(cmd == 'TBreak'), cond)
                if rv is not None:
                    for path in sys.path:
                        rv = self.set_break(
                            os.path.join(path, fn),
                            lno, int(cmd == 'TBreak'), cond)
                        if rv is None:
                            break
                if rv is None:
                    log.info('Break set at %s:%d [%s]' % (fn, lno, rv))
                    if fn == current['file']:
                        self.send('BreakSet|%s' % dump({
                            'lno': lno, 'cond': cond
                        }))
                    else:
                        self.send('BreakSet|%s' % dump({}))
                else:
                    self.send('Log|%s' % dump({
                        'message': rv
                    }))

            elif cmd == 'Unbreak':
                lno = int(data)
                current_file = current['file']
                log.info('Break unset at %s:%d' % (current_file, lno))
                self.clear_break(current_file, lno)
                self.send('BreakUnset|%s' % dump({'lno': lno}))

            elif cmd == 'Jump':
                lno = int(data)
                if current_index != len(trace) - 1:
                    log.error('Must be at bottom frame')
                    continue

                try:
                    stack[current_index][0].f_lineno = lno
                except ValueError:
                    log.error('Jump failed')
                    continue

                trace[current_index]['lno'] = lno
                self.send('Trace|%s' % dump({
                    'trace': trace
                }))
                self.send('Select|%s' % dump({
                    'frame': current,
                    'breaks': self.get_file_breaks(current['file'])
                }))

            elif cmd == 'Complete':
                current_file = current['file']
                file_ = self.get_file(current_file, False).decode('utf-8')
                lines = file_.split(u'\n')
                lno = trace[current_index]['lno']
                line_before = lines[lno - 1]
                indent = len(line_before) - len(line_before.lstrip())
                segments = data.split(u'\n')
                for segment in reversed(segments):
                    line = u' ' * indent + segment
                    lines.insert(lno - 1, line)
                script = Script(
                    u'\n'.join(lines), lno - 1 + len(segments),
                    len(segments[-1]) + indent, '')
                try:
                    completions = script.complete()
                except:
                    self.send('Log|%s' % dump({
                        'message': 'Completion failed for %s' %
                        '\n'.join(reversed(segments))
                    }))
                else:
                    fun = script.get_in_function_call()
                    self.send('Suggest|%s' % dump({
                        'params': {
                            'params': [p.get_code().replace('\n', '')
                                       for p in fun.params],
                            'index': fun.index,
                            'module': fun.module.path,
                            'call_name': fun.call_name} if fun else None,
                        'completions': [{
                            'base': comp.word[
                                :len(comp.word) - len(comp.complete)],
                            'complete': comp.complete,
                            'description': comp.description
                        } for comp in completions if comp.word.endswith(
                            comp.complete)]
                    }))

            elif cmd == 'Quit':
                if hasattr(self, 'botframe'):
                    self.set_continue()
                    raise BdbQuit()
                break

            else:
                log.warn('Unknown command %s' % cmd)
Example #17
0
from operator import abs

from jedi import Script
code = """print(end"""
s = Script(code=code)
comps = s.complete(line=1, column=len(code), fuzzy=True)

for comp in comps:
    print("--------------------------")
    """
    for name in dir(comp):
        if not name.startswith("_"):
            print(name, ":", getattr(comp, name))
    """
    print(comp.name, comp.get_completion_prefix_length())