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

Delta Between Two Patch Sets: Python/ast.c

Issue 28128: Improve the warning message for invalid escape sequences
Left Patch Set: Created 3 years, 1 month ago
Right Patch Set: Created 3 years, 1 month 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:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Objects/unicodeobject.c ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 /* 1 /*
2 * This file includes functions to transform a concrete syntax tree (CST) to 2 * This file includes functions to transform a concrete syntax tree (CST) to
3 * an abstract syntax tree (AST). The main function is PyAST_FromNode(). 3 * an abstract syntax tree (AST). The main function is PyAST_FromNode().
4 * 4 *
5 */ 5 */
6 #include "Python.h" 6 #include "Python.h"
7 #include "Python-ast.h" 7 #include "Python-ast.h"
8 #include "node.h" 8 #include "node.h"
9 #include "ast.h" 9 #include "ast.h"
10 #include "token.h" 10 #include "token.h"
(...skipping 4095 matching lines...) Expand 10 before | Expand all | Expand 10 after
4106 decode_utf8(struct compiling *c, const char **sPtr, const char *end) 4106 decode_utf8(struct compiling *c, const char **sPtr, const char *end)
4107 { 4107 {
4108 const char *s, *t; 4108 const char *s, *t;
4109 t = s = *sPtr; 4109 t = s = *sPtr;
4110 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */ 4110 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
4111 while (s < end && (*s & 0x80)) s++; 4111 while (s < end && (*s & 0x80)) s++;
4112 *sPtr = s; 4112 *sPtr = s;
4113 return PyUnicode_DecodeUTF8(t, s - t, NULL); 4113 return PyUnicode_DecodeUTF8(t, s - t, NULL);
4114 } 4114 }
4115 4115
4116 static int
4117 warn_invalid_escape_sequence(struct compiling *c, const node *n,
4118 char first_invalid_escape_char)
4119 {
4120 PyObject *msg = PyUnicode_FromFormat("invalid escape sequence \\%c",
4121 first_invalid_escape_char);
4122 if (msg == NULL) {
4123 return -1;
4124 }
4125 if (PyErr_WarnExplicitObject(PyExc_DeprecationWarning, msg,
4126 c->c_filename, LINENO(n),
4127 NULL, NULL) < 0 &&
4128 PyErr_ExceptionMatches(PyExc_DeprecationWarning))
4129 {
4130 const char *s = PyUnicode_AsUTF8(msg);
4131 if (s != NULL) {
4132 ast_error(c, n, s);
4133 }
4134 Py_DECREF(msg);
4135 return -1;
4136 }
4137 Py_DECREF(msg);
4138 return 0;
4139 }
4140
4116 static PyObject * 4141 static PyObject *
4117 decode_unicode_with_escapes(struct compiling *c, const node *n, const char *s, 4142 decode_unicode_with_escapes(struct compiling *c, const node *n, const char *s,
4118 size_t len) 4143 size_t len)
4119 { 4144 {
4120 PyObject *v, *u; 4145 PyObject *v, *u;
4121 char *buf; 4146 char *buf;
4122 char *p; 4147 char *p;
4123 const char *end; 4148 const char *end;
4124 4149
4125 /* check for integer overflow */ 4150 /* check for integer overflow */
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
4162 assert(p - buf <= Py_SIZE(u)); 4187 assert(p - buf <= Py_SIZE(u));
4163 Py_DECREF(w); 4188 Py_DECREF(w);
4164 } else { 4189 } else {
4165 *p++ = *s++; 4190 *p++ = *s++;
4166 } 4191 }
4167 } 4192 }
4168 len = p - buf; 4193 len = p - buf;
4169 s = buf; 4194 s = buf;
4170 4195
4171 const char *first_invalid_escape; 4196 const char *first_invalid_escape;
4172 char first_invalid_escape_char = 0;
4173 v = _PyUnicode_DecodeUnicodeEscape(s, len, NULL, &first_invalid_escape); 4197 v = _PyUnicode_DecodeUnicodeEscape(s, len, NULL, &first_invalid_escape);
4174 4198
4175 /* We have to remember this before we decref u, because it points
4176 inside u. */
4177 if (v != NULL && first_invalid_escape != NULL) { 4199 if (v != NULL && first_invalid_escape != NULL) {
4178 first_invalid_escape_char = *first_invalid_escape; 4200 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4179 } 4201 /* We have not decref u before because first_invalid_escape points
4180 4202 inside u. */
4203 Py_XDECREF(u);
4204 Py_DECREF(v);
4205 return NULL;
4206 }
4207 }
4181 Py_XDECREF(u); 4208 Py_XDECREF(u);
4182
4183 if (v != NULL && first_invalid_escape != NULL) {
4184 char msg[300];
4185 PyOS_snprintf(msg, sizeof(msg),
4186 "invalid escape sequence \\%c",
4187 first_invalid_escape_char);
4188 ast_error(c, n, msg);
4189 Py_DECREF(v);
4190 return NULL;
4191 }
4192 return v; 4209 return v;
4193 } 4210 }
4194 4211
4195 static PyObject * 4212 static PyObject *
4196 decode_bytes_with_escapes(struct compiling *c, const node *n, const char *s, 4213 decode_bytes_with_escapes(struct compiling *c, const node *n, const char *s,
4197 size_t len) 4214 size_t len)
4198 { 4215 {
4199 const char *first_invalid_escape; 4216 const char *first_invalid_escape;
4200 PyObject *result = _PyBytes_DecodeEscape(s, len, NULL, 0, NULL, 4217 PyObject *result = _PyBytes_DecodeEscape(s, len, NULL, 0, NULL,
4201 &first_invalid_escape); 4218 &first_invalid_escape);
4202 if (result == NULL) 4219 if (result == NULL)
4203 return NULL; 4220 return NULL;
4204 4221
4205 if (first_invalid_escape != NULL) { 4222 if (first_invalid_escape != NULL) {
4206 char first_invalid_escape_char = *first_invalid_escape; 4223 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4207 char msg[300]; 4224 Py_DECREF(result);
4208 PyOS_snprintf(msg, sizeof(msg), 4225 return NULL;
4209 "invalid escape sequence \\%c", 4226 }
4210 first_invalid_escape_char);
4211 ast_error(c, n, msg);
4212 Py_DECREF(result);
4213 return NULL;
4214 } 4227 }
4215 return result; 4228 return result;
4216 } 4229 }
4217 4230
4218 /* Compile this expression in to an expr_ty. Add parens around the 4231 /* Compile this expression in to an expr_ty. Add parens around the
4219 expression, in order to allow leading spaces in the expression. */ 4232 expression, in order to allow leading spaces in the expression. */
4220 static expr_ty 4233 static expr_ty
4221 fstring_compile_expr(const char *expr_start, const char *expr_end, 4234 fstring_compile_expr(const char *expr_start, const char *expr_end,
4222 struct compiling *c, const node *n) 4235 struct compiling *c, const node *n)
4223 4236
(...skipping 957 matching lines...) Expand 10 before | Expand all | Expand 10 after
5181 /* We're not a bytes string, bytes_str should never have been set. */ 5194 /* We're not a bytes string, bytes_str should never have been set. */
5182 assert(bytes_str == NULL); 5195 assert(bytes_str == NULL);
5183 5196
5184 return FstringParser_Finish(&state, c, n); 5197 return FstringParser_Finish(&state, c, n);
5185 5198
5186 error: 5199 error:
5187 Py_XDECREF(bytes_str); 5200 Py_XDECREF(bytes_str);
5188 FstringParser_Dealloc(&state); 5201 FstringParser_Dealloc(&state);
5189 return NULL; 5202 return NULL;
5190 } 5203 }
LEFTRIGHT

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