[Python-3000] Warning about future-unsafe usage patterns in Python 2.x e.g. dict.keys().sort() (original) (raw)
David Hopwood david.nospam.hopwood at blueyonder.co.uk
Mon Aug 28 17:33:31 CEST 2006
- Previous message: [Python-3000] Warning about future-unsafe usage patterns in Python 2.x e.g. dict.keys().sort()
- Next message: [Python-3000] Warning about future-unsafe usage patterns in Python 2.x e.g. dict.keys().sort()
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Brian Quinlan wrote:
It is my understanding that, in Python 3000, certain functions and methods that currently return lists will return some sort of view type (e.g. dict.values()) or an iterator (e.g. zip). So certain usage patterns will no longer be supported e.g. d.keys().sort().
The attached patch, which is a diff against the subversion "trunk" of Python 2.x, tries to warn the user about these kind of future-unsafe usage patterns. It works by storing the type that the list will become in the future, at creation time, and checking to see if called list functions will be supported by that type in the future.
+1 on the idea of the patch.
Some nitpicking:
+#define PYREMAINLIST 0x01 /* List will remain a list in Py2K */
"in Py3K".
+ /* XXX This should be PyExcPendingDeprecationWarning */ + if (PyErrWarnEx(PyExcDeprecationWarning, message, 1) < 0) + return -1;
Why isn't it PyExc_PendingDeprecationWarning?
_+#define WARNLISTUSAGE(self, supportedtypes, operation) _ _+ if (warnfutureusage((PyListObject *) self, _ _+ supportedtypes, operation) < 0) _ + return NULL; + _+#define WARNLISTUSAGEINT(self, supportedtypes, operation) _ _+ if (warnfutureusage((PyListObject *) self, _ _+ supportedtypes, operation) < 0) _ + return -1;
These are macros that hide control flow. In this case I don't think that the difference in verbosity between, say,
if (warn_future_usage(a, PY_REMAIN_LIST | PY_BECOME_DICTVIEW, "len") < 0)
return -1;and
WARN_LIST_USAGE_INT(a, PY_REMAIN_LIST | PY_BECOME_DICTVIEW, "len");is sufficient to justify hiding the return in a macro.
(The cast to PyListObject * is not needed: you have the same cast within warn_future_usage, so its 'self' argument could just as well be declared as PyObject *.)
The 'operation' string is sometimes a gerund ("slicing", etc.) and sometimes the name of a method. This should be more consistent.
+ WARNLISTUSAGE(a, PYREMAINLIST, "repitition");
"repetition"
-- David Hopwood <david.nospam.hopwood at blueyonder.co.uk>
- Previous message: [Python-3000] Warning about future-unsafe usage patterns in Python 2.x e.g. dict.keys().sort()
- Next message: [Python-3000] Warning about future-unsafe usage patterns in Python 2.x e.g. dict.keys().sort()
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]