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

Delta Between Two Patch Sets: Python/compile.c

Issue 25843: lambdas on the same line may incorrectly share code objects
Left Patch Set: Created 4 years, 2 months ago
Right Patch Set: Created 4 years, 1 month 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 | « Objects/codeobject.c ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 /* 1 /*
2 * This file compiles an abstract syntax tree (AST) into Python bytecode. 2 * This file compiles an abstract syntax tree (AST) into Python bytecode.
3 * 3 *
4 * The primary entry point is PyAST_Compile(), which returns a 4 * The primary entry point is PyAST_Compile(), which returns a
5 * PyCodeObject. The compiler makes several passes to build the code 5 * PyCodeObject. The compiler makes several passes to build the code
6 * object: 6 * object:
7 * 1. Checks for future statements. See future.c 7 * 1. Checks for future statements. See future.c
8 * 2. Builds a symbol table. See symtable.c. 8 * 2. Builds a symbol table. See symtable.c.
9 * 3. Generate code for basic blocks. See compiler_mod() in this file. 9 * 3. Generate code for basic blocks. See compiler_mod() in this file.
10 * 4. Assemble the basic blocks into final code. See assemble() in 10 * 4. Assemble the basic blocks into final code. See assemble() in
(...skipping 375 matching lines...) Expand 10 before | Expand all | Expand 10 after
386 if (!dict) return NULL; 386 if (!dict) return NULL;
387 387
388 n = PyList_Size(list); 388 n = PyList_Size(list);
389 for (i = 0; i < n; i++) { 389 for (i = 0; i < n; i++) {
390 v = PyLong_FromSsize_t(i); 390 v = PyLong_FromSsize_t(i);
391 if (!v) { 391 if (!v) {
392 Py_DECREF(dict); 392 Py_DECREF(dict);
393 return NULL; 393 return NULL;
394 } 394 }
395 k = PyList_GET_ITEM(list, i); 395 k = PyList_GET_ITEM(list, i);
396 k = PyTuple_Pack(2, k, k->ob_type); 396 k = _PyCode_ConstantKey(k);
397 if (k == NULL || PyDict_SetItem(dict, k, v) < 0) { 397 if (k == NULL || PyDict_SetItem(dict, k, v) < 0) {
398 Py_XDECREF(k); 398 Py_XDECREF(k);
399 Py_DECREF(v); 399 Py_DECREF(v);
400 Py_DECREF(dict); 400 Py_DECREF(dict);
401 return NULL; 401 return NULL;
402 } 402 }
403 Py_DECREF(k); 403 Py_DECREF(k);
404 Py_DECREF(v); 404 Py_DECREF(v);
405 } 405 }
406 return dict; 406 return dict;
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
449 scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK; 449 scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK;
450 450
451 if (scope == scope_type || vi & flag) { 451 if (scope == scope_type || vi & flag) {
452 PyObject *tuple, *item = PyLong_FromSsize_t(i); 452 PyObject *tuple, *item = PyLong_FromSsize_t(i);
453 if (item == NULL) { 453 if (item == NULL) {
454 Py_DECREF(sorted_keys); 454 Py_DECREF(sorted_keys);
455 Py_DECREF(dest); 455 Py_DECREF(dest);
456 return NULL; 456 return NULL;
457 } 457 }
458 i++; 458 i++;
459 tuple = PyTuple_Pack(2, k, k->ob_type); 459 tuple = _PyCode_ConstantKey(k);
460 if (!tuple || PyDict_SetItem(dest, tuple, item) < 0) { 460 if (!tuple || PyDict_SetItem(dest, tuple, item) < 0) {
461 Py_DECREF(sorted_keys); 461 Py_DECREF(sorted_keys);
462 Py_DECREF(item); 462 Py_DECREF(item);
463 Py_DECREF(dest); 463 Py_DECREF(dest);
464 Py_XDECREF(tuple); 464 Py_XDECREF(tuple);
465 return NULL; 465 return NULL;
466 } 466 }
467 Py_DECREF(item); 467 Py_DECREF(item);
468 Py_DECREF(tuple); 468 Py_DECREF(tuple);
469 } 469 }
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
552 _Py_IDENTIFIER(__class__); 552 _Py_IDENTIFIER(__class__);
553 PyObject *tuple, *name, *zero; 553 PyObject *tuple, *name, *zero;
554 int res; 554 int res;
555 assert(u->u_scope_type == COMPILER_SCOPE_CLASS); 555 assert(u->u_scope_type == COMPILER_SCOPE_CLASS);
556 assert(PyDict_Size(u->u_cellvars) == 0); 556 assert(PyDict_Size(u->u_cellvars) == 0);
557 name = _PyUnicode_FromId(&PyId___class__); 557 name = _PyUnicode_FromId(&PyId___class__);
558 if (!name) { 558 if (!name) {
559 compiler_unit_free(u); 559 compiler_unit_free(u);
560 return 0; 560 return 0;
561 } 561 }
562 tuple = PyTuple_Pack(2, name, Py_TYPE(name)); 562 tuple = _PyCode_ConstantKey(name);
563 if (!tuple) { 563 if (!tuple) {
564 compiler_unit_free(u); 564 compiler_unit_free(u);
565 return 0; 565 return 0;
566 } 566 }
567 zero = PyLong_FromLong(0); 567 zero = PyLong_FromLong(0);
568 if (!zero) { 568 if (!zero) {
569 Py_DECREF(tuple); 569 Py_DECREF(tuple);
570 compiler_unit_free(u); 570 compiler_unit_free(u);
571 return 0; 571 return 0;
572 } 572 }
(...skipping 840 matching lines...) Expand 10 before | Expand all | Expand 10 after
1413 Py_FatalError(buf); 1413 Py_FatalError(buf);
1414 } 1414 }
1415 1415
1416 return scope; 1416 return scope;
1417 } 1417 }
1418 1418
1419 static int 1419 static int
1420 compiler_lookup_arg(PyObject *dict, PyObject *name) 1420 compiler_lookup_arg(PyObject *dict, PyObject *name)
1421 { 1421 {
1422 PyObject *k, *v; 1422 PyObject *k, *v;
1423 k = PyTuple_Pack(2, name, name->ob_type); 1423 k = _PyCode_ConstantKey(name);
1424 if (k == NULL) 1424 if (k == NULL)
1425 return -1; 1425 return -1;
1426 v = PyDict_GetItem(dict, k); 1426 v = PyDict_GetItem(dict, k);
1427 Py_DECREF(k); 1427 Py_DECREF(k);
1428 if (v == NULL) 1428 if (v == NULL)
1429 return -1; 1429 return -1;
1430 return PyLong_AS_LONG(v); 1430 return PyLong_AS_LONG(v);
1431 } 1431 }
1432 1432
1433 static int 1433 static int
(...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after
1754 */ 1754 */
1755 1755
1756 /* 1. compile the class body into a code object */ 1756 /* 1. compile the class body into a code object */
1757 if (!compiler_enter_scope(c, s->v.ClassDef.name, 1757 if (!compiler_enter_scope(c, s->v.ClassDef.name,
1758 COMPILER_SCOPE_CLASS, (void *)s, s->lineno)) 1758 COMPILER_SCOPE_CLASS, (void *)s, s->lineno))
1759 return 0; 1759 return 0;
1760 /* this block represents what we do in the new scope */ 1760 /* this block represents what we do in the new scope */
1761 { 1761 {
1762 /* use the class name for name mangling */ 1762 /* use the class name for name mangling */
1763 Py_INCREF(s->v.ClassDef.name); 1763 Py_INCREF(s->v.ClassDef.name);
1764 Py_XDECREF(c->u->u_private); 1764 Py_SETREF(c->u->u_private, s->v.ClassDef.name);
1765 c->u->u_private = s->v.ClassDef.name;
1766 /* load (global) __name__ ... */ 1765 /* load (global) __name__ ... */
1767 str = PyUnicode_InternFromString("__name__"); 1766 str = PyUnicode_InternFromString("__name__");
1768 if (!str || !compiler_nameop(c, str, Load)) { 1767 if (!str || !compiler_nameop(c, str, Load)) {
1769 Py_XDECREF(str); 1768 Py_XDECREF(str);
1770 compiler_exit_scope(c); 1769 compiler_exit_scope(c);
1771 return 0; 1770 return 0;
1772 } 1771 }
1773 Py_DECREF(str); 1772 Py_DECREF(str);
1774 /* ... and store it as __module__ */ 1773 /* ... and store it as __module__ */
1775 str = PyUnicode_InternFromString("__module__"); 1774 str = PyUnicode_InternFromString("__module__");
(...skipping 2631 matching lines...) Expand 10 before | Expand all | Expand 10 after
4407 assemble_lnotab(struct assembler *a, struct instr *i) 4406 assemble_lnotab(struct assembler *a, struct instr *i)
4408 { 4407 {
4409 int d_bytecode, d_lineno; 4408 int d_bytecode, d_lineno;
4410 Py_ssize_t len; 4409 Py_ssize_t len;
4411 unsigned char *lnotab; 4410 unsigned char *lnotab;
4412 4411
4413 d_bytecode = a->a_offset - a->a_lineno_off; 4412 d_bytecode = a->a_offset - a->a_lineno_off;
4414 d_lineno = i->i_lineno - a->a_lineno; 4413 d_lineno = i->i_lineno - a->a_lineno;
4415 4414
4416 assert(d_bytecode >= 0); 4415 assert(d_bytecode >= 0);
4417 assert(d_lineno >= 0);
4418 4416
4419 if(d_bytecode == 0 && d_lineno == 0) 4417 if(d_bytecode == 0 && d_lineno == 0)
4420 return 1; 4418 return 1;
4421 4419
4422 if (d_bytecode > 255) { 4420 if (d_bytecode > 255) {
4423 int j, nbytes, ncodes = d_bytecode / 255; 4421 int j, nbytes, ncodes = d_bytecode / 255;
4424 nbytes = a->a_lnotab_off + 2 * ncodes; 4422 nbytes = a->a_lnotab_off + 2 * ncodes;
4425 len = PyBytes_GET_SIZE(a->a_lnotab); 4423 len = PyBytes_GET_SIZE(a->a_lnotab);
4426 if (nbytes >= len) { 4424 if (nbytes >= len) {
4427 if ((len <= INT_MAX / 2) && (len * 2 < nbytes)) 4425 if ((len <= INT_MAX / 2) && (len * 2 < nbytes))
4428 len = nbytes; 4426 len = nbytes;
4429 else if (len <= INT_MAX / 2) 4427 else if (len <= INT_MAX / 2)
4430 len *= 2; 4428 len *= 2;
4431 else { 4429 else {
4432 PyErr_NoMemory(); 4430 PyErr_NoMemory();
4433 return 0; 4431 return 0;
4434 } 4432 }
4435 if (_PyBytes_Resize(&a->a_lnotab, len) < 0) 4433 if (_PyBytes_Resize(&a->a_lnotab, len) < 0)
4436 return 0; 4434 return 0;
4437 } 4435 }
4438 lnotab = (unsigned char *) 4436 lnotab = (unsigned char *)
4439 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off; 4437 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
4440 for (j = 0; j < ncodes; j++) { 4438 for (j = 0; j < ncodes; j++) {
4441 *lnotab++ = 255; 4439 *lnotab++ = 255;
4442 *lnotab++ = 0; 4440 *lnotab++ = 0;
4443 } 4441 }
4444 d_bytecode -= ncodes * 255; 4442 d_bytecode -= ncodes * 255;
4445 a->a_lnotab_off += ncodes * 2; 4443 a->a_lnotab_off += ncodes * 2;
4446 } 4444 }
4447 assert(d_bytecode <= 255); 4445 assert(0 <= d_bytecode && d_bytecode <= 255);
4448 if (d_lineno > 255) { 4446
4449 int j, nbytes, ncodes = d_lineno / 255; 4447 if (d_lineno < -128 || 127 < d_lineno) {
4448 int j, nbytes, ncodes, k;
4449 if (d_lineno < 0) {
4450 k = -128;
4451 /* use division on positive numbers */
4452 ncodes = (-d_lineno) / 128;
4453 }
4454 else {
4455 k = 127;
4456 ncodes = d_lineno / 127;
4457 }
4458 d_lineno -= ncodes * k;
4459 assert(ncodes >= 1);
4450 nbytes = a->a_lnotab_off + 2 * ncodes; 4460 nbytes = a->a_lnotab_off + 2 * ncodes;
4451 len = PyBytes_GET_SIZE(a->a_lnotab); 4461 len = PyBytes_GET_SIZE(a->a_lnotab);
4452 if (nbytes >= len) { 4462 if (nbytes >= len) {
4453 if ((len <= INT_MAX / 2) && len * 2 < nbytes) 4463 if ((len <= INT_MAX / 2) && len * 2 < nbytes)
4454 len = nbytes; 4464 len = nbytes;
4455 else if (len <= INT_MAX / 2) 4465 else if (len <= INT_MAX / 2)
4456 len *= 2; 4466 len *= 2;
4457 else { 4467 else {
4458 PyErr_NoMemory(); 4468 PyErr_NoMemory();
4459 return 0; 4469 return 0;
4460 } 4470 }
4461 if (_PyBytes_Resize(&a->a_lnotab, len) < 0) 4471 if (_PyBytes_Resize(&a->a_lnotab, len) < 0)
4462 return 0; 4472 return 0;
4463 } 4473 }
4464 lnotab = (unsigned char *) 4474 lnotab = (unsigned char *)
4465 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off; 4475 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
4466 *lnotab++ = d_bytecode; 4476 *lnotab++ = d_bytecode;
4467 *lnotab++ = 255; 4477 *lnotab++ = k;
4468 d_bytecode = 0; 4478 d_bytecode = 0;
4469 for (j = 1; j < ncodes; j++) { 4479 for (j = 1; j < ncodes; j++) {
4470 *lnotab++ = 0; 4480 *lnotab++ = 0;
4471 *lnotab++ = 255; 4481 *lnotab++ = k;
4472 } 4482 }
4473 d_lineno -= ncodes * 255;
4474 a->a_lnotab_off += ncodes * 2; 4483 a->a_lnotab_off += ncodes * 2;
4475 } 4484 }
4485 assert(-128 <= d_lineno && d_lineno <= 127);
4476 4486
4477 len = PyBytes_GET_SIZE(a->a_lnotab); 4487 len = PyBytes_GET_SIZE(a->a_lnotab);
4478 if (a->a_lnotab_off + 2 >= len) { 4488 if (a->a_lnotab_off + 2 >= len) {
4479 if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0) 4489 if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0)
4480 return 0; 4490 return 0;
4481 } 4491 }
4482 lnotab = (unsigned char *) 4492 lnotab = (unsigned char *)
4483 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off; 4493 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
4484 4494
4485 a->a_lnotab_off += 2; 4495 a->a_lnotab_off += 2;
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
4601 dict_keys_inorder(PyObject *dict, Py_ssize_t offset) 4611 dict_keys_inorder(PyObject *dict, Py_ssize_t offset)
4602 { 4612 {
4603 PyObject *tuple, *k, *v; 4613 PyObject *tuple, *k, *v;
4604 Py_ssize_t i, pos = 0, size = PyDict_Size(dict); 4614 Py_ssize_t i, pos = 0, size = PyDict_Size(dict);
4605 4615
4606 tuple = PyTuple_New(size); 4616 tuple = PyTuple_New(size);
4607 if (tuple == NULL) 4617 if (tuple == NULL)
4608 return NULL; 4618 return NULL;
4609 while (PyDict_Next(dict, &pos, &k, &v)) { 4619 while (PyDict_Next(dict, &pos, &k, &v)) {
4610 i = PyLong_AS_LONG(v); 4620 i = PyLong_AS_LONG(v);
4611 /* The keys of the dictionary are tuples. (see compiler_add_o) 4621 /* The keys of the dictionary are tuples. (see compiler_add_o
4612 The object we want is always first, though. */ 4622 * and _PyCode_ConstantKey). The object we want is always second,
4613 k = PyTuple_GET_ITEM(k, 0); 4623 * though. */
4624 k = PyTuple_GET_ITEM(k, 1);
4614 Py_INCREF(k); 4625 Py_INCREF(k);
4615 assert((i - offset) < size); 4626 assert((i - offset) < size);
4616 assert((i - offset) >= 0); 4627 assert((i - offset) >= 0);
4617 PyTuple_SET_ITEM(tuple, i - offset, k); 4628 PyTuple_SET_ITEM(tuple, i - offset, k);
4618 } 4629 }
4619 return tuple; 4630 return tuple;
4620 } 4631 }
4621 4632
4622 static int 4633 static int
4623 compute_code_flags(struct compiler *c) 4634 compute_code_flags(struct compiler *c)
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after
4822 return co; 4833 return co;
4823 } 4834 }
4824 4835
4825 #undef PyAST_Compile 4836 #undef PyAST_Compile
4826 PyAPI_FUNC(PyCodeObject *) 4837 PyAPI_FUNC(PyCodeObject *)
4827 PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags, 4838 PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
4828 PyArena *arena) 4839 PyArena *arena)
4829 { 4840 {
4830 return PyAST_CompileEx(mod, filename, flags, -1, arena); 4841 return PyAST_CompileEx(mod, filename, flags, -1, arena);
4831 } 4842 }
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+