LibreOffice Module pyuno (master) 1
pyuno_except.cxx
Go to the documentation of this file.
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 <typelib/typedescription.hxx>
22#include <com/sun/star/script/CannotConvertException.hpp>
23
24
25using com::sun::star::uno::RuntimeException;
26using com::sun::star::uno::XInterface;
27using com::sun::star::uno::TypeDescription;
28
29namespace pyuno
30{
31
32void raisePyExceptionWithAny( const css::uno::Any &anyExc )
33{
34 try
35 {
36 Runtime runtime;
37 PyRef exc = runtime.any2PyObject( anyExc );
38 if( exc.is() )
39 {
40 PyRef type( getClass( anyExc.getValueType().getTypeName(),runtime ) );
41 PyErr_SetObject( type.get(), exc.get());
42 }
43 else
44 {
45 css::uno::Exception e;
46 anyExc >>= e;
47
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() );
53 }
54 }
55 catch(const css::lang::IllegalArgumentException & e)
56 {
57 PyErr_SetString( PyExc_SystemError,
58 OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US).getStr() );
59 }
60 catch(const css::script::CannotConvertException & e)
61 {
62 PyErr_SetString( PyExc_SystemError,
63 OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US).getStr() );
64 }
65 catch(const RuntimeException & e)
66 {
67 PyErr_SetString( PyExc_SystemError,
68 OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US).getStr() );
69 }
70}
71
73static PyRef createClass( const OUString & name, const Runtime &runtime )
74{
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() )
79 {
80 throw RuntimeException( "pyuno.getClass: uno exception " + name + " is unknown" );
81 }
82
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 )
87 {
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" );
91 }
92
93 // retrieve base class
94 PyRef base;
95 if( isInterface )
96 {
97 typelib_InterfaceTypeDescription *pDesc = reinterpret_cast<typelib_InterfaceTypeDescription *>(desc.get());
98 if( pDesc->pBaseTypeDescription )
99 {
100 base = getClass( pDesc->pBaseTypeDescription->aBase.pTypeName, runtime );
101 }
102 else
103 {
104 // must be XInterface !
105 }
106 }
107 else
108 {
109 typelib_CompoundTypeDescription *pDesc = reinterpret_cast<typelib_CompoundTypeDescription*>(desc.get());
110 if( pDesc->pBaseTypeDescription )
111 {
112 base = getClass( pDesc->pBaseTypeDescription->aBase.pTypeName, runtime );
113 }
114 else
115 {
116 if( isExc )
117 // we are currently creating the root UNO exception
118 base = PyRef(PyExc_Exception);
119 }
120 }
121 PyRef args( PyTuple_New( 3 ), SAL_NO_ACQUIRE, NOT_NULL );
122
123 PyRef pyTypeName = ustring2PyString( name /*.replace( '.', '_' )*/ );
124
125 PyRef bases;
126 if( base.is() )
127 {
128 { // for CC, keeping ref-count being 1
129 bases = PyRef( PyTuple_New( 1 ), SAL_NO_ACQUIRE );
130 }
131 PyTuple_SetItem( bases.get(), 0 , base.getAcquired() );
132 }
133 else
134 {
135 bases = PyRef( PyTuple_New( 0 ), SAL_NO_ACQUIRE );
136 }
137
138 PyTuple_SetItem( args.get(), 0, pyTypeName.getAcquired());
139 PyTuple_SetItem( args.get(), 1, bases.getAcquired() );
140 PyTuple_SetItem( args.get(), 2, PyDict_New() );
141
142 PyRef ret(
143 PyObject_CallObject(reinterpret_cast<PyObject *>(&PyType_Type) , args.get()),
144 SAL_NO_ACQUIRE );
145
146 // now overwrite ctor and attrib functions
147 if( isInterface )
148 {
149 PyObject_SetAttrString(
150 ret.get(), "__pyunointerface__",
152 }
153 else
154 {
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__" );
161
162 PyObject_SetAttrString(
163 ret.get(), "__pyunostruct__",
165 PyObject_SetAttrString(
166 ret.get(), "typeName",
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() );
182 }
183 return ret;
184}
185
187{
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;
195}
196
197bool isInterfaceClass( const Runtime &runtime, PyObject * obj )
198{
199 const ClassSet & set = runtime.getImpl()->cargo->interfaceSet;
200 return set.find( obj ) != set.end();
201}
202
203PyRef getClass( const OUString & name , const Runtime &runtime)
204{
205 PyRef ret;
206
207 RuntimeCargo *cargo =runtime.getImpl()->cargo;
208 ExceptionClassMap::iterator ii = cargo->exceptionMap.find( name );
209 if( ii == cargo->exceptionMap.end() )
210 {
211 ret = createClass( name, runtime );
212 cargo->exceptionMap[name] = ret;
213 if( PyObject_HasAttrString(
214 ret.get(), "__pyunointerface__" ) )
215 cargo->interfaceSet.insert( ret );
216
217 PyObject_SetAttrString(
218 ret.get(), "__pyunointerface__",
220 }
221 else
222 {
223 ret = ii->second;
224 }
225
226 return ret;
227}
228
229
230}
231
232/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Helper class for keeping references to python objects.
Definition: pyuno.hxx:80
bool is() const
returns 1 when the reference points to a python object python object, otherwise 0.
Definition: pyuno.hxx:139
PyObject * get() const noexcept
Definition: pyuno.hxx:99
PyObject * getAcquired() const
Definition: pyuno.hxx:101
The pyuno::Runtime class keeps the internal state of the python UNO bridge for the currently in use p...
Definition: pyuno.hxx:164
PyRef any2PyObject(const css::uno::Any &source) const
converts something contained in a UNO Any to a Python object
RuntimeImpl * getImpl() const
Returns the internal handle.
Definition: pyuno.hxx:242
void const * base
const char * name
void set(css::uno::UnoInterfaceReference const &value)
args
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
Definition: pyuno.cxx:72
bool isInstanceOfStructOrException(PyObject *obj)
PyRef ustring2PyString(std::u16string_view source)
Definition: pyuno_util.cxx:51
static PyRef createClass(const OUString &name, const Runtime &runtime)
const char * typeClassToString(css::uno::TypeClass t)
void raisePyExceptionWithAny(const css::uno::Any &anyExc)
std::unordered_set< PyRef, PyRef::Hash > ClassSet
Definition: pyuno_impl.hxx:116
bool isInterfaceClass(const Runtime &runtime, PyObject *obj)
@ NOT_NULL
definition of a no acquire enum for ctors
Definition: pyuno.hxx:65
PyRef getObjectFromUnoModule(const Runtime &runtime, const char *object)
Definition: pyuno_util.cxx:79
PyRef getClass(const OUString &name, const Runtime &runtime)
css::uno::Reference< css::linguistic2::XProofreadingIterator > get(css::uno::Reference< css::uno::XComponentContext > const &context)
def isInterface(obj)
Definition: uno.py:106
ExceptionClassMap exceptionMap
Definition: pyuno_impl.hxx:226
PyObject_HEAD struct RuntimeCargo * cargo
Definition: pyuno_impl.hxx:238
ResultType type