Skip to content

Commit

Permalink
gh-93649: Split float/long tests from _testcapimodule.c (GH-99549)
Browse files Browse the repository at this point in the history
Automerge-Triggered-By: GH:erlend-aasland
  • Loading branch information
erlend-aasland authored Nov 17, 2022
1 parent 5fdd49d commit 12b5a3c
Show file tree
Hide file tree
Showing 9 changed files with 677 additions and 625 deletions.
2 changes: 1 addition & 1 deletion Makefile.pre.in
Original file line number Diff line number Diff line change
Expand Up @@ -2595,7 +2595,7 @@ MODULE__SHA3_DEPS=$(srcdir)/Modules/_sha3/sha3.c $(srcdir)/Modules/_sha3/sha3.h
MODULE__SHA512_DEPS=$(srcdir)/Modules/hashlib.h
MODULE__SOCKET_DEPS=$(srcdir)/Modules/socketmodule.h $(srcdir)/Modules/addrinfo.h $(srcdir)/Modules/getaddrinfo.c $(srcdir)/Modules/getnameinfo.c
MODULE__SSL_DEPS=$(srcdir)/Modules/_ssl.h $(srcdir)/Modules/_ssl/cert.c $(srcdir)/Modules/_ssl/debughelpers.c $(srcdir)/Modules/_ssl/misc.c $(srcdir)/Modules/_ssl_data.h $(srcdir)/Modules/_ssl_data_111.h $(srcdir)/Modules/_ssl_data_300.h $(srcdir)/Modules/socketmodule.h
MODULE__TESTCAPI_DEPS=$(srcdir)/Modules/testcapi_long.h $(srcdir)/Modules/_testcapi/parts.h
MODULE__TESTCAPI_DEPS=$(srcdir)/Modules/_testcapi/testcapi_long.h $(srcdir)/Modules/_testcapi/parts.h
MODULE__SQLITE3_DEPS=$(srcdir)/Modules/_sqlite/connection.h $(srcdir)/Modules/_sqlite/cursor.h $(srcdir)/Modules/_sqlite/microprotocols.h $(srcdir)/Modules/_sqlite/module.h $(srcdir)/Modules/_sqlite/prepare_protocol.h $(srcdir)/Modules/_sqlite/row.h $(srcdir)/Modules/_sqlite/util.h

# IF YOU PUT ANYTHING HERE IT WILL GO AWAY
Expand Down
2 changes: 1 addition & 1 deletion Modules/Setup.stdlib.in
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@
@MODULE__XXTESTFUZZ_TRUE@_xxtestfuzz _xxtestfuzz/_xxtestfuzz.c _xxtestfuzz/fuzzer.c
@MODULE__TESTBUFFER_TRUE@_testbuffer _testbuffer.c
@MODULE__TESTINTERNALCAPI_TRUE@_testinternalcapi _testinternalcapi.c
@MODULE__TESTCAPI_TRUE@_testcapi _testcapimodule.c _testcapi/vectorcall.c _testcapi/vectorcall_limited.c _testcapi/heaptype.c _testcapi/unicode.c _testcapi/getargs.c _testcapi/pytime.c _testcapi/datetime.c _testcapi/docstring.c _testcapi/mem.c _testcapi/watchers.c
@MODULE__TESTCAPI_TRUE@_testcapi _testcapimodule.c _testcapi/vectorcall.c _testcapi/vectorcall_limited.c _testcapi/heaptype.c _testcapi/unicode.c _testcapi/getargs.c _testcapi/pytime.c _testcapi/datetime.c _testcapi/docstring.c _testcapi/mem.c _testcapi/watchers.c _testcapi/long.c _testcapi/float.c

# Some testing modules MUST be built as shared libraries.
*shared*
Expand Down
98 changes: 98 additions & 0 deletions Modules/_testcapi/float.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#define PY_SSIZE_T_CLEAN

#include "parts.h"


// Test PyFloat_Pack2(), PyFloat_Pack4() and PyFloat_Pack8()
static PyObject *
test_float_pack(PyObject *self, PyObject *args)
{
int size;
double d;
int le;
if (!PyArg_ParseTuple(args, "idi", &size, &d, &le)) {
return NULL;
}
switch (size)
{
case 2:
{
char data[2];
if (PyFloat_Pack2(d, data, le) < 0) {
return NULL;
}
return PyBytes_FromStringAndSize(data, Py_ARRAY_LENGTH(data));
}
case 4:
{
char data[4];
if (PyFloat_Pack4(d, data, le) < 0) {
return NULL;
}
return PyBytes_FromStringAndSize(data, Py_ARRAY_LENGTH(data));
}
case 8:
{
char data[8];
if (PyFloat_Pack8(d, data, le) < 0) {
return NULL;
}
return PyBytes_FromStringAndSize(data, Py_ARRAY_LENGTH(data));
}
default: break;
}

PyErr_SetString(PyExc_ValueError, "size must 2, 4 or 8");
return NULL;
}


// Test PyFloat_Unpack2(), PyFloat_Unpack4() and PyFloat_Unpack8()
static PyObject *
test_float_unpack(PyObject *self, PyObject *args)
{
assert(!PyErr_Occurred());
const char *data;
Py_ssize_t size;
int le;
if (!PyArg_ParseTuple(args, "y#i", &data, &size, &le)) {
return NULL;
}
double d;
switch (size)
{
case 2:
d = PyFloat_Unpack2(data, le);
break;
case 4:
d = PyFloat_Unpack4(data, le);
break;
case 8:
d = PyFloat_Unpack8(data, le);
break;
default:
PyErr_SetString(PyExc_ValueError, "data length must 2, 4 or 8 bytes");
return NULL;
}

if (d == -1.0 && PyErr_Occurred()) {
return NULL;
}
return PyFloat_FromDouble(d);
}

static PyMethodDef test_methods[] = {
{"float_pack", test_float_pack, METH_VARARGS, NULL},
{"float_unpack", test_float_unpack, METH_VARARGS, NULL},
{NULL},
};

int
_PyTestCapi_Init_Float(PyObject *mod)
{
if (PyModule_AddFunctions(mod, test_methods) < 0) {
return -1;
}

return 0;
}
Loading

0 comments on commit 12b5a3c

Please sign in to comment.