mirror of
https://github.com/msgpack/msgpack-python.git
synced 2025-12-08 06:09:49 +00:00
packer: Use PyUnicode_AsUTF8AndSize() for utf-8 (#272)
This commit is contained in:
parent
5534d0c7af
commit
60ef3879d7
3 changed files with 71 additions and 14 deletions
|
|
@ -67,6 +67,53 @@ static inline int msgpack_pack_write(msgpack_packer* pk, const char *data, size_
|
|||
|
||||
#include "pack_template.h"
|
||||
|
||||
// return -2 when o is too long
|
||||
static inline int
|
||||
msgpack_pack_unicode(msgpack_packer *pk, PyObject *o, long long limit)
|
||||
{
|
||||
#if PY_MAJOR_VERSION >= 3
|
||||
assert(PyUnicode_Check(o));
|
||||
|
||||
Py_ssize_t len;
|
||||
const char* buf = PyUnicode_AsUTF8AndSize(o, &len);
|
||||
if (buf == NULL)
|
||||
return -1;
|
||||
|
||||
if (len > limit) {
|
||||
return -2;
|
||||
}
|
||||
|
||||
int ret = msgpack_pack_raw(pk, len);
|
||||
if (ret) return ret;
|
||||
|
||||
return msgpack_pack_raw_body(pk, buf, len);
|
||||
#else
|
||||
PyObject *bytes;
|
||||
Py_ssize_t len;
|
||||
int ret;
|
||||
|
||||
// py2
|
||||
bytes = PyUnicode_AsUTF8String(o);
|
||||
if (bytes == NULL)
|
||||
return -1;
|
||||
|
||||
len = PyString_GET_SIZE(bytes);
|
||||
if (len > limit) {
|
||||
Py_DECREF(bytes);
|
||||
return -2;
|
||||
}
|
||||
|
||||
ret = msgpack_pack_raw(pk, len);
|
||||
if (ret) {
|
||||
Py_DECREF(bytes);
|
||||
return -1;
|
||||
}
|
||||
ret = msgpack_pack_raw_body(pk, PyString_AS_STRING(bytes), len);
|
||||
Py_DECREF(bytes);
|
||||
return ret;
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue