File: preload_imports.py

package info (click to toggle)
python-lsp-server 1.12.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 796 kB
  • sloc: python: 7,791; sh: 12; makefile: 4
file content (79 lines) | stat: -rw-r--r-- 1,530 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# Copyright 2017-2020 Palantir Technologies, Inc.
# Copyright 2021- Python Language Server Contributors.

import logging

from pylsp import hookimpl

log = logging.getLogger(__name__)

MODULES = [
    "OpenGL",
    "PIL",
    "array",
    "audioop",
    "binascii",
    "cPickle",
    "cStringIO",
    "cmath",
    "collections",
    "datetime",
    "errno",
    "exceptions",
    "gc",
    "imageop",
    "imp",
    "itertools",
    "marshal",
    "math",
    "matplotlib",
    "mmap",
    "mpmath",
    "msvcrt",
    "networkx",
    "nose",
    "nt",
    "numpy",
    "operator",
    "os",
    "os.path",
    "pandas",
    "parser",
    "rgbimg",
    "scipy",
    "signal",
    "skimage",
    "sklearn",
    "statsmodels",
    "strop",
    "sympy",
    "sys",
    "thread",
    "time",
    "wx",
    "xxsubtype",
    "zipimport",
    "zlib",
]


@hookimpl
def pylsp_settings():
    # Setup default modules to preload, and rope extension modules
    return {
        "plugins": {"preload": {"modules": MODULES}},
        "rope": {"extensionModules": MODULES},
    }


@hookimpl
def pylsp_initialize(config) -> None:
    for mod_name in config.plugin_settings("preload").get("modules", []):
        try:
            __import__(mod_name)
            log.debug("Preloaded module %s", mod_name)
        except Exception:
            # Catch any exception since not only ImportError can be raised here
            # For example, old versions of NumPy can cause a ValueError.
            # See spyder-ide/spyder#13985
            pass