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

Unified Diff: Objects/unicodeobject.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 | « Objects/bytesobject.c ('k') | Python/ast.c » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
--- a/Objects/unicodeobject.c Mon Oct 31 08:31:13 2016 +0200
+++ b/Objects/unicodeobject.c Mon Oct 31 14:28:19 2016 +0200
@@ -5896,9 +5896,10 @@ PyUnicode_AsUTF16String(PyObject *unicod
static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL;
PyObject *
-PyUnicode_DecodeUnicodeEscape(const char *s,
- Py_ssize_t size,
- const char *errors)
+_PyUnicode_DecodeUnicodeEscape(const char *s,
+ Py_ssize_t size,
+ const char *errors,
+ const char **first_invalid_escape)
{
const char *starts = s;
_PyUnicodeWriter writer;
@@ -5906,6 +5907,9 @@ PyUnicode_DecodeUnicodeEscape(const char
PyObject *errorHandler = NULL;
PyObject *exc = NULL;
+ // so we can remember if we've seen an invalid escape char or not
+ *first_invalid_escape = NULL;
+
if (size == 0) {
_Py_RETURN_UNICODE_EMPTY();
}
@@ -6080,9 +6084,10 @@ PyUnicode_DecodeUnicodeEscape(const char
goto error;
default:
- if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
- "invalid escape sequence '\\%c'", c) < 0)
- goto onError;
+ if (*first_invalid_escape == NULL) {
+ *first_invalid_escape = s-1; /* Back up one char, since we've
+ already incremented s. */
+ }
WRITE_ASCII_CHAR('\\');
WRITE_CHAR(c);
continue;
@@ -6117,6 +6122,27 @@ PyUnicode_DecodeUnicodeEscape(const char
return NULL;
}
+PyObject *
+PyUnicode_DecodeUnicodeEscape(const char *s,
+ Py_ssize_t size,
+ const char *errors)
+{
+ const char *first_invalid_escape;
+ PyObject *result = _PyUnicode_DecodeUnicodeEscape(s, size, errors,
+ &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;
+}
+
/* Return a Unicode-Escape string version of the Unicode object.
If quotes is true, the string is enclosed in u"" or u'' quotes as
« no previous file with comments | « Objects/bytesobject.c ('k') | Python/ast.c » ('j') | no next file with comments »

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