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