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

Side by Side Diff: Python/compile.c

Issue 25843: lambdas on the same line may incorrectly share code objects
Patch Set: Created 4 years, 4 months 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:
View unified diff | Download patch
« Objects/codeobject.c ('K') | « Objects/codeobject.c ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 1087 matching lines...) Expand 10 before | Expand all | Expand 10 after
1098 b->b_return = 1; 1098 b->b_return = 1;
1099 compiler_set_lineno(c, off); 1099 compiler_set_lineno(c, off);
1100 return 1; 1100 return 1;
1101 } 1101 }
1102 1102
1103 static Py_ssize_t 1103 static Py_ssize_t
1104 compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o) 1104 compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o)
1105 { 1105 {
1106 PyObject *t, *v; 1106 PyObject *t, *v;
1107 Py_ssize_t arg; 1107 Py_ssize_t arg;
1108 double d;
1109 1108
1110 /* necessary to make sure types aren't coerced (e.g., float and complex) */ 1109 t = _PyCode_ConstantKey(o);
1111 /* _and_ to distinguish 0.0 from -0.0 e.g. on IEEE platforms */
1112 if (PyFloat_Check(o)) {
1113 d = PyFloat_AS_DOUBLE(o);
1114 /* all we need is to make the tuple different in either the 0.0
1115 * or -0.0 case from all others, just to avoid the "coercion".
1116 */
1117 if (d == 0.0 && copysign(1.0, d) < 0.0)
1118 t = PyTuple_Pack(3, o, o->ob_type, Py_None);
1119 else
1120 t = PyTuple_Pack(2, o, o->ob_type);
1121 }
1122 else if (PyComplex_Check(o)) {
1123 Py_complex z;
1124 int real_negzero, imag_negzero;
1125 /* For the complex case we must make complex(x, 0.)
1126 different from complex(x, -0.) and complex(0., y)
1127 different from complex(-0., y), for any x and y.
1128 All four complex zeros must be distinguished.*/
1129 z = PyComplex_AsCComplex(o);
1130 real_negzero = z.real == 0.0 && copysign(1.0, z.real) < 0.0;
1131 imag_negzero = z.imag == 0.0 && copysign(1.0, z.imag) < 0.0;
1132 if (real_negzero && imag_negzero) {
1133 t = PyTuple_Pack(5, o, o->ob_type,
1134 Py_None, Py_None, Py_None);
1135 }
1136 else if (imag_negzero) {
1137 t = PyTuple_Pack(4, o, o->ob_type, Py_None, Py_None);
1138 }
1139 else if (real_negzero) {
1140 t = PyTuple_Pack(3, o, o->ob_type, Py_None);
1141 }
1142 else {
1143 t = PyTuple_Pack(2, o, o->ob_type);
1144 }
1145 }
1146 else {
1147 t = PyTuple_Pack(2, o, o->ob_type);
1148 }
1149 if (t == NULL) 1110 if (t == NULL)
1150 return -1; 1111 return -1;
1151 1112
1152 v = PyDict_GetItem(dict, t); 1113 v = PyDict_GetItem(dict, t);
1153 if (!v) { 1114 if (!v) {
1154 if (PyErr_Occurred()) { 1115 if (PyErr_Occurred()) {
1155 Py_DECREF(t); 1116 Py_DECREF(t);
1156 return -1; 1117 return -1;
1157 } 1118 }
1158 arg = PyDict_Size(dict); 1119 arg = PyDict_Size(dict);
(...skipping 3712 matching lines...) Expand 10 before | Expand all | Expand 10 after
4871 return co; 4832 return co;
4872 } 4833 }
4873 4834
4874 #undef PyAST_Compile 4835 #undef PyAST_Compile
4875 PyAPI_FUNC(PyCodeObject *) 4836 PyAPI_FUNC(PyCodeObject *)
4876 PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags, 4837 PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
4877 PyArena *arena) 4838 PyArena *arena)
4878 { 4839 {
4879 return PyAST_CompileEx(mod, filename, flags, -1, arena); 4840 return PyAST_CompileEx(mod, filename, flags, -1, arena);
4880 } 4841 }
OLDNEW
« Objects/codeobject.c ('K') | « Objects/codeobject.c ('k') | no next file » | no next file with comments »

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