Skip to content

Commit d3ea2ec

Browse files
committed
fix(string): short path to fix the embedded null character issue with Python 3.13+
1 parent 6849db8 commit d3ea2ec

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/StrType.cc

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,11 @@ PyObject *StrType::proxifyString(JSContext *cx, JS::HandleValue strVal) {
135135

136136
if (JS::LinearStringHasLatin1Chars(lstr)) { // latin1 spidermonkey, latin1 python
137137
const JS::Latin1Char *chars = JS::GetLatin1LinearStringChars(nogc, lstr);
138-
if (chars[length] != 0) { // not a null-terminated string
139-
// most Python C APIs assume the string buffer is null-terminated, so we need to create a copy
140-
PyObject *copied = PyUnicode_FromObject(pyString); // create a copy when it's not a true Unicode object
138+
if (Py_Version >= 0x030d0000) { // Python version is greater than 3.13
139+
// Short path to temporarily fix the issue with Python 3.13+ compact unicode representation.
140+
// It would error with `ValueError: embedded null character`, which is caused by the fact that
141+
// most Python C APIs assume the string buffer is null-terminated, so we need to create a copy.
142+
PyObject *copied = PyUnicode_FromObject((PyObject *)pyString); // create a copy when it's not a true Unicode object
141143
Py_DECREF(pyString);
142144
return copied;
143145
}
@@ -163,8 +165,8 @@ PyObject *StrType::proxifyString(JSContext *cx, JS::HandleValue strVal) {
163165
}
164166
else { // utf16 spidermonkey, ucs2 python
165167
const char16_t *chars = JS::GetTwoByteLinearStringChars(nogc, lstr);
166-
if (chars[length] != 0) { // not a null-terminated string
167-
PyObject *copied = PyUnicode_FromObject(pyString);
168+
if (Py_Version >= 0x030d0000) { // Python 3.13+, see above
169+
PyObject *copied = PyUnicode_FromObject((PyObject *)pyString); // create a copy when it's not a true Unicode object
168170
Py_DECREF(pyString);
169171
return copied;
170172
}

0 commit comments

Comments
 (0)