Mercurial > cpython-lru_cache-weakref
annotate Objects/codeobject.c @ 42802:01ee6311ba46
Ensure that code object names (co_name) are unicode.
Verify that they print properly too.
| author | Neal Norwitz <nnorwitz@gmail.com> |
|---|---|
| date | Fri, 24 Aug 2007 23:12:06 +0000 |
| parents | e70cdce0e8b2 |
| children | 19319e70fc3e |
| 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 } | |
|
42802
01ee6311ba46
Ensure that code object names (co_name) are unicode.
Neal Norwitz <nnorwitz@gmail.com>
parents:
42334
diff
changeset
|
68 if (PyString_Check(name)) { |
|
01ee6311ba46
Ensure that code object names (co_name) are unicode.
Neal Norwitz <nnorwitz@gmail.com>
parents:
42334
diff
changeset
|
69 name = PyUnicode_FromString(PyString_AS_STRING(name)); |
|
01ee6311ba46
Ensure that code object names (co_name) are unicode.
Neal Norwitz <nnorwitz@gmail.com>
parents:
42334
diff
changeset
|
70 if (name == NULL) |
|
01ee6311ba46
Ensure that code object names (co_name) are unicode.
Neal Norwitz <nnorwitz@gmail.com>
parents:
42334
diff
changeset
|
71 return NULL; |
|
01ee6311ba46
Ensure that code object names (co_name) are unicode.
Neal Norwitz <nnorwitz@gmail.com>
parents:
42334
diff
changeset
|
72 } else { |
|
01ee6311ba46
Ensure that code object names (co_name) are unicode.
Neal Norwitz <nnorwitz@gmail.com>
parents:
42334
diff
changeset
|
73 Py_INCREF(name); |
|
01ee6311ba46
Ensure that code object names (co_name) are unicode.
Neal Norwitz <nnorwitz@gmail.com>
parents:
42334
diff
changeset
|
74 } |
| 35742 | 75 intern_strings(names); |
| 76 intern_strings(varnames); | |
| 77 intern_strings(freevars); | |
| 78 intern_strings(cellvars); | |
| 79 /* Intern selected string constants */ | |
| 80 for (i = PyTuple_Size(consts); --i >= 0; ) { | |
| 81 PyObject *v = PyTuple_GetItem(consts, i); | |
| 82 if (!PyString_Check(v)) | |
| 83 continue; | |
| 84 if (!all_name_chars((unsigned char *)PyString_AS_STRING(v))) | |
| 85 continue; | |
| 86 PyString_InternInPlace(&PyTuple_GET_ITEM(consts, i)); | |
| 87 } | |
| 88 co = PyObject_NEW(PyCodeObject, &PyCode_Type); | |
| 89 if (co != NULL) { | |
| 90 co->co_argcount = argcount; | |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39658
diff
changeset
|
91 co->co_kwonlyargcount = kwonlyargcount; |
| 35742 | 92 co->co_nlocals = nlocals; |
| 93 co->co_stacksize = stacksize; | |
| 94 co->co_flags = flags; | |
| 95 Py_INCREF(code); | |
| 96 co->co_code = code; | |
| 97 Py_INCREF(consts); | |
| 98 co->co_consts = consts; | |
| 99 Py_INCREF(names); | |
| 100 co->co_names = names; | |
| 101 Py_INCREF(varnames); | |
| 102 co->co_varnames = varnames; | |
| 103 Py_INCREF(freevars); | |
| 104 co->co_freevars = freevars; | |
| 105 Py_INCREF(cellvars); | |
| 106 co->co_cellvars = cellvars; | |
| 107 Py_INCREF(filename); | |
| 108 co->co_filename = filename; | |
| 109 Py_INCREF(name); | |
| 110 co->co_name = name; | |
| 111 co->co_firstlineno = firstlineno; | |
| 112 Py_INCREF(lnotab); | |
| 113 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
|
114 co->co_zombieframe = NULL; |
| 35742 | 115 } |
|
42802
01ee6311ba46
Ensure that code object names (co_name) are unicode.
Neal Norwitz <nnorwitz@gmail.com>
parents:
42334
diff
changeset
|
116 Py_DECREF(name); |
| 35742 | 117 return co; |
| 118 } | |
| 119 | |
| 120 | |
| 121 #define OFF(x) offsetof(PyCodeObject, x) | |
| 122 | |
| 123 static PyMemberDef code_memberlist[] = { | |
| 124 {"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
|
125 {"co_kwonlyargcount", T_INT, OFF(co_kwonlyargcount), READONLY}, |
| 35742 | 126 {"co_nlocals", T_INT, OFF(co_nlocals), READONLY}, |
| 127 {"co_stacksize",T_INT, OFF(co_stacksize), READONLY}, | |
| 128 {"co_flags", T_INT, OFF(co_flags), READONLY}, | |
| 129 {"co_code", T_OBJECT, OFF(co_code), READONLY}, | |
| 130 {"co_consts", T_OBJECT, OFF(co_consts), READONLY}, | |
| 131 {"co_names", T_OBJECT, OFF(co_names), READONLY}, | |
| 132 {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY}, | |
| 133 {"co_freevars", T_OBJECT, OFF(co_freevars), READONLY}, | |
| 134 {"co_cellvars", T_OBJECT, OFF(co_cellvars), READONLY}, | |
| 135 {"co_filename", T_OBJECT, OFF(co_filename), READONLY}, | |
| 136 {"co_name", T_OBJECT, OFF(co_name), READONLY}, | |
| 137 {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY}, | |
| 138 {"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY}, | |
| 139 {NULL} /* Sentinel */ | |
| 140 }; | |
| 141 | |
| 142 /* Helper for code_new: return a shallow copy of a tuple that is | |
| 143 guaranteed to contain exact strings, by converting string subclasses | |
| 144 to exact strings and complaining if a non-string is found. */ | |
| 145 static PyObject* | |
| 146 validate_and_copy_tuple(PyObject *tup) | |
| 147 { | |
| 148 PyObject *newtuple; | |
| 149 PyObject *item; | |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35742
diff
changeset
|
150 Py_ssize_t i, len; |
| 35742 | 151 |
| 152 len = PyTuple_GET_SIZE(tup); | |
| 153 newtuple = PyTuple_New(len); | |
| 154 if (newtuple == NULL) | |
| 155 return NULL; | |
| 156 | |
| 157 for (i = 0; i < len; i++) { | |
| 158 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
|
159 if (PyUnicode_CheckExact(item)) { |
| 35742 | 160 Py_INCREF(item); |
| 161 } | |
|
42013
cd125fe83051
Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents:
41962
diff
changeset
|
162 else if (!PyUnicode_Check(item)) { |
| 35742 | 163 PyErr_Format( |
| 164 PyExc_TypeError, | |
| 165 "name tuples must contain only " | |
| 166 "strings, not '%.500s'", | |
| 167 item->ob_type->tp_name); | |
| 168 Py_DECREF(newtuple); | |
| 169 return NULL; | |
| 170 } | |
| 171 else { | |
|
42013
cd125fe83051
Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents:
41962
diff
changeset
|
172 item = PyUnicode_FromUnicode( |
|
cd125fe83051
Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents:
41962
diff
changeset
|
173 PyUnicode_AS_UNICODE(item), |
|
cd125fe83051
Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents:
41962
diff
changeset
|
174 PyUnicode_GET_SIZE(item)); |
| 35742 | 175 if (item == NULL) { |
| 176 Py_DECREF(newtuple); | |
| 177 return NULL; | |
| 178 } | |
| 179 } | |
| 180 PyTuple_SET_ITEM(newtuple, i, item); | |
| 181 } | |
| 182 | |
| 183 return newtuple; | |
| 184 } | |
| 185 | |
| 186 PyDoc_STRVAR(code_doc, | |
| 187 "code(argcount, nlocals, stacksize, flags, codestring, constants, names,\n\ | |
| 188 varnames, filename, name, firstlineno, lnotab[, freevars[, cellvars]])\n\ | |
| 189 \n\ | |
| 190 Create a code object. Not for the faint of heart."); | |
| 191 | |
| 192 static PyObject * | |
| 193 code_new(PyTypeObject *type, PyObject *args, PyObject *kw) | |
| 194 { | |
| 195 int argcount; | |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39658
diff
changeset
|
196 int kwonlyargcount; |
| 35742 | 197 int nlocals; |
| 198 int stacksize; | |
| 199 int flags; | |
| 200 PyObject *co = NULL; | |
| 201 PyObject *code; | |
| 202 PyObject *consts; | |
| 203 PyObject *names, *ournames = NULL; | |
| 204 PyObject *varnames, *ourvarnames = NULL; | |
| 205 PyObject *freevars = NULL, *ourfreevars = NULL; | |
| 206 PyObject *cellvars = NULL, *ourcellvars = NULL; | |
| 207 PyObject *filename; | |
| 208 PyObject *name; | |
| 209 int firstlineno; | |
| 210 PyObject *lnotab; | |
| 211 | |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39658
diff
changeset
|
212 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
|
213 &argcount, &kwonlyargcount, |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39658
diff
changeset
|
214 &nlocals, &stacksize, &flags, |
| 35742 | 215 &code, |
| 216 &PyTuple_Type, &consts, | |
| 217 &PyTuple_Type, &names, | |
| 218 &PyTuple_Type, &varnames, | |
| 219 &filename, &name, | |
| 220 &firstlineno, &lnotab, | |
| 221 &PyTuple_Type, &freevars, | |
| 222 &PyTuple_Type, &cellvars)) | |
| 223 return NULL; | |
| 224 | |
| 225 if (argcount < 0) { | |
| 226 PyErr_SetString( | |
| 227 PyExc_ValueError, | |
| 228 "code: argcount must not be negative"); | |
| 229 goto cleanup; | |
| 230 } | |
| 231 | |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39658
diff
changeset
|
232 if (kwonlyargcount < 0) { |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39658
diff
changeset
|
233 PyErr_SetString( |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39658
diff
changeset
|
234 PyExc_ValueError, |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39658
diff
changeset
|
235 "code: kwonlyargcount must not be negative"); |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39658
diff
changeset
|
236 goto cleanup; |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39658
diff
changeset
|
237 } |
| 35742 | 238 if (nlocals < 0) { |
| 239 PyErr_SetString( | |
| 240 PyExc_ValueError, | |
| 241 "code: nlocals must not be negative"); | |
| 242 goto cleanup; | |
| 243 } | |
| 244 | |
| 245 ournames = validate_and_copy_tuple(names); | |
| 246 if (ournames == NULL) | |
| 247 goto cleanup; | |
| 248 ourvarnames = validate_and_copy_tuple(varnames); | |
| 249 if (ourvarnames == NULL) | |
| 250 goto cleanup; | |
| 251 if (freevars) | |
| 252 ourfreevars = validate_and_copy_tuple(freevars); | |
| 253 else | |
| 254 ourfreevars = PyTuple_New(0); | |
| 255 if (ourfreevars == NULL) | |
| 256 goto cleanup; | |
| 257 if (cellvars) | |
| 258 ourcellvars = validate_and_copy_tuple(cellvars); | |
| 259 else | |
| 260 ourcellvars = PyTuple_New(0); | |
| 261 if (ourcellvars == NULL) | |
| 262 goto cleanup; | |
| 263 | |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39658
diff
changeset
|
264 co = (PyObject *)PyCode_New(argcount, kwonlyargcount, |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39658
diff
changeset
|
265 nlocals, stacksize, flags, |
| 35742 | 266 code, consts, ournames, ourvarnames, |
| 267 ourfreevars, ourcellvars, filename, | |
| 268 name, firstlineno, lnotab); | |
| 269 cleanup: | |
| 270 Py_XDECREF(ournames); | |
| 271 Py_XDECREF(ourvarnames); | |
| 272 Py_XDECREF(ourfreevars); | |
| 273 Py_XDECREF(ourcellvars); | |
| 274 return co; | |
| 275 } | |
| 276 | |
| 277 static void | |
| 278 code_dealloc(PyCodeObject *co) | |
| 279 { | |
| 280 Py_XDECREF(co->co_code); | |
| 281 Py_XDECREF(co->co_consts); | |
| 282 Py_XDECREF(co->co_names); | |
| 283 Py_XDECREF(co->co_varnames); | |
| 284 Py_XDECREF(co->co_freevars); | |
| 285 Py_XDECREF(co->co_cellvars); | |
| 286 Py_XDECREF(co->co_filename); | |
| 287 Py_XDECREF(co->co_name); | |
| 288 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
|
289 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
|
290 PyObject_GC_Del(co->co_zombieframe); |
| 35742 | 291 PyObject_DEL(co); |
| 292 } | |
| 293 | |
| 294 static PyObject * | |
| 295 code_repr(PyCodeObject *co) | |
| 296 { | |
| 297 int lineno = -1; | |
| 298 char *filename = "???"; | |
| 299 | |
| 300 if (co->co_firstlineno != 0) | |
| 301 lineno = co->co_firstlineno; | |
| 302 if (co->co_filename && PyString_Check(co->co_filename)) | |
| 303 filename = PyString_AS_STRING(co->co_filename); | |
|
41962
11930dcf9d4f
Use PyUnicode_FromFormat() directly.
Walter Dörwald <walter@livinglogic.de>
parents:
41769
diff
changeset
|
304 return PyUnicode_FromFormat( |
|
42802
01ee6311ba46
Ensure that code object names (co_name) are unicode.
Neal Norwitz <nnorwitz@gmail.com>
parents:
42334
diff
changeset
|
305 "<code object %.100U at %p, file \"%.300s\", line %d>", |
|
01ee6311ba46
Ensure that code object names (co_name) are unicode.
Neal Norwitz <nnorwitz@gmail.com>
parents:
42334
diff
changeset
|
306 co->co_name, co, filename, lineno); |
| 35742 | 307 } |
| 308 | |
|
39644
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
309 static PyObject * |
|
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
310 code_richcompare(PyObject *self, PyObject *other, int op) |
| 35742 | 311 { |
|
39650
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
312 PyCodeObject *co, *cp; |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
313 int eq; |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
314 PyObject *res; |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
315 |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
316 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
|
317 !PyCode_Check(self) || |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
318 !PyCode_Check(other)) { |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
319 Py_INCREF(Py_NotImplemented); |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
320 return Py_NotImplemented; |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
321 } |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
322 |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
323 co = (PyCodeObject *)self; |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
324 cp = (PyCodeObject *)other; |
|
39644
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
325 |
|
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
|
326 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
|
327 if (eq <= 0) goto unequal; |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
328 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
|
329 if (!eq) goto unequal; |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39658
diff
changeset
|
330 eq = co->co_kwonlyargcount == cp->co_kwonlyargcount; |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39658
diff
changeset
|
331 if (!eq) goto unequal; |
|
39650
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
332 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
|
333 if (!eq) goto unequal; |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
334 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
|
335 if (!eq) goto unequal; |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
336 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
|
337 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
|
338 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
|
339 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
|
340 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
|
341 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
|
342 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
|
343 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
|
344 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
|
345 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
|
346 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
|
347 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
|
348 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
|
349 if (eq <= 0) goto unequal; |
| 35742 | 350 |
|
39650
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
351 if (op == Py_EQ) |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
352 res = Py_True; |
| 35742 | 353 else |
|
39650
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
354 res = Py_False; |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
355 goto done; |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
356 |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
357 unequal: |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
358 if (eq < 0) |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
359 return NULL; |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
360 if (op == Py_NE) |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
361 res = Py_True; |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
362 else |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
363 res = Py_False; |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
364 |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
365 done: |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
366 Py_INCREF(res); |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
367 return res; |
| 35742 | 368 } |
| 369 | |
| 370 static long | |
| 371 code_hash(PyCodeObject *co) | |
| 372 { | |
| 373 long h, h0, h1, h2, h3, h4, h5, h6; | |
| 374 h0 = PyObject_Hash(co->co_name); | |
| 375 if (h0 == -1) return -1; | |
| 376 h1 = PyObject_Hash(co->co_code); | |
| 377 if (h1 == -1) return -1; | |
| 378 h2 = PyObject_Hash(co->co_consts); | |
| 379 if (h2 == -1) return -1; | |
| 380 h3 = PyObject_Hash(co->co_names); | |
| 381 if (h3 == -1) return -1; | |
| 382 h4 = PyObject_Hash(co->co_varnames); | |
| 383 if (h4 == -1) return -1; | |
| 384 h5 = PyObject_Hash(co->co_freevars); | |
| 385 if (h5 == -1) return -1; | |
| 386 h6 = PyObject_Hash(co->co_cellvars); | |
| 387 if (h6 == -1) return -1; | |
| 388 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
|
389 co->co_argcount ^ co->co_kwonlyargcount ^ |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39658
diff
changeset
|
390 co->co_nlocals ^ co->co_flags; |
| 35742 | 391 if (h == -1) h = -2; |
| 392 return h; | |
| 393 } | |
| 394 | |
| 395 /* XXX code objects need to participate in GC? */ | |
| 396 | |
| 397 PyTypeObject PyCode_Type = { | |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
42013
diff
changeset
|
398 PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 35742 | 399 "code", |
| 400 sizeof(PyCodeObject), | |
| 401 0, | |
| 402 (destructor)code_dealloc, /* tp_dealloc */ | |
| 403 0, /* tp_print */ | |
| 404 0, /* tp_getattr */ | |
| 405 0, /* tp_setattr */ | |
|
39644
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
406 0, /* tp_compare */ |
| 35742 | 407 (reprfunc)code_repr, /* tp_repr */ |
| 408 0, /* tp_as_number */ | |
| 409 0, /* tp_as_sequence */ | |
| 410 0, /* tp_as_mapping */ | |
|
39650
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
411 (hashfunc)code_hash, /* tp_hash */ |
| 35742 | 412 0, /* tp_call */ |
| 413 0, /* tp_str */ | |
| 414 PyObject_GenericGetAttr, /* tp_getattro */ | |
| 415 0, /* tp_setattro */ | |
| 416 0, /* tp_as_buffer */ | |
| 417 Py_TPFLAGS_DEFAULT, /* tp_flags */ | |
| 418 code_doc, /* tp_doc */ | |
| 419 0, /* tp_traverse */ | |
| 420 0, /* tp_clear */ | |
|
39650
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
421 code_richcompare, /* tp_richcompare */ |
| 35742 | 422 0, /* tp_weaklistoffset */ |
| 423 0, /* tp_iter */ | |
| 424 0, /* tp_iternext */ | |
| 425 0, /* tp_methods */ | |
| 426 code_memberlist, /* tp_members */ | |
| 427 0, /* tp_getset */ | |
| 428 0, /* tp_base */ | |
| 429 0, /* tp_dict */ | |
| 430 0, /* tp_descr_get */ | |
| 431 0, /* tp_descr_set */ | |
| 432 0, /* tp_dictoffset */ | |
| 433 0, /* tp_init */ | |
| 434 0, /* tp_alloc */ | |
| 435 code_new, /* tp_new */ | |
| 436 }; | |
| 437 | |
| 438 /* All about c_lnotab. | |
| 439 | |
| 440 c_lnotab is an array of unsigned bytes disguised as a Python string. In -O | |
| 441 mode, SET_LINENO opcodes aren't generated, and bytecode offsets are mapped | |
| 442 to source code line #s (when needed for tracebacks) via c_lnotab instead. | |
| 443 The array is conceptually a list of | |
| 444 (bytecode offset increment, line number increment) | |
| 445 pairs. The details are important and delicate, best illustrated by example: | |
| 446 | |
| 447 byte code offset source code line number | |
| 448 0 1 | |
| 449 6 2 | |
| 450 50 7 | |
| 451 350 307 | |
| 452 361 308 | |
| 453 | |
| 454 The first trick is that these numbers aren't stored, only the increments | |
| 455 from one row to the next (this doesn't really work, but it's a start): | |
| 456 | |
| 457 0, 1, 6, 1, 44, 5, 300, 300, 11, 1 | |
| 458 | |
| 459 The second trick is that an unsigned byte can't hold negative values, or | |
| 460 values larger than 255, so (a) there's a deep assumption that byte code | |
| 461 offsets and their corresponding line #s both increase monotonically, and (b) | |
| 462 if at least one column jumps by more than 255 from one row to the next, more | |
| 463 than one pair is written to the table. In case #b, there's no way to know | |
| 464 from looking at the table later how many were written. That's the delicate | |
| 465 part. A user of c_lnotab desiring to find the source line number | |
| 466 corresponding to a bytecode address A should do something like this | |
| 467 | |
| 468 lineno = addr = 0 | |
| 469 for addr_incr, line_incr in c_lnotab: | |
| 470 addr += addr_incr | |
| 471 if addr > A: | |
| 472 return lineno | |
| 473 lineno += line_incr | |
| 474 | |
| 475 In order for this to work, when the addr field increments by more than 255, | |
| 476 the line # increment in each pair generated must be 0 until the remaining addr | |
| 477 increment is < 256. So, in the example above, com_set_lineno should not (as | |
| 478 was actually done until 2.2) expand 300, 300 to 255, 255, 45, 45, but to | |
| 479 255, 0, 45, 255, 0, 45. | |
| 480 */ | |
| 481 | |
| 482 int | |
| 483 PyCode_Addr2Line(PyCodeObject *co, int addrq) | |
| 484 { | |
| 485 int size = PyString_Size(co->co_lnotab) / 2; | |
| 486 unsigned char *p = (unsigned char*)PyString_AsString(co->co_lnotab); | |
| 487 int line = co->co_firstlineno; | |
| 488 int addr = 0; | |
| 489 while (--size >= 0) { | |
| 490 addr += *p++; | |
| 491 if (addr > addrq) | |
| 492 break; | |
| 493 line += *p++; | |
| 494 } | |
| 495 return line; | |
| 496 } | |
|
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
|
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 /* |
|
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 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
|
500 |
|
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 */ |
|
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 /* 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
|
504 |
|
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 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
|
506 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
|
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 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
|
509 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
|
510 |
|
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 *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
|
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 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
|
514 |
|
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 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
|
516 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
|
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 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
|
519 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
|
520 |
|
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 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
|
522 |
|
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 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
|
524 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
|
525 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
|
526 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
|
527 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
|
528 |
|
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 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
|
530 |
|
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 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
|
532 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
|
533 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
|
534 |
|
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 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
|
536 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
|
537 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
|
538 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
|
539 >> 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
|
540 |
|
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 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
|
542 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
|
543 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
|
544 >> 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
|
545 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
|
546 |
|
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 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
|
548 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
|
549 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
|
550 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
|
551 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
|
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 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
|
554 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
|
555 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
|
556 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
|
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 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
|
559 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
|
560 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
|
561 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
|
562 |
|
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 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
|
564 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
|
565 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
|
566 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
|
567 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
|
568 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
|
569 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
|
570 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
|
571 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
|
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 |
|
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 |
|
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 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
|
576 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
|
577 { |
|
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 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
|
579 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
|
580 |
|
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 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
|
582 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
|
583 |
|
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 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
|
585 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
|
586 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
|
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 /* 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
|
589 (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
|
590 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
|
591 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
|
592 |
|
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 /* 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
|
594 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
|
595 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
|
596 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
|
597 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
|
598 |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
599 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
|
600 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
|
601 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
|
602 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
|
603 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
|
604 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
|
605 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
|
606 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
|
607 --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
|
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 |
|
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 /* 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
|
611 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
|
612 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
|
613 */ |
|
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 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
|
615 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
|
616 |
|
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 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
|
618 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
|
619 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
|
620 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
|
621 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
|
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 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
|
624 } |
|
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 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
|
626 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
|
627 } |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
628 |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
629 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
|
630 } |
