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