FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[3.11] gh-89653: PEP 670: Fix PyUnicode_READ() cast (GH-92872) by miss-islington · Pull Request #92884 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .cpp  (1) .h  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
3 changes: 2 additions & 1 deletion Include/cpython/unicodeobject.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,8 @@ static inline Py_UCS4 PyUnicode_READ(int kind,
}
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
#define PyUnicode_READ(kind, data, index) \
PyUnicode_READ(_Py_STATIC_CAST(int, kind), _Py_CAST(const void*, data), \
PyUnicode_READ(_Py_STATIC_CAST(int, kind), \
_Py_STATIC_CAST(const void*, data), \
(index))
#endif

Expand Down
31 changes: 31 additions & 0 deletions Lib/test/_testcppext.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
};

Expand Down

Back | FazBrowse Home | New Git URL