gh-111495: Add tests for PyFloat C API (GH-111624)

This commit is contained in:
Sergey B Kirpichev 2023-11-05 10:20:12 +03:00 committed by GitHub
parent 72e27a67b9
commit b452202a11
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 191 additions and 0 deletions

View file

@ -2,9 +2,75 @@
#define PYTESTCAPI_NEED_INTERNAL_API
#include "parts.h"
#include "util.h"
#include "clinic/float.c.h"
static PyObject *
float_check(PyObject *Py_UNUSED(module), PyObject *obj)
{
NULLABLE(obj);
return PyLong_FromLong(PyFloat_Check(obj));
}
static PyObject *
float_checkexact(PyObject *Py_UNUSED(module), PyObject *obj)
{
NULLABLE(obj);
return PyLong_FromLong(PyFloat_CheckExact(obj));
}
static PyObject *
float_fromstring(PyObject *Py_UNUSED(module), PyObject *obj)
{
NULLABLE(obj);
return PyFloat_FromString(obj);
}
static PyObject *
float_fromdouble(PyObject *Py_UNUSED(module), PyObject *obj)
{
double d;
if (!PyArg_Parse(obj, "d", &d)) {
return NULL;
}
return PyFloat_FromDouble(d);
}
static PyObject *
float_asdouble(PyObject *Py_UNUSED(module), PyObject *obj)
{
double d;
NULLABLE(obj);
d = PyFloat_AsDouble(obj);
if (d == -1. && PyErr_Occurred()) {
return NULL;
}
return PyFloat_FromDouble(d);
}
static PyObject *
float_getinfo(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(arg))
{
return PyFloat_GetInfo();
}
static PyObject *
float_getmax(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(arg))
{
return PyFloat_FromDouble(PyFloat_GetMax());
}
static PyObject *
float_getmin(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(arg))
{
return PyFloat_FromDouble(PyFloat_GetMin());
}
/*[clinic input]
module _testcapi
[clinic start generated code]*/
@ -99,6 +165,14 @@ _testcapi_float_unpack_impl(PyObject *module, const char *data,
}
static PyMethodDef test_methods[] = {
{"float_check", float_check, METH_O},
{"float_checkexact", float_checkexact, METH_O},
{"float_fromstring", float_fromstring, METH_O},
{"float_fromdouble", float_fromdouble, METH_O},
{"float_asdouble", float_asdouble, METH_O},
{"float_getinfo", float_getinfo, METH_NOARGS},
{"float_getmax", float_getmax, METH_NOARGS},
{"float_getmin", float_getmin, METH_NOARGS},
_TESTCAPI_FLOAT_PACK_METHODDEF
_TESTCAPI_FLOAT_UNPACK_METHODDEF
{NULL},