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
;
36 struct PyUNO_callable_Internals
38 Reference
<XInvocation2
> xInvocation
;
46 PyUNO_callable_Internals
* members
;
51 static void PyUNO_callable_del (PyObject
* self
)
55 me
= reinterpret_cast<PyUNO_callable
*>(self
);
60 static PyObject
* PyUNO_callable_call(
61 PyObject
* self
, PyObject
* args
, SAL_UNUSED_PARAMETER PyObject
*)
65 Sequence
<short> aOutParamIndex
;
66 Sequence
<Any
> aOutParam
;
67 Sequence
<Any
> aParams
;
70 RuntimeCargo
*cargo
= nullptr;
71 me
= reinterpret_cast<PyUNO_callable
*>(self
);
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
;
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
);
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
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());
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 )
185 sizeof (PyUNO_callable
),
187 ::pyuno::PyUNO_callable_del
,
188 #if PY_VERSION_HEX >= 0x03080000
189 0, // Py_ssize_t tp_vectorcall_offset
191 nullptr, // printfunc tp_print
201 ::pyuno::PyUNO_callable_call
,
234 #if PY_VERSION_HEX >= 0x03040000
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"
243 , nullptr // tp_print
244 #if defined __clang__
245 #pragma clang diagnostic pop
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
);
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: */