0001 # Magic utility that "redirects" to pywintypesxx.dll 0002 0003 def __import_pywin32_system_module__(modname, globs): 0004 # *sigh* - non-admin installs will not have pywintypesxx.dll in the 0005 # system directory, so 'import win32api' will fail looking 0006 # for pywintypes - the exact DLL we are trying to load! 0007 # So if it exists in sys.prefix, then we try and load it from 0008 # there, as that way we can avoid the win32api import 0009 import imp, sys, os 0010 if not sys.platform.startswith("win32"): 0011 # These extensions can be built on Linux via the 'mainwin' toolkit. 0012 # Look for a native 'lib{modname}.so' 0013 for ext, mode, ext_type in imp.get_suffixes(): 0014 if ext_type==imp.C_EXTENSION: 0015 for path in sys.path: 0016 look = os.path.join(path, "lib" + modname + ext) 0017 if os.path.isfile(look): 0018 mod = imp.load_module(modname, None, look, 0019 (ext, mode, ext_type)) 0020 # and fill our namespace with it. 0021 globs.update(mod.__dict__) 0022 return 0023 raise ImportError, "No dynamic module " + modname 0024 # See if this is a debug build. 0025 for suffix_item in imp.get_suffixes(): 0026 if suffix_item[0]=='_d.pyd': 0027 suffix = '_d' 0028 break 0029 else: 0030 suffix = "" 0031 filename = "%s%d%d%s.dll" % \ 0032 (modname, sys.version_info[0], sys.version_info[1], suffix) 0033 if hasattr(sys, "frozen"): 0034 # If we are running from a frozen program (py2exe, McMillan, freeze) 0035 # then we try and load the DLL from our sys.path 0036 for look in sys.path: 0037 # If the sys.path entry is a (presumably) .zip file, use the 0038 # directory 0039 if os.path.isfile(look): 0040 look = os.path.dirname(look) 0041 found = os.path.join(look, filename) 0042 if os.path.isfile(found): 0043 break 0044 else: 0045 raise ImportError, \ 0046 "Module '%s' isn't in frozen sys.path %s" % (modname, sys.path) 0047 else: 0048 # If there is a version in our Python directory, use that 0049 # (it may not be on the PATH, so may fail to be loaded by win32api) 0050 # Non-admin installs will have the system files there. 0051 found = None 0052 if os.path.isfile(os.path.join(sys.prefix, filename)): 0053 found = os.path.join(sys.prefix, filename) 0054 # Allow Windows to find it. We have tried various other tricks, 0055 # but in some cases, we ended up with *2* versions of the libraries 0056 # loaded - the one found by Windows when doing a later "import win32*", 0057 # and the one we found here. 0058 # A remaining trick would be to simulate LoadLibrary(), using the 0059 # registry to determine the system32 directory. However, Python 0060 # 2.2 doesn't have sys.getwindowsversion(), which is kinda needed 0061 # to determine the correct places to look. 0062 0063 # The downside of this is that we need to use win32api, and this 0064 # depends on pywintypesxx.dll, which may be what we are trying to 0065 # find! If this fails, we get a dialog box, followed by the import 0066 # error. The dialog box is undesirable, but should only happen 0067 # when something is badly broken, and is a less harmful side-effect 0068 # than loading the DLL twice! 0069 if found is None: 0070 import win32api # failure here means Windows can't find it either! 0071 found = win32api.GetModuleFileName(win32api.LoadLibrary(filename)) 0072 0073 # Python can load the module 0074 mod = imp.load_module(modname, None, found, 0075 ('.dll', 'rb', imp.C_EXTENSION)) 0076 # and fill our namespace with it. 0077 globs.update(mod.__dict__) 0078 0079 __import_pywin32_system_module__("pywintypes", globals()) 0080
Generated by PyXR 0.9.4