mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +00:00
Make C and Python implementations of pickle load STRING opcodes the same way.
The C version tried to remove trailing whitespace between the last quote and the newline character. I am not sure why it had this because pickle never generated such pickles---for this to happen repr(some_string) would need to return trailing whitespace. It was maybe there to make it easier for people to write pickles in text editors. Anyhow, the Python version doesn't do this so there is no point keeping this around anymore. Also, I've changed the exception raised when a bad pickle is encountered. Again this unlikely to make much difference to anyone though it does make testing slightly nicer for us.
This commit is contained in:
parent
48a2e7c357
commit
7c5e094cbf
3 changed files with 40 additions and 49 deletions
|
@ -4205,36 +4205,23 @@ load_string(UnpicklerObject *self)
|
|||
|
||||
if ((len = _Unpickler_Readline(self, &s)) < 0)
|
||||
return -1;
|
||||
if (len < 2)
|
||||
return bad_readline();
|
||||
if ((s = strdup(s)) == NULL) {
|
||||
PyErr_NoMemory();
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Strip the newline */
|
||||
len--;
|
||||
/* Strip outermost quotes */
|
||||
while (len > 0 && s[len - 1] <= ' ')
|
||||
len--;
|
||||
if (len > 1 && s[0] == '"' && s[len - 1] == '"') {
|
||||
s[len - 1] = '\0';
|
||||
p = s + 1;
|
||||
len -= 2;
|
||||
}
|
||||
else if (len > 1 && s[0] == '\'' && s[len - 1] == '\'') {
|
||||
s[len - 1] = '\0';
|
||||
if (len >= 2 && s[0] == s[len - 1] && (s[0] == '\'' || s[0] == '"')) {
|
||||
p = s + 1;
|
||||
len -= 2;
|
||||
}
|
||||
else {
|
||||
free(s);
|
||||
PyErr_SetString(PyExc_ValueError, "insecure string pickle");
|
||||
PyErr_SetString(UnpicklingError,
|
||||
"the STRING opcode argument must be quoted");
|
||||
return -1;
|
||||
}
|
||||
assert(len >= 0);
|
||||
|
||||
/* Use the PyBytes API to decode the string, since that is what is used
|
||||
to encode, and then coerce the result to Unicode. */
|
||||
bytes = PyBytes_DecodeEscape(p, len, NULL, 0, NULL);
|
||||
free(s);
|
||||
if (bytes == NULL)
|
||||
return -1;
|
||||
str = PyUnicode_FromEncodedObject(bytes, self->encoding, self->errors);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue