Issue 7994: Make object.__format__() raise a PendingDeprecationWarning

if the format string is not empty. Manually merge r79596 and r84772
from 2.x.

Also, apparently test_format() from test_builtin never made it into
3.x. I've added it as well. It tests the basic format()
infrastructure.
This commit is contained in:
Eric Smith 2010-09-13 20:48:43 +00:00
parent af9d10aa30
commit e4d6317c87
4 changed files with 144 additions and 6 deletions

View file

@ -3315,9 +3315,26 @@ object_format(PyObject *self, PyObject *args)
return NULL;
self_as_str = PyObject_Str(self);
if (self_as_str != NULL)
result = PyObject_Format(self_as_str, format_spec);
if (self_as_str != NULL) {
/* Issue 7994: If we're converting to a string, we
should reject format specifications */
if (PyUnicode_GET_SIZE(format_spec) > 0) {
if (PyErr_WarnEx(PyExc_PendingDeprecationWarning,
"object.__format__ with a non-empty format "
"string is deprecated", 1) < 0) {
goto done;
}
/* Eventually this will become an error:
PyErr_Format(PyExc_TypeError,
"non-empty format string passed to object.__format__");
goto done;
*/
}
result = PyObject_Format(self_as_str, format_spec);
}
done:
Py_XDECREF(self_as_str);
return result;