diff -r 775b74e0e103 Include/code.h --- a/Include/code.h Wed Jan 20 12:16:21 2016 +0100 +++ b/Include/code.h Wed Jan 20 14:59:07 2016 +0100 @@ -114,6 +114,12 @@ typedef struct _addr_pair { #ifndef Py_LIMITED_API PyAPI_FUNC(int) _PyCode_CheckLineNumber(PyCodeObject* co, int lasti, PyAddrPair *bounds); + +/* Create a comparable object used to compare constants taking in account the + * object type. For example, 0 (int) and 0.0 (float) must not be seen equal + * even if their value is equal. +0.0 and -0.0 are also considered as + * different. */ +PyAPI_FUNC(PyObject*) _PyCode_ConstantKey(PyObject *o); #endif PyAPI_FUNC(PyObject*) PyCode_Optimize(PyObject *code, PyObject* consts, diff -r 775b74e0e103 Lib/test/test_compile.py --- a/Lib/test/test_compile.py Wed Jan 20 12:16:21 2016 +0100 +++ b/Lib/test/test_compile.py Wed Jan 20 14:59:07 2016 +0100 @@ -572,6 +572,78 @@ if 1: exec(memoryview(b"ax = 123")[1:-1], namespace) self.assertEqual(namespace['x'], 12) + def check_constant(self, func, expected): + for const in func.__code__.co_consts: + if const != expected or type(const) != type(expected): + continue + if type(expected) in (tuple, frozenset): + if list(map(type, const)) != list(map(type, expected)): + continue + # its ok, we found it + break + else: + self.fail("unable to find constant %r in %r" + % (expected, func.__code__.co_consts)) + + # Merging equal constants is not a strict requirements for the Python + # semantics, it's a more an implementation detail. + @support.cpython_only + def test_merge_constants(self): + # Issue #25843: compile() must merge constants which are equal + # and have the same type. + + def check_same_constant(const): + ns = {} + code = "f1, f2 = lambda: %r, lambda: %r" % (const, const) + exec(code, ns) + f1 = ns['f1'] + f2 = ns['f2'] + self.assertIs(f1.__code__, f2.__code__) + self.check_constant(f1, const) + + check_same_constant(None) + check_same_constant(0) + check_same_constant(0.0) + check_same_constant(b'abc') + check_same_constant('abc') + + # Note: "lambda: ..." emits "LOAD_CONST Ellipsis", + # whereas "lambda: Ellipsis" emits "LOAD_GLOBAL Ellipsis" + f1, f2 = lambda: ..., lambda: ... + self.assertIs(f1.__code__, f2.__code__) + self.check_constant(f1, Ellipsis) + + # {0} is converted to a constant frozenset({0}) by the peephole + # optimizer + f1, f2 = lambda x: x in {0}, lambda x: x in {0} + self.assertIs(f1.__code__, f2.__code__) + self.check_constant(f1, frozenset({0})) + + def test_dont_merge_constants(self): + # Issue #25843: compile() must not merge constants which are equal + # but have a different type. + + def check_different_constants(const1, const2): + ns = {} + exec("f1, f2 = lambda: %r, lambda: %r" % (const1, const2), ns) + f1 = ns['f1'] + f2 = ns['f2'] + self.assertIsNot(f1.__code__, f2.__code__) + self.check_constant(f1, const1) + self.check_constant(f2, const2) + + check_different_constants(0, 0.0) + check_different_constants(+0.0, -0.0) + check_different_constants(+0.0j, -0.0j) + check_different_constants((0,), (0.0,)) + + # {0} is converted to a constant frozenset({0}) by the peephole + # optimizer + f1, f2 = lambda x: x in {0}, lambda x: x in {0.0} + self.assertIsNot(f1.__code__, f2.__code__) + self.check_constant(f1, frozenset({0})) + self.check_constant(f2, frozenset({0.0})) + class TestStackSize(unittest.TestCase): # These tests check that the computed stack size for a code object diff -r 775b74e0e103 Objects/codeobject.c --- a/Objects/codeobject.c Wed Jan 20 12:16:21 2016 +0100 +++ b/Objects/codeobject.c Wed Jan 20 14:59:07 2016 +0100 @@ -409,11 +409,130 @@ code_repr(PyCodeObject *co) } } +PyObject* +_PyCode_ConstantKey(PyObject *op) +{ + PyObject *key; + + /* necessary to make sure types aren't coerced (e.g., float and complex) + * _and_ to distinguish 0.0 from -0.0 e.g. on IEEE platforms */ + if (PyFloat_Check(op)) { + double d = PyFloat_AS_DOUBLE(op); + /* all we need is to make the tuple different in either the 0.0 + * or -0.0 case from all others, just to avoid the "coercion". + */ + if (d == 0.0 && copysign(1.0, d) < 0.0) + key = PyTuple_Pack(3, op, Py_TYPE(op), Py_None); + else + key = PyTuple_Pack(2, op, Py_TYPE(op)); + } + else if (PyComplex_Check(op)) { + Py_complex z; + int real_negzero, imag_negzero; + /* For the complex case we must make complex(x, 0.) + different from complex(x, -0.) and complex(0., y) + different from complex(-0., y), for any x and y. + All four complex zeros must be distinguished.*/ + z = PyComplex_AsCComplex(op); + real_negzero = z.real == 0.0 && copysign(1.0, z.real) < 0.0; + imag_negzero = z.imag == 0.0 && copysign(1.0, z.imag) < 0.0; + if (real_negzero && imag_negzero) { + key = PyTuple_Pack(5, op, Py_TYPE(op), + Py_None, Py_None, Py_None); + } + else if (imag_negzero) { + key = PyTuple_Pack(4, op, Py_TYPE(op), Py_None, Py_None); + } + else if (real_negzero) { + key = PyTuple_Pack(3, op, Py_TYPE(op), Py_None); + } + else { + key = PyTuple_Pack(2, op, Py_TYPE(op)); + } + } + else if (PyTuple_Check(op)) { + Py_ssize_t i, len; + PyObject *keys; + + len = PyTuple_GET_SIZE(op); + keys = PyTuple_New(len); + if (keys == NULL) + return NULL; + + for (i=0; i < len; i++) { + PyObject *item, *item_key; + + item = PyTuple_GET_ITEM(op, i); + item_key = _PyCode_ConstantKey(item); + if (item_key == NULL) { + Py_DECREF(keys); + return NULL; + } + + PyTuple_SET_ITEM(keys, i, item_key); + } + + key = PyTuple_Pack(2, op, keys); + } + else if (PyAnySet_CheckExact(op)) { + Py_ssize_t pos = 0; + PyObject *item; + Py_hash_t hash; + Py_ssize_t i, len; + PyObject *keys; + + len = PySet_GET_SIZE(op); + keys = PyTuple_New(len); + if (keys == NULL) + return NULL; + + i = 0; + while (_PySet_NextEntry(op, &pos, &item, &hash)) { + PyObject *item_key; + + item_key = _PyCode_ConstantKey(item); + if (item_key == NULL) { + Py_DECREF(keys); + return NULL; + } + + PyTuple_SET_ITEM(keys, i, item_key); + i++; + if (i >= len) { + /* set size changed during iteration, ignore */ + break; + } + } + return keys; + } + /* Py_None and Py_Ellipsis are singleton */ + else if (op == Py_None || op == Py_Ellipsis + || PyLong_CheckExact(op) + || PyBool_Check(op) + || PyBytes_CheckExact(op) + || PyUnicode_CheckExact(op) + /* code_richcompare() uses _PyCode_ConstantKey() internally */ + || PyCode_Check(op)) { + key = PyTuple_Pack(2, op, Py_TYPE(op)); + } + else { + /* for other types, use the identifier to *not* merge them + * even if they are equal */ + PyObject *obj_id = PyLong_FromVoidPtr(op); + if (obj_id == NULL) + return NULL; + key = PyTuple_Pack(3, op, Py_TYPE(op), obj_id); + Py_DECREF(obj_id); + } + return key; +} + static PyObject * code_richcompare(PyObject *self, PyObject *other, int op) { PyCodeObject *co, *cp; int eq; + PyObject *consts1, *consts2; PyObject *res; if ((op != Py_EQ && op != Py_NE) || @@ -439,8 +558,21 @@ code_richcompare(PyObject *self, PyObjec if (!eq) goto unequal; eq = PyObject_RichCompareBool(co->co_code, cp->co_code, Py_EQ); if (eq <= 0) goto unequal; - eq = PyObject_RichCompareBool(co->co_consts, cp->co_consts, Py_EQ); + + /* compare constants */ + consts1 = _PyCode_ConstantKey(co->co_consts); + if (!consts1) + return NULL; + consts2 = _PyCode_ConstantKey(cp->co_consts); + if (!consts2) { + Py_DECREF(consts1); + return NULL; + } + eq = PyObject_RichCompareBool(consts1, consts2, Py_EQ); + Py_DECREF(consts1); + Py_DECREF(consts2); if (eq <= 0) goto unequal; + eq = PyObject_RichCompareBool(co->co_names, cp->co_names, Py_EQ); if (eq <= 0) goto unequal; eq = PyObject_RichCompareBool(co->co_varnames, cp->co_varnames, Py_EQ); diff -r 775b74e0e103 Python/compile.c --- a/Python/compile.c Wed Jan 20 12:16:21 2016 +0100 +++ b/Python/compile.c Wed Jan 20 14:59:07 2016 +0100 @@ -1105,47 +1105,8 @@ compiler_add_o(struct compiler *c, PyObj { PyObject *t, *v; Py_ssize_t arg; - double d; - - /* necessary to make sure types aren't coerced (e.g., float and complex) */ - /* _and_ to distinguish 0.0 from -0.0 e.g. on IEEE platforms */ - if (PyFloat_Check(o)) { - d = PyFloat_AS_DOUBLE(o); - /* all we need is to make the tuple different in either the 0.0 - * or -0.0 case from all others, just to avoid the "coercion". - */ - if (d == 0.0 && copysign(1.0, d) < 0.0) - t = PyTuple_Pack(3, o, o->ob_type, Py_None); - else - t = PyTuple_Pack(2, o, o->ob_type); - } - else if (PyComplex_Check(o)) { - Py_complex z; - int real_negzero, imag_negzero; - /* For the complex case we must make complex(x, 0.) - different from complex(x, -0.) and complex(0., y) - different from complex(-0., y), for any x and y. - All four complex zeros must be distinguished.*/ - z = PyComplex_AsCComplex(o); - real_negzero = z.real == 0.0 && copysign(1.0, z.real) < 0.0; - imag_negzero = z.imag == 0.0 && copysign(1.0, z.imag) < 0.0; - if (real_negzero && imag_negzero) { - t = PyTuple_Pack(5, o, o->ob_type, - Py_None, Py_None, Py_None); - } - else if (imag_negzero) { - t = PyTuple_Pack(4, o, o->ob_type, Py_None, Py_None); - } - else if (real_negzero) { - t = PyTuple_Pack(3, o, o->ob_type, Py_None); - } - else { - t = PyTuple_Pack(2, o, o->ob_type); - } - } - else { - t = PyTuple_Pack(2, o, o->ob_type); - } + + t = _PyCode_ConstantKey(o); if (t == NULL) return -1;