Skip to content

Commit a17120a

Browse files
author
thomas.heller
committed
Issue 1872: Changed the struct module typecode from 't' to '?', for
compatibility with PEP3118. git-svn-id: http://svn.python.org/projects/python/trunk@61253 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent b2941e6 commit a17120a

8 files changed

Lines changed: 41 additions & 29 deletions

File tree

Doc/library/struct.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ Python values should be obvious given their types:
7777
+--------+-------------------------+--------------------+-------+
7878
| ``B`` | :ctype:`unsigned char` | integer | |
7979
+--------+-------------------------+--------------------+-------+
80-
| ``t`` | :ctype:`_Bool` | bool | \(1) |
80+
| ``?`` | :ctype:`_Bool` | bool | \(1) |
8181
+--------+-------------------------+--------------------+-------+
8282
| ``h`` | :ctype:`short` | integer | |
8383
+--------+-------------------------+--------------------+-------+
@@ -110,7 +110,7 @@ Python values should be obvious given their types:
110110
Notes:
111111

112112
(1)
113-
The ``'t'`` conversion code corresponds to the :ctype:`_Bool` type defined by
113+
The ``'?'`` conversion code corresponds to the :ctype:`_Bool` type defined by
114114
C99. If this type is not available, it is simulated using a :ctype:`char`. In
115115
standard mode, it is always represented by one byte.
116116

@@ -158,7 +158,7 @@ may be used. For example, the Alpha and Merced processors use 64-bit pointer
158158
values, meaning a Python long integer will be used to hold the pointer; other
159159
platforms use 32-bit pointers and will use a Python integer.
160160

161-
For the ``'t'`` format character, the return value is either :const:`True` or
161+
For the ``'?'`` format character, the return value is either :const:`True` or
162162
:const:`False`. When packing, the truth value of the argument object is used.
163163
Either 0 or 1 in the native or standard bool representation will be packed, and
164164
any non-zero value will be True when unpacking.

Lib/ctypes/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ class c_void_p(_SimpleCData):
240240
_check_size(c_void_p)
241241

242242
class c_bool(_SimpleCData):
243-
_type_ = "t"
243+
_type_ = "?"
244244

245245
# This cache maps types to pointers to them.
246246
_pointer_type_cache = {}

Lib/test/test_struct.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ def deprecated_err(func, *args):
8484
if sz * 3 != struct.calcsize('iii'):
8585
raise TestFailed, 'inconsistent sizes'
8686

87-
fmt = 'cbxxxxxxhhhhiillffdt'
88-
fmt3 = '3c3b18x12h6i6l6f3d3t'
87+
fmt = 'cbxxxxxxhhhhiillffd?'
88+
fmt3 = '3c3b18x12h6i6l6f3d3?'
8989
sz = struct.calcsize(fmt)
9090
sz3 = struct.calcsize(fmt3)
9191
if sz * 3 != sz3:
@@ -111,7 +111,7 @@ def deprecated_err(func, *args):
111111
t = True
112112

113113
for prefix in ('', '@', '<', '>', '=', '!'):
114-
for format in ('xcbhilfdt', 'xcBHILfdt'):
114+
for format in ('xcbhilfd?', 'xcBHILfd?'):
115115
format = prefix + format
116116
if verbose:
117117
print "trying:", format
@@ -160,11 +160,11 @@ def deprecated_err(func, *args):
160160
('f', -2.0, '\300\000\000\000', '\000\000\000\300', 0),
161161
('d', -2.0, '\300\000\000\000\000\000\000\000',
162162
'\000\000\000\000\000\000\000\300', 0),
163-
('t', 0, '\0', '\0', 0),
164-
('t', 3, '\1', '\1', 1),
165-
('t', True, '\1', '\1', 0),
166-
('t', [], '\0', '\0', 1),
167-
('t', (1,), '\1', '\1', 1),
163+
('?', 0, '\0', '\0', 0),
164+
('?', 3, '\1', '\1', 1),
165+
('?', True, '\1', '\1', 0),
166+
('?', [], '\0', '\0', 1),
167+
('?', (1,), '\1', '\1', 1),
168168
]
169169

