Skip to content
Prev Previous commit
Next Next commit
Use assert() for unicode objects
  • Loading branch information
sobolevn committed Oct 7, 2022
commit 4b2447e7233d714b0462cf7944aee349a27b6059
10 changes: 10 additions & 0 deletions Lib/test/test_unicode.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,10 @@ def test_count(self):
self.checkequal(0, 'a' * 10, 'count', 'a\u0102')
self.checkequal(0, 'a' * 10, 'count', 'a\U00100304')
self.checkequal(0, '\u0102' * 10, 'count', '\u0102\U00100304')
# test subclass
class MyStr(str):
pass
self.checkequal(3, MyStr('aaa'), 'count', 'a')

def test_find(self):
string_tests.CommonTest.test_find(self)
Expand Down Expand Up @@ -2983,6 +2987,12 @@ def test_count(self):
self.assertEqual(unicode_count(uni, ch, 0, len(uni)), 1)
self.assertEqual(unicode_count(st, ch, 0, len(st)), 0)

# subclasses should still work
class MyStr(str):
pass

self.assertEqual(unicode_count(MyStr('aab'), 'a', 0, 3), 2)

# Test PyUnicode_FindChar()
@support.cpython_only
@unittest.skipIf(_testcapi is None, 'need _testcapi module')
Expand Down
5 changes: 3 additions & 2 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -8970,8 +8970,9 @@ any_unicode_count(PyObject *str,
Py_ssize_t start,
Py_ssize_t end)
{
// You must ensure that `str` and `substr` are both unicode objects
// before calling this function.
assert(PyUnicode_Check(str));
assert(PyUnicode_Check(substr));

Py_ssize_t result;
int kind1, kind2;
const void *buf1 = NULL, *buf2 = NULL;
Expand Down