Skip to content

Commit c51db0e

Browse files
authored
bpo-41531: Fix compilation of dict literals with more than 0xFFFF elements (GH-21850)
1 parent 8ecc0c4 commit c51db0e

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

Lib/test/test_compile.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,16 @@ def continue_in_while():
752752
self.assertEqual(None, opcodes[0].argval)
753753
self.assertEqual('RETURN_VALUE', opcodes[1].opname)
754754

755+
def test_big_dict_literal(self):
756+
# The compiler has a flushing point in "compiler_dict" that calls compiles
757+
# a portion of the dictionary literal when the loop that iterates over the items
758+
# reaches 0xFFFF elements but the code was not including the boundary element,
759+
# dropping the key at position 0xFFFF. See bpo-41531 for more information
760+
761+
dict_size = 0xFFFF + 1
762+
the_dict = "{" + ",".join(f"{x}:{x}" for x in range(dict_size)) + "}"
763+
self.assertEqual(len(eval(the_dict)), dict_size)
764+
755765
class TestExpressionStackSize(unittest.TestCase):
756766
# These tests check that the computed stack size for a code object
757767
# stays within reasonable bounds (see issue #21523 for an example
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a bug that was dropping keys when compiling dict literals with more than
2+
0xFFFF elements. Patch by Pablo Galindo.

Python/compile.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3894,7 +3894,7 @@ compiler_dict(struct compiler *c, expr_ty e)
38943894
}
38953895
else {
38963896
if (elements == 0xFFFF) {
3897-
if (!compiler_subdict(c, e, i - elements, i)) {
3897+
if (!compiler_subdict(c, e, i - elements, i + 1)) {
38983898
return 0;
38993899
}
39003900
if (have_dict) {

0 commit comments

Comments
 (0)