[Python-checkins] r51495 - in python/branches/int_unification: Include/boolobject.h Objects/boolobject.c

martin.v.loewis python-checkins at python.org
Wed Aug 23 00:49:54 CEST 2006


Author: martin.v.loewis
Date: Wed Aug 23 00:49:53 2006
New Revision: 51495

Modified:
   python/branches/int_unification/Include/boolobject.h
   python/branches/int_unification/Objects/boolobject.c
Log:
Make bool a subtype of long.


Modified: python/branches/int_unification/Include/boolobject.h
==============================================================================
--- python/branches/int_unification/Include/boolobject.h	(original)
+++ python/branches/int_unification/Include/boolobject.h	Wed Aug 23 00:49:53 2006
@@ -7,11 +7,6 @@
 #endif
 
 
-typedef struct {
-    PyObject_HEAD
-    long ob_ival;
-} PyBoolObject;
-
 PyAPI_DATA(PyTypeObject) PyBool_Type;
 
 #define PyBool_Check(x) ((x)->ob_type == &PyBool_Type)
@@ -20,7 +15,7 @@
 Don't forget to apply Py_INCREF() when returning either!!! */
 
 /* Don't use these directly */
-PyAPI_DATA(PyBoolObject) _Py_FalseStruct, _Py_TrueStruct;
+PyAPI_DATA(struct _longobject) _Py_FalseStruct, _Py_TrueStruct;
 
 /* Use these macros */
 #define Py_False ((PyObject *) &_Py_FalseStruct)

Modified: python/branches/int_unification/Objects/boolobject.c
==============================================================================
--- python/branches/int_unification/Objects/boolobject.c	(original)
+++ python/branches/int_unification/Objects/boolobject.c	Wed Aug 23 00:49:53 2006
@@ -1,13 +1,14 @@
 /* Boolean type, a subtype of int */
 
 #include "Python.h"
+#include "longintrepr.h"
 
 /* We need to define bool_print to override int_print */
 
 static int
-bool_print(PyBoolObject *self, FILE *fp, int flags)
+bool_print(PyObject *self, FILE *fp, int flags)
 {
-	fputs(self->ob_ival == 0 ? "False" : "True", fp);
+	fputs(self == Py_False ? "False" : "True", fp);
 	return 0;
 }
 
@@ -17,11 +18,11 @@
 static PyObject *true_str = NULL;
 
 static PyObject *
-bool_repr(PyBoolObject *self)
+bool_repr(PyObject *self)
 {
 	PyObject *s;
 
-	if (self->ob_ival)
+	if (self == Py_True)
 		s = true_str ? true_str :
 			(true_str = PyString_InternFromString("True"));
 	else
@@ -67,40 +68,25 @@
 static PyObject *
 bool_and(PyObject *a, PyObject *b)
 {
-	if (!PyBool_Check(a) || !PyBool_Check(b)) {
-		PyErr_BadInternalCall();
-		return NULL;
-	}
-	return PyBool_FromLong(
-		((PyBoolObject *)a)->ob_ival & ((PyBoolObject *)b)->ob_ival);
+	if (!PyBool_Check(a) || !PyBool_Check(b))
+		return PyLong_Type.tp_as_number->nb_and(a, b);
+	return PyBool_FromLong((a == Py_True) & (b == Py_True));
 }
 
 static PyObject *
 bool_or(PyObject *a, PyObject *b)
 {
-	if (!PyBool_Check(a) || !PyBool_Check(b)) {
-		PyErr_BadInternalCall();
-		return NULL;
-	}
-	return PyBool_FromLong(
-		((PyBoolObject *)a)->ob_ival | ((PyBoolObject *)b)->ob_ival);
+	if (!PyBool_Check(a) || !PyBool_Check(b))
+		return PyLong_Type.tp_as_number->nb_or(a, b);
+	return PyBool_FromLong((a == Py_True) | (b == Py_True));
 }
 
 static PyObject *
 bool_xor(PyObject *a, PyObject *b)
 {
-	if (!PyBool_Check(a) || !PyBool_Check(b)) {
-		PyErr_BadInternalCall();
-		return NULL;
-	}
-	return PyBool_FromLong(
-		((PyBoolObject *)a)->ob_ival ^ ((PyBoolObject *)b)->ob_ival);
-}
-
-static PyObject *
-bool_index(PyObject *a)
-{
-	return PyInt_FromLong(((PyBoolObject *)a)->ob_ival);
+	if (!PyBool_Check(a) || !PyBool_Check(b))
+		return PyLong_Type.tp_as_number->nb_xor(a, b);
+	return PyBool_FromLong((a == Py_True) ^ (b == Py_True));
 }
 
 /* Doc string */
@@ -151,7 +137,7 @@
 	0,			/* nb_true_divide */
 	0,			/* nb_inplace_floor_divide */
 	0,			/* nb_inplace_true_divide */
-	bool_index,		/* nb_index */
+	0,			/* nb_index */
 };
 
 /* The type object for bool.  Note that this cannot be subclassed! */
@@ -160,20 +146,20 @@
 	PyObject_HEAD_INIT(&PyType_Type)
 	0,
 	"bool",
-	sizeof(PyBoolObject),
+	sizeof(struct _longobject),
 	0,
 	0,					/* tp_dealloc */
-	(printfunc)bool_print,			/* tp_print */
+	bool_print,				/* tp_print */
 	0,					/* tp_getattr */
 	0,					/* tp_setattr */
 	0,					/* tp_compare */
-	(reprfunc)bool_repr,			/* tp_repr */
+	bool_repr,				/* tp_repr */
 	&bool_as_number,			/* tp_as_number */
 	0,					/* tp_as_sequence */
 	0,					/* tp_as_mapping */
 	0,					/* tp_hash */
         0,					/* tp_call */
-        (reprfunc)bool_repr,			/* tp_str */
+        bool_repr,				/* tp_str */
 	0,					/* tp_getattro */
 	0,					/* tp_setattro */
 	0,					/* tp_as_buffer */
@@ -188,7 +174,7 @@
 	0,					/* tp_methods */
 	0,					/* tp_members */
 	0,					/* tp_getset */
-	0,					/* tp_base */
+	&PyLong_Type,				/* tp_base */
 	0,					/* tp_dict */
 	0,					/* tp_descr_get */
 	0,					/* tp_descr_set */
@@ -201,12 +187,12 @@
 /* The objects representing bool values False and True */
 
 /* Named Zero for link-level compatibility */
-PyBoolObject _Py_FalseStruct = {
+struct _longobject _Py_FalseStruct = {
 	PyObject_HEAD_INIT(&PyBool_Type)
-	0
+	0, { 0 }
 };
 
-PyBoolObject _Py_TrueStruct = {
+struct _longobject _Py_TrueStruct = {
 	PyObject_HEAD_INIT(&PyBool_Type)
-	1
+	1, { 1 }
 };


More information about the Python-checkins mailing list