@@ -50,9 +50,40 @@ test_api_casts(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
50
50
}
51
51
52
52
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
+
53
83
static PyMethodDef _testcppext_methods[] = {
54
84
{" add" , _testcppext_add, METH_VARARGS, _testcppext_add_doc},
55
85
{" test_api_casts" , test_api_casts, METH_NOARGS, nullptr },
86
+ {" test_unicode" , test_unicode, METH_NOARGS, nullptr },
56
87
{nullptr , nullptr , 0 , nullptr } /* sentinel */
57
88
};
58
89
0 commit comments