Skip to content

bpo-40736: Improve the error message for re.search() TypeError #23312

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -374,7 +374,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