home *** CD-ROM | disk | FTP | other *** search
/ PC User 2004 November / PCU1104CD1.iso / software / online / files / jyte.exe / pywintypes.py < prev    next >
Encoding:
Text File  |  2004-02-06  |  2.5 KB  |  56 lines

  1. # Magic utility that "redirects" to pywintypesxx.dll
  2.  
  3. def __import_pywin32_system_module__(modname, globs):
  4.     # *sigh* - non-admin installs will not have pywintypesxx.dll in the 
  5.     # system directory, so 'import win32api' will fail looking
  6.     # for pywintypes - the exact DLL we are trying to load!
  7.     # So if it exists in sys.prefix, then we try and load it from
  8.     # there, as that way we can avoid the win32api import
  9.     import imp, sys, os
  10.     # See if this is a debug build.
  11.     for suffix_item in imp.get_suffixes():
  12.         if suffix_item[0]=='_d.pyd':
  13.             suffix = '_d'
  14.             break
  15.     else:
  16.         suffix = ""
  17.     filename = "%s%d%d%s.dll" % \
  18.                (modname, sys.version_info[0], sys.version_info[1], suffix)
  19.     if hasattr(sys, "frozen"):
  20.         # If we are running from a frozen program (py2exe, McMillan, freeze)
  21.         # then we try and load the DLL from our sys.path
  22.         for look in sys.path:
  23.             # If the sys.path entry is a (presumably) .zip file, use the
  24.             # directory 
  25.             if os.path.isfile(look):
  26.                 look = os.path.dirname(look)            
  27.             found = os.path.join(look, filename)
  28.             if os.path.isfile(found):
  29.                 break
  30.         else:
  31.             raise ImportError, \
  32.                   "Module '%s' isn't in frozen sys.path directories" % modname
  33.     else:
  34.         search_dirs = [sys.prefix] + \
  35.                       os.environ.get("PATH", "").split(os.pathsep)
  36.         for d in search_dirs:
  37.             found = os.path.join(d, filename)
  38.             if os.path.isfile(found):
  39.                 break
  40.         else:
  41.             # Eeek - can't find on the path.  Try "LoadLibrary", as it
  42.             # has slightly different semantics than a simple sys.path search
  43.             # XXX - OK, we *don't* try LoadLibrary - if we can't find it, 
  44.             # there is an excellent change Windows can't find it, and
  45.             # the attempt to bring in win32api *needs* Windows to find it.
  46.             # If Windows can't find it, it displays a dialog trying to 
  47.             # import win32api, which is not what we want!
  48.             raise ImportError, "Can not locate " + filename
  49.     # Python can load the module
  50.     mod = imp.load_module(modname, None, found, 
  51.                           ('.dll', 'rb', imp.C_EXTENSION))
  52.     # and fill our namespace with it.
  53.     globs.update(mod.__dict__)
  54.  
  55. __import_pywin32_system_module__("pywintypes", globals())
  56.