Skip to content

Commit 42da46e

Browse files
bpo-29753: revert 0d7ad9f (GH-19850) (GH-27085)
This reverts commit 0d7ad9f as it has a regression. See https://github.com/python/cpython/pull/19850GH-issuecomment-869410686 (cherry picked from commit e14d5ae) Co-authored-by: Filipe Laíns <[email protected]>
1 parent 1577259 commit 42da46e

File tree

2 files changed

+8
-90
lines changed

2 files changed

+8
-90
lines changed

Lib/ctypes/test/test_bitfields.py

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -233,69 +233,6 @@ class X(Structure):
233233
else:
234234
self.assertEqual(sizeof(X), sizeof(c_int) * 2)
235235

236-
@unittest.skipIf(os.name == 'nt', reason='Posix only')
237-
def test_packed_posix(self):
238-
test_cases = {
239-
(
240-
("a", c_uint8, 4),
241-
("b", c_uint8, 4),
242-
): 1,
243-
(
244-
("a", c_uint8, 1),
245-
("b", c_uint16, 1),
246-
("c", c_uint32, 1),
247-
("d", c_uint64, 1),
248-
): 1,
249-
(
250-
("a", c_uint8, 8),
251-
("b", c_uint16, 1),
252-
("c", c_uint32, 1),
253-
("d", c_uint64, 1),
254-
): 2,
255-
(
256-
("a", c_uint32, 9),
257-
("b", c_uint16, 10),
258-
("c", c_uint32, 25),
259-
("d", c_uint64, 1),
260-
): 6,
261-
(
262-
("a", c_uint32, 9),
263-
("b", c_uint16, 10),
264-
("c", c_uint32, 25),
265-
("d", c_uint64, 5),
266-
): 7,
267-
(
268-
("a", c_uint16),
269-
("b", c_uint16, 9),
270-
("c", c_uint16, 1),
271-
("d", c_uint16, 1),
272-
("e", c_uint16, 1),
273-
("f", c_uint16, 1),
274-
("g", c_uint16, 3),
275-
("h", c_uint32, 10),
276-
("i", c_uint32, 20),
277-
("j", c_uint32, 2),
278-
): 8,
279-
(
280-
("a", c_uint16, 9),
281-
("b", c_uint16, 10),
282-
("d", c_uint16),
283-
("c", c_uint8, 8),
284-
): 6,
285-
(
286-
("a", c_uint32, 9),
287-
("b", c_uint32),
288-
("c", c_uint32, 8),
289-
): 7,
290-
}
291-
292-
for fields, size in test_cases.items():
293-
with self.subTest(fields=fields):
294-
class X(Structure):
295-
_pack_ = 1
296-
_fields_ = list(fields)
297-
self.assertEqual(sizeof(X), size)
298-
299236
def test_anon_bitfields(self):
300237
# anonymous bit-fields gave a strange error message
301238
class X(Structure):

Modules/_ctypes/cfield.c

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,6 @@ PyCField_FromDesc(PyObject *desc, Py_ssize_t index,
7171
Py_DECREF(self);
7272
return NULL;
7373
}
74-
75-
#ifndef MS_WIN32
76-
/* if we have a packed bitfield, calculate the minimum number of bytes we
77-
need to fit it. otherwise use the specified size. */
78-
if (pack && bitsize) {
79-
size = (bitsize - 1) / 8 + 1;
80-
} else
81-
#endif
82-
size = dict->size;
83-
84-
proto = desc;
85-
8674
if (bitsize /* this is a bitfield request */
8775
&& *pfield_size /* we have a bitfield open */
8876
#ifdef MS_WIN32
@@ -99,26 +87,25 @@ PyCField_FromDesc(PyObject *desc, Py_ssize_t index,
9987
} else if (bitsize /* this is a bitfield request */
10088
&& *pfield_size /* we have a bitfield open */
10189
&& dict->size * 8 >= *pfield_size
102-
/* if this is a packed bitfield, always expand it.
103-
otherwise calculate if we need to expand it. */
104-
&& (((*pbitofs + bitsize) <= dict->size * 8) || pack)) {
90+
&& (*pbitofs + bitsize) <= dict->size * 8) {
10591
/* expand bit field */
10692
fieldtype = EXPAND_BITFIELD;
10793
#endif
10894
} else if (bitsize) {
10995
/* start new bitfield */
11096
fieldtype = NEW_BITFIELD;
11197
*pbitofs = 0;
112-
/* use our calculated size (size) instead of type size (dict->size),
113-
which can be different for packed bitfields */
114-
*pfield_size = size * 8;
98+
*pfield_size = dict->size * 8;
11599
} else {
116100
/* not a bit field */
117101
fieldtype = NO_BITFIELD;
118102
*pbitofs = 0;
119103
*pfield_size = 0;
120104
}
121105

106+
size = dict->size;
107+
proto = desc;
108+
122109
/* Field descriptors for 'c_char * n' are be scpecial cased to
123110
return a Python string instead of an Array object instance...
124111
*/
@@ -183,16 +170,10 @@ PyCField_FromDesc(PyObject *desc, Py_ssize_t index,
183170
break;
184171

185172
case EXPAND_BITFIELD:
186-
/* increase the size if it is a packed bitfield.
187-
EXPAND_BITFIELD should not be selected for non-packed fields if the
188-
current size isn't already enough. */
189-
if (pack)
190-
size = (*pbitofs + bitsize - 1) / 8 + 1;
191-
192-
*poffset += size - *pfield_size/8;
193-
*psize += size - *pfield_size/8;
173+
*poffset += dict->size - *pfield_size/8;
174+
*psize += dict->size - *pfield_size/8;
194175

195-
*pfield_size = size * 8;
176+
*pfield_size = dict->size * 8;
196177

197178
if (big_endian)
198179
self->size = (bitsize << 16) + *pfield_size - *pbitofs - bitsize;

0 commit comments

Comments
 (0)