title: How to initialize a struct from a dict in cython
body: <p>Assuming I have a struct defined as such:</p>
  
  <pre><code>cdef extern from "blah.h":
      struct my_struct:
          int a
          int b
  </code></pre>
  
  <p>I need to be able to convert a <code>dict</code> into <code>my_struct</code>, without assuming any knowledge of my_struct's fields. In other words, I need the following conversion to happen:</p>
  
  <pre><code>def do_something(dict_arg):
      cdef my_struct s = dict_arg
      do_somthing_with_s(s)
  </code></pre>
  
  <p>The problem is that Cython won't do it: <a href="http://docs.cython.org/src/userguide/language_basics.html#automatic-type-conversions" rel="noreferrer">http://docs.cython.org/src/userguide/language_basics.html#automatic-type-conversions</a></p>
  
  <p>Of course, If I had knowledge on the <code>my_struct</code> field's name, I could do this:</p>
  
  <pre><code>def do_something(dict_arg):
      cdef my_struct s = my_struct(a=dict_arg['a'], b=dict_arg['b'])
      do_somthing_with_s(s)
  </code></pre>
  
  <p>Doing this crashes the cython compiler:</p>
  
  <pre><code>def do_something(dict_arg):
      cdef my_struct s = my_struct(**dict_arg)
      do_somthing_with_s(s)
  </code></pre>
  
  <p>The reason why I don't know the field's name is because the code is autogenerated, and I don't want to make an ugly hack to handle this case.</p>
  
  <p><strong>How to initialize a struct from a Python dict with Cython ?</strong></p>