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 31*cdf0e10cSrcweir #include <typelib/typedescription.hxx> 32*cdf0e10cSrcweir 33*cdf0e10cSrcweir using rtl::OUString; 34*cdf0e10cSrcweir using rtl::OUStringBuffer; 35*cdf0e10cSrcweir using rtl::OUStringToOString; 36*cdf0e10cSrcweir 37*cdf0e10cSrcweir using com::sun::star::uno::RuntimeException; 38*cdf0e10cSrcweir using com::sun::star::uno::Sequence; 39*cdf0e10cSrcweir using com::sun::star::uno::Type; 40*cdf0e10cSrcweir using com::sun::star::uno::Reference; 41*cdf0e10cSrcweir using com::sun::star::uno::XInterface; 42*cdf0e10cSrcweir using com::sun::star::uno::TypeDescription; 43*cdf0e10cSrcweir 44*cdf0e10cSrcweir namespace pyuno 45*cdf0e10cSrcweir { 46*cdf0e10cSrcweir 47*cdf0e10cSrcweir void raisePyExceptionWithAny( const com::sun::star::uno::Any &anyExc ) 48*cdf0e10cSrcweir { 49*cdf0e10cSrcweir try 50*cdf0e10cSrcweir { 51*cdf0e10cSrcweir Runtime runtime; 52*cdf0e10cSrcweir PyRef exc = runtime.any2PyObject( anyExc ); 53*cdf0e10cSrcweir if( exc.is() ) 54*cdf0e10cSrcweir { 55*cdf0e10cSrcweir PyRef type( getClass( anyExc.getValueType().getTypeName(),runtime ) ); 56*cdf0e10cSrcweir PyErr_SetObject( type.get(), exc.get()); 57*cdf0e10cSrcweir } 58*cdf0e10cSrcweir else 59*cdf0e10cSrcweir { 60*cdf0e10cSrcweir com::sun::star::uno::Exception e; 61*cdf0e10cSrcweir anyExc >>= e; 62*cdf0e10cSrcweir 63*cdf0e10cSrcweir OUStringBuffer buf; 64*cdf0e10cSrcweir buf.appendAscii( "Couldn't convert uno exception to a python exception (" ); 65*cdf0e10cSrcweir buf.append(anyExc.getValueType().getTypeName()); 66*cdf0e10cSrcweir buf.appendAscii( ": " ); 67*cdf0e10cSrcweir buf.append(e.Message ); 68*cdf0e10cSrcweir buf.appendAscii( ")" ); 69*cdf0e10cSrcweir PyErr_SetString( 70*cdf0e10cSrcweir PyExc_SystemError, 71*cdf0e10cSrcweir OUStringToOString(buf.makeStringAndClear(),RTL_TEXTENCODING_ASCII_US) ); 72*cdf0e10cSrcweir } 73*cdf0e10cSrcweir } 74*cdf0e10cSrcweir catch( com::sun::star::lang::IllegalArgumentException & e) 75*cdf0e10cSrcweir { 76*cdf0e10cSrcweir PyErr_SetString( PyExc_SystemError, 77*cdf0e10cSrcweir OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US) ); 78*cdf0e10cSrcweir } 79*cdf0e10cSrcweir catch( com::sun::star::script::CannotConvertException & e) 80*cdf0e10cSrcweir { 81*cdf0e10cSrcweir PyErr_SetString( PyExc_SystemError, 82*cdf0e10cSrcweir OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US) ); 83*cdf0e10cSrcweir } 84*cdf0e10cSrcweir catch( RuntimeException & e) 85*cdf0e10cSrcweir { 86*cdf0e10cSrcweir PyErr_SetString( PyExc_SystemError, 87*cdf0e10cSrcweir OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US) ); 88*cdf0e10cSrcweir } 89*cdf0e10cSrcweir } 90*cdf0e10cSrcweir 91*cdf0e10cSrcweir 92*cdf0e10cSrcweir static PyRef createClass( const OUString & name, const Runtime &runtime ) 93*cdf0e10cSrcweir throw ( RuntimeException ) 94*cdf0e10cSrcweir { 95*cdf0e10cSrcweir // assuming that this is never deleted ! 96*cdf0e10cSrcweir // note I don't have the knowledge how to initialize these type objects correctly ! 97*cdf0e10cSrcweir TypeDescription desc( name ); 98*cdf0e10cSrcweir if( ! desc.is() ) 99*cdf0e10cSrcweir { 100*cdf0e10cSrcweir OUStringBuffer buf; 101*cdf0e10cSrcweir buf.appendAscii( "pyuno.getClass: uno exception " ); 102*cdf0e10cSrcweir buf.append(name).appendAscii( " is unknown" ); 103*cdf0e10cSrcweir throw RuntimeException( buf.makeStringAndClear(), Reference< XInterface > () ); 104*cdf0e10cSrcweir } 105*cdf0e10cSrcweir 106*cdf0e10cSrcweir sal_Bool isStruct = desc.get()->eTypeClass == typelib_TypeClass_STRUCT; 107*cdf0e10cSrcweir sal_Bool isExc = desc.get()->eTypeClass == typelib_TypeClass_EXCEPTION; 108*cdf0e10cSrcweir sal_Bool isInterface = desc.get()->eTypeClass == typelib_TypeClass_INTERFACE; 109*cdf0e10cSrcweir if( !isStruct && !isExc && ! isInterface ) 110*cdf0e10cSrcweir { 111*cdf0e10cSrcweir OUStringBuffer buf; 112*cdf0e10cSrcweir buf.appendAscii( "pyuno.getClass: " ).append(name).appendAscii( "is a " ); 113*cdf0e10cSrcweir buf.appendAscii( 114*cdf0e10cSrcweir typeClassToString( (com::sun::star::uno::TypeClass) desc.get()->eTypeClass)); 115*cdf0e10cSrcweir buf.appendAscii( ", expected EXCEPTION, STRUCT or INTERFACE" ); 116*cdf0e10cSrcweir throw RuntimeException( buf.makeStringAndClear(), Reference< XInterface>() ); 117*cdf0e10cSrcweir } 118*cdf0e10cSrcweir 119*cdf0e10cSrcweir // retrieve base class 120*cdf0e10cSrcweir PyRef base; 121*cdf0e10cSrcweir if( isInterface ) 122*cdf0e10cSrcweir { 123*cdf0e10cSrcweir typelib_InterfaceTypeDescription *pDesc = (typelib_InterfaceTypeDescription * )desc.get(); 124*cdf0e10cSrcweir if( pDesc->pBaseTypeDescription ) 125*cdf0e10cSrcweir { 126*cdf0e10cSrcweir base = getClass( pDesc->pBaseTypeDescription->aBase.pTypeName, runtime ); 127*cdf0e10cSrcweir } 128*cdf0e10cSrcweir else 129*cdf0e10cSrcweir { 130*cdf0e10cSrcweir // must be XInterface ! 131*cdf0e10cSrcweir } 132*cdf0e10cSrcweir } 133*cdf0e10cSrcweir else 134*cdf0e10cSrcweir { 135*cdf0e10cSrcweir typelib_CompoundTypeDescription *pDesc = (typelib_CompoundTypeDescription*)desc.get(); 136*cdf0e10cSrcweir if( pDesc->pBaseTypeDescription ) 137*cdf0e10cSrcweir { 138*cdf0e10cSrcweir base = getClass( pDesc->pBaseTypeDescription->aBase.pTypeName, runtime ); 139*cdf0e10cSrcweir } 140*cdf0e10cSrcweir else 141*cdf0e10cSrcweir { 142*cdf0e10cSrcweir if( isExc ) 143*cdf0e10cSrcweir // we are currently creating the root UNO exception 144*cdf0e10cSrcweir base = PyRef(PyExc_Exception); 145*cdf0e10cSrcweir } 146*cdf0e10cSrcweir } 147*cdf0e10cSrcweir PyRef args( PyTuple_New( 3 ), SAL_NO_ACQUIRE ); 148*cdf0e10cSrcweir 149*cdf0e10cSrcweir PyRef pyTypeName = ustring2PyString( name /*.replace( '.', '_' )*/ ); 150*cdf0e10cSrcweir 151*cdf0e10cSrcweir PyRef bases; 152*cdf0e10cSrcweir if( base.is() ) 153*cdf0e10cSrcweir { 154*cdf0e10cSrcweir { // for CC, keeping ref-count being 1 155*cdf0e10cSrcweir bases = PyRef( PyTuple_New( 1 ), SAL_NO_ACQUIRE ); 156*cdf0e10cSrcweir } 157*cdf0e10cSrcweir PyTuple_SetItem( bases.get(), 0 , base.getAcquired() ); 158*cdf0e10cSrcweir } 159*cdf0e10cSrcweir else 160*cdf0e10cSrcweir { 161*cdf0e10cSrcweir bases = PyRef( PyTuple_New( 0 ), SAL_NO_ACQUIRE ); 162*cdf0e10cSrcweir } 163*cdf0e10cSrcweir 164*cdf0e10cSrcweir PyTuple_SetItem( args.get(), 0, pyTypeName.getAcquired()); 165*cdf0e10cSrcweir PyTuple_SetItem( args.get(), 1, bases.getAcquired() ); 166*cdf0e10cSrcweir PyTuple_SetItem( args.get(), 2, PyDict_New() ); 167*cdf0e10cSrcweir 168*cdf0e10cSrcweir PyRef ret( 169*cdf0e10cSrcweir PyObject_CallObject(reinterpret_cast<PyObject *>(&PyClass_Type) , args.get()), 170*cdf0e10cSrcweir SAL_NO_ACQUIRE ); 171*cdf0e10cSrcweir 172*cdf0e10cSrcweir // now overwrite ctor and attrib functions 173*cdf0e10cSrcweir if( isInterface ) 174*cdf0e10cSrcweir { 175*cdf0e10cSrcweir PyObject_SetAttrString( 176*cdf0e10cSrcweir ret.get(), const_cast< char * >("__pyunointerface__"), 177*cdf0e10cSrcweir ustring2PyString(name).get() ); 178*cdf0e10cSrcweir } 179*cdf0e10cSrcweir else 180*cdf0e10cSrcweir { 181*cdf0e10cSrcweir PyRef ctor = getObjectFromUnoModule( runtime,"_uno_struct__init__" ); 182*cdf0e10cSrcweir PyRef setter = getObjectFromUnoModule( runtime,"_uno_struct__setattr__" ); 183*cdf0e10cSrcweir PyRef getter = getObjectFromUnoModule( runtime,"_uno_struct__getattr__" ); 184*cdf0e10cSrcweir PyRef repr = getObjectFromUnoModule( runtime,"_uno_struct__repr__" ); 185*cdf0e10cSrcweir PyRef eq = getObjectFromUnoModule( runtime,"_uno_struct__eq__" ); 186*cdf0e10cSrcweir 187*cdf0e10cSrcweir PyObject_SetAttrString( 188*cdf0e10cSrcweir ret.get(), const_cast< char * >("__pyunostruct__"), 189*cdf0e10cSrcweir ustring2PyString(name).get() ); 190*cdf0e10cSrcweir PyObject_SetAttrString( 191*cdf0e10cSrcweir ret.get(), const_cast< char * >("typeName"), 192*cdf0e10cSrcweir ustring2PyString(name).get() ); 193*cdf0e10cSrcweir PyObject_SetAttrString( 194*cdf0e10cSrcweir ret.get(), const_cast< char * >("__init__"), ctor.get() ); 195*cdf0e10cSrcweir PyObject_SetAttrString( 196*cdf0e10cSrcweir ret.get(), const_cast< char * >("__getattr__"), getter.get() ); 197*cdf0e10cSrcweir PyObject_SetAttrString( 198*cdf0e10cSrcweir ret.get(), const_cast< char * >("__setattr__"), setter.get() ); 199*cdf0e10cSrcweir PyObject_SetAttrString( 200*cdf0e10cSrcweir ret.get(), const_cast< char * >("__repr__"), repr.get() ); 201*cdf0e10cSrcweir PyObject_SetAttrString( 202*cdf0e10cSrcweir ret.get(), const_cast< char * >("__str__"), repr.get() ); 203*cdf0e10cSrcweir PyObject_SetAttrString( 204*cdf0e10cSrcweir ret.get(), const_cast< char * >("__eq__"), eq.get() ); 205*cdf0e10cSrcweir } 206*cdf0e10cSrcweir return ret; 207*cdf0e10cSrcweir } 208*cdf0e10cSrcweir 209*cdf0e10cSrcweir bool isInstanceOfStructOrException( PyObject *obj) 210*cdf0e10cSrcweir { 211*cdf0e10cSrcweir PyRef attr( 212*cdf0e10cSrcweir PyObject_GetAttrString(obj, const_cast< char * >("__class__")), 213*cdf0e10cSrcweir SAL_NO_ACQUIRE ); 214*cdf0e10cSrcweir return PyObject_HasAttrString( 215*cdf0e10cSrcweir attr.get(), const_cast< char * >("__pyunostruct__")); 216*cdf0e10cSrcweir } 217*cdf0e10cSrcweir 218*cdf0e10cSrcweir sal_Bool isInterfaceClass( const Runtime &runtime, PyObject * obj ) 219*cdf0e10cSrcweir { 220*cdf0e10cSrcweir const ClassSet & set = runtime.getImpl()->cargo->interfaceSet; 221*cdf0e10cSrcweir return set.find( obj ) != set.end(); 222*cdf0e10cSrcweir } 223*cdf0e10cSrcweir 224*cdf0e10cSrcweir PyRef getClass( const OUString & name , const Runtime &runtime) 225*cdf0e10cSrcweir { 226*cdf0e10cSrcweir PyRef ret; 227*cdf0e10cSrcweir 228*cdf0e10cSrcweir RuntimeCargo *cargo =runtime.getImpl()->cargo; 229*cdf0e10cSrcweir ExceptionClassMap::iterator ii = cargo->exceptionMap.find( name ); 230*cdf0e10cSrcweir if( ii == cargo->exceptionMap.end() ) 231*cdf0e10cSrcweir { 232*cdf0e10cSrcweir ret = createClass( name, runtime ); 233*cdf0e10cSrcweir cargo->exceptionMap[name] = ret; 234*cdf0e10cSrcweir if( PyObject_HasAttrString( 235*cdf0e10cSrcweir ret.get(), const_cast< char * >("__pyunointerface__") ) ) 236*cdf0e10cSrcweir cargo->interfaceSet.insert( ret ); 237*cdf0e10cSrcweir 238*cdf0e10cSrcweir PyObject_SetAttrString( 239*cdf0e10cSrcweir ret.get(), const_cast< char * >("__pyunointerface__"), 240*cdf0e10cSrcweir ustring2PyString(name).get() ); 241*cdf0e10cSrcweir } 242*cdf0e10cSrcweir else 243*cdf0e10cSrcweir { 244*cdf0e10cSrcweir ret = ii->second; 245*cdf0e10cSrcweir } 246*cdf0e10cSrcweir 247*cdf0e10cSrcweir return ret; 248*cdf0e10cSrcweir } 249*cdf0e10cSrcweir 250*cdf0e10cSrcweir 251*cdf0e10cSrcweir } 252