Issue2523

classification
Title: defaultdict.__getitem__() does not propagate exceptions raised by calling default_factory
Type: behaviour Severity: normal
Components: Library Versions: Jython 2.7
Milestone: Jython 2.7.1
process
Status: pending Resolution: fixed
Dependencies: Superseder:
Assigned To: stefan.richthofer Nosy List: buelgsk8er, stefan.richthofer, zyasoft
Priority: Keywords:

Created on 2016-09-25.19:56:31 by buelgsk8er, last changed 2017-01-12.14:17:23 by stefan.richthofer.

Messages
msg10957 (view) Author: (buelgsk8er) Date: 2016-09-25.19:56:30
How to reproduce
================

in CPython 2.7:

>>> from collections import defaultdict
>>> class FooError(Exception): pass
>>> def factory():
...     raise FooError()
>>> defaultdict(factory).__getitem__(123)
Traceback (most recent call last):
FooError

in Jython 2.7.1b3:

>>> from collections import defaultdict
>>> class FooError(Exception): pass
>>> def factory():
...     raise FooError()
>>> defaultdict(factory).__getitem__(123)
Traceback (most recent call last):
KeyError: 123


Rationale
=========

defaultdict functionality is the same as for the dict class except the
overriden method __missing__ and default_factory instance variable[1], so
d[key] operation should returns or raises whatever is returned or raised
by the __missing__(key) call [2].

And in defaultdict.__missing__() calling default factory raises an exception
this should be propagated unchanged[3].

So d[key] operation on a defaultdict should get whatever default factory raises.

----

[1] https://docs.python.org/2/library/collections.html#collections.defaultdict

... It overrides one method and adds one writable instance variable.
The remaining functionality is the same as for the dict class and is
not documented here.

[2] https://docs.python.org/2/library/stdtypes.html#dict

... The d[key] operation then returns or raises whatever is returned or
raised by the __missing__(key) call.

[3] https://docs.python.org/2/library/collections.html#collections.defaultdict.__missing__

... If calling default_factory raises an exception this exception
is propagated unchanged.
msg10959 (view) Author: (buelgsk8er) Date: 2016-09-25.20:35:58
relevant Pull Request:
https://github.com/jythontools/jython/pull/49
msg10960 (view) Author: Jim Baker (zyasoft) Date: 2016-09-26.16:34:34
Thanks, that looks like a great solution here. What's you name so we can add you to the contributors list? Also this is a fairly small change, but we should have you complete the contributors agreement so we can incorporate:

https://www.python.org/psf/contrib/contrib-form/
msg10962 (view) Author: (buelgsk8er) Date: 2016-10-03.21:34:24
thanks, but I'm afraid I don't want to expose my name under this account. I messed up with my usual username when I signed up here :(

I think it's just small pieces of code and you can just proceed it with them. I relinquish any right with them. (= both of pull requests)
msg11013 (view) Author: Stefan Richthofer (stefan.richthofer) Date: 2017-01-11.15:26:29
I would like to accept this one already now for Jython 2.7.1, given that the release was delayed so much. Also, regrtests go fine (at least not worse than usual) and the change doesn't look like it might break anything. Same for #2522.

If there are no concerns, I'd merge it.
msg11014 (view) Author: Jim Baker (zyasoft) Date: 2017-01-11.16:57:22
+1 Stefan to incorporate
msg11016 (view) Author: Stefan Richthofer (stefan.richthofer) Date: 2017-01-12.14:17:22
Jim: Alright!

Merged PR 49 github.com/jythontools/jython/pull/49 as of github.com/jythontools/jython/commit/03f1f40aa6d14a0906fe2dd9cae48baa9ed551cb

Thanks to buelgsk8er: If you reconsider your statement regarding ACKNOWLEDGEMENTS-file, tell me any time, then we will add you.
History
Date User Action Args
2017-01-12 14:17:23stefan.richthofersetstatus: open -> pending
resolution: fixed
messages: + msg11016
assignee: stefan.richthofer
milestone: Jython 2.7.2 -> Jython 2.7.1
type: behaviour
2017-01-11 16:57:22zyasoftsetmessages: + msg11014
2017-01-11 15:26:29stefan.richthofersetnosy: + stefan.richthofer
messages: + msg11013
2016-10-03 21:34:25buelgsk8ersetmessages: + msg10962
2016-09-30 16:18:22zyasoftsetmilestone: Jython 2.7.2
2016-09-26 16:34:35zyasoftsetnosy: + zyasoft
messages: + msg10960
2016-09-25 20:35:58buelgsk8ersetmessages: + msg10959
2016-09-25 19:56:31buelgsk8ercreate