@@ -366,68 +366,158 @@ test_longlong_api(PyObject* self, PyObject *args)
366366static PyObject *
367367test_long_and_overflow (PyObject * self )
368368{
369- PyObject * num ;
369+ PyObject * num , * one , * temp ;
370370 long value ;
371371 int overflow ;
372372
373- /* a number larger than LONG_MAX even on 64-bit platforms */
373+ /* Test that overflow is set properly for a large value. */
374+ /* num is a number larger than LONG_MAX even on 64-bit platforms */
374375 num = PyLong_FromString ("FFFFFFFFFFFFFFFFFFFFFFFF" , NULL , 16 );
375376 if (num == NULL )
376377 return NULL ;
377-
378- /* Test that overflow is set properly for a large value. */
379378 overflow = 1234 ;
380379 value = PyLong_AsLongAndOverflow (num , & overflow );
380+ Py_DECREF (num );
381+ if (value == -1 && PyErr_Occurred ())
382+ return NULL ;
383+ if (value != -1 )
384+ return raiseTestError ("test_long_and_overflow" ,
385+ "return value was not set to -1" );
381386 if (overflow != 1 )
382387 return raiseTestError ("test_long_and_overflow" ,
383388 "overflow was not set to 1" );
384389
390+ /* Same again, with num = LONG_MAX + 1 */
391+ num = PyLong_FromLong (LONG_MAX );
392+ if (num == NULL )
393+ return NULL ;
394+ one = PyLong_FromLong (1L );
395+ if (one == NULL ) {
396+ Py_DECREF (num );
397+ return NULL ;
398+ }
399+ temp = PyNumber_Add (num , one );
400+ Py_DECREF (one );
401+ Py_DECREF (num );
402+ num = temp ;
403+ if (num == NULL )
404+ return NULL ;
385405 overflow = 0 ;
386406 value = PyLong_AsLongAndOverflow (num , & overflow );
407+ Py_DECREF (num );
408+ if (value == -1 && PyErr_Occurred ())
409+ return NULL ;
410+ if (value != -1 )
411+ return raiseTestError ("test_long_and_overflow" ,
412+ "return value was not set to -1" );
387413 if (overflow != 1 )
388414 return raiseTestError ("test_long_and_overflow" ,
389- "overflow was not set to 0" );
390-
391- Py_DECREF (num );
415+ "overflow was not set to 1" );
392416
393- /* a number smaller than LONG_MIN even on 64-bit platforms */
417+ /* Test that overflow is set properly for a large negative value. */
418+ /* num is a number smaller than LONG_MIN even on 64-bit platforms */
394419 num = PyLong_FromString ("-FFFFFFFFFFFFFFFFFFFFFFFF" , NULL , 16 );
395420 if (num == NULL )
396421 return NULL ;
397-
398- /* Test that overflow is set properly for a large negative value. */
399422 overflow = 1234 ;
400423 value = PyLong_AsLongAndOverflow (num , & overflow );
424+ Py_DECREF (num );
425+ if (value == -1 && PyErr_Occurred ())
426+ return NULL ;
427+ if (value != -1 )
428+ return raiseTestError ("test_long_and_overflow" ,
429+ "return value was not set to -1" );
401430 if (overflow != -1 )
402431 return raiseTestError ("test_long_and_overflow" ,
403432 "overflow was not set to -1" );
404433
434+ /* Same again, with num = LONG_MIN - 1 */
435+ num = PyLong_FromLong (LONG_MIN );
436+ if (num == NULL )
437+ return NULL ;
438+ one = PyLong_FromLong (1L );
439+ if (one == NULL ) {
440+ Py_DECREF (num );
441+ return NULL ;
442+ }
443+ temp = PyNumber_Subtract (num , one );
444+ Py_DECREF (one );
445+ Py_DECREF (num );
446+ num = temp ;
447+ if (num == NULL )
448+ return NULL ;
405449 overflow = 0 ;
406450 value = PyLong_AsLongAndOverflow (num , & overflow );
451+ Py_DECREF (num );
452+ if (value == -1 && PyErr_Occurred ())
453+ return NULL ;
454+ if (value != -1 )
455+ return raiseTestError ("test_long_and_overflow" ,
456+ "return value was not set to -1" );
407457 if (overflow != -1 )
408458 return raiseTestError ("test_long_and_overflow" ,
409- "overflow was not set to 0" );
410-
411- Py_DECREF (num );
459+ "overflow was not set to -1" );
412460
461+ /* Test that overflow is cleared properly for small values. */
413462 num = PyLong_FromString ("FF" , NULL , 16 );
414463 if (num == NULL )
415464 return NULL ;
416-
417- /* Test that overflow is cleared properly for a small value. */
418465 overflow = 1234 ;
419466 value = PyLong_AsLongAndOverflow (num , & overflow );
467+ Py_DECREF (num );
468+ if (value == -1 && PyErr_Occurred ())
469+ return NULL ;
470+ if (value != 0xFF )
471+ return raiseTestError ("test_long_and_overflow" ,
472+ "expected return value 0xFF" );
420473 if (overflow != 0 )
421474 return raiseTestError ("test_long_and_overflow" ,
422475 "overflow was not cleared" );
423476
477+ num = PyLong_FromString ("-FF" , NULL , 16 );
478+ if (num == NULL )
479+ return NULL ;
424480 overflow = 0 ;
425481 value = PyLong_AsLongAndOverflow (num , & overflow );
482+ Py_DECREF (num );
483+ if (value == -1 && PyErr_Occurred ())
484+ return NULL ;
485+ if (value != -0xFF )
486+ return raiseTestError ("test_long_and_overflow" ,
487+ "expected return value 0xFF" );
426488 if (overflow != 0 )
427489 return raiseTestError ("test_long_and_overflow" ,
428490 "overflow was set incorrectly" );
429491
492+ num = PyLong_FromLong (LONG_MAX );
493+ if (num == NULL )
494+ return NULL ;
495+ overflow = 1234 ;
496+ value = PyLong_AsLongAndOverflow (num , & overflow );
430497 Py_DECREF (num );
498+ if (value == -1 && PyErr_Occurred ())
499+ return NULL ;
500+ if (value != LONG_MAX )
501+ return raiseTestError ("test_long_and_overflow" ,
502+ "expected return value LONG_MAX" );
503+ if (overflow != 0 )
504+ return raiseTestError ("test_long_and_overflow" ,
505+ "overflow was not cleared" );
506+
507+ num = PyLong_FromLong (LONG_MIN );
508+ if (num == NULL )
509+ return NULL ;
510+ overflow = 0 ;
511+ value = PyLong_AsLongAndOverflow (num , & overflow );
512+ Py_DECREF (num );
513+ if (value == -1 && PyErr_Occurred ())
514+ return NULL ;
515+ if (value != LONG_MIN )
516+ return raiseTestError ("test_long_and_overflow" ,
517+ "expected return value LONG_MIN" );
518+ if (overflow != 0 )
519+ return raiseTestError ("test_long_and_overflow" ,
520+ "overflow was not cleared" );
431521
432522 Py_INCREF (Py_None );
433523 return Py_None ;
@@ -1116,7 +1206,8 @@ static PyMethodDef TestMethods[] = {
11161206 {"test_dict_iteration" , (PyCFunction )test_dict_iteration ,METH_NOARGS },
11171207 {"test_lazy_hash_inheritance" , (PyCFunction )test_lazy_hash_inheritance ,METH_NOARGS },
11181208 {"test_long_api" , (PyCFunction )test_long_api , METH_NOARGS },
1119- {"test_long_and_overflow" , (PyCFunction )test_long_and_overflow , METH_NOARGS },
1209+ {"test_long_and_overflow" , (PyCFunction )test_long_and_overflow ,
1210+ METH_NOARGS },
11201211 {"test_long_numbits" , (PyCFunction )test_long_numbits , METH_NOARGS },
11211212 {"test_k_code" , (PyCFunction )test_k_code , METH_NOARGS },
11221213 {"test_empty_argparse" , (PyCFunction )test_empty_argparse ,METH_NOARGS },
0 commit comments