[issue30322] PyObject_GetIter does not behave as documented on dict objects
Sam De Meyer
report at bugs.python.org
Tue May 9 16:19:39 EDT 2017
New submission from Sam De Meyer:
According to the docs (https://docs.python.org/3/c-api/object.html) the `PyObject_GetIter` method should be equivalent to the python call `iter(<some_dict>)`, but, when given a dict, the `PyObject_GetIter` returns an iterator over key-value pairs whereas the `iter()` method returns an iterator over keys only.
I tripped over this when giving the `<some_dict>.update()` a dict-like object that does not inherit from the builtin dict and implements its own `__iter__()`.
The `update()` method eventually reaches the following piece of code:
https://hg.python.org/cpython/file/4243df51fe43/Objects/dictobject.c#l2383
> it = PyObject_GetIter(seq2);
> ...
> item = PyIter_Next(it);
> ...
> fast = PySequence_Fast(item, "");
> ...
> key = PySequence_Fast_GET_ITEM(fast, 0);
> value = PySequence_Fast_GET_ITEM(fast, 1);
displaying the difference in behaviour between `PyObject_GetIter` and `iter(o)`.
----------
assignee: docs at python
components: Documentation
messages: 293346
nosy: Sam De Meyer, docs at python
priority: normal
severity: normal
status: open
title: PyObject_GetIter does not behave as documented on dict objects
versions: Python 3.6
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue30322>
_______________________________________
More information about the Python-bugs-list
mailing list