diff options
| author | Victor Stinner <vstinner@python.org> | 2022-05-17 17:20:37 (GMT) |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-05-17 17:20:37 (GMT) |
| commit | e6fd7992a92879103215b3e9f218fe07212af9b1 (patch) | |
| tree | 1387bb0ba893f41e9b579058d6c04ce10e180931 /Lib/test/_testcppext.cpp | |
| parent | 524f03c08ca6688785c0fe99d8f2b385bf92e58f (diff) | |
| download | cpython-e6fd7992a92879103215b3e9f218fe07212af9b1.zip cpython-e6fd7992a92879103215b3e9f218fe07212af9b1.tar.gz cpython-e6fd7992a92879103215b3e9f218fe07212af9b1.tar.bz2 | |
gh-89653: PEP 670: Fix PyUnicode_READ() cast (#92872)
_Py_CAST() cannot be used with a constant type: use _Py_STATIC_CAST()
instead.
Diffstat (limited to 'Lib/test/_testcppext.cpp')
| -rw-r--r-- | Lib/test/_testcppext.cpp | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/Lib/test/_testcppext.cpp b/Lib/test/_testcppext.cpp index dc40f0e..f38b487 100644 --- a/Lib/test/_testcppext.cpp +++ b/Lib/test/_testcppext.cpp @@ -50,9 +50,40 @@ test_api_casts(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args)) } +static PyObject * +test_unicode(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args)) +{ + PyObject *str = PyUnicode_FromString("abc"); + if (str == nullptr) { + return nullptr; + } + + assert(PyUnicode_Check(str)); + assert(PyUnicode_GET_LENGTH(str) == 3); + + // gh-92800: test PyUnicode_READ() + const void* data = PyUnicode_DATA(str); + assert(data != nullptr); + int kind = PyUnicode_KIND(str); + assert(kind == PyUnicode_1BYTE_KIND); + assert(PyUnicode_READ(kind, data, 0) == 'a'); + + // gh-92800: test PyUnicode_READ() casts + const void* const_data = PyUnicode_DATA(str); + unsigned int ukind = static_cast<unsigned int>(kind); + assert(PyUnicode_READ(ukind, const_data, 2) == 'c'); + + assert(PyUnicode_READ_CHAR(str, 1) == 'b'); + + Py_DECREF(str); + Py_RETURN_NONE; +} + + static PyMethodDef _testcppext_methods[] = { {"add", _testcppext_add, METH_VARARGS, _testcppext_add_doc}, {"test_api_casts", test_api_casts, METH_NOARGS, nullptr}, + {"test_unicode", test_unicode, METH_NOARGS, nullptr}, {nullptr, nullptr, 0, nullptr} /* sentinel */ }; |
