Main Page | Class List | Directories | File List | Class Members | File Members

boolean.c File Reference

#include "Python.h"
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include <math.h>

Go to the source code of this file.

Classes

struct  PyBooleanObject

Defines

#define NaN_Check(x)   isnan(x)
#define Inf_Check(x)   isinf(x)
#define Boolean_Check(v)   ((v)->ob_type == &PyBoolean_Type)
#define Boolean_Value(v)   (((PyBooleanObject *)(v))->value)

Functions

static PyObject * BooleanValue (PyObject *self, PyObject *args)
static int pyobj_as_boolean_int (PyObject *obj)
static PyObject * IsBooleanType (PyObject *self, PyObject *args)
static PyBooleanObjectboolean_NEW (int initval)
static void boolean_dealloc (PyObject *self)
static int boolean_cmp (PyObject *o1, PyObject *o2)
static PyObject * boolean_repr (PyObject *self)
static int boolean_coerce (PyObject **v, PyObject **w)
static PyObject * boolean_and (PyObject *o1, PyObject *o2)
static PyObject * boolean_or (PyObject *o1, PyObject *o2)
static PyObject * boolean_xor (PyObject *o1, PyObject *o2)
static int boolean_nonzero (PyObject *o)
static PyObject * boolean_int (PyObject *o)
static PyObject * boolean_long (PyObject *o)
static PyObject * boolean_float (PyObject *o)
 initboolean (void)

Variables

static PyBooleanObjectg_true
static PyBooleanObjectg_false
static PyObject * g_true_string
static PyObject * g_false_string
static PyTypeObject PyBoolean_Type
static PyNumberMethods boolean_as_number
static PyMethodDef booleanMethods []


Define Documentation

#define Boolean_Check  )     ((v)->ob_type == &PyBoolean_Type)
 

Definition at line 23 of file boolean.c.

Referenced by boolean_cmp(), BooleanValue(), IsBooleanType(), and pyobj_as_boolean_int().

#define Boolean_Value  )     (((PyBooleanObject *)(v))->value)
 

Definition at line 24 of file boolean.c.

Referenced by boolean_cmp(), boolean_float(), boolean_int(), boolean_long(), boolean_nonzero(), boolean_repr(), and pyobj_as_boolean_int().

#define Inf_Check  )     isinf(x)
 

Definition at line 20 of file boolean.c.

#define NaN_Check  )     isnan(x)
 

Definition at line 19 of file boolean.c.

Referenced by BooleanValue().


Function Documentation

static PyObject* boolean_and PyObject *  o1,
PyObject *  o2
[static]
 

Definition at line 175 of file boolean.c.

References pyobj_as_boolean_int().

00176 {
00177     /* FIXME: Check whether we need to conver the 1st arg.
00178        The Python/C docs don't help */
00179     int lhs = pyobj_as_boolean_int(o1), rhs = pyobj_as_boolean_int(o2);
00180     PyObject *result = NULL;
00181 
00182     result = PyInt_FromLong((long)(lhs && rhs));
00183     Py_INCREF(result);
00184     return result;
00185 }

static int boolean_cmp PyObject *  o1,
PyObject *  o2
[static]
 

Definition at line 118 of file boolean.c.

References Boolean_Check, Boolean_Value, and pyobj_as_boolean_int().

00118                                        {
00119     int result = -1;
00120     PyBooleanObject *b1;
00121     PyBooleanObject *b2;
00122 
00123     if (Boolean_Check(o1) && Boolean_Check(o2)) {
00124         b1 = (PyBooleanObject *)o1;
00125         b2 = (PyBooleanObject *)o2;
00126         result = !(Boolean_Value(o1) == Boolean_Value(o2));
00127     }
00128     else if (Boolean_Check(o1)) {
00129         b1 = (PyBooleanObject *)o1;
00130         result = !(Boolean_Value(o1) == pyobj_as_boolean_int(o2));
00131     }
00132     else if (Boolean_Check(o2)) {
00133         b2 = (PyBooleanObject *)o2;
00134         result = !(Boolean_Value(o2) == pyobj_as_boolean_int(o1));
00135     }
00136     return result;
00137 }

static int boolean_coerce PyObject **  v,
PyObject **  w
[static]
 

Definition at line 153 of file boolean.c.

00154 {
00155     PyObject *newv, *neww;
00156 
00157     if ((*v)->ob_type == (*w)->ob_type){
00158         Py_INCREF(*v);
00159         Py_INCREF(*w);
00160         return 0;
00161     }
00162     newv = PyNumber_Int(*v);
00163     neww = PyNumber_Int(*w);
00164     if (newv && neww){
00165         *v = newv;
00166         *w = neww;
00167         return 0;
00168     }
00169     Py_XDECREF(newv);
00170     Py_XDECREF(neww);
00171     return -1;  /* couldn't do it */
00172 }

static void boolean_dealloc PyObject *  self  )  [static]
 

Definition at line 112 of file boolean.c.

00113 {
00114   PyMem_DEL(self);
00115 }

static PyObject* boolean_float PyObject *  o  )  [static]
 

Definition at line 230 of file boolean.c.

References Boolean_Value.

00231 {
00232     PyBooleanObject *obj = (PyBooleanObject *)o;
00233 
00234     return PyFloat_FromDouble((double)Boolean_Value(obj));
00235 }

static PyObject* boolean_int PyObject *  o  )  [static]
 

Definition at line 214 of file boolean.c.

References Boolean_Value.

00215 {
00216     PyBooleanObject *obj = (PyBooleanObject *)o;
00217 
00218     return PyInt_FromLong((long)Boolean_Value(obj));
00219 }

static PyObject* boolean_long PyObject *  o  )  [static]
 

Definition at line 222 of file boolean.c.

References Boolean_Value.

00223 {
00224     PyBooleanObject *obj = (PyBooleanObject *)o;
00225 
00226     return PyLong_FromLong((long)Boolean_Value(obj));
00227 }

static PyBooleanObject* boolean_NEW int  initval  )  [static]
 

Definition at line 104 of file boolean.c.

References PyBoolean_Type, and PyBooleanObject::value.

Referenced by initboolean().

00105 {
00106   PyBooleanObject *object = PyObject_NEW(PyBooleanObject, &PyBoolean_Type);
00107   object->value = initval;
00108   return object;
00109 }

static int boolean_nonzero PyObject *  o  )  [static]
 

Definition at line 208 of file boolean.c.

References Boolean_Value.

00209 {
00210     return Boolean_Value((PyBooleanObject *)o);
00211 }

static PyObject* boolean_or PyObject *  o1,
PyObject *  o2
[static]
 

Definition at line 188 of file boolean.c.

References pyobj_as_boolean_int().

00189 {
00190     /* FIXME: Check whether we need to conver the 1st arg.
00191        The Python/C docs don't help */
00192     int lhs = pyobj_as_boolean_int(o1), rhs = pyobj_as_boolean_int(o2);
00193 
00194     return PyInt_FromLong((long)(lhs || rhs));
00195 }

static PyObject* boolean_repr PyObject *  self  )  [static]
 

Definition at line 140 of file boolean.c.

References Boolean_Value, g_false_string, and g_true_string.

00141 {
00142     PyObject *result;
00143 
00144     if (Boolean_Value((PyBooleanObject *)self))
00145         result = g_true_string;
00146     else
00147         result = g_false_string;
00148     Py_INCREF(result);
00149     return result;
00150 }

static PyObject* boolean_xor PyObject *  o1,
PyObject *  o2
[static]
 

Definition at line 198 of file boolean.c.

References pyobj_as_boolean_int().

00199 {
00200     /* FIXME: Check whether we need to conver the 1st arg.
00201        The Python/C docs don't help */
00202     int lhs = pyobj_as_boolean_int(o1), rhs = pyobj_as_boolean_int(o2);
00203 
00204     return PyInt_FromLong((long)(lhs ^ rhs));
00205 }

static PyObject* BooleanValue PyObject *  self,
PyObject *  args
[static]
 

Definition at line 41 of file boolean.c.

References Boolean_Check, and NaN_Check.

00041                                              {
00042     PyObject *obj;
00043     PyObject *str_func;
00044     PyBooleanObject *result = NULL;
00045 
00046     if (!PyArg_ParseTuple(args, "O|O:BooleanValue", &obj, &str_func))
00047         return NULL;
00048 
00049     if (Boolean_Check(obj)){
00050         result = (PyBooleanObject *)obj;
00051     }
00052     else if (PyFloat_Check(obj)) {
00053         if (NaN_Check(PyFloat_AS_DOUBLE(obj))) {
00054             result = g_false;
00055         }
00056         else {
00057             result = PyObject_IsTrue(obj) ? g_true : g_false;
00058         }
00059     }
00060     else if (PyNumber_Check(obj) || PySequence_Check(obj)){
00061         result = PyObject_IsTrue(obj) ? g_true : g_false;
00062     }
00063     else if (str_func) {
00064         obj = PyObject_CallFunction(str_func, "(O)", obj);
00065         if (!obj)
00066             return NULL;
00067         result = PyObject_IsTrue(obj) ? g_true : g_false;
00068         Py_DECREF(obj);
00069     }
00070     else {
00071         result = g_false;
00072     }
00073     Py_INCREF(result);
00074     return (PyObject *)result;
00075 }

initboolean void   ) 
 

Definition at line 292 of file boolean.c.

References boolean_NEW(), booleanMethods, g_false_string, g_true_string, and PyBoolean_Type.

00292                   {
00293   PyObject *m;
00294 
00295   m = Py_InitModule("boolean", booleanMethods);
00296 
00297   PyBoolean_Type.ob_type = &PyType_Type;
00298   Py_INCREF(&PyBoolean_Type);
00299   PyModule_AddObject(m, "BooleanType", (PyObject *)&PyBoolean_Type);
00300 
00301   if (g_true_string == NULL)
00302       g_true_string = PyString_FromString("true");
00303   if (g_false_string == NULL)
00304       g_false_string = PyString_FromString("false");
00305 
00306   if (g_true == NULL)
00307       g_true = boolean_NEW(1);
00308   if (g_false == NULL)
00309       g_false = boolean_NEW(0);
00310 
00311   Py_INCREF(g_true);
00312   PyModule_AddObject(m, "true", (PyObject *)g_true);
00313   Py_INCREF(g_false);
00314   PyModule_AddObject(m, "false", (PyObject *)g_false);
00315 
00316   return;
00317 }

static PyObject* IsBooleanType PyObject *  self,
PyObject *  args
[static]
 

Definition at line 88 of file boolean.c.

References Boolean_Check.

00088                                               {
00089     PyObject *obj;
00090     PyObject *result = NULL;
00091 
00092     if (!PyArg_ParseTuple(args, "O:IsBooleanType", &obj))
00093         return NULL;
00094 
00095     if (Boolean_Check(obj))
00096         result = Py_True;
00097     else
00098         result = Py_False;
00099     Py_INCREF(result);
00100     return result;
00101 }

static int pyobj_as_boolean_int PyObject *  obj  )  [static]
 

Definition at line 78 of file boolean.c.

References Boolean_Check, and Boolean_Value.

Referenced by boolean_and(), boolean_cmp(), boolean_or(), and boolean_xor().

00078                                     {
00079     if (Boolean_Check(obj))
00080         return Boolean_Value((PyBooleanObject *)obj);
00081     else if (PyNumber_Check(obj) || PySequence_Check(obj))
00082         return PyObject_IsTrue(obj) ? 1 : 0;
00083     else
00084         return 0;
00085 }


Variable Documentation

PyNumberMethods boolean_as_number [static]
 

Initial value:

 {
    0,                 
    0,                 
    0,                 
    0,                 
    0,                 
    0,                 
    0,                 
    0,                 
    0,                 
    0,                 
    boolean_nonzero,   
    0,                 
    0,                 
    0,                 
    boolean_and,       
    boolean_xor,       
    boolean_or,        
    boolean_coerce,    
    boolean_int,       
    boolean_long,      
    boolean_float,     
    0,                 
    0,                 
}

Definition at line 237 of file boolean.c.

PyMethodDef booleanMethods[] [static]
 

Initial value:

 {
     { "BooleanValue",  BooleanValue,  METH_VARARGS },
     { "IsBooleanType", IsBooleanType, METH_VARARGS },
     { NULL, NULL }
}

Definition at line 285 of file boolean.c.

Referenced by initboolean().

PyBooleanObject* g_false [static]
 

Definition at line 32 of file boolean.c.

PyObject* g_false_string [static]
 

Definition at line 35 of file boolean.c.

Referenced by boolean_repr(), and initboolean().

PyBooleanObject* g_true [static]
 

Definition at line 31 of file boolean.c.

PyObject* g_true_string [static]
 

Definition at line 34 of file boolean.c.

Referenced by boolean_repr(), and initboolean().

static PyTypeObject PyBoolean_Type [static]
 

Initial value:

 {
    PyObject_HEAD_INIT(0)
    0,
    "boolean",
    sizeof(PyBooleanObject),
    0,
    boolean_dealloc,      
    0,                    
    0,                    
    0,                    
    (cmpfunc)boolean_cmp, 
    boolean_repr,         
    &boolean_as_number,   
    0,                    
    0,                    
    0,                    
    0,                    
    0,                    
    0,                    
    0,                    
}

Definition at line 263 of file boolean.c.

Referenced by boolean_NEW(), and initboolean().


© sourcejam.com 2005-2008