colesbury · GitHub

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

Read the original on github.com ↗