summaryrefslogtreecommitdiffstats
path: root/Modules/_pickle.c
diff options
context:
space:
mode:
authorAlexandre Vassalotti <alexandre@peadrop.com>2013-04-16 06:14:55 (GMT)
committerAlexandre Vassalotti <alexandre@peadrop.com>2013-04-16 06:14:55 (GMT)
commit7c5e094cbfa6769bf4cabfa5f883f2dc5320667b (patch)
tree096be7fc037ead2cf551a1ce25c8b7afd4dee463 /Modules/_pickle.c
parent48a2e7c357b7f2148ada9473c796351cc562284b (diff)
downloadcpython-7c5e094cbfa6769bf4cabfa5f883f2dc5320667b.zip
cpython-7c5e094cbfa6769bf4cabfa5f883f2dc5320667b.tar.gz
cpython-7c5e094cbfa6769bf4cabfa5f883f2dc5320667b.tar.bz2
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.
Diffstat (limited to 'Modules/_pickle.c')
-rw-r--r--Modules/_pickle.c25
1 files changed, 6 insertions, 19 deletions
diff --git a/Modules/_pickle.c b/Modules/_pickle.c
index 2c83185..fbbb745 100644
--- a/Modules/_pickle.c
+++ b/Modules/_pickle.c
@@ -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);