Skip to content

Clinic causes compiler warnings when using a getter and setter with a docstring #127341

Closed
@ZeroIntensity

Description

@ZeroIntensity

Bug report

Bug description:

I noticed this while working on GH-126890, as some odd macro redefinition warnings came up from the generated clinic file. As it turns out, clinic has a bug where using both @getter and @setter where the getter has a docstring doesn't work correctly. Quick reproducer:

/*[clinic input]
@getter
Test.test

My silly docstring
[clinic start generated code]*/

/*[clinic input]
@setter
Test.test
[clinic start generated code]*/

This results in generated code that looks like this:

PyDoc_STRVAR(Test_test__doc__,
"My silly docstring");
#define Test_test_HAS_DOCSTR

#if defined(Test_test_HAS_DOCSTR)
#  define Test_test_DOCSTR Test_test__doc__
#else
#  define Test_test_DOCSTR NULL
#endif
#if defined(TEST_TEST_GETSETDEF)
#  undef TEST_TEST_GETSETDEF
#  define TEST_TEST_GETSETDEF {"test", (getter)Test_test_get, (setter)Test_test_set, Test_test_DOCSTR},
#else
#  define TEST_TEST_GETSETDEF {"test", (getter)Test_test_get, NULL, Test_test_DOCSTR},
#endif

static PyObject *
Test_test_get_impl(TestObj *self);

static PyObject *
Test_test_get(TestObj *self, void *Py_UNUSED(context))
{
    return Test_test_get_impl(self);
}

#if defined(TEST_TEST_HAS_DOCSTR)
#  define Test_test_DOCSTR Test_test__doc__
#else
#  define Test_test_DOCSTR NULL
#endif
#if defined(TEST_TEST_GETSETDEF)
#  undef TEST_TEST_GETSETDEF
#  define TEST_TEST_GETSETDEF {"test", (getter)Test_test_get, (setter)Test_test_set, Test_test_DOCSTR},
#else
#  define TEST_TEST_GETSETDEF {"test", NULL, (setter)Test_test_set, NULL},
#endif

There's two bugs here:

  • The setter uses the wrong name for the HAS_DOCSTR part; it uses TEST_TEST_HAS_DOCSTR while Test_test_HAS_DOCSTR, so the resulting docstring ends up being redefined to NULL.
  • Even with that fixed, the docstring macro would get redefined anyway.

The simplest fix is to just drop the HAS_DOCSTR entirely, and just check whether the actual DOCSTR macro is defined or not, and then set it to NULL in that case (thanks @erlend-aasland for this suggestion).

I've created GH-127310 as a fix.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.13bugs and security fixes3.14bugs and security fixesbuildThe build process and cross-buildtopic-argument-clinictype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions