Skip to content

Commit 29847ae

Browse files
committed
Fix intake#65: support new buffer API
1 parent 6962825 commit 29847ae

File tree

2 files changed

+39
-17
lines changed

2 files changed

+39
-17
lines changed

snappy/snappymodule.cc

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -91,29 +91,30 @@ snappy_strerror(snappy_status status)
9191
static PyObject *
9292
snappy__compress(PyObject *self, PyObject *args)
9393
{
94-
const char * input;
95-
int input_size;
94+
Py_buffer input;
9695
size_t compressed_size, actual_size;
9796
PyObject * result;
9897
snappy_status status;
9998

10099
#if PY_MAJOR_VERSION >= 3
101-
if (!PyArg_ParseTuple(args, "y#", &input, &input_size))
100+
if (!PyArg_ParseTuple(args, "y*", &input))
102101
#else
103-
if (!PyArg_ParseTuple(args, "s#", &input, &input_size))
102+
if (!PyArg_ParseTuple(args, "s*", &input))
104103
#endif
105104
return NULL;
106105

107106
// Ask for the max size of the compressed object.
108-
compressed_size = snappy_max_compressed_length(input_size);
107+
compressed_size = snappy_max_compressed_length(input.len);
109108

110109
// Make snappy compression
111110
result = PyBytes_FromStringAndSize(NULL, compressed_size);
112111
if (result) {
113112
actual_size = compressed_size;
114113
Py_BEGIN_ALLOW_THREADS
115-
status = snappy_compress(input, input_size, PyBytes_AS_STRING(result), &actual_size);
114+
status = snappy_compress((const char *) input.buf, input.len,
115+
PyBytes_AS_STRING(result), &actual_size);
116116
Py_END_ALLOW_THREADS
117+
PyBuffer_Release(&input);
117118
if (status == SNAPPY_OK) {
118119
return maybe_resize(result, compressed_size, actual_size);
119120
}
@@ -123,30 +124,33 @@ snappy__compress(PyObject *self, PyObject *args)
123124
PyErr_Format(SnappyCompressError,
124125
"Error while compressing: %s", snappy_strerror(status));
125126
}
126-
127-
PyErr_Format(SnappyCompressError,
128-
"Error while compressing: unable to acquire output string");
127+
else {
128+
PyBuffer_Release(&input);
129+
PyErr_Format(SnappyCompressError,
130+
"Error while compressing: unable to acquire output string");
131+
}
129132
return NULL;
130133
}
131134

132135
static PyObject *
133136
snappy__uncompress(PyObject *self, PyObject *args)
134137
{
135-
const char * compressed;
136-
int comp_size;
138+
Py_buffer compressed;
137139
size_t uncomp_size, actual_size;
138140
PyObject * result;
139141
snappy_status status;
140142

141143
#if PY_MAJOR_VERSION >=3
142-
if (!PyArg_ParseTuple(args, "y#", &compressed, &comp_size))
144+
if (!PyArg_ParseTuple(args, "y*", &compressed))
143145
#else
144-
if (!PyArg_ParseTuple(args, "s#", &compressed, &comp_size))
146+
if (!PyArg_ParseTuple(args, "s*", &compressed))
145147
#endif
146148
return NULL;
147149

148-
status = snappy_uncompressed_length(compressed, comp_size, &uncomp_size);
150+
status = snappy_uncompressed_length((const char *) compressed.buf, compressed.len,
151+
&uncomp_size);
149152
if (status != SNAPPY_OK) {
153+
PyBuffer_Release(&compressed);
150154
PyErr_SetString(SnappyCompressedLengthError,
151155
"Can not calculate uncompressed length");
152156
return NULL;
@@ -156,17 +160,22 @@ snappy__uncompress(PyObject *self, PyObject *args)
156160
if (result) {
157161
actual_size = uncomp_size;
158162
Py_BEGIN_ALLOW_THREADS
159-
status = snappy_uncompress(compressed, comp_size, PyBytes_AS_STRING(result), &actual_size);
163+
status = snappy_uncompress((const char *) compressed.buf, compressed.len,
164+
PyBytes_AS_STRING(result), &actual_size);
160165
Py_END_ALLOW_THREADS
166+
PyBuffer_Release(&compressed);
161167
if (SNAPPY_OK == status) {
162168
return maybe_resize(result, uncomp_size, actual_size);
163169
}
164170
else {
165171
Py_DECREF(result);
172+
PyErr_Format(SnappyUncompressError,
173+
"Error while decompressing: %s", snappy_strerror(status));
166174
}
167175
}
168-
PyErr_Format(SnappyUncompressError,
169-
"Error while decompressing: %s", snappy_strerror(status));
176+
else {
177+
PyBuffer_Release(&compressed);
178+
}
170179
return NULL;
171180
}
172181

test_snappy.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,19 @@ def test_big_string(self):
7777
compressed = snappy.compress(text)
7878
self.assertEqual(text, snappy.decompress(compressed))
7979

80+
def test_compress_memoryview(self):
81+
data = b"hello world!"
82+
expected = snappy.compress(data)
83+
actual = snappy.compress(memoryview(data))
84+
self.assertEqual(actual, expected)
85+
86+
def test_decompress_memoryview(self):
87+
data = b"hello world!"
88+
compressed = snappy.compress(data)
89+
expected = snappy.uncompress(compressed)
90+
actual = snappy.uncompress(memoryview(compressed))
91+
self.assertEqual(actual, expected)
92+
8093

8194
class SnappyValidBufferTest(TestCase):
8295

0 commit comments

Comments
 (0)