/src/cpython/Objects/clinic/sentinelobject.c.h
Line | Count | Source |
1 | | /*[clinic input] |
2 | | preserve |
3 | | [clinic start generated code]*/ |
4 | | |
5 | | #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) |
6 | | # include "pycore_gc.h" // PyGC_Head |
7 | | # include "pycore_runtime.h" // _Py_ID() |
8 | | #endif |
9 | | #include "pycore_modsupport.h" // _PyArg_UnpackKeywords() |
10 | | |
11 | | static PyObject * |
12 | | sentinel_new_impl(PyTypeObject *type, PyObject *name, PyObject *repr); |
13 | | |
14 | | static PyObject * |
15 | | sentinel_new_helper(PyTypeObject *type, PyObject *const *args, |
16 | | Py_ssize_t nargs, Py_ssize_t nkw, PyObject *kwargs, PyObject *kwnames) |
17 | 152 | { |
18 | 152 | PyObject *return_value = NULL; |
19 | 152 | #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) |
20 | | |
21 | 152 | #define NUM_KEYWORDS 1 |
22 | 152 | static struct { |
23 | 152 | PyGC_Head _this_is_not_used; |
24 | 152 | PyObject_VAR_HEAD |
25 | 152 | Py_hash_t ob_hash; |
26 | 152 | PyObject *ob_item[NUM_KEYWORDS]; |
27 | 152 | } _kwtuple = { |
28 | 152 | .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) |
29 | 152 | .ob_hash = -1, |
30 | 152 | .ob_item = { &_Py_ID(repr), }, |
31 | 152 | }; |
32 | 152 | #undef NUM_KEYWORDS |
33 | 152 | #define KWTUPLE (&_kwtuple.ob_base.ob_base) |
34 | | |
35 | | #else // !Py_BUILD_CORE |
36 | | # define KWTUPLE NULL |
37 | | #endif // !Py_BUILD_CORE |
38 | | |
39 | 152 | static const char * const _keywords[] = {"", "repr", NULL}; |
40 | 152 | static _PyArg_Parser _parser = { |
41 | 152 | .keywords = _keywords, |
42 | 152 | .fname = "sentinel", |
43 | 152 | .kwtuple = KWTUPLE, |
44 | 152 | }; |
45 | 152 | #undef KWTUPLE |
46 | 152 | PyObject *argsbuf[2]; |
47 | 152 | PyObject * const *fastargs; |
48 | 152 | Py_ssize_t noptargs = nargs + nkw - 1; |
49 | 152 | PyObject *name; |
50 | 152 | PyObject *repr = Py_None; |
51 | | |
52 | 152 | fastargs = _PyArg_UnpackKeywords(args, nargs, kwargs, kwnames, &_parser, |
53 | 152 | /*minpos*/ 1, /*maxpos*/ 1, /*minkw*/ 0, /*varpos*/ 0, argsbuf); |
54 | 152 | if (!fastargs) { |
55 | 0 | goto exit; |
56 | 0 | } |
57 | 152 | if (!PyUnicode_Check(fastargs[0])) { |
58 | 0 | _PyArg_BadArgument("sentinel", "argument 1", "str", fastargs[0]); |
59 | 0 | goto exit; |
60 | 0 | } |
61 | 152 | name = fastargs[0]; |
62 | 152 | if (!noptargs) { |
63 | 148 | goto skip_optional_kwonly; |
64 | 148 | } |
65 | 4 | repr = fastargs[1]; |
66 | 152 | skip_optional_kwonly: |
67 | 152 | return_value = sentinel_new_impl(type, name, repr); |
68 | | |
69 | 152 | exit: |
70 | 152 | return return_value; |
71 | 152 | } |
72 | | |
73 | | static PyObject * |
74 | | sentinel_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
75 | 0 | { |
76 | 0 | return sentinel_new_helper(type, _PyTuple_CAST(args)->ob_item, |
77 | 0 | PyTuple_GET_SIZE(args), |
78 | 0 | kwargs ? PyDict_GET_SIZE(kwargs) : 0, |
79 | 0 | kwargs, NULL); |
80 | 0 | } |
81 | | |
82 | | static PyObject * |
83 | | sentinel_vectorcall(PyObject *type, PyObject *const *args, |
84 | | size_t nargsf, PyObject *kwnames) |
85 | 152 | { |
86 | 152 | Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); |
87 | | |
88 | 152 | assert(_PyType_CAST(type)->tp_new == sentinel_new); |
89 | | /* Make sure the type object is immutable: the generated |
90 | | * vectorcall doesn't deal e.g. with users reassigning __init__. */ |
91 | 152 | assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE)); |
92 | 152 | return sentinel_new_helper(_PyType_CAST(type), args, nargs, |
93 | 152 | kwnames ? PyTuple_GET_SIZE(kwnames) : 0, |
94 | | NULL, kwnames); |
95 | 152 | } |
96 | | /*[clinic end generated code: output=388b220fe03ae62c input=a9049054013a1b77]*/ |