Skip to content

Commit f5a3846

Browse files
committed
fix(string): Python 3.8 and 3.9 do not have Py_NewRef
1 parent 5375774 commit f5a3846

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/StrType.cc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
#define PY_UNICODE_OBJECT_READY(op) (PY_ASCII_OBJECT_CAST(op)->state.ready)
3434
#endif
3535

36+
/**
37+
* @brief check if UTF-16 encoded `chars` contain a surrogate pair
38+
*/
3639
static bool containsSurrogatePair(const char16_t *chars, size_t length) {
3740
for (size_t i = 0; i < length; i++) {
3841
if (Py_UNICODE_IS_SURROGATE(chars[i])) {
@@ -109,7 +112,8 @@ StrType::StrType(JSContext *cx, JSString *str) {
109112
return;
110113
}
111114
Py_DECREF(pyObject); // cleanup the old `pyObject`
112-
pyObject = Py_NewRef(ucs4Obj);
115+
Py_INCREF(ucs4Obj); // XXX: Same as the above `Py_INCREF(pyObject);`. Why double freed on GC?
116+
pyObject = ucs4Obj;
113117
}
114118
}
115119
}
@@ -121,7 +125,9 @@ const char *StrType::getValue() const {
121125
/* static */
122126
PyObject *StrType::asUCS4(PyObject *pyObject) {
123127
if (PyUnicode_KIND(pyObject) != PyUnicode_2BYTE_KIND) {
124-
return Py_NewRef(pyObject);
128+
// return a new reference to match the behaviour of `PyUnicode_FromKindAndData`
129+
Py_INCREF(pyObject);
130+
return pyObject;
125131
}
126132

127133
uint16_t *chars = PY_UNICODE_OBJECT_DATA_UCS2(pyObject);

0 commit comments

Comments
 (0)