| 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 |
static PyObject * |
| 96 |
pylong_aspid(PyObject *module, PyObject *arg) |
| 97 |
{ |
| 98 |
NULLABLE(arg); |
| 99 |
pid_t value = PyLong_AsPid(arg); |
| 100 |
if (value == -1 && PyErr_Occurred()) { |
| 101 |
return NULL; |
| 102 |
} |
| 103 |
return PyLong_FromPid(value); |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
static PyMethodDef test_methods[] = { |
| 108 |
_TESTCAPI_CALL_LONG_COMPACT_API_METHODDEF |
| 109 |
{"pylong_fromunicodeobject", pylong_fromunicodeobject, METH_VARARGS}, |
| 110 |
{"pylong_asnativebytes", pylong_asnativebytes, METH_VARARGS}, |
| 111 |
{"pylong_fromnativebytes", pylong_fromnativebytes, METH_VARARGS}, |
| 112 |
{"pylong_aspid", pylong_aspid, METH_O}, |
| 113 |
{NULL}, |
| 114 |
}; |
| 115 |
|
| 116 |
int |
| 117 |
_PyTestCapi_Init_Long(PyObject *mod) |
| 118 |
{ |
| 119 |
if (PyModule_AddFunctions(mod, test_methods) < 0) { |
| 120 |
return -1; |
| 121 |
} |
| 122 |
return 0; |
| 123 |
} |