LCOV - code coverage report
Current view: top level - pyuno/source/module - pyuno_except.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 76 97 78.4 %
Date: 2015-06-13 12:38:46 Functions: 5 5 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : #include "pyuno_impl.hxx"
      20             : 
      21             : #include <rtl/ustrbuf.hxx>
      22             : 
      23             : #include <typelib/typedescription.hxx>
      24             : 
      25             : 
      26             : using com::sun::star::uno::RuntimeException;
      27             : using com::sun::star::uno::Sequence;
      28             : using com::sun::star::uno::Type;
      29             : using com::sun::star::uno::Reference;
      30             : using com::sun::star::uno::XInterface;
      31             : using com::sun::star::uno::TypeDescription;
      32             : 
      33             : namespace pyuno
      34             : {
      35             : 
      36         259 : void raisePyExceptionWithAny( const com::sun::star::uno::Any &anyExc )
      37             : {
      38             :     try
      39             :     {
      40         259 :         Runtime runtime;
      41         518 :         PyRef exc = runtime.any2PyObject( anyExc );
      42         259 :         if( exc.is() )
      43             :         {
      44         259 :             PyRef type( getClass( anyExc.getValueType().getTypeName(),runtime ) );
      45         259 :             PyErr_SetObject( type.get(), exc.get());
      46             :         }
      47             :         else
      48             :         {
      49           0 :             com::sun::star::uno::Exception e;
      50           0 :             anyExc >>= e;
      51             : 
      52           0 :             OUStringBuffer buf;
      53           0 :             buf.appendAscii( "Couldn't convert uno exception to a python exception (" );
      54           0 :             buf.append(anyExc.getValueType().getTypeName());
      55           0 :             buf.appendAscii( ": " );
      56           0 :             buf.append(e.Message );
      57           0 :             buf.appendAscii( ")" );
      58             :             PyErr_SetString(
      59             :                 PyExc_SystemError,
      60           0 :                 OUStringToOString(buf.makeStringAndClear(),RTL_TEXTENCODING_ASCII_US).getStr() );
      61         259 :         }
      62             :     }
      63           0 :     catch(const com::sun::star::lang::IllegalArgumentException & e)
      64             :     {
      65             :         PyErr_SetString( PyExc_SystemError,
      66           0 :                          OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US).getStr() );
      67             :     }
      68           0 :     catch(const com::sun::star::script::CannotConvertException & e)
      69             :     {
      70             :         PyErr_SetString( PyExc_SystemError,
      71           0 :                          OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US).getStr() );
      72             :     }
      73           0 :     catch(const RuntimeException & e)
      74             :     {
      75             :         PyErr_SetString( PyExc_SystemError,
      76           0 :                          OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US).getStr() );
      77             :     }
      78         259 : }
      79             : 
      80             : 
      81         237 : static PyRef createClass( const OUString & name, const Runtime &runtime )
      82             :     throw ( RuntimeException )
      83             : {
      84             :     // assuming that this is never deleted !
      85             :     // note I don't have the knowledge how to initialize these type objects correctly !
      86         237 :     TypeDescription desc( name );
      87         237 :     if( ! desc.is() )
      88             :     {
      89         140 :         OUStringBuffer buf;
      90         140 :         buf.appendAscii( "pyuno.getClass: uno exception " );
      91         140 :         buf.append(name).appendAscii( " is unknown" );
      92         140 :         throw RuntimeException( buf.makeStringAndClear() );
      93             :     }
      94             : 
      95          97 :     bool isStruct = desc.get()->eTypeClass == typelib_TypeClass_STRUCT;
      96          97 :     bool isExc = desc.get()->eTypeClass == typelib_TypeClass_EXCEPTION;
      97          97 :     bool isInterface = desc.get()->eTypeClass == typelib_TypeClass_INTERFACE;
      98          97 :     if( !isStruct  && !isExc && ! isInterface )
      99             :     {
     100           0 :         OUStringBuffer buf;
     101           0 :         buf.appendAscii( "pyuno.getClass: " ).append(name).appendAscii( "is a " );
     102             :         buf.appendAscii(
     103           0 :             typeClassToString( (com::sun::star::uno::TypeClass) desc.get()->eTypeClass));
     104           0 :         buf.appendAscii( ", expected EXCEPTION, STRUCT or INTERFACE" );
     105           0 :         throw RuntimeException( buf.makeStringAndClear() );
     106             :     }
     107             : 
     108             :     // retrieve base class
     109         194 :     PyRef base;
     110          97 :     if( isInterface )
     111             :     {
     112          39 :         typelib_InterfaceTypeDescription *pDesc = reinterpret_cast<typelib_InterfaceTypeDescription *>(desc.get());
     113          39 :         if( pDesc->pBaseTypeDescription )
     114             :         {
     115          36 :             base = getClass( pDesc->pBaseTypeDescription->aBase.pTypeName, runtime );
     116             :         }
     117             :         else
     118             :         {
     119             :             // must be XInterface !
     120             :         }
     121             :     }
     122             :     else
     123             :     {
     124          58 :         typelib_CompoundTypeDescription *pDesc = reinterpret_cast<typelib_CompoundTypeDescription*>(desc.get());
     125          58 :         if( pDesc->pBaseTypeDescription )
     126             :         {
     127          25 :             base = getClass( pDesc->pBaseTypeDescription->aBase.pTypeName, runtime );
     128             :         }
     129             :         else
     130             :         {
     131          33 :             if( isExc )
     132             :                 // we are currently creating the root UNO exception
     133           3 :                 base = PyRef(PyExc_Exception);
     134             :         }
     135             :     }
     136         194 :     PyRef args( PyTuple_New( 3 ), SAL_NO_ACQUIRE, NOT_NULL );
     137             : 
     138         194 :     PyRef pyTypeName = ustring2PyString( name /*.replace( '.', '_' )*/ );
     139             : 
     140         194 :     PyRef bases;
     141          97 :     if( base.is() )
     142             :     {
     143             :         { // for CC, keeping ref-count being 1
     144          64 :         bases = PyRef( PyTuple_New( 1 ), SAL_NO_ACQUIRE );
     145             :         }
     146          64 :         PyTuple_SetItem( bases.get(), 0 , base.getAcquired() );
     147             :     }
     148             :     else
     149             :     {
     150          33 :         bases = PyRef( PyTuple_New( 0 ), SAL_NO_ACQUIRE );
     151             :     }
     152             : 
     153          97 :     PyTuple_SetItem( args.get(), 0, pyTypeName.getAcquired());
     154          97 :     PyTuple_SetItem( args.get(), 1, bases.getAcquired() );
     155          97 :     PyTuple_SetItem( args.get(), 2, PyDict_New() );
     156             : 
     157             :     PyRef ret(
     158             :         PyObject_CallObject(reinterpret_cast<PyObject *>(&PyType_Type) , args.get()),
     159          97 :         SAL_NO_ACQUIRE );
     160             : 
     161             :     // now overwrite ctor and attrib functions
     162          97 :     if( isInterface )
     163             :     {
     164             :         PyObject_SetAttrString(
     165             :             ret.get(), "__pyunointerface__",
     166          39 :             ustring2PyString(name).get() );
     167             :     }
     168             :     else
     169             :     {
     170          58 :         PyRef ctor = getObjectFromUnoModule( runtime,"_uno_struct__init__" );
     171         116 :         PyRef setter = getObjectFromUnoModule( runtime,"_uno_struct__setattr__" );
     172         116 :         PyRef getter = getObjectFromUnoModule( runtime,"_uno_struct__getattr__" );
     173         116 :         PyRef repr = getObjectFromUnoModule( runtime,"_uno_struct__repr__" );
     174         116 :         PyRef eq = getObjectFromUnoModule( runtime,"_uno_struct__eq__" );
     175             : 
     176             :         PyObject_SetAttrString(
     177             :             ret.get(), "__pyunostruct__",
     178          58 :             ustring2PyString(name).get() );
     179             :         PyObject_SetAttrString(
     180             :             ret.get(), "typeName",
     181          58 :             ustring2PyString(name).get() );
     182             :         PyObject_SetAttrString(
     183          58 :             ret.get(), "__init__", ctor.get() );
     184             :         PyObject_SetAttrString(
     185          58 :             ret.get(), "__getattr__", getter.get() );
     186             :         PyObject_SetAttrString(
     187          58 :             ret.get(), "__setattr__", setter.get() );
     188             :         PyObject_SetAttrString(
     189          58 :             ret.get(), "__repr__", repr.get() );
     190             :         PyObject_SetAttrString(
     191          58 :             ret.get(), "__str__", repr.get() );
     192             :         PyObject_SetAttrString(
     193         116 :             ret.get(), "__eq__", eq.get() );
     194             :     }
     195         334 :     return ret;
     196             : }
     197             : 
     198          92 : bool isInstanceOfStructOrException( PyObject *obj)
     199             : {
     200             :     PyRef attr(
     201             :         PyObject_GetAttrString(obj, "__class__"),
     202          92 :         SAL_NO_ACQUIRE );
     203          92 :     if(attr.is())
     204          92 :         return PyObject_HasAttrString(attr.get(), "__pyunostruct__");
     205             :     else
     206           0 :         return false;
     207             : }
     208             : 
     209          31 : bool isInterfaceClass( const Runtime &runtime, PyObject * obj )
     210             : {
     211          31 :     const ClassSet & set = runtime.getImpl()->cargo->interfaceSet;
     212          31 :     return set.find( obj ) != set.end();
     213             : }
     214             : 
     215        2465 : PyRef getClass( const OUString & name , const Runtime &runtime)
     216             : {
     217        2465 :     PyRef ret;
     218             : 
     219        2465 :     RuntimeCargo *cargo =runtime.getImpl()->cargo;
     220        2465 :     ExceptionClassMap::iterator ii = cargo->exceptionMap.find( name );
     221        2465 :     if( ii == cargo->exceptionMap.end() )
     222             :     {
     223         237 :         ret = createClass( name, runtime );
     224          97 :         cargo->exceptionMap[name] = ret;
     225          97 :         if( PyObject_HasAttrString(
     226          97 :                 ret.get(), "__pyunointerface__" ) )
     227          64 :             cargo->interfaceSet.insert( ret );
     228             : 
     229             :         PyObject_SetAttrString(
     230             :             ret.get(), "__pyunointerface__",
     231          97 :             ustring2PyString(name).get() );
     232             :     }
     233             :     else
     234             :     {
     235        2228 :         ret = ii->second;
     236             :     }
     237             : 
     238        2325 :     return ret;
     239             : }
     240             : 
     241             : 
     242             : }
     243             : 
     244             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11