ยปCore Development>Code coverage>Lib/ctypes/test/test_find.py

Python code coverage for Lib/ctypes/test/test_find.py

#countcontent
1n/aimport unittest
2n/aimport os.path
3n/aimport sys
4n/aimport test.support
5n/afrom ctypes import *
6n/afrom ctypes.util import find_library
7n/a
8n/a# On some systems, loading the OpenGL libraries needs the RTLD_GLOBAL mode.
9n/aclass Test_OpenGL_libs(unittest.TestCase):
10n/a @classmethod
11n/a def setUpClass(cls):
12n/a lib_gl = lib_glu = lib_gle = None
13n/a if sys.platform == "win32":
14n/a lib_gl = find_library("OpenGL32")
15n/a lib_glu = find_library("Glu32")
16n/a elif sys.platform == "darwin":
17n/a lib_gl = lib_glu = find_library("OpenGL")
18n/a else:
19n/a lib_gl = find_library("GL")
20n/a lib_glu = find_library("GLU")
21n/a lib_gle = find_library("gle")
22n/a
23n/a ## print, for debugging
24n/a if test.support.verbose:
25n/a print("OpenGL libraries:")
26n/a for item in (("GL", lib_gl),
27n/a ("GLU", lib_glu),
28n/a ("gle", lib_gle)):
29n/a print("\t", item)
30n/a
31n/a cls.gl = cls.glu = cls.gle = None
32n/a if lib_gl:
33n/a try:
34n/a cls.gl = CDLL(lib_gl, mode=RTLD_GLOBAL)
35n/a except OSError:
36n/a pass
37n/a if lib_glu:
38n/a try:
39n/a cls.glu = CDLL(lib_glu, RTLD_GLOBAL)
40n/a except OSError:
41n/a pass
42n/a if lib_gle:
43n/a try:
44n/a cls.gle = CDLL(lib_gle)
45n/a except OSError:
46n/a pass
47n/a
48n/a @classmethod
49n/a def tearDownClass(cls):
50n/a cls.gl = cls.glu = cls.gle = None
51n/a
52n/a def test_gl(self):
53n/a if self.gl is None:
54n/a self.skipTest('lib_gl not available')
55n/a self.gl.glClearIndex
56n/a
57n/a def test_glu(self):
58n/a if self.glu is None:
59n/a self.skipTest('lib_glu not available')
60n/a self.glu.gluBeginCurve
61n/a
62n/a def test_gle(self):
63n/a if self.gle is None:
64n/a self.skipTest('lib_gle not available')
65n/a self.gle.gleGetJoinStyle
66n/a
67n/a def test_shell_injection(self):
68n/a result = find_library('; echo Hello shell > ' + test.support.TESTFN)
69n/a self.assertFalse(os.path.lexists(test.support.TESTFN))
70n/a self.assertIsNone(result)
71n/a
72n/a
73n/a@unittest.skipUnless(sys.platform.startswith('linux'),
74n/a 'Test only valid for Linux')
75n/aclass LibPathFindTest(unittest.TestCase):
76n/a def test_find_on_libpath(self):
77n/a import subprocess
78n/a import tempfile
79n/a
80n/a try:
81n/a p = subprocess.Popen(['gcc', '--version'], stdout=subprocess.PIPE,
82n/a stderr=subprocess.DEVNULL)
83n/a out, _ = p.communicate()
84n/a except OSError:
85n/a raise unittest.SkipTest('gcc, needed for test, not available')
86n/a with tempfile.TemporaryDirectory() as d:
87n/a # create an empty temporary file
88n/a srcname = os.path.join(d, 'dummy.c')
89n/a libname = 'py_ctypes_test_dummy'
90n/a dstname = os.path.join(d, 'lib%s.so' % libname)
91n/a with open(srcname, 'w') as f:
92n/a pass
93n/a self.assertTrue(os.path.exists(srcname))
94n/a # compile the file to a shared library
95n/a cmd = ['gcc', '-o', dstname, '--shared',
96n/a '-Wl,-soname,lib%s.so' % libname, srcname]
97n/a out = subprocess.check_output(cmd)
98n/a self.assertTrue(os.path.exists(dstname))
99n/a # now check that the .so can't be found (since not in
100n/a # LD_LIBRARY_PATH)
101n/a self.assertIsNone(find_library(libname))
102n/a # now add the location to LD_LIBRARY_PATH
103n/a with test.support.EnvironmentVarGuard() as env:
104n/a KEY = 'LD_LIBRARY_PATH'
105n/a if KEY not in env:
106n/a v = d
107n/a else:
108n/a v = '%s:%s' % (env[KEY], d)
109n/a env.set(KEY, v)
110n/a # now check that the .so can be found (since in
111n/a # LD_LIBRARY_PATH)
112n/a self.assertEqual(find_library(libname), 'lib%s.so' % libname)
113n/a
114n/a
115n/aif __name__ == "__main__":
116n/a unittest.main()