summaryrefslogtreecommitdiff
path: root/msgpack/pack.h
diff options
context:
space:
mode:
authorINADA Naoki <methane@users.noreply.github.com>2018-01-11 19:41:05 +0900
committerGitHub <noreply@github.com>2018-01-11 19:41:05 +0900
commit60ef3879d792ec92480cf9d6d610951657c2e8c7 (patch)
tree15d22f6c87d2da77feb86e8d09e8eb9a452bf4f5 /msgpack/pack.h
parent5534d0c7af0114db3d27f7b96c82a7fe22ce1e40 (diff)
downloadmsgpack-python-60ef3879d792ec92480cf9d6d610951657c2e8c7.tar.gz
packer: Use PyUnicode_AsUTF8AndSize() for utf-8 (#272)
Diffstat (limited to 'msgpack/pack.h')
-rw-r--r--msgpack/pack.h47
1 files changed, 47 insertions, 0 deletions
diff --git a/msgpack/pack.h b/msgpack/pack.h
index 3bc21ea..4f3ce1d 100644
--- a/msgpack/pack.h
+++ b/msgpack/pack.h
@@ -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