2
* The contents of this file are subject to the Mozilla Public License Version
3
* 1.1 (the "License"); you may not use this file except in compliance with the
4
* License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
6
* Software distributed under the License is distributed on an "AS IS" basis,
7
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
8
* the specific language governing rights and limitations under the License.
10
* The Original Code is the Python XPCOM language bindings.
12
* The Initial Developer of the Original Code is ActiveState Tool Corp.
13
* Portions created by ActiveState Tool Corp. are Copyright (C) 2000, 2001
14
* ActiveState Tool Corp. All Rights Reserved.
16
* Contributor(s): Mark Hammond <mhammond@skippinet.com.au> (original author)
21
// This code is part of the XPCOM extensions for Python.
23
// Written May 2000 by Mark Hammond.
25
// Based heavily on the Python COM support, which is
26
// (c) Mark Hammond and Greg Stein.
28
// (c) 2000, ActiveState corp.
30
#include "PyXPCOM_std.h"
33
static nsIInterfaceInfo *GetI(PyObject *self) {
34
nsIID iid = NS_GET_IID(nsIInterfaceInfo);
36
if (!Py_nsISupports::Check(self, iid)) {
37
PyErr_SetString(PyExc_TypeError, "This object is not the correct interface");
40
return (nsIInterfaceInfo *)Py_nsISupports::GetI(self);
43
static PyObject *PyGetName(PyObject *self, PyObject *args)
45
if (!PyArg_ParseTuple(args, ":GetName"))
48
nsIInterfaceInfo *pI = GetI(self);
54
Py_BEGIN_ALLOW_THREADS;
55
r = pI->GetName(&name);
58
return PyXPCOM_BuildPyException(r);
59
PyObject *ret = PyString_FromString(name);
64
static PyObject *PyGetIID(PyObject *self, PyObject *args)
66
if (!PyArg_ParseTuple(args, ":GetIID"))
69
nsIInterfaceInfo *pI = GetI(self);
75
Py_BEGIN_ALLOW_THREADS;
76
r = pI->GetInterfaceIID(&iid_ret);
79
return PyXPCOM_BuildPyException(r);
80
PyObject *ret = Py_nsIID::PyObjectFromIID(*iid_ret);
81
nsMemory::Free(iid_ret);
85
static PyObject *PyIsScriptable(PyObject *self, PyObject *args)
87
if (!PyArg_ParseTuple(args, ":IsScriptable"))
90
nsIInterfaceInfo *pI = GetI(self);
96
Py_BEGIN_ALLOW_THREADS;
97
r = pI->IsScriptable(&b_ret);
100
return PyXPCOM_BuildPyException(r);
101
return PyInt_FromLong(b_ret);
104
static PyObject *PyGetParent(PyObject *self, PyObject *args)
106
if (!PyArg_ParseTuple(args, ":GetParent"))
109
nsIInterfaceInfo *pI = GetI(self);
113
nsIInterfaceInfo *pRet;
115
Py_BEGIN_ALLOW_THREADS;
116
r = pI->GetParent(&pRet);
117
Py_END_ALLOW_THREADS;
119
return PyXPCOM_BuildPyException(r);
120
return Py_nsISupports::PyObjectFromInterface(pRet, NS_GET_IID(nsIInterfaceInfo), PR_FALSE, PR_FALSE);
123
static PyObject *PyGetMethodCount(PyObject *self, PyObject *args)
125
if (!PyArg_ParseTuple(args, ":GetMethodCount"))
128
nsIInterfaceInfo *pI = GetI(self);
134
Py_BEGIN_ALLOW_THREADS;
135
r = pI->GetMethodCount(&ret);
136
Py_END_ALLOW_THREADS;
138
return PyXPCOM_BuildPyException(r);
139
return PyInt_FromLong(ret);
143
static PyObject *PyGetConstantCount(PyObject *self, PyObject *args)
145
if (!PyArg_ParseTuple(args, ":GetConstantCount"))
148
nsIInterfaceInfo *pI = GetI(self);
154
Py_BEGIN_ALLOW_THREADS;
155
r = pI->GetConstantCount(&ret);
156
Py_END_ALLOW_THREADS;
158
return PyXPCOM_BuildPyException(r);
159
return PyInt_FromLong(ret);
162
static PyObject *PyGetMethodInfo(PyObject *self, PyObject *args)
165
if (!PyArg_ParseTuple(args, "h:GetMethodInfo", &index))
168
nsIInterfaceInfo *pI = GetI(self);
173
pI->GetMethodCount(&nmethods);
174
if (index>=nmethods) {
175
PyErr_SetString(PyExc_ValueError, "The method index is out of range");
179
const nsXPTMethodInfo *pRet;
181
Py_BEGIN_ALLOW_THREADS;
182
r = pI->GetMethodInfo(index, &pRet);
183
Py_END_ALLOW_THREADS;
185
return PyXPCOM_BuildPyException(r);
186
return PyObject_FromXPTMethodDescriptor(pRet);
189
static PyObject *PyGetMethodInfoForName(PyObject *self, PyObject *args)
192
if (!PyArg_ParseTuple(args, "s:GetMethodInfoForName", &name))
195
nsIInterfaceInfo *pI = GetI(self);
199
const nsXPTMethodInfo *pRet;
202
Py_BEGIN_ALLOW_THREADS;
203
r = pI->GetMethodInfoForName(name, &index, &pRet);
204
Py_END_ALLOW_THREADS;
206
return PyXPCOM_BuildPyException(r);
207
PyObject *ret_i = PyObject_FromXPTMethodDescriptor(pRet);
210
PyObject *real_ret = Py_BuildValue("iO", (int)index, ret_i);
216
static PyObject *PyGetConstant(PyObject *self, PyObject *args)
219
if (!PyArg_ParseTuple(args, "h:GetConstant", &index))
222
nsIInterfaceInfo *pI = GetI(self);
226
const nsXPTConstant *pRet;
228
Py_BEGIN_ALLOW_THREADS;
229
r = pI->GetConstant(index, &pRet);
230
Py_END_ALLOW_THREADS;
232
return PyXPCOM_BuildPyException(r);
233
return PyObject_FromXPTConstant(pRet);
236
static PRBool __GetMethodInfoHelper(nsIInterfaceInfo *pii, int mi, int pi, const nsXPTMethodInfo **ppmi)
239
pii->GetMethodCount(&nmethods);
240
if (mi<0 || mi>=nmethods) {
241
PyErr_SetString(PyExc_ValueError, "The method index is out of range");
244
const nsXPTMethodInfo *pmi;
245
nsresult r = pii->GetMethodInfo(mi, &pmi);
246
if ( NS_FAILED(r) ) {
247
PyXPCOM_BuildPyException(r);
252
nparams = pmi->GetParamCount();
253
if (pi<0 || pi>=nparams) {
254
PyErr_SetString(PyExc_ValueError, "The param index is out of range");
261
static PyObject *PyGetInfoForParam(PyObject *self, PyObject *args)
263
nsIInterfaceInfo *pii = GetI(self);
267
if (!PyArg_ParseTuple(args, "hh:GetInfoForParam", &mi, &pi))
269
const nsXPTMethodInfo *pmi;
270
if (!__GetMethodInfoHelper(pii, mi, pi, &pmi))
272
const nsXPTParamInfo& param_info = pmi->GetParam((PRUint8)pi);
273
nsIInterfaceInfo *pnewii = nsnull;
274
nsresult n = pii->GetInfoForParam(mi, ¶m_info, &pnewii);
276
return PyXPCOM_BuildPyException(n);
277
return Py_nsISupports::PyObjectFromInterface(pnewii, NS_GET_IID(nsIInterfaceInfo), PR_FALSE);
280
static PyObject *PyGetIIDForParam(PyObject *self, PyObject *args)
282
nsIInterfaceInfo *pii = GetI(self);
286
if (!PyArg_ParseTuple(args, "hh:GetIIDForParam", &mi, &pi))
288
const nsXPTMethodInfo *pmi;
289
if (!__GetMethodInfoHelper(pii, mi, pi, &pmi))
291
const nsXPTParamInfo& param_info = pmi->GetParam((PRUint8)pi);
293
nsresult n = pii->GetIIDForParam(mi, ¶m_info, &piid);
294
if (NS_FAILED(n) || piid==nsnull)
295
return PyXPCOM_BuildPyException(n);
296
PyObject *rc = Py_nsIID::PyObjectFromIID(*piid);
297
nsMemory::Free((void*)piid);
301
static PyObject *PyGetTypeForParam(PyObject *self, PyObject *args)
303
nsIInterfaceInfo *pii = GetI(self);
306
PRUint16 mi, pi, dim;
307
if (!PyArg_ParseTuple(args, "hhh:GetTypeForParam", &mi, &pi, &dim))
309
const nsXPTMethodInfo *pmi;
310
if (!__GetMethodInfoHelper(pii, mi, pi, &pmi))
313
const nsXPTParamInfo& param_info = pmi->GetParam((PRUint8)pi);
314
nsresult n = pii->GetTypeForParam(mi, ¶m_info, dim, &datumType);
316
return PyXPCOM_BuildPyException(n);
317
return PyObject_FromXPTType(&datumType);
320
static PyObject *PyGetSizeIsArgNumberForParam(PyObject *self, PyObject *args)
322
nsIInterfaceInfo *pii = GetI(self);
325
PRUint16 mi, pi, dim;
326
if (!PyArg_ParseTuple(args, "hhh:GetSizeIsArgNumberForParam", &mi, &pi, &dim))
328
const nsXPTMethodInfo *pmi;
329
if (!__GetMethodInfoHelper(pii, mi, pi, &pmi))
332
const nsXPTParamInfo& param_info = pmi->GetParam((PRUint8)pi);
333
nsresult n = pii->GetSizeIsArgNumberForParam(mi, ¶m_info, dim, &ret);
335
return PyXPCOM_BuildPyException(n);
336
return PyInt_FromLong(ret);
339
static PyObject *PyGetLengthIsArgNumberForParam(PyObject *self, PyObject *args)
341
nsIInterfaceInfo *pii = GetI(self);
344
PRUint16 mi, pi, dim;
345
if (!PyArg_ParseTuple(args, "hhh:GetLengthIsArgNumberForParam", &mi, &pi, &dim))
347
const nsXPTMethodInfo *pmi;
348
if (!__GetMethodInfoHelper(pii, mi, pi, &pmi))
351
const nsXPTParamInfo& param_info = pmi->GetParam((PRUint8)pi);
352
nsresult n = pii->GetLengthIsArgNumberForParam(mi, ¶m_info, dim, &ret);
354
return PyXPCOM_BuildPyException(n);
355
return PyInt_FromLong(ret);
358
static PyObject *PyGetInterfaceIsArgNumberForParam(PyObject *self, PyObject *args)
360
nsIInterfaceInfo *pii = GetI(self);
364
if (!PyArg_ParseTuple(args, "hhh:GetInterfaceIsArgNumberForParam", &mi, &pi))
366
const nsXPTMethodInfo *pmi;
367
if (!__GetMethodInfoHelper(pii, mi, pi, &pmi))
370
const nsXPTParamInfo& param_info = pmi->GetParam((PRUint8)pi);
371
nsresult n = pii->GetInterfaceIsArgNumberForParam(mi, ¶m_info, &ret);
373
return PyXPCOM_BuildPyException(n);
374
return PyInt_FromLong(ret);
378
PyMethods_IInterfaceInfo[] =
380
{ "GetName", PyGetName, 1},
381
{ "GetIID", PyGetIID, 1},
382
{ "IsScriptable", PyIsScriptable, 1},
383
{ "GetParent", PyGetParent, 1},
384
{ "GetMethodCount", PyGetMethodCount, 1},
385
{ "GetConstantCount", PyGetConstantCount, 1},
386
{ "GetMethodInfo", PyGetMethodInfo, 1},
387
{ "GetMethodInfoForName", PyGetMethodInfoForName, 1},
388
{ "GetConstant", PyGetConstant, 1},
389
{ "GetInfoForParam", PyGetInfoForParam, 1},
390
{ "GetIIDForParam", PyGetIIDForParam, 1},
391
{ "GetTypeForParam", PyGetTypeForParam, 1},
392
{ "GetSizeIsArgNumberForParam", PyGetSizeIsArgNumberForParam, 1},
393
{ "GetLengthIsArgNumberForParam", PyGetLengthIsArgNumberForParam, 1},
394
{ "GetInterfaceIsArgNumberForParam", PyGetInterfaceIsArgNumberForParam, 1},
399
NS_IMETHOD GetMethodInfo(PRUint16 index, const nsXPTMethodInfo * *info) = 0;
400
NS_IMETHOD GetMethodInfoForName(const char *methodName, PRUint16 *index, const nsXPTMethodInfo * *info) = 0;
401
NS_IMETHOD GetConstant(PRUint16 index, const nsXPTConstant * *constant) = 0;
402
NS_IMETHOD GetInfoForParam(PRUint16 methodIndex, const nsXPTParamInfo * param, nsIInterfaceInfo **_retval) = 0;
403
NS_IMETHOD GetIIDForParam(PRUint16 methodIndex, const nsXPTParamInfo * param, nsIID * *_retval) = 0;
404
NS_IMETHOD GetTypeForParam(PRUint16 methodIndex, const nsXPTParamInfo * param, PRUint16 dimension, nsXPTType *_retval) = 0;
405
NS_IMETHOD GetSizeIsArgNumberForParam(PRUint16 methodIndex, const nsXPTParamInfo * param, PRUint16 dimension, PRUint8 *_retval) = 0;
406
NS_IMETHOD GetLengthIsArgNumberForParam(PRUint16 methodIndex, const nsXPTParamInfo * param, PRUint16 dimension, PRUint8 *_retval) = 0;
407
NS_IMETHOD GetInterfaceIsArgNumberForParam(PRUint16 methodIndex, const nsXPTParamInfo * param, PRUint8 *_retval) = 0;