summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorMeador Inge <meadori@gmail.com>2012-07-18 19:20:11 (GMT)
committerMeador Inge <meadori@gmail.com>2012-07-18 19:20:11 (GMT)
commit2ca6315d151d9a376b46c4ae32ffeaf776da0cbc (patch)
treef3d2e612f1eb6113f5baa9eb49fe851dab468755 /Python
parent0b350c6b3e6c811a4634ee468acc6cc477b6ae30 (diff)
downloadcpython-2ca6315d151d9a376b46c4ae32ffeaf776da0cbc.zip
cpython-2ca6315d151d9a376b46c4ae32ffeaf776da0cbc.tar.gz
cpython-2ca6315d151d9a376b46c4ae32ffeaf776da0cbc.tar.bz2
Issue #15368: make bytecode generation deterministic.
Diffstat (limited to 'Python')
-rw-r--r--Python/compile.c24
1 files changed, 22 insertions, 2 deletions
diff --git a/Python/compile.c b/Python/compile.c
index 19e7cb2..8268b1c 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -367,16 +367,33 @@ each key.
static PyObject *
dictbytype(PyObject *src, int scope_type, int flag, int offset)
{
- Py_ssize_t pos = 0, i = offset, scope;
+ Py_ssize_t pos = 0, i = offset, scope, num_keys, key_i;
PyObject *k, *v, *dest = PyDict_New();
+ PyObject *sorted_keys;
assert(offset >= 0);
if (dest == NULL)
return NULL;
- while (PyDict_Next(src, &pos, &k, &v)) {
+ /* Sort the keys so that we have a deterministic order on the indexes
+ saved in the returned dictionary. These indexes are used as indexes
+ into the free and cell var storage. Therefore if they aren't
+ deterministic, then the generated bytecode is not deterministic.
+ */
+ sorted_keys = PyDict_Keys(src);
+ if (sorted_keys == NULL)
+ return NULL;
+ if (PyList_Sort(sorted_keys) != 0) {
+ Py_DECREF(sorted_keys);
+ return NULL;
+ }
+ num_keys = PyList_GET_SIZE(src);
+
+ for (key_i = 0; key_i < num_keys; key_i++) {
/* XXX this should probably be a macro in symtable.h */
long vi;
+ k = PyList_GET_ITEM(sorted_keys, key_i);
+ v = PyDict_GetItem(src, k);
assert(PyLong_Check(v));
vi = PyLong_AS_LONG(v);
scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK;
@@ -384,12 +401,14 @@ dictbytype(PyObject *src, int scope_type, int flag, int offset)
if (scope == scope_type || vi & flag) {
PyObject *tuple, *item = PyLong_FromLong(i);
if (item == NULL) {
+ Py_DECREF(sorted_keys);
Py_DECREF(dest);
return NULL;
}
i++;
tuple = PyTuple_Pack(2, k, k->ob_type);
if (!tuple || PyDict_SetItem(dest, tuple, item) < 0) {
+ Py_DECREF(sorted_keys);
Py_DECREF(item);
Py_DECREF(dest);
Py_XDECREF(tuple);
@@ -399,6 +418,7 @@ dictbytype(PyObject *src, int scope_type, int flag, int offset)
Py_DECREF(tuple);
}
}
+ Py_DECREF(sorted_keys);
return dest;
}