mirror of
https://github.com/python/cpython.git
synced 2025-08-03 08:34:29 +00:00
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.
This commit is contained in:
parent
e9c0358bf4
commit
77f6a65eb0
29 changed files with 489 additions and 378 deletions
|
@ -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);
|
||||
|
@ -2635,6 +2655,20 @@ load_none(Unpicklerobject *self)
|
|||
return 0;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
|
@ -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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue