@@ -1075,6 +1075,15 @@ compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
1075
1075
return 0; \
1076
1076
}
1077
1077
1078
+ /* Same as ADDOP_O, but steals a reference. */
1079
+ #define ADDOP_N (C , OP , O , TYPE ) { \
1080
+ if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) { \
1081
+ Py_DECREF((O)); \
1082
+ return 0; \
1083
+ } \
1084
+ Py_DECREF((O)); \
1085
+ }
1086
+
1078
1087
#define ADDOP_NAME (C , OP , O , TYPE ) { \
1079
1088
if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1080
1089
return 0; \
@@ -1890,8 +1899,7 @@ compiler_import_as(struct compiler *c, identifier name, identifier asname)
1890
1899
dot ? dot - src : strlen (src ));
1891
1900
if (!attr )
1892
1901
return 0 ;
1893
- ADDOP_O (c , LOAD_ATTR , attr , names );
1894
- Py_DECREF (attr );
1902
+ ADDOP_N (c , LOAD_ATTR , attr , names );
1895
1903
src = dot + 1 ;
1896
1904
}
1897
1905
}
@@ -1923,8 +1931,7 @@ compiler_import(struct compiler *c, stmt_ty s)
1923
1931
if (level == NULL )
1924
1932
return 0 ;
1925
1933
1926
- ADDOP_O (c , LOAD_CONST , level , consts );
1927
- Py_DECREF (level );
1934
+ ADDOP_N (c , LOAD_CONST , level , consts );
1928
1935
ADDOP_O (c , LOAD_CONST , Py_None , consts );
1929
1936
ADDOP_NAME (c , IMPORT_NAME , alias -> name , names );
1930
1937
@@ -1959,8 +1966,7 @@ compiler_from_import(struct compiler *c, stmt_ty s)
1959
1966
{
1960
1967
int i , n = asdl_seq_LEN (s -> v .ImportFrom .names );
1961
1968
1962
- PyObject * names = PyTuple_New (n );
1963
- PyObject * level ;
1969
+ PyObject * level , * names ;
1964
1970
static PyObject * empty_string ;
1965
1971
1966
1972
if (!empty_string ) {
@@ -1969,19 +1975,20 @@ compiler_from_import(struct compiler *c, stmt_ty s)
1969
1975
return 0 ;
1970
1976
}
1971
1977
1972
- if (!names )
1973
- return 0 ;
1974
-
1975
1978
if (s -> v .ImportFrom .level == 0 && c -> c_flags &&
1976
1979
!(c -> c_flags -> cf_flags & CO_FUTURE_ABSOLUTE_IMPORT ))
1977
1980
level = PyInt_FromLong (-1 );
1978
1981
else
1979
1982
level = PyInt_FromLong (s -> v .ImportFrom .level );
1980
1983
1981
1984
if (!level ) {
1982
- Py_DECREF (names );
1983
1985
return 0 ;
1984
1986
}
1987
+ ADDOP_N (c , LOAD_CONST , level , consts );
1988
+
1989
+ names = PyTuple_New (n );
1990
+ if (!names )
1991
+ return 0 ;
1985
1992
1986
1993
/* build up the names */
1987
1994
for (i = 0 ; i < n ; i ++ ) {
@@ -1992,16 +1999,12 @@ compiler_from_import(struct compiler *c, stmt_ty s)
1992
1999
1993
2000
if (s -> lineno > c -> c_future -> ff_lineno && s -> v .ImportFrom .module &&
1994
2001
!strcmp (PyString_AS_STRING (s -> v .ImportFrom .module ), "__future__" )) {
1995
- Py_DECREF (level );
1996
2002
Py_DECREF (names );
1997
2003
return compiler_error (c , "from __future__ imports must occur "
1998
2004
"at the beginning of the file" );
1999
2005
}
2006
+ ADDOP_N (c , LOAD_CONST , names , consts );
2000
2007
2001
- ADDOP_O (c , LOAD_CONST , level , consts );
2002
- Py_DECREF (level );
2003
- ADDOP_O (c , LOAD_CONST , names , consts );
2004
- Py_DECREF (names );
2005
2008
if (s -> v .ImportFrom .module ) {
2006
2009
ADDOP_NAME (c , IMPORT_NAME , s -> v .ImportFrom .module , names );
2007
2010
}
@@ -2024,7 +2027,6 @@ compiler_from_import(struct compiler *c, stmt_ty s)
2024
2027
store_name = alias -> asname ;
2025
2028
2026
2029
if (!compiler_nameop (c , store_name , Store )) {
2027
- Py_DECREF (names );
2028
2030
return 0 ;
2029
2031
}
2030
2032
}
@@ -2391,8 +2393,7 @@ compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
2391
2393
"param invalid for local variable" );
2392
2394
return 0 ;
2393
2395
}
2394
- ADDOP_O (c , op , mangled , varnames );
2395
- Py_DECREF (mangled );
2396
+ ADDOP_N (c , op , mangled , varnames );
2396
2397
return 1 ;
2397
2398
case OP_GLOBAL :
2398
2399
switch (ctx ) {
0 commit comments