/src/cpython/Objects/sentinelobject.c
Line | Count | Source |
1 | | /* Sentinel object implementation */ |
2 | | |
3 | | #include "Python.h" |
4 | | #include "descrobject.h" // PyMemberDef |
5 | | #include "pycore_ceval.h" // _PyThreadState_GET() |
6 | | #include "pycore_interpframe.h" // _PyFrame_IsIncomplete() |
7 | | #include "pycore_object.h" // _PyObject_GC_TRACK/UNTRACK() |
8 | | #include "pycore_stackref.h" // PyStackRef_AsPyObjectBorrow() |
9 | | #include "pycore_tuple.h" // _PyTuple_FromPair |
10 | | #include "pycore_typeobject.h" // _Py_BaseObject_RichCompare() |
11 | | #include "pycore_unionobject.h" // _Py_union_type_or() |
12 | | |
13 | | typedef struct { |
14 | | PyObject_HEAD |
15 | | PyObject *name; |
16 | | PyObject *module; |
17 | | PyObject *repr; |
18 | | } sentinelobject; |
19 | | |
20 | | #define sentinelobject_CAST(op) \ |
21 | 8.65k | (assert(PySentinel_Check(op)), _Py_CAST(sentinelobject *, (op))) |
22 | | |
23 | | /*[clinic input] |
24 | | class sentinel "sentinelobject *" "&PySentinel_Type" |
25 | | [clinic start generated code]*/ |
26 | | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=8b88f8268d3b5775]*/ |
27 | | |
28 | | #include "clinic/sentinelobject.c.h" |
29 | | |
30 | | |
31 | | static PyObject * |
32 | | caller(void) |
33 | 152 | { |
34 | 152 | _PyInterpreterFrame *f = _PyThreadState_GET()->current_frame; |
35 | 152 | if (f == NULL || PyStackRef_IsNull(f->f_funcobj)) { |
36 | 0 | assert(!PyErr_Occurred()); |
37 | 0 | Py_RETURN_NONE; |
38 | 0 | } |
39 | 152 | PyFunctionObject *func = _PyFrame_GetFunction(f); |
40 | 152 | assert(PyFunction_Check(func)); |
41 | 152 | PyObject *r = PyFunction_GetModule((PyObject *)func); |
42 | 152 | if (!r) { |
43 | 0 | assert(!PyErr_Occurred()); |
44 | 0 | Py_RETURN_NONE; |
45 | 0 | } |
46 | 152 | return Py_NewRef(r); |
47 | 152 | } |
48 | | |
49 | | static PyObject * |
50 | | sentinel_new_with_module(PyTypeObject *type, PyObject *name, PyObject *module, PyObject *repr) |
51 | 180 | { |
52 | 180 | assert(PyUnicode_Check(name)); |
53 | | |
54 | 180 | sentinelobject *self = PyObject_GC_New(sentinelobject, type); |
55 | 180 | if (self == NULL) { |
56 | 0 | return NULL; |
57 | 0 | } |
58 | 180 | self->name = Py_NewRef(name); |
59 | 180 | self->module = Py_NewRef(module); |
60 | 180 | self->repr = Py_XNewRef(repr); |
61 | 180 | _PyObject_GC_TRACK(self); |
62 | 180 | return (PyObject *)self; |
63 | 180 | } |
64 | | |
65 | | /*[clinic input] |
66 | | @vectorcall |
67 | | @classmethod |
68 | | sentinel.__new__ as sentinel_new |
69 | | |
70 | | name: object(subclass_of='&PyUnicode_Type') |
71 | | / |
72 | | * |
73 | | repr: object = None |
74 | | [clinic start generated code]*/ |
75 | | |
76 | | static PyObject * |
77 | | sentinel_new_impl(PyTypeObject *type, PyObject *name, PyObject *repr) |
78 | | /*[clinic end generated code: output=1eb7fab52e57d8c8 input=af2ba470062edc82]*/ |
79 | 152 | { |
80 | 152 | if (repr == Py_None) { |
81 | 148 | repr = NULL; |
82 | 148 | } |
83 | 4 | else if (!PyUnicode_Check(repr)) { |
84 | 0 | _PyArg_BadArgument("sentinel", "argument 'repr'", "str or None", repr); |
85 | 0 | return NULL; |
86 | 0 | } |
87 | 152 | PyObject *module = caller(); |
88 | 152 | PyObject *self = sentinel_new_with_module(type, name, module, repr); |
89 | 152 | Py_DECREF(module); |
90 | 152 | return self; |
91 | 152 | } |
92 | | |
93 | | PyObject * |
94 | | PySentinel_New(const char *name, const char *module_name, const char *repr) |
95 | 28 | { |
96 | 28 | PyObject *name_obj = PyUnicode_FromString(name); |
97 | 28 | if (name_obj == NULL) { |
98 | 0 | return NULL; |
99 | 0 | } |
100 | 28 | PyObject *repr_obj = NULL; |
101 | 28 | if (repr != NULL) { |
102 | 0 | repr_obj = PyUnicode_FromString(repr); |
103 | 0 | if (repr_obj == NULL) { |
104 | 0 | Py_DECREF(name_obj); |
105 | 0 | return NULL; |
106 | 0 | } |
107 | 0 | } |
108 | 28 | PyObject *module_obj = module_name == NULL |
109 | 28 | ? Py_None |
110 | 28 | : PyUnicode_FromString(module_name); |
111 | 28 | if (module_obj == NULL) { |
112 | 0 | Py_DECREF(name_obj); |
113 | 0 | Py_XDECREF(repr_obj); |
114 | 0 | return NULL; |
115 | 0 | } |
116 | | |
117 | 28 | PyObject *sentinel = sentinel_new_with_module( |
118 | 28 | &PySentinel_Type, name_obj, module_obj, repr_obj); |
119 | 28 | Py_DECREF(module_obj); |
120 | 28 | Py_DECREF(name_obj); |
121 | 28 | Py_XDECREF(repr_obj); |
122 | 28 | return sentinel; |
123 | 28 | } |
124 | | |
125 | | static int |
126 | | sentinel_clear(PyObject *op) |
127 | 28 | { |
128 | 28 | sentinelobject *self = sentinelobject_CAST(op); |
129 | 28 | Py_CLEAR(self->name); |
130 | 28 | Py_CLEAR(self->module); |
131 | 28 | Py_CLEAR(self->repr); |
132 | 28 | return 0; |
133 | 28 | } |
134 | | |
135 | | static void |
136 | | sentinel_dealloc(PyObject *op) |
137 | 28 | { |
138 | 28 | _PyObject_GC_UNTRACK(op); |
139 | 28 | (void)sentinel_clear(op); |
140 | 28 | Py_TYPE(op)->tp_free(op); |
141 | 28 | } |
142 | | |
143 | | static int |
144 | | sentinel_traverse(PyObject *op, visitproc visit, void *arg) |
145 | 8.62k | { |
146 | 8.62k | sentinelobject *self = sentinelobject_CAST(op); |
147 | 8.62k | Py_VISIT(self->name); |
148 | 8.62k | Py_VISIT(self->module); |
149 | 8.62k | Py_VISIT(self->repr); |
150 | 8.62k | return 0; |
151 | 8.62k | } |
152 | | |
153 | | static PyObject * |
154 | | sentinel_repr(PyObject *op) |
155 | 0 | { |
156 | 0 | sentinelobject *self = sentinelobject_CAST(op); |
157 | 0 | if (self->repr != NULL) { |
158 | 0 | return Py_NewRef(self->repr); |
159 | 0 | } |
160 | 0 | return Py_NewRef(self->name); |
161 | 0 | } |
162 | | |
163 | | static PyObject * |
164 | | sentinel_copy(PyObject *self, PyObject *Py_UNUSED(ignored)) |
165 | 0 | { |
166 | 0 | return Py_NewRef(self); |
167 | 0 | } |
168 | | |
169 | | static PyObject * |
170 | | sentinel_deepcopy(PyObject *self, PyObject *Py_UNUSED(memo)) |
171 | 0 | { |
172 | 0 | return Py_NewRef(self); |
173 | 0 | } |
174 | | |
175 | | static PyObject * |
176 | | sentinel_reduce(PyObject *op, PyObject *Py_UNUSED(ignored)) |
177 | 0 | { |
178 | 0 | sentinelobject *self = sentinelobject_CAST(op); |
179 | 0 | return Py_NewRef(self->name); |
180 | 0 | } |
181 | | |
182 | | static PyMethodDef sentinel_methods[] = { |
183 | | {"__copy__", sentinel_copy, METH_NOARGS, NULL}, |
184 | | {"__deepcopy__", sentinel_deepcopy, METH_O, NULL}, |
185 | | {"__reduce__", sentinel_reduce, METH_NOARGS, NULL}, |
186 | | {NULL, NULL} |
187 | | }; |
188 | | |
189 | | static PyMemberDef sentinel_members[] = { |
190 | | {"__name__", Py_T_OBJECT_EX, offsetof(sentinelobject, name), Py_READONLY}, |
191 | | {"__module__", Py_T_OBJECT_EX, offsetof(sentinelobject, module), 0}, |
192 | | {NULL} |
193 | | }; |
194 | | |
195 | | static PyNumberMethods sentinel_as_number = { |
196 | | .nb_or = _Py_union_type_or, |
197 | | }; |
198 | | |
199 | | PyDoc_STRVAR(sentinel_doc, |
200 | | "sentinel(name, /, *, repr=None)\n" |
201 | | "--\n\n" |
202 | | "Create a unique sentinel object with the given name."); |
203 | | |
204 | | PyTypeObject PySentinel_Type = { |
205 | | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
206 | | .tp_name = "sentinel", |
207 | | .tp_basicsize = sizeof(sentinelobject), |
208 | | .tp_dealloc = sentinel_dealloc, |
209 | | .tp_repr = sentinel_repr, |
210 | | .tp_as_number = &sentinel_as_number, |
211 | | .tp_hash = PyObject_GenericHash, |
212 | | .tp_getattro = PyObject_GenericGetAttr, |
213 | | .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE |
214 | | | Py_TPFLAGS_HAVE_GC, |
215 | | .tp_doc = sentinel_doc, |
216 | | .tp_traverse = sentinel_traverse, |
217 | | .tp_clear = sentinel_clear, |
218 | | .tp_richcompare = _Py_BaseObject_RichCompare, |
219 | | .tp_methods = sentinel_methods, |
220 | | .tp_members = sentinel_members, |
221 | | .tp_new = sentinel_new, |
222 | | .tp_vectorcall = sentinel_vectorcall, |
223 | | .tp_free = PyObject_GC_Del, |
224 | | }; |