Exemple #1
0
def test_load_newer_grammar():
    # This version shouldn't be out for a while, but if we somehow get this it
    # should just take the latest Python grammar.
    load_grammar('15.8')
    # The same is true for very old grammars (even though this is probably not
    # going to be an issue.
    load_grammar('1.5')
Exemple #2
0
def test_load_newer_grammar():
    # This version shouldn't be out for a while, but if we somehow get this it
    # should just take the latest Python grammar.
    load_grammar('15.8')
    # The same is true for very old grammars (even though this is probably not
    # going to be an issue.
    load_grammar('1.5')
Exemple #3
0
def test_modulepickling_change_cache_dir(monkeypatch, tmpdir):
    """
    ParserPickling should not save old cache when cache_directory is changed.

    See: `#168 <https://github.com/davidhalter/jedi/pull/168>`_
    """
    dir_1 = str(tmpdir.mkdir('first'))
    dir_2 = str(tmpdir.mkdir('second'))

    item_1 = _NodeCacheItem('bla', [])
    item_2 = _NodeCacheItem('bla', [])
    path_1 = 'fake path 1'
    path_2 = 'fake path 2'

    monkeypatch.setattr(settings, 'cache_directory', dir_1)
    grammar = load_grammar()
    _save_to_file_system(grammar, path_1, item_1)
    parser_cache.clear()
    cached = load_stored_item(grammar, path_1, item_1)
    assert cached == item_1.node

    monkeypatch.setattr(settings, 'cache_directory', dir_2)
    _save_to_file_system(grammar, path_2, item_2)
    cached = load_stored_item(grammar, path_1, item_1)
    assert cached is None
Exemple #4
0
def test_modulepickling_simulate_deleted_cache(tmpdir):
    """
    Tests loading from a cache file after it is deleted.
    According to macOS `dev docs`__,

        Note that the system may delete the Caches/ directory to free up disk
        space, so your app must be able to re-create or download these files as
        needed.

    It is possible that other supported platforms treat cache files the same
    way.

    __ https://developer.apple.com/library/content/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html
    """
    grammar = load_grammar()
    module = 'fake parser'

    # Create the file
    path = tmpdir.dirname + '/some_path'
    with open(path, 'w'):
        pass

    save_module(grammar, path, module, [])
    assert load_module(grammar, path) == module

    unlink(_get_hashed_path(grammar, path))
    parser_cache.clear()

    cached2 = load_module(grammar, path)
    assert cached2 is None
class Differ(object):
    grammar = load_grammar()

    def initialize(self, code):
        debug.dbg('differ: initialize', color='YELLOW')
        self.lines = splitlines(code, keepends=True)
        parser_cache.pop(None, None)
        self.module = parse(code, diff_cache=True, cache=True)
        return self.module

    def parse(self, code, copies=0, parsers=0, expect_error_leaves=False):
        debug.dbg('differ: parse copies=%s parsers=%s',
                  copies,
                  parsers,
                  color='YELLOW')
        lines = splitlines(code, keepends=True)
        diff_parser = DiffParser(self.grammar, self.module)
        new_module = diff_parser.update(self.lines, lines)
        self.lines = lines
        assert code == new_module.get_code()
        assert diff_parser._copy_count == copies
        assert diff_parser._parser_count == parsers

        assert expect_error_leaves == _check_error_leaves_nodes(new_module)
        _assert_valid_graph(new_module)
        return new_module
def test_modulepickling_change_cache_dir(monkeypatch, tmpdir):
    """
    ParserPickling should not save old cache when cache_directory is changed.

    See: `#168 <https://github.com/davidhalter/jedi/pull/168>`_
    """
    dir_1 = str(tmpdir.mkdir('first'))
    dir_2 = str(tmpdir.mkdir('second'))

    item_1 = _NodeCacheItem('bla', [])
    item_2 = _NodeCacheItem('bla', [])
    path_1 = 'fake path 1'
    path_2 = 'fake path 2'

    monkeypatch.setattr(settings, 'cache_directory', dir_1)
    grammar = load_grammar()
    _save_to_file_system(grammar, path_1, item_1)
    parser_cache.clear()
    cached = load_stored_item(grammar, path_1, item_1)
    assert cached == item_1.node

    monkeypatch.setattr(settings, 'cache_directory', dir_2)
    _save_to_file_system(grammar, path_2, item_2)
    cached = load_stored_item(grammar, path_1, item_1)
    assert cached is None
