[Python-Dev] Re: Path hacking

Guido van Rossum guido@CNRI.Reston.VA.US
Thu, 16 Sep 1999 09:49:16 -0400


[me]
> > Why would you want your own exceptions.py and site.py?
[JimA]
> I don't.  I never change Python library files.  I am worried
> that they won't be found because I don't trust PYTHONPATH.

Hmm...  PYTHONPATH gets inserted in front of the default sys.path.
(Many moons ago that was different.  But it has been like this for a
loooooong time.)  So are you worried that someone put a *different*
exceptions.py or site.py on their path?

> > Again - why would anyone register their own site.py?
> 
> I wouldn't, I am worried that someone else will break my installation.
> Remember that site.py was invented as a site-specific module, although
> that function moved to sitecustomize.py.

Hm, I dug out the oldest site.py I have (used in Python 1.4), and it
doesn't encourage editing it at all -- it tells you to use
sitecustomize.py.  I guess they could break your installation anyway,
but only by messing with the general Python installation.

> > Sounds right.  All tricks to make the app unique require using a
> > different registry key, which requires a change to the DLL.  However,
> > you can do this without recompiling!  The version string is used is
> > embedded in a resource, so you can patch it using some kind of
> > resource editor.  Mark Hammond planned it this way!
> 
> I don't understand this.  Is there documentation?

The usual :-)

Python/import.c shows that import calls PyWin_FindRegisteredModule()
to find a registered module before looking in sys.path (but after
checking for builtin and frozen modules).

PC/import_nt.c shows that PyWin_FindRegisteredModule() uses a registry
key of the form
"Software\Python\PythonCore\<PyWin_DLLVersionString>\Modules\<modulename><debugstring>"
where <modulename> is the module name, <debugstring> is empty or
"\Debug" depending on whether we are compiled with _DEBUG define.  The
resource value points to a file (either .py, .pyc/.pyo, .pyd or .dll;
in fact any of the prefixes returned by imp.get_suffixes()).

PC/dl_nt.c shows that PyWin_DLLVersionString is set to string 1000
loaded from the string resource table.

PC/python_nt.rc shows that there's a stringtable with item 1000 being
the MS_DLL_ID string, set to "1.5" in that file.

Note that this value (PyWin_DLLVersionString) is also to Python code
as sys.winver.

I hope that Mark Hammond can point you to a tool that you can use to
edit a string resource in an executable or DLL.

--Guido van Rossum (home page: http://www.python.org/~guido/)