bump product version to 4.1.6.2
[LibreOffice.git] / pyuno / source / module / pyuno_callable.cxx
blob4175c8066e769dbc503077315324246f80e99cc7
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 #include "pyuno_impl.hxx"
21 #include <osl/thread.h>
22 #include <rtl/ustrbuf.hxx>
24 using com::sun::star::uno::Sequence;
25 using com::sun::star::uno::Reference;
26 using com::sun::star::uno::XInterface;
27 using com::sun::star::uno::Any;
28 using com::sun::star::uno::Type;
29 using com::sun::star::uno::TypeClass;
30 using com::sun::star::uno::RuntimeException;
31 using com::sun::star::uno::XComponentContext;
32 using com::sun::star::lang::XSingleServiceFactory;
33 using com::sun::star::script::XTypeConverter;
34 using com::sun::star::script::XInvocation2;
36 namespace pyuno
38 typedef struct
40 Reference<XInvocation2> xInvocation;
41 Reference<XSingleServiceFactory> xInvocationFactory;
42 Reference<XTypeConverter> xTypeConverter;
43 OUString methodName;
44 ConversionMode mode;
45 } PyUNO_callable_Internals;
47 typedef struct
49 PyObject_HEAD
50 PyUNO_callable_Internals* members;
51 } PyUNO_callable;
53 void PyUNO_callable_del (PyObject* self)
55 PyUNO_callable* me;
57 me = (PyUNO_callable*) self;
58 delete me->members;
59 PyObject_Del (self);
61 return;
64 PyObject* PyUNO_callable_call(
65 PyObject* self, PyObject* args, SAL_UNUSED_PARAMETER PyObject*)
67 PyUNO_callable* me;
69 Sequence<short> aOutParamIndex;
70 Sequence<Any> aOutParam;
71 Sequence<Any> aParams;
72 Sequence<Type> aParamTypes;
73 Any any_params;
74 Any out_params;
75 Any ret_value;
76 RuntimeCargo *cargo = 0;
77 me = (PyUNO_callable*) self;
79 PyRef ret;
80 try
82 Runtime runtime;
83 cargo = runtime.getImpl()->cargo;
84 any_params = runtime.pyObject2Any (args, me->members->mode);
86 if (any_params.getValueTypeClass () == com::sun::star::uno::TypeClass_SEQUENCE)
88 any_params >>= aParams;
90 else
92 aParams.realloc (1);
93 aParams [0] <<= any_params;
97 PyThreadDetach antiguard; //pyhton free zone
99 // do some logging if desired ...
100 if( isLog( cargo, LogLevel::CALL ) )
102 logCall( cargo, "try py->uno[0x", me->members->xInvocation.get(),
103 me->members->methodName, aParams );
106 // do the call
107 ret_value = me->members->xInvocation->invoke (
108 me->members->methodName, aParams, aOutParamIndex, aOutParam);
110 // log the reply, if desired
111 if( isLog( cargo, LogLevel::CALL ) )
113 logReply( cargo, "success py->uno[0x", me->members->xInvocation.get(),
114 me->members->methodName, ret_value, aOutParam);
119 PyRef temp = runtime.any2PyObject (ret_value);
120 if( aOutParam.getLength() )
122 PyRef return_list( PyTuple_New (1+aOutParam.getLength()), SAL_NO_ACQUIRE );
123 PyTuple_SetItem (return_list.get(), 0, temp.getAcquired());
125 // initialize with defaults in case of exceptions
126 int i;
127 for( i = 1 ; i < 1+aOutParam.getLength() ; i ++ )
129 Py_INCREF( Py_None );
130 PyTuple_SetItem( return_list.get() , i , Py_None );
133 for( i = 0 ; i < aOutParam.getLength() ; i ++ )
135 PyRef ref = runtime.any2PyObject( aOutParam[i] );
136 PyTuple_SetItem (return_list.get(), 1+i, ref.getAcquired());
138 ret = return_list;
140 else
142 ret = temp;
145 catch( const com::sun::star::reflection::InvocationTargetException & e )
148 if( isLog( cargo, LogLevel::CALL ) )
150 logException( cargo, "except py->uno[0x", me->members->xInvocation.get() ,
151 me->members->methodName, e.TargetException.getValue(), e.TargetException.getValueTypeRef());
153 raisePyExceptionWithAny( e.TargetException );
155 catch( const com::sun::star::script::CannotConvertException &e )
157 if( isLog( cargo, LogLevel::CALL ) )
159 logException( cargo, "error py->uno[0x", me->members->xInvocation.get() ,
160 me->members->methodName, &e, getCppuType(&e).getTypeLibType());
162 raisePyExceptionWithAny( com::sun::star::uno::makeAny( e ) );
164 catch( const com::sun::star::lang::IllegalArgumentException &e )
166 if( isLog( cargo, LogLevel::CALL ) )
168 logException( cargo, "error py->uno[0x", me->members->xInvocation.get() ,
169 me->members->methodName, &e, getCppuType(&e).getTypeLibType());
171 raisePyExceptionWithAny( com::sun::star::uno::makeAny( e ) );
173 catch (const ::com::sun::star::uno::RuntimeException &e)
175 if( cargo && isLog( cargo, LogLevel::CALL ) )
177 logException( cargo, "error py->uno[0x", me->members->xInvocation.get() ,
178 me->members->methodName, &e, getCppuType(&e).getTypeLibType());
180 raisePyExceptionWithAny( com::sun::star::uno::makeAny( e ) );
183 return ret.getAcquired();
187 static PyTypeObject PyUNO_callable_Type =
189 PyVarObject_HEAD_INIT( &PyType_Type, 0 )
190 "PyUNO_callable",
191 sizeof (PyUNO_callable),
193 (destructor) ::pyuno::PyUNO_callable_del,
194 (printfunc) 0,
195 (getattrfunc) 0,
196 (setattrfunc) 0,
198 (reprfunc) 0,
202 (hashfunc) 0,
203 (ternaryfunc) ::pyuno::PyUNO_callable_call,
204 (reprfunc) 0,
205 (getattrofunc)0,
206 (setattrofunc)0,
207 NULL,
209 NULL,
210 (traverseproc)0,
211 (inquiry)0,
212 (richcmpfunc)0,
214 (getiterfunc)0,
215 (iternextfunc)0,
216 NULL,
217 NULL,
218 NULL,
219 NULL,
220 NULL,
221 (descrgetfunc)0,
222 (descrsetfunc)0,
224 (initproc)0,
225 (allocfunc)0,
226 (newfunc)0,
227 (freefunc)0,
228 (inquiry)0,
229 NULL,
230 NULL,
231 NULL,
232 NULL,
233 NULL,
234 (destructor)0
235 #if PY_VERSION_HEX >= 0x02060000
237 #endif
240 PyRef PyUNO_callable_new (
241 const Reference<XInvocation2> &my_inv,
242 const OUString & methodName,
243 const Reference<XSingleServiceFactory> &xInvocationFactory,
244 const Reference<XTypeConverter> &tc,
245 enum ConversionMode mode )
247 PyUNO_callable* self;
249 OSL_ENSURE (my_inv.is(), "XInvocation must be valid");
251 self = PyObject_New (PyUNO_callable, &PyUNO_callable_Type);
252 if (self == NULL)
253 return NULL; //NULL == Error!
255 self->members = new PyUNO_callable_Internals;
256 self->members->xInvocation = my_inv;
257 self->members->methodName = methodName;
258 self->members->xInvocationFactory = xInvocationFactory;
259 self->members->xTypeConverter = tc;
260 self->members->mode = mode;
262 return PyRef( (PyObject*)self, SAL_NO_ACQUIRE );
267 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */