From a4ea603b055533e71920a088acb1c106e4895dbd Mon Sep 17 00:00:00 2001 From: Thomas Heller Date: Thu, 17 Apr 2003 18:55:45 +0000 Subject: SF # 595026: support for masks in getargs.c. New functions: unsigned long PyInt_AsUnsignedLongMask(PyObject *); unsigned PY_LONG_LONG) PyInt_AsUnsignedLongLongMask(PyObject *); unsigned long PyLong_AsUnsignedLongMask(PyObject *); unsigned PY_LONG_LONG) PyLong_AsUnsignedLongLongMask(PyObject *); New and changed format codes: b unsigned char 0..UCHAR_MAX B unsigned char none ** h unsigned short 0..USHRT_MAX H unsigned short none ** i int INT_MIN..INT_MAX I * unsigned int 0..UINT_MAX l long LONG_MIN..LONG_MAX k * unsigned long none L long long LLONG_MIN..LLONG_MAX K * unsigned long long none Notes: * New format codes. ** Changed from previous "range-and-a-half" to "none"; the range-and-a-half checking wasn't particularly useful. New test test_getargs2.py, to verify all this. --- Python/getargs.c | 76 ++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 49 insertions(+), 27 deletions(-) (limited to 'Python/getargs.c') diff --git a/Python/getargs.c b/Python/getargs.c index 1f37e41d83..e9808d541a 100644 --- a/Python/getargs.c +++ b/Python/getargs.c @@ -448,25 +448,15 @@ convertsimple(PyObject *arg, char **p_format, va_list *p_va, char *msgbuf, long ival; if (float_argument_error(arg)) return NULL; - ival = PyInt_AsLong(arg); + ival = PyInt_AsUnsignedLongMask(arg); if (ival == -1 && PyErr_Occurred()) - return converterr("integer", arg, msgbuf, bufsize); - else if (ival < SCHAR_MIN) { - PyErr_SetString(PyExc_OverflowError, - "byte-sized integer bitfield is less than minimum"); return converterr("integer", arg, msgbuf, bufsize); - } - else if (ival > (int)UCHAR_MAX) { - PyErr_SetString(PyExc_OverflowError, - "byte-sized integer bitfield is greater than maximum"); - return converterr("integer", arg, msgbuf, bufsize); - } else *p = (unsigned char) ival; break; } - case 'h': {/* signed short int */ + case 'h': {/* unsigned short int */ short *p = va_arg(*p_va, short *); long ival; if (float_argument_error(arg)) @@ -474,14 +464,14 @@ convertsimple(PyObject *arg, char **p_format, va_list *p_va, char *msgbuf, ival = PyInt_AsLong(arg); if (ival == -1 && PyErr_Occurred()) return converterr("integer", arg, msgbuf, bufsize); - else if (ival < SHRT_MIN) { + else if (ival < 0) { PyErr_SetString(PyExc_OverflowError, - "signed short integer is less than minimum"); + "unsigned short integer is less than minimum"); return converterr("integer", arg, msgbuf, bufsize); } - else if (ival > SHRT_MAX) { + else if (ival > USHRT_MAX) { PyErr_SetString(PyExc_OverflowError, - "signed short integer is greater than maximum"); + "unsigned short integer is greater than maximum"); return converterr("integer", arg, msgbuf, bufsize); } else @@ -495,19 +485,9 @@ convertsimple(PyObject *arg, char **p_format, va_list *p_va, char *msgbuf, long ival; if (float_argument_error(arg)) return NULL; - ival = PyInt_AsLong(arg); + ival = PyInt_AsUnsignedLongMask(arg); if (ival == -1 && PyErr_Occurred()) return converterr("integer", arg, msgbuf, bufsize); - else if (ival < SHRT_MIN) { - PyErr_SetString(PyExc_OverflowError, - "short integer bitfield is less than minimum"); - return converterr("integer", arg, msgbuf, bufsize); - } - else if (ival > USHRT_MAX) { - PyErr_SetString(PyExc_OverflowError, - "short integer bitfield is greater than maximum"); - return converterr("integer", arg, msgbuf, bufsize); - } else *p = (unsigned short) ival; break; @@ -536,6 +516,20 @@ convertsimple(PyObject *arg, char **p_format, va_list *p_va, char *msgbuf, break; } + case 'I': { /* int sized bitfield, both signed and + unsigned allowed */ + unsigned int *p = va_arg(*p_va, unsigned int *); + unsigned int ival; + if (float_argument_error(arg)) + return NULL; + ival = PyInt_AsUnsignedLongMask(arg); + if (ival == -1 && PyErr_Occurred()) + return converterr("integer", arg, msgbuf, bufsize); + else + *p = ival; + break; + } + case 'l': {/* long int */ long *p = va_arg(*p_va, long *); long ival; @@ -548,6 +542,19 @@ convertsimple(PyObject *arg, char **p_format, va_list *p_va, char *msgbuf, *p = ival; break; } + + case 'k': { /* long sized bitfield */ + unsigned long *p = va_arg(*p_va, unsigned long *); + unsigned long ival; + if (PyInt_Check(arg)) + ival = PyInt_AsUnsignedLongMask(arg); + else if (PyLong_Check(arg)) + ival = PyLong_AsUnsignedLongMask(arg); + else + return converterr("integer", arg, msgbuf, bufsize); + *p = ival; + break; + } #ifdef HAVE_LONG_LONG case 'L': {/* PY_LONG_LONG */ @@ -560,6 +567,21 @@ convertsimple(PyObject *arg, char **p_format, va_list *p_va, char *msgbuf, } break; } + + case 'K': { /* long long sized bitfield */ + unsigned PY_LONG_LONG *p = va_arg(*p_va, unsigned PY_LONG_LONG *); + unsigned PY_LONG_LONG ival; + if (float_argument_error(arg)) + return NULL; + if (PyInt_Check(arg)) + ival = PyInt_AsUnsignedLongMask(arg); + else if (PyLong_Check(arg)) + ival = PyLong_AsUnsignedLongLongMask(arg); + else + return converterr("integer", arg, msgbuf, bufsize); + *p = ival; + break; + } #endif case 'f': {/* float */ -- cgit v1.2.1