Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Lib/ctypes/test/test_bitfields.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ def test_ints(self):
self.assertEqual((name, i, getattr(b, name)), (name, i, func(byref(b), name)))

def test_shorts(self):
b = BITS()
if func(byref(b), "M") == 999:
self.skipTest("Compiler does not support signed short bitfields")
for i in range(256):
for name in "MNOPQRS":
b = BITS()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix Lib/ctypes/test/test_bitfields.py following a suggestion by martin.panter
22 changes: 19 additions & 3 deletions Modules/_ctypes/_ctypes_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -396,8 +396,20 @@ EXPORT(PY_LONG_LONG) last_tf_arg_s;
EXPORT(unsigned PY_LONG_LONG) last_tf_arg_u;

struct BITS {
int A: 1, B:2, C:3, D:4, E: 5, F: 6, G: 7, H: 8, I: 9;
short M: 1, N: 2, O: 3, P: 4, Q: 5, R: 6, S: 7;
signed int A: 1, B:2, C:3, D:4, E: 5, F: 6, G: 7, H: 8, I: 9;
/*
* The test case needs "signed short" bitfields, but the
* IBM XLC compiler does not support this
*/
#ifndef _AIX
#define SIGNED_SHORT_BITFIELDS
short M: 1, N: 2, O: 3, P: 4, Q: 5, R: 6, S: 7;
#else
#ifdef __GCC_HAVE_SYNC_COMPARE_AND_SWAP_1 /* Something to distinguish GCC and XLC */
#define SIGNED_SHORT_BITFIELDS
short M: 1, N: 2, O: 3, P: 4, Q: 5, R: 6, S: 7;
#endif
#endif
};

DL_EXPORT(void) set_bitfields(struct BITS *bits, char name, int value)
Expand All @@ -413,13 +425,15 @@ DL_EXPORT(void) set_bitfields(struct BITS *bits, char name, int value)
case 'H': bits->H = value; break;
case 'I': bits->I = value; break;

#ifdef SIGNED_SHORT_BITFIELDS
case 'M': bits->M = value; break;
case 'N': bits->N = value; break;
case 'O': bits->O = value; break;
case 'P': bits->P = value; break;
case 'Q': bits->Q = value; break;
case 'R': bits->R = value; break;
case 'S': bits->S = value; break;
#endif
}
}

Expand All @@ -436,15 +450,17 @@ DL_EXPORT(int) unpack_bitfields(struct BITS *bits, char name)
case 'H': return bits->H;
case 'I': return bits->I;

#ifdef SIGNED_SHORT_BITFIELDS
case 'M': return bits->M;
case 'N': return bits->N;
case 'O': return bits->O;
case 'P': return bits->P;
case 'Q': return bits->Q;
case 'R': return bits->R;
case 'S': return bits->S;
#endif
}
return 0;
return 999;
}

static PyMethodDef module_methods[] = {
Expand Down