[Patches] [Patch #101702] Modify co_filename in frozen programs

noreply@sourceforge.net noreply@sourceforge.net
Wed, 25 Oct 2000 01:36:14 -0700


Patch #101702 has been updated. 

Project: 
Category: demos and tools
Status: Open
Summary: Modify co_filename in frozen programs

Follow-Ups:

Date: 2000-Sep-29 04:39
By: lhudson

Comment:
A new feature for freeze.

This patch was developed primarily to reduce the size of the frozen binary.  It is particularly useful when freezing for 'small' platforms, such as Palm OS, where you really want to save that last miserable byte.

A limitation of this patch is that it does not provide any feedback about the replacements being made.  As the path matching is case-sensitive this may lead to unexpected behaviour for DOS and Windows people, eg
    > freeze.py -r C:\Python\Lib\=py\ goats.py
should probably be:
    > freeze.py -r c:\python\lib\=py\ goats.py

-------------------------------------------------------

Date: 2000-Sep-29 07:28
By: jhylton

Comment:
This sounds like a reasonable enough feature, but we are in feature freeze for Python 2.0.

-------------------------------------------------------

Date: 2000-Oct-23 12:33
By: gvanrossum

Comment:
I like the idea, but I have a few suggestions.

- To avoid the case sensitivity issue you mention, you could use os.path.normpath() before the string comparisons.

- Rather than writing to a real temporary file, you could marshal to a string and than unmarshal from a StringIO file.

- And the big whopper: rather than using a modified version of unmarshal, I suggest writing a recursive code object transformer. It takes a code object and a replace_paths list, and returns a similar code object with the co_filename attribute changed according to the list. The trick is that it should also do this, recursively, to any code object found in the co_consts tuple. (That's the only place where code objects can occur recursively.) While this is probably not much less code than the marshaller you wrote, it has the advantage of not being dependent on the details of the marshal format.
Please upload a new patch -- I'm interested in getting this into Python 2.1.
-------------------------------------------------------

Date: 2000-Oct-25 01:36
By: lhudson

Comment:
A recursive code object transformer has been implemented as a member of the ModuleFinder.

Case sensitivity issue:
normpath strips trailing '/' or '\\' which reduces the flexibility of the 
path replacement.  It also does not seem to solve the case-sensitivity problem:
    D:\>python
    Python 2.0 (#8, Oct 17 2000, 15:27:24) [MSC 32 bit (Intel)] on win32
    Type "copyright", "credits" or "license" for more information.
    >>> import os
    >>> os.path.normpath("c:\\python\\lib\\")
    'c:\\python\\lib'
    >>> os.path.normpath("c:\\python\\Lib\\")
    'c:\\python\\Lib'
    >>> 
ModuleFinder's debugging system is used to report the status of each unique path encountered.

Temporary file:
marshal.dump() and marshal.load() only work with file objects, not file-like objects.  Now that new code objects are being created there is no longer any reason to re-marshal so this problem disappears.

-------------------------------------------------------

-------------------------------------------------------
For more info, visit:

http://sourceforge.net/patch/?func=detailpatch&patch_id=101702&group_id=5470