saghul/pyuv — pull request #4 by weak (2012-01-06 09:42:00+00:00)
Title: write support for python strings with NULL characters and Unicode strings (interpreted as UTF-8)

I'm really enjoying pyuv! Great idea and well implemented!

I'm working on implementing pyuv into an existing client application of mine which communicates to a server using a binary data protocol. This protocol uses byte 0 ('\0') extensively. It looks like the stream#write method interprets data as a string which causes the data to be truncated. If I try:

```python
tcp_handle.write('\x12\x00\x04', None)
```

The data will be cut off after \x12.

I wasn't sure the best way to go about solving this issue, but I gave it a try by using PyObject_GetBuffer and memcpy. I should note that I'm a bit of a novice with CPython, so if I have made any mistakes, I apologize.

Additionally, the protocol i'm using exceeds the bounds of ascii causing python to convert occasionally convert strings to unicode. I added support for parsing Unicode strings and converting to UTF-8. I'm not sure if this is the best solution either, perhaps adding another parameter to write for the target encoding would be a better way to go about doing this (much like Buffer.write in node.js has an encoding parameter).