@@ -50,9 +50,40 @@ test_api_casts(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
5050}
5151
5252
53+ static PyObject *
54+ test_unicode (PyObject *Py_UNUSED (module ), PyObject *Py_UNUSED(args))
55+ {
56+ PyObject *str = PyUnicode_FromString (" abc" );
57+ if (str == nullptr ) {
58+ return nullptr ;
59+ }
60+
61+ assert (PyUnicode_Check (str));
62+ assert (PyUnicode_GET_LENGTH (str) == 3 );
63+
64+ // gh-92800: test PyUnicode_READ()
65+ const void * data = PyUnicode_DATA (str);
66+ assert (data != nullptr );
67+ int kind = PyUnicode_KIND (str);
68+ assert (kind == PyUnicode_1BYTE_KIND);
69+ assert (PyUnicode_READ (kind, data, 0 ) == ' a' );
70+
71+ // gh-92800: test PyUnicode_READ() casts
72+ const void * const_data = PyUnicode_DATA (str);
73+ unsigned int ukind = static_cast <unsigned int >(kind);
74+ assert (PyUnicode_READ (ukind, const_data, 2 ) == ' c' );
75+
76+ assert (PyUnicode_READ_CHAR (str, 1 ) == ' b' );
77+
78+ Py_DECREF (str);
79+ Py_RETURN_NONE;
80+ }
81+
82+
5383static PyMethodDef _testcppext_methods[] = {
5484 {" add" , _testcppext_add, METH_VARARGS, _testcppext_add_doc},
5585 {" test_api_casts" , test_api_casts, METH_NOARGS, nullptr },
86+ {" test_unicode" , test_unicode, METH_NOARGS, nullptr },
5687 {nullptr , nullptr , 0 , nullptr } /* sentinel */
5788};
5889
0 commit comments