LCOV - code coverage report
Current view: top level - pyuno/source/module - pyuno_module.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 409 0.0 %
Date: 2014-04-14 Functions: 0 28 0.0 %
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           0 :     fillStructState()
      94           0 :         : used (PyDict_New())
      95             :         , initialised ()
      96           0 :         , nPosConsumed (0)
      97             :     {
      98           0 :         if ( ! used )
      99           0 :             throw RuntimeException("pyuno._createUnoStructHelper failed to create new dictionary", Reference< XInterface > ());
     100           0 :     }
     101           0 :     ~fillStructState()
     102           0 :     {
     103           0 :         Py_DECREF(used);
     104           0 :     }
     105           0 :     void setUsed(PyObject *key)
     106             :     {
     107           0 :         PyDict_SetItem(used, key, Py_True);
     108           0 :     }
     109           0 :     void setInitialised(const OUString& key, sal_Int32 pos = -1)
     110             :     {
     111           0 :         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           0 :         initialised[key] = true;
     126           0 :         if ( pos >= 0 )
     127           0 :             ++nPosConsumed;
     128           0 :     }
     129           0 :     bool isInitialised(const OUString& key)
     130             :     {
     131           0 :         return initialised[key];
     132             :     }
     133           0 :     PyObject *getUsed() const
     134             :     {
     135           0 :         return used;
     136             :     }
     137           0 :     sal_Int32 getCntConsumed() const
     138             :     {
     139           0 :         return nPosConsumed;
     140             :     }
     141             : };
     142             : 
     143           0 : 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           0 :     if( pCompType->pBaseTypeDescription )
     152           0 :         fillStruct( inv, pCompType->pBaseTypeDescription, initializer, kwinitializer, state, runtime );
     153             : 
     154           0 :     const sal_Int32 nMembers = pCompType->nMembers;
     155             :     {
     156           0 :         for( int i = 0 ; i < nMembers ; i ++ )
     157             :         {
     158           0 :             const OUString OUMemberName (pCompType->ppMemberNames[i]);
     159             :             PyObject *pyMemberName =
     160             :                 PyStr_FromString(OUStringToOString(OUMemberName,
     161           0 :                         RTL_TEXTENCODING_UTF8).getStr());
     162           0 :             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           0 :         }
     170             :     }
     171             :     {
     172           0 :         const int remainingPosInitialisers = PyTuple_Size(initializer) - state.getCntConsumed();
     173           0 :         for( int i = 0 ; i < remainingPosInitialisers && i < nMembers ; i ++ )
     174             :         {
     175           0 :             const int tupleIndex = state.getCntConsumed();
     176           0 :             const OUString pMemberName (pCompType->ppMemberNames[i]);
     177           0 :             state.setInitialised(pMemberName, tupleIndex);
     178           0 :             PyObject *element = PyTuple_GetItem( initializer, tupleIndex );
     179           0 :             Any a = runtime.pyObject2Any( element, ACCEPT_UNO_ANY );
     180           0 :             inv->setValue( pMemberName, a );
     181           0 :         }
     182             :     }
     183           0 :     for ( int i = 0; i < nMembers ; ++i)
     184             :     {
     185           0 :         const OUString memberName (pCompType->ppMemberNames[i]);
     186           0 :         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           0 :     }
     197           0 : }
     198             : 
     199           0 : OUString getLibDir()
     200             : {
     201             :     static OUString *pLibDir;
     202           0 :     if( !pLibDir )
     203             :     {
     204           0 :         osl::MutexGuard guard( osl::Mutex::getGlobalMutex() );
     205           0 :         if( ! pLibDir )
     206             :         {
     207           0 :             static OUString libDir;
     208             : 
     209             :             // workarounds the $(ORIGIN) until it is available
     210           0 :             if( Module::getUrlFromAddress(
     211             :                     reinterpret_cast< oslGenericFunction >(getLibDir), libDir ) )
     212             :             {
     213           0 :                 libDir = OUString( libDir.getStr(), libDir.lastIndexOf('/' ) );
     214           0 :                 OUString name ( "PYUNOLIBDIR" );
     215           0 :                 rtl_bootstrap_set( name.pData, libDir.pData );
     216             :             }
     217           0 :             pLibDir = &libDir;
     218           0 :         }
     219             :     }
     220           0 :     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           0 : static PyObject* getComponentContext(
     236             :     SAL_UNUSED_PARAMETER PyObject*, SAL_UNUSED_PARAMETER PyObject*)
     237             : {
     238           0 :     PyRef ret;
     239             :     try
     240             :     {
     241           0 :         Reference<XComponentContext> ctx;
     242             : 
     243             :         // getLibDir() must be called in order to set bootstrap variables correctly !
     244           0 :         OUString path( getLibDir());
     245           0 :         if( Runtime::isInitialized() )
     246             :         {
     247           0 :             Runtime runtime;
     248           0 :             ctx = runtime.getImpl()->cargo->xContext;
     249             :         }
     250             :         else
     251             :         {
     252             :             // cppu::defaultBootstrap_InitialComponentContext expects
     253             :             // command line arguments to be present
     254           0 :             osl_setCommandArgs(0, 0); // fake it
     255             : 
     256           0 :             OUString iniFile;
     257           0 :             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           0 :             OUStringBuffer iniFileName;
     266           0 :             iniFileName.append( path );
     267             : #if HAVE_FEATURE_MACOSX_MACLIKE_APP_STRUCTURE
     268             :             iniFileName.appendAscii( "/../" LIBO_ETC_FOLDER );
     269             : #endif
     270           0 :             iniFileName.appendAscii( "/" );
     271           0 :             iniFileName.appendAscii( SAL_CONFIGFILE( "pyuno" ) );
     272           0 :             iniFile = iniFileName.makeStringAndClear();
     273           0 :             osl::DirectoryItem item;
     274           0 :             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           0 :                 PyThreadDetach antiguard;
     284           0 :                 ctx = cppu::defaultBootstrap_InitialComponentContext ();
     285           0 :             }
     286             : 
     287             :         }
     288             : 
     289           0 :         if( ! Runtime::isInitialized() )
     290             :         {
     291           0 :             Runtime::initialize( ctx );
     292             :         }
     293           0 :         Runtime runtime;
     294           0 :         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           0 :     return ret.getAcquired();
     320             : }
     321             : 
     322           0 : 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           0 :         PyObject *const ctx(getComponentContext(0, 0));
     331           0 :         if (!ctx) { abort(); }
     332           0 :         Runtime const runtime;
     333           0 :         Any const a(runtime.pyObject2Any(ctx));
     334           0 :         Reference<XComponentContext> xContext;
     335           0 :         a >>= xContext;
     336           0 :         if (!xContext.is()) { abort(); }
     337             :         using com::sun::star::lang::XMultiServiceFactory;
     338             :         Reference<XMultiServiceFactory> const xMSF(
     339           0 :             xContext->getServiceManager(),
     340           0 :             com::sun::star::uno::UNO_QUERY_THROW);
     341           0 :         if (!xMSF.is()) { abort(); }
     342           0 :         char *const testlib = getenv("TEST_LIB");
     343           0 :         if (!testlib) { abort(); }
     344           0 :         OString const libname = OString(testlib, strlen(testlib))
     345             : #ifdef _WIN32
     346             :             .replaceAll(OString('/'), OString('\\'))
     347             : #endif
     348             :             ;
     349             : 
     350           0 :         osl::Module &mod = runtime.getImpl()->cargo->testModule;
     351           0 :         mod.load(OStringToOUString(libname, osl_getThreadTextEncoding()),
     352           0 :                                 SAL_LOADMODULE_LAZY | SAL_LOADMODULE_GLOBAL);
     353           0 :         if (!mod.is()) { abort(); }
     354             :         oslGenericFunction const pFunc(
     355           0 :                 mod.getFunctionSymbol("test_init"));
     356           0 :         if (!pFunc) { abort(); }
     357             :         // guess casting pFunc is undefined behavior but don't see a better way
     358           0 :         ((void (SAL_CALL *)(XMultiServiceFactory*)) pFunc) (xMSF.get());
     359             :     }
     360           0 :     catch (const com::sun::star::uno::Exception &)
     361             :     {
     362           0 :         abort();
     363             :     }
     364           0 :     return Py_None;
     365             : }
     366             : 
     367           0 : PyObject * extractOneStringArg( PyObject *args, char const *funcName )
     368             : {
     369           0 :     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           0 :     PyObject *obj = PyTuple_GetItem( args, 0 );
     377           0 :     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           0 :     return obj;
     385             : }
     386             : 
     387           0 : static PyObject *createUnoStructHelper(
     388             :     SAL_UNUSED_PARAMETER PyObject *, PyObject* args, PyObject* keywordArgs)
     389             : {
     390           0 :     Any IdlStruct;
     391           0 :     PyRef ret;
     392             :     try
     393             :     {
     394           0 :         Runtime runtime;
     395           0 :         if( PyTuple_Size( args ) == 2 )
     396             :         {
     397           0 :             PyObject *structName = PyTuple_GetItem(args, 0);
     398           0 :             PyObject *initializer = PyTuple_GetItem(args, 1);
     399             : 
     400           0 :             if (PyStr_Check(structName))
     401             :             {
     402           0 :                 if( PyTuple_Check( initializer ) && PyDict_Check ( keywordArgs ) )
     403             :                 {
     404           0 :                     OUString typeName( OUString::createFromAscii(PyStr_AsString(structName)));
     405           0 :                     RuntimeCargo *c = runtime.getImpl()->cargo;
     406           0 :                     Reference<XIdlClass> idl_class ( c->xCoreReflection->forName (typeName),UNO_QUERY);
     407           0 :                     if (idl_class.is ())
     408             :                     {
     409           0 :                         idl_class->createObject (IdlStruct);
     410           0 :                         PyUNO *me = (PyUNO*)PyUNO_new_UNCHECKED( IdlStruct, c->xInvocation );
     411           0 :                         PyRef returnCandidate( (PyObject*)me, SAL_NO_ACQUIRE );
     412           0 :                         TypeDescription desc( typeName );
     413             :                         OSL_ASSERT( desc.is() ); // could already instantiate an XInvocation2 !
     414             : 
     415             :                         typelib_CompoundTypeDescription *pCompType =
     416           0 :                             ( typelib_CompoundTypeDescription * ) desc.get();
     417           0 :                         fillStructState state;
     418           0 :                         if ( PyTuple_Size( initializer ) > 0 || PyDict_Size( keywordArgs ) > 0 )
     419           0 :                             fillStruct( me->members->xInvocation, pCompType, initializer, keywordArgs, state, runtime );
     420           0 :                         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           0 :                         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           0 :                     }
     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           0 :         }
     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           0 :     return ret.getAcquired();
     471             : }
     472             : 
     473           0 : static PyObject *getTypeByName(
     474             :     SAL_UNUSED_PARAMETER PyObject *, PyObject *args )
     475             : {
     476           0 :     PyObject * ret = NULL;
     477             : 
     478             :     try
     479             :     {
     480             :         char *name;
     481             : 
     482           0 :         if (PyArg_ParseTuple (args, "s", &name))
     483             :         {
     484           0 :             OUString typeName ( OUString::createFromAscii( name ) );
     485           0 :             TypeDescription typeDesc( typeName );
     486           0 :             if( typeDesc.is() )
     487             :             {
     488           0 :                 Runtime runtime;
     489             :                 ret = PyUNO_Type_new(
     490           0 :                     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           0 :             }
     498             :         }
     499             :     }
     500           0 :     catch ( const RuntimeException & e )
     501             :     {
     502           0 :         raisePyExceptionWithAny( makeAny( e ) );
     503             :     }
     504           0 :     return ret;
     505             : }
     506             : 
     507           0 : static PyObject *getConstantByName(
     508             :     SAL_UNUSED_PARAMETER PyObject *, PyObject *args )
     509             : {
     510           0 :     PyObject *ret = 0;
     511             :     try
     512             :     {
     513             :         char *name;
     514             : 
     515           0 :         if (PyArg_ParseTuple (args, "s", &name))
     516             :         {
     517           0 :             OUString typeName ( OUString::createFromAscii( name ) );
     518           0 :             Runtime runtime;
     519           0 :             css::uno::Reference< css::reflection::XConstantTypeDescription > td;
     520           0 :             if (!(runtime.getImpl()->cargo->xTdMgr->getByHierarchicalName(
     521           0 :                       typeName)
     522           0 :                   >>= 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           0 :             PyRef constant = runtime.any2PyObject( td->getConstantValue() );
     530           0 :             ret = constant.getAcquired();
     531             :         }
     532             :     }
     533           0 :     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           0 :         RuntimeException runExc( e.Message, Reference< XInterface > () );
     538           0 :         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           0 :     return ret;
     553             : }
     554             : 
     555           0 : static PyObject *checkType( SAL_UNUSED_PARAMETER PyObject *, PyObject *args )
     556             : {
     557           0 :     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           0 :     PyObject *obj = PyTuple_GetItem( args, 0 );
     565             : 
     566             :     try
     567             :     {
     568           0 :         PyType2Type( obj );
     569             :     }
     570           0 :     catch(const  RuntimeException & e)
     571             :     {
     572           0 :         raisePyExceptionWithAny( makeAny( e ) );
     573           0 :         return NULL;
     574             :     }
     575           0 :     Py_INCREF( Py_None );
     576           0 :     return Py_None;
     577             : }
     578             : 
     579           0 : static PyObject *checkEnum( SAL_UNUSED_PARAMETER PyObject *, PyObject *args )
     580             : {
     581           0 :     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           0 :     PyObject *obj = PyTuple_GetItem( args, 0 );
     589             : 
     590             :     try
     591             :     {
     592           0 :         PyEnum2Enum( obj );
     593             :     }
     594           0 :     catch(const RuntimeException & e)
     595             :     {
     596           0 :         raisePyExceptionWithAny( makeAny( e) );
     597           0 :         return NULL;
     598             :     }
     599           0 :     Py_INCREF( Py_None );
     600           0 :     return Py_None;
     601             : }
     602             : 
     603           0 : static PyObject *getClass( SAL_UNUSED_PARAMETER PyObject *, PyObject *args )
     604             : {
     605           0 :     PyObject *obj = extractOneStringArg( args, "pyuno.getClass");
     606           0 :     if( ! obj )
     607           0 :         return NULL;
     608             : 
     609             :     try
     610             :     {
     611           0 :         Runtime runtime;
     612           0 :         PyRef ret = getClass(pyString2ustring(obj), runtime);
     613           0 :         Py_XINCREF( ret.get() );
     614           0 :         return ret.get();
     615             :     }
     616           0 :     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           0 :         raisePyExceptionWithAny( makeAny(e) );
     623             :     }
     624           0 :     return NULL;
     625             : }
     626             : 
     627           0 : static PyObject *isInterface( SAL_UNUSED_PARAMETER PyObject *, PyObject *args )
     628             : {
     629             : 
     630           0 :     if( PyTuple_Check( args ) && PyTuple_Size( args ) == 1 )
     631             :     {
     632           0 :         PyObject *obj = PyTuple_GetItem( args, 0 );
     633           0 :         Runtime r;
     634           0 :         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           0 : PyObject* PyInit_pyuno()
     868             : {
     869           0 :     PyUNO_initType();
     870             :     // noop when called already, otherwise needed to allow multiple threads
     871           0 :     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           0 :     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