title: Passing binary data from Python to C API extension
body: <p>I'm writing a Python (<strong>2.6</strong>) extension, and I have a situation where I need to pass an opaque binary blob (with embedded null bytes) to my extension.</p>
  
  <p>Here is a snippet of my code:</p>
  
  <pre><code>from authbind import authenticate
  
  creds = 'foo\x00bar\x00'
  authenticate(creds)
  </code></pre>
  
  <p>which throws the following:</p>
  
  <pre><code>TypeError: argument 1 must be string without null bytes, not str
  </code></pre>
  
  <p>Here is some of authbind.cc:</p>
  
  <pre><code>static PyObject* authenticate(PyObject *self, PyObject *args) {
  
      const char* creds;
  
      if (!PyArg_ParseTuple(args, "s", &amp;creds))
          return NULL;
  }
  </code></pre>
  
  <p>So far, I have tried passing the blob as a raw string, like <code>creds = '%r' % creds</code>, but that not only gives me embedded quotes around the string but also turns the <code>\x00</code> bytes into their literal string representations, which I do not want to mess around with in C.</p>
  
  <p>How can I accomplish what I need? I know about the <code>y</code>, <code>y#</code> and <code>y*</code> PyArg_ParseTuple() format characters in 3.2, but I am limited to 2.6.</p>