nss: upgrade to release 3.73
[LibreOffice.git] / pyuno / source / module / pyuno_callable.cxx
blob1c138f71b419ca68873af60c0157080bc2ca53e6
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/diagnose.h>
23 #include <com/sun/star/script/CannotConvertException.hpp>
24 #include <com/sun/star/script/XInvocation2.hpp>
26 using com::sun::star::uno::Sequence;
27 using com::sun::star::uno::Reference;
28 using com::sun::star::uno::Any;
29 using com::sun::star::uno::RuntimeException;
30 using com::sun::star::script::XInvocation2;
32 namespace pyuno
34 namespace {
36 struct PyUNO_callable_Internals
38 Reference<XInvocation2> xInvocation;
39 OUString methodName;
40 ConversionMode mode;
43 struct PyUNO_callable
45 PyObject_HEAD
46 PyUNO_callable_Internals* members;
51 static void PyUNO_callable_del (PyObject* self)
53 PyUNO_callable* me;
55 me = reinterpret_cast<PyUNO_callable*>(self);
56 delete me->members;
57 PyObject_Del (self);
60 static PyObject* PyUNO_callable_call(
61 PyObject* self, PyObject* args, SAL_UNUSED_PARAMETER PyObject*)
63 PyUNO_callable* me;
65 Sequence<short> aOutParamIndex;
66 Sequence<Any> aOutParam;
67 Sequence<Any> aParams;
68 Any any_params;
69 Any ret_value;
70 RuntimeCargo *cargo = nullptr;
71 me = reinterpret_cast<PyUNO_callable*>(self);
73 PyRef ret;
74 try
76 Runtime runtime;
77 cargo = runtime.getImpl()->cargo;
78 any_params = runtime.pyObject2Any (args, me->members->mode);
80 if (any_params.getValueTypeClass () == css::uno::TypeClass_SEQUENCE)
82 any_params >>= aParams;
84 else
86 aParams.realloc (1);
87 aParams [0] = any_params;
91 PyThreadDetach antiguard; //python free zone
93 // do some logging if desired ...
94 if( isLog( cargo, LogLevel::CALL ) )
96 logCall( cargo, "try py->uno[0x", me->members->xInvocation.get(),
97 me->members->methodName, aParams );
100 // do the call
101 ret_value = me->members->xInvocation->invoke (
102 me->members->methodName, aParams, aOutParamIndex, aOutParam);
104 // log the reply, if desired
105 if( isLog( cargo, LogLevel::CALL ) )
107 logReply( cargo, "success py->uno[0x", me->members->xInvocation.get(),
108 me->members->methodName, ret_value, aOutParam);
113 PyRef temp = runtime.any2PyObject (ret_value);
114 if( aOutParam.getLength() )
116 PyRef return_list( PyTuple_New (1+aOutParam.getLength()), SAL_NO_ACQUIRE, NOT_NULL );
117 PyTuple_SetItem (return_list.get(), 0, temp.getAcquired());
119 // initialize with defaults in case of exceptions
120 int i;
121 for( i = 1 ; i < 1+aOutParam.getLength() ; i ++ )
123 Py_INCREF( Py_None );
124 PyTuple_SetItem( return_list.get() , i , Py_None );
127 for( i = 0 ; i < aOutParam.getLength() ; i ++ )
129 PyRef ref = runtime.any2PyObject( aOutParam[i] );
130 PyTuple_SetItem (return_list.get(), 1+i, ref.getAcquired());
132 ret = return_list;
134 else
136 ret = temp;
139 catch( const css::reflection::InvocationTargetException & e )
142 if( isLog( cargo, LogLevel::CALL ) )
144 logException( cargo, "except py->uno[0x", me->members->xInvocation.get() ,
145 me->members->methodName, e.TargetException.getValue(), e.TargetException.getValueTypeRef());
147 raisePyExceptionWithAny( e.TargetException );
149 catch( const css::script::CannotConvertException &e )
151 if( isLog( cargo, LogLevel::CALL ) )
153 logException( cargo, "error py->uno[0x", me->members->xInvocation.get() ,
154 me->members->methodName, &e, cppu::UnoType<decltype(e)>::get().getTypeLibType());
156 raisePyExceptionWithAny( css::uno::makeAny( e ) );
158 catch( const css::lang::IllegalArgumentException &e )
160 if( isLog( cargo, LogLevel::CALL ) )
162 logException( cargo, "error py->uno[0x", me->members->xInvocation.get() ,
163 me->members->methodName, &e, cppu::UnoType<decltype(e)>::get().getTypeLibType());
165 raisePyExceptionWithAny( css::uno::makeAny( e ) );
167 catch (const css::uno::RuntimeException &e)
169 if( cargo && isLog( cargo, LogLevel::CALL ) )
171 logException( cargo, "error py->uno[0x", me->members->xInvocation.get() ,
172 me->members->methodName, &e, cppu::UnoType<decltype(e)>::get().getTypeLibType());
174 raisePyExceptionWithAny( css::uno::makeAny( e ) );
177 return ret.getAcquired();
181 static PyTypeObject PyUNO_callable_Type =
183 PyVarObject_HEAD_INIT( &PyType_Type, 0 )
184 "PyUNO_callable",
185 sizeof (PyUNO_callable),
187 ::pyuno::PyUNO_callable_del,
188 #if PY_VERSION_HEX >= 0x03080000
189 0, // Py_ssize_t tp_vectorcall_offset
190 #else
191 nullptr, // printfunc tp_print
192 #endif
193 nullptr,
194 nullptr,
195 nullptr,
196 nullptr,
197 nullptr,
198 nullptr,
199 nullptr,
200 nullptr,
201 ::pyuno::PyUNO_callable_call,
202 nullptr,
203 nullptr,
204 nullptr,
205 nullptr,
207 nullptr,
208 nullptr,
209 nullptr,
210 nullptr,
212 nullptr,
213 nullptr,
214 nullptr,
215 nullptr,
216 nullptr,
217 nullptr,
218 nullptr,
219 nullptr,
220 nullptr,
222 nullptr,
223 nullptr,
224 nullptr,
225 nullptr,
226 nullptr,
227 nullptr,
228 nullptr,
229 nullptr,
230 nullptr,
231 nullptr,
232 nullptr
234 #if PY_VERSION_HEX >= 0x03040000
235 , nullptr
236 #if PY_VERSION_HEX >= 0x03080000
237 , nullptr // vectorcallfunc tp_vectorcall
238 #if PY_VERSION_HEX < 0x03090000
239 #if defined __clang__
240 #pragma clang diagnostic push
241 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
242 #endif
243 , nullptr // tp_print
244 #if defined __clang__
245 #pragma clang diagnostic pop
246 #endif
247 #endif
248 #endif
249 #endif
252 PyRef PyUNO_callable_new (
253 const Reference<XInvocation2> &my_inv,
254 const OUString & methodName,
255 enum ConversionMode mode )
257 PyUNO_callable* self;
259 OSL_ENSURE (my_inv.is(), "XInvocation must be valid");
261 self = PyObject_New (PyUNO_callable, &PyUNO_callable_Type);
262 if (self == nullptr)
263 return nullptr; //NULL == Error!
265 self->members = new PyUNO_callable_Internals;
266 self->members->xInvocation = my_inv;
267 self->members->methodName = methodName;
268 self->members->mode = mode;
270 return PyRef( reinterpret_cast<PyObject*>(self), SAL_NO_ACQUIRE );
275 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */