[Python] How to combine PyVarObject_HEAD_INIT and designated initializers?

Discussão em 'Python' iniciado por Stack, Agosto 14, 2025.

  1. Stack

    Stack Membro Participativo

    In my own "native" Python module I declare a new type this way:

    PyTypeObject calculatorType = {
    PyVarObject_HEAD_INIT(NULL, 0)
    .tp_name = "My.Calculator",
    .tp_basicsize = sizeof(PyMyCalculator),
    .tp_itemsize = 0,
    .tp_dealloc = (destructor)NULL,
    .tp_getattr = NULL,
    .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
    .tp_doc = "Python wrapper of My Calculator",
    .tp_methods = calculatorMethods,
    .tp_members = NULL,
    .tp_init = NULL,
    .tp_new = calcInitialize
    };


    This works, but CLang (the default compiler on FreeBSD) produces a warning:

    warning: mixture of designated and non-designated initializers in the same initializer list is a C99 extension [-Wc99-designator]


    This is because the PyVarObject_HEAD_INIT-macro does not explicitly name the fields...

    I like designated initializers for obvious reasons, but I don't want to trigger a useless warning either. Disabling it with -Wno-c99-designator would work for CLang, but on Linux we use g++14, which does not have this particular warning and complains:

    unrecognized command-line option '-Wno-c99-designator'


    How should I deal with it? I can make adding the flag conditional based on the operating system's name, but some day we might use CLang on Linux or GNU C++ on FreeBSD...

    Is there a way to query, from inside setup.py, which compiler will be invoked -- a way, that would work with Python as old as 3.6?

    Continue reading...

Compartilhe esta Página