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
;
43 } PyUNO_callable_Internals
;
48 PyUNO_callable_Internals
* members
;
51 void PyUNO_callable_del (PyObject
* self
)
55 me
= (PyUNO_callable
*) self
;
62 PyObject
* PyUNO_callable_call(
63 PyObject
* self
, PyObject
* args
, SAL_UNUSED_PARAMETER PyObject
*)
67 Sequence
<short> aOutParamIndex
;
68 Sequence
<Any
> aOutParam
;
69 Sequence
<Any
> aParams
;
72 RuntimeCargo
*cargo
= 0;
73 me
= (PyUNO_callable
*) self
;
79 cargo
= runtime
.getImpl()->cargo
;
80 any_params
= runtime
.pyObject2Any (args
, me
->members
->mode
);
82 if (any_params
.getValueTypeClass () == com::sun::star::uno::TypeClass_SEQUENCE
)
84 any_params
>>= aParams
;
89 aParams
[0] <<= any_params
;
93 PyThreadDetach antiguard
; //pyhton free zone
95 // do some logging if desired ...
96 if( isLog( cargo
, LogLevel::CALL
) )
98 logCall( cargo
, "try py->uno[0x", me
->members
->xInvocation
.get(),
99 me
->members
->methodName
, aParams
);
103 ret_value
= me
->members
->xInvocation
->invoke (
104 me
->members
->methodName
, aParams
, aOutParamIndex
, aOutParam
);
106 // log the reply, if desired
107 if( isLog( cargo
, LogLevel::CALL
) )
109 logReply( cargo
, "success py->uno[0x", me
->members
->xInvocation
.get(),
110 me
->members
->methodName
, ret_value
, aOutParam
);
115 PyRef temp
= runtime
.any2PyObject (ret_value
);
116 if( aOutParam
.getLength() )
118 PyRef
return_list( PyTuple_New (1+aOutParam
.getLength()), SAL_NO_ACQUIRE
);
119 PyTuple_SetItem (return_list
.get(), 0, temp
.getAcquired());
121 // initialize with defaults in case of exceptions
123 for( i
= 1 ; i
< 1+aOutParam
.getLength() ; i
++ )
125 Py_INCREF( Py_None
);
126 PyTuple_SetItem( return_list
.get() , i
, Py_None
);
129 for( i
= 0 ; i
< aOutParam
.getLength() ; i
++ )
131 PyRef ref
= runtime
.any2PyObject( aOutParam
[i
] );
132 PyTuple_SetItem (return_list
.get(), 1+i
, ref
.getAcquired());
141 catch( const com::sun::star::reflection::InvocationTargetException
& e
)
144 if( isLog( cargo
, LogLevel::CALL
) )
146 logException( cargo
, "except py->uno[0x", me
->members
->xInvocation
.get() ,
147 me
->members
->methodName
, e
.TargetException
.getValue(), e
.TargetException
.getValueTypeRef());
149 raisePyExceptionWithAny( e
.TargetException
);
151 catch( const com::sun::star::script::CannotConvertException
&e
)
153 if( isLog( cargo
, LogLevel::CALL
) )
155 logException( cargo
, "error py->uno[0x", me
->members
->xInvocation
.get() ,
156 me
->members
->methodName
, &e
, getCppuType(&e
).getTypeLibType());
158 raisePyExceptionWithAny( com::sun::star::uno::makeAny( e
) );
160 catch( const com::sun::star::lang::IllegalArgumentException
&e
)
162 if( isLog( cargo
, LogLevel::CALL
) )
164 logException( cargo
, "error py->uno[0x", me
->members
->xInvocation
.get() ,
165 me
->members
->methodName
, &e
, getCppuType(&e
).getTypeLibType());
167 raisePyExceptionWithAny( com::sun::star::uno::makeAny( e
) );
169 catch (const ::com::sun::star::uno::RuntimeException
&e
)
171 if( cargo
&& isLog( cargo
, LogLevel::CALL
) )
173 logException( cargo
, "error py->uno[0x", me
->members
->xInvocation
.get() ,
174 me
->members
->methodName
, &e
, getCppuType(&e
).getTypeLibType());
176 raisePyExceptionWithAny( com::sun::star::uno::makeAny( e
) );
179 return ret
.getAcquired();
183 static PyTypeObject PyUNO_callable_Type
=
185 PyVarObject_HEAD_INIT( &PyType_Type
, 0 )
187 sizeof (PyUNO_callable
),
189 (destructor
) ::pyuno::PyUNO_callable_del
,
199 (ternaryfunc
) ::pyuno::PyUNO_callable_call
,
231 #if PY_VERSION_HEX >= 0x02060000
236 PyRef
PyUNO_callable_new (
237 const Reference
<XInvocation2
> &my_inv
,
238 const OUString
& methodName
,
239 enum ConversionMode mode
)
241 PyUNO_callable
* self
;
243 OSL_ENSURE (my_inv
.is(), "XInvocation must be valid");
245 self
= PyObject_New (PyUNO_callable
, &PyUNO_callable_Type
);
247 return NULL
; //NULL == Error!
249 self
->members
= new PyUNO_callable_Internals
;
250 self
->members
->xInvocation
= my_inv
;
251 self
->members
->methodName
= methodName
;
252 self
->members
->mode
= mode
;
254 return PyRef( (PyObject
*)self
, SAL_NO_ACQUIRE
);
259 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */