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 28*cdf0e10cSrcweir #include "pyuno_impl.hxx" 29*cdf0e10cSrcweir 30*cdf0e10cSrcweir #include <osl/thread.h> 31*cdf0e10cSrcweir #include <osl/module.h> 32*cdf0e10cSrcweir #include <osl/process.h> 33*cdf0e10cSrcweir #include <rtl/strbuf.hxx> 34*cdf0e10cSrcweir #include <rtl/ustrbuf.hxx> 35*cdf0e10cSrcweir #include <rtl/bootstrap.hxx> 36*cdf0e10cSrcweir #include <locale.h> 37*cdf0e10cSrcweir 38*cdf0e10cSrcweir #include <typelib/typedescription.hxx> 39*cdf0e10cSrcweir 40*cdf0e10cSrcweir #include <com/sun/star/beans/XMaterialHolder.hpp> 41*cdf0e10cSrcweir 42*cdf0e10cSrcweir using rtl::OUString; 43*cdf0e10cSrcweir using rtl::OUStringToOString; 44*cdf0e10cSrcweir using rtl::OUStringBuffer; 45*cdf0e10cSrcweir using rtl::OStringBuffer; 46*cdf0e10cSrcweir using rtl::OString; 47*cdf0e10cSrcweir 48*cdf0e10cSrcweir using com::sun::star::uno::Reference; 49*cdf0e10cSrcweir using com::sun::star::uno::XInterface; 50*cdf0e10cSrcweir using com::sun::star::uno::Any; 51*cdf0e10cSrcweir using com::sun::star::uno::TypeDescription; 52*cdf0e10cSrcweir using com::sun::star::uno::Sequence; 53*cdf0e10cSrcweir using com::sun::star::uno::Type; 54*cdf0e10cSrcweir using com::sun::star::uno::UNO_QUERY; 55*cdf0e10cSrcweir using com::sun::star::uno::RuntimeException; 56*cdf0e10cSrcweir using com::sun::star::uno::XComponentContext; 57*cdf0e10cSrcweir using com::sun::star::lang::XSingleServiceFactory; 58*cdf0e10cSrcweir using com::sun::star::lang::XUnoTunnel; 59*cdf0e10cSrcweir using com::sun::star::reflection::XIdlReflection; 60*cdf0e10cSrcweir using com::sun::star::script::XTypeConverter; 61*cdf0e10cSrcweir using com::sun::star::script::XInvocationAdapterFactory2; 62*cdf0e10cSrcweir using com::sun::star::script::XInvocation; 63*cdf0e10cSrcweir using com::sun::star::beans::XMaterialHolder; 64*cdf0e10cSrcweir using com::sun::star::beans::XIntrospection; 65*cdf0e10cSrcweir 66*cdf0e10cSrcweir namespace pyuno 67*cdf0e10cSrcweir { 68*cdf0e10cSrcweir #define USTR_ASCII(x) OUString( RTL_CONSTASCII_USTRINGPARAM( x ) ) 69*cdf0e10cSrcweir 70*cdf0e10cSrcweir static PyTypeObject RuntimeImpl_Type = 71*cdf0e10cSrcweir { 72*cdf0e10cSrcweir PyObject_HEAD_INIT (&PyType_Type) 73*cdf0e10cSrcweir 0, 74*cdf0e10cSrcweir const_cast< char * >("pyuno_runtime"), 75*cdf0e10cSrcweir sizeof (RuntimeImpl), 76*cdf0e10cSrcweir 0, 77*cdf0e10cSrcweir (destructor) RuntimeImpl::del, 78*cdf0e10cSrcweir (printfunc) 0, 79*cdf0e10cSrcweir (getattrfunc) 0, 80*cdf0e10cSrcweir (setattrfunc) 0, 81*cdf0e10cSrcweir (cmpfunc) 0, 82*cdf0e10cSrcweir (reprfunc) 0, 83*cdf0e10cSrcweir 0, 84*cdf0e10cSrcweir 0, 85*cdf0e10cSrcweir 0, 86*cdf0e10cSrcweir (hashfunc) 0, 87*cdf0e10cSrcweir (ternaryfunc) 0, 88*cdf0e10cSrcweir (reprfunc) 0, 89*cdf0e10cSrcweir (getattrofunc)0, 90*cdf0e10cSrcweir (setattrofunc)0, 91*cdf0e10cSrcweir NULL, 92*cdf0e10cSrcweir 0, 93*cdf0e10cSrcweir NULL, 94*cdf0e10cSrcweir (traverseproc)0, 95*cdf0e10cSrcweir (inquiry)0, 96*cdf0e10cSrcweir (richcmpfunc)0, 97*cdf0e10cSrcweir 0, 98*cdf0e10cSrcweir (getiterfunc)0, 99*cdf0e10cSrcweir (iternextfunc)0, 100*cdf0e10cSrcweir NULL, 101*cdf0e10cSrcweir NULL, 102*cdf0e10cSrcweir NULL, 103*cdf0e10cSrcweir NULL, 104*cdf0e10cSrcweir NULL, 105*cdf0e10cSrcweir (descrgetfunc)0, 106*cdf0e10cSrcweir (descrsetfunc)0, 107*cdf0e10cSrcweir 0, 108*cdf0e10cSrcweir (initproc)0, 109*cdf0e10cSrcweir (allocfunc)0, 110*cdf0e10cSrcweir (newfunc)0, 111*cdf0e10cSrcweir (freefunc)0, 112*cdf0e10cSrcweir (inquiry)0, 113*cdf0e10cSrcweir NULL, 114*cdf0e10cSrcweir NULL, 115*cdf0e10cSrcweir NULL, 116*cdf0e10cSrcweir NULL, 117*cdf0e10cSrcweir NULL, 118*cdf0e10cSrcweir (destructor)0 119*cdf0e10cSrcweir #if PY_VERSION_HEX >= 0x02060000 120*cdf0e10cSrcweir , 0 121*cdf0e10cSrcweir #endif 122*cdf0e10cSrcweir }; 123*cdf0e10cSrcweir 124*cdf0e10cSrcweir /*---------------------------------------------------------------------- 125*cdf0e10cSrcweir Runtime implementation 126*cdf0e10cSrcweir -----------------------------------------------------------------------*/ 127*cdf0e10cSrcweir static void getRuntimeImpl( PyRef & globalDict, PyRef &runtimeImpl ) 128*cdf0e10cSrcweir throw ( com::sun::star::uno::RuntimeException ) 129*cdf0e10cSrcweir { 130*cdf0e10cSrcweir PyThreadState * state = PyThreadState_Get(); 131*cdf0e10cSrcweir if( ! state ) 132*cdf0e10cSrcweir { 133*cdf0e10cSrcweir throw RuntimeException( OUString( RTL_CONSTASCII_USTRINGPARAM( 134*cdf0e10cSrcweir "python global interpreter must be held (thread must be attached)" )), 135*cdf0e10cSrcweir Reference< XInterface > () ); 136*cdf0e10cSrcweir } 137*cdf0e10cSrcweir 138*cdf0e10cSrcweir globalDict = PyRef( PyModule_GetDict(PyImport_AddModule(const_cast< char * >("__main__")))); 139*cdf0e10cSrcweir 140*cdf0e10cSrcweir if( ! globalDict.is() ) // FATAL ! 141*cdf0e10cSrcweir { 142*cdf0e10cSrcweir throw RuntimeException( OUString( RTL_CONSTASCII_USTRINGPARAM( 143*cdf0e10cSrcweir "can't find __main__ module" )), Reference< XInterface > ()); 144*cdf0e10cSrcweir } 145*cdf0e10cSrcweir runtimeImpl = PyDict_GetItemString( globalDict.get() , "pyuno_runtime" ); 146*cdf0e10cSrcweir } 147*cdf0e10cSrcweir 148*cdf0e10cSrcweir static PyRef importUnoModule( ) throw ( RuntimeException ) 149*cdf0e10cSrcweir { 150*cdf0e10cSrcweir PyRef globalDict = PyRef( PyModule_GetDict(PyImport_AddModule(const_cast< char * >("__main__")))); 151*cdf0e10cSrcweir // import the uno module 152*cdf0e10cSrcweir PyRef module( PyImport_ImportModule( const_cast< char * >("uno") ), SAL_NO_ACQUIRE ); 153*cdf0e10cSrcweir if( PyErr_Occurred() ) 154*cdf0e10cSrcweir { 155*cdf0e10cSrcweir PyRef excType, excValue, excTraceback; 156*cdf0e10cSrcweir PyErr_Fetch( (PyObject **)&excType, (PyObject**)&excValue,(PyObject**)&excTraceback); 157*cdf0e10cSrcweir PyRef str( PyObject_Repr( excTraceback.get() ), SAL_NO_ACQUIRE ); 158*cdf0e10cSrcweir 159*cdf0e10cSrcweir OUStringBuffer buf; 160*cdf0e10cSrcweir buf.appendAscii( "python object raised an unknown exception (" ); 161*cdf0e10cSrcweir PyRef valueRep( PyObject_Repr( excValue.get() ), SAL_NO_ACQUIRE ); 162*cdf0e10cSrcweir buf.appendAscii( PyString_AsString( valueRep.get())).appendAscii( ", traceback follows\n" ); 163*cdf0e10cSrcweir buf.appendAscii( PyString_AsString( str.get() ) ); 164*cdf0e10cSrcweir throw RuntimeException( buf.makeStringAndClear(), Reference< XInterface > () ); 165*cdf0e10cSrcweir } 166*cdf0e10cSrcweir PyRef dict( PyModule_GetDict( module.get() ) ); 167*cdf0e10cSrcweir return dict; 168*cdf0e10cSrcweir } 169*cdf0e10cSrcweir 170*cdf0e10cSrcweir static void readLoggingConfig( sal_Int32 *pLevel, FILE **ppFile ) 171*cdf0e10cSrcweir { 172*cdf0e10cSrcweir *pLevel = LogLevel::NONE; 173*cdf0e10cSrcweir *ppFile = 0; 174*cdf0e10cSrcweir OUString fileName; 175*cdf0e10cSrcweir osl_getModuleURLFromFunctionAddress( 176*cdf0e10cSrcweir reinterpret_cast< oslGenericFunction >(readLoggingConfig), 177*cdf0e10cSrcweir (rtl_uString **) &fileName ); 178*cdf0e10cSrcweir fileName = OUString( fileName.getStr(), fileName.lastIndexOf( '/' )+1 ); 179*cdf0e10cSrcweir fileName += OUString::createFromAscii( SAL_CONFIGFILE("pyuno") ); 180*cdf0e10cSrcweir rtl::Bootstrap bootstrapHandle( fileName ); 181*cdf0e10cSrcweir 182*cdf0e10cSrcweir OUString str; 183*cdf0e10cSrcweir if( bootstrapHandle.getFrom( USTR_ASCII( "PYUNO_LOGLEVEL" ), str ) ) 184*cdf0e10cSrcweir { 185*cdf0e10cSrcweir if( str.equalsAscii( "NONE" ) ) 186*cdf0e10cSrcweir *pLevel = LogLevel::NONE; 187*cdf0e10cSrcweir else if( str.equalsAscii( "CALL" ) ) 188*cdf0e10cSrcweir *pLevel = LogLevel::CALL; 189*cdf0e10cSrcweir else if( str.equalsAscii( "ARGS" ) ) 190*cdf0e10cSrcweir *pLevel = LogLevel::ARGS; 191*cdf0e10cSrcweir else 192*cdf0e10cSrcweir { 193*cdf0e10cSrcweir fprintf( stderr, "unknown loglevel %s\n", 194*cdf0e10cSrcweir OUStringToOString( str, RTL_TEXTENCODING_UTF8 ).getStr() ); 195*cdf0e10cSrcweir } 196*cdf0e10cSrcweir } 197*cdf0e10cSrcweir if( *pLevel > LogLevel::NONE ) 198*cdf0e10cSrcweir { 199*cdf0e10cSrcweir *ppFile = stdout; 200*cdf0e10cSrcweir if( bootstrapHandle.getFrom( USTR_ASCII( "PYUNO_LOGTARGET" ), str ) ) 201*cdf0e10cSrcweir { 202*cdf0e10cSrcweir if( str.equalsAscii( "stdout" ) ) 203*cdf0e10cSrcweir *ppFile = stdout; 204*cdf0e10cSrcweir else if( str.equalsAscii( "stderr" ) ) 205*cdf0e10cSrcweir *ppFile = stderr; 206*cdf0e10cSrcweir else 207*cdf0e10cSrcweir { 208*cdf0e10cSrcweir oslProcessInfo data; 209*cdf0e10cSrcweir data.Size = sizeof( data ); 210*cdf0e10cSrcweir osl_getProcessInfo( 211*cdf0e10cSrcweir 0 , osl_Process_IDENTIFIER , &data ); 212*cdf0e10cSrcweir osl_getSystemPathFromFileURL( str.pData, &str.pData); 213*cdf0e10cSrcweir OString o = OUStringToOString( str, osl_getThreadTextEncoding() ); 214*cdf0e10cSrcweir o += "."; 215*cdf0e10cSrcweir o += OString::valueOf( (sal_Int32)data.Ident ); 216*cdf0e10cSrcweir 217*cdf0e10cSrcweir *ppFile = fopen( o.getStr() , "w" ); 218*cdf0e10cSrcweir if ( *ppFile ) 219*cdf0e10cSrcweir { 220*cdf0e10cSrcweir // do not buffer (useful if e.g. analyzing a crash) 221*cdf0e10cSrcweir setvbuf( *ppFile, 0, _IONBF, 0 ); 222*cdf0e10cSrcweir } 223*cdf0e10cSrcweir else 224*cdf0e10cSrcweir { 225*cdf0e10cSrcweir fprintf( stderr, "couldn't create file %s\n", 226*cdf0e10cSrcweir OUStringToOString( str, RTL_TEXTENCODING_UTF8 ).getStr() ); 227*cdf0e10cSrcweir 228*cdf0e10cSrcweir } 229*cdf0e10cSrcweir } 230*cdf0e10cSrcweir } 231*cdf0e10cSrcweir } 232*cdf0e10cSrcweir } 233*cdf0e10cSrcweir 234*cdf0e10cSrcweir /*------------------------------------------------------------------- 235*cdf0e10cSrcweir RuntimeImpl implementations 236*cdf0e10cSrcweir *-------------------------------------------------------------------*/ 237*cdf0e10cSrcweir PyRef stRuntimeImpl::create( const Reference< XComponentContext > &ctx ) 238*cdf0e10cSrcweir throw( com::sun::star::uno::RuntimeException ) 239*cdf0e10cSrcweir { 240*cdf0e10cSrcweir RuntimeImpl *me = PyObject_New (RuntimeImpl, &RuntimeImpl_Type); 241*cdf0e10cSrcweir if( ! me ) 242*cdf0e10cSrcweir throw RuntimeException( 243*cdf0e10cSrcweir OUString( RTL_CONSTASCII_USTRINGPARAM( "cannot instantiate pyuno::RuntimeImpl" ) ), 244*cdf0e10cSrcweir Reference< XInterface > () ); 245*cdf0e10cSrcweir me->cargo = 0; 246*cdf0e10cSrcweir // must use a different struct here, as the PyObject_New 247*cdf0e10cSrcweir // makes C++ unusable 248*cdf0e10cSrcweir RuntimeCargo *c = new RuntimeCargo(); 249*cdf0e10cSrcweir readLoggingConfig( &(c->logLevel) , &(c->logFile) ); 250*cdf0e10cSrcweir log( c, LogLevel::CALL, "Instantiating pyuno bridge" ); 251*cdf0e10cSrcweir 252*cdf0e10cSrcweir c->valid = 1; 253*cdf0e10cSrcweir c->xContext = ctx; 254*cdf0e10cSrcweir c->xInvocation = Reference< XSingleServiceFactory > ( 255*cdf0e10cSrcweir ctx->getServiceManager()->createInstanceWithContext( 256*cdf0e10cSrcweir OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.script.Invocation" ) ), 257*cdf0e10cSrcweir ctx ), 258*cdf0e10cSrcweir UNO_QUERY ); 259*cdf0e10cSrcweir if( ! c->xInvocation.is() ) 260*cdf0e10cSrcweir throw RuntimeException( 261*cdf0e10cSrcweir OUString( RTL_CONSTASCII_USTRINGPARAM( "pyuno: couldn't instantiate invocation service" ) ), 262*cdf0e10cSrcweir Reference< XInterface > () ); 263*cdf0e10cSrcweir 264*cdf0e10cSrcweir c->xTypeConverter = Reference< XTypeConverter > ( 265*cdf0e10cSrcweir ctx->getServiceManager()->createInstanceWithContext( 266*cdf0e10cSrcweir OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.script.Converter" ) ), 267*cdf0e10cSrcweir ctx ), 268*cdf0e10cSrcweir UNO_QUERY ); 269*cdf0e10cSrcweir if( ! c->xTypeConverter.is() ) 270*cdf0e10cSrcweir throw RuntimeException( 271*cdf0e10cSrcweir OUString( RTL_CONSTASCII_USTRINGPARAM( "pyuno: couldn't instantiate typeconverter service" )), 272*cdf0e10cSrcweir Reference< XInterface > () ); 273*cdf0e10cSrcweir 274*cdf0e10cSrcweir c->xCoreReflection = Reference< XIdlReflection > ( 275*cdf0e10cSrcweir ctx->getServiceManager()->createInstanceWithContext( 276*cdf0e10cSrcweir OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.reflection.CoreReflection" ) ), 277*cdf0e10cSrcweir ctx ), 278*cdf0e10cSrcweir UNO_QUERY ); 279*cdf0e10cSrcweir if( ! c->xCoreReflection.is() ) 280*cdf0e10cSrcweir throw RuntimeException( 281*cdf0e10cSrcweir OUString( RTL_CONSTASCII_USTRINGPARAM( "pyuno: couldn't instantiate corereflection service" )), 282*cdf0e10cSrcweir Reference< XInterface > () ); 283*cdf0e10cSrcweir 284*cdf0e10cSrcweir c->xAdapterFactory = Reference< XInvocationAdapterFactory2 > ( 285*cdf0e10cSrcweir ctx->getServiceManager()->createInstanceWithContext( 286*cdf0e10cSrcweir OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.script.InvocationAdapterFactory" ) ), 287*cdf0e10cSrcweir ctx ), 288*cdf0e10cSrcweir UNO_QUERY ); 289*cdf0e10cSrcweir if( ! c->xAdapterFactory.is() ) 290*cdf0e10cSrcweir throw RuntimeException( 291*cdf0e10cSrcweir OUString( RTL_CONSTASCII_USTRINGPARAM( "pyuno: couldn't instantiate invocation adapter factory service" )), 292*cdf0e10cSrcweir Reference< XInterface > () ); 293*cdf0e10cSrcweir 294*cdf0e10cSrcweir c->xIntrospection = Reference< XIntrospection > ( 295*cdf0e10cSrcweir ctx->getServiceManager()->createInstanceWithContext( 296*cdf0e10cSrcweir OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.beans.Introspection" ) ), 297*cdf0e10cSrcweir ctx ), 298*cdf0e10cSrcweir UNO_QUERY ); 299*cdf0e10cSrcweir if( ! c->xIntrospection.is() ) 300*cdf0e10cSrcweir throw RuntimeException( 301*cdf0e10cSrcweir OUString( RTL_CONSTASCII_USTRINGPARAM( "pyuno: couldn't instantiate introspection service" )), 302*cdf0e10cSrcweir Reference< XInterface > () ); 303*cdf0e10cSrcweir 304*cdf0e10cSrcweir Any a = ctx->getValueByName(OUString( 305*cdf0e10cSrcweir RTL_CONSTASCII_USTRINGPARAM("/singletons/com.sun.star.reflection.theTypeDescriptionManager" )) ); 306*cdf0e10cSrcweir a >>= c->xTdMgr; 307*cdf0e10cSrcweir if( ! c->xTdMgr.is() ) 308*cdf0e10cSrcweir throw RuntimeException( 309*cdf0e10cSrcweir OUString( RTL_CONSTASCII_USTRINGPARAM( "pyuno: couldn't retrieve typedescriptionmanager" )), 310*cdf0e10cSrcweir Reference< XInterface > () ); 311*cdf0e10cSrcweir 312*cdf0e10cSrcweir me->cargo =c; 313*cdf0e10cSrcweir return PyRef( reinterpret_cast< PyObject * > ( me ), SAL_NO_ACQUIRE ); 314*cdf0e10cSrcweir } 315*cdf0e10cSrcweir 316*cdf0e10cSrcweir void stRuntimeImpl::del(PyObject* self) 317*cdf0e10cSrcweir { 318*cdf0e10cSrcweir RuntimeImpl *me = reinterpret_cast< RuntimeImpl * > ( self ); 319*cdf0e10cSrcweir if( me->cargo->logFile ) 320*cdf0e10cSrcweir fclose( me->cargo->logFile ); 321*cdf0e10cSrcweir delete me->cargo; 322*cdf0e10cSrcweir PyObject_Del (self); 323*cdf0e10cSrcweir } 324*cdf0e10cSrcweir 325*cdf0e10cSrcweir 326*cdf0e10cSrcweir void Runtime::initialize( const Reference< XComponentContext > & ctx ) 327*cdf0e10cSrcweir throw ( RuntimeException ) 328*cdf0e10cSrcweir { 329*cdf0e10cSrcweir PyRef globalDict, runtime; 330*cdf0e10cSrcweir getRuntimeImpl( globalDict , runtime ); 331*cdf0e10cSrcweir RuntimeImpl *impl = reinterpret_cast< RuntimeImpl * > (runtime.get()); 332*cdf0e10cSrcweir 333*cdf0e10cSrcweir if( runtime.is() && impl->cargo->valid ) 334*cdf0e10cSrcweir { 335*cdf0e10cSrcweir throw RuntimeException( OUString( RTL_CONSTASCII_USTRINGPARAM( 336*cdf0e10cSrcweir "pyuno runtime has already been initialized before" ) ), 337*cdf0e10cSrcweir Reference< XInterface > () ); 338*cdf0e10cSrcweir } 339*cdf0e10cSrcweir PyRef keep( RuntimeImpl::create( ctx ) ); 340*cdf0e10cSrcweir PyDict_SetItemString( globalDict.get(), "pyuno_runtime" , keep.get() ); 341*cdf0e10cSrcweir Py_XINCREF( keep.get() ); 342*cdf0e10cSrcweir } 343*cdf0e10cSrcweir 344*cdf0e10cSrcweir 345*cdf0e10cSrcweir bool Runtime::isInitialized() throw ( RuntimeException ) 346*cdf0e10cSrcweir { 347*cdf0e10cSrcweir PyRef globalDict, runtime; 348*cdf0e10cSrcweir getRuntimeImpl( globalDict , runtime ); 349*cdf0e10cSrcweir RuntimeImpl *impl = reinterpret_cast< RuntimeImpl * > (runtime.get()); 350*cdf0e10cSrcweir return runtime.is() && impl->cargo->valid; 351*cdf0e10cSrcweir } 352*cdf0e10cSrcweir 353*cdf0e10cSrcweir void Runtime::finalize() throw (RuntimeException) 354*cdf0e10cSrcweir { 355*cdf0e10cSrcweir PyRef globalDict, runtime; 356*cdf0e10cSrcweir getRuntimeImpl( globalDict , runtime ); 357*cdf0e10cSrcweir RuntimeImpl *impl = reinterpret_cast< RuntimeImpl * > (runtime.get()); 358*cdf0e10cSrcweir if( !runtime.is() || ! impl->cargo->valid ) 359*cdf0e10cSrcweir { 360*cdf0e10cSrcweir throw RuntimeException( OUString( RTL_CONSTASCII_USTRINGPARAM( 361*cdf0e10cSrcweir "pyuno bridge must have been initialized before finalizing" )), 362*cdf0e10cSrcweir Reference< XInterface > () ); 363*cdf0e10cSrcweir } 364*cdf0e10cSrcweir impl->cargo->valid = false; 365*cdf0e10cSrcweir impl->cargo->xInvocation.clear(); 366*cdf0e10cSrcweir impl->cargo->xContext.clear(); 367*cdf0e10cSrcweir impl->cargo->xTypeConverter.clear(); 368*cdf0e10cSrcweir } 369*cdf0e10cSrcweir 370*cdf0e10cSrcweir Runtime::Runtime() throw( RuntimeException ) 371*cdf0e10cSrcweir : impl( 0 ) 372*cdf0e10cSrcweir { 373*cdf0e10cSrcweir PyRef globalDict, runtime; 374*cdf0e10cSrcweir getRuntimeImpl( globalDict , runtime ); 375*cdf0e10cSrcweir if( ! runtime.is() ) 376*cdf0e10cSrcweir { 377*cdf0e10cSrcweir throw RuntimeException( 378*cdf0e10cSrcweir OUString( RTL_CONSTASCII_USTRINGPARAM("pyuno runtime is not initialized, " 379*cdf0e10cSrcweir "(the pyuno.bootstrap needs to be called before using any uno classes)")), 380*cdf0e10cSrcweir Reference< XInterface > () ); 381*cdf0e10cSrcweir } 382*cdf0e10cSrcweir impl = reinterpret_cast< RuntimeImpl * > (runtime.get()); 383*cdf0e10cSrcweir Py_XINCREF( runtime.get() ); 384*cdf0e10cSrcweir } 385*cdf0e10cSrcweir 386*cdf0e10cSrcweir Runtime::Runtime( const Runtime & r ) 387*cdf0e10cSrcweir { 388*cdf0e10cSrcweir impl = r.impl; 389*cdf0e10cSrcweir Py_XINCREF( reinterpret_cast< PyObject * >(impl) ); 390*cdf0e10cSrcweir } 391*cdf0e10cSrcweir 392*cdf0e10cSrcweir Runtime::~Runtime() 393*cdf0e10cSrcweir { 394*cdf0e10cSrcweir Py_XDECREF( reinterpret_cast< PyObject * >(impl) ); 395*cdf0e10cSrcweir } 396*cdf0e10cSrcweir 397*cdf0e10cSrcweir Runtime & Runtime::operator = ( const Runtime & r ) 398*cdf0e10cSrcweir { 399*cdf0e10cSrcweir PyRef temp( reinterpret_cast< PyObject * >(r.impl) ); 400*cdf0e10cSrcweir Py_XINCREF( temp.get() ); 401*cdf0e10cSrcweir Py_XDECREF( reinterpret_cast< PyObject * >(impl) ); 402*cdf0e10cSrcweir impl = r.impl; 403*cdf0e10cSrcweir return *this; 404*cdf0e10cSrcweir } 405*cdf0e10cSrcweir 406*cdf0e10cSrcweir PyRef Runtime::any2PyObject (const Any &a ) const 407*cdf0e10cSrcweir throw ( com::sun::star::script::CannotConvertException, 408*cdf0e10cSrcweir com::sun::star::lang::IllegalArgumentException, 409*cdf0e10cSrcweir RuntimeException) 410*cdf0e10cSrcweir { 411*cdf0e10cSrcweir if( ! impl->cargo->valid ) 412*cdf0e10cSrcweir { 413*cdf0e10cSrcweir throw RuntimeException( OUString( RTL_CONSTASCII_USTRINGPARAM( 414*cdf0e10cSrcweir "pyuno runtime must be initialized before calling any2PyObject" )), 415*cdf0e10cSrcweir Reference< XInterface > () ); 416*cdf0e10cSrcweir } 417*cdf0e10cSrcweir 418*cdf0e10cSrcweir switch (a.getValueTypeClass ()) 419*cdf0e10cSrcweir { 420*cdf0e10cSrcweir case typelib_TypeClass_VOID: 421*cdf0e10cSrcweir { 422*cdf0e10cSrcweir Py_INCREF (Py_None); 423*cdf0e10cSrcweir return PyRef(Py_None); 424*cdf0e10cSrcweir } 425*cdf0e10cSrcweir case typelib_TypeClass_CHAR: 426*cdf0e10cSrcweir { 427*cdf0e10cSrcweir sal_Unicode c = *(sal_Unicode*)a.getValue(); 428*cdf0e10cSrcweir return PyRef( PyUNO_char_new( c , *this ), SAL_NO_ACQUIRE ); 429*cdf0e10cSrcweir } 430*cdf0e10cSrcweir case typelib_TypeClass_BOOLEAN: 431*cdf0e10cSrcweir { 432*cdf0e10cSrcweir sal_Bool b = sal_Bool(); 433*cdf0e10cSrcweir if ((a >>= b) && b) 434*cdf0e10cSrcweir return Py_True; 435*cdf0e10cSrcweir else 436*cdf0e10cSrcweir return Py_False; 437*cdf0e10cSrcweir } 438*cdf0e10cSrcweir case typelib_TypeClass_BYTE: 439*cdf0e10cSrcweir case typelib_TypeClass_SHORT: 440*cdf0e10cSrcweir case typelib_TypeClass_UNSIGNED_SHORT: 441*cdf0e10cSrcweir case typelib_TypeClass_LONG: 442*cdf0e10cSrcweir { 443*cdf0e10cSrcweir sal_Int32 l = 0; 444*cdf0e10cSrcweir a >>= l; 445*cdf0e10cSrcweir return PyRef( PyInt_FromLong (l), SAL_NO_ACQUIRE ); 446*cdf0e10cSrcweir } 447*cdf0e10cSrcweir case typelib_TypeClass_UNSIGNED_LONG: 448*cdf0e10cSrcweir { 449*cdf0e10cSrcweir sal_uInt32 l = 0; 450*cdf0e10cSrcweir a >>= l; 451*cdf0e10cSrcweir return PyRef( PyLong_FromUnsignedLong (l), SAL_NO_ACQUIRE ); 452*cdf0e10cSrcweir } 453*cdf0e10cSrcweir case typelib_TypeClass_HYPER: 454*cdf0e10cSrcweir { 455*cdf0e10cSrcweir sal_Int64 l = 0; 456*cdf0e10cSrcweir a >>= l; 457*cdf0e10cSrcweir return PyRef( PyLong_FromLongLong (l), SAL_NO_ACQUIRE); 458*cdf0e10cSrcweir } 459*cdf0e10cSrcweir case typelib_TypeClass_UNSIGNED_HYPER: 460*cdf0e10cSrcweir { 461*cdf0e10cSrcweir sal_uInt64 l = 0; 462*cdf0e10cSrcweir a >>= l; 463*cdf0e10cSrcweir return PyRef( PyLong_FromUnsignedLongLong (l), SAL_NO_ACQUIRE); 464*cdf0e10cSrcweir } 465*cdf0e10cSrcweir case typelib_TypeClass_FLOAT: 466*cdf0e10cSrcweir { 467*cdf0e10cSrcweir float f = 0.0; 468*cdf0e10cSrcweir a >>= f; 469*cdf0e10cSrcweir return PyRef(PyFloat_FromDouble (f), SAL_NO_ACQUIRE); 470*cdf0e10cSrcweir } 471*cdf0e10cSrcweir case typelib_TypeClass_DOUBLE: 472*cdf0e10cSrcweir { 473*cdf0e10cSrcweir double d = 0.0; 474*cdf0e10cSrcweir a >>= d; 475*cdf0e10cSrcweir return PyRef( PyFloat_FromDouble (d), SAL_NO_ACQUIRE); 476*cdf0e10cSrcweir } 477*cdf0e10cSrcweir case typelib_TypeClass_STRING: 478*cdf0e10cSrcweir { 479*cdf0e10cSrcweir OUString tmp_ostr; 480*cdf0e10cSrcweir a >>= tmp_ostr; 481*cdf0e10cSrcweir return ustring2PyUnicode( tmp_ostr ); 482*cdf0e10cSrcweir } 483*cdf0e10cSrcweir case typelib_TypeClass_TYPE: 484*cdf0e10cSrcweir { 485*cdf0e10cSrcweir Type t; 486*cdf0e10cSrcweir a >>= t; 487*cdf0e10cSrcweir OString o = OUStringToOString( t.getTypeName(), RTL_TEXTENCODING_ASCII_US ); 488*cdf0e10cSrcweir return PyRef( 489*cdf0e10cSrcweir PyUNO_Type_new ( 490*cdf0e10cSrcweir o.getStr(), (com::sun::star::uno::TypeClass)t.getTypeClass(), *this), 491*cdf0e10cSrcweir SAL_NO_ACQUIRE); 492*cdf0e10cSrcweir } 493*cdf0e10cSrcweir case typelib_TypeClass_ANY: 494*cdf0e10cSrcweir { 495*cdf0e10cSrcweir //I don't think this can happen. 496*cdf0e10cSrcweir Py_INCREF (Py_None); 497*cdf0e10cSrcweir return Py_None; 498*cdf0e10cSrcweir } 499*cdf0e10cSrcweir case typelib_TypeClass_ENUM: 500*cdf0e10cSrcweir { 501*cdf0e10cSrcweir sal_Int32 l = *(sal_Int32 *) a.getValue(); 502*cdf0e10cSrcweir TypeDescription desc( a.getValueType() ); 503*cdf0e10cSrcweir if( desc.is() ) 504*cdf0e10cSrcweir { 505*cdf0e10cSrcweir desc.makeComplete(); 506*cdf0e10cSrcweir typelib_EnumTypeDescription *pEnumDesc = 507*cdf0e10cSrcweir (typelib_EnumTypeDescription *) desc.get(); 508*cdf0e10cSrcweir for( int i = 0 ; i < pEnumDesc->nEnumValues ; i ++ ) 509*cdf0e10cSrcweir { 510*cdf0e10cSrcweir if( pEnumDesc->pEnumValues[i] == l ) 511*cdf0e10cSrcweir { 512*cdf0e10cSrcweir OString v = OUStringToOString( pEnumDesc->ppEnumNames[i], RTL_TEXTENCODING_ASCII_US); 513*cdf0e10cSrcweir OString e = OUStringToOString( pEnumDesc->aBase.pTypeName, RTL_TEXTENCODING_ASCII_US); 514*cdf0e10cSrcweir return PyRef( PyUNO_Enum_new(e.getStr(),v.getStr(), *this ), SAL_NO_ACQUIRE ); 515*cdf0e10cSrcweir } 516*cdf0e10cSrcweir } 517*cdf0e10cSrcweir } 518*cdf0e10cSrcweir OUStringBuffer buf; 519*cdf0e10cSrcweir buf.appendAscii( "Any carries enum " ); 520*cdf0e10cSrcweir buf.append( a.getValueType().getTypeName()); 521*cdf0e10cSrcweir buf.appendAscii( " with invalid value " ).append( l ); 522*cdf0e10cSrcweir throw RuntimeException( buf.makeStringAndClear() , Reference< XInterface > () ); 523*cdf0e10cSrcweir } 524*cdf0e10cSrcweir case typelib_TypeClass_EXCEPTION: 525*cdf0e10cSrcweir case typelib_TypeClass_STRUCT: 526*cdf0e10cSrcweir { 527*cdf0e10cSrcweir PyRef excClass = getClass( a.getValueType().getTypeName(), *this ); 528*cdf0e10cSrcweir PyRef value = PyRef( PyUNO_new_UNCHECKED (a, getImpl()->cargo->xInvocation), SAL_NO_ACQUIRE); 529*cdf0e10cSrcweir PyRef argsTuple( PyTuple_New( 1 ) , SAL_NO_ACQUIRE ); 530*cdf0e10cSrcweir PyTuple_SetItem( argsTuple.get() , 0 , value.getAcquired() ); 531*cdf0e10cSrcweir PyRef ret( PyObject_CallObject( excClass.get() , argsTuple.get() ), SAL_NO_ACQUIRE ); 532*cdf0e10cSrcweir if( ! ret.is() ) 533*cdf0e10cSrcweir { 534*cdf0e10cSrcweir OUStringBuffer buf; 535*cdf0e10cSrcweir buf.appendAscii( "Couldn't instantiate python representation of structered UNO type " ); 536*cdf0e10cSrcweir buf.append( a.getValueType().getTypeName() ); 537*cdf0e10cSrcweir throw RuntimeException( buf.makeStringAndClear(), Reference< XInterface > () ); 538*cdf0e10cSrcweir } 539*cdf0e10cSrcweir 540*cdf0e10cSrcweir if( com::sun::star::uno::TypeClass_EXCEPTION == a.getValueTypeClass() ) 541*cdf0e10cSrcweir { 542*cdf0e10cSrcweir // add the message in a standard python way ! 543*cdf0e10cSrcweir PyRef args( PyTuple_New( 1 ), SAL_NO_ACQUIRE ); 544*cdf0e10cSrcweir 545*cdf0e10cSrcweir // assuming that the Message is always the first member, wuuuu 546*cdf0e10cSrcweir void *pData = (void*)a.getValue(); 547*cdf0e10cSrcweir OUString message = *(OUString * )pData; 548*cdf0e10cSrcweir PyRef pymsg = ustring2PyString( message ); 549*cdf0e10cSrcweir PyTuple_SetItem( args.get(), 0 , pymsg.getAcquired() ); 550*cdf0e10cSrcweir // the exception base functions want to have an "args" tuple, 551*cdf0e10cSrcweir // which contains the message 552*cdf0e10cSrcweir PyObject_SetAttrString( ret.get(), const_cast< char * >("args"), args.get() ); 553*cdf0e10cSrcweir } 554*cdf0e10cSrcweir return ret; 555*cdf0e10cSrcweir } 556*cdf0e10cSrcweir case typelib_TypeClass_SEQUENCE: 557*cdf0e10cSrcweir { 558*cdf0e10cSrcweir Sequence<Any> s; 559*cdf0e10cSrcweir 560*cdf0e10cSrcweir Sequence< sal_Int8 > byteSequence; 561*cdf0e10cSrcweir if( a >>= byteSequence ) 562*cdf0e10cSrcweir { 563*cdf0e10cSrcweir // byte sequence is treated in a special way because of peformance reasons 564*cdf0e10cSrcweir // @since 0.9.2 565*cdf0e10cSrcweir return PyRef( PyUNO_ByteSequence_new( byteSequence, *this ), SAL_NO_ACQUIRE ); 566*cdf0e10cSrcweir } 567*cdf0e10cSrcweir else 568*cdf0e10cSrcweir { 569*cdf0e10cSrcweir Reference< XTypeConverter > tc = getImpl()->cargo->xTypeConverter; 570*cdf0e10cSrcweir Reference< XSingleServiceFactory > ssf = getImpl()->cargo->xInvocation; 571*cdf0e10cSrcweir tc->convertTo (a, ::getCppuType (&s)) >>= s; 572*cdf0e10cSrcweir PyRef tuple( PyTuple_New (s.getLength()), SAL_NO_ACQUIRE); 573*cdf0e10cSrcweir int i=0; 574*cdf0e10cSrcweir OUString errMsg; 575*cdf0e10cSrcweir try 576*cdf0e10cSrcweir { 577*cdf0e10cSrcweir for ( i = 0; i < s.getLength (); i++) 578*cdf0e10cSrcweir { 579*cdf0e10cSrcweir PyRef element; 580*cdf0e10cSrcweir element = any2PyObject (tc->convertTo (s[i], s[i].getValueType() )); 581*cdf0e10cSrcweir OSL_ASSERT( element.is() ); 582*cdf0e10cSrcweir PyTuple_SetItem( tuple.get(), i, element.getAcquired() ); 583*cdf0e10cSrcweir } 584*cdf0e10cSrcweir } 585*cdf0e10cSrcweir catch( com::sun::star::uno::Exception & ) 586*cdf0e10cSrcweir { 587*cdf0e10cSrcweir for( ; i < s.getLength() ; i ++ ) 588*cdf0e10cSrcweir { 589*cdf0e10cSrcweir Py_INCREF( Py_None ); 590*cdf0e10cSrcweir PyTuple_SetItem( tuple.get(), i, Py_None ); 591*cdf0e10cSrcweir } 592*cdf0e10cSrcweir throw; 593*cdf0e10cSrcweir } 594*cdf0e10cSrcweir return tuple; 595*cdf0e10cSrcweir } 596*cdf0e10cSrcweir } 597*cdf0e10cSrcweir case typelib_TypeClass_INTERFACE: 598*cdf0e10cSrcweir { 599*cdf0e10cSrcweir Reference< XUnoTunnel > tunnel; 600*cdf0e10cSrcweir a >>= tunnel; 601*cdf0e10cSrcweir if( tunnel.is() ) 602*cdf0e10cSrcweir { 603*cdf0e10cSrcweir sal_Int64 that = tunnel->getSomething( ::pyuno::Adapter::getUnoTunnelImplementationId() ); 604*cdf0e10cSrcweir if( that ) 605*cdf0e10cSrcweir return ((Adapter*)sal::static_int_cast< sal_IntPtr >(that))->getWrappedObject(); 606*cdf0e10cSrcweir } 607*cdf0e10cSrcweir //This is just like the struct case: 608*cdf0e10cSrcweir return PyRef( PyUNO_new (a, getImpl()->cargo->xInvocation), SAL_NO_ACQUIRE ); 609*cdf0e10cSrcweir } 610*cdf0e10cSrcweir default: 611*cdf0e10cSrcweir { 612*cdf0e10cSrcweir OUStringBuffer buf; 613*cdf0e10cSrcweir buf.appendAscii( "Unknonwn UNO type class " ); 614*cdf0e10cSrcweir buf.append( (sal_Int32 ) a.getValueTypeClass() ); 615*cdf0e10cSrcweir throw RuntimeException(buf.makeStringAndClear( ), Reference< XInterface > () ); 616*cdf0e10cSrcweir } 617*cdf0e10cSrcweir } 618*cdf0e10cSrcweir //We shouldn't be here... 619*cdf0e10cSrcweir Py_INCREF( Py_None ); 620*cdf0e10cSrcweir return Py_None; 621*cdf0e10cSrcweir } 622*cdf0e10cSrcweir 623*cdf0e10cSrcweir static Sequence< Type > invokeGetTypes( const Runtime & r , PyObject * o ) 624*cdf0e10cSrcweir { 625*cdf0e10cSrcweir Sequence< Type > ret; 626*cdf0e10cSrcweir 627*cdf0e10cSrcweir PyRef method( PyObject_GetAttrString( o , const_cast< char * >("getTypes") ), SAL_NO_ACQUIRE ); 628*cdf0e10cSrcweir raiseInvocationTargetExceptionWhenNeeded( r ); 629*cdf0e10cSrcweir if( method.is() && PyCallable_Check( method.get() ) ) 630*cdf0e10cSrcweir { 631*cdf0e10cSrcweir PyRef types( PyObject_CallObject( method.get(), 0 ) , SAL_NO_ACQUIRE ); 632*cdf0e10cSrcweir raiseInvocationTargetExceptionWhenNeeded( r ); 633*cdf0e10cSrcweir if( types.is() && PyTuple_Check( types.get() ) ) 634*cdf0e10cSrcweir { 635*cdf0e10cSrcweir int size = PyTuple_Size( types.get() ); 636*cdf0e10cSrcweir 637*cdf0e10cSrcweir // add the XUnoTunnel interface for uno object identity concept (hack) 638*cdf0e10cSrcweir ret.realloc( size + 1 ); 639*cdf0e10cSrcweir for( int i = 0 ; i < size ; i ++ ) 640*cdf0e10cSrcweir { 641*cdf0e10cSrcweir Any a = r.pyObject2Any(PyTuple_GetItem(types.get(),i)); 642*cdf0e10cSrcweir a >>= ret[i]; 643*cdf0e10cSrcweir } 644*cdf0e10cSrcweir ret[size] = getCppuType( (Reference< com::sun::star::lang::XUnoTunnel> *) 0 ); 645*cdf0e10cSrcweir } 646*cdf0e10cSrcweir } 647*cdf0e10cSrcweir return ret; 648*cdf0e10cSrcweir } 649*cdf0e10cSrcweir 650*cdf0e10cSrcweir Any Runtime::pyObject2Any ( const PyRef & source, enum ConversionMode mode ) const 651*cdf0e10cSrcweir throw ( com::sun::star::uno::RuntimeException ) 652*cdf0e10cSrcweir { 653*cdf0e10cSrcweir if( ! impl->cargo->valid ) 654*cdf0e10cSrcweir { 655*cdf0e10cSrcweir throw RuntimeException( OUString( RTL_CONSTASCII_USTRINGPARAM( 656*cdf0e10cSrcweir "pyuno runtime must be initialized before calling any2PyObject" )), 657*cdf0e10cSrcweir Reference< XInterface > () ); 658*cdf0e10cSrcweir } 659*cdf0e10cSrcweir 660*cdf0e10cSrcweir Any a; 661*cdf0e10cSrcweir PyObject *o = source.get(); 662*cdf0e10cSrcweir if( Py_None == o ) 663*cdf0e10cSrcweir { 664*cdf0e10cSrcweir 665*cdf0e10cSrcweir } 666*cdf0e10cSrcweir else if (PyInt_Check (o)) 667*cdf0e10cSrcweir { 668*cdf0e10cSrcweir if( o == Py_True ) 669*cdf0e10cSrcweir { 670*cdf0e10cSrcweir sal_Bool b = sal_True; 671*cdf0e10cSrcweir a = Any( &b, getBooleanCppuType() ); 672*cdf0e10cSrcweir } 673*cdf0e10cSrcweir else if ( o == Py_False ) 674*cdf0e10cSrcweir { 675*cdf0e10cSrcweir sal_Bool b = sal_False; 676*cdf0e10cSrcweir a = Any( &b, getBooleanCppuType() ); 677*cdf0e10cSrcweir } 678*cdf0e10cSrcweir else 679*cdf0e10cSrcweir { 680*cdf0e10cSrcweir sal_Int32 l = (sal_Int32) PyInt_AsLong( o ); 681*cdf0e10cSrcweir if( l < 128 && l >= -128 ) 682*cdf0e10cSrcweir { 683*cdf0e10cSrcweir sal_Int8 b = (sal_Int8 ) l; 684*cdf0e10cSrcweir a <<= b; 685*cdf0e10cSrcweir } 686*cdf0e10cSrcweir else if( l <= 0x7fff && l >= -0x8000 ) 687*cdf0e10cSrcweir { 688*cdf0e10cSrcweir sal_Int16 s = (sal_Int16) l; 689*cdf0e10cSrcweir a <<= s; 690*cdf0e10cSrcweir } 691*cdf0e10cSrcweir else 692*cdf0e10cSrcweir { 693*cdf0e10cSrcweir a <<= l; 694*cdf0e10cSrcweir } 695*cdf0e10cSrcweir } 696*cdf0e10cSrcweir } 697*cdf0e10cSrcweir else if (PyLong_Check (o)) 698*cdf0e10cSrcweir { 699*cdf0e10cSrcweir sal_Int64 l = (sal_Int64)PyLong_AsLong (o); 700*cdf0e10cSrcweir if( l < 128 && l >= -128 ) 701*cdf0e10cSrcweir { 702*cdf0e10cSrcweir sal_Int8 b = (sal_Int8 ) l; 703*cdf0e10cSrcweir a <<= b; 704*cdf0e10cSrcweir } 705*cdf0e10cSrcweir else if( l <= 0x7fff && l >= -0x8000 ) 706*cdf0e10cSrcweir { 707*cdf0e10cSrcweir sal_Int16 s = (sal_Int16) l; 708*cdf0e10cSrcweir a <<= s; 709*cdf0e10cSrcweir } 710*cdf0e10cSrcweir else if( l <= SAL_CONST_INT64(0x7fffffff) && 711*cdf0e10cSrcweir l >= -SAL_CONST_INT64(0x80000000) ) 712*cdf0e10cSrcweir { 713*cdf0e10cSrcweir sal_Int32 l32 = (sal_Int32) l; 714*cdf0e10cSrcweir a <<= l32; 715*cdf0e10cSrcweir } 716*cdf0e10cSrcweir else 717*cdf0e10cSrcweir { 718*cdf0e10cSrcweir a <<= l; 719*cdf0e10cSrcweir } 720*cdf0e10cSrcweir } 721*cdf0e10cSrcweir else if (PyFloat_Check (o)) 722*cdf0e10cSrcweir { 723*cdf0e10cSrcweir double d = PyFloat_AsDouble (o); 724*cdf0e10cSrcweir a <<= d; 725*cdf0e10cSrcweir } 726*cdf0e10cSrcweir else if (PyString_Check (o)) 727*cdf0e10cSrcweir a <<= pyString2ustring(o); 728*cdf0e10cSrcweir else if( PyUnicode_Check( o ) ) 729*cdf0e10cSrcweir a <<= pyString2ustring(o); 730*cdf0e10cSrcweir else if (PyTuple_Check (o)) 731*cdf0e10cSrcweir { 732*cdf0e10cSrcweir Sequence<Any> s (PyTuple_Size (o)); 733*cdf0e10cSrcweir for (int i = 0; i < PyTuple_Size (o); i++) 734*cdf0e10cSrcweir { 735*cdf0e10cSrcweir s[i] = pyObject2Any (PyTuple_GetItem (o, i), mode ); 736*cdf0e10cSrcweir } 737*cdf0e10cSrcweir a <<= s; 738*cdf0e10cSrcweir } 739*cdf0e10cSrcweir else 740*cdf0e10cSrcweir { 741*cdf0e10cSrcweir Runtime runtime; 742*cdf0e10cSrcweir // should be removed, in case ByteSequence gets derived from String 743*cdf0e10cSrcweir if( PyObject_IsInstance( o, getByteSequenceClass( runtime ).get() ) ) 744*cdf0e10cSrcweir { 745*cdf0e10cSrcweir PyRef str(PyObject_GetAttrString( o , const_cast< char * >("value") ),SAL_NO_ACQUIRE); 746*cdf0e10cSrcweir Sequence< sal_Int8 > seq; 747*cdf0e10cSrcweir if( PyString_Check( str.get() ) ) 748*cdf0e10cSrcweir { 749*cdf0e10cSrcweir seq = Sequence<sal_Int8 > ( 750*cdf0e10cSrcweir (sal_Int8*) PyString_AsString(str.get()), PyString_Size(str.get())); 751*cdf0e10cSrcweir } 752*cdf0e10cSrcweir a <<= seq; 753*cdf0e10cSrcweir } 754*cdf0e10cSrcweir else 755*cdf0e10cSrcweir if( PyObject_IsInstance( o, getTypeClass( runtime ).get() ) ) 756*cdf0e10cSrcweir { 757*cdf0e10cSrcweir Type t = PyType2Type( o ); 758*cdf0e10cSrcweir a <<= t; 759*cdf0e10cSrcweir } 760*cdf0e10cSrcweir else if( PyObject_IsInstance( o, getEnumClass( runtime ).get() ) ) 761*cdf0e10cSrcweir { 762*cdf0e10cSrcweir a = PyEnum2Enum( o ); 763*cdf0e10cSrcweir } 764*cdf0e10cSrcweir else if( isInstanceOfStructOrException( o ) ) 765*cdf0e10cSrcweir { 766*cdf0e10cSrcweir PyRef struc(PyObject_GetAttrString( o , const_cast< char * >("value") ),SAL_NO_ACQUIRE); 767*cdf0e10cSrcweir PyUNO * obj = (PyUNO*)struc.get(); 768*cdf0e10cSrcweir Reference< XMaterialHolder > holder( obj->members->xInvocation, UNO_QUERY ); 769*cdf0e10cSrcweir if( holder.is( ) ) 770*cdf0e10cSrcweir a = holder->getMaterial(); 771*cdf0e10cSrcweir else 772*cdf0e10cSrcweir { 773*cdf0e10cSrcweir throw RuntimeException( 774*cdf0e10cSrcweir USTR_ASCII( "struct or exception wrapper does not support XMaterialHolder" ), 775*cdf0e10cSrcweir Reference< XInterface > () ); 776*cdf0e10cSrcweir } 777*cdf0e10cSrcweir } 778*cdf0e10cSrcweir else if( PyObject_IsInstance( o, getPyUnoClass( runtime ).get() ) ) 779*cdf0e10cSrcweir { 780*cdf0e10cSrcweir PyUNO* o_pi; 781*cdf0e10cSrcweir o_pi = (PyUNO*) o; 782*cdf0e10cSrcweir if (o_pi->members->wrappedObject.getValueTypeClass () == 783*cdf0e10cSrcweir com::sun::star::uno::TypeClass_STRUCT || 784*cdf0e10cSrcweir o_pi->members->wrappedObject.getValueTypeClass () == 785*cdf0e10cSrcweir com::sun::star::uno::TypeClass_EXCEPTION) 786*cdf0e10cSrcweir { 787*cdf0e10cSrcweir Reference<XMaterialHolder> my_mh (o_pi->members->xInvocation, UNO_QUERY); 788*cdf0e10cSrcweir 789*cdf0e10cSrcweir if (!my_mh.is ()) 790*cdf0e10cSrcweir { 791*cdf0e10cSrcweir throw RuntimeException( 792*cdf0e10cSrcweir USTR_ASCII( "struct wrapper does not support XMaterialHolder" ), 793*cdf0e10cSrcweir Reference< XInterface > () ); 794*cdf0e10cSrcweir } 795*cdf0e10cSrcweir else 796*cdf0e10cSrcweir a = my_mh->getMaterial (); 797*cdf0e10cSrcweir } 798*cdf0e10cSrcweir else 799*cdf0e10cSrcweir { 800*cdf0e10cSrcweir a = o_pi->members->wrappedObject; 801*cdf0e10cSrcweir } 802*cdf0e10cSrcweir } 803*cdf0e10cSrcweir else if( PyObject_IsInstance( o, getCharClass( runtime ).get() ) ) 804*cdf0e10cSrcweir { 805*cdf0e10cSrcweir sal_Unicode c = PyChar2Unicode( o ); 806*cdf0e10cSrcweir a.setValue( &c, getCharCppuType( )); 807*cdf0e10cSrcweir } 808*cdf0e10cSrcweir else if( PyObject_IsInstance( o, getAnyClass( runtime ).get() ) ) 809*cdf0e10cSrcweir { 810*cdf0e10cSrcweir if( ACCEPT_UNO_ANY == mode ) 811*cdf0e10cSrcweir { 812*cdf0e10cSrcweir a = pyObject2Any( PyRef( PyObject_GetAttrString( o , const_cast< char * >("value") ), SAL_NO_ACQUIRE) ); 813*cdf0e10cSrcweir Type t; 814*cdf0e10cSrcweir pyObject2Any( PyRef( PyObject_GetAttrString( o, const_cast< char * >("type") ), SAL_NO_ACQUIRE ) ) >>= t; 815*cdf0e10cSrcweir 816*cdf0e10cSrcweir try 817*cdf0e10cSrcweir { 818*cdf0e10cSrcweir a = getImpl()->cargo->xTypeConverter->convertTo( a, t ); 819*cdf0e10cSrcweir } 820*cdf0e10cSrcweir catch( com::sun::star::uno::Exception & e ) 821*cdf0e10cSrcweir { 822*cdf0e10cSrcweir throw RuntimeException( e.Message, e.Context ); 823*cdf0e10cSrcweir } 824*cdf0e10cSrcweir } 825*cdf0e10cSrcweir else 826*cdf0e10cSrcweir { 827*cdf0e10cSrcweir throw RuntimeException( 828*cdf0e10cSrcweir OUString( RTL_CONSTASCII_USTRINGPARAM( 829*cdf0e10cSrcweir "uno.Any instance not accepted during method call, " 830*cdf0e10cSrcweir "use uno.invoke instead" ) ), 831*cdf0e10cSrcweir Reference< XInterface > () ); 832*cdf0e10cSrcweir } 833*cdf0e10cSrcweir } 834*cdf0e10cSrcweir else 835*cdf0e10cSrcweir { 836*cdf0e10cSrcweir Reference< XInterface > mappedObject; 837*cdf0e10cSrcweir Reference< XInvocation > adapterObject; 838*cdf0e10cSrcweir 839*cdf0e10cSrcweir // instance already mapped out to the world ? 840*cdf0e10cSrcweir PyRef2Adapter::iterator ii = impl->cargo->mappedObjects.find( PyRef( o ) ); 841*cdf0e10cSrcweir if( ii != impl->cargo->mappedObjects.end() ) 842*cdf0e10cSrcweir { 843*cdf0e10cSrcweir adapterObject = ii->second; 844*cdf0e10cSrcweir } 845*cdf0e10cSrcweir 846*cdf0e10cSrcweir if( adapterObject.is() ) 847*cdf0e10cSrcweir { 848*cdf0e10cSrcweir // object got already bridged ! 849*cdf0e10cSrcweir Reference< com::sun::star::lang::XUnoTunnel > tunnel( adapterObject, UNO_QUERY ); 850*cdf0e10cSrcweir 851*cdf0e10cSrcweir Adapter *pAdapter = ( Adapter * ) 852*cdf0e10cSrcweir sal::static_int_cast< sal_IntPtr >( 853*cdf0e10cSrcweir tunnel->getSomething( 854*cdf0e10cSrcweir ::pyuno::Adapter::getUnoTunnelImplementationId() ) ); 855*cdf0e10cSrcweir 856*cdf0e10cSrcweir mappedObject = impl->cargo->xAdapterFactory->createAdapter( 857*cdf0e10cSrcweir adapterObject, pAdapter->getWrappedTypes() ); 858*cdf0e10cSrcweir } 859*cdf0e10cSrcweir else 860*cdf0e10cSrcweir { 861*cdf0e10cSrcweir Sequence< Type > interfaces = invokeGetTypes( *this, o ); 862*cdf0e10cSrcweir if( interfaces.getLength() ) 863*cdf0e10cSrcweir { 864*cdf0e10cSrcweir Adapter *pAdapter = new Adapter( o, interfaces ); 865*cdf0e10cSrcweir mappedObject = 866*cdf0e10cSrcweir getImpl()->cargo->xAdapterFactory->createAdapter( 867*cdf0e10cSrcweir pAdapter, interfaces ); 868*cdf0e10cSrcweir 869*cdf0e10cSrcweir // keep a list of exported objects to ensure object identity ! 870*cdf0e10cSrcweir impl->cargo->mappedObjects[ PyRef(o) ] = 871*cdf0e10cSrcweir com::sun::star::uno::WeakReference< XInvocation > ( pAdapter ); 872*cdf0e10cSrcweir } 873*cdf0e10cSrcweir } 874*cdf0e10cSrcweir if( mappedObject.is() ) 875*cdf0e10cSrcweir { 876*cdf0e10cSrcweir a = com::sun::star::uno::makeAny( mappedObject ); 877*cdf0e10cSrcweir } 878*cdf0e10cSrcweir else 879*cdf0e10cSrcweir { 880*cdf0e10cSrcweir OUStringBuffer buf; 881*cdf0e10cSrcweir buf.appendAscii( "Couldn't convert " ); 882*cdf0e10cSrcweir PyRef reprString( PyObject_Str( o ) , SAL_NO_ACQUIRE ); 883*cdf0e10cSrcweir buf.appendAscii( PyString_AsString( reprString.get() ) ); 884*cdf0e10cSrcweir buf.appendAscii( " to a UNO type" ); 885*cdf0e10cSrcweir throw RuntimeException( buf.makeStringAndClear(), Reference< XInterface > () ); 886*cdf0e10cSrcweir } 887*cdf0e10cSrcweir } 888*cdf0e10cSrcweir } 889*cdf0e10cSrcweir return a; 890*cdf0e10cSrcweir } 891*cdf0e10cSrcweir 892*cdf0e10cSrcweir Any Runtime::extractUnoException( const PyRef & excType, const PyRef &excValue, const PyRef &excTraceback) const 893*cdf0e10cSrcweir { 894*cdf0e10cSrcweir PyRef str; 895*cdf0e10cSrcweir Any ret; 896*cdf0e10cSrcweir if( excTraceback.is() ) 897*cdf0e10cSrcweir { 898*cdf0e10cSrcweir PyRef unoModule( impl ? impl->cargo->getUnoModule() : 0 ); 899*cdf0e10cSrcweir if( unoModule.is() ) 900*cdf0e10cSrcweir { 901*cdf0e10cSrcweir PyRef extractTraceback( 902*cdf0e10cSrcweir PyDict_GetItemString(unoModule.get(),"_uno_extract_printable_stacktrace" ) ); 903*cdf0e10cSrcweir 904*cdf0e10cSrcweir if( extractTraceback.is() ) 905*cdf0e10cSrcweir { 906*cdf0e10cSrcweir PyRef args( PyTuple_New( 1), SAL_NO_ACQUIRE ); 907*cdf0e10cSrcweir PyTuple_SetItem( args.get(), 0, excTraceback.getAcquired() ); 908*cdf0e10cSrcweir str = PyRef( PyObject_CallObject( extractTraceback.get(),args.get() ), SAL_NO_ACQUIRE); 909*cdf0e10cSrcweir } 910*cdf0e10cSrcweir else 911*cdf0e10cSrcweir { 912*cdf0e10cSrcweir str = PyRef( 913*cdf0e10cSrcweir PyString_FromString( "Couldn't find uno._uno_extract_printable_stacktrace" ), 914*cdf0e10cSrcweir SAL_NO_ACQUIRE ); 915*cdf0e10cSrcweir } 916*cdf0e10cSrcweir } 917*cdf0e10cSrcweir else 918*cdf0e10cSrcweir { 919*cdf0e10cSrcweir str = PyRef( 920*cdf0e10cSrcweir PyString_FromString( "Couldn't find uno.py, no stacktrace available" ), 921*cdf0e10cSrcweir SAL_NO_ACQUIRE ); 922*cdf0e10cSrcweir } 923*cdf0e10cSrcweir 924*cdf0e10cSrcweir } 925*cdf0e10cSrcweir else 926*cdf0e10cSrcweir { 927*cdf0e10cSrcweir // it may occur, that no traceback is given (e.g. only native code below) 928*cdf0e10cSrcweir str = PyRef( PyString_FromString( "no traceback available" ), SAL_NO_ACQUIRE); 929*cdf0e10cSrcweir } 930*cdf0e10cSrcweir 931*cdf0e10cSrcweir if( isInstanceOfStructOrException( excValue.get() ) ) 932*cdf0e10cSrcweir { 933*cdf0e10cSrcweir ret = pyObject2Any( excValue ); 934*cdf0e10cSrcweir } 935*cdf0e10cSrcweir else 936*cdf0e10cSrcweir { 937*cdf0e10cSrcweir OUStringBuffer buf; 938*cdf0e10cSrcweir PyRef typeName( PyObject_Str( excType.get() ), SAL_NO_ACQUIRE ); 939*cdf0e10cSrcweir if( typeName.is() ) 940*cdf0e10cSrcweir { 941*cdf0e10cSrcweir buf.appendAscii( PyString_AsString( typeName.get() ) ); 942*cdf0e10cSrcweir } 943*cdf0e10cSrcweir else 944*cdf0e10cSrcweir { 945*cdf0e10cSrcweir buf.appendAscii( "no typename available" ); 946*cdf0e10cSrcweir } 947*cdf0e10cSrcweir buf.appendAscii( ": " ); 948*cdf0e10cSrcweir PyRef valueRep( PyObject_Str( excValue.get() ), SAL_NO_ACQUIRE ); 949*cdf0e10cSrcweir if( valueRep.is() ) 950*cdf0e10cSrcweir { 951*cdf0e10cSrcweir buf.appendAscii( PyString_AsString( valueRep.get())); 952*cdf0e10cSrcweir } 953*cdf0e10cSrcweir else 954*cdf0e10cSrcweir { 955*cdf0e10cSrcweir buf.appendAscii( "Couldn't convert exception value to a string" ); 956*cdf0e10cSrcweir } 957*cdf0e10cSrcweir buf.appendAscii( ", traceback follows\n" ); 958*cdf0e10cSrcweir if( str.is() ) 959*cdf0e10cSrcweir { 960*cdf0e10cSrcweir buf.appendAscii( PyString_AsString( str.get() ) ); 961*cdf0e10cSrcweir } 962*cdf0e10cSrcweir else 963*cdf0e10cSrcweir { 964*cdf0e10cSrcweir buf.appendAscii( ", no traceback available\n" ); 965*cdf0e10cSrcweir } 966*cdf0e10cSrcweir RuntimeException e; 967*cdf0e10cSrcweir e.Message = buf.makeStringAndClear(); 968*cdf0e10cSrcweir ret = com::sun::star::uno::makeAny( e ); 969*cdf0e10cSrcweir } 970*cdf0e10cSrcweir return ret; 971*cdf0e10cSrcweir } 972*cdf0e10cSrcweir 973*cdf0e10cSrcweir 974*cdf0e10cSrcweir static const char * g_NUMERICID = "pyuno.lcNumeric"; 975*cdf0e10cSrcweir static ::std::vector< rtl::OString > g_localeList; 976*cdf0e10cSrcweir 977*cdf0e10cSrcweir static const char *ensureUnlimitedLifetime( const char *str ) 978*cdf0e10cSrcweir { 979*cdf0e10cSrcweir int size = g_localeList.size(); 980*cdf0e10cSrcweir int i; 981*cdf0e10cSrcweir for( i = 0 ; i < size ; i ++ ) 982*cdf0e10cSrcweir { 983*cdf0e10cSrcweir if( 0 == strcmp( g_localeList[i].getStr(), str ) ) 984*cdf0e10cSrcweir break; 985*cdf0e10cSrcweir } 986*cdf0e10cSrcweir if( i == size ) 987*cdf0e10cSrcweir { 988*cdf0e10cSrcweir g_localeList.push_back( str ); 989*cdf0e10cSrcweir } 990*cdf0e10cSrcweir return g_localeList[i].getStr(); 991*cdf0e10cSrcweir } 992*cdf0e10cSrcweir 993*cdf0e10cSrcweir 994*cdf0e10cSrcweir PyThreadAttach::PyThreadAttach( PyInterpreterState *interp) 995*cdf0e10cSrcweir throw ( com::sun::star::uno::RuntimeException ) 996*cdf0e10cSrcweir { 997*cdf0e10cSrcweir tstate = PyThreadState_New( interp ); 998*cdf0e10cSrcweir if( !tstate ) 999*cdf0e10cSrcweir throw RuntimeException( 1000*cdf0e10cSrcweir OUString(RTL_CONSTASCII_USTRINGPARAM( "Couldn't create a pythreadstate" ) ), 1001*cdf0e10cSrcweir Reference< XInterface > () ); 1002*cdf0e10cSrcweir PyEval_AcquireThread( tstate); 1003*cdf0e10cSrcweir // set LC_NUMERIC to "C" 1004*cdf0e10cSrcweir const char * oldLocale = 1005*cdf0e10cSrcweir ensureUnlimitedLifetime( setlocale( LC_NUMERIC, 0 ) ); 1006*cdf0e10cSrcweir setlocale( LC_NUMERIC, "C" ); 1007*cdf0e10cSrcweir PyRef locale( // python requires C locale 1008*cdf0e10cSrcweir PyLong_FromVoidPtr( (void*)oldLocale ), SAL_NO_ACQUIRE); 1009*cdf0e10cSrcweir PyDict_SetItemString( 1010*cdf0e10cSrcweir PyThreadState_GetDict(), g_NUMERICID, locale.get() ); 1011*cdf0e10cSrcweir } 1012*cdf0e10cSrcweir 1013*cdf0e10cSrcweir PyThreadAttach::~PyThreadAttach() 1014*cdf0e10cSrcweir { 1015*cdf0e10cSrcweir PyObject *value = 1016*cdf0e10cSrcweir PyDict_GetItemString( PyThreadState_GetDict( ), g_NUMERICID ); 1017*cdf0e10cSrcweir if( value ) 1018*cdf0e10cSrcweir setlocale( LC_NUMERIC, (const char * ) PyLong_AsVoidPtr( value ) ); 1019*cdf0e10cSrcweir PyThreadState_Clear( tstate ); 1020*cdf0e10cSrcweir PyEval_ReleaseThread( tstate ); 1021*cdf0e10cSrcweir PyThreadState_Delete( tstate ); 1022*cdf0e10cSrcweir 1023*cdf0e10cSrcweir } 1024*cdf0e10cSrcweir 1025*cdf0e10cSrcweir PyThreadDetach::PyThreadDetach() throw ( com::sun::star::uno::RuntimeException ) 1026*cdf0e10cSrcweir { 1027*cdf0e10cSrcweir tstate = PyThreadState_Get(); 1028*cdf0e10cSrcweir PyObject *value = 1029*cdf0e10cSrcweir PyDict_GetItemString( PyThreadState_GetDict( ), g_NUMERICID ); 1030*cdf0e10cSrcweir if( value ) 1031*cdf0e10cSrcweir setlocale( LC_NUMERIC, (const char * ) PyLong_AsVoidPtr( value ) ); 1032*cdf0e10cSrcweir PyEval_ReleaseThread( tstate ); 1033*cdf0e10cSrcweir } 1034*cdf0e10cSrcweir 1035*cdf0e10cSrcweir /** Acquires the global interpreter lock again 1036*cdf0e10cSrcweir 1037*cdf0e10cSrcweir */ 1038*cdf0e10cSrcweir PyThreadDetach::~PyThreadDetach() 1039*cdf0e10cSrcweir { 1040*cdf0e10cSrcweir PyEval_AcquireThread( tstate ); 1041*cdf0e10cSrcweir // PyObject *value = 1042*cdf0e10cSrcweir // PyDict_GetItemString( PyThreadState_GetDict( ), g_NUMERICID ); 1043*cdf0e10cSrcweir 1044*cdf0e10cSrcweir // python requires C LC_NUMERIC locale, 1045*cdf0e10cSrcweir // always set even when it is already "C" 1046*cdf0e10cSrcweir setlocale( LC_NUMERIC, "C" ); 1047*cdf0e10cSrcweir } 1048*cdf0e10cSrcweir 1049*cdf0e10cSrcweir 1050*cdf0e10cSrcweir PyRef RuntimeCargo::getUnoModule() 1051*cdf0e10cSrcweir { 1052*cdf0e10cSrcweir if( ! dictUnoModule.is() ) 1053*cdf0e10cSrcweir { 1054*cdf0e10cSrcweir dictUnoModule = importUnoModule(); 1055*cdf0e10cSrcweir } 1056*cdf0e10cSrcweir return dictUnoModule; 1057*cdf0e10cSrcweir } 1058*cdf0e10cSrcweir } 1059