File: pclassobject.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 (176 lines) | stat: -rw-r--r-- 4,681 bytes parent folder | download | duplicates (2)
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include "pclassobject.h"
#include "ptupleobject.h"


DEFINEFN
vinfo_t* PsycoMethod_New(PyObject* func, vinfo_t* self, PyObject* cls)
{
	vinfo_t* result = vinfo_new(VirtualTime_New(&psyco_computed_method));
	
	result->array = array_new(METHOD_TOTAL);
	result->array->items[iOB_TYPE] =
		vinfo_new(CompileTime_New((long)(&PyMethod_Type)));
        
        Py_INCREF(func);
	result->array->items[iMETHOD_IM_FUNC] =
		vinfo_new(CompileTime_NewSk(sk_new((long) func, SkFlagPyObj)));
        
	vinfo_incref(self);
	result->array->items[iMETHOD_IM_SELF] = self;
        
        Py_INCREF(cls);
	result->array->items[iMETHOD_IM_CLASS] =
		vinfo_new(CompileTime_NewSk(sk_new((long) cls, SkFlagPyObj)));
        
	return result;
}


static bool compute_method(PsycoObject* po, vinfo_t* methobj)
{
	vinfo_t* newobj;
	vinfo_t* im_func;
	vinfo_t* im_self;
	vinfo_t* im_class;
	
	/* get the fields from the Python object 'methobj' */
	im_func = vinfo_getitem(methobj, iMETHOD_IM_FUNC);
	if (im_func == NULL)
		return false;
	im_self = vinfo_getitem(methobj, iMETHOD_IM_SELF);
	if (im_self == NULL)
		return false;
	im_class = vinfo_getitem(methobj, iMETHOD_IM_CLASS);
	if (im_class == NULL)
		return false;

	/* call PyMethod_New() */
	newobj = psyco_generic_call(po, PyMethod_New,
				    CfPure|CfReturnRef|CfPyErrIfNull,
				    "vvv", im_func, im_self, im_class);
	if (newobj == NULL)
		return false;

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

static PyObject* direct_compute_method(vinfo_t* methobj, char* data)
{
	PyObject* im_func;
	PyObject* im_self;
	PyObject* im_class;
	PyObject* result = NULL;

	im_func = direct_xobj_vinfo(
			vinfo_getitem(methobj, iMETHOD_IM_FUNC), data);
	im_self = direct_xobj_vinfo(
			vinfo_getitem(methobj, iMETHOD_IM_SELF), data);
	im_class = direct_xobj_vinfo(
			vinfo_getitem(methobj, iMETHOD_IM_CLASS), data);
	
	if (!PyErr_Occurred() && im_func != NULL)
		result = PyMethod_New(im_func, im_self, im_class);
	
	Py_XDECREF(im_class);
	Py_XDECREF(im_self);
	Py_XDECREF(im_func);
	return result;
}


DEFINEVAR source_virtual_t psyco_computed_method;


 /***************************************************************/
  /*** instance method objects meta-implementation             ***/


DEFINEFN
vinfo_t* pinstancemethod_call(PsycoObject* po, vinfo_t* methobj,
                              vinfo_t* arg, vinfo_t* kw)
{
	vinfo_t* im_func;
	vinfo_t* im_self;
	vinfo_t* result;
	condition_code_t cc;
	
	/* get the 'im_self' field from the Python object 'methobj' */
	im_self = psyco_get_const(po, methobj, METHOD_im_self);
	if (im_self == NULL)
		return NULL;

	cc = object_non_null(po, im_self);
	if (cc == CC_ERROR)  /* error or more likely promotion */
		return NULL;
	
	if (!runtime_condition_t(po, cc)) {
		/* Unbound methods, XXX implement me */
		goto fallback;
	}
	else
	{
		int i, argcount;
		vinfo_t* newarg;
		if (PycException_Occurred(po))
			return NULL;
		
		argcount = PsycoTuple_Load(arg);
		if (argcount < 0)
			goto fallback;
		
		newarg = PsycoTuple_New(argcount+1, NULL);
		vinfo_incref(im_self);
		PsycoTuple_GET_ITEM(newarg, 0) = im_self;
		for (i = 0; i < argcount; i++) {
			vinfo_t* v =  PsycoTuple_GET_ITEM(arg, i);
			vinfo_incref(v);
			PsycoTuple_GET_ITEM(newarg, i+1) = v;
		}
		arg = newarg;
	}

	im_func = psyco_get_const(po, methobj, METHOD_im_func);
	if (im_func == NULL)
		result = NULL;
	else
		result = PsycoObject_Call(po, im_func, arg, kw);
	vinfo_decref(arg, po);
	return result;

  fallback:
#if NEW_STYLE_TYPES   /* Python >= 2.2b1 */
	return psyco_generic_call(po, PyMethod_Type.tp_call,
				  CfReturnRef|CfPyErrIfNull,
				  "vvv", methobj, arg, kw);
#else
        /* PyMethod_Type.tp_call == NULL... */
        return psyco_generic_call(po, PyEval_CallObjectWithKeywords,
                                  CfReturnRef|CfPyErrIfNull,
                                  "vvv", methobj, arg, kw);
#endif
}

 /***************************************************************/
  /*** instances and classes                                   ***/

DEF_KNOWN_RET_TYPE_3(pinstance_new, PyInstance_New,
		     CfReturnRef|CfPyErrIfNull, &PyInstance_Type)


INITIALIZATIONFN
void psy_classobject_init(void)
{
#if NEW_STYLE_TYPES   /* Python >= 2.2b1 */
	Psyco_DefineMeta(PyMethod_Type.tp_call, pinstancemethod_call);
#endif
	Psyco_DefineMeta(PyInstance_New,	pinstance_new);

        INIT_SVIRTUAL(psyco_computed_method, compute_method,
		      direct_compute_method,
                      (1 << iMETHOD_IM_FUNC) |
                      (1 << iMETHOD_IM_SELF) |
                      (1 << iMETHOD_IM_CLASS),
                      0, 0);
}