[python-win32] Why the strange frozen dance in win32com/__init__.py?

quest at openend.se quest at openend.se
Mon Oct 8 10:32:18 CEST 2007


(My appologies if this has been discussed earlier; my Google-fu may be
weak.)

pywin32's win32com/__init__.py does this (slightly compacted for
brevity):

  def SetupEnvironment():
      try:
          keyName = "SOFTWARE\\Python\\PythonCore\\%s\\PythonPath\\win32com" % sys.winver
          key = win32api.RegOpenKey(HKEY_LOCAL_MACHINE , keyName, 0,
                                    KEY_QUERY_VALUE)
      except (win32api.error, AttributeError):
          key = None

      try:
          found = 0
          if key is not None:
              try:
                  __path__.append(win32api.RegQueryValue(key, "Extensions" ))
                  found = 1
              except win32api.error:
                  pass
          if not found:
              try:
                  __path__.append( win32api.GetFullPathName(__path__[0] + "\\.\
.\\win32comext") )
              except win32api.error:
                  pass

  if not sys.frozen:
      SetupEnvironment()

Being a developer of a commercial Python application, I have beef with
this:

I would much prefer this code to work in the opposite fashion: first
look at the place relative to __path__, then fallback to registry
lookup. My reason is of course, that I have no wish to install a
"standalone" python on the user's box, nor do I want to be tripped by
the presence of one. E.g.:

  cand_extdir = os.path.join(os.path.dirname(__path__[0]), 'win32comext')
  if not os.path.exists(cand_extdir):
      keyName = "SOFTWARE\\Python\\PythonCore\\%s\\PythonPath\\win32com" % \
                sys.winver
      key = win32api.RegOpenKey(HKEY_LOCAL_MACHINE , keyName, 0,
                                KEY_QUERY_VALUE)
      try:
          cand_extdir = win32api.RegQueryValue(key, "Extensions" )
      except win32api.error:
          cand_extdir = None

  if cand_extdir:
      __path__.append(cand_extdir)

The above disregards that SetupEnvironment code is not executed at all
in a frozen state, but I don't have a problem with explicitly calling
a method to get this inited; e.g:

  if getattr(sys, 'frozen', False):
      win32com.SetupEnvironment()
-- 
Anders Qvist, Open End AB



More information about the python-win32 mailing list