# test_dll_load_failed.py # # Build up to 256 C extension modules and import them. # # On Python 3.5.0rc3 for Windows this raises "ImportError: DLL load failed: # A dynamic link library (DLL) initialization routine failed." # # http://bugs.python.org/issue25027 import os import sys from importlib import import_module from distutils import ccompiler assert sys.platform == 'win32' assert sys.version_info > (3, 2) c_source = """/* Minimal Python 3 extension module */ #include "Python.h" static struct PyModuleDef moduledef = { PyModuleDef_HEAD_INIT, "%s", NULL, -1, NULL, NULL, NULL, NULL, NULL }; PyMODINIT_FUNC PyInit_%s(void) { return PyModule_Create(&moduledef); } """ cl = ccompiler.new_compiler() for i in range(256): name = '_tmp%.3i' % i try: os.remove(name+'.pyd') except FileNotFoundError: pass with open(name+'.c', 'w') as fh: fh.write(c_source % (name, name)) objects = cl.compile([name+'.c']) cl.link_shared_lib(objects, output_libname=name, extra_postargs=['/DLL'], export_symbols=["PyInit_"+name]) for ext in 'c', 'obj', 'exp', 'lib': os.remove(name+'.'+ext) os.rename(name+'.dll', name+'.pyd') import_module(name)