如何在CPython中实现字符串乘法?

Air*_*Air 5 python string cpython multiplication

Python允许整数乘以字符串:

>>> 'hello' * 5
'hellohellohellohellohello'
Run Code Online (Sandbox Code Playgroud)

如何在CPython中实现?

我特别感谢指向源代码的指针; Mercurial存储库是一个超出我导航能力的迷宫.

iCo*_*dez 5

对于Python 3.x,可以在中找到实现Objects/unicodeobject.c.具体而言,它开始线12175,其中unicode_repeat定义:

static PyObject*
unicode_repeat(PyObject *str, Py_ssize_t len)
{
    PyObject *u;
    Py_ssize_t nchars, n;

    if (len < 1)
        _Py_RETURN_UNICODE_EMPTY();

    /* no repeat, return original string */
    if (len == 1)
        return unicode_result_unchanged(str);

    if (PyUnicode_READY(str) == -1)
        return NULL;

    if (PyUnicode_GET_LENGTH(str) > PY_SSIZE_T_MAX / len) {
        PyErr_SetString(PyExc_OverflowError,
                        "repeated string is too long");
        return NULL;
    }
    nchars = len * PyUnicode_GET_LENGTH(str);

    u = PyUnicode_New(nchars, PyUnicode_MAX_CHAR_VALUE(str));
    if (!u)
        return NULL;
    assert(PyUnicode_KIND(u) == PyUnicode_KIND(str));

    if (PyUnicode_GET_LENGTH(str) == 1) {
        const int kind = PyUnicode_KIND(str);
        const Py_UCS4 fill_char = PyUnicode_READ(kind, PyUnicode_DATA(str), 0);
        if (kind == PyUnicode_1BYTE_KIND) {
            void *to = PyUnicode_DATA(u);
            memset(to, (unsigned char)fill_char, len);
        }
        else if (kind == PyUnicode_2BYTE_KIND) {
            Py_UCS2 *ucs2 = PyUnicode_2BYTE_DATA(u);
            for (n = 0; n < len; ++n)
                ucs2[n] = fill_char;
        } else {
            Py_UCS4 *ucs4 = PyUnicode_4BYTE_DATA(u);
            assert(kind == PyUnicode_4BYTE_KIND);
            for (n = 0; n < len; ++n)
                ucs4[n] = fill_char;
        }
    }
    else {
        /* number of characters copied this far */
        Py_ssize_t done = PyUnicode_GET_LENGTH(str);
        const Py_ssize_t char_size = PyUnicode_KIND(str);
        char *to = (char *) PyUnicode_DATA(u);
        Py_MEMCPY(to, PyUnicode_DATA(str),
                  PyUnicode_GET_LENGTH(str) * char_size);
        while (done < nchars) {
            n = (done <= nchars-done) ? done : nchars-done;
            Py_MEMCPY(to + (done * char_size), to, n * char_size);
            done += n;
        }
    }

    assert(_PyUnicode_CheckConsistency(u, 1));
    return u;
}
Run Code Online (Sandbox Code Playgroud)

后,在线路13703,该功能为提供sq_repeat用于所述槽PySequenceMethods的的PyUnicode对象.


eca*_*mur 5

注意:我将回答Python 3,其中的字符串类型称为PyUnicode。Python 2与之类似。

BINARY_MULTIPLY执行操作码(在中Python/ceval.c)时,可以调用两个可能的插槽:PyNumberMethods.nb_multiplyPySequenceMethods.sq_repeat。(位于PyNumber_Multiply,位于中Objects/abstract.c):

PyObject *
PyNumber_Multiply(PyObject *v, PyObject *w)
{
    PyObject *result = binary_op1(v, w, NB_SLOT(nb_multiply));
    if (result == Py_NotImplemented) {
        // call sq_repeat on either side if available
Run Code Online (Sandbox Code Playgroud)

PyUnicode在中实现后者unicode_repeat

static PyObject*
unicode_repeat(PyObject *str, Py_ssize_t len)
{
    // ...
Run Code Online (Sandbox Code Playgroud)