FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Convert Packer's buf_size once by hdimer · Pull Request #726 · msgpack/msgpack-python · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) .pyx  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
2 changes: 1 addition & 1 deletion msgpack/_packer.pyx
This file contains hidden or 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
Expand Up @@ -110,7 +110,7 @@ cdef class Packer:
cdef bint autoreset
cdef bint datetime

def __cinit__(self, buf_size=256*1024, **_kwargs):
def __cinit__(self, size_t buf_size=256*1024, **_kwargs):
self.pk.buf = <char*> PyMem_Malloc(buf_size)
if self.pk.buf == NULL:
raise MemoryError("Unable to allocate internal buffer.")
Expand Down
21 changes: 21 additions & 0 deletions test/test_pack.py
This file contains hidden or 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
Expand Up @@ -179,3 +179,24 @@ def test_get_buffer():

expected = packb([1, 2], use_bin_type=True)
assert written == expected


@pytest.mark.skipif(
Packer.__module__ == "msgpack.fallback",
reason="buf_size only allocates in the C extension",
)
def test_buf_size_is_converted_once():
# Asking twice let the allocation and the recorded capacity disagree,
# so the packer overflowed a buffer smaller than the size it recorded.
class Counting:
count = 0

def __int__(self):
self.count += 1
return 600

__index__ = __int__

buf_size = Counting()
Packer(buf_size=buf_size)
assert buf_size.count == 1

Back | FazBrowse Home | New Git URL