Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(4743)

Delta Between Two Patch Sets: Objects/codeobject.c

Issue 13405: Add DTrace probes
Left Patch Set: Created 1 year, 1 month ago
Right Patch Set: Created 10 months, 3 weeks ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Modules/gcmodule.c ('k') | Objects/frameobject.c » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 #include "Python.h" 1 #include "Python.h"
2 #include "code.h" 2 #include "code.h"
3 #include "structmember.h" 3 #include "structmember.h"
4 4
5 #define NAME_CHARS \ 5 #define NAME_CHARS \
6 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz" 6 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
7 7
8 /* all_name_chars(s): true iff all chars in s are valid NAME_CHARS */ 8 /* all_name_chars(s): true iff all chars in s are valid NAME_CHARS */
9 9
10 static int 10 static int
11 all_name_chars(unsigned char *s) 11 all_name_chars(PyObject *o)
12 { 12 {
13 static char ok_name_char[256]; 13 static char ok_name_char[256];
14 static unsigned char *name_chars = (unsigned char *)NAME_CHARS; 14 static unsigned char *name_chars = (unsigned char *)NAME_CHARS;
15 PyUnicodeObject *u = (PyUnicodeObject *)o;
16 const unsigned char *s;
17
18 if (!PyUnicode_Check(o) || PyUnicode_READY(u) == -1 ||
19 PyUnicode_MAX_CHAR_VALUE(u) >= 128)
20 return 0;
15 21
16 if (ok_name_char[*name_chars] == 0) { 22 if (ok_name_char[*name_chars] == 0) {
17 unsigned char *p; 23 unsigned char *p;
18 for (p = name_chars; *p; p++) 24 for (p = name_chars; *p; p++)
19 ok_name_char[*p] = 1; 25 ok_name_char[*p] = 1;
20 } 26 }
27 s = PyUnicode_1BYTE_DATA(u);
21 while (*s) { 28 while (*s) {
22 if (ok_name_char[*s++] == 0) 29 if (ok_name_char[*s++] == 0)
23 return 0; 30 return 0;
24 } 31 }
25 return 1; 32 return 1;
26 } 33 }
27 34
28 static void 35 static void
29 intern_strings(PyObject *tuple) 36 intern_strings(PyObject *tuple)
30 { 37 {
31 Py_ssize_t i; 38 Py_ssize_t i;
32 39
33 for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) { 40 for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
34 PyObject *v = PyTuple_GET_ITEM(tuple, i); 41 PyObject *v = PyTuple_GET_ITEM(tuple, i);
35 if (v == NULL || !PyString_CheckExact(v)) { 42 if (v == NULL || !PyUnicode_CheckExact(v)) {
36 Py_FatalError("non-string found in code slot"); 43 Py_FatalError("non-string found in code slot");
37 } 44 }
38 PyString_InternInPlace(&PyTuple_GET_ITEM(tuple, i)); 45 PyUnicode_InternInPlace(&PyTuple_GET_ITEM(tuple, i));
39 } 46 }
40 } 47 }
41 48
42 49
43 PyCodeObject * 50 PyCodeObject *
44 PyCode_New(int argcount, int nlocals, int stacksize, int flags, 51 PyCode_New(int argcount, int kwonlyargcount,
52 int nlocals, int stacksize, int flags,
45 PyObject *code, PyObject *consts, PyObject *names, 53 PyObject *code, PyObject *consts, PyObject *names,
46 PyObject *varnames, PyObject *freevars, PyObject *cellvars, 54 PyObject *varnames, PyObject *freevars, PyObject *cellvars,
47 PyObject *filename, PyObject *name, int firstlineno, 55 PyObject *filename, PyObject *name, int firstlineno,
48 PyObject *lnotab) 56 PyObject *lnotab)
49 { 57 {
50 PyCodeObject *co; 58 PyCodeObject *co;
51 Py_ssize_t i; 59 unsigned char *cell2arg = NULL;
60 Py_ssize_t i, n_cellvars;
61
52 /* Check argument types */ 62 /* Check argument types */
53 if (argcount < 0 || nlocals < 0 || 63 if (argcount < 0 || kwonlyargcount < 0 || nlocals < 0 ||
54 code == NULL || 64 code == NULL ||
55 consts == NULL || !PyTuple_Check(consts) || 65 consts == NULL || !PyTuple_Check(consts) ||
56 names == NULL || !PyTuple_Check(names) || 66 names == NULL || !PyTuple_Check(names) ||
57 varnames == NULL || !PyTuple_Check(varnames) || 67 varnames == NULL || !PyTuple_Check(varnames) ||
58 freevars == NULL || !PyTuple_Check(freevars) || 68 freevars == NULL || !PyTuple_Check(freevars) ||
59 cellvars == NULL || !PyTuple_Check(cellvars) || 69 cellvars == NULL || !PyTuple_Check(cellvars) ||
60 name == NULL || !PyString_Check(name) || 70 name == NULL || !PyUnicode_Check(name) ||
61 filename == NULL || !PyString_Check(filename) || 71 filename == NULL || !PyUnicode_Check(filename) ||
62 lnotab == NULL || !PyString_Check(lnotab) || 72 lnotab == NULL || !PyBytes_Check(lnotab) ||
63 !PyObject_CheckReadBuffer(code)) { 73 !PyObject_CheckReadBuffer(code)) {
64 PyErr_BadInternalCall(); 74 PyErr_BadInternalCall();
65 return NULL; 75 return NULL;
66 } 76 }
77 n_cellvars = PyTuple_GET_SIZE(cellvars);
67 intern_strings(names); 78 intern_strings(names);
68 intern_strings(varnames); 79 intern_strings(varnames);
69 intern_strings(freevars); 80 intern_strings(freevars);
70 intern_strings(cellvars); 81 intern_strings(cellvars);
71 /* Intern selected string constants */ 82 /* Intern selected string constants */
72 for (i = PyTuple_Size(consts); --i >= 0; ) { 83 for (i = PyTuple_GET_SIZE(consts); --i >= 0; ) {
73 PyObject *v = PyTuple_GetItem(consts, i); 84 PyObject *v = PyTuple_GetItem(consts, i);
74 if (!PyString_Check(v)) 85 if (!all_name_chars(v))
75 continue; 86 continue;
76 if (!all_name_chars((unsigned char *)PyString_AS_STRING(v))) 87 PyUnicode_InternInPlace(&PyTuple_GET_ITEM(consts, i));
77 continue; 88 }
78 PyString_InternInPlace(&PyTuple_GET_ITEM(consts, i)); 89 /* Create mapping between cells and arguments if needed. */
90 if (n_cellvars) {
91 Py_ssize_t total_args = argcount + kwonlyargcount +
92 ((flags & CO_VARARGS) != 0) + ((flags & CO_VARKEYWORDS) != 0);
93 Py_ssize_t alloc_size = sizeof(unsigned char) * n_cellvars;
94 int used_cell2arg = 0;
95 cell2arg = PyMem_MALLOC(alloc_size);
96 if (cell2arg == NULL)
97 return NULL;
98 memset(cell2arg, CO_CELL_NOT_AN_ARG, alloc_size);
99 /* Find cells which are also arguments. */
100 for (i = 0; i < n_cellvars; i++) {
101 Py_ssize_t j;
102 PyObject *cell = PyTuple_GET_ITEM(cellvars, i);
103 for (j = 0; j < total_args; j++) {
104 PyObject *arg = PyTuple_GET_ITEM(varnames, j);
105 if (!PyUnicode_Compare(cell, arg)) {
106 cell2arg[i] = j;
107 used_cell2arg = 1;
108 break;
109 }
110 }
111 }
112 if (!used_cell2arg) {
113 PyMem_FREE(cell2arg);
114 cell2arg = NULL;
115 }
79 } 116 }
80 co = PyObject_NEW(PyCodeObject, &PyCode_Type); 117 co = PyObject_NEW(PyCodeObject, &PyCode_Type);
81 if (co != NULL) { 118 if (co == NULL) {
82 co->co_argcount = argcount; 119 if (cell2arg)
83 co->co_nlocals = nlocals; 120 PyMem_FREE(cell2arg);
84 co->co_stacksize = stacksize; 121 return NULL;
85 co->co_flags = flags; 122 }
86 Py_INCREF(code); 123 co->co_argcount = argcount;
87 co->co_code = code; 124 co->co_kwonlyargcount = kwonlyargcount;
88 Py_INCREF(consts); 125 co->co_nlocals = nlocals;
89 co->co_consts = consts; 126 co->co_stacksize = stacksize;
90 Py_INCREF(names); 127 co->co_flags = flags;
91 co->co_names = names; 128 Py_INCREF(code);
92 Py_INCREF(varnames); 129 co->co_code = code;
93 co->co_varnames = varnames; 130 Py_INCREF(consts);
94 Py_INCREF(freevars); 131 co->co_consts = consts;
95 co->co_freevars = freevars; 132 Py_INCREF(names);
96 Py_INCREF(cellvars); 133 co->co_names = names;
97 co->co_cellvars = cellvars; 134 Py_INCREF(varnames);
98 Py_INCREF(filename); 135 co->co_varnames = varnames;
99 co->co_filename = filename; 136 Py_INCREF(freevars);
100 Py_INCREF(name); 137 co->co_freevars = freevars;
101 co->co_name = name; 138 Py_INCREF(cellvars);
102 co->co_firstlineno = firstlineno; 139 co->co_cellvars = cellvars;
103 Py_INCREF(lnotab); 140 co->co_cell2arg = cell2arg;
104 co->co_lnotab = lnotab; 141 Py_INCREF(filename);
105 co->co_zombieframe = NULL; 142 co->co_filename = filename;
106 co->co_weakreflist = NULL; 143 Py_INCREF(name);
144 co->co_name = name;
145 co->co_firstlineno = firstlineno;
146 Py_INCREF(lnotab);
147 co->co_lnotab = lnotab;
148 co->co_zombieframe = NULL;
149 co->co_weakreflist = NULL;
150
107 #ifdef WITH_DTRACE 151 #ifdef WITH_DTRACE
108 i = PyString_Size(co->co_code); 152 /*
109 co->co_linenos = PyMem_Malloc(sizeof(unsigned short) * i); 153 ** Cache UTF8 internally, available
110 if (co->co_linenos) { 154 ** for the pythonframe stack walker.
111 unsigned short *p = (unsigned short *)(co->co_linenos); 155 */
112 unsigned char *p2 = (unsigned char*)PyString_AsString(co->co_lnotab) ; 156 PyUnicode_AsUTF8(filename);
113 int size = PyString_Size(co->co_lnotab) / 2; 157 PyUnicode_AsUTF8(name);
114 int i2; 158
115 unsigned short offset = 0; 159 i = PyBytes_Size(co->co_code);
116 160 co->co_linenos = PyMem_Malloc(sizeof(unsigned short) * i);
117 while (size) { 161 if (co->co_linenos) {
118 size -= 1; 162 unsigned short *p = (unsigned short *)(co->co_linenos);
119 i2 = *p2++; 163 unsigned char *p2 = (unsigned char*)PyBytes_AsString(co->co_lnotab);
120 i-=i2; 164 int size = PyBytes_Size(co->co_lnotab) / 2;
121 while (i2--) 165 int i2;
122 *p++ = offset; 166 unsigned short offset = 0;
123 offset += *p2++; 167
124 } 168 while (size) {
125 while(i--) 169 size -= 1;
170 i2 = *p2++;
171 i-=i2;
172 while (i2--)
126 *p++ = offset; 173 *p++ = offset;
127 } 174 offset += *p2++;
175 }
176 while(i--)
177 *p++ = offset;
178 }
128 #endif 179 #endif
129 } 180
130 return co; 181 return co;
131 } 182 }
132 183
133 PyCodeObject * 184 PyCodeObject *
134 PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno) 185 PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno)
135 { 186 {
136 static PyObject *emptystring = NULL; 187 static PyObject *emptystring = NULL;
137 static PyObject *nulltuple = NULL; 188 static PyObject *nulltuple = NULL;
138 PyObject *filename_ob = NULL; 189 PyObject *filename_ob = NULL;
139 PyObject *funcname_ob = NULL; 190 PyObject *funcname_ob = NULL;
140 PyCodeObject *result = NULL; 191 PyCodeObject *result = NULL;
141 if (emptystring == NULL) { 192 if (emptystring == NULL) {
142 emptystring = PyString_FromString(""); 193 emptystring = PyBytes_FromString("");
143 if (emptystring == NULL) 194 if (emptystring == NULL)
144 goto failed; 195 goto failed;
145 } 196 }
146 if (nulltuple == NULL) { 197 if (nulltuple == NULL) {
147 nulltuple = PyTuple_New(0); 198 nulltuple = PyTuple_New(0);
148 if (nulltuple == NULL) 199 if (nulltuple == NULL)
149 goto failed; 200 goto failed;
150 } 201 }
151 funcname_ob = PyString_FromString(funcname); 202 funcname_ob = PyUnicode_FromString(funcname);
152 if (funcname_ob == NULL) 203 if (funcname_ob == NULL)
153 goto failed; 204 goto failed;
154 filename_ob = PyString_FromString(filename); 205 filename_ob = PyUnicode_DecodeFSDefault(filename);
155 if (filename_ob == NULL) 206 if (filename_ob == NULL)
156 goto failed; 207 goto failed;
157 208
158 result = PyCode_New(0, /* argcount */ 209 result = PyCode_New(0, /* argcount */
210 0, /* kwonlyargcount */
159 0, /* nlocals */ 211 0, /* nlocals */
160 0, /* stacksize */ 212 0, /* stacksize */
161 0, /* flags */ 213 0, /* flags */
162 emptystring, /* code */ 214 emptystring, /* code */
163 nulltuple, /* consts */ 215 nulltuple, /* consts */
164 nulltuple, /* names */ 216 nulltuple, /* names */
165 nulltuple, /* varnames */ 217 nulltuple, /* varnames */
166 nulltuple, /* freevars */ 218 nulltuple, /* freevars */
167 nulltuple, /* cellvars */ 219 nulltuple, /* cellvars */
168 filename_ob, /* filename */ 220 filename_ob, /* filename */
169 funcname_ob, /* name */ 221 funcname_ob, /* name */
170 firstlineno, /* firstlineno */ 222 firstlineno, /* firstlineno */
171 emptystring /* lnotab */ 223 emptystring /* lnotab */
172 ); 224 );
173 225
174 failed: 226 failed:
175 Py_XDECREF(funcname_ob); 227 Py_XDECREF(funcname_ob);
176 Py_XDECREF(filename_ob); 228 Py_XDECREF(filename_ob);
177 return result; 229 return result;
178 } 230 }
179 231
180 #define OFF(x) offsetof(PyCodeObject, x) 232 #define OFF(x) offsetof(PyCodeObject, x)
181 233
182 static PyMemberDef code_memberlist[] = { 234 static PyMemberDef code_memberlist[] = {
183 {"co_argcount", T_INT, OFF(co_argcount), READONLY}, 235 {"co_argcount", T_INT, OFF(co_argcount), READONLY},
236 {"co_kwonlyargcount", T_INT, OFF(co_kwonlyargcount), READONLY},
184 {"co_nlocals", T_INT, OFF(co_nlocals), READONLY}, 237 {"co_nlocals", T_INT, OFF(co_nlocals), READONLY},
185 {"co_stacksize",T_INT, OFF(co_stacksize), READONLY}, 238 {"co_stacksize",T_INT, OFF(co_stacksize), READONLY},
186 {"co_flags", T_INT, OFF(co_flags), READONLY}, 239 {"co_flags", T_INT, OFF(co_flags), READONLY},
187 {"co_code", T_OBJECT, OFF(co_code), READONLY}, 240 {"co_code", T_OBJECT, OFF(co_code), READONLY},
188 {"co_consts", T_OBJECT, OFF(co_consts), READONLY}, 241 {"co_consts", T_OBJECT, OFF(co_consts), READONLY},
189 {"co_names", T_OBJECT, OFF(co_names), READONLY}, 242 {"co_names", T_OBJECT, OFF(co_names), READONLY},
190 {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY}, 243 {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY},
191 {"co_freevars", T_OBJECT, OFF(co_freevars), READONLY}, 244 {"co_freevars", T_OBJECT, OFF(co_freevars), READONLY},
192 {"co_cellvars", T_OBJECT, OFF(co_cellvars), READONLY}, 245 {"co_cellvars", T_OBJECT, OFF(co_cellvars), READONLY},
193 {"co_filename", T_OBJECT, OFF(co_filename), READONLY}, 246 {"co_filename", T_OBJECT, OFF(co_filename), READONLY},
(...skipping 13 matching lines...) Expand all
207 PyObject *item; 260 PyObject *item;
208 Py_ssize_t i, len; 261 Py_ssize_t i, len;
209 262
210 len = PyTuple_GET_SIZE(tup); 263 len = PyTuple_GET_SIZE(tup);
211 newtuple = PyTuple_New(len); 264 newtuple = PyTuple_New(len);
212 if (newtuple == NULL) 265 if (newtuple == NULL)
213 return NULL; 266 return NULL;
214 267
215 for (i = 0; i < len; i++) { 268 for (i = 0; i < len; i++) {
216 item = PyTuple_GET_ITEM(tup, i); 269 item = PyTuple_GET_ITEM(tup, i);
217 if (PyString_CheckExact(item)) { 270 if (PyUnicode_CheckExact(item)) {
218 Py_INCREF(item); 271 Py_INCREF(item);
219 } 272 }
220 else if (!PyString_Check(item)) { 273 else if (!PyUnicode_Check(item)) {
221 PyErr_Format( 274 PyErr_Format(
222 PyExc_TypeError, 275 PyExc_TypeError,
223 "name tuples must contain only " 276 "name tuples must contain only "
224 "strings, not '%.500s'", 277 "strings, not '%.500s'",
225 item->ob_type->tp_name); 278 item->ob_type->tp_name);
226 Py_DECREF(newtuple); 279 Py_DECREF(newtuple);
227 return NULL; 280 return NULL;
228 } 281 }
229 else { 282 else {
230 item = PyString_FromStringAndSize( 283 item = _PyUnicode_Copy(item);
231 PyString_AS_STRING(item),
232 PyString_GET_SIZE(item));
233 if (item == NULL) { 284 if (item == NULL) {
234 Py_DECREF(newtuple); 285 Py_DECREF(newtuple);
235 return NULL; 286 return NULL;
236 } 287 }
237 } 288 }
238 PyTuple_SET_ITEM(newtuple, i, item); 289 PyTuple_SET_ITEM(newtuple, i, item);
239 } 290 }
240 291
241 return newtuple; 292 return newtuple;
242 } 293 }
243 294
244 PyDoc_STRVAR(code_doc, 295 PyDoc_STRVAR(code_doc,
245 "code(argcount, nlocals, stacksize, flags, codestring, constants, names,\n\ 296 "code(argcount, kwonlyargcount, nlocals, stacksize, flags, codestring,\n\
246 varnames, filename, name, firstlineno, lnotab[, freevars[, cellvars]])\n\ 297 constants, names, varnames, filename, name, firstlineno,\n\
298 lnotab[, freevars[, cellvars]])\n\
247 \n\ 299 \n\
248 Create a code object. Not for the faint of heart."); 300 Create a code object. Not for the faint of heart.");
249 301
250 static PyObject * 302 static PyObject *
251 code_new(PyTypeObject *type, PyObject *args, PyObject *kw) 303 code_new(PyTypeObject *type, PyObject *args, PyObject *kw)
252 { 304 {
253 int argcount; 305 int argcount;
306 int kwonlyargcount;
254 int nlocals; 307 int nlocals;
255 int stacksize; 308 int stacksize;
256 int flags; 309 int flags;
257 PyObject *co = NULL; 310 PyObject *co = NULL;
258 PyObject *code; 311 PyObject *code;
259 PyObject *consts; 312 PyObject *consts;
260 PyObject *names, *ournames = NULL; 313 PyObject *names, *ournames = NULL;
261 PyObject *varnames, *ourvarnames = NULL; 314 PyObject *varnames, *ourvarnames = NULL;
262 PyObject *freevars = NULL, *ourfreevars = NULL; 315 PyObject *freevars = NULL, *ourfreevars = NULL;
263 PyObject *cellvars = NULL, *ourcellvars = NULL; 316 PyObject *cellvars = NULL, *ourcellvars = NULL;
264 PyObject *filename; 317 PyObject *filename;
265 PyObject *name; 318 PyObject *name;
266 int firstlineno; 319 int firstlineno;
267 PyObject *lnotab; 320 PyObject *lnotab;
268 321
269 if (!PyArg_ParseTuple(args, "iiiiSO!O!O!SSiS|O!O!:code", 322 if (!PyArg_ParseTuple(args, "iiiiiSO!O!O!UUiS|O!O!:code",
270 &argcount, &nlocals, &stacksize, &flags, 323 &argcount, &kwonlyargcount,
324 &nlocals, &stacksize, &flags,
271 &code, 325 &code,
272 &PyTuple_Type, &consts, 326 &PyTuple_Type, &consts,
273 &PyTuple_Type, &names, 327 &PyTuple_Type, &names,
274 &PyTuple_Type, &varnames, 328 &PyTuple_Type, &varnames,
275 &filename, &name, 329 &filename, &name,
276 &firstlineno, &lnotab, 330 &firstlineno, &lnotab,
277 &PyTuple_Type, &freevars, 331 &PyTuple_Type, &freevars,
278 &PyTuple_Type, &cellvars)) 332 &PyTuple_Type, &cellvars))
279 return NULL; 333 return NULL;
280 334
281 if (argcount < 0) { 335 if (argcount < 0) {
282 PyErr_SetString( 336 PyErr_SetString(
283 PyExc_ValueError, 337 PyExc_ValueError,
284 "code: argcount must not be negative"); 338 "code: argcount must not be negative");
285 goto cleanup; 339 goto cleanup;
286 } 340 }
287 341
342 if (kwonlyargcount < 0) {
343 PyErr_SetString(
344 PyExc_ValueError,
345 "code: kwonlyargcount must not be negative");
346 goto cleanup;
347 }
288 if (nlocals < 0) { 348 if (nlocals < 0) {
289 PyErr_SetString( 349 PyErr_SetString(
290 PyExc_ValueError, 350 PyExc_ValueError,
291 "code: nlocals must not be negative"); 351 "code: nlocals must not be negative");
292 goto cleanup; 352 goto cleanup;
293 } 353 }
294 354
295 ournames = validate_and_copy_tuple(names); 355 ournames = validate_and_copy_tuple(names);
296 if (ournames == NULL) 356 if (ournames == NULL)
297 goto cleanup; 357 goto cleanup;
298 ourvarnames = validate_and_copy_tuple(varnames); 358 ourvarnames = validate_and_copy_tuple(varnames);
299 if (ourvarnames == NULL) 359 if (ourvarnames == NULL)
300 goto cleanup; 360 goto cleanup;
301 if (freevars) 361 if (freevars)
302 ourfreevars = validate_and_copy_tuple(freevars); 362 ourfreevars = validate_and_copy_tuple(freevars);
303 else 363 else
304 ourfreevars = PyTuple_New(0); 364 ourfreevars = PyTuple_New(0);
305 if (ourfreevars == NULL) 365 if (ourfreevars == NULL)
306 goto cleanup; 366 goto cleanup;
307 if (cellvars) 367 if (cellvars)
308 ourcellvars = validate_and_copy_tuple(cellvars); 368 ourcellvars = validate_and_copy_tuple(cellvars);
309 else 369 else
310 ourcellvars = PyTuple_New(0); 370 ourcellvars = PyTuple_New(0);
311 if (ourcellvars == NULL) 371 if (ourcellvars == NULL)
312 goto cleanup; 372 goto cleanup;
313 373
314 co = (PyObject *)PyCode_New(argcount, nlocals, stacksize, flags, 374 co = (PyObject *)PyCode_New(argcount, kwonlyargcount,
375 nlocals, stacksize, flags,
315 code, consts, ournames, ourvarnames, 376 code, consts, ournames, ourvarnames,
316 ourfreevars, ourcellvars, filename, 377 ourfreevars, ourcellvars, filename,
317 name, firstlineno, lnotab); 378 name, firstlineno, lnotab);
318 cleanup: 379 cleanup:
319 Py_XDECREF(ournames); 380 Py_XDECREF(ournames);
320 Py_XDECREF(ourvarnames); 381 Py_XDECREF(ourvarnames);
321 Py_XDECREF(ourfreevars); 382 Py_XDECREF(ourfreevars);
322 Py_XDECREF(ourcellvars); 383 Py_XDECREF(ourcellvars);
323 return co; 384 return co;
324 } 385 }
325 386
326 static void 387 static void
327 code_dealloc(PyCodeObject *co) 388 code_dealloc(PyCodeObject *co)
328 { 389 {
329 Py_XDECREF(co->co_code); 390 Py_XDECREF(co->co_code);
330 Py_XDECREF(co->co_consts); 391 Py_XDECREF(co->co_consts);
331 Py_XDECREF(co->co_names); 392 Py_XDECREF(co->co_names);
332 Py_XDECREF(co->co_varnames); 393 Py_XDECREF(co->co_varnames);
333 Py_XDECREF(co->co_freevars); 394 Py_XDECREF(co->co_freevars);
334 Py_XDECREF(co->co_cellvars); 395 Py_XDECREF(co->co_cellvars);
335 Py_XDECREF(co->co_filename); 396 Py_XDECREF(co->co_filename);
336 Py_XDECREF(co->co_name); 397 Py_XDECREF(co->co_name);
337 Py_XDECREF(co->co_lnotab); 398 Py_XDECREF(co->co_lnotab);
338 #ifdef WITH_DTRACE 399 if (co->co_cell2arg != NULL)
339 if (co->co_linenos) 400 PyMem_FREE(co->co_cell2arg);
340 PyMem_Free(co->co_linenos);
341 #endif
342 if (co->co_zombieframe != NULL) 401 if (co->co_zombieframe != NULL)
343 PyObject_GC_Del(co->co_zombieframe); 402 PyObject_GC_Del(co->co_zombieframe);
344 if (co->co_weakreflist != NULL) 403 if (co->co_weakreflist != NULL)
345 PyObject_ClearWeakRefs((PyObject*)co); 404 PyObject_ClearWeakRefs((PyObject*)co);
346 PyObject_DEL(co); 405 PyObject_DEL(co);
347 } 406 }
348 407
349 static PyObject * 408 static PyObject *
350 code_repr(PyCodeObject *co) 409 code_repr(PyCodeObject *co)
351 { 410 {
352 char buf[500]; 411 int lineno;
353 int lineno = -1;
354 char *filename = "???";
355 char *name = "???";
356
357 if (co->co_firstlineno != 0) 412 if (co->co_firstlineno != 0)
358 lineno = co->co_firstlineno; 413 lineno = co->co_firstlineno;
359 if (co->co_filename && PyString_Check(co->co_filename))
360 filename = PyString_AS_STRING(co->co_filename);
361 if (co->co_name && PyString_Check(co->co_name))
362 name = PyString_AS_STRING(co->co_name);
363 PyOS_snprintf(buf, sizeof(buf),
364 "<code object %.100s at %p, file \"%.300s\", line %d>",
365 name, co, filename, lineno);
366 return PyString_FromString(buf);
367 }
368
369 static int
370 code_compare(PyCodeObject *co, PyCodeObject *cp)
371 {
372 int cmp;
373 cmp = PyObject_Compare(co->co_name, cp->co_name);
374 if (cmp) return cmp;
375 cmp = co->co_argcount - cp->co_argcount;
376 if (cmp) goto normalize;
377 cmp = co->co_nlocals - cp->co_nlocals;
378 if (cmp) goto normalize;
379 cmp = co->co_flags - cp->co_flags;
380 if (cmp) goto normalize;
381 cmp = co->co_firstlineno - cp->co_firstlineno;
382 if (cmp) goto normalize;
383 cmp = PyObject_Compare(co->co_code, cp->co_code);
384 if (cmp) return cmp;
385 cmp = PyObject_Compare(co->co_consts, cp->co_consts);
386 if (cmp) return cmp;
387 cmp = PyObject_Compare(co->co_names, cp->co_names);
388 if (cmp) return cmp;
389 cmp = PyObject_Compare(co->co_varnames, cp->co_varnames);
390 if (cmp) return cmp;
391 cmp = PyObject_Compare(co->co_freevars, cp->co_freevars);
392 if (cmp) return cmp;
393 cmp = PyObject_Compare(co->co_cellvars, cp->co_cellvars);
394 return cmp;
395
396 normalize:
397 if (cmp > 0)
398 return 1;
399 else if (cmp < 0)
400 return -1;
401 else 414 else
402 return 0; 415 lineno = -1;
416 if (co->co_filename && PyUnicode_Check(co->co_filename)) {
417 return PyUnicode_FromFormat(
418 "<code object %U at %p, file \"%U\", line %d>",
419 co->co_name, co, co->co_filename, lineno);
420 } else {
421 return PyUnicode_FromFormat(
422 "<code object %U at %p, file ???, line %d>",
423 co->co_name, co, lineno);
424 }
403 } 425 }
404 426
405 static PyObject * 427 static PyObject *
406 code_richcompare(PyObject *self, PyObject *other, int op) 428 code_richcompare(PyObject *self, PyObject *other, int op)
407 { 429 {
408 PyCodeObject *co, *cp; 430 PyCodeObject *co, *cp;
409 int eq; 431 int eq;
410 PyObject *res; 432 PyObject *res;
411 433
412 if ((op != Py_EQ && op != Py_NE) || 434 if ((op != Py_EQ && op != Py_NE) ||
413 !PyCode_Check(self) || 435 !PyCode_Check(self) ||
414 !PyCode_Check(other)) { 436 !PyCode_Check(other)) {
415 437 Py_RETURN_NOTIMPLEMENTED;
416 /* Py3K warning if types are not equal and comparison
417 isn't == or != */
418 if (PyErr_WarnPy3k("code inequality comparisons not supported "
419 "in 3.x", 1) < 0) {
420 return NULL;
421 }
422
423 Py_INCREF(Py_NotImplemented);
424 return Py_NotImplemented;
425 } 438 }
426 439
427 co = (PyCodeObject *)self; 440 co = (PyCodeObject *)self;
428 cp = (PyCodeObject *)other; 441 cp = (PyCodeObject *)other;
429 442
430 eq = PyObject_RichCompareBool(co->co_name, cp->co_name, Py_EQ); 443 eq = PyObject_RichCompareBool(co->co_name, cp->co_name, Py_EQ);
431 if (eq <= 0) goto unequal; 444 if (eq <= 0) goto unequal;
432 eq = co->co_argcount == cp->co_argcount; 445 eq = co->co_argcount == cp->co_argcount;
446 if (!eq) goto unequal;
447 eq = co->co_kwonlyargcount == cp->co_kwonlyargcount;
433 if (!eq) goto unequal; 448 if (!eq) goto unequal;
434 eq = co->co_nlocals == cp->co_nlocals; 449 eq = co->co_nlocals == cp->co_nlocals;
435 if (!eq) goto unequal; 450 if (!eq) goto unequal;
436 eq = co->co_flags == cp->co_flags; 451 eq = co->co_flags == cp->co_flags;
437 if (!eq) goto unequal; 452 if (!eq) goto unequal;
438 eq = co->co_firstlineno == cp->co_firstlineno; 453 eq = co->co_firstlineno == cp->co_firstlineno;
439 if (!eq) goto unequal; 454 if (!eq) goto unequal;
440 eq = PyObject_RichCompareBool(co->co_code, cp->co_code, Py_EQ); 455 eq = PyObject_RichCompareBool(co->co_code, cp->co_code, Py_EQ);
441 if (eq <= 0) goto unequal; 456 if (eq <= 0) goto unequal;
442 eq = PyObject_RichCompareBool(co->co_consts, cp->co_consts, Py_EQ); 457 eq = PyObject_RichCompareBool(co->co_consts, cp->co_consts, Py_EQ);
(...skipping 19 matching lines...) Expand all
462 if (op == Py_NE) 477 if (op == Py_NE)
463 res = Py_True; 478 res = Py_True;
464 else 479 else
465 res = Py_False; 480 res = Py_False;
466 481
467 done: 482 done:
468 Py_INCREF(res); 483 Py_INCREF(res);
469 return res; 484 return res;
470 } 485 }
471 486
472 static long 487 static Py_hash_t
473 code_hash(PyCodeObject *co) 488 code_hash(PyCodeObject *co)
474 { 489 {
475 long h, h0, h1, h2, h3, h4, h5, h6; 490 Py_hash_t h, h0, h1, h2, h3, h4, h5, h6;
476 h0 = PyObject_Hash(co->co_name); 491 h0 = PyObject_Hash(co->co_name);
477 if (h0 == -1) return -1; 492 if (h0 == -1) return -1;
478 h1 = PyObject_Hash(co->co_code); 493 h1 = PyObject_Hash(co->co_code);
479 if (h1 == -1) return -1; 494 if (h1 == -1) return -1;
480 h2 = PyObject_Hash(co->co_consts); 495 h2 = PyObject_Hash(co->co_consts);
481 if (h2 == -1) return -1; 496 if (h2 == -1) return -1;
482 h3 = PyObject_Hash(co->co_names); 497 h3 = PyObject_Hash(co->co_names);
483 if (h3 == -1) return -1; 498 if (h3 == -1) return -1;
484 h4 = PyObject_Hash(co->co_varnames); 499 h4 = PyObject_Hash(co->co_varnames);
485 if (h4 == -1) return -1; 500 if (h4 == -1) return -1;
486 h5 = PyObject_Hash(co->co_freevars); 501 h5 = PyObject_Hash(co->co_freevars);
487 if (h5 == -1) return -1; 502 if (h5 == -1) return -1;
488 h6 = PyObject_Hash(co->co_cellvars); 503 h6 = PyObject_Hash(co->co_cellvars);
489 if (h6 == -1) return -1; 504 if (h6 == -1) return -1;
490 h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^ 505 h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^
491 co->co_argcount ^ co->co_nlocals ^ co->co_flags; 506 co->co_argcount ^ co->co_kwonlyargcount ^
507 co->co_nlocals ^ co->co_flags;
492 if (h == -1) h = -2; 508 if (h == -1) h = -2;
493 return h; 509 return h;
494 } 510 }
495 511
496 /* XXX code objects need to participate in GC? */ 512 /* XXX code objects need to participate in GC? */
497 513
498 PyTypeObject PyCode_Type = { 514 PyTypeObject PyCode_Type = {
499 PyVarObject_HEAD_INIT(&PyType_Type, 0) 515 PyVarObject_HEAD_INIT(&PyType_Type, 0)
500 "code", 516 "code",
501 sizeof(PyCodeObject), 517 sizeof(PyCodeObject),
502 0, 518 0,
503 (destructor)code_dealloc, /* tp_dealloc */ 519 (destructor)code_dealloc, /* tp_dealloc */
504 0, /* tp_print */ 520 0, /* tp_print */
505 0, /* tp_getattr */ 521 0, /* tp_getattr */
506 0, /* tp_setattr */ 522 0, /* tp_setattr */
507 (cmpfunc)code_compare, /* tp_compare */ 523 0, /* tp_reserved */
508 (reprfunc)code_repr, /* tp_repr */ 524 (reprfunc)code_repr, /* tp_repr */
509 0, /* tp_as_number */ 525 0, /* tp_as_number */
510 0, /* tp_as_sequence */ 526 0, /* tp_as_sequence */
511 0, /* tp_as_mapping */ 527 0, /* tp_as_mapping */
512 (hashfunc)code_hash, /* tp_hash */ 528 (hashfunc)code_hash, /* tp_hash */
513 0, /* tp_call */ 529 0, /* tp_call */
514 0, /* tp_str */ 530 0, /* tp_str */
515 PyObject_GenericGetAttr, /* tp_getattro */ 531 PyObject_GenericGetAttr, /* tp_getattro */
516 0, /* tp_setattro */ 532 0, /* tp_setattro */
517 0, /* tp_as_buffer */ 533 0, /* tp_as_buffer */
518 Py_TPFLAGS_DEFAULT, /* tp_flags */ 534 Py_TPFLAGS_DEFAULT, /* tp_flags */
519 code_doc, /* tp_doc */ 535 code_doc, /* tp_doc */
520 0, /* tp_traverse */ 536 0, /* tp_traverse */
521 0, /* tp_clear */ 537 0, /* tp_clear */
522 code_richcompare, /* tp_richcompare */ 538 code_richcompare, /* tp_richcompare */
523 offsetof(PyCodeObject, co_weakreflist), /* tp_weaklistoffset */ 539 offsetof(PyCodeObject, co_weakreflist), /* tp_weaklistoffset */
524 0, /* tp_iter */ 540 0, /* tp_iter */
525 0, /* tp_iternext */ 541 0, /* tp_iternext */
526 0, /* tp_methods */ 542 0, /* tp_methods */
527 code_memberlist, /* tp_members */ 543 code_memberlist, /* tp_members */
528 0, /* tp_getset */ 544 0, /* tp_getset */
529 0, /* tp_base */ 545 0, /* tp_base */
530 0, /* tp_dict */ 546 0, /* tp_dict */
531 0, /* tp_descr_get */ 547 0, /* tp_descr_get */
532 0, /* tp_descr_set */ 548 0, /* tp_descr_set */
533 0, /* tp_dictoffset */ 549 0, /* tp_dictoffset */
534 0, /* tp_init */ 550 0, /* tp_init */
535 0, /* tp_alloc */ 551 0, /* tp_alloc */
536 code_new, /* tp_new */ 552 code_new, /* tp_new */
537 }; 553 };
538 554
539 /* Use co_lnotab to compute the line number from a bytecode index, addrq. See 555 /* Use co_lnotab to compute the line number from a bytecode index, addrq. See
540 lnotab_notes.txt for the details of the lnotab representation. 556 lnotab_notes.txt for the details of the lnotab representation.
541 */ 557 */
542 558
543 int 559 int
544 PyCode_Addr2Line(PyCodeObject *co, int addrq) 560 PyCode_Addr2Line(PyCodeObject *co, int addrq)
545 { 561 {
546 int size = PyString_Size(co->co_lnotab) / 2; 562 Py_ssize_t size = PyBytes_Size(co->co_lnotab) / 2;
547 unsigned char *p = (unsigned char*)PyString_AsString(co->co_lnotab); 563 unsigned char *p = (unsigned char*)PyBytes_AsString(co->co_lnotab);
548 int line = co->co_firstlineno; 564 int line = co->co_firstlineno;
549 int addr = 0; 565 int addr = 0;
550 while (--size >= 0) { 566 while (--size >= 0) {
551 addr += *p++; 567 addr += *p++;
552 if (addr > addrq) 568 if (addr > addrq)
553 break; 569 break;
554 line += *p++; 570 line += *p++;
555 } 571 }
556 return line; 572 return line;
557 } 573 }
558 574
559 /* Update *bounds to describe the first and one-past-the-last instructions in 575 /* Update *bounds to describe the first and one-past-the-last instructions in
560 the same line as lasti. Return the number of that line. */ 576 the same line as lasti. Return the number of that line. */
561 int 577 int
562 _PyCode_CheckLineNumber(PyCodeObject* co, int lasti, PyAddrPair *bounds) 578 _PyCode_CheckLineNumber(PyCodeObject* co, int lasti, PyAddrPair *bounds)
563 { 579 {
564 int size, addr, line; 580 Py_ssize_t size;
581 int addr, line;
565 unsigned char* p; 582 unsigned char* p;
566 583
567 p = (unsigned char*)PyString_AS_STRING(co->co_lnotab); 584 p = (unsigned char*)PyBytes_AS_STRING(co->co_lnotab);
568 size = PyString_GET_SIZE(co->co_lnotab) / 2; 585 size = PyBytes_GET_SIZE(co->co_lnotab) / 2;
569 586
570 addr = 0; 587 addr = 0;
571 line = co->co_firstlineno; 588 line = co->co_firstlineno;
572 assert(line > 0); 589 assert(line > 0);
573 590
574 /* possible optimization: if f->f_lasti == instr_ub 591 /* possible optimization: if f->f_lasti == instr_ub
575 (likely to be a common case) then we already know 592 (likely to be a common case) then we already know
576 instr_lb -- if we stored the matching value of p 593 instr_lb -- if we stored the matching value of p
577 somwhere we could skip the first while loop. */ 594 somwhere we could skip the first while loop. */
578 595
(...skipping 19 matching lines...) Expand all
598 break; 615 break;
599 } 616 }
600 bounds->ap_upper = addr; 617 bounds->ap_upper = addr;
601 } 618 }
602 else { 619 else {
603 bounds->ap_upper = INT_MAX; 620 bounds->ap_upper = INT_MAX;
604 } 621 }
605 622
606 return line; 623 return line;
607 } 624 }
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld cbc36f91f3f7