cpython: 0a985f7c6731 (original) (raw)
Mercurial > cpython
changeset 104829:0a985f7c6731 3.6
Issue #28385: An error message when non-empty format spec is passed to object.__format__ now contains the name of actual type. [#28385]
Serhiy Storchaka storchaka@gmail.com | |
---|---|
date | Sun, 30 Oct 2016 19:37:46 +0200 |
parents | f1abc92a756a(current diff)92cae79fa5d9(diff) |
children | 6e8183abcc35 de8e83262644 |
files | Lib/test/test_builtin.py Lib/test/test_bytes.py Objects/typeobject.c |
diffstat | 3 files changed, 30 insertions(+), 33 deletions(-)[+] [-] Lib/test/test_builtin.py 24 Lib/test/test_bytes.py 9 Objects/typeobject.c 30 |
line wrap: on
line diff
--- a/Lib/test/test_builtin.py +++ b/Lib/test/test_builtin.py @@ -11,6 +11,7 @@ import os import pickle import platform import random +import re import sys import traceback import types @@ -1447,21 +1448,14 @@ class BuiltinTest(unittest.TestCase): # -------------------------------------------------------------------- # Issue #7994: object.format with a non-empty format string is
# deprecated[](#l1.15)
def test_deprecated_format_string(obj, fmt_str, should_raise):[](#l1.16)
if should_raise:[](#l1.17)
self.assertRaises(TypeError, format, obj, fmt_str)[](#l1.18)
else:[](#l1.19)
format(obj, fmt_str)[](#l1.20)
fmt_strs = ['', 's'][](#l1.22)
# disallowed[](#l1.24) class A:[](#l1.25) def __format__(self, fmt_str):[](#l1.26) return format('', fmt_str)[](#l1.27)
for fmt_str in fmt_strs:[](#l1.29)
test_deprecated_format_string(A(), fmt_str, False)[](#l1.30)
self.assertEqual(format(A()), '')[](#l1.31)
self.assertEqual(format(A(), ''), '')[](#l1.32)
self.assertEqual(format(A(), 's'), '')[](#l1.33)
class B: pass @@ -1470,8 +1464,12 @@ class BuiltinTest(unittest.TestCase): pass for cls in [object, B, C]:
for fmt_str in fmt_strs:[](#l1.41)
test_deprecated_format_string(cls(), fmt_str, len(fmt_str) != 0)[](#l1.42)
obj = cls()[](#l1.43)
self.assertEqual(format(obj), str(obj))[](#l1.44)
self.assertEqual(format(obj, ''), str(obj))[](#l1.45)
with self.assertRaisesRegex(TypeError,[](#l1.46)
r'\b%s\b' % re.escape(cls.__name__)):[](#l1.47)
format(obj, 's')[](#l1.48) # --------------------------------------------------------------------[](#l1.49)
# make sure we can take a subclass of str as a format spec
--- a/Lib/test/test_bytes.py +++ b/Lib/test/test_bytes.py @@ -1416,6 +1416,15 @@ class AssortedBytesTest(unittest.TestCas self.assertEqual(f(b"'"), '''b"'"''') # ''' self.assertEqual(f(b"'""), r"""b''"'""") # '
- @check_bytes_warnings
- def test_format(self):
for b in b'abc', bytearray(b'abc'):[](#l2.9)
self.assertEqual(format(b), str(b))[](#l2.10)
self.assertEqual(format(b, ''), str(b))[](#l2.11)
with self.assertRaisesRegex(TypeError,[](#l2.12)
r'\b%s\b' % re.escape(type(b).__name__)):[](#l2.13)
format(b, 's')[](#l2.14)
+ def test_compare_bytes_to_bytearray(self): self.assertEqual(b"abc" == bytes(b"abc"), True) self.assertEqual(b"ab" != bytes(b"abc"), True)
--- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -4392,13 +4392,6 @@ PyDoc_STRVAR(object_init_subclass_doc, "The default implementation does nothing. It may be\n" "overridden to extend subclasses.\n"); -/*
- from PEP 3101, this code implements: -
- class object:
def __format__(self, format_spec):[](#l3.11)
return format(str(self), format_spec)[](#l3.12)
-*/ static PyObject * object_format(PyObject *self, PyObject *args) { @@ -4409,22 +4402,19 @@ object_format(PyObject *self, PyObject * if (!PyArg_ParseTuple(args, "U:format", &format_spec)) return NULL;
- /* Issue 7994: If we're converting to a string, we
should reject format specifications */[](#l3.22)
- if (PyUnicode_GET_LENGTH(format_spec) > 0) {
PyErr_Format(PyExc_TypeError,[](#l3.24)
"unsupported format string passed to %.200s.__format__",[](#l3.25)
self->ob_type->tp_name);[](#l3.26)
return NULL;[](#l3.27)
- } self_as_str = PyObject_Str(self); if (self_as_str != NULL) {
/* Issue 7994: If we're converting to a string, we[](#l3.31)
should reject format specifications */[](#l3.32)
if (PyUnicode_GET_LENGTH(format_spec) > 0) {[](#l3.33)
PyErr_SetString(PyExc_TypeError,[](#l3.34)
"non-empty format string passed to object.__format__");[](#l3.35)
goto done;[](#l3.36)
}[](#l3.37)