@@ -331,35 +331,51 @@ II_getitem(arrayobject *ap, Py_ssize_t i)
331
331
(unsigned long ) ((unsigned int * )ap -> ob_item )[i ]);
332
332
}
333
333
334
+ static PyObject *
335
+ get_int_unless_float (PyObject * v )
336
+ {
337
+ if (PyFloat_Check (v )) {
338
+ PyErr_SetString (PyExc_TypeError ,
339
+ "array item must be integer" );
340
+ return NULL ;
341
+ }
342
+ return (PyObject * )_PyLong_FromNbInt (v );
343
+ }
344
+
334
345
static int
335
346
II_setitem (arrayobject * ap , Py_ssize_t i , PyObject * v )
336
347
{
337
348
unsigned long x ;
338
- if (PyLong_Check (v )) {
339
- x = PyLong_AsUnsignedLong (v );
340
- if (x == (unsigned long ) -1 && PyErr_Occurred ())
349
+ int do_decref = 0 ; /* if nb_int was called */
350
+
351
+ if (!PyLong_Check (v )) {
352
+ v = get_int_unless_float (v );
353
+ if (NULL == v ) {
341
354
return -1 ;
355
+ }
356
+ do_decref = 1 ;
342
357
}
343
- else {
344
- long y ;
345
- if (!PyArg_Parse (v , "l;array item must be integer" , & y ))
346
- return -1 ;
347
- if (y < 0 ) {
348
- PyErr_SetString (PyExc_OverflowError ,
349
- "unsigned int is less than minimum" );
350
- return -1 ;
358
+ x = PyLong_AsUnsignedLong (v );
359
+ if (x == (unsigned long )-1 && PyErr_Occurred ()) {
360
+ if (do_decref ) {
361
+ Py_DECREF (v );
351
362
}
352
- x = (unsigned long )y ;
353
-
363
+ return -1 ;
354
364
}
355
365
if (x > UINT_MAX ) {
356
366
PyErr_SetString (PyExc_OverflowError ,
357
- "unsigned int is greater than maximum" );
367
+ "unsigned int is greater than maximum" );
368
+ if (do_decref ) {
369
+ Py_DECREF (v );
370
+ }
358
371
return -1 ;
359
372
}
360
-
361
373
if (i >= 0 )
362
374
((unsigned int * )ap -> ob_item )[i ] = (unsigned int )x ;
375
+
376
+ if (do_decref ) {
377
+ Py_DECREF (v );
378
+ }
363
379
return 0 ;
364
380
}
365
381
@@ -390,31 +406,28 @@ static int
390
406
LL_setitem (arrayobject * ap , Py_ssize_t i , PyObject * v )
391
407
{
392
408
unsigned long x ;
393
- if (PyLong_Check (v )) {
394
- x = PyLong_AsUnsignedLong (v );
395
- if (x == (unsigned long ) -1 && PyErr_Occurred ())
396
- return -1 ;
397
- }
398
- else {
399
- long y ;
400
- if (!PyArg_Parse (v , "l;array item must be integer" , & y ))
401
- return -1 ;
402
- if (y < 0 ) {
403
- PyErr_SetString (PyExc_OverflowError ,
404
- "unsigned long is less than minimum" );
409
+ int do_decref = 0 ; /* if nb_int was called */
410
+
411
+ if (!PyLong_Check (v )) {
412
+ v = get_int_unless_float (v );
413
+ if (NULL == v ) {
405
414
return -1 ;
406
415
}
407
- x = (unsigned long )y ;
408
-
416
+ do_decref = 1 ;
409
417
}
410
- if (x > ULONG_MAX ) {
411
- PyErr_SetString (PyExc_OverflowError ,
412
- "unsigned long is greater than maximum" );
418
+ x = PyLong_AsUnsignedLong (v );
419
+ if (x == (unsigned long )-1 && PyErr_Occurred ()) {
420
+ if (do_decref ) {
421
+ Py_DECREF (v );
422
+ }
413
423
return -1 ;
414
424
}
415
-
416
425
if (i >= 0 )
417
426
((unsigned long * )ap -> ob_item )[i ] = x ;
427
+
428
+ if (do_decref ) {
429
+ Py_DECREF (v );
430
+ }
418
431
return 0 ;
419
432
}
420
433
@@ -448,25 +461,28 @@ static int
448
461
QQ_setitem (arrayobject * ap , Py_ssize_t i , PyObject * v )
449
462
{
450
463
unsigned PY_LONG_LONG x ;
451
- if (PyLong_Check (v )) {
452
- x = PyLong_AsUnsignedLongLong (v );
453
- if (x == (unsigned PY_LONG_LONG ) -1 && PyErr_Occurred ())
464
+ int do_decref = 0 ; /* if nb_int was called */
465
+
466
+ if (!PyLong_Check (v )) {
467
+ v = get_int_unless_float (v );
468
+ if (NULL == v ) {
454
469
return -1 ;
470
+ }
471
+ do_decref = 1 ;
455
472
}
456
- else {
457
- PY_LONG_LONG y ;
458
- if (!PyArg_Parse (v , "L;array item must be integer" , & y ))
459
- return -1 ;
460
- if (y < 0 ) {
461
- PyErr_SetString (PyExc_OverflowError ,
462
- "unsigned long long is less than minimum" );
463
- return -1 ;
473
+ x = PyLong_AsUnsignedLongLong (v );
474
+ if (x == (unsigned PY_LONG_LONG )-1 && PyErr_Occurred ()) {
475
+ if (do_decref ) {
476
+ Py_DECREF (v );
464
477
}
465
- x = ( unsigned PY_LONG_LONG ) y ;
478
+ return -1 ;
466
479
}
467
-
468
480
if (i >= 0 )
469
481
((unsigned PY_LONG_LONG * )ap -> ob_item )[i ] = x ;
482
+
483
+ if (do_decref ) {
484
+ Py_DECREF (v );
485
+ }
470
486
return 0 ;
471
487
}
472
488
#endif
0 commit comments