Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / pyuno / source / module / pyuno_except.cxx
blob1a42330f47ed1f6b09f090d55acbe88d164a7e76
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 <rtl/ustrbuf.hxx>
23 #include <typelib/typedescription.hxx>
26 using com::sun::star::uno::RuntimeException;
27 using com::sun::star::uno::XInterface;
28 using com::sun::star::uno::TypeDescription;
30 namespace pyuno
33 void raisePyExceptionWithAny( const css::uno::Any &anyExc )
35 try
37 Runtime runtime;
38 PyRef exc = runtime.any2PyObject( anyExc );
39 if( exc.is() )
41 PyRef type( getClass( anyExc.getValueType().getTypeName(),runtime ) );
42 PyErr_SetObject( type.get(), exc.get());
44 else
46 css::uno::Exception e;
47 anyExc >>= e;
49 OUStringBuffer buf;
50 buf.append( "Couldn't convert uno exception to a python exception (" );
51 buf.append(anyExc.getValueType().getTypeName());
52 buf.append( ": " );
53 buf.append(e.Message );
54 buf.append( ")" );
55 PyErr_SetString(
56 PyExc_SystemError,
57 OUStringToOString(buf.makeStringAndClear(),RTL_TEXTENCODING_ASCII_US).getStr() );
60 catch(const css::lang::IllegalArgumentException & e)
62 PyErr_SetString( PyExc_SystemError,
63 OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US).getStr() );
65 catch(const css::script::CannotConvertException & e)
67 PyErr_SetString( PyExc_SystemError,
68 OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US).getStr() );
70 catch(const RuntimeException & e)
72 PyErr_SetString( PyExc_SystemError,
73 OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US).getStr() );
78 static PyRef createClass( const OUString & name, const Runtime &runtime )
79 throw ( RuntimeException )
81 // assuming that this is never deleted !
82 // note I don't have the knowledge how to initialize these type objects correctly !
83 TypeDescription desc( name );
84 if( ! desc.is() )
86 OUStringBuffer buf;
87 buf.append( "pyuno.getClass: uno exception " );
88 buf.append(name).append( " is unknown" );
89 throw RuntimeException( buf.makeStringAndClear() );
92 bool isStruct = desc.get()->eTypeClass == typelib_TypeClass_STRUCT;
93 bool isExc = desc.get()->eTypeClass == typelib_TypeClass_EXCEPTION;
94 bool isInterface = desc.get()->eTypeClass == typelib_TypeClass_INTERFACE;
95 if( !isStruct && !isExc && ! isInterface )
97 OUStringBuffer buf;
98 buf.append( "pyuno.getClass: " ).append(name).append( "is a " );
99 buf.appendAscii(
100 typeClassToString( (css::uno::TypeClass) desc.get()->eTypeClass));
101 buf.append( ", expected EXCEPTION, STRUCT or INTERFACE" );
102 throw RuntimeException( buf.makeStringAndClear() );
105 // retrieve base class
106 PyRef base;
107 if( isInterface )
109 typelib_InterfaceTypeDescription *pDesc = reinterpret_cast<typelib_InterfaceTypeDescription *>(desc.get());
110 if( pDesc->pBaseTypeDescription )
112 base = getClass( pDesc->pBaseTypeDescription->aBase.pTypeName, runtime );
114 else
116 // must be XInterface !
119 else
121 typelib_CompoundTypeDescription *pDesc = reinterpret_cast<typelib_CompoundTypeDescription*>(desc.get());
122 if( pDesc->pBaseTypeDescription )
124 base = getClass( pDesc->pBaseTypeDescription->aBase.pTypeName, runtime );
126 else
128 if( isExc )
129 // we are currently creating the root UNO exception
130 base = PyRef(PyExc_Exception);
133 PyRef args( PyTuple_New( 3 ), SAL_NO_ACQUIRE, NOT_NULL );
135 PyRef pyTypeName = ustring2PyString( name /*.replace( '.', '_' )*/ );
137 PyRef bases;
138 if( base.is() )
140 { // for CC, keeping ref-count being 1
141 bases = PyRef( PyTuple_New( 1 ), SAL_NO_ACQUIRE );
143 PyTuple_SetItem( bases.get(), 0 , base.getAcquired() );
145 else
147 bases = PyRef( PyTuple_New( 0 ), SAL_NO_ACQUIRE );
150 PyTuple_SetItem( args.get(), 0, pyTypeName.getAcquired());
151 PyTuple_SetItem( args.get(), 1, bases.getAcquired() );
152 PyTuple_SetItem( args.get(), 2, PyDict_New() );
154 PyRef ret(
155 PyObject_CallObject(reinterpret_cast<PyObject *>(&PyType_Type) , args.get()),
156 SAL_NO_ACQUIRE );
158 // now overwrite ctor and attrib functions
159 if( isInterface )
161 PyObject_SetAttrString(
162 ret.get(), "__pyunointerface__",
163 ustring2PyString(name).get() );
165 else
167 PyRef ctor = getObjectFromUnoModule( runtime,"_uno_struct__init__" );
168 PyRef setter = getObjectFromUnoModule( runtime,"_uno_struct__setattr__" );
169 PyRef getter = getObjectFromUnoModule( runtime,"_uno_struct__getattr__" );
170 PyRef repr = getObjectFromUnoModule( runtime,"_uno_struct__repr__" );
171 PyRef eq = getObjectFromUnoModule( runtime,"_uno_struct__eq__" );
173 PyObject_SetAttrString(
174 ret.get(), "__pyunostruct__",
175 ustring2PyString(name).get() );
176 PyObject_SetAttrString(
177 ret.get(), "typeName",
178 ustring2PyString(name).get() );
179 PyObject_SetAttrString(
180 ret.get(), "__init__", ctor.get() );
181 PyObject_SetAttrString(
182 ret.get(), "__getattr__", getter.get() );
183 PyObject_SetAttrString(
184 ret.get(), "__setattr__", setter.get() );
185 PyObject_SetAttrString(
186 ret.get(), "__repr__", repr.get() );
187 PyObject_SetAttrString(
188 ret.get(), "__str__", repr.get() );
189 PyObject_SetAttrString(
190 ret.get(), "__eq__", eq.get() );
192 return ret;
195 bool isInstanceOfStructOrException( PyObject *obj)
197 PyRef attr(
198 PyObject_GetAttrString(obj, "__class__"),
199 SAL_NO_ACQUIRE );
200 if(attr.is())
201 return PyObject_HasAttrString(attr.get(), "__pyunostruct__");
202 else
203 return false;
206 bool isInterfaceClass( const Runtime &runtime, PyObject * obj )
208 const ClassSet & set = runtime.getImpl()->cargo->interfaceSet;
209 return set.find( obj ) != set.end();
212 PyRef getClass( const OUString & name , const Runtime &runtime)
214 PyRef ret;
216 RuntimeCargo *cargo =runtime.getImpl()->cargo;
217 ExceptionClassMap::iterator ii = cargo->exceptionMap.find( name );
218 if( ii == cargo->exceptionMap.end() )
220 ret = createClass( name, runtime );
221 cargo->exceptionMap[name] = ret;
222 if( PyObject_HasAttrString(
223 ret.get(), "__pyunointerface__" ) )
224 cargo->interfaceSet.insert( ret );
226 PyObject_SetAttrString(
227 ret.get(), "__pyunointerface__",
228 ustring2PyString(name).get() );
230 else
232 ret = ii->second;
235 return ret;
241 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */