-
-
Notifications
You must be signed in to change notification settings - Fork 29.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
urlopen() has a hidden default for its timeout argument #62617
Comments
urllib.request.urlopen() takes a The problem comes if you are passing a set of arguments to urlopen() and want to pass in a timeout that is the same as default. Because its undocumented, you have to UTSL to figure out what the value is, and then you have to use a non-public attribute of the socket module. It would be better if urlopen() was documented to default The documentation should also be updated. Now if you want to call urlopen() with the default values, it would just be |
Targeting only 3.4 because, while this should no break any (reasonable <wink>) backward compatibility, it is technically an API change and thus should not get backported. |
Please see bpo-14425 and then let us know if this is still valid. It's been a while since I looked at the code and I no longer remember the details, but I seemed confident in my conclusion at the time... :) |
See also bpo-4079, if you are concerned about timeouts in urllib. |
On Jul 10, 2013, at 05:53 AM, R. David Murray wrote:
I'm not sure I completely understand the arguments in bpo-14425 and bpo-2451 which Even if socket._GLOBAL_DEFAULT_TIMEOUT is the effective default for I don't see why DEFAULT_TIMEOUT = object() or DEFAULT_TIMEOUT = socket._GLOBAL_DEFAULT_TIMEOUT With the latter, the function's signature would then change to: def urlopen(url, data=None, timeout=DEFAULT_TIMEOUT, ...) and nothing functional would change. The documentation update would be much Then users of this module could do something like this: from urllib.request import DEFAULT_TIMEOUT, urlopen
def my_open(url, timeout=None):
my_timeout = DEFAULT_TIMEOUT if timeout is None else timeout
return urlopen(url, timeout=my_timeout) |
OK, I reviewed the issue enough to remember: If socket.setdefaulttimeout is never called, then the default timeout is None (no timeout). Since socket.setdefaulttimeout is deprecated, the global default should always be None. Therefore, if you want to use the "default" timeout in a new application, you could pass None as the value for timeout...but of course that probably doesn't do what you want. What you *really* want to do is pass the default timeout value being used by your application, whatever that is, managed however your application/library manages it. If you are writing a library that for some reason wants to retain backward compatibility with setdefaulttimeout (and I think you probably shouldn't be), you could call getdefaulttimeout to get the current default value. Since urlopen is creating a new socket object, this should have the correct semantics. Mind, I don't claim that there can't be a better solution, I'm just trying to explain the current state of affairs as I understand it. However, aside from the ugliness of the current signature for urlopen, at the moment it seems reasonable to me :) That said, this argument is premised firmly on setdefaulttimeout being deprecated, and there is currently no indication of that in its documentation that I can see. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: