1*67c7d1c1SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*67c7d1c1SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*67c7d1c1SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*67c7d1c1SAndrew Rist * distributed with this work for additional information 6*67c7d1c1SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*67c7d1c1SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*67c7d1c1SAndrew Rist * "License"); you may not use this file except in compliance 9*67c7d1c1SAndrew Rist * with the License. You may obtain a copy of the License at 10*67c7d1c1SAndrew Rist * 11*67c7d1c1SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*67c7d1c1SAndrew Rist * 13*67c7d1c1SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*67c7d1c1SAndrew Rist * software distributed under the License is distributed on an 15*67c7d1c1SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*67c7d1c1SAndrew Rist * KIND, either express or implied. See the License for the 17*67c7d1c1SAndrew Rist * specific language governing permissions and limitations 18*67c7d1c1SAndrew Rist * under the License. 19*67c7d1c1SAndrew Rist * 20*67c7d1c1SAndrew Rist *************************************************************/ 21*67c7d1c1SAndrew Rist 22*67c7d1c1SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir #include "pyuno_impl.hxx" 25cdf0e10cSrcweir 26cdf0e10cSrcweir #include <rtl/strbuf.hxx> 27cdf0e10cSrcweir #include <rtl/ustrbuf.hxx> 28cdf0e10cSrcweir 29cdf0e10cSrcweir #include <osl/thread.h> 30cdf0e10cSrcweir 31cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp> 32cdf0e10cSrcweir #include <com/sun/star/lang/XTypeProvider.hpp> 33cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp> 34cdf0e10cSrcweir #include <com/sun/star/beans/XMaterialHolder.hpp> 35cdf0e10cSrcweir 36cdf0e10cSrcweir #define TO_ASCII(x) OUStringToOString( x , RTL_TEXTENCODING_ASCII_US).getStr() 37cdf0e10cSrcweir 38cdf0e10cSrcweir using rtl::OStringBuffer; 39cdf0e10cSrcweir using rtl::OUStringBuffer; 40cdf0e10cSrcweir using rtl::OUStringToOString; 41cdf0e10cSrcweir using rtl::OUString; 42cdf0e10cSrcweir using com::sun::star::uno::Sequence; 43cdf0e10cSrcweir using com::sun::star::uno::Reference; 44cdf0e10cSrcweir using com::sun::star::uno::XInterface; 45cdf0e10cSrcweir using com::sun::star::uno::Any; 46cdf0e10cSrcweir using com::sun::star::uno::makeAny; 47cdf0e10cSrcweir using com::sun::star::uno::UNO_QUERY; 48cdf0e10cSrcweir using com::sun::star::uno::Type; 49cdf0e10cSrcweir using com::sun::star::uno::TypeClass; 50cdf0e10cSrcweir using com::sun::star::uno::RuntimeException; 51cdf0e10cSrcweir using com::sun::star::uno::Exception; 52cdf0e10cSrcweir using com::sun::star::uno::XComponentContext; 53cdf0e10cSrcweir using com::sun::star::lang::XSingleServiceFactory; 54cdf0e10cSrcweir using com::sun::star::lang::XServiceInfo; 55cdf0e10cSrcweir using com::sun::star::lang::XTypeProvider; 56cdf0e10cSrcweir using com::sun::star::script::XTypeConverter; 57cdf0e10cSrcweir using com::sun::star::script::XInvocation2; 58cdf0e10cSrcweir using com::sun::star::beans::XMaterialHolder; 59cdf0e10cSrcweir 60cdf0e10cSrcweir namespace pyuno 61cdf0e10cSrcweir { 62cdf0e10cSrcweir 63cdf0e10cSrcweir PyObject *PyUNO_str( PyObject * self ); 64cdf0e10cSrcweir 65cdf0e10cSrcweir void PyUNO_del (PyObject* self) 66cdf0e10cSrcweir { 67cdf0e10cSrcweir PyUNO* me = reinterpret_cast< PyUNO* > (self); 68cdf0e10cSrcweir { 69cdf0e10cSrcweir PyThreadDetach antiguard; 70cdf0e10cSrcweir delete me->members; 71cdf0e10cSrcweir } 72cdf0e10cSrcweir PyObject_Del (self); 73cdf0e10cSrcweir } 74cdf0e10cSrcweir 75cdf0e10cSrcweir 76cdf0e10cSrcweir 77cdf0e10cSrcweir OUString val2str( const void * pVal, typelib_TypeDescriptionReference * pTypeRef , sal_Int32 mode ) SAL_THROW( () ) 78cdf0e10cSrcweir { 79cdf0e10cSrcweir OSL_ASSERT( pVal ); 80cdf0e10cSrcweir if (pTypeRef->eTypeClass == typelib_TypeClass_VOID) 81cdf0e10cSrcweir return OUString( RTL_CONSTASCII_USTRINGPARAM("void") ); 82cdf0e10cSrcweir 83cdf0e10cSrcweir OUStringBuffer buf( 64 ); 84cdf0e10cSrcweir buf.append( (sal_Unicode)'(' ); 85cdf0e10cSrcweir buf.append( pTypeRef->pTypeName ); 86cdf0e10cSrcweir buf.append( (sal_Unicode)')' ); 87cdf0e10cSrcweir 88cdf0e10cSrcweir switch (pTypeRef->eTypeClass) 89cdf0e10cSrcweir { 90cdf0e10cSrcweir case typelib_TypeClass_INTERFACE: 91cdf0e10cSrcweir { 92cdf0e10cSrcweir buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("0x") ); 93cdf0e10cSrcweir buf.append( reinterpret_cast< sal_IntPtr >(*(void **)pVal), 16 ); 94cdf0e10cSrcweir if( VAL2STR_MODE_DEEP == mode ) 95cdf0e10cSrcweir { 96cdf0e10cSrcweir buf.appendAscii( "{" ); Reference< XInterface > r = *( Reference< XInterface > * ) pVal; 97cdf0e10cSrcweir Reference< XServiceInfo > serviceInfo( r, UNO_QUERY); 98cdf0e10cSrcweir Reference< XTypeProvider > typeProvider(r,UNO_QUERY); 99cdf0e10cSrcweir if( serviceInfo.is() ) 100cdf0e10cSrcweir { 101cdf0e10cSrcweir buf.appendAscii("implementationName=" ); 102cdf0e10cSrcweir buf.append(serviceInfo->getImplementationName() ); 103cdf0e10cSrcweir buf.appendAscii(", supportedServices={" ); 104cdf0e10cSrcweir Sequence< OUString > seq = serviceInfo->getSupportedServiceNames(); 105cdf0e10cSrcweir for( int i = 0 ; i < seq.getLength() ; i ++ ) 106cdf0e10cSrcweir { 107cdf0e10cSrcweir buf.append( seq[i] ); 108cdf0e10cSrcweir if( i +1 != seq.getLength() ) 109cdf0e10cSrcweir buf.appendAscii( "," ); 110cdf0e10cSrcweir } 111cdf0e10cSrcweir buf.appendAscii("}"); 112cdf0e10cSrcweir } 113cdf0e10cSrcweir 114cdf0e10cSrcweir if( typeProvider.is() ) 115cdf0e10cSrcweir { 116cdf0e10cSrcweir buf.appendAscii(", supportedInterfaces={" ); 117cdf0e10cSrcweir Sequence< Type > seq (typeProvider->getTypes()); 118cdf0e10cSrcweir for( int i = 0 ; i < seq.getLength() ; i ++ ) 119cdf0e10cSrcweir { 120cdf0e10cSrcweir buf.append(seq[i].getTypeName()); 121cdf0e10cSrcweir if( i +1 != seq.getLength() ) 122cdf0e10cSrcweir buf.appendAscii( "," ); 123cdf0e10cSrcweir } 124cdf0e10cSrcweir buf.appendAscii("}"); 125cdf0e10cSrcweir } 126cdf0e10cSrcweir buf.appendAscii( "}" ); 127cdf0e10cSrcweir } 128cdf0e10cSrcweir 129cdf0e10cSrcweir break; 130cdf0e10cSrcweir } 131cdf0e10cSrcweir case typelib_TypeClass_UNION: 132cdf0e10cSrcweir { 133cdf0e10cSrcweir // typelib_TypeDescription * pTypeDescr = 0; 134cdf0e10cSrcweir // TYPELIB_DANGER_GET( &pTypeDescr, pTypeRef ); 135cdf0e10cSrcweir // buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("{ ") ); 136cdf0e10cSrcweir // buf.append( val2str( (char *)pVal + ((typelib_UnionTypeDescription *)pTypeDescr)->nValueOffset, 137cdf0e10cSrcweir // union_getSetType( pVal, pTypeDescr ) ) ); 138cdf0e10cSrcweir // buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(" }") ); 139cdf0e10cSrcweir // TYPELIB_DANGER_RELEASE( pTypeDescr ); 140cdf0e10cSrcweir break; 141cdf0e10cSrcweir } 142cdf0e10cSrcweir case typelib_TypeClass_STRUCT: 143cdf0e10cSrcweir case typelib_TypeClass_EXCEPTION: 144cdf0e10cSrcweir { 145cdf0e10cSrcweir buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("{ ") ); 146cdf0e10cSrcweir typelib_TypeDescription * pTypeDescr = 0; 147cdf0e10cSrcweir TYPELIB_DANGER_GET( &pTypeDescr, pTypeRef ); 148cdf0e10cSrcweir OSL_ASSERT( pTypeDescr ); 149cdf0e10cSrcweir 150cdf0e10cSrcweir typelib_CompoundTypeDescription * pCompType = (typelib_CompoundTypeDescription *)pTypeDescr; 151cdf0e10cSrcweir sal_Int32 nDescr = pCompType->nMembers; 152cdf0e10cSrcweir 153cdf0e10cSrcweir if (pCompType->pBaseTypeDescription) 154cdf0e10cSrcweir { 155cdf0e10cSrcweir buf.append( val2str( pVal, ((typelib_TypeDescription *)pCompType->pBaseTypeDescription)->pWeakRef,mode ) ); 156cdf0e10cSrcweir if (nDescr) 157cdf0e10cSrcweir buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(", ") ); 158cdf0e10cSrcweir } 159cdf0e10cSrcweir 160cdf0e10cSrcweir typelib_TypeDescriptionReference ** ppTypeRefs = pCompType->ppTypeRefs; 161cdf0e10cSrcweir sal_Int32 * pMemberOffsets = pCompType->pMemberOffsets; 162cdf0e10cSrcweir rtl_uString ** ppMemberNames = pCompType->ppMemberNames; 163cdf0e10cSrcweir 164cdf0e10cSrcweir for ( sal_Int32 nPos = 0; nPos < nDescr; ++nPos ) 165cdf0e10cSrcweir { 166cdf0e10cSrcweir buf.append( ppMemberNames[nPos] ); 167cdf0e10cSrcweir buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(" = ") ); 168cdf0e10cSrcweir typelib_TypeDescription * pMemberType = 0; 169cdf0e10cSrcweir TYPELIB_DANGER_GET( &pMemberType, ppTypeRefs[nPos] ); 170cdf0e10cSrcweir buf.append( val2str( (char *)pVal + pMemberOffsets[nPos], pMemberType->pWeakRef, mode ) ); 171cdf0e10cSrcweir TYPELIB_DANGER_RELEASE( pMemberType ); 172cdf0e10cSrcweir if (nPos < (nDescr -1)) 173cdf0e10cSrcweir buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(", ") ); 174cdf0e10cSrcweir } 175cdf0e10cSrcweir 176cdf0e10cSrcweir TYPELIB_DANGER_RELEASE( pTypeDescr ); 177cdf0e10cSrcweir 178cdf0e10cSrcweir buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(" }") ); 179cdf0e10cSrcweir break; 180cdf0e10cSrcweir } 181cdf0e10cSrcweir case typelib_TypeClass_SEQUENCE: 182cdf0e10cSrcweir { 183cdf0e10cSrcweir typelib_TypeDescription * pTypeDescr = 0; 184cdf0e10cSrcweir TYPELIB_DANGER_GET( &pTypeDescr, pTypeRef ); 185cdf0e10cSrcweir 186cdf0e10cSrcweir uno_Sequence * pSequence = *(uno_Sequence **)pVal; 187cdf0e10cSrcweir typelib_TypeDescription * pElementTypeDescr = 0; 188cdf0e10cSrcweir TYPELIB_DANGER_GET( &pElementTypeDescr, ((typelib_IndirectTypeDescription *)pTypeDescr)->pType ); 189cdf0e10cSrcweir 190cdf0e10cSrcweir sal_Int32 nElementSize = pElementTypeDescr->nSize; 191cdf0e10cSrcweir sal_Int32 nElements = pSequence->nElements; 192cdf0e10cSrcweir 193cdf0e10cSrcweir if (nElements) 194cdf0e10cSrcweir { 195cdf0e10cSrcweir buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("{ ") ); 196cdf0e10cSrcweir char * pElements = pSequence->elements; 197cdf0e10cSrcweir for ( sal_Int32 nPos = 0; nPos < nElements; ++nPos ) 198cdf0e10cSrcweir { 199cdf0e10cSrcweir buf.append( val2str( pElements + (nElementSize * nPos), pElementTypeDescr->pWeakRef, mode ) ); 200cdf0e10cSrcweir if (nPos < (nElements -1)) 201cdf0e10cSrcweir buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(", ") ); 202cdf0e10cSrcweir } 203cdf0e10cSrcweir buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(" }") ); 204cdf0e10cSrcweir } 205cdf0e10cSrcweir else 206cdf0e10cSrcweir { 207cdf0e10cSrcweir buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("{}") ); 208cdf0e10cSrcweir } 209cdf0e10cSrcweir TYPELIB_DANGER_RELEASE( pElementTypeDescr ); 210cdf0e10cSrcweir TYPELIB_DANGER_RELEASE( pTypeDescr ); 211cdf0e10cSrcweir break; 212cdf0e10cSrcweir } 213cdf0e10cSrcweir case typelib_TypeClass_ANY: 214cdf0e10cSrcweir buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("{ ") ); 215cdf0e10cSrcweir buf.append( val2str( ((uno_Any *)pVal)->pData, 216cdf0e10cSrcweir ((uno_Any *)pVal)->pType , 217cdf0e10cSrcweir mode) ); 218cdf0e10cSrcweir buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(" }") ); 219cdf0e10cSrcweir break; 220cdf0e10cSrcweir case typelib_TypeClass_TYPE: 221cdf0e10cSrcweir buf.append( (*(typelib_TypeDescriptionReference **)pVal)->pTypeName ); 222cdf0e10cSrcweir break; 223cdf0e10cSrcweir case typelib_TypeClass_STRING: 224cdf0e10cSrcweir buf.append( (sal_Unicode)'\"' ); 225cdf0e10cSrcweir buf.append( *(rtl_uString **)pVal ); 226cdf0e10cSrcweir buf.append( (sal_Unicode)'\"' ); 227cdf0e10cSrcweir break; 228cdf0e10cSrcweir case typelib_TypeClass_ENUM: 229cdf0e10cSrcweir { 230cdf0e10cSrcweir typelib_TypeDescription * pTypeDescr = 0; 231cdf0e10cSrcweir TYPELIB_DANGER_GET( &pTypeDescr, pTypeRef ); 232cdf0e10cSrcweir 233cdf0e10cSrcweir sal_Int32 * pValues = ((typelib_EnumTypeDescription *)pTypeDescr)->pEnumValues; 234cdf0e10cSrcweir sal_Int32 nPos = ((typelib_EnumTypeDescription *)pTypeDescr)->nEnumValues; 235cdf0e10cSrcweir while (nPos--) 236cdf0e10cSrcweir { 237cdf0e10cSrcweir if (pValues[nPos] == *(int *)pVal) 238cdf0e10cSrcweir break; 239cdf0e10cSrcweir } 240cdf0e10cSrcweir if (nPos >= 0) 241cdf0e10cSrcweir buf.append( ((typelib_EnumTypeDescription *)pTypeDescr)->ppEnumNames[nPos] ); 242cdf0e10cSrcweir else 243cdf0e10cSrcweir buf.append( (sal_Unicode)'?' ); 244cdf0e10cSrcweir 245cdf0e10cSrcweir TYPELIB_DANGER_RELEASE( pTypeDescr ); 246cdf0e10cSrcweir break; 247cdf0e10cSrcweir } 248cdf0e10cSrcweir case typelib_TypeClass_BOOLEAN: 249cdf0e10cSrcweir if (*(sal_Bool *)pVal) 250cdf0e10cSrcweir buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("true") ); 251cdf0e10cSrcweir else 252cdf0e10cSrcweir buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("false") ); 253cdf0e10cSrcweir break; 254cdf0e10cSrcweir case typelib_TypeClass_CHAR: 255cdf0e10cSrcweir buf.append( (sal_Unicode)'\'' ); 256cdf0e10cSrcweir buf.append( *(sal_Unicode *)pVal ); 257cdf0e10cSrcweir buf.append( (sal_Unicode)'\'' ); 258cdf0e10cSrcweir break; 259cdf0e10cSrcweir case typelib_TypeClass_FLOAT: 260cdf0e10cSrcweir buf.append( *(float *)pVal ); 261cdf0e10cSrcweir break; 262cdf0e10cSrcweir case typelib_TypeClass_DOUBLE: 263cdf0e10cSrcweir buf.append( *(double *)pVal ); 264cdf0e10cSrcweir break; 265cdf0e10cSrcweir case typelib_TypeClass_BYTE: 266cdf0e10cSrcweir buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("0x") ); 267cdf0e10cSrcweir buf.append( (sal_Int32)*(sal_Int8 *)pVal, 16 ); 268cdf0e10cSrcweir break; 269cdf0e10cSrcweir case typelib_TypeClass_SHORT: 270cdf0e10cSrcweir buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("0x") ); 271cdf0e10cSrcweir buf.append( (sal_Int32)*(sal_Int16 *)pVal, 16 ); 272cdf0e10cSrcweir break; 273cdf0e10cSrcweir case typelib_TypeClass_UNSIGNED_SHORT: 274cdf0e10cSrcweir buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("0x") ); 275cdf0e10cSrcweir buf.append( (sal_Int32)*(sal_uInt16 *)pVal, 16 ); 276cdf0e10cSrcweir break; 277cdf0e10cSrcweir case typelib_TypeClass_LONG: 278cdf0e10cSrcweir buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("0x") ); 279cdf0e10cSrcweir buf.append( *(sal_Int32 *)pVal, 16 ); 280cdf0e10cSrcweir break; 281cdf0e10cSrcweir case typelib_TypeClass_UNSIGNED_LONG: 282cdf0e10cSrcweir buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("0x") ); 283cdf0e10cSrcweir buf.append( (sal_Int64)*(sal_uInt32 *)pVal, 16 ); 284cdf0e10cSrcweir break; 285cdf0e10cSrcweir case typelib_TypeClass_HYPER: 286cdf0e10cSrcweir case typelib_TypeClass_UNSIGNED_HYPER: 287cdf0e10cSrcweir buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("0x") ); 288cdf0e10cSrcweir #if defined(GCC) && defined(SPARC) 289cdf0e10cSrcweir { 290cdf0e10cSrcweir sal_Int64 aVal; 291cdf0e10cSrcweir *(sal_Int32 *)&aVal = *(sal_Int32 *)pVal; 292cdf0e10cSrcweir *((sal_Int32 *)&aVal +1)= *((sal_Int32 *)pVal +1); 293cdf0e10cSrcweir buf.append( aVal, 16 ); 294cdf0e10cSrcweir } 295cdf0e10cSrcweir #else 296cdf0e10cSrcweir buf.append( *(sal_Int64 *)pVal, 16 ); 297cdf0e10cSrcweir #endif 298cdf0e10cSrcweir break; 299cdf0e10cSrcweir 300cdf0e10cSrcweir case typelib_TypeClass_VOID: 301cdf0e10cSrcweir case typelib_TypeClass_ARRAY: 302cdf0e10cSrcweir case typelib_TypeClass_UNKNOWN: 303cdf0e10cSrcweir case typelib_TypeClass_SERVICE: 304cdf0e10cSrcweir case typelib_TypeClass_MODULE: 305cdf0e10cSrcweir default: 306cdf0e10cSrcweir buf.append( (sal_Unicode)'?' ); 307cdf0e10cSrcweir } 308cdf0e10cSrcweir 309cdf0e10cSrcweir return buf.makeStringAndClear(); 310cdf0e10cSrcweir } 311cdf0e10cSrcweir 312cdf0e10cSrcweir 313cdf0e10cSrcweir PyObject *PyUNO_repr( PyObject * self ) 314cdf0e10cSrcweir { 315cdf0e10cSrcweir PyUNO *me = (PyUNO * ) self; 316cdf0e10cSrcweir PyObject * ret = 0; 317cdf0e10cSrcweir 318cdf0e10cSrcweir if( me->members->wrappedObject.getValueType().getTypeClass() 319cdf0e10cSrcweir == com::sun::star::uno::TypeClass_EXCEPTION ) 320cdf0e10cSrcweir { 321cdf0e10cSrcweir Reference< XMaterialHolder > rHolder(me->members->xInvocation,UNO_QUERY); 322cdf0e10cSrcweir if( rHolder.is() ) 323cdf0e10cSrcweir { 324cdf0e10cSrcweir Any a = rHolder->getMaterial(); 325cdf0e10cSrcweir Exception e; 326cdf0e10cSrcweir a >>= e; 327cdf0e10cSrcweir ret = ustring2PyUnicode(e.Message ).getAcquired(); 328cdf0e10cSrcweir } 329cdf0e10cSrcweir } 330cdf0e10cSrcweir else 331cdf0e10cSrcweir { 332cdf0e10cSrcweir ret = PyUNO_str( self ); 333cdf0e10cSrcweir } 334cdf0e10cSrcweir return ret; 335cdf0e10cSrcweir } 336cdf0e10cSrcweir 337cdf0e10cSrcweir PyObject *PyUNO_invoke( PyObject *object, const char *name , PyObject *args ) 338cdf0e10cSrcweir { 339cdf0e10cSrcweir PyRef ret; 340cdf0e10cSrcweir try 341cdf0e10cSrcweir { 342cdf0e10cSrcweir Runtime runtime; 343cdf0e10cSrcweir 344cdf0e10cSrcweir PyRef paras,callable; 345cdf0e10cSrcweir if( PyObject_IsInstance( object, getPyUnoClass( runtime ).get() ) ) 346cdf0e10cSrcweir { 347cdf0e10cSrcweir PyUNO* me = (PyUNO*) object; 348cdf0e10cSrcweir OUString attrName = OUString::createFromAscii(name); 349cdf0e10cSrcweir if (! me->members->xInvocation->hasMethod (attrName)) 350cdf0e10cSrcweir { 351cdf0e10cSrcweir OUStringBuffer buf; 352cdf0e10cSrcweir buf.appendAscii( "Attribute " ); 353cdf0e10cSrcweir buf.append( attrName ); 354cdf0e10cSrcweir buf.appendAscii( " unknown" ); 355cdf0e10cSrcweir throw RuntimeException( buf.makeStringAndClear(), Reference< XInterface > () ); 356cdf0e10cSrcweir } 357cdf0e10cSrcweir callable = PyUNO_callable_new ( 358cdf0e10cSrcweir me->members->xInvocation, 359cdf0e10cSrcweir attrName, 360cdf0e10cSrcweir runtime.getImpl()->cargo->xInvocation, 361cdf0e10cSrcweir runtime.getImpl()->cargo->xTypeConverter, 362cdf0e10cSrcweir ACCEPT_UNO_ANY); 363cdf0e10cSrcweir paras = args; 364cdf0e10cSrcweir } 365cdf0e10cSrcweir else 366cdf0e10cSrcweir { 367cdf0e10cSrcweir // clean the tuple from uno.Any ! 368cdf0e10cSrcweir int size = PyTuple_Size( args ); 369cdf0e10cSrcweir { // for CC, keeping ref-count of tuple being 1 370cdf0e10cSrcweir paras = PyRef(PyTuple_New( size ), SAL_NO_ACQUIRE); 371cdf0e10cSrcweir } 372cdf0e10cSrcweir for( int i = 0 ; i < size ;i ++ ) 373cdf0e10cSrcweir { 374cdf0e10cSrcweir PyObject * element = PyTuple_GetItem( args , i ); 375cdf0e10cSrcweir if( PyObject_IsInstance( element , getAnyClass( runtime ).get() ) ) 376cdf0e10cSrcweir { 377cdf0e10cSrcweir element = PyObject_GetAttrString( 378cdf0e10cSrcweir element, const_cast< char * >("value") ); 379cdf0e10cSrcweir } 380cdf0e10cSrcweir else 381cdf0e10cSrcweir { 382cdf0e10cSrcweir Py_XINCREF( element ); 383cdf0e10cSrcweir } 384cdf0e10cSrcweir PyTuple_SetItem( paras.get(), i , element ); 385cdf0e10cSrcweir } 386cdf0e10cSrcweir callable = PyRef( PyObject_GetAttrString( object , (char*)name ), SAL_NO_ACQUIRE ); 387cdf0e10cSrcweir if( !callable.is() ) 388cdf0e10cSrcweir return 0; 389cdf0e10cSrcweir } 390cdf0e10cSrcweir ret = PyRef( PyObject_CallObject( callable.get(), paras.get() ), SAL_NO_ACQUIRE ); 391cdf0e10cSrcweir } 392cdf0e10cSrcweir catch (::com::sun::star::lang::IllegalArgumentException &e) 393cdf0e10cSrcweir { 394cdf0e10cSrcweir raisePyExceptionWithAny( com::sun::star::uno::makeAny( e ) ); 395cdf0e10cSrcweir } 396cdf0e10cSrcweir catch (::com::sun::star::script::CannotConvertException &e) 397cdf0e10cSrcweir { 398cdf0e10cSrcweir raisePyExceptionWithAny( com::sun::star::uno::makeAny( e ) ); 399cdf0e10cSrcweir } 400cdf0e10cSrcweir catch (::com::sun::star::uno::RuntimeException &e) 401cdf0e10cSrcweir { 402cdf0e10cSrcweir raisePyExceptionWithAny( com::sun::star::uno::makeAny( e ) ); 403cdf0e10cSrcweir } 404cdf0e10cSrcweir catch (::com::sun::star::uno::Exception &e) 405cdf0e10cSrcweir { 406cdf0e10cSrcweir raisePyExceptionWithAny( com::sun::star::uno::makeAny( e ) ); 407cdf0e10cSrcweir } 408cdf0e10cSrcweir 409cdf0e10cSrcweir return ret.getAcquired(); 410cdf0e10cSrcweir } 411cdf0e10cSrcweir 412cdf0e10cSrcweir PyObject *PyUNO_str( PyObject * self ) 413cdf0e10cSrcweir { 414cdf0e10cSrcweir PyUNO *me = ( PyUNO * ) self; 415cdf0e10cSrcweir 416cdf0e10cSrcweir OStringBuffer buf; 417cdf0e10cSrcweir 418cdf0e10cSrcweir 419cdf0e10cSrcweir if( me->members->wrappedObject.getValueType().getTypeClass() 420cdf0e10cSrcweir == com::sun::star::uno::TypeClass_STRUCT || 421cdf0e10cSrcweir me->members->wrappedObject.getValueType().getTypeClass() 422cdf0e10cSrcweir == com::sun::star::uno::TypeClass_EXCEPTION) 423cdf0e10cSrcweir { 424cdf0e10cSrcweir Reference< XMaterialHolder > rHolder(me->members->xInvocation,UNO_QUERY); 425cdf0e10cSrcweir if( rHolder.is() ) 426cdf0e10cSrcweir { 427cdf0e10cSrcweir PyThreadDetach antiguard; 428cdf0e10cSrcweir Any a = rHolder->getMaterial(); 429cdf0e10cSrcweir OUString s = val2str( (void*) a.getValue(), a.getValueType().getTypeLibType() ); 430cdf0e10cSrcweir buf.append( OUStringToOString(s,RTL_TEXTENCODING_ASCII_US) ); 431cdf0e10cSrcweir } 432cdf0e10cSrcweir } 433cdf0e10cSrcweir else 434cdf0e10cSrcweir { 435cdf0e10cSrcweir // a common UNO object 436cdf0e10cSrcweir PyThreadDetach antiguard; 437cdf0e10cSrcweir buf.append( "pyuno object " ); 438cdf0e10cSrcweir 439cdf0e10cSrcweir OUString s = val2str( (void*)me->members->wrappedObject.getValue(), 440cdf0e10cSrcweir me->members->wrappedObject.getValueType().getTypeLibType() ); 441cdf0e10cSrcweir buf.append( OUStringToOString(s,RTL_TEXTENCODING_ASCII_US) ); 442cdf0e10cSrcweir } 443cdf0e10cSrcweir 444cdf0e10cSrcweir return PyString_FromString( buf.getStr()); 445cdf0e10cSrcweir } 446cdf0e10cSrcweir 447cdf0e10cSrcweir PyObject* PyUNO_getattr (PyObject* self, char* name) 448cdf0e10cSrcweir { 449cdf0e10cSrcweir PyUNO* me; 450cdf0e10cSrcweir 451cdf0e10cSrcweir try 452cdf0e10cSrcweir { 453cdf0e10cSrcweir 454cdf0e10cSrcweir Runtime runtime; 455cdf0e10cSrcweir 456cdf0e10cSrcweir me = (PyUNO*) self; 457cdf0e10cSrcweir //Handle Python dir () stuff first... 458cdf0e10cSrcweir if (strcmp (name, "__members__") == 0) 459cdf0e10cSrcweir { 460cdf0e10cSrcweir PyObject* member_list; 461cdf0e10cSrcweir Sequence<OUString> oo_member_list; 462cdf0e10cSrcweir 463cdf0e10cSrcweir oo_member_list = me->members->xInvocation->getMemberNames (); 464cdf0e10cSrcweir member_list = PyList_New (oo_member_list.getLength ()); 465cdf0e10cSrcweir for (int i = 0; i < oo_member_list.getLength (); i++) 466cdf0e10cSrcweir { 467cdf0e10cSrcweir // setitem steals a reference 468cdf0e10cSrcweir PyList_SetItem (member_list, i, ustring2PyString(oo_member_list[i]).getAcquired() ); 469cdf0e10cSrcweir } 470cdf0e10cSrcweir return member_list; 471cdf0e10cSrcweir } 472cdf0e10cSrcweir 473cdf0e10cSrcweir if (strcmp (name, "__dict__") == 0) 474cdf0e10cSrcweir { 475cdf0e10cSrcweir Py_INCREF (Py_None); 476cdf0e10cSrcweir return Py_None; 477cdf0e10cSrcweir } 478cdf0e10cSrcweir if (strcmp (name, "__methods__") == 0) 479cdf0e10cSrcweir { 480cdf0e10cSrcweir Py_INCREF (Py_None); 481cdf0e10cSrcweir return Py_None; 482cdf0e10cSrcweir } 483cdf0e10cSrcweir if (strcmp (name, "__class__") == 0) 484cdf0e10cSrcweir { 485cdf0e10cSrcweir if( me->members->wrappedObject.getValueTypeClass() == 486cdf0e10cSrcweir com::sun::star::uno::TypeClass_STRUCT || 487cdf0e10cSrcweir me->members->wrappedObject.getValueTypeClass() == 488cdf0e10cSrcweir com::sun::star::uno::TypeClass_EXCEPTION ) 489cdf0e10cSrcweir { 490cdf0e10cSrcweir return getClass( 491cdf0e10cSrcweir me->members->wrappedObject.getValueType().getTypeName(), runtime ).getAcquired(); 492cdf0e10cSrcweir } 493cdf0e10cSrcweir Py_INCREF (Py_None); 494cdf0e10cSrcweir return Py_None; 495cdf0e10cSrcweir } 496cdf0e10cSrcweir 497cdf0e10cSrcweir OUString attrName( OUString::createFromAscii( name ) ); 498cdf0e10cSrcweir //We need to find out if it's a method... 499cdf0e10cSrcweir if (me->members->xInvocation->hasMethod (attrName)) 500cdf0e10cSrcweir { 501cdf0e10cSrcweir //Create a callable object to invoke this... 502cdf0e10cSrcweir PyRef ret = PyUNO_callable_new ( 503cdf0e10cSrcweir me->members->xInvocation, 504cdf0e10cSrcweir attrName, 505cdf0e10cSrcweir runtime.getImpl()->cargo->xInvocation, 506cdf0e10cSrcweir runtime.getImpl()->cargo->xTypeConverter); 507cdf0e10cSrcweir Py_XINCREF( ret.get() ); 508cdf0e10cSrcweir return ret.get(); 509cdf0e10cSrcweir 510cdf0e10cSrcweir } 511cdf0e10cSrcweir 512cdf0e10cSrcweir //or a property 513cdf0e10cSrcweir if (me->members->xInvocation->hasProperty ( attrName)) 514cdf0e10cSrcweir { 515cdf0e10cSrcweir //Return the value of the property 516cdf0e10cSrcweir Any anyRet; 517cdf0e10cSrcweir { 518cdf0e10cSrcweir PyThreadDetach antiguard; 519cdf0e10cSrcweir anyRet = me->members->xInvocation->getValue (attrName); 520cdf0e10cSrcweir } 521cdf0e10cSrcweir PyRef ret = runtime.any2PyObject(anyRet); 522cdf0e10cSrcweir Py_XINCREF( ret.get() ); 523cdf0e10cSrcweir return ret.get(); 524cdf0e10cSrcweir } 525cdf0e10cSrcweir 526cdf0e10cSrcweir //or else... 527cdf0e10cSrcweir PyErr_SetString (PyExc_AttributeError, name); 528cdf0e10cSrcweir } 529cdf0e10cSrcweir catch( com::sun::star::reflection::InvocationTargetException & e ) 530cdf0e10cSrcweir { 531cdf0e10cSrcweir raisePyExceptionWithAny( makeAny(e.TargetException) ); 532cdf0e10cSrcweir } 533cdf0e10cSrcweir catch( com::sun::star::beans::UnknownPropertyException & e ) 534cdf0e10cSrcweir { 535cdf0e10cSrcweir raisePyExceptionWithAny( makeAny(e) ); 536cdf0e10cSrcweir } 537cdf0e10cSrcweir catch( com::sun::star::lang::IllegalArgumentException &e ) 538cdf0e10cSrcweir { 539cdf0e10cSrcweir raisePyExceptionWithAny( makeAny(e) ); 540cdf0e10cSrcweir } 541cdf0e10cSrcweir catch( com::sun::star::script::CannotConvertException &e ) 542cdf0e10cSrcweir { 543cdf0e10cSrcweir raisePyExceptionWithAny( makeAny(e) ); 544cdf0e10cSrcweir } 545cdf0e10cSrcweir catch( RuntimeException &e ) 546cdf0e10cSrcweir { 547cdf0e10cSrcweir raisePyExceptionWithAny( makeAny(e) ); 548cdf0e10cSrcweir } 549cdf0e10cSrcweir 550cdf0e10cSrcweir return NULL; 551cdf0e10cSrcweir } 552cdf0e10cSrcweir 553cdf0e10cSrcweir int PyUNO_setattr (PyObject* self, char* name, PyObject* value) 554cdf0e10cSrcweir { 555cdf0e10cSrcweir PyUNO* me; 556cdf0e10cSrcweir 557cdf0e10cSrcweir me = (PyUNO*) self; 558cdf0e10cSrcweir try 559cdf0e10cSrcweir { 560cdf0e10cSrcweir Runtime runtime; 561cdf0e10cSrcweir Any val= runtime.pyObject2Any(value, ACCEPT_UNO_ANY); 562cdf0e10cSrcweir 563cdf0e10cSrcweir OUString attrName( OUString::createFromAscii( name ) ); 564cdf0e10cSrcweir { 565cdf0e10cSrcweir PyThreadDetach antiguard; 566cdf0e10cSrcweir if (me->members->xInvocation->hasProperty (attrName)) 567cdf0e10cSrcweir { 568cdf0e10cSrcweir me->members->xInvocation->setValue (attrName, val); 569cdf0e10cSrcweir return 0; //Keep with Python's boolean system 570cdf0e10cSrcweir } 571cdf0e10cSrcweir } 572cdf0e10cSrcweir } 573cdf0e10cSrcweir catch( com::sun::star::reflection::InvocationTargetException & e ) 574cdf0e10cSrcweir { 575cdf0e10cSrcweir raisePyExceptionWithAny( makeAny(e.TargetException) ); 576cdf0e10cSrcweir return 1; 577cdf0e10cSrcweir } 578cdf0e10cSrcweir catch( com::sun::star::beans::UnknownPropertyException & e ) 579cdf0e10cSrcweir { 580cdf0e10cSrcweir raisePyExceptionWithAny( makeAny(e) ); 581cdf0e10cSrcweir return 1; 582cdf0e10cSrcweir } 583cdf0e10cSrcweir catch( com::sun::star::script::CannotConvertException &e ) 584cdf0e10cSrcweir { 585cdf0e10cSrcweir raisePyExceptionWithAny( makeAny(e) ); 586cdf0e10cSrcweir return 1; 587cdf0e10cSrcweir } 588cdf0e10cSrcweir catch( RuntimeException & e ) 589cdf0e10cSrcweir { 590cdf0e10cSrcweir raisePyExceptionWithAny( makeAny( e ) ); 591cdf0e10cSrcweir return 1; 592cdf0e10cSrcweir } 593cdf0e10cSrcweir PyErr_SetString (PyExc_AttributeError, name); 594cdf0e10cSrcweir return 1; //as above. 595cdf0e10cSrcweir } 596cdf0e10cSrcweir 597cdf0e10cSrcweir // ensure object identity and struct equality 598cdf0e10cSrcweir static int PyUNO_cmp( PyObject *self, PyObject *that ) 599cdf0e10cSrcweir { 600cdf0e10cSrcweir if( self == that ) 601cdf0e10cSrcweir return 0; 602cdf0e10cSrcweir int retDefault = self > that ? 1 : -1; 603cdf0e10cSrcweir try 604cdf0e10cSrcweir { 605cdf0e10cSrcweir Runtime runtime; 606cdf0e10cSrcweir if( PyObject_IsInstance( that, getPyUnoClass( runtime ).get() ) ) 607cdf0e10cSrcweir { 608cdf0e10cSrcweir 609cdf0e10cSrcweir PyUNO *me = reinterpret_cast< PyUNO*> ( self ); 610cdf0e10cSrcweir PyUNO *other = reinterpret_cast< PyUNO *> (that ); 611cdf0e10cSrcweir com::sun::star::uno::TypeClass tcMe = me->members->wrappedObject.getValueTypeClass(); 612cdf0e10cSrcweir com::sun::star::uno::TypeClass tcOther = other->members->wrappedObject.getValueTypeClass(); 613cdf0e10cSrcweir 614cdf0e10cSrcweir if( tcMe == tcOther ) 615cdf0e10cSrcweir { 616cdf0e10cSrcweir if( tcMe == com::sun::star::uno::TypeClass_STRUCT || 617cdf0e10cSrcweir tcMe == com::sun::star::uno::TypeClass_EXCEPTION ) 618cdf0e10cSrcweir { 619cdf0e10cSrcweir Reference< XMaterialHolder > xMe( me->members->xInvocation,UNO_QUERY); 620cdf0e10cSrcweir Reference< XMaterialHolder > xOther( other->members->xInvocation,UNO_QUERY ); 621cdf0e10cSrcweir if( xMe->getMaterial() == xOther->getMaterial() ) 622cdf0e10cSrcweir return 0; 623cdf0e10cSrcweir } 624cdf0e10cSrcweir else if( tcMe == com::sun::star::uno::TypeClass_INTERFACE ) 625cdf0e10cSrcweir { 626cdf0e10cSrcweir if( me->members->wrappedObject == other->members->wrappedObject ) 627cdf0e10cSrcweir // if( me->members->xInvocation == other->members->xInvocation ) 628cdf0e10cSrcweir return 0; 629cdf0e10cSrcweir } 630cdf0e10cSrcweir } 631cdf0e10cSrcweir } 632cdf0e10cSrcweir } 633cdf0e10cSrcweir catch( com::sun::star::uno::RuntimeException & e) 634cdf0e10cSrcweir { 635cdf0e10cSrcweir raisePyExceptionWithAny( makeAny( e ) ); 636cdf0e10cSrcweir } 637cdf0e10cSrcweir return retDefault; 638cdf0e10cSrcweir } 639cdf0e10cSrcweir 640cdf0e10cSrcweir static PyTypeObject PyUNOType = 641cdf0e10cSrcweir { 642cdf0e10cSrcweir PyObject_HEAD_INIT (&PyType_Type) 643cdf0e10cSrcweir 0, 644cdf0e10cSrcweir const_cast< char * >("pyuno"), 645cdf0e10cSrcweir sizeof (PyUNO), 646cdf0e10cSrcweir 0, 647cdf0e10cSrcweir (destructor) PyUNO_del, 648cdf0e10cSrcweir (printfunc) 0, 649cdf0e10cSrcweir (getattrfunc) PyUNO_getattr, 650cdf0e10cSrcweir (setattrfunc) PyUNO_setattr, 651cdf0e10cSrcweir (cmpfunc) PyUNO_cmp, 652cdf0e10cSrcweir (reprfunc) PyUNO_repr, 653cdf0e10cSrcweir 0, 654cdf0e10cSrcweir 0, 655cdf0e10cSrcweir 0, 656cdf0e10cSrcweir (hashfunc) 0, 657cdf0e10cSrcweir (ternaryfunc) 0, 658cdf0e10cSrcweir (reprfunc) PyUNO_str, 659cdf0e10cSrcweir (getattrofunc)0, 660cdf0e10cSrcweir (setattrofunc)0, 661cdf0e10cSrcweir NULL, 662cdf0e10cSrcweir 0, 663cdf0e10cSrcweir NULL, 664cdf0e10cSrcweir (traverseproc)0, 665cdf0e10cSrcweir (inquiry)0, 666cdf0e10cSrcweir (richcmpfunc)0, 667cdf0e10cSrcweir 0, 668cdf0e10cSrcweir (getiterfunc)0, 669cdf0e10cSrcweir (iternextfunc)0, 670cdf0e10cSrcweir NULL, 671cdf0e10cSrcweir NULL, 672cdf0e10cSrcweir NULL, 673cdf0e10cSrcweir NULL, 674cdf0e10cSrcweir NULL, 675cdf0e10cSrcweir (descrgetfunc)0, 676cdf0e10cSrcweir (descrsetfunc)0, 677cdf0e10cSrcweir 0, 678cdf0e10cSrcweir (initproc)0, 679cdf0e10cSrcweir (allocfunc)0, 680cdf0e10cSrcweir (newfunc)0, 681cdf0e10cSrcweir (freefunc)0, 682cdf0e10cSrcweir (inquiry)0, 683cdf0e10cSrcweir NULL, 684cdf0e10cSrcweir NULL, 685cdf0e10cSrcweir NULL, 686cdf0e10cSrcweir NULL, 687cdf0e10cSrcweir NULL, 688cdf0e10cSrcweir (destructor)0 689cdf0e10cSrcweir #if PY_VERSION_HEX >= 0x02060000 690cdf0e10cSrcweir , 0 691cdf0e10cSrcweir #endif 692cdf0e10cSrcweir }; 693cdf0e10cSrcweir 694cdf0e10cSrcweir PyRef getPyUnoClass( const Runtime &) 695cdf0e10cSrcweir { 696cdf0e10cSrcweir return PyRef( reinterpret_cast< PyObject * > ( &PyUNOType ) ); 697cdf0e10cSrcweir } 698cdf0e10cSrcweir 699cdf0e10cSrcweir PyObject* PyUNO_new ( 700cdf0e10cSrcweir const Any & targetInterface, const Reference<XSingleServiceFactory> &ssf) 701cdf0e10cSrcweir { 702cdf0e10cSrcweir Reference<XInterface> tmp_interface; 703cdf0e10cSrcweir 704cdf0e10cSrcweir targetInterface >>= tmp_interface; 705cdf0e10cSrcweir if (!tmp_interface.is ()) 706cdf0e10cSrcweir { 707cdf0e10cSrcweir // empty reference ! 708cdf0e10cSrcweir Py_INCREF( Py_None ); 709cdf0e10cSrcweir return Py_None; 710cdf0e10cSrcweir } 711cdf0e10cSrcweir 712cdf0e10cSrcweir return PyUNO_new_UNCHECKED (targetInterface, ssf); 713cdf0e10cSrcweir } 714cdf0e10cSrcweir 715cdf0e10cSrcweir 716cdf0e10cSrcweir PyObject* PyUNO_new_UNCHECKED ( 717cdf0e10cSrcweir const Any &targetInterface, 718cdf0e10cSrcweir const Reference<XSingleServiceFactory> &ssf ) 719cdf0e10cSrcweir { 720cdf0e10cSrcweir PyUNO* self; 721cdf0e10cSrcweir Sequence<Any> arguments (1); 722cdf0e10cSrcweir Reference<XInterface> tmp_interface; 723cdf0e10cSrcweir 724cdf0e10cSrcweir self = PyObject_New (PyUNO, &PyUNOType); 725cdf0e10cSrcweir if (self == NULL) 726cdf0e10cSrcweir return NULL; //NULL == error 727cdf0e10cSrcweir self->members = new PyUNOInternals(); 728cdf0e10cSrcweir 729cdf0e10cSrcweir arguments[0] <<= targetInterface; 730cdf0e10cSrcweir { 731cdf0e10cSrcweir PyThreadDetach antiguard; 732cdf0e10cSrcweir tmp_interface = ssf->createInstanceWithArguments (arguments); 733cdf0e10cSrcweir Reference<XInvocation2> tmp_invocation (tmp_interface, UNO_QUERY); 734cdf0e10cSrcweir self->members->xInvocation = tmp_invocation; 735cdf0e10cSrcweir self->members->wrappedObject = targetInterface; 736cdf0e10cSrcweir } 737cdf0e10cSrcweir return (PyObject*) self; 738cdf0e10cSrcweir } 739cdf0e10cSrcweir 740cdf0e10cSrcweir } 741