Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(167601)

Delta Between Two Patch Sets: Modules/_codecsmodule.c

Issue 23001: Accept mutable bytes-like objects
Left Patch Set: Created 5 years ago
Right Patch Set: Created 4 years, 8 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
LEFTRIGHT
1 /* ------------------------------------------------------------------------ 1 /* ------------------------------------------------------------------------
2 2
3 _codecs -- Provides access to the codec registry and the builtin 3 _codecs -- Provides access to the codec registry and the builtin
4 codecs. 4 codecs.
5 5
6 This module should never be imported directly. The standard library 6 This module should never be imported directly. The standard library
7 module "codecs" wraps this builtin module for use within Python. 7 module "codecs" wraps this builtin module for use within Python.
8 8
9 The codec registry is accessible via: 9 The codec registry is accessible via:
10 10
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
47 [clinic start generated code]*/ 47 [clinic start generated code]*/
48 /*[clinic end generated code: output=da39a3ee5e6b4b0d input=e1390e3da3cb9deb]*/ 48 /*[clinic end generated code: output=da39a3ee5e6b4b0d input=e1390e3da3cb9deb]*/
49 49
50 50
51 /* --- Registry ----------------------------------------------------------- */ 51 /* --- Registry ----------------------------------------------------------- */
52 52
53 PyDoc_STRVAR(register__doc__, 53 PyDoc_STRVAR(register__doc__,
54 "register(search_function)\n\ 54 "register(search_function)\n\
55 \n\ 55 \n\
56 Register a codec search function. Search functions are expected to take\n\ 56 Register a codec search function. Search functions are expected to take\n\
57 one argument, the encoding name in all lower case letters, and return\n\ 57 one argument, the encoding name in all lower case letters, and either\n\
58 a tuple of functions (encoder, decoder, stream_reader, stream_writer)\n\ 58 return None, or a tuple of functions (encoder, decoder, stream_reader,\n\
59 (or a CodecInfo object)."); 59 stream_writer) (or a CodecInfo object).");
60 60
61 static 61 static
62 PyObject *codec_register(PyObject *self, PyObject *search_function) 62 PyObject *codec_register(PyObject *self, PyObject *search_function)
63 { 63 {
64 if (PyCodec_Register(search_function)) 64 if (PyCodec_Register(search_function))
65 return NULL; 65 return NULL;
66 66
67 Py_RETURN_NONE; 67 Py_RETURN_NONE;
68 } 68 }
69 69
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after
284 } 284 }
285 285
286 /* --- Decoder ------------------------------------------------------------ */ 286 /* --- Decoder ------------------------------------------------------------ */
287 287
288 static PyObject * 288 static PyObject *
289 unicode_internal_decode(PyObject *self, 289 unicode_internal_decode(PyObject *self,
290 PyObject *args) 290 PyObject *args)
291 { 291 {
292 PyObject *obj; 292 PyObject *obj;
293 const char *errors = NULL; 293 const char *errors = NULL;
294 const char *data;
295 Py_ssize_t size;
296 294
297 if (!PyArg_ParseTuple(args, "O|z:unicode_internal_decode", 295 if (!PyArg_ParseTuple(args, "O|z:unicode_internal_decode",
298 &obj, &errors)) 296 &obj, &errors))
299 return NULL; 297 return NULL;
300 298
301 if (PyUnicode_Check(obj)) { 299 if (PyUnicode_Check(obj)) {
302 if (PyUnicode_READY(obj) < 0) 300 if (PyUnicode_READY(obj) < 0)
303 return NULL; 301 return NULL;
304 Py_INCREF(obj); 302 Py_INCREF(obj);
305 return codec_tuple(obj, PyUnicode_GET_LENGTH(obj)); 303 return codec_tuple(obj, PyUnicode_GET_LENGTH(obj));
306 } 304 }
307 else { 305 else {
308 if (PyObject_AsReadBuffer(obj, (const void **)&data, &size)) 306 Py_buffer view;
307 PyObject *result;
308 if (PyObject_GetBuffer(obj, &view, PyBUF_SIMPLE) != 0)
309 return NULL; 309 return NULL;
310 310
311 return codec_tuple(_PyUnicode_DecodeUnicodeInternal(data, size, errors), 311 result = codec_tuple(
312 size); 312 _PyUnicode_DecodeUnicodeInternal(view.buf, view.len, errors),
313 view.len);
314 PyBuffer_Release(&view);
315 return result;
313 } 316 }
314 } 317 }
315 318
316 static PyObject * 319 static PyObject *
317 utf_7_decode(PyObject *self, 320 utf_7_decode(PyObject *self,
318 PyObject *args) 321 PyObject *args)
319 { 322 {
320 Py_buffer pbuf; 323 Py_buffer pbuf;
321 const char *errors = NULL; 324 const char *errors = NULL;
322 int final = 0; 325 int final = 0;
(...skipping 404 matching lines...) Expand 10 before | Expand all | Expand 10 after
727 PyBuffer_Release(&pdata); 730 PyBuffer_Release(&pdata);
728 return codec_tuple(result, size); 731 return codec_tuple(result, size);
729 } 732 }
730 733
731 static PyObject * 734 static PyObject *
732 unicode_internal_encode(PyObject *self, 735 unicode_internal_encode(PyObject *self,
733 PyObject *args) 736 PyObject *args)
734 { 737 {
735 PyObject *obj; 738 PyObject *obj;
736 const char *errors = NULL; 739 const char *errors = NULL;
737 const char *data;
738 Py_ssize_t len, size;
739 740
740 if (PyErr_WarnEx(PyExc_DeprecationWarning, 741 if (PyErr_WarnEx(PyExc_DeprecationWarning,
741 "unicode_internal codec has been deprecated", 742 "unicode_internal codec has been deprecated",
742 1)) 743 1))
743 return NULL; 744 return NULL;
744 745
745 if (!PyArg_ParseTuple(args, "O|z:unicode_internal_encode", 746 if (!PyArg_ParseTuple(args, "O|z:unicode_internal_encode",
746 &obj, &errors)) 747 &obj, &errors))
747 return NULL; 748 return NULL;
748 749
749 if (PyUnicode_Check(obj)) { 750 if (PyUnicode_Check(obj)) {
750 Py_UNICODE *u; 751 Py_UNICODE *u;
752 Py_ssize_t len, size;
751 753
752 if (PyUnicode_READY(obj) < 0) 754 if (PyUnicode_READY(obj) < 0)
753 return NULL; 755 return NULL;
754 756
755 u = PyUnicode_AsUnicodeAndSize(obj, &len); 757 u = PyUnicode_AsUnicodeAndSize(obj, &len);
756 if (u == NULL) 758 if (u == NULL)
757 return NULL; 759 return NULL;
758 if ((size_t)len > (size_t)PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) 760 if ((size_t)len > (size_t)PY_SSIZE_T_MAX / sizeof(Py_UNICODE))
759 return PyErr_NoMemory(); 761 return PyErr_NoMemory();
760 size = len * sizeof(Py_UNICODE); 762 size = len * sizeof(Py_UNICODE);
761 return codec_tuple(PyBytes_FromStringAndSize((const char*)u, size), 763 return codec_tuple(PyBytes_FromStringAndSize((const char*)u, size),
762 PyUnicode_GET_LENGTH(obj)); 764 PyUnicode_GET_LENGTH(obj));
763 } 765 }
764 else { 766 else {
765 if (PyObject_AsReadBuffer(obj, (const void **)&data, &size)) 767 Py_buffer view;
768 PyObject *result;
769 if (PyObject_GetBuffer(obj, &view, PyBUF_SIMPLE) != 0)
766 return NULL; 770 return NULL;
767 return codec_tuple(PyBytes_FromStringAndSize(data, size), size); 771 result = codec_tuple(PyBytes_FromStringAndSize(view.buf, view.len), view .len);
772 PyBuffer_Release(&view);
773 return result;
768 } 774 }
769 } 775 }
770 776
771 static PyObject * 777 static PyObject *
772 utf_7_encode(PyObject *self, 778 utf_7_encode(PyObject *self,
773 PyObject *args) 779 PyObject *args)
774 { 780 {
775 PyObject *str, *v; 781 PyObject *str, *v;
776 const char *errors = NULL; 782 const char *errors = NULL;
777 783
(...skipping 464 matching lines...) Expand 10 before | Expand all | Expand 10 after
1242 NULL, 1248 NULL,
1243 NULL, 1249 NULL,
1244 NULL 1250 NULL
1245 }; 1251 };
1246 1252
1247 PyMODINIT_FUNC 1253 PyMODINIT_FUNC
1248 PyInit__codecs(void) 1254 PyInit__codecs(void)
1249 { 1255 {
1250 return PyModule_Create(&codecsmodule); 1256 return PyModule_Create(&codecsmodule);
1251 } 1257 }
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+