[Patches] [ python-Patches-403789 ] Add support to zipfile for passing file-like objects

noreply@sourceforge.net noreply@sourceforge.net
Tue, 20 Mar 2001 09:20:01 -0800


Patches item #403789, was updated on 2001-02-14 04:58
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=305470&aid=403789&group_id=5470

>Category: library
Group: None
>Status: Open
>Priority: 6
Submitted By: Itamar S.T. (itamar)
Assigned to: Fred L. Drake, Jr. (fdrake)
Summary: Add support to zipfile for passing file-like objects

Initial Comment:
zipfile.ZipFile, in its current state, requires the user to pass a filename, which is then opened by ZipFile. I (and probably other people) require the ability to pass a file-like objects to ZipFile instead, e.g. have the zipped data stored in a StringIO object. This patch adds that ability. (Sorry, no unit testing included, and only lightly tested. But the changes are very straightforward.)

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

>Comment By: Fred L. Drake, Jr. (fdrake)
Date: 2001-03-20 09:20

Message:
Logged In: YES 
user_id=3066

Re-opened since the patch has been updated & Itamar reminded me that I was going to look at this.  I expect to look at this & others tomorrow.

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

Comment By: Itamar S.T. (itamar)
Date: 2001-02-20 05:14

Message:
As per Yhg1s's suggestion (he won't give me his real name - thomas?), I resubmitted everything as a patch.

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

Comment By: Itamar S.T. (itamar)
Date: 2001-02-20 04:46

Message:
OK, here are some tests (I submitted the whole file, not a patch, since I basically rewrote it.).  And I fixed zipfile.py based on your comments.

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

Comment By: Fred L. Drake, Jr. (fdrake)
Date: 2001-02-19 11:46

Message:
Please provide test cases for this.  The tests should be added to Lib/test/test_zipfile.py.  File-ness should probably be determined by checking if the object is a string or not (remember to test for a Unicode string as well!).

Instead of catching an exception from checking for file.name, you can instead fetch the name using getattr(file, 'name', None) and not worry about exceptions; only the AttributeError you were actually trying to catch will be caught, and everything else will "do the right thing" instead of getting masked, as the current code does.

I'll re-evaluate the patch when it gets updated at SF.  (I think the idea is right -- there's no need to require a built-in file object.)

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

Comment By: Itamar S.T. (itamar)
Date: 2001-02-14 05:26

Message:
Right now I detect file-like objects by doing hasattr(file, "read"). Doing hasattr(file, "write") might be better though, since for mode="w" ZipFiles, all you need are write() and tell() methods on the file-like object.

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

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=305470&aid=403789&group_id=5470