def test_modulepickling_simulate_deleted_cache(tmpdir):
    """
    Tests loading from a cache file after it is deleted.
    According to macOS `dev docs`__,

        Note that the system may delete the Caches/ directory to free up disk
        space, so your app must be able to re-create or download these files as
        needed.

    It is possible that other supported platforms treat cache files the same
    way.

    __ https://developer.apple.com/library/content/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html
    """
    grammar = load_grammar()
    module = 'fake parser'

    # Create the file
    path = tmpdir.dirname + '/some_path'
    with open(path, 'w'):
        pass

    save_module(grammar, path, module, [])
    assert load_module(grammar, path) == module

    unlink(_get_hashed_path(grammar, path))
    parser_cache.clear()

    cached2 = load_module(grammar, path)
    assert cached2 is None
def test_sys_path_with_modifications():
    code = dedent("""
        import os
    """)

    path = os.path.abspath(os.path.join(os.curdir, 'module_name.py'))
    grammar = load_grammar()
    module_node = parse(code, path=path)
    module_context = ModuleContext(Evaluator(grammar), module_node, path=path)
    paths = sys_path_with_modifications(module_context.evaluator, module_context)
    assert '/tmp/.buildout/eggs/important_package.egg' in paths
Exemple #9
0
 def check(src, result):
     # Python 2 tuple params should be ignored for now.
     grammar = load_grammar('%s.%s' % sys.version_info[:2])
     m = parse(src, grammar=grammar)
     if is_py3:
         assert not m.subscopes
     else:
         # We don't want b and c to be a part of the param enumeration. Just
         # ignore them, because it's not what we want to support in the
         # future.
         assert [str(param.name) for param in m.subscopes[0].params] == result
Exemple #10
0
 def check(src, result):
     # Python 2 tuple params should be ignored for now.
     grammar = load_grammar('%s.%s' % sys.version_info[:2])
     m = parse(src, grammar=grammar)
     if is_py3:
         assert not list(m.iter_funcdefs())
     else:
         # We don't want b and c to be a part of the param enumeration. Just
         # ignore them, because it's not what we want to support in the
         # future.
         assert [param.name.value for param in next(m.iter_funcdefs()).params] == result
Exemple #11
0
    def __init__(self,
                 source=None,
                 line=None,
                 column=None,
                 path=None,
                 encoding='utf-8',
                 source_path=None,
                 source_encoding=None,
                 sys_path=None):
        if source_path is not None:
            warnings.warn(
                "Deprecated since version 0.7. Use path instead of source_path.",
                DeprecationWarning,
                stacklevel=2)
            path = source_path
        if source_encoding is not None:
            warnings.warn(
                "Deprecated since version 0.8. Use encoding instead of source_encoding.",
                DeprecationWarning,
                stacklevel=2)
            encoding = source_encoding

        self._orig_path = path
        # An empty path (also empty string) should always result in no path.
        self.path = os.path.abspath(path) if path else None

        if source is None:
            # TODO add a better warning than the traceback!
            with open(path, 'rb') as f:
                source = f.read()

        self._source = common.source_to_unicode(source, encoding)
        self._code_lines = common.splitlines(self._source)
        line = max(len(self._code_lines), 1) if line is None else line
        if not (0 < line <= len(self._code_lines)):
            raise ValueError('`line` parameter is not in a valid range.')

        line_len = len(self._code_lines[line - 1])
        column = line_len if column is None else column
        if not (0 <= column <= line_len):
            raise ValueError('`column` parameter is not in a valid range.')
        self._pos = line, column
        self._path = path

        cache.clear_time_caches()
        debug.reset_time()
        self._grammar = load_grammar(version='%s.%s' % sys.version_info[:2])
        if sys_path is None:
            venv = os.getenv('VIRTUAL_ENV')
            if venv:
                sys_path = list(get_venv_path(venv))
        self._evaluator = Evaluator(self._grammar, sys_path=sys_path)
        debug.speed('init')
Exemple #12
0
def test_sys_path_with_modifications():
    code = dedent("""
        import os
    """)

    path = os.path.abspath(os.path.join(os.curdir, 'module_name.py'))
    grammar = load_grammar()
    module_node = parse(code, path=path)
    module_context = ModuleContext(Evaluator(grammar), module_node, path=path)
    paths = sys_path_with_modifications(module_context.evaluator,
                                        module_context)
    assert '/tmp/.buildout/eggs/important_package.egg' in paths
Exemple #13
0
 def check(src, result):
     # Python 2 tuple params should be ignored for now.
     grammar = load_grammar('%s.%s' % sys.version_info[:2])
     m = parse(src, grammar=grammar)
     if is_py3:
         assert not list(m.iter_funcdefs())
     else:
         # We don't want b and c to be a part of the param enumeration. Just
         # ignore them, because it's not what we want to support in the
         # future.
         assert [
             param.name.value for param in next(m.iter_funcdefs()).params
         ] == result
