diff options
author | Eric V. Smith <eric@trueblade.com> | 2016-10-31 18:46:26 (GMT) |
---|---|---|
committer | Eric V. Smith <eric@trueblade.com> | 2016-10-31 18:46:26 (GMT) |
commit | 5646648678295a44aa82636c6e92826651baf33a (patch) | |
tree | 2a41306ca416712ba7b55a4e51bcb836ab2a693c /Objects/bytesobject.c | |
parent | 7f0514ad54dd806817ce6d1f54969b8979475d34 (diff) | |
download | cpython-5646648678295a44aa82636c6e92826651baf33a.zip cpython-5646648678295a44aa82636c6e92826651baf33a.tar.gz cpython-5646648678295a44aa82636c6e92826651baf33a.tar.bz2 |
Issue 28128: Print out better error/warning messages for invalid string escapes. Backport to 3.6.
Diffstat (limited to 'Objects/bytesobject.c')
-rw-r--r-- | Objects/bytesobject.c | 37 |
1 files changed, 33 insertions, 4 deletions
diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c index 598f6a1..779fe29 100644 --- a/Objects/bytesobject.c +++ b/Objects/bytesobject.c @@ -1105,11 +1105,12 @@ _PyBytes_DecodeEscapeRecode(const char **s, const char *end, return p; } -PyObject *PyBytes_DecodeEscape(const char *s, +PyObject *_PyBytes_DecodeEscape(const char *s, Py_ssize_t len, const char *errors, Py_ssize_t unicode, - const char *recode_encoding) + const char *recode_encoding, + const char **first_invalid_escape) { int c; char *p; @@ -1123,6 +1124,8 @@ PyObject *PyBytes_DecodeEscape(const char *s, return NULL; writer.overallocate = 1; + *first_invalid_escape = NULL; + end = s + len; while (s < end) { if (*s != '\\') { @@ -1207,9 +1210,12 @@ PyObject *PyBytes_DecodeEscape(const char *s, break; default: - if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, "invalid escape sequence '\\%c'", *(--s)) < 0) - goto failed; + if (*first_invalid_escape == NULL) { + *first_invalid_escape = s-1; /* Back up one char, since we've + already incremented s. */ + } *p++ = '\\'; + s--; goto non_esc; /* an arbitrary number of unescaped UTF-8 bytes may follow. */ } @@ -1222,6 +1228,29 @@ PyObject *PyBytes_DecodeEscape(const char *s, return NULL; } +PyObject *PyBytes_DecodeEscape(const char *s, + Py_ssize_t len, + const char *errors, + Py_ssize_t unicode, + const char *recode_encoding) +{ + const char* first_invalid_escape; + PyObject *result = _PyBytes_DecodeEscape(s, len, errors, unicode, + recode_encoding, + &first_invalid_escape); + if (result == NULL) + return NULL; + if (first_invalid_escape != NULL) { + if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, + "invalid escape sequence '\\%c'", + *first_invalid_escape) < 0) { + Py_DECREF(result); + return NULL; + } + } + return result; + +} /* -------------------------------------------------------------------- */ /* object api */ |