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
|
#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;
}
|