def main(args):
    if args['--debug']:
        jedi.set_debug_function(notices=True)

    with open(args['<file>']) as f:
        code = f.read()
    grammar = load_grammar()
    parser = ParserWithRecovery(grammar, u(code))
    # Make sure used_names is loaded
    parser.module.used_names

    code =  code + '\na\n'  # Add something so the diff parser needs to run.
    lines = splitlines(code, keepends=True)
    cProfile.runctx('run(parser, lines)', globals(), locals(), sort=args['-s'])
Exemple #15
0
def main(args):
    if args['--debug']:
        jedi.set_debug_function(notices=True)

    with open(args['<file>']) as f:
        code = f.read()
    grammar = load_grammar()
    parser = ParserWithRecovery(grammar, code)
    # Make sure used_names is loaded
    parser.module.used_names

    code = code + '\na\n'  # Add something so the diff parser needs to run.
    lines = splitlines(code, keepends=True)
    cProfile.runctx('run(parser, lines)', globals(), locals(), sort=args['-s'])
def test_modulepickling_delete_incompatible_cache():
    item = _NodeCacheItem('fake parser', [])
    path = 'fake path'

    cache1 = ParserPicklingCls()
    cache1.version = 1
    grammar = load_grammar()
    cache1.save_item(grammar, path, item)
    cached1 = load_stored_item(grammar, cache1, path, item)
    assert cached1 == item.node

    cache2 = ParserPicklingCls()
    cache2.version = 2
    cached2 = load_stored_item(grammar, cache2, path, item)
    assert cached2 is None
Exemple #17
0
def test_modulepickling_delete_incompatible_cache():
    item = _NodeCacheItem('fake parser', [])
    path = 'fake path'

    cache1 = ParserPicklingCls()
    cache1.version = 1
    grammar = load_grammar()
    cache1.save_item(grammar, path, item)
    cached1 = load_stored_item(grammar, cache1, path, item)
    assert cached1 == item.node

    cache2 = ParserPicklingCls()
    cache2.version = 2
    cached2 = load_stored_item(grammar, cache2, path, item)
    assert cached2 is None
Exemple #18
0
    def __init__(self, source=None, line=None, column=None, path=None,
                 encoding='utf-8', source_path=None, source_encoding=None,
                 sys_path=None):
        if source_path is not None:
            warnings.warn("Deprecated since version 0.7. Use path instead of source_path.", DeprecationWarning, stacklevel=2)
            path = source_path
        if source_encoding is not None:
            warnings.warn("Deprecated since version 0.8. Use encoding instead of source_encoding.", DeprecationWarning, stacklevel=2)
            encoding = source_encoding

        self._orig_path = path
        # An empty path (also empty string) should always result in no path.
        self.path = os.path.abspath(path) if path else None

        if source is None:
            # TODO add a better warning than the traceback!
            with open(path, 'rb') as f:
                source = f.read()

        self._source = common.source_to_unicode(source, encoding)
        self._code_lines = common.splitlines(self._source)
        line = max(len(self._code_lines), 1) if line is None else line
        if not (0 < line <= len(self._code_lines)):
            raise ValueError('`line` parameter is not in a valid range.')

        line_len = len(self._code_lines[line - 1])
        column = line_len if column is None else column
        if not (0 <= column <= line_len):
            raise ValueError('`column` parameter is not in a valid range.')
        self._pos = line, column
        self._path = path

        cache.clear_time_caches()
        debug.reset_time()
        self._grammar = load_grammar(version='%s.%s' % sys.version_info[:2])
        if sys_path is None:
            venv = os.getenv('VIRTUAL_ENV')
            if venv:
                sys_path = list(get_venv_path(venv))
        self._evaluator = Evaluator(self._grammar, sys_path=sys_path)
        debug.speed('init')
def parse(code, version='3.4'):
    code = dedent(code) + "\n\n"
    grammar = load_grammar(version=version)
    return _parse(code, grammar=grammar, error_recovery=False)
Exemple #20
0
def check_module_test(code):
    grammar = load_grammar()
    module_context = ModuleContext(Evaluator(grammar), parse(code), path=None)
    return _check_module(module_context)
Exemple #21
0
def _evaluator():
    return Evaluator(load_grammar())
Exemple #22
0
def parse(code, version='3.4'):
    code = dedent(code) + "\n\n"
    grammar = load_grammar(version=version)
    return _parse(code, grammar=grammar, error_recovery=False)
def check_module_test(code):
    grammar = load_grammar()
    module_context = ModuleContext(Evaluator(grammar), parse(code), path=None)
    return _check_module(module_context)