Issue 10392: GZipFile crash when fileobj.mode is None (original) (raw)

Created on 2010-11-12 05:58 by bgreenlee, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
gzip_mode_fix.diff bgreenlee,2010-11-12 05:58
Messages (7)
msg121021 - (view) Author: Brad Greenlee (bgreenlee) Date: 2010-11-12 05:58
If GZipFile.__init_ is passed a fileobj that has a mode attribute set to None, it will crash with a "'NoneType' object is unsubscriptable" error when it tries to read the first character of the mode. I ran across this when trying to pass GZipFile an uploaded file in Django 1.2.3. Django produced an InMemoryUploadedFile object that has a mode attribute set to None. The attached patch fixes the issue by only using fileobj.mode if it exists and is not None. (The patch is against 2.7, although the issue exists in all versions I've looked at.)
msg121107 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2010-11-13 03:24
Why do you consider this a bug in GZipFile rather than a bug in Django? GZipFile is already careful to consider mode only when it is defined as an attribute. It seems to me that if it is defined, it should be meaningful.
msg121111 - (view) Author: Brad Greenlee (bgreenlee) Date: 2010-11-13 04:54
GzipFile.__init__ considers mode == None to be the equivalent of undefined, and sets it to the default of 'rb'. I see fileobj.mode == None as the same thing. That said, it is probably a bug in Django as well; I'll look into that. I still think that GzipFile should handle this gracefully.
msg121112 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2010-11-13 05:00
No, the __init__ argument default value is the standard way of indicating "this argument was not specified". It is not in any way a value for 'mode'.
msg121113 - (view) Author: Brad Greenlee (bgreenlee) Date: 2010-11-13 05:11
Yes, but if I actually passed mode=None in, the behavior would be the same, no?
msg121120 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2010-11-13 08:45
Yes. However, if None were a valid value for mode, then the would would instead do something like: SENTINEL = object() class GZipFile... def __init__(self, filename=None, mode=SENTINEL, ... and then where None currently appears in the logic of the method, mode would be checked against SENTINEL to see if no value had been passed to the mode argument. Thus the presence of None in the __init__ signature actually indicates that None is explicitly *not* a valid value for mode (and thus can safely be used as a sentinel value).
msg121153 - (view) Author: Brad Greenlee (bgreenlee) Date: 2010-11-13 22:54
Understood. I just felt that fileobj.mode == None should be handled the same way that GzipFile(...,mode=None) is handled. I've submitted a patch to Django: http://code.djangoproject.com/ticket/14681
History
Date User Action Args
2022-04-11 14:57:08 admin set github: 54601
2010-11-13 22:54:12 bgreenlee set messages: +
2010-11-13 08:45:00 r.david.murray set messages: +
2010-11-13 05:11:14 bgreenlee set messages: +
2010-11-13 05:00:06 r.david.murray set status: open -> closedmessages: +
2010-11-13 04:54:54 bgreenlee set status: pending -> openmessages: +
2010-11-13 03:24:23 r.david.murray set status: open -> pendingtype: crash -> behaviorversions: - Python 2.6, Python 2.5, Python 3.3nosy: + r.david.murray, pitroumessages: + resolution: not a bugstage: resolved
2010-11-12 05:58:43 bgreenlee create