Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2cd8ce4

Browse files
committed
Issue #9856: Replace deprecation warinigs to raising TypeError in object.__format__
Patch by Florent Xicluna.
1 parent bf8f2f9 commit 2cd8ce4

3 files changed

Lines changed: 11 additions & 28 deletions

File tree

Lib/test/test_builtin.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1479,17 +1479,11 @@ def __format__(self, format_spec):
14791479
# --------------------------------------------------------------------
14801480
# Issue #7994: object.__format__ with a non-empty format string is
14811481
# deprecated
1482-
def test_deprecated_format_string(obj, fmt_str, should_raise_warning):
1483-
with warnings.catch_warnings(record=True) as w:
1484-
warnings.simplefilter("always", DeprecationWarning)
1485-
format(obj, fmt_str)
1486-
if should_raise_warning:
1487-
self.assertEqual(len(w), 1)
1488-
self.assertIsInstance(w[0].message, DeprecationWarning)
1489-
self.assertIn('object.__format__ with a non-empty format '
1490-
'string', str(w[0].message))
1482+
def test_deprecated_format_string(obj, fmt_str, should_raise):
1483+
if should_raise:
1484+
self.assertRaises(TypeError, format, obj, fmt_str)
14911485
else:
1492-
self.assertEqual(len(w), 0)
1486+
format(obj, fmt_str)
14931487

14941488
fmt_strs = ['', 's']
14951489

Lib/test/test_unicode.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -842,11 +842,9 @@ def __format__(self, format_spec):
842842
self.assertEqual('{0:d}'.format(G('data')), 'G(data)')
843843
self.assertEqual('{0!s}'.format(G('data')), 'string is data')
844844

845-
msg = 'object.__format__ with a non-empty format string is deprecated'
846-
with support.check_warnings((msg, DeprecationWarning)):
847-
self.assertEqual('{0:^10}'.format(E('data')), ' E(data) ')
848-
self.assertEqual('{0:^10s}'.format(E('data')), ' E(data) ')
849-
self.assertEqual('{0:>15s}'.format(G('data')), ' string is data')
845+
self.assertRaises(TypeError, '{0:^10}'.format, E('data'))
846+
self.assertRaises(TypeError, '{0:^10s}'.format, E('data'))
847+
self.assertRaises(TypeError, '{0:>15s}'.format, G('data'))
850848

851849
self.assertEqual("{0:date: %Y-%m-%d}".format(I(year=2007,
852850
month=8,

Objects/typeobject.c

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3654,16 +3654,9 @@ object_format(PyObject *self, PyObject *args)
36543654
/* Issue 7994: If we're converting to a string, we
36553655
should reject format specifications */
36563656
if (PyUnicode_GET_LENGTH(format_spec) > 0) {
3657-
if (PyErr_WarnEx(PyExc_DeprecationWarning,
3658-
"object.__format__ with a non-empty format "
3659-
"string is deprecated", 1) < 0) {
3660-
goto done;
3661-
}
3662-
/* Eventually this will become an error:
3663-
PyErr_Format(PyExc_TypeError,
3657+
PyErr_SetString(PyExc_TypeError,
36643658
"non-empty format string passed to object.__format__");
3665-
goto done;
3666-
*/
3659+
goto done;
36673660
}
36683661

36693662
result = PyObject_Format(self_as_str, format_spec);
@@ -4288,13 +4281,11 @@ PyType_Ready(PyTypeObject *type)
42884281
/* Warn for a type that implements tp_compare (now known as
42894282
tp_reserved) but not tp_richcompare. */
42904283
if (type->tp_reserved && !type->tp_richcompare) {
4291-
int error;
4292-
error = PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
4284+
PyErr_Format(PyExc_TypeError,
42934285
"Type %.100s defines tp_reserved (formerly tp_compare) "
42944286
"but not tp_richcompare. Comparisons may not behave as intended.",
42954287
type->tp_name);
4296-
if (error == -1)
4297-
goto error;
4288+
goto error;
42984289
}
42994290

43004291
/* All done -- set the ready flag */

0 commit comments

Comments
 (0)