8000 Address feedback from PR review · python/cpython@23723cb · GitHub
[go: up one dir, main page]

Skip to content

Commit 23723cb

Browse files
committed
Address feedback from PR review
1 parent 597be1c commit 23723cb

File tree

3 files changed

+31
-20
lines changed

Lib/test/test_marshal.py

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -345,18 +345,29 @@ def test_eof(self):
345345
self.assertRaises(EOFError, marshal.loads, data[0: i])
346346

347347
def test_deterministic_sets(self):
348-
# bpo-37596: sets and frozensets must always marshal deterministically!
349-
# Test this by seeding string hashes:
350-
elements = "(float('nan'), b'a', b'b', b'c', 'x', 'y', 'z')"
348+
# bpo-37596: To support reproducible builds, sets and frozensets need to
349+
# have their elements serialized in a consistent order (even when they
350+
# have been scrambled by hash randomization):
351351
for kind in ("set", "frozenset"):
352-
script = f"import marshal; print(marshal.dumps({kind}({elements})))"
353-
with self.subTest(kind):
354-
# {nan, b'b', 'x', b'a', b'c', 'y', 'z'}
355-
_, a, _ = assert_python_ok("-c", script, PYTHONHASHSEED="0")
356-
# {nan, 'x', b'a', 'z', b'b', 'y', b'c'}
357-
_, b, _ = assert_python_ok("-c", script, PYTHONHASHSEED="1")
358-
self.assertEqual(a, b)
359-
352+
for elements in (
353+
"float('nan'), b'a', b'b', b'c', 'x', 'y', 'z'",
354+
# Also test for bad interactions with backreferencing:
355+
"('string', 1), ('string', 2), ('string', 3)",
356+
):
357+
s = f"{kind}([{elements}])"
358+
with self.subTest(s):
359+
# First, make sure that our test case still has different
360+
# orders under hash seeds 0 and 1. If this check fails, we
361+
# need to update this test with different elements:
362+
args = ["-c", f"print({s})"]
363+
_, repr_0, _ = assert_python_ok(*args, PYTHONHASHSEED="0")
364+
_, repr_1, _ = assert_python_ok(*args, PYTHONHASHSEED="1")
365+
self.assertNotEqual(repr_0, repr_1)
366+
# Then, perform the actual test:
367+
args = ["-c", f"import marshal; print(marshal.dumps({s}))"]
368+
_, dump_0, _ = assert_python_ok(*args, PYTHONHASHSEED="0")
369+
_, dump_1, _ = assert_python_ok(*args, PYTHONHASHSEED="1")
370+
self.assertEqual(dump_0, dump_1)
360371

361372
LARGE_SIZE = 2**31
362373
pointer_size = 8 if sys.maxsize > 0xFFFFFFFF else 4
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
Ensure that :class:`set` and :class:`frozenset` objects are always
2-
:mod:`marshalled <marshal>` deterministically.
2+
:mod:`marshalled <marshal>` reproducibly.

Python/marshal.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -503,10 +503,10 @@ w_complex_object(PyObject *v, char flag, WFILE *p)
503503
W_TYPE(TYPE_SET, p);
504504
n = PySet_GET_SIZE(v);
505505
W_SIZE(n, p);
506-
// bpo-37596: We need to write the elements in a deterministic order!
507-
// Since we know that they all need marshal support anyways, this can
508-
// be conveniently accomplished by using their marshal serializations
509-
// as sort keys:
506+
// bpo-37596: To support reproducible builds, sets and frozensets need
507+
// to have their elements serialized in a consistent order (even when
508+
// they have been scrambled by hash randomization). To ensure this, we
509+
// use an order equivalent to sorted(v, key=marshal.dumps):
510510
PyObject *pairs = PyList_New(0);
511511
if (pairs == NULL) {
512512
p->error = WFERR_NOMEMORY;
@@ -520,15 +520,15 @@ w_complex_object(PyObject *v, char flag, WFILE *p)
520520
}
521521
PyObject *pair = PyTuple_Pack(2, dump, value);
522522
Py_DECREF(dump);
523-
int error = pair == NULL || PyList_Append(pairs, pair);
524-
Py_XDECREF(pair);
525-
if (error) {
523+
if (pair == NULL || PyList_Append(pairs, pair)) {
526524
p->error = WFERR_NOMEMORY;
525+
Py_XDECREF(pair);
527526
goto anyset_done;
528527
}
528+
Py_DECREF(pair);
529529
}
530530
if (PyList_Sort(pairs)) {
531-
p->error = WFERR_UNMARSHALLABLE;
531+
p->error = WFERR_NOMEMORY;
532532
goto anyset_done;
533533
}
534534
for (Py_ssize_t i = 0; i < n; i++) {

0 commit comments

Comments
 (0)
0