18
18
#define ERROR -1
19
19
20
20
#define RETURN_IF_ERROR (X ) \
21
- if ((X) == -1 ) { \
21
+ if ((X) < 0 ) { \
22
22
return ERROR; \
23
23
}
24
24
@@ -448,13 +448,17 @@ static PyObject *
448
448
dict_keys_inorder (PyObject * dict , Py_ssize_t offset )
449
449
{
450
450
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 );
452
452
453
453
tuple = PyTuple_New (size );
454
454
if (tuple == NULL )
455
455
return NULL ;
456
456
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
+ }
458
462
assert ((i - offset ) < size );
459
463
assert ((i - offset ) >= 0 );
460
464
PyTuple_SET_ITEM (tuple , i - offset , Py_NewRef (k ));
@@ -466,24 +470,34 @@ dict_keys_inorder(PyObject *dict, Py_ssize_t offset)
466
470
extern void _Py_set_localsplus_info (int , PyObject * , unsigned char ,
467
471
PyObject * , PyObject * );
468
472
469
- static void
473
+ static int
470
474
compute_localsplus_info (_PyCompile_CodeUnitMetadata * umd , int nlocalsplus ,
471
475
PyObject * names , PyObject * kinds )
472
476
{
473
477
PyObject * k , * v ;
474
478
Py_ssize_t pos = 0 ;
475
479
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
+ }
477
484
assert (offset >= 0 );
478
485
assert (offset < nlocalsplus );
486
+
479
487
// For now we do not distinguish arg kinds.
480
488
_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 ) {
482
492
kind |= CO_FAST_HIDDEN ;
483
493
}
484
- if (PyDict_GetItem (umd -> u_cellvars , k ) != NULL ) {
494
+
495
+ has_key = PyDict_Contains (umd -> u_cellvars , k );
496
+ RETURN_IF_ERROR (has_key );
497
+ if (has_key ) {
485
498
kind |= CO_FAST_CELL ;
486
499
}
500
+
487
501
_Py_set_localsplus_info (offset , k , kind , names , kinds );
488
502
}
489
503
int nlocals = (int )PyDict_GET_SIZE (umd -> u_varnames );
@@ -492,12 +506,18 @@ compute_localsplus_info(_PyCompile_CodeUnitMetadata *umd, int nlocalsplus,
492
506
int numdropped = 0 ;
493
507
pos = 0 ;
494
508
while (PyDict_Next (umd -> u_cellvars , & pos , & k , & v )) {
495
- if (PyDict_GetItem (umd -> u_varnames , k ) != NULL ) {
509
+ int has_name = PyDict_Contains (umd -> u_varnames , k );
510
+ RETURN_IF_ERROR (has_name );
511
+ if (has_name ) {
496
512
// Skip cells that are already covered by locals.
497
513
numdropped += 1 ;
498
514
continue ;
499
515
}
500
- int offset = (int )PyLong_AS_LONG (v );
516
+
517
+ int offset = _PyLong_AsInt (v );
518
+ if (offset == -1 && PyErr_Occurred ()) {
519
+ return ERROR ;
520
+ }
501
521
assert (offset >= 0 );
502
522
offset += nlocals - numdropped ;
503
523
assert (offset < nlocalsplus );
@@ -506,12 +526,16 @@ compute_localsplus_info(_PyCompile_CodeUnitMetadata *umd, int nlocalsplus,
506
526
507
527
pos = 0 ;
508
528
while (PyDict_Next (umd -> u_freevars , & pos , & k , & v )) {
509
- int offset = (int )PyLong_AS_LONG (v );
529
+ int offset = _PyLong_AsInt (v );
530
+ if (offset == -1 && PyErr_Occurred ()) {
531
+ return ERROR ;
532
+ }
510
533
assert (offset >= 0 );
511
534
offset += nlocals - numdropped ;
512
535
assert (offset < nlocalsplus );
513
536
_Py_set_localsplus_info (offset , k , CO_FAST_FREE , names , kinds );
514
537
}
538
+ return SUCCESS ;
515
539
}
516
540
517
541
static PyCodeObject *
@@ -556,7 +580,10 @@ makecode(_PyCompile_CodeUnitMetadata *umd, struct assembler *a, PyObject *const_
556
580
if (localspluskinds == NULL ) {
557
581
goto error ;
558
582
}
559
- compute_localsplus_info (umd , nlocalsplus , localsplusnames , localspluskinds );
583
+ if (compute_localsplus_info (umd , nlocalsplus ,
584
+ localsplusnames , localspluskinds ) == ERROR ) {
585
+ goto error ;
586
+ }
560
587
561
588
struct _PyCodeConstructor con = {
562
589
.filename = filename ,
0 commit comments