170170
for fmt, arg, big, lil, asy in tests:
@@ -633,13 +633,13 @@ def test_bool():
633633
false = (), [], [], '', 0
634634
true = [1], 'test', 5, -1, 0xffffffffL+1, 0xffffffff/2
635635

636-
falseFormat = prefix + 't' * len(false)
636+
falseFormat = prefix + '?' * len(false)
637637
if verbose:
638638
print 'trying bool pack/unpack on', false, 'using format', falseFormat
639639
packedFalse = struct.pack(falseFormat, *false)
640640
unpackedFalse = struct.unpack(falseFormat, packedFalse)
641641

642-
trueFormat = prefix + 't' * len(true)
642+
trueFormat = prefix + '?' * len(true)
643643
if verbose:
644644
print 'trying bool pack/unpack on', true, 'using format', trueFormat
645645
packedTrue = struct.pack(trueFormat, *true)
@@ -658,10 +658,10 @@ def test_bool():
658658
raise TestFailed('%r did not unpack as false' % t)
659659

660660
if prefix and verbose:
661-
print 'trying size of bool with format %r' % (prefix+'t')
662-
packed = struct.pack(prefix+'t', 1)
661+
print 'trying size of bool with format %r' % (prefix+'?')
662+
packed = struct.pack(prefix+'?', 1)
663663

664-
if len(packed) != struct.calcsize(prefix+'t'):
664+
if len(packed) != struct.calcsize(prefix+'?'):
665665
raise TestFailed('packed length is not equal to calculated size')
666666

667667
if len(packed) != 1 and prefix:
@@ -670,7 +670,7 @@ def test_bool():
670670
print 'size of bool in native format is %i' % (len(packed))
671671

672672
for c in '\x01\x7f\xff\x0f\xf0':
673-
if struct.unpack('>t', c)[0] is not True:
673+
if struct.unpack('>?', c)[0] is not True:
674674
raise TestFailed('%c did not unpack as True' % c)
675675

676676
test_bool()

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ Core and builtins
1818
Library
1919
-------
2020

21+
- Issue #1872: The struct module typecode for _Bool has been changed
22+
from 't' to '?'.
23+
2124
- The bundled libffi copy is now in sync with the recently released
2225
libffi3.0.4 version, apart from some small changes to
2326
Modules/_ctypes/libffi/configure.ac.

Modules/_ctypes/_ctypes.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1242,7 +1242,7 @@ _type_ attribute.
12421242
12431243
*/
12441244

1245-
static char *SIMPLE_TYPE_CHARS = "cbBhHiIlLdfuzZqQPXOvtg";
1245+
static char *SIMPLE_TYPE_CHARS = "cbBhHiIlLdfuzZqQPXOv?g";
12461246

12471247
static PyObject *
12481248
c_wchar_p_from_param(PyObject *type, PyObject *value)

Modules/_ctypes/_ctypes_test.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@
2525

2626
/* some functions handy for testing */
2727

28+
EXPORT(void)testfunc_array(int values[4])
29+
{
30+
printf("testfunc_array %d %d %d %d\n",
31+
values[0],
32+
values[1],
33+
values[2],
34+
values[3]);
35+
}
36+
2837
EXPORT(long double)testfunc_Ddd(double a, double b)
2938
{
3039
long double result = (long double)(a * b);

Modules/_ctypes/cfield.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,7 @@ vBOOL_get(void *ptr, Py_ssize_t size)
726726
#endif
727727

728728
static PyObject *
729-
t_set(void *ptr, PyObject *value, Py_ssize_t size)
729+
bool_set(void *ptr, PyObject *value, Py_ssize_t size)
730730
{
731731
switch (PyObject_IsTrue(value)) {
732732
case -1:
@@ -741,7 +741,7 @@ t_set(void *ptr, PyObject *value, Py_ssize_t size)
741741
}
742742

743743
static PyObject *
744-
t_get(void *ptr, Py_ssize_t size)
744+
bool_get(void *ptr, Py_ssize_t size)
745745
{
746746
return PyBool_FromLong((long)*(BOOL_TYPE *)ptr);
747747
}
@@ -1645,15 +1645,15 @@ static struct fielddesc formattable[] = {
16451645
{ 'v', vBOOL_set, vBOOL_get, &ffi_type_sshort},
16461646
#endif
16471647
#if SIZEOF__BOOL == 1
1648-
{ 't', t_set, t_get, &ffi_type_uchar}, /* Also fallback for no native _Bool support */
1648+
{ '?', bool_set, bool_get, &ffi_type_uchar}, /* Also fallback for no native _Bool support */
16491649
#elif SIZEOF__BOOL == SIZEOF_SHORT
1650-
{ 't', t_set, t_get, &ffi_type_ushort},
1650+
{ '?', bool_set, bool_get, &ffi_type_ushort},
16511651
#elif SIZEOF__BOOL == SIZEOF_INT
1652-
{ 't', t_set, t_get, &ffi_type_uint, I_set_sw, I_get_sw},
1652+
{ '?', bool_set, bool_get, &ffi_type_uint, I_set_sw, I_get_sw},
16531653
#elif SIZEOF__BOOL == SIZEOF_LONG
1654-
{ 't', t_set, t_get, &ffi_type_ulong, L_set_sw, L_get_sw},
1654+
{ '?', bool_set, bool_get, &ffi_type_ulong, L_set_sw, L_get_sw},
16551655
#elif SIZEOF__BOOL == SIZEOF_LONG_LONG
1656-
{ 't', t_set, t_get, &ffi_type_ulong, Q_set_sw, Q_get_sw},
1656+
{ '?', bool_set, bool_get, &ffi_type_ulong, Q_set_sw, Q_get_sw},
16571657
#endif /* SIZEOF__BOOL */
16581658
{ 'O', O_set, O_get, &ffi_type_pointer},
16591659
{ 0, NULL, NULL, NULL},

Modules/_struct.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,7 @@ static formatdef native_table[] = {
799799
{'q', sizeof(PY_LONG_LONG), LONG_LONG_ALIGN, nu_longlong, np_longlong},
800800
{'Q', sizeof(PY_LONG_LONG), LONG_LONG_ALIGN, nu_ulonglong,np_ulonglong},
801801
#endif
802-
{'t', sizeof(BOOL_TYPE), BOOL_ALIGN, nu_bool, np_bool},
802+
{'?', sizeof(BOOL_TYPE), BOOL_ALIGN, nu_bool, np_bool},
803803
{'f', sizeof(float), FLOAT_ALIGN, nu_float, np_float},
804804
{'d', sizeof(double), DOUBLE_ALIGN, nu_double, np_double},
805805
{'P', sizeof(void *), VOID_P_ALIGN, nu_void_p, np_void_p},
@@ -1036,7 +1036,7 @@ static formatdef bigendian_table[] = {
10361036
{'L', 4, 0, bu_uint, bp_uint},
10371037
{'q', 8, 0, bu_longlong, bp_longlong},
10381038
{'Q', 8, 0, bu_ulonglong, bp_ulonglong},
1039-
{'t', 1, 0, bu_bool, bp_bool},
1039+
{'?', 1, 0, bu_bool, bp_bool},
10401040
{'f', 4, 0, bu_float, bp_float},
10411041
{'d', 8, 0, bu_double, bp_double},
10421042
{0}
@@ -1255,7 +1255,7 @@ static formatdef lilendian_table[] = {
12551255
{'L', 4, 0, lu_uint, lp_uint},
12561256
{'q', 8, 0, lu_longlong, lp_longlong},
12571257
{'Q', 8, 0, lu_ulonglong, lp_ulonglong},
1258-
{'t', 1, 0, bu_bool, bp_bool}, /* Std rep not endian dep,
1258+
{'?', 1, 0, bu_bool, bp_bool}, /* Std rep not endian dep,
12591259
but potentially different from native rep -- reuse bx_bool funcs. */
12601260
{'f', 4, 0, lu_float, lp_float},
12611261
{'d', 8, 0, lu_double, lp_double},

0 commit comments

Comments
 (0)