Coverage Report

Created: 2026-12-01 06:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cpython/Objects/interpolationobject.c
Line
Count
Source
1
/* t-string Interpolation object implementation */
2
3
#include "Python.h"
4
#include "pycore_initconfig.h"    // _PyStatus_OK
5
#include "pycore_interpolation.h"
6
#include "pycore_typeobject.h"    // _PyType_GetDict
7
8
static int
9
_conversion_converter(PyObject *arg, PyObject **conversion)
10
0
{
11
0
    if (arg == Py_None) {
12
0
        return 1;
13
0
    }
14
15
0
    if (!PyUnicode_Check(arg)) {
16
0
        PyErr_Format(PyExc_TypeError,
17
0
            "Interpolation() argument 'conversion' must be str, not %T",
18
0
            arg);
19
0
        return 0;
20
0
    }
21
22
0
    Py_ssize_t len;
23
0
    const char *conv_str = PyUnicode_AsUTF8AndSize(arg, &len);
24
0
    if (len != 1 || !(conv_str[0] == 'a' || conv_str[0] == 'r' || conv_str[0] == 's')) {
25
0
        PyErr_SetString(PyExc_ValueError,
26
0
            "Interpolation() argument 'conversion' must be one of 's', 'a' or 'r'");
27
0
        return 0;
28
0
    }
29
30
0
    *conversion = arg;
31
0
    return 1;
32
0
}
33
34
#include "clinic/interpolationobject.c.h"
35
36
/*[clinic input]
37
class Interpolation "interpolationobject *" "&_PyInterpolation_Type"
38
[clinic start generated code]*/
39
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=161c64a16f9c4544]*/
40
41
typedef struct {
42
    PyObject_HEAD
43
    PyObject *value;
44
    PyObject *expression;
45
    PyObject *conversion;
46
    PyObject *format_spec;
47
} interpolationobject;
48
49
#define interpolationobject_CAST(op) \
50
4
    (assert(_PyInterpolation_CheckExact(op)), _Py_CAST(interpolationobject*, (op)))
51
52
/*[clinic input]
53
@vectorcall
54
@classmethod
55
Interpolation.__new__ as interpolation_new
56
57
    value: object
58
    expression: object(subclass_of='&PyUnicode_Type', c_default='&_Py_STR(empty)') = ""
59
    conversion: object(converter='_conversion_converter') = None
60
    format_spec: object(subclass_of='&PyUnicode_Type', c_default='&_Py_STR(empty)') = ""
61
[clinic start generated code]*/
62
63
static PyObject *
64
interpolation_new_impl(PyTypeObject *type, PyObject *value,
65
                       PyObject *expression, PyObject *conversion,
66
                       PyObject *format_spec)
67
/*[clinic end generated code: output=6488e288765bc1a9 input=de7e890ea8f801e5]*/
68
0
{
69
0
    interpolationobject *self = PyObject_GC_New(interpolationobject, type);
70
0
    if (!self) {
71
0
        return NULL;
72
0
    }
73
74
0
    self->value = Py_NewRef(value);
75
0
    self->expression = Py_NewRef(expression);
76
0
    self->conversion = Py_NewRef(conversion);
77
0
    self->format_spec = Py_NewRef(format_spec);
78
0
    PyObject_GC_Track(self);
79
0
    return (PyObject *) self;
80
0
}
81
82
static void
83
interpolation_dealloc(PyObject *op)
84
4
{
85
4
    PyObject_GC_UnTrack(op);
86
4
    Py_TYPE(op)->tp_clear(op);
87
4
    Py_TYPE(op)->tp_free(op);
88
4
}
89
90
static int
91
interpolation_clear(PyObject *op)
92
4
{
93
4
    interpolationobject *self = interpolationobject_CAST(op);
94
4
    Py_CLEAR(self->value);
95
4
    Py_CLEAR(self->expression);
96
4
    Py_CLEAR(self->conversion);
97
4
    Py_CLEAR(self->format_spec);
98
4
    return 0;
99
4
}
100
101
static int
102
interpolation_traverse(PyObject *op, visitproc visit, void *arg)
103
0
{
104
0
    interpolationobject *self = interpolationobject_CAST(op);
105
0
    Py_VISIT(self->value);
106
0
    Py_VISIT(self->expression);
107
0
    Py_VISIT(self->conversion);
108
0
    Py_VISIT(self->format_spec);
109
0
    return 0;
110
0
}
111
112
static PyObject *
113
interpolation_repr(PyObject *op)
114
0
{
115
0
    interpolationobject *self = interpolationobject_CAST(op);
116
0
    return PyUnicode_FromFormat("%s(%R, %R, %R, %R)",
117
0
                                _PyType_Name(Py_TYPE(self)), self->value, self->expression,
118
0
                                self->conversion, self->format_spec);
119
0
}
120
121
static PyMemberDef interpolation_members[] = {
122
    {"value", Py_T_OBJECT_EX, offsetof(interpolationobject, value), Py_READONLY, "Value"},
123
    {"expression", Py_T_OBJECT_EX, offsetof(interpolationobject, expression), Py_READONLY, "Expression"},
124
    {"conversion", Py_T_OBJECT_EX, offsetof(interpolationobject, conversion), Py_READONLY, "Conversion"},
125
    {"format_spec", Py_T_OBJECT_EX, offsetof(interpolationobject, format_spec), Py_READONLY, "Format specifier"},
126
    {NULL}
127
};
128
129
static PyObject*
130
interpolation_reduce(PyObject *op, PyObject *Py_UNUSED(dummy))
131
0
{
132
0
    interpolationobject *self = interpolationobject_CAST(op);
133
0
    return Py_BuildValue("(O(OOOO))", (PyObject *)Py_TYPE(op),
134
0
                         self->value, self->expression,
135
0
                         self->conversion, self->format_spec);
136
0
}
137
138
static PyMethodDef interpolation_methods[] = {
139
    {"__reduce__", interpolation_reduce, METH_NOARGS,
140
        PyDoc_STR("__reduce__() -> (cls, state)")},
141
    {"__class_getitem__", Py_GenericAlias,
142
        METH_O|METH_CLASS, PyDoc_STR("Interpolations are generic over the types of their values")},
143
    {NULL, NULL},
144
};
145
146
PyTypeObject _PyInterpolation_Type = {
147
    PyVarObject_HEAD_INIT(NULL, 0)
148
    .tp_name = "string.templatelib.Interpolation",
149
    .tp_doc = PyDoc_STR("Interpolation object"),
150
    .tp_basicsize = sizeof(interpolationobject),
151
    .tp_itemsize = 0,
152
    .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
153
    .tp_new = interpolation_new,
154
    .tp_vectorcall = interpolation_vectorcall,
155
    .tp_alloc = PyType_GenericAlloc,
156
    .tp_dealloc = interpolation_dealloc,
157
    .tp_clear = interpolation_clear,
158
    .tp_free = PyObject_GC_Del,
159
    .tp_repr = interpolation_repr,
160
    .tp_members = interpolation_members,
161
    .tp_methods = interpolation_methods,
162
    .tp_traverse = interpolation_traverse,
163
};
164
165
PyStatus
166
_PyInterpolation_InitTypes(PyInterpreterState *interp)
167
37
{
168
37
    PyObject *tuple = Py_BuildValue("(ssss)", "value", "expression", "conversion", "format_spec");
169
37
    if (!tuple) {
170
0
        goto error;
171
0
    }
172
173
37
    PyObject *dict = _PyType_GetDict(&_PyInterpolation_Type);
174
37
    if (!dict) {
175
0
        Py_DECREF(tuple);
176
0
        goto error;
177
0
    }
178
179
37
    int status = PyDict_SetItemString(dict, "__match_args__", tuple);
180
37
    Py_DECREF(tuple);
181
37
    if (status < 0) {
182
0
        goto error;
183
0
    }
184
37
    return _PyStatus_OK();
185
186
0
error:
187
0
    return _PyStatus_ERR("Can't initialize interpolation types");
188
37
}
189
190
PyObject *
191
_PyInterpolation_Build(PyObject *value, PyObject *str, int conversion, PyObject *format_spec)
192
4
{
193
4
    interpolationobject *interpolation = PyObject_GC_New(interpolationobject, &_PyInterpolation_Type);
194
4
    if (!interpolation) {
195
0
        return NULL;
196
0
    }
197
198
4
    interpolation->value = Py_NewRef(value);
199
4
    interpolation->expression = Py_NewRef(str);
200
4
    interpolation->format_spec = Py_NewRef(format_spec);
201
4
    interpolation->conversion = NULL;
202
203
4
    if (conversion == 0) {
204
4
        interpolation->conversion = Py_None;
205
4
    }
206
0
    else {
207
0
        switch (conversion) {
208
0
            case FVC_ASCII:
209
0
                interpolation->conversion = _Py_LATIN1_CHR('a');
210
0
                break;
211
0
            case FVC_REPR:
212
0
                interpolation->conversion = _Py_LATIN1_CHR('r');
213
0
                break;
214
0
            case FVC_STR:
215
0
                interpolation->conversion = _Py_LATIN1_CHR('s');
216
0
                break;
217
0
            default:
218
0
                PyErr_SetString(PyExc_SystemError,
219
0
                    "Interpolation() argument 'conversion' must be one of 's', 'a' or 'r'");
220
0
                Py_DECREF(interpolation);
221
0
                return NULL;
222
0
        }
223
0
    }
224
225
4
    PyObject_GC_Track(interpolation);
226
4
    return (PyObject *) interpolation;
227
4
}
228
229
PyObject *
230
_PyInterpolation_GetValueRef(PyObject *interpolation)
231
0
{
232
0
    return Py_NewRef(interpolationobject_CAST(interpolation)->value);
233
0
}