|
1 | 1 | # Copyright (c) 2024 nggit |
2 | 2 |
|
| 3 | +import sys |
| 4 | + |
3 | 5 | from types import ModuleType |
4 | 6 |
|
5 | 7 | from libc.stdio cimport (FILE, fopen, fclose, fread, feof, ferror, |
@@ -50,26 +52,44 @@ def exec_module(module, code=None, size_t max_size=8 * 1048576): |
50 | 52 | exec(code, module.__dict__) |
51 | 53 |
|
52 | 54 |
|
53 | | -def cleanup_modules(modules, tuple excludes=()): |
| 55 | +def cleanup_modules(modules, int debug=0): |
54 | 56 | cdef str module_name, name |
55 | 57 | cdef dict module_dict, value_dict |
56 | 58 |
|
| 59 | + if debug: |
| 60 | + if debug == 1: |
| 61 | + print(' cleanup_modules:') |
| 62 | + |
| 63 | + debug += 4 |
| 64 | + |
57 | 65 | for module_name, module in modules.items(): |
58 | 66 | module_dict = getattr(module, '__dict__', None) |
59 | 67 |
|
60 | 68 | if module_dict: |
61 | 69 | for name, value in module_dict.items(): |
62 | | - if value in excludes or name.startswith('__'): |
| 70 | + if name.startswith('__'): |
| 71 | + continue |
| 72 | + |
| 73 | + value_module = getattr(value, '__module__', '__main__') |
| 74 | + |
| 75 | + if value_module != '__main__' and value_module in sys.modules: |
63 | 76 | continue |
64 | 77 |
|
65 | 78 | if not (value is module or |
66 | 79 | isinstance(value, (type, ModuleType))): |
67 | 80 | value_dict = getattr(value, '__dict__', None) |
68 | 81 |
|
69 | 82 | if value_dict: |
70 | | - cleanup_modules(value_dict, excludes) |
| 83 | + cleanup_modules(value_dict, debug) |
71 | 84 |
|
72 | 85 | module_dict[name] = None |
73 | 86 |
|
| 87 | + if debug: |
| 88 | + print(' ' * debug, ',-- deleted:', name, value) |
| 89 | + |
74 | 90 | if not module_name.startswith('__'): |
75 | 91 | modules[module_name] = None |
| 92 | + |
| 93 | + if debug: |
| 94 | + print(' ' * debug, '|') |
| 95 | + print(' ' * debug, 'deleted:', module_name, module) |
0 commit comments