diff options
author | Jeffrey Yasskin <jyasskin@gmail.com> | 2009-05-08 21:51:06 (GMT) |
---|---|---|
committer | Jeffrey Yasskin <jyasskin@gmail.com> | 2009-05-08 21:51:06 (GMT) |
commit | 1aa4700234aa0657ee8cb12cfd9b615fef9e0300 (patch) | |
tree | 6a480c31e4b83218ae9412f7ab2e9d41b34f8fd0 /Objects/codeobject.c | |
parent | 083d1f9f9a26486cf9f0d85fb0e76e6cf9474b0c (diff) | |
download | cpython-1aa4700234aa0657ee8cb12cfd9b615fef9e0300.zip cpython-1aa4700234aa0657ee8cb12cfd9b615fef9e0300.tar.gz cpython-1aa4700234aa0657ee8cb12cfd9b615fef9e0300.tar.bz2 |
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.
Diffstat (limited to 'Objects/codeobject.c')
-rw-r--r-- | Objects/codeobject.c | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/Objects/codeobject.c b/Objects/codeobject.c index e94b4cc..55f3fb8 100644 --- a/Objects/codeobject.c +++ b/Objects/codeobject.c @@ -107,6 +107,52 @@ PyCode_New(int argcount, int nlocals, int stacksize, int flags, 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) |