From: Zackery Spytz Date: Fri, 21 May 2021 21:02:42 +0000 (-0700) Subject: bpo-40736: Improve the error message for re.search() TypeError (GH-23312) X-Git-Tag: v3.11.0a1~1059 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=6cc8ac949907b9a1c0f73709c6978b7a43e634e3;p=thirdparty%2FPython%2Fcpython.git bpo-40736: Improve the error message for re.search() TypeError (GH-23312) Include the invalid type in the error message. --- diff --git a/Lib/test/test_re.py b/Lib/test/test_re.py index e1b2c7942918..0ed243da3ff9 100644 --- a/Lib/test/test_re.py +++ b/Lib/test/test_re.py @@ -2104,6 +2104,12 @@ ELSE {'tag': 'foo', 'text': None}, {'tag': 'foo', 'text': None}]) + def test_bug_40736(self): + with self.assertRaisesRegex(TypeError, "got 'int'"): + re.search("x*", 5) + with self.assertRaisesRegex(TypeError, "got 'type'"): + re.search("x*", type) + class PatternReprTests(unittest.TestCase): def check(self, pattern, expected): diff --git a/Modules/_sre.c b/Modules/_sre.c index 9d0fc4ab7c03..a313ea19981f 100644 --- a/Modules/_sre.c +++ b/Modules/_sre.c @@ -389,7 +389,8 @@ getstring(PyObject* string, Py_ssize_t* p_length, /* get pointer to byte string buffer */ if (PyObject_GetBuffer(string, view, PyBUF_SIMPLE) != 0) { - PyErr_SetString(PyExc_TypeError, "expected string or bytes-like object"); + PyErr_Format(PyExc_TypeError, "expected string or bytes-like " + "object, got '%.200s'", Py_TYPE(string)->tp_name); return NULL; }