GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — `_operator`: `methodcaller_clear` has the wrong signature for the `tp_clear` slot (returns `void`, not `int`) · Issue #156762 · python/cpython

_operator: methodcaller_clear has the wrong signature for the tp_clear slot (returns void, not int) #156762

New issue
Copy link
New issue
Copy link
Closed
Closed
_operator: methodcaller_clear has the wrong signature for the tp_clear slot (returns void, not int)#156762
Copy link
Labels
3.14bugs and security fixesbugs and security fixes3.15bugs and security fixesbugs and security fixes3.16new features, bugs and security fixesnew features, bugs and security fixesextension-modulesC modules in the Modules dirC modules in the Modules dirtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@ashm-dev
ashm-dev
opened on Sep 1, 2026
Issue body actions

Bug report

Bug description:

methodcaller_clear in Modules/_operator.c is declared as returning void, but it is
installed in the Py_tp_clear slot, which requires inquiry
(typedef int (*inquiry)(PyObject *);, Include/object.h:285).

/* Modules/_operator.c:1743 */
static void
methodcaller_clear(PyObject *op)
{
    methodcallerobject *mc = methodcallerobject_CAST(op);
    Py_CLEAR(mc->name);
    Py_CLEAR(mc->args);
    Py_CLEAR(mc->kwds);
    Py_CLEAR(mc->vectorcall_args);
    Py_CLEAR(mc->vectorcall_kwnames);
}

/* Modules/_operator.c:1928 */
static PyType_Slot methodcaller_type_slots[] = {
    ...
    {Py_tp_clear, methodcaller_clear},
    ...
};

The mismatch is not diagnosed at compile time because PyType_Slot.pfunc is void *
(Include/object.h:327), so the function pointer converts implicitly and is later cast
back to inquiry.

UBSan with -fsanitize=function reports it whenever a methodcaller instance is cleared
by the cyclic GC:

Python/gc_free_threading.c:1761:24: runtime error: call to function methodcaller_clear through pointer to incorrect function type 'int (*)(struct _object *)'
Modules/_operator.c:1745: note: methodcaller_clear defined here

The trace above is from a free-threaded build. The defect is in the slot registration, not in
either GC, so it is present in both builds and both reach the bad call the same way:

  • default build: Python/gc.c:1105
  • free-threaded build: Python/gc_free_threading.c:1761

Impact

Benign on the mainstream targets I checked. Both GC implementations discard the result
((void) clear(op); at Python/gc.c:1105 and Python/gc_free_threading.c:1761), and
operator.methodcaller does not set Py_TPFLAGS_BASETYPE, so the return baseclear(self);
path in subtype_clear (Objects/typeobject.c:2751) cannot reach it.

It is still an indirect call through an incompatible function type, i.e. undefined behaviour,
and it matters for:

  • builds using Clang CFI (-fsanitize=cfi-icall), where an indirect-call type mismatch aborts;
  • WebAssembly targets, where indirect call signatures are checked by the runtime;
  • UBSan builds in general, where it is persistent noise that hides other findings.

methodcaller_traverse immediately below (Modules/_operator.c:1764) uses the correct
static int form, so this looks like an oversight from the slot-signature cleanup.

I grepped every tp_clear slot registration under Modules/, Objects/ and Python/:
methodcaller_clear is the only one with a void return type.

Reproducer

The wrong slot signature is present in every build; a sanitizer build is only what makes it
visible. Configured with:

CC=clang CXX=clang++ LDFLAGS='-fuse-ld=lld' ./configure --with-address-sanitizer --with-undefined-behavior-sanitizer --with-pydebug --disable-gil

--with-undefined-behavior-sanitizer adds -fsanitize=undefined (configure.ac:3528), and
Clang includes function in that group, which is the check that fires here.

Then:

./python -c "import gc, operator
class C: pass
c = C()
c.m = operator.methodcaller('foo', c)
del c
gc.collect()"

The methodcaller keeps a reference to c through its args tuple, so the cycle
c -> m -> args -> c reaches tp_clear and the diagnostic fires.

Suggested fix

-static void
+static int
 methodcaller_clear(PyObject *op)
 {
     methodcallerobject *mc = methodcallerobject_CAST(op);
     Py_CLEAR(mc->name);
     Py_CLEAR(mc->args);
     Py_CLEAR(mc->kwds);
     Py_CLEAR(mc->vectorcall_args);
     Py_CLEAR(mc->vectorcall_kwnames);
+    return 0;
 }

methodcaller_dealloc (Modules/_operator.c:1759) already calls it as
(void)methodcaller_clear(op); and needs no change.

Environment

  • CPython main, commit b93576a2b66
  • Linux x86-64
  • Clang 22.1.8, lld
  • Free-threaded debug build with ASan and UBSan:
CC=clang CXX=clang++ LDFLAGS='-fuse-ld=lld' ./configure --with-address-sanitizer --with-undefined-behavior-sanitizer --with-pydebug --disable-gil

CPython versions tested on:

3.16, CPython main branch, 3.15

Operating systems tested on:

Linux

Linked PRs

  • gh-156762: Fix tp_clear slot signature for operator.methodcaller #156769
  • [3.15] gh-156762: Fix tp_clear slot signature for operator.methodcaller() (GH-156769) #157448
  • [3.14] gh-156762: Fix tp_clear slot signature for operator.methodcaller() (GH-156769) #157449

Activity

  1. ashm-dev
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Sep 1, 2026
  2. sergey-miryanov
    added
    extension-modulesC modules in the Modules dirC modules in the Modules dir
    3.15bugs and security fixesbugs and security fixes
    3.16new features, bugs and security fixesnew features, bugs and security fixes
    3.14bugs and security fixesbugs and security fixes
    on Sep 1, 2026
  3. seth-957
    added a commit that references this issue on Sep 6, 2026

    pythongh-156762: Fix methodcaller_clear having wrong signature for tp…

    9d45fc5
  4. vstinner commented on Sep 11, 2026

    @vstinner
    vstinner
    on Sep 11, 2026
    Member
    More actions

    This issue reminds me issue gh-111178 which fixed tons of bugs related to -fsanitize=function.

  5. vstinner commented on Sep 11, 2026

    @vstinner
    vstinner
    on Sep 11, 2026
    Member
    More actions

    Using the configure command and the command of "Reproducer", I'm able to reproduce the error on Fedora 44 (clang version 22.1.8).

    Reacted by Shamil
  6. vstinner commented on Sep 11, 2026

    @vstinner
    vstinner
    on Sep 11, 2026
    Member
    More actions

    I ran ./python -m test -j0 -r -u all,-gui on Python built with ./configure --with-pydebug CFLAGS=-fsanitize=function LDFLAGS=-fsanitize=function CC=clang LD=clang --disable-gil: the full Python test suite passed successfully.

    Reacted by Shamil
  7. vstinner
    added 2 commits that reference this issue on Sep 13, 2026

    gh-156762: Fix tp_clear slot signature for operator.methodcaller() (#…

    a4ca6e8

    [3.14] gh-156762: Fix tp_clear slot signature for operator.methodcall…

    cb81ae4
  8. johnslavik
    added a commit that references this issue on Sep 14, 2026

    pythongh-156762: Fix tp_clear slot signature for operator.methodcalle…

    6f369bb
  9. hugovk
    added a commit that references this issue on Sep 15, 2026

    [3.15] gh-156762: Fix tp_clear slot signature for operator.methodcall…

    54d12bd
  10. hugovk
    closed this as completedon Sep 15, 2026
  11. nakedtatsuya
    added a commit that references this issue on Nov 1, 2026

    Merge pull request #4 from Progate/tatsuyafukui/methodcaller-clear-si…

    547a512
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.14bugs and security fixesbugs and security fixes3.15bugs and security fixesbugs and security fixes3.16new features, bugs and security fixesnew features, bugs and security fixesextension-modulesC modules in the Modules dirC modules in the Modules dirtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.