Esempio n. 1
0
def _all_vars_in_module(module_name):
    if module_name is None:
        return []

    # If imported module, use dir.
    if module_name in sys.modules:
        res = dir(sys.modules[module_name])
    else:
        p = resolve_module_path(module_name)
        if p is None:
            # If resolving fails, use module_completion.
            return module_completion("from %s import " % (module_name, ))
        res = None
        if os.path.isdir(p):
            res = list_modules_in(p)

        # Ohterwise, parse the file.
        if res is None:
            res = []
            regexp = re.compile(
                "^{name} *= *|^def +{name}|^class +{name}".format(
                    name="([a-zA-Z0-9_]+)"))
            with open(p) as f:
                for l in f:
                    m = regexp.match(l)
                    if m is not None:
                        res.extend([c for c in m.groups() if c is not None])
    res = [a for a in res if special_att_regexp.match(a) is None]
    return res
Esempio n. 2
0
def _all_vars_in_module(module_name):
    if module_name is None:
        return []

    # If imported module, use dir.
    if module_name in sys.modules:
        res = dir(sys.modules[module_name])
    else:
        p = resolve_module_path(module_name)
        if p is None:
            # If resolving fails, use module_completion.
            return module_completion("from %s import " % (module_name, ))
        res = None
        if os.path.isdir(p):
            res = list_modules_in(p)

        # Ohterwise, parse the file.
        if res is None:
            res = []
            regexp = re.compile(
                "^{name} *= *|^def +{name}|^class +{name}".format(
                    name="([a-zA-Z0-9_]+)"))
            with open(p) as f:
                for l in f:
                    m = regexp.match(l)
                    if m is not None:
                        res.extend([c for c in m.groups() if c is not None])
    res = [a for a in res if special_att_regexp.match(a) is None]
    return res
Esempio n. 3
0
def _all_modules(module_name):
    if module_name is None:
        return list_modules_in_syspath()
    else:
        return [
            a.split(".")[-1]
            for a in module_completion("import %s." % (module_name, ))
        ]
Esempio n. 4
0
def test_valid_exported_submodules():
    """
    Test checking exported (__all__) objects are submodules
    """
    results = module_completion("import os.pa")
    # ensure we get a valid submodule:
    assert "os.path" in results
    # ensure we don't get objects that aren't submodules:
    assert "os.pathconf" not in results
Esempio n. 5
0
def test_bad_module_all():
    """Test module with invalid __all__

    https://github.com/ipython/ipython/issues/9678
    """
    testsdir = os.path.dirname(__file__)
    sys.path.insert(0, testsdir)
    try:
        results = module_completion("from bad_all import ")
        assert "puppies" in results
        for r in results:
            assert isinstance(r, str)

        # bad_all doesn't contain submodules, but this completion
        # should finish without raising an exception:
        results = module_completion("import bad_all.")
        assert results == []
    finally:
        sys.path.remove(testsdir)
Esempio n. 6
0
def _all_modules(module_name):
    if module_name is None:
        return list_modules_in_syspath()
    else:
        # Prefer naive implementation because module_completion seem to have
        # side effect.
        mod_path = resolve_module_path(module_name)
        if mod_path is None:
            return [
                a.split(".")[-1]
                for a in module_completion("import %s." % (module_name, ))
            ]
        else:
            return list_modules_in(mod_path)
def test_bad_module_all():
    """Test module with invalid __all__

    https://github.com/ipython/ipython/issues/9678
    """
    testsdir = os.path.dirname(__file__)
    sys.path.insert(0, testsdir)
    try:
        results = module_completion('from bad_all import ')
        nt.assert_in('puppies', results)
        for r in results:
            nt.assert_is_instance(r, str)
    finally:
        sys.path.remove(testsdir)
Esempio n. 8
0
def test_bad_module_all():
    """Test module with invalid __all__

    https://github.com/ipython/ipython/issues/9678
    """
    testsdir = os.path.dirname(__file__)
    sys.path.insert(0, testsdir)
    try:
        results = module_completion('from bad_all import ')
        nt.assert_in('puppies', results)
        for r in results:
            nt.assert_is_instance(r, py3compat.string_types)
    finally:
        sys.path.remove(testsdir)
Esempio n. 9
0
def test_import_invalid_module():
    """Testing of issue https://github.com/ipython/ipython/issues/1107"""
    invalid_module_names = {'foo-bar', 'foo:bar', '10foo'}
    valid_module_names = {'foobar'}
    with TemporaryDirectory() as tmpdir:
        sys.path.insert( 0, tmpdir )
        for name in invalid_module_names | valid_module_names:
            filename = os.path.join(tmpdir, name + '.py')
            open(filename, 'w').close()

        s = set( module_completion('import foo') )
        intersection = s.intersection(invalid_module_names)
        nt.assert_equal(intersection, set())

        assert valid_module_names.issubset(s), valid_module_names.intersection(s)
    def test_import_invalid_module(self):
        """Testing of issue https://github.com/ipython/ipython/issues/1107"""
        invalid_module_names = set(['foo-bar', 'foo:bar', '10foo'])
        valid_module_names = set(['foobar'])
        with TemporaryDirectory() as tmpdir:
            sys.path.insert( 0, tmpdir )
            for name in invalid_module_names | valid_module_names:
                filename = os.path.join(tmpdir, name + '.py')
                open(filename, 'w').close()

            s = set( module_completion('import foo') )
            intersection = s.intersection(invalid_module_names)
            self.assertFalse(intersection, intersection)
            
            assert valid_module_names.issubset(s), valid_module_names.intersection(s)
Esempio n. 11
0
def list_modules_in_syspath():
    return module_completion("import ")
Esempio n. 12
0
def _all_modules(module_name):
    if module_name is None:
        return list_modules_in_syspath()
    else:
        return [a.split(".")[-1] for a in
                module_completion("import %s." % (module_name, ))]
Esempio n. 13
0
def list_modules_in_syspath():
    return module_completion("import ")