diff options
author | Tim Peters <tim.peters@gmail.com> | 2003-02-02 16:09:05 (GMT) |
---|---|---|
committer | Tim Peters <tim.peters@gmail.com> | 2003-02-02 16:09:05 (GMT) |
commit | 4190fb84293b4c54127e1b7be2d9bdf06edf2910 (patch) | |
tree | 7a307603c38ceececee2b17ffd9f7aa69ec8e69c /Modules | |
parent | 1ecfb73c269f1475d4b2738ef76c20eaecbaa7e9 (diff) | |
download | cpython-4190fb84293b4c54127e1b7be2d9bdf06edf2910.zip cpython-4190fb84293b4c54127e1b7be2d9bdf06edf2910.tar.gz cpython-4190fb84293b4c54127e1b7be2d9bdf06edf2910.tar.bz2 |
Add cPickle support for PROTO. Duplicated PROTO/LONG1/LONG4 code in
the hitherto unknown (to me) noload() cPickle function, which is (a)
something we don't test at all, and (b) pickle.py doesn't have.
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/cPickle.c | 58 |
1 files changed, 56 insertions, 2 deletions
diff --git a/Modules/cPickle.c b/Modules/cPickle.c index b59f573..43a8d33 100644 --- a/Modules/cPickle.c +++ b/Modules/cPickle.c @@ -2213,13 +2213,22 @@ dump(Picklerobject *self, PyObject *args) { static char stop = STOP; + if (self->proto >= 2) { + char bytes[2]; + + bytes[0] = PROTO; + bytes[1] = CURRENT_PROTOCOL_NUMBER; + if (self->write_func(self, bytes, 2) < 0) + return -1; + } + if (save(self, args, 0) < 0) return -1; - if ((*self->write_func)(self, &stop, 1) < 0) + if (self->write_func(self, &stop, 1) < 0) return -1; - if ((*self->write_func)(self, NULL, 0) < 0) + if (self->write_func(self, NULL, 0) < 0) return -1; return 0; @@ -3870,6 +3879,31 @@ load_reduce(Unpicklerobject *self) return 0; } +/* Just raises an error if we don't know the protocol specified. PROTO + * is the first opcode for protocols >= 2. + */ +static int +load_proto(Unpicklerobject *self) +{ + int i; + char *protobyte; + + i = self->read_func(self, &protobyte, 1); + if (i < 0) + return -1; + + i = calc_binint(protobyte, 1); + /* No point checking for < 0, since calc_binint returns an unsigned + * int when chewing on 1 byte. + */ + assert(i >= 0); + if (i <= CURRENT_PROTOCOL_NUMBER) + return 0; + + PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i); + return -1; +} + static PyObject * load(Unpicklerobject *self) { @@ -4099,6 +4133,11 @@ load(Unpicklerobject *self) break; continue; + case PROTO: + if (load_proto(self) < 0) + break; + continue; + case '\0': /* end of file */ PyErr_SetNone(PyExc_EOFError); @@ -4227,6 +4266,16 @@ noload(Unpicklerobject *self) break; continue; + case LONG1: + if (load_counted_long(self, 1) < 0) + break; + continue; + + case LONG4: + if (load_counted_long(self, 4) < 0) + break; + continue; + case FLOAT: if (load_float(self) < 0) break; @@ -4402,6 +4451,11 @@ noload(Unpicklerobject *self) break; continue; + case PROTO: + if (load_proto(self) < 0) + break; + continue; + default: cPickle_ErrFormat(UnpicklingError, "invalid load key, '%s'.", |