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

Unified Diff: Objects/bytesobject.c

Issue 28128: Improve the warning message for invalid escape sequences
Patch Set: Created 3 years, 1 month ago
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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « Lib/test/test_unicode.py ('k') | Objects/unicodeobject.c » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
--- a/Objects/bytesobject.c Mon Oct 31 08:31:13 2016 +0200
+++ b/Objects/bytesobject.c Mon Oct 31 14:28:19 2016 +0200
@@ -1105,11 +1105,12 @@ static char *
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 cha
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 cha
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 cha
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 */
« no previous file with comments | « Lib/test/test_unicode.py ('k') | Objects/unicodeobject.c » ('j') | no next file with comments »

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