]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Backport leak fix for new code objects.
authorRaymond Hettinger <python@rcn.com>
Tue, 16 Sep 2003 04:36:33 +0000 (04:36 +0000)
committerRaymond Hettinger <python@rcn.com>
Tue, 16 Sep 2003 04:36:33 +0000 (04:36 +0000)
Misc/NEWS
Python/compile.c

index ce1fb7e0d5da0d2d50ae71b97d8bb17b25a0095c..c60771af2ba6b0ef9f586d691996e226829550dd 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,8 @@ What's New in Python 2.3.1?
 Core and builtins
 -----------------
 
+- Fixed a leak when new code objects are instantiated.
+
 - Bug #800796: slice(1).__hash__() now raises a TypeError, unhashable type.
 
 - Bug #603724: Pass an explicit buffer to setvbuf in PyFile_SetBufSize().
index 38624d4cceb537945b447d72f7072fd1e28e3df9..9ac0032bab2829fc3b659360023a6092461a086e 100644 (file)
@@ -104,6 +104,8 @@ code_new(PyTypeObject *type, PyObject *args, PyObject *kw)
        int nlocals;
        int stacksize;
        int flags;
+       PyObject *co;
+       PyObject *empty = NULL;
        PyObject *code;
        PyObject *consts;
        PyObject *names;
@@ -127,31 +129,28 @@ code_new(PyTypeObject *type, PyObject *args, PyObject *kw)
                              &PyTuple_Type, &cellvars))
                return NULL;
 
-       if (freevars == NULL || cellvars == NULL) {
-               PyObject *empty = PyTuple_New(0);
-               if (empty == NULL)
-                   return NULL;
-               if (freevars == NULL) {
-                   freevars = empty;
-                   Py_INCREF(freevars);
-               }
-               if (cellvars == NULL) {
-                   cellvars = empty;
-                   Py_INCREF(cellvars);
-               }
-               Py_DECREF(empty);
-       }
-
        if (!PyObject_CheckReadBuffer(code)) {
                PyErr_SetString(PyExc_TypeError,
                  "bytecode object must be a single-segment read-only buffer");
                return NULL;
        }
 
-       return (PyObject *)PyCode_New(argcount, nlocals, stacksize, flags,
+       if (freevars == NULL || cellvars == NULL) {
+               empty = PyTuple_New(0);
+               if (empty == NULL)
+                       return NULL;
+               if (freevars == NULL)
+                       freevars = empty;
+               if (cellvars == NULL)
+                       cellvars = empty;
+       }
+
+       co = (PyObject *) PyCode_New(argcount, nlocals, stacksize, flags,
                                      code, consts, names, varnames,
                                      freevars, cellvars, filename, name,
-                                     firstlineno, lnotab); 
+                                     firstlineno, lnotab);
+       Py_XDECREF(empty);
+       return co;
 }
 
 static void