zipfile stupidly broken

Nick Craig-Wood nick at craig-wood.com
Sun May 20 01:35:09 EDT 2007


Martin Maney <maney at two14.net> wrote:
>  Nick Craig-Wood <nick at craig-wood.com> wrote:
> > You don't need to do that, you can just "monkey patch" the _EndRecData
> > function.
> 
>  For a quick & dirty test, sure.  If I were certain I'd only ever use
>  this on one machine for a limited time (viz, no system upgrades that
>  replace zipfile.py) it might suffice.  But that doesn't generalize
>  worth a damn.

>From the above I don't think you've understood the concept of monkey
patching - it is run time patching.  You patch the zipfile module from
your code - no messing with the installed python needed.  Eg something
like :-

------------------------------------------------------------
import zipfile

OriginalEndRecData = zipfile._EndRecData

def MyEndRecData(fpin):
    """
    Return data from the "End of Central Directory" record, or
    None.
    """
    # Try the builtin one first
    endrec = OriginalEndRecData(fpin)
    if endrec is None:
        # didn't work so do something extra
        # you fill this bit in!
        pass
    return endrec

zipfile._EndRecData = MyEndRecData

# Now use your run time patched zipfile module as normal
------------------------------------------------------------

It isn't ideal, but it certainly does generalise.

-- 
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick



More information about the Python-list mailing list