@@ -91,29 +91,30 @@ snappy_strerror(snappy_status status)
91
91
static PyObject *
92
92
snappy__compress (PyObject *self, PyObject *args)
93
93
{
94
- const char * input;
95
- int input_size;
94
+ Py_buffer input;
96
95
size_t compressed_size, actual_size;
97
96
PyObject * result;
98
97
snappy_status status;
99
98
100
99
#if PY_MAJOR_VERSION >= 3
101
- if (!PyArg_ParseTuple (args, " y# " , &input, &input_size ))
100
+ if (!PyArg_ParseTuple (args, " y* " , &input))
102
101
#else
103
- if (!PyArg_ParseTuple (args, " s# " , &input, &input_size ))
102
+ if (!PyArg_ParseTuple (args, " s* " , &input))
104
103
#endif
105
104
return NULL ;
106
105
107
106
// 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 );
109
108
110
109
// Make snappy compression
111
110
result = PyBytes_FromStringAndSize (NULL , compressed_size);
112
111
if (result) {
113
112
actual_size = compressed_size;
114
113
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);
116
116
Py_END_ALLOW_THREADS
117
+ PyBuffer_Release (&input);
117
118
if (status == SNAPPY_OK) {
118
119
return maybe_resize (result, compressed_size, actual_size);
119
120
}
@@ -123,30 +124,33 @@ snappy__compress(PyObject *self, PyObject *args)
123
124
PyErr_Format (SnappyCompressError,
124
125
" Error while compressing: %s" , snappy_strerror (status));
125
126
}
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
+ }
129
132
return NULL ;
130
133
}
131
134
132
135
static PyObject *
133
136
snappy__uncompress (PyObject *self, PyObject *args)
134
137
{
135
- const char * compressed;
136
- int comp_size;
138
+ Py_buffer compressed;
137
139
size_t uncomp_size, actual_size;
138
140
PyObject * result;
139
141
snappy_status status;
140
142
141
143
#if PY_MAJOR_VERSION >=3
142
- if (!PyArg_ParseTuple (args, " y# " , &compressed, &comp_size ))
144
+ if (!PyArg_ParseTuple (args, " y* " , &compressed))
143
145
#else
144
- if (!PyArg_ParseTuple (args, " s# " , &compressed, &comp_size ))
146
+ if (!PyArg_ParseTuple (args, " s* " , &compressed))
145
147
#endif
146
148
return NULL ;
147
149
148
- status = snappy_uncompressed_length (compressed, comp_size, &uncomp_size);
150
+ status = snappy_uncompressed_length ((const char *) compressed.buf , compressed.len ,
151
+ &uncomp_size);
149
152
if (status != SNAPPY_OK) {
153
+ PyBuffer_Release (&compressed);
150
154
PyErr_SetString (SnappyCompressedLengthError,
151
155
" Can not calculate uncompressed length" );
152
156
return NULL ;
@@ -156,17 +160,22 @@ snappy__uncompress(PyObject *self, PyObject *args)
156
160
if (result) {
157
161
actual_size = uncomp_size;
158
162
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);
160
165
Py_END_ALLOW_THREADS
166
+ PyBuffer_Release (&compressed);
161
167
if (SNAPPY_OK == status) {
162
168
return maybe_resize (result, uncomp_size, actual_size);
163
169
}
164
170
else {
165
171
Py_DECREF (result);
172
+ PyErr_Format (SnappyUncompressError,
173
+ " Error while decompressing: %s" , snappy_strerror (status));
166
174
}
167
175
}
168
- PyErr_Format (SnappyUncompressError,
169
- " Error while decompressing: %s" , snappy_strerror (status));
176
+ else {
177
+ PyBuffer_Release (&compressed);
178
+ }
170
179
return NULL ;
171
180
}
172
181
0 commit comments