Skip to content

classmethod and staticmethod can crash with NULL / uninitialized callables #144330

Closed

Description

@colesbury

Crash report

The following code crashes in a debug build:

f = classmethod.__new__(classmethod, None)
print(f)

Or equivalently:

class MyStaticMethod(staticmethod):
    def __init__(self, func):
        print(self)

print(MyStaticMethod(lambda x: x))
Objects/unicodeobject.c:3103: const char *unicode_fromformat_arg(_PyUnicodeWriter *, const char *, va_list *): Assertion `obj' failed.

The problem is that sm_callable/cm_callable fields are only initialized in the __init__ function and not all code (like the repr) handles the NULL case.

Additionally, even though __func__ isn't writable, you can modify it by calling __init__ on an already initialized staticmethod or classmethod.

Linked PRs

Activity

  1. added
    interpreter-core(Objects, Python, Grammar, and Parser dirs)
    type-crashA hard crash of the interpreter, possibly with a core dump
    3.13only security fixes
    3.14bugs and security fixes
    3.15bugs and security fixes
    on Jan 29, 2026
  2. colesbury commented on Jan 29, 2026

    @colesbury
    ContributorAuthor

    For 3.15, it would be nice to move the initialization to a tp_new instead of tp_init and avoid all these problems

  3. Aniketsy commented on Jan 30, 2026

    @Aniketsy
    Contributor

    I’d be happy to work on this, as suggested.

  4. skirpichev commented on Jan 30, 2026

    @skirpichev
    Member

    it would be nice to move the initialization to a tp_new instead of tp_init and avoid all these problems

    I think we can't do this without prior deprecation, no? See #143715 for similar case.

  5. added a commit that references this issue on Feb 4, 2026
  6. vstinner commented on Feb 4, 2026

    @vstinner
    Member

    @colesbury:

    For 3.15, it would be nice to move the initialization to a tp_new instead of tp_init and avoid all these problems

    I wrote PR gh-144469 to implement this change.

  7. added a commit that references this issue on Feb 5, 2026
  8. vstinner commented on Feb 5, 2026

    @vstinner
    Member

    Oh, 3.13 and 3.14 are also affected, I reopen the issue.

    I wrote a simpler fix for 3.13 and 3.14: #144498

  9. added a commit that references this issue on Feb 5, 2026
  10. added a commit that references this issue on Feb 5, 2026
  11. added a commit that references this issue on Feb 6, 2026
  12. added a commit that references this issue on Feb 6, 2026
  13. added a commit that references this issue on Feb 6, 2026
  14. StanFromIreland commented on Feb 7, 2026

    @StanFromIreland
    Member

    Victor's backports were merged.

  15. added a commit that references this issue on Feb 15, 2026
  16. added a commit that references this issue on Apr 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.13only security fixes3.14bugs and security fixes3.15bugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)type-crashA hard crash of the interpreter, possibly with a core dump

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions