LCOV - code coverage report
Current view: top level - pyuno/source/module - pyuno_module.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 193 408 47.3 %
Date: 2014-11-03 Functions: 19 28 67.9 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; eval:(c-set-style "bsd"); 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 <config_features.h>
      21             : #include <config_folders.h>
      22             : 
      23             : #include "pyuno_impl.hxx"
      24             : 
      25             : #include <boost/unordered_map.hpp>
      26             : #include <utility>
      27             : 
      28             : #include <osl/module.hxx>
      29             : #include <osl/thread.h>
      30             : #include <osl/process.h>
      31             : #include <osl/file.hxx>
      32             : 
      33             : #include <typelib/typedescription.hxx>
      34             : 
      35             : #include <rtl/ustring.hxx>
      36             : #include <rtl/strbuf.hxx>
      37             : #include <rtl/ustrbuf.hxx>
      38             : #include <rtl/uuid.h>
      39             : #include <rtl/bootstrap.hxx>
      40             : 
      41             : #include <uno/current_context.hxx>
      42             : #include <cppuhelper/bootstrap.hxx>
      43             : 
      44             : #include <com/sun/star/reflection/XConstantTypeDescription.hpp>
      45             : #include <com/sun/star/reflection/XIdlReflection.hpp>
      46             : #include <com/sun/star/reflection/XIdlClass.hpp>
      47             : #include <com/sun/star/registry/InvalidRegistryException.hpp>
      48             : 
      49             : using osl::Module;
      50             : 
      51             : 
      52             : using com::sun::star::uno::Sequence;
      53             : using com::sun::star::uno::Reference;
      54             : using com::sun::star::uno::XInterface;
      55             : using com::sun::star::uno::Any;
      56             : using com::sun::star::uno::makeAny;
      57             : using com::sun::star::uno::UNO_QUERY;
      58             : using com::sun::star::uno::RuntimeException;
      59             : using com::sun::star::uno::TypeDescription;
      60             : using com::sun::star::uno::XComponentContext;
      61             : using com::sun::star::container::NoSuchElementException;
      62             : using com::sun::star::reflection::XIdlReflection;
      63             : using com::sun::star::reflection::XIdlClass;
      64             : using com::sun::star::script::XInvocation2;
      65             : 
      66             : using namespace pyuno;
      67             : 
      68             : namespace {
      69             : 
      70             : /**
      71             :    @ index of the next to be used member in the initializer list !
      72             :  */
      73             : // LEM TODO: export member names as keyword arguments in initialiser?
      74             : // Python supports very flexible variadic functions. By marking
      75             : // variables with one asterisk (e.g. *var) the given variable is
      76             : // defined to be a tuple of all the extra arguments. By marking
      77             : // variables with two asterisks (e.g. **var) the given variable is a
      78             : // dictionary of all extra keyword arguments; the keys are strings,
      79             : // which are the names that were used to identify the arguments. If
      80             : // they exist, these arguments must be the last one in the list.
      81             : 
      82             : class fillStructState
      83             : {
      84             :     // Keyword arguments used
      85             :     PyObject *used;
      86             :     // Which structure members are initialised
      87             :     boost::unordered_map <const OUString, bool, OUStringHash> initialised;
      88             :     // How many positional arguments are consumed
      89             :     // This is always the so-many first ones
      90             :     sal_Int32 nPosConsumed;
      91             : 
      92             : public:
      93          54 :     fillStructState()
      94          54 :         : used (PyDict_New())
      95             :         , initialised ()
      96          54 :         , nPosConsumed (0)
      97             :     {
      98          54 :         if ( ! used )
      99           0 :             throw RuntimeException("pyuno._createUnoStructHelper failed to create new dictionary");
     100          54 :     }
     101          54 :     ~fillStructState()
     102          54 :     {
     103          54 :         Py_DECREF(used);
     104          54 :     }
     105           0 :     int setUsed(PyObject *key)
     106             :     {
     107           0 :         return PyDict_SetItem(used, key, Py_True);
     108             :     }
     109         232 :     void setInitialised(const OUString& key, sal_Int32 pos = -1)
     110             :     {
     111         232 :         if (initialised[key])
     112             :         {
     113           0 :             OUStringBuffer buf;
     114           0 :             buf.appendAscii( "pyuno._createUnoStructHelper: member '");
     115           0 :             buf.append(key);
     116           0 :             buf.appendAscii( "'");
     117           0 :             if ( pos >= 0 )
     118             :             {
     119           0 :                 buf.appendAscii( " at position ");
     120           0 :                 buf.append(pos);
     121             :             }
     122           0 :             buf.appendAscii( " initialised multiple times.");
     123           0 :             throw RuntimeException(buf.makeStringAndClear());
     124             :         }
     125         232 :         initialised[key] = true;
     126         232 :         if ( pos >= 0 )
     127         232 :             ++nPosConsumed;
     128         232 :     }
     129         232 :     bool isInitialised(const OUString& key)
     130             :     {
     131         232 :         return initialised[key];
     132             :     }
     133          54 :     PyObject *getUsed() const
     134             :     {
     135          54 :         return used;
     136             :     }
     137         348 :     sal_Int32 getCntConsumed() const
     138             :     {
     139         348 :         return nPosConsumed;
     140             :     }
     141             : };
     142             : 
     143          62 : static void fillStruct(
     144             :     const Reference< XInvocation2 > &inv,
     145             :     typelib_CompoundTypeDescription *pCompType,
     146             :     PyObject *initializer,
     147             :     PyObject *kwinitializer,
     148             :     fillStructState &state,
     149             :     const Runtime &runtime) throw ( RuntimeException )
     150             : {
     151          62 :     if( pCompType->pBaseTypeDescription )
     152           8 :         fillStruct( inv, pCompType->pBaseTypeDescription, initializer, kwinitializer, state, runtime );
     153             : 
     154          62 :     const sal_Int32 nMembers = pCompType->nMembers;
     155             :     {
     156         294 :         for( int i = 0 ; i < nMembers ; i ++ )
     157             :         {
     158         232 :             const OUString OUMemberName (pCompType->ppMemberNames[i]);
     159             :             PyObject *pyMemberName =
     160             :                 PyStr_FromString(OUStringToOString(OUMemberName,
     161         232 :                         RTL_TEXTENCODING_UTF8).getStr());
     162         232 :             if ( PyObject *element = PyDict_GetItem(kwinitializer, pyMemberName ) )
     163             :             {
     164           0 :                 state.setInitialised(OUMemberName);
     165           0 :                 state.setUsed(pyMemberName);
     166           0 :                 Any a = runtime.pyObject2Any( element, ACCEPT_UNO_ANY );
     167           0 :                 inv->setValue( OUMemberName, a );
     168             :             }
     169         232 :         }
     170             :     }
     171             :     {
     172          62 :         const int remainingPosInitialisers = PyTuple_Size(initializer) - state.getCntConsumed();
     173         294 :         for( int i = 0 ; i < remainingPosInitialisers && i < nMembers ; i ++ )
     174             :         {
     175         232 :             const int tupleIndex = state.getCntConsumed();
     176         232 :             const OUString pMemberName (pCompType->ppMemberNames[i]);
     177         232 :             state.setInitialised(pMemberName, tupleIndex);
     178         232 :             PyObject *element = PyTuple_GetItem( initializer, tupleIndex );
     179         464 :             Any a = runtime.pyObject2Any( element, ACCEPT_UNO_ANY );
     180         232 :             inv->setValue( pMemberName, a );
     181         232 :         }
     182             :     }
     183         294 :     for ( int i = 0; i < nMembers ; ++i)
     184             :     {
     185         232 :         const OUString memberName (pCompType->ppMemberNames[i]);
     186         232 :         if ( ! state.isInitialised( memberName ) )
     187             :         {
     188           0 :             OUStringBuffer buf;
     189           0 :             buf.appendAscii( "pyuno._createUnoStructHelper: member '");
     190           0 :             buf.append(memberName);
     191           0 :             buf.appendAscii( "' of struct type '");
     192           0 :             buf.append(pCompType->aBase.pTypeName);
     193           0 :             buf.appendAscii( "' not given a value.");
     194           0 :             throw RuntimeException(buf.makeStringAndClear());
     195             :         }
     196         232 :     }
     197          62 : }
     198             : 
     199          34 : OUString getLibDir()
     200             : {
     201             :     static OUString *pLibDir;
     202          34 :     if( !pLibDir )
     203             :     {
     204           6 :         osl::MutexGuard guard( osl::Mutex::getGlobalMutex() );
     205           6 :         if( ! pLibDir )
     206             :         {
     207           6 :             static OUString libDir;
     208             : 
     209             :             // workarounds the $(ORIGIN) until it is available
     210           6 :             if( Module::getUrlFromAddress(
     211             :                     reinterpret_cast< oslGenericFunction >(getLibDir), libDir ) )
     212             :             {
     213           6 :                 libDir = OUString( libDir.getStr(), libDir.lastIndexOf('/' ) );
     214           6 :                 OUString name ( "PYUNOLIBDIR" );
     215           6 :                 rtl_bootstrap_set( name.pData, libDir.pData );
     216             :             }
     217           6 :             pLibDir = &libDir;
     218           6 :         }
     219             :     }
     220          34 :     return *pLibDir;
     221             : }
     222             : 
     223           0 : void raisePySystemException( const char * exceptionType, const OUString & message )
     224             : {
     225           0 :     OStringBuffer buf;
     226           0 :     buf.append( "Error during bootstrapping uno (");
     227           0 :     buf.append( exceptionType );
     228           0 :     buf.append( "):" );
     229           0 :     buf.append( OUStringToOString( message, osl_getThreadTextEncoding() ) );
     230           0 :     PyErr_SetString( PyExc_SystemError, buf.makeStringAndClear().getStr() );
     231           0 : }
     232             : 
     233             : extern "C" {
     234             : 
     235          34 : static PyObject* getComponentContext(
     236             :     SAL_UNUSED_PARAMETER PyObject*, SAL_UNUSED_PARAMETER PyObject*)
     237             : {
     238          34 :     PyRef ret;
     239             :     try
     240             :     {
     241          34 :         Reference<XComponentContext> ctx;
     242             : 
     243             :         // getLibDir() must be called in order to set bootstrap variables correctly !
     244          68 :         OUString path( getLibDir());
     245          34 :         if( Runtime::isInitialized() )
     246             :         {
     247          30 :             Runtime runtime;
     248          30 :             ctx = runtime.getImpl()->cargo->xContext;
     249             :         }
     250             :         else
     251             :         {
     252             :             // cppu::defaultBootstrap_InitialComponentContext expects
     253             :             // command line arguments to be present
     254           4 :             osl_setCommandArgs(0, 0); // fake it
     255             : 
     256           4 :             OUString iniFile;
     257           4 :             if( path.isEmpty() )
     258             :             {
     259             :                 PyErr_SetString(
     260             :                     PyExc_RuntimeError, "osl_getUrlFromAddress fails, that's why I cannot find ini "
     261           0 :                     "file for bootstrapping python uno bridge\n" );
     262           0 :                 return NULL;
     263             :             }
     264             : 
     265           8 :             OUStringBuffer iniFileName;
     266           4 :             iniFileName.append( path );
     267             : #ifdef MACOSX
     268             :             iniFileName.appendAscii( "/../" LIBO_ETC_FOLDER );
     269             : #endif
     270           4 :             iniFileName.appendAscii( "/" );
     271           4 :             iniFileName.appendAscii( SAL_CONFIGFILE( "pyuno" ) );
     272           4 :             iniFile = iniFileName.makeStringAndClear();
     273           8 :             osl::DirectoryItem item;
     274           4 :             if( osl::DirectoryItem::get( iniFile, item ) == item.E_None )
     275             :             {
     276             :                 // in case pyuno.ini exists, use this file for bootstrapping
     277           0 :                 PyThreadDetach antiguard;
     278           0 :                 ctx = cppu::defaultBootstrap_InitialComponentContext (iniFile);
     279             :             }
     280             :             else
     281             :             {
     282             :                 // defaulting to the standard bootstrapping
     283           4 :                 PyThreadDetach antiguard;
     284           4 :                 ctx = cppu::defaultBootstrap_InitialComponentContext ();
     285           4 :             }
     286             : 
     287             :         }
     288             : 
     289          34 :         if( ! Runtime::isInitialized() )
     290             :         {
     291           4 :             Runtime::initialize( ctx );
     292             :         }
     293          68 :         Runtime runtime;
     294          68 :         ret = runtime.any2PyObject( makeAny( ctx ) );
     295             :     }
     296           0 :     catch (const com::sun::star::registry::InvalidRegistryException &e)
     297             :     {
     298             :         // can't use raisePyExceptionWithAny() here, because the function
     299             :         // does any conversions, which will not work with a
     300             :         // wrongly bootstrapped pyuno!
     301           0 :         raisePySystemException( "InvalidRegistryException", e.Message );
     302             :     }
     303           0 :     catch(const com::sun::star::lang::IllegalArgumentException & e)
     304             :     {
     305           0 :         raisePySystemException( "IllegalArgumentException", e.Message );
     306             :     }
     307           0 :     catch(const com::sun::star::script::CannotConvertException & e)
     308             :     {
     309           0 :         raisePySystemException( "CannotConvertException", e.Message );
     310             :     }
     311           0 :     catch (const com::sun::star::uno::RuntimeException & e)
     312             :     {
     313           0 :         raisePySystemException( "RuntimeException", e.Message );
     314             :     }
     315           0 :     catch (const com::sun::star::uno::Exception & e)
     316             :     {
     317           0 :         raisePySystemException( "uno::Exception", e.Message );
     318             :     }
     319          34 :     return ret.getAcquired();
     320             : }
     321             : 
     322          14 : static PyObject* initTestEnvironment(
     323             :     SAL_UNUSED_PARAMETER PyObject*, SAL_UNUSED_PARAMETER PyObject*)
     324             : {
     325             :     // this tries to bootstrap enough of the soffice from python to run
     326             :     // unit tests, which is only possible indirectly because pyuno is URE
     327             :     // so load "test" library and invoke a function there to do the work
     328             :     try
     329             :     {
     330          14 :         PyObject *const ctx(getComponentContext(0, 0));
     331          14 :         if (!ctx) { abort(); }
     332          14 :         Runtime const runtime;
     333          28 :         Any const a(runtime.pyObject2Any(ctx));
     334          28 :         Reference<XComponentContext> xContext;
     335          14 :         a >>= xContext;
     336          14 :         if (!xContext.is()) { abort(); }
     337             :         using com::sun::star::lang::XMultiServiceFactory;
     338             :         Reference<XMultiServiceFactory> const xMSF(
     339          14 :             xContext->getServiceManager(),
     340          28 :             com::sun::star::uno::UNO_QUERY_THROW);
     341          14 :         if (!xMSF.is()) { abort(); }
     342          14 :         char *const testlib = getenv("TEST_LIB");
     343          14 :         if (!testlib) { abort(); }
     344          28 :         OString const libname = OString(testlib, strlen(testlib))
     345             : #ifdef _WIN32
     346             :             .replaceAll(OString('/'), OString('\\'))
     347             : #endif
     348             :             ;
     349             : 
     350          14 :         osl::Module &mod = runtime.getImpl()->cargo->testModule;
     351          14 :         mod.load(OStringToOUString(libname, osl_getThreadTextEncoding()),
     352          14 :                                 SAL_LOADMODULE_LAZY | SAL_LOADMODULE_GLOBAL);
     353          14 :         if (!mod.is()) { abort(); }
     354             :         oslGenericFunction const pFunc(
     355          14 :                 mod.getFunctionSymbol("test_init"));
     356          14 :         if (!pFunc) { abort(); }
     357             :         // guess casting pFunc is undefined behavior but don't see a better way
     358          28 :         ((void (SAL_CALL *)(XMultiServiceFactory*)) pFunc) (xMSF.get());
     359             :     }
     360           0 :     catch (const com::sun::star::uno::Exception &)
     361             :     {
     362           0 :         abort();
     363             :     }
     364          14 :     return Py_None;
     365             : }
     366             : 
     367         252 : PyObject * extractOneStringArg( PyObject *args, char const *funcName )
     368             : {
     369         252 :     if( !PyTuple_Check( args ) || PyTuple_Size( args) != 1 )
     370             :     {
     371           0 :         OStringBuffer buf;
     372           0 :         buf.append( funcName ).append( ": expecting one string argument" );
     373           0 :         PyErr_SetString( PyExc_RuntimeError, buf.getStr() );
     374           0 :         return NULL;
     375             :     }
     376         252 :     PyObject *obj = PyTuple_GetItem( args, 0 );
     377         252 :     if (!PyStr_Check(obj) && !PyUnicode_Check(obj))
     378             :     {
     379           0 :         OStringBuffer buf;
     380           0 :         buf.append( funcName ).append( ": expecting one string argument" );
     381           0 :         PyErr_SetString( PyExc_TypeError, buf.getStr());
     382           0 :         return NULL;
     383             :     }
     384         252 :     return obj;
     385             : }
     386             : 
     387          54 : static PyObject *createUnoStructHelper(
     388             :     SAL_UNUSED_PARAMETER PyObject *, PyObject* args, PyObject* keywordArgs)
     389             : {
     390          54 :     Any IdlStruct;
     391         108 :     PyRef ret;
     392             :     try
     393             :     {
     394          54 :         Runtime runtime;
     395          54 :         if( PyTuple_Size( args ) == 2 )
     396             :         {
     397          54 :             PyObject *structName = PyTuple_GetItem(args, 0);
     398          54 :             PyObject *initializer = PyTuple_GetItem(args, 1);
     399             : 
     400          54 :             if (PyStr_Check(structName))
     401             :             {
     402          54 :                 if( PyTuple_Check( initializer ) && PyDict_Check ( keywordArgs ) )
     403             :                 {
     404          54 :                     OUString typeName( OUString::createFromAscii(PyStr_AsString(structName)));
     405          54 :                     RuntimeCargo *c = runtime.getImpl()->cargo;
     406         108 :                     Reference<XIdlClass> idl_class ( c->xCoreReflection->forName (typeName),UNO_QUERY);
     407          54 :                     if (idl_class.is ())
     408             :                     {
     409          54 :                         idl_class->createObject (IdlStruct);
     410          54 :                         PyUNO *me = (PyUNO*)PyUNO_new_UNCHECKED( IdlStruct, c->xInvocation );
     411          54 :                         PyRef returnCandidate( (PyObject*)me, SAL_NO_ACQUIRE );
     412         108 :                         TypeDescription desc( typeName );
     413             :                         OSL_ASSERT( desc.is() ); // could already instantiate an XInvocation2 !
     414             : 
     415             :                         typelib_CompoundTypeDescription *pCompType =
     416          54 :                             ( typelib_CompoundTypeDescription * ) desc.get();
     417         108 :                         fillStructState state;
     418          54 :                         if ( PyTuple_Size( initializer ) > 0 || PyDict_Size( keywordArgs ) > 0 )
     419          54 :                             fillStruct( me->members->xInvocation, pCompType, initializer, keywordArgs, state, runtime );
     420          54 :                         if( state.getCntConsumed() != PyTuple_Size(initializer) )
     421             :                         {
     422           0 :                             OUStringBuffer buf;
     423           0 :                             buf.appendAscii( "pyuno._createUnoStructHelper: too many ");
     424           0 :                             buf.appendAscii( "elements in the initializer list, expected " );
     425           0 :                             buf.append( state.getCntConsumed() );
     426           0 :                             buf.appendAscii( ", got " );
     427           0 :                             buf.append( (sal_Int32) PyTuple_Size(initializer) );
     428           0 :                             throw RuntimeException( buf.makeStringAndClear());
     429             :                         }
     430         108 :                         ret = PyRef( PyTuple_Pack(2, returnCandidate.get(), state.getUsed()), SAL_NO_ACQUIRE);
     431             :                     }
     432             :                     else
     433             :                     {
     434           0 :                         OStringBuffer buf;
     435           0 :                         buf.append( "UNO struct " );
     436           0 :                         buf.append( PyStr_AsString(structName) );
     437           0 :                         buf.append( " is unknown" );
     438           0 :                         PyErr_SetString (PyExc_RuntimeError, buf.getStr());
     439          54 :                     }
     440             :                 }
     441             :                 else
     442             :                 {
     443             :                     PyErr_SetString(
     444             :                         PyExc_RuntimeError,
     445           0 :                         "pyuno._createUnoStructHelper: 2nd argument (initializer sequence) is no tuple" );
     446             :                 }
     447             :             }
     448             :             else
     449             :             {
     450           0 :                 PyErr_SetString (PyExc_AttributeError, "createUnoStruct: first argument wasn't a string");
     451             :             }
     452             :         }
     453             :         else
     454             :         {
     455           0 :             PyErr_SetString (PyExc_AttributeError, "pyuno._createUnoStructHelper: expects exactly two non-keyword arguments:\n\tStructure Name\n\tinitialiser tuple; may be the empty tuple");
     456          54 :         }
     457             :     }
     458           0 :     catch( const com::sun::star::uno::RuntimeException & e )
     459             :     {
     460           0 :         raisePyExceptionWithAny( makeAny( e ) );
     461             :     }
     462           0 :     catch( const com::sun::star::script::CannotConvertException & e )
     463             :     {
     464           0 :         raisePyExceptionWithAny( makeAny( e ) );
     465             :     }
     466           0 :     catch( const com::sun::star::uno::Exception & e )
     467             :     {
     468           0 :         raisePyExceptionWithAny( makeAny( e ) );
     469             :     }
     470         108 :     return ret.getAcquired();
     471             : }
     472             : 
     473          20 : static PyObject *getTypeByName(
     474             :     SAL_UNUSED_PARAMETER PyObject *, PyObject *args )
     475             : {
     476          20 :     PyObject * ret = NULL;
     477             : 
     478             :     try
     479             :     {
     480             :         char *name;
     481             : 
     482          20 :         if (PyArg_ParseTuple (args, "s", &name))
     483             :         {
     484          20 :             OUString typeName ( OUString::createFromAscii( name ) );
     485          40 :             TypeDescription typeDesc( typeName );
     486          20 :             if( typeDesc.is() )
     487             :             {
     488          20 :                 Runtime runtime;
     489             :                 ret = PyUNO_Type_new(
     490          20 :                     name, (com::sun::star::uno::TypeClass)typeDesc.get()->eTypeClass, runtime );
     491             :             }
     492             :             else
     493             :             {
     494           0 :                 OStringBuffer buf;
     495           0 :                 buf.append( "Type " ).append(name).append( " is unknown" );
     496           0 :                 PyErr_SetString( PyExc_RuntimeError, buf.getStr() );
     497          20 :             }
     498             :         }
     499             :     }
     500           0 :     catch ( const RuntimeException & e )
     501             :     {
     502           0 :         raisePyExceptionWithAny( makeAny( e ) );
     503             :     }
     504          20 :     return ret;
     505             : }
     506             : 
     507          58 : static PyObject *getConstantByName(
     508             :     SAL_UNUSED_PARAMETER PyObject *, PyObject *args )
     509             : {
     510          58 :     PyObject *ret = 0;
     511             :     try
     512             :     {
     513             :         char *name;
     514             : 
     515          58 :         if (PyArg_ParseTuple (args, "s", &name))
     516             :         {
     517          58 :             OUString typeName ( OUString::createFromAscii( name ) );
     518         116 :             Runtime runtime;
     519         116 :             css::uno::Reference< css::reflection::XConstantTypeDescription > td;
     520         170 :             if (!(runtime.getImpl()->cargo->xTdMgr->getByHierarchicalName(
     521          58 :                       typeName)
     522         114 :                   >>= td))
     523             :             {
     524           0 :                 OUStringBuffer buf;
     525           0 :                 buf.appendAscii( "pyuno.getConstantByName: " ).append( typeName );
     526           0 :                 buf.appendAscii( "is not a constant" );
     527           0 :                 throw RuntimeException(buf.makeStringAndClear() );
     528             :             }
     529         112 :             PyRef constant = runtime.any2PyObject( td->getConstantValue() );
     530         114 :             ret = constant.getAcquired();
     531             :         }
     532             :     }
     533           4 :     catch( const NoSuchElementException & e )
     534             :     {
     535             :         // to the python programmer, this is a runtime exception,
     536             :         // do not support tweakings with the type system
     537           2 :         RuntimeException runExc( e.Message );
     538           2 :         raisePyExceptionWithAny( makeAny( runExc ) );
     539             :     }
     540           0 :     catch(const com::sun::star::script::CannotConvertException & e)
     541             :     {
     542           0 :         raisePyExceptionWithAny( makeAny( e ) );
     543             :     }
     544           0 :     catch(const com::sun::star::lang::IllegalArgumentException & e)
     545             :     {
     546           0 :         raisePyExceptionWithAny( makeAny( e ) );
     547             :     }
     548           0 :     catch( const RuntimeException & e )
     549             :     {
     550           0 :         raisePyExceptionWithAny( makeAny(e) );
     551             :     }
     552          58 :     return ret;
     553             : }
     554             : 
     555          20 : static PyObject *checkType( SAL_UNUSED_PARAMETER PyObject *, PyObject *args )
     556             : {
     557          20 :     if( !PyTuple_Check( args ) || PyTuple_Size( args) != 1 )
     558             :     {
     559           0 :         OStringBuffer buf;
     560           0 :         buf.append( "pyuno.checkType : expecting one uno.Type argument" );
     561           0 :         PyErr_SetString( PyExc_RuntimeError, buf.getStr() );
     562           0 :         return NULL;
     563             :     }
     564          20 :     PyObject *obj = PyTuple_GetItem( args, 0 );
     565             : 
     566             :     try
     567             :     {
     568          20 :         PyType2Type( obj );
     569             :     }
     570           0 :     catch(const  RuntimeException & e)
     571             :     {
     572           0 :         raisePyExceptionWithAny( makeAny( e ) );
     573           0 :         return NULL;
     574             :     }
     575          20 :     Py_INCREF( Py_None );
     576          20 :     return Py_None;
     577             : }
     578             : 
     579         106 : static PyObject *checkEnum( SAL_UNUSED_PARAMETER PyObject *, PyObject *args )
     580             : {
     581         106 :     if( !PyTuple_Check( args ) || PyTuple_Size( args) != 1 )
     582             :     {
     583           0 :         OStringBuffer buf;
     584           0 :         buf.append( "pyuno.checkType : expecting one uno.Type argument" );
     585           0 :         PyErr_SetString( PyExc_RuntimeError, buf.getStr() );
     586           0 :         return NULL;
     587             :     }
     588         106 :     PyObject *obj = PyTuple_GetItem( args, 0 );
     589             : 
     590             :     try
     591             :     {
     592         106 :         PyEnum2Enum( obj );
     593             :     }
     594          58 :     catch(const RuntimeException & e)
     595             :     {
     596          58 :         raisePyExceptionWithAny( makeAny( e) );
     597          58 :         return NULL;
     598             :     }
     599          48 :     Py_INCREF( Py_None );
     600          48 :     return Py_None;
     601             : }
     602             : 
     603         252 : static PyObject *getClass( SAL_UNUSED_PARAMETER PyObject *, PyObject *args )
     604             : {
     605         252 :     PyObject *obj = extractOneStringArg( args, "pyuno.getClass");
     606         252 :     if( ! obj )
     607           0 :         return NULL;
     608             : 
     609             :     try
     610             :     {
     611         252 :         Runtime runtime;
     612         504 :         PyRef ret = getClass(pyString2ustring(obj), runtime);
     613         176 :         Py_XINCREF( ret.get() );
     614         428 :         return ret.get();
     615             :     }
     616          76 :     catch(const RuntimeException & e)
     617             :     {
     618          76 :         raisePyExceptionWithAny( makeAny(e) );
     619             :     }
     620          76 :     return NULL;
     621             : }
     622             : 
     623          28 : static PyObject *isInterface( SAL_UNUSED_PARAMETER PyObject *, PyObject *args )
     624             : {
     625             : 
     626          28 :     if( PyTuple_Check( args ) && PyTuple_Size( args ) == 1 )
     627             :     {
     628          28 :         PyObject *obj = PyTuple_GetItem( args, 0 );
     629          28 :         Runtime r;
     630          28 :         return PyLong_FromLong( isInterfaceClass( r, obj ) );
     631             :     }
     632           0 :     return PyLong_FromLong( 0 );
     633             : }
     634             : 
     635           0 : static PyObject * generateUuid(
     636             :     SAL_UNUSED_PARAMETER PyObject *, SAL_UNUSED_PARAMETER PyObject * )
     637             : {
     638           0 :     Sequence< sal_Int8 > seq( 16 );
     639           0 :     rtl_createUuid( (sal_uInt8*)seq.getArray() , 0 , sal_False );
     640           0 :     PyRef ret;
     641             :     try
     642             :     {
     643           0 :         Runtime runtime;
     644           0 :         ret = runtime.any2PyObject( makeAny( seq ) );
     645             :     }
     646           0 :     catch( const RuntimeException & e )
     647             :     {
     648           0 :         raisePyExceptionWithAny( makeAny(e) );
     649             :     }
     650           0 :     return ret.getAcquired();
     651             : }
     652             : 
     653           0 : static PyObject *systemPathToFileUrl(
     654             :     SAL_UNUSED_PARAMETER PyObject *, PyObject * args )
     655             : {
     656           0 :     PyObject *obj = extractOneStringArg( args, "pyuno.systemPathToFileUrl" );
     657           0 :     if( ! obj )
     658           0 :         return NULL;
     659             : 
     660           0 :     OUString sysPath = pyString2ustring( obj );
     661           0 :     OUString url;
     662           0 :     osl::FileBase::RC e = osl::FileBase::getFileURLFromSystemPath( sysPath, url );
     663             : 
     664           0 :     if( e != osl::FileBase::E_None )
     665             :     {
     666           0 :         OUStringBuffer buf;
     667           0 :         buf.appendAscii( "Couldn't convert " );
     668           0 :         buf.append( sysPath );
     669           0 :         buf.appendAscii( " to a file url for reason (" );
     670           0 :         buf.append( (sal_Int32) e );
     671           0 :         buf.appendAscii( ")" );
     672             :         raisePyExceptionWithAny(
     673           0 :             makeAny( RuntimeException( buf.makeStringAndClear() )));
     674           0 :         return NULL;
     675             :     }
     676           0 :     return ustring2PyUnicode( url ).getAcquired();
     677             : }
     678             : 
     679           0 : static PyObject * fileUrlToSystemPath(
     680             :     SAL_UNUSED_PARAMETER PyObject *, PyObject * args )
     681             : {
     682           0 :     PyObject *obj = extractOneStringArg( args, "pyuno.fileUrlToSystemPath" );
     683           0 :     if( ! obj )
     684           0 :         return NULL;
     685             : 
     686           0 :     OUString url = pyString2ustring( obj );
     687           0 :     OUString sysPath;
     688           0 :     osl::FileBase::RC e = osl::FileBase::getSystemPathFromFileURL( url, sysPath );
     689             : 
     690           0 :     if( e != osl::FileBase::E_None )
     691             :     {
     692           0 :         OUStringBuffer buf;
     693           0 :         buf.appendAscii( "Couldn't convert file url " );
     694           0 :         buf.append( sysPath );
     695           0 :         buf.appendAscii( " to a system path for reason (" );
     696           0 :         buf.append( (sal_Int32) e );
     697           0 :         buf.appendAscii( ")" );
     698             :         raisePyExceptionWithAny(
     699           0 :             makeAny( RuntimeException( buf.makeStringAndClear() )));
     700           0 :         return NULL;
     701             :     }
     702           0 :     return ustring2PyUnicode( sysPath ).getAcquired();
     703             : }
     704             : 
     705           0 : static PyObject * absolutize( SAL_UNUSED_PARAMETER PyObject *, PyObject * args )
     706             : {
     707           0 :     if( PyTuple_Check( args ) && PyTuple_Size( args ) == 2 )
     708             :     {
     709           0 :         OUString ouPath = pyString2ustring( PyTuple_GetItem( args , 0 ) );
     710           0 :         OUString ouRel = pyString2ustring( PyTuple_GetItem( args, 1 ) );
     711           0 :         OUString ret;
     712           0 :         oslFileError e = osl_getAbsoluteFileURL( ouPath.pData, ouRel.pData, &(ret.pData) );
     713           0 :         if( e != osl_File_E_None )
     714             :         {
     715           0 :             OUStringBuffer buf;
     716           0 :             buf.appendAscii( "Couldn't absolutize " );
     717           0 :             buf.append( ouRel );
     718           0 :             buf.appendAscii( " using root " );
     719           0 :             buf.append( ouPath );
     720           0 :             buf.appendAscii( " for reason (" );
     721           0 :             buf.append( (sal_Int32) e );
     722           0 :             buf.appendAscii( ")" );
     723             : 
     724             :             PyErr_SetString(
     725             :                 PyExc_OSError,
     726           0 :                 OUStringToOString(buf.makeStringAndClear(),osl_getThreadTextEncoding()).getStr());
     727           0 :             return 0;
     728             :         }
     729           0 :         return ustring2PyUnicode( ret ).getAcquired();
     730             :     }
     731           0 :     return 0;
     732             : }
     733             : 
     734           0 : static PyObject * invoke(SAL_UNUSED_PARAMETER PyObject *, PyObject *args)
     735             : {
     736           0 :     PyObject *ret = 0;
     737           0 :     if(PyTuple_Check(args) && PyTuple_Size(args) == 3)
     738             :     {
     739           0 :         PyObject *object = PyTuple_GetItem(args, 0);
     740           0 :         PyObject *item1 = PyTuple_GetItem(args, 1);
     741           0 :         if (PyStr_Check(item1))
     742             :         {
     743           0 :             const char *name = PyStr_AsString(item1);
     744           0 :             PyObject *item2 = PyTuple_GetItem(args, 2);
     745           0 :             if(PyTuple_Check(item2))
     746             :             {
     747           0 :                 ret = PyUNO_invoke(object, name, item2);
     748             :             }
     749             :             else
     750             :             {
     751           0 :                 OStringBuffer buf;
     752           0 :                 buf.append("uno.invoke expects a tuple as 3rd argument, got ");
     753           0 :                 buf.append(PyStr_AsString(PyObject_Str(item2)));
     754             :                 PyErr_SetString(
     755           0 :                     PyExc_RuntimeError, buf.makeStringAndClear().getStr());
     756             :             }
     757             :         }
     758             :         else
     759             :         {
     760           0 :             OStringBuffer buf;
     761           0 :             buf.append("uno.invoke expected a string as 2nd argument, got ");
     762           0 :             buf.append(PyStr_AsString(PyObject_Str(item1)));
     763             :             PyErr_SetString(
     764           0 :                 PyExc_RuntimeError, buf.makeStringAndClear().getStr());
     765             :         }
     766             :     }
     767             :     else
     768             :     {
     769           0 :         OStringBuffer buf;
     770           0 :         buf.append("uno.invoke expects object, name, (arg1, arg2, ... )\n");
     771           0 :         PyErr_SetString(PyExc_RuntimeError, buf.makeStringAndClear().getStr());
     772             :     }
     773           0 :     return ret;
     774             : }
     775             : 
     776           0 : static PyObject *getCurrentContext(
     777             :     SAL_UNUSED_PARAMETER PyObject *, SAL_UNUSED_PARAMETER PyObject * )
     778             : {
     779           0 :     PyRef ret;
     780             :     try
     781             :     {
     782           0 :         Runtime runtime;
     783           0 :         ret = runtime.any2PyObject(
     784           0 :             makeAny( com::sun::star::uno::getCurrentContext() ) );
     785             :     }
     786           0 :     catch( const com::sun::star::uno::Exception & e )
     787             :     {
     788           0 :         raisePyExceptionWithAny( makeAny( e ) );
     789             :     }
     790           0 :     return ret.getAcquired();
     791             : }
     792             : 
     793           0 : static PyObject *setCurrentContext(
     794             :     SAL_UNUSED_PARAMETER PyObject *, SAL_UNUSED_PARAMETER PyObject * args )
     795             : {
     796           0 :     PyRef ret;
     797             :     try
     798             :     {
     799           0 :         if( PyTuple_Check( args ) && PyTuple_Size( args ) == 1 )
     800             :         {
     801             : 
     802           0 :             Runtime runtime;
     803           0 :             Any a = runtime.pyObject2Any( PyTuple_GetItem( args, 0 ) );
     804             : 
     805           0 :             Reference< com::sun::star::uno::XCurrentContext > context;
     806             : 
     807           0 :             if( (a.hasValue() && (a >>= context)) || ! a.hasValue() )
     808             :             {
     809           0 :                 ret = com::sun::star::uno::setCurrentContext( context ) ? Py_True : Py_False;
     810             :             }
     811             :             else
     812             :             {
     813           0 :                 OStringBuffer buf;
     814           0 :                 buf.append( "uno.setCurrentContext expects an XComponentContext implementation, got " );
     815             :                 buf.append(
     816           0 :                     PyStr_AsString(PyObject_Str(PyTuple_GetItem(args, 0))));
     817             :                 PyErr_SetString(
     818           0 :                     PyExc_RuntimeError, buf.makeStringAndClear().getStr() );
     819           0 :             }
     820             :         }
     821             :         else
     822             :         {
     823           0 :             OStringBuffer buf;
     824           0 :             buf.append( "uno.setCurrentContext expects exactly one argument (the current Context)\n" );
     825             :             PyErr_SetString(
     826           0 :                 PyExc_RuntimeError, buf.makeStringAndClear().getStr() );
     827             :         }
     828             :     }
     829           0 :     catch( const com::sun::star::uno::Exception & e )
     830             :     {
     831           0 :         raisePyExceptionWithAny( makeAny( e ) );
     832             :     }
     833           0 :     return ret.getAcquired();
     834             : }
     835             : 
     836             : }
     837             : 
     838             : struct PyMethodDef PyUNOModule_methods [] =
     839             : {
     840             :     {"private_initTestEnvironment", initTestEnvironment, METH_VARARGS, NULL},
     841             :     {"getComponentContext", getComponentContext, METH_VARARGS, NULL},
     842             :     {"_createUnoStructHelper", reinterpret_cast<PyCFunction>(createUnoStructHelper), METH_VARARGS | METH_KEYWORDS, NULL},
     843             :     {"getTypeByName", getTypeByName, METH_VARARGS, NULL},
     844             :     {"getConstantByName", getConstantByName, METH_VARARGS, NULL},
     845             :     {"getClass", getClass, METH_VARARGS, NULL},
     846             :     {"checkEnum", checkEnum, METH_VARARGS, NULL},
     847             :     {"checkType", checkType, METH_VARARGS, NULL},
     848             :     {"generateUuid", generateUuid, METH_VARARGS, NULL},
     849             :     {"systemPathToFileUrl", systemPathToFileUrl, METH_VARARGS, NULL},
     850             :     {"fileUrlToSystemPath", fileUrlToSystemPath, METH_VARARGS, NULL},
     851             :     {"absolutize", absolutize, METH_VARARGS | METH_KEYWORDS, NULL},
     852             :     {"isInterface", isInterface, METH_VARARGS, NULL},
     853             :     {"invoke", invoke, METH_VARARGS | METH_KEYWORDS, NULL},
     854             :     {"setCurrentContext", setCurrentContext, METH_VARARGS, NULL},
     855             :     {"getCurrentContext", getCurrentContext, METH_VARARGS, NULL},
     856             :     {NULL, NULL, 0, NULL}
     857             : };
     858             : 
     859             : }
     860             : 
     861             : extern "C"
     862             : #if PY_MAJOR_VERSION >= 3
     863           6 : PyObject* PyInit_pyuno()
     864             : {
     865           6 :     PyUNO_initType();
     866             :     // noop when called already, otherwise needed to allow multiple threads
     867           6 :     PyEval_InitThreads();
     868             :     static struct PyModuleDef moduledef =
     869             :     {
     870             :         PyModuleDef_HEAD_INIT,
     871             :         "pyuno",             // module name
     872             :         0,                   // module documentation
     873             :         -1,                  // module keeps state in global variables,
     874             :         PyUNOModule_methods, // modules methods
     875             :         0,                   // m_reload (must be 0)
     876             :         0,                   // m_traverse
     877             :         0,                   // m_clear
     878             :         0,                   // m_free
     879             :     };
     880           6 :     return PyModule_Create(&moduledef);
     881             : }
     882             : #else
     883             : void initpyuno()
     884             : {
     885             :     PyUNO_initType();
     886             :     PyEval_InitThreads();
     887             :     Py_InitModule ("pyuno", PyUNOModule_methods);
     888             : }
     889             : #endif /* PY_MAJOR_VERSION >= 3 */
     890             : 
     891             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10