1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir #include "pyuno_impl.hxx"
28*cdf0e10cSrcweir 
29*cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
30*cdf0e10cSrcweir #include <rtl/strbuf.hxx>
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir #include <com/sun/star/beans/MethodConcept.hpp>
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir #include <cppuhelper/typeprovider.hxx>
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir using rtl::OUStringToOString;
37*cdf0e10cSrcweir using rtl::OUString;
38*cdf0e10cSrcweir using rtl::OUStringBuffer;
39*cdf0e10cSrcweir using rtl::OString;
40*cdf0e10cSrcweir using rtl::OStringBuffer;
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir using com::sun::star::beans::XIntrospectionAccess;
43*cdf0e10cSrcweir using com::sun::star::beans::XIntrospection;
44*cdf0e10cSrcweir using com::sun::star::uno::Any;
45*cdf0e10cSrcweir using com::sun::star::uno::makeAny;
46*cdf0e10cSrcweir using com::sun::star::uno::Reference;
47*cdf0e10cSrcweir using com::sun::star::uno::Sequence;
48*cdf0e10cSrcweir using com::sun::star::uno::RuntimeException;
49*cdf0e10cSrcweir using com::sun::star::uno::XInterface;
50*cdf0e10cSrcweir using com::sun::star::uno::Type;
51*cdf0e10cSrcweir using com::sun::star::lang::XUnoTunnel;
52*cdf0e10cSrcweir using com::sun::star::lang::IllegalArgumentException;
53*cdf0e10cSrcweir using com::sun::star::beans::UnknownPropertyException;
54*cdf0e10cSrcweir using com::sun::star::script::CannotConvertException;
55*cdf0e10cSrcweir using com::sun::star::reflection::InvocationTargetException;
56*cdf0e10cSrcweir using com::sun::star::reflection::XIdlMethod;
57*cdf0e10cSrcweir using com::sun::star::reflection::ParamInfo;
58*cdf0e10cSrcweir using com::sun::star::reflection::XIdlClass;
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir #define TO_ASCII(x) OUStringToOString( x , RTL_TEXTENCODING_ASCII_US).getStr()
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir namespace pyuno
63*cdf0e10cSrcweir {
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir Adapter::Adapter( const PyRef & ref, const Sequence< Type > &types )
66*cdf0e10cSrcweir     : mWrappedObject( ref ),
67*cdf0e10cSrcweir       mInterpreter( (PyThreadState_Get()->interp) ),
68*cdf0e10cSrcweir       mTypes( types )
69*cdf0e10cSrcweir {}
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir Adapter::~Adapter()
72*cdf0e10cSrcweir {
73*cdf0e10cSrcweir     // Problem: We don't know, if we have the python interpreter lock
74*cdf0e10cSrcweir     //       There is no runtime function to get to know this.
75*cdf0e10cSrcweir     decreaseRefCount( mInterpreter, mWrappedObject.get() );
76*cdf0e10cSrcweir     mWrappedObject.scratch();
77*cdf0e10cSrcweir }
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir static cppu::OImplementationId g_id( sal_False );
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir Sequence<sal_Int8> Adapter::getUnoTunnelImplementationId()
82*cdf0e10cSrcweir {
83*cdf0e10cSrcweir     return g_id.getImplementationId();
84*cdf0e10cSrcweir }
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir sal_Int64 Adapter::getSomething( const Sequence< sal_Int8 > &id) throw (RuntimeException)
87*cdf0e10cSrcweir {
88*cdf0e10cSrcweir     if( id == g_id.getImplementationId() )
89*cdf0e10cSrcweir         return reinterpret_cast<sal_Int64>(this);
90*cdf0e10cSrcweir     return 0;
91*cdf0e10cSrcweir }
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir void raiseInvocationTargetExceptionWhenNeeded( const Runtime &runtime )
94*cdf0e10cSrcweir     throw ( InvocationTargetException )
95*cdf0e10cSrcweir {
96*cdf0e10cSrcweir     if( PyErr_Occurred() )
97*cdf0e10cSrcweir     {
98*cdf0e10cSrcweir         PyRef excType, excValue, excTraceback;
99*cdf0e10cSrcweir         PyErr_Fetch( (PyObject **)&excType, (PyObject**)&excValue,(PyObject**)&excTraceback);
100*cdf0e10cSrcweir         Any unoExc( runtime.extractUnoException( excType, excValue, excTraceback ) );
101*cdf0e10cSrcweir         throw InvocationTargetException(
102*cdf0e10cSrcweir             ((com::sun::star::uno::Exception*)unoExc.getValue())->Message,
103*cdf0e10cSrcweir             Reference<XInterface>(), unoExc );
104*cdf0e10cSrcweir     }
105*cdf0e10cSrcweir }
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir Reference< XIntrospectionAccess > Adapter::getIntrospection()
108*cdf0e10cSrcweir     throw ( RuntimeException )
109*cdf0e10cSrcweir {
110*cdf0e10cSrcweir     // not supported
111*cdf0e10cSrcweir     return Reference< XIntrospectionAccess > ();
112*cdf0e10cSrcweir }
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir Sequence< sal_Int16 > Adapter::getOutIndexes( const OUString & functionName )
115*cdf0e10cSrcweir {
116*cdf0e10cSrcweir     Sequence< sal_Int16 > ret;
117*cdf0e10cSrcweir     MethodOutIndexMap::const_iterator ii = m_methodOutIndexMap.find( functionName );
118*cdf0e10cSrcweir     if( ii == m_methodOutIndexMap.end() )
119*cdf0e10cSrcweir     {
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir         Runtime runtime;
122*cdf0e10cSrcweir         {
123*cdf0e10cSrcweir             PyThreadDetach antiguard;
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir             // retrieve the adapter object again. It will be the same instance as before,
126*cdf0e10cSrcweir             // (the adapter factory keeps a weak map inside, which I couldn't have outside)
127*cdf0e10cSrcweir             Reference< XInterface > unoAdapterObject =
128*cdf0e10cSrcweir                 runtime.getImpl()->cargo->xAdapterFactory->createAdapter( this, mTypes );
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir             // uuuh, that's really expensive. The alternative would have been, to store
131*cdf0e10cSrcweir             // an instance of the introspection at (this), but this results in a cyclic
132*cdf0e10cSrcweir             // reference, which is never broken (as it is up to OOo1.1.0).
133*cdf0e10cSrcweir             Reference< XIntrospectionAccess > introspection =
134*cdf0e10cSrcweir                 runtime.getImpl()->cargo->xIntrospection->inspect( makeAny( unoAdapterObject ) );
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir             if( !introspection.is() )
137*cdf0e10cSrcweir             {
138*cdf0e10cSrcweir                 throw RuntimeException(
139*cdf0e10cSrcweir                     OUString( RTL_CONSTASCII_USTRINGPARAM( "pyuno bridge: Couldn't inspect uno adapter ( the python class must implement com.sun.star.lang.XTypeProvider !)" ) ),
140*cdf0e10cSrcweir                     Reference< XInterface > () );
141*cdf0e10cSrcweir             }
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir             Reference< XIdlMethod > method = introspection->getMethod(
144*cdf0e10cSrcweir                 functionName, com::sun::star::beans::MethodConcept::ALL );
145*cdf0e10cSrcweir             if( ! method.is( ) )
146*cdf0e10cSrcweir             {
147*cdf0e10cSrcweir                 throw RuntimeException(
148*cdf0e10cSrcweir                     (OUString(
149*cdf0e10cSrcweir                         RTL_CONSTASCII_USTRINGPARAM(
150*cdf0e10cSrcweir                             "pyuno bridge: Couldn't get reflection for method "))
151*cdf0e10cSrcweir                      + functionName),
152*cdf0e10cSrcweir                     Reference< XInterface > () );
153*cdf0e10cSrcweir             }
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir             Sequence< ParamInfo > seqInfo = method->getParameterInfos();
156*cdf0e10cSrcweir             int i;
157*cdf0e10cSrcweir             int nOuts = 0;
158*cdf0e10cSrcweir             for( i = 0 ; i < seqInfo.getLength() ; i ++ )
159*cdf0e10cSrcweir             {
160*cdf0e10cSrcweir                 if( seqInfo[i].aMode == com::sun::star::reflection::ParamMode_OUT ||
161*cdf0e10cSrcweir                     seqInfo[i].aMode == com::sun::star::reflection::ParamMode_INOUT )
162*cdf0e10cSrcweir                 {
163*cdf0e10cSrcweir                     // sequence must be interpreted as return value/outparameter tuple !
164*cdf0e10cSrcweir                     nOuts ++;
165*cdf0e10cSrcweir                 }
166*cdf0e10cSrcweir             }
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir             if( nOuts )
169*cdf0e10cSrcweir             {
170*cdf0e10cSrcweir                 ret.realloc( nOuts );
171*cdf0e10cSrcweir                 sal_Int32 nOutsAssigned = 0;
172*cdf0e10cSrcweir                 for( i = 0 ; i < seqInfo.getLength() ; i ++ )
173*cdf0e10cSrcweir                 {
174*cdf0e10cSrcweir                     if( seqInfo[i].aMode == com::sun::star::reflection::ParamMode_OUT ||
175*cdf0e10cSrcweir                         seqInfo[i].aMode == com::sun::star::reflection::ParamMode_INOUT )
176*cdf0e10cSrcweir                     {
177*cdf0e10cSrcweir                         ret[nOutsAssigned] = (sal_Int16) i;
178*cdf0e10cSrcweir                         nOutsAssigned ++;
179*cdf0e10cSrcweir                     }
180*cdf0e10cSrcweir                 }
181*cdf0e10cSrcweir             }
182*cdf0e10cSrcweir         }
183*cdf0e10cSrcweir         // guard active again !
184*cdf0e10cSrcweir         m_methodOutIndexMap[ functionName ] = ret;
185*cdf0e10cSrcweir     }
186*cdf0e10cSrcweir     else
187*cdf0e10cSrcweir     {
188*cdf0e10cSrcweir         ret = ii->second;
189*cdf0e10cSrcweir     }
190*cdf0e10cSrcweir     return ret;
191*cdf0e10cSrcweir }
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir Any Adapter::invoke( const OUString &aFunctionName,
194*cdf0e10cSrcweir                      const Sequence< Any >& aParams,
195*cdf0e10cSrcweir                      Sequence< sal_Int16 > &aOutParamIndex,
196*cdf0e10cSrcweir                      Sequence< Any > &aOutParam)
197*cdf0e10cSrcweir     throw (IllegalArgumentException,CannotConvertException,InvocationTargetException,RuntimeException)
198*cdf0e10cSrcweir {
199*cdf0e10cSrcweir     Any ret;
200*cdf0e10cSrcweir 
201*cdf0e10cSrcweir     // special hack for the uno object identity concept. The XUnoTunnel.getSomething() call is
202*cdf0e10cSrcweir     // always handled by the adapter directly.
203*cdf0e10cSrcweir     if( aParams.getLength() == 1 && 0 == aFunctionName.compareToAscii( "getSomething" ) )
204*cdf0e10cSrcweir     {
205*cdf0e10cSrcweir         Sequence< sal_Int8 > id;
206*cdf0e10cSrcweir         if( aParams[0] >>= id )
207*cdf0e10cSrcweir             return com::sun::star::uno::makeAny( getSomething( id ) );
208*cdf0e10cSrcweir 
209*cdf0e10cSrcweir     }
210*cdf0e10cSrcweir 
211*cdf0e10cSrcweir     RuntimeCargo *cargo = 0;
212*cdf0e10cSrcweir     try
213*cdf0e10cSrcweir     {
214*cdf0e10cSrcweir     PyThreadAttach guard( mInterpreter );
215*cdf0e10cSrcweir     {
216*cdf0e10cSrcweir         // convert parameters to python args
217*cdf0e10cSrcweir         // TODO: Out parameter
218*cdf0e10cSrcweir         Runtime runtime;
219*cdf0e10cSrcweir         cargo = runtime.getImpl()->cargo;
220*cdf0e10cSrcweir         if( isLog( cargo, LogLevel::CALL ) )
221*cdf0e10cSrcweir         {
222*cdf0e10cSrcweir             logCall( cargo, "try     uno->py[0x",
223*cdf0e10cSrcweir                      mWrappedObject.get(), aFunctionName, aParams );
224*cdf0e10cSrcweir         }
225*cdf0e10cSrcweir 
226*cdf0e10cSrcweir         sal_Int32 size = aParams.getLength();
227*cdf0e10cSrcweir         PyRef argsTuple(PyTuple_New( size ), SAL_NO_ACQUIRE );
228*cdf0e10cSrcweir         int i;
229*cdf0e10cSrcweir         // fill tuple with default values in case of exceptions
230*cdf0e10cSrcweir         for(  i = 0 ;i < size ; i ++ )
231*cdf0e10cSrcweir         {
232*cdf0e10cSrcweir             Py_INCREF( Py_None );
233*cdf0e10cSrcweir             PyTuple_SetItem( argsTuple.get(), i, Py_None );
234*cdf0e10cSrcweir         }
235*cdf0e10cSrcweir 
236*cdf0e10cSrcweir         // convert args to python
237*cdf0e10cSrcweir         for( i = 0; i < size ; i ++  )
238*cdf0e10cSrcweir         {
239*cdf0e10cSrcweir             PyRef val = runtime.any2PyObject( aParams[i] );
240*cdf0e10cSrcweir             PyTuple_SetItem( argsTuple.get(), i, val.getAcquired() );
241*cdf0e10cSrcweir         }
242*cdf0e10cSrcweir 
243*cdf0e10cSrcweir         // get callable
244*cdf0e10cSrcweir         PyRef method(PyObject_GetAttrString( mWrappedObject.get(), (char*)TO_ASCII(aFunctionName)),
245*cdf0e10cSrcweir                      SAL_NO_ACQUIRE);
246*cdf0e10cSrcweir         raiseInvocationTargetExceptionWhenNeeded( runtime);
247*cdf0e10cSrcweir         if( !method.is() )
248*cdf0e10cSrcweir         {
249*cdf0e10cSrcweir             OUStringBuffer buf;
250*cdf0e10cSrcweir             buf.appendAscii( "pyuno::Adapater: Method " ).append( aFunctionName );
251*cdf0e10cSrcweir             buf.appendAscii( " is not implemented at object " );
252*cdf0e10cSrcweir             PyRef str( PyObject_Repr( mWrappedObject.get() ), SAL_NO_ACQUIRE );
253*cdf0e10cSrcweir             buf.appendAscii( PyString_AsString( str.get() ));
254*cdf0e10cSrcweir             throw IllegalArgumentException( buf.makeStringAndClear(), Reference< XInterface > (),0 );
255*cdf0e10cSrcweir         }
256*cdf0e10cSrcweir 
257*cdf0e10cSrcweir         PyRef pyRet( PyObject_CallObject( method.get(), argsTuple.get() ), SAL_NO_ACQUIRE );
258*cdf0e10cSrcweir         raiseInvocationTargetExceptionWhenNeeded( runtime);
259*cdf0e10cSrcweir         if( pyRet.is() )
260*cdf0e10cSrcweir         {
261*cdf0e10cSrcweir             ret = runtime.pyObject2Any( pyRet );
262*cdf0e10cSrcweir 
263*cdf0e10cSrcweir             if( ret.hasValue() &&
264*cdf0e10cSrcweir                 ret.getValueTypeClass() == com::sun::star::uno::TypeClass_SEQUENCE &&
265*cdf0e10cSrcweir                 0 != aFunctionName.compareToAscii( "getTypes" ) &&  // needed by introspection itself !
266*cdf0e10cSrcweir                 0 != aFunctionName.compareToAscii( "getImplementationId" ) ) // needed by introspection itself !
267*cdf0e10cSrcweir             {
268*cdf0e10cSrcweir                 // the sequence can either be
269*cdf0e10cSrcweir                 // 1)  a simple sequence return value
270*cdf0e10cSrcweir                 // 2)  a sequence, where the first element is the return value
271*cdf0e10cSrcweir                 //     and the following elements are interpreted as the outparameter
272*cdf0e10cSrcweir                 // I can only decide for one solution by checking the method signature,
273*cdf0e10cSrcweir                 // so I need the reflection of the adapter !
274*cdf0e10cSrcweir                 aOutParamIndex = getOutIndexes( aFunctionName );
275*cdf0e10cSrcweir                 if( aOutParamIndex.getLength() )
276*cdf0e10cSrcweir                 {
277*cdf0e10cSrcweir                     // out parameters exist, extract the sequence
278*cdf0e10cSrcweir                     Sequence< Any  > seq;
279*cdf0e10cSrcweir                     if( ! ( ret >>= seq ) )
280*cdf0e10cSrcweir                     {
281*cdf0e10cSrcweir                         throw RuntimeException(
282*cdf0e10cSrcweir                             (OUString(
283*cdf0e10cSrcweir                                 RTL_CONSTASCII_USTRINGPARAM(
284*cdf0e10cSrcweir                                     "pyuno bridge: Couldn't extract out"
285*cdf0e10cSrcweir                                     " parameters for method "))
286*cdf0e10cSrcweir                              + aFunctionName),
287*cdf0e10cSrcweir                             Reference< XInterface > () );
288*cdf0e10cSrcweir                     }
289*cdf0e10cSrcweir 
290*cdf0e10cSrcweir                     if( aOutParamIndex.getLength() +1 != seq.getLength() )
291*cdf0e10cSrcweir                     {
292*cdf0e10cSrcweir                         OUStringBuffer buf;
293*cdf0e10cSrcweir                         buf.appendAscii( "pyuno bridge: expected for method " );
294*cdf0e10cSrcweir                         buf.append( aFunctionName );
295*cdf0e10cSrcweir                         buf.appendAscii( " one return value and " );
296*cdf0e10cSrcweir                         buf.append( (sal_Int32) aOutParamIndex.getLength() );
297*cdf0e10cSrcweir                         buf.appendAscii( " out parameters, got a sequence of " );
298*cdf0e10cSrcweir                         buf.append( seq.getLength() );
299*cdf0e10cSrcweir                         buf.appendAscii( " elements as return value." );
300*cdf0e10cSrcweir                         throw RuntimeException(buf.makeStringAndClear(), *this );
301*cdf0e10cSrcweir                     }
302*cdf0e10cSrcweir 
303*cdf0e10cSrcweir                     aOutParam.realloc( aOutParamIndex.getLength() );
304*cdf0e10cSrcweir                     ret = seq[0];
305*cdf0e10cSrcweir                     for( i = 0 ; i < aOutParamIndex.getLength() ; i ++ )
306*cdf0e10cSrcweir                     {
307*cdf0e10cSrcweir                         aOutParam[i] = seq[1+i];
308*cdf0e10cSrcweir                     }
309*cdf0e10cSrcweir                 }
310*cdf0e10cSrcweir                 // else { sequence is a return value !}
311*cdf0e10cSrcweir             }
312*cdf0e10cSrcweir         }
313*cdf0e10cSrcweir 
314*cdf0e10cSrcweir         // log the reply, if desired
315*cdf0e10cSrcweir         if( isLog( cargo, LogLevel::CALL ) )
316*cdf0e10cSrcweir         {
317*cdf0e10cSrcweir             logReply( cargo, "success uno->py[0x" ,
318*cdf0e10cSrcweir                       mWrappedObject.get(), aFunctionName, ret, aOutParam );
319*cdf0e10cSrcweir         }
320*cdf0e10cSrcweir     }
321*cdf0e10cSrcweir 
322*cdf0e10cSrcweir     }
323*cdf0e10cSrcweir     catch(InvocationTargetException & e )
324*cdf0e10cSrcweir     {
325*cdf0e10cSrcweir         if( isLog( cargo, LogLevel::CALL ) )
326*cdf0e10cSrcweir         {
327*cdf0e10cSrcweir             logException(
328*cdf0e10cSrcweir                 cargo, "except  uno->py[0x" ,
329*cdf0e10cSrcweir                 mWrappedObject.get(), aFunctionName,
330*cdf0e10cSrcweir                 e.TargetException.getValue(),e.TargetException.getValueType() );
331*cdf0e10cSrcweir         }
332*cdf0e10cSrcweir         throw;
333*cdf0e10cSrcweir     }
334*cdf0e10cSrcweir     catch( RuntimeException & e )
335*cdf0e10cSrcweir     {
336*cdf0e10cSrcweir         if( cargo && isLog( cargo, LogLevel::CALL ) )
337*cdf0e10cSrcweir         {
338*cdf0e10cSrcweir             logException(
339*cdf0e10cSrcweir                 cargo, "except  uno->py[0x" ,
340*cdf0e10cSrcweir                 mWrappedObject.get(), aFunctionName, &e,getCppuType(&e) );
341*cdf0e10cSrcweir         }
342*cdf0e10cSrcweir         throw;
343*cdf0e10cSrcweir     }
344*cdf0e10cSrcweir     catch( CannotConvertException & e )
345*cdf0e10cSrcweir     {
346*cdf0e10cSrcweir         if( isLog( cargo, LogLevel::CALL ) )
347*cdf0e10cSrcweir         {
348*cdf0e10cSrcweir             logException(
349*cdf0e10cSrcweir                 cargo, "except  uno->py[0x" ,
350*cdf0e10cSrcweir                 mWrappedObject.get(), aFunctionName, &e,getCppuType(&e) );
351*cdf0e10cSrcweir         }
352*cdf0e10cSrcweir         throw;
353*cdf0e10cSrcweir     }
354*cdf0e10cSrcweir     catch(  IllegalArgumentException & e )
355*cdf0e10cSrcweir     {
356*cdf0e10cSrcweir         if( isLog( cargo, LogLevel::CALL ) )
357*cdf0e10cSrcweir         {
358*cdf0e10cSrcweir             logException(
359*cdf0e10cSrcweir                 cargo,  "except  uno->py[0x" ,
360*cdf0e10cSrcweir                 mWrappedObject.get(), aFunctionName, &e,getCppuType(&e) );
361*cdf0e10cSrcweir         }
362*cdf0e10cSrcweir         throw;
363*cdf0e10cSrcweir     }
364*cdf0e10cSrcweir     return ret;
365*cdf0e10cSrcweir }
366*cdf0e10cSrcweir 
367*cdf0e10cSrcweir void Adapter::setValue( const OUString & aPropertyName, const Any & value )
368*cdf0e10cSrcweir     throw( UnknownPropertyException, CannotConvertException, InvocationTargetException,RuntimeException)
369*cdf0e10cSrcweir {
370*cdf0e10cSrcweir     PyThreadAttach guard( mInterpreter );
371*cdf0e10cSrcweir     try
372*cdf0e10cSrcweir     {
373*cdf0e10cSrcweir         Runtime runtime;
374*cdf0e10cSrcweir         PyRef obj = runtime.any2PyObject( value );
375*cdf0e10cSrcweir 
376*cdf0e10cSrcweir         if( !hasProperty( aPropertyName ) )
377*cdf0e10cSrcweir         {
378*cdf0e10cSrcweir             OUStringBuffer buf;
379*cdf0e10cSrcweir             buf.appendAscii( "pyuno::Adapater: Property " ).append( aPropertyName );
380*cdf0e10cSrcweir             buf.appendAscii( " is unknown." );
381*cdf0e10cSrcweir             throw UnknownPropertyException( buf.makeStringAndClear(), Reference< XInterface > () );
382*cdf0e10cSrcweir         }
383*cdf0e10cSrcweir 
384*cdf0e10cSrcweir         PyObject_SetAttrString(
385*cdf0e10cSrcweir             mWrappedObject.get(), (char*)TO_ASCII(aPropertyName), obj.get() );
386*cdf0e10cSrcweir         raiseInvocationTargetExceptionWhenNeeded( runtime);
387*cdf0e10cSrcweir 
388*cdf0e10cSrcweir     }
389*cdf0e10cSrcweir     catch( IllegalArgumentException & exc )
390*cdf0e10cSrcweir     {
391*cdf0e10cSrcweir         throw InvocationTargetException( exc.Message, *this, com::sun::star::uno::makeAny( exc ) );
392*cdf0e10cSrcweir     }
393*cdf0e10cSrcweir }
394*cdf0e10cSrcweir 
395*cdf0e10cSrcweir Any Adapter::getValue( const OUString & aPropertyName )
396*cdf0e10cSrcweir     throw ( UnknownPropertyException, RuntimeException )
397*cdf0e10cSrcweir {
398*cdf0e10cSrcweir     Any ret;
399*cdf0e10cSrcweir     PyThreadAttach guard( mInterpreter );
400*cdf0e10cSrcweir     {
401*cdf0e10cSrcweir         Runtime runtime;
402*cdf0e10cSrcweir         PyRef pyRef(
403*cdf0e10cSrcweir             PyObject_GetAttrString( mWrappedObject.get(), (char*)TO_ASCII(aPropertyName) ),
404*cdf0e10cSrcweir             SAL_NO_ACQUIRE );
405*cdf0e10cSrcweir 
406*cdf0e10cSrcweir         raiseInvocationTargetExceptionWhenNeeded( runtime);
407*cdf0e10cSrcweir         if( !pyRef.is() )
408*cdf0e10cSrcweir         {
409*cdf0e10cSrcweir             OUStringBuffer buf;
410*cdf0e10cSrcweir             buf.appendAscii( "pyuno::Adapater: Property " ).append( aPropertyName );
411*cdf0e10cSrcweir             buf.appendAscii( " is unknown." );
412*cdf0e10cSrcweir             throw UnknownPropertyException( buf.makeStringAndClear(), Reference< XInterface > () );
413*cdf0e10cSrcweir         }
414*cdf0e10cSrcweir         ret = runtime.pyObject2Any( pyRef );
415*cdf0e10cSrcweir     }
416*cdf0e10cSrcweir     return ret;
417*cdf0e10cSrcweir }
418*cdf0e10cSrcweir 
419*cdf0e10cSrcweir sal_Bool Adapter::hasMethod( const OUString & aMethodName )
420*cdf0e10cSrcweir     throw ( RuntimeException )
421*cdf0e10cSrcweir {
422*cdf0e10cSrcweir     return hasProperty( aMethodName );
423*cdf0e10cSrcweir }
424*cdf0e10cSrcweir 
425*cdf0e10cSrcweir sal_Bool Adapter::hasProperty( const OUString & aPropertyName )
426*cdf0e10cSrcweir     throw ( RuntimeException )
427*cdf0e10cSrcweir {
428*cdf0e10cSrcweir     bool bRet = false;
429*cdf0e10cSrcweir     PyThreadAttach guard( mInterpreter );
430*cdf0e10cSrcweir     {
431*cdf0e10cSrcweir         bRet = PyObject_HasAttrString(
432*cdf0e10cSrcweir             mWrappedObject.get() , (char*) TO_ASCII( aPropertyName ));
433*cdf0e10cSrcweir     }
434*cdf0e10cSrcweir     return bRet;
435*cdf0e10cSrcweir }
436*cdf0e10cSrcweir 
437*cdf0e10cSrcweir }
438