1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 #include "pyuno_impl.hxx" 24 25 #include <osl/thread.h> 26 #include <rtl/ustrbuf.hxx> 27 28 using rtl::OUStringToOString; 29 using rtl::OUString; 30 using com::sun::star::uno::Sequence; 31 using com::sun::star::uno::Reference; 32 using com::sun::star::uno::XInterface; 33 using com::sun::star::uno::Any; 34 using com::sun::star::uno::Type; 35 using com::sun::star::uno::TypeClass; 36 using com::sun::star::uno::RuntimeException; 37 using com::sun::star::uno::XComponentContext; 38 using com::sun::star::lang::XSingleServiceFactory; 39 using com::sun::star::script::XTypeConverter; 40 using com::sun::star::script::XInvocation2; 41 42 namespace pyuno 43 { 44 typedef struct 45 { 46 Reference<XInvocation2> xInvocation; 47 OUString methodName; 48 ConversionMode mode; 49 } PyUNO_callable_Internals; 50 51 typedef struct 52 { 53 PyObject_HEAD 54 PyUNO_callable_Internals* members; 55 } PyUNO_callable; 56 57 void PyUNO_callable_del (PyObject* self) 58 { 59 PyUNO_callable* me; 60 61 me = (PyUNO_callable*) self; 62 delete me->members; 63 PyObject_Del (self); 64 65 return; 66 } 67 68 PyObject* PyUNO_callable_call (PyObject* self, PyObject* args, PyObject*) 69 { 70 PyUNO_callable* me; 71 72 Sequence<short> aOutParamIndex; 73 Sequence<Any> aOutParam; 74 Sequence<Any> aParams; 75 Sequence<Type> aParamTypes; 76 Any any_params; 77 Any out_params; 78 Any ret_value; 79 RuntimeCargo *cargo = 0; 80 me = (PyUNO_callable*) self; 81 82 PyRef ret; 83 try 84 { 85 Runtime runtime; 86 cargo = runtime.getImpl()->cargo; 87 any_params = runtime.pyObject2Any (args, me->members->mode); 88 89 if (any_params.getValueTypeClass () == com::sun::star::uno::TypeClass_SEQUENCE) 90 { 91 any_params >>= aParams; 92 } 93 else 94 { 95 aParams.realloc (1); 96 aParams [0] <<= any_params; 97 } 98 99 { 100 PyThreadDetach antiguard; //pyhton free zone 101 102 // do some logging if desired ... 103 if( isLog( cargo, LogLevel::CALL ) ) 104 { 105 logCall( cargo, "try py->uno[0x", me->members->xInvocation.get(), 106 me->members->methodName, aParams ); 107 } 108 109 // do the call 110 ret_value = me->members->xInvocation->invoke ( 111 me->members->methodName, aParams, aOutParamIndex, aOutParam); 112 113 // log the reply, if desired 114 if( isLog( cargo, LogLevel::CALL ) ) 115 { 116 logReply( cargo, "success py->uno[0x", me->members->xInvocation.get(), 117 me->members->methodName, ret_value, aOutParam); 118 } 119 } 120 121 122 PyRef temp = runtime.any2PyObject (ret_value); 123 if( aOutParam.getLength() ) 124 { 125 PyRef return_list( PyTuple_New (1+aOutParam.getLength()), SAL_NO_ACQUIRE ); 126 PyTuple_SetItem (return_list.get(), 0, temp.getAcquired()); 127 128 // initialize with defaults in case of exceptions 129 int i; 130 for( i = 1 ; i < 1+aOutParam.getLength() ; i ++ ) 131 { 132 Py_INCREF( Py_None ); 133 PyTuple_SetItem( return_list.get() , i , Py_None ); 134 } 135 136 for( i = 0 ; i < aOutParam.getLength() ; i ++ ) 137 { 138 PyRef ref = runtime.any2PyObject( aOutParam[i] ); 139 PyTuple_SetItem (return_list.get(), 1+i, ref.getAcquired()); 140 } 141 ret = return_list; 142 } 143 else 144 { 145 ret = temp; 146 } 147 } 148 catch( com::sun::star::reflection::InvocationTargetException & e ) 149 { 150 151 if( isLog( cargo, LogLevel::CALL ) ) 152 { 153 logException( cargo, "except py->uno[0x", me->members->xInvocation.get() , 154 me->members->methodName, e.TargetException.getValue(), e.TargetException.getValueTypeRef()); 155 } 156 raisePyExceptionWithAny( e.TargetException ); 157 } 158 catch( com::sun::star::script::CannotConvertException &e ) 159 { 160 if( isLog( cargo, LogLevel::CALL ) ) 161 { 162 logException( cargo, "error py->uno[0x", me->members->xInvocation.get() , 163 me->members->methodName, &e, getCppuType(&e).getTypeLibType()); 164 } 165 raisePyExceptionWithAny( com::sun::star::uno::makeAny( e ) ); 166 } 167 catch( com::sun::star::lang::IllegalArgumentException &e ) 168 { 169 if( isLog( cargo, LogLevel::CALL ) ) 170 { 171 logException( cargo, "error py->uno[0x", me->members->xInvocation.get() , 172 me->members->methodName, &e, getCppuType(&e).getTypeLibType()); 173 } 174 raisePyExceptionWithAny( com::sun::star::uno::makeAny( e ) ); 175 } 176 catch (::com::sun::star::uno::RuntimeException &e) 177 { 178 if( cargo && isLog( cargo, LogLevel::CALL ) ) 179 { 180 logException( cargo, "error py->uno[0x", me->members->xInvocation.get() , 181 me->members->methodName, &e, getCppuType(&e).getTypeLibType()); 182 } 183 raisePyExceptionWithAny( com::sun::star::uno::makeAny( e ) ); 184 } 185 186 return ret.getAcquired(); 187 } 188 189 190 static PyTypeObject PyUNO_callable_Type = 191 { 192 PyVarObject_HEAD_INIT(&PyType_Type, 0) 193 const_cast< char * >("PyUNO_callable"), 194 sizeof (PyUNO_callable), 195 0, 196 (destructor) ::pyuno::PyUNO_callable_del, 197 (printfunc) 0, 198 (getattrfunc) 0, 199 (setattrfunc) 0, 200 #if PY_MAJOR_VERSION >= 3 201 0, 202 #else 203 (cmpfunc) 0, 204 #endif 205 (reprfunc) 0, 206 0, 207 0, 208 0, 209 (hashfunc) 0, 210 (ternaryfunc) ::pyuno::PyUNO_callable_call, 211 (reprfunc) 0, 212 (getattrofunc)0, 213 (setattrofunc)0, 214 NULL, 215 0, 216 NULL, 217 (traverseproc)0, 218 (inquiry)0, 219 (richcmpfunc)0, 220 0, 221 (getiterfunc)0, 222 (iternextfunc)0, 223 NULL, 224 NULL, 225 NULL, 226 NULL, 227 NULL, 228 (descrgetfunc)0, 229 (descrsetfunc)0, 230 0, 231 (initproc)0, 232 (allocfunc)0, 233 (newfunc)0, 234 (freefunc)0, 235 (inquiry)0, 236 NULL, 237 NULL, 238 NULL, 239 NULL, 240 NULL, 241 (destructor)0 242 #if PY_VERSION_HEX >= 0x02060000 243 , 0 244 #endif 245 }; 246 247 PyRef PyUNO_callable_new ( 248 const Reference<XInvocation2> &my_inv, 249 const OUString & methodName, 250 enum ConversionMode mode ) 251 { 252 PyUNO_callable* self; 253 254 self = PyObject_New (PyUNO_callable, &PyUNO_callable_Type); 255 if (self == NULL) 256 return NULL; //NULL == Error! 257 258 self->members = new PyUNO_callable_Internals; 259 self->members->xInvocation = my_inv; 260 self->members->methodName = methodName; 261 self->members->mode = mode; 262 263 return PyRef( (PyObject*)self, SAL_NO_ACQUIRE ); 264 } 265 266 } 267