title: Writing to new Python buffer interface
body: <p>I have implemented the <strong>new</strong> python buffer interface in C++ outlined here:</p>
  
  <p><a href="https://docs.python.org/2/c-api/buffer.html" rel="nofollow">https://docs.python.org/2/c-api/buffer.html</a></p>
  
  <p>I have implemented my Py_buffer struct and filled it in:</p>
  
  <pre><code>template&lt;typename T&gt;
  static int getbuffer(PyObject *obj, Py_buffer *view, int flags) 
  {
      flags;
      static const Py_ssize_t suboffsets[1] = { 0};
      view-&gt;buf           = (void*)(_Cast&lt;T&gt;(obj)-&gt;getbuffer());
      view-&gt;obj           = NULL;
      view-&gt;len           = _Cast&lt;T&gt;(obj)-&gt;getbuffersize();
      view-&gt;itemsize      = 1;
      view-&gt;readonly      = _Cast&lt;T&gt;(obj)-&gt;getreadonly();
      view-&gt;ndim          = 0;
      view-&gt;format        = NULL;
      view-&gt;shape         = NULL;
      view-&gt;strides       = NULL;
      view-&gt;suboffsets    = NULL;
      view-&gt;internal      = NULL;
      return 0;
  }
  </code></pre>
  
  <p>I am creating my Python buffer class in Python and handing it to C++. I am getting a pyObject along with my Py_Buffer. <strong>So now my question is, how am I supposed to write and resize this pyBuffer in C++?</strong> I can get access to the pointer directly and a size. But if its a newly created buffer how do I tell it how much space I need? There does not seem to be any sort of resize function for me to call. </p>
  
  <p>I can use: <code>int result = PyBuffer_FromContiguous(&amp;m_view, const_cast&lt;void*&gt;(data), pySize, 'A');</code></p>
  
  <p>to add data to my buffer. But my buffer must already have the correct size or it wont write. I do not think this is the correct way to be using it anyway. </p>
  
  <p>Cython is not an option.</p>