@@ -36,6 +36,9 @@ redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
3636#ifdef HAVE_LIMITS_H
3737#include <limits.h>
3838#endif
39+ #ifndef INT_MAX
40+ #define INT_MAX 2147483647
41+ #endif
3942
4043/* Three symbols from graminit.h are also defined in Python.h, with
4144 Py_ prefixes to their names. Python.h can't include graminit.h
@@ -572,11 +575,17 @@ com_set_lineno(struct compiling *c, int lineno)
572575static void
573576com_addoparg (struct compiling * c , int op , int arg )
574577{
578+ int extended_arg = arg >> 16 ;
575579 if (op == SET_LINENO ) {
576580 com_set_lineno (c , arg );
577581 if (Py_OptimizeFlag )
578582 return ;
579583 }
584+ if (extended_arg ){
585+ com_addbyte (c , EXTENDED_ARG );
586+ com_addint (c , extended_arg );
587+ arg &= 0xffff ;
588+ }
580589 com_addbyte (c , op );
581590 com_addint (c , arg );
582591}
@@ -606,7 +615,14 @@ com_backpatch(struct compiling *c, int anchor)
606615 prev = code [anchor ] + (code [anchor + 1 ] << 8 );
607616 dist = target - (anchor + 2 );
608617 code [anchor ] = dist & 0xff ;
609- code [anchor + 1 ] = dist >> 8 ;
618+ dist >>= 8 ;
619+ code [anchor + 1 ] = dist ;
620+ dist >>= 8 ;
621+ if (dist ) {
622+ com_error (c , PyExc_SystemError ,
623+ "com_backpatch: offset too large" );
624+ break ;
625+ }
610626 if (!prev )
611627 break ;
612628 anchor -= prev ;
@@ -3364,6 +3380,7 @@ optimize(struct compiling *c)
33643380 break ;
33653381 if (HAS_ARG (opcode ))
33663382 oparg = NEXTARG ();
3383+ dispatch_opcode1 :
33673384 switch (opcode ) {
33683385 case STORE_NAME :
33693386 case DELETE_NAME :
@@ -3374,6 +3391,11 @@ optimize(struct compiling *c)
33743391 case EXEC_STMT :
33753392 c -> c_flags &= ~CO_OPTIMIZED ;
33763393 break ;
3394+ case EXTENDED_ARG :
3395+ opcode = NEXTOP ();
3396+ oparg = oparg <<16 | NEXTARG ();
3397+ goto dispatch_opcode1 ;
3398+ break ;
33773399 }
33783400 }
33793401
@@ -3389,6 +3411,7 @@ optimize(struct compiling *c)
33893411 break ;
33903412 if (HAS_ARG (opcode ))
33913413 oparg = NEXTARG ();
3414+ dispatch_opcode2 :
33923415 if (opcode == LOAD_NAME ||
33933416 opcode == STORE_NAME ||
33943417 opcode == DELETE_NAME ) {
@@ -3403,13 +3426,20 @@ optimize(struct compiling *c)
34033426 continue ;
34043427 }
34053428 i = PyInt_AsLong (v );
3429+ if (i >> 16 ) /* too big for 2 bytes */
3430+ continue ;
34063431 switch (opcode ) {
34073432 case LOAD_NAME : cur_instr [0 ] = LOAD_FAST ; break ;
34083433 case STORE_NAME : cur_instr [0 ] = STORE_FAST ; break ;
34093434 case DELETE_NAME : cur_instr [0 ] = DELETE_FAST ; break ;
34103435 }
34113436 cur_instr [1 ] = i & 0xff ;
3412- cur_instr [2 ] = (i >>8 ) & 0xff ;
3437+ cur_instr [2 ] = i >> 8 ;
3438+ }
3439+ if (opcode == EXTENDED_ARG ) {
3440+ opcode = NEXTOP ();
3441+ oparg = oparg <<16 | NEXTARG ();
3442+ goto dispatch_opcode2 ;
34133443 }
34143444 }
34153445
0 commit comments