-
-
Notifications
You must be signed in to change notification settings - Fork 29.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add the iterator protocol to dbm modules #49986
Comments
In Python 2.6, dbm modules othar than bsddb don't support the iterator >>> import dbm
>>> d = dbm.open('spam.dbm', 'c')
>>> for k in range(5): d["key%d" % k] = "value%d" % k
...
>>> for k in d: print k, d[k]
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'dbm.dbm' object is not iterable Adding iterator support would make dbm modules more convenient and |
Attached is a patch that adds the iterator protocol. >>> for k in d: print k, d[k]
...
key1 vale1
key3 vale3
key0 vale0
key2 vale2
key4 vale4 The problem is there is no way to get the internal pointer back to the >>> for k in d: print k, d[k]
... The solution to this would be:
|
Revised patch adds firstkey and nextkey to dbm. |
Would you like to fix gdbm as well? |
Here's another patch which addsd iter to dbm and gdbm. Note that dbm and gdbm C API is a little different. >>> import dbm
>>> d = dbm.open('foo', 'n')
>>> d['k1'] = 'v1';d['k2'] = 'v2';
>>> for i in d: print i; break
...
k1
>>> for i in d: print i
...
k2
>>> for i in d: print i
...
>>> import gdbm
>>> gd = gdbm.open('foo.gdbm', 'n')
>>> gd['k1'] = 'v1';gd['k2'] = 'v2';
>>> for i in gd: print i; break
...
k2
>>> for i in gd: print i
for i in gd: print i
...
k1
>>> for i in gd: print i
...
k2
k1 |
Of course iter should work in the same way in all dbm modules. >>> dumb = dumbdbm.open('foo', 'n')
>>> dumb['k1'] = 'v1';dumb['k2'] = 'v2';
>>> for i in dumb: print i; break
...
k2
>>> for i in dumb: print i
for i in dumb: print i
...
k2
k1
>>> for i in dumb: print i
for i in dumb: print i
...
k2
k1 |
Akira> Note that dbm and gdbm C API is a little different. gdbm_nextkey I don't think this is going to fly. A static variable is not thread-safe. |
skip> What's worse, even in a non-threaded environment you might want to Or iterate over two different gdbm files simultaneously. |
I agree with Skip that using a static variable is not appropriate. The |
Yes, using a static variable there is wrong and dbm iterator should behave just like one in dict. Attached is minimal tests for dbm iterator. |
Another reason this issue is really important, is that the lack of a I suspect that for most users, shelve is the main way they will access Once dbm and gdbm support the iterator protocol, it will be trivial to |
This patch just uses PyObject_GetIter to get an iter. |
This may be superseded by bpo-9523. There are comments and patches in both issues, so I’m not closing either as duplicate of the other. |
bpo-9523 has a more comprehensive patch in progress, adding __iter__ and other mapping methods, so I’m closing this one. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: