167c7d1c1SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 367c7d1c1SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 467c7d1c1SAndrew Rist * or more contributor license agreements. See the NOTICE file 567c7d1c1SAndrew Rist * distributed with this work for additional information 667c7d1c1SAndrew Rist * regarding copyright ownership. The ASF licenses this file 767c7d1c1SAndrew Rist * to you under the Apache License, Version 2.0 (the 867c7d1c1SAndrew Rist * "License"); you may not use this file except in compliance 967c7d1c1SAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 1167c7d1c1SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 1367c7d1c1SAndrew Rist * Unless required by applicable law or agreed to in writing, 1467c7d1c1SAndrew Rist * software distributed under the License is distributed on an 1567c7d1c1SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 1667c7d1c1SAndrew Rist * KIND, either express or implied. See the License for the 1767c7d1c1SAndrew Rist * specific language governing permissions and limitations 1867c7d1c1SAndrew Rist * under the License. 19cdf0e10cSrcweir * 2067c7d1c1SAndrew Rist *************************************************************/ 2167c7d1c1SAndrew Rist 2267c7d1c1SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir #include <pyuno/pyuno.hxx> 25cdf0e10cSrcweir 26cdf0e10cSrcweir #include <osl/process.h> 27cdf0e10cSrcweir #include <osl/file.h> 28cdf0e10cSrcweir #include <osl/thread.h> 29cdf0e10cSrcweir 30cdf0e10cSrcweir #include <rtl/ustrbuf.hxx> 31cdf0e10cSrcweir #include <rtl/strbuf.hxx> 32cdf0e10cSrcweir #include <rtl/bootstrap.hxx> 33cdf0e10cSrcweir 34cdf0e10cSrcweir #include <cppuhelper/implementationentry.hxx> 35cdf0e10cSrcweir #include <cppuhelper/factory.hxx> 36cdf0e10cSrcweir 37cdf0e10cSrcweir using rtl::OUString; 38cdf0e10cSrcweir using rtl::OUStringBuffer; 39cdf0e10cSrcweir using rtl::OString; 40cdf0e10cSrcweir 41cdf0e10cSrcweir using pyuno::PyRef; 42cdf0e10cSrcweir using pyuno::Runtime; 43cdf0e10cSrcweir using pyuno::PyThreadAttach; 44cdf0e10cSrcweir 45cdf0e10cSrcweir using com::sun::star::registry::XRegistryKey; 46cdf0e10cSrcweir using com::sun::star::uno::Reference; 47cdf0e10cSrcweir using com::sun::star::uno::XInterface; 48cdf0e10cSrcweir using com::sun::star::uno::Sequence; 49cdf0e10cSrcweir using com::sun::star::uno::XComponentContext; 50cdf0e10cSrcweir using com::sun::star::uno::RuntimeException; 51cdf0e10cSrcweir 52cdf0e10cSrcweir namespace pyuno_loader 53cdf0e10cSrcweir { 54cdf0e10cSrcweir 55cdf0e10cSrcweir static void raiseRuntimeExceptionWhenNeeded() throw ( RuntimeException ) 56cdf0e10cSrcweir { 57cdf0e10cSrcweir if( PyErr_Occurred() ) 58cdf0e10cSrcweir { 59cdf0e10cSrcweir PyRef excType, excValue, excTraceback; 60cdf0e10cSrcweir PyErr_Fetch( (PyObject **)&excType, (PyObject**)&excValue,(PyObject**)&excTraceback); 61cdf0e10cSrcweir Runtime runtime; 62cdf0e10cSrcweir com::sun::star::uno::Any a = runtime.extractUnoException( excType, excValue, excTraceback ); 63cdf0e10cSrcweir OUStringBuffer buf; 64cdf0e10cSrcweir buf.appendAscii( "python-loader:" ); 65cdf0e10cSrcweir if( a.hasValue() ) 66cdf0e10cSrcweir buf.append( ((com::sun::star::uno::Exception *)a.getValue())->Message ); 67cdf0e10cSrcweir throw RuntimeException( buf.makeStringAndClear(), Reference< XInterface> () ); 68cdf0e10cSrcweir } 69cdf0e10cSrcweir } 70cdf0e10cSrcweir 71cdf0e10cSrcweir static PyRef getLoaderModule() throw( RuntimeException ) 72cdf0e10cSrcweir { 73cdf0e10cSrcweir PyRef module( 74cdf0e10cSrcweir PyImport_ImportModule( const_cast< char * >("pythonloader") ), 75cdf0e10cSrcweir SAL_NO_ACQUIRE ); 76cdf0e10cSrcweir raiseRuntimeExceptionWhenNeeded(); 77cdf0e10cSrcweir if( !module.is() ) 78cdf0e10cSrcweir { 79cdf0e10cSrcweir throw RuntimeException( 80cdf0e10cSrcweir OUString( RTL_CONSTASCII_USTRINGPARAM( "pythonloader: Couldn't load pythonloader module" ) ), 81cdf0e10cSrcweir Reference< XInterface > () ); 82cdf0e10cSrcweir } 83cdf0e10cSrcweir return PyRef( PyModule_GetDict( module.get() )); 84cdf0e10cSrcweir } 85cdf0e10cSrcweir 86cdf0e10cSrcweir static PyRef getObjectFromLoaderModule( const char * func ) 87cdf0e10cSrcweir throw ( RuntimeException ) 88cdf0e10cSrcweir { 89cdf0e10cSrcweir PyRef object( PyDict_GetItemString(getLoaderModule().get(), (char*)func ) ); 90cdf0e10cSrcweir if( !object.is() ) 91cdf0e10cSrcweir { 92cdf0e10cSrcweir OUStringBuffer buf; 93cdf0e10cSrcweir buf.appendAscii( "pythonloader: couldn't find core element pythonloader." ); 94cdf0e10cSrcweir buf.appendAscii( func ); 95cdf0e10cSrcweir throw RuntimeException(buf.makeStringAndClear(),Reference< XInterface >()); 96cdf0e10cSrcweir } 97cdf0e10cSrcweir return object; 98cdf0e10cSrcweir } 99cdf0e10cSrcweir 100cdf0e10cSrcweir OUString getImplementationName() 101cdf0e10cSrcweir { 102cdf0e10cSrcweir return OUString( RTL_CONSTASCII_USTRINGPARAM( "org.openoffice.comp.pyuno.Loader" ) ); 103cdf0e10cSrcweir } 104cdf0e10cSrcweir 105cdf0e10cSrcweir Sequence< OUString > getSupportedServiceNames() 106cdf0e10cSrcweir { 107cdf0e10cSrcweir OUString serviceName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.loader.Python" ) ); 108cdf0e10cSrcweir return Sequence< OUString > ( &serviceName, 1 ); 109cdf0e10cSrcweir } 110cdf0e10cSrcweir 111cdf0e10cSrcweir static void setPythonHome ( const OUString & pythonHome ) 112cdf0e10cSrcweir { 113cdf0e10cSrcweir OUString systemPythonHome; 114cdf0e10cSrcweir osl_getSystemPathFromFileURL( pythonHome.pData, &(systemPythonHome.pData) ); 115cdf0e10cSrcweir OString o = rtl::OUStringToOString( systemPythonHome, osl_getThreadTextEncoding() ); 116*d1c4de67SPedro Giffuni #if PY_MAJOR_VERSION < 3 || PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION <= 4 117cdf0e10cSrcweir rtl_string_acquire(o.pData); // leak this string (thats the api!) 118cdf0e10cSrcweir Py_SetPythonHome( o.pData->buffer); 119*d1c4de67SPedro Giffuni #else 120*d1c4de67SPedro Giffuni wchar_t *wpath = Py_DecodeLocale(o.pData->buffer, NULL); 121*d1c4de67SPedro Giffuni if (wpath == NULL) { 122*d1c4de67SPedro Giffuni PyErr_SetString(PyExc_SystemError, "Cannot decode python home path"); 123*d1c4de67SPedro Giffuni return; 124*d1c4de67SPedro Giffuni } 125*d1c4de67SPedro Giffuni Py_SetPythonHome(wpath); 126*d1c4de67SPedro Giffuni #endif 127cdf0e10cSrcweir } 128cdf0e10cSrcweir 129cdf0e10cSrcweir static void prependPythonPath( const OUString & pythonPathBootstrap ) 130cdf0e10cSrcweir { 131cdf0e10cSrcweir rtl::OUStringBuffer bufPYTHONPATH( 256 ); 132cdf0e10cSrcweir sal_Int32 nIndex = 0; 133cdf0e10cSrcweir while( 1 ) 134cdf0e10cSrcweir { 135cdf0e10cSrcweir sal_Int32 nNew = pythonPathBootstrap.indexOf( ' ', nIndex ); 136cdf0e10cSrcweir OUString fileUrl; 137cdf0e10cSrcweir if( nNew == -1 ) 138cdf0e10cSrcweir { 139cdf0e10cSrcweir fileUrl = OUString( &( pythonPathBootstrap[nIndex] ) ); 140cdf0e10cSrcweir } 141cdf0e10cSrcweir else 142cdf0e10cSrcweir { 143cdf0e10cSrcweir fileUrl = OUString( &(pythonPathBootstrap[nIndex]) , nNew - nIndex ); 144cdf0e10cSrcweir } 145cdf0e10cSrcweir OUString systemPath; 146cdf0e10cSrcweir osl_getSystemPathFromFileURL( fileUrl.pData, &(systemPath.pData) ); 147cdf0e10cSrcweir bufPYTHONPATH.append( systemPath ); 148cdf0e10cSrcweir bufPYTHONPATH.append( static_cast<sal_Unicode>(SAL_PATHSEPARATOR) ); 149cdf0e10cSrcweir if( nNew == -1 ) 150cdf0e10cSrcweir break; 151cdf0e10cSrcweir nIndex = nNew + 1; 152cdf0e10cSrcweir } 153cdf0e10cSrcweir const char * oldEnv = getenv( "PYTHONPATH"); 154cdf0e10cSrcweir if( oldEnv ) 155cdf0e10cSrcweir bufPYTHONPATH.append( rtl::OUString(oldEnv, strlen(oldEnv), osl_getThreadTextEncoding()) ); 156cdf0e10cSrcweir 157cdf0e10cSrcweir rtl::OUString envVar(RTL_CONSTASCII_USTRINGPARAM("PYTHONPATH")); 158cdf0e10cSrcweir rtl::OUString envValue(bufPYTHONPATH.makeStringAndClear()); 159cdf0e10cSrcweir osl_setEnvironment(envVar.pData, envValue.pData); 160cdf0e10cSrcweir } 161cdf0e10cSrcweir 162cdf0e10cSrcweir Reference< XInterface > CreateInstance( const Reference< XComponentContext > & ctx ) 163cdf0e10cSrcweir { 164cdf0e10cSrcweir Reference< XInterface > ret; 165cdf0e10cSrcweir 166cdf0e10cSrcweir if( ! Py_IsInitialized() ) 167cdf0e10cSrcweir { 168cdf0e10cSrcweir OUString pythonPath; 169cdf0e10cSrcweir OUString pythonHome; 170cdf0e10cSrcweir OUString path( RTL_CONSTASCII_USTRINGPARAM( "$OOO_BASE_DIR/program/" SAL_CONFIGFILE("pythonloader.uno" ))); 171cdf0e10cSrcweir rtl::Bootstrap::expandMacros(path); //TODO: detect failure 172cdf0e10cSrcweir rtl::Bootstrap bootstrap(path); 173cdf0e10cSrcweir 174cdf0e10cSrcweir // look for pythonhome 175cdf0e10cSrcweir bootstrap.getFrom( OUString( RTL_CONSTASCII_USTRINGPARAM( "PYUNO_LOADER_PYTHONHOME") ), pythonHome ); 176cdf0e10cSrcweir bootstrap.getFrom( OUString( RTL_CONSTASCII_USTRINGPARAM( "PYUNO_LOADER_PYTHONPATH" ) ) , pythonPath ); 177cdf0e10cSrcweir 178cdf0e10cSrcweir // pythonhome+pythonpath must be set before Py_Initialize(), otherwise there appear warning on the console 179cdf0e10cSrcweir // sadly, there is no api for setting the pythonpath, we have to use the environment variable 180cdf0e10cSrcweir if( pythonHome.getLength() ) 181cdf0e10cSrcweir setPythonHome( pythonHome ); 182cdf0e10cSrcweir 183cdf0e10cSrcweir if( pythonPath.getLength() ) 184cdf0e10cSrcweir prependPythonPath( pythonPath ); 185cdf0e10cSrcweir 186cdf0e10cSrcweir // initialize python 187cdf0e10cSrcweir Py_Initialize(); 188cdf0e10cSrcweir PyEval_InitThreads(); 189cdf0e10cSrcweir 190cdf0e10cSrcweir PyThreadState *tstate = PyThreadState_Get(); 191cdf0e10cSrcweir PyEval_ReleaseThread( tstate ); 192cdf0e10cSrcweir } 193cdf0e10cSrcweir 194cdf0e10cSrcweir PyThreadAttach attach( PyInterpreterState_Head() ); 195cdf0e10cSrcweir { 196cdf0e10cSrcweir if( ! Runtime::isInitialized() ) 197cdf0e10cSrcweir { 198cdf0e10cSrcweir Runtime::initialize( ctx ); 199cdf0e10cSrcweir } 200cdf0e10cSrcweir Runtime runtime; 201cdf0e10cSrcweir 202cdf0e10cSrcweir PyRef pyCtx = runtime.any2PyObject( 203cdf0e10cSrcweir com::sun::star::uno::makeAny( ctx ) ); 204cdf0e10cSrcweir 205cdf0e10cSrcweir PyRef clazz = getObjectFromLoaderModule( "Loader" ); 206cdf0e10cSrcweir PyRef args ( PyTuple_New( 1 ), SAL_NO_ACQUIRE ); 207cdf0e10cSrcweir PyTuple_SetItem( args.get(), 0 , pyCtx.getAcquired() ); 208cdf0e10cSrcweir PyRef pyInstance( PyObject_CallObject( clazz.get() , args.get() ), SAL_NO_ACQUIRE ); 209cdf0e10cSrcweir runtime.pyObject2Any( pyInstance ) >>= ret; 210cdf0e10cSrcweir } 211cdf0e10cSrcweir return ret; 212cdf0e10cSrcweir } 213cdf0e10cSrcweir 214cdf0e10cSrcweir } 215cdf0e10cSrcweir 216cdf0e10cSrcweir 217cdf0e10cSrcweir static struct cppu::ImplementationEntry g_entries[] = 218cdf0e10cSrcweir { 219cdf0e10cSrcweir { 220cdf0e10cSrcweir pyuno_loader::CreateInstance, pyuno_loader::getImplementationName, 221cdf0e10cSrcweir pyuno_loader::getSupportedServiceNames, cppu::createSingleComponentFactory, 222cdf0e10cSrcweir 0 , 0 223cdf0e10cSrcweir }, 224cdf0e10cSrcweir { 0, 0, 0, 0, 0, 0 } 225cdf0e10cSrcweir }; 226cdf0e10cSrcweir 227cdf0e10cSrcweir extern "C" 228cdf0e10cSrcweir { 229cdf0e10cSrcweir 230cdf0e10cSrcweir //================================================================================================== 2311c25f252Sdamjan SAL_DLLPUBLIC_EXPORT void SAL_CALL component_getImplementationEnvironment( 232cdf0e10cSrcweir const sal_Char ** ppEnvTypeName, uno_Environment ** ) 233cdf0e10cSrcweir { 234cdf0e10cSrcweir *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME; 235cdf0e10cSrcweir } 236cdf0e10cSrcweir //================================================================================================== 2371c25f252Sdamjan SAL_DLLPUBLIC_EXPORT void * SAL_CALL component_getFactory( 238cdf0e10cSrcweir const sal_Char * pImplName, void * pServiceManager, void * pRegistryKey ) 239cdf0e10cSrcweir { 240cdf0e10cSrcweir return cppu::component_getFactoryHelper( pImplName, pServiceManager, pRegistryKey , g_entries ); 241cdf0e10cSrcweir } 242cdf0e10cSrcweir 243cdf0e10cSrcweir } 244cdf0e10cSrcweir 245