gh-156963: Raise TypeError when concatenating complexstr with another… · python/cpython@326f7f7

GitHub

Original file line numberDiff line numberDiff line change@@ -796,6 +796,9 @@ def test_complexstr(self):

796796self.assertEqual(str(s[1:]), 'bc')

797797self.assertEqual(str(s[::-1]), 'cbA')

798798self.assertEqual(str(s+curses.complexstr(['Z'])), 'AbcZ')

799+# Concatenating anything else raises instead of returning NotImplemented.

800+self.assertRaises(TypeError, lambda: s+'Z')

801+self.assertRaises(TypeError, lambda: s+cc('Z'))

799802# The empty complexstr.

800803self.assertEqual(len(curses.complexstr([])), 0)

801804self.assertEqual(str(curses.complexstr('')), '')

Original file line numberDiff line numberDiff line change@@ -1590,7 +1590,10 @@ complexstr_concat(PyObject *a, PyObject *b)

15901590{

15911591cursesmodule_state*state=get_cursesmodule_state_by_cls(Py_TYPE(a));

15921592if (!Py_IS_TYPE(b, state->complexstr_type)) {

1593-Py_RETURN_NOTIMPLEMENTED;

1593+PyErr_Format(PyExc_TypeError,

1594+"can only concatenate complexstr to complexstr, not %T",

1595+b);

1596+returnNULL;

15941597 }

15951598PyCursesComplexStrObject*sa=_PyCursesComplexStrObject_CAST(a);

15961599PyCursesComplexStrObject*sb=_PyCursesComplexStrObject_CAST(b);