[Python-3000] Warning about future-unsafe usage patterns in Python 2.x e.g. dict.keys().sort()
David Hopwood
david.nospam.hopwood at blueyonder.co.uk
Mon Aug 28 17:33:31 CEST 2006
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 PY_REMAIN_LIST 0x01 /* List will remain a list in Py2K */
"in Py3K".
> + /* XXX This should be PyExc_PendingDeprecationWarning */
> + if (PyErr_WarnEx(PyExc_DeprecationWarning, message, 1) < 0)
> + return -1;
Why isn't it PyExc_PendingDeprecationWarning?
> +#define WARN_LIST_USAGE(self, supported_types, operation) \
> + if (warn_future_usage((PyListObject *) self, \
> + supported_types, operation) < 0) \
> + return NULL;
> +
> +#define WARN_LIST_USAGE_INT(self, supported_types, operation) \
> + if (warn_future_usage((PyListObject *) self, \
> + supported_types, 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.
> + WARN_LIST_USAGE(a, PY_REMAIN_LIST, "repitition");
"repetition"
--
David Hopwood <david.nospam.hopwood at blueyonder.co.uk>
More information about the Python-3000
mailing list