--- /dev/null
+#include "Python.h"\r
+#include "code.h"\r
+#include "structmember.h"\r
+\r
+#define NAME_CHARS \\r
+ "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"\r
+\r
+/* all_name_chars(s): true iff all chars in s are valid NAME_CHARS */\r
+\r
+static int\r
+all_name_chars(unsigned char *s)\r
+{\r
+ static char ok_name_char[256];\r
+ static unsigned char *name_chars = (unsigned char *)NAME_CHARS;\r
+\r
+ if (ok_name_char[*name_chars] == 0) {\r
+ unsigned char *p;\r
+ for (p = name_chars; *p; p++)\r
+ ok_name_char[*p] = 1;\r
+ }\r
+ while (*s) {\r
+ if (ok_name_char[*s++] == 0)\r
+ return 0;\r
+ }\r
+ return 1;\r
+}\r
+\r
+static void\r
+intern_strings(PyObject *tuple)\r
+{\r
+ Py_ssize_t i;\r
+\r
+ for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {\r
+ PyObject *v = PyTuple_GET_ITEM(tuple, i);\r
+ if (v == NULL || !PyString_CheckExact(v)) {\r
+ Py_FatalError("non-string found in code slot");\r
+ }\r
+ PyString_InternInPlace(&PyTuple_GET_ITEM(tuple, i));\r
+ }\r
+}\r
+\r
+\r
+PyCodeObject *\r
+PyCode_New(int argcount, int nlocals, int stacksize, int flags,\r
+ PyObject *code, PyObject *consts, PyObject *names,\r
+ PyObject *varnames, PyObject *freevars, PyObject *cellvars,\r
+ PyObject *filename, PyObject *name, int firstlineno,\r
+ PyObject *lnotab)\r
+{\r
+ PyCodeObject *co;\r
+ Py_ssize_t i;\r
+ /* Check argument types */\r
+ if (argcount < 0 || nlocals < 0 ||\r
+ code == NULL ||\r
+ consts == NULL || !PyTuple_Check(consts) ||\r
+ names == NULL || !PyTuple_Check(names) ||\r
+ varnames == NULL || !PyTuple_Check(varnames) ||\r
+ freevars == NULL || !PyTuple_Check(freevars) ||\r
+ cellvars == NULL || !PyTuple_Check(cellvars) ||\r
+ name == NULL || !PyString_Check(name) ||\r
+ filename == NULL || !PyString_Check(filename) ||\r
+ lnotab == NULL || !PyString_Check(lnotab) ||\r
+ !PyObject_CheckReadBuffer(code)) {\r
+ PyErr_BadInternalCall();\r
+ return NULL;\r
+ }\r
+ intern_strings(names);\r
+ intern_strings(varnames);\r
+ intern_strings(freevars);\r
+ intern_strings(cellvars);\r
+ /* Intern selected string constants */\r
+ for (i = PyTuple_Size(consts); --i >= 0; ) {\r
+ PyObject *v = PyTuple_GetItem(consts, i);\r
+ if (!PyString_Check(v))\r
+ continue;\r
+ if (!all_name_chars((unsigned char *)PyString_AS_STRING(v)))\r
+ continue;\r
+ PyString_InternInPlace(&PyTuple_GET_ITEM(consts, i));\r
+ }\r
+ co = PyObject_NEW(PyCodeObject, &PyCode_Type);\r
+ if (co != NULL) {\r
+ co->co_argcount = argcount;\r
+ co->co_nlocals = nlocals;\r
+ co->co_stacksize = stacksize;\r
+ co->co_flags = flags;\r
+ Py_INCREF(code);\r
+ co->co_code = code;\r
+ Py_INCREF(consts);\r
+ co->co_consts = consts;\r
+ Py_INCREF(names);\r
+ co->co_names = names;\r
+ Py_INCREF(varnames);\r
+ co->co_varnames = varnames;\r
+ Py_INCREF(freevars);\r
+ co->co_freevars = freevars;\r
+ Py_INCREF(cellvars);\r
+ co->co_cellvars = cellvars;\r
+ Py_INCREF(filename);\r
+ co->co_filename = filename;\r
+ Py_INCREF(name);\r
+ co->co_name = name;\r
+ co->co_firstlineno = firstlineno;\r
+ Py_INCREF(lnotab);\r
+ co->co_lnotab = lnotab;\r
+ co->co_zombieframe = NULL;\r
+ co->co_weakreflist = NULL;\r
+ }\r
+ return co;\r
+}\r
+\r
+PyCodeObject *\r
+PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno)\r
+{\r
+ static PyObject *emptystring = NULL;\r
+ static PyObject *nulltuple = NULL;\r
+ PyObject *filename_ob = NULL;\r
+ PyObject *funcname_ob = NULL;\r
+ PyCodeObject *result = NULL;\r
+ if (emptystring == NULL) {\r
+ emptystring = PyString_FromString("");\r
+ if (emptystring == NULL)\r
+ goto failed;\r
+ }\r
+ if (nulltuple == NULL) {\r
+ nulltuple = PyTuple_New(0);\r
+ if (nulltuple == NULL)\r
+ goto failed;\r
+ }\r
+ funcname_ob = PyString_FromString(funcname);\r
+ if (funcname_ob == NULL)\r
+ goto failed;\r
+ filename_ob = PyString_FromString(filename);\r
+ if (filename_ob == NULL)\r
+ goto failed;\r
+\r
+ result = PyCode_New(0, /* argcount */\r
+ 0, /* nlocals */\r
+ 0, /* stacksize */\r
+ 0, /* flags */\r
+ emptystring, /* code */\r
+ nulltuple, /* consts */\r
+ nulltuple, /* names */\r
+ nulltuple, /* varnames */\r
+ nulltuple, /* freevars */\r
+ nulltuple, /* cellvars */\r
+ filename_ob, /* filename */\r
+ funcname_ob, /* name */\r
+ firstlineno, /* firstlineno */\r
+ emptystring /* lnotab */\r
+ );\r
+\r
+failed:\r
+ Py_XDECREF(funcname_ob);\r
+ Py_XDECREF(filename_ob);\r
+ return result;\r
+}\r
+\r
+#define OFF(x) offsetof(PyCodeObject, x)\r
+\r
+static PyMemberDef code_memberlist[] = {\r
+ {"co_argcount", T_INT, OFF(co_argcount), READONLY},\r
+ {"co_nlocals", T_INT, OFF(co_nlocals), READONLY},\r
+ {"co_stacksize",T_INT, OFF(co_stacksize), READONLY},\r
+ {"co_flags", T_INT, OFF(co_flags), READONLY},\r
+ {"co_code", T_OBJECT, OFF(co_code), READONLY},\r
+ {"co_consts", T_OBJECT, OFF(co_consts), READONLY},\r
+ {"co_names", T_OBJECT, OFF(co_names), READONLY},\r
+ {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY},\r
+ {"co_freevars", T_OBJECT, OFF(co_freevars), READONLY},\r
+ {"co_cellvars", T_OBJECT, OFF(co_cellvars), READONLY},\r
+ {"co_filename", T_OBJECT, OFF(co_filename), READONLY},\r
+ {"co_name", T_OBJECT, OFF(co_name), READONLY},\r
+ {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY},\r
+ {"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY},\r
+ {NULL} /* Sentinel */\r
+};\r
+\r
+/* Helper for code_new: return a shallow copy of a tuple that is\r
+ guaranteed to contain exact strings, by converting string subclasses\r
+ to exact strings and complaining if a non-string is found. */\r
+static PyObject*\r
+validate_and_copy_tuple(PyObject *tup)\r
+{\r
+ PyObject *newtuple;\r
+ PyObject *item;\r
+ Py_ssize_t i, len;\r
+\r
+ len = PyTuple_GET_SIZE(tup);\r
+ newtuple = PyTuple_New(len);\r
+ if (newtuple == NULL)\r
+ return NULL;\r
+\r
+ for (i = 0; i < len; i++) {\r
+ item = PyTuple_GET_ITEM(tup, i);\r
+ if (PyString_CheckExact(item)) {\r
+ Py_INCREF(item);\r
+ }\r
+ else if (!PyString_Check(item)) {\r
+ PyErr_Format(\r
+ PyExc_TypeError,\r
+ "name tuples must contain only "\r
+ "strings, not '%.500s'",\r
+ item->ob_type->tp_name);\r
+ Py_DECREF(newtuple);\r
+ return NULL;\r
+ }\r
+ else {\r
+ item = PyString_FromStringAndSize(\r
+ PyString_AS_STRING(item),\r
+ PyString_GET_SIZE(item));\r
+ if (item == NULL) {\r
+ Py_DECREF(newtuple);\r
+ return NULL;\r
+ }\r
+ }\r
+ PyTuple_SET_ITEM(newtuple, i, item);\r
+ }\r
+\r
+ return newtuple;\r
+}\r
+\r
+PyDoc_STRVAR(code_doc,\r
+"code(argcount, nlocals, stacksize, flags, codestring, constants, names,\n\\r
+ varnames, filename, name, firstlineno, lnotab[, freevars[, cellvars]])\n\\r
+\n\\r
+Create a code object. Not for the faint of heart.");\r
+\r
+static PyObject *\r
+code_new(PyTypeObject *type, PyObject *args, PyObject *kw)\r
+{\r
+ int argcount;\r
+ int nlocals;\r
+ int stacksize;\r
+ int flags;\r
+ PyObject *co = NULL;\r
+ PyObject *code;\r
+ PyObject *consts;\r
+ PyObject *names, *ournames = NULL;\r
+ PyObject *varnames, *ourvarnames = NULL;\r
+ PyObject *freevars = NULL, *ourfreevars = NULL;\r
+ PyObject *cellvars = NULL, *ourcellvars = NULL;\r
+ PyObject *filename;\r
+ PyObject *name;\r
+ int firstlineno;\r
+ PyObject *lnotab;\r
+\r
+ if (!PyArg_ParseTuple(args, "iiiiSO!O!O!SSiS|O!O!:code",\r
+ &argcount, &nlocals, &stacksize, &flags,\r
+ &code,\r
+ &PyTuple_Type, &consts,\r
+ &PyTuple_Type, &names,\r
+ &PyTuple_Type, &varnames,\r
+ &filename, &name,\r
+ &firstlineno, &lnotab,\r
+ &PyTuple_Type, &freevars,\r
+ &PyTuple_Type, &cellvars))\r
+ return NULL;\r
+\r
+ if (argcount < 0) {\r
+ PyErr_SetString(\r
+ PyExc_ValueError,\r
+ "code: argcount must not be negative");\r
+ goto cleanup;\r
+ }\r
+\r
+ if (nlocals < 0) {\r
+ PyErr_SetString(\r
+ PyExc_ValueError,\r
+ "code: nlocals must not be negative");\r
+ goto cleanup;\r
+ }\r
+\r
+ ournames = validate_and_copy_tuple(names);\r
+ if (ournames == NULL)\r
+ goto cleanup;\r
+ ourvarnames = validate_and_copy_tuple(varnames);\r
+ if (ourvarnames == NULL)\r
+ goto cleanup;\r
+ if (freevars)\r
+ ourfreevars = validate_and_copy_tuple(freevars);\r
+ else\r
+ ourfreevars = PyTuple_New(0);\r
+ if (ourfreevars == NULL)\r
+ goto cleanup;\r
+ if (cellvars)\r
+ ourcellvars = validate_and_copy_tuple(cellvars);\r
+ else\r
+ ourcellvars = PyTuple_New(0);\r
+ if (ourcellvars == NULL)\r
+ goto cleanup;\r
+\r
+ co = (PyObject *)PyCode_New(argcount, nlocals, stacksize, flags,\r
+ code, consts, ournames, ourvarnames,\r
+ ourfreevars, ourcellvars, filename,\r
+ name, firstlineno, lnotab);\r
+ cleanup:\r
+ Py_XDECREF(ournames);\r
+ Py_XDECREF(ourvarnames);\r
+ Py_XDECREF(ourfreevars);\r
+ Py_XDECREF(ourcellvars);\r
+ return co;\r
+}\r
+\r
+static void\r
+code_dealloc(PyCodeObject *co)\r
+{\r
+ Py_XDECREF(co->co_code);\r
+ Py_XDECREF(co->co_consts);\r
+ Py_XDECREF(co->co_names);\r
+ Py_XDECREF(co->co_varnames);\r
+ Py_XDECREF(co->co_freevars);\r
+ Py_XDECREF(co->co_cellvars);\r
+ Py_XDECREF(co->co_filename);\r
+ Py_XDECREF(co->co_name);\r
+ Py_XDECREF(co->co_lnotab);\r
+ if (co->co_zombieframe != NULL)\r
+ PyObject_GC_Del(co->co_zombieframe);\r
+ if (co->co_weakreflist != NULL)\r
+ PyObject_ClearWeakRefs((PyObject*)co);\r
+ PyObject_DEL(co);\r
+}\r
+\r
+static PyObject *\r
+code_repr(PyCodeObject *co)\r
+{\r
+ char buf[500];\r
+ int lineno = -1;\r
+ char *filename = "???";\r
+ char *name = "???";\r
+\r
+ if (co->co_firstlineno != 0)\r
+ lineno = co->co_firstlineno;\r
+ if (co->co_filename && PyString_Check(co->co_filename))\r
+ filename = PyString_AS_STRING(co->co_filename);\r
+ if (co->co_name && PyString_Check(co->co_name))\r
+ name = PyString_AS_STRING(co->co_name);\r
+ PyOS_snprintf(buf, sizeof(buf),\r
+ "<code object %.100s at %p, file \"%.300s\", line %d>",\r
+ name, co, filename, lineno);\r
+ return PyString_FromString(buf);\r
+}\r
+\r
+static int\r
+code_compare(PyCodeObject *co, PyCodeObject *cp)\r
+{\r
+ int cmp;\r
+ cmp = PyObject_Compare(co->co_name, cp->co_name);\r
+ if (cmp) return cmp;\r
+ cmp = co->co_argcount - cp->co_argcount;\r
+ if (cmp) goto normalize;\r
+ cmp = co->co_nlocals - cp->co_nlocals;\r
+ if (cmp) goto normalize;\r
+ cmp = co->co_flags - cp->co_flags;\r
+ if (cmp) goto normalize;\r
+ cmp = co->co_firstlineno - cp->co_firstlineno;\r
+ if (cmp) goto normalize;\r
+ cmp = PyObject_Compare(co->co_code, cp->co_code);\r
+ if (cmp) return cmp;\r
+ cmp = PyObject_Compare(co->co_consts, cp->co_consts);\r
+ if (cmp) return cmp;\r
+ cmp = PyObject_Compare(co->co_names, cp->co_names);\r
+ if (cmp) return cmp;\r
+ cmp = PyObject_Compare(co->co_varnames, cp->co_varnames);\r
+ if (cmp) return cmp;\r
+ cmp = PyObject_Compare(co->co_freevars, cp->co_freevars);\r
+ if (cmp) return cmp;\r
+ cmp = PyObject_Compare(co->co_cellvars, cp->co_cellvars);\r
+ return cmp;\r
+\r
+ normalize:\r
+ if (cmp > 0)\r
+ return 1;\r
+ else if (cmp < 0)\r
+ return -1;\r
+ else\r
+ return 0;\r
+}\r
+\r
+static PyObject *\r
+code_richcompare(PyObject *self, PyObject *other, int op)\r
+{\r
+ PyCodeObject *co, *cp;\r
+ int eq;\r
+ PyObject *res;\r
+\r
+ if ((op != Py_EQ && op != Py_NE) ||\r
+ !PyCode_Check(self) ||\r
+ !PyCode_Check(other)) {\r
+\r
+ /* Py3K warning if types are not equal and comparison\r
+ isn't == or != */\r
+ if (PyErr_WarnPy3k("code inequality comparisons not supported "\r
+ "in 3.x", 1) < 0) {\r
+ return NULL;\r
+ }\r
+\r
+ Py_INCREF(Py_NotImplemented);\r
+ return Py_NotImplemented;\r
+ }\r
+\r
+ co = (PyCodeObject *)self;\r
+ cp = (PyCodeObject *)other;\r
+\r
+ eq = PyObject_RichCompareBool(co->co_name, cp->co_name, Py_EQ);\r
+ if (eq <= 0) goto unequal;\r
+ eq = co->co_argcount == cp->co_argcount;\r
+ if (!eq) goto unequal;\r
+ eq = co->co_nlocals == cp->co_nlocals;\r
+ if (!eq) goto unequal;\r
+ eq = co->co_flags == cp->co_flags;\r
+ if (!eq) goto unequal;\r
+ eq = co->co_firstlineno == cp->co_firstlineno;\r
+ if (!eq) goto unequal;\r
+ eq = PyObject_RichCompareBool(co->co_code, cp->co_code, Py_EQ);\r
+ if (eq <= 0) goto unequal;\r
+ eq = PyObject_RichCompareBool(co->co_consts, cp->co_consts, Py_EQ);\r
+ if (eq <= 0) goto unequal;\r
+ eq = PyObject_RichCompareBool(co->co_names, cp->co_names, Py_EQ);\r
+ if (eq <= 0) goto unequal;\r
+ eq = PyObject_RichCompareBool(co->co_varnames, cp->co_varnames, Py_EQ);\r
+ if (eq <= 0) goto unequal;\r
+ eq = PyObject_RichCompareBool(co->co_freevars, cp->co_freevars, Py_EQ);\r
+ if (eq <= 0) goto unequal;\r
+ eq = PyObject_RichCompareBool(co->co_cellvars, cp->co_cellvars, Py_EQ);\r
+ if (eq <= 0) goto unequal;\r
+\r
+ if (op == Py_EQ)\r
+ res = Py_True;\r
+ else\r
+ res = Py_False;\r
+ goto done;\r
+\r
+ unequal:\r
+ if (eq < 0)\r
+ return NULL;\r
+ if (op == Py_NE)\r
+ res = Py_True;\r
+ else\r
+ res = Py_False;\r
+\r
+ done:\r
+ Py_INCREF(res);\r
+ return res;\r
+}\r
+\r
+static long\r
+code_hash(PyCodeObject *co)\r
+{\r
+ long h, h0, h1, h2, h3, h4, h5, h6;\r
+ h0 = PyObject_Hash(co->co_name);\r
+ if (h0 == -1) return -1;\r
+ h1 = PyObject_Hash(co->co_code);\r
+ if (h1 == -1) return -1;\r
+ h2 = PyObject_Hash(co->co_consts);\r
+ if (h2 == -1) return -1;\r
+ h3 = PyObject_Hash(co->co_names);\r
+ if (h3 == -1) return -1;\r
+ h4 = PyObject_Hash(co->co_varnames);\r
+ if (h4 == -1) return -1;\r
+ h5 = PyObject_Hash(co->co_freevars);\r
+ if (h5 == -1) return -1;\r
+ h6 = PyObject_Hash(co->co_cellvars);\r
+ if (h6 == -1) return -1;\r
+ h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^\r
+ co->co_argcount ^ co->co_nlocals ^ co->co_flags;\r
+ if (h == -1) h = -2;\r
+ return h;\r
+}\r
+\r
+/* XXX code objects need to participate in GC? */\r
+\r
+PyTypeObject PyCode_Type = {\r
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)\r
+ "code",\r
+ sizeof(PyCodeObject),\r
+ 0,\r
+ (destructor)code_dealloc, /* tp_dealloc */\r
+ 0, /* tp_print */\r
+ 0, /* tp_getattr */\r
+ 0, /* tp_setattr */\r
+ (cmpfunc)code_compare, /* tp_compare */\r
+ (reprfunc)code_repr, /* tp_repr */\r
+ 0, /* tp_as_number */\r
+ 0, /* tp_as_sequence */\r
+ 0, /* tp_as_mapping */\r
+ (hashfunc)code_hash, /* tp_hash */\r
+ 0, /* tp_call */\r
+ 0, /* tp_str */\r
+ PyObject_GenericGetAttr, /* tp_getattro */\r
+ 0, /* tp_setattro */\r
+ 0, /* tp_as_buffer */\r
+ Py_TPFLAGS_DEFAULT, /* tp_flags */\r
+ code_doc, /* tp_doc */\r
+ 0, /* tp_traverse */\r
+ 0, /* tp_clear */\r
+ code_richcompare, /* tp_richcompare */\r
+ offsetof(PyCodeObject, co_weakreflist), /* tp_weaklistoffset */\r
+ 0, /* tp_iter */\r
+ 0, /* tp_iternext */\r
+ 0, /* tp_methods */\r
+ code_memberlist, /* tp_members */\r
+ 0, /* tp_getset */\r
+ 0, /* tp_base */\r
+ 0, /* tp_dict */\r
+ 0, /* tp_descr_get */\r
+ 0, /* tp_descr_set */\r
+ 0, /* tp_dictoffset */\r
+ 0, /* tp_init */\r
+ 0, /* tp_alloc */\r
+ code_new, /* tp_new */\r
+};\r
+\r
+/* Use co_lnotab to compute the line number from a bytecode index, addrq. See\r
+ lnotab_notes.txt for the details of the lnotab representation.\r
+*/\r
+\r
+int\r
+PyCode_Addr2Line(PyCodeObject *co, int addrq)\r
+{\r
+ int size = PyString_Size(co->co_lnotab) / 2;\r
+ unsigned char *p = (unsigned char*)PyString_AsString(co->co_lnotab);\r
+ int line = co->co_firstlineno;\r
+ int addr = 0;\r
+ while (--size >= 0) {\r
+ addr += *p++;\r
+ if (addr > addrq)\r
+ break;\r
+ line += *p++;\r
+ }\r
+ return line;\r
+}\r
+\r
+/* Update *bounds to describe the first and one-past-the-last instructions in\r
+ the same line as lasti. Return the number of that line. */\r
+int\r
+_PyCode_CheckLineNumber(PyCodeObject* co, int lasti, PyAddrPair *bounds)\r
+{\r
+ int size, addr, line;\r
+ unsigned char* p;\r
+\r
+ p = (unsigned char*)PyString_AS_STRING(co->co_lnotab);\r
+ size = PyString_GET_SIZE(co->co_lnotab) / 2;\r
+\r
+ addr = 0;\r
+ line = co->co_firstlineno;\r
+ assert(line > 0);\r
+\r
+ /* possible optimization: if f->f_lasti == instr_ub\r
+ (likely to be a common case) then we already know\r
+ instr_lb -- if we stored the matching value of p\r
+ somwhere we could skip the first while loop. */\r
+\r
+ /* See lnotab_notes.txt for the description of\r
+ co_lnotab. A point to remember: increments to p\r
+ come in (addr, line) pairs. */\r
+\r
+ bounds->ap_lower = 0;\r
+ while (size > 0) {\r
+ if (addr + *p > lasti)\r
+ break;\r
+ addr += *p++;\r
+ if (*p)\r
+ bounds->ap_lower = addr;\r
+ line += *p++;\r
+ --size;\r
+ }\r
+\r
+ if (size > 0) {\r
+ while (--size >= 0) {\r
+ addr += *p++;\r
+ if (*p++)\r
+ break;\r
+ }\r
+ bounds->ap_upper = addr;\r
+ }\r
+ else {\r
+ bounds->ap_upper = INT_MAX;\r
+ }\r
+\r
+ return line;\r
+}\r