Implemented encoding for strings

* Packer by default uses `utf-8` encoding by default
* Unpacker uses `None` by default, so no decoding is done
* Both pack and unpack has `encoding` and `unicode_errors` arguments,
  if `encoding` is `None` no encoding/decoding is done, otherwise
  it is python codec. `unicode_errors` is supplied as `errors`
  parameter to codec
This commit is contained in:
tailhook 2011-04-15 17:36:17 +03:00
parent af7113bb31
commit 752e3d1b78
5 changed files with 169 additions and 25 deletions

View file

@ -23,6 +23,8 @@ typedef struct unpack_user {
int use_list;
PyObject *object_hook;
PyObject *list_hook;
const char *encoding;
const char *unicode_errors;
} unpack_user;
@ -197,7 +199,11 @@ static inline int template_callback_map_end(unpack_user* u, msgpack_unpack_objec
static inline int template_callback_raw(unpack_user* u, const char* b, const char* p, unsigned int l, msgpack_unpack_object* o)
{
PyObject *py;
py = PyBytes_FromStringAndSize(p, l);
if(u->encoding) {
py = PyUnicode_Decode(p, l, u->encoding, u->unicode_errors);
} else {
py = PyBytes_FromStringAndSize(p, l);
}
if (!py)
return -1;
*o = py;