Mercurial > cpython-lru_cache-weakref
annotate Objects/codeobject.c @ 42334:e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/p3yk
................
r56477 | martin.v.loewis | 2007-07-21 09:04:38 +0200 (Sa, 21 Jul 2007) | 11 lines
Merged revisions 56466-56476 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r56476 | martin.v.loewis | 2007-07-21 08:55:02 +0200 (Sa, 21 Jul 2007) | 4 lines
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
backwards compatibility. Add Py_Refcnt, Py_Type, Py_Size, and
PyVarObject_HEAD_INIT.
........
................
r56478 | martin.v.loewis | 2007-07-21 09:47:23 +0200 (Sa, 21 Jul 2007) | 2 lines
PEP 3123: Use proper C inheritance for PyObject.
................
r56479 | martin.v.loewis | 2007-07-21 10:06:55 +0200 (Sa, 21 Jul 2007) | 3 lines
Add longintrepr.h to Python.h, so that the compiler can
see that PyFalse is really some kind of PyObject*.
................
r56480 | martin.v.loewis | 2007-07-21 10:47:18 +0200 (Sa, 21 Jul 2007) | 2 lines
Qualify SHIFT, MASK, BASE.
................
r56482 | martin.v.loewis | 2007-07-21 19:10:57 +0200 (Sa, 21 Jul 2007) | 2 lines
Correctly refer to _ob_next.
................
| author | Martin v. Löwis <martin@v.loewis.de> |
|---|---|
| date | Sat, 21 Jul 2007 17:22:18 +0000 |
| parents | cd125fe83051 |
| children | 01ee6311ba46 |
| rev | line source |
|---|---|
| 35742 | 1 #include "Python.h" |
| 2 #include "code.h" | |
| 3 #include "structmember.h" | |
| 4 | |
| 5 #define NAME_CHARS \ | |
| 6 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz" | |
| 7 | |
| 8 /* all_name_chars(s): true iff all chars in s are valid NAME_CHARS */ | |
| 9 | |
| 10 static int | |
| 11 all_name_chars(unsigned char *s) | |
| 12 { | |
| 13 static char ok_name_char[256]; | |
| 14 static unsigned char *name_chars = (unsigned char *)NAME_CHARS; | |
| 15 | |
| 16 if (ok_name_char[*name_chars] == 0) { | |
| 17 unsigned char *p; | |
| 18 for (p = name_chars; *p; p++) | |
| 19 ok_name_char[*p] = 1; | |
| 20 } | |
| 21 while (*s) { | |
| 22 if (ok_name_char[*s++] == 0) | |
| 23 return 0; | |
| 24 } | |
| 25 return 1; | |
| 26 } | |
| 27 | |
| 28 static void | |
| 29 intern_strings(PyObject *tuple) | |
| 30 { | |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35742
diff
changeset
|
31 Py_ssize_t i; |
| 35742 | 32 |
| 33 for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) { | |
| 34 PyObject *v = PyTuple_GET_ITEM(tuple, i); | |
|
42013
cd125fe83051
Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents:
41962
diff
changeset
|
35 if (v == NULL || !PyUnicode_CheckExact(v)) { |
| 35742 | 36 Py_FatalError("non-string found in code slot"); |
| 37 } | |
|
42013
cd125fe83051
Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents:
41962
diff
changeset
|
38 PyUnicode_InternInPlace(&PyTuple_GET_ITEM(tuple, i)); |
| 35742 | 39 } |
| 40 } | |
| 41 | |
| 42 | |
| 43 PyCodeObject * | |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39658
diff
changeset
|
44 PyCode_New(int argcount, int kwonlyargcount, |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39658
diff
changeset
|
45 int nlocals, int stacksize, int flags, |
| 35742 | 46 PyObject *code, PyObject *consts, PyObject *names, |
| 47 PyObject *varnames, PyObject *freevars, PyObject *cellvars, | |
| 48 PyObject *filename, PyObject *name, int firstlineno, | |
| 49 PyObject *lnotab) | |
| 50 { | |
| 51 PyCodeObject *co; | |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35742
diff
changeset
|
52 Py_ssize_t i; |
| 35742 | 53 /* Check argument types */ |
| 54 if (argcount < 0 || nlocals < 0 || | |
| 55 code == NULL || | |
| 56 consts == NULL || !PyTuple_Check(consts) || | |
| 57 names == NULL || !PyTuple_Check(names) || | |
| 58 varnames == NULL || !PyTuple_Check(varnames) || | |
| 59 freevars == NULL || !PyTuple_Check(freevars) || | |
| 60 cellvars == NULL || !PyTuple_Check(cellvars) || | |
|
42013
cd125fe83051
Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents:
41962
diff
changeset
|
61 name == NULL || (!PyString_Check(name) && !PyUnicode_Check(name)) || |
| 35742 | 62 filename == NULL || !PyString_Check(filename) || |
| 63 lnotab == NULL || !PyString_Check(lnotab) || | |
| 64 !PyObject_CheckReadBuffer(code)) { | |
| 65 PyErr_BadInternalCall(); | |
| 66 return NULL; | |
| 67 } | |
| 68 intern_strings(names); | |
| 69 intern_strings(varnames); | |
| 70 intern_strings(freevars); | |
| 71 intern_strings(cellvars); | |
| 72 /* Intern selected string constants */ | |
| 73 for (i = PyTuple_Size(consts); --i >= 0; ) { | |
| 74 PyObject *v = PyTuple_GetItem(consts, i); | |
| 75 if (!PyString_Check(v)) | |
| 76 continue; | |
| 77 if (!all_name_chars((unsigned char *)PyString_AS_STRING(v))) | |
| 78 continue; | |
| 79 PyString_InternInPlace(&PyTuple_GET_ITEM(consts, i)); | |
| 80 } | |
| 81 co = PyObject_NEW(PyCodeObject, &PyCode_Type); | |
| 82 if (co != NULL) { | |
| 83 co->co_argcount = argcount; | |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39658
diff
changeset
|
84 co->co_kwonlyargcount = kwonlyargcount; |
| 35742 | 85 co->co_nlocals = nlocals; |
| 86 co->co_stacksize = stacksize; | |
| 87 co->co_flags = flags; | |
| 88 Py_INCREF(code); | |
| 89 co->co_code = code; | |
| 90 Py_INCREF(consts); | |
| 91 co->co_consts = consts; | |
| 92 Py_INCREF(names); | |
| 93 co->co_names = names; | |
| 94 Py_INCREF(varnames); | |
| 95 co->co_varnames = varnames; | |
| 96 Py_INCREF(freevars); | |
| 97 co->co_freevars = freevars; | |
| 98 Py_INCREF(cellvars); | |
| 99 co->co_cellvars = cellvars; | |
| 100 Py_INCREF(filename); | |
| 101 co->co_filename = filename; | |
| 102 Py_INCREF(name); | |
| 103 co->co_name = name; | |
| 104 co->co_firstlineno = firstlineno; | |
| 105 Py_INCREF(lnotab); | |
| 106 co->co_lnotab = lnotab; | |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
107 co->co_zombieframe = NULL; |
| 35742 | 108 } |
| 109 return co; | |
| 110 } | |
| 111 | |
| 112 | |
| 113 #define OFF(x) offsetof(PyCodeObject, x) | |
| 114 | |
| 115 static PyMemberDef code_memberlist[] = { | |
| 116 {"co_argcount", T_INT, OFF(co_argcount), READONLY}, | |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39658
diff
changeset
|
117 {"co_kwonlyargcount", T_INT, OFF(co_kwonlyargcount), READONLY}, |
| 35742 | 118 {"co_nlocals", T_INT, OFF(co_nlocals), READONLY}, |
| 119 {"co_stacksize",T_INT, OFF(co_stacksize), READONLY}, | |
| 120 {"co_flags", T_INT, OFF(co_flags), READONLY}, | |
| 121 {"co_code", T_OBJECT, OFF(co_code), READONLY}, | |
| 122 {"co_consts", T_OBJECT, OFF(co_consts), READONLY}, | |
| 123 {"co_names", T_OBJECT, OFF(co_names), READONLY}, | |
| 124 {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY}, | |
| 125 {"co_freevars", T_OBJECT, OFF(co_freevars), READONLY}, | |
| 126 {"co_cellvars", T_OBJECT, OFF(co_cellvars), READONLY}, | |
| 127 {"co_filename", T_OBJECT, OFF(co_filename), READONLY}, | |
| 128 {"co_name", T_OBJECT, OFF(co_name), READONLY}, | |
| 129 {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY}, | |
| 130 {"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY}, | |
| 131 {NULL} /* Sentinel */ | |
| 132 }; | |
| 133 | |
| 134 /* Helper for code_new: return a shallow copy of a tuple that is | |
| 135 guaranteed to contain exact strings, by converting string subclasses | |
| 136 to exact strings and complaining if a non-string is found. */ | |
| 137 static PyObject* | |
| 138 validate_and_copy_tuple(PyObject *tup) | |
| 139 { | |
| 140 PyObject *newtuple; | |
| 141 PyObject *item; | |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35742
diff
changeset
|
142 Py_ssize_t i, len; |
| 35742 | 143 |
| 144 len = PyTuple_GET_SIZE(tup); | |
| 145 newtuple = PyTuple_New(len); | |
| 146 if (newtuple == NULL) | |
| 147 return NULL; | |
| 148 | |
| 149 for (i = 0; i < len; i++) { | |
| 150 item = PyTuple_GET_ITEM(tup, i); | |
|
42013
cd125fe83051
Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents:
41962
diff
changeset
|
151 if (PyUnicode_CheckExact(item)) { |
| 35742 | 152 Py_INCREF(item); |
| 153 } | |
|
42013
cd125fe83051
Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents:
41962
diff
changeset
|
154 else if (!PyUnicode_Check(item)) { |
| 35742 | 155 PyErr_Format( |
| 156 PyExc_TypeError, | |
| 157 "name tuples must contain only " | |
| 158 "strings, not '%.500s'", | |
| 159 item->ob_type->tp_name); | |
| 160 Py_DECREF(newtuple); | |
| 161 return NULL; | |
| 162 } | |
| 163 else { | |
|
42013
cd125fe83051
Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents:
41962
diff
changeset
|
164 item = PyUnicode_FromUnicode( |
|
cd125fe83051
Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents:
41962
diff
changeset
|
165 PyUnicode_AS_UNICODE(item), |
|
cd125fe83051
Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents:
41962
diff
changeset
|
166 PyUnicode_GET_SIZE(item)); |
| 35742 | 167 if (item == NULL) { |
| 168 Py_DECREF(newtuple); | |
| 169 return NULL; | |
| 170 } | |
| 171 } | |
| 172 PyTuple_SET_ITEM(newtuple, i, item); | |
| 173 } | |
| 174 | |
| 175 return newtuple; | |
| 176 } | |
| 177 | |
| 178 PyDoc_STRVAR(code_doc, | |
| 179 "code(argcount, nlocals, stacksize, flags, codestring, constants, names,\n\ | |
| 180 varnames, filename, name, firstlineno, lnotab[, freevars[, cellvars]])\n\ | |
| 181 \n\ | |
| 182 Create a code object. Not for the faint of heart."); | |
| 183 | |
| 184 static PyObject * | |
| 185 code_new(PyTypeObject *type, PyObject *args, PyObject *kw) | |
| 186 { | |
| 187 int argcount; | |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39658
diff
changeset
|
188 int kwonlyargcount; |
| 35742 | 189 int nlocals; |
| 190 int stacksize; | |
| 191 int flags; | |
| 192 PyObject *co = NULL; | |
| 193 PyObject *code; | |
| 194 PyObject *consts; | |
| 195 PyObject *names, *ournames = NULL; | |
| 196 PyObject *varnames, *ourvarnames = NULL; | |
| 197 PyObject *freevars = NULL, *ourfreevars = NULL; | |
| 198 PyObject *cellvars = NULL, *ourcellvars = NULL; | |
| 199 PyObject *filename; | |
| 200 PyObject *name; | |
| 201 int firstlineno; | |
| 202 PyObject *lnotab; | |
| 203 | |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39658
diff
changeset
|
204 if (!PyArg_ParseTuple(args, "iiiiiSO!O!O!SSiS|O!O!:code", |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39658
diff
changeset
|
205 &argcount, &kwonlyargcount, |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39658
diff
changeset
|
206 &nlocals, &stacksize, &flags, |
| 35742 | 207 &code, |
| 208 &PyTuple_Type, &consts, | |
| 209 &PyTuple_Type, &names, | |
| 210 &PyTuple_Type, &varnames, | |
| 211 &filename, &name, | |
| 212 &firstlineno, &lnotab, | |
| 213 &PyTuple_Type, &freevars, | |
| 214 &PyTuple_Type, &cellvars)) | |
| 215 return NULL; | |
| 216 | |
| 217 if (argcount < 0) { | |
| 218 PyErr_SetString( | |
| 219 PyExc_ValueError, | |
| 220 "code: argcount must not be negative"); | |
| 221 goto cleanup; | |
| 222 } | |
| 223 | |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39658
diff
changeset
|
224 if (kwonlyargcount < 0) { |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39658
diff
changeset
|
225 PyErr_SetString( |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39658
diff
changeset
|
226 PyExc_ValueError, |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39658
diff
changeset
|
227 "code: kwonlyargcount must not be negative"); |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39658
diff
changeset
|
228 goto cleanup; |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39658
diff
changeset
|
229 } |
| 35742 | 230 if (nlocals < 0) { |
| 231 PyErr_SetString( | |
| 232 PyExc_ValueError, | |
| 233 "code: nlocals must not be negative"); | |
| 234 goto cleanup; | |
| 235 } | |
| 236 | |
| 237 ournames = validate_and_copy_tuple(names); | |
| 238 if (ournames == NULL) | |
| 239 goto cleanup; | |
| 240 ourvarnames = validate_and_copy_tuple(varnames); | |
| 241 if (ourvarnames == NULL) | |
| 242 goto cleanup; | |
| 243 if (freevars) | |
| 244 ourfreevars = validate_and_copy_tuple(freevars); | |
| 245 else | |
| 246 ourfreevars = PyTuple_New(0); | |
| 247 if (ourfreevars == NULL) | |
| 248 goto cleanup; | |
| 249 if (cellvars) | |
| 250 ourcellvars = validate_and_copy_tuple(cellvars); | |
| 251 else | |
| 252 ourcellvars = PyTuple_New(0); | |
| 253 if (ourcellvars == NULL) | |
| 254 goto cleanup; | |
| 255 | |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39658
diff
changeset
|
256 co = (PyObject *)PyCode_New(argcount, kwonlyargcount, |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39658
diff
changeset
|
257 nlocals, stacksize, flags, |
| 35742 | 258 code, consts, ournames, ourvarnames, |
| 259 ourfreevars, ourcellvars, filename, | |
| 260 name, firstlineno, lnotab); | |
| 261 cleanup: | |
| 262 Py_XDECREF(ournames); | |
| 263 Py_XDECREF(ourvarnames); | |
| 264 Py_XDECREF(ourfreevars); | |
| 265 Py_XDECREF(ourcellvars); | |
| 266 return co; | |
| 267 } | |
| 268 | |
| 269 static void | |
| 270 code_dealloc(PyCodeObject *co) | |
| 271 { | |
| 272 Py_XDECREF(co->co_code); | |
| 273 Py_XDECREF(co->co_consts); | |
| 274 Py_XDECREF(co->co_names); | |
| 275 Py_XDECREF(co->co_varnames); | |
| 276 Py_XDECREF(co->co_freevars); | |
| 277 Py_XDECREF(co->co_cellvars); | |
| 278 Py_XDECREF(co->co_filename); | |
| 279 Py_XDECREF(co->co_name); | |
| 280 Py_XDECREF(co->co_lnotab); | |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
281 if (co->co_zombieframe != NULL) |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
282 PyObject_GC_Del(co->co_zombieframe); |
| 35742 | 283 PyObject_DEL(co); |
| 284 } | |
| 285 | |
| 286 static PyObject * | |
| 287 code_repr(PyCodeObject *co) | |
| 288 { | |
| 289 int lineno = -1; | |
| 290 char *filename = "???"; | |
| 291 char *name = "???"; | |
| 292 | |
| 293 if (co->co_firstlineno != 0) | |
| 294 lineno = co->co_firstlineno; | |
| 295 if (co->co_filename && PyString_Check(co->co_filename)) | |
| 296 filename = PyString_AS_STRING(co->co_filename); | |
| 297 if (co->co_name && PyString_Check(co->co_name)) | |
| 298 name = PyString_AS_STRING(co->co_name); | |
|
41962
11930dcf9d4f
Use PyUnicode_FromFormat() directly.
Walter Dörwald <walter@livinglogic.de>
parents:
41769
diff
changeset
|
299 return PyUnicode_FromFormat( |
|
11930dcf9d4f
Use PyUnicode_FromFormat() directly.
Walter Dörwald <walter@livinglogic.de>
parents:
41769
diff
changeset
|
300 "<code object %.100s at %p, file \"%.300s\", line %d>", |
|
11930dcf9d4f
Use PyUnicode_FromFormat() directly.
Walter Dörwald <walter@livinglogic.de>
parents:
41769
diff
changeset
|
301 name, co, filename, lineno); |
| 35742 | 302 } |
| 303 | |
|
39644
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
304 static PyObject * |
|
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
305 code_richcompare(PyObject *self, PyObject *other, int op) |
| 35742 | 306 { |
|
39650
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
307 PyCodeObject *co, *cp; |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
308 int eq; |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
309 PyObject *res; |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
310 |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
311 if ((op != Py_EQ && op != Py_NE) || |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
312 !PyCode_Check(self) || |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
313 !PyCode_Check(other)) { |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
314 Py_INCREF(Py_NotImplemented); |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
315 return Py_NotImplemented; |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
316 } |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
317 |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
318 co = (PyCodeObject *)self; |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
319 cp = (PyCodeObject *)other; |
|
39644
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
320 |
|
39658
a5048c8d8f33
Fix a bunch of compiler warnings. In at least one case these were serious bugs!
Guido van Rossum <guido@python.org>
parents:
39650
diff
changeset
|
321 eq = PyObject_RichCompareBool(co->co_name, cp->co_name, Py_EQ); |
|
39650
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
322 if (eq <= 0) goto unequal; |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
323 eq = co->co_argcount == cp->co_argcount; |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
324 if (!eq) goto unequal; |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39658
diff
changeset
|
325 eq = co->co_kwonlyargcount == cp->co_kwonlyargcount; |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39658
diff
changeset
|
326 if (!eq) goto unequal; |
|
39650
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
327 eq = co->co_nlocals == cp->co_nlocals; |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
328 if (!eq) goto unequal; |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
329 eq = co->co_flags == cp->co_flags; |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
330 if (!eq) goto unequal; |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
331 eq = co->co_firstlineno == cp->co_firstlineno; |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
332 if (!eq) goto unequal; |
|
39658
a5048c8d8f33
Fix a bunch of compiler warnings. In at least one case these were serious bugs!
Guido van Rossum <guido@python.org>
parents:
39650
diff
changeset
|
333 eq = PyObject_RichCompareBool(co->co_code, cp->co_code, Py_EQ); |
|
39650
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
334 if (eq <= 0) goto unequal; |
|
39658
a5048c8d8f33
Fix a bunch of compiler warnings. In at least one case these were serious bugs!
Guido van Rossum <guido@python.org>
parents:
39650
diff
changeset
|
335 eq = PyObject_RichCompareBool(co->co_consts, cp->co_consts, Py_EQ); |
|
39650
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
336 if (eq <= 0) goto unequal; |
|
39658
a5048c8d8f33
Fix a bunch of compiler warnings. In at least one case these were serious bugs!
Guido van Rossum <guido@python.org>
parents:
39650
diff
changeset
|
337 eq = PyObject_RichCompareBool(co->co_names, cp->co_names, Py_EQ); |
|
39650
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
338 if (eq <= 0) goto unequal; |
|
39658
a5048c8d8f33
Fix a bunch of compiler warnings. In at least one case these were serious bugs!
Guido van Rossum <guido@python.org>
parents:
39650
diff
changeset
|
339 eq = PyObject_RichCompareBool(co->co_varnames, cp->co_varnames, Py_EQ); |
|
39650
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
340 if (eq <= 0) goto unequal; |
|
39658
a5048c8d8f33
Fix a bunch of compiler warnings. In at least one case these were serious bugs!
Guido van Rossum <guido@python.org>
parents:
39650
diff
changeset
|
341 eq = PyObject_RichCompareBool(co->co_freevars, cp->co_freevars, Py_EQ); |
|
39650
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
342 if (eq <= 0) goto unequal; |
|
39658
a5048c8d8f33
Fix a bunch of compiler warnings. In at least one case these were serious bugs!
Guido van Rossum <guido@python.org>
parents:
39650
diff
changeset
|
343 eq = PyObject_RichCompareBool(co->co_cellvars, cp->co_cellvars, Py_EQ); |
|
39650
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
344 if (eq <= 0) goto unequal; |
| 35742 | 345 |
|
39650
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
346 if (op == Py_EQ) |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
347 res = Py_True; |
| 35742 | 348 else |
|
39650
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
349 res = Py_False; |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
350 goto done; |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
351 |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
352 unequal: |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
353 if (eq < 0) |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
354 return NULL; |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
355 if (op == Py_NE) |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
356 res = Py_True; |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
357 else |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
358 res = Py_False; |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
359 |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
360 done: |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
361 Py_INCREF(res); |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
362 return res; |
| 35742 | 363 } |
| 364 | |
| 365 static long | |
| 366 code_hash(PyCodeObject *co) | |
| 367 { | |
| 368 long h, h0, h1, h2, h3, h4, h5, h6; | |
| 369 h0 = PyObject_Hash(co->co_name); | |
| 370 if (h0 == -1) return -1; | |
| 371 h1 = PyObject_Hash(co->co_code); | |
| 372 if (h1 == -1) return -1; | |
| 373 h2 = PyObject_Hash(co->co_consts); | |
| 374 if (h2 == -1) return -1; | |
| 375 h3 = PyObject_Hash(co->co_names); | |
| 376 if (h3 == -1) return -1; | |
| 377 h4 = PyObject_Hash(co->co_varnames); | |
| 378 if (h4 == -1) return -1; | |
| 379 h5 = PyObject_Hash(co->co_freevars); | |
| 380 if (h5 == -1) return -1; | |
| 381 h6 = PyObject_Hash(co->co_cellvars); | |
| 382 if (h6 == -1) return -1; | |
| 383 h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^ | |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39658
diff
changeset
|
384 co->co_argcount ^ co->co_kwonlyargcount ^ |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39658
diff
changeset
|
385 co->co_nlocals ^ co->co_flags; |
| 35742 | 386 if (h == -1) h = -2; |
| 387 return h; | |
| 388 } | |
| 389 | |
| 390 /* XXX code objects need to participate in GC? */ | |
| 391 | |
| 392 PyTypeObject PyCode_Type = { | |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
42013
diff
changeset
|
393 PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 35742 | 394 "code", |
| 395 sizeof(PyCodeObject), | |
| 396 0, | |
| 397 (destructor)code_dealloc, /* tp_dealloc */ | |
| 398 0, /* tp_print */ | |
| 399 0, /* tp_getattr */ | |
| 400 0, /* tp_setattr */ | |
|
39644
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
401 0, /* tp_compare */ |
| 35742 | 402 (reprfunc)code_repr, /* tp_repr */ |
| 403 0, /* tp_as_number */ | |
| 404 0, /* tp_as_sequence */ | |
| 405 0, /* tp_as_mapping */ | |
|
39650
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
406 (hashfunc)code_hash, /* tp_hash */ |
| 35742 | 407 0, /* tp_call */ |
| 408 0, /* tp_str */ | |
| 409 PyObject_GenericGetAttr, /* tp_getattro */ | |
| 410 0, /* tp_setattro */ | |
| 411 0, /* tp_as_buffer */ | |
| 412 Py_TPFLAGS_DEFAULT, /* tp_flags */ | |
| 413 code_doc, /* tp_doc */ | |
| 414 0, /* tp_traverse */ | |
| 415 0, /* tp_clear */ | |
|
39650
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
416 code_richcompare, /* tp_richcompare */ |
| 35742 | 417 0, /* tp_weaklistoffset */ |
| 418 0, /* tp_iter */ | |
| 419 0, /* tp_iternext */ | |
| 420 0, /* tp_methods */ | |
| 421 code_memberlist, /* tp_members */ | |
| 422 0, /* tp_getset */ | |
| 423 0, /* tp_base */ | |
| 424 0, /* tp_dict */ | |
| 425 0, /* tp_descr_get */ | |
| 426 0, /* tp_descr_set */ | |
| 427 0, /* tp_dictoffset */ | |
| 428 0, /* tp_init */ | |
| 429 0, /* tp_alloc */ | |
| 430 code_new, /* tp_new */ | |
| 431 }; | |
| 432 | |
| 433 /* All about c_lnotab. | |
| 434 | |
| 435 c_lnotab is an array of unsigned bytes disguised as a Python string. In -O | |
| 436 mode, SET_LINENO opcodes aren't generated, and bytecode offsets are mapped | |
| 437 to source code line #s (when needed for tracebacks) via c_lnotab instead. | |
| 438 The array is conceptually a list of | |
| 439 (bytecode offset increment, line number increment) | |
| 440 pairs. The details are important and delicate, best illustrated by example: | |
| 441 | |
| 442 byte code offset source code line number | |
| 443 0 1 | |
| 444 6 2 | |
| 445 50 7 | |
| 446 350 307 | |
| 447 361 308 | |
| 448 | |
| 449 The first trick is that these numbers aren't stored, only the increments | |
| 450 from one row to the next (this doesn't really work, but it's a start): | |
| 451 | |
| 452 0, 1, 6, 1, 44, 5, 300, 300, 11, 1 | |
| 453 | |
| 454 The second trick is that an unsigned byte can't hold negative values, or | |
| 455 values larger than 255, so (a) there's a deep assumption that byte code | |
| 456 offsets and their corresponding line #s both increase monotonically, and (b) | |
| 457 if at least one column jumps by more than 255 from one row to the next, more | |
| 458 than one pair is written to the table. In case #b, there's no way to know | |
| 459 from looking at the table later how many were written. That's the delicate | |
| 460 part. A user of c_lnotab desiring to find the source line number | |
| 461 corresponding to a bytecode address A should do something like this | |
| 462 | |
| 463 lineno = addr = 0 | |
| 464 for addr_incr, line_incr in c_lnotab: | |
| 465 addr += addr_incr | |
| 466 if addr > A: | |
| 467 return lineno | |
| 468 lineno += line_incr | |
| 469 | |
| 470 In order for this to work, when the addr field increments by more than 255, | |
| 471 the line # increment in each pair generated must be 0 until the remaining addr | |
| 472 increment is < 256. So, in the example above, com_set_lineno should not (as | |
| 473 was actually done until 2.2) expand 300, 300 to 255, 255, 45, 45, but to | |
| 474 255, 0, 45, 255, 0, 45. | |
| 475 */ | |
| 476 | |
| 477 int | |
| 478 PyCode_Addr2Line(PyCodeObject *co, int addrq) | |
| 479 { | |
| 480 int size = PyString_Size(co->co_lnotab) / 2; | |
| 481 unsigned char *p = (unsigned char*)PyString_AsString(co->co_lnotab); | |
| 482 int line = co->co_firstlineno; | |
| 483 int addr = 0; | |
| 484 while (--size >= 0) { | |
| 485 addr += *p++; | |
| 486 if (addr > addrq) | |
| 487 break; | |
| 488 line += *p++; | |
| 489 } | |
| 490 return line; | |
| 491 } | |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
492 |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
493 /* |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
494 Check whether the current instruction is at the start of a line. |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
495 |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
496 */ |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
497 |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
498 /* The theory of SET_LINENO-less tracing. |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
499 |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
500 In a nutshell, we use the co_lnotab field of the code object |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
501 to tell when execution has moved onto a different line. |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
502 |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
503 As mentioned above, the basic idea is so set things up so |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
504 that |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
505 |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
506 *instr_lb <= frame->f_lasti < *instr_ub |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
507 |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
508 is true so long as execution does not change lines. |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
509 |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
510 This is all fairly simple. Digging the information out of |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
511 co_lnotab takes some work, but is conceptually clear. |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
512 |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
513 Somewhat harder to explain is why we don't *always* call the |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
514 line trace function when the above test fails. |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
515 |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
516 Consider this code: |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
517 |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
518 1: def f(a): |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
519 2: if a: |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
520 3: print 1 |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
521 4: else: |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
522 5: print 2 |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
523 |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
524 which compiles to this: |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
525 |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
526 2 0 LOAD_FAST 0 (a) |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
527 3 JUMP_IF_FALSE 9 (to 15) |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
528 6 POP_TOP |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
529 |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
530 3 7 LOAD_CONST 1 (1) |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
531 10 PRINT_ITEM |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
532 11 PRINT_NEWLINE |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
533 12 JUMP_FORWARD 6 (to 21) |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
534 >> 15 POP_TOP |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
535 |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
536 5 16 LOAD_CONST 2 (2) |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
537 19 PRINT_ITEM |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
538 20 PRINT_NEWLINE |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
539 >> 21 LOAD_CONST 0 (None) |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
540 24 RETURN_VALUE |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
541 |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
542 If 'a' is false, execution will jump to instruction at offset |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
543 15 and the co_lnotab will claim that execution has moved to |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
544 line 3. This is at best misleading. In this case we could |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
545 associate the POP_TOP with line 4, but that doesn't make |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
546 sense in all cases (I think). |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
547 |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
548 What we do is only call the line trace function if the co_lnotab |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
549 indicates we have jumped to the *start* of a line, i.e. if the |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
550 current instruction offset matches the offset given for the |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
551 start of a line by the co_lnotab. |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
552 |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
553 This also takes care of the situation where 'a' is true. |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
554 Execution will jump from instruction offset 12 to offset 21. |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
555 Then the co_lnotab would imply that execution has moved to line |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
556 5, which is again misleading. |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
557 |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
558 Why do we set f_lineno when tracing? Well, consider the code |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
559 above when 'a' is true. If stepping through this with 'n' in |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
560 pdb, you would stop at line 1 with a "call" type event, then |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
561 line events on lines 2 and 3, then a "return" type event -- but |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
562 you would be shown line 5 during this event. This is a change |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
563 from the behaviour in 2.2 and before, and I've found it |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
564 confusing in practice. By setting and using f_lineno when |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
565 tracing, one can report a line number different from that |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
566 suggested by f_lasti on this one occasion where it's desirable. |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
567 */ |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
568 |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
569 |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
570 int |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
571 PyCode_CheckLineNumber(PyCodeObject* co, int lasti, PyAddrPair *bounds) |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
572 { |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
573 int size, addr, line; |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
574 unsigned char* p; |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
575 |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
576 p = (unsigned char*)PyString_AS_STRING(co->co_lnotab); |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
577 size = PyString_GET_SIZE(co->co_lnotab) / 2; |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
578 |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
579 addr = 0; |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
580 line = co->co_firstlineno; |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
581 assert(line > 0); |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
582 |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
583 /* possible optimization: if f->f_lasti == instr_ub |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
584 (likely to be a common case) then we already know |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
585 instr_lb -- if we stored the matching value of p |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
586 somwhere we could skip the first while loop. */ |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
587 |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
588 /* see comments in compile.c for the description of |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
589 co_lnotab. A point to remember: increments to p |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
590 should come in pairs -- although we don't care about |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
591 the line increments here, treating them as byte |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
592 increments gets confusing, to say the least. */ |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
593 |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
594 bounds->ap_lower = 0; |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
595 while (size > 0) { |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
596 if (addr + *p > lasti) |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
597 break; |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
598 addr += *p++; |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
599 if (*p) |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
600 bounds->ap_lower = addr; |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
601 line += *p++; |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
602 --size; |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
603 } |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
604 |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
605 /* If lasti and addr don't match exactly, we don't want to |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
606 change the lineno slot on the frame or execute a trace |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
607 function. Return -1 instead. |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
608 */ |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
609 if (addr != lasti) |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
610 line = -1; |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
611 |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
612 if (size > 0) { |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
613 while (--size >= 0) { |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
614 addr += *p++; |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
615 if (*p++) |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
616 break; |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
617 } |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
618 bounds->ap_upper = addr; |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
619 } |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
620 else { |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
621 bounds->ap_upper = INT_MAX; |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
622 } |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
623 |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
624 return line; |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
625 } |
