1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: pyuno_except.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 #include "pyuno_impl.hxx"
32 #include <rtl/ustrbuf.hxx>
34 #include <typelib/typedescription.hxx>
37 using rtl::OUStringBuffer
;
38 using rtl::OUStringToOString
;
40 using com::sun::star::uno::RuntimeException
;
41 using com::sun::star::uno::Sequence
;
42 using com::sun::star::uno::Type
;
43 using com::sun::star::uno::Reference
;
44 using com::sun::star::uno::XInterface
;
45 using com::sun::star::uno::TypeDescription
;
50 void raisePyExceptionWithAny( const com::sun::star::uno::Any
&anyExc
)
55 PyRef exc
= runtime
.any2PyObject( anyExc
);
58 PyRef
type( getClass( anyExc
.getValueType().getTypeName(),runtime
) );
59 PyErr_SetObject( type
.get(), exc
.get());
63 com::sun::star::uno::Exception e
;
67 buf
.appendAscii( "Couldn't convert uno exception to a python exception (" );
68 buf
.append(anyExc
.getValueType().getTypeName());
69 buf
.appendAscii( ": " );
70 buf
.append(e
.Message
);
71 buf
.appendAscii( ")" );
74 OUStringToOString(buf
.makeStringAndClear(),RTL_TEXTENCODING_ASCII_US
) );
77 catch( com::sun::star::lang::IllegalArgumentException
& e
)
79 PyErr_SetString( PyExc_SystemError
,
80 OUStringToOString( e
.Message
, RTL_TEXTENCODING_ASCII_US
) );
82 catch( com::sun::star::script::CannotConvertException
& e
)
84 PyErr_SetString( PyExc_SystemError
,
85 OUStringToOString( e
.Message
, RTL_TEXTENCODING_ASCII_US
) );
87 catch( RuntimeException
& e
)
89 PyErr_SetString( PyExc_SystemError
,
90 OUStringToOString( e
.Message
, RTL_TEXTENCODING_ASCII_US
) );
95 static PyRef
createClass( const OUString
& name
, const Runtime
&runtime
)
96 throw ( RuntimeException
)
98 // assuming that this is never deleted !
99 // note I don't have the knowledge how to initialize these type objects correctly !
100 TypeDescription
desc( name
);
104 buf
.appendAscii( "pyuno.getClass: uno exception " );
105 buf
.append(name
).appendAscii( " is unknown" );
106 throw RuntimeException( buf
.makeStringAndClear(), Reference
< XInterface
> () );
109 sal_Bool isStruct
= desc
.get()->eTypeClass
== typelib_TypeClass_STRUCT
;
110 sal_Bool isExc
= desc
.get()->eTypeClass
== typelib_TypeClass_EXCEPTION
;
111 sal_Bool isInterface
= desc
.get()->eTypeClass
== typelib_TypeClass_INTERFACE
;
112 if( !isStruct
&& !isExc
&& ! isInterface
)
115 buf
.appendAscii( "pyuno.getClass: " ).append(name
).appendAscii( "is a " );
117 typeClassToString( (com::sun::star::uno::TypeClass
) desc
.get()->eTypeClass
));
118 buf
.appendAscii( ", expected EXCEPTION, STRUCT or INTERFACE" );
119 throw RuntimeException( buf
.makeStringAndClear(), Reference
< XInterface
>() );
122 // retrieve base class
126 typelib_InterfaceTypeDescription
*pDesc
= (typelib_InterfaceTypeDescription
* )desc
.get();
127 if( pDesc
->pBaseTypeDescription
)
129 base
= getClass( pDesc
->pBaseTypeDescription
->aBase
.pTypeName
, runtime
);
133 // must be XInterface !
138 typelib_CompoundTypeDescription
*pDesc
= (typelib_CompoundTypeDescription
*)desc
.get();
139 if( pDesc
->pBaseTypeDescription
)
141 base
= getClass( pDesc
->pBaseTypeDescription
->aBase
.pTypeName
, runtime
);
146 // we are currently creating the root UNO exception
147 base
= PyRef(PyExc_Exception
);
150 PyRef
args( PyTuple_New( 3 ), SAL_NO_ACQUIRE
);
152 PyRef pyTypeName
= ustring2PyString( name
/*.replace( '.', '_' )*/ );
157 { // for CC, keeping ref-count being 1
158 bases
= PyRef( PyTuple_New( 1 ), SAL_NO_ACQUIRE
);
160 PyTuple_SetItem( bases
.get(), 0 , base
.getAcquired() );
164 bases
= PyRef( PyTuple_New( 0 ), SAL_NO_ACQUIRE
);
167 PyTuple_SetItem( args
.get(), 0, pyTypeName
.getAcquired());
168 PyTuple_SetItem( args
.get(), 1, bases
.getAcquired() );
169 PyTuple_SetItem( args
.get(), 2, PyDict_New() );
172 PyObject_CallObject(reinterpret_cast<PyObject
*>(&PyClass_Type
) , args
.get()),
175 // now overwrite ctor and attrib functions
178 PyObject_SetAttrString(
179 ret
.get(), const_cast< char * >("__pyunointerface__"),
180 ustring2PyString(name
).get() );
184 PyRef ctor
= getObjectFromUnoModule( runtime
,"_uno_struct__init__" );
185 PyRef setter
= getObjectFromUnoModule( runtime
,"_uno_struct__setattr__" );
186 PyRef getter
= getObjectFromUnoModule( runtime
,"_uno_struct__getattr__" );
187 PyRef repr
= getObjectFromUnoModule( runtime
,"_uno_struct__repr__" );
188 PyRef eq
= getObjectFromUnoModule( runtime
,"_uno_struct__eq__" );
190 PyObject_SetAttrString(
191 ret
.get(), const_cast< char * >("__pyunostruct__"),
192 ustring2PyString(name
).get() );
193 PyObject_SetAttrString(
194 ret
.get(), const_cast< char * >("typeName"),
195 ustring2PyString(name
).get() );
196 PyObject_SetAttrString(
197 ret
.get(), const_cast< char * >("__init__"), ctor
.get() );
198 PyObject_SetAttrString(
199 ret
.get(), const_cast< char * >("__getattr__"), getter
.get() );
200 PyObject_SetAttrString(
201 ret
.get(), const_cast< char * >("__setattr__"), setter
.get() );
202 PyObject_SetAttrString(
203 ret
.get(), const_cast< char * >("__repr__"), repr
.get() );
204 PyObject_SetAttrString(
205 ret
.get(), const_cast< char * >("__str__"), repr
.get() );
206 PyObject_SetAttrString(
207 ret
.get(), const_cast< char * >("__eq__"), eq
.get() );
212 bool isInstanceOfStructOrException( PyObject
*obj
)
215 PyObject_GetAttrString(obj
, const_cast< char * >("__class__")),
217 return PyObject_HasAttrString(
218 attr
.get(), const_cast< char * >("__pyunostruct__"));
221 sal_Bool
isInterfaceClass( const Runtime
&runtime
, PyObject
* obj
)
223 const ClassSet
& set
= runtime
.getImpl()->cargo
->interfaceSet
;
224 return set
.find( obj
) != set
.end();
227 PyRef
getClass( const OUString
& name
, const Runtime
&runtime
)
231 RuntimeCargo
*cargo
=runtime
.getImpl()->cargo
;
232 ExceptionClassMap::iterator ii
= cargo
->exceptionMap
.find( name
);
233 if( ii
== cargo
->exceptionMap
.end() )
235 ret
= createClass( name
, runtime
);
236 cargo
->exceptionMap
[name
] = ret
;
237 if( PyObject_HasAttrString(
238 ret
.get(), const_cast< char * >("__pyunointerface__") ) )
239 cargo
->interfaceSet
.insert( ret
);
241 PyObject_SetAttrString(
242 ret
.get(), const_cast< char * >("__pyunointerface__"),
243 ustring2PyString(name
).get() );