1 /* -*- Mode: C++; eval:(c-set-style "bsd"); tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <config_folders.h>
22 #include "pyuno_impl.hxx"
25 #include <unordered_map>
27 #include <osl/module.hxx>
28 #include <osl/thread.h>
29 #include <osl/file.hxx>
30 #include <sal/log.hxx>
32 #include <typelib/typedescription.hxx>
34 #include <rtl/ustring.hxx>
35 #include <rtl/strbuf.hxx>
36 #include <rtl/ustrbuf.hxx>
38 #include <rtl/bootstrap.hxx>
40 #include <uno/current_context.hxx>
41 #include <cppuhelper/bootstrap.hxx>
43 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
44 #include <com/sun/star/reflection/XConstantTypeDescription.hpp>
45 #include <com/sun/star/reflection/XIdlClass.hpp>
46 #include <com/sun/star/registry/InvalidRegistryException.hpp>
47 #include <com/sun/star/script/CannotConvertException.hpp>
48 #include <com/sun/star/uno/XComponentContext.hpp>
49 #include <com/sun/star/script/XInvocation2.hpp>
50 #include <com/sun/star/reflection/XIdlReflection.hpp>
51 #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
56 using com::sun::star::uno::Sequence
;
57 using com::sun::star::uno::Reference
;
58 using com::sun::star::uno::Any
;
59 using com::sun::star::uno::makeAny
;
60 using com::sun::star::uno::RuntimeException
;
61 using com::sun::star::uno::TypeDescription
;
62 using com::sun::star::uno::XComponentContext
;
63 using com::sun::star::container::NoSuchElementException
;
64 using com::sun::star::reflection::XIdlClass
;
65 using com::sun::star::script::XInvocation2
;
67 using namespace pyuno
;
72 @ index of the next to be used member in the initializer list !
74 // LEM TODO: export member names as keyword arguments in initialiser?
75 // Python supports very flexible variadic functions. By marking
76 // variables with one asterisk (e.g. *var) the given variable is
77 // defined to be a tuple of all the extra arguments. By marking
78 // variables with two asterisks (e.g. **var) the given variable is a
79 // dictionary of all extra keyword arguments; the keys are strings,
80 // which are the names that were used to identify the arguments. If
81 // they exist, these arguments must be the last one in the list.
85 // Keyword arguments used
87 // Which structure members are initialised
88 std::unordered_map
<OUString
, bool> initialised
;
89 // How many positional arguments are consumed
90 // This is always the so-many first ones
91 sal_Int32 nPosConsumed
;
100 throw RuntimeException("pyuno._createUnoStructHelper failed to create new dictionary");
106 void setUsed(PyObject
*key
)
108 PyDict_SetItem(used
, key
, Py_True
);
110 void setInitialised(const OUString
& key
, sal_Int32 pos
= -1)
112 if (initialised
[key
])
115 buf
.append( "pyuno._createUnoStructHelper: member '" + key
+ "'");
118 buf
.append( " at position " + OUString::number(pos
));
120 buf
.append( " initialised multiple times.");
121 throw RuntimeException(buf
.makeStringAndClear());
123 initialised
[key
] = true;
127 bool isInitialised(const OUString
& key
)
129 return initialised
[key
];
131 PyObject
*getUsed() const
135 sal_Int32
getCntConsumed() const
141 /// @throws RuntimeException
143 const Reference
< XInvocation2
> &inv
,
144 typelib_CompoundTypeDescription
*pCompType
,
145 PyObject
*initializer
,
146 PyObject
*kwinitializer
,
147 fillStructState
&state
,
148 const Runtime
&runtime
)
150 if( pCompType
->pBaseTypeDescription
)
151 fillStruct( inv
, pCompType
->pBaseTypeDescription
, initializer
, kwinitializer
, state
, runtime
);
153 const sal_Int32 nMembers
= pCompType
->nMembers
;
155 for( int i
= 0 ; i
< nMembers
; i
++ )
157 const OUString
OUMemberName (pCompType
->ppMemberNames
[i
]);
158 PyObject
*pyMemberName
=
159 PyUnicode_FromString(OUStringToOString(OUMemberName
,
160 RTL_TEXTENCODING_UTF8
).getStr());
161 if ( PyObject
*element
= PyDict_GetItem(kwinitializer
, pyMemberName
) )
163 state
.setInitialised(OUMemberName
);
164 state
.setUsed(pyMemberName
);
165 Any a
= runtime
.pyObject2Any( element
, ACCEPT_UNO_ANY
);
166 inv
->setValue( OUMemberName
, a
);
171 const int remainingPosInitialisers
= PyTuple_Size(initializer
) - state
.getCntConsumed();
172 for( int i
= 0 ; i
< remainingPosInitialisers
&& i
< nMembers
; i
++ )
174 const int tupleIndex
= state
.getCntConsumed();
175 const OUString
& rMemberName (pCompType
->ppMemberNames
[i
]);
176 state
.setInitialised(rMemberName
, tupleIndex
);
177 PyObject
*element
= PyTuple_GetItem( initializer
, tupleIndex
);
178 Any a
= runtime
.pyObject2Any( element
, ACCEPT_UNO_ANY
);
179 inv
->setValue( rMemberName
, a
);
182 if ( PyTuple_Size( initializer
) <= 0 )
185 // Allow partial initialisation when only keyword arguments are given
186 for ( int i
= 0; i
< nMembers
; ++i
)
188 const OUString
memberName (pCompType
->ppMemberNames
[i
]);
189 if ( ! state
.isInitialised( memberName
) )
191 OUString buf
= "pyuno._createUnoStructHelper: member '" +
193 "' of struct type '" +
194 OUString::unacquired(&pCompType
->aBase
.pTypeName
) +
195 "' not given a value.";
196 throw RuntimeException(buf
);
203 static OUString sLibDir
= []() {
206 // workarounds the $(ORIGIN) until it is available
207 if (Module::getUrlFromAddress(reinterpret_cast<oslGenericFunction
>(getLibDir
), libDir
))
209 libDir
= libDir
.copy(0, libDir
.lastIndexOf('/'));
210 OUString
name("PYUNOLIBDIR");
211 rtl_bootstrap_set(name
.pData
, libDir
.pData
);
219 void raisePySystemException( const char * exceptionType
, const OUString
& message
)
221 OString buf
= OStringLiteral("Error during bootstrapping uno (") +
224 OUStringToOString( message
, osl_getThreadTextEncoding() );
225 PyErr_SetString( PyExc_SystemError
, buf
.getStr() );
230 static PyObject
* getComponentContext(
231 SAL_UNUSED_PARAMETER PyObject
*, SAL_UNUSED_PARAMETER PyObject
*)
236 Reference
<XComponentContext
> ctx
;
238 // getLibDir() must be called in order to set bootstrap variables correctly !
239 OUString
path( getLibDir());
240 if( Runtime::isInitialized() )
243 ctx
= runtime
.getImpl()->cargo
->xContext
;
250 PyExc_RuntimeError
, "osl_getUrlFromAddress fails, that's why I cannot find ini "
251 "file for bootstrapping python uno bridge\n" );
255 OUString iniFile
= path
+
257 "/../" LIBO_ETC_FOLDER
259 "/" SAL_CONFIGFILE( "pyuno" );
260 osl::DirectoryItem item
;
261 if( osl::DirectoryItem::get( iniFile
, item
) == osl::FileBase::E_None
)
263 // in case pyuno.ini exists, use this file for bootstrapping
264 PyThreadDetach antiguard
;
265 ctx
= cppu::defaultBootstrap_InitialComponentContext (iniFile
);
269 // defaulting to the standard bootstrapping
270 PyThreadDetach antiguard
;
271 ctx
= cppu::defaultBootstrap_InitialComponentContext ();
276 if( ! Runtime::isInitialized() )
278 Runtime::initialize( ctx
);
281 ret
= runtime
.any2PyObject( makeAny( ctx
) );
283 catch (const css::registry::InvalidRegistryException
&e
)
285 // can't use raisePyExceptionWithAny() here, because the function
286 // does any conversions, which will not work with a
287 // wrongly bootstrapped pyuno!
288 raisePySystemException( "InvalidRegistryException", e
.Message
);
290 catch(const css::lang::IllegalArgumentException
& e
)
292 raisePySystemException( "IllegalArgumentException", e
.Message
);
294 catch(const css::script::CannotConvertException
& e
)
296 raisePySystemException( "CannotConvertException", e
.Message
);
298 catch (const css::uno::RuntimeException
& e
)
300 raisePySystemException( "RuntimeException", e
.Message
);
302 catch (const css::uno::Exception
& e
)
304 raisePySystemException( "uno::Exception", e
.Message
);
306 return ret
.getAcquired();
309 // While pyuno.private_initTestEnvironment is called from individual Python tests (e.g., from
310 // UnoInProcess in unotest/source/python/org/libreoffice/unotest.py, which makes sure to call it
311 // only once), pyuno.private_deinitTestEnvironment is called centrally from
312 // unotest/source/python/org/libreoffice/unittest.py at the end of every PythonTest (to DeInitVCL
313 // exactly once near the end of the process, if InitVCL has ever been called via
314 // pyuno.private_initTestEnvironment):
316 osl::Module
* testModule
= nullptr;
318 static PyObject
* initTestEnvironment(
319 SAL_UNUSED_PARAMETER PyObject
*, SAL_UNUSED_PARAMETER PyObject
*)
321 // this tries to bootstrap enough of the soffice from python to run
322 // unit tests, which is only possible indirectly because pyuno is URE
323 // so load "test" library and invoke a function there to do the work
324 assert(testModule
== nullptr);
327 PyObject
*const ctx(getComponentContext(nullptr, nullptr));
328 if (!ctx
) { abort(); }
329 Runtime
const runtime
;
330 Any
const a(runtime
.pyObject2Any(ctx
));
331 Reference
<XComponentContext
> xContext
;
333 if (!xContext
.is()) { abort(); }
334 using css::lang::XMultiServiceFactory
;
335 Reference
<XMultiServiceFactory
> const xMSF(
336 xContext
->getServiceManager(),
337 css::uno::UNO_QUERY_THROW
);
338 char *const testlib
= getenv("TEST_LIB");
339 if (!testlib
) { abort(); }
341 OString
const libname
= OString(testlib
, strlen(testlib
))
342 .replaceAll(OString('/'), OString('\\'));
344 OString
const libname(testlib
, strlen(testlib
));
347 osl::Module
&mod
= runtime
.getImpl()->cargo
->testModule
;
348 mod
.load(OStringToOUString(libname
, osl_getThreadTextEncoding()),
349 SAL_LOADMODULE_LAZY
| SAL_LOADMODULE_GLOBAL
);
350 if (!mod
.is()) { abort(); }
351 oslGenericFunction
const pFunc(
352 mod
.getFunctionSymbol("test_init"));
353 if (!pFunc
) { abort(); }
354 reinterpret_cast<void (SAL_CALL
*)(XMultiServiceFactory
*)>(pFunc
)(xMSF
.get());
357 catch (const css::uno::Exception
&)
364 static PyObject
* deinitTestEnvironment(
365 SAL_UNUSED_PARAMETER PyObject
*, SAL_UNUSED_PARAMETER PyObject
*)
367 if (testModule
!= nullptr)
371 oslGenericFunction
const pFunc(
372 testModule
->getFunctionSymbol("test_deinit"));
373 if (!pFunc
) { abort(); }
374 reinterpret_cast<void (SAL_CALL
*)()>(pFunc
)();
376 catch (const css::uno::Exception
&)
384 PyObject
* extractOneStringArg( PyObject
*args
, char const *funcName
)
386 if( !PyTuple_Check( args
) || PyTuple_Size( args
) != 1 )
388 OString buf
= funcName
+ OStringLiteral(": expecting one string argument");
389 PyErr_SetString( PyExc_RuntimeError
, buf
.getStr() );
392 PyObject
*obj
= PyTuple_GetItem( args
, 0 );
393 if (!PyUnicode_Check(obj
))
395 OString buf
= funcName
+ OStringLiteral(": expecting one string argument");
396 PyErr_SetString( PyExc_TypeError
, buf
.getStr());
402 static PyObject
*createUnoStructHelper(
403 SAL_UNUSED_PARAMETER PyObject
*, PyObject
* args
, PyObject
* keywordArgs
)
410 if( PyTuple_Size( args
) == 2 )
412 PyObject
*structName
= PyTuple_GetItem(args
, 0);
413 PyObject
*initializer
= PyTuple_GetItem(args
, 1);
415 if (PyUnicode_Check(structName
))
417 if( PyTuple_Check( initializer
) && PyDict_Check ( keywordArgs
) )
419 OUString
typeName( OUString::createFromAscii(PyUnicode_AsUTF8(structName
)));
420 RuntimeCargo
*c
= runtime
.getImpl()->cargo
;
421 Reference
<XIdlClass
> idl_class
= c
->xCoreReflection
->forName (typeName
);
424 idl_class
->createObject (IdlStruct
);
425 PyRef
returnCandidate( PyUNOStruct_new( IdlStruct
, c
->xInvocation
) );
426 PyUNO
*me
= reinterpret_cast<PyUNO
*>( returnCandidate
.get() );
427 TypeDescription
desc( typeName
);
428 OSL_ASSERT( desc
.is() ); // could already instantiate an XInvocation2 !
430 typelib_CompoundTypeDescription
*pCompType
=
431 reinterpret_cast<typelib_CompoundTypeDescription
*>(desc
.get());
432 fillStructState state
;
433 if ( PyTuple_Size( initializer
) > 0 || PyDict_Size( keywordArgs
) > 0 )
434 fillStruct( me
->members
->xInvocation
, pCompType
, initializer
, keywordArgs
, state
, runtime
);
435 if( state
.getCntConsumed() != PyTuple_Size(initializer
) )
437 throw RuntimeException( "pyuno._createUnoStructHelper: too many "
438 "elements in the initializer list, expected " +
439 OUString::number(state
.getCntConsumed()) + ", got " +
440 OUString::number( PyTuple_Size(initializer
) ) );
442 ret
= PyRef( PyTuple_Pack(2, returnCandidate
.get(), state
.getUsed()), SAL_NO_ACQUIRE
);
447 buf
.append( "UNO struct " );
448 buf
.append( PyUnicode_AsUTF8(structName
) );
449 buf
.append( " is unknown" );
450 PyErr_SetString (PyExc_RuntimeError
, buf
.getStr());
457 "pyuno._createUnoStructHelper: 2nd argument (initializer sequence) is no tuple" );
462 PyErr_SetString (PyExc_AttributeError
, "createUnoStruct: first argument wasn't a string");
467 PyErr_SetString (PyExc_AttributeError
, "pyuno._createUnoStructHelper: expects exactly two non-keyword arguments:\n\tStructure Name\n\tinitialiser tuple; may be the empty tuple");
470 catch( const css::uno::RuntimeException
& e
)
472 raisePyExceptionWithAny( makeAny( e
) );
474 catch( const css::script::CannotConvertException
& e
)
476 raisePyExceptionWithAny( makeAny( e
) );
478 catch( const css::uno::Exception
& e
)
480 raisePyExceptionWithAny( makeAny( e
) );
482 return ret
.getAcquired();
485 static PyObject
*getTypeByName(
486 SAL_UNUSED_PARAMETER PyObject
*, PyObject
*args
)
488 PyObject
* ret
= nullptr;
494 if (PyArg_ParseTuple (args
, "s", &name
))
496 OUString
typeName ( OUString::createFromAscii( name
) );
497 TypeDescription
typeDesc( typeName
);
501 ret
= PyUNO_Type_new(
502 name
, static_cast<css::uno::TypeClass
>(typeDesc
.get()->eTypeClass
), runtime
);
506 OString buf
= OStringLiteral("Type ") + name
+ " is unknown";
507 PyErr_SetString( PyExc_RuntimeError
, buf
.getStr() );
511 catch ( const RuntimeException
& e
)
513 raisePyExceptionWithAny( makeAny( e
) );
518 static PyObject
*getConstantByName(
519 SAL_UNUSED_PARAMETER PyObject
*, PyObject
*args
)
521 PyObject
*ret
= nullptr;
526 if (PyArg_ParseTuple (args
, "s", &name
))
528 OUString
typeName ( OUString::createFromAscii( name
) );
530 css::uno::Reference
< css::reflection::XConstantTypeDescription
> td
;
531 if (!(runtime
.getImpl()->cargo
->xTdMgr
->getByHierarchicalName(
535 throw RuntimeException( "pyuno.getConstantByName: " + typeName
+ "is not a constant" );
537 PyRef constant
= runtime
.any2PyObject( td
->getConstantValue() );
538 ret
= constant
.getAcquired();
541 catch( const NoSuchElementException
& e
)
543 // to the python programmer, this is a runtime exception,
544 // do not support tweakings with the type system
545 RuntimeException
runExc( e
.Message
);
546 raisePyExceptionWithAny( makeAny( runExc
) );
548 catch(const css::script::CannotConvertException
& e
)
550 raisePyExceptionWithAny( makeAny( e
) );
552 catch(const css::lang::IllegalArgumentException
& e
)
554 raisePyExceptionWithAny( makeAny( e
) );
556 catch( const RuntimeException
& e
)
558 raisePyExceptionWithAny( makeAny(e
) );
563 static PyObject
*checkType( SAL_UNUSED_PARAMETER PyObject
*, PyObject
*args
)
565 if( !PyTuple_Check( args
) || PyTuple_Size( args
) != 1 )
567 OString buf
= "pyuno.checkType : expecting one uno.Type argument";
568 PyErr_SetString( PyExc_RuntimeError
, buf
.getStr() );
571 PyObject
*obj
= PyTuple_GetItem( args
, 0 );
577 catch(const RuntimeException
& e
)
579 raisePyExceptionWithAny( makeAny( e
) );
582 Py_INCREF( Py_None
);
586 static PyObject
*checkEnum( SAL_UNUSED_PARAMETER PyObject
*, PyObject
*args
)
588 if( !PyTuple_Check( args
) || PyTuple_Size( args
) != 1 )
590 OString buf
= "pyuno.checkType : expecting one uno.Type argument";
591 PyErr_SetString( PyExc_RuntimeError
, buf
.getStr() );
594 PyObject
*obj
= PyTuple_GetItem( args
, 0 );
600 catch(const RuntimeException
& e
)
602 raisePyExceptionWithAny( makeAny( e
) );
605 Py_INCREF( Py_None
);
609 static PyObject
*getClass( SAL_UNUSED_PARAMETER PyObject
*, PyObject
*args
)
611 PyObject
*obj
= extractOneStringArg( args
, "pyuno.getClass");
618 PyRef ret
= getClass(pyString2ustring(obj
), runtime
);
619 Py_XINCREF( ret
.get() );
622 catch(const RuntimeException
& e
)
624 raisePyExceptionWithAny( makeAny(e
) );
629 static PyObject
*isInterface( SAL_UNUSED_PARAMETER PyObject
*, PyObject
*args
)
632 if( PyTuple_Check( args
) && PyTuple_Size( args
) == 1 )
634 PyObject
*obj
= PyTuple_GetItem( args
, 0 );
636 return PyLong_FromLong( isInterfaceClass( r
, obj
) );
638 return PyLong_FromLong( 0 );
641 static PyObject
* generateUuid(
642 SAL_UNUSED_PARAMETER PyObject
*, SAL_UNUSED_PARAMETER PyObject
* )
644 Sequence
< sal_Int8
> seq( 16 );
645 rtl_createUuid( reinterpret_cast<sal_uInt8
*>(seq
.getArray()) , nullptr , false );
650 ret
= runtime
.any2PyObject( makeAny( seq
) );
652 catch( const RuntimeException
& e
)
654 raisePyExceptionWithAny( makeAny(e
) );
656 return ret
.getAcquired();
659 static PyObject
*systemPathToFileUrl(
660 SAL_UNUSED_PARAMETER PyObject
*, PyObject
* args
)
662 PyObject
*obj
= extractOneStringArg( args
, "pyuno.systemPathToFileUrl" );
666 OUString sysPath
= pyString2ustring( obj
);
668 osl::FileBase::RC e
= osl::FileBase::getFileURLFromSystemPath( sysPath
, url
);
670 if( e
!= osl::FileBase::E_None
)
672 OUString buf
= "Couldn't convert " +
674 " to a file url for reason (" +
675 OUString::number( static_cast<sal_Int32
>(e
) ) +
677 raisePyExceptionWithAny(
678 makeAny( RuntimeException( buf
)));
681 return ustring2PyUnicode( url
).getAcquired();
684 static PyObject
* fileUrlToSystemPath(
685 SAL_UNUSED_PARAMETER PyObject
*, PyObject
* args
)
687 PyObject
*obj
= extractOneStringArg( args
, "pyuno.fileUrlToSystemPath" );
691 OUString url
= pyString2ustring( obj
);
693 osl::FileBase::RC e
= osl::FileBase::getSystemPathFromFileURL( url
, sysPath
);
695 if( e
!= osl::FileBase::E_None
)
697 OUString buf
= "Couldn't convert file url " +
699 " to a system path for reason (" +
700 OUString::number( static_cast<sal_Int32
>(e
) ) +
702 raisePyExceptionWithAny(
703 makeAny( RuntimeException( buf
)));
706 return ustring2PyUnicode( sysPath
).getAcquired();
709 static PyObject
* absolutize( SAL_UNUSED_PARAMETER PyObject
*, PyObject
* args
)
711 if( PyTuple_Check( args
) && PyTuple_Size( args
) == 2 )
713 OUString ouPath
= pyString2ustring( PyTuple_GetItem( args
, 0 ) );
714 OUString ouRel
= pyString2ustring( PyTuple_GetItem( args
, 1 ) );
716 oslFileError e
= osl_getAbsoluteFileURL( ouPath
.pData
, ouRel
.pData
, &(ret
.pData
) );
717 if( e
!= osl_File_E_None
)
720 "Couldn't absolutize " +
725 OUString::number(static_cast<sal_Int32
>(e
) ) +
730 OUStringToOString(buf
,osl_getThreadTextEncoding()).getStr());
733 return ustring2PyUnicode( ret
).getAcquired();
738 static PyObject
* invoke(SAL_UNUSED_PARAMETER PyObject
*, PyObject
*args
)
740 PyObject
*ret
= nullptr;
741 if(PyTuple_Check(args
) && PyTuple_Size(args
) == 3)
743 PyObject
*object
= PyTuple_GetItem(args
, 0);
744 PyObject
*item1
= PyTuple_GetItem(args
, 1);
745 if (PyUnicode_Check(item1
))
747 const char *name
= PyUnicode_AsUTF8(item1
);
748 PyObject
*item2
= PyTuple_GetItem(args
, 2);
749 if(PyTuple_Check(item2
))
751 ret
= PyUNO_invoke(object
, name
, item2
);
756 buf
.append("uno.invoke expects a tuple as 3rd argument, got ");
757 buf
.append(PyUnicode_AsUTF8(PyObject_Str(item2
)));
759 PyExc_RuntimeError
, buf
.makeStringAndClear().getStr());
765 buf
.append("uno.invoke expected a string as 2nd argument, got ");
766 buf
.append(PyUnicode_AsUTF8(PyObject_Str(item1
)));
768 PyExc_RuntimeError
, buf
.makeStringAndClear().getStr());
773 OString buf
= "uno.invoke expects object, name, (arg1, arg2, ... )\n";
774 PyErr_SetString(PyExc_RuntimeError
, buf
.getStr());
779 static PyObject
*getCurrentContext(
780 SAL_UNUSED_PARAMETER PyObject
*, SAL_UNUSED_PARAMETER PyObject
* )
786 ret
= runtime
.any2PyObject(
787 makeAny( css::uno::getCurrentContext() ) );
789 catch( const css::uno::Exception
& e
)
791 raisePyExceptionWithAny( makeAny( e
) );
793 return ret
.getAcquired();
796 static PyObject
*setCurrentContext(
797 SAL_UNUSED_PARAMETER PyObject
*, SAL_UNUSED_PARAMETER PyObject
* args
)
802 if( PyTuple_Check( args
) && PyTuple_Size( args
) == 1 )
806 Any a
= runtime
.pyObject2Any( PyTuple_GetItem( args
, 0 ) );
808 Reference
< css::uno::XCurrentContext
> context
;
810 if( (a
.hasValue() && (a
>>= context
)) || ! a
.hasValue() )
812 ret
= css::uno::setCurrentContext( context
) ? Py_True
: Py_False
;
817 buf
.append( "uno.setCurrentContext expects an XComponentContext implementation, got " );
819 PyUnicode_AsUTF8(PyObject_Str(PyTuple_GetItem(args
, 0))));
821 PyExc_RuntimeError
, buf
.makeStringAndClear().getStr() );
826 OString buf
= "uno.setCurrentContext expects exactly one argument (the current Context)\n";
828 PyExc_RuntimeError
, buf
.getStr() );
831 catch( const css::uno::Exception
& e
)
833 raisePyExceptionWithAny( makeAny( e
) );
835 return ret
.getAcquired();
838 static PyObject
*sal_debug(
839 SAL_UNUSED_PARAMETER PyObject
*, SAL_UNUSED_PARAMETER PyObject
* args
)
841 Py_INCREF( Py_None
);
842 if( !PyTuple_Check( args
) || PyTuple_Size( args
) != 1 )
845 OUString line
= pyString2ustring( PyTuple_GetItem( args
, 0 ) );
854 struct PyMethodDef PyUNOModule_methods
[] =
856 {"private_initTestEnvironment", initTestEnvironment
, METH_VARARGS
, nullptr},
857 {"private_deinitTestEnvironment", deinitTestEnvironment
, METH_VARARGS
, nullptr},
858 {"getComponentContext", getComponentContext
, METH_VARARGS
, nullptr},
859 {"_createUnoStructHelper", reinterpret_cast<PyCFunction
>(createUnoStructHelper
), METH_VARARGS
| METH_KEYWORDS
, nullptr},
860 {"getTypeByName", getTypeByName
, METH_VARARGS
, nullptr},
861 {"getConstantByName", getConstantByName
, METH_VARARGS
, nullptr},
862 {"getClass", getClass
, METH_VARARGS
, nullptr},
863 {"checkEnum", checkEnum
, METH_VARARGS
, nullptr},
864 {"checkType", checkType
, METH_VARARGS
, nullptr},
865 {"generateUuid", generateUuid
, METH_VARARGS
, nullptr},
866 {"systemPathToFileUrl", systemPathToFileUrl
, METH_VARARGS
, nullptr},
867 {"fileUrlToSystemPath", fileUrlToSystemPath
, METH_VARARGS
, nullptr},
868 {"absolutize", absolutize
, METH_VARARGS
| METH_KEYWORDS
, nullptr},
869 {"isInterface", isInterface
, METH_VARARGS
, nullptr},
870 {"invoke", invoke
, METH_VARARGS
| METH_KEYWORDS
, nullptr},
871 {"setCurrentContext", setCurrentContext
, METH_VARARGS
, nullptr},
872 {"getCurrentContext", getCurrentContext
, METH_VARARGS
, nullptr},
873 {"sal_debug", sal_debug
, METH_VARARGS
, nullptr},
874 {nullptr, nullptr, 0, nullptr}
880 PyObject
* PyInit_pyuno()
883 PyUNOStruct_initType();
884 // noop when called already, otherwise needed to allow multiple threads
885 PyEval_InitThreads();
886 static struct PyModuleDef moduledef
=
888 PyModuleDef_HEAD_INIT
,
889 "pyuno", // module name
890 nullptr, // module documentation
891 -1, // module keeps state in global variables,
892 PyUNOModule_methods
, // modules methods
893 nullptr, // m_reload (must be 0)
894 nullptr, // m_traverse
898 return PyModule_Create(&moduledef
);
901 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */