1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
;
34 struct PyUNO_callable_Internals
36 Reference
<XInvocation2
> xInvocation
;
44 PyUNO_callable_Internals
* members
;
47 static void PyUNO_callable_del (PyObject
* self
)
51 me
= reinterpret_cast<PyUNO_callable
*>(self
);
56 static PyObject
* PyUNO_callable_call(
57 PyObject
* self
, PyObject
* args
, SAL_UNUSED_PARAMETER PyObject
*)
61 Sequence
<short> aOutParamIndex
;
62 Sequence
<Any
> aOutParam
;
63 Sequence
<Any
> aParams
;
66 RuntimeCargo
*cargo
= nullptr;
67 me
= reinterpret_cast<PyUNO_callable
*>(self
);
73 cargo
= runtime
.getImpl()->cargo
;
74 any_params
= runtime
.pyObject2Any (args
, me
->members
->mode
);
76 if (any_params
.getValueTypeClass () == css::uno::TypeClass_SEQUENCE
)
78 any_params
>>= aParams
;
83 aParams
[0] = any_params
;
87 PyThreadDetach antiguard
; //python free zone
89 // do some logging if desired ...
90 if( isLog( cargo
, LogLevel::CALL
) )
92 logCall( cargo
, "try py->uno[0x", me
->members
->xInvocation
.get(),
93 me
->members
->methodName
, aParams
);
97 ret_value
= me
->members
->xInvocation
->invoke (
98 me
->members
->methodName
, aParams
, aOutParamIndex
, aOutParam
);
100 // log the reply, if desired
101 if( isLog( cargo
, LogLevel::CALL
) )
103 logReply( cargo
, "success py->uno[0x", me
->members
->xInvocation
.get(),
104 me
->members
->methodName
, ret_value
, aOutParam
);
109 PyRef temp
= runtime
.any2PyObject (ret_value
);
110 if( aOutParam
.getLength() )
112 PyRef
return_list( PyTuple_New (1+aOutParam
.getLength()), SAL_NO_ACQUIRE
, NOT_NULL
);
113 PyTuple_SetItem (return_list
.get(), 0, temp
.getAcquired());
115 // initialize with defaults in case of exceptions
117 for( i
= 1 ; i
< 1+aOutParam
.getLength() ; i
++ )
119 Py_INCREF( Py_None
);
120 PyTuple_SetItem( return_list
.get() , i
, Py_None
);
123 for( i
= 0 ; i
< aOutParam
.getLength() ; i
++ )
125 PyRef ref
= runtime
.any2PyObject( aOutParam
[i
] );
126 PyTuple_SetItem (return_list
.get(), 1+i
, ref
.getAcquired());
135 catch( const css::reflection::InvocationTargetException
& e
)
138 if( isLog( cargo
, LogLevel::CALL
) )
140 logException( cargo
, "except py->uno[0x", me
->members
->xInvocation
.get() ,
141 me
->members
->methodName
, e
.TargetException
.getValue(), e
.TargetException
.getValueTypeRef());
143 raisePyExceptionWithAny( e
.TargetException
);
145 catch( const css::script::CannotConvertException
&e
)
147 if( isLog( cargo
, LogLevel::CALL
) )
149 logException( cargo
, "error py->uno[0x", me
->members
->xInvocation
.get() ,
150 me
->members
->methodName
, &e
, cppu::UnoType
<decltype(e
)>::get().getTypeLibType());
152 raisePyExceptionWithAny( css::uno::makeAny( e
) );
154 catch( const css::lang::IllegalArgumentException
&e
)
156 if( isLog( cargo
, LogLevel::CALL
) )
158 logException( cargo
, "error py->uno[0x", me
->members
->xInvocation
.get() ,
159 me
->members
->methodName
, &e
, cppu::UnoType
<decltype(e
)>::get().getTypeLibType());
161 raisePyExceptionWithAny( css::uno::makeAny( e
) );
163 catch (const css::uno::RuntimeException
&e
)
165 if( cargo
&& isLog( cargo
, LogLevel::CALL
) )
167 logException( cargo
, "error py->uno[0x", me
->members
->xInvocation
.get() ,
168 me
->members
->methodName
, &e
, cppu::UnoType
<decltype(e
)>::get().getTypeLibType());
170 raisePyExceptionWithAny( css::uno::makeAny( e
) );
173 return ret
.getAcquired();
177 static PyTypeObject PyUNO_callable_Type
=
179 PyVarObject_HEAD_INIT( &PyType_Type
, 0 )
181 sizeof (PyUNO_callable
),
183 ::pyuno::PyUNO_callable_del
,
184 #if PY_VERSION_HEX >= 0x03080000
185 0, // Py_ssize_t tp_vectorcall_offset
187 nullptr, // printfunc tp_print
197 ::pyuno::PyUNO_callable_call
,
230 #if PY_VERSION_HEX >= 0x03040000
232 #if PY_VERSION_HEX >= 0x03080000
233 , nullptr // vectorcallfunc tp_vectorcall
238 PyRef
PyUNO_callable_new (
239 const Reference
<XInvocation2
> &my_inv
,
240 const OUString
& methodName
,
241 enum ConversionMode mode
)
243 PyUNO_callable
* self
;
245 OSL_ENSURE (my_inv
.is(), "XInvocation must be valid");
247 self
= PyObject_New (PyUNO_callable
, &PyUNO_callable_Type
);
249 return nullptr; //NULL == Error!
251 self
->members
= new PyUNO_callable_Internals
;
252 self
->members
->xInvocation
= my_inv
;
253 self
->members
->methodName
= methodName
;
254 self
->members
->mode
= mode
;
256 return PyRef( reinterpret_cast<PyObject
*>(self
), SAL_NO_ACQUIRE
);
261 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */