1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23 #include "pyuno_impl.hxx"
24
25 #include <osl/thread.h>
26 #include <rtl/ustrbuf.hxx>
27
28 using rtl::OUStringToOString;
29 using rtl::OUString;
30 using com::sun::star::uno::Sequence;
31 using com::sun::star::uno::Reference;
32 using com::sun::star::uno::XInterface;
33 using com::sun::star::uno::Any;
34 using com::sun::star::uno::Type;
35 using com::sun::star::uno::TypeClass;
36 using com::sun::star::uno::RuntimeException;
37 using com::sun::star::uno::XComponentContext;
38 using com::sun::star::lang::XSingleServiceFactory;
39 using com::sun::star::script::XTypeConverter;
40 using com::sun::star::script::XInvocation2;
41
42 namespace pyuno
43 {
44 typedef struct
45 {
46 Reference<XInvocation2> xInvocation;
47 OUString methodName;
48 ConversionMode mode;
49 } PyUNO_callable_Internals;
50
51 typedef struct
52 {
53 PyObject_HEAD
54 PyUNO_callable_Internals* members;
55 } PyUNO_callable;
56
PyUNO_callable_del(PyObject * self)57 void PyUNO_callable_del (PyObject* self)
58 {
59 PyUNO_callable* me;
60
61 me = (PyUNO_callable*) self;
62 delete me->members;
63 PyObject_Del (self);
64
65 return;
66 }
67
PyUNO_callable_call(PyObject * self,PyObject * args,PyObject *)68 PyObject* PyUNO_callable_call (PyObject* self, PyObject* args, PyObject*)
69 {
70 PyUNO_callable* me;
71
72 Sequence<short> aOutParamIndex;
73 Sequence<Any> aOutParam;
74 Sequence<Any> aParams;
75 Sequence<Type> aParamTypes;
76 Any any_params;
77 Any out_params;
78 Any ret_value;
79 RuntimeCargo *cargo = 0;
80 me = (PyUNO_callable*) self;
81
82 PyRef ret;
83 try
84 {
85 Runtime runtime;
86 cargo = runtime.getImpl()->cargo;
87 any_params = runtime.pyObject2Any (args, me->members->mode);
88
89 if (any_params.getValueTypeClass () == com::sun::star::uno::TypeClass_SEQUENCE)
90 {
91 any_params >>= aParams;
92 }
93 else
94 {
95 aParams.realloc (1);
96 aParams [0] <<= any_params;
97 }
98
99 {
100 PyThreadDetach antiguard; //python free zone
101
102 // do some logging if desired ...
103 if( isLog( cargo, LogLevel::CALL ) )
104 {
105 logCall( cargo, "try py->uno[0x", me->members->xInvocation.get(),
106 me->members->methodName, aParams );
107 }
108
109 // do the call
110 ret_value = me->members->xInvocation->invoke (
111 me->members->methodName, aParams, aOutParamIndex, aOutParam);
112
113 // log the reply, if desired
114 if( isLog( cargo, LogLevel::CALL ) )
115 {
116 logReply( cargo, "success py->uno[0x", me->members->xInvocation.get(),
117 me->members->methodName, ret_value, aOutParam);
118 }
119 }
120
121
122 PyRef temp = runtime.any2PyObject (ret_value);
123 if( aOutParam.getLength() )
124 {
125 PyRef return_list( PyTuple_New (1+aOutParam.getLength()), SAL_NO_ACQUIRE );
126 PyTuple_SetItem (return_list.get(), 0, temp.getAcquired());
127
128 // initialize with defaults in case of exceptions
129 int i;
130 for( i = 1 ; i < 1+aOutParam.getLength() ; i ++ )
131 {
132 Py_INCREF( Py_None );
133 PyTuple_SetItem( return_list.get() , i , Py_None );
134 }
135
136 for( i = 0 ; i < aOutParam.getLength() ; i ++ )
137 {
138 PyRef ref = runtime.any2PyObject( aOutParam[i] );
139 PyTuple_SetItem (return_list.get(), 1+i, ref.getAcquired());
140 }
141 ret = return_list;
142 }
143 else
144 {
145 ret = temp;
146 }
147 }
148 catch( com::sun::star::reflection::InvocationTargetException & e )
149 {
150
151 if( isLog( cargo, LogLevel::CALL ) )
152 {
153 logException( cargo, "except py->uno[0x", me->members->xInvocation.get() ,
154 me->members->methodName, e.TargetException.getValue(), e.TargetException.getValueTypeRef());
155 }
156 raisePyExceptionWithAny( e.TargetException );
157 }
158 catch( com::sun::star::script::CannotConvertException &e )
159 {
160 if( isLog( cargo, LogLevel::CALL ) )
161 {
162 logException( cargo, "error py->uno[0x", me->members->xInvocation.get() ,
163 me->members->methodName, &e, getCppuType(&e).getTypeLibType());
164 }
165 raisePyExceptionWithAny( com::sun::star::uno::makeAny( e ) );
166 }
167 catch( com::sun::star::lang::IllegalArgumentException &e )
168 {
169 if( isLog( cargo, LogLevel::CALL ) )
170 {
171 logException( cargo, "error py->uno[0x", me->members->xInvocation.get() ,
172 me->members->methodName, &e, getCppuType(&e).getTypeLibType());
173 }
174 raisePyExceptionWithAny( com::sun::star::uno::makeAny( e ) );
175 }
176 catch (::com::sun::star::uno::RuntimeException &e)
177 {
178 if( cargo && isLog( cargo, LogLevel::CALL ) )
179 {
180 logException( cargo, "error py->uno[0x", me->members->xInvocation.get() ,
181 me->members->methodName, &e, getCppuType(&e).getTypeLibType());
182 }
183 raisePyExceptionWithAny( com::sun::star::uno::makeAny( e ) );
184 }
185
186 return ret.getAcquired();
187 }
188
189
190 static PyTypeObject PyUNO_callable_Type =
191 {
192 PyVarObject_HEAD_INIT(&PyType_Type, 0)
193 const_cast< char * >("PyUNO_callable"),
194 sizeof (PyUNO_callable),
195 0,
196 (destructor) ::pyuno::PyUNO_callable_del,
197 (printfunc) 0,
198 (getattrfunc) 0,
199 (setattrfunc) 0,
200 0,
201 (reprfunc) 0,
202 0,
203 0,
204 0,
205 (hashfunc) 0,
206 (ternaryfunc) ::pyuno::PyUNO_callable_call,
207 (reprfunc) 0,
208 (getattrofunc)0,
209 (setattrofunc)0,
210 NULL,
211 0,
212 NULL,
213 (traverseproc)0,
214 (inquiry)0,
215 (richcmpfunc)0,
216 0,
217 (getiterfunc)0,
218 (iternextfunc)0,
219 NULL,
220 NULL,
221 NULL,
222 NULL,
223 NULL,
224 (descrgetfunc)0,
225 (descrsetfunc)0,
226 0,
227 (initproc)0,
228 (allocfunc)0,
229 (newfunc)0,
230 (freefunc)0,
231 (inquiry)0,
232 NULL,
233 NULL,
234 NULL,
235 NULL,
236 NULL,
237 (destructor)0,
238 0
239 };
240
PyUNO_callable_new(const Reference<XInvocation2> & my_inv,const OUString & methodName,enum ConversionMode mode)241 PyRef PyUNO_callable_new (
242 const Reference<XInvocation2> &my_inv,
243 const OUString & methodName,
244 enum ConversionMode mode )
245 {
246 PyUNO_callable* self;
247
248 self = PyObject_New (PyUNO_callable, &PyUNO_callable_Type);
249 if (self == NULL)
250 return NULL; //NULL == Error!
251
252 self->members = new PyUNO_callable_Internals;
253 self->members->xInvocation = my_inv;
254 self->members->methodName = methodName;
255 self->members->mode = mode;
256
257 return PyRef( (PyObject*)self, SAL_NO_ACQUIRE );
258 }
259
260 }
261