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: Strange error reporting with "with" statement
Type: behavior Stage:
Components: Interpreter Core, Library (Lib) Versions: Python 3.1, Python 3.2, Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: amaury.forgeotdarc, benjamin.peterson, georg.brandl, pitrou, stutzbach
Priority: normal Keywords:

Created on 2010-09-24 15:41 by pitrou, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (4)
msg117299 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-09-24 15:41
Under 3.2 and 3.1:

>>> with open("foo", "wb") as x: pass
... 
>>> with open("foo", "wb") as (x, y): pass
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
io.UnsupportedOperation: read

Similar oddities under 2.7:

>>> with open("foo", "wb") as (x, y): pass
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IOError: File not open for reading
>>> with io.open("foo", "wb") as (x, y): pass
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
io.UnsupportedOperation: read
msg117307 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2010-09-24 16:39
Of course, you get the same error with:

>>> f = open('foo', 'wb')
>>> x, y = f
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IOError: File not open for reading

i.e. the tuple assignment iterates over the file, and calls readline()
msg117314 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2010-09-24 17:33
I would say it's strange but correct. (and unavoidable I think)
msg117351 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2010-09-25 10:22
At least not without losing generality, and we don't like that.
History
Date User Action Args
2022-04-11 14:57:06adminsetgithub: 54149
2010-09-25 10:22:15georg.brandlsetnosy: + georg.brandl
messages: + msg117351
2010-09-24 17:33:41benjamin.petersonsetstatus: open -> closed
resolution: not a bug
messages: + msg117314
2010-09-24 16:39:45amaury.forgeotdarcsetnosy: + amaury.forgeotdarc
messages: + msg117307
2010-09-24 15:41:54pitroucreate