Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7160155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:21:08+00:00 2026-05-28T13:21:08+00:00

SWIG seems to be generating incorrect bindings for converting a struct field of type

  • 0

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)
    %}
};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T13:21:09+00:00Added an answer on May 28, 2026 at 1:21 pm

    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 })
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

when generating python wrappers with swig the python wrapper classes in the generated python
I am attempting to create python bindings for some C++ code using swig. I
I'm trying to build a C++ extension for python using swig. I've followed the
I'm using SWIG to wrap C++ objects for use in lua, and Im trying
I'm trying to write a SWIG wrapper for a C library that uses pointers
I've been trying to use SWIG to wrap around a simple library that uses
I'm using the Ruby SVN bindings built with SWIG. Here's a little tutorial. When
I have a python module that imports a module generated with swig. When I
So I'm playing about with Python, C++0x, and SWIG 2.0. I've got a header
I used SWIG for generating some native JNI function interface for Irrlicht C/C++ 3D

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.