Skip to content

Commit a58b4e4

Browse files
committed
address review: use native types
1 parent b82ee36 commit a58b4e4

2 files changed

Lines changed: 30 additions & 25 deletions

File tree

Lib/test/test_ctypes/test_pep3118.py

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,15 @@ class Complete(Structure):
119119
#
120120

121121
# Platform-specific type codes
122-
s_bool = {1: '?', 2: 'H', 4: 'L', 8: 'Q'}[sizeof(c_bool)]
123-
s_short = {2: 'h', 4: 'l', 8: 'q'}[sizeof(c_short)]
124-
s_ushort = {2: 'H', 4: 'L', 8: 'Q'}[sizeof(c_ushort)]
125-
s_int = {2: 'h', 4: 'i', 8: 'q'}[sizeof(c_int)]
126-
s_uint = {2: 'H', 4: 'I', 8: 'Q'}[sizeof(c_uint)]
127-
s_long = {4: 'l', 8: 'q'}[sizeof(c_long)]
128-
s_ulong = {4: 'L', 8: 'Q'}[sizeof(c_ulong)]
129-
s_longlong = "q"
130-
s_ulonglong = "Q"
122+
s_bool = c_bool._type_
123+
s_short = c_short._type_
124+
s_ushort = c_ushort._type_
125+
s_int = c_int._type_
126+
s_uint = c_uint._type_
127+
s_long = c_long._type_
128+
s_ulong = c_ulong._type_
129+
s_longlong = c_longlong._type_
130+
s_ulonglong = c_ulonglong._type_
131131
s_float = "f"
132132
s_double = "d"
133133
s_longdouble = "g"
@@ -194,9 +194,9 @@ class Complete(Structure):
194194
(Point, "T{l:x:l:y:}".replace('l', s_long), (), Point),
195195
(PackedPoint, "T{l:x:l:y:}".replace('l', s_long), (), PackedPoint),
196196
(PointMidPad, "T{b:x:3xI:y:}".replace('I', s_uint), (), PointMidPad),
197-
(PackedPointMidPad, "T{b:x:xQ:y:}", (), PackedPointMidPad),
197+
(PackedPointMidPad, "T{b:x:xQ:y:}".replace('Q', s_ulonglong), (), PackedPointMidPad),
198198
(PointEndPad, "T{I:x:b:y:3x}".replace('I', s_uint), (), PointEndPad),
199-
(PackedPointEndPad, "T{Q:x:b:y:x}", (), PackedPointEndPad),
199+
(PackedPointEndPad, "T{Q:x:b:y:x}".replace('Q', s_ulonglong), (), PackedPointEndPad),
200200
(EmptyStruct, "T{}", (), EmptyStruct),
201201
# the pep doesn't support unions
202202
(aUnion, "B", (), aUnion),
@@ -229,21 +229,24 @@ class LEPoint(LittleEndianStructure):
229229
_fields_ = [("x", c_long), ("y", c_long)]
230230

231231

232+
s_long2 = {4: 'l', 8: 'q'}[sizeof(c_long)]
233+
234+
232235
# This table contains format strings as they really look, on both big
233236
# and little endian machines.
234237
if sys.byteorder == "little":
235238
endian_types = [
236-
(BEPoint, "T{>l:x:>l:y:}".replace('l', s_long), (), BEPoint),
237-
(LEPoint * 1, "T{l:x:l:y:}".replace('l', s_long), (1,), LEPoint),
238-
(POINTER(BEPoint), "&T{>l:x:>l:y:}".replace('l', s_long), (), POINTER(BEPoint)),
239-
(POINTER(LEPoint), "&T{l:x:l:y:}".replace('l', s_long), (), POINTER(LEPoint)),
239+
(BEPoint, "T{>l:x:>l:y:}".replace('l', s_long2), (), BEPoint),
240+
(LEPoint * 1, "T{l:x:l:y:}", (1,), LEPoint),
241+
(POINTER(BEPoint), "&T{>l:x:>l:y:}".replace('l', s_long2), (), POINTER(BEPoint)),
242+
(POINTER(LEPoint), "&T{l:x:l:y:}", (), POINTER(LEPoint)),
240243
]
241244
else:
242245
endian_types = [
243-
(BEPoint * 1, "T{l:x:l:y:}".replace('l', s_long), (1,), BEPoint),
244-
(LEPoint, "T{<l:x:<l:y:}".replace('l', s_long), (), LEPoint),
245-
(POINTER(BEPoint), "&T{l:x:l:y:}".replace('l', s_long), (), POINTER(BEPoint)),
246-
(POINTER(LEPoint), "&T{<l:x:<l:y:}".replace('l', s_long), (), POINTER(LEPoint)),
246+
(BEPoint * 1, "T{l:x:l:y:}", (1,), BEPoint),
247+
(LEPoint, "T{<l:x:<l:y:}".replace('l', s_long2), (), LEPoint),
248+
(POINTER(BEPoint), "&T{l:x:l:y:}", (), POINTER(BEPoint)),
249+
(POINTER(LEPoint), "&T{<l:x:<l:y:}".replace('l', s_long2), (), POINTER(LEPoint)),
247250
]
248251

249252

Modules/_ctypes/_ctypes.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ _PyDict_GetItemProxy(PyObject *dict, PyObject *key, PyObject **presult)
267267
later on.
268268
*/
269269
static char *
270-
_ctypes_alloc_format_string_for_type(const char *code)
270+
_ctypes_alloc_format_string_for_type(const char *code, int big_endian)
271271
{
272272
const char *pep_code = NULL;
273273

@@ -310,13 +310,14 @@ _ctypes_alloc_format_string_for_type(const char *code)
310310
break;
311311
}
312312

313-
char *result = PyMem_Malloc(1 + strlen(pep_code));
313+
char *result = PyMem_Malloc(1 + strlen(pep_code) + 1);
314314
if (result == NULL) {
315315
PyErr_NoMemory();
316316
return NULL;
317317
}
318318

319-
strcpy(result, pep_code);
319+
result[0] = big_endian ? '>' : '<';
320+
strcpy(result + 1, pep_code);
320321
return result;
321322
}
322323

@@ -2404,11 +2405,12 @@ PyCSimpleType_init(PyObject *self, PyObject *args, PyObject *kwds)
24042405
stginfo->size = fmt->pffi_type->size;
24052406
stginfo->setfunc = fmt->setfunc;
24062407
stginfo->getfunc = fmt->getfunc;
2407-
stginfo->format = _ctypes_alloc_format_string_for_type(proto_str);
2408+
stginfo->format = PyMem_Malloc(1 + strlen(proto_str));
24082409
if (stginfo->format == NULL) {
24092410
Py_DECREF(proto);
24102411
return -1;
24112412
}
2413+
strcpy(stginfo->format, proto_str);
24122414

24132415
stginfo->paramfunc = PyCSimpleType_paramfunc;
24142416
/*
@@ -2499,14 +2501,14 @@ PyCSimpleType_init(PyObject *self, PyObject *args, PyObject *kwds)
24992501
PyObject_SetAttrString(swapped, "__ctype_be__", self);
25002502
PyObject_SetAttrString(swapped, "__ctype_le__", swapped);
25012503
/* We are creating the type for the OTHER endian */
2502-
sw_info->format = _ctypes_alloc_format_string("<", stginfo->format);
2504+
sw_info->format = _ctypes_alloc_format_string_for_type(stginfo->format, 0);
25032505
#else
25042506
PyObject_SetAttrString(self, "__ctype_be__", swapped);
25052507
PyObject_SetAttrString(self, "__ctype_le__", self);
25062508
PyObject_SetAttrString(swapped, "__ctype_le__", self);
25072509
PyObject_SetAttrString(swapped, "__ctype_be__", swapped);
25082510
/* We are creating the type for the OTHER endian */
2509-
sw_info->format = _ctypes_alloc_format_string(">", stginfo->format);
2511+
sw_info->format = _ctypes_alloc_format_string_for_type(stginfo->format, 1);
25102512
#endif
25112513
Py_DECREF(swapped);
25122514
if (PyErr_Occurred()) {

0 commit comments

Comments
 (0)