コード例 #1
0
def _load_faked_module(module):
    module_name = module.__name__
    if module_name == '__builtin__' and not is_py3:
        module_name = 'builtins'

    try:
        return modules[module_name]
    except KeyError:
        path = os.path.dirname(os.path.abspath(__file__))
        try:
            with open(os.path.join(path, 'fake', module_name) + '.pym') as f:
                source = f.read()
        except IOError:
            modules[module_name] = None
            return
        grammar = load_grammar('grammar3.4')
        module = Parser(grammar, unicode(source), module_name).module
        modules[module_name] = module

        if module_name == 'builtins' and not is_py3:
            # There are two implementations of `open` for either python 2/3.
            # -> Rename the python2 version (`look at fake/builtins.pym`).
            open_func = search_scope(module, 'open')
            open_func.children[1] = FakeName('open_python3')
            open_func = search_scope(module, 'open_python2')
            open_func.children[1] = FakeName('open')
        return module
コード例 #2
0
ファイル: sys_path.py プロジェクト: alx901/dotfiles
def sys_path_with_modifications(evaluator, module):
    if module.path is None:
        # Support for modules without a path is bad, therefore return the
        # normal path.
        return list(get_sys_path())

    curdir = os.path.abspath(os.curdir)
    with common.ignored(OSError):
        os.chdir(os.path.dirname(module.path))

    result = _check_module(evaluator, module)
    result += _detect_django_path(module.path)
    # buildout scripts often contain the same sys.path modifications
    # the set here is used to avoid duplicate sys.path entries
    buildout_paths = set()
    for module_path in _get_buildout_scripts(module.path):
        try:
            with open(module_path, 'rb') as f:
                source = f.read()
        except IOError:
            pass
        else:
            p = Parser(evaluator.grammar, common.source_to_unicode(source),
                       module_path)
            for path in _check_module(p.module):
                if path not in buildout_paths:
                    buildout_paths.add(path)
                    result.append(path)
    # cleanup, back to old directory
    os.chdir(curdir)
    return list(result)
コード例 #3
0
def test_path_from_invalid_sys_path_assignment():
    SRC = u("""
import sys
sys.path = 'invalid'""")
    p = Parser(SRC)
    paths = _check_module(p.module)
    assert len(paths) > 0
    assert 'invalid' not in paths
コード例 #4
0
ファイル: test_tokenize.py プロジェクト: meteor5118/myvim
 def test_end_pos_multi_line(self):
     parsed = Parser(load_grammar(), dedent(u('''
     def testit():
         a = """huhu
     asdfasdf""" + "h"
     ''')))
     tok = parsed.module.subscopes[0].statements[0].children[2].children[0]
     assert tok.end_pos == (4, 11)
コード例 #5
0
def test_sys_path_with_modifications():
    SRC = dedent(u("""
        import os
    """))
    grammar = load_grammar()
    p = Parser(grammar, SRC)
    p.module.path = os.path.abspath(os.path.join(os.curdir, 'module_name.py'))
    paths = sys_path_with_modifications(Evaluator(grammar), p.module)
    assert '/tmp/.buildout/eggs/important_package.egg' in paths
コード例 #6
0
def test_append_on_non_sys_path():
    SRC = u("""
class Dummy(object):
    path = []

d = Dummy()
d.path.append('foo')""")
    p = Parser(SRC)
    paths = _check_module(p.module)
    assert len(paths) > 0
    assert 'foo' not in paths
コード例 #7
0
def assert_params(param_string, **wanted_dct):
    source = dedent('''
    def x(%s):
        pass
    ''') % param_string

    parser = Parser(load_grammar(), dedent(source))
    funcdef = parser.get_parsed_node().subscopes[0]
    dct = dict((p.name.value, p.default and p.default.get_code())
               for p in funcdef.params)
    assert dct == wanted_dct
    assert parser.get_parsed_node().get_code() == source
コード例 #8
0
def test_append_on_non_sys_path():
    SRC = dedent(
        u("""
        class Dummy(object):
            path = []

        d = Dummy()
        d.path.append('foo')"""))
    grammar = load_grammar()
    p = Parser(grammar, SRC)
    paths = _check_module(Evaluator(grammar), p.module)
    assert len(paths) > 0
    assert 'foo' not in paths
コード例 #9
0
 def _get_under_cursor_stmt(self, cursor_txt):
     tokenizer = source_tokens(cursor_txt, self._pos[0] - 1)
     r = Parser(cursor_txt, no_docstr=True, tokenizer=tokenizer)
     try:
         # Take the last statement available.
         stmt = r.module.statements[-1]
     except IndexError:
         raise NotFoundError()
     # Set the start_pos to a pseudo position, that doesn't exist but works
     # perfectly well (for both completions in docstrings and statements).
     stmt.start_pos = self._pos
     stmt.parent = self._parser.user_scope()
     return stmt
コード例 #10
0
def test_path_from_sys_path_assignment():
    SRC = u("""
#!/usr/bin/python

import sys
sys.path[0:0] = [
  '/usr/lib/python3.4/site-packages',
  '/home/test/.buildout/eggs/important_package.egg'
  ]

path[0:0] = [1]

import important_package

if __name__ == '__main__':
    sys.exit(important_package.main())""")
    p = Parser(SRC)
    paths = _check_module(p.module)
    assert 1 not in paths
    assert '/home/test/.buildout/eggs/important_package.egg' in paths