]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-40736: Improve the error message for re.search() TypeError (GH-23312)
authorZackery Spytz <zspytz@gmail.com>
Fri, 21 May 2021 21:02:42 +0000 (14:02 -0700)
committerGitHub <noreply@github.com>
Fri, 21 May 2021 21:02:42 +0000 (22:02 +0100)
Include the invalid type in the error message.

Lib/test/test_re.py
Modules/_sre.c

index e1b2c794291848cf64f9357019c1dd8ef3c24b98..0ed243da3ff9e2ddca7d9164eec0ddd151212da9 100644 (file)
@@ -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):
index 9d0fc4ab7c03ab69de4defd0863d8c45b9501888..a313ea19981fbf1b4470b2598c60d9af66028083 100644 (file)
@@ -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;
     }