forked from sympy/sympy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_external_imports.py
executable file
·92 lines (65 loc) · 2.71 KB
/
test_external_imports.py
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
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env python
"""
Test that
from sympy import *
doesn't import anything other than SymPy, it's hard dependencies (mpmath), and
hard optional dependencies (gmpy2). Importing unnecessary libraries
can accidentally add hard dependencies to SymPy in the worst case, or at best
slow down the SymPy import time when they are installed.
Note, for this test to be effective, every external library that could
potentially be imported by SymPy must be installed.
TODO: Monkeypatch the importer to detect non-standard library imports even
when they aren't installed.
Based on code from
https://stackoverflow.com/questions/22195382/how-to-check-if-a-module-library-package-is-part-of-the-python-standard-library.
"""
# These libraries will always be imported with SymPy
hard_dependencies = ['mpmath']
# These libraries are optional, but are always imported at SymPy import time
# when they are installed. External libraries should only be added to this
# list if they are required for core SymPy functionality.
hard_optional_dependencies = ['gmpy', 'gmpy2', 'pycosat', 'python-sat']
import sys
import os
def is_stdlib(p):
return ((p.startswith(sys.prefix)
or p.startswith(sys.base_prefix))
and 'site-packages' not in p)
stdlib = {p for p in sys.path if is_stdlib(p)}
existing_modules = list(sys.modules.keys())
# hook in-tree SymPy into Python path, if possible
this_path = os.path.abspath(__file__)
this_dir = os.path.dirname(this_path)
sympy_top = os.path.split(this_dir)[0]
sympy_dir = os.path.join(sympy_top, 'sympy')
if os.path.isdir(sympy_dir):
sys.path.insert(0, sympy_top)
def test_external_imports():
exec("from sympy import *", {})
bad = []
for mod in sys.modules:
if '.' in mod and mod.split('.')[0] in sys.modules:
# Only worry about the top-level modules
continue
if mod in existing_modules:
continue
if any(mod == i or mod.startswith(i + '.') for i in ['sympy'] +
hard_dependencies + hard_optional_dependencies):
continue
if mod in sys.builtin_module_names:
continue
fname = getattr(sys.modules[mod], "__file__", None)
if fname is None:
bad.append(mod)
continue
if fname.endswith(('__init__.py', '__init__.pyc', '__init__.pyo')):
fname = os.path.dirname(fname)
if os.path.dirname(fname) in stdlib:
continue
bad.append(mod)
if bad:
raise RuntimeError("""Unexpected external modules found when running 'from sympy import *':
""" + '\n '.join(bad))
print("No unexpected external modules were imported with 'from sympy import *'!")
if __name__ == '__main__':
test_external_imports()