8000 Add templatelib module & clean-up types/objects etc. · python/cpython@499d70c · GitHub
[go: up one dir, main page]

Skip to content

Commit 499d70c

Browse files
committed
Add templatelib module & clean-up types/objects etc.
1 parent a095a9b commit 499d70c

File tree

9 files changed

+94
-83
lines changed

9 files changed

+94
-83
lines changed

Include/internal/pycore_interpolation.h

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,12 @@ extern "C" {
99
# error "this header requires Py_BUILD_CORE define"
1010
#endif
1111

12-
typedef struct {
13-
PyObject_HEAD
14-
PyObject *value;
15-
PyObject *expr;
16-
PyObject *conv;
17-
PyObject *format_spec;
18-
} PyInterpolationObject;
12+
#include "pycore_stackref.h" // _PyStackRef
1913

20-
extern PyTypeObject PyInterpolation_Type;
14+
extern PyTypeObject _PyInterpolation_Type;
2115

2216
PyAPI_FUNC(PyObject *) _PyInterpolation_FromStackRefSteal(_PyStackRef *values);
2317

24-
extern PyStatus _PyInterpolation_InitTypes(PyInterpreterState *);
25-
extern void _PyInterpolation_FiniTypes(PyInterpreterState *);
26-
2718
#ifdef __cplusplus
2819
}
2920
#endif

Include/internal/pycore_template.h

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,10 @@ extern "C" {
99
# error "this header requires Py_BUILD_CORE define"
1010
#endif
1111

12-
typedef struct {
13-
PyObject_HEAD
14-
PyObject *args;
15-
} PyTemplateObject;
16-
17-
extern PyTypeObject PyTemplate_Type;
12+
extern PyTypeObject _PyTemplate_Type;
1813

1914
PyAPI_FUNC(PyObject *) _PyTemplate_Create(PyObject **values, Py_ssize_t n);
2015

21-
extern PyStatus _PyTemplate_InitTypes(PyInterpreterState *);
22-
extern void _PyTemplate_FiniTypes(PyInterpreterState *);
23-
2416
#ifdef __cplusplus
2517
}
2618
#endif

Lib/templatelib.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from _templatelib import Template, Interpolation
2+
3+
__all__ = ['Template', 'Interpolation']

Modules/Setup.bootstrap.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ _thread _threadmodule.c
2525
time timemodule.c
2626
_typing _typingmodule.c
2727
_weakref _weakref.c
28+
_templatelib _templatelibmodule.c
2829

2930
# commonly used core modules
3031
_abc _abc.c

Modules/_templatelibmodule.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/* interpreter-internal types for templatelib */
2+
3+
#ifndef Py_BUILD_CORE
4+
#define Py_BUILD_CORE
5+
#endif
6+
7+
#include "Python.h"
8+
#include "pycore_template.h"
9+
#include "pycore_interpolation.h"
10+
11+
static int
12+
_templatelib_exec(PyObject *m)
13+
{
14+
if (PyModule_AddObjectRef(m, "Template", (PyObject *)&_PyTemplate_Type) < 0) {
15+
return -1;
16+
}
17+
if (PyModule_AddObjectRef(m, "Interpolation", (PyObject *)&_PyInterpolation_Type) < 0) {
18+
return -1;
19+
}
20+
return 0;
21+
}
22+
23+
PyDoc_STRVAR(_templatelib_doc,
24+
"Interpreter-internal types for t-string templates.\n");
25+
26+
static struct PyModuleDef_Slot _templatelib_slots[] = {
27+
{Py_mod_exec, _templatelib_exec},
28+
{Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
29+
{Py_mod_gil, Py_MOD_GIL_NOT_USED},
30+
{0, NULL}
31+
};
32+
33+
static struct PyModuleDef _templatemodule = {
34+
PyModuleDef_HEAD_INIT,
35+
"_templatelib",
36+
_templatelib_doc,
37+
0,
38+
NULL,
39+
_templatelib_slots,
40+
NULL,
41+
NULL,
42+
NULL
43+
};
44+
45+
PyMODINIT_FUNC
46+
PyInit__templatelib(void)
47+
{
48+
return PyModuleDef_Init(&_templatemodule);
49+
}

Objects/interpolationobject.c

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,18 @@
1111

1212
#include "pycore_interpolation.h"
1313

14-
static PyInterpolationObject *
14+
typedef struct {
15+
PyObject_HEAD
16+
PyObject *value;
17+
PyObject *expr;
18+
PyObject *conv;
19+
PyObject *format_spec;
20+
} interpolationobject;
21+
22+
static interpolationobject *
1523
interpolation_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1624
{
17-
PyInterpolationObject *self = (PyInterpolationObject *) type->tp_alloc(type, 0);
25+
interpolationobject *self = (interpolationobject *) type->tp_alloc(type, 0);
1826
if (!self) {
1927
return NULL;
2028
}
@@ -40,7 +48,7 @@ interpolation_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
4048
}
4149

4250
static void
43-
interpolation_dealloc(PyInterpolationObject *self)
51+
interpolation_dealloc(interpolationobject *self)
4452
{
4553
Py_CLEAR(self->value);
4654
Py_CLEAR(self->expr);
@@ -50,7 +58,7 @@ interpolation_dealloc(PyInterpolationObject *self)
5058
}
5159

5260
static PyObject *
53-
interpolation_repr(PyInterpolationObject *self)
61+
interpolation_repr(interpolationobject *self)
5462
{
5563
return PyUnicode_FromFormat("%s(%R, %R, %R, %R)",
5664
_PyType_Name(Py_TYPE(self)),
@@ -59,18 +67,18 @@ interpolation_repr(PyInterpolationObject *self)
5967
}
6068

6169
static PyMemberDef interpolation_members[] = {
62-
{"value", Py_T_OBJECT_EX, offsetof(PyInterpolationObject, value), Py_READONLY, "Value"},
63-
{"expr", Py_T_OBJECT_EX, offsetof(PyInterpolationObject, expr), Py_READONLY, "Expr"},
64-
{"conv", Py_T_OBJECT_EX, offsetof(PyInterpolationObject, conv), Py_READONLY, "Conversion"},
65-
{"format_spec", Py_T_OBJECT_EX, offsetof(PyInterpolationObject, format_spec), Py_READONLY, "Format specifier"},
70+
{"value", Py_T_OBJECT_EX, offsetof(interpolationobject, value), Py_READONLY, "Value"},
71+
{"expr", Py_T_OBJECT_EX, offsetof(interpolationobject, expr), Py_READONLY, "Expr"},
72+
{"conv", Py_T_OBJECT_EX, offsetof(interpolationobject, conv), Py_READONLY, "Conversion"},
73+
{"format_spec", Py_T_OBJECT_EX, offsetof(interpolationobject, format_spec), Py_READONLY, "Format specifier"},
6674
{NULL}
6775
};
6876

69-
PyTypeObject PyInterpolation_Type = {
77+
PyTypeObject _PyInterpolation_Type = {
7078
PyVarObject_HEAD_INIT(NULL, 0)
71-
.tp_name = "Interpolation",
79+
.tp_name = "templatelib.Interpolation",
7280
.tp_doc = PyDoc_STR("Interpolation object"),
73-
.tp_basicsize = sizeof(PyInterpolationObject),
81+
.tp_basicsize = sizeof(interpolationobject),
7482
.tp_itemsize = 0,
7583
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | _Py_TPFLAGS_MATCH_SELF,
7684
.tp_new = (newfunc) interpolation_new,
@@ -79,21 +87,6 @@ PyTypeObject PyInterpolation_Type = {
7987
.tp_members = interpolation_members,
8088
};
8189

82-
PyStatus
83-
_PyInterpolation_InitTypes(PyInterpreterState *interp)
84-
{
85-
if (_PyStaticType_InitBuiltin(interp, &PyInterpolation_Type) < 0) {
86-
return _PyStatus_ERR("Can't initialize builtin type");
87-
}
88-
return _PyStatus_OK();
89-
}
90-
91-
void
92-
_PyInterpolation_FiniTypes(PyInterpreterState *interp)
93-
{
94-
_PyStaticType_FiniBuiltin(interp, &PyInterpolation_Type);
95-
}
96-
9790
PyObject *
9891
_PyInterpolation_FromStackRefSteal(_PyStackRef *values)
9992
{
@@ -111,7 +104,7 @@ _PyInterpolation_FromStackRefSteal(_PyStackRef *values)
111104
PyObject *format_spec = PyStackRef_AsPyObjectSteal(values[3]);
112105
PyTuple_SET_ITEM(args, 3, format_spec ? format_spec : &_Py_STR(empty));
113106

114-
PyObject *interpolation = PyObject_CallObject((PyObject *) &PyInterpolation_Type, args);
107+
PyObject *interpolation = PyObject_CallObject((PyObject *) &_PyInterpolation_Type, args);
115108
if (!interpolation) {
116109
Py_DECREF(args);
117110
goto error;

Objects/object.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "pycore_freelist.h" // _PyObject_ClearFreeLists()
1414
#include "pycore_initconfig.h" // _PyStatus_EXCEPTION()
1515
#include "pycore_instruction_sequence.h" // _PyInstructionSequence_Type
16+
#include "pycore_interpolation.h" // _PyInterpolation_Type
1617
#include "pycore_hashtable.h" // _Py_hashtable_new()
1718
#include "pycore_memoryobject.h" // _PyManagedBuffer_Type
1819
#include "pycore_namespace.h" // _PyNamespace_Type
@@ -24,6 +25,7 @@
2425
#include "pycore_pymem.h" // _PyMem_IsPtrFreed()
2526
#include "pycore_pystate.h" // _PyThreadState_GET()
2627
#include "pycore_symtable.h" // PySTEntry_Type
28+
#include "pycore_template.h" // _PyTemplate_Type
2729
#include "pycore_typeobject.h" // _PyBufferWrapper_Type
2830
#include "pycore_typevarobject.h" // _PyTypeAlias_Type, _Py_initialize_generic
2931
#include "pycore_unionobject.h" // _PyUnion_Type
@@ -2345,6 +2347,8 @@ static PyTypeObject* static_types[] = {
23452347
&_PyWeakref_RefType,
23462348
&_PyTypeAlias_Type,
23472349
&_PyNoDefault_Type,
2350+
&_PyInterpolation_Type,
2351+
&_PyTemplate_Type,
23482352

23492353
// subclasses: _PyTypes_FiniTypes() deallocates them before their base
23502354
// class

Objects/templateobject.c

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,15 @@
99

1010
#include "pycore_template.h"
1111

12-
static PyTemplateObject *
12+
typedef struct {
13+
PyObject_HEAD
14+
PyObject *args;
15+
} templateobject;
16+
17+
static templateobject *
1318
template_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1419
{
15-
PyTemplateObject *self = (PyTemplateObject *) type->tp_alloc(type, 0);
20+
templateobject *self = (templateobject *) type->tp_alloc(type, 0);
1621
if (!self) {
1722
return NULL;
1823
}
@@ -31,30 +36,30 @@ template_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3136
}
3237

3338
static void
34-
template_dealloc(PyTemplateObject *self)
39+
template_dealloc(templateobject *self)
3540
{
3641
Py_CLEAR(self->args);
3742
Py_TYPE(self)->tp_free(self);
3843
}
3944

4045
static PyObject *
41-
template_repr(PyTemplateObject *self)
46+
template_repr(templateobject *self)
4247
{
4348
return PyUnicode_FromFormat("%s(%R)",
4449
_PyType_Name(Py_TYPE(self)),
4550
self->args);
4651
}
4752

4853
static PyMemberDef template_members[] = {
49-
{"args", Py_T_OBJECT_EX, offsetof(PyTemplateObject, args), Py_READONLY, "Args"},
54+
{"args", Py_T_OBJECT_EX, offsetof(templateobject, args), Py_READONLY, "Args"},
5055
{NULL}
5156
};
5257

53-
PyTypeObject PyTemplate_Type = {
58+
PyTypeObject _PyTemplate_Type = {
5459
PyVarObject_HEAD_INIT(NULL, 0)
55-
.tp_name = "Template",
60+
.tp_name = "templatelib.Template",
5661
.tp_doc = PyDoc_STR("Template object"),
57-
.tp_basicsize = sizeof(PyTemplateObject),
62+
.tp_basicsize = sizeof(templateobject),
5863
.tp_itemsize = 0,
5964
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | _Py_TPFLAGS_MATCH_SELF,
6065
.tp_new = (newfunc) template_new,
@@ -63,21 +68,6 @@ PyTypeObject PyTemplate_Type = {
6368
.tp_members = template_members,
6469
};
6570

66-
PyStatus
67-
_PyTemplate_InitTypes(PyInterpreterState *interp)
68-
{
69-
if (_PyStaticType_InitBuiltin(interp, &PyTemplate_Type) < 0) {
70-
return _PyStatus_ERR("Can't initialize builtin type");
71-
}
72-
return _PyStatus_OK();
73-
}
74-
75-
void
76-
_PyTemplate_FiniTypes(PyInterpreterState *interp)
77-
{
78-
_PyStaticType_FiniBuiltin(interp, &PyTemplate_Type);
79-
}
80-
8171
PyObject *
8272
_PyTemplate_Create(PyObject **values, Py_ssize_t oparg)
8373
{
@@ -90,7 +80,7 @@ _PyTemplate_Create(PyObject **values, Py_ssize_t oparg)
9080
PyTuple_SET_ITEM(tuple, i, Py_NewRef(values[i]));
9181
}
9282

93-
PyObject *template = PyObject_CallOneArg((PyObject *) &PyTemplate_Type, tuple);
83+
PyObject *template = PyObject_CallOneArg((PyObject *) &_PyTemplate_Type, tuple);
9484
Py_DECREF(tuple);
9585
return template;
9686
}

Python/pylifecycle.c

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numbe CB8A rDiff line change
@@ -774,16 +774,6 @@ pycore_init_types(PyInterpreterState *interp)
774774
return status;
775775
}
776776

777-
status = _PyInterpolation_InitTypes(interp);
778-
if (_PyStatus_EXCEPTION(status)) {
779-
return status;
780-
}
781-
782-
status = _PyTemplate_InitTypes(interp);
783-
if (_PyStatus_EXCEPTION(status)) {
784-
return status;
785-
}
786-
787777
return _PyStatus_OK();
788778
}
789779

@@ -1842,8 +1832,6 @@ finalize_interp_types(PyInterpreterState *interp)
18421832
_PyFloat_FiniType(interp);
18431833
_PyLong_FiniTypes(interp);
18441834
_PyThread_FiniType(interp);
1845-
_PyInterpolation_FiniTypes(interp);
1846-
_PyTemplate_FiniTypes(interp);
18471835
// XXX fini collections module static types (_PyStaticType_Dealloc())
18481836
// XXX fini IO module static types (_PyStaticType_Dealloc())
18491837
_PyErr_FiniTypes(interp);

0 commit comments

Comments
 (0)
0