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

Side by Side Diff: Objects/bytesobject.c

Issue 25270: codecs.escape_encode systemerror on empty byte string
Patch Set: Created 3 years, 5 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:
View unified diff | Download patch
« Lib/test/test_codecs.py ('K') | « Lib/test/test_codecs.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* bytes object implementation */ 1 /* bytes object implementation */
2 2
3 #define PY_SSIZE_T_CLEAN 3 #define PY_SSIZE_T_CLEAN
4 4
5 #include "Python.h" 5 #include "Python.h"
6 6
7 #include "bytes_methods.h" 7 #include "bytes_methods.h"
8 #include "pystrhex.h" 8 #include "pystrhex.h"
9 #include <stddef.h> 9 #include <stddef.h>
10 10
(...skipping 2891 matching lines...) Expand 10 before | Expand all | Expand 10 after
2902 returned, and the value in *pv may or may not be the same as on input. 2902 returned, and the value in *pv may or may not be the same as on input.
2903 As always, an extra byte is allocated for a trailing \0 byte (newsize 2903 As always, an extra byte is allocated for a trailing \0 byte (newsize
2904 does *not* include that), and a trailing \0 byte is stored. 2904 does *not* include that), and a trailing \0 byte is stored.
2905 */ 2905 */
2906 2906
2907 int 2907 int
2908 _PyBytes_Resize(PyObject **pv, Py_ssize_t newsize) 2908 _PyBytes_Resize(PyObject **pv, Py_ssize_t newsize)
2909 { 2909 {
2910 PyObject *v; 2910 PyObject *v;
2911 PyBytesObject *sv; 2911 PyBytesObject *sv;
2912 Py_ssize_t oldsize;
2912 v = *pv; 2913 v = *pv;
2913 if (!PyBytes_Check(v) || Py_REFCNT(v) != 1 || newsize < 0) { 2914 if (!PyBytes_Check(v) || newsize < 0) {
2914 *pv = 0; 2915 goto error;
2915 Py_DECREF(v); 2916 }
2916 PyErr_BadInternalCall(); 2917 oldsize = Py_SIZE(v);
storchaka 2016/09/16 12:20:39 oldsize is used only once. It can be inlined.
berkerpeksag 2016/09/16 12:41:15 Done.
2917 return -1; 2918 if (oldsize == newsize) {
2919 return 0;
berkerpeksag 2016/09/16 12:41:15 Question: Should I add a comment to explain this?
2920 }
2921 if (Py_REFCNT(v) != 1) {
2922 goto error;
2918 } 2923 }
2919 /* XXX UNREF/NEWREF interface should be more symmetrical */ 2924 /* XXX UNREF/NEWREF interface should be more symmetrical */
2920 _Py_DEC_REFTOTAL; 2925 _Py_DEC_REFTOTAL;
2921 _Py_ForgetReference(v); 2926 _Py_ForgetReference(v);
2922 *pv = (PyObject *) 2927 *pv = (PyObject *)
2923 PyObject_REALLOC(v, PyBytesObject_SIZE + newsize); 2928 PyObject_REALLOC(v, PyBytesObject_SIZE + newsize);
2924 if (*pv == NULL) { 2929 if (*pv == NULL) {
2925 PyObject_Del(v); 2930 PyObject_Del(v);
2926 PyErr_NoMemory(); 2931 PyErr_NoMemory();
2927 return -1; 2932 return -1;
2928 } 2933 }
2929 _Py_NewReference(*pv); 2934 _Py_NewReference(*pv);
2930 sv = (PyBytesObject *) *pv; 2935 sv = (PyBytesObject *) *pv;
2931 Py_SIZE(sv) = newsize; 2936 Py_SIZE(sv) = newsize;
2932 sv->ob_sval[newsize] = '\0'; 2937 sv->ob_sval[newsize] = '\0';
2933 sv->ob_shash = -1; /* invalidate cached hash value */ 2938 sv->ob_shash = -1; /* invalidate cached hash value */
2934 return 0; 2939 return 0;
2940 error:
2941 *pv = 0;
2942 Py_DECREF(v);
2943 PyErr_BadInternalCall();
2944 return -1;
2935 } 2945 }
2936 2946
2937 void 2947 void
2938 PyBytes_Fini(void) 2948 PyBytes_Fini(void)
2939 { 2949 {
2940 int i; 2950 int i;
2941 for (i = 0; i < UCHAR_MAX + 1; i++) 2951 for (i = 0; i < UCHAR_MAX + 1; i++)
2942 Py_CLEAR(characters[i]); 2952 Py_CLEAR(characters[i]);
2943 Py_CLEAR(nullstring); 2953 Py_CLEAR(nullstring);
2944 } 2954 }
(...skipping 425 matching lines...) Expand 10 before | Expand all | Expand 10 after
3370 3380
3371 str = _PyBytesWriter_Prepare(writer, str, size); 3381 str = _PyBytesWriter_Prepare(writer, str, size);
3372 if (str == NULL) 3382 if (str == NULL)
3373 return NULL; 3383 return NULL;
3374 3384
3375 memcpy(str, bytes, size); 3385 memcpy(str, bytes, size);
3376 str += size; 3386 str += size;
3377 3387
3378 return str; 3388 return str;
3379 } 3389 }
OLDNEW
« Lib/test/test_codecs.py ('K') | « Lib/test/test_codecs.py ('k') | no next file » | no next file with comments »

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