LCOV - code coverage report
Current view: top level - pyuno/source/module - pyuno_module.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 193 409 47.2 %
Date: 2014-04-11 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          24 :     fillStructState()
      94          24 :         : used (PyDict_New())
      95             :         , initialised ()
      96          24 :         , nPosConsumed (0)
      97             :     {
      98          24 :         if ( ! used )
      99           0 :             throw RuntimeException("pyuno._createUnoStructHelper failed to create new dictionary", Reference< XInterface > ());
     100          24 :     }
     101          24 :     ~fillStructState()
     102          24 :     {
     103          24 :         Py_DECREF(used);
     104          24 :     }
     105           0 :     void setUsed(PyObject *key)
     106             :     {
     107           0 :         PyDict_SetItem(used, key, Py_True);
     108           0 :     }
     109         104 :     void setInitialised(const OUString& key, sal_Int32 pos = -1)
     110             :     {
     111         104 :         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(), Reference< XInterface > ());
     124             :         }
     125         104 :         initialised[key] = true;
     126         104 :         if ( pos >= 0 )
     127         104 :             ++nPosConsumed;
     128         104 :     }
     129         104 :     bool isInitialised(const OUString& key)
     130             :     {
     131         104 :         return initialised[key];
     132             :     }
     133          24 :     PyObject *getUsed() const
     134             :     {
     135          24 :         return used;
     136             :     }
     137         156 :     sal_Int32 getCntConsumed() const
     138             :     {
     139         156 :         return nPosConsumed;
     140             :     }
     141             : };
     142             : 
     143          28 : 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          28 :     if( pCompType->pBaseTypeDescription )
     152           4 :         fillStruct( inv, pCompType->pBaseTypeDescription, initializer, kwinitializer, state, runtime );
     153             : 
     154          28 :     const sal_Int32 nMembers = pCompType->nMembers;
     155             :     {
     156         132 :         for( int i = 0 ; i < nMembers ; i ++ )
     157             :         {
     158         104 :             const OUString OUMemberName (pCompType->ppMemberNames[i]);
     159             :             PyObject *pyMemberName =
     160             :                 PyStr_FromString(OUStringToOString(OUMemberName,
     161         104 :                         RTL_TEXTENCODING_UTF8).getStr());
     162         104 :             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         104 :         }
     170             :     }
     171             :     {
     172          28 :         const int remainingPosInitialisers = PyTuple_Size(initializer) - state.getCntConsumed();
     173         132 :         for( int i = 0 ; i < remainingPosInitialisers && i < nMembers ; i ++ )
     174             :         {
     175         104 :             const int tupleIndex = state.getCntConsumed();
     176         104 :             const OUString pMemberName (pCompType->ppMemberNames[i]);
     177         104 :             state.setInitialised(pMemberName, tupleIndex);
     178         104 :             PyObject *element = PyTuple_GetItem( initializer, tupleIndex );
     179         208 :             Any a = runtime.pyObject2Any( element, ACCEPT_UNO_ANY );
     180         104 :             inv->setValue( pMemberName, a );
     181         104 :         }
     182             :     }
     183         132 :     for ( int i = 0; i < nMembers ; ++i)
     184             :     {
     185         104 :         const OUString memberName (pCompType->ppMemberNames[i]);
     186         104 :         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(), Reference< XInterface > ());
     195             :         }
     196         104 :     }
     197          28 : }
     198             : 
     199          14 : OUString getLibDir()
     200             : {
     201             :     static OUString *pLibDir;
     202          14 :     if( !pLibDir )
     203             :     {
     204           2 :         osl::MutexGuard guard( osl::Mutex::getGlobalMutex() );
     205           2 :         if( ! pLibDir )
     206             :         {
     207           2 :             static OUString libDir;
     208             : 
     209             :             // workarounds the $(ORIGIN) until it is available
     210           2 :             if( Module::getUrlFromAddress(
     211             :                     reinterpret_cast< oslGenericFunction >(getLibDir), libDir ) )
     212             :             {
     213           2 :                 libDir = OUString( libDir.getStr(), libDir.lastIndexOf('/' ) );
     214           2 :                 OUString name ( "PYUNOLIBDIR" );
     215           2 :                 rtl_bootstrap_set( name.pData, libDir.pData );
     216             :             }
     217           2 :             pLibDir = &libDir;
     218           2 :         }
     219             :     }
     220          14 :     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          14 : static PyObject* getComponentContext(
     236             :     SAL_UNUSED_PARAMETER PyObject*, SAL_UNUSED_PARAMETER PyObject*)
     237             : {
     238          14 :     PyRef ret;
     239             :     try
     240             :     {
     241          14 :         Reference<XComponentContext> ctx;
     242             : 
     243             :         // getLibDir() must be called in order to set bootstrap variables correctly !
     244          28 :         OUString path( getLibDir());
     245          14 :         if( Runtime::isInitialized() )
     246             :         {
     247          13 :             Runtime runtime;
     248          13 :             ctx = runtime.getImpl()->cargo->xContext;
     249             :         }
     250             :         else
     251             :         {
     252             :             // cppu::defaultBootstrap_InitialComponentContext expects
     253             :             // command line arguments to be present
     254           1 :             osl_setCommandArgs(0, 0); // fake it
     255             : 
     256           1 :             OUString iniFile;
     257           1 :             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           2 :             OUStringBuffer iniFileName;
     266           1 :             iniFileName.append( path );
     267             : #if HAVE_FEATURE_MACOSX_MACLIKE_APP_STRUCTURE
     268             :             iniFileName.appendAscii( "/../" LIBO_ETC_FOLDER );
     269             : #endif
     270           1 :             iniFileName.appendAscii( "/" );
     271           1 :             iniFileName.appendAscii( SAL_CONFIGFILE( "pyuno" ) );
     272           1 :             iniFile = iniFileName.makeStringAndClear();
     273           2 :             osl::DirectoryItem item;
     274           1 :             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           1 :                 PyThreadDetach antiguard;
     284           1 :                 ctx = cppu::defaultBootstrap_InitialComponentContext ();
     285           1 :             }
     286             : 
     287             :         }
     288             : 
     289          14 :         if( ! Runtime::isInitialized() )
     290             :         {
     291           1 :             Runtime::initialize( ctx );
     292             :         }
     293          28 :         Runtime runtime;
     294          28 :         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          14 :     return ret.getAcquired();
     320             : }
     321             : 
     322           6 : static PyObject* initPoniesMode(
     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           6 :         PyObject *const ctx(getComponentContext(0, 0));
     331           6 :         if (!ctx) { abort(); }
     332           6 :         Runtime const runtime;
     333          12 :         Any const a(runtime.pyObject2Any(ctx));
     334          12 :         Reference<XComponentContext> xContext;
     335           6 :         a >>= xContext;
     336           6 :         if (!xContext.is()) { abort(); }
     337             :         using com::sun::star::lang::XMultiServiceFactory;
     338             :         Reference<XMultiServiceFactory> const xMSF(
     339           6 :             xContext->getServiceManager(),
     340          12 :             com::sun::star::uno::UNO_QUERY_THROW);
     341           6 :         if (!xMSF.is()) { abort(); }
     342           6 :         char *const testlib = getenv("TEST_LIB");
     343           6 :         if (!testlib) { abort(); }
     344          12 :         OString const libname = OString(testlib, strlen(testlib))
     345             : #ifdef _WIN32
     346             :             .replaceAll(OString('/'), OString('\\'))
     347             : #endif
     348             :             ;
     349             : 
     350           6 :         osl::Module &mod = runtime.getImpl()->cargo->testModule;
     351           6 :         mod.load(OStringToOUString(libname, osl_getThreadTextEncoding()),
     352           6 :                                 SAL_LOADMODULE_LAZY | SAL_LOADMODULE_GLOBAL);
     353           6 :         if (!mod.is()) { abort(); }
     354             :         oslGenericFunction const pFunc(
     355           6 :                 mod.getFunctionSymbol("test_init"));
     356           6 :         if (!pFunc) { abort(); }
     357             :         // guess casting pFunc is undefined behavior but don't see a better way
     358          12 :         ((void (SAL_CALL *)(XMultiServiceFactory*)) pFunc) (xMSF.get());
     359             :     }
     360           0 :     catch (const com::sun::star::uno::Exception &)
     361             :     {
     362           0 :         abort();
     363             :     }
     364           6 :     return Py_None;
     365             : }
     366             : 
     367         121 : PyObject * extractOneStringArg( PyObject *args, char const *funcName )
     368             : {
     369         121 :     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         121 :     PyObject *obj = PyTuple_GetItem( args, 0 );
     377         121 :     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         121 :     return obj;
     385             : }
     386             : 
     387          24 : static PyObject *createUnoStructHelper(
     388             :     SAL_UNUSED_PARAMETER PyObject *, PyObject* args, PyObject* keywordArgs)
     389             : {
     390          24 :     Any IdlStruct;
     391          48 :     PyRef ret;
     392             :     try
     393             :     {
     394          24 :         Runtime runtime;
     395          24 :         if( PyTuple_Size( args ) == 2 )
     396             :         {
     397          24 :             PyObject *structName = PyTuple_GetItem(args, 0);
     398          24 :             PyObject *initializer = PyTuple_GetItem(args, 1);
     399             : 
     400          24 :             if (PyStr_Check(structName))
     401             :             {
     402          24 :                 if( PyTuple_Check( initializer ) && PyDict_Check ( keywordArgs ) )
     403             :                 {
     404          24 :                     OUString typeName( OUString::createFromAscii(PyStr_AsString(structName)));
     405          24 :                     RuntimeCargo *c = runtime.getImpl()->cargo;
     406          48 :                     Reference<XIdlClass> idl_class ( c->xCoreReflection->forName (typeName),UNO_QUERY);
     407          24 :                     if (idl_class.is ())
     408             :                     {
     409          24 :                         idl_class->createObject (IdlStruct);
     410          24 :                         PyUNO *me = (PyUNO*)PyUNO_new_UNCHECKED( IdlStruct, c->xInvocation );
     411          24 :                         PyRef returnCandidate( (PyObject*)me, SAL_NO_ACQUIRE );
     412          48 :                         TypeDescription desc( typeName );
     413             :                         OSL_ASSERT( desc.is() ); // could already instantiate an XInvocation2 !
     414             : 
     415             :                         typelib_CompoundTypeDescription *pCompType =
     416          24 :                             ( typelib_CompoundTypeDescription * ) desc.get();
     417          48 :                         fillStructState state;
     418          24 :                         if ( PyTuple_Size( initializer ) > 0 || PyDict_Size( keywordArgs ) > 0 )
     419          24 :                             fillStruct( me->members->xInvocation, pCompType, initializer, keywordArgs, state, runtime );
     420          24 :                         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(), Reference< XInterface > ());
     429             :                         }
     430          48 :                         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          24 :                     }
     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          24 :         }
     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          48 :     return ret.getAcquired();
     471             : }
     472             : 
     473          10 : static PyObject *getTypeByName(
     474             :     SAL_UNUSED_PARAMETER PyObject *, PyObject *args )
     475             : {
     476          10 :     PyObject * ret = NULL;
     477             : 
     478             :     try
     479             :     {
     480             :         char *name;
     481             : 
     482          10 :         if (PyArg_ParseTuple (args, "s", &name))
     483             :         {
     484          10 :             OUString typeName ( OUString::createFromAscii( name ) );
     485          20 :             TypeDescription typeDesc( typeName );
     486          10 :             if( typeDesc.is() )
     487             :             {
     488          10 :                 Runtime runtime;
     489             :                 ret = PyUNO_Type_new(
     490          10 :                     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          10 :             }
     498             :         }
     499             :     }
     500           0 :     catch ( const RuntimeException & e )
     501             :     {
     502           0 :         raisePyExceptionWithAny( makeAny( e ) );
     503             :     }
     504          10 :     return ret;
     505             : }
     506             : 
     507          29 : static PyObject *getConstantByName(
     508             :     SAL_UNUSED_PARAMETER PyObject *, PyObject *args )
     509             : {
     510          29 :     PyObject *ret = 0;
     511             :     try
     512             :     {
     513             :         char *name;
     514             : 
     515          29 :         if (PyArg_ParseTuple (args, "s", &name))
     516             :         {
     517          29 :             OUString typeName ( OUString::createFromAscii( name ) );
     518          58 :             Runtime runtime;
     519          58 :             css::uno::Reference< css::reflection::XConstantTypeDescription > td;
     520          85 :             if (!(runtime.getImpl()->cargo->xTdMgr->getByHierarchicalName(
     521          29 :                       typeName)
     522          57 :                   >>= 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(), Reference< XInterface > () );
     528             :             }
     529          56 :             PyRef constant = runtime.any2PyObject( td->getConstantValue() );
     530          57 :             ret = constant.getAcquired();
     531             :         }
     532             :     }
     533           2 :     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           1 :         RuntimeException runExc( e.Message, Reference< XInterface > () );
     538           1 :         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          29 :     return ret;
     553             : }
     554             : 
     555          10 : static PyObject *checkType( SAL_UNUSED_PARAMETER PyObject *, PyObject *args )
     556             : {
     557          10 :     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          10 :     PyObject *obj = PyTuple_GetItem( args, 0 );
     565             : 
     566             :     try
     567             :     {
     568          10 :         PyType2Type( obj );
     569             :     }
     570           0 :     catch(const  RuntimeException & e)
     571             :     {
     572           0 :         raisePyExceptionWithAny( makeAny( e ) );
     573           0 :         return NULL;
     574             :     }
     575          10 :     Py_INCREF( Py_None );
     576          10 :     return Py_None;
     577             : }
     578             : 
     579          53 : static PyObject *checkEnum( SAL_UNUSED_PARAMETER PyObject *, PyObject *args )
     580             : {
     581          53 :     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          53 :     PyObject *obj = PyTuple_GetItem( args, 0 );
     589             : 
     590             :     try
     591             :     {
     592          53 :         PyEnum2Enum( obj );
     593             :     }
     594          29 :     catch(const RuntimeException & e)
     595             :     {
     596          29 :         raisePyExceptionWithAny( makeAny( e) );
     597          29 :         return NULL;
     598             :     }
     599          24 :     Py_INCREF( Py_None );
     600          24 :     return Py_None;
     601             : }
     602             : 
     603         121 : static PyObject *getClass( SAL_UNUSED_PARAMETER PyObject *, PyObject *args )
     604             : {
     605         121 :     PyObject *obj = extractOneStringArg( args, "pyuno.getClass");
     606         121 :     if( ! obj )
     607           0 :         return NULL;
     608             : 
     609             :     try
     610             :     {
     611         121 :         Runtime runtime;
     612         242 :         PyRef ret = getClass(pyString2ustring(obj), runtime);
     613          83 :         Py_XINCREF( ret.get() );
     614         204 :         return ret.get();
     615             :     }
     616          38 :     catch(const RuntimeException & e)
     617             :     {
     618             :         // NOOPT !!!
     619             :         // gcc 3.2.3 crashes here in the regcomp test scenario
     620             :         // only since migration to python 2.3.4 ???? strange thing
     621             :         // optimization switched off for this module !
     622          38 :         raisePyExceptionWithAny( makeAny(e) );
     623             :     }
     624          38 :     return NULL;
     625             : }
     626             : 
     627          14 : static PyObject *isInterface( SAL_UNUSED_PARAMETER PyObject *, PyObject *args )
     628             : {
     629             : 
     630          14 :     if( PyTuple_Check( args ) && PyTuple_Size( args ) == 1 )
     631             :     {
     632          14 :         PyObject *obj = PyTuple_GetItem( args, 0 );
     633          14 :         Runtime r;
     634          14 :         return PyLong_FromLong( isInterfaceClass( r, obj ) );
     635             :     }
     636           0 :     return PyLong_FromLong( 0 );
     637             : }
     638             : 
     639           0 : static PyObject * generateUuid(
     640             :     SAL_UNUSED_PARAMETER PyObject *, SAL_UNUSED_PARAMETER PyObject * )
     641             : {
     642           0 :     Sequence< sal_Int8 > seq( 16 );
     643           0 :     rtl_createUuid( (sal_uInt8*)seq.getArray() , 0 , sal_False );
     644           0 :     PyRef ret;
     645             :     try
     646             :     {
     647           0 :         Runtime runtime;
     648           0 :         ret = runtime.any2PyObject( makeAny( seq ) );
     649             :     }
     650           0 :     catch( const RuntimeException & e )
     651             :     {
     652           0 :         raisePyExceptionWithAny( makeAny(e) );
     653             :     }
     654           0 :     return ret.getAcquired();
     655             : }
     656             : 
     657           0 : static PyObject *systemPathToFileUrl(
     658             :     SAL_UNUSED_PARAMETER PyObject *, PyObject * args )
     659             : {
     660           0 :     PyObject *obj = extractOneStringArg( args, "pyuno.systemPathToFileUrl" );
     661           0 :     if( ! obj )
     662           0 :         return NULL;
     663             : 
     664           0 :     OUString sysPath = pyString2ustring( obj );
     665           0 :     OUString url;
     666           0 :     osl::FileBase::RC e = osl::FileBase::getFileURLFromSystemPath( sysPath, url );
     667             : 
     668           0 :     if( e != osl::FileBase::E_None )
     669             :     {
     670           0 :         OUStringBuffer buf;
     671           0 :         buf.appendAscii( "Couldn't convert " );
     672           0 :         buf.append( sysPath );
     673           0 :         buf.appendAscii( " to a file url for reason (" );
     674           0 :         buf.append( (sal_Int32) e );
     675           0 :         buf.appendAscii( ")" );
     676             :         raisePyExceptionWithAny(
     677           0 :             makeAny( RuntimeException( buf.makeStringAndClear(), Reference< XInterface > () )));
     678           0 :         return NULL;
     679             :     }
     680           0 :     return ustring2PyUnicode( url ).getAcquired();
     681             : }
     682             : 
     683           0 : static PyObject * fileUrlToSystemPath(
     684             :     SAL_UNUSED_PARAMETER PyObject *, PyObject * args )
     685             : {
     686           0 :     PyObject *obj = extractOneStringArg( args, "pyuno.fileUrlToSystemPath" );
     687           0 :     if( ! obj )
     688           0 :         return NULL;
     689             : 
     690           0 :     OUString url = pyString2ustring( obj );
     691           0 :     OUString sysPath;
     692           0 :     osl::FileBase::RC e = osl::FileBase::getSystemPathFromFileURL( url, sysPath );
     693             : 
     694           0 :     if( e != osl::FileBase::E_None )
     695             :     {
     696           0 :         OUStringBuffer buf;
     697           0 :         buf.appendAscii( "Couldn't convert file url " );
     698           0 :         buf.append( sysPath );
     699           0 :         buf.appendAscii( " to a system path for reason (" );
     700           0 :         buf.append( (sal_Int32) e );
     701           0 :         buf.appendAscii( ")" );
     702             :         raisePyExceptionWithAny(
     703           0 :             makeAny( RuntimeException( buf.makeStringAndClear(), Reference< XInterface > () )));
     704           0 :         return NULL;
     705             :     }
     706           0 :     return ustring2PyUnicode( sysPath ).getAcquired();
     707             : }
     708             : 
     709           0 : static PyObject * absolutize( SAL_UNUSED_PARAMETER PyObject *, PyObject * args )
     710             : {
     711           0 :     if( PyTuple_Check( args ) && PyTuple_Size( args ) == 2 )
     712             :     {
     713           0 :         OUString ouPath = pyString2ustring( PyTuple_GetItem( args , 0 ) );
     714           0 :         OUString ouRel = pyString2ustring( PyTuple_GetItem( args, 1 ) );
     715           0 :         OUString ret;
     716           0 :         oslFileError e = osl_getAbsoluteFileURL( ouPath.pData, ouRel.pData, &(ret.pData) );
     717           0 :         if( e != osl_File_E_None )
     718             :         {
     719           0 :             OUStringBuffer buf;
     720           0 :             buf.appendAscii( "Couldn't absolutize " );
     721           0 :             buf.append( ouRel );
     722           0 :             buf.appendAscii( " using root " );
     723           0 :             buf.append( ouPath );
     724           0 :             buf.appendAscii( " for reason (" );
     725           0 :             buf.append( (sal_Int32) e );
     726           0 :             buf.appendAscii( ")" );
     727             : 
     728             :             PyErr_SetString(
     729             :                 PyExc_OSError,
     730           0 :                 OUStringToOString(buf.makeStringAndClear(),osl_getThreadTextEncoding()).getStr());
     731           0 :             return 0;
     732             :         }
     733           0 :         return ustring2PyUnicode( ret ).getAcquired();
     734             :     }
     735           0 :     return 0;
     736             : }
     737             : 
     738           0 : static PyObject * invoke(SAL_UNUSED_PARAMETER PyObject *, PyObject *args)
     739             : {
     740           0 :     PyObject *ret = 0;
     741           0 :     if(PyTuple_Check(args) && PyTuple_Size(args) == 3)
     742             :     {
     743           0 :         PyObject *object = PyTuple_GetItem(args, 0);
     744           0 :         PyObject *item1 = PyTuple_GetItem(args, 1);
     745           0 :         if (PyStr_Check(item1))
     746             :         {
     747           0 :             const char *name = PyStr_AsString(item1);
     748           0 :             PyObject *item2 = PyTuple_GetItem(args, 2);
     749           0 :             if(PyTuple_Check(item2))
     750             :             {
     751           0 :                 ret = PyUNO_invoke(object, name, item2);
     752             :             }
     753             :             else
     754             :             {
     755           0 :                 OStringBuffer buf;
     756           0 :                 buf.append("uno.invoke expects a tuple as 3rd argument, got ");
     757           0 :                 buf.append(PyStr_AsString(PyObject_Str(item2)));
     758             :                 PyErr_SetString(
     759           0 :                     PyExc_RuntimeError, buf.makeStringAndClear().getStr());
     760             :             }
     761             :         }
     762             :         else
     763             :         {
     764           0 :             OStringBuffer buf;
     765           0 :             buf.append("uno.invoke expected a string as 2nd argument, got ");
     766           0 :             buf.append(PyStr_AsString(PyObject_Str(item1)));
     767             :             PyErr_SetString(
     768           0 :                 PyExc_RuntimeError, buf.makeStringAndClear().getStr());
     769             :         }
     770             :     }
     771             :     else
     772             :     {
     773           0 :         OStringBuffer buf;
     774           0 :         buf.append("uno.invoke expects object, name, (arg1, arg2, ... )\n");
     775           0 :         PyErr_SetString(PyExc_RuntimeError, buf.makeStringAndClear().getStr());
     776             :     }
     777           0 :     return ret;
     778             : }
     779             : 
     780           0 : static PyObject *getCurrentContext(
     781             :     SAL_UNUSED_PARAMETER PyObject *, SAL_UNUSED_PARAMETER PyObject * )
     782             : {
     783           0 :     PyRef ret;
     784             :     try
     785             :     {
     786           0 :         Runtime runtime;
     787           0 :         ret = runtime.any2PyObject(
     788           0 :             makeAny( com::sun::star::uno::getCurrentContext() ) );
     789             :     }
     790           0 :     catch( const com::sun::star::uno::Exception & e )
     791             :     {
     792           0 :         raisePyExceptionWithAny( makeAny( e ) );
     793             :     }
     794           0 :     return ret.getAcquired();
     795             : }
     796             : 
     797           0 : static PyObject *setCurrentContext(
     798             :     SAL_UNUSED_PARAMETER PyObject *, SAL_UNUSED_PARAMETER PyObject * args )
     799             : {
     800           0 :     PyRef ret;
     801             :     try
     802             :     {
     803           0 :         if( PyTuple_Check( args ) && PyTuple_Size( args ) == 1 )
     804             :         {
     805             : 
     806           0 :             Runtime runtime;
     807           0 :             Any a = runtime.pyObject2Any( PyTuple_GetItem( args, 0 ) );
     808             : 
     809           0 :             Reference< com::sun::star::uno::XCurrentContext > context;
     810             : 
     811           0 :             if( (a.hasValue() && (a >>= context)) || ! a.hasValue() )
     812             :             {
     813           0 :                 ret = com::sun::star::uno::setCurrentContext( context ) ? Py_True : Py_False;
     814             :             }
     815             :             else
     816             :             {
     817           0 :                 OStringBuffer buf;
     818           0 :                 buf.append( "uno.setCurrentContext expects an XComponentContext implementation, got " );
     819             :                 buf.append(
     820           0 :                     PyStr_AsString(PyObject_Str(PyTuple_GetItem(args, 0))));
     821             :                 PyErr_SetString(
     822           0 :                     PyExc_RuntimeError, buf.makeStringAndClear().getStr() );
     823           0 :             }
     824             :         }
     825             :         else
     826             :         {
     827           0 :             OStringBuffer buf;
     828           0 :             buf.append( "uno.setCurrentContext expects exactly one argument (the current Context)\n" );
     829             :             PyErr_SetString(
     830           0 :                 PyExc_RuntimeError, buf.makeStringAndClear().getStr() );
     831             :         }
     832             :     }
     833           0 :     catch( const com::sun::star::uno::Exception & e )
     834             :     {
     835           0 :         raisePyExceptionWithAny( makeAny( e ) );
     836             :     }
     837           0 :     return ret.getAcquired();
     838             : }
     839             : 
     840             : }
     841             : 
     842             : struct PyMethodDef PyUNOModule_methods [] =
     843             : {
     844             :     {"experimentalExtraMagic", initPoniesMode, METH_VARARGS, NULL},
     845             :     {"getComponentContext", getComponentContext, METH_VARARGS, NULL},
     846             :     {"_createUnoStructHelper", reinterpret_cast<PyCFunction>(createUnoStructHelper), METH_VARARGS | METH_KEYWORDS, NULL},
     847             :     {"getTypeByName", getTypeByName, METH_VARARGS, NULL},
     848             :     {"getConstantByName", getConstantByName, METH_VARARGS, NULL},
     849             :     {"getClass", getClass, METH_VARARGS, NULL},
     850             :     {"checkEnum", checkEnum, METH_VARARGS, NULL},
     851             :     {"checkType", checkType, METH_VARARGS, NULL},
     852             :     {"generateUuid", generateUuid, METH_VARARGS, NULL},
     853             :     {"systemPathToFileUrl", systemPathToFileUrl, METH_VARARGS, NULL},
     854             :     {"fileUrlToSystemPath", fileUrlToSystemPath, METH_VARARGS, NULL},
     855             :     {"absolutize", absolutize, METH_VARARGS | METH_KEYWORDS, NULL},
     856             :     {"isInterface", isInterface, METH_VARARGS, NULL},
     857             :     {"invoke", invoke, METH_VARARGS | METH_KEYWORDS, NULL},
     858             :     {"setCurrentContext", setCurrentContext, METH_VARARGS, NULL},
     859             :     {"getCurrentContext", getCurrentContext, METH_VARARGS, NULL},
     860             :     {NULL, NULL, 0, NULL}
     861             : };
     862             : 
     863             : }
     864             : 
     865             : extern "C"
     866             : #if PY_MAJOR_VERSION >= 3
     867           2 : PyObject* PyInit_pyuno()
     868             : {
     869           2 :     PyUNO_initType();
     870             :     // noop when called already, otherwise needed to allow multiple threads
     871           2 :     PyEval_InitThreads();
     872             :     static struct PyModuleDef moduledef =
     873             :     {
     874             :         PyModuleDef_HEAD_INIT,
     875             :         "pyuno",             // module name
     876             :         0,                   // module documentation
     877             :         -1,                  // module keeps state in global variables,
     878             :         PyUNOModule_methods, // modules methods
     879             :         0,                   // m_reload (must be 0)
     880             :         0,                   // m_traverse
     881             :         0,                   // m_clear
     882             :         0,                   // m_free
     883             :     };
     884           2 :     return PyModule_Create(&moduledef);
     885             : }
     886             : #else
     887             : void initpyuno()
     888             : {
     889             :     PyUNO_initType();
     890             :     PyEval_InitThreads();
     891             :     Py_InitModule ("pyuno", PyUNOModule_methods);
     892             : }
     893             : #endif /* PY_MAJOR_VERSION >= 3 */
     894             : 
     895             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10