#1246 Swig python C interface files warn with -Wunused

closed-fixed
python (259)
5
2012-08-08
2012-05-26
John Ralls
No

The following simple patch prevents the warnings:
--- a/Lib/python/pyinit.swg (revision 13127)
+++ b/Lib/pythonpyinit.swg (working copy)
@@ -324,7 +324,10 @@
void
#endif
SWIG_init(void) {
- PyObject *m, *d, *md;
+ PyObject *m, *d;
+#if defined(SWIGPYTHON_BUILTIN)
+ PyObject *md;
+#endif
#if PY_VERSION_HEX >= 0x03000000
static struct PyModuleDef SWIG_module = {
# if PY_VERSION_HEX >= 0x03020000

Discussion

  • William Fulton
    William Fulton
    2012-05-28

    Which version are you using? I can't replicate this and it looks to me as though md is used all the time.

     
  • John Ralls
    John Ralls
    2012-05-28

    I encountered the problem with Swig-2.0.5, but the code is the same in trunk (which is what the patch is against). Note that you must use a version of gcc which implements "set-but-not-used" (I've got 4.6.2), and of course SWIGPYTHON_BUILTIN mustn't be defined. Here's an example error message:

    swig-unittest-support-python.c: In function 'init_unittest_support':
    swig-unittest-support-python.c:4341:21: error: variable 'md' set but not used [-Werror=unused-but-set-variable]

    I notice, thougn, an omission from my patch, which should be:
    Index: pyinit.swg
    ===================================================================
    --- a/Lib/python/pyinit.swg (revision 13127)
    +++ b/Lib/python/pyinit.swg (working copy)
    @@ -324,7 +324,10 @@
    void
    #endif
    SWIG_init(void) {
    - PyObject *m, *d, *md;
    + PyObject *m, *d;
    +#if defined(SWIGPYTHON_BUILTIN)
    + PyObject *md;
    +#endif
    #if PY_VERSION_HEX >= 0x03000000
    static struct PyModuleDef SWIG_module = {
    # if PY_VERSION_HEX >= 0x03020000
    @@ -397,7 +400,7 @@
    #else
    m = Py_InitModule((char *) SWIG_name, SwigMethods);
    #endif
    - md = d = PyModule_GetDict(m);
    + d = PyModule_GetDict(m);

    SWIG_InitializeModule(0);

    @@ -428,7 +431,7 @@
    public_interface = PyList_New(0);
    public_symbol = 0;
    (void)public_symbol;
    -
    + md = d;
    PyDict_SetItemString(md, "__all__", public_interface);
    Py_DECREF(public_interface);
    for (i = 0; SwigMethods[i].ml_name != NULL; ++i)

    I had missed the bit where I moved the initialization of md inside the second SWIGPYTHON_BUILTIN block.

     
  • William Fulton
    William Fulton
    2012-08-08

    • assigned_to: nobody --> wsfulton
    • status: open --> closed-fixed
     
  • William Fulton
    William Fulton
    2012-08-08

    md is also used if not using -builtin so the patch won't work. I've suppressed the error using '(void)md;' instead.

     
  • William Fulton
    William Fulton
    2012-08-08

    md is also used if not using -builtin so the patch won't work. I've suppressed the error using '(void)md;' instead.