xref: /AOO41X/main/pyuno/source/module/pyuno_runtime.cxx (revision 0ce050fecb06fbc37d87513e4975591538ed770f)
167c7d1c1SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
367c7d1c1SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
467c7d1c1SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
567c7d1c1SAndrew Rist  * distributed with this work for additional information
667c7d1c1SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
767c7d1c1SAndrew Rist  * to you under the Apache License, Version 2.0 (the
867c7d1c1SAndrew Rist  * "License"); you may not use this file except in compliance
967c7d1c1SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
1167c7d1c1SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
1367c7d1c1SAndrew Rist  * Unless required by applicable law or agreed to in writing,
1467c7d1c1SAndrew Rist  * software distributed under the License is distributed on an
1567c7d1c1SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1667c7d1c1SAndrew Rist  * KIND, either express or implied.  See the License for the
1767c7d1c1SAndrew Rist  * specific language governing permissions and limitations
1867c7d1c1SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
2067c7d1c1SAndrew Rist  *************************************************************/
2167c7d1c1SAndrew Rist 
2267c7d1c1SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include "pyuno_impl.hxx"
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include <osl/thread.h>
27cdf0e10cSrcweir #include <osl/module.h>
28cdf0e10cSrcweir #include <osl/process.h>
29cdf0e10cSrcweir #include <rtl/strbuf.hxx>
30cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
31cdf0e10cSrcweir #include <rtl/bootstrap.hxx>
32cdf0e10cSrcweir #include <locale.h>
33cdf0e10cSrcweir 
34cdf0e10cSrcweir #include <typelib/typedescription.hxx>
35cdf0e10cSrcweir 
36cdf0e10cSrcweir #include <com/sun/star/beans/XMaterialHolder.hpp>
37cdf0e10cSrcweir 
38cdf0e10cSrcweir using rtl::OUString;
39cdf0e10cSrcweir using rtl::OUStringToOString;
40cdf0e10cSrcweir using rtl::OUStringBuffer;
41cdf0e10cSrcweir using rtl::OStringBuffer;
42cdf0e10cSrcweir using rtl::OString;
43cdf0e10cSrcweir 
44cdf0e10cSrcweir using com::sun::star::uno::Reference;
45cdf0e10cSrcweir using com::sun::star::uno::XInterface;
46cdf0e10cSrcweir using com::sun::star::uno::Any;
47cdf0e10cSrcweir using com::sun::star::uno::TypeDescription;
48cdf0e10cSrcweir using com::sun::star::uno::Sequence;
49cdf0e10cSrcweir using com::sun::star::uno::Type;
50cdf0e10cSrcweir using com::sun::star::uno::UNO_QUERY;
51cdf0e10cSrcweir using com::sun::star::uno::RuntimeException;
52cdf0e10cSrcweir using com::sun::star::uno::XComponentContext;
53cdf0e10cSrcweir using com::sun::star::lang::XSingleServiceFactory;
54cdf0e10cSrcweir using com::sun::star::lang::XUnoTunnel;
55cdf0e10cSrcweir using com::sun::star::reflection::XIdlReflection;
56cdf0e10cSrcweir using com::sun::star::script::XTypeConverter;
57cdf0e10cSrcweir using com::sun::star::script::XInvocationAdapterFactory2;
58cdf0e10cSrcweir using com::sun::star::script::XInvocation;
59cdf0e10cSrcweir using com::sun::star::beans::XMaterialHolder;
60cdf0e10cSrcweir using com::sun::star::beans::XIntrospection;
61cdf0e10cSrcweir 
62cdf0e10cSrcweir namespace pyuno
63cdf0e10cSrcweir {
64cdf0e10cSrcweir #define USTR_ASCII(x) OUString( RTL_CONSTASCII_USTRINGPARAM( x ) )
65cdf0e10cSrcweir 
66cdf0e10cSrcweir static PyTypeObject RuntimeImpl_Type =
67cdf0e10cSrcweir {
685c3821d8SPedro Giffuni     PyVarObject_HEAD_INIT(&PyType_Type, 0)
69cdf0e10cSrcweir     const_cast< char * >("pyuno_runtime"),
70cdf0e10cSrcweir     sizeof (RuntimeImpl),
71cdf0e10cSrcweir     0,
72cdf0e10cSrcweir     (destructor) RuntimeImpl::del,
73cdf0e10cSrcweir     (printfunc) 0,
74cdf0e10cSrcweir     (getattrfunc) 0,
75cdf0e10cSrcweir     (setattrfunc) 0,
76cdf0e10cSrcweir     (cmpfunc) 0,
77cdf0e10cSrcweir     (reprfunc) 0,
78cdf0e10cSrcweir     0,
79cdf0e10cSrcweir     0,
80cdf0e10cSrcweir     0,
81cdf0e10cSrcweir     (hashfunc) 0,
82cdf0e10cSrcweir     (ternaryfunc) 0,
83cdf0e10cSrcweir     (reprfunc) 0,
84cdf0e10cSrcweir     (getattrofunc)0,
85cdf0e10cSrcweir     (setattrofunc)0,
86cdf0e10cSrcweir     NULL,
87cdf0e10cSrcweir     0,
88cdf0e10cSrcweir     NULL,
89cdf0e10cSrcweir     (traverseproc)0,
90cdf0e10cSrcweir     (inquiry)0,
91cdf0e10cSrcweir     (richcmpfunc)0,
92cdf0e10cSrcweir     0,
93cdf0e10cSrcweir     (getiterfunc)0,
94cdf0e10cSrcweir     (iternextfunc)0,
95cdf0e10cSrcweir     NULL,
96cdf0e10cSrcweir     NULL,
97cdf0e10cSrcweir     NULL,
98cdf0e10cSrcweir     NULL,
99cdf0e10cSrcweir     NULL,
100cdf0e10cSrcweir     (descrgetfunc)0,
101cdf0e10cSrcweir     (descrsetfunc)0,
102cdf0e10cSrcweir     0,
103cdf0e10cSrcweir     (initproc)0,
104cdf0e10cSrcweir     (allocfunc)0,
105cdf0e10cSrcweir     (newfunc)0,
106cdf0e10cSrcweir     (freefunc)0,
107cdf0e10cSrcweir     (inquiry)0,
108cdf0e10cSrcweir     NULL,
109cdf0e10cSrcweir     NULL,
110cdf0e10cSrcweir     NULL,
111cdf0e10cSrcweir     NULL,
112cdf0e10cSrcweir     NULL,
113cdf0e10cSrcweir     (destructor)0
114cdf0e10cSrcweir #if PY_VERSION_HEX >= 0x02060000
115cdf0e10cSrcweir     , 0
116cdf0e10cSrcweir #endif
117cdf0e10cSrcweir };
118cdf0e10cSrcweir 
119cdf0e10cSrcweir /*----------------------------------------------------------------------
120cdf0e10cSrcweir   Runtime implementation
121cdf0e10cSrcweir  -----------------------------------------------------------------------*/
122cdf0e10cSrcweir static void getRuntimeImpl( PyRef & globalDict, PyRef &runtimeImpl )
123cdf0e10cSrcweir     throw ( com::sun::star::uno::RuntimeException )
124cdf0e10cSrcweir {
125cdf0e10cSrcweir     PyThreadState * state = PyThreadState_Get();
126cdf0e10cSrcweir     if( ! state )
127cdf0e10cSrcweir     {
128cdf0e10cSrcweir         throw RuntimeException( OUString( RTL_CONSTASCII_USTRINGPARAM(
129cdf0e10cSrcweir             "python global interpreter must be held (thread must be attached)" )),
130cdf0e10cSrcweir                                 Reference< XInterface > () );
131cdf0e10cSrcweir     }
132cdf0e10cSrcweir 
133cdf0e10cSrcweir     globalDict = PyRef( PyModule_GetDict(PyImport_AddModule(const_cast< char * >("__main__"))));
134cdf0e10cSrcweir 
135cdf0e10cSrcweir     if( ! globalDict.is() ) // FATAL !
136cdf0e10cSrcweir     {
137cdf0e10cSrcweir         throw RuntimeException( OUString( RTL_CONSTASCII_USTRINGPARAM(
138cdf0e10cSrcweir             "can't find __main__ module" )), Reference< XInterface > ());
139cdf0e10cSrcweir     }
140cdf0e10cSrcweir     runtimeImpl = PyDict_GetItemString( globalDict.get() , "pyuno_runtime" );
141cdf0e10cSrcweir }
142cdf0e10cSrcweir 
143cdf0e10cSrcweir static PyRef importUnoModule( ) throw ( RuntimeException )
144cdf0e10cSrcweir {
145cdf0e10cSrcweir     PyRef globalDict = PyRef( PyModule_GetDict(PyImport_AddModule(const_cast< char * >("__main__"))));
146cdf0e10cSrcweir     // import the uno module
147cdf0e10cSrcweir     PyRef module( PyImport_ImportModule( const_cast< char * >("uno") ), SAL_NO_ACQUIRE );
148cdf0e10cSrcweir     if( PyErr_Occurred() )
149cdf0e10cSrcweir     {
150cdf0e10cSrcweir         PyRef excType, excValue, excTraceback;
151cdf0e10cSrcweir         PyErr_Fetch( (PyObject **)&excType, (PyObject**)&excValue,(PyObject**)&excTraceback);
152cdf0e10cSrcweir         PyRef str( PyObject_Repr( excTraceback.get() ), SAL_NO_ACQUIRE );
153cdf0e10cSrcweir 
154cdf0e10cSrcweir         OUStringBuffer buf;
155cdf0e10cSrcweir         buf.appendAscii( "python object raised an unknown exception (" );
156cdf0e10cSrcweir         PyRef valueRep( PyObject_Repr( excValue.get() ), SAL_NO_ACQUIRE );
157564d9007SPedro Giffuni         buf.appendAscii( PyBytes_AsString( valueRep.get())).appendAscii( ", traceback follows\n" );
158564d9007SPedro Giffuni         buf.appendAscii( PyBytes_AsString( str.get() ) );
159cdf0e10cSrcweir         throw RuntimeException( buf.makeStringAndClear(), Reference< XInterface > () );
160cdf0e10cSrcweir     }
161cdf0e10cSrcweir     PyRef dict( PyModule_GetDict( module.get() ) );
162cdf0e10cSrcweir     return dict;
163cdf0e10cSrcweir }
164cdf0e10cSrcweir 
165cdf0e10cSrcweir static void readLoggingConfig( sal_Int32 *pLevel, FILE **ppFile )
166cdf0e10cSrcweir {
167cdf0e10cSrcweir     *pLevel = LogLevel::NONE;
168cdf0e10cSrcweir     *ppFile = 0;
169cdf0e10cSrcweir     OUString fileName;
170cdf0e10cSrcweir     osl_getModuleURLFromFunctionAddress(
171cdf0e10cSrcweir         reinterpret_cast< oslGenericFunction >(readLoggingConfig),
172cdf0e10cSrcweir         (rtl_uString **) &fileName );
173cdf0e10cSrcweir     fileName = OUString( fileName.getStr(), fileName.lastIndexOf( '/' )+1 );
174cdf0e10cSrcweir     fileName += OUString::createFromAscii(  SAL_CONFIGFILE("pyuno") );
175cdf0e10cSrcweir     rtl::Bootstrap bootstrapHandle( fileName );
176cdf0e10cSrcweir 
177cdf0e10cSrcweir     OUString str;
178cdf0e10cSrcweir     if( bootstrapHandle.getFrom( USTR_ASCII( "PYUNO_LOGLEVEL" ), str ) )
179cdf0e10cSrcweir     {
180cdf0e10cSrcweir         if( str.equalsAscii( "NONE" ) )
181cdf0e10cSrcweir             *pLevel = LogLevel::NONE;
182cdf0e10cSrcweir         else if( str.equalsAscii( "CALL" ) )
183cdf0e10cSrcweir             *pLevel = LogLevel::CALL;
184cdf0e10cSrcweir         else if( str.equalsAscii( "ARGS" ) )
185cdf0e10cSrcweir             *pLevel = LogLevel::ARGS;
186cdf0e10cSrcweir         else
187cdf0e10cSrcweir         {
188cdf0e10cSrcweir             fprintf( stderr, "unknown loglevel %s\n",
189cdf0e10cSrcweir                      OUStringToOString( str, RTL_TEXTENCODING_UTF8 ).getStr() );
190cdf0e10cSrcweir         }
191cdf0e10cSrcweir     }
192cdf0e10cSrcweir     if( *pLevel > LogLevel::NONE )
193cdf0e10cSrcweir     {
194cdf0e10cSrcweir         *ppFile = stdout;
195cdf0e10cSrcweir         if( bootstrapHandle.getFrom( USTR_ASCII( "PYUNO_LOGTARGET" ), str ) )
196cdf0e10cSrcweir         {
197cdf0e10cSrcweir             if( str.equalsAscii( "stdout" ) )
198cdf0e10cSrcweir                 *ppFile = stdout;
199cdf0e10cSrcweir             else if( str.equalsAscii( "stderr" ) )
200cdf0e10cSrcweir                 *ppFile = stderr;
201cdf0e10cSrcweir             else
202cdf0e10cSrcweir             {
203cdf0e10cSrcweir                 oslProcessInfo data;
204cdf0e10cSrcweir                 data.Size = sizeof( data );
205cdf0e10cSrcweir                 osl_getProcessInfo(
206cdf0e10cSrcweir                     0 , osl_Process_IDENTIFIER , &data );
207cdf0e10cSrcweir                 osl_getSystemPathFromFileURL( str.pData, &str.pData);
208cdf0e10cSrcweir                 OString o = OUStringToOString( str, osl_getThreadTextEncoding() );
209cdf0e10cSrcweir                 o += ".";
210cdf0e10cSrcweir                 o += OString::valueOf( (sal_Int32)data.Ident );
211cdf0e10cSrcweir 
212cdf0e10cSrcweir                 *ppFile = fopen( o.getStr() , "w" );
213cdf0e10cSrcweir                 if ( *ppFile )
214cdf0e10cSrcweir                 {
215cdf0e10cSrcweir                     // do not buffer (useful if e.g. analyzing a crash)
216cdf0e10cSrcweir                     setvbuf( *ppFile, 0, _IONBF, 0 );
217cdf0e10cSrcweir                 }
218cdf0e10cSrcweir                 else
219cdf0e10cSrcweir                 {
220cdf0e10cSrcweir                     fprintf( stderr, "couldn't create file %s\n",
221cdf0e10cSrcweir                              OUStringToOString( str, RTL_TEXTENCODING_UTF8 ).getStr() );
222cdf0e10cSrcweir 
223cdf0e10cSrcweir                 }
224cdf0e10cSrcweir             }
225cdf0e10cSrcweir         }
226cdf0e10cSrcweir     }
227cdf0e10cSrcweir }
228cdf0e10cSrcweir 
229cdf0e10cSrcweir /*-------------------------------------------------------------------
230cdf0e10cSrcweir  RuntimeImpl implementations
231cdf0e10cSrcweir  *-------------------------------------------------------------------*/
232cdf0e10cSrcweir PyRef stRuntimeImpl::create( const Reference< XComponentContext > &ctx )
233cdf0e10cSrcweir     throw( com::sun::star::uno::RuntimeException )
234cdf0e10cSrcweir {
235cdf0e10cSrcweir     RuntimeImpl *me = PyObject_New (RuntimeImpl, &RuntimeImpl_Type);
236cdf0e10cSrcweir     if( ! me )
237cdf0e10cSrcweir         throw RuntimeException(
238cdf0e10cSrcweir             OUString( RTL_CONSTASCII_USTRINGPARAM( "cannot instantiate pyuno::RuntimeImpl" ) ),
239cdf0e10cSrcweir             Reference< XInterface > () );
240cdf0e10cSrcweir     me->cargo = 0;
241cdf0e10cSrcweir     // must use a different struct here, as the PyObject_New
242cdf0e10cSrcweir     // makes C++ unusable
243cdf0e10cSrcweir     RuntimeCargo *c = new RuntimeCargo();
244cdf0e10cSrcweir     readLoggingConfig( &(c->logLevel) , &(c->logFile) );
245cdf0e10cSrcweir     log( c, LogLevel::CALL, "Instantiating pyuno bridge" );
246cdf0e10cSrcweir 
247cdf0e10cSrcweir     c->valid = 1;
248cdf0e10cSrcweir     c->xContext = ctx;
249cdf0e10cSrcweir     c->xInvocation = Reference< XSingleServiceFactory > (
250cdf0e10cSrcweir         ctx->getServiceManager()->createInstanceWithContext(
251cdf0e10cSrcweir             OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.script.Invocation" ) ),
252cdf0e10cSrcweir             ctx ),
253cdf0e10cSrcweir         UNO_QUERY );
254cdf0e10cSrcweir     if( ! c->xInvocation.is() )
255cdf0e10cSrcweir         throw RuntimeException(
256cdf0e10cSrcweir             OUString( RTL_CONSTASCII_USTRINGPARAM( "pyuno: couldn't instantiate invocation service" ) ),
257cdf0e10cSrcweir             Reference< XInterface > () );
258cdf0e10cSrcweir 
259cdf0e10cSrcweir     c->xTypeConverter = Reference< XTypeConverter > (
260cdf0e10cSrcweir         ctx->getServiceManager()->createInstanceWithContext(
261cdf0e10cSrcweir             OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.script.Converter" ) ),
262cdf0e10cSrcweir             ctx ),
263cdf0e10cSrcweir         UNO_QUERY );
264cdf0e10cSrcweir     if( ! c->xTypeConverter.is() )
265cdf0e10cSrcweir         throw RuntimeException(
266cdf0e10cSrcweir             OUString( RTL_CONSTASCII_USTRINGPARAM( "pyuno: couldn't instantiate typeconverter service" )),
267cdf0e10cSrcweir             Reference< XInterface > () );
268cdf0e10cSrcweir 
269cdf0e10cSrcweir     c->xCoreReflection = Reference< XIdlReflection > (
270cdf0e10cSrcweir         ctx->getServiceManager()->createInstanceWithContext(
271cdf0e10cSrcweir             OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.reflection.CoreReflection" ) ),
272cdf0e10cSrcweir             ctx ),
273cdf0e10cSrcweir         UNO_QUERY );
274cdf0e10cSrcweir     if( ! c->xCoreReflection.is() )
275cdf0e10cSrcweir         throw RuntimeException(
276cdf0e10cSrcweir             OUString( RTL_CONSTASCII_USTRINGPARAM( "pyuno: couldn't instantiate corereflection service" )),
277cdf0e10cSrcweir             Reference< XInterface > () );
278cdf0e10cSrcweir 
279cdf0e10cSrcweir     c->xAdapterFactory = Reference< XInvocationAdapterFactory2 > (
280cdf0e10cSrcweir         ctx->getServiceManager()->createInstanceWithContext(
281cdf0e10cSrcweir             OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.script.InvocationAdapterFactory" ) ),
282cdf0e10cSrcweir             ctx ),
283cdf0e10cSrcweir         UNO_QUERY );
284cdf0e10cSrcweir     if( ! c->xAdapterFactory.is() )
285cdf0e10cSrcweir         throw RuntimeException(
286cdf0e10cSrcweir             OUString( RTL_CONSTASCII_USTRINGPARAM( "pyuno: couldn't instantiate invocation adapter factory service" )),
287cdf0e10cSrcweir             Reference< XInterface > () );
288cdf0e10cSrcweir 
289cdf0e10cSrcweir     c->xIntrospection = Reference< XIntrospection > (
290cdf0e10cSrcweir         ctx->getServiceManager()->createInstanceWithContext(
291cdf0e10cSrcweir             OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.beans.Introspection" ) ),
292cdf0e10cSrcweir             ctx ),
293cdf0e10cSrcweir         UNO_QUERY );
294cdf0e10cSrcweir     if( ! c->xIntrospection.is() )
295cdf0e10cSrcweir         throw RuntimeException(
296cdf0e10cSrcweir             OUString( RTL_CONSTASCII_USTRINGPARAM( "pyuno: couldn't instantiate introspection service" )),
297cdf0e10cSrcweir             Reference< XInterface > () );
298cdf0e10cSrcweir 
299cdf0e10cSrcweir     Any a = ctx->getValueByName(OUString(
300cdf0e10cSrcweir         RTL_CONSTASCII_USTRINGPARAM("/singletons/com.sun.star.reflection.theTypeDescriptionManager" )) );
301cdf0e10cSrcweir     a >>= c->xTdMgr;
302cdf0e10cSrcweir     if( ! c->xTdMgr.is() )
303cdf0e10cSrcweir         throw RuntimeException(
304cdf0e10cSrcweir             OUString( RTL_CONSTASCII_USTRINGPARAM( "pyuno: couldn't retrieve typedescriptionmanager" )),
305cdf0e10cSrcweir             Reference< XInterface > () );
306cdf0e10cSrcweir 
307cdf0e10cSrcweir     me->cargo =c;
308cdf0e10cSrcweir     return PyRef( reinterpret_cast< PyObject * > ( me ), SAL_NO_ACQUIRE );
309cdf0e10cSrcweir }
310cdf0e10cSrcweir 
311cdf0e10cSrcweir void  stRuntimeImpl::del(PyObject* self)
312cdf0e10cSrcweir {
313cdf0e10cSrcweir     RuntimeImpl *me = reinterpret_cast< RuntimeImpl * > ( self );
314cdf0e10cSrcweir     if( me->cargo->logFile )
315cdf0e10cSrcweir         fclose( me->cargo->logFile );
316cdf0e10cSrcweir     delete me->cargo;
317cdf0e10cSrcweir     PyObject_Del (self);
318cdf0e10cSrcweir }
319cdf0e10cSrcweir 
320cdf0e10cSrcweir 
321cdf0e10cSrcweir void Runtime::initialize( const Reference< XComponentContext > & ctx )
322cdf0e10cSrcweir     throw ( RuntimeException )
323cdf0e10cSrcweir {
324cdf0e10cSrcweir     PyRef globalDict, runtime;
325cdf0e10cSrcweir     getRuntimeImpl( globalDict , runtime );
326cdf0e10cSrcweir     RuntimeImpl *impl = reinterpret_cast< RuntimeImpl * > (runtime.get());
327cdf0e10cSrcweir 
328cdf0e10cSrcweir     if( runtime.is() && impl->cargo->valid )
329cdf0e10cSrcweir     {
330cdf0e10cSrcweir         throw RuntimeException( OUString( RTL_CONSTASCII_USTRINGPARAM(
331cdf0e10cSrcweir             "pyuno runtime has already been initialized before" ) ),
332cdf0e10cSrcweir                                 Reference< XInterface > () );
333cdf0e10cSrcweir     }
334cdf0e10cSrcweir     PyRef keep( RuntimeImpl::create( ctx ) );
335cdf0e10cSrcweir     PyDict_SetItemString( globalDict.get(), "pyuno_runtime" , keep.get() );
336cdf0e10cSrcweir     Py_XINCREF( keep.get() );
337cdf0e10cSrcweir }
338cdf0e10cSrcweir 
339cdf0e10cSrcweir 
340cdf0e10cSrcweir bool Runtime::isInitialized() throw ( RuntimeException )
341cdf0e10cSrcweir {
342cdf0e10cSrcweir     PyRef globalDict, runtime;
343cdf0e10cSrcweir     getRuntimeImpl( globalDict , runtime );
344cdf0e10cSrcweir     RuntimeImpl *impl = reinterpret_cast< RuntimeImpl * > (runtime.get());
345cdf0e10cSrcweir     return runtime.is() && impl->cargo->valid;
346cdf0e10cSrcweir }
347cdf0e10cSrcweir 
348cdf0e10cSrcweir void Runtime::finalize() throw (RuntimeException)
349cdf0e10cSrcweir {
350cdf0e10cSrcweir     PyRef globalDict, runtime;
351cdf0e10cSrcweir     getRuntimeImpl( globalDict , runtime );
352cdf0e10cSrcweir     RuntimeImpl *impl = reinterpret_cast< RuntimeImpl * > (runtime.get());
353cdf0e10cSrcweir     if( !runtime.is() || ! impl->cargo->valid )
354cdf0e10cSrcweir     {
355cdf0e10cSrcweir         throw RuntimeException( OUString( RTL_CONSTASCII_USTRINGPARAM(
356cdf0e10cSrcweir             "pyuno bridge must have been initialized before finalizing" )),
357cdf0e10cSrcweir                                 Reference< XInterface > () );
358cdf0e10cSrcweir     }
359cdf0e10cSrcweir     impl->cargo->valid = false;
360cdf0e10cSrcweir     impl->cargo->xInvocation.clear();
361cdf0e10cSrcweir     impl->cargo->xContext.clear();
362cdf0e10cSrcweir     impl->cargo->xTypeConverter.clear();
363cdf0e10cSrcweir }
364cdf0e10cSrcweir 
365cdf0e10cSrcweir Runtime::Runtime() throw(  RuntimeException )
366cdf0e10cSrcweir     : impl( 0 )
367cdf0e10cSrcweir {
368cdf0e10cSrcweir     PyRef globalDict, runtime;
369cdf0e10cSrcweir     getRuntimeImpl( globalDict , runtime );
370cdf0e10cSrcweir     if( ! runtime.is() )
371cdf0e10cSrcweir     {
372cdf0e10cSrcweir         throw RuntimeException(
373cdf0e10cSrcweir             OUString( RTL_CONSTASCII_USTRINGPARAM("pyuno runtime is not initialized, "
374cdf0e10cSrcweir                                                   "(the pyuno.bootstrap needs to be called before using any uno classes)")),
375cdf0e10cSrcweir             Reference< XInterface > () );
376cdf0e10cSrcweir     }
377cdf0e10cSrcweir     impl = reinterpret_cast< RuntimeImpl * > (runtime.get());
378cdf0e10cSrcweir     Py_XINCREF( runtime.get() );
379cdf0e10cSrcweir }
380cdf0e10cSrcweir 
381cdf0e10cSrcweir Runtime::Runtime( const Runtime & r )
382cdf0e10cSrcweir {
383cdf0e10cSrcweir     impl = r.impl;
384cdf0e10cSrcweir     Py_XINCREF( reinterpret_cast< PyObject * >(impl) );
385cdf0e10cSrcweir }
386cdf0e10cSrcweir 
387cdf0e10cSrcweir Runtime::~Runtime()
388cdf0e10cSrcweir {
389cdf0e10cSrcweir     Py_XDECREF( reinterpret_cast< PyObject * >(impl) );
390cdf0e10cSrcweir }
391cdf0e10cSrcweir 
392cdf0e10cSrcweir Runtime & Runtime::operator = ( const Runtime & r )
393cdf0e10cSrcweir {
394cdf0e10cSrcweir     PyRef temp( reinterpret_cast< PyObject * >(r.impl) );
395cdf0e10cSrcweir     Py_XINCREF( temp.get() );
396cdf0e10cSrcweir     Py_XDECREF( reinterpret_cast< PyObject * >(impl) );
397cdf0e10cSrcweir     impl = r.impl;
398cdf0e10cSrcweir     return *this;
399cdf0e10cSrcweir }
400cdf0e10cSrcweir 
401cdf0e10cSrcweir PyRef Runtime::any2PyObject (const Any &a ) const
402cdf0e10cSrcweir     throw ( com::sun::star::script::CannotConvertException,
403cdf0e10cSrcweir             com::sun::star::lang::IllegalArgumentException,
404cdf0e10cSrcweir             RuntimeException)
405cdf0e10cSrcweir {
406cdf0e10cSrcweir     if( ! impl->cargo->valid )
407cdf0e10cSrcweir     {
408cdf0e10cSrcweir         throw RuntimeException( OUString( RTL_CONSTASCII_USTRINGPARAM(
409cdf0e10cSrcweir             "pyuno runtime must be initialized before calling any2PyObject" )),
410cdf0e10cSrcweir                                 Reference< XInterface > () );
411cdf0e10cSrcweir     }
412cdf0e10cSrcweir 
413cdf0e10cSrcweir     switch (a.getValueTypeClass ())
414cdf0e10cSrcweir     {
415cdf0e10cSrcweir     case typelib_TypeClass_VOID:
416cdf0e10cSrcweir 	{
417cdf0e10cSrcweir         Py_INCREF (Py_None);
418cdf0e10cSrcweir         return PyRef(Py_None);
419cdf0e10cSrcweir 	}
420cdf0e10cSrcweir     case typelib_TypeClass_CHAR:
421cdf0e10cSrcweir 	{
422cdf0e10cSrcweir         sal_Unicode c = *(sal_Unicode*)a.getValue();
423cdf0e10cSrcweir         return PyRef( PyUNO_char_new( c , *this ), SAL_NO_ACQUIRE );
424cdf0e10cSrcweir 	}
425cdf0e10cSrcweir     case typelib_TypeClass_BOOLEAN:
426cdf0e10cSrcweir 	{
427cdf0e10cSrcweir         sal_Bool b = sal_Bool();
428cdf0e10cSrcweir         if ((a >>= b) && b)
429cdf0e10cSrcweir             return Py_True;
430cdf0e10cSrcweir         else
431cdf0e10cSrcweir             return Py_False;
432cdf0e10cSrcweir 	}
433cdf0e10cSrcweir     case typelib_TypeClass_BYTE:
434cdf0e10cSrcweir     case typelib_TypeClass_SHORT:
435cdf0e10cSrcweir     case typelib_TypeClass_UNSIGNED_SHORT:
436cdf0e10cSrcweir     case typelib_TypeClass_LONG:
437cdf0e10cSrcweir 	{
438cdf0e10cSrcweir         sal_Int32 l = 0;
439cdf0e10cSrcweir         a >>= l;
4405c3821d8SPedro Giffuni #if PY_MAJOR_VERSION >= 3
4415c3821d8SPedro Giffuni         return PyRef( PyLong_FromLong (l), SAL_NO_ACQUIRE );
4425c3821d8SPedro Giffuni #else
443cdf0e10cSrcweir         return PyRef( PyInt_FromLong (l), SAL_NO_ACQUIRE );
4445c3821d8SPedro Giffuni #endif
445cdf0e10cSrcweir 	}
446cdf0e10cSrcweir     case typelib_TypeClass_UNSIGNED_LONG:
447cdf0e10cSrcweir 	{
448cdf0e10cSrcweir         sal_uInt32 l = 0;
449cdf0e10cSrcweir         a >>= l;
450cdf0e10cSrcweir         return PyRef( PyLong_FromUnsignedLong (l), SAL_NO_ACQUIRE );
451cdf0e10cSrcweir 	}
452cdf0e10cSrcweir     case typelib_TypeClass_HYPER:
453cdf0e10cSrcweir 	{
454cdf0e10cSrcweir         sal_Int64 l = 0;
455cdf0e10cSrcweir         a >>= l;
456cdf0e10cSrcweir         return PyRef( PyLong_FromLongLong (l), SAL_NO_ACQUIRE);
457cdf0e10cSrcweir 	}
458cdf0e10cSrcweir     case typelib_TypeClass_UNSIGNED_HYPER:
459cdf0e10cSrcweir 	{
460cdf0e10cSrcweir         sal_uInt64 l = 0;
461cdf0e10cSrcweir         a >>= l;
462cdf0e10cSrcweir         return PyRef( PyLong_FromUnsignedLongLong (l), SAL_NO_ACQUIRE);
463cdf0e10cSrcweir 	}
464cdf0e10cSrcweir     case typelib_TypeClass_FLOAT:
465cdf0e10cSrcweir 	{
466cdf0e10cSrcweir         float f = 0.0;
467cdf0e10cSrcweir         a >>= f;
468cdf0e10cSrcweir         return PyRef(PyFloat_FromDouble (f), SAL_NO_ACQUIRE);
469cdf0e10cSrcweir 	}
470cdf0e10cSrcweir     case typelib_TypeClass_DOUBLE:
471cdf0e10cSrcweir 	{
472cdf0e10cSrcweir         double d = 0.0;
473cdf0e10cSrcweir         a >>= d;
474cdf0e10cSrcweir         return PyRef( PyFloat_FromDouble (d), SAL_NO_ACQUIRE);
475cdf0e10cSrcweir 	}
476cdf0e10cSrcweir     case typelib_TypeClass_STRING:
477cdf0e10cSrcweir 	{
478cdf0e10cSrcweir         OUString tmp_ostr;
479cdf0e10cSrcweir         a >>= tmp_ostr;
480cdf0e10cSrcweir         return ustring2PyUnicode( tmp_ostr );
481cdf0e10cSrcweir 	}
482cdf0e10cSrcweir     case typelib_TypeClass_TYPE:
483cdf0e10cSrcweir 	{
484cdf0e10cSrcweir         Type t;
485cdf0e10cSrcweir         a >>= t;
486cdf0e10cSrcweir         OString o = OUStringToOString( t.getTypeName(), RTL_TEXTENCODING_ASCII_US );
487cdf0e10cSrcweir         return PyRef(
488cdf0e10cSrcweir             PyUNO_Type_new (
489cdf0e10cSrcweir                 o.getStr(),  (com::sun::star::uno::TypeClass)t.getTypeClass(), *this),
490cdf0e10cSrcweir             SAL_NO_ACQUIRE);
491cdf0e10cSrcweir 	}
492cdf0e10cSrcweir     case typelib_TypeClass_ANY:
493cdf0e10cSrcweir 	{
494cdf0e10cSrcweir         //I don't think this can happen.
495cdf0e10cSrcweir         Py_INCREF (Py_None);
496cdf0e10cSrcweir         return Py_None;
497cdf0e10cSrcweir 	}
498cdf0e10cSrcweir     case typelib_TypeClass_ENUM:
499cdf0e10cSrcweir 	{
500cdf0e10cSrcweir         sal_Int32 l = *(sal_Int32 *) a.getValue();
501cdf0e10cSrcweir         TypeDescription desc( a.getValueType() );
502cdf0e10cSrcweir         if( desc.is() )
503cdf0e10cSrcweir         {
504cdf0e10cSrcweir             desc.makeComplete();
505cdf0e10cSrcweir             typelib_EnumTypeDescription *pEnumDesc =
506cdf0e10cSrcweir                 (typelib_EnumTypeDescription *) desc.get();
507cdf0e10cSrcweir             for( int i = 0 ; i < pEnumDesc->nEnumValues ; i ++ )
508cdf0e10cSrcweir             {
509cdf0e10cSrcweir                 if( pEnumDesc->pEnumValues[i] == l )
510cdf0e10cSrcweir                 {
511cdf0e10cSrcweir                     OString v = OUStringToOString( pEnumDesc->ppEnumNames[i], RTL_TEXTENCODING_ASCII_US);
512cdf0e10cSrcweir                     OString e = OUStringToOString( pEnumDesc->aBase.pTypeName, RTL_TEXTENCODING_ASCII_US);
513cdf0e10cSrcweir                     return PyRef( PyUNO_Enum_new(e.getStr(),v.getStr(), *this ), SAL_NO_ACQUIRE );
514cdf0e10cSrcweir                 }
515cdf0e10cSrcweir             }
516cdf0e10cSrcweir         }
517cdf0e10cSrcweir         OUStringBuffer buf;
518cdf0e10cSrcweir         buf.appendAscii( "Any carries enum " );
519cdf0e10cSrcweir         buf.append( a.getValueType().getTypeName());
520cdf0e10cSrcweir         buf.appendAscii( " with invalid value " ).append( l );
521cdf0e10cSrcweir         throw RuntimeException( buf.makeStringAndClear() , Reference< XInterface > ()  );
522cdf0e10cSrcweir 	}
523cdf0e10cSrcweir     case typelib_TypeClass_EXCEPTION:
524cdf0e10cSrcweir     case typelib_TypeClass_STRUCT:
525cdf0e10cSrcweir     {
526cdf0e10cSrcweir         PyRef excClass = getClass( a.getValueType().getTypeName(), *this );
527cdf0e10cSrcweir         PyRef value = PyRef( PyUNO_new_UNCHECKED (a, getImpl()->cargo->xInvocation), SAL_NO_ACQUIRE);
528cdf0e10cSrcweir         PyRef argsTuple( PyTuple_New( 1 ) , SAL_NO_ACQUIRE );
529cdf0e10cSrcweir         PyTuple_SetItem( argsTuple.get() , 0 , value.getAcquired() );
530cdf0e10cSrcweir         PyRef ret( PyObject_CallObject( excClass.get() , argsTuple.get() ), SAL_NO_ACQUIRE );
531cdf0e10cSrcweir         if( ! ret.is() )
532cdf0e10cSrcweir         {
533cdf0e10cSrcweir             OUStringBuffer buf;
534cdf0e10cSrcweir             buf.appendAscii( "Couldn't instantiate python representation of structered UNO type " );
535cdf0e10cSrcweir             buf.append( a.getValueType().getTypeName() );
536cdf0e10cSrcweir             throw RuntimeException( buf.makeStringAndClear(), Reference< XInterface > () );
537cdf0e10cSrcweir         }
538cdf0e10cSrcweir 
539cdf0e10cSrcweir         if( com::sun::star::uno::TypeClass_EXCEPTION == a.getValueTypeClass() )
540cdf0e10cSrcweir         {
541cdf0e10cSrcweir             // add the message in a standard python way !
542cdf0e10cSrcweir             PyRef args( PyTuple_New( 1 ), SAL_NO_ACQUIRE );
543cdf0e10cSrcweir 
544cdf0e10cSrcweir             // assuming that the Message is always the first member, wuuuu
545cdf0e10cSrcweir             void *pData = (void*)a.getValue();
546cdf0e10cSrcweir             OUString message = *(OUString * )pData;
547cdf0e10cSrcweir             PyRef pymsg = ustring2PyString( message );
548cdf0e10cSrcweir             PyTuple_SetItem( args.get(), 0 , pymsg.getAcquired() );
549cdf0e10cSrcweir             // the exception base functions want to have an "args" tuple,
550cdf0e10cSrcweir             // which contains the message
551cdf0e10cSrcweir             PyObject_SetAttrString( ret.get(), const_cast< char * >("args"), args.get() );
552cdf0e10cSrcweir         }
553cdf0e10cSrcweir         return ret;
554cdf0e10cSrcweir     }
555cdf0e10cSrcweir     case typelib_TypeClass_SEQUENCE:
556cdf0e10cSrcweir 	{
557cdf0e10cSrcweir         Sequence<Any> s;
558cdf0e10cSrcweir 
559cdf0e10cSrcweir         Sequence< sal_Int8 > byteSequence;
560cdf0e10cSrcweir         if( a >>= byteSequence )
561cdf0e10cSrcweir         {
562cdf0e10cSrcweir             // byte sequence is treated in a special way because of peformance reasons
563cdf0e10cSrcweir             // @since 0.9.2
564cdf0e10cSrcweir             return PyRef( PyUNO_ByteSequence_new( byteSequence, *this ), SAL_NO_ACQUIRE );
565cdf0e10cSrcweir         }
566cdf0e10cSrcweir         else
567cdf0e10cSrcweir         {
568cdf0e10cSrcweir             Reference< XTypeConverter > tc = getImpl()->cargo->xTypeConverter;
569cdf0e10cSrcweir             Reference< XSingleServiceFactory > ssf = getImpl()->cargo->xInvocation;
570cdf0e10cSrcweir             tc->convertTo (a, ::getCppuType (&s)) >>= s;
571cdf0e10cSrcweir             PyRef tuple( PyTuple_New (s.getLength()), SAL_NO_ACQUIRE);
572cdf0e10cSrcweir             int i=0;
573cdf0e10cSrcweir             OUString errMsg;
574cdf0e10cSrcweir             try
575cdf0e10cSrcweir             {
576cdf0e10cSrcweir                 for ( i = 0; i < s.getLength (); i++)
577cdf0e10cSrcweir                 {
578cdf0e10cSrcweir                     PyRef element;
579cdf0e10cSrcweir                     element = any2PyObject (tc->convertTo (s[i], s[i].getValueType() ));
580cdf0e10cSrcweir                     OSL_ASSERT( element.is() );
581cdf0e10cSrcweir                     PyTuple_SetItem( tuple.get(), i, element.getAcquired() );
582cdf0e10cSrcweir                 }
583cdf0e10cSrcweir             }
584cdf0e10cSrcweir             catch( com::sun::star::uno::Exception & )
585cdf0e10cSrcweir             {
586cdf0e10cSrcweir                 for( ; i < s.getLength() ; i ++ )
587cdf0e10cSrcweir                 {
588cdf0e10cSrcweir                     Py_INCREF( Py_None );
589cdf0e10cSrcweir                     PyTuple_SetItem( tuple.get(), i,  Py_None );
590cdf0e10cSrcweir                 }
591cdf0e10cSrcweir                 throw;
592cdf0e10cSrcweir             }
593cdf0e10cSrcweir             return tuple;
594cdf0e10cSrcweir 	    }
595cdf0e10cSrcweir 	}
596cdf0e10cSrcweir     case typelib_TypeClass_INTERFACE:
597cdf0e10cSrcweir 	{
598cdf0e10cSrcweir         Reference< XUnoTunnel > tunnel;
599cdf0e10cSrcweir         a >>= tunnel;
600cdf0e10cSrcweir         if( tunnel.is() )
601cdf0e10cSrcweir         {
602cdf0e10cSrcweir             sal_Int64 that = tunnel->getSomething( ::pyuno::Adapter::getUnoTunnelImplementationId() );
603cdf0e10cSrcweir             if( that )
604cdf0e10cSrcweir                 return ((Adapter*)sal::static_int_cast< sal_IntPtr >(that))->getWrappedObject();
605cdf0e10cSrcweir         }
606cdf0e10cSrcweir         //This is just like the struct case:
607cdf0e10cSrcweir         return PyRef( PyUNO_new (a, getImpl()->cargo->xInvocation), SAL_NO_ACQUIRE );
608cdf0e10cSrcweir 	}
609cdf0e10cSrcweir     default:
610cdf0e10cSrcweir 	{
611cdf0e10cSrcweir         OUStringBuffer buf;
612cdf0e10cSrcweir         buf.appendAscii( "Unknonwn UNO type class " );
613cdf0e10cSrcweir         buf.append( (sal_Int32 ) a.getValueTypeClass() );
614cdf0e10cSrcweir         throw RuntimeException(buf.makeStringAndClear( ), Reference< XInterface > () );
615cdf0e10cSrcweir 	}
616cdf0e10cSrcweir     }
617cdf0e10cSrcweir     //We shouldn't be here...
618cdf0e10cSrcweir     Py_INCREF( Py_None );
619cdf0e10cSrcweir     return Py_None;
620cdf0e10cSrcweir }
621cdf0e10cSrcweir 
622cdf0e10cSrcweir static Sequence< Type > invokeGetTypes( const Runtime & r , PyObject * o )
623cdf0e10cSrcweir {
624cdf0e10cSrcweir     Sequence< Type > ret;
625cdf0e10cSrcweir 
626cdf0e10cSrcweir     PyRef method( PyObject_GetAttrString( o , const_cast< char * >("getTypes") ), SAL_NO_ACQUIRE );
627cdf0e10cSrcweir     raiseInvocationTargetExceptionWhenNeeded( r );
628cdf0e10cSrcweir     if( method.is() && PyCallable_Check( method.get() ) )
629cdf0e10cSrcweir     {
630cdf0e10cSrcweir         PyRef types( PyObject_CallObject( method.get(), 0 ) , SAL_NO_ACQUIRE );
631cdf0e10cSrcweir         raiseInvocationTargetExceptionWhenNeeded( r );
632cdf0e10cSrcweir         if( types.is() && PyTuple_Check( types.get() ) )
633cdf0e10cSrcweir         {
634cdf0e10cSrcweir             int size = PyTuple_Size( types.get() );
635cdf0e10cSrcweir 
636cdf0e10cSrcweir             // add the XUnoTunnel interface  for uno object identity concept (hack)
637cdf0e10cSrcweir             ret.realloc( size + 1 );
638cdf0e10cSrcweir             for( int i = 0 ; i < size ; i ++ )
639cdf0e10cSrcweir             {
640cdf0e10cSrcweir                 Any a = r.pyObject2Any(PyTuple_GetItem(types.get(),i));
641cdf0e10cSrcweir                 a >>= ret[i];
642cdf0e10cSrcweir             }
643cdf0e10cSrcweir             ret[size] = getCppuType( (Reference< com::sun::star::lang::XUnoTunnel> *) 0 );
644cdf0e10cSrcweir         }
645cdf0e10cSrcweir     }
646cdf0e10cSrcweir     return ret;
647cdf0e10cSrcweir }
648cdf0e10cSrcweir 
649cdf0e10cSrcweir Any Runtime::pyObject2Any ( const PyRef & source, enum ConversionMode mode ) const
650cdf0e10cSrcweir     throw ( com::sun::star::uno::RuntimeException )
651cdf0e10cSrcweir {
652cdf0e10cSrcweir     if( ! impl->cargo->valid )
653cdf0e10cSrcweir     {
654cdf0e10cSrcweir         throw RuntimeException( OUString( RTL_CONSTASCII_USTRINGPARAM(
655cdf0e10cSrcweir             "pyuno runtime must be initialized before calling any2PyObject" )),
656cdf0e10cSrcweir                                 Reference< XInterface > () );
657cdf0e10cSrcweir     }
658cdf0e10cSrcweir 
659cdf0e10cSrcweir     Any a;
660cdf0e10cSrcweir     PyObject *o = source.get();
661cdf0e10cSrcweir     if( Py_None == o )
662cdf0e10cSrcweir     {
663cdf0e10cSrcweir 
664cdf0e10cSrcweir     }
665*0ce050feSPedro Giffuni #if PY_MAJOR_VERSION < 3	// Python 3 has no PyInt
666cdf0e10cSrcweir     else if (PyInt_Check (o))
667cdf0e10cSrcweir     {
668cdf0e10cSrcweir         if( o == Py_True )
669cdf0e10cSrcweir         {
670cdf0e10cSrcweir             sal_Bool b = sal_True;
671cdf0e10cSrcweir             a = Any( &b, getBooleanCppuType() );
672cdf0e10cSrcweir         }
673cdf0e10cSrcweir         else if ( o == Py_False )
674cdf0e10cSrcweir         {
675cdf0e10cSrcweir             sal_Bool b = sal_False;
676cdf0e10cSrcweir             a = Any( &b, getBooleanCppuType() );
677cdf0e10cSrcweir         }
678cdf0e10cSrcweir         else
679cdf0e10cSrcweir         {
680cdf0e10cSrcweir             sal_Int32 l = (sal_Int32) PyInt_AsLong( o );
681cdf0e10cSrcweir             if( l < 128 && l >= -128 )
682cdf0e10cSrcweir             {
683cdf0e10cSrcweir                 sal_Int8 b = (sal_Int8 ) l;
684cdf0e10cSrcweir                 a <<= b;
685cdf0e10cSrcweir             }
686cdf0e10cSrcweir             else if( l <= 0x7fff && l >= -0x8000 )
687cdf0e10cSrcweir             {
688cdf0e10cSrcweir                 sal_Int16 s = (sal_Int16) l;
689cdf0e10cSrcweir                 a <<= s;
690cdf0e10cSrcweir             }
691cdf0e10cSrcweir             else
692cdf0e10cSrcweir             {
693cdf0e10cSrcweir                 a <<= l;
694cdf0e10cSrcweir             }
695cdf0e10cSrcweir         }
696cdf0e10cSrcweir     }
697*0ce050feSPedro Giffuni #endif				// Python 3 has no PyInt
698cdf0e10cSrcweir     else if (PyLong_Check (o))
699cdf0e10cSrcweir     {
700cdf0e10cSrcweir         sal_Int64 l = (sal_Int64)PyLong_AsLong (o);
701cdf0e10cSrcweir         if( l < 128 && l >= -128 )
702cdf0e10cSrcweir         {
703cdf0e10cSrcweir             sal_Int8 b = (sal_Int8 ) l;
704cdf0e10cSrcweir             a <<= b;
705cdf0e10cSrcweir         }
706cdf0e10cSrcweir         else if( l <= 0x7fff && l >= -0x8000 )
707cdf0e10cSrcweir         {
708cdf0e10cSrcweir             sal_Int16 s = (sal_Int16) l;
709cdf0e10cSrcweir             a <<= s;
710cdf0e10cSrcweir         }
711cdf0e10cSrcweir         else if( l <= SAL_CONST_INT64(0x7fffffff) &&
712cdf0e10cSrcweir                  l >= -SAL_CONST_INT64(0x80000000) )
713cdf0e10cSrcweir         {
714cdf0e10cSrcweir             sal_Int32 l32 = (sal_Int32) l;
715cdf0e10cSrcweir             a <<= l32;
716cdf0e10cSrcweir         }
717cdf0e10cSrcweir         else
718cdf0e10cSrcweir         {
719cdf0e10cSrcweir             a <<= l;
720cdf0e10cSrcweir         }
721cdf0e10cSrcweir     }
722cdf0e10cSrcweir     else if (PyFloat_Check (o))
723cdf0e10cSrcweir     {
724cdf0e10cSrcweir         double d = PyFloat_AsDouble (o);
725cdf0e10cSrcweir         a <<= d;
726cdf0e10cSrcweir     }
727564d9007SPedro Giffuni     else if (PyBytes_Check (o))
728cdf0e10cSrcweir 	a <<= pyString2ustring(o);
729cdf0e10cSrcweir     else if( PyUnicode_Check( o ) )
730cdf0e10cSrcweir 	a <<= pyString2ustring(o);
731cdf0e10cSrcweir     else if (PyTuple_Check (o))
732cdf0e10cSrcweir     {
733cdf0e10cSrcweir         Sequence<Any> s (PyTuple_Size (o));
734cdf0e10cSrcweir         for (int i = 0; i < PyTuple_Size (o); i++)
735cdf0e10cSrcweir         {
736cdf0e10cSrcweir             s[i] = pyObject2Any (PyTuple_GetItem (o, i), mode );
737cdf0e10cSrcweir         }
738cdf0e10cSrcweir         a <<= s;
739cdf0e10cSrcweir     }
740cdf0e10cSrcweir     else
741cdf0e10cSrcweir     {
742cdf0e10cSrcweir         Runtime runtime;
743cdf0e10cSrcweir         // should be removed, in case ByteSequence gets derived from String
744cdf0e10cSrcweir         if( PyObject_IsInstance( o, getByteSequenceClass( runtime ).get() ) )
745cdf0e10cSrcweir         {
746cdf0e10cSrcweir             PyRef str(PyObject_GetAttrString( o , const_cast< char * >("value") ),SAL_NO_ACQUIRE);
747cdf0e10cSrcweir             Sequence< sal_Int8 > seq;
748564d9007SPedro Giffuni             if( PyBytes_Check( str.get() ) )
749cdf0e10cSrcweir             {
750cdf0e10cSrcweir                 seq = Sequence<sal_Int8 > (
751564d9007SPedro Giffuni                     (sal_Int8*) PyBytes_AsString(str.get()), PyBytes_Size(str.get()));
752cdf0e10cSrcweir             }
753cdf0e10cSrcweir             a <<= seq;
754cdf0e10cSrcweir         }
755cdf0e10cSrcweir         else
756cdf0e10cSrcweir         if( PyObject_IsInstance( o, getTypeClass( runtime ).get() ) )
757cdf0e10cSrcweir         {
758cdf0e10cSrcweir             Type t = PyType2Type( o );
759cdf0e10cSrcweir             a <<= t;
760cdf0e10cSrcweir         }
761cdf0e10cSrcweir         else if( PyObject_IsInstance( o, getEnumClass( runtime ).get() ) )
762cdf0e10cSrcweir         {
763cdf0e10cSrcweir             a = PyEnum2Enum( o );
764cdf0e10cSrcweir         }
765cdf0e10cSrcweir         else if( isInstanceOfStructOrException( o ) )
766cdf0e10cSrcweir         {
767cdf0e10cSrcweir             PyRef struc(PyObject_GetAttrString( o , const_cast< char * >("value") ),SAL_NO_ACQUIRE);
768cdf0e10cSrcweir             PyUNO * obj = (PyUNO*)struc.get();
769cdf0e10cSrcweir             Reference< XMaterialHolder > holder( obj->members->xInvocation, UNO_QUERY );
770cdf0e10cSrcweir             if( holder.is( ) )
771cdf0e10cSrcweir                 a = holder->getMaterial();
772cdf0e10cSrcweir             else
773cdf0e10cSrcweir             {
774cdf0e10cSrcweir                 throw RuntimeException(
775cdf0e10cSrcweir                     USTR_ASCII( "struct or exception wrapper does not support XMaterialHolder" ),
776cdf0e10cSrcweir                     Reference< XInterface > () );
777cdf0e10cSrcweir             }
778cdf0e10cSrcweir         }
779cdf0e10cSrcweir         else if( PyObject_IsInstance( o, getPyUnoClass( runtime ).get() ) )
780cdf0e10cSrcweir         {
781cdf0e10cSrcweir             PyUNO* o_pi;
782cdf0e10cSrcweir             o_pi = (PyUNO*) o;
783cdf0e10cSrcweir             if (o_pi->members->wrappedObject.getValueTypeClass () ==
784cdf0e10cSrcweir                 com::sun::star::uno::TypeClass_STRUCT ||
785cdf0e10cSrcweir                 o_pi->members->wrappedObject.getValueTypeClass () ==
786cdf0e10cSrcweir                 com::sun::star::uno::TypeClass_EXCEPTION)
787cdf0e10cSrcweir             {
788cdf0e10cSrcweir                 Reference<XMaterialHolder> my_mh (o_pi->members->xInvocation, UNO_QUERY);
789cdf0e10cSrcweir 
790cdf0e10cSrcweir                 if (!my_mh.is ())
791cdf0e10cSrcweir                 {
792cdf0e10cSrcweir                     throw RuntimeException(
793cdf0e10cSrcweir                         USTR_ASCII( "struct wrapper does not support XMaterialHolder" ),
794cdf0e10cSrcweir                         Reference< XInterface > () );
795cdf0e10cSrcweir                 }
796cdf0e10cSrcweir                 else
797cdf0e10cSrcweir                     a = my_mh->getMaterial ();
798cdf0e10cSrcweir             }
799cdf0e10cSrcweir             else
800cdf0e10cSrcweir             {
801cdf0e10cSrcweir                 a = o_pi->members->wrappedObject;
802cdf0e10cSrcweir             }
803cdf0e10cSrcweir         }
804cdf0e10cSrcweir         else if( PyObject_IsInstance( o, getCharClass( runtime ).get() ) )
805cdf0e10cSrcweir         {
806cdf0e10cSrcweir             sal_Unicode c = PyChar2Unicode( o );
807cdf0e10cSrcweir             a.setValue( &c, getCharCppuType( ));
808cdf0e10cSrcweir         }
809cdf0e10cSrcweir         else if( PyObject_IsInstance( o, getAnyClass( runtime ).get() ) )
810cdf0e10cSrcweir         {
811cdf0e10cSrcweir             if( ACCEPT_UNO_ANY == mode )
812cdf0e10cSrcweir             {
813cdf0e10cSrcweir                 a = pyObject2Any( PyRef( PyObject_GetAttrString( o , const_cast< char * >("value") ), SAL_NO_ACQUIRE) );
814cdf0e10cSrcweir                 Type t;
815cdf0e10cSrcweir                 pyObject2Any( PyRef( PyObject_GetAttrString( o, const_cast< char * >("type") ), SAL_NO_ACQUIRE ) ) >>= t;
816cdf0e10cSrcweir 
817cdf0e10cSrcweir                 try
818cdf0e10cSrcweir                 {
819cdf0e10cSrcweir                     a = getImpl()->cargo->xTypeConverter->convertTo( a, t );
820cdf0e10cSrcweir                 }
821cdf0e10cSrcweir                 catch( com::sun::star::uno::Exception & e )
822cdf0e10cSrcweir                 {
823cdf0e10cSrcweir                     throw RuntimeException( e.Message, e.Context );
824cdf0e10cSrcweir                 }
825cdf0e10cSrcweir             }
826cdf0e10cSrcweir             else
827cdf0e10cSrcweir             {
828cdf0e10cSrcweir                 throw RuntimeException(
829cdf0e10cSrcweir                     OUString( RTL_CONSTASCII_USTRINGPARAM(
830cdf0e10cSrcweir                                   "uno.Any instance not accepted during method call, "
831cdf0e10cSrcweir                                   "use uno.invoke instead" ) ),
832cdf0e10cSrcweir                     Reference< XInterface > () );
833cdf0e10cSrcweir             }
834cdf0e10cSrcweir         }
835cdf0e10cSrcweir         else
836cdf0e10cSrcweir         {
837cdf0e10cSrcweir             Reference< XInterface > mappedObject;
838cdf0e10cSrcweir             Reference< XInvocation > adapterObject;
839cdf0e10cSrcweir 
840cdf0e10cSrcweir             // instance already mapped out to the world ?
841cdf0e10cSrcweir             PyRef2Adapter::iterator ii = impl->cargo->mappedObjects.find( PyRef( o ) );
842cdf0e10cSrcweir             if( ii != impl->cargo->mappedObjects.end() )
843cdf0e10cSrcweir             {
844cdf0e10cSrcweir                 adapterObject = ii->second;
845cdf0e10cSrcweir             }
846cdf0e10cSrcweir 
847cdf0e10cSrcweir             if( adapterObject.is() )
848cdf0e10cSrcweir             {
849cdf0e10cSrcweir                 // object got already bridged !
850cdf0e10cSrcweir                 Reference< com::sun::star::lang::XUnoTunnel > tunnel( adapterObject, UNO_QUERY );
851cdf0e10cSrcweir 
852cdf0e10cSrcweir                 Adapter *pAdapter = ( Adapter * )
853cdf0e10cSrcweir                     sal::static_int_cast< sal_IntPtr >(
854cdf0e10cSrcweir                         tunnel->getSomething(
855cdf0e10cSrcweir                             ::pyuno::Adapter::getUnoTunnelImplementationId() ) );
856cdf0e10cSrcweir 
857cdf0e10cSrcweir                 mappedObject = impl->cargo->xAdapterFactory->createAdapter(
858cdf0e10cSrcweir                     adapterObject, pAdapter->getWrappedTypes() );
859cdf0e10cSrcweir             }
860cdf0e10cSrcweir             else
861cdf0e10cSrcweir             {
862cdf0e10cSrcweir                 Sequence< Type > interfaces = invokeGetTypes( *this, o );
863cdf0e10cSrcweir                 if( interfaces.getLength() )
864cdf0e10cSrcweir                 {
865cdf0e10cSrcweir                     Adapter *pAdapter = new Adapter( o, interfaces );
866cdf0e10cSrcweir                     mappedObject =
867cdf0e10cSrcweir                         getImpl()->cargo->xAdapterFactory->createAdapter(
868cdf0e10cSrcweir                             pAdapter, interfaces );
869cdf0e10cSrcweir 
870cdf0e10cSrcweir                     // keep a list of exported objects to ensure object identity !
871cdf0e10cSrcweir                     impl->cargo->mappedObjects[ PyRef(o) ] =
872cdf0e10cSrcweir                         com::sun::star::uno::WeakReference< XInvocation > ( pAdapter );
873cdf0e10cSrcweir                 }
874cdf0e10cSrcweir             }
875cdf0e10cSrcweir             if( mappedObject.is() )
876cdf0e10cSrcweir             {
877cdf0e10cSrcweir                 a = com::sun::star::uno::makeAny( mappedObject );
878cdf0e10cSrcweir             }
879cdf0e10cSrcweir             else
880cdf0e10cSrcweir             {
881cdf0e10cSrcweir                 OUStringBuffer buf;
882cdf0e10cSrcweir                 buf.appendAscii( "Couldn't convert " );
883cdf0e10cSrcweir                 PyRef reprString( PyObject_Str( o ) , SAL_NO_ACQUIRE );
884564d9007SPedro Giffuni                 buf.appendAscii( PyBytes_AsString( reprString.get() ) );
885cdf0e10cSrcweir                 buf.appendAscii( " to a UNO type" );
886cdf0e10cSrcweir                 throw RuntimeException( buf.makeStringAndClear(), Reference< XInterface > () );
887cdf0e10cSrcweir             }
888cdf0e10cSrcweir         }
889cdf0e10cSrcweir     }
890cdf0e10cSrcweir     return a;
891cdf0e10cSrcweir }
892cdf0e10cSrcweir 
893cdf0e10cSrcweir Any Runtime::extractUnoException( const PyRef & excType, const PyRef &excValue, const PyRef &excTraceback) const
894cdf0e10cSrcweir {
895cdf0e10cSrcweir     PyRef str;
896cdf0e10cSrcweir     Any ret;
897cdf0e10cSrcweir     if( excTraceback.is() )
898cdf0e10cSrcweir     {
899cdf0e10cSrcweir         PyRef unoModule( impl ? impl->cargo->getUnoModule() : 0 );
900cdf0e10cSrcweir         if( unoModule.is() )
901cdf0e10cSrcweir         {
902cdf0e10cSrcweir             PyRef extractTraceback(
903cdf0e10cSrcweir                 PyDict_GetItemString(unoModule.get(),"_uno_extract_printable_stacktrace" ) );
904cdf0e10cSrcweir 
905cdf0e10cSrcweir             if( extractTraceback.is() )
906cdf0e10cSrcweir             {
907cdf0e10cSrcweir                 PyRef args( PyTuple_New( 1), SAL_NO_ACQUIRE );
908cdf0e10cSrcweir                 PyTuple_SetItem( args.get(), 0, excTraceback.getAcquired() );
909cdf0e10cSrcweir                 str = PyRef( PyObject_CallObject( extractTraceback.get(),args.get() ), SAL_NO_ACQUIRE);
910cdf0e10cSrcweir             }
911cdf0e10cSrcweir             else
912cdf0e10cSrcweir             {
913cdf0e10cSrcweir                 str = PyRef(
914564d9007SPedro Giffuni                     PyBytes_FromString( "Couldn't find uno._uno_extract_printable_stacktrace" ),
915cdf0e10cSrcweir                     SAL_NO_ACQUIRE );
916cdf0e10cSrcweir             }
917cdf0e10cSrcweir         }
918cdf0e10cSrcweir         else
919cdf0e10cSrcweir         {
920cdf0e10cSrcweir             str = PyRef(
921564d9007SPedro Giffuni                 PyBytes_FromString( "Couldn't find uno.py, no stacktrace available" ),
922cdf0e10cSrcweir                 SAL_NO_ACQUIRE );
923cdf0e10cSrcweir         }
924cdf0e10cSrcweir 
925cdf0e10cSrcweir     }
926cdf0e10cSrcweir     else
927cdf0e10cSrcweir     {
928cdf0e10cSrcweir         // it may occur, that no traceback is given (e.g. only native code below)
929564d9007SPedro Giffuni         str = PyRef( PyBytes_FromString( "no traceback available" ), SAL_NO_ACQUIRE);
930cdf0e10cSrcweir     }
931cdf0e10cSrcweir 
932cdf0e10cSrcweir     if( isInstanceOfStructOrException( excValue.get() ) )
933cdf0e10cSrcweir     {
934cdf0e10cSrcweir         ret = pyObject2Any( excValue );
935cdf0e10cSrcweir     }
936cdf0e10cSrcweir     else
937cdf0e10cSrcweir     {
938cdf0e10cSrcweir         OUStringBuffer buf;
939cdf0e10cSrcweir         PyRef typeName( PyObject_Str( excType.get() ), SAL_NO_ACQUIRE );
940cdf0e10cSrcweir         if( typeName.is() )
941cdf0e10cSrcweir         {
942564d9007SPedro Giffuni             buf.appendAscii( PyBytes_AsString( typeName.get() ) );
943cdf0e10cSrcweir         }
944cdf0e10cSrcweir         else
945cdf0e10cSrcweir         {
946cdf0e10cSrcweir             buf.appendAscii( "no typename available" );
947cdf0e10cSrcweir         }
948cdf0e10cSrcweir         buf.appendAscii( ": " );
949cdf0e10cSrcweir         PyRef valueRep( PyObject_Str( excValue.get() ), SAL_NO_ACQUIRE );
950cdf0e10cSrcweir         if( valueRep.is() )
951cdf0e10cSrcweir         {
952564d9007SPedro Giffuni             buf.appendAscii( PyBytes_AsString( valueRep.get()));
953cdf0e10cSrcweir         }
954cdf0e10cSrcweir         else
955cdf0e10cSrcweir         {
956cdf0e10cSrcweir             buf.appendAscii( "Couldn't convert exception value to a string" );
957cdf0e10cSrcweir         }
958cdf0e10cSrcweir         buf.appendAscii( ", traceback follows\n" );
959cdf0e10cSrcweir         if( str.is() )
960cdf0e10cSrcweir         {
961564d9007SPedro Giffuni             buf.appendAscii( PyBytes_AsString( str.get() ) );
962cdf0e10cSrcweir         }
963cdf0e10cSrcweir         else
964cdf0e10cSrcweir         {
965cdf0e10cSrcweir             buf.appendAscii( ", no traceback available\n" );
966cdf0e10cSrcweir         }
967cdf0e10cSrcweir         RuntimeException e;
968cdf0e10cSrcweir         e.Message = buf.makeStringAndClear();
969cdf0e10cSrcweir         ret = com::sun::star::uno::makeAny( e );
970cdf0e10cSrcweir     }
971cdf0e10cSrcweir     return ret;
972cdf0e10cSrcweir }
973cdf0e10cSrcweir 
974cdf0e10cSrcweir 
975cdf0e10cSrcweir static const char * g_NUMERICID = "pyuno.lcNumeric";
976cdf0e10cSrcweir static ::std::vector< rtl::OString > g_localeList;
977cdf0e10cSrcweir 
978cdf0e10cSrcweir static const char *ensureUnlimitedLifetime( const char *str )
979cdf0e10cSrcweir {
980cdf0e10cSrcweir     int size = g_localeList.size();
981cdf0e10cSrcweir     int i;
982cdf0e10cSrcweir     for( i = 0 ; i < size ; i ++ )
983cdf0e10cSrcweir     {
984cdf0e10cSrcweir         if( 0 == strcmp( g_localeList[i].getStr(), str ) )
985cdf0e10cSrcweir             break;
986cdf0e10cSrcweir     }
987cdf0e10cSrcweir     if( i == size )
988cdf0e10cSrcweir     {
989cdf0e10cSrcweir         g_localeList.push_back( str );
990cdf0e10cSrcweir     }
991cdf0e10cSrcweir     return g_localeList[i].getStr();
992cdf0e10cSrcweir }
993cdf0e10cSrcweir 
994cdf0e10cSrcweir 
995cdf0e10cSrcweir PyThreadAttach::PyThreadAttach( PyInterpreterState *interp)
996cdf0e10cSrcweir     throw ( com::sun::star::uno::RuntimeException )
997cdf0e10cSrcweir {
998cdf0e10cSrcweir     tstate = PyThreadState_New( interp );
999cdf0e10cSrcweir     if( !tstate  )
1000cdf0e10cSrcweir         throw RuntimeException(
1001cdf0e10cSrcweir             OUString(RTL_CONSTASCII_USTRINGPARAM( "Couldn't create a pythreadstate" ) ),
1002cdf0e10cSrcweir             Reference< XInterface > () );
1003cdf0e10cSrcweir     PyEval_AcquireThread( tstate);
1004cdf0e10cSrcweir     // set LC_NUMERIC to "C"
1005cdf0e10cSrcweir     const char * oldLocale =
1006cdf0e10cSrcweir         ensureUnlimitedLifetime( setlocale( LC_NUMERIC, 0 )  );
1007cdf0e10cSrcweir     setlocale( LC_NUMERIC, "C" );
1008cdf0e10cSrcweir     PyRef locale( // python requires C locale
1009cdf0e10cSrcweir         PyLong_FromVoidPtr( (void*)oldLocale ), SAL_NO_ACQUIRE);
1010cdf0e10cSrcweir     PyDict_SetItemString(
1011cdf0e10cSrcweir         PyThreadState_GetDict(), g_NUMERICID, locale.get() );
1012cdf0e10cSrcweir }
1013cdf0e10cSrcweir 
1014cdf0e10cSrcweir PyThreadAttach::~PyThreadAttach()
1015cdf0e10cSrcweir {
1016cdf0e10cSrcweir     PyObject *value =
1017cdf0e10cSrcweir         PyDict_GetItemString( PyThreadState_GetDict( ), g_NUMERICID );
1018cdf0e10cSrcweir     if( value )
1019cdf0e10cSrcweir         setlocale( LC_NUMERIC, (const char * ) PyLong_AsVoidPtr( value ) );
1020cdf0e10cSrcweir     PyThreadState_Clear( tstate );
1021cdf0e10cSrcweir     PyEval_ReleaseThread( tstate );
1022cdf0e10cSrcweir     PyThreadState_Delete( tstate );
1023cdf0e10cSrcweir 
1024cdf0e10cSrcweir }
1025cdf0e10cSrcweir 
1026cdf0e10cSrcweir PyThreadDetach::PyThreadDetach() throw ( com::sun::star::uno::RuntimeException )
1027cdf0e10cSrcweir {
1028cdf0e10cSrcweir     tstate = PyThreadState_Get();
1029cdf0e10cSrcweir     PyObject *value =
1030cdf0e10cSrcweir         PyDict_GetItemString( PyThreadState_GetDict( ), g_NUMERICID );
1031cdf0e10cSrcweir     if( value )
1032cdf0e10cSrcweir         setlocale( LC_NUMERIC, (const char * ) PyLong_AsVoidPtr( value ) );
1033cdf0e10cSrcweir     PyEval_ReleaseThread( tstate );
1034cdf0e10cSrcweir }
1035cdf0e10cSrcweir 
1036cdf0e10cSrcweir     /** Acquires the global interpreter lock again
1037cdf0e10cSrcweir 
1038cdf0e10cSrcweir     */
1039cdf0e10cSrcweir PyThreadDetach::~PyThreadDetach()
1040cdf0e10cSrcweir {
1041cdf0e10cSrcweir     PyEval_AcquireThread( tstate );
1042cdf0e10cSrcweir //     PyObject *value =
1043cdf0e10cSrcweir //         PyDict_GetItemString( PyThreadState_GetDict( ), g_NUMERICID );
1044cdf0e10cSrcweir 
1045cdf0e10cSrcweir     // python requires C LC_NUMERIC locale,
1046cdf0e10cSrcweir     // always set even when it is already "C"
1047cdf0e10cSrcweir     setlocale( LC_NUMERIC, "C" );
1048cdf0e10cSrcweir }
1049cdf0e10cSrcweir 
1050cdf0e10cSrcweir 
1051cdf0e10cSrcweir PyRef RuntimeCargo::getUnoModule()
1052cdf0e10cSrcweir {
1053cdf0e10cSrcweir     if( ! dictUnoModule.is() )
1054cdf0e10cSrcweir     {
1055cdf0e10cSrcweir         dictUnoModule = importUnoModule();
1056cdf0e10cSrcweir     }
1057cdf0e10cSrcweir     return dictUnoModule;
1058cdf0e10cSrcweir }
1059cdf0e10cSrcweir }
1060