1
1
# coding: utf-8
2
- # cython: embedsignature=True
2
+ # cython: embedsignature=True, c_string_encoding=ascii
3
3
4
+ from cpython.version cimport PY_MAJOR_VERSION
4
5
from cpython.bytes cimport (
5
6
PyBytes_AsString,
6
7
PyBytes_FromStringAndSize,
@@ -75,7 +76,7 @@ cdef inline init_ctx(unpack_context *ctx,
75
76
object object_hook, object object_pairs_hook,
76
77
object list_hook, object ext_hook,
77
78
bint use_list, bint raw,
78
- char * encoding, char * unicode_errors,
79
+ const char * encoding, const char * unicode_errors,
79
80
Py_ssize_t max_str_len, Py_ssize_t max_bin_len,
80
81
Py_ssize_t max_array_len, Py_ssize_t max_map_len,
81
82
Py_ssize_t max_ext_len):
@@ -180,24 +181,16 @@ def unpackb(object packed, object object_hook=None, object list_hook=None,
180
181
cdef Py_buffer view
181
182
cdef char * buf = NULL
182
183
cdef Py_ssize_t buf_len
183
- cdef char * cenc = NULL
184
- cdef char * cerr = NULL
184
+ cdef const char * cenc = NULL
185
+ cdef const char * cerr = NULL
185
186
cdef int new_protocol = 0
186
187
187
188
if encoding is not None :
188
189
PyErr_WarnEx(PendingDeprecationWarning , " encoding is deprecated, Use raw=False instead." , 1 )
189
- if isinstance (encoding, unicode ):
190
- encoding = encoding.encode(' ascii' )
191
- elif not isinstance (encoding, bytes):
192
- raise TypeError (" encoding should be bytes or unicode" )
193
- cenc = PyBytes_AsString(encoding)
190
+ cenc = encoding
194
191
195
192
if unicode_errors is not None :
196
- if isinstance (unicode_errors, unicode ):
197
- unicode_errors = unicode_errors.encode(' ascii' )
198
- elif not isinstance (unicode_errors, bytes):
199
- raise TypeError (" unicode_errors should be bytes or unicode" )
200
- cerr = PyBytes_AsString(unicode_errors)
193
+ cerr = unicode_errors
201
194
202
195
get_data_from_buffer(packed, & view, & buf, & buf_len, & new_protocol)
203
196
try :
@@ -219,7 +212,7 @@ def unpackb(object packed, object object_hook=None, object list_hook=None,
219
212
220
213
221
214
def unpack (object stream , object object_hook = None , object list_hook = None ,
222
- bint use_list = 1 , encoding = None , unicode_errors = " strict " ,
215
+ bint use_list = 1 , encoding = None , unicode_errors = None ,
223
216
object_pairs_hook = None , ext_hook = ExtType,
224
217
Py_ssize_t max_str_len = 2147483647 , # 2**32-1
225
218
Py_ssize_t max_bin_len = 2147483647 ,
@@ -352,8 +345,8 @@ cdef class Unpacker(object):
352
345
Py_ssize_t max_array_len = 2147483647 ,
353
346
Py_ssize_t max_map_len = 2147483647 ,
354
347
Py_ssize_t max_ext_len = 2147483647 ):
355
- cdef char * cenc= NULL ,
356
- cdef char * cerr= NULL
348
+ cdef const char * cenc= NULL ,
349
+ cdef const char * cerr= NULL
357
350
358
351
self .object_hook = object_hook
359
352
self .object_pairs_hook = object_pairs_hook
@@ -383,22 +376,12 @@ cdef class Unpacker(object):
383
376
384
377
if encoding is not None :
385
378
PyErr_WarnEx(PendingDeprecationWarning , " encoding is deprecated, Use raw=False instead." , 1 )
386
- if isinstance (encoding, unicode ):
387
- self .encoding = encoding.encode(' ascii' )
388
- elif
AAC9
isinstance (encoding, bytes):
389
- self .encoding = encoding
390
- else :
391
- raise TypeError (" encoding should be bytes or unicode" )
392
- cenc = PyBytes_AsString(self .encoding)
379
+ self .encoding = encoding
380
+ cenc = encoding
393
381
394
382
if unicode_errors is not None :
395
- if isinstance (unicode_errors, unicode ):
396
- self .unicode_errors = unicode_errors.encode(' ascii' )
397
- elif isinstance (unicode_errors, bytes):
398
- self .unicode_errors = unicode_errors
399
- else :
400
- raise TypeError (" unicode_errors should be bytes or unicode" )
401
- cerr = PyBytes_AsString(self .unicode_errors)
383
+ self .unicode_errors = unicode_errors
384
+ cerr = unicode_errors
402
385
403
386
init_ctx(& self .ctx, object_hook, object_pairs_hook, list_hook,
404
387
ext_hook, use_list, raw, cenc, cerr,
0 commit comments