diff options
author | Guido van Rossum <guido@python.org> | 2002-04-03 22:41:51 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2002-04-03 22:41:51 (GMT) |
commit | 77f6a65eb00f005939c6c7c5d6ac0f037a0ce1bd (patch) | |
tree | e92163095e7ae548c36cea459dad87db74a413ef /Modules | |
parent | e9c0358bf45bd6e0fe0b17720b41d20d618e6d9d (diff) | |
download | cpython-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')
-rw-r--r-- | Modules/cPickle.c | 44 | ||||
-rw-r--r-- | Modules/operator.c | 20 |
2 files changed, 57 insertions, 7 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; diff --git a/Modules/operator.c b/Modules/operator.c index 0c6dfed..d73744d 100644 --- a/Modules/operator.c +++ b/Modules/operator.c @@ -102,7 +102,7 @@ used for special class methods; variants without leading and trailing\n\ PyObject *a1; long r; \ if(! PyArg_ParseTuple(a,"O:" #OP,&a1)) return NULL; \ if(-1 == (r=AOP(a1))) return NULL; \ - return PyInt_FromLong(r); } + return PyBool_FromLong(r); } #define spami2(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \ PyObject *a1, *a2; long r; \ @@ -110,6 +110,12 @@ used for special class methods; variants without leading and trailing\n\ if(-1 == (r=AOP(a1,a2))) return NULL; \ return PyInt_FromLong(r); } +#define spami2b(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \ + PyObject *a1, *a2; long r; \ + if(! PyArg_ParseTuple(a,"OO:" #OP,&a1,&a2)) return NULL; \ + if(-1 == (r=AOP(a1,a2))) return NULL; \ + return PyBool_FromLong(r); } + #define spamrc(OP,A) static PyObject *OP(PyObject *s, PyObject *a) { \ PyObject *a1, *a2; \ if(! PyArg_ParseTuple(a,"OO:" #OP,&a1,&a2)) return NULL; \ @@ -139,8 +145,8 @@ spam2(op_or_ , PyNumber_Or) spami(isSequenceType , PySequence_Check) spam2(op_concat , PySequence_Concat) spamoi(op_repeat , PySequence_Repeat) -spami2(op_contains , PySequence_Contains) -spami2(sequenceIncludes, PySequence_Contains) +spami2b(op_contains , PySequence_Contains) +spami2b(sequenceIncludes, PySequence_Contains) spami2(indexOf , PySequence_Index) spami2(countOf , PySequence_Count) spami(isMappingType , PyMapping_Check) @@ -208,11 +214,11 @@ static struct PyMethodDef operator_methods[] = { spam1(isCallable, "isCallable(a) -- Same as callable(a).") spam1(isNumberType, - "isNumberType(a) -- Return 1 if a has a numeric type, and zero otherwise.") + "isNumberType(a) -- Return True if a has a numeric type, False otherwise.") spam1(isSequenceType, - "isSequenceType(a) -- Return 1 if a has a sequence type, and zero otherwise.") + "isSequenceType(a) -- Return True if a has a sequence type, False otherwise.") spam1(truth, - "truth(a) -- Return 1 if a is true, and 0 otherwise.") + "truth(a) -- Return True if a is true, False otherwise.") spam2(contains,__contains__, "contains(a, b) -- Same as b in a (note reversed operands).") spam1(sequenceIncludes, @@ -222,7 +228,7 @@ spam1(indexOf, spam1(countOf, "countOf(a, b) -- Return the number of times b occurs in a.") spam1(isMappingType, - "isMappingType(a) -- Return 1 if a has a mapping type, and zero otherwise.") + "isMappingType(a) -- Return True if a has a mapping type, False otherwise.") spam2(add,__add__, "add(a, b) -- Same as a + b.") spam2(sub,__sub__, "sub(a, b) -- Same as a - b.") |