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/thread.h>
22 #include <rtl/ustrbuf.hxx>
24 using com::sun::star::uno::Sequence
;
25 using com::sun::star::uno::Reference
;
26 using com::sun::star::uno::XInterface
;
27 using com::sun::star::uno::Any
;
28 using com::sun::star::uno::Type
;
29 using com::sun::star::uno::TypeClass
;
30 using com::sun::star::uno::RuntimeException
;
31 using com::sun::star::uno::XComponentContext
;
32 using com::sun::star::lang::XSingleServiceFactory
;
33 using com::sun::star::script::XTypeConverter
;
34 using com::sun::star::script::XInvocation2
;
40 Reference
<XInvocation2
> xInvocation
;
41 Reference
<XSingleServiceFactory
> xInvocationFactory
;
42 Reference
<XTypeConverter
> xTypeConverter
;
45 } PyUNO_callable_Internals
;
50 PyUNO_callable_Internals
* members
;
53 void PyUNO_callable_del (PyObject
* self
)
57 me
= (PyUNO_callable
*) self
;
64 PyObject
* PyUNO_callable_call(
65 PyObject
* self
, PyObject
* args
, SAL_UNUSED_PARAMETER PyObject
*)
69 Sequence
<short> aOutParamIndex
;
70 Sequence
<Any
> aOutParam
;
71 Sequence
<Any
> aParams
;
72 Sequence
<Type
> aParamTypes
;
76 RuntimeCargo
*cargo
= 0;
77 me
= (PyUNO_callable
*) self
;
83 cargo
= runtime
.getImpl()->cargo
;
84 any_params
= runtime
.pyObject2Any (args
, me
->members
->mode
);
86 if (any_params
.getValueTypeClass () == com::sun::star::uno::TypeClass_SEQUENCE
)
88 any_params
>>= aParams
;
93 aParams
[0] <<= any_params
;
97 PyThreadDetach antiguard
; //pyhton free zone
99 // do some logging if desired ...
100 if( isLog( cargo
, LogLevel::CALL
) )
102 logCall( cargo
, "try py->uno[0x", me
->members
->xInvocation
.get(),
103 me
->members
->methodName
, aParams
);
107 ret_value
= me
->members
->xInvocation
->invoke (
108 me
->members
->methodName
, aParams
, aOutParamIndex
, aOutParam
);
110 // log the reply, if desired
111 if( isLog( cargo
, LogLevel::CALL
) )
113 logReply( cargo
, "success py->uno[0x", me
->members
->xInvocation
.get(),
114 me
->members
->methodName
, ret_value
, aOutParam
);
119 PyRef temp
= runtime
.any2PyObject (ret_value
);
120 if( aOutParam
.getLength() )
122 PyRef
return_list( PyTuple_New (1+aOutParam
.getLength()), SAL_NO_ACQUIRE
);
123 PyTuple_SetItem (return_list
.get(), 0, temp
.getAcquired());
125 // initialize with defaults in case of exceptions
127 for( i
= 1 ; i
< 1+aOutParam
.getLength() ; i
++ )
129 Py_INCREF( Py_None
);
130 PyTuple_SetItem( return_list
.get() , i
, Py_None
);
133 for( i
= 0 ; i
< aOutParam
.getLength() ; i
++ )
135 PyRef ref
= runtime
.any2PyObject( aOutParam
[i
] );
136 PyTuple_SetItem (return_list
.get(), 1+i
, ref
.getAcquired());
145 catch( const com::sun::star::reflection::InvocationTargetException
& e
)
148 if( isLog( cargo
, LogLevel::CALL
) )
150 logException( cargo
, "except py->uno[0x", me
->members
->xInvocation
.get() ,
151 me
->members
->methodName
, e
.TargetException
.getValue(), e
.TargetException
.getValueTypeRef());
153 raisePyExceptionWithAny( e
.TargetException
);
155 catch( const com::sun::star::script::CannotConvertException
&e
)
157 if( isLog( cargo
, LogLevel::CALL
) )
159 logException( cargo
, "error py->uno[0x", me
->members
->xInvocation
.get() ,
160 me
->members
->methodName
, &e
, getCppuType(&e
).getTypeLibType());
162 raisePyExceptionWithAny( com::sun::star::uno::makeAny( e
) );
164 catch( const com::sun::star::lang::IllegalArgumentException
&e
)
166 if( isLog( cargo
, LogLevel::CALL
) )
168 logException( cargo
, "error py->uno[0x", me
->members
->xInvocation
.get() ,
169 me
->members
->methodName
, &e
, getCppuType(&e
).getTypeLibType());
171 raisePyExceptionWithAny( com::sun::star::uno::makeAny( e
) );
173 catch (const ::com::sun::star::uno::RuntimeException
&e
)
175 if( cargo
&& isLog( cargo
, LogLevel::CALL
) )
177 logException( cargo
, "error py->uno[0x", me
->members
->xInvocation
.get() ,
178 me
->members
->methodName
, &e
, getCppuType(&e
).getTypeLibType());
180 raisePyExceptionWithAny( com::sun::star::uno::makeAny( e
) );
183 return ret
.getAcquired();
187 static PyTypeObject PyUNO_callable_Type
=
189 PyVarObject_HEAD_INIT( &PyType_Type
, 0 )
191 sizeof (PyUNO_callable
),
193 (destructor
) ::pyuno::PyUNO_callable_del
,
203 (ternaryfunc
) ::pyuno::PyUNO_callable_call
,
235 #if PY_VERSION_HEX >= 0x02060000
240 PyRef
PyUNO_callable_new (
241 const Reference
<XInvocation2
> &my_inv
,
242 const OUString
& methodName
,
243 const Reference
<XSingleServiceFactory
> &xInvocationFactory
,
244 const Reference
<XTypeConverter
> &tc
,
245 enum ConversionMode mode
)
247 PyUNO_callable
* self
;
249 OSL_ENSURE (my_inv
.is(), "XInvocation must be valid");
251 self
= PyObject_New (PyUNO_callable
, &PyUNO_callable_Type
);
253 return NULL
; //NULL == Error!
255 self
->members
= new PyUNO_callable_Internals
;
256 self
->members
->xInvocation
= my_inv
;
257 self
->members
->methodName
= methodName
;
258 self
->members
->xInvocationFactory
= xInvocationFactory
;
259 self
->members
->xTypeConverter
= tc
;
260 self
->members
->mode
= mode
;
262 return PyRef( (PyObject
*)self
, SAL_NO_ACQUIRE
);
267 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */