Version 7.1.7.1, tag libreoffice-7.1.7.1
[LibreOffice.git] / pyuno / source / module / pyuno_except.cxx
blobb833970e87512a29b316432f3e57f2b193d26ddc
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
19 #include "pyuno_impl.hxx"
21 #include <typelib/typedescription.hxx>
22 #include <com/sun/star/script/CannotConvertException.hpp>
25 using com::sun::star::uno::RuntimeException;
26 using com::sun::star::uno::XInterface;
27 using com::sun::star::uno::TypeDescription;
29 namespace pyuno
32 void raisePyExceptionWithAny( const css::uno::Any &anyExc )
34 try
36 Runtime runtime;
37 PyRef exc = runtime.any2PyObject( anyExc );
38 if( exc.is() )
40 PyRef type( getClass( anyExc.getValueType().getTypeName(),runtime ) );
41 PyErr_SetObject( type.get(), exc.get());
43 else
45 css::uno::Exception e;
46 anyExc >>= e;
48 OUString buf = "Couldn't convert uno exception to a python exception (" +
49 anyExc.getValueType().getTypeName() + ": " + e.Message + ")";
50 PyErr_SetString(
51 PyExc_SystemError,
52 OUStringToOString(buf,RTL_TEXTENCODING_ASCII_US).getStr() );
55 catch(const css::lang::IllegalArgumentException & e)
57 PyErr_SetString( PyExc_SystemError,
58 OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US).getStr() );
60 catch(const css::script::CannotConvertException & e)
62 PyErr_SetString( PyExc_SystemError,
63 OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US).getStr() );
65 catch(const RuntimeException & e)
67 PyErr_SetString( PyExc_SystemError,
68 OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US).getStr() );
72 /// @throws RuntimeException
73 static PyRef createClass( const OUString & name, const Runtime &runtime )
75 // assuming that this is never deleted !
76 // note I don't have the knowledge how to initialize these type objects correctly !
77 TypeDescription desc( name );
78 if( ! desc.is() )
80 throw RuntimeException( "pyuno.getClass: uno exception " + name + " is unknown" );
83 bool isStruct = desc.get()->eTypeClass == typelib_TypeClass_STRUCT;
84 bool isExc = desc.get()->eTypeClass == typelib_TypeClass_EXCEPTION;
85 bool isInterface = desc.get()->eTypeClass == typelib_TypeClass_INTERFACE;
86 if( !isStruct && !isExc && ! isInterface )
88 throw RuntimeException( "pyuno.getClass: " + name + "is a " +
89 OUString::createFromAscii( typeClassToString( static_cast<css::uno::TypeClass>(desc.get()->eTypeClass)) ) +
90 ", expected EXCEPTION, STRUCT or INTERFACE" );
93 // retrieve base class
94 PyRef base;
95 if( isInterface )
97 typelib_InterfaceTypeDescription *pDesc = reinterpret_cast<typelib_InterfaceTypeDescription *>(desc.get());
98 if( pDesc->pBaseTypeDescription )
100 base = getClass( pDesc->pBaseTypeDescription->aBase.pTypeName, runtime );
102 else
104 // must be XInterface !
107 else
109 typelib_CompoundTypeDescription *pDesc = reinterpret_cast<typelib_CompoundTypeDescription*>(desc.get());
110 if( pDesc->pBaseTypeDescription )
112 base = getClass( pDesc->pBaseTypeDescription->aBase.pTypeName, runtime );
114 else
116 if( isExc )
117 // we are currently creating the root UNO exception
118 base = PyRef(PyExc_Exception);
121 PyRef args( PyTuple_New( 3 ), SAL_NO_ACQUIRE, NOT_NULL );
123 PyRef pyTypeName = ustring2PyString( name /*.replace( '.', '_' )*/ );
125 PyRef bases;
126 if( base.is() )
128 { // for CC, keeping ref-count being 1
129 bases = PyRef( PyTuple_New( 1 ), SAL_NO_ACQUIRE );
131 PyTuple_SetItem( bases.get(), 0 , base.getAcquired() );
133 else
135 bases = PyRef( PyTuple_New( 0 ), SAL_NO_ACQUIRE );
138 PyTuple_SetItem( args.get(), 0, pyTypeName.getAcquired());
139 PyTuple_SetItem( args.get(), 1, bases.getAcquired() );
140 PyTuple_SetItem( args.get(), 2, PyDict_New() );
142 PyRef ret(
143 PyObject_CallObject(reinterpret_cast<PyObject *>(&PyType_Type) , args.get()),
144 SAL_NO_ACQUIRE );
146 // now overwrite ctor and attrib functions
147 if( isInterface )
149 PyObject_SetAttrString(
150 ret.get(), "__pyunointerface__",
151 ustring2PyString(name).get() );
153 else
155 PyRef ctor = getObjectFromUnoModule( runtime,"_uno_struct__init__" );
156 PyRef setter = getObjectFromUnoModule( runtime,"_uno_struct__setattr__" );
157 PyRef getter = getObjectFromUnoModule( runtime,"_uno_struct__getattr__" );
158 PyRef repr = getObjectFromUnoModule( runtime,"_uno_struct__repr__" );
159 PyRef eq = getObjectFromUnoModule( runtime,"_uno_struct__eq__" );
160 PyRef ne = getObjectFromUnoModule( runtime,"_uno_struct__ne__" );
162 PyObject_SetAttrString(
163 ret.get(), "__pyunostruct__",
164 ustring2PyString(name).get() );
165 PyObject_SetAttrString(
166 ret.get(), "typeName",
167 ustring2PyString(name).get() );
168 PyObject_SetAttrString(
169 ret.get(), "__init__", ctor.get() );
170 PyObject_SetAttrString(
171 ret.get(), "__getattr__", getter.get() );
172 PyObject_SetAttrString(
173 ret.get(), "__setattr__", setter.get() );
174 PyObject_SetAttrString(
175 ret.get(), "__repr__", repr.get() );
176 PyObject_SetAttrString(
177 ret.get(), "__str__", repr.get() );
178 PyObject_SetAttrString(
179 ret.get(), "__eq__", eq.get() );
180 PyObject_SetAttrString(
181 ret.get(), "__ne__", ne.get() );
183 return ret;
186 bool isInstanceOfStructOrException( PyObject *obj)
188 PyRef attr(
189 PyObject_GetAttrString(obj, "__class__"),
190 SAL_NO_ACQUIRE );
191 if(attr.is())
192 return PyObject_HasAttrString(attr.get(), "__pyunostruct__");
193 else
194 return false;
197 bool isInterfaceClass( const Runtime &runtime, PyObject * obj )
199 const ClassSet & set = runtime.getImpl()->cargo->interfaceSet;
200 return set.find( obj ) != set.end();
203 PyRef getClass( const OUString & name , const Runtime &runtime)
205 PyRef ret;
207 RuntimeCargo *cargo =runtime.getImpl()->cargo;
208 ExceptionClassMap::iterator ii = cargo->exceptionMap.find( name );
209 if( ii == cargo->exceptionMap.end() )
211 ret = createClass( name, runtime );
212 cargo->exceptionMap[name] = ret;
213 if( PyObject_HasAttrString(
214 ret.get(), "__pyunointerface__" ) )
215 cargo->interfaceSet.insert( ret );
217 PyObject_SetAttrString(
218 ret.get(), "__pyunointerface__",
219 ustring2PyString(name).get() );
221 else
223 ret = ii->second;
226 return ret;
232 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */