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