Skip to content

Commit 901201f

Browse files
committed
+ fix bp/lp_float_complex + indentation
1 parent cc09ac8 commit 901201f

2 files changed

Lines changed: 26 additions & 22 deletions

File tree

‎Lib/test/test_struct.py‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -433,12 +433,12 @@ def test_float_complex_overflow(self):
433433
with self.subTest(value=value, format=format):
434434
self.assertRaises(OverflowError, struct.pack, format, value)
435435

436-
ba = bytearray(8)
437-
try:
438-
struct.Struct(format).pack_into(ba, 0, value)
439-
assert False
440-
except OverflowError:
441-
self.assertEqual(ba, bytearray(8))
436+
ba = bytearray(8)
437+
try:
438+
struct.Struct(format).pack_into(ba, 0, value)
439+
assert False
440+
except OverflowError:
441+
self.assertEqual(ba, bytearray(8))
442442

443443
def test_1530559(self):
444444
for code, byteorder in iter_integer_formats():

‎Modules/_struct.c‎

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -790,25 +790,20 @@ np_float_complex(_structmodulestate *state, char *p, PyObject *v,
790790
const formatdef *f)
791791
{
792792
Py_complex c = PyComplex_AsCComplex(v);
793+
char tmp[8];
793794

794795
if (c.real == -1 && PyErr_Occurred()) {
795796
PyErr_SetString(state->StructError,
796797
"required argument is not a complex");
797798
return -1;
798799
}
799-
800-
char tmp[8];
801-
int ret = PyFloat_Pack4(c.real, tmp, PY_LITTLE_ENDIAN);
802-
803-
if (ret) {
804-
return ret;
805-
}
806-
ret = PyFloat_Pack4(c.imag, tmp + 4, PY_LITTLE_ENDIAN);
807-
if (ret) {
808-
return ret;
800+
if (PyFloat_Pack4(c.real, tmp, PY_LITTLE_ENDIAN)
801+
|| PyFloat_Pack4(c.imag, tmp + 4, PY_LITTLE_ENDIAN))
802+
{
803+
return -1;
809804
}
810805
memcpy(p, tmp, 8);
811-
return ret;
806+
return 0;
812807
}
813808

814809
static int
@@ -1142,15 +1137,20 @@ static int
11421137
bp_float_complex(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
11431138
{
11441139
Py_complex x = PyComplex_AsCComplex(v);
1140+
char tmp[8];
1141+
11451142
if (x.real == -1 && PyErr_Occurred()) {
11461143
PyErr_SetString(state->StructError,
11471144
"required argument is not a complex");
11481145
return -1;
11491146
}
1150-
if (PyFloat_Pack4(x.real, p, 0)) {
1147+
if (PyFloat_Pack4(x.real, tmp, 0)
1148+
|| PyFloat_Pack4(x.imag, tmp + 4, 0))
1149+
{
11511150
return -1;
11521151
}
1153-
return PyFloat_Pack4(x.imag, p + 4, 0);
1152+
memcpy(p, tmp, 8);
1153+
return 0;
11541154
}
11551155

11561156
static int
@@ -1468,16 +1468,20 @@ static int
14681468
lp_float_complex(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
14691469
{
14701470
Py_complex x = PyComplex_AsCComplex(v);
1471+
char tmp[8];
1472+
14711473
if (x.real == -1 && PyErr_Occurred()) {
14721474
PyErr_SetString(state->StructError,
14731475
"required argument is not a complex");
14741476
return -1;
14751477
}
1476-
if (PyFloat_Pack4(x.real, p, 1)) {
1478+
if (PyFloat_Pack4(x.real, tmp, 1)
1479+
|| PyFloat_Pack4(x.imag, tmp + 4, 1))
1480+
{
14771481
return -1;
14781482
}
1479-
return PyFloat_Pack4(x.imag, p + 4, 1);
1480-
1483+
memcpy(p, tmp, 8);
1484+
return 0;
14811485
}
14821486

14831487
static int

0 commit comments

Comments
 (0)