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
1067c7d1c1SAndrew Rist  *
1167c7d1c1SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
1267c7d1c1SAndrew Rist  *
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.
1967c7d1c1SAndrew Rist  *
2067c7d1c1SAndrew Rist  *************************************************************/
2167c7d1c1SAndrew Rist 
2267c7d1c1SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include "pyuno_impl.hxx"
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include <osl/module.hxx>
27cdf0e10cSrcweir #include <osl/thread.h>
28cdf0e10cSrcweir #include <osl/file.hxx>
29cdf0e10cSrcweir 
30cdf0e10cSrcweir #include <typelib/typedescription.hxx>
31cdf0e10cSrcweir 
32cdf0e10cSrcweir #include <rtl/strbuf.hxx>
33cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
34cdf0e10cSrcweir #include <rtl/uuid.h>
35cdf0e10cSrcweir #include <rtl/bootstrap.hxx>
36cdf0e10cSrcweir 
37cdf0e10cSrcweir #include <uno/current_context.hxx>
38cdf0e10cSrcweir #include <cppuhelper/bootstrap.hxx>
39cdf0e10cSrcweir 
40cdf0e10cSrcweir #include <com/sun/star/reflection/XIdlReflection.hpp>
41cdf0e10cSrcweir #include <com/sun/star/reflection/XIdlClass.hpp>
42cdf0e10cSrcweir #include <com/sun/star/registry/InvalidRegistryException.hpp>
43cdf0e10cSrcweir 
44cdf0e10cSrcweir using osl::Module;
45cdf0e10cSrcweir 
46cdf0e10cSrcweir using rtl::OString;
47cdf0e10cSrcweir using rtl::OUString;
48cdf0e10cSrcweir using rtl::OUStringToOString;
49cdf0e10cSrcweir using rtl::OUStringBuffer;
50cdf0e10cSrcweir using rtl::OStringBuffer;
51cdf0e10cSrcweir 
52cdf0e10cSrcweir using com::sun::star::uno::Sequence;
53cdf0e10cSrcweir using com::sun::star::uno::Reference;
54cdf0e10cSrcweir using com::sun::star::uno::XInterface;
55cdf0e10cSrcweir using com::sun::star::uno::Any;
56cdf0e10cSrcweir using com::sun::star::uno::makeAny;
57cdf0e10cSrcweir using com::sun::star::uno::UNO_QUERY;
58cdf0e10cSrcweir using com::sun::star::uno::RuntimeException;
59cdf0e10cSrcweir using com::sun::star::uno::TypeDescription;
60cdf0e10cSrcweir using com::sun::star::uno::XComponentContext;
61cdf0e10cSrcweir using com::sun::star::container::NoSuchElementException;
62cdf0e10cSrcweir using com::sun::star::reflection::XIdlReflection;
63cdf0e10cSrcweir using com::sun::star::reflection::XIdlClass;
64cdf0e10cSrcweir using com::sun::star::script::XInvocation2;
65cdf0e10cSrcweir 
66cdf0e10cSrcweir using namespace pyuno;
67cdf0e10cSrcweir 
68cdf0e10cSrcweir namespace {
69cdf0e10cSrcweir 
70cdf0e10cSrcweir /**
71cdf0e10cSrcweir    @ index of the next to be used member in the initializer list !
72cdf0e10cSrcweir  */
73cdf0e10cSrcweir sal_Int32 fillStructWithInitializer(
74cdf0e10cSrcweir     const Reference< XInvocation2 > &inv,
75cdf0e10cSrcweir     typelib_CompoundTypeDescription *pCompType,
76cdf0e10cSrcweir     PyObject *initializer,
77cdf0e10cSrcweir     const Runtime &runtime) throw ( RuntimeException )
78cdf0e10cSrcweir {
79cdf0e10cSrcweir     sal_Int32 nIndex = 0;
80cdf0e10cSrcweir     if( pCompType->pBaseTypeDescription )
81cdf0e10cSrcweir         nIndex = fillStructWithInitializer(
82cdf0e10cSrcweir             inv, pCompType->pBaseTypeDescription, initializer, runtime );
83cdf0e10cSrcweir 
84cdf0e10cSrcweir     sal_Int32 nTupleSize =  PyTuple_Size(initializer);
85cdf0e10cSrcweir     int i;
86cdf0e10cSrcweir     for( i = 0 ; i < pCompType->nMembers ; i ++ )
87cdf0e10cSrcweir     {
88cdf0e10cSrcweir         if( i + nIndex >= nTupleSize )
89cdf0e10cSrcweir         {
90cdf0e10cSrcweir             OUStringBuffer buf;
91cdf0e10cSrcweir             buf.appendAscii( "pyuno._createUnoStructHelper: too few elements in the initializer tuple,");
92cdf0e10cSrcweir             buf.appendAscii( "expected at least " ).append( nIndex + pCompType->nMembers );
93cdf0e10cSrcweir             buf.appendAscii( ", got " ).append( nTupleSize );
94cdf0e10cSrcweir             throw RuntimeException(buf.makeStringAndClear(), Reference< XInterface > ());
95cdf0e10cSrcweir         }
96cdf0e10cSrcweir         PyObject *element = PyTuple_GetItem( initializer, i + nIndex );
97cdf0e10cSrcweir         Any a = runtime.pyObject2Any( element, ACCEPT_UNO_ANY );
98cdf0e10cSrcweir         inv->setValue( pCompType->ppMemberNames[i], a );
99cdf0e10cSrcweir     }
100cdf0e10cSrcweir     return i+nIndex;
101cdf0e10cSrcweir }
102cdf0e10cSrcweir 
103cdf0e10cSrcweir OUString getLibDir()
104cdf0e10cSrcweir {
105cdf0e10cSrcweir     static OUString *pLibDir;
106cdf0e10cSrcweir     if( !pLibDir )
107cdf0e10cSrcweir     {
108cdf0e10cSrcweir         osl::MutexGuard guard( osl::Mutex::getGlobalMutex() );
109cdf0e10cSrcweir         if( ! pLibDir )
110cdf0e10cSrcweir         {
111cdf0e10cSrcweir             static OUString libDir;
112cdf0e10cSrcweir 
113cdf0e10cSrcweir             // workarounds the $(ORIGIN) until it is available
114cdf0e10cSrcweir             if( Module::getUrlFromAddress(
115cdf0e10cSrcweir                     reinterpret_cast< oslGenericFunction >(getLibDir), libDir ) )
116cdf0e10cSrcweir             {
117cdf0e10cSrcweir                 libDir = OUString( libDir.getStr(), libDir.lastIndexOf('/' ) );
118cdf0e10cSrcweir                 OUString name ( RTL_CONSTASCII_USTRINGPARAM( "PYUNOLIBDIR" ) );
119cdf0e10cSrcweir                 rtl_bootstrap_set( name.pData, libDir.pData );
120cdf0e10cSrcweir             }
121cdf0e10cSrcweir             pLibDir = &libDir;
122cdf0e10cSrcweir         }
123cdf0e10cSrcweir     }
124cdf0e10cSrcweir     return *pLibDir;
125cdf0e10cSrcweir }
126cdf0e10cSrcweir 
127cdf0e10cSrcweir void raisePySystemException( const char * exceptionType, const OUString & message )
128cdf0e10cSrcweir {
129cdf0e10cSrcweir     OStringBuffer buf;
130cdf0e10cSrcweir     buf.append( "Error during bootstrapping uno (");
131cdf0e10cSrcweir     buf.append( exceptionType );
132cdf0e10cSrcweir     buf.append( "):" );
133cdf0e10cSrcweir     buf.append( OUStringToOString( message, osl_getThreadTextEncoding() ) );
134cdf0e10cSrcweir     PyErr_SetString( PyExc_SystemError, buf.makeStringAndClear().getStr() );
135cdf0e10cSrcweir }
136cdf0e10cSrcweir 
137cdf0e10cSrcweir extern "C" {
138cdf0e10cSrcweir 
139cdf0e10cSrcweir static PyObject* getComponentContext (PyObject*, PyObject*)
140cdf0e10cSrcweir {
141cdf0e10cSrcweir     PyRef ret;
142cdf0e10cSrcweir     try
143cdf0e10cSrcweir     {
144cdf0e10cSrcweir         Reference<XComponentContext> ctx;
145cdf0e10cSrcweir 
146cdf0e10cSrcweir         // getLibDir() must be called in order to set bootstrap variables correctly !
147cdf0e10cSrcweir         OUString path( getLibDir());
148cdf0e10cSrcweir         if( Runtime::isInitialized() )
149cdf0e10cSrcweir         {
150cdf0e10cSrcweir             Runtime runtime;
151cdf0e10cSrcweir             ctx = runtime.getImpl()->cargo->xContext;
152cdf0e10cSrcweir         }
153cdf0e10cSrcweir         else
154cdf0e10cSrcweir         {
155cdf0e10cSrcweir             OUString iniFile;
156cdf0e10cSrcweir             if( !path.getLength() )
157cdf0e10cSrcweir             {
158cdf0e10cSrcweir                 PyErr_SetString(
159cdf0e10cSrcweir                     PyExc_RuntimeError, "osl_getUrlFromAddress fails, that's why I cannot find ini "
160cdf0e10cSrcweir                     "file for bootstrapping python uno bridge\n" );
161cdf0e10cSrcweir                 return NULL;
162cdf0e10cSrcweir             }
163cdf0e10cSrcweir 
164cdf0e10cSrcweir             OUStringBuffer iniFileName;
165cdf0e10cSrcweir             iniFileName.append( path );
166cdf0e10cSrcweir             iniFileName.appendAscii( "/" );
167cdf0e10cSrcweir             iniFileName.appendAscii( SAL_CONFIGFILE( "pyuno" ) );
168cdf0e10cSrcweir             iniFile = iniFileName.makeStringAndClear();
169cdf0e10cSrcweir             osl::DirectoryItem item;
170cdf0e10cSrcweir             if( osl::DirectoryItem::get( iniFile, item ) == item.E_None )
171cdf0e10cSrcweir             {
172cdf0e10cSrcweir                 // in case pyuno.ini exists, use this file for bootstrapping
173cdf0e10cSrcweir                 PyThreadDetach antiguard;
174cdf0e10cSrcweir                 ctx = cppu::defaultBootstrap_InitialComponentContext (iniFile);
175cdf0e10cSrcweir             }
176cdf0e10cSrcweir             else
177cdf0e10cSrcweir             {
178cdf0e10cSrcweir                 // defaulting to the standard bootstrapping
179cdf0e10cSrcweir                 PyThreadDetach antiguard;
180cdf0e10cSrcweir                 ctx = cppu::defaultBootstrap_InitialComponentContext ();
181cdf0e10cSrcweir             }
182cdf0e10cSrcweir 
183cdf0e10cSrcweir         }
184cdf0e10cSrcweir 
185cdf0e10cSrcweir         if( ! Runtime::isInitialized() )
186cdf0e10cSrcweir         {
187cdf0e10cSrcweir             Runtime::initialize( ctx );
188cdf0e10cSrcweir         }
189cdf0e10cSrcweir         Runtime runtime;
190cdf0e10cSrcweir         ret = runtime.any2PyObject( makeAny( ctx ) );
191cdf0e10cSrcweir     }
192cdf0e10cSrcweir     catch (com::sun::star::registry::InvalidRegistryException &e)
193cdf0e10cSrcweir     {
194cdf0e10cSrcweir         // can't use raisePyExceptionWithAny() here, because the function
195cdf0e10cSrcweir         // does any conversions, which will not work with a
196cdf0e10cSrcweir         // wrongly bootstrapped pyuno!
197cdf0e10cSrcweir         raisePySystemException( "InvalidRegistryException", e.Message );
198cdf0e10cSrcweir     }
199cdf0e10cSrcweir     catch( com::sun::star::lang::IllegalArgumentException & e)
200cdf0e10cSrcweir     {
201cdf0e10cSrcweir         raisePySystemException( "IllegalArgumentException", e.Message );
202cdf0e10cSrcweir     }
203cdf0e10cSrcweir     catch( com::sun::star::script::CannotConvertException & e)
204cdf0e10cSrcweir     {
205cdf0e10cSrcweir         raisePySystemException( "CannotConvertException", e.Message );
206cdf0e10cSrcweir     }
207cdf0e10cSrcweir     catch (com::sun::star::uno::RuntimeException & e)
208cdf0e10cSrcweir     {
209cdf0e10cSrcweir         raisePySystemException( "RuntimeException", e.Message );
210cdf0e10cSrcweir     }
211cdf0e10cSrcweir     catch (com::sun::star::uno::Exception & e)
212cdf0e10cSrcweir     {
213cdf0e10cSrcweir         raisePySystemException( "uno::Exception", e.Message );
214cdf0e10cSrcweir     }
215cdf0e10cSrcweir     return ret.getAcquired();
216cdf0e10cSrcweir }
217cdf0e10cSrcweir 
218cdf0e10cSrcweir PyObject * extractOneStringArg( PyObject *args, char const *funcName )
219cdf0e10cSrcweir {
220cdf0e10cSrcweir     if( !PyTuple_Check( args ) || PyTuple_Size( args) != 1 )
221cdf0e10cSrcweir     {
222cdf0e10cSrcweir         OStringBuffer buf;
223cdf0e10cSrcweir         buf.append( funcName ).append( ": expecting one string argument" );
224cdf0e10cSrcweir         PyErr_SetString( PyExc_RuntimeError, buf.getStr() );
225cdf0e10cSrcweir         return NULL;
226cdf0e10cSrcweir     }
227cdf0e10cSrcweir     PyObject *obj = PyTuple_GetItem( args, 0 );
228*77dc4149SPedro Giffuni #if PY_MAJOR_VERSION >= 3
229*77dc4149SPedro Giffuni     if( ! PyUnicode_Check(obj) )
230*77dc4149SPedro Giffuni #else
231564d9007SPedro Giffuni     if( !PyBytes_Check( obj ) && ! PyUnicode_Check(obj))
232*77dc4149SPedro Giffuni #endif
233cdf0e10cSrcweir     {
234cdf0e10cSrcweir         OStringBuffer buf;
235cdf0e10cSrcweir         buf.append( funcName ).append( ": expecting one string argument" );
236cdf0e10cSrcweir         PyErr_SetString( PyExc_TypeError, buf.getStr());
237cdf0e10cSrcweir         return NULL;
238cdf0e10cSrcweir     }
239cdf0e10cSrcweir     return obj;
240cdf0e10cSrcweir }
241cdf0e10cSrcweir 
242cdf0e10cSrcweir static PyObject *createUnoStructHelper(PyObject *, PyObject* args )
243cdf0e10cSrcweir {
244cdf0e10cSrcweir     Any IdlStruct;
245cdf0e10cSrcweir     PyRef ret;
246cdf0e10cSrcweir 
247cdf0e10cSrcweir     try
248cdf0e10cSrcweir     {
249cdf0e10cSrcweir         Runtime runtime;
250cdf0e10cSrcweir         if( PyTuple_Size( args ) == 2 )
251cdf0e10cSrcweir         {
252cdf0e10cSrcweir             PyObject *structName = PyTuple_GetItem( args,0 );
253cdf0e10cSrcweir             PyObject *initializer = PyTuple_GetItem( args ,1 );
254cdf0e10cSrcweir 
255*77dc4149SPedro Giffuni             if( PYSTR_CHECK( structName ) )
256cdf0e10cSrcweir             {
257cdf0e10cSrcweir                 if( PyTuple_Check( initializer ) )
258cdf0e10cSrcweir                 {
259*77dc4149SPedro Giffuni                     OUString typeName( pyString2ustring( structName ) );
260cdf0e10cSrcweir                     RuntimeCargo *c = runtime.getImpl()->cargo;
261cdf0e10cSrcweir                     Reference<XIdlClass> idl_class ( c->xCoreReflection->forName (typeName),UNO_QUERY);
262cdf0e10cSrcweir                     if (idl_class.is ())
263cdf0e10cSrcweir                     {
264cdf0e10cSrcweir                         idl_class->createObject (IdlStruct);
265cdf0e10cSrcweir                         PyUNO *me = (PyUNO*)PyUNO_new_UNCHECKED( IdlStruct, c->xInvocation );
266cdf0e10cSrcweir                         PyRef returnCandidate( (PyObject*)me, SAL_NO_ACQUIRE );
267cdf0e10cSrcweir                         if( PyTuple_Size( initializer ) > 0 )
268cdf0e10cSrcweir                         {
269cdf0e10cSrcweir                             TypeDescription desc( typeName );
270cdf0e10cSrcweir                             OSL_ASSERT( desc.is() ); // could already instantiate an XInvocation2 !
271cdf0e10cSrcweir 
272cdf0e10cSrcweir                             typelib_CompoundTypeDescription *pCompType =
273cdf0e10cSrcweir                                 ( typelib_CompoundTypeDescription * ) desc.get();
274cdf0e10cSrcweir                             sal_Int32 n = fillStructWithInitializer(
275cdf0e10cSrcweir                                 me->members->xInvocation, pCompType, initializer, runtime );
276cdf0e10cSrcweir                             if( n != PyTuple_Size(initializer) )
277cdf0e10cSrcweir                             {
278cdf0e10cSrcweir                                 OUStringBuffer buf;
279cdf0e10cSrcweir                                 buf.appendAscii( "pyuno._createUnoStructHelper: wrong number of ");
280cdf0e10cSrcweir                                 buf.appendAscii( "elements in the initializer list, expected " );
281cdf0e10cSrcweir                                 buf.append( n );
282cdf0e10cSrcweir                                 buf.appendAscii( ", got " );
283cdf0e10cSrcweir                                 buf.append( (sal_Int32) PyTuple_Size(initializer) );
284cdf0e10cSrcweir                                 throw RuntimeException(
285cdf0e10cSrcweir                                     buf.makeStringAndClear(), Reference< XInterface > ());
286cdf0e10cSrcweir                             }
287cdf0e10cSrcweir                         }
288cdf0e10cSrcweir                         ret = returnCandidate;
289cdf0e10cSrcweir                     }
290cdf0e10cSrcweir                     else
291cdf0e10cSrcweir                     {
292cdf0e10cSrcweir                         OStringBuffer buf;
293cdf0e10cSrcweir                         buf.append( "UNO struct " );
294*77dc4149SPedro Giffuni                         buf.append( OUStringToOString( typeName, RTL_TEXTENCODING_ASCII_US ) );
295cdf0e10cSrcweir                         buf.append( " is unkown" );
296cdf0e10cSrcweir                         PyErr_SetString (PyExc_RuntimeError, buf.getStr());
297cdf0e10cSrcweir                     }
298cdf0e10cSrcweir                 }
299cdf0e10cSrcweir                 else
300cdf0e10cSrcweir                 {
301cdf0e10cSrcweir                     PyErr_SetString(
302cdf0e10cSrcweir                         PyExc_RuntimeError,
303cdf0e10cSrcweir                         "pyuno._createUnoStructHelper: 2nd argument (initializer sequence) is no tuple" );
304cdf0e10cSrcweir                 }
305cdf0e10cSrcweir             }
306cdf0e10cSrcweir             else
307cdf0e10cSrcweir             {
308cdf0e10cSrcweir                 PyErr_SetString (PyExc_AttributeError, "createUnoStruct: first argument wasn't a string");
309cdf0e10cSrcweir             }
310cdf0e10cSrcweir         }
311cdf0e10cSrcweir         else
312cdf0e10cSrcweir         {
313cdf0e10cSrcweir             PyErr_SetString (PyExc_AttributeError, "1 Arguments: Structure Name");
314cdf0e10cSrcweir         }
315cdf0e10cSrcweir     }
316cdf0e10cSrcweir     catch( com::sun::star::uno::RuntimeException & e )
317cdf0e10cSrcweir     {
318cdf0e10cSrcweir         raisePyExceptionWithAny( makeAny( e ) );
319cdf0e10cSrcweir     }
320cdf0e10cSrcweir     catch( com::sun::star::script::CannotConvertException & e )
321cdf0e10cSrcweir     {
322cdf0e10cSrcweir         raisePyExceptionWithAny( makeAny( e ) );
323cdf0e10cSrcweir     }
324cdf0e10cSrcweir     catch( com::sun::star::uno::Exception & e )
325cdf0e10cSrcweir     {
326cdf0e10cSrcweir         raisePyExceptionWithAny( makeAny( e ) );
327cdf0e10cSrcweir     }
328cdf0e10cSrcweir     return ret.getAcquired();
329cdf0e10cSrcweir }
330cdf0e10cSrcweir 
331cdf0e10cSrcweir static PyObject *getTypeByName( PyObject *, PyObject *args )
332cdf0e10cSrcweir {
333cdf0e10cSrcweir     PyObject * ret = NULL;
334cdf0e10cSrcweir 
335cdf0e10cSrcweir     try
336cdf0e10cSrcweir     {
337cdf0e10cSrcweir         char *name;
338cdf0e10cSrcweir 
339cdf0e10cSrcweir         if (PyArg_ParseTuple (args, const_cast< char * >("s"), &name))
340cdf0e10cSrcweir         {
341cdf0e10cSrcweir             OUString typeName ( OUString::createFromAscii( name ) );
342cdf0e10cSrcweir             TypeDescription typeDesc( typeName );
343cdf0e10cSrcweir             if( typeDesc.is() )
344cdf0e10cSrcweir             {
345cdf0e10cSrcweir                 Runtime runtime;
346cdf0e10cSrcweir                 ret = PyUNO_Type_new(
347cdf0e10cSrcweir                     name, (com::sun::star::uno::TypeClass)typeDesc.get()->eTypeClass, runtime );
348cdf0e10cSrcweir             }
349cdf0e10cSrcweir             else
350cdf0e10cSrcweir             {
351cdf0e10cSrcweir                 OStringBuffer buf;
352cdf0e10cSrcweir                 buf.append( "Type " ).append(name).append( " is unknown" );
353cdf0e10cSrcweir                 PyErr_SetString( PyExc_RuntimeError, buf.getStr() );
354cdf0e10cSrcweir             }
355cdf0e10cSrcweir         }
356cdf0e10cSrcweir     }
357cdf0e10cSrcweir     catch ( RuntimeException & e )
358cdf0e10cSrcweir     {
359cdf0e10cSrcweir         raisePyExceptionWithAny( makeAny( e ) );
360cdf0e10cSrcweir     }
361cdf0e10cSrcweir     return ret;
362cdf0e10cSrcweir }
363cdf0e10cSrcweir 
364cdf0e10cSrcweir static PyObject *getConstantByName( PyObject *, PyObject *args )
365cdf0e10cSrcweir {
366cdf0e10cSrcweir     PyObject *ret = 0;
367cdf0e10cSrcweir     try
368cdf0e10cSrcweir     {
369cdf0e10cSrcweir         char *name;
370cdf0e10cSrcweir 
371cdf0e10cSrcweir         if (PyArg_ParseTuple (args, const_cast< char * >("s"), &name))
372cdf0e10cSrcweir         {
373cdf0e10cSrcweir             OUString typeName ( OUString::createFromAscii( name ) );
374cdf0e10cSrcweir             Runtime runtime;
375cdf0e10cSrcweir             Any a = runtime.getImpl()->cargo->xTdMgr->getByHierarchicalName(typeName);
376cdf0e10cSrcweir             if( a.getValueType().getTypeClass() ==
377cdf0e10cSrcweir                 com::sun::star::uno::TypeClass_INTERFACE )
378cdf0e10cSrcweir             {
379cdf0e10cSrcweir                 // a idl constant cannot be an instance of an uno-object, thus
380cdf0e10cSrcweir                 // this cannot be a constant
381cdf0e10cSrcweir                 OUStringBuffer buf;
382cdf0e10cSrcweir                 buf.appendAscii( "pyuno.getConstantByName: " ).append( typeName );
383cdf0e10cSrcweir                 buf.appendAscii( "is not a constant" );
384cdf0e10cSrcweir                 throw RuntimeException(buf.makeStringAndClear(), Reference< XInterface > () );
385cdf0e10cSrcweir             }
386cdf0e10cSrcweir             PyRef constant = runtime.any2PyObject( a );
387cdf0e10cSrcweir             ret = constant.getAcquired();
388cdf0e10cSrcweir         }
389cdf0e10cSrcweir     }
390cdf0e10cSrcweir     catch( NoSuchElementException & e )
391cdf0e10cSrcweir     {
392cdf0e10cSrcweir         // to the python programmer, this is a runtime exception,
393cdf0e10cSrcweir         // do not support tweakings with the type system
394cdf0e10cSrcweir         RuntimeException runExc( e.Message, Reference< XInterface > () );
395cdf0e10cSrcweir         raisePyExceptionWithAny( makeAny( runExc ) );
396cdf0e10cSrcweir     }
397cdf0e10cSrcweir     catch( com::sun::star::script::CannotConvertException & e)
398cdf0e10cSrcweir     {
399cdf0e10cSrcweir         raisePyExceptionWithAny( makeAny( e ) );
400cdf0e10cSrcweir     }
401cdf0e10cSrcweir     catch( com::sun::star::lang::IllegalArgumentException & e)
402cdf0e10cSrcweir     {
403cdf0e10cSrcweir         raisePyExceptionWithAny( makeAny( e ) );
404cdf0e10cSrcweir     }
405cdf0e10cSrcweir     catch( RuntimeException & e )
406cdf0e10cSrcweir     {
407cdf0e10cSrcweir         raisePyExceptionWithAny( makeAny(e) );
408cdf0e10cSrcweir     }
409cdf0e10cSrcweir     return ret;
410cdf0e10cSrcweir }
411cdf0e10cSrcweir 
412cdf0e10cSrcweir static PyObject *checkType( PyObject *, PyObject *args )
413cdf0e10cSrcweir {
414cdf0e10cSrcweir     if( !PyTuple_Check( args ) || PyTuple_Size( args) != 1 )
415cdf0e10cSrcweir     {
416cdf0e10cSrcweir         OStringBuffer buf;
417cdf0e10cSrcweir         buf.append( "pyuno.checkType : expecting one uno.Type argument" );
418cdf0e10cSrcweir         PyErr_SetString( PyExc_RuntimeError, buf.getStr() );
419cdf0e10cSrcweir         return NULL;
420cdf0e10cSrcweir     }
421cdf0e10cSrcweir     PyObject *obj = PyTuple_GetItem( args, 0 );
422cdf0e10cSrcweir 
423cdf0e10cSrcweir     try
424cdf0e10cSrcweir     {
425cdf0e10cSrcweir         PyType2Type( obj );
426cdf0e10cSrcweir     }
427cdf0e10cSrcweir     catch( RuntimeException & e)
428cdf0e10cSrcweir     {
429cdf0e10cSrcweir         raisePyExceptionWithAny( makeAny( e ) );
430cdf0e10cSrcweir         return NULL;
431cdf0e10cSrcweir     }
432cdf0e10cSrcweir     Py_INCREF( Py_None );
433cdf0e10cSrcweir     return Py_None;
434cdf0e10cSrcweir }
435cdf0e10cSrcweir 
436cdf0e10cSrcweir static PyObject *checkEnum( PyObject *, PyObject *args )
437cdf0e10cSrcweir {
438cdf0e10cSrcweir     if( !PyTuple_Check( args ) || PyTuple_Size( args) != 1 )
439cdf0e10cSrcweir     {
440cdf0e10cSrcweir         OStringBuffer buf;
441cdf0e10cSrcweir         buf.append( "pyuno.checkType : expecting one uno.Type argument" );
442cdf0e10cSrcweir         PyErr_SetString( PyExc_RuntimeError, buf.getStr() );
443cdf0e10cSrcweir         return NULL;
444cdf0e10cSrcweir     }
445cdf0e10cSrcweir     PyObject *obj = PyTuple_GetItem( args, 0 );
446cdf0e10cSrcweir 
447cdf0e10cSrcweir     try
448cdf0e10cSrcweir     {
449cdf0e10cSrcweir         PyEnum2Enum( obj );
450cdf0e10cSrcweir     }
451cdf0e10cSrcweir     catch( RuntimeException & e)
452cdf0e10cSrcweir     {
453cdf0e10cSrcweir         raisePyExceptionWithAny( makeAny( e) );
454cdf0e10cSrcweir         return NULL;
455cdf0e10cSrcweir     }
456cdf0e10cSrcweir     Py_INCREF( Py_None );
457cdf0e10cSrcweir     return Py_None;
458cdf0e10cSrcweir }
459cdf0e10cSrcweir 
460cdf0e10cSrcweir static PyObject *getClass( PyObject *, PyObject *args )
461cdf0e10cSrcweir {
462cdf0e10cSrcweir     PyObject *obj = extractOneStringArg( args, "pyuno.getClass");
463cdf0e10cSrcweir     if( ! obj )
464cdf0e10cSrcweir         return NULL;
465cdf0e10cSrcweir 
466cdf0e10cSrcweir     try
467cdf0e10cSrcweir     {
468cdf0e10cSrcweir         Runtime runtime;
469*77dc4149SPedro Giffuni         PyRef ret = getClass( pyString2ustring(obj), runtime );
470cdf0e10cSrcweir         Py_XINCREF( ret.get() );
471cdf0e10cSrcweir         return ret.get();
472cdf0e10cSrcweir     }
473cdf0e10cSrcweir     catch( RuntimeException & e)
474cdf0e10cSrcweir     {
475cdf0e10cSrcweir         // NOOPT !!!
476cdf0e10cSrcweir         // gcc 3.2.3 crashes here in the regcomp test scenario
477cdf0e10cSrcweir         // only since migration to python 2.3.4 ???? strange thing
478cdf0e10cSrcweir         // optimization switched off for this module !
479cdf0e10cSrcweir         raisePyExceptionWithAny( makeAny(e) );
480cdf0e10cSrcweir     }
481cdf0e10cSrcweir     return NULL;
482cdf0e10cSrcweir }
483cdf0e10cSrcweir 
484cdf0e10cSrcweir static PyObject *isInterface( PyObject *, PyObject *args )
485cdf0e10cSrcweir {
486cdf0e10cSrcweir 
487cdf0e10cSrcweir     if( PyTuple_Check( args ) && PyTuple_Size( args ) == 1 )
488cdf0e10cSrcweir     {
489cdf0e10cSrcweir         PyObject *obj = PyTuple_GetItem( args, 0 );
490cdf0e10cSrcweir         Runtime r;
4915c3821d8SPedro Giffuni #if PY_MAJOR_VERSION >= 3
4925c3821d8SPedro Giffuni         return PyLong_FromLong( isInterfaceClass( r, obj ) );
4935c3821d8SPedro Giffuni #else
494cdf0e10cSrcweir         return PyInt_FromLong( isInterfaceClass( r, obj ) );
4955c3821d8SPedro Giffuni 
4965c3821d8SPedro Giffuni #endif
497cdf0e10cSrcweir     }
4985c3821d8SPedro Giffuni #if PY_MAJOR_VERSION >= 3
4995c3821d8SPedro Giffuni     return PyLong_FromLong( 0 );
5005c3821d8SPedro Giffuni #else
501cdf0e10cSrcweir     return PyInt_FromLong( 0 );
5025c3821d8SPedro Giffuni #endif
503cdf0e10cSrcweir }
504cdf0e10cSrcweir 
505cdf0e10cSrcweir static PyObject * generateUuid( PyObject *, PyObject * )
506cdf0e10cSrcweir {
507cdf0e10cSrcweir     Sequence< sal_Int8 > seq( 16 );
508cdf0e10cSrcweir     rtl_createUuid( (sal_uInt8*)seq.getArray() , 0 , sal_False );
509cdf0e10cSrcweir     PyRef ret;
510cdf0e10cSrcweir     try
511cdf0e10cSrcweir     {
512cdf0e10cSrcweir         Runtime runtime;
513cdf0e10cSrcweir         ret = runtime.any2PyObject( makeAny( seq ) );
514cdf0e10cSrcweir     }
515cdf0e10cSrcweir     catch( RuntimeException & e )
516cdf0e10cSrcweir     {
517cdf0e10cSrcweir         raisePyExceptionWithAny( makeAny(e) );
518cdf0e10cSrcweir     }
519cdf0e10cSrcweir     return ret.getAcquired();
520cdf0e10cSrcweir }
521cdf0e10cSrcweir 
522cdf0e10cSrcweir static PyObject *systemPathToFileUrl( PyObject *, PyObject * args )
523cdf0e10cSrcweir {
524cdf0e10cSrcweir     PyObject *obj = extractOneStringArg( args, "pyuno.systemPathToFileUrl" );
525cdf0e10cSrcweir     if( ! obj )
526cdf0e10cSrcweir         return NULL;
527cdf0e10cSrcweir 
528cdf0e10cSrcweir     OUString sysPath = pyString2ustring( obj );
529cdf0e10cSrcweir     OUString url;
530cdf0e10cSrcweir     osl::FileBase::RC e = osl::FileBase::getFileURLFromSystemPath( sysPath, url );
531cdf0e10cSrcweir 
532cdf0e10cSrcweir     if( e != osl::FileBase::E_None )
533cdf0e10cSrcweir     {
534cdf0e10cSrcweir         OUStringBuffer buf;
535cdf0e10cSrcweir         buf.appendAscii( "Couldn't convert " );
536cdf0e10cSrcweir         buf.append( sysPath );
537cdf0e10cSrcweir         buf.appendAscii( " to a file url for reason (" );
538cdf0e10cSrcweir         buf.append( (sal_Int32) e );
539cdf0e10cSrcweir         buf.appendAscii( ")" );
540cdf0e10cSrcweir         raisePyExceptionWithAny(
541cdf0e10cSrcweir             makeAny( RuntimeException( buf.makeStringAndClear(), Reference< XInterface > () )));
542cdf0e10cSrcweir         return NULL;
543cdf0e10cSrcweir     }
544cdf0e10cSrcweir     return ustring2PyUnicode( url ).getAcquired();
545cdf0e10cSrcweir }
546cdf0e10cSrcweir 
547cdf0e10cSrcweir static PyObject * fileUrlToSystemPath( PyObject *, PyObject * args )
548cdf0e10cSrcweir {
549cdf0e10cSrcweir     PyObject *obj = extractOneStringArg( args, "pyuno.fileUrlToSystemPath" );
550cdf0e10cSrcweir     if( ! obj )
551cdf0e10cSrcweir         return NULL;
552cdf0e10cSrcweir 
553cdf0e10cSrcweir     OUString url = pyString2ustring( obj );
554cdf0e10cSrcweir     OUString sysPath;
555cdf0e10cSrcweir     osl::FileBase::RC e = osl::FileBase::getSystemPathFromFileURL( url, sysPath );
556cdf0e10cSrcweir 
557cdf0e10cSrcweir     if( e != osl::FileBase::E_None )
558cdf0e10cSrcweir     {
559cdf0e10cSrcweir         OUStringBuffer buf;
560cdf0e10cSrcweir         buf.appendAscii( "Couldn't convert file url " );
561cdf0e10cSrcweir         buf.append( sysPath );
562cdf0e10cSrcweir         buf.appendAscii( " to a system path for reason (" );
563cdf0e10cSrcweir         buf.append( (sal_Int32) e );
564cdf0e10cSrcweir         buf.appendAscii( ")" );
565cdf0e10cSrcweir         raisePyExceptionWithAny(
566cdf0e10cSrcweir             makeAny( RuntimeException( buf.makeStringAndClear(), Reference< XInterface > () )));
567cdf0e10cSrcweir         return NULL;
568cdf0e10cSrcweir     }
569cdf0e10cSrcweir     return ustring2PyUnicode( sysPath ).getAcquired();
570cdf0e10cSrcweir }
571cdf0e10cSrcweir 
572cdf0e10cSrcweir static PyObject * absolutize( PyObject *, PyObject * args )
573cdf0e10cSrcweir {
574cdf0e10cSrcweir     if( PyTuple_Check( args ) && PyTuple_Size( args ) == 2 )
575cdf0e10cSrcweir     {
576cdf0e10cSrcweir         OUString ouPath = pyString2ustring( PyTuple_GetItem( args , 0 ) );
577cdf0e10cSrcweir         OUString ouRel = pyString2ustring( PyTuple_GetItem( args, 1 ) );
578cdf0e10cSrcweir         OUString ret;
579cdf0e10cSrcweir         oslFileError e = osl_getAbsoluteFileURL( ouPath.pData, ouRel.pData, &(ret.pData) );
580cdf0e10cSrcweir         if( e != osl_File_E_None )
581cdf0e10cSrcweir         {
582cdf0e10cSrcweir             OUStringBuffer buf;
583cdf0e10cSrcweir             buf.appendAscii( "Couldn't absolutize " );
584cdf0e10cSrcweir             buf.append( ouRel );
585cdf0e10cSrcweir             buf.appendAscii( " using root " );
586cdf0e10cSrcweir             buf.append( ouPath );
587cdf0e10cSrcweir             buf.appendAscii( " for reason (" );
588cdf0e10cSrcweir             buf.append( (sal_Int32) e );
589cdf0e10cSrcweir             buf.appendAscii( ")" );
590cdf0e10cSrcweir 
591cdf0e10cSrcweir             PyErr_SetString(
592cdf0e10cSrcweir                 PyExc_OSError,
593cdf0e10cSrcweir                 OUStringToOString(buf.makeStringAndClear(),osl_getThreadTextEncoding()));
594cdf0e10cSrcweir             return 0;
595cdf0e10cSrcweir         }
596cdf0e10cSrcweir         return ustring2PyUnicode( ret ).getAcquired();
597cdf0e10cSrcweir     }
598cdf0e10cSrcweir     return 0;
599cdf0e10cSrcweir }
600cdf0e10cSrcweir 
601cdf0e10cSrcweir static PyObject * invoke ( PyObject *, PyObject * args )
602cdf0e10cSrcweir {
603cdf0e10cSrcweir     PyObject *ret = 0;
604cdf0e10cSrcweir     if( PyTuple_Check( args ) && PyTuple_Size( args ) == 3 )
605cdf0e10cSrcweir     {
606cdf0e10cSrcweir         PyObject *object = PyTuple_GetItem( args, 0 );
607cdf0e10cSrcweir 
608*77dc4149SPedro Giffuni         if( PYSTR_CHECK( PyTuple_GetItem( args, 1 ) ) )
609cdf0e10cSrcweir         {
610*77dc4149SPedro Giffuni #if PY_VERSION_HEX >= 0x03030000
611*77dc4149SPedro Giffuni             const char *name = PyUnicode_AsUTF8( PyTuple_GetItem( args, 1 ) );
612*77dc4149SPedro Giffuni #elif PY_MAJOR_VERSION >= 3
613*77dc4149SPedro Giffuni             PyRef pUtf8(PyUnicode_AsUTF8String( PyTuple_GetItem( args, 1 ) ), SAL_NO_ACQUIRE);
614*77dc4149SPedro Giffuni             const char *name = PyBytes_AsString( pUtf8.get() );
615*77dc4149SPedro Giffuni #else
616564d9007SPedro Giffuni             const char *name = PyBytes_AsString( PyTuple_GetItem( args, 1 ) );
617*77dc4149SPedro Giffuni #endif
618cdf0e10cSrcweir             if( PyTuple_Check( PyTuple_GetItem( args , 2 )))
619cdf0e10cSrcweir             {
620cdf0e10cSrcweir                 ret = PyUNO_invoke( object, name , PyTuple_GetItem( args, 2 ) );
621cdf0e10cSrcweir             }
622cdf0e10cSrcweir             else
623cdf0e10cSrcweir             {
624cdf0e10cSrcweir                 OStringBuffer buf;
625cdf0e10cSrcweir                 buf.append( "uno.invoke expects a tuple as 3rd argument, got " );
626*77dc4149SPedro Giffuni #if PY_MAJOR_VERSION >= 3
627*77dc4149SPedro Giffuni                 buf.append( OUStringToOString( pyString2ustring( PyTuple_GetItem( args, 2 ) ), RTL_TEXTENCODING_ASCII_US) );
628*77dc4149SPedro Giffuni #else
629564d9007SPedro Giffuni                 buf.append( PyBytes_AsString( PyObject_Str( PyTuple_GetItem( args, 2) ) ) );
630*77dc4149SPedro Giffuni #endif
631cdf0e10cSrcweir                 PyErr_SetString( PyExc_RuntimeError, buf.makeStringAndClear() );
632cdf0e10cSrcweir             }
633cdf0e10cSrcweir         }
634cdf0e10cSrcweir         else
635cdf0e10cSrcweir         {
636cdf0e10cSrcweir             OStringBuffer buf;
637cdf0e10cSrcweir             buf.append( "uno.invoke expected a string as 2nd argument, got " );
638*77dc4149SPedro Giffuni #if PY_MAJOR_VERSION >= 3
639*77dc4149SPedro Giffuni             buf.append( OUStringToOString( pyString2ustring( PyTuple_GetItem( args, 1 ) ), RTL_TEXTENCODING_ASCII_US ) );
640*77dc4149SPedro Giffuni #else
641564d9007SPedro Giffuni             buf.append( PyBytes_AsString( PyObject_Str( PyTuple_GetItem( args, 1) ) ) );
642*77dc4149SPedro Giffuni #endif
643cdf0e10cSrcweir             PyErr_SetString( PyExc_RuntimeError, buf.makeStringAndClear() );
644cdf0e10cSrcweir         }
645cdf0e10cSrcweir     }
646cdf0e10cSrcweir     else
647cdf0e10cSrcweir     {
648cdf0e10cSrcweir         OStringBuffer buf;
649cdf0e10cSrcweir         buf.append( "uno.invoke expects object, name, (arg1, arg2, ... )\n" );
650cdf0e10cSrcweir         PyErr_SetString( PyExc_RuntimeError, buf.makeStringAndClear() );
651cdf0e10cSrcweir     }
652cdf0e10cSrcweir     return ret;
653cdf0e10cSrcweir }
654cdf0e10cSrcweir 
655cdf0e10cSrcweir static PyObject *getCurrentContext( PyObject *, PyObject * )
656cdf0e10cSrcweir {
657cdf0e10cSrcweir     PyRef ret;
658cdf0e10cSrcweir     try
659cdf0e10cSrcweir     {
660cdf0e10cSrcweir         Runtime runtime;
661cdf0e10cSrcweir         ret = runtime.any2PyObject(
662cdf0e10cSrcweir             makeAny( com::sun::star::uno::getCurrentContext() ) );
663cdf0e10cSrcweir     }
664cdf0e10cSrcweir     catch( com::sun::star::uno::Exception & e )
665cdf0e10cSrcweir     {
666cdf0e10cSrcweir         raisePyExceptionWithAny( makeAny( e ) );
667cdf0e10cSrcweir     }
668cdf0e10cSrcweir     return ret.getAcquired();
669cdf0e10cSrcweir }
670cdf0e10cSrcweir 
671cdf0e10cSrcweir static PyObject *setCurrentContext( PyObject *, PyObject * args )
672cdf0e10cSrcweir {
673cdf0e10cSrcweir     PyRef ret;
674cdf0e10cSrcweir     try
675cdf0e10cSrcweir     {
676cdf0e10cSrcweir         if( PyTuple_Check( args ) && PyTuple_Size( args ) == 1 )
677cdf0e10cSrcweir         {
678cdf0e10cSrcweir 
679cdf0e10cSrcweir             Runtime runtime;
680cdf0e10cSrcweir             Any a = runtime.pyObject2Any( PyTuple_GetItem( args, 0 ) );
681cdf0e10cSrcweir 
682cdf0e10cSrcweir             Reference< com::sun::star::uno::XCurrentContext > context;
683cdf0e10cSrcweir 
684cdf0e10cSrcweir             if( (a.hasValue() && (a >>= context)) || ! a.hasValue() )
685cdf0e10cSrcweir             {
686cdf0e10cSrcweir                 ret = com::sun::star::uno::setCurrentContext( context ) ? Py_True : Py_False;
687cdf0e10cSrcweir             }
688cdf0e10cSrcweir             else
689cdf0e10cSrcweir             {
690cdf0e10cSrcweir                 OStringBuffer buf;
691cdf0e10cSrcweir                 buf.append( "uno.setCurrentContext expects an XComponentContext implementation, got " );
692*77dc4149SPedro Giffuni #if PY_MAJOR_VERSION >= 3
693*77dc4149SPedro Giffuni                 buf.append( OUStringToOString( pyString2ustring( PyTuple_GetItem( args, 0 ) ), RTL_TEXTENCODING_ASCII_US ) );
694*77dc4149SPedro Giffuni #else
695564d9007SPedro Giffuni                 buf.append( PyBytes_AsString( PyObject_Str( PyTuple_GetItem( args, 0) ) ) );
696*77dc4149SPedro Giffuni #endif
697cdf0e10cSrcweir                 PyErr_SetString( PyExc_RuntimeError, buf.makeStringAndClear() );
698cdf0e10cSrcweir             }
699cdf0e10cSrcweir         }
700cdf0e10cSrcweir         else
701cdf0e10cSrcweir         {
702cdf0e10cSrcweir             OStringBuffer buf;
703cdf0e10cSrcweir             buf.append( "uno.setCurrentContext expects exactly one argument (the current Context)\n" );
704cdf0e10cSrcweir             PyErr_SetString( PyExc_RuntimeError, buf.makeStringAndClear() );
705cdf0e10cSrcweir         }
706cdf0e10cSrcweir     }
707cdf0e10cSrcweir     catch( com::sun::star::uno::Exception & e )
708cdf0e10cSrcweir     {
709cdf0e10cSrcweir         raisePyExceptionWithAny( makeAny( e ) );
710cdf0e10cSrcweir     }
711cdf0e10cSrcweir     return ret.getAcquired();
712cdf0e10cSrcweir }
713cdf0e10cSrcweir 
714cdf0e10cSrcweir }
715cdf0e10cSrcweir 
716cdf0e10cSrcweir struct PyMethodDef PyUNOModule_methods [] =
717cdf0e10cSrcweir {
718*77dc4149SPedro Giffuni     {const_cast< char * >("getComponentContext"), getComponentContext, METH_NOARGS, NULL},
719*77dc4149SPedro Giffuni     {const_cast< char * >("_createUnoStructHelper"), createUnoStructHelper, METH_VARARGS, NULL},
720*77dc4149SPedro Giffuni     {const_cast< char * >("getTypeByName"), getTypeByName, METH_VARARGS, NULL},
721*77dc4149SPedro Giffuni     {const_cast< char * >("getConstantByName"), getConstantByName, METH_VARARGS, NULL},
722*77dc4149SPedro Giffuni     {const_cast< char * >("getClass"), getClass, METH_VARARGS, NULL},
723*77dc4149SPedro Giffuni     {const_cast< char * >("checkEnum"), checkEnum, METH_VARARGS, NULL},
724*77dc4149SPedro Giffuni     {const_cast< char * >("checkType"), checkType, METH_VARARGS, NULL},
725*77dc4149SPedro Giffuni     {const_cast< char * >("generateUuid"), generateUuid, METH_NOARGS, NULL},
726*77dc4149SPedro Giffuni     {const_cast< char * >("systemPathToFileUrl"), systemPathToFileUrl, METH_VARARGS, NULL},
727*77dc4149SPedro Giffuni     {const_cast< char * >("fileUrlToSystemPath"), fileUrlToSystemPath, METH_VARARGS, NULL},
728*77dc4149SPedro Giffuni     {const_cast< char * >("absolutize"), absolutize, METH_VARARGS, NULL},
729*77dc4149SPedro Giffuni     {const_cast< char * >("isInterface"), isInterface, METH_VARARGS, NULL},
730*77dc4149SPedro Giffuni     {const_cast< char * >("invoke"), invoke, METH_VARARGS, NULL},
731*77dc4149SPedro Giffuni     {const_cast< char * >("setCurrentContext"), setCurrentContext, METH_VARARGS, NULL},
732*77dc4149SPedro Giffuni     {const_cast< char * >("getCurrentContext"), getCurrentContext, METH_NOARGS, NULL},
733cdf0e10cSrcweir     {NULL, NULL, 0, NULL}
734cdf0e10cSrcweir };
735cdf0e10cSrcweir 
736*77dc4149SPedro Giffuni #if PY_MAJOR_VERSION >= 3
737*77dc4149SPedro Giffuni static struct PyModuleDef PyUNOModule =
738*77dc4149SPedro Giffuni {
739*77dc4149SPedro Giffuni     PyModuleDef_HEAD_INIT,
740*77dc4149SPedro Giffuni     const_cast< char * >("pyuno"),
741*77dc4149SPedro Giffuni     NULL,
742*77dc4149SPedro Giffuni     -1,
743*77dc4149SPedro Giffuni     PyUNOModule_methods
744*77dc4149SPedro Giffuni };
745*77dc4149SPedro Giffuni #endif
746cdf0e10cSrcweir }
747cdf0e10cSrcweir 
748*77dc4149SPedro Giffuni #if PY_MAJOR_VERSION >= 3
749*77dc4149SPedro Giffuni extern "C" PyMODINIT_FUNC PyInit_pyuno(void)
750*77dc4149SPedro Giffuni {
751*77dc4149SPedro Giffuni     PyObject *m;
752*77dc4149SPedro Giffuni 
753*77dc4149SPedro Giffuni     PyEval_InitThreads();
754*77dc4149SPedro Giffuni 
755*77dc4149SPedro Giffuni     m = PyModule_Create(&PyUNOModule);
756*77dc4149SPedro Giffuni     if (m == NULL)
757*77dc4149SPedro Giffuni         return NULL;
758*77dc4149SPedro Giffuni 
759*77dc4149SPedro Giffuni     if (PyType_Ready((PyTypeObject *)getPyUnoClass().get()))
760*77dc4149SPedro Giffuni         return NULL;
761*77dc4149SPedro Giffuni     return m;
762*77dc4149SPedro Giffuni }
763*77dc4149SPedro Giffuni #else
764cdf0e10cSrcweir extern "C" PY_DLLEXPORT void initpyuno()
765cdf0e10cSrcweir {
766cdf0e10cSrcweir     // noop when called already, otherwise needed to allow multiple threads
767564d9007SPedro Giffuni     // This has to be reworked for Python 3.
768cdf0e10cSrcweir     PyEval_InitThreads();
769cdf0e10cSrcweir     Py_InitModule (const_cast< char * >("pyuno"), PyUNOModule_methods);
770cdf0e10cSrcweir }
771*77dc4149SPedro Giffuni #endif
772