From cc22fbe3dbf3374de0827fc52f6765de0af2a7d8 Mon Sep 17 00:00:00 2001 From: Jack Jansen Date: Sat, 5 Aug 2000 21:29:58 +0000 Subject: [PATCH] Changed H specifier to mean "bitfield", i.e. any value from -32768..65535 is acceptable. Added B specifier (with values from -128..255). No L added (which would have completed the set) because l already accepts any value (and the letter L is taken for quadwords). --- Python/getargs.c | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/Python/getargs.c b/Python/getargs.c index ceaf6cb8ac4..359e3bdf268 100644 --- a/Python/getargs.c +++ b/Python/getargs.c @@ -409,6 +409,27 @@ convertsimple1(PyObject *arg, char **p_format, va_list *p_va) break; } + case 'B': /* byte sized bitfield - both signed and unsigned values allowed */ + { + char *p = va_arg(*p_va, char *); + long ival = PyInt_AsLong(arg); + if (ival == -1 && PyErr_Occurred()) + return "integer"; + else if (ival < SCHAR_MIN) { + PyErr_SetString(PyExc_OverflowError, + "byte-sized integer bitfield is less than minimum"); + return "integer"; + } + else if (ival > UCHAR_MAX) { + PyErr_SetString(PyExc_OverflowError, + "byte-sized integer bitfield is greater than maximum"); + return "integer"; + } + else + *p = (unsigned char) ival; + break; + } + case 'h': /* signed short int */ { short *p = va_arg(*p_va, short *); @@ -430,20 +451,20 @@ convertsimple1(PyObject *arg, char **p_format, va_list *p_va) break; } - case 'H': /* unsigned short int */ + case 'H': /* short int sized bitfield, both signed and unsigned allowed */ { unsigned short *p = va_arg(*p_va, unsigned short *); long ival = PyInt_AsLong(arg); if (ival == -1 && PyErr_Occurred()) return "integer"; - else if (ival < 0) { + else if (ival < SHRT_MIN) { PyErr_SetString(PyExc_OverflowError, - "unsigned short integer is less than minimum"); + "short integer bitfield is less than minimum"); return "integer"; } else if (ival > USHRT_MAX) { PyErr_SetString(PyExc_OverflowError, - "unsigned short integer is greater than maximum"); + "short integer bitfield is greater than maximum"); return "integer"; } else @@ -1133,6 +1154,7 @@ skipitem(char **p_format, va_list *p_va) switch (c) { case 'b': /* byte -- very short int */ + case 'B': /* byte as bitfield */ { (void) va_arg(*p_va, char *); break; @@ -1144,7 +1166,7 @@ skipitem(char **p_format, va_list *p_va) break; } - case 'H': /* unsigned short int */ + case 'H': /* short int as bitfield */ { (void) va_arg(*p_va, unsigned short *); break;