Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 607f8a5

Browse files
Issue #27301: Fixed incorrect return codes for errors in compile.c.
2 parents f0f2960 + 694de3b commit 607f8a5

1 file changed

Lines changed: 38 additions & 24 deletions

File tree

Python/compile.c

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1532,10 +1532,14 @@ compiler_decorators(struct compiler *c, asdl_seq* decos)
15321532
return 1;
15331533
}
15341534

1535-
static Py_ssize_t
1535+
static int
15361536
compiler_visit_kwonlydefaults(struct compiler *c, asdl_seq *kwonlyargs,
15371537
asdl_seq *kw_defaults)
15381538
{
1539+
/* Push a dict of keyword-only default values.
1540+
1541+
Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed.
1542+
*/
15391543
int i;
15401544
PyObject *keys = NULL;
15411545

@@ -1551,7 +1555,7 @@ compiler_visit_kwonlydefaults(struct compiler *c, asdl_seq *kwonlyargs,
15511555
keys = PyList_New(1);
15521556
if (keys == NULL) {
15531557
Py_DECREF(mangled);
1554-
return -1;
1558+
return 0;
15551559
}
15561560
PyList_SET_ITEM(keys, 0, mangled);
15571561
}
@@ -1572,19 +1576,20 @@ compiler_visit_kwonlydefaults(struct compiler *c, asdl_seq *kwonlyargs,
15721576
PyObject *keys_tuple = PyList_AsTuple(keys);
15731577
Py_DECREF(keys);
15741578
if (keys_tuple == NULL) {
1575-
return -1;
1579+
return 0;
15761580
}
15771581
ADDOP_N(c, LOAD_CONST, keys_tuple, consts);
15781582
ADDOP_I(c, BUILD_CONST_KEY_MAP, default_count);
1579-
return default_count;
1583+
assert(default_count > 0);
1584+
return 1;
15801585
}
15811586
else {
1582-
return 0;
1587+
return -1;
15831588
}
15841589

15851590
error:
15861591
Py_XDECREF(keys);
1587-
return -1;
1592+
return 0;
15881593
}
15891594

15901595
static int
@@ -1623,21 +1628,21 @@ compiler_visit_argannotations(struct compiler *c, asdl_seq* args,
16231628
return 1;
16241629
}
16251630

1626-
static Py_ssize_t
1631+
static int
16271632
compiler_visit_annotations(struct compiler *c, arguments_ty args,
16281633
expr_ty returns)
16291634
{
1630-
/* Push arg annotation dict. Return # of items pushed.
1635+
/* Push arg annotation dict.
16311636
The expressions are evaluated out-of-order wrt the source code.
16321637
1633-
Returns -1 on error.
1638+
Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed.
16341639
*/
16351640
static identifier return_str;
16361641
PyObject *names;
16371642
Py_ssize_t len;
16381643
names = PyList_New(0);
16391644
if (!names)
1640-
return -1;
1645+
return 0;
16411646

16421647
if (!compiler_visit_argannotations(c, args->args, names))
16431648
goto error;
@@ -1666,34 +1671,43 @@ compiler_visit_annotations(struct compiler *c, arguments_ty args,
16661671
PyObject *keytuple = PyList_AsTuple(names);
16671672
Py_DECREF(names);
16681673
if (keytuple == NULL) {
1669-
return -1;
1674+
return 0;
16701675
}
16711676
ADDOP_N(c, LOAD_CONST, keytuple, consts);
16721677
ADDOP_I(c, BUILD_CONST_KEY_MAP, len);
1678+
return 1;
16731679
}
16741680
else {
16751681
Py_DECREF(names);
1682+
return -1;
16761683
}
1677-
return len;
16781684

16791685
error:
16801686
Py_DECREF(names);
1681-
return -1;
1687+
return 0;
1688+
}
1689+
1690+
static int
1691+
compiler_visit_defaults(struct compiler *c, arguments_ty args)
1692+
{
1693+
VISIT_SEQ(c, expr, args->defaults);
1694+
ADDOP_I(c, BUILD_TUPLE, asdl_seq_LEN(args->defaults));
1695+
return 1;
16821696
}
16831697

16841698
static Py_ssize_t
16851699
compiler_default_arguments(struct compiler *c, arguments_ty args)
16861700
{
16871701
Py_ssize_t funcflags = 0;
16881702
if (args->defaults && asdl_seq_LEN(args->defaults) > 0) {
1689-
VISIT_SEQ(c, expr, args->defaults);
1690-
ADDOP_I(c, BUILD_TUPLE, asdl_seq_LEN(args->defaults));
1703+
if (!compiler_visit_defaults(c, args))
1704+
return -1;
16911705
funcflags |= 0x01;
16921706
}
16931707
if (args->kwonlyargs) {
1694-
Py_ssize_t res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1708+
int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
16951709
args->kw_defaults);
1696-
if (res < 0) {
1710+
if (res == 0) {
16971711
return -1;
16981712
}
16991713
else if (res > 0) {
@@ -1716,7 +1730,7 @@ compiler_function(struct compiler *c, stmt_ty s, int is_async)
17161730
stmt_ty st;
17171731
Py_ssize_t i, n, funcflags;
17181732
int docstring;
1719-
int num_annotations;
1733+
int annotations;
17201734
int scope_type;
17211735

17221736
if (is_async) {
@@ -1749,11 +1763,11 @@ compiler_function(struct compiler *c, stmt_ty s, int is_async)
17491763
return 0;
17501764
}
17511765

1752-
num_annotations = compiler_visit_annotations(c, args, returns);
1753-
if (num_annotations < 0) {
1766+
annotations = compiler_visit_annotations(c, args, returns);
1767+
if (annotations == 0) {
17541768
return 0;
17551769
}
1756-
else if (num_annotations > 0) {
1770+
else if (annotations > 0) {
17571771
funcflags |= 0x04;
17581772
}
17591773

@@ -2478,7 +2492,7 @@ compiler_import_as(struct compiler *c, identifier name, identifier asname)
24782492
Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0,
24792493
PyUnicode_GET_LENGTH(name), 1);
24802494
if (dot == -2)
2481-
return -1;
2495+
return 0;
24822496
if (dot != -1) {
24832497
/* Consume the base module name to get the first attribute */
24842498
Py_ssize_t pos = dot + 1;
@@ -2487,12 +2501,12 @@ compiler_import_as(struct compiler *c, identifier name, identifier asname)
24872501
dot = PyUnicode_FindChar(name, '.', pos,
24882502
PyUnicode_GET_LENGTH(name), 1);
24892503
if (dot == -2)
2490-
return -1;
2504+
return 0;
24912505
attr = PyUnicode_Substring(name, pos,
24922506
(dot != -1) ? dot :
24932507
PyUnicode_GET_LENGTH(name));
24942508
if (!attr)
2495-
return -1;
2509+
return 0;
24962510
ADDOP_O(c, LOAD_ATTR, attr, names);
24972511
Py_DECREF(attr);
24982512
pos = dot + 1;

0 commit comments

Comments
 (0)