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

gh-128632: fix segfault on nested __classdict__ type param #128744

Open
wants to merge 14 commits into
base: main
Choose a base branch
from

Conversation

tom-pytel
Copy link
Contributor

@tom-pytel tom-pytel commented Jan 11, 2025

Add reserved name check in symtable.c:symtable_visit_type_param_bound_or_default() for __class__ and __classdict__ to prevent segfault when __classdict__ is used.

@bedevere-app
Copy link

bedevere-app bot commented Jan 11, 2025

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@@ -2607,6 +2607,14 @@ def test_continuation_bad_indentation(self):

self.assertRaises(IndentationError, exec, code)

@support.cpython_only
def test_disallowed_type_param_names(self):
# See gh-128632
Copy link
Member

Choose a reason for hiding this comment

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

Could you also test the following related cases:

  • Using a generic function or type alias instead of a class with these names for the type parameters
  • Using the names __classcell__ and __classdictcell__, which appear internally in the implementation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added func and alias tests. Didn't add __classcell__ or __classdictcell__ tests as those are not excluded and don't cause problems currently. Do you want to add them to the forbidden list?

>>> class A:
...     class B[__classcell__]: print(type(__classcell__))
...     
<class 'typing.TypeVar'>
>>> class A:
...     class B[__classdictcell__]: print(type(__classdictcell__))
...     
<class 'typing.TypeVar'>

Copy link
Member

Choose a reason for hiding this comment

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

Thanks for adding the tests! I want the *cell* names to also be tested, because they're also somewhat likely to cause problems, even if they don't currently crash.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So I interpret 'somewhat likely to cause problems' to mean yes disallow these names (and test them).

Copy link
Member

Choose a reason for hiding this comment

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

Since they work fine now we should not disallow them, especially because we'd want to backport this change. However, we should have a test to make sure future changes don't introduce crashes with these names.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Names re-allowed and tests added. Also re-allowed __class__ as that doesn't crash either even though it is special.

@tom-pytel
Copy link
Contributor Author

Just a reminder, according to @ZeroIntensity this needs backporting to 3.13 and 3.12.

@ZeroIntensity
Copy link
Member

I'm going to yield to Jelle. This seems complex enough that backporting could be too risky.

@tom-pytel
Copy link
Contributor Author

I'm going to yield to Jelle. This seems complex enough that backporting could be too risky.

Had a look and doesn't look too bad, same functions are present to fix.

@JelleZijlstra JelleZijlstra self-requested a review January 17, 2025 04:40
@JelleZijlstra JelleZijlstra added needs backport to 3.12 bug and security fixes needs backport to 3.13 bugs and security fixes labels Mar 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting merge needs backport to 3.12 bug and security fixes needs backport to 3.13 bugs and security fixes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants