Version 4.2.0.1, tag libreoffice-4.2.0.1
[LibreOffice.git] / pyuno / source / module / pyuno_callable.cxx
blob79ee6ffb862db7042b86af4899208cd668dfc2f2
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 OUString methodName;
42 ConversionMode mode;
43 } PyUNO_callable_Internals;
45 typedef struct
47 PyObject_HEAD
48 PyUNO_callable_Internals* members;
49 } PyUNO_callable;
51 void PyUNO_callable_del (PyObject* self)
53 PyUNO_callable* me;
55 me = (PyUNO_callable*) self;
56 delete me->members;
57 PyObject_Del (self);
59 return;
62 PyObject* PyUNO_callable_call(
63 PyObject* self, PyObject* args, SAL_UNUSED_PARAMETER PyObject*)
65 PyUNO_callable* me;
67 Sequence<short> aOutParamIndex;
68 Sequence<Any> aOutParam;
69 Sequence<Any> aParams;
70 Any any_params;
71 Any ret_value;
72 RuntimeCargo *cargo = 0;
73 me = (PyUNO_callable*) self;
75 PyRef ret;
76 try
78 Runtime runtime;
79 cargo = runtime.getImpl()->cargo;
80 any_params = runtime.pyObject2Any (args, me->members->mode);
82 if (any_params.getValueTypeClass () == com::sun::star::uno::TypeClass_SEQUENCE)
84 any_params >>= aParams;
86 else
88 aParams.realloc (1);
89 aParams [0] <<= any_params;
93 PyThreadDetach antiguard; //pyhton free zone
95 // do some logging if desired ...
96 if( isLog( cargo, LogLevel::CALL ) )
98 logCall( cargo, "try py->uno[0x", me->members->xInvocation.get(),
99 me->members->methodName, aParams );
102 // do the call
103 ret_value = me->members->xInvocation->invoke (
104 me->members->methodName, aParams, aOutParamIndex, aOutParam);
106 // log the reply, if desired
107 if( isLog( cargo, LogLevel::CALL ) )
109 logReply( cargo, "success py->uno[0x", me->members->xInvocation.get(),
110 me->members->methodName, ret_value, aOutParam);
115 PyRef temp = runtime.any2PyObject (ret_value);
116 if( aOutParam.getLength() )
118 PyRef return_list( PyTuple_New (1+aOutParam.getLength()), SAL_NO_ACQUIRE );
119 PyTuple_SetItem (return_list.get(), 0, temp.getAcquired());
121 // initialize with defaults in case of exceptions
122 int i;
123 for( i = 1 ; i < 1+aOutParam.getLength() ; i ++ )
125 Py_INCREF( Py_None );
126 PyTuple_SetItem( return_list.get() , i , Py_None );
129 for( i = 0 ; i < aOutParam.getLength() ; i ++ )
131 PyRef ref = runtime.any2PyObject( aOutParam[i] );
132 PyTuple_SetItem (return_list.get(), 1+i, ref.getAcquired());
134 ret = return_list;
136 else
138 ret = temp;
141 catch( const com::sun::star::reflection::InvocationTargetException & e )
144 if( isLog( cargo, LogLevel::CALL ) )
146 logException( cargo, "except py->uno[0x", me->members->xInvocation.get() ,
147 me->members->methodName, e.TargetException.getValue(), e.TargetException.getValueTypeRef());
149 raisePyExceptionWithAny( e.TargetException );
151 catch( const com::sun::star::script::CannotConvertException &e )
153 if( isLog( cargo, LogLevel::CALL ) )
155 logException( cargo, "error py->uno[0x", me->members->xInvocation.get() ,
156 me->members->methodName, &e, getCppuType(&e).getTypeLibType());
158 raisePyExceptionWithAny( com::sun::star::uno::makeAny( e ) );
160 catch( const com::sun::star::lang::IllegalArgumentException &e )
162 if( isLog( cargo, LogLevel::CALL ) )
164 logException( cargo, "error py->uno[0x", me->members->xInvocation.get() ,
165 me->members->methodName, &e, getCppuType(&e).getTypeLibType());
167 raisePyExceptionWithAny( com::sun::star::uno::makeAny( e ) );
169 catch (const ::com::sun::star::uno::RuntimeException &e)
171 if( cargo && isLog( cargo, LogLevel::CALL ) )
173 logException( cargo, "error py->uno[0x", me->members->xInvocation.get() ,
174 me->members->methodName, &e, getCppuType(&e).getTypeLibType());
176 raisePyExceptionWithAny( com::sun::star::uno::makeAny( e ) );
179 return ret.getAcquired();
183 static PyTypeObject PyUNO_callable_Type =
185 PyVarObject_HEAD_INIT( &PyType_Type, 0 )
186 "PyUNO_callable",
187 sizeof (PyUNO_callable),
189 (destructor) ::pyuno::PyUNO_callable_del,
190 (printfunc) 0,
191 (getattrfunc) 0,
192 (setattrfunc) 0,
194 (reprfunc) 0,
198 (hashfunc) 0,
199 (ternaryfunc) ::pyuno::PyUNO_callable_call,
200 (reprfunc) 0,
201 (getattrofunc)0,
202 (setattrofunc)0,
203 NULL,
205 NULL,
206 (traverseproc)0,
207 (inquiry)0,
208 (richcmpfunc)0,
210 (getiterfunc)0,
211 (iternextfunc)0,
212 NULL,
213 NULL,
214 NULL,
215 NULL,
216 NULL,
217 (descrgetfunc)0,
218 (descrsetfunc)0,
220 (initproc)0,
221 (allocfunc)0,
222 (newfunc)0,
223 (freefunc)0,
224 (inquiry)0,
225 NULL,
226 NULL,
227 NULL,
228 NULL,
229 NULL,
230 (destructor)0
231 #if PY_VERSION_HEX >= 0x02060000
233 #endif
236 PyRef PyUNO_callable_new (
237 const Reference<XInvocation2> &my_inv,
238 const OUString & methodName,
239 enum ConversionMode mode )
241 PyUNO_callable* self;
243 OSL_ENSURE (my_inv.is(), "XInvocation must be valid");
245 self = PyObject_New (PyUNO_callable, &PyUNO_callable_Type);
246 if (self == NULL)
247 return NULL; //NULL == Error!
249 self->members = new PyUNO_callable_Internals;
250 self->members->xInvocation = my_inv;
251 self->members->methodName = methodName;
252 self->members->mode = mode;
254 return PyRef( (PyObject*)self, SAL_NO_ACQUIRE );
259 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */