|
2 | 2 | import os |
3 | 3 | import importlib.util |
4 | 4 | import traceback |
| 5 | +import sysconfig |
5 | 6 | from jedi.api.classes import Name |
6 | 7 | from parserlog import log, bcolors |
7 | 8 | from parserconfig import ParserConfig |
8 | 9 |
|
9 | 10 | class PYBuiltin: |
10 | 11 | builtin = {} |
11 | 12 |
|
| 13 | + @staticmethod |
| 14 | + def __searchDirectory(directory: str): |
| 15 | + for root, dirs, files in os.walk(directory): |
| 16 | + for file in files: |
| 17 | + p = os.path.join(root, file) |
| 18 | + ext = os.path.splitext(p)[1] |
| 19 | + |
| 20 | + if ext and ext.lower() == '.py': |
| 21 | + PYBuiltin.builtin[p] = True |
| 22 | + |
12 | 23 | @staticmethod |
13 | 24 | def findBuiltins(config: ParserConfig): |
14 | 25 | try: |
| 26 | + # Consider all Python modules in the stdlib directory builtin |
| 27 | + sysconfig_paths = sysconfig.get_paths() |
| 28 | + if "stdlib" in sysconfig_paths: |
| 29 | + PYBuiltin.__searchDirectory(sysconfig_paths["stdlib"]) |
| 30 | + |
| 31 | + # Locate module paths via ModuleSpec |
| 32 | + # However, this can return "frozen" and "built-in" as module origin and not the actual file |
15 | 33 | # Note: Python 3.10+ required |
16 | 34 | stdlib_modules = sys.stdlib_module_names |
17 | | - |
18 | 35 | for e in stdlib_modules: |
19 | 36 | spec = importlib.util.find_spec(e) |
20 | | - if spec and spec.origin: |
| 37 | + if spec and spec.origin and spec.origin != "frozen" and spec.origin != "built-in": |
21 | 38 | PYBuiltin.builtin[spec.origin] = True |
22 | 39 |
|
23 | 40 | if spec and spec.submodule_search_locations: |
24 | 41 | for submodule_dir in spec.submodule_search_locations: |
25 | | - for root, dirs, files in os.walk(submodule_dir): |
26 | | - for file in files: |
27 | | - p = os.path.join(root, file) |
28 | | - ext = os.path.splitext(p)[1] |
29 | | - |
30 | | - if ext and ext.lower() == '.py': |
31 | | - PYBuiltin.builtin[p] = True |
32 | | - |
| 42 | + PYBuiltin.__searchDirectory(submodule_dir) |
33 | 43 | except: |
34 | 44 | log(f"{bcolors.FAIL}Failed to find Python builtins!") |
35 | 45 | if config.stack_trace: |
|
0 commit comments