diff options
author | Thomas Heller <theller@ctypes.org> | 2003-04-17 18:55:45 (GMT) |
---|---|---|
committer | Thomas Heller <theller@ctypes.org> | 2003-04-17 18:55:45 (GMT) |
commit | a4ea603b055533e71920a088acb1c106e4895dbd (patch) | |
tree | c50efc24264738dee727367ed0e55bc3e6aedf84 /Python/getargs.c | |
parent | e13ddc9ec85287b17fd03454f836f495c1167de9 (diff) | |
download | cpython-a4ea603b055533e71920a088acb1c106e4895dbd.zip cpython-a4ea603b055533e71920a088acb1c106e4895dbd.tar.gz cpython-a4ea603b055533e71920a088acb1c106e4895dbd.tar.bz2 |
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.
Diffstat (limited to 'Python/getargs.c')
-rw-r--r-- | Python/getargs.c | 76 |
1 files changed, 49 insertions, 27 deletions
diff --git a/Python/getargs.c b/Python/getargs.c index 1f37e41..e9808d5 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<b>", arg, msgbuf, bufsize); - else if (ival < SCHAR_MIN) { - PyErr_SetString(PyExc_OverflowError, - "byte-sized integer bitfield is less than minimum"); return converterr("integer<B>", arg, msgbuf, bufsize); - } - else if (ival > (int)UCHAR_MAX) { - PyErr_SetString(PyExc_OverflowError, - "byte-sized integer bitfield is greater than maximum"); - return converterr("integer<B>", 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<h>", 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<h>", 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<h>", 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<H>", arg, msgbuf, bufsize); - else if (ival < SHRT_MIN) { - PyErr_SetString(PyExc_OverflowError, - "short integer bitfield is less than minimum"); - return converterr("integer<H>", arg, msgbuf, bufsize); - } - else if (ival > USHRT_MAX) { - PyErr_SetString(PyExc_OverflowError, - "short integer bitfield is greater than maximum"); - return converterr("integer<H>", 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<I>", 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<k>", 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<K>", arg, msgbuf, bufsize); + *p = ival; + break; + } #endif case 'f': {/* float */ |