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.
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 @@
#define TUPLE 't'
#define EMPTY_TUPLE ')'
#define SETITEMS 'u'
+#define TRUE 'Z'
+#define FALSE 'z'
+
static char MARKv = MARK;
@@ -978,6 +981,17 @@
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 @@
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 @@
}
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 @@
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;