summaryrefslogtreecommitdiffstats
path: root/Modules/cPickle.c
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2002-04-03 22:41:51 (GMT)
committerGuido van Rossum <guido@python.org>2002-04-03 22:41:51 (GMT)
commit77f6a65eb00f005939c6c7c5d6ac0f037a0ce1bd (patch)
treee92163095e7ae548c36cea459dad87db74a413ef /Modules/cPickle.c
parente9c0358bf45bd6e0fe0b17720b41d20d618e6d9d (diff)
downloadcpython-77f6a65eb00f005939c6c7c5d6ac0f037a0ce1bd.zip
cpython-77f6a65eb00f005939c6c7c5d6ac0f037a0ce1bd.tar.gz
cpython-77f6a65eb00f005939c6c7c5d6ac0f037a0ce1bd.tar.bz2
Add the 'bool' type and its values 'False' and 'True', as described in
PEP 285. Everything described in the PEP is here, and there is even some documentation. I had to fix 12 unit tests; all but one of these were printing Boolean outcomes that changed from 0/1 to False/True. (The exception is test_unicode.py, which did a type(x) == type(y) style comparison. I could've fixed that with a single line using issubtype(x, type(y)), but instead chose to be explicit about those places where a bool is expected. Still to do: perhaps more documentation; change standard library modules to return False/True from predicates.
Diffstat (limited to 'Modules/cPickle.c')
-rw-r--r--Modules/cPickle.c44
1 files changed, 44 insertions, 0 deletions
diff --git a/Modules/cPickle.c b/Modules/cPickle.c
index fb44f05..db12d4b 100644
--- a/Modules/cPickle.c
+++ b/Modules/cPickle.c
@@ -125,6 +125,9 @@ LONG Long (unbounded) integer; repr(i), then newline.
#define TUPLE 't'
#define EMPTY_TUPLE ')'
#define SETITEMS 'u'
+#define TRUE 'Z'
+#define FALSE 'z'
+
static char MARKv = MARK;
@@ -978,6 +981,17 @@ save_none(Picklerobject *self, PyObject *args)
return 0;
}
+static int
+save_bool(Picklerobject *self, PyObject *args)
+{
+ static char buf[2] = {FALSE, TRUE};
+ long l = PyInt_AS_LONG((PyIntObject *)args);
+
+ if ((*self->write_func)(self, buf + l, 1) < 0)
+ return -1;
+
+ return 0;
+}
static int
save_int(Picklerobject *self, PyObject *args)
@@ -1921,6 +1935,12 @@ save(Picklerobject *self, PyObject *args, int pers_save)
type = args->ob_type;
switch (type->tp_name[0]) {
+ case 'b':
+ if (args == Py_False || args == Py_True) {
+ res = save_bool(self, args);
+ goto finally;
+ }
+ break;
case 'i':
if (type == &PyInt_Type) {
res = save_int(self, args);
@@ -2636,6 +2656,20 @@ load_none(Unpicklerobject *self)
}
static int
+load_false(Unpicklerobject *self)
+{
+ PDATA_APPEND(self->stack, Py_False, -1);
+ return 0;
+}
+
+static int
+load_true(Unpicklerobject *self)
+{
+ PDATA_APPEND(self->stack, Py_True, -1);
+ return 0;
+}
+
+static int
bad_readline(void)
{
PyErr_SetString(UnpicklingError, "pickle data was truncated");
@@ -3777,6 +3811,16 @@ 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;