Skip to content

Commit

Permalink
bpo-40736: Improve the error message for re.search() TypeError (pytho…
Browse files Browse the repository at this point in the history
…nGH-23312)

Include the invalid type in the error message.
  • Loading branch information
ZackerySpytz committed May 21, 2021
1 parent 498383c commit 6cc8ac9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
6 changes: 6 additions & 0 deletions Lib/test/test_re.py
Original file line number Diff line number Diff line change
Expand Up @@ -2104,6 +2104,12 @@ def test_bug_34294(self):
{'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):
Expand Down
3 changes: 2 additions & 1 deletion Modules/_sre.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit 6cc8ac9

Please sign in to comment.