diff options
author | INADA Naoki <songofacandy@gmail.com> | 2010-09-02 01:29:57 +0900 |
---|---|---|
committer | INADA Naoki <songofacandy@gmail.com> | 2010-09-02 01:29:57 +0900 |
commit | 0dc8938dbfff67760834bd97150fee5fdb8e875c (patch) | |
tree | 4781af3acd96da2a4c2f9d26672d28a3a34e2170 | |
parent | 76cc80c25d10d1224f0f16c6a74b531bf452aede (diff) | |
download | msgpack-python-0dc8938dbfff67760834bd97150fee5fdb8e875c.tar.gz |
python: Support Python3.
-rw-r--r-- | msgpack/__init__.py | 2 | ||||
-rw-r--r-- | msgpack/_msgpack.pyx | 37 | ||||
-rw-r--r-- | msgpack/unpack.h | 2 |
3 files changed, 21 insertions, 20 deletions
diff --git a/msgpack/__init__.py b/msgpack/__init__.py index 797b29c..9593714 100644 --- a/msgpack/__init__.py +++ b/msgpack/__init__.py @@ -1,3 +1,3 @@ # coding: utf-8 -from _msgpack import * +from msgpack._msgpack import * diff --git a/msgpack/_msgpack.pyx b/msgpack/_msgpack.pyx index 61ae36b..6a0b1a5 100644 --- a/msgpack/_msgpack.pyx +++ b/msgpack/_msgpack.pyx @@ -1,26 +1,24 @@ # coding: utf-8 -import cStringIO - cdef extern from "Python.h": ctypedef char* const_char_ptr "const char*" ctypedef struct PyObject - cdef object PyString_FromStringAndSize(const_char_ptr b, Py_ssize_t len) + cdef object PyBytes_FromStringAndSize(const_char_ptr b, Py_ssize_t len) cdef PyObject* Py_True cdef PyObject* Py_False - cdef char* PyString_AsString(object o) cdef long long PyLong_AsLongLong(object o) cdef unsigned long long PyLong_AsUnsignedLongLong(object o) - cdef int PyMapping_Check(object o) - cdef int PySequence_Check(object o) - cdef int PyLong_Check(object o) - cdef int PyInt_Check(object o) - cdef int PyFloat_Check(object o) - cdef int PyString_Check(object o) - cdef int PyUnicode_Check(object o) + cdef bint PyBool_Check(object o) + cdef bint PyMapping_Check(object o) + cdef bint PySequence_Check(object o) + cdef bint PyLong_Check(object o) + cdef bint PyInt_Check(object o) + cdef bint PyFloat_Check(object o) + cdef bint PyBytes_Check(object o) + cdef bint PyUnicode_Check(object o) cdef extern from "stdlib.h": void* malloc(size_t) @@ -81,10 +79,12 @@ cdef class Packer(object): if o is None: ret = msgpack_pack_nil(&self.pk) - elif <PyObject*>o == Py_True: - ret = msgpack_pack_true(&self.pk) - elif <PyObject*>o == Py_False: - ret = msgpack_pack_false(&self.pk) + #elif PyBool_Check(o): + elif isinstance(o, bool): + if o: + ret = msgpack_pack_true(&self.pk) + else: + ret = msgpack_pack_false(&self.pk) elif PyLong_Check(o): if o > 0: ullval = PyLong_AsUnsignedLongLong(o) @@ -98,7 +98,7 @@ cdef class Packer(object): elif PyFloat_Check(o): fval = o ret = msgpack_pack_double(&self.pk, fval) - elif PyString_Check(o): + elif PyBytes_Check(o): rawval = o ret = msgpack_pack_raw(&self.pk, len(o)) if ret == 0: @@ -133,7 +133,7 @@ cdef class Packer(object): ret = self.__pack(obj) if ret: raise TypeError - buf = PyString_FromStringAndSize(self.pk.buf, self.pk.length) + buf = PyBytes_FromStringAndSize(self.pk.buf, self.pk.length) self.pk.length = 0 return buf @@ -262,10 +262,11 @@ cdef class Unpacker(object): cdef char* buf = self.buf cdef Py_ssize_t tail = self.buf_tail cdef Py_ssize_t l + cdef bytes b for b in self.waiting_bytes: l = len(b) - memcpy(buf + tail, PyString_AsString(b), l) + memcpy(buf + tail, <char*>(b), l) tail += l self.buf_tail = tail del self.waiting_bytes[:] diff --git a/msgpack/unpack.h b/msgpack/unpack.h index 61a3786..9eb8ce7 100644 --- a/msgpack/unpack.h +++ b/msgpack/unpack.h @@ -175,7 +175,7 @@ static inline int template_callback_map_item(unpack_user* u, msgpack_unpack_obje 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 = PyString_FromStringAndSize(p, l); + py = PyBytes_FromStringAndSize(p, l); if (!py) return -1; *o = py; |