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
10
11 PyDoc_STRVAR(_dbm_dbm_close__doc__,
12 "close($self, /)\n"
13 "--\n"
14 "\n"
15 "Close the database.");
16
17 #define _DBM_DBM_CLOSE_METHODDEF \
18 {"close", (PyCFunction)_dbm_dbm_close, METH_NOARGS, _dbm_dbm_close__doc__},
19
20 static PyObject *
21 _dbm_dbm_close_impl(dbmobject *self);
22
23 static PyObject *
24 _dbm_dbm_close(dbmobject *self, PyObject *Py_UNUSED(ignored))
25 {
26 return _dbm_dbm_close_impl(self);
27 }
28
29 PyDoc_STRVAR(_dbm_dbm_keys__doc__,
30 "keys($self, /)\n"
31 "--\n"
32 "\n"
33 "Return a list of all keys in the database.");
34
35 #define _DBM_DBM_KEYS_METHODDEF \
36 {"keys", _PyCFunction_CAST(_dbm_dbm_keys), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _dbm_dbm_keys__doc__},
37
38 static PyObject *
39 _dbm_dbm_keys_impl(dbmobject *self, PyTypeObject *cls);
40
41 static PyObject *
42 _dbm_dbm_keys(dbmobject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
43 {
44 if (nargs) {
45 PyErr_SetString(PyExc_TypeError, "keys() takes no arguments");
46 return NULL;
47 }
48 return _dbm_dbm_keys_impl(self, cls);
49 }
50
51 PyDoc_STRVAR(_dbm_dbm_get__doc__,
52 "get($self, key, default=None, /)\n"
53 "--\n"
54 "\n"
55 "Return the value for key if present, otherwise default.");
56
57 #define _DBM_DBM_GET_METHODDEF \
58 {"get", _PyCFunction_CAST(_dbm_dbm_get), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _dbm_dbm_get__doc__},
59
60 static PyObject *
61 _dbm_dbm_get_impl(dbmobject *self, PyTypeObject *cls, const char *key,
62 Py_ssize_t key_length, PyObject *default_value);
63
64 static PyObject *
65 _dbm_dbm_get(dbmobject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
66 {
67 PyObject *return_value = NULL;
68 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
69 # define KWTUPLE (PyObject *)&_Py_SINGLETON(tuple_empty)
70 #else
71 # define KWTUPLE NULL
72 #endif
73
74 static const char * const _keywords[] = {"", "", NULL};
75 static _PyArg_Parser _parser = {
76 .keywords = _keywords,
77 .format = "s#|O:get",
78 .kwtuple = KWTUPLE,
79 };
80 #undef KWTUPLE
81 const char *key;
82 Py_ssize_t key_length;
83 PyObject *default_value = Py_None;
84
85 if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
86 &key, &key_length, &default_value)) {
87 goto exit;
88 }
89 return_value = _dbm_dbm_get_impl(self, cls, key, key_length, default_value);
90
91 exit:
92 return return_value;
93 }
94
95 PyDoc_STRVAR(_dbm_dbm_setdefault__doc__,
96 "setdefault($self, key, default=b\'\', /)\n"
97 "--\n"
98 "\n"
99 "Return the value for key if present, otherwise default.\n"
100 "\n"
101 "If key is not in the database, it is inserted with default as the value.");
102
103 #define _DBM_DBM_SETDEFAULT_METHODDEF \
104 {"setdefault", _PyCFunction_CAST(_dbm_dbm_setdefault), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _dbm_dbm_setdefault__doc__},
105
106 static PyObject *
107 _dbm_dbm_setdefault_impl(dbmobject *self, PyTypeObject *cls, const char *key,
108 Py_ssize_t key_length, PyObject *default_value);
109
110 static PyObject *
111 _dbm_dbm_setdefault(dbmobject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
112 {
113 PyObject *return_value = NULL;
114 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
115 # define KWTUPLE (PyObject *)&_Py_SINGLETON(tuple_empty)
116 #else
117 # define KWTUPLE NULL
118 #endif
119
120 static const char * const _keywords[] = {"", "", NULL};
121 static _PyArg_Parser _parser = {
122 .keywords = _keywords,
123 .format = "s#|O:setdefault",
124 .kwtuple = KWTUPLE,
125 };
126 #undef KWTUPLE
127 const char *key;
128 Py_ssize_t key_length;
129 PyObject *default_value = NULL;
130
131 if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
132 &key, &key_length, &default_value)) {
133 goto exit;
134 }
135 return_value = _dbm_dbm_setdefault_impl(self, cls, key, key_length, default_value);
136
137 exit:
138 return return_value;
139 }
140
141 PyDoc_STRVAR(dbmopen__doc__,
142 "open($module, filename, flags=\'r\', mode=0o666, /)\n"
143 "--\n"
144 "\n"
145 "Return a database object.\n"
146 "\n"
147 " filename\n"
148 " The filename to open.\n"
149 " flags\n"
150 " How to open the file. \"r\" for reading, \"w\" for writing, etc.\n"
151 " mode\n"
152 " If creating a new file, the mode bits for the new file\n"
153 " (e.g. os.O_RDWR).");
154
155 #define DBMOPEN_METHODDEF \
156 {"open", _PyCFunction_CAST(dbmopen), METH_FASTCALL, dbmopen__doc__},
157
158 static PyObject *
159 dbmopen_impl(PyObject *module, PyObject *filename, const char *flags,
160 int mode);
161
162 static PyObject *
163 dbmopen(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
164 {
165 PyObject *return_value = NULL;
166 PyObject *filename;
167 const char *flags = "r";
168 int mode = 438;
169
170 if (!_PyArg_CheckPositional("open", nargs, 1, 3)) {
171 goto exit;
172 }
173 filename = args[0];
174 if (nargs < 2) {
175 goto skip_optional;
176 }
177 if (!PyUnicode_Check(args[1])) {
178 _PyArg_BadArgument("open", "argument 2", "str", args[1]);
179 goto exit;
180 }
181 Py_ssize_t flags_length;
182 flags = PyUnicode_AsUTF8AndSize(args[1], &flags_length);
183 if (flags == NULL) {
184 goto exit;
185 }
186 if (strlen(flags) != (size_t)flags_length) {
187 PyErr_SetString(PyExc_ValueError, "embedded null character");
188 goto exit;
189 }
190 if (nargs < 3) {
191 goto skip_optional;
192 }
193 mode = _PyLong_AsInt(args[2]);
194 if (mode == -1 && PyErr_Occurred()) {
195 goto exit;
196 }
197 skip_optional:
198 return_value = dbmopen_impl(module, filename, flags, mode);
199
200 exit:
201 return return_value;
202 }
203 /*[clinic end generated code: output=28dcf736654137c2 input=a9049054013a1b77]*/