Skip to content

Commit 99184cc

Browse files
committed
Add __match_args__ to Interpolation
1 parent 94371bd commit 99184cc

File tree

4 files changed

+66
-8
lines changed

4 files changed

+66
-8
lines changed

Include/internal/pycore_interpolation.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ extern PyTypeObject _PyInterpolation_Type;
1515

1616
PyAPI_FUNC(PyObject *) _PyInterpolation_FromStackRefSteal(_PyStackRef *values);
1717

18+
extern PyStatus _PyInterpolation_InitTypes(PyInterpreterState *);
19+
1820
#ifdef __cplusplus
1921
}
2022
#endif

Objects/interpolationobject.c

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
33
#include <stddef.h>
44

55
#include "pycore_initconfig.h" // _PyStatus_OK
6-
#include "pycore_typeobject.h" // _PyStaticType_InitBuiltin
76
#include "pycore_stackref.h" // _PyStackRef
87
#include "pycore_global_objects.h" // _Py_STR
98
#include "pycore_runtime.h" // _Py_STR
10-
#include "pycore_object.h" // _PyObject_GC_TRACK
119

1210
#include "pycore_interpolation.h"
1311

@@ -122,6 +120,65 @@ PyTypeObject _PyInterpolation_Type = {
122120
.tp_members = interpolation_members,
123121
};
124122

123+
static PyObject *
124+
_get_match_args(void)
125+
{
126+
PyObject *value = NULL, *expr = NULL, *conv = NULL, *format_spec = NULL;
127+
128+
value = PyUnicode_FromString("value");
129+
if (!value) {
130+
goto error;
131+
}
132+
expr = PyUnicode_FromString("expr");
133+
if (!expr) {
134+
goto error;
135+
}
136+
conv = PyUnicode_FromString("conv");
137+
if (!conv) {
138+
goto error;
139+
}
140+
format_spec = PyUnicode_FromString("format_spec");
141+
if (!format_spec) {
142+
goto error;
143+
}
144+
145+
PyObject *tuple = PyTuple_Pack(4, value, expr, conv, format_spec);
146+
if (!tuple) {
147+
goto error;
148+
}
149+
return tuple;
150+
151+
error:
152+
Py_XDECREF(value);
153+
Py_XDECREF(expr);
154+
Py_XDECREF(conv);
155+
Py_XDECREF(format_spec);
156+
return NULL;
157+
158+
}
159+
160+
PyStatus
161+
_PyInterpolation_InitTypes(PyInterpreterState *interp)
162+
{
163+
PyObject *tuple = _get_match_args();
164+
if (!tuple) {
165+
goto error;
166+
}
167+
168+
int status = PyDict_SetItemString(_PyType_GetDict(&_PyInterpolation_Type),
169+
"__match_args__",
170+
tuple);
171+
Py_DECREF(tuple);
172+
173+
if (status < 0) {
174+
goto error;
175+
}
176+
return _PyStatus_OK();
177+
178+
error:
179+
return _PyStatus_ERR("Can't initialize interpolation types");
180+
}
181+
125182
PyObject *
126183
_PyInterpolation_FromStackRefSteal(_PyStackRef *values)
127184
{

Objects/templateobject.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@
22
#include "Python.h"
33
#include <stddef.h>
44

5-
#include "pycore_initconfig.h" // _PyStatus_OK
6-
#include "pycore_typeobject.h" // _PyStaticType_InitBuiltin
7-
#include "pycore_stackref.h" // _PyStackRef
8-
#include "pycore_object.h" // _PyObject_GC_TRACK
9-
105
#include "pycore_template.h"
116

127
typedef struct {

Python/pylifecycle.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
#include "pycore_unicodeobject.h" // _PyUnicode_InitTypes()
3636
#include "pycore_weakref.h" // _PyWeakref_GET_REF()
3737
#include "pycore_obmalloc.h" // _PyMem_init_obmalloc()
38-
#include "pycore_template.h" // _PyTemplate_InitTypes()
3938
#include "pycore_interpolation.h" // _PyInterpolation_InitTypes()
4039

4140
#include "opcode.h"
@@ -774,6 +773,11 @@ pycore_init_types(PyInterpreterState *interp)
774773
return status;
775774
}
776775

776+
status = _PyInterpolation_InitTypes(interp);
777+
if (_PyStatus_EXCEPTION(status)) {
778+
return status;
779+
}
780+
777781
return _PyStatus_OK();
778782
}
779783

0 commit comments

Comments
 (0)