22
22
*/
23
23
24
24
#include "Python.h"
25
+ #include "pycore_pymem.h" // _PyMem_IsPtrFreed()
25
26
#include "pycore_long.h" // _PyLong_GetZero()
26
27
27
28
#include "Python-ast.h"
@@ -520,9 +521,7 @@ compiler_unit_check(struct compiler_unit *u)
520
521
{
521
522
basicblock * block ;
522
523
for (block = u -> u_blocks ; block != NULL ; block = block -> b_list ) {
523
- assert ((uintptr_t )block != 0xcbcbcbcbU );
524
- assert ((uintptr_t )block != 0xfbfbfbfbU );
525
- assert ((uintptr_t )block != 0xdbdbdbdbU );
524
+ assert (!_PyMem_IsPtrFreed (block ));
526
525
if (block -> b_instr != NULL ) {
527
526
assert (block -> b_ialloc > 0 );
528
527
assert (block -> b_iused >= 0 );
@@ -681,7 +680,8 @@ compiler_exit_scope(struct compiler *c)
681
680
assert (c -> u );
682
681
/* we are deleting from a list so this really shouldn't fail */
683
682
if (PySequence_DelItem (c -> c_stack , n ) < 0 ) {
684
- Py_FatalError ("PySequence_DelItem failed" );
683
+ _PyErr_WriteUnraisableMsg ("on removing the last compiler "
684
+ "stack item" , NULL );
685
685
}
686
686
compiler_unit_check (c -> u );
687
687
}
@@ -1898,17 +1898,15 @@ get_ref_type(struct compiler *c, PyObject *name)
1898
1898
return CELL ;
1899
1899
scope = PyST_GetScope (c -> u -> u_ste , name );
1900
1900
if (scope == 0 ) {
1901
- _Py_FatalErrorFormat (__func__ ,
1902
- "unknown scope for %.100s in %.100s(%s)\n"
1903
- "symbols: %s\nlocals: %s\nglobals: %s" ,
1904
- PyUnicode_AsUTF8 (name ),
1905
- PyUnicode_AsUTF8 (c -> u -> u_name ),
1906
- PyUnicode_AsUTF8 (PyObject_Repr (c -> u -> u_ste -> ste_id )),
1907
- PyUnicode_AsUTF8 (PyObject_Repr (c -> u -> u_ste -> ste_symbols )),
1908
- PyUnicode_AsUTF8 (PyObject_Repr (c -> u -> u_varnames )),
1909
- PyUnicode_AsUTF8 (PyObject_Repr (c -> u -> u_names )));
1901
+ PyErr_Format (PyExc_SystemError ,
1902
+ "PyST_GetScope(name=%R) failed: "
1903
+ "unknown scope in unit %S (%R); "
1904
+ "symbols: %R; locals: %R; globals: %R" ,
1905
+ name ,
1906
+ c -> u -> u_name , c -> u -> u_ste -> ste_id ,
1907
+ c -> u -> u_ste -> ste_symbols , c -> u -> u_varnames , c -> u -> u_names );
1908
+ return -1 ;
1910
1909
}
1911
-
1912
1910
return scope ;
1913
1911
}
1914
1912
@@ -1923,7 +1921,8 @@ compiler_lookup_arg(PyObject *dict, PyObject *name)
1923
1921
}
1924
1922
1925
1923
static int
1926
- compiler_make_closure (struct compiler * c , PyCodeObject * co , Py_ssize_t flags , PyObject * qualname )
1924
+ compiler_make_closure (struct compiler * c , PyCodeObject * co , Py_ssize_t flags ,
1925
+ PyObject * qualname )
1927
1926
{
1928
1927
Py_ssize_t i , free = PyCode_GetNumFree (co );
1929
1928
if (qualname == NULL )
@@ -1935,28 +1934,34 @@ compiler_make_closure(struct compiler *c, PyCodeObject *co, Py_ssize_t flags, Py
1935
1934
LOAD_DEREF but LOAD_CLOSURE is needed.
1936
1935
*/
1937
1936
PyObject * name = PyTuple_GET_ITEM (co -> co_freevars , i );
1938
- int arg , reftype ;
1939
1937
1940
1938
/* Special case: If a class contains a method with a
1941
1939
free variable that has the same name as a method,
1942
1940
the name will be considered free *and* local in the
1943
1941
class. It should be handled by the closure, as
1944
1942
well as by the normal name lookup logic.
1945
1943
*/
1946
- reftype = get_ref_type (c , name );
1947
- if (reftype == CELL )
1944
+ int reftype = get_ref_type (c , name );
1945
+ if (reftype == -1 ) {
1946
+ return 0 ;
1947
+ }
1948
+ int arg ;
1949
+ if (reftype == CELL ) {
1948
1950
arg = compiler_lookup_arg (c -> u -> u_cellvars , name );
1949
- else /* (reftype == FREE) */
1951
+ }
1952
+ else {
1950
1953
arg = compiler_lookup_arg (c -> u -> u_freevars , name );
1954
+ }
1951
1955
if (arg == -1 ) {
1952
- _Py_FatalErrorFormat (__func__ ,
1953
- "lookup %s in %s %d %d\n"
1954
- "freevars of %s: %s\n" ,
1955
- PyUnicode_AsUTF8 (PyObject_Repr (name )),
1956
- PyUnicode_AsUTF8 (c -> u -> u_name ),
1957
- reftype , arg ,
1958
- PyUnicode_AsUTF8 (co -> co_name ),
1959
- PyUnicode_AsUTF8 (PyObject_Repr (co -> co_freevars )));
1956
+ PyErr_Format (PyExc_SystemError ,
1957
+ "compiler_lookup_arg(name=%R) with reftype=%d failed in %S; "
1958
+ "freevars of code %S: %R" ,
1959
+ name ,
1960
+ reftype ,
1961
+ c -> u -> u_name ,
1962
+ co -> co_name ,
1963
+ co -> co_freevars );
1964
+ return 0 ;
1960
1965
}
1961
1966
ADDOP_I (c , LOAD_CLOSURE , arg );
1962
1967
}
@@ -2294,7 +2299,11 @@ compiler_function(struct compiler *c, stmt_ty s, int is_async)
2294
2299
return 0 ;
2295
2300
}
2296
2301
2297
- compiler_make_closure (c , co , funcflags , qualname );
2302
+ if (!compiler_make_closure (c , co , funcflags , qualname )) {
2303
+ Py_DECREF (qualname );
2304
+ Py_DECREF (co );
2305
+ return 0 ;
2306
+ }
2298
2307
Py_DECREF (qualname );
2299
2308
Py_DECREF (co );
2300
2309
@@ -2419,7 +2428,10 @@ compiler_class(struct compiler *c, stmt_ty s)
2419
2428
ADDOP (c , LOAD_BUILD_CLASS );
2420
2429
2421
2430
/* 3. load a function (or closure) made from the code object */
2422
- compiler_make_closure (c , co , 0 , NULL );
2431
+ if (!compiler_make_closure (c , co , 0 , NULL )) {
2432
+ Py_DECREF (co );
2433
+ return 0 ;
2434
+ }
2423
2435
Py_DECREF (co );
2424
2436
2425
2437
/* 4. load class name */
@@ -2697,7 +2709,11 @@ compiler_lambda(struct compiler *c, expr_ty e)
2697
2709
return 0 ;
2698
2710
}
2699
2711
2700
- compiler_make_closure (c , co , funcflags , qualname );
2712
+ if (!compiler_make_closure (c , co , funcflags , qualname )) {
2713
+ Py_DECREF (qualname );
2714
+ Py_DECREF (co );
2715
+ return 0 ;
2716
+ }
2701
2717
Py_DECREF (qualname );
2702
2718
Py_DECREF (co );
2703
2719
@@ -4660,8 +4676,9 @@ compiler_comprehension(struct compiler *c, expr_ty e, int type,
4660
4676
if (co == NULL )
4661
4677
goto error ;
4662
4678
4663
- if (!compiler_make_closure (c , co , 0 , qualname ))
4679
+ if (!compiler_make_closure (c , co , 0 , qualname )) {
4664
4680
goto error ;
4681
+ }
4665
4682
Py_DECREF (qualname );
4666
4683
Py_DECREF (co );
4667
4684
@@ -5468,8 +5485,10 @@ stackdepth(struct compiler *c)
5468
5485
struct instr * instr = & b -> b_instr [i ];
5469
5486
int effect = stack_effect (instr -> i_opcode , instr -> i_oparg , 0 );
5470
5487
if (effect == PY_INVALID_STACK_EFFECT ) {
5471
- _Py_FatalErrorFormat (__func__ ,
5472
- "opcode = %d" , instr -> i_opcode );
5488
+ PyErr_Format (PyExc_SystemError ,
5489
+ "compiler stack_effect(opcode=%d, arg=%i) failed" ,
5490
+ instr -> i_opcode , instr -> i_oparg );
5491
+ return -1 ;
5473
5492
}
5474
5493
int new_depth = depth + effect ;
5475
5494
if (new_depth > maxdepth ) {
@@ -6675,4 +6694,3 @@ PyCode_Optimize(PyObject *code, PyObject* Py_UNUSED(consts),
6675
6694
Py_INCREF (code );
6676
6695
return code ;
6677
6696
}
6678
-
0 commit comments