Skip to content

Commit

Permalink
[mypyc] Fix bytes join on Python 3.13
Browse files Browse the repository at this point in the history
`_PyBytes_Join` is no longer available.

Work on mypyc/mypyc#1056.
  • Loading branch information
JukkaL authored and cdce8p committed Jul 14, 2024
1 parent bb8584a commit 4281e71
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions mypyc/lib-rt/bytes_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,26 @@ PyObject *CPyBytes_GetSlice(PyObject *obj, CPyTagged start, CPyTagged end) {
return CPyObject_GetSlice(obj, start, end);
}

// Like _PyBytes_Join but fallback to dynamic call if 'sep' is not bytes
// (mostly commonly, for bytearrays)
PyObject *CPyBytes_Join(PyObject *sep, PyObject *iter) {
#if CPY_3_13_FEATURES
PyObject *args[2] = {sep, iter};
_Py_IDENTIFIER(join);
PyObject *method = _PyUnicode_FromId(&PyId_join); /* borrowed */
if (method == NULL) {
return NULL;
}
PyObject *res = PyObject_VectorcallMethod(
method, args, 2 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
return res;
#else
// Fallback to dynamic call if 'sep' is not bytes (most commonly, for bytearrays)
if (PyBytes_CheckExact(sep)) {
return _PyBytes_Join(sep, iter);
} else {
_Py_IDENTIFIER(join);
return _PyObject_CallMethodIdOneArg(sep, &PyId_join, iter);
}
#endif
}

PyObject *CPyBytes_Build(Py_ssize_t len, ...) {
Expand Down

0 comments on commit 4281e71

Please sign in to comment.