classification
Title: Using `with` statement causes dict to start papering over attribute errors
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: charlesc, r.david.murray, tim.golden
Priority: normal Keywords:

Created on 2017-04-25 16:19 by charlesc, last changed 2017-04-25 16:28 by tim.golden. This issue is now closed.

Messages (4)
msg292270 - (view) Author: Charles Cazabon (charlesc) Date: 2017-04-25 16:19
This is a weird one.  I've reproduced it with 3 versions of 2.7, including the latest 2.7.13.  I didn't find an open bug about this, but I had difficulty crafting a search string for it, so I may have missed something.

Basically, using a `with` statement (maybe any such statement, but using an open file definitely does it, even when I do nothing with it) causes the built-in dict class to stop raising AttributeErrors, which can result in odd bugs.

Example:

Python 2.7.13 (default, Apr 25 2017, 10:12:36) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> with sys.stderr as foo:
...     pass
... 
>>> {}.nosuchattribute
>>> {}.nosuchattribute is None
>>> 

I haven't tried the latest 3.x, but it's definitely still there in 3.2.3:

Python 3.2.3 (default, Nov 17 2016, 01:04:00) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> with sys.stderr as foo:
...     pass
... 
>>> {}.nosuchattribute
>>> {}.nosuchattribute is None
>>>
msg292271 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2017-04-25 16:22
You've closed stderr.
msg292272 - (view) Author: Charles Cazabon (charlesc) Date: 2017-04-25 16:25
oh ffs ;)
msg292273 - (view) Author: Tim Golden (tim.golden) * (Python committer) Date: 2017-04-25 16:28
I think you're suppressing sys.stderr after the "with". Try a NameError 
or anything.

Alternatively, try a "with" which isn't using sys.stderr

Obviously, the next question is why *that's* happening.
History
Date User Action Args
2017-04-25 16:28:28tim.goldensetnosy: + tim.golden
messages: + msg292273
2017-04-25 16:25:16charlescsetmessages: + msg292272
2017-04-25 16:22:31r.david.murraysetstatus: open -> closed

nosy: + r.david.murray
messages: + msg292271

resolution: not a bug
stage: resolved
2017-04-25 16:19:38charlesccreate