1818#define ERROR -1
1919
2020#define RETURN_IF_ERROR (X ) \
21- if ((X) == -1 ) { \
21+ if ((X) < 0 ) { \
2222 return ERROR; \
2323 }
2424
@@ -448,13 +448,17 @@ static PyObject *
448448dict_keys_inorder (PyObject * dict , Py_ssize_t offset )
449449{
450450 PyObject * tuple , * k , * v ;
451- Py_ssize_t i , pos = 0 , size = PyDict_GET_SIZE (dict );
451+ Py_ssize_t pos = 0 , size = PyDict_GET_SIZE (dict );
452452
453453 tuple = PyTuple_New (size );
454454 if (tuple == NULL )
455455 return NULL ;
456456 while (PyDict_Next (dict , & pos , & k , & v )) {
457- i = PyLong_AS_LONG (v );
457+ Py_ssize_t i = PyLong_AsSsize_t (v );
458+ if (i == -1 && PyErr_Occurred ()) {
459+ Py_DECREF (tuple );
460+ return NULL ;
461+ }
458462 assert ((i - offset ) < size );
459463 assert ((i - offset ) >= 0 );
460464 PyTuple_SET_ITEM (tuple , i - offset , Py_NewRef (k ));
@@ -473,21 +477,27 @@ compute_localsplus_info(_PyCompile_CodeUnitMetadata *umd, int nlocalsplus,
473477 PyObject * k , * v ;
474478 Py_ssize_t pos = 0 ;
475479 while (PyDict_Next (umd -> u_varnames , & pos , & k , & v )) {
476- int offset = (int )PyLong_AS_LONG (v );
480+ int offset = _PyLong_AsInt (v );
481+ if (offset == -1 && PyErr_Occurred ()) {
482+ return ERROR ;
483+ }
477484 assert (offset >= 0 );
478485 assert (offset < nlocalsplus );
486+
479487 // For now we do not distinguish arg kinds.
480488 _PyLocals_Kind kind = CO_FAST_LOCAL ;
481- if (PyDict_Contains (umd -> u_fasthidden , k )) {
489+ int has_key = PyDict_Contains (umd -> u_fasthidden , k );
490+ RETURN_IF_ERROR (has_key );
491+ if (has_key ) {
482492 kind |= CO_FAST_HIDDEN ;
483493 }
484- int has_cell = PyDict_Contains (umd -> u_cellvars , k );
485- if (has_cell < 0 ) {
486- return -1 ;
487- }
488- if (has_cell ) {
494+
495+ has_key = PyDict_Contains (umd -> u_cellvars , k );
496+ RETURN_IF_ERROR (has_key );
497+ if (has_key ) {
489498 kind |= CO_FAST_CELL ;
490499 }
500+
491501 _Py_set_localsplus_info (offset , k , kind , names , kinds );
492502 }
493503 int nlocals = (int )PyDict_GET_SIZE (umd -> u_varnames );
@@ -497,15 +507,17 @@ compute_localsplus_info(_PyCompile_CodeUnitMetadata *umd, int nlocalsplus,
497507 pos = 0 ;
498508 while (PyDict_Next (umd -> u_cellvars , & pos , & k , & v )) {
499509 int has_name = PyDict_Contains (umd -> u_varnames , k );
500- if (has_name < 0 ) {
501- return -1 ;
502- }
510+ RETURN_IF_ERROR (has_name );
503511 if (has_name ) {
504512 // Skip cells that are already covered by locals.
505513 numdropped += 1 ;
506514 continue ;
507515 }
508- int offset = (int )PyLong_AS_LONG (v );
516+
517+ int offset = _PyLong_AsInt (v );
518+ if (offset == -1 && PyErr_Occurred ()) {
519+ return ERROR ;
520+ }
509521 assert (offset >= 0 );
510522 offset += nlocals - numdropped ;
511523 assert (offset < nlocalsplus );
@@ -514,13 +526,16 @@ compute_localsplus_info(_PyCompile_CodeUnitMetadata *umd, int nlocalsplus,
514526
515527 pos = 0 ;
516528 while (PyDict_Next (umd -> u_freevars , & pos , & k , & v )) {
517- int offset = (int )PyLong_AS_LONG (v );
529+ int offset = _PyLong_AsInt (v );
530+ if (offset == -1 && PyErr_Occurred ()) {
531+ return ERROR ;
532+ }
518533 assert (offset >= 0 );
519534 offset += nlocals - numdropped ;
520535 assert (offset < nlocalsplus );
521536 _Py_set_localsplus_info (offset , k , CO_FAST_FREE , names , kinds );
522537 }
523- return 0 ;
538+ return SUCCESS ;
524539}
525540
526541static PyCodeObject *
@@ -566,7 +581,7 @@ makecode(_PyCompile_CodeUnitMetadata *umd, struct assembler *a, PyObject *const_
566581 goto error ;
567582 }
568583 if (compute_localsplus_info (umd , nlocalsplus ,
569- localsplusnames , localspluskinds ) < 0 ) {
584+ localsplusnames , localspluskinds ) == ERROR ) {
570585 goto error ;
571586 }
572587
0 commit comments