Menu

#919 c++ private assignment operator not obeyed for arrays

None
closed
python (260)
5
2023-09-09
2008-05-23
No

Using swig 1.3.35 the python interface generated for this c++ code:

class foo
{

private:

const foo& operator=(const foo&);

};

foo bar[16];

will generate the __wrap.cxx file containing:

SWIGINTERN int Swig_var_bar_set(PyObject *_val) {
{
foo *inp = 0;
int res = SWIG_ConvertPtr(_val, SWIG_as_voidptrptr(&inp),
SWIGTYPE_p_foo, 0 );
if (!SWIG_IsOK(res)) {
SWIG_exception_fail(SWIG_ArgError(res), "in variable '""bar""'
of type '""foo [16]""'");
} else if (inp) {
size_t ii = 0;
for (; ii < (size_t)16; ++ii) bar[ii] = inp[ii];
} else {
SWIG_exception_fail(SWIG_ValueError, "invalid null reference "
"in variable '""bar""' of type '""foo [16]""'");
}
}
return 0;
fail:
return 1;
}

which calls the private assignment operator of foo.

A fix for this would also take into account when the private assignment operator is inherited from another class.

Discussion

  • Olly Betts

    Olly Betts - 2022-03-07

    Reproducible with current git master.

    An assignment operator normally returns a non-const reference, but changing that doesn't help.

    Here's a complete test case:

    $ cat test.i
    %module test
    %inline {
    class foo
    {
    
    private:
    
    const foo& operator=(const foo&);
    
    };
    
    foo bar[16];
    }
    $ ../preinst-swig -c++ -python test.i
    $ g++ `python3-config --cflags` -fPIC -shared test_wrap.cxx -o _test.so
    test_wrap.cxx: In function int Swig_var_bar_set(PyObject*):
    test_wrap.cxx:3152:73: error: const foo& foo::operator=(const foo&) is private within this context
     3152 |       for (; ii < (size_t)16; ++ii) *(foo *)&bar[ii] = *((foo *)inp + ii);
          |                                                                         ^
    test_wrap.cxx:3089:12: note: declared private here
     3089 | const foo& operator=(const foo&);
          |            ^~~~~~~~
    
     
  • William Fulton

    William Fulton - 2023-09-09
    • status: open --> closed
    • assigned_to: William Fulton
    • Group: -->
     

Log in to post a comment.