From 8fbf4fa9edcb9efd1ca5d650ed235eb4770dea3a Mon Sep 17 00:00:00 2001 From: Benjamin Peterson Date: Thu, 6 Dec 2012 17:41:04 -0500 Subject: create NameConstant AST class for None, True, and False literals (closes #16619) --- Python/peephole.c | 46 ---------------------------------------------- 1 file changed, 46 deletions(-) (limited to 'Python/peephole.c') diff --git a/Python/peephole.c b/Python/peephole.c index 5d536779ac..e6877bb3e6 100644 --- a/Python/peephole.c +++ b/Python/peephole.c @@ -327,37 +327,6 @@ markblocks(unsigned char *code, Py_ssize_t len) return blocks; } -/* Helper to replace LOAD_NAME None/True/False with LOAD_CONST - Returns: 0 if no change, 1 if change, -1 if error */ -static int -load_global(unsigned char *codestr, Py_ssize_t i, char *name, PyObject *consts) -{ - Py_ssize_t j; - PyObject *obj; - if (name == NULL) - return 0; - if (strcmp(name, "None") == 0) - obj = Py_None; - else if (strcmp(name, "True") == 0) - obj = Py_True; - else if (strcmp(name, "False") == 0) - obj = Py_False; - else - return 0; - for (j = 0; j < PyList_GET_SIZE(consts); j++) { - if (PyList_GET_ITEM(consts, j) == obj) - break; - } - if (j == PyList_GET_SIZE(consts)) { - if (PyList_Append(consts, obj) < 0) - return -1; - } - assert(PyList_GET_ITEM(consts, j) == obj); - codestr[i] = LOAD_CONST; - SETARG(codestr, i, j); - return 1; -} - /* Perform basic peephole optimizations to components of a code object. The consts object should still be in list form to allow new constants to be appended. @@ -392,7 +361,6 @@ PyCode_Optimize(PyObject *code, PyObject* consts, PyObject *names, Py_ssize_t const_stack_size = 0; int in_consts = 0; /* whether we are in a LOAD_CONST sequence */ unsigned int *blocks = NULL; - char *name; /* Bail out if an exception is set */ if (PyErr_Occurred()) @@ -475,20 +443,6 @@ PyCode_Optimize(PyObject *code, PyObject* consts, PyObject *names, codestr[i+3] = NOP; break; - /* Replace LOAD_GLOBAL/LOAD_NAME None/True/False - with LOAD_CONST None/True/False */ - case LOAD_NAME: - case LOAD_GLOBAL: - j = GETARG(codestr, i); - name = _PyUnicode_AsString(PyTuple_GET_ITEM(names, j)); - h = load_global(codestr, i, name, consts); - if (h < 0) - goto exitError; - else if (h == 0) - continue; - CONST_STACK_PUSH_OP(i); - break; - /* Skip over LOAD_CONST trueconst POP_JUMP_IF_FALSE xx. This improves "while 1" performance. */ -- cgit v1.2.1 From 1e63d3b8c5c229cb02145b3fcab50f1f4cbdfcc6 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 9 Jul 2013 00:32:04 +0200 Subject: Issue #18408: Fix PyCode_Optimize(): raise a MemoryError on memory allocation failure. --- Python/peephole.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'Python/peephole.c') diff --git a/Python/peephole.c b/Python/peephole.c index e6877bb3e6..a49790a60f 100644 --- a/Python/peephole.c +++ b/Python/peephole.c @@ -381,8 +381,10 @@ PyCode_Optimize(PyObject *code, PyObject* consts, PyObject *names, /* Make a modifiable copy of the code string */ codestr = (unsigned char *)PyMem_Malloc(codelen); - if (codestr == NULL) + if (codestr == NULL) { + PyErr_NoMemory(); goto exitError; + } codestr = (unsigned char *)memcpy(codestr, PyBytes_AS_STRING(code), codelen); @@ -396,8 +398,10 @@ PyCode_Optimize(PyObject *code, PyObject* consts, PyObject *names, /* Mapping to new jump targets after NOPs are removed */ addrmap = (int *)PyMem_Malloc(codelen * sizeof(int)); - if (addrmap == NULL) + if (addrmap == NULL) { + PyErr_NoMemory(); goto exitError; + } blocks = markblocks(codestr, codelen); if (blocks == NULL) -- cgit v1.2.1 From 6055df2a55bf5b38d13b1a9363c2d65116e8560e Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 14 Nov 2013 01:21:00 +0100 Subject: Issue #19437: Fix fold_unaryops_on_constants() of the peephole optimizer, clear the exception when PyList_Append() fails --- Python/peephole.c | 1 + 1 file changed, 1 insertion(+) (limited to 'Python/peephole.c') diff --git a/Python/peephole.c b/Python/peephole.c index a49790a60f..4185462b34 100644 --- a/Python/peephole.c +++ b/Python/peephole.c @@ -275,6 +275,7 @@ fold_unaryops_on_constants(unsigned char *codestr, PyObject *consts, PyObject *v len_consts = PyList_GET_SIZE(consts); if (PyList_Append(consts, newconst)) { Py_DECREF(newconst); + PyErr_Clear(); return 0; } Py_DECREF(newconst); -- cgit v1.2.1