Document that `urllib.request.install_opener(None)` will uninstall the global opener

Currently, there is no documented way to uninstall a global opener installed with urllib.request.install_opener(...), however, it seems that either of the following will uninstall the global opener:

  • urllib.request.install_opener(None): This will set urllib.request._opener to its initial value, None.

  • urllib.request.urlcleanup(): This will also set urllib.request._opener to its initial value, None. (see https://github.com/python/cpython/issues/102591 for more info)

The documentation for neither of these indicates that they will uninstall global openers installed with urllib.request.install_opener(...) - so relying on them doing it may not be that wise. However, I think it would be quite useful to have a documented way of uninstalling openers, and I think that urllib.request.install_opener(None) would be best to do that.

1 Like

The doc should not reference the private attribute ._opener. You purpose would be fulfilled by adding a sentence after the current first sentence.

Install an OpenerDirector instance as the default global opener. Pass None to uninstall a previously install opener.

The only question is whether we want to guarantee this behavior. Seems reasonable to me, but I suggest another opinion before making a PR.

@gpshead @Senthil

1 Like

Agreed on this point, I did not mean to suggest it should reference it either, I just mentioned it to point out that those methods will uninstall the global opener, even though it is not documented to be the case.

Your suggestion will be entirely satisfactory.

1 Like

Agreed, documenting these existing behaviors makes sense. Someone is presumably already relying upon them.

1 Like