Skip to content
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

bpo-46161: fix the bug of starunpack_helper in compile.c #30235

Merged
merged 3 commits into from Jan 17, 2022
Merged

bpo-46161: fix the bug of starunpack_helper in compile.c #30235

merged 3 commits into from Jan 17, 2022

Conversation

zq1997
Copy link
Contributor

@zq1997 zq1997 commented Dec 23, 2021

pushed should also be considered when building tuples. Otherwise, when the remaining arguments are constant, it will ignore the pushed ones.

Consider this code,

class MyMetaclass(type): pass
use_metaclass = {'metaclass': MyMetaclass}
class MyClass(1, 2, 3, **use_metaclass): pass

and its bytecode,

  1           0 LOAD_BUILD_CLASS
              2 LOAD_CONST               0 (<code object MyMetaclass at 0x7f4dfea6db40, file "bug.py", line 1>)
              4 MAKE_FUNCTION            0
              6 LOAD_CONST               1 ('MyMetaclass')
              8 LOAD_NAME                0 (type)
             10 CALL_NO_KW               3
             12 STORE_NAME               1 (MyMetaclass)

  2          14 LOAD_CONST               2 ('metaclass')
             16 LOAD_NAME                1 (MyMetaclass)
             18 BUILD_MAP                1
             20 STORE_NAME               2 (use_metaclass)

  3          22 LOAD_BUILD_CLASS
             24 LOAD_CONST               3 (<code object MyClass at 0x7f4dfea6d540, file "bug.py", line 3>)
             26 MAKE_FUNCTION            0
             28 LOAD_CONST               4 ('MyClass')
             30 LOAD_CONST               5 ((1, 2, 3))
             32 BUILD_MAP                0
             34 LOAD_NAME                2 (use_metaclass)
             36 DICT_MERGE               1
             38 CALL_FUNCTION_EX         1
             40 STORE_NAME               3 (MyClass)
             42 LOAD_CONST               6 (None)
             44 RETURN_VALUE

Obviously, the arguments were incorrectly constructed, and the string MyClass was called instead of the class builder.

Traceback (most recent call last):
  File "xxxx/bug.py", line 3, in <module>
    class MyClass(1, 2, 3, **use_metaclass): pass
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: 'str' object is not callable

The more serious problem is that it makes the stack unbalanced.

https://bugs.python.org/issue46161

`pushed` should also be considered when building tuples
@the-knights-who-say-ni
Copy link

Hello, and thanks for your contribution!

I'm a bot set up to make sure that the project can legally accept this contribution by verifying everyone involved has signed the PSF contributor agreement (CLA).

Recognized GitHub username

We couldn't find a bugs.python.org (b.p.o) account corresponding to the following GitHub usernames:

@zq1997

This might be simply due to a missing "GitHub Name" entry in one's b.p.o account settings. This is necessary for legal reasons before we can look at this contribution. Please follow the steps outlined in the CPython devguide to rectify this issue.

You can check yourself to see if the CLA has been received.

Thanks again for the contribution, we look forward to reviewing it!

@zq1997 zq1997 changed the title fix the bug of starunpack_helper in compile.c bpo-46161: fix the bug of starunpack_helper in compile.c Dec 23, 2021
@sweeneyde
Copy link
Member

Hi @zq1997, thanks for the report and the patch! It would be nice if you could add some sort of regression test.

Also, as the bot said, make sure to get your CLA signed, and this should get a NEWS entry as well. Thanks!

@zq1997
Copy link
Contributor Author

zq1997 commented Dec 23, 2021

I have added the test code and NEWS entry. The CLA seems to take a while to take effect.

@sweeneyde
Copy link
Member

@markshannon Can this be merged now?

@markshannon markshannon merged commit c118c24 into python:main Jan 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants