Skip to content

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

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

Closed
ZeroIntensity opened this issue Nov 27, 2024 · 1 comment · Fixed by #127310
Closed
Labels
3.13 bugs and security fixes 3.14 new features, bugs and security fixes build The build process and cross-build topic-argument-clinic type-bug An unexpected behavior, bug, or error

Comments

@ZeroIntensity
Copy link
Member

ZeroIntensity commented Nov 27, 2024

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

@ZeroIntensity ZeroIntensity added type-bug An unexpected behavior, bug, or error build The build process and cross-build topic-argument-clinic labels Nov 27, 2024
erlend-aasland added a commit that referenced this issue Nov 29, 2024
…cstrings (#127310)

Co-authored-by: Erlend E. Aasland <erlend@python.org>
@erlend-aasland erlend-aasland added 3.13 bugs and security fixes 3.14 new features, bugs and security fixes labels Nov 29, 2024
ZeroIntensity added a commit to ZeroIntensity/cpython that referenced this issue Nov 29, 2024
…tters with docstrings (pythonGH-127310)

(cherry picked from commit 9949091)

Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
Co-authored-by: Erlend E. Aasland <erlend@python.org>
erlend-aasland added a commit that referenced this issue Nov 30, 2024
…with docstrings (GH-127310) (#127431)

(cherry picked from commit 9949091)

Co-authored-by: Erlend E. Aasland <erlend@python.org>
@erlend-aasland
Copy link
Contributor

Thanks, Peter!

srinivasreddy pushed a commit to srinivasreddy/cpython that referenced this issue Jan 8, 2025
…ith docstrings (python#127310)

Co-authored-by: Erlend E. Aasland <erlend@python.org>
ebonnal pushed a commit to ebonnal/cpython that referenced this issue Jan 12, 2025
…ith docstrings (python#127310)

Co-authored-by: Erlend E. Aasland <erlend@python.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.13 bugs and security fixes 3.14 new features, bugs and security fixes build The build process and cross-build topic-argument-clinic type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants