Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b808e99

Browse files
committed
Raise a TypeError if a float is passed when an integer is specified.
Calling PyInt_AsLong() on a float truncates it which is almost never the desired behavior. This closes SF bug #660144.
1 parent 3422c99 commit b808e99

1 file changed

Lines changed: 24 additions & 6 deletions

File tree

Python/getargs.c

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,10 @@ convertsimple(PyObject *arg, char **p_format, va_list *p_va, char *msgbuf,
408408

409409
case 'b': { /* unsigned byte -- very short int */
410410
char *p = va_arg(*p_va, char *);
411-
long ival = PyInt_AsLong(arg);
411+
long ival;
412+
if (PyFloat_Check(arg))
413+
return converterr("integer", arg, msgbuf, bufsize);
414+
ival = PyInt_AsLong(arg);
412415
if (ival == -1 && PyErr_Occurred())
413416
return converterr("integer<b>", arg, msgbuf, bufsize);
414417
else if (ival < 0) {
@@ -429,7 +432,10 @@ convertsimple(PyObject *arg, char **p_format, va_list *p_va, char *msgbuf,
429432
case 'B': {/* byte sized bitfield - both signed and unsigned
430433
values allowed */
431434
char *p = va_arg(*p_va, char *);
432-
long ival = PyInt_AsLong(arg);
435+
long ival;
436+
if (PyFloat_Check(arg))
437+
return converterr("integer", arg, msgbuf, bufsize);
438+
ival = PyInt_AsLong(arg);
433439
if (ival == -1 && PyErr_Occurred())
434440
return converterr("integer<b>", arg, msgbuf, bufsize);
435441
else if (ival < SCHAR_MIN) {
@@ -449,7 +455,10 @@ convertsimple(PyObject *arg, char **p_format, va_list *p_va, char *msgbuf,
449455

450456
case 'h': {/* signed short int */
451457
short *p = va_arg(*p_va, short *);
452-
long ival = PyInt_AsLong(arg);
458+
long ival;
459+
if (PyFloat_Check(arg))
460+
return converterr("integer", arg, msgbuf, bufsize);
461+
ival = PyInt_AsLong(arg);
453462
if (ival == -1 && PyErr_Occurred())
454463
return converterr("integer<h>", arg, msgbuf, bufsize);
455464
else if (ival < SHRT_MIN) {
@@ -470,7 +479,10 @@ convertsimple(PyObject *arg, char **p_format, va_list *p_va, char *msgbuf,
470479
case 'H': { /* short int sized bitfield, both signed and
471480
unsigned allowed */
472481
unsigned short *p = va_arg(*p_va, unsigned short *);
473-
long ival = PyInt_AsLong(arg);
482+
long ival;
483+
if (PyFloat_Check(arg))
484+
return converterr("integer", arg, msgbuf, bufsize);
485+
ival = PyInt_AsLong(arg);
474486
if (ival == -1 && PyErr_Occurred())
475487
return converterr("integer<H>", arg, msgbuf, bufsize);
476488
else if (ival < SHRT_MIN) {
@@ -490,7 +502,10 @@ convertsimple(PyObject *arg, char **p_format, va_list *p_va, char *msgbuf,
490502

491503
case 'i': {/* signed int */
492504
int *p = va_arg(*p_va, int *);
493-
long ival = PyInt_AsLong(arg);
505+
long ival;
506+
if (PyFloat_Check(arg))
507+
return converterr("integer", arg, msgbuf, bufsize);
508+
ival = PyInt_AsLong(arg);
494509
if (ival == -1 && PyErr_Occurred())
495510
return converterr("integer<i>", arg, msgbuf, bufsize);
496511
else if (ival > INT_MAX) {
@@ -510,7 +525,10 @@ convertsimple(PyObject *arg, char **p_format, va_list *p_va, char *msgbuf,
510525

511526
case 'l': {/* long int */
512527
long *p = va_arg(*p_va, long *);
513-
long ival = PyInt_AsLong(arg);
528+
long ival;
529+
if (PyFloat_Check(arg))
530+
return converterr("integer", arg, msgbuf, bufsize);
531+
ival = PyInt_AsLong(arg);
514532
if (ival == -1 && PyErr_Occurred())
515533
return converterr("integer<l>", arg, msgbuf, bufsize);
516534
else

0 commit comments

Comments
 (0)