8000 [mypyc] Fix bytes join on Python 3.13 by JukkaL · Pull Request #17527 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

[mypyc] Fix bytes join on Python 3.13 #17527

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might fail with 3.8. PyObject_VectorcallMethod was provisional there. The leading underscore was removed in 3.9. _PyObject_VectorcallMethod is available as alias in 3.13 for backwards compatibility.

Copy link
Collaborator
@hauntsaninja hauntsaninja Jul 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is guarded by CPY_3_13_FEATURES so should be fine on that front, right?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, missed that. Although now _PyBytes_Join doesn't need to be fixed for 3.13 anymore. See python/cpython#122287

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
Loading
0