LCOV - code coverage report
Current view: top level - libreoffice/pyuno/source/loader - pyuno_loader.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 96 0.0 %
Date: 2012-12-27 Functions: 0 9 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include <pyuno/pyuno.hxx>
      21             : 
      22             : #include <osl/process.h>
      23             : #include <osl/file.hxx>
      24             : #include <osl/thread.h>
      25             : 
      26             : #include <rtl/ustrbuf.hxx>
      27             : #include <rtl/strbuf.hxx>
      28             : #include <rtl/bootstrap.hxx>
      29             : 
      30             : #include <cppuhelper/implementationentry.hxx>
      31             : #include <cppuhelper/factory.hxx>
      32             : 
      33             : // apparently PATH_MAX is not standard and not defined by MSVC
      34             : #ifndef PATH_MAX
      35             : #ifdef _MAX_PATH
      36             : #define PATH_MAX _MAX_PATH
      37             : #else
      38             : #ifdef MAX_PATH
      39             : #define PATH_MAX MAX_PATH
      40             : #else
      41             : #error no PATH_MAX
      42             : #endif
      43             : #endif
      44             : #endif
      45             : 
      46             : using rtl::OUString;
      47             : using rtl::OUStringBuffer;
      48             : using rtl::OString;
      49             : 
      50             : using pyuno::PyRef;
      51             : using pyuno::Runtime;
      52             : using pyuno::PyThreadAttach;
      53             : 
      54             : using com::sun::star::registry::XRegistryKey;
      55             : using com::sun::star::uno::Reference;
      56             : using com::sun::star::uno::XInterface;
      57             : using com::sun::star::uno::Sequence;
      58             : using com::sun::star::uno::XComponentContext;
      59             : using com::sun::star::uno::RuntimeException;
      60             : 
      61             : namespace pyuno_loader
      62             : {
      63             : 
      64           0 : static void raiseRuntimeExceptionWhenNeeded() throw ( RuntimeException )
      65             : {
      66           0 :     if( PyErr_Occurred() )
      67             :     {
      68           0 :         PyRef excType, excValue, excTraceback;
      69           0 :         PyErr_Fetch( (PyObject **)&excType, (PyObject**)&excValue,(PyObject**)&excTraceback);
      70           0 :         Runtime runtime;
      71           0 :         com::sun::star::uno::Any a = runtime.extractUnoException( excType, excValue, excTraceback );
      72           0 :         OUStringBuffer buf;
      73           0 :         buf.appendAscii( "python-loader:" );
      74           0 :         if( a.hasValue() )
      75           0 :             buf.append( ((com::sun::star::uno::Exception *)a.getValue())->Message );
      76           0 :         throw RuntimeException( buf.makeStringAndClear(), Reference< XInterface> () );
      77             :     }
      78           0 : }
      79             : 
      80           0 : static PyRef getLoaderModule() throw( RuntimeException )
      81             : {
      82             :     PyRef module(
      83             :         PyImport_ImportModule( "pythonloader" ),
      84           0 :         SAL_NO_ACQUIRE );
      85           0 :     raiseRuntimeExceptionWhenNeeded();
      86           0 :     if( !module.is() )
      87             :     {
      88             :         throw RuntimeException(
      89             :             OUString( RTL_CONSTASCII_USTRINGPARAM( "pythonloader: Couldn't load pythonloader module" ) ),
      90           0 :             Reference< XInterface > () );
      91             :     }
      92           0 :     return PyRef( PyModule_GetDict( module.get() ));
      93             : }
      94             : 
      95           0 : static PyRef getObjectFromLoaderModule( const char * func )
      96             :     throw ( RuntimeException )
      97             : {
      98           0 :     PyRef object( PyDict_GetItemString(getLoaderModule().get(), (char*)func ) );
      99           0 :     if( !object.is() )
     100             :     {
     101           0 :         OUStringBuffer buf;
     102           0 :         buf.appendAscii( "pythonloader: couldn't find core element pythonloader." );
     103           0 :         buf.appendAscii( func );
     104           0 :         throw RuntimeException(buf.makeStringAndClear(),Reference< XInterface >());
     105             :     }
     106           0 :     return object;
     107             : }
     108             : 
     109           0 : OUString getImplementationName()
     110             : {
     111           0 :     return OUString( RTL_CONSTASCII_USTRINGPARAM( "org.openoffice.comp.pyuno.Loader" ) );
     112             : }
     113             : 
     114           0 : Sequence< OUString > getSupportedServiceNames()
     115             : {
     116           0 :     OUString serviceName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.loader.Python" ) );
     117           0 :     return Sequence< OUString > ( &serviceName, 1 );
     118             : }
     119             : 
     120           0 : static void setPythonHome ( const OUString & pythonHome )
     121             : {
     122           0 :     OUString systemPythonHome;
     123           0 :     osl_getSystemPathFromFileURL( pythonHome.pData, &(systemPythonHome.pData) );
     124           0 :     OString o = rtl::OUStringToOString( systemPythonHome, osl_getThreadTextEncoding() );
     125             : #if PY_MAJOR_VERSION >= 3
     126             :     // static because Py_SetPythonHome just copies the "wide" pointer
     127             :     static wchar_t wide[PATH_MAX + 1];
     128           0 :     size_t len = mbstowcs(wide, o.pData->buffer, PATH_MAX + 1);
     129           0 :     if(len == (size_t)-1)
     130             :     {
     131           0 :         PyErr_SetString(PyExc_SystemError, "invalid multibyte sequence in python home path");
     132             :         return;
     133             :     }
     134           0 :     if(len == PATH_MAX + 1)
     135             :     {
     136           0 :         PyErr_SetString(PyExc_SystemError, "python home path is too long");
     137             :         return;
     138             :     }
     139           0 :     Py_SetPythonHome(wide);
     140             : #else
     141             :     rtl_string_acquire(o.pData); // increase reference count
     142             :     Py_SetPythonHome(o.pData->buffer);
     143             : #endif
     144             : }
     145             : 
     146           0 : static void prependPythonPath( const OUString & pythonPathBootstrap )
     147             : {
     148           0 :     rtl::OUStringBuffer bufPYTHONPATH( 256 );
     149           0 :     sal_Int32 nIndex = 0;
     150           0 :     while( 1 )
     151             :     {
     152           0 :         sal_Int32 nNew = pythonPathBootstrap.indexOf( ' ', nIndex );
     153           0 :         OUString fileUrl;
     154           0 :         if( nNew == -1 )
     155             :         {
     156           0 :             fileUrl = pythonPathBootstrap.copy(nIndex);
     157             :         }
     158             :         else
     159             :         {
     160           0 :             fileUrl = pythonPathBootstrap.copy(nIndex, nNew - nIndex);
     161             :         }
     162           0 :         OUString systemPath;
     163           0 :         osl_getSystemPathFromFileURL( fileUrl.pData, &(systemPath.pData) );
     164           0 :         bufPYTHONPATH.append( systemPath );
     165           0 :         bufPYTHONPATH.append( static_cast<sal_Unicode>(SAL_PATHSEPARATOR) );
     166           0 :         if( nNew == -1 )
     167             :             break;
     168           0 :         nIndex = nNew + 1;
     169           0 :     }
     170           0 :     const char * oldEnv = getenv( "PYTHONPATH");
     171           0 :     if( oldEnv )
     172           0 :         bufPYTHONPATH.append( rtl::OUString(oldEnv, strlen(oldEnv), osl_getThreadTextEncoding()) );
     173             : 
     174           0 :     rtl::OUString envVar(RTL_CONSTASCII_USTRINGPARAM("PYTHONPATH"));
     175           0 :     rtl::OUString envValue(bufPYTHONPATH.makeStringAndClear());
     176           0 :     osl_setEnvironment(envVar.pData, envValue.pData);
     177           0 : }
     178             : 
     179           0 : Reference< XInterface > CreateInstance( const Reference< XComponentContext > & ctx )
     180             : {
     181           0 :     Reference< XInterface > ret;
     182             : 
     183           0 :     if( ! Py_IsInitialized() )
     184             :     {
     185           0 :         OUString pythonPath;
     186           0 :         OUString pythonHome;
     187           0 :         OUString path( RTL_CONSTASCII_USTRINGPARAM( "$BRAND_BASE_DIR/program/" SAL_CONFIGFILE("pythonloader.uno" )));
     188           0 :         rtl::Bootstrap::expandMacros(path); //TODO: detect failure
     189           0 :         rtl::Bootstrap bootstrap(path);
     190             : 
     191             :         // look for pythonhome
     192           0 :         bootstrap.getFrom( OUString( RTL_CONSTASCII_USTRINGPARAM( "PYUNO_LOADER_PYTHONHOME") ), pythonHome );
     193           0 :         bootstrap.getFrom( OUString( RTL_CONSTASCII_USTRINGPARAM( "PYUNO_LOADER_PYTHONPATH" ) ) , pythonPath );
     194             : 
     195             :         // pythonhome+pythonpath must be set before Py_Initialize(), otherwise there appear warning on the console
     196             :         // sadly, there is no api for setting the pythonpath, we have to use the environment variable
     197           0 :         if( !pythonHome.isEmpty() )
     198           0 :             setPythonHome( pythonHome );
     199             : 
     200           0 :         if( !pythonPath.isEmpty() )
     201           0 :             prependPythonPath( pythonPath );
     202             : 
     203             : #if WNT
     204             :     //extend PATH under windows to include the branddir/program so ssl libs will be found
     205             :     //for use by terminal mailmerge dependency _ssl.pyd
     206             :     rtl::OUString sEnvName(RTL_CONSTASCII_USTRINGPARAM("PATH"));
     207             :     rtl::OUString sPath;
     208             :     osl_getEnvironment(sEnvName.pData, &sPath.pData);
     209             :     rtl::OUString sBrandLocation(RTL_CONSTASCII_USTRINGPARAM("$BRAND_BASE_DIR/program"));
     210             :     rtl::Bootstrap::expandMacros(sBrandLocation);
     211             :     osl::FileBase::getSystemPathFromFileURL(sBrandLocation, sBrandLocation);
     212             :     sPath = rtl::OUStringBuffer(sPath).
     213             :         append(static_cast<sal_Unicode>(SAL_PATHSEPARATOR)).
     214             :         append(sBrandLocation).makeStringAndClear();
     215             :     osl_setEnvironment(sEnvName.pData, sPath.pData);
     216             : #endif
     217             : 
     218             : #if PY_MAJOR_VERSION >= 3
     219           0 :         PyImport_AppendInittab( (char*)"pyuno", PyInit_pyuno );
     220             : #else
     221             :         PyImport_AppendInittab( (char*)"pyuno", initpyuno );
     222             : #endif
     223             :         // initialize python
     224           0 :         Py_Initialize();
     225           0 :         PyEval_InitThreads();
     226             : 
     227           0 :         PyThreadState *tstate = PyThreadState_Get();
     228           0 :         PyEval_ReleaseThread( tstate );
     229             :     }
     230             : 
     231           0 :     PyThreadAttach attach( PyInterpreterState_Head() );
     232             :     {
     233           0 :         if( ! Runtime::isInitialized() )
     234             :         {
     235           0 :             Runtime::initialize( ctx );
     236             :         }
     237           0 :         Runtime runtime;
     238             : 
     239             :         PyRef pyCtx = runtime.any2PyObject(
     240           0 :             com::sun::star::uno::makeAny( ctx ) );
     241             : 
     242           0 :         PyRef clazz = getObjectFromLoaderModule( "Loader" );
     243           0 :         PyRef args ( PyTuple_New( 1 ), SAL_NO_ACQUIRE );
     244           0 :         PyTuple_SetItem( args.get(), 0 , pyCtx.getAcquired() );
     245           0 :         PyRef pyInstance( PyObject_CallObject( clazz.get() , args.get() ), SAL_NO_ACQUIRE );
     246           0 :         runtime.pyObject2Any( pyInstance ) >>= ret;
     247             :     }
     248           0 :     return ret;
     249             : }
     250             : 
     251             : }
     252             : 
     253             : 
     254             : static struct cppu::ImplementationEntry g_entries[] =
     255             : {
     256             :     {
     257             :         pyuno_loader::CreateInstance, pyuno_loader::getImplementationName,
     258             :         pyuno_loader::getSupportedServiceNames, cppu::createSingleComponentFactory,
     259             :         0 , 0
     260             :     },
     261             :     { 0, 0, 0, 0, 0, 0 }
     262             : };
     263             : 
     264             : extern "C"
     265             : {
     266             : 
     267           0 : SAL_DLLPUBLIC_EXPORT void * SAL_CALL pythonloader_component_getFactory(
     268             :     const sal_Char * pImplName, void * pServiceManager, void * pRegistryKey )
     269             : {
     270           0 :     return cppu::component_getFactoryHelper( pImplName, pServiceManager, pRegistryKey , g_entries );
     271             : }
     272             : 
     273             : }
     274             : 
     275             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10