title: How to get precise struct size using Python 'struct' module
body: <p>I'm trying to send a message over TCP from an C (Obj-C actually, but object orientation is out of the equation here) client to a python server. Now I'm sending an <code>unsigned short</code> first with the message size and then the message, which is a C struct. I wanted to append a dynamic string at the end of the package, so I decided to use the struct size to split the package in two, but then the problems began.</p>
  
  <p>The thing is, either I'm doing something wrong, or there's a bug on Python's struct library size + padding calculation. </p>
  
  <p>Python struct seems to resolve paddings correctly. For example, for this struct:</p>
  
  <pre><code>struct.Struct("H I").size == 8
  </code></pre>
  
  <p>which matches with <code>sizeof</code> return value for this struct:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  typedef struct {
          unsigned short a;
          unsigned int b;
  } test;
  
  int main() {
          printf("%ld\n", sizeof(test));
          return 0;
  }
  
  $ gcc test.c 
  $ ./a.out
  8
  </code></pre>
  
  <p>But I'm not getting always the same result for some struct definitions. For example, in this case:</p>
  
  <pre><code>struct.Struct("H 5s").size == 7
  
  typedef struct {
      unsigned short a;
      char b[5];
  } test;
  sizeof(test) == 8
  </code></pre>
  
  <p>I've read somewhere that the compiler may fill an struct to ensure proper memory access when using structs in an array. I'm not sure if this is the case (seems to be), but if it is, I can't understand why this struct doesn't get padded to 8 bytes (assuming a 4 bytes packing):</p>
  
  <pre><code>struct.Struct("H 4s").size == 6
  
  typedef struct {
      unsigned short a;
      char b[4];
  } test;
  sizeof(test) == 6
  </code></pre>
  
  <p><strong>So, to clarify, my question is how may I get the precise size of a given struct in Python, since it is not applying the final padding.</strong></p>
  
  <hr>
  
  <p><em>What I've tried:</em></p>
  
  <p>Add final padding size manually:</p>
  
  <pre><code>real_struct_size = self._struct.size + self._struct.size % 4
  </code></pre>
  
  <p>This, of course, didn't work because a single member struct does not adds padding, and as you can see in the last case it doesn't work for small structs neither (unsigned short + char[4]). (Maybe I'm over-simplifying the problem here. Maybe it's not about small structs but related to another factor that I can't identify.)</p>
  
  <p>Then I opened the Python's struct library to see how may I find out how many parameters are expected so I can ask if it is 1, then avoid the final padding, but there's not way to access the <code>s_len</code> property of <code>PyStructObject</code> (see Python-2.7.5/Modules/_struct.c:48) which is where the qty of packed parameters are stored.</p>
  
  <p>So, as a workaround I put an offset value at the beginning of the data packet to know where the extra/dynamic string starts.</p>
  
  <p>But I think there's a bug here (mine or from Python's struct library). Either way, if it's me I really need to know what I'm doing wrong, or if it's Python's library I want to report the issue. If someone could help me to get to the bottom of this, I'll be very thankful.</p>
  
  <p>So, thanks in advance! Sorry for the long post :)</p>