Make Py3k warnings consistent w.r.t. punctuation; also respect the

EOL 80 limit and supply more alternatives in warning messages.
This commit is contained in:
Georg Brandl 2008-03-25 08:29:14 +00:00
parent 80055f6295
commit d5b635f196
16 changed files with 67 additions and 54 deletions

View File

@ -93,7 +93,7 @@ A new command-line switch, :option:`-3`, enables warnings
about features that will be removed in Python 3.0. You can run code about features that will be removed in Python 3.0. You can run code
with this switch to see how much work will be necessary to port with this switch to see how much work will be necessary to port
code to 3.0. The value of this switch is available code to 3.0. The value of this switch is available
to Python code as the boolean variable ``sys.py3kwarning``, to Python code as the boolean variable :data:`sys.py3kwarning`,
and to C extension code as :cdata:`Py_Py3kWarningFlag`. and to C extension code as :cdata:`Py_Py3kWarningFlag`.
Python 3.0 adds several new built-in functions and change the Python 3.0 adds several new built-in functions and change the

View File

@ -11,21 +11,21 @@ if not sys.py3kwarning:
class TestPy3KWarnings(unittest.TestCase): class TestPy3KWarnings(unittest.TestCase):
def test_type_inequality_comparisons(self): def test_type_inequality_comparisons(self):
expected = 'type inequality comparisons not supported in 3.x.' expected = 'type inequality comparisons not supported in 3.x'
with catch_warning() as w: with catch_warning() as w:
self.assertWarning(int < str, w, expected) self.assertWarning(int < str, w, expected)
with catch_warning() as w: with catch_warning() as w:
self.assertWarning(type < object, w, expected) self.assertWarning(type < object, w, expected)
def test_object_inequality_comparisons(self): def test_object_inequality_comparisons(self):
expected = 'comparing unequal types not supported in 3.x.' expected = 'comparing unequal types not supported in 3.x'
with catch_warning() as w: with catch_warning() as w:
self.assertWarning(str < [], w, expected) self.assertWarning(str < [], w, expected)
with catch_warning() as w: with catch_warning() as w:
self.assertWarning(object() < (1, 2), w, expected) self.assertWarning(object() < (1, 2), w, expected)
def test_dict_inequality_comparisons(self): def test_dict_inequality_comparisons(self):
expected = 'dict inequality comparisons not supported in 3.x.' expected = 'dict inequality comparisons not supported in 3.x'
with catch_warning() as w: with catch_warning() as w:
self.assertWarning({} < {2:3}, w, expected) self.assertWarning({} < {2:3}, w, expected)
with catch_warning() as w: with catch_warning() as w:
@ -36,7 +36,7 @@ class TestPy3KWarnings(unittest.TestCase):
self.assertWarning({2:3} >= {}, w, expected) self.assertWarning({2:3} >= {}, w, expected)
def test_cell_inequality_comparisons(self): def test_cell_inequality_comparisons(self):
expected = 'cell comparisons not supported in 3.x.' expected = 'cell comparisons not supported in 3.x'
def f(x): def f(x):
def g(): def g():
return x return x
@ -49,7 +49,7 @@ class TestPy3KWarnings(unittest.TestCase):
self.assertWarning(cell0 < cell1, w, expected) self.assertWarning(cell0 < cell1, w, expected)
def test_code_inequality_comparisons(self): def test_code_inequality_comparisons(self):
expected = 'code inequality comparisons not supported in 3.x.' expected = 'code inequality comparisons not supported in 3.x'
def f(x): def f(x):
pass pass
def g(x): def g(x):
@ -65,7 +65,7 @@ class TestPy3KWarnings(unittest.TestCase):
def test_builtin_function_or_method_comparisons(self): def test_builtin_function_or_method_comparisons(self):
expected = ('builtin_function_or_method ' expected = ('builtin_function_or_method '
'inequality comparisons not supported in 3.x.') 'inequality comparisons not supported in 3.x')
func = eval func = eval
meth = {}.get meth = {}.get
with catch_warning() as w: with catch_warning() as w:
@ -81,7 +81,7 @@ class TestPy3KWarnings(unittest.TestCase):
self.assertEqual(str(warning.message), expected_message) self.assertEqual(str(warning.message), expected_message)
def test_sort_cmp_arg(self): def test_sort_cmp_arg(self):
expected = "In 3.x, the cmp argument is no longer supported." expected = "the cmp argument is not supported in 3.x"
lst = range(5) lst = range(5)
cmp = lambda x,y: -1 cmp = lambda x,y: -1
@ -95,7 +95,7 @@ class TestPy3KWarnings(unittest.TestCase):
self.assertWarning(sorted(lst, cmp), w, expected) self.assertWarning(sorted(lst, cmp), w, expected)
def test_sys_exc_clear(self): def test_sys_exc_clear(self):
expected = 'sys.exc_clear() not supported in 3.x. Use except clauses.' expected = 'sys.exc_clear() not supported in 3.x; use except clauses'
with catch_warning() as w: with catch_warning() as w:
self.assertWarning(sys.exc_clear(), w, expected) self.assertWarning(sys.exc_clear(), w, expected)
@ -119,7 +119,7 @@ class TestPy3KWarnings(unittest.TestCase):
self.assertWarning(set(), w, expected) self.assertWarning(set(), w, expected)
def test_buffer(self): def test_buffer(self):
expected = 'buffer will be removed in 3.x' expected = 'buffer() not supported in 3.x; use memoryview()'
with catch_warning() as w: with catch_warning() as w:
self.assertWarning(buffer('a'), w, expected) self.assertWarning(buffer('a'), w, expected)

View File

@ -231,7 +231,8 @@ buffer_new(PyTypeObject *type, PyObject *args, PyObject *kw)
{ {
if (Py_Py3kWarningFlag && if (Py_Py3kWarningFlag &&
PyErr_WarnEx(PyExc_DeprecationWarning, PyErr_WarnEx(PyExc_DeprecationWarning,
"buffer will be removed in 3.x", 1) < 0) "buffer() not supported in 3.x; "
"use memoryview()", 1) < 0)
return NULL; return NULL;
PyObject *ob; PyObject *ob;

View File

@ -55,8 +55,9 @@ static int
cell_compare(PyCellObject *a, PyCellObject *b) cell_compare(PyCellObject *a, PyCellObject *b)
{ {
/* Py3K warning for comparisons */ /* Py3K warning for comparisons */
if (Py_Py3kWarningFlag && PyErr_Warn(PyExc_DeprecationWarning, if (Py_Py3kWarningFlag &&
"cell comparisons not supported in 3.x.") < 0) { PyErr_Warn(PyExc_DeprecationWarning,
"cell comparisons not supported in 3.x") < 0) {
return -2; return -2;
} }

View File

@ -338,9 +338,12 @@ code_richcompare(PyObject *self, PyObject *other, int op)
!PyCode_Check(self) || !PyCode_Check(self) ||
!PyCode_Check(other)) { !PyCode_Check(other)) {
/* Py3K warning if types are not equal and comparison isn't == or != */ /* Py3K warning if types are not equal and comparison
if (Py_Py3kWarningFlag && PyErr_Warn(PyExc_DeprecationWarning, isn't == or != */
"code inequality comparisons not supported in 3.x.") < 0) { if (Py_Py3kWarningFlag &&
PyErr_Warn(PyExc_DeprecationWarning,
"code inequality comparisons not supported "
"in 3.x") < 0) {
return NULL; return NULL;
} }

View File

@ -1778,8 +1778,10 @@ dict_richcompare(PyObject *v, PyObject *w, int op)
} }
else { else {
/* Py3K warning if comparison isn't == or != */ /* Py3K warning if comparison isn't == or != */
if (Py_Py3kWarningFlag && PyErr_Warn(PyExc_DeprecationWarning, if (Py_Py3kWarningFlag &&
"dict inequality comparisons not supported in 3.x.") < 0) { PyErr_Warn(PyExc_DeprecationWarning,
"dict inequality comparisons not supported "
"in 3.x") < 0) {
return NULL; return NULL;
} }
res = Py_NotImplemented; res = Py_NotImplemented;
@ -1811,7 +1813,8 @@ dict_has_key(register PyDictObject *mp, PyObject *key)
{ {
if (Py_Py3kWarningFlag && if (Py_Py3kWarningFlag &&
PyErr_Warn(PyExc_DeprecationWarning, PyErr_Warn(PyExc_DeprecationWarning,
"dict.has_key() not supported in 3.x") < 0) "dict.has_key() not supported in 3.x; "
"use the in operator") < 0)
return NULL; return NULL;
return dict_contains(mp, key); return dict_contains(mp, key);
} }

View File

@ -190,10 +190,10 @@ static PyObject *
BaseException_getitem(PyBaseExceptionObject *self, Py_ssize_t index) BaseException_getitem(PyBaseExceptionObject *self, Py_ssize_t index)
{ {
if (Py_Py3kWarningFlag) { if (Py_Py3kWarningFlag) {
if (PyErr_Warn(PyExc_DeprecationWarning, if (PyErr_Warn(PyExc_DeprecationWarning,
"In 3.x, __getitem__ is not supported for exception " "__getitem__ not supported for exception "
"classes, use args attribute") == -1) "classes in 3.x; use args attribute") == -1)
return NULL; return NULL;
} }
return PySequence_GetItem(self->args, index); return PySequence_GetItem(self->args, index);
} }
@ -203,10 +203,10 @@ BaseException_getslice(PyBaseExceptionObject *self,
Py_ssize_t start, Py_ssize_t stop) Py_ssize_t start, Py_ssize_t stop)
{ {
if (Py_Py3kWarningFlag) { if (Py_Py3kWarningFlag) {
if (PyErr_Warn(PyExc_DeprecationWarning, if (PyErr_Warn(PyExc_DeprecationWarning,
"In 3.x, __getslice__ is not supported for exception " "__getslice__ not supported for exception "
"classes, use args attribute") == -1) "classes in 3.x; use args attribute") == -1)
return NULL; return NULL;
} }
return PySequence_GetSlice(self->args, start, stop); return PySequence_GetSlice(self->args, start, stop);
} }

View File

@ -2040,7 +2040,7 @@ listsort(PyListObject *self, PyObject *args, PyObject *kwds)
if (compare != NULL && if (compare != NULL &&
Py_Py3kWarningFlag && Py_Py3kWarningFlag &&
PyErr_Warn(PyExc_DeprecationWarning, PyErr_Warn(PyExc_DeprecationWarning,
"In 3.x, the cmp argument is no longer supported.") < 0) "the cmp argument is not supported in 3.x") < 0)
return NULL; return NULL;
if (keyfunc == Py_None) if (keyfunc == Py_None)
keyfunc = NULL; keyfunc = NULL;

View File

@ -235,9 +235,10 @@ meth_richcompare(PyObject *self, PyObject *other, int op)
!PyCFunction_Check(other)) !PyCFunction_Check(other))
{ {
/* Py3K warning if types are not equal and comparison isn't == or != */ /* Py3K warning if types are not equal and comparison isn't == or != */
if (Py_Py3kWarningFlag && PyErr_Warn(PyExc_DeprecationWarning, if (Py_Py3kWarningFlag &&
"builtin_function_or_method " PyErr_Warn(PyExc_DeprecationWarning,
"inequality comparisons not supported in 3.x.") < 0) { "builtin_function_or_method inequality "
"comparisons not supported in 3.x") < 0) {
return NULL; return NULL;
} }
@ -353,12 +354,10 @@ Py_FindMethodInChain(PyMethodChain *chain, PyObject *self, const char *name)
{ {
if (name[0] == '_' && name[1] == '_') { if (name[0] == '_' && name[1] == '_') {
if (strcmp(name, "__methods__") == 0) { if (strcmp(name, "__methods__") == 0) {
if (Py_Py3kWarningFlag) { if (Py_Py3kWarningFlag &&
if (PyErr_Warn(PyExc_DeprecationWarning, PyErr_Warn(PyExc_DeprecationWarning,
"__methods__ not supported " "__methods__ not supported in 3.x") < 0)
"in 3.x") < 0) return NULL;
return NULL;
}
return listmethodchain(chain); return listmethodchain(chain);
} }
if (strcmp(name, "__doc__") == 0) { if (strcmp(name, "__doc__") == 0) {

View File

@ -867,9 +867,10 @@ try_3way_to_rich_compare(PyObject *v, PyObject *w, int op)
/* Py3K warning if types are not equal and comparison isn't == or != */ /* Py3K warning if types are not equal and comparison isn't == or != */
if (Py_Py3kWarningFlag && if (Py_Py3kWarningFlag &&
v->ob_type != w->ob_type && op != Py_EQ && op != Py_NE && v->ob_type != w->ob_type && op != Py_EQ && op != Py_NE &&
PyErr_Warn(PyExc_DeprecationWarning, PyErr_Warn(PyExc_DeprecationWarning,
"comparing unequal types not supported in 3.x.") < 0) { "comparing unequal types not supported "
"in 3.x") < 0) {
return NULL; return NULL;
} }
@ -1691,8 +1692,8 @@ merge_list_attr(PyObject* dict, PyObject* obj, const char *attrname)
(strcmp(attrname, "__members__") == 0 || (strcmp(attrname, "__members__") == 0 ||
strcmp(attrname, "__methods__") == 0)) { strcmp(attrname, "__methods__") == 0)) {
if (PyErr_Warn(PyExc_DeprecationWarning, if (PyErr_Warn(PyExc_DeprecationWarning,
"__members__ and __methods__ not supported " "__members__ and __methods__ not "
"in 3.x") < 0) { "supported in 3.x") < 0) {
Py_XDECREF(list); Py_XDECREF(list);
return -1; return -1;
} }

View File

@ -609,7 +609,8 @@ type_richcompare(PyObject *v, PyObject *w, int op)
/* Py3K warning if comparison isn't == or != */ /* Py3K warning if comparison isn't == or != */
if (Py_Py3kWarningFlag && op != Py_EQ && op != Py_NE && if (Py_Py3kWarningFlag && op != Py_EQ && op != Py_NE &&
PyErr_Warn(PyExc_DeprecationWarning, PyErr_Warn(PyExc_DeprecationWarning,
"type inequality comparisons not supported in 3.x.") < 0) { "type inequality comparisons not supported "
"in 3.x") < 0) {
return NULL; return NULL;
} }

View File

@ -1531,7 +1531,7 @@ tok_get(register struct tok_state *tok, char **p_start, char **p_end)
#ifndef PGEN #ifndef PGEN
if (Py_Py3kWarningFlag && token == NOTEQUAL && c == '<') { if (Py_Py3kWarningFlag && token == NOTEQUAL && c == '<') {
if (PyErr_WarnExplicit(PyExc_DeprecationWarning, if (PyErr_WarnExplicit(PyExc_DeprecationWarning,
"<> not supported in 3.x", "<> not supported in 3.x; use !=",
tok->filename, tok->lineno, tok->filename, tok->lineno,
NULL, NULL)) { NULL, NULL)) {
return ERRORTOKEN; return ERRORTOKEN;

View File

@ -1363,7 +1363,7 @@ ast_for_atom(struct compiling *c, const node *n)
expr_ty expression; expr_ty expression;
if (Py_Py3kWarningFlag) { if (Py_Py3kWarningFlag) {
if (PyErr_WarnExplicit(PyExc_DeprecationWarning, if (PyErr_WarnExplicit(PyExc_DeprecationWarning,
"backquote not supported in 3.x", "backquote not supported in 3.x; use repr()",
c->c_filename, LINENO(n), c->c_filename, LINENO(n),
NULL, NULL)) { NULL, NULL)) {
return NULL; return NULL;

View File

@ -166,7 +166,8 @@ builtin_apply(PyObject *self, PyObject *args)
if (Py_Py3kWarningFlag && if (Py_Py3kWarningFlag &&
PyErr_Warn(PyExc_DeprecationWarning, PyErr_Warn(PyExc_DeprecationWarning,
"apply() not supported in 3.x. Use func(*args, **kwargs).") < 0) "apply() not supported in 3.x; "
"use func(*args, **kwargs)") < 0)
return NULL; return NULL;
if (!PyArg_UnpackTuple(args, "apply", 1, 3, &func, &alist, &kwdict)) if (!PyArg_UnpackTuple(args, "apply", 1, 3, &func, &alist, &kwdict))
@ -225,7 +226,8 @@ builtin_callable(PyObject *self, PyObject *v)
{ {
if (Py_Py3kWarningFlag && if (Py_Py3kWarningFlag &&
PyErr_Warn(PyExc_DeprecationWarning, PyErr_Warn(PyExc_DeprecationWarning,
"callable() not supported in 3.x. Use hasattr(o, '__call__').") < 0) "callable() not supported in 3.x; "
"use hasattr(o, '__call__')") < 0)
return NULL; return NULL;
return PyBool_FromLong((long)PyCallable_Check(v)); return PyBool_FromLong((long)PyCallable_Check(v));
} }
@ -684,7 +686,7 @@ builtin_execfile(PyObject *self, PyObject *args)
if (Py_Py3kWarningFlag && if (Py_Py3kWarningFlag &&
PyErr_Warn(PyExc_DeprecationWarning, PyErr_Warn(PyExc_DeprecationWarning,
"execfile() not supported in 3.x. Use exec().") < 0) "execfile() not supported in 3.x; use exec()") < 0)
return NULL; return NULL;
if (!PyArg_ParseTuple(args, "s|O!O:execfile", if (!PyArg_ParseTuple(args, "s|O!O:execfile",
@ -912,7 +914,8 @@ builtin_map(PyObject *self, PyObject *args)
if (func == Py_None) { if (func == Py_None) {
if (Py_Py3kWarningFlag && if (Py_Py3kWarningFlag &&
PyErr_Warn(PyExc_DeprecationWarning, PyErr_Warn(PyExc_DeprecationWarning,
"map(None, ...) not supported in 3.x. Use list(...).") < 0) "map(None, ...) not supported in 3.x; "
"use list(...)") < 0)
return NULL; return NULL;
if (n == 1) { if (n == 1) {
/* map(None, S) is the same as list(S). */ /* map(None, S) is the same as list(S). */
@ -1934,7 +1937,8 @@ builtin_reduce(PyObject *self, PyObject *args)
if (Py_Py3kWarningFlag && if (Py_Py3kWarningFlag &&
PyErr_Warn(PyExc_DeprecationWarning, PyErr_Warn(PyExc_DeprecationWarning,
"reduce() not supported in 3.x") < 0) "reduce() not supported in 3.x; "
"use functools.reduce()") < 0)
return NULL; return NULL;
if (!PyArg_UnpackTuple(args, "reduce", 2, 3, &func, &seq, &result)) if (!PyArg_UnpackTuple(args, "reduce", 2, 3, &func, &seq, &result))
@ -2011,7 +2015,7 @@ builtin_reload(PyObject *self, PyObject *v)
{ {
if (Py_Py3kWarningFlag && if (Py_Py3kWarningFlag &&
PyErr_Warn(PyExc_DeprecationWarning, PyErr_Warn(PyExc_DeprecationWarning,
"reload() not supported in 3.x") < 0) "reload() not supported in 3.x; use imp.reload()") < 0)
return NULL; return NULL;
return PyImport_ReloadModule(v); return PyImport_ReloadModule(v);

View File

@ -4056,7 +4056,7 @@ assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
PyType_FastSubclass((PyTypeObject*)(x), Py_TPFLAGS_BASE_EXC_SUBCLASS)) PyType_FastSubclass((PyTypeObject*)(x), Py_TPFLAGS_BASE_EXC_SUBCLASS))
#define CANNOT_CATCH_MSG "catching classes that don't inherit from " \ #define CANNOT_CATCH_MSG "catching classes that don't inherit from " \
"BaseException is not allowed in 3.x." "BaseException is not allowed in 3.x"
static PyObject * static PyObject *
cmp_outcome(int op, register PyObject *v, register PyObject *w) cmp_outcome(int op, register PyObject *v, register PyObject *w)

View File

@ -174,8 +174,8 @@ sys_exc_clear(PyObject *self, PyObject *noargs)
if (Py_Py3kWarningFlag && if (Py_Py3kWarningFlag &&
PyErr_Warn(PyExc_DeprecationWarning, PyErr_Warn(PyExc_DeprecationWarning,
"sys.exc_clear() not supported in 3.x. " "sys.exc_clear() not supported in 3.x; "
"Use except clauses.") < 0) "use except clauses") < 0)
return NULL; return NULL;
tstate = PyThreadState_GET(); tstate = PyThreadState_GET();