xref: /trunk/main/pyuno/source/module/pyuno_callable.cxx (revision 60fcfbce1cf55d512c6ebbf7aee11464c2706d80)
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::XInvocation;
41 using com::sun::star::script::CannotConvertException;
42 using com::sun::star::lang::IllegalArgumentException;
43 using com::sun::star::reflection::XIdlClass;
44 using com::sun::star::reflection::XIdlMethod;
45 using com::sun::star::reflection::ParamInfo;
46 using com::sun::star::reflection::ParamMode_IN;
47 using com::sun::star::reflection::ParamMode_OUT;
48 
49 namespace pyuno
50 {
51 typedef struct
52 {
53     Reference<XInvocation> xInvocation;
54     OUString methodName;
55     ConversionMode mode;
56     Reference<XIdlMethod> xMethod;  // set when the object's XInvocation does not know the method
57     Any target;
58 } PyUNO_callable_Internals;
59 
60 typedef struct
61 {
62     PyObject_HEAD
63     PyUNO_callable_Internals* members;
64 } PyUNO_callable;
65 
PyUNO_callable_del(PyObject * self)66 void PyUNO_callable_del (PyObject* self)
67 {
68     PyUNO_callable* me;
69 
70     me = (PyUNO_callable*) self;
71     delete me->members;
72     PyObject_Del (self);
73 
74     return;
75 }
76 
77 // Same argument and out-parameter handling as Invocation_Impl::invoke() has for introspected objects.
invokeReflected(RuntimeCargo * cargo,const Reference<XIdlMethod> & xMethod,const Any & target,const Sequence<Any> & aParams,Sequence<sal_Int16> & aOutIndex,Sequence<Any> & aOutParam)78 static Any invokeReflected(
79     RuntimeCargo *cargo, const Reference<XIdlMethod> &xMethod, const Any &target,
80     const Sequence<Any> &aParams, Sequence<sal_Int16> &aOutIndex, Sequence<Any> &aOutParam )
81 {
82     Sequence<ParamInfo> infos = xMethod->getParameterInfos();
83     sal_Int32 nParams = infos.getLength();
84     if( nParams != aParams.getLength() )
85         throw IllegalArgumentException(
86             OUString( RTL_CONSTASCII_USTRINGPARAM(
87                           "incorrect number of parameters passed invoking function " ) )
88             + xMethod->getName(),
89             Reference<XInterface>(), (sal_Int16) 1 );
90 
91     Sequence<Any> aInvoke( nParams );
92     aOutIndex.realloc( nParams );
93     sal_Int32 nOut = 0;
94     for( sal_Int32 i = 0 ; i < nParams ; i ++ )
95     {
96         try
97         {
98             const ParamInfo &info = infos[i];
99             if( info.aMode != ParamMode_OUT )
100             {
101                 Reference<XIdlClass> src(
102                     cargo->xCoreReflection->forName( aParams[i].getValueType().getTypeName() ) );
103                 if( src.is() && info.aType->isAssignableFrom( src ) )
104                     aInvoke[i] = aParams[i];
105                 else
106                     aInvoke[i] = cargo->xTypeConverter->convertTo(
107                         aParams[i], Type( info.aType->getTypeClass(), info.aType->getName() ) );
108             }
109             if( info.aMode != ParamMode_IN )
110             {
111                 aOutIndex[nOut++] = (sal_Int16) i;
112                 if( info.aMode == ParamMode_OUT )
113                     info.aType->createObject( aInvoke[i] );
114             }
115         }
116         catch( CannotConvertException &e )
117         {
118             e.ArgumentIndex = (sal_Int16) i;
119             throw;
120         }
121     }
122 
123     Any ret = xMethod->invoke( target, aInvoke );
124 
125     aOutIndex.realloc( nOut );
126     aOutParam.realloc( nOut );
127     for( sal_Int32 j = 0 ; j < nOut ; j ++ )
128         aOutParam[j] = aInvoke[ aOutIndex[j] ];
129     return ret;
130 }
131 
PyUNO_callable_call(PyObject * self,PyObject * args,PyObject *)132 PyObject* PyUNO_callable_call (PyObject* self, PyObject* args, PyObject*)
133 {
134     PyUNO_callable* me;
135 
136     Sequence<short> aOutParamIndex;
137     Sequence<Any> aOutParam;
138     Sequence<Any> aParams;
139     Sequence<Type> aParamTypes;
140     Any any_params;
141     Any out_params;
142     Any ret_value;
143     RuntimeCargo *cargo = 0;
144     me = (PyUNO_callable*) self;
145 
146     PyRef ret;
147     try
148     {
149         Runtime runtime;
150         cargo = runtime.getImpl()->cargo;
151         any_params = runtime.pyObject2Any (args, me->members->mode);
152 
153         if (any_params.getValueTypeClass () == com::sun::star::uno::TypeClass_SEQUENCE)
154         {
155             any_params >>= aParams;
156         }
157         else
158         {
159             aParams.realloc (1);
160             aParams [0] <<= any_params;
161         }
162 
163         {
164             PyThreadDetach antiguard; //python free zone
165 
166             // do some logging if desired ...
167             if( isLog( cargo, LogLevel::CALL ) )
168             {
169                 logCall( cargo, "try     py->uno[0x", me->members->xInvocation.get(),
170                          me->members->methodName, aParams );
171             }
172 
173             // do the call
174             if( me->members->xMethod.is() )
175                 ret_value = invokeReflected(
176                     cargo, me->members->xMethod, me->members->target,
177                     aParams, aOutParamIndex, aOutParam );
178             else
179                 ret_value = me->members->xInvocation->invoke (
180                     me->members->methodName, aParams, aOutParamIndex, aOutParam);
181 
182             // log the reply, if desired
183             if( isLog( cargo, LogLevel::CALL ) )
184             {
185                 logReply( cargo, "success py->uno[0x", me->members->xInvocation.get(),
186                           me->members->methodName, ret_value, aOutParam);
187             }
188         }
189 
190 
191         PyRef temp = runtime.any2PyObject (ret_value);
192         if( aOutParam.getLength() )
193         {
194             PyRef return_list( PyTuple_New (1+aOutParam.getLength()), SAL_NO_ACQUIRE );
195             PyTuple_SetItem (return_list.get(), 0, temp.getAcquired());
196 
197             // initialize with defaults in case of exceptions
198             int i;
199             for( i = 1 ; i < 1+aOutParam.getLength() ; i ++ )
200             {
201                 Py_INCREF( Py_None );
202                 PyTuple_SetItem( return_list.get() , i , Py_None );
203             }
204 
205             for( i = 0 ; i < aOutParam.getLength() ; i ++ )
206             {
207                 PyRef ref = runtime.any2PyObject( aOutParam[i] );
208                 PyTuple_SetItem (return_list.get(), 1+i, ref.getAcquired());
209             }
210             ret = return_list;
211         }
212         else
213         {
214             ret = temp;
215         }
216     }
217     catch( com::sun::star::reflection::InvocationTargetException & e )
218     {
219 
220         if( isLog( cargo, LogLevel::CALL ) )
221         {
222             logException( cargo, "except  py->uno[0x", me->members->xInvocation.get() ,
223                           me->members->methodName, e.TargetException.getValue(), e.TargetException.getValueTypeRef());
224         }
225         raisePyExceptionWithAny( e.TargetException );
226     }
227     catch( com::sun::star::script::CannotConvertException &e )
228     {
229         if( isLog( cargo, LogLevel::CALL ) )
230         {
231             logException( cargo, "error  py->uno[0x", me->members->xInvocation.get() ,
232                           me->members->methodName, &e, getCppuType(&e).getTypeLibType());
233         }
234         raisePyExceptionWithAny( com::sun::star::uno::makeAny( e ) );
235     }
236     catch( com::sun::star::lang::IllegalArgumentException &e )
237     {
238         if( isLog( cargo, LogLevel::CALL ) )
239         {
240             logException( cargo, "error  py->uno[0x", me->members->xInvocation.get() ,
241                           me->members->methodName, &e, getCppuType(&e).getTypeLibType());
242         }
243         raisePyExceptionWithAny( com::sun::star::uno::makeAny( e ) );
244     }
245     catch (::com::sun::star::uno::RuntimeException &e)
246     {
247         if( cargo && isLog( cargo, LogLevel::CALL ) )
248         {
249             logException( cargo, "error  py->uno[0x", me->members->xInvocation.get() ,
250                           me->members->methodName, &e, getCppuType(&e).getTypeLibType());
251         }
252         raisePyExceptionWithAny( com::sun::star::uno::makeAny( e ) );
253     }
254 
255     return ret.getAcquired();
256 }
257 
258 
259 static PyTypeObject PyUNO_callable_Type =
260 {
261     PyVarObject_HEAD_INIT(&PyType_Type, 0)
262     const_cast< char * >("PyUNO_callable"),
263     sizeof (PyUNO_callable),
264     0,
265     (destructor) ::pyuno::PyUNO_callable_del,
266     (printfunc) 0,
267     (getattrfunc) 0,
268     (setattrfunc) 0,
269     0,
270     (reprfunc) 0,
271     0,
272     0,
273     0,
274     (hashfunc) 0,
275     (ternaryfunc) ::pyuno::PyUNO_callable_call,
276     (reprfunc) 0,
277         (getattrofunc)0,
278     (setattrofunc)0,
279     NULL,
280     0,
281     NULL,
282     (traverseproc)0,
283     (inquiry)0,
284     (richcmpfunc)0,
285     0,
286     (getiterfunc)0,
287     (iternextfunc)0,
288     NULL,
289     NULL,
290     NULL,
291     NULL,
292     NULL,
293     (descrgetfunc)0,
294     (descrsetfunc)0,
295     0,
296     (initproc)0,
297     (allocfunc)0,
298     (newfunc)0,
299     (freefunc)0,
300     (inquiry)0,
301     NULL,
302     NULL,
303     NULL,
304     NULL,
305     NULL,
306     (destructor)0,
307     0
308 };
309 
PyUNO_callable_new(const Reference<XInvocation> & my_inv,const OUString & methodName,enum ConversionMode mode,const Reference<XIdlMethod> & xMethod,const Any & target)310 PyRef PyUNO_callable_new (
311     const Reference<XInvocation> &my_inv,
312     const OUString & methodName,
313     enum ConversionMode mode,
314     const Reference<XIdlMethod> &xMethod,
315     const Any &target )
316 {
317     PyUNO_callable* self;
318 
319     self = PyObject_New (PyUNO_callable, &PyUNO_callable_Type);
320     if (self == NULL)
321         return NULL; //NULL == Error!
322 
323     self->members = new PyUNO_callable_Internals;
324     self->members->xInvocation = my_inv;
325     self->members->methodName = methodName;
326     self->members->mode = mode;
327     self->members->xMethod = xMethod;
328     self->members->target = target;
329 
330     return PyRef( (PyObject*)self, SAL_NO_ACQUIRE );
331 }
332 
333 }
334