Debian Codegraph @

Modules/_testcapi/long.c

raw
1 #ifndef Py_BUILD_CORE_BUILTIN
2 # define Py_BUILD_CORE_MODULE 1
3 #endif
4
5 #include "parts.h"
6 #include "util.h"
7 #include "clinic/long.c.h"
8
9 /*[clinic input]
10 module _testcapi
11 [clinic start generated code]*/
12 /*[clinic end generated code: output=da39a3ee5e6b4b0d input=6361033e795369fc]*/
13
14
15 /*[clinic input]
16 _testcapi.call_long_compact_api
17 arg: object
18 /
19 [clinic start generated code]*/
20
21 static PyObject *
22 _testcapi_call_long_compact_api(PyObject *module, PyObject *arg)
23 /*[clinic end generated code: output=7e3894f611b1b2b7 input=87b87396967af14c]*/
24
25 {
26 assert(PyLong_Check(arg));
27 int is_compact = PyUnstable_Long_IsCompact((PyLongObject*)arg);
28 Py_ssize_t value = -1;
29 if (is_compact) {
30 value = PyUnstable_Long_CompactValue((PyLongObject*)arg);
31 }
32 return Py_BuildValue("in", is_compact, value);
33 }
34
35
36 static PyObject *
37 pylong_fromunicodeobject(PyObject *module, PyObject *args)
38 {
39 PyObject *unicode;
40 int base;
41 if (!PyArg_ParseTuple(args, "Oi", &unicode, &base)) {
42 return NULL;
43 }
44
45 NULLABLE(unicode);
46 return PyLong_FromUnicodeObject(unicode, base);
47 }
48
49
50 static PyObject *
51 pylong_asnativebytes(PyObject *module, PyObject *args)
52 {
53 PyObject *v;
54 Py_buffer buffer;
55 Py_ssize_t n, flags;
56 if (!PyArg_ParseTuple(args, "Ow*nn", &v, &buffer, &n, &flags)) {
57 return NULL;
58 }
59 if (buffer.readonly) {
60 PyErr_SetString(PyExc_TypeError, "buffer must be writable");
61 PyBuffer_Release(&buffer);
62 return NULL;
63 }
64 if (buffer.len < n) {
65 PyErr_SetString(PyExc_ValueError, "buffer must be at least 'n' bytes");
66 PyBuffer_Release(&buffer);
67 return NULL;
68 }
69 Py_ssize_t res = PyLong_AsNativeBytes(v, buffer.buf, n, (int)flags);
70 PyBuffer_Release(&buffer);
71 return res >= 0 ? PyLong_FromSsize_t(res) : NULL;
72 }
73
74
75 static PyObject *
76 pylong_fromnativebytes(PyObject *module, PyObject *args)
77 {
78 Py_buffer buffer;
79 Py_ssize_t n, flags, signed_;
80 if (!PyArg_ParseTuple(args, "y*nnn", &buffer, &n, &flags, &signed_)) {
81 return NULL;
82 }
83 if (buffer.len < n) {
84 PyErr_SetString(PyExc_ValueError, "buffer must be at least 'n' bytes");
85 PyBuffer_Release(&buffer);
86 return NULL;
87 }
88 PyObject *res = signed_
89 ? PyLong_FromNativeBytes(buffer.buf, n, (int)flags)
90 : PyLong_FromUnsignedNativeBytes(buffer.buf, n, (int)flags);
91 PyBuffer_Release(&buffer);
92 return res;
93 }
94
95
96 static PyObject *
97 pylong_getsign(PyObject *module, PyObject *arg)
98 {
99 int sign;
100 NULLABLE(arg);
101 if (PyLong_GetSign(arg, &sign) == -1) {
102 return NULL;
103 }
104 return PyLong_FromLong(sign);
105 }
106
107
108 static PyObject *
109 pylong_ispositive(PyObject *module, PyObject *arg)
110 {
111 NULLABLE(arg);
112 RETURN_INT(PyLong_IsPositive(arg));
113 }
114
115
116 static PyObject *
117 pylong_isnegative(PyObject *module, PyObject *arg)
118 {
119 NULLABLE(arg);
120 RETURN_INT(PyLong_IsNegative(arg));
121 }
122
123
124 static PyObject *
125 pylong_iszero(PyObject *module, PyObject *arg)
126 {
127 NULLABLE(arg);
128 RETURN_INT(PyLong_IsZero(arg));
129 }
130
131
132 static PyObject *
133 pylong_aspid(PyObject *module, PyObject *arg)
134 {
135 NULLABLE(arg);
136 pid_t value = PyLong_AsPid(arg);
137 if (value == -1 && PyErr_Occurred()) {
138 return NULL;
139 }
140 return PyLong_FromPid(value);
141 }
142
143
144 static PyObject *
145 layout_to_dict(const PyLongLayout *layout)
146 {
147 return Py_BuildValue("{sisisisi}",
148 "bits_per_digit", (int)layout->bits_per_digit,
149 "digit_size", (int)layout->digit_size,
150 "digits_order", (int)layout->digits_order,
151 "digit_endianness", (int)layout->digit_endianness);
152 }
153
154
155 static PyObject *
156 pylong_export(PyObject *module, PyObject *obj)
157 {
158 PyLongExport export_long;
159 if (PyLong_Export(obj, &export_long) < 0) {
160 return NULL;
161 }
162
163 if (export_long.digits == NULL) {
164 assert(export_long.negative == 0);
165 assert(export_long.ndigits == 0);
166 assert(export_long.digits == NULL);
167 PyObject *res = PyLong_FromInt64(export_long.value);
168 PyLong_FreeExport(&export_long);
169 return res;
170 }
171
172 assert(PyLong_GetNativeLayout()->digit_size == sizeof(digit));
173 const digit *export_long_digits = export_long.digits;
174
175 PyObject *digits = PyList_New(0);
176 if (digits == NULL) {
177 goto error;
178 }
179 for (Py_ssize_t i = 0; i < export_long.ndigits; i++) {
180 PyObject *item = PyLong_FromUnsignedLong(export_long_digits[i]);
181 if (item == NULL) {
182 goto error;
183 }
184
185 if (PyList_Append(digits, item) < 0) {
186 Py_DECREF(item);
187 goto error;
188 }
189 Py_DECREF(item);
190 }
191
192 assert(export_long.value == 0);
193 PyObject *res = Py_BuildValue("(iN)", export_long.negative, digits);
194
195 PyLong_FreeExport(&export_long);
196 assert(export_long._reserved == 0);
197
198 return res;
199
200 error:
201 Py_XDECREF(digits);
202 PyLong_FreeExport(&export_long);
203 return NULL;
204 }
205
206
207 static PyObject *
208 pylongwriter_create(PyObject *module, PyObject *args)
209 {
210 int negative;
211 PyObject *list;
212 // TODO(vstinner): write test for negative ndigits and digits==NULL
213 if (!PyArg_ParseTuple(args, "iO!", &negative, &PyList_Type, &list)) {
214 return NULL;
215 }
216 Py_ssize_t ndigits = PyList_GET_SIZE(list);
217
218 digit *digits = PyMem_Malloc((size_t)ndigits * sizeof(digit));
219 if (digits == NULL) {
220 return PyErr_NoMemory();
221 }
222
223 for (Py_ssize_t i = 0; i < ndigits; i++) {
224 PyObject *item = PyList_GET_ITEM(list, i);
225
226 long num = PyLong_AsLong(item);
227 if (num == -1 && PyErr_Occurred()) {
228 goto error;
229 }
230
231 if (num < 0 || num >= (long)PyLong_BASE) {
232 PyErr_SetString(PyExc_ValueError, "digit doesn't fit into digit");
233 goto error;
234 }
235 digits[i] = (digit)num;
236 }
237
238 void *writer_digits;
239 PyLongWriter *writer = PyLongWriter_Create(negative, ndigits,
240 &writer_digits);
241 if (writer == NULL) {
242 goto error;
243 }
244 assert(PyLong_GetNativeLayout()->digit_size == sizeof(digit));
245 memcpy(writer_digits, digits, (size_t)ndigits * sizeof(digit));
246 PyObject *res = PyLongWriter_Finish(writer);
247 PyMem_Free(digits);
248
249 return res;
250
251 error:
252 PyMem_Free(digits);
253 return NULL;
254 }
255
256
257 static PyObject *
258 get_pylong_layout(PyObject *module, PyObject *Py_UNUSED(args))
259 {
260 const PyLongLayout *layout = PyLong_GetNativeLayout();
261 return layout_to_dict(layout);
262 }
263
264
265 static PyMethodDef test_methods[] = {
266 _TESTCAPI_CALL_LONG_COMPACT_API_METHODDEF
267 {"pylong_fromunicodeobject", pylong_fromunicodeobject, METH_VARARGS},
268 {"pylong_asnativebytes", pylong_asnativebytes, METH_VARARGS},
269 {"pylong_fromnativebytes", pylong_fromnativebytes, METH_VARARGS},
270 {"pylong_getsign", pylong_getsign, METH_O},
271 {"pylong_aspid", pylong_aspid, METH_O},
272 {"pylong_export", pylong_export, METH_O},
273 {"pylongwriter_create", pylongwriter_create, METH_VARARGS},
274 {"get_pylong_layout", get_pylong_layout, METH_NOARGS},
275 {"pylong_ispositive", pylong_ispositive, METH_O},
276 {"pylong_isnegative", pylong_isnegative, METH_O},
277 {"pylong_iszero", pylong_iszero, METH_O},
278 {NULL},
279 };
280
281 int
282 _PyTestCapi_Init_Long(PyObject *mod)
283 {
284 if (PyModule_AddFunctions(mod, test_methods) < 0) {
285 return -1;
286 }
287 return 0;
288 }