Skip to content

bpo-41531: Fix compilation of dict literals with more than 0xFFFF elements #21850

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Lib/test/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,16 @@ def continue_in_while():
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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a bug that was dropping keys when compiling dict literals with more than
0xFFFF elements. Patch by Pablo Galindo.
2 changes: 1 addition & 1 deletion Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Copy link
Member Author

@pablogsal pablogsal Aug 12, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just made the minimum edit to fix the problem but I would just remove this checkpoint and simply keep incrementing elements and leave the last check fill the dict, which is simpler and less error prone. Any reason we want to flush when we are in USHRT_MAX elements @markshannon ?

To be clear, this is what I propose:

diff --git a/Python/compile.c b/Python/compile.c
index b2beef9327..a348b13676 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -3893,19 +3893,7 @@ compiler_dict(struct compiler *c, expr_ty e)
             ADDOP_I(c, DICT_UPDATE, 1);
         }
         else {
-            if (elements == USHRT_MAX) {
-                if (!compiler_subdict(c, e, i - elements, i + 1)) {
-                    return 0;
-                }
-                if (have_dict) {
-                    ADDOP_I(c, DICT_UPDATE, 1);
-                }
-                have_dict = 1;
-                elements = 0;
-            }
-            else {
                 elements++;
-            }
         }
     }
     if (elements) {

return 0;
}
if (have_dict) {
Expand Down