diff Python/ceval.c @ 31878:26ee1b3eb345 legacy-trunk

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
author Raymond Hettinger <python@rcn.com>
date Wed, 07 Apr 2004 11:39:21 +0000
parents 1fb2313ac3ce
children 1a3a48cbb162
line wrap: on
line diff
--- a/Python/ceval.c	Tue Apr 06 19:43:03 2004 +0000
+++ b/Python/ceval.c	Wed Apr 07 11:39:21 2004 +0000
@@ -1155,18 +1155,15 @@
 				long i = PyInt_AsLong(w);
 				if (i < 0)
 					i += PyList_GET_SIZE(v);
-				if (i < 0 ||
-				    i >= PyList_GET_SIZE(v)) {
-					PyErr_SetString(PyExc_IndexError,
-						"list index out of range");
-					x = NULL;
-				}
-				else {
+				if (i >= 0 && i < PyList_GET_SIZE(v)) {
 					x = PyList_GET_ITEM(v, i);
 					Py_INCREF(x);
 				}
+				else
+					goto slow_get;
 			}
 			else
+			  slow_get:
 				x = PyObject_GetItem(v, w);
 			Py_DECREF(v);
 			Py_DECREF(w);
@@ -1608,13 +1605,12 @@
 			break;
 
 		case LOAD_LOCALS:
-			if ((x = f->f_locals) == NULL) {
-				PyErr_SetString(PyExc_SystemError,
-						"no locals");
-				break;
+			if ((x = f->f_locals) != NULL) {
+				Py_INCREF(x);
+				PUSH(x);
+				continue;
 			}
-			Py_INCREF(x);
-			PUSH(x);
+			PyErr_SetString(PyExc_SystemError, "no locals");
 			break;
 
 		case RETURN_VALUE:
@@ -1687,27 +1683,27 @@
 		case STORE_NAME:
 			w = GETITEM(names, oparg);
 			v = POP();
-			if ((x = f->f_locals) == NULL) {
-				PyErr_Format(PyExc_SystemError,
-					     "no locals found when storing %s",
-					     PyObject_REPR(w));
+			if ((x = f->f_locals) != NULL) {
+				err = PyDict_SetItem(x, w, v);
+				Py_DECREF(v);
 				break;
 			}
-			err = PyDict_SetItem(x, w, v);
-			Py_DECREF(v);
+			PyErr_Format(PyExc_SystemError,
+				     "no locals found when storing %s",
+				     PyObject_REPR(w));
 			break;
 
 		case DELETE_NAME:
 			w = GETITEM(names, oparg);
-			if ((x = f->f_locals) == NULL) {
-				PyErr_Format(PyExc_SystemError,
-					     "no locals when deleting %s",
-					     PyObject_REPR(w));
+			if ((x = f->f_locals) != NULL) {
+				if ((err = PyDict_DelItem(x, w)) != 0)
+					format_exc_check_arg(PyExc_NameError,
+								NAME_ERROR_MSG ,w);
 				break;
 			}
-			if ((err = PyDict_DelItem(x, w)) != 0)
-				format_exc_check_arg(PyExc_NameError,
-							NAME_ERROR_MSG ,w);
+			PyErr_Format(PyExc_SystemError,
+				     "no locals when deleting %s",
+				     PyObject_REPR(w));
 			break;
 
 		PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
@@ -1794,7 +1790,7 @@
 			}
 			Py_INCREF(x);
 			PUSH(x);
-			break;
+			continue;
 
 		case LOAD_GLOBAL:
 			w = GETITEM(names, oparg);
@@ -1840,16 +1836,16 @@
 
 		case DELETE_FAST:
 			x = GETLOCAL(oparg);
-			if (x == NULL) {
-				format_exc_check_arg(
-					PyExc_UnboundLocalError,
-					UNBOUNDLOCAL_ERROR_MSG,
-					PyTuple_GetItem(co->co_varnames, oparg)
-					);
-				break;
+			if (x != NULL) {
+				SETLOCAL(oparg, NULL);
+				continue;
 			}
-			SETLOCAL(oparg, NULL);
-			continue;
+			format_exc_check_arg(
+				PyExc_UnboundLocalError,
+				UNBOUNDLOCAL_ERROR_MSG,
+				PyTuple_GetItem(co->co_varnames, oparg)
+				);
+			break;
 
 		case LOAD_CLOSURE:
 			x = freevars[oparg];
@@ -1860,30 +1856,30 @@
 		case LOAD_DEREF:
 			x = freevars[oparg];
 			w = PyCell_Get(x);
-			if (w == NULL) {
-				err = -1;
-				/* Don't stomp existing exception */
-				if (PyErr_Occurred())
-					break;
-				if (oparg < f->f_ncells) {
-					v = PyTuple_GetItem(co->co_cellvars,
-							       oparg);
-				       format_exc_check_arg(
-					       PyExc_UnboundLocalError,
-					       UNBOUNDLOCAL_ERROR_MSG,
-					       v);
-				} else {
-				       v = PyTuple_GetItem(
-						      co->co_freevars,
-						      oparg - f->f_ncells);
-				       format_exc_check_arg(
-					       PyExc_NameError,
-					       UNBOUNDFREE_ERROR_MSG,
-					       v);
-				}
+			if (w != NULL) {
+				PUSH(w);
+				continue;
+			}
+			err = -1;
+			/* Don't stomp existing exception */
+			if (PyErr_Occurred())
 				break;
+			if (oparg < f->f_ncells) {
+				v = PyTuple_GetItem(co->co_cellvars,
+						       oparg);
+			       format_exc_check_arg(
+				       PyExc_UnboundLocalError,
+				       UNBOUNDLOCAL_ERROR_MSG,
+				       v);
+			} else {
+			       v = PyTuple_GetItem(
+					      co->co_freevars,
+					      oparg - f->f_ncells);
+			       format_exc_check_arg(
+				       PyExc_NameError,
+				       UNBOUNDFREE_ERROR_MSG,
+				       v);
 			}
-			PUSH(w);
 			break;
 
 		case STORE_DEREF: