summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFredrik Lundh <fredrik@pythonware.com>2000-09-02 20:11:27 (GMT)
committerFredrik Lundh <fredrik@pythonware.com>2000-09-02 20:11:27 (GMT)
commit1fa0b895ec69d6e514a7fb457181cba339041d6c (patch)
treea48b6cee9f5ebbd622c72c7fea06aa32daf89dce
parent510c97ba2f573bb5336681126ea6aad45a928a52 (diff)
downloadcpython-1fa0b895ec69d6e514a7fb457181cba339041d6c.zip
cpython-1fa0b895ec69d6e514a7fb457181cba339041d6c.tar.gz
cpython-1fa0b895ec69d6e514a7fb457181cba339041d6c.tar.bz2
changed \x to consume exactly two hex digits. implements PEP-223
for 8-bit strings.
-rw-r--r--Python/compile.c44
1 files changed, 28 insertions, 16 deletions
diff --git a/Python/compile.c b/Python/compile.c
index 15c4606..155761f 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -873,10 +873,11 @@ parsestr(char *s)
return PyUnicode_DecodeUnicodeEscape(
s, len, NULL);
}
- else if (rawmode || strchr(s, '\\') == NULL) {
+ if (rawmode || strchr(s, '\\') == NULL)
return PyString_FromStringAndSize(s, len);
- }
v = PyString_FromStringAndSize((char *)NULL, len);
+ if (v == NULL)
+ return NULL;
p = buf = PyString_AsString(v);
end = s + len;
while (s < end) {
@@ -909,24 +910,35 @@ parsestr(char *s)
*p++ = c;
break;
case 'x':
- if (isxdigit(Py_CHARMASK(*s))) {
+ if (isxdigit(Py_CHARMASK(s[0])) && isxdigit(Py_CHARMASK(s[1]))) {
unsigned int x = 0;
- do {
- c = Py_CHARMASK(*s);
- s++;
- x = (x<<4) & ~0xF;
- if (isdigit(c))
- x += c - '0';
- else if (islower(c))
- x += 10 + c - 'a';
- else
- x += 10 + c - 'A';
- } while (isxdigit(Py_CHARMASK(*s)));
+ c = Py_CHARMASK(*s);
+ s++;
+ if (isdigit(c))
+ x = c - '0';
+ else if (islower(c))
+ x = 10 + c - 'a';
+ else
+ x = 10 + c - 'A';
+ x = x << 4;
+ c = Py_CHARMASK(*s);
+ s++;
+ if (isdigit(c))
+ x += c - '0';
+ else if (islower(c))
+ x += 10 + c - 'a';
+ else
+ x += 10 + c - 'A';
*p++ = x;
break;
}
- /* FALLTHROUGH */
- default: *p++ = '\\'; *p++ = s[-1]; break;
+ PyErr_SetString(PyExc_ValueError, "invalid \\x escape");
+ Py_DECREF(v);
+ return NULL;
+ default:
+ *p++ = '\\';
+ *p++ = s[-1];
+ break;
}
}
_PyString_Resize(&v, (int)(p - buf));