]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-41531: Fix compilation of dict literals with more than 0xFFFF elements (GH-21850)
authorPablo Galindo <Pablogsal@gmail.com>
Thu, 13 Aug 2020 08:48:41 +0000 (09:48 +0100)
committerGitHub <noreply@github.com>
Thu, 13 Aug 2020 08:48:41 +0000 (09:48 +0100)
Lib/test/test_compile.py
Misc/NEWS.d/next/Core and Builtins/2020-08-12-19-32-15.bpo-41531.WgPzjT.rst [new file with mode: 0644]
Python/compile.c

index 3dd8c8d1db810343f366bfd317b0d67c4ce7a2fd..6055192bf7050367dbcd9a17bd27dd2326c04320 100644 (file)
@@ -752,6 +752,16 @@ if 1:
             self.assertEqual(None, opcodes[0].argval)
             self.assertEqual('RETURN_VALUE', opcodes[1].opname)
 
+    def test_big_dict_literal(self):
+        # The compiler has a flushing point in "compiler_dict" that calls compiles
+        # a portion of the dictionary literal when the loop that iterates over the items
+        # reaches 0xFFFF elements but the code was not including the boundary element,
+        # dropping the key at position 0xFFFF. See bpo-41531 for more information
+
+        dict_size = 0xFFFF + 1
+        the_dict = "{" + ",".join(f"{x}:{x}" for x in range(dict_size)) + "}"
+        self.assertEqual(len(eval(the_dict)), dict_size)
+
 class TestExpressionStackSize(unittest.TestCase):
     # These tests check that the computed stack size for a code object
     # stays within reasonable bounds (see issue #21523 for an example
diff --git a/Misc/NEWS.d/next/Core and Builtins/2020-08-12-19-32-15.bpo-41531.WgPzjT.rst b/Misc/NEWS.d/next/Core and Builtins/2020-08-12-19-32-15.bpo-41531.WgPzjT.rst
new file mode 100644 (file)
index 0000000..8544664
--- /dev/null
@@ -0,0 +1,2 @@
+Fix a bug that was dropping keys when compiling dict literals with more than
+0xFFFF elements. Patch by Pablo Galindo.
index 5dbd9f221fdf143a07d89217761598569aa103a1..2c5326686f8663c519e5d3f1d86bb1ec5ca3c97b 100644 (file)
@@ -3894,7 +3894,7 @@ compiler_dict(struct compiler *c, expr_ty e)
         }
         else {
             if (elements == 0xFFFF) {
-                if (!compiler_subdict(c, e, i - elements, i)) {
+                if (!compiler_subdict(c, e, i - elements, i + 1)) {
                     return 0;
                 }
                 if (have_dict) {