Installer vs py2exe: Windows DLLs

Paul Moore gustav at morpheus.demon.co.uk
Thu Jun 6 05:32:11 EDT 2002


Gordon McMillan <gmcm at hypernet.com> writes:

> Paul Moore wrote:
>
>> When building a distribution for the same script using Gordon
>> McMillan's Installer and py2exe, I notice that Installer includes
>> PyWinTypes22.dll and win32api.pyd, whereas py2exe doesn't. 
>
> [snip]
>
>> More to the point, in some ways, why does Installer think these files
>> are needed? Is there some situation in which py2exe generated
>> executables will fail because these files aren't there?
>
> David's answer was 100% correct.
>
> The specific call here is os.path.abspath(...). Before 2.2 and 
> without win32api, this simply examined its argument. In some
> rare corner cases, it would yield an incorrect answer.

OK, I analyzed this a bit further. This is all for Python 2.2 on
Windows 2000.

The win32api dependency (and hence the PyWinTypes22 dependency) comes
from iu.py, in the RegistryImportDirector. Two questions come to mind
- first is whether it is appropriate for Installould use the
_winreg module, to avoid the dependency in the first place. Ther-built applications
to look in the registry for Python entries in any case, as the whole
point is to run independently of any installed copy of Python. I
imagine that something later in the process disables picking up
registry entries, but that leaves the RegistryImportDirector code as
dead code (which isn't worth worrying about, except for the spurious
dependency).

The second question is whether RegistryImportDirector she
translation is pretty much automatic - I attach a patch. This gives a
dependency on _winreg.pyd (pity - I'd assumed _winreg would be built
in on Windows :-() but that is *much* smaller than win32api +
PyWinTypes22. Minimal testing shows no problems, but a better test
would be worth doing...

The dependency on _sre comes from posixpath (in expandvars()), which
is imported conditionally by os. And os is imported by archive.py, to
get at os.path.basename, and os.path.splitext. Would it not be
possible here to directly use {dos,mac,nt,posix}path?

I know that in the grand scheme of things, removing dependencies like
this is fairly pointless for any "real" application, but it appeals to
my sense of minimalism. At least the _winreg patch is pretty minor,
and so probably reasonable... (and win32api/PyWinTypes22 is the big
hit).

Hope this is useful,
Paul Moore.

[CC'd to the installer list, as that's probably a more appropriate place]

--- Patch to use _winreg in iu.py ---

--- \Applications\Python\Installer\iu.py.orig	Mon Nov 19 17:05:56 2001
+++ \Applications\Python\Installer\iu.py	Thu Jun 06 10:17:52 2002
@@ -111,7 +111,7 @@
         self.path = "WindowsRegistry"
         self.map = {}
         try:
-            import win32api
+            import _winreg
 ##            import win32con
         except ImportError:
             pass
@@ -122,15 +122,15 @@
             subkey = r"Software\Python\PythonCore\%s\Modules" % sys.winver
             for root in (HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE):
                 try:
-                    hkey = win32api.RegOpenKeyEx(root, subkey, 0, KEY_ALL_ACCESS)
+                    hkey = _winreg.OpenKey(root, subkey, 0, KEY_ALL_ACCESS)
                 except:
                     pass
                 else:
-                    numsubkeys, numvalues, lastmodified = win32api.RegQueryInfoKey(hkey)
+                    numsubkeys, numvalues, lastmodified = _winreg.QueryInfoKey(hkey)
                     for i in range(numsubkeys):
-                        subkeyname = win32api.RegEnumKey(hkey, i)
-                        hskey = win32api.RegOpenKeyEx(hkey, subkeyname, 0, KEY_ALL_ACCESS)
-                        val = win32api.RegQueryValueEx(hskey, '')
+                        subkeyname = _winreg.EnumKey(hkey, i)
+                        hskey = _winreg.OpenKey(hkey, subkeyname, 0, KEY_ALL_ACCESS)
+                        val = _winreg.QueryValue(hskey, '')
                         desc = getDescr(val[0])
                         self.map[subkeyname] = (val[0], desc)
                         hskey.Close()




More information about the Python-list mailing list