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