On 05/23/2012 06:55 PM, Bohuslav Kabrda wrote:
>> # monkey patch ZipFile to behave like TarFile
>> ZipFile.getmembers = ZipFile.infolist
>> ZipFile.extractfile = ZipFile.open
>> ZipFile.open = ZipFile # this line is at fault here
>> ZipInfo.name = ZipInfo.filename
>>
>> Real zipfile.ZipFile.open opens member of archive not an archive
>> itself.
>> So in code above z.read(info.filename) ends up calling ZipFile
>> instead
>> of real zipfile.ZipFile.open, providing wrong aruments.
>>
>> --
>> Zart
> 
> Thanks a lot... I didn't know that all the modules get imported during 
> install... I guess moving that code somewhere to the archive class will do 
> fine.

Is monkeypatching really necessary here? Monkeypatching affects global
process state (i.e. class definitions in imported modules), so it's a
fairly extreme approach to the problem of inconsistent method names that
prevent duck-typing. It's generally preferable to use a wrapper class or
a custom subclass to adapt between the two APIs at the point of use
rather than changing state that can be seen by every other module that
gets imported into the same process.

Cheers,
Nick.

-- 
Nick Coghlan
Red Hat Infrastructure Engineering & Development, Brisbane
_______________________________________________
python-devel mailing list
[email protected]
https://admin.fedoraproject.org/mailman/listinfo/python-devel

Reply via email to