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

Skip to content

Commit 467a698

Browse files
committed
Small code improvements for readability, code size, and/or speed.
BINARY_SUBSCR: * invert test for normal case fall through * eliminate err handling code by jumping to slow_case LOAD_LOCALS: * invert test for normal case fall through * continue instead of break for the non-error case STORE_NAME and DELETE_NAME: * invert test for normal case fall through LOAD_NAME: * continue instead of break for the non-error case DELETE_FAST: * invert test for normal case fall through LOAD_DEREF: * invert test for normal case fall through * continue instead of break for the non-error case
1 parent 22ab06e commit 467a698

1 file changed

Lines changed: 54 additions & 58 deletions

File tree

Python/ceval.c

Lines changed: 54 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,18 +1155,15 @@ eval_frame(PyFrameObject *f)
11551155
long i = PyInt_AsLong(w);
11561156
if (i < 0)
11571157
i += PyList_GET_SIZE(v);
1158-
if (i < 0 ||
1159-
i >= PyList_GET_SIZE(v)) {
1160-
PyErr_SetString(PyExc_IndexError,
1161-
"list index out of range");
1162-
x = NULL;
1163-
}
1164-
else {
1158+
if (i >= 0 && i < PyList_GET_SIZE(v)) {
11651159
x = PyList_GET_ITEM(v, i);
11661160
Py_INCREF(x);
11671161
}
1162+
else
1163+
goto slow_get;
11681164
}
11691165
else
1166+
slow_get:
11701167
x = PyObject_GetItem(v, w);
11711168
Py_DECREF(v);
11721169
Py_DECREF(w);
@@ -1608,13 +1605,12 @@ eval_frame(PyFrameObject *f)
16081605
break;
16091606

16101607
case LOAD_LOCALS:
1611-
if ((x = f->f_locals) == NULL) {
1612-
PyErr_SetString(PyExc_SystemError,
1613-
"no locals");
1614-
break;
1608+
if ((x = f->f_locals) != NULL) {
1609+
Py_INCREF(x);
1610+
PUSH(x);
1611+
continue;
16151612
}
1616-
Py_INCREF(x);
1617-
PUSH(x);
1613+
PyErr_SetString(PyExc_SystemError, "no locals");
16181614
break;
16191615

16201616
case RETURN_VALUE:
@@ -1687,27 +1683,27 @@ eval_frame(PyFrameObject *f)
16871683
case STORE_NAME:
16881684
w = GETITEM(names, oparg);
16891685
v = POP();
1690-
if ((x = f->f_locals) == NULL) {
1691-
PyErr_Format(PyExc_SystemError,
1692-
"no locals found when storing %s",
1693-
PyObject_REPR(w));
1686+
if ((x = f->f_locals) != NULL) {
1687+
err = PyDict_SetItem(x, w, v);
1688+
Py_DECREF(v);
16941689
break;
16951690
}
1696-
err = PyDict_SetItem(x, w, v);
1697-
Py_DECREF(v);
1691+
PyErr_Format(PyExc_SystemError,
1692+
"no locals found when storing %s",
1693+
PyObject_REPR(w));
16981694
break;
16991695

17001696
case DELETE_NAME:
17011697
w = GETITEM(names, oparg);
1702-
if ((x = f->f_locals) == NULL) {
1703-
PyErr_Format(PyExc_SystemError,
1704-
"no locals when deleting %s",
1705-
PyObject_REPR(w));
1698+
if ((x = f->f_locals) != NULL) {
1699+
if ((err = PyDict_DelItem(x, w)) != 0)
1700+
format_exc_check_arg(PyExc_NameError,
1701+
NAME_ERROR_MSG ,w);
17061702
break;
17071703
}
1708-
if ((err = PyDict_DelItem(x, w)) != 0)
1709-
format_exc_check_arg(PyExc_NameError,
1710-
NAME_ERROR_MSG ,w);
1704+
PyErr_Format(PyExc_SystemError,
1705+
"no locals when deleting %s",
1706+
PyObject_REPR(w));
17111707
break;
17121708

17131709
PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
@@ -1794,7 +1790,7 @@ eval_frame(PyFrameObject *f)
17941790
}
17951791
Py_INCREF(x);
17961792
PUSH(x);
1797-
break;
1793+
continue;
17981794

17991795
case LOAD_GLOBAL:
18001796
w = GETITEM(names, oparg);
@@ -1840,16 +1836,16 @@ eval_frame(PyFrameObject *f)
18401836

18411837
case DELETE_FAST:
18421838
x = GETLOCAL(oparg);
1843-
if (x == NULL) {
1844-
format_exc_check_arg(
1845-
PyExc_UnboundLocalError,
1846-
UNBOUNDLOCAL_ERROR_MSG,
1847-
PyTuple_GetItem(co->co_varnames, oparg)
1848-
);
1849-
break;
1839+
if (x != NULL) {
1840+
SETLOCAL(oparg, NULL);
1841+
continue;
18501842
}
1851-
SETLOCAL(oparg, NULL);
1852-
continue;
1843+
format_exc_check_arg(
1844+
PyExc_UnboundLocalError,
1845+
UNBOUNDLOCAL_ERROR_MSG,
1846+
PyTuple_GetItem(co->co_varnames, oparg)
1847+
);
1848+
break;
18531849

18541850
case LOAD_CLOSURE:
18551851
x = freevars[oparg];
@@ -1860,30 +1856,30 @@ eval_frame(PyFrameObject *f)
18601856
case LOAD_DEREF:
18611857
x = freevars[oparg];
18621858
w = PyCell_Get(x);
1863-
if (w == NULL) {
1864-
err = -1;
1865-
/* Don't stomp existing exception */
1866-
if (PyErr_Occurred())
1867-
break;
1868-
if (oparg < f->f_ncells) {
1869-
v = PyTuple_GetItem(co->co_cellvars,
1870-
oparg);
1871-
format_exc_check_arg(
1872-
PyExc_UnboundLocalError,
1873-
UNBOUNDLOCAL_ERROR_MSG,
1874-
v);
1875-
} else {
1876-
v = PyTuple_GetItem(
1877-
co->co_freevars,
1878-
oparg - f->f_ncells);
1879-
format_exc_check_arg(
1880-
PyExc_NameError,
1881-
UNBOUNDFREE_ERROR_MSG,
1882-
v);
1883-
}
1859+
if (w != NULL) {
1860+
PUSH(w);
1861+
continue;
1862+
}
1863+
err = -1;
1864+
/* Don't stomp existing exception */
1865+
if (PyErr_Occurred())
18841866
break;
1867+
if (oparg < f->f_ncells) {
1868+
v = PyTuple_GetItem(co->co_cellvars,
1869+
oparg);
1870+
format_exc_check_arg(
1871+
PyExc_UnboundLocalError,
1872+
UNBOUNDLOCAL_ERROR_MSG,
1873+
v);
1874+
} else {
1875+
v = PyTuple_GetItem(
1876+
co->co_freevars,
1877+
oparg - f->f_ncells);
1878+
format_exc_check_arg(
1879+
PyExc_NameError,
1880+
UNBOUNDFREE_ERROR_MSG,
1881+
v);
18851882
}
1886-
PUSH(w);
18871883
break;
18881884

18891885
case STORE_DEREF:

0 commit comments

Comments
 (0)