Mercurial > cpython-lru_cache-weakref
diff Objects/codeobject.c @ 53930:a171048daf76 legacy-trunk
PyCode_NewEmpty:
Most uses of PyCode_New found by http://www.google.com/codesearch?q=PyCode_New
are trying to build an empty code object, usually to put it in a dummy frame
object. This patch adds a PyCode_NewEmpty wrapper which lets the user specify
just the filename, function name, and first line number, instead of also
requiring lots of code internals.
| author | Jeffrey Yasskin <jyasskin@gmail.com> |
|---|---|
| date | Fri, 08 May 2009 21:51:06 +0000 |
| parents | 52294af4b663 |
| children | dddf424bd669 |
line wrap: on
line diff
--- a/Objects/codeobject.c Fri May 08 20:58:08 2009 +0000 +++ b/Objects/codeobject.c Fri May 08 21:51:06 2009 +0000 @@ -107,6 +107,52 @@ return co; } +PyCodeObject * +PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno) +{ + static PyObject *emptystring = NULL; + static PyObject *nulltuple = NULL; + PyObject *filename_ob = NULL; + PyObject *funcname_ob = NULL; + PyCodeObject *result = NULL; + if (emptystring == NULL) { + emptystring = PyString_FromString(""); + if (emptystring == NULL) + goto failed; + } + if (nulltuple == NULL) { + nulltuple = PyTuple_New(0); + if (nulltuple == NULL) + goto failed; + } + funcname_ob = PyString_FromString(funcname); + if (funcname_ob == NULL) + goto failed; + filename_ob = PyString_FromString(filename); + if (filename_ob == NULL) + goto failed; + + result = PyCode_New(0, /* argcount */ + 0, /* nlocals */ + 0, /* stacksize */ + 0, /* flags */ + emptystring, /* code */ + nulltuple, /* consts */ + nulltuple, /* names */ + nulltuple, /* varnames */ + nulltuple, /* freevars */ + nulltuple, /* cellvars */ + filename_ob, /* filename */ + funcname_ob, /* name */ + firstlineno, /* firstlineno */ + emptystring /* lnotab */ + ); + +failed: + Py_XDECREF(funcname_ob); + Py_XDECREF(filename_ob); + return result; +} #define OFF(x) offsetof(PyCodeObject, x)
