2023-09-01 04:13:07 -03:00
|
|
|
#ifndef Py_BUILD_CORE_BUILTIN
|
|
|
|
# define Py_BUILD_CORE_MODULE 1
|
|
|
|
#endif
|
|
|
|
|
2022-11-17 04:56:56 -04:00
|
|
|
#include "parts.h"
|
2023-10-14 02:53:57 -03:00
|
|
|
#include "util.h"
|
2023-05-23 10:52:36 -03:00
|
|
|
#include "clinic/long.c.h"
|
|
|
|
|
|
|
|
/*[clinic input]
|
|
|
|
module _testcapi
|
|
|
|
[clinic start generated code]*/
|
|
|
|
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=6361033e795369fc]*/
|
2022-11-17 04:56:56 -04:00
|
|
|
|
|
|
|
|
2023-05-23 10:52:36 -03:00
|
|
|
/*[clinic input]
|
|
|
|
_testcapi.call_long_compact_api
|
|
|
|
arg: object
|
|
|
|
/
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
2023-05-21 10:45:48 -03:00
|
|
|
static PyObject *
|
2023-05-23 10:52:36 -03:00
|
|
|
_testcapi_call_long_compact_api(PyObject *module, PyObject *arg)
|
|
|
|
/*[clinic end generated code: output=7e3894f611b1b2b7 input=87b87396967af14c]*/
|
|
|
|
|
2023-05-21 10:45:48 -03:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2023-10-14 02:53:57 -03:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-02-12 16:13:13 -04:00
|
|
|
static PyObject *
|
|
|
|
pylong_asnativebytes(PyObject *module, PyObject *args)
|
|
|
|
{
|
|
|
|
PyObject *v;
|
|
|
|
Py_buffer buffer;
|
2024-04-05 11:21:16 -03:00
|
|
|
Py_ssize_t n, flags;
|
|
|
|
if (!PyArg_ParseTuple(args, "Ow*nn", &v, &buffer, &n, &flags)) {
|
2024-02-12 16:13:13 -04:00
|
|
|
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;
|
|
|
|
}
|
2024-04-05 11:21:16 -03:00
|
|
|
Py_ssize_t res = PyLong_AsNativeBytes(v, buffer.buf, n, (int)flags);
|
2024-02-12 16:13:13 -04:00
|
|
|
PyBuffer_Release(&buffer);
|
|
|
|
return res >= 0 ? PyLong_FromSsize_t(res) : NULL;
|
|
|
|
}
|
|
|
|
|
2024-03-19 11:04:23 -03:00
|
|
|
|
2024-02-12 16:13:13 -04:00
|
|
|
static PyObject *
|
|
|
|
pylong_fromnativebytes(PyObject *module, PyObject *args)
|
|
|
|
{
|
|
|
|
Py_buffer buffer;
|
2024-04-05 11:21:16 -03:00
|
|
|
Py_ssize_t n, flags, signed_;
|
|
|
|
if (!PyArg_ParseTuple(args, "y*nnn", &buffer, &n, &flags, &signed_)) {
|
2024-02-12 16:13:13 -04:00
|
|
|
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_
|
2024-04-05 11:21:16 -03:00
|
|
|
? PyLong_FromNativeBytes(buffer.buf, n, (int)flags)
|
|
|
|
: PyLong_FromUnsignedNativeBytes(buffer.buf, n, (int)flags);
|
2024-02-12 16:13:13 -04:00
|
|
|
PyBuffer_Release(&buffer);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2024-06-03 09:06:31 -03:00
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
pylong_getsign(PyObject *module, PyObject *arg)
|
|
|
|
{
|
|
|
|
int sign;
|
|
|
|
NULLABLE(arg);
|
|
|
|
if (PyLong_GetSign(arg, &sign) == -1) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return PyLong_FromLong(sign);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-03-20 10:39:53 -03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2024-02-12 16:13:13 -04:00
|
|
|
|
2022-11-17 04:56:56 -04:00
|
|
|
static PyMethodDef test_methods[] = {
|
2023-05-23 10:52:36 -03:00
|
|
|
_TESTCAPI_CALL_LONG_COMPACT_API_METHODDEF
|
2023-10-14 02:53:57 -03:00
|
|
|
{"pylong_fromunicodeobject", pylong_fromunicodeobject, METH_VARARGS},
|
2024-02-12 16:13:13 -04:00
|
|
|
{"pylong_asnativebytes", pylong_asnativebytes, METH_VARARGS},
|
|
|
|
{"pylong_fromnativebytes", pylong_fromnativebytes, METH_VARARGS},
|
2024-06-03 09:06:31 -03:00
|
|
|
{"pylong_getsign", pylong_getsign, METH_O},
|
2024-03-20 10:39:53 -03:00
|
|
|
{"pylong_aspid", pylong_aspid, METH_O},
|
2022-11-17 04:56:56 -04:00
|
|
|
{NULL},
|
|
|
|
};
|
|
|
|
|
|
|
|
int
|
|
|
|
_PyTestCapi_Init_Long(PyObject *mod)
|
|
|
|
{
|
|
|
|
if (PyModule_AddFunctions(mod, test_methods) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|