23#include <com/sun/star/beans/MethodConcept.hpp>
24#include <com/sun/star/beans/UnknownPropertyException.hpp>
25#include <com/sun/star/script/CannotConvertException.hpp>
26#include <com/sun/star/script/XInvocationAdapterFactory2.hpp>
27#include <com/sun/star/beans/XIntrospection.hpp>
35using com::sun::star::beans::XIntrospectionAccess;
36using com::sun::star::uno::Any;
39using com::sun::star::uno::RuntimeException;
40using com::sun::star::uno::XInterface;
41using com::sun::star::uno::Type;
42using com::sun::star::lang::XUnoTunnel;
43using com::sun::star::lang::IllegalArgumentException;
44using com::sun::star::beans::UnknownPropertyException;
45using com::sun::star::script::CannotConvertException;
46using com::sun::star::reflection::InvocationTargetException;
47using com::sun::star::reflection::XIdlMethod;
48using com::sun::star::reflection::ParamInfo;
50#define TO_ASCII(x) OUStringToOString( x , RTL_TEXTENCODING_ASCII_US).getStr()
56 : mWrappedObject(
std::move( ref )),
57 mInterpreter( (PyThreadState_Get()->interp) ),
82 if( !Py_IsInitialized() )
83 throw InvocationTargetException();
85 if( PyErr_Occurred() )
87 PyRef excType, excValue, excTraceback;
88 PyErr_Fetch(
reinterpret_cast<PyObject **
>(&excType),
reinterpret_cast<PyObject**
>(&excValue),
reinterpret_cast<PyObject**
>(&excTraceback));
90 throw InvocationTargetException(
91 o3tl::doAccess<css::uno::Exception>(unoExc)->Message,
124 if( !introspection.is() )
127 "pyuno bridge: Couldn't inspect uno adapter ( the python class must implement com.sun.star.lang.XTypeProvider !)" );
131 functionName, css::beans::MethodConcept::ALL );
135 "pyuno bridge: Couldn't get reflection for method " + functionName );
139 std::vector<sal_Int16> retVec;
140 for( sal_Int32
i = 0;
i < seqInfo.getLength(); ++
i )
142 if( seqInfo[
i].aMode == css::reflection::ParamMode_OUT ||
143 seqInfo[
i].aMode == css::reflection::ParamMode_INOUT )
145 retVec.push_back(
static_cast<sal_Int16
>(
i));
170 if( aParams.getLength() == 1 && aFunctionName ==
"getSomething" )
173 if( aParams[0] >>=
id )
183 if( !Py_IsInitialized() )
184 throw InvocationTargetException();
192 logCall( cargo,
"try uno->py[0x",
196 sal_Int32
size = aParams.getLength();
202 Py_INCREF( Py_None );
203 PyTuple_SetItem( argsTuple.
get(),
i, Py_None );
212 if( !Py_IsInitialized() )
213 throw InvocationTargetException();
227 OUString sMsg =
"pyuno::Adapter: Method "
229 +
" is not implemented at object "
234 PyRef pyRet( PyObject_CallObject( method.
get(), argsTuple.
get() ), SAL_NO_ACQUIRE );
240 if( ret.hasValue() &&
241 ret.getValueTypeClass() == css::uno::TypeClass_SEQUENCE &&
242 aFunctionName !=
"getTypes" &&
243 aFunctionName !=
"getImplementationId" )
252 if( aOutParamIndex.hasElements() )
256 if( ! ( ret >>= seq ) )
259 "pyuno bridge: Couldn't extract out parameters for method " + aFunctionName );
262 auto nOutLength = aOutParamIndex.getLength();
263 if( nOutLength + 1 != seq.getLength() )
265 OUString sMsg =
"pyuno bridge: expected for method "
267 +
" one return value and "
268 + OUString::number(nOutLength)
269 +
" out parameters, got a sequence of "
270 + OUString::number(seq.getLength())
271 +
" elements as return value.";
275 aOutParam.realloc( nOutLength );
276 ret = std::as_const(seq)[0];
277 std::copy_n(std::next(std::cbegin(seq)), nOutLength, aOutParam.getArray());
286 logReply( cargo,
"success uno->py[0x" ,
292 catch(
const InvocationTargetException & e )
297 cargo,
"except uno->py[0x" ,
299 e.TargetException.getValue(),e.TargetException.getValueType() );
303 catch(
const IllegalArgumentException & e )
308 cargo,
"except uno->py[0x" ,
318 cargo,
"except uno->py[0x" ,
323 catch(
const CannotConvertException & e )
328 cargo,
"except uno->py[0x" ,
340 throw UnknownPropertyException(
"pyuno::Adapter: Property " + aPropertyName +
" is unknown." );
346 if( !Py_IsInitialized() )
347 throw InvocationTargetException();
353 if( !Py_IsInitialized() )
354 throw InvocationTargetException();
356 PyObject_SetAttrString(
361 catch(
const IllegalArgumentException & exc )
364 throw InvocationTargetException( exc.Message, *
this, anyEx );
374 if( !Py_IsInitialized() )
382 if (!pyRef.
is() || PyErr_Occurred())
384 throw UnknownPropertyException(
"pyuno::Adapter: Property " + aPropertyName +
" is unknown." );
402 if( !Py_IsInitialized() )
405 bRet = PyObject_HasAttrString(
const css::uno::Sequence< sal_Int8 > & getSeq() const
virtual css::uno::Reference< css::beans::XIntrospectionAccess > SAL_CALL getIntrospection() override
Adapter(PyRef obj, const css::uno::Sequence< css::uno::Type > &types)
MethodOutIndexMap m_methodOutIndexMap
virtual css::uno::Any SAL_CALL getValue(const OUString &aPropertyName) override
virtual ~Adapter() override
virtual void SAL_CALL setValue(const OUString &aPropertyName, const css::uno::Any &aValue) override
virtual sal_Bool SAL_CALL hasProperty(const OUString &aName) override
css::uno::Sequence< sal_Int16 > getOutIndexes(const OUString &functionName)
virtual css::uno::Any SAL_CALL invoke(const OUString &aFunctionName, const css::uno::Sequence< css::uno::Any > &aParams, css::uno::Sequence< sal_Int16 > &aOutParamIndex, css::uno::Sequence< css::uno::Any > &aOutParam) override
virtual sal_Bool SAL_CALL hasMethod(const OUString &aName) override
PyInterpreterState * mInterpreter
static const css::uno::Sequence< sal_Int8 > & getUnoTunnelId()
css::uno::Sequence< css::uno::Type > mTypes
virtual sal_Int64 SAL_CALL getSomething(const css::uno::Sequence< sal_Int8 > &aIdentifier) override
Helper class for keeping references to python objects.
void scratch() noexcept
clears the reference without decreasing the reference count only seldom needed !
bool is() const
returns 1 when the reference points to a python object python object, otherwise 0.
PyObject * get() const noexcept
PyObject * getAcquired() const
helper class for attaching the current thread to the python runtime.
helper class for detaching the current thread from the python runtime to do some blocking,...
The pyuno::Runtime class keeps the internal state of the python UNO bridge for the currently in use p...
css::uno::Any extractUnoException(const PyRef &excType, const PyRef &excValue, const PyRef &excTraceback) const
extracts a proper uno exception from a given python exception
css::uno::Any pyObject2Any(const PyRef &source, enum ConversionMode mode=REJECT_UNO_ANY) const
converts a Python object to a UNO any
PyRef any2PyObject(const css::uno::Any &source) const
converts something contained in a UNO Any to a Python object
RuntimeImpl * getImpl() const
Returns the internal handle.
sal_Int64 getSomethingImpl(const css::uno::Sequence< sal_Int8 > &rId, T *pThis, FallbackToGetSomethingOf< Base >={})
css::uno::Sequence< DstElementType > containerToSequence(const SrcType &i_Container)
Any SAL_CALL getCaughtException()
void raiseInvocationTargetExceptionWhenNeeded(const Runtime &runtime)
bool isLog(RuntimeCargo const *cargo, sal_Int32 loglevel)
void logCall(RuntimeCargo *cargo, const char *intro, void *ptr, std::u16string_view aFunctionName, const css::uno::Sequence< css::uno::Any > &args)
void decreaseRefCount(PyInterpreterState *interpreter, PyObject *object)
releases a refcount on the interpreter object and on another given python object.
static StaticDestructorGuard guard
void logException(RuntimeCargo *cargo, const char *intro, void *ptr, std::u16string_view aFunctionName, const void *data, const css::uno::Type &type)
OUString pyString2ustring(PyObject *str)
@ NOT_NULL
definition of a no acquire enum for ctors
void logReply(RuntimeCargo *cargo, const char *intro, void *ptr, std::u16string_view aFunctionName, const css::uno::Any &returnValue, const css::uno::Sequence< css::uno::Any > &args)
css::uno::Reference< css::beans::XIntrospection > xIntrospection
css::uno::Reference< css::script::XInvocationAdapterFactory2 > xAdapterFactory
PyObject_HEAD struct RuntimeCargo * cargo