File: pboolobject.c

package info (click to toggle)
psyco 1.5.1-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,864 kB
  • ctags: 3,295
  • sloc: ansic: 24,491; python: 5,573; perl: 1,309; makefile: 166; sh: 1
file content (110 lines) | stat: -rw-r--r-- 2,664 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include "pboolobject.h"
#include "pintobject.h"

#if BOOLEAN_TYPE    /* Booleans were introduced in Python 2.3 */


static bool compute_bool(PsycoObject* po, vinfo_t* boolobj)
{
	vinfo_t* newobj;
	vinfo_t* x;
	condition_code_t cc;
	
	/* get the field 'ob_ival' from the Python object 'boolobj' */
	x = vinfo_getitem(boolobj, iBOOL_OB_IVAL);
	if (x == NULL)
		return false;

	cc = integer_non_null(po, x);
	if (cc == CC_ERROR)
		return false;
	newobj = integer_conditional(po, cc,
				     (long) Py_True,
				     (long) Py_False);
	if (newobj == NULL)
		return false;

	/* move the resulting non-virtual Python object back into 'boolobj' */
	vinfo_move(po, boolobj, newobj);
	return true;
}

static PyObject* direct_compute_bool(vinfo_t* boolobj, char* data)
{
	PyObject* result;
	int ival;
	ival = direct_read_vinfo(vinfo_getitem(boolobj, iBOOL_OB_IVAL), data);
	if (ival == -1 && PyErr_Occurred())
		return NULL;
	result = ival ? Py_True : Py_False;
	Py_INCREF(result);
	return result;
}


DEFINEVAR source_virtual_t psyco_computed_bool;


 /***************************************************************/
  /*** boolean objects meta-implementation                     ***/

#define CONVERT_TO_BOOL(vobj, vlng)				\
	switch (Psyco_VerifyType(po, vobj, &PyBool_Type)) {	\
	case true:   /* vobj is a PyBoolObject */		\
		vlng = PsycoInt_AS_LONG(po, vobj);		\
		if (vlng == NULL)				\
			return NULL;				\
		break;						\
	case false:  /* vobj is not a PyBoolObject */		\
		return pint_base2op(po, v, w, op);		\
	default:     /* error or promotion */			\
		return NULL;					\
	}

static vinfo_t* pbool_base2op(PsycoObject* po, vinfo_t* v, vinfo_t* w,
			      vinfo_t*(*op)(PsycoObject*,vinfo_t*,vinfo_t*))
{
	vinfo_t* a;
	vinfo_t* b;
	vinfo_t* x;
	CONVERT_TO_BOOL(v, a);
	CONVERT_TO_BOOL(w, b);
	x = op (po, a, b);
	if (x != NULL)
		x = PsycoBool_FROM_LONG(x);
	return x;
}

static vinfo_t* pbool_or(PsycoObject* po, vinfo_t* v, vinfo_t* w)
{
	return pbool_base2op(po, v, w, integer_or);
}

static vinfo_t* pbool_xor(PsycoObject* po, vinfo_t* v, vinfo_t* w)
{
	return pbool_base2op(po, v, w, integer_xor);
}

static vinfo_t* pbool_and(PsycoObject* po, vinfo_t* v, vinfo_t* w)
{
	return pbool_base2op(po, v, w, integer_and);
}


INITIALIZATIONFN
void psy_boolobject_init(void)
{
	PyNumberMethods *m = PyBool_Type.tp_as_number;

	Psyco_DefineMeta(m->nb_or,       pbool_or);
	Psyco_DefineMeta(m->nb_xor,      pbool_xor);
	Psyco_DefineMeta(m->nb_and,      pbool_and);

	INIT_SVIRTUAL(psyco_computed_bool, compute_bool,
		      direct_compute_bool, 0, 0, 0);
}

#else /* !BOOLEAN_TYPE */
INITIALIZATIONFN
void psy_boolobject_init(void) { }
#endif /* BOOLEAN_TYPE */