diff options
author | Eric V. Smith <eric@trueblade.com> | 2016-10-31 09:22:08 -0400 |
---|---|---|
committer | Eric V. Smith <eric@trueblade.com> | 2016-10-31 09:22:08 -0400 |
commit | 42454af094fd572a97a6302e8c1226be68c0efa9 (patch) | |
tree | 29a7b5737fe214a0571af7c6daf53a4d85c6bae4 /Objects/bytesobject.c | |
parent | a99cdb21a7d67ce5d1e3cc93b08eb3b6eecba60b (diff) | |
download | cpython-git-42454af094fd572a97a6302e8c1226be68c0efa9.tar.gz |
Issue 28128: Print out better error/warning messages for invalid string escapes.
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 598f6a13cf..779fe295db 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 */ |