Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 : #include "pyuno_impl.hxx"
20 :
21 : #include <osl/thread.h>
22 : #include <rtl/ustrbuf.hxx>
23 :
24 : using rtl::OUStringToOString;
25 : using rtl::OUString;
26 : using com::sun::star::uno::Sequence;
27 : using com::sun::star::uno::Reference;
28 : using com::sun::star::uno::XInterface;
29 : using com::sun::star::uno::Any;
30 : using com::sun::star::uno::Type;
31 : using com::sun::star::uno::TypeClass;
32 : using com::sun::star::uno::RuntimeException;
33 : using com::sun::star::uno::XComponentContext;
34 : using com::sun::star::lang::XSingleServiceFactory;
35 : using com::sun::star::script::XTypeConverter;
36 : using com::sun::star::script::XInvocation2;
37 :
38 : namespace pyuno
39 : {
40 : typedef struct
41 0 : {
42 : Reference<XInvocation2> xInvocation;
43 : Reference<XSingleServiceFactory> xInvocationFactory;
44 : Reference<XTypeConverter> xTypeConverter;
45 : OUString methodName;
46 : ConversionMode mode;
47 0 : } PyUNO_callable_Internals;
48 :
49 : typedef struct
50 : {
51 : PyObject_HEAD
52 : PyUNO_callable_Internals* members;
53 : } PyUNO_callable;
54 :
55 0 : void PyUNO_callable_del (PyObject* self)
56 : {
57 : PyUNO_callable* me;
58 :
59 0 : me = (PyUNO_callable*) self;
60 0 : delete me->members;
61 0 : PyObject_Del (self);
62 :
63 0 : return;
64 : }
65 :
66 0 : PyObject* PyUNO_callable_call(
67 : PyObject* self, PyObject* args, SAL_UNUSED_PARAMETER PyObject*)
68 : {
69 : PyUNO_callable* me;
70 :
71 0 : Sequence<short> aOutParamIndex;
72 0 : Sequence<Any> aOutParam;
73 0 : Sequence<Any> aParams;
74 0 : Sequence<Type> aParamTypes;
75 0 : Any any_params;
76 0 : Any out_params;
77 0 : Any ret_value;
78 0 : RuntimeCargo *cargo = 0;
79 0 : me = (PyUNO_callable*) self;
80 :
81 0 : PyRef ret;
82 : try
83 : {
84 0 : Runtime runtime;
85 0 : cargo = runtime.getImpl()->cargo;
86 0 : any_params = runtime.pyObject2Any (args, me->members->mode);
87 :
88 0 : if (any_params.getValueTypeClass () == com::sun::star::uno::TypeClass_SEQUENCE)
89 : {
90 0 : any_params >>= aParams;
91 : }
92 : else
93 : {
94 0 : aParams.realloc (1);
95 0 : aParams [0] <<= any_params;
96 : }
97 :
98 : {
99 0 : PyThreadDetach antiguard; //pyhton free zone
100 :
101 : // do some logging if desired ...
102 0 : if( isLog( cargo, LogLevel::CALL ) )
103 : {
104 0 : logCall( cargo, "try py->uno[0x", me->members->xInvocation.get(),
105 0 : me->members->methodName, aParams );
106 : }
107 :
108 : // do the call
109 0 : ret_value = me->members->xInvocation->invoke (
110 0 : me->members->methodName, aParams, aOutParamIndex, aOutParam);
111 :
112 : // log the reply, if desired
113 0 : if( isLog( cargo, LogLevel::CALL ) )
114 : {
115 0 : logReply( cargo, "success py->uno[0x", me->members->xInvocation.get(),
116 0 : me->members->methodName, ret_value, aOutParam);
117 0 : }
118 : }
119 :
120 :
121 0 : PyRef temp = runtime.any2PyObject (ret_value);
122 0 : if( aOutParam.getLength() )
123 : {
124 0 : PyRef return_list( PyTuple_New (1+aOutParam.getLength()), SAL_NO_ACQUIRE );
125 0 : PyTuple_SetItem (return_list.get(), 0, temp.getAcquired());
126 :
127 : // initialize with defaults in case of exceptions
128 : int i;
129 0 : for( i = 1 ; i < 1+aOutParam.getLength() ; i ++ )
130 : {
131 0 : Py_INCREF( Py_None );
132 0 : PyTuple_SetItem( return_list.get() , i , Py_None );
133 : }
134 :
135 0 : for( i = 0 ; i < aOutParam.getLength() ; i ++ )
136 : {
137 0 : PyRef ref = runtime.any2PyObject( aOutParam[i] );
138 0 : PyTuple_SetItem (return_list.get(), 1+i, ref.getAcquired());
139 0 : }
140 0 : ret = return_list;
141 : }
142 : else
143 : {
144 0 : ret = temp;
145 0 : }
146 : }
147 0 : catch( const com::sun::star::reflection::InvocationTargetException & e )
148 : {
149 :
150 0 : if( isLog( cargo, LogLevel::CALL ) )
151 : {
152 0 : logException( cargo, "except py->uno[0x", me->members->xInvocation.get() ,
153 0 : me->members->methodName, e.TargetException.getValue(), e.TargetException.getValueTypeRef());
154 : }
155 0 : raisePyExceptionWithAny( e.TargetException );
156 : }
157 0 : catch( const com::sun::star::script::CannotConvertException &e )
158 : {
159 0 : if( isLog( cargo, LogLevel::CALL ) )
160 : {
161 0 : logException( cargo, "error py->uno[0x", me->members->xInvocation.get() ,
162 0 : me->members->methodName, &e, getCppuType(&e).getTypeLibType());
163 : }
164 0 : raisePyExceptionWithAny( com::sun::star::uno::makeAny( e ) );
165 : }
166 0 : catch( const com::sun::star::lang::IllegalArgumentException &e )
167 : {
168 0 : if( isLog( cargo, LogLevel::CALL ) )
169 : {
170 0 : logException( cargo, "error py->uno[0x", me->members->xInvocation.get() ,
171 0 : me->members->methodName, &e, getCppuType(&e).getTypeLibType());
172 : }
173 0 : raisePyExceptionWithAny( com::sun::star::uno::makeAny( e ) );
174 : }
175 0 : catch (const ::com::sun::star::uno::RuntimeException &e)
176 : {
177 0 : if( cargo && isLog( cargo, LogLevel::CALL ) )
178 : {
179 0 : logException( cargo, "error py->uno[0x", me->members->xInvocation.get() ,
180 0 : me->members->methodName, &e, getCppuType(&e).getTypeLibType());
181 : }
182 0 : raisePyExceptionWithAny( com::sun::star::uno::makeAny( e ) );
183 : }
184 :
185 0 : return ret.getAcquired();
186 : }
187 :
188 :
189 : static PyTypeObject PyUNO_callable_Type =
190 : {
191 : PyVarObject_HEAD_INIT( &PyType_Type, 0 )
192 : "PyUNO_callable",
193 : sizeof (PyUNO_callable),
194 : 0,
195 : (destructor) ::pyuno::PyUNO_callable_del,
196 : (printfunc) 0,
197 : (getattrfunc) 0,
198 : (setattrfunc) 0,
199 : 0,
200 : (reprfunc) 0,
201 : 0,
202 : 0,
203 : 0,
204 : (hashfunc) 0,
205 : (ternaryfunc) ::pyuno::PyUNO_callable_call,
206 : (reprfunc) 0,
207 : (getattrofunc)0,
208 : (setattrofunc)0,
209 : NULL,
210 : 0,
211 : NULL,
212 : (traverseproc)0,
213 : (inquiry)0,
214 : (richcmpfunc)0,
215 : 0,
216 : (getiterfunc)0,
217 : (iternextfunc)0,
218 : NULL,
219 : NULL,
220 : NULL,
221 : NULL,
222 : NULL,
223 : (descrgetfunc)0,
224 : (descrsetfunc)0,
225 : 0,
226 : (initproc)0,
227 : (allocfunc)0,
228 : (newfunc)0,
229 : (freefunc)0,
230 : (inquiry)0,
231 : NULL,
232 : NULL,
233 : NULL,
234 : NULL,
235 : NULL,
236 : (destructor)0
237 : #if PY_VERSION_HEX >= 0x02060000
238 : , 0
239 : #endif
240 : };
241 :
242 0 : PyRef PyUNO_callable_new (
243 : const Reference<XInvocation2> &my_inv,
244 : const OUString & methodName,
245 : const Reference<XSingleServiceFactory> &xInvocationFactory,
246 : const Reference<XTypeConverter> &tc,
247 : enum ConversionMode mode )
248 : {
249 : PyUNO_callable* self;
250 :
251 : OSL_ENSURE (my_inv.is(), "XInvocation must be valid");
252 :
253 0 : self = PyObject_New (PyUNO_callable, &PyUNO_callable_Type);
254 0 : if (self == NULL)
255 0 : return NULL; //NULL == Error!
256 :
257 0 : self->members = new PyUNO_callable_Internals;
258 0 : self->members->xInvocation = my_inv;
259 0 : self->members->methodName = methodName;
260 0 : self->members->xInvocationFactory = xInvocationFactory;
261 0 : self->members->xTypeConverter = tc;
262 0 : self->members->mode = mode;
263 :
264 0 : return PyRef( (PyObject*)self, SAL_NO_ACQUIRE );
265 : }
266 :
267 : }
268 :
269 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|