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>
22 #include <osl/thread.h>
23 #include <rtl/ustrbuf.hxx>
25 using com::sun::star::uno::Sequence
;
26 using com::sun::star::uno::Reference
;
27 using com::sun::star::uno::XInterface
;
28 using com::sun::star::uno::Any
;
29 using com::sun::star::uno::Type
;
30 using com::sun::star::uno::TypeClass
;
31 using com::sun::star::uno::RuntimeException
;
32 using com::sun::star::uno::XComponentContext
;
33 using com::sun::star::lang::XSingleServiceFactory
;
34 using com::sun::star::script::XTypeConverter
;
35 using com::sun::star::script::XInvocation2
;
41 Reference
<XInvocation2
> xInvocation
;
44 } PyUNO_callable_Internals
;
49 PyUNO_callable_Internals
* members
;
52 void PyUNO_callable_del (PyObject
* self
)
56 me
= reinterpret_cast<PyUNO_callable
*>(self
);
63 PyObject
* PyUNO_callable_call(
64 PyObject
* self
, PyObject
* args
, SAL_UNUSED_PARAMETER PyObject
*)
68 Sequence
<short> aOutParamIndex
;
69 Sequence
<Any
> aOutParam
;
70 Sequence
<Any
> aParams
;
73 RuntimeCargo
*cargo
= 0;
74 me
= reinterpret_cast<PyUNO_callable
*>(self
);
80 cargo
= runtime
.getImpl()->cargo
;
81 any_params
= runtime
.pyObject2Any (args
, me
->members
->mode
);
83 if (any_params
.getValueTypeClass () == com::sun::star::uno::TypeClass_SEQUENCE
)
85 any_params
>>= aParams
;
90 aParams
[0] <<= any_params
;
94 PyThreadDetach antiguard
; //pyhton free zone
96 // do some logging if desired ...
97 if( isLog( cargo
, LogLevel::CALL
) )
99 logCall( cargo
, "try py->uno[0x", me
->members
->xInvocation
.get(),
100 me
->members
->methodName
, aParams
);
104 ret_value
= me
->members
->xInvocation
->invoke (
105 me
->members
->methodName
, aParams
, aOutParamIndex
, aOutParam
);
107 // log the reply, if desired
108 if( isLog( cargo
, LogLevel::CALL
) )
110 logReply( cargo
, "success py->uno[0x", me
->members
->xInvocation
.get(),
111 me
->members
->methodName
, ret_value
, aOutParam
);
116 PyRef temp
= runtime
.any2PyObject (ret_value
);
117 if( aOutParam
.getLength() )
119 PyRef
return_list( PyTuple_New (1+aOutParam
.getLength()), SAL_NO_ACQUIRE
, NOT_NULL
);
120 PyTuple_SetItem (return_list
.get(), 0, temp
.getAcquired());
122 // initialize with defaults in case of exceptions
124 for( i
= 1 ; i
< 1+aOutParam
.getLength() ; i
++ )
126 Py_INCREF( Py_None
);
127 PyTuple_SetItem( return_list
.get() , i
, Py_None
);
130 for( i
= 0 ; i
< aOutParam
.getLength() ; i
++ )
132 PyRef ref
= runtime
.any2PyObject( aOutParam
[i
] );
133 PyTuple_SetItem (return_list
.get(), 1+i
, ref
.getAcquired());
142 catch( const com::sun::star::reflection::InvocationTargetException
& e
)
145 if( isLog( cargo
, LogLevel::CALL
) )
147 logException( cargo
, "except py->uno[0x", me
->members
->xInvocation
.get() ,
148 me
->members
->methodName
, e
.TargetException
.getValue(), e
.TargetException
.getValueTypeRef());
150 raisePyExceptionWithAny( e
.TargetException
);
152 catch( const com::sun::star::script::CannotConvertException
&e
)
154 if( isLog( cargo
, LogLevel::CALL
) )
156 logException( cargo
, "error py->uno[0x", me
->members
->xInvocation
.get() ,
157 me
->members
->methodName
, &e
, cppu::UnoType
<decltype(e
)>::get().getTypeLibType());
159 raisePyExceptionWithAny( com::sun::star::uno::makeAny( e
) );
161 catch( const com::sun::star::lang::IllegalArgumentException
&e
)
163 if( isLog( cargo
, LogLevel::CALL
) )
165 logException( cargo
, "error py->uno[0x", me
->members
->xInvocation
.get() ,
166 me
->members
->methodName
, &e
, cppu::UnoType
<decltype(e
)>::get().getTypeLibType());
168 raisePyExceptionWithAny( com::sun::star::uno::makeAny( e
) );
170 catch (const ::com::sun::star::uno::RuntimeException
&e
)
172 if( cargo
&& isLog( cargo
, LogLevel::CALL
) )
174 logException( cargo
, "error py->uno[0x", me
->members
->xInvocation
.get() ,
175 me
->members
->methodName
, &e
, cppu::UnoType
<decltype(e
)>::get().getTypeLibType());
177 raisePyExceptionWithAny( com::sun::star::uno::makeAny( e
) );
180 return ret
.getAcquired();
184 static PyTypeObject PyUNO_callable_Type
=
186 PyVarObject_HEAD_INIT( &PyType_Type
, 0 )
188 sizeof (PyUNO_callable
),
190 (destructor
) ::pyuno::PyUNO_callable_del
,
200 (ternaryfunc
) ::pyuno::PyUNO_callable_call
,
232 #if PY_VERSION_HEX >= 0x02060000
235 #if PY_VERSION_HEX >= 0x03040000
240 PyRef
PyUNO_callable_new (
241 const Reference
<XInvocation2
> &my_inv
,
242 const OUString
& methodName
,
243 enum ConversionMode mode
)
245 PyUNO_callable
* self
;
247 OSL_ENSURE (my_inv
.is(), "XInvocation must be valid");
249 self
= PyObject_New (PyUNO_callable
, &PyUNO_callable_Type
);
251 return NULL
; //NULL == Error!
253 self
->members
= new PyUNO_callable_Internals
;
254 self
->members
->xInvocation
= my_inv
;
255 self
->members
->methodName
= methodName
;
256 self
->members
->mode
= mode
;
258 return PyRef( reinterpret_cast<PyObject
*>(self
), SAL_NO_ACQUIRE
);
263 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */