VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/python/src/PyIClassInfo.cpp@ 59795

Last change on this file since 59795 was 59795, checked in by vboxsync, 8 years ago

temporarily back out the recent Python 3 changes (r105649, r105645, r105644, r105643, r105641)

  • Property svn:eol-style set to native
File size: 5.2 KB
Line 
1/* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3 *
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
8 *
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
13 *
14 * The Original Code is the Python XPCOM language bindings.
15 *
16 * The Initial Developer of the Original Code is
17 * ActiveState Tool Corp. Portions created by ActiveState Tool Corp. are Copyright (C) 2001 ActiveState Tool Corp. All Rights Reserved.
18 * Portions created by the Initial Developer are Copyright (C) 2001
19 * the Initial Developer. All Rights Reserved.
20 *
21 * Contributor(s):
22 * Mark Hammond <mhammond@skippinet.com.au> (original author)
23 *
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
35 *
36 * ***** END LICENSE BLOCK ***** */
37
38//
39// This code is part of the XPCOM extensions for Python.
40//
41// Written May 2001 by Mark Hammond.
42//
43// Based heavily on the Python COM support, which is
44// (c) Mark Hammond and Greg Stein.
45//
46// (c) 2001, ActiveState corp.
47
48#include "PyXPCOM_std.h"
49#include "nsIClassInfo.h"
50
51static nsIClassInfo *_GetI(PyObject *self) {
52 nsIID iid = NS_GET_IID(nsIClassInfo);
53
54 if (!Py_nsISupports::Check(self, iid)) {
55 PyErr_SetString(PyExc_TypeError, "This object is not the correct interface");
56 return NULL;
57 }
58 return (nsIClassInfo *)Py_nsISupports::GetI(self);
59}
60
61static PyObject *PyGetInterfaces(PyObject *self, PyObject *args)
62{
63 if (!PyArg_ParseTuple(args, ""))
64 return NULL;
65 nsIClassInfo *pI = _GetI(self);
66 if (pI==NULL)
67 return NULL;
68
69 nsIID** iidArray = nsnull;
70 PRUint32 iidCount = 0;
71 nsresult r;
72 Py_BEGIN_ALLOW_THREADS;
73 r = pI->GetInterfaces(&iidCount, &iidArray);
74 Py_END_ALLOW_THREADS;
75 if ( NS_FAILED(r) )
76 return PyXPCOM_BuildPyException(r);
77
78 PyObject *ret = PyTuple_New(iidCount);
79 if (ret==NULL)
80 return NULL;
81 for (PRUint32 i=0;i<iidCount;i++)
82 PyTuple_SET_ITEM( ret, i, Py_nsIID::PyObjectFromIID(*(iidArray[i])) );
83 return ret;
84}
85
86static PyObject *PyGetHelperForLanguage(PyObject *self, PyObject *args)
87{
88 PRUint32 language = nsIProgrammingLanguage::PYTHON;
89 if (!PyArg_ParseTuple(args, "|i", &language))
90 return NULL;
91 nsIClassInfo *pI = _GetI(self);
92 if (pI==NULL)
93 return NULL;
94
95 nsresult r;
96 nsCOMPtr<nsISupports> pi;
97 Py_BEGIN_ALLOW_THREADS;
98 r = pI->GetHelperForLanguage(language, getter_AddRefs(pi));
99 Py_END_ALLOW_THREADS;
100 if ( NS_FAILED(r) )
101 return PyXPCOM_BuildPyException(r);
102
103 return Py_nsISupports::PyObjectFromInterface(pi, NS_GET_IID(nsISupports));
104}
105
106static PyObject *MakeStringOrNone(char *v)
107{
108 if (v)
109 return PyString_FromString(v);
110 Py_INCREF(Py_None);
111 return Py_None;
112}
113
114#define GETATTR_CHECK_RESULT(nr) if (NS_FAILED(nr)) return PyXPCOM_BuildPyException(nr)
115
116PyObject *
117Py_nsIClassInfo::getattr(const char *name)
118{
119 nsIClassInfo *pI = _GetI(this);
120 if (pI==NULL)
121 return NULL;
122
123 nsresult nr;
124 PyObject *ret = NULL;
125 if (strcmp(name, "contractID")==0) {
126 char *str_ret = NULL;
127 Py_BEGIN_ALLOW_THREADS;
128 nr = pI->GetContractID(&str_ret);
129 Py_END_ALLOW_THREADS;
130 GETATTR_CHECK_RESULT(nr);
131 ret = MakeStringOrNone(str_ret);
132 nsMemory::Free(str_ret);
133 } else if (strcmp(name, "classDescription")==0) {
134 char *str_ret = NULL;
135 Py_BEGIN_ALLOW_THREADS;
136 nr = pI->GetClassDescription(&str_ret);
137 Py_END_ALLOW_THREADS;
138 GETATTR_CHECK_RESULT(nr);
139 ret = MakeStringOrNone(str_ret);
140 nsMemory::Free(str_ret);
141 } else if (strcmp(name, "classID")==0) {
142 nsIID *iid = NULL;
143 Py_BEGIN_ALLOW_THREADS;
144 nr = pI->GetClassID(&iid);
145 Py_END_ALLOW_THREADS;
146 GETATTR_CHECK_RESULT(nr);
147 ret = Py_nsIID::PyObjectFromIID(*iid);
148 nsMemory::Free(iid);
149 } else if (strcmp(name, "implementationLanguage")==0) {
150 PRUint32 i;
151 Py_BEGIN_ALLOW_THREADS;
152 nr = pI->GetImplementationLanguage(&i);
153 Py_END_ALLOW_THREADS;
154 GETATTR_CHECK_RESULT(nr);
155 ret = PyInt_FromLong(i);
156 } else {
157 ret = Py_nsISupports::getattr(name);
158 }
159 return ret;
160}
161
162int
163Py_nsIClassInfo::setattr(const char *name, PyObject *v)
164{
165 return Py_nsISupports::setattr(name, v);
166
167}
168
169struct PyMethodDef
170PyMethods_IClassInfo[] =
171{
172 { "getInterfaces", PyGetInterfaces, 1},
173 { "GetInterfaces", PyGetInterfaces, 1},
174 { "getHelperForLanguage", PyGetHelperForLanguage, 1},
175 { "GetHelperForLanguage", PyGetHelperForLanguage, 1},
176 {NULL}
177};
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use