Skip to content

Commit d2bd673

Browse files
committed
Use Argument Clinic for interpolation_new
1 parent c9362e3 commit d2bd673

File tree

7 files changed

+154
-19
lines changed

7 files changed

+154
-19
lines changed

Include/internal/pycore_global_objects_fini_generated.h

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_global_strings.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@ struct _Py_global_strings {
356356
STRUCT_FOR_ID(consts)
357357
STRUCT_FOR_ID(context)
358358
STRUCT_FOR_ID(contravariant)
359+
STRUCT_FOR_ID(conv)
359360
STRUCT_FOR_ID(cookie)
360361
STRUCT_FOR_ID(copy)
361362
STRUCT_FOR_ID(copyreg)
@@ -413,6 +414,7 @@ struct _Py_global_strings {
413414
STRUCT_FOR_ID(exception)
414415
STRUCT_FOR_ID(existing_file_name)
415416
STRUCT_FOR_ID(exp)
417+
STRUCT_FOR_ID(expr)
416418
STRUCT_FOR_ID(extend)
417419
STRUCT_FOR_ID(extra_tokens)
418420
STRUCT_FOR_ID(facility)

Include/internal/pycore_interpolation.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ extern "C" {
1111

1212
#include "pycore_stackref.h" // _PyStackRef
1313

14+
extern int _conversion_converter(PyObject *arg, PyObject **Py_UNUSED(unused));
15+
1416
extern PyTypeObject _PyInterpolation_Type;
1517

1618
PyAPI_FUNC(PyObject *) _PyInterpolation_FromStackRefSteal(_PyStackRef *values);
1719

18-
extern PyStatus _PyInterpolation_InitTypes(PyInterpreterState *);
20+
extern PyStatus _PyInterpolation_InitTypes(PyInterpreterState *interp);
1921

2022
#ifdef __cplusplus
2123
}

Include/internal/pycore_runtime_init_generated.h

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_unicodeobject_generated.h

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Objects/clinic/interpolationobject.c.h

Lines changed: 85 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Objects/interpolationobject.c

Lines changed: 52 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,41 @@
99

1010
#include "pycore_interpolation.h"
1111

12+
/*[clinic input]
13+
module templatelib
14+
[clinic start generated code]*/
15+
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=dbb2236a3de68808]*/
16+
17+
#include "clinic/interpolationobject.c.h"
18+
19+
/*[clinic input]
20+
class templatelib.Interpolation "interpolationobject *" "&_PyInterpolation_Type"
21+
[clinic start generated code]*/
22+
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=5b183514b4d7e5af]*/
23+
24+
int
25+
_conversion_converter(PyObject *arg, PyObject **Py_UNUSED(unused))
26+
{
27+
if (arg == Py_None) {
28+
return 1;
29+
}
30+
31+
if (!PyUnicode_Check(arg)) {
32+
_PyArg_BadArgument("Interpolation", "argument 'conv'", "str", arg);
33+
return 0;
34+
}
35+
36+
Py_ssize_t len;
37+
const char *conv = PyUnicode_AsUTF8AndSize(arg, &len);
38+
if (len != 1 || !(conv[0] == 'a' || conv[0] == 'r' || conv[0] == 's')) {
39+
PyErr_SetString(PyExc_ValueError,
40+
"Interpolation() argument 'conv' must be one of 's', 'a' or 'r'");
41+
return 0;
42+
}
43+
44+
return 1;
45+
}
46+
1247
typedef struct {
1348
PyObject_HEAD
1449
PyObject *value;
@@ -17,32 +52,31 @@ typedef struct {
1752
PyObject *format_spec;
1853
} interpolationobject;
1954

20-
static interpolationobject *
21-
interpolation_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
55+
/*[clinic input]
56+
@classmethod
57+
templatelib.Interpolation.__new__ as interpolation_new
58+
59+
value: object
60+
expr: object(subclass_of='&PyUnicode_Type')
61+
conv: object(converter='_conversion_converter') = None
62+
format_spec: object(subclass_of='&PyUnicode_Type', c_default='&_Py_STR(empty)') = ""
63+
[clinic start generated code]*/
64+
65+
static PyObject *
66+
interpolation_new_impl(PyTypeObject *type, PyObject *value, PyObject *expr,
67+
PyObject *conv, PyObject *format_spec)
68+
/*[clinic end generated code: output=417d59bccab99648 input=348d81ee06c4be20]*/
2269
{
2370
interpolationobject *self = (interpolationobject *) type->tp_alloc(type, 0);
2471
if (!self) {
2572
return NULL;
2673
}
2774

28-
static char *kwlist[] = {"value", "expr", "conv", "format_spec", NULL};
29-
30-
PyObject *value, *expr;
31-
PyObject *conv = NULL;
32-
PyObject *format_spec = NULL;
33-
34-
if (PyArg_ParseTupleAndKeywords(args, kwds, "OO|OO", kwlist,
35-
&value, &expr,
36-
&conv, &format_spec) < 0) {
37-
Py_DECREF(self);
38-
return NULL;
39-
}
40-
4175
Py_XSETREF(self->value, Py_NewRef(value));
4276
Py_XSETREF(self->expr, Py_NewRef(expr));
43-
Py_XSETREF(self->conv, Py_NewRef(conv ? conv : Py_None));
44-
Py_XSETREF(self->format_spec, format_spec ? Py_NewRef(format_spec) : &_Py_STR(empty));
45-
return self;
77+
Py_XSETREF(self->conv, Py_NewRef(conv));
78+
Py_XSETREF(self->format_spec, Py_NewRef(format_spec));
79+
return (PyObject *) self;
4680
}
4781

4882
static void

0 commit comments

Comments
 (0)