[issue12955] urllib2.build_opener().open() is not friendly to "with ... as:"

Terry J. Reedy report at bugs.python.org
Fri Sep 16 19:23:33 CEST 2011


Terry J. Reedy <tjreedy at udel.edu> added the comment:

As I understand this, you are asking that 2.7 urllib2.build_opener().open(), which in 3.x is urllib.request.build_opener().open(), be upgraded to return an object that works as a context manager. Unless the docs say that this should already be the case, this is a feature request for 3.3.

I am unable to test whether this feature is already present (in 3.2.2). Your example line "with urllib2.build_opener().open() as:" has an obvious syntax error. When I correct that and adjust for 3.x

import urllib.request as ur
with ur.build_opener().open() as f:
    pass
#
TypeError: open() takes at least 2 arguments (1 given)

The doc for build_opener says it returns an OpenerDirector instance. help(ur.OpenerDirector.open) just says it needs a 'fullurl'. But when I add 'http:www.python.org' as an argument, I get
urllib.error.URLError: <urlopen error no host given>
I do not know what else is needed.

Please copy and paste the ACTUAL (minimal) code you ran to get the AttributeError.

----------
components:  -Documentation, IO
nosy: +terry.reedy
stage:  -> test needed
type: resource usage -> feature request
versions:  -Python 2.6, Python 2.7, Python 3.1, Python 3.2, Python 3.4

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue12955>
_______________________________________


More information about the Python-bugs-list mailing list