Skip to content

Commit 69d0e48

Browse files
committed
refactor(string): do not throw in StrType::asUCS4
1 parent f624778 commit 69d0e48

File tree

2 files changed

+10
-19
lines changed

2 files changed

+10
-19
lines changed

src/StrType.cc

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ StrType::StrType(JSContext *cx, JSString *str) {
106106
PyObject *ucs4Obj = this->asUCS4(); // convert `pyObject` to a new PyUnicodeObject with UCS4 data
107107
if (!ucs4Obj) {
108108
// conversion fails, keep the original `pyObject`
109-
PyErr_Clear();
110109
return;
111110
}
112111
Py_DECREF(pyObject); // cleanup the old `pyObject`
@@ -130,32 +129,19 @@ PyObject *StrType::asUCS4() {
130129
uint32_t ucs4String[length];
131130
size_t ucs4Length = 0;
132131

133-
for (size_t i = 0; i < length; i++) {
134-
if (Py_UNICODE_IS_LOW_SURROGATE(chars[i])) // character is an unpaired low surrogate
135-
{
136-
char hexString[5];
137-
sprintf(hexString, "%x", (unsigned int)chars[i]);
138-
std::string errorString = std::string("string contains an unpaired low surrogate at position: ") + std::to_string(i) + std::string(" with a value of 0x") + hexString;
139-
PyErr_SetString(PyExc_UnicodeTranslateError, errorString.c_str());
132+
for (size_t i = 0; i < length; i++, ucs4Length++) {
133+
if (Py_UNICODE_IS_LOW_SURROGATE(chars[i])) { // character is an unpaired low surrogate
140134
return NULL;
141-
}
142-
else if (Py_UNICODE_IS_HIGH_SURROGATE(chars[i])) { // character is a high surrogate
135+
} else if (Py_UNICODE_IS_HIGH_SURROGATE(chars[i])) { // character is a high surrogate
143136
if ((i + 1 < length) && Py_UNICODE_IS_LOW_SURROGATE(chars[i+1])) { // next character is a low surrogate
144137
ucs4String[ucs4Length] = Py_UNICODE_JOIN_SURROGATES(chars[i], chars[i+1]);
145-
ucs4Length++;
146138
i++; // skip over low surrogate
147139
}
148140
else { // next character is not a low surrogate
149-
char hexString[5];
150-
sprintf(hexString, "%x", (unsigned int)chars[i]);
151-
std::string errorString = std::string("string contains an unpaired high surrogate at position: ") + std::to_string(i) + std::string(" with a value of 0x") + hexString;
152-
PyErr_SetString(PyExc_UnicodeTranslateError, errorString.c_str());
153141
return NULL;
154142
}
155-
}
156-
else { // character is not a surrogate, and is in the BMP
143+
} else { // character is not a surrogate, and is in the BMP
157144
ucs4String[ucs4Length] = chars[i];
158-
ucs4Length++;
159145
}
160146
}
161147

src/modules/pythonmonkey/pythonmonkey.cc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,12 @@ static PyObject *asUCS4(PyObject *self, PyObject *args) {
149149
return NULL;
150150
}
151151

152-
return str->asUCS4();
152+
PyObject *ucs4Str = str->asUCS4();
153+
if (!ucs4Str) {
154+
PyErr_SetString(PyExc_UnicodeTranslateError, "string contains an unpaired surrogates");
155+
return NULL;
156+
}
157+
return ucs4Str;
153158
}
154159

155160
static bool getEvalOption(PyObject *evalOptions, const char *optionName, const char **s_p) {

0 commit comments

Comments
 (0)