diff options
author | Guido van Rossum <guido@python.org> | 2002-04-05 19:30:08 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2002-04-05 19:30:08 (GMT) |
commit | e276339cea6a8ea9ac9ff72ed9e1b8740eceac49 (patch) | |
tree | b6bed6eb8aae111a9d2fe7cbb000c50a881719e3 /Modules | |
parent | d15a0a05d3161930928355e0b89091994bc54ee5 (diff) | |
download | cpython-e276339cea6a8ea9ac9ff72ed9e1b8740eceac49.zip cpython-e276339cea6a8ea9ac9ff72ed9e1b8740eceac49.tar.gz cpython-e276339cea6a8ea9ac9ff72ed9e1b8740eceac49.tar.bz2 |
Implement an idea by Paul Rubin:
Change pickling format for bools to use a backwards compatible
encoding. This means you can pickle True or False on Python 2.3
and Python 2.2 or before will read it back as 1 or 0. The code
used for pickling bools before would create pickles that could
not be read in previous Python versions.
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/cPickle.c | 26 |
1 files changed, 11 insertions, 15 deletions
diff --git a/Modules/cPickle.c b/Modules/cPickle.c index 9871627..a99d69f 100644 --- a/Modules/cPickle.c +++ b/Modules/cPickle.c @@ -77,8 +77,8 @@ LONG Long (unbounded) integer; repr(i), then newline. #define TUPLE 't' #define EMPTY_TUPLE ')' #define SETITEMS 'u' -#define TRUE 'Z' -#define FALSE 'z' +#define TRUE "I01\n" +#define FALSE "I00\n" static char MARKv = MARK; @@ -936,10 +936,11 @@ save_none(Picklerobject *self, PyObject *args) static int save_bool(Picklerobject *self, PyObject *args) { - static char buf[2] = {FALSE, TRUE}; + static char *buf[2] = {FALSE, TRUE}; + static char len[2] = {sizeof(FALSE)-1, sizeof(TRUE)-1}; long l = PyInt_AS_LONG((PyIntObject *)args); - if ((*self->write_func)(self, buf + l, 1) < 0) + if ((*self->write_func)(self, buf[l], len[l]) < 0) return -1; return 0; @@ -2655,7 +2656,12 @@ load_int(Unpicklerobject *self) } } else { - if (!( py_int = PyInt_FromLong(l))) goto finally; + if (len == 3 && (l == 0 || l == 1)) { + if (!( py_int = PyBool_FromLong(l))) goto finally; + } + else { + if (!( py_int = PyInt_FromLong(l))) goto finally; + } } free(s); @@ -3763,16 +3769,6 @@ load(Unpicklerobject *self) break; continue; - case FALSE: - if (load_false(self) < 0) - break; - continue; - - case TRUE: - if (load_true(self) < 0) - break; - continue; - case BININT: if (load_binint(self) < 0) break; |