/src/cpython/Objects/templateobject.c
Line | Count | Source |
1 | | /* t-string Template object implementation */ |
2 | | |
3 | | #include "Python.h" |
4 | | #include "pycore_interpolation.h" // _PyInterpolation_CheckExact() |
5 | | #include "pycore_runtime.h" // _Py_STR() |
6 | | #include "pycore_template.h" |
7 | | |
8 | | #include "clinic/templateobject.c.h" |
9 | | |
10 | | /*[clinic input] |
11 | | class Template "templateobject *" "&_PyTemplate_Type" |
12 | | [clinic start generated code]*/ |
13 | | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=ee41f8d8c49f0271]*/ |
14 | | |
15 | | typedef struct { |
16 | | PyObject_HEAD |
17 | | PyObject *stringsiter; |
18 | | PyObject *interpolationsiter; |
19 | | int from_strings; |
20 | | } templateiterobject; |
21 | | |
22 | | #define templateiterobject_CAST(op) \ |
23 | 0 | (assert(_PyTemplateIter_CheckExact(op)), _Py_CAST(templateiterobject*, (op))) |
24 | | |
25 | | static PyObject * |
26 | | templateiter_next(PyObject *op) |
27 | 0 | { |
28 | 0 | templateiterobject *self = templateiterobject_CAST(op); |
29 | 0 | PyObject *item; |
30 | 0 | if (self->from_strings) { |
31 | 0 | item = PyIter_Next(self->stringsiter); |
32 | 0 | self->from_strings = 0; |
33 | 0 | if (item == NULL) { |
34 | 0 | return NULL; |
35 | 0 | } |
36 | 0 | if (PyUnicode_GET_LENGTH(item) == 0) { |
37 | 0 | Py_SETREF(item, PyIter_Next(self->interpolationsiter)); |
38 | 0 | self->from_strings = 1; |
39 | 0 | } |
40 | 0 | } |
41 | 0 | else { |
42 | 0 | item = PyIter_Next(self->interpolationsiter); |
43 | 0 | self->from_strings = 1; |
44 | 0 | } |
45 | 0 | return item; |
46 | 0 | } |
47 | | |
48 | | static void |
49 | | templateiter_dealloc(PyObject *op) |
50 | 0 | { |
51 | 0 | PyObject_GC_UnTrack(op); |
52 | 0 | Py_TYPE(op)->tp_clear(op); |
53 | 0 | Py_TYPE(op)->tp_free(op); |
54 | 0 | } |
55 | | |
56 | | static int |
57 | | templateiter_clear(PyObject *op) |
58 | 0 | { |
59 | 0 | templateiterobject *self = templateiterobject_CAST(op); |
60 | 0 | Py_CLEAR(self->stringsiter); |
61 | 0 | Py_CLEAR(self->interpolationsiter); |
62 | 0 | return 0; |
63 | 0 | } |
64 | | |
65 | | static int |
66 | | templateiter_traverse(PyObject *op, visitproc visit, void *arg) |
67 | 0 | { |
68 | 0 | templateiterobject *self = templateiterobject_CAST(op); |
69 | 0 | Py_VISIT(self->stringsiter); |
70 | 0 | Py_VISIT(self->interpolationsiter); |
71 | 0 | return 0; |
72 | 0 | } |
73 | | |
74 | | PyTypeObject _PyTemplateIter_Type = { |
75 | | PyVarObject_HEAD_INIT(NULL, 0) |
76 | | .tp_name = "string.templatelib.TemplateIter", |
77 | | .tp_doc = PyDoc_STR("Template iterator object"), |
78 | | .tp_basicsize = sizeof(templateiterobject), |
79 | | .tp_itemsize = 0, |
80 | | .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, |
81 | | .tp_alloc = PyType_GenericAlloc, |
82 | | .tp_dealloc = templateiter_dealloc, |
83 | | .tp_clear = templateiter_clear, |
84 | | .tp_free = PyObject_GC_Del, |
85 | | .tp_traverse = templateiter_traverse, |
86 | | .tp_iter = PyObject_SelfIter, |
87 | | .tp_iternext = templateiter_next, |
88 | | }; |
89 | | |
90 | | typedef struct { |
91 | | PyObject_HEAD |
92 | | PyObject *strings; |
93 | | PyObject *interpolations; |
94 | | } templateobject; |
95 | | |
96 | | #define templateobject_CAST(op) \ |
97 | 11 | (assert(_PyTemplate_CheckExact(op)), _Py_CAST(templateobject*, (op))) |
98 | | |
99 | | /*[clinic input] |
100 | | @vectorcall |
101 | | @classmethod |
102 | | Template.__new__ as template_new |
103 | | *args: array |
104 | | [clinic start generated code]*/ |
105 | | |
106 | | static PyObject * |
107 | | template_new_impl(PyTypeObject *type, PyObject * const *args, |
108 | | Py_ssize_t args_length) |
109 | | /*[clinic end generated code: output=5831a1056a1a68d5 input=65f04e91969ffccd]*/ |
110 | 0 | { |
111 | 0 | Py_ssize_t stringslen = 0; |
112 | 0 | Py_ssize_t interpolationslen = 0; |
113 | 0 | int last_was_str = 0; |
114 | |
|
115 | 0 | for (Py_ssize_t i = 0; i < args_length; i++) { |
116 | 0 | PyObject *item = args[i]; |
117 | 0 | if (PyUnicode_Check(item)) { |
118 | 0 | if (!last_was_str) { |
119 | 0 | stringslen++; |
120 | 0 | } |
121 | 0 | last_was_str = 1; |
122 | 0 | } |
123 | 0 | else if (_PyInterpolation_CheckExact(item)) { |
124 | 0 | if (!last_was_str) { |
125 | 0 | stringslen++; |
126 | 0 | } |
127 | 0 | interpolationslen++; |
128 | 0 | last_was_str = 0; |
129 | 0 | } |
130 | 0 | else { |
131 | 0 | PyErr_Format( |
132 | 0 | PyExc_TypeError, |
133 | 0 | "Template.__new__ *args need to be of type 'str' or 'Interpolation', got %T", |
134 | 0 | item); |
135 | 0 | return NULL; |
136 | 0 | } |
137 | 0 | } |
138 | 0 | if (!last_was_str) { |
139 | 0 | stringslen++; |
140 | 0 | } |
141 | |
|
142 | 0 | PyObject *strings = PyTuple_New(stringslen); |
143 | 0 | if (!strings) { |
144 | 0 | return NULL; |
145 | 0 | } |
146 | | |
147 | 0 | PyObject *interpolations = PyTuple_New(interpolationslen); |
148 | 0 | if (!interpolations) { |
149 | 0 | Py_DECREF(strings); |
150 | 0 | return NULL; |
151 | 0 | } |
152 | | |
153 | 0 | last_was_str = 0; |
154 | 0 | Py_ssize_t stringsidx = 0, interpolationsidx = 0; |
155 | 0 | for (Py_ssize_t i = 0; i < args_length; i++) { |
156 | 0 | PyObject *item = args[i]; |
157 | 0 | if (PyUnicode_Check(item)) { |
158 | 0 | if (last_was_str) { |
159 | 0 | PyObject *laststring = PyTuple_GET_ITEM(strings, stringsidx - 1); |
160 | 0 | PyObject *concat = PyUnicode_Concat(laststring, item); |
161 | 0 | if (!concat) { |
162 | 0 | Py_DECREF(strings); |
163 | 0 | Py_DECREF(interpolations); |
164 | 0 | return NULL; |
165 | 0 | } |
166 | | /* Replace laststring with concat */ |
167 | 0 | PyTuple_SET_ITEM(strings, stringsidx - 1, concat); |
168 | 0 | Py_DECREF(laststring); |
169 | 0 | } |
170 | 0 | else { |
171 | 0 | PyTuple_SET_ITEM(strings, stringsidx++, Py_NewRef(item)); |
172 | 0 | } |
173 | 0 | last_was_str = 1; |
174 | 0 | } |
175 | 0 | else if (_PyInterpolation_CheckExact(item)) { |
176 | 0 | if (!last_was_str) { |
177 | 0 | _Py_DECLARE_STR(empty, ""); |
178 | 0 | PyTuple_SET_ITEM(strings, stringsidx++, &_Py_STR(empty)); |
179 | 0 | } |
180 | 0 | PyTuple_SET_ITEM(interpolations, interpolationsidx++, Py_NewRef(item)); |
181 | 0 | last_was_str = 0; |
182 | 0 | } |
183 | 0 | } |
184 | 0 | if (!last_was_str) { |
185 | 0 | _Py_DECLARE_STR(empty, ""); |
186 | 0 | PyTuple_SET_ITEM(strings, stringsidx++, &_Py_STR(empty)); |
187 | 0 | } |
188 | |
|
189 | 0 | PyObject *template = _PyTemplate_Build(strings, interpolations); |
190 | 0 | Py_DECREF(strings); |
191 | 0 | Py_DECREF(interpolations); |
192 | 0 | return template; |
193 | 0 | } |
194 | | |
195 | | static void |
196 | | template_dealloc(PyObject *op) |
197 | 11 | { |
198 | 11 | PyObject_GC_UnTrack(op); |
199 | 11 | Py_TYPE(op)->tp_clear(op); |
200 | 11 | Py_TYPE(op)->tp_free(op); |
201 | 11 | } |
202 | | |
203 | | static int |
204 | | template_clear(PyObject *op) |
205 | 11 | { |
206 | 11 | templateobject *self = templateobject_CAST(op); |
207 | 11 | Py_CLEAR(self->strings); |
208 | 11 | Py_CLEAR(self->interpolations); |
209 | 11 | return 0; |
210 | 11 | } |
211 | | |
212 | | static int |
213 | | template_traverse(PyObject *op, visitproc visit, void *arg) |
214 | 0 | { |
215 | 0 | templateobject *self = templateobject_CAST(op); |
216 | 0 | Py_VISIT(self->strings); |
217 | 0 | Py_VISIT(self->interpolations); |
218 | 0 | return 0; |
219 | 0 | } |
220 | | |
221 | | static PyObject * |
222 | | template_repr(PyObject *op) |
223 | 0 | { |
224 | 0 | templateobject *self = templateobject_CAST(op); |
225 | 0 | return PyUnicode_FromFormat("%s(strings=%R, interpolations=%R)", |
226 | 0 | _PyType_Name(Py_TYPE(self)), |
227 | 0 | self->strings, |
228 | 0 | self->interpolations); |
229 | 0 | } |
230 | | |
231 | | static PyObject * |
232 | | template_iter(PyObject *op) |
233 | 0 | { |
234 | 0 | templateobject *self = templateobject_CAST(op); |
235 | 0 | templateiterobject *iter = PyObject_GC_New(templateiterobject, &_PyTemplateIter_Type); |
236 | 0 | if (iter == NULL) { |
237 | 0 | return NULL; |
238 | 0 | } |
239 | 0 | iter->stringsiter = NULL; |
240 | 0 | iter->interpolationsiter = NULL; |
241 | |
|
242 | 0 | PyObject *stringsiter = PyObject_GetIter(self->strings); |
243 | 0 | if (stringsiter == NULL) { |
244 | 0 | Py_DECREF(iter); |
245 | 0 | return NULL; |
246 | 0 | } |
247 | | |
248 | 0 | PyObject *interpolationsiter = PyObject_GetIter(self->interpolations); |
249 | 0 | if (interpolationsiter == NULL) { |
250 | 0 | Py_DECREF(iter); |
251 | 0 | Py_DECREF(stringsiter); |
252 | 0 | return NULL; |
253 | 0 | } |
254 | | |
255 | 0 | iter->stringsiter = stringsiter; |
256 | 0 | iter->interpolationsiter = interpolationsiter; |
257 | 0 | iter->from_strings = 1; |
258 | 0 | PyObject_GC_Track(iter); |
259 | 0 | return (PyObject *)iter; |
260 | 0 | } |
261 | | |
262 | | static PyObject * |
263 | | template_strings_concat(PyObject *left, PyObject *right) |
264 | 0 | { |
265 | 0 | Py_ssize_t left_stringslen = PyTuple_GET_SIZE(left); |
266 | 0 | PyObject *left_laststring = PyTuple_GET_ITEM(left, left_stringslen - 1); |
267 | 0 | Py_ssize_t right_stringslen = PyTuple_GET_SIZE(right); |
268 | 0 | PyObject *right_firststring = PyTuple_GET_ITEM(right, 0); |
269 | |
|
270 | 0 | PyObject *concat = PyUnicode_Concat(left_laststring, right_firststring); |
271 | 0 | if (concat == NULL) { |
272 | 0 | return NULL; |
273 | 0 | } |
274 | | |
275 | 0 | PyObject *newstrings = PyTuple_New(left_stringslen + right_stringslen - 1); |
276 | 0 | if (newstrings == NULL) { |
277 | 0 | Py_DECREF(concat); |
278 | 0 | return NULL; |
279 | 0 | } |
280 | | |
281 | 0 | Py_ssize_t index = 0; |
282 | 0 | for (Py_ssize_t i = 0; i < left_stringslen - 1; i++) { |
283 | 0 | PyTuple_SET_ITEM(newstrings, index++, Py_NewRef(PyTuple_GET_ITEM(left, i))); |
284 | 0 | } |
285 | 0 | PyTuple_SET_ITEM(newstrings, index++, concat); |
286 | 0 | for (Py_ssize_t i = 1; i < right_stringslen; i++) { |
287 | 0 | PyTuple_SET_ITEM(newstrings, index++, Py_NewRef(PyTuple_GET_ITEM(right, i))); |
288 | 0 | } |
289 | |
|
290 | 0 | return newstrings; |
291 | 0 | } |
292 | | |
293 | | static PyObject * |
294 | | template_concat_templates(templateobject *self, templateobject *other) |
295 | 0 | { |
296 | 0 | PyObject *newstrings = template_strings_concat(self->strings, other->strings); |
297 | 0 | if (newstrings == NULL) { |
298 | 0 | return NULL; |
299 | 0 | } |
300 | | |
301 | 0 | PyObject *newinterpolations = PySequence_Concat(self->interpolations, other->interpolations); |
302 | 0 | if (newinterpolations == NULL) { |
303 | 0 | Py_DECREF(newstrings); |
304 | 0 | return NULL; |
305 | 0 | } |
306 | | |
307 | 0 | PyObject *newtemplate = _PyTemplate_Build(newstrings, newinterpolations); |
308 | 0 | Py_DECREF(newstrings); |
309 | 0 | Py_DECREF(newinterpolations); |
310 | 0 | return newtemplate; |
311 | 0 | } |
312 | | |
313 | | PyObject * |
314 | | _PyTemplate_Concat(PyObject *self, PyObject *other) |
315 | 0 | { |
316 | 0 | if (_PyTemplate_CheckExact(self) && _PyTemplate_CheckExact(other)) { |
317 | 0 | return template_concat_templates((templateobject *) self, (templateobject *) other); |
318 | 0 | } |
319 | | |
320 | 0 | PyErr_Format(PyExc_TypeError, |
321 | 0 | "can only concatenate string.templatelib.Template (not \"%T\") to string.templatelib.Template", |
322 | 0 | other); |
323 | 0 | return NULL; |
324 | 0 | } |
325 | | |
326 | | static PyObject * |
327 | | template_values_get(PyObject *op, void *Py_UNUSED(data)) |
328 | 0 | { |
329 | 0 | templateobject *self = templateobject_CAST(op); |
330 | |
|
331 | 0 | Py_ssize_t len = PyTuple_GET_SIZE(self->interpolations); |
332 | 0 | PyObject *values = PyTuple_New(PyTuple_GET_SIZE(self->interpolations)); |
333 | 0 | if (values == NULL) { |
334 | 0 | return NULL; |
335 | 0 | } |
336 | | |
337 | 0 | for (Py_ssize_t i = 0; i < len; i++) { |
338 | 0 | PyObject *item = PyTuple_GET_ITEM(self->interpolations, i); |
339 | 0 | PyTuple_SET_ITEM(values, i, _PyInterpolation_GetValueRef(item)); |
340 | 0 | } |
341 | |
|
342 | 0 | return values; |
343 | 0 | } |
344 | | |
345 | | static PyMemberDef template_members[] = { |
346 | | {"strings", Py_T_OBJECT_EX, offsetof(templateobject, strings), Py_READONLY, "Strings"}, |
347 | | {"interpolations", Py_T_OBJECT_EX, offsetof(templateobject, interpolations), Py_READONLY, "Interpolations"}, |
348 | | {NULL}, |
349 | | }; |
350 | | |
351 | | static PyGetSetDef template_getset[] = { |
352 | | {"values", template_values_get, NULL, |
353 | | PyDoc_STR("Values of interpolations"), NULL}, |
354 | | {NULL}, |
355 | | }; |
356 | | |
357 | | static PySequenceMethods template_as_sequence = { |
358 | | .sq_concat = _PyTemplate_Concat, |
359 | | }; |
360 | | |
361 | | static PyObject* |
362 | | template_reduce(PyObject *op, PyObject *Py_UNUSED(dummy)) |
363 | 0 | { |
364 | 0 | PyObject *mod = PyImport_ImportModule("string.templatelib"); |
365 | 0 | if (mod == NULL) { |
366 | 0 | return NULL; |
367 | 0 | } |
368 | 0 | PyObject *func = PyObject_GetAttrString(mod, "_template_unpickle"); |
369 | 0 | Py_DECREF(mod); |
370 | 0 | if (func == NULL) { |
371 | 0 | return NULL; |
372 | 0 | } |
373 | | |
374 | 0 | templateobject *self = templateobject_CAST(op); |
375 | 0 | PyObject *result = Py_BuildValue("O(OO)", |
376 | 0 | func, |
377 | 0 | self->strings, |
378 | 0 | self->interpolations); |
379 | |
|
380 | 0 | Py_DECREF(func); |
381 | 0 | return result; |
382 | 0 | } |
383 | | |
384 | | static PyMethodDef template_methods[] = { |
385 | | {"__reduce__", template_reduce, METH_NOARGS, NULL}, |
386 | | {"__class_getitem__", Py_GenericAlias, |
387 | | METH_O|METH_CLASS, |
388 | | // note that this is not supported in typeshed, and it is not clear if the |
389 | | // type for this is a simple TypeVar or a TypeVarTuple |
390 | | // for details, see: https://github.com/python/typeshed/issues/14878 |
391 | | PyDoc_STR("Template supports [] for generic usage")}, |
392 | | {NULL, NULL}, |
393 | | }; |
394 | | |
395 | | PyTypeObject _PyTemplate_Type = { |
396 | | PyVarObject_HEAD_INIT(NULL, 0) |
397 | | .tp_name = "string.templatelib.Template", |
398 | | .tp_doc = PyDoc_STR("Template object"), |
399 | | .tp_basicsize = sizeof(templateobject), |
400 | | .tp_itemsize = 0, |
401 | | .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, |
402 | | .tp_as_sequence = &template_as_sequence, |
403 | | .tp_new = template_new, |
404 | | .tp_vectorcall = template_vectorcall, |
405 | | .tp_alloc = PyType_GenericAlloc, |
406 | | .tp_dealloc = template_dealloc, |
407 | | .tp_clear = template_clear, |
408 | | .tp_free = PyObject_GC_Del, |
409 | | .tp_repr = template_repr, |
410 | | .tp_members = template_members, |
411 | | .tp_methods = template_methods, |
412 | | .tp_getset = template_getset, |
413 | | .tp_iter = template_iter, |
414 | | .tp_traverse = template_traverse, |
415 | | }; |
416 | | |
417 | | PyObject * |
418 | | _PyTemplate_Build(PyObject *strings, PyObject *interpolations) |
419 | 11 | { |
420 | 11 | templateobject *template = PyObject_GC_New(templateobject, &_PyTemplate_Type); |
421 | 11 | if (template == NULL) { |
422 | 0 | return NULL; |
423 | 0 | } |
424 | | |
425 | 11 | template->strings = Py_NewRef(strings); |
426 | 11 | template->interpolations = Py_NewRef(interpolations); |
427 | 11 | PyObject_GC_Track(template); |
428 | 11 | return (PyObject *) template; |
429 | 11 | } |