|
1 | 1 | from ctypes import * |
2 | 2 | import os |
| 3 | +import shutil |
| 4 | +import subprocess |
3 | 5 | import sys |
| 6 | +import sysconfig |
4 | 7 | import unittest |
5 | 8 | import test.support |
6 | 9 | from ctypes.util import find_library |
@@ -112,5 +115,65 @@ def test_1703286_B(self): |
112 | 115 | # This is the real test: call the function via 'call_function' |
113 | 116 | self.assertEqual(0, call_function(proc, (None,))) |
114 | 117 |
|
| 118 | + @unittest.skipUnless(os.name == "nt", |
| 119 | + 'test specific to Windows') |
| 120 | + def test_load_dll_with_flags(self): |
| 121 | + _sqlite3 = test.support.import_module("_sqlite3") |
| 122 | + src = _sqlite3.__file__ |
| 123 | + if src.lower().endswith("_d.pyd"): |
| 124 | + ext = "_d.dll" |
| 125 | + else: |
| 126 | + ext = ".dll" |
| 127 | + |
| 128 | + with test.support.temp_dir() as tmp: |
| 129 | + # We copy two files and load _sqlite3.dll (formerly .pyd), |
| 130 | + # which has a dependency on sqlite3.dll. Then we test |
| 131 | + # loading it in subprocesses to avoid it starting in memory |
| 132 | + # for each test. |
| 133 | + target = os.path.join(tmp, "_sqlite3.dll") |
| 134 | + shutil.copy(src, target) |
| 135 | + shutil.copy(os.path.join(os.path.dirname(src), "sqlite3" + ext), |
| 136 | + os.path.join(tmp, "sqlite3" + ext)) |
| 137 | + |
| 138 | + def should_pass(command): |
| 139 | + with self.subTest(command): |
| 140 | + subprocess.check_output( |
| 141 | + [sys.executable, "-c", |
| 142 | + "from ctypes import *; import nt;" + command], |
| 143 | + cwd=tmp |
| 144 | + ) |
| 145 | + |
| 146 | + def should_fail(command): |
| 147 | + with self.subTest(command): |
| 148 | + with self.assertRaises(subprocess.CalledProcessError): |
| 149 | + subprocess.check_output( |
| 150 | + [sys.executable, "-c", |
| 151 | + "from ctypes import *; import nt;" + command], |
| 152 | + cwd=tmp, stderr=subprocess.STDOUT, |
| 153 | + ) |
| 154 | + |
| 155 | + # Default load should not find this in CWD |
| 156 | + should_fail("WinDLL('_sqlite3.dll')") |
| 157 | + |
| 158 | + # Relative path (but not just filename) should succeed |
| 159 | + should_pass("WinDLL('./_sqlite3.dll')") |
| 160 | + |
| 161 | + # Insecure load flags should succeed |
| 162 | + should_pass("WinDLL('_sqlite3.dll', winmode=0)") |
| 163 | + |
| 164 | + # Full path load without DLL_LOAD_DIR shouldn't find dependency |
| 165 | + should_fail("WinDLL(nt._getfullpathname('_sqlite3.dll'), " + |
| 166 | + "winmode=nt._LOAD_LIBRARY_SEARCH_SYSTEM32)") |
| 167 | + |
| 168 | + # Full path load with DLL_LOAD_DIR should succeed |
| 169 | + should_pass("WinDLL(nt._getfullpathname('_sqlite3.dll'), " + |
| 170 | + "winmode=nt._LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR)") |
| 171 | + |
| 172 | + # User-specified directory should succeed |
| 173 | + should_pass("import os; p = os.add_dll_directory(os.getcwd());" + |
| 174 | + "WinDLL('_sqlite3.dll'); p.close()") |
| 175 | + |
| 176 | + |
| 177 | + |
115 | 178 | if __name__ == "__main__": |
116 | 179 | unittest.main() |
0 commit comments