summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_import
diff options
context:
space:
mode:
authorSteve Dower <steve.dower@microsoft.com>2019-03-29 23:37:16 (GMT)
committerGitHub <noreply@github.com>2019-03-29 23:37:16 (GMT)
commit2438cdf0e932a341c7613bf4323d06b91ae9f1f1 (patch)
tree231cdf3f22e1d5eb9f88fe7a511ab47e3cf8d225 /Lib/test/test_import
parent32119e10b792ad7ee4e5f951a2d89ddbaf111cc5 (diff)
downloadcpython-2438cdf0e932a341c7613bf4323d06b91ae9f1f1.zip
cpython-2438cdf0e932a341c7613bf4323d06b91ae9f1f1.tar.gz
cpython-2438cdf0e932a341c7613bf4323d06b91ae9f1f1.tar.bz2
bpo-36085: Enable better DLL resolution on Windows (GH-12302)
Diffstat (limited to 'Lib/test/test_import')
-rw-r--r--Lib/test/test_import/__init__.py48
1 files changed, 48 insertions, 0 deletions
diff --git a/Lib/test/test_import/__init__.py b/Lib/test/test_import/__init__.py
index 7306e0f..a0bfe1a 100644
--- a/Lib/test/test_import/__init__.py
+++ b/Lib/test/test_import/__init__.py
@@ -8,6 +8,8 @@ import os
import platform
import py_compile
import random
+import shutil
+import subprocess
import stat
import sys
import threading
@@ -17,6 +19,7 @@ import unittest.mock as mock
import textwrap
import errno
import contextlib
+import glob
import test.support
from test.support import (
@@ -460,6 +463,51 @@ class ImportTests(unittest.TestCase):
finally:
del sys.path[0]
+ @unittest.skipUnless(sys.platform == "win32", "Windows-specific")
+ def test_dll_dependency_import(self):
+ from _winapi import GetModuleFileName
+ dllname = GetModuleFileName(sys.dllhandle)
+ pydname = importlib.util.find_spec("_sqlite3").origin
+ depname = os.path.join(
+ os.path.dirname(pydname),
+ "sqlite3{}.dll".format("_d" if "_d" in pydname else ""))
+
+ with test.support.temp_dir() as tmp:
+ tmp2 = os.path.join(tmp, "DLLs")
+ os.mkdir(tmp2)
+
+ pyexe = os.path.join(tmp, os.path.basename(sys.executable))
+ shutil.copy(sys.executable, pyexe)
+ shutil.copy(dllname, tmp)
+ for f in glob.glob(os.path.join(sys.prefix, "vcruntime*.dll")):
+ shutil.copy(f, tmp)
+
+ shutil.copy(pydname, tmp2)
+
+ env = None
+ env = {k.upper(): os.environ[k] for k in os.environ}
+ env["PYTHONPATH"] = tmp2 + ";" + os.path.dirname(os.__file__)
+
+ # Test 1: import with added DLL directory
+ subprocess.check_call([
+ pyexe, "-Sc", ";".join([
+ "import os",
+ "p = os.add_dll_directory({!r})".format(
+ os.path.dirname(depname)),
+ "import _sqlite3",
+ "p.close"
+ ])],
+ stderr=subprocess.STDOUT,
+ env=env,
+ cwd=os.path.dirname(pyexe))
+
+ # Test 2: import with DLL adjacent to PYD
+ shutil.copy(depname, tmp2)
+ subprocess.check_call([pyexe, "-Sc", "import _sqlite3"],
+ stderr=subprocess.STDOUT,
+ env=env,
+ cwd=os.path.dirname(pyexe))
+
@skip_if_dont_write_bytecode
class FilePermissionTests(unittest.TestCase):