-
-
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
mailbox.Mailbox does odd hasattr() check #64928
Comments
Only noticed because I was searching the stdlib for hasattr calls, but in mailbox.Mailbox.update(), a check is done thus: if hasattr(arg, 'iteritems'):
source = arg.items()
elif hasattr(arg, 'items'):
source = arg.items()
else:
source = arg If this is meant to support Python 2, it should probably use iteritems() in the first branch, but for Python 3, it's probably simpler to just drop the first check altogether: if hasattr(arg, 'items'):
source = arg.items()
else:
source = arg Or possibly switch to EAFP:
|
Actually it should be iteritems(). This is meant to support Mailbox, which has both iteritems() and items() methods. iteritems() returns an iterator and items() returns a list. Looks as changeset f340cb045bf9 was incorrectly applied to mailbox. Here is a patch which partially reverts changeset f340cb045bf9 for the mailbox module and fixes tests in 3.x. Perhaps we should change items() to return an iterator in Python 4.0. |
You have opened the door on a slight mess. The mailbox module provides a set + dict interface to on-disk mailbax files in various formats. The hg annotate and revision history commands indicate that most of 3.4 mailbox is unchanged since the trunk version was merged into py3k on 2006-5-27 in rev38453. However, on 2007-2-11 Guido changed .iterxxx to .xxx throughout the stdlib in rev40809. The bug you note is a side-effect of this patch. It overall left mailbax in a somewhat inconsistent state as it did *not* update the mailbox dict API by removing the mailbox.iterkeys/values/items methods and replacing the .keys/values/items methods. As a result, the mailbox methods that space efficiently iterated thru self.iterkeys now iterate through self.keys == list(self.iterkeys). Example: To fix this, I think we should either
|
Do you expect many use cases that rely on items(), keys(), and values() being lists? |
Note that there is a difference between Mailbox and dict interface: __iter__() clear() should use keys(), not iterkeys(), because it modifies iterated dict. |
If there are no objections I'll commit this patch. |
Committing the patch seems like the right thing to do at this point in time. |
New changeset 5fd1f8271e8a by Serhiy Storchaka in branch '3.4': New changeset acb30ed7eceb by Serhiy Storchaka in branch 'default': |
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: