Skip to content

Commit 5375774

Browse files
committed
refactor(string): remove pm.asUCS4 method as encoding conversion is done automatically
1 parent 521c4e2 commit 5375774

File tree

5 files changed

+5
-43
lines changed

5 files changed

+5
-43
lines changed

include/modules/pythonmonkey/pythonmonkey.hh

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,6 @@ void handleSharedPythonMonkeyMemory(JSContext *cx, JSGCStatus status, JS::GCReas
6464
*/
6565
static PyObject *collect(PyObject *self, PyObject *args);
6666

67-
/**
68-
* @brief Function exposed by the python module to convert UTF16 strings to UCS4 strings
69-
*
70-
* @param self - Pointer to the module object
71-
* @param args - Pointer to the python tuple of arguments (expected to contain a UTF16-encoded string as the first element)
72-
* @return PyObject* - A new python string in UCS4 encoding
73-
*/
74-
static PyObject *asUCS4(PyObject *self, PyObject *args);
75-
7667
/**
7768
* @brief Function exposed by the python module for evaluating arbitrary JS code
7869
*

python/pythonmonkey/pythonmonkey.pyi

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,6 @@ def collect() -> None:
1616
Calls the spidermonkey garbage collector
1717
"""
1818

19-
@_typing.overload
20-
def asUCS4(utf16_str: str, /) -> str:
21-
"""
22-
Expects a python string in UTF16 encoding, and returns a new equivalent string in UCS4.
23-
Undefined behaviour if the string is not in UTF16.
24-
"""
25-
@_typing.overload
26-
def asUCS4(anything_else: _typing.Any, /) -> _typing.NoReturn: ...
27-
2819
class bigint(int):
2920
"""
3021
Representing JavaScript BigInt in Python

src/modules/pythonmonkey/pythonmonkey.cc

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -142,21 +142,6 @@ static PyObject *collect(PyObject *self, PyObject *args) {
142142
Py_RETURN_NONE;
143143
}
144144

145-
static PyObject *asUCS4(PyObject *self, PyObject *args) {
146-
PyObject *str = PyTuple_GetItem(args, 0);
147-
if (!PyUnicode_Check(str)) {
148-
PyErr_SetString(PyExc_TypeError, "pythonmonkey.asUCS4 expects a string as its first argument");
149-
return NULL;
150-
}
151-
152-
PyObject *ucs4Str = StrType::asUCS4(str);
153-
if (!ucs4Str) {
154-
PyErr_SetString(PyExc_UnicodeTranslateError, "string contains an unpaired surrogates");
155-
return NULL;
156-
}
157-
return ucs4Str;
158-
}
159-
160145
static bool getEvalOption(PyObject *evalOptions, const char *optionName, const char **s_p) {
161146
PyObject *value;
162147

@@ -287,7 +272,6 @@ PyMethodDef PythonMonkeyMethods[] = {
287272
{"eval", eval, METH_VARARGS, "Javascript evaluator in Python"},
288273
{"isCompilableUnit", isCompilableUnit, METH_VARARGS, "Hint if a string might be compilable Javascript"},
289274
{"collect", collect, METH_VARARGS, "Calls the spidermonkey garbage collector"},
290-
{"asUCS4", asUCS4, METH_VARARGS, "Expects a python string in UTF16 encoding, and returns a new equivalent string in UCS4. Undefined behaviour if the string is not in UTF16."},
291275
{NULL, NULL, 0, NULL}
292276
};
293277

tests/python/test_pythonmonkey_eval.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ def test_eval_functions_ucs4_string_args():
201201
codepoint = random.randint(0x010000, 0x10FFFF)
202202
string2 += chr(codepoint)
203203

204-
assert pm.asUCS4(concatenate(string1, string2)) == (string1 + string2)
204+
assert concatenate(string1, string2) == (string1 + string2)
205205

206206
def test_eval_functions_roundtrip():
207207
# BF-60 https://github.com/Distributive-Network/PythonMonkey/pull/18

tests/python/test_strings.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ def test_eval_unpaired_surrogate_string_matches_evaluated_string():
2929

3030
def test_eval_ucs4_string_matches_evaluated_string():
3131
py_ucs4_string = "🀄🀛🜢"
32-
js_utf16_string = pm.eval(repr(py_ucs4_string))
33-
js_ucs4_string = pm.asUCS4(js_utf16_string)
32+
js_ucs4_string = pm.eval(repr(py_ucs4_string))
3433
assert py_ucs4_string == js_ucs4_string
3534

3635
def test_eval_latin1_string_fuzztest():
@@ -111,8 +110,7 @@ def test_eval_ucs4_string_fuzztest():
111110
INITIAL_STRING = string1
112111
m = 10
113112
for _ in range(m):
114-
utf16_string2 = pm.eval("'" + string1 + "'")
115-
string2 = pm.asUCS4(utf16_string2)
113+
string2 = pm.eval("'" + string1 + "'")
116114
assert len(string1) == length
117115
assert len(string2) == length
118116
assert len(string1) == len(string2)
@@ -158,8 +156,7 @@ def test_eval_boxed_unpaired_surrogate_string_matches_evaluated_string():
158156

159157
def test_eval_boxed_ucs4_string_matches_evaluated_string():
160158
py_ucs4_string = "🀄🀛🜢"
161-
js_utf16_string = pm.eval(f'new String({repr(py_ucs4_string)})')
162-
js_ucs4_string = pm.asUCS4(js_utf16_string)
159+
js_ucs4_string = pm.eval(f'new String({repr(py_ucs4_string)})')
163160
assert py_ucs4_string == js_ucs4_string
164161

165162
def test_eval_boxed_latin1_string_fuzztest():
@@ -240,8 +237,7 @@ def test_eval_boxed_ucs4_string_fuzztest():
240237
INITIAL_STRING = string1
241238
m = 10
242239
for _ in range(m):
243-
utf16_string2 = pm.eval(f'new String("{string1}")')
244-
string2 = pm.asUCS4(utf16_string2)
240+
string2 = pm.eval(f'new String("{string1}")')
245241
assert len(string1) == length
246242
assert len(string2) == length
247243
assert len(string1) == len(string2)

0 commit comments

Comments
 (0)