This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: make build_opener raise exception when not passed a handler
Type: Stage:
Components: Library (Lib) Versions: Python 2.6
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: georg.brandl, jjlee, silentstrike
Priority: normal Keywords: patch

Created on 2007-07-10 04:14 by silentstrike, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
add_handler_rejects_bogus_handlers.patch silentstrike, 2007-07-10 04:14
Messages (4)
msg52814 - (view) Author: Robert Renaud (silentstrike) Date: 2007-07-10 04:14
A bug bit me where I had code that looked like

urllib2.build_opener(func_returning_cookie_jar())

instead of

urllib2.build_opener(HTTPCookieProcessor(func_returning_cookie_jar())

the subsequent http request went through just fine, except it didn't send the cookie.  Nothing complained, except me, now.

Throwing the exception deeper in the code made the unittest fail, so I suspect the "silently ignore handlers which don't actually do any handling" behavior might be by design, but it seems terrible to me.
msg52815 - (view) Author: John J Lee (jjlee) Date: 2007-07-11 23:16
Patch #1752270 seems more idiomatic (TypeError, not ValueError) and mildly less intrusive (it does not change the return value of urllib2.OpenerDirector.add_handler()).  Also, your patch adds a backwards incompatibility which the new one does not: Currently a correct call to urllib2.build_opener() may return successfully without adding all of its handler arguments to the returned urllib2.OpenerDirector, since a urllib2.BaseHandler instance may have no appropriate methods (no .http_open(), etc. etc.).  Your patch causes build_opener() (but not .add_handler()) to raise ValueError in that case.  One could argue that it should raise an exception, but there is still scope for breakage, e.g. since these attributes may be added at runtime, and the number of added attributes may happen to be zero (and in fact, urllib2.ProxyHandler falls into this category).

I think the docs do not need to be updated if my revised patch is applied, since they already state that urllib2.OpenerDirector.add_handler() accepts only urllib2.BaseHandler instances (and typically the possibility of a TypeError being raised is not explicitly documented in stdlib docs).
msg52816 - (view) Author: Robert Renaud (silentstrike) Date: 2007-07-11 23:52
Your patch looks good to me, I have no objection so long as this error will be caught instead of failing without complaint.  I am rejecting my patch in favor of yours.  Thanks for looking into this.
msg52817 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2007-07-12 08:05
Committed #1752270.
History
Date User Action Args
2022-04-11 14:56:25adminsetgithub: 45161
2007-07-10 04:14:47silentstrikecreate