Reputation: 614
SWIG seems to be generating incorrect bindings for converting a struct field of type map, resulting in a TypeError trying to set the map field to a python dictionary. Is there an error I am missing? an unsupported use-case? a bug in SWIG?
First the output
Traceback (most recent call last):
File ".\use_test.py", line 4, in <module>
struct.data = { 'A':1, 'B':2 }
File "C:\Users\kmahan\Projects\SwigTest\test.py", line 150, in <lambda>
__setattr__ = lambda self, name, value: _swig_setattr(self, MyStruct, name, value)
File "C:\Users\kmahan\Projects\SwigTest\test.py", line 49, in _swig_setattr
return _swig_setattr_nondynamic(self,class_type,name,value,0)
File "C:\Users\kmahan\Projects\SwigTest\test.py", line 42, in _swig_setattr_nondynamic
if method: return method(self,value)
TypeError: in method 'MyStruct_data_set', argument 2 of type 'std::map< std::string,unsigned int,std::less< std::string >,std::allocator< std::pair< std::string const,unsigned int > > > *'
And here is my test case:
test.i
%module test
%include "std_string.i"
%include "std_map.i"
namespace std {
%template(StringIntMap) map<string, unsigned int>;
}
%{
#include "test.h"
%}
%include "test.h"
test.h
#ifndef _TEST_H_
#define _TEST_H_
#include <string>
#include <map>
struct MyStruct
{
std::map<std::string, unsigned int> data;
};
#endif //_TEST_H_
test.cpp
#include "test.h"
run_test.py
import test
struct = test.MyStruct()
struct.data = { 'A':1, 'B':2 }
print struct.data
I build test_wrapper.cpp with swig -python -c++ -o test_wrapper.cpp test.i, compile everything else, and run run_test.py.
As a workaround I can explicitly define a setter instead (
void setData(const map<string, unsigned int>& data)
) which generates different conversion code -- it goes through traits_asptr instead of SWIG_ConvertPtr -- and seems to work but is not very pythonic!
EDIT
Here is my .i file that pipes gets and sets of the attribute itself to C++ getters and setters. I think this is what Nathan suggested in the comment below his answer.
%module test
%include "std_string.i"
%include "std_map.i"
namespace std {
%template(StringIntMap) map<string, unsigned int>;
}
struct MyStruct
{
const std::map<std::string, unsigned int>& getData() const;
void setData(const std::map<std::string, unsigned int>&);
%pythoncode %{
__swig_getmethods__["data"] = getData
__swig_setmethods__["data"] = setData
if _newclass:
data = _swig_property(getData, setData)
%}
};
Upvotes: 3
Views: 1872
Reputation: 9124
When you're setting struct.data
, it's expecting a test.StringIntMap
, not a python dict
.
The easiest thing is for you to do this:
struct.data = test.StringIntMap({ 'A':1, 'B':2 })
Upvotes: 1