Skip to content

Commit 974f6a4

Browse files
committed
adding a custom rule for memoryview
1 parent 27abbb7 commit 974f6a4

1 file changed

Lines changed: 24 additions & 9 deletions

File tree

wolframclient/deserializers/wxf/wxfparser.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,28 @@
1010
)
1111
from wolframclient.serializers.wxfencoder.streaming import ExactSizeReader, ZipCompressedReader
1212
from wolframclient.utils import six
13+
from wolframclient.utils.dispatch import Dispatch
14+
15+
wxf_input_to_buffer = Dispatch()
16+
17+
@wxf_input_to_buffer.dispatch((six.binary_type, six.buffer_types))
18+
def encode_buffer(wxf_input):
19+
return six.BytesIO(wxf_input)
20+
21+
if six.PY2:
22+
@wxf_input_to_buffer.dispatch(memoryview, replace_existing = True)
23+
def encode_buffer(wxf_input):
24+
return six.BytesIO(wxf_input.tobytes())
25+
26+
@wxf_input_to_buffer.dispatch(object)
27+
def encode_default(wxf_input):
28+
if hasattr(wxf_input, 'read'):
29+
return wxf_input
30+
raise TypeError(
31+
"Class %s neither implements a read method nor is a binary type."
32+
% wxf_input.__class__.__name__
33+
)
34+
1335

1436

1537
class WXFParser(object):
@@ -58,15 +80,8 @@ def __init__(self, wxf_input):
5880
"""WXF parser returning Python object from a WXF encoded byte sequence.
5981
"""
6082
self.context = SerializationContext()
61-
if isinstance(wxf_input, (six.binary_type, six.buffer_types)):
62-
self.reader = six.BytesIO(wxf_input)
63-
elif hasattr(wxf_input, "read"):
64-
self.reader = wxf_input
65-
else:
66-
raise TypeError(
67-
"Class %s neither implements a read method nor is a binary type."
68-
% wxf_input.__class__.__name__
69-
)
83+
self.reader = wxf_input_to_buffer(wxf_input)
84+
7085
version, compress = self.parse_header()
7186
if compress == True:
7287
self.reader = ZipCompressedReader(self.reader)

0 commit comments

Comments
 (0)