Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
107 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
Advanced usage | ||
=============== | ||
|
||
Packer | ||
------ | ||
|
||
autoreset | ||
~~~~~~~~~ | ||
|
||
When you used ``autoreset=False`` option of :class:`~msgpack.Packer`, | ||
``pack()`` method doesn't return packed ``bytes``. | ||
|
||
You can use :meth:`~msgpack.Packer.bytes` or :meth:`~msgpack.Packer.getbuffer` to | ||
get packed data. | ||
|
||
``bytes()`` returns ``bytes`` object. ``getbuffer()`` returns some bytes-like | ||
object. It's concrete type is implement detail and it will be changed in future | ||
versions. | ||
|
||
You can reduce temporary bytes object by using ``Unpacker.getbuffer()``. | ||
|
||
.. code-block:: python | ||
packer = Packer(use_bin_type=True, autoreset=False) | ||
packer.pack([1, 2]) | ||
packer.pack([3, 4]) | ||
with open('data.bin', 'wb') as f: | ||
f.write(packer.getbuffer()) | ||
packer.reset() # reset internal buffer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ language data exchange. | |
:maxdepth: 1 | ||
|
||
api | ||
advanced |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#include "Python.h" | ||
|
||
/* cython does not support this preprocessor check => write it in raw C */ | ||
#if PY_MAJOR_VERSION == 2 | ||
static PyObject * | ||
buff_to_buff(char *buff, Py_ssize_t size) | ||
{ | ||
return PyBuffer_FromMemory(buff, size); | ||
} | ||
|
||
#elif (PY_MAJOR_VERSION == 3) && (PY_MINOR_VERSION >= 3) | ||
static PyObject * | ||
buff_to_buff(char *buff, Py_ssize_t size) | ||
{ | ||
return PyMemoryView_FromMemory(buff, size, PyBUF_READ); | ||
} | ||
#else | ||
static PyObject * | ||
buff_to_buff(char *buff, Py_ssize_t size) | ||
{ | ||
Py_buffer pybuf; | ||
if (PyBuffer_FillInfo(&pybuf, NULL, buff, size, 1, PyBUF_FULL_RO) == -1) { | ||
return NULL; | ||
} | ||
|
||
return PyMemoryView_FromBuffer(&pybuf); | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters