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::UNO_QUERY
;
61 using com::sun::star::uno::RuntimeException
;
62 using com::sun::star::uno::TypeDescription
;
63 using com::sun::star::uno::XComponentContext
;
64 using com::sun::star::container::NoSuchElementException
;
65 using com::sun::star::reflection::XIdlClass
;
66 using com::sun::star::script::XInvocation2
;
68 using namespace pyuno
;
73 @ index of the next to be used member in the initializer list !
75 // LEM TODO: export member names as keyword arguments in initialiser?
76 // Python supports very flexible variadic functions. By marking
77 // variables with one asterisk (e.g. *var) the given variable is
78 // defined to be a tuple of all the extra arguments. By marking
79 // variables with two asterisks (e.g. **var) the given variable is a
80 // dictionary of all extra keyword arguments; the keys are strings,
81 // which are the names that were used to identify the arguments. If
82 // they exist, these arguments must be the last one in the list.
86 // Keyword arguments used
88 // Which structure members are initialised
89 std::unordered_map
<OUString
, bool> initialised
;
90 // How many positional arguments are consumed
91 // This is always the so-many first ones
92 sal_Int32 nPosConsumed
;
101 throw RuntimeException("pyuno._createUnoStructHelper failed to create new dictionary");
107 void setUsed(PyObject
*key
)
109 PyDict_SetItem(used
, key
, Py_True
);
111 void setInitialised(const OUString
& key
, sal_Int32 pos
= -1)
113 if (initialised
[key
])
116 buf
.append( "pyuno._createUnoStructHelper: member '" + key
+ "'");
119 buf
.append( " at position " + OUString::number(pos
));
121 buf
.append( " initialised multiple times.");
122 throw RuntimeException(buf
.makeStringAndClear());
124 initialised
[key
] = true;
128 bool isInitialised(const OUString
& key
)
130 return initialised
[key
];
132 PyObject
*getUsed() const
136 sal_Int32
getCntConsumed() const
142 /// @throws RuntimeException
144 const Reference
< XInvocation2
> &inv
,
145 typelib_CompoundTypeDescription
*pCompType
,
146 PyObject
*initializer
,
147 PyObject
*kwinitializer
,
148 fillStructState
&state
,
149 const Runtime
&runtime
)
151 if( pCompType
->pBaseTypeDescription
)
152 fillStruct( inv
, pCompType
->pBaseTypeDescription
, initializer
, kwinitializer
, state
, runtime
);
154 const sal_Int32 nMembers
= pCompType
->nMembers
;
156 for( int i
= 0 ; i
< nMembers
; i
++ )
158 const OUString
OUMemberName (pCompType
->ppMemberNames
[i
]);
159 PyObject
*pyMemberName
=
160 PyStr_FromString(OUStringToOString(OUMemberName
,
161 RTL_TEXTENCODING_UTF8
).getStr());
162 if ( PyObject
*element
= PyDict_GetItem(kwinitializer
, pyMemberName
) )
164 state
.setInitialised(OUMemberName
);
165 state
.setUsed(pyMemberName
);
166 Any a
= runtime
.pyObject2Any( element
, ACCEPT_UNO_ANY
);
167 inv
->setValue( OUMemberName
, a
);
172 const int remainingPosInitialisers
= PyTuple_Size(initializer
) - state
.getCntConsumed();
173 for( int i
= 0 ; i
< remainingPosInitialisers
&& i
< nMembers
; i
++ )
175 const int tupleIndex
= state
.getCntConsumed();
176 const OUString
& rMemberName (pCompType
->ppMemberNames
[i
]);
177 state
.setInitialised(rMemberName
, tupleIndex
);
178 PyObject
*element
= PyTuple_GetItem( initializer
, tupleIndex
);
179 Any a
= runtime
.pyObject2Any( element
, ACCEPT_UNO_ANY
);
180 inv
->setValue( rMemberName
, a
);
183 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
);
204 static OUString sLibDir
= []() {
207 // workarounds the $(ORIGIN) until it is available
208 if (Module::getUrlFromAddress(reinterpret_cast<oslGenericFunction
>(getLibDir
), libDir
))
210 libDir
= libDir
.copy(0, libDir
.lastIndexOf('/'));
211 OUString
name("PYUNOLIBDIR");
212 rtl_bootstrap_set(name
.pData
, libDir
.pData
);
220 void raisePySystemException( const char * exceptionType
, const OUString
& message
)
222 OString buf
= OStringLiteral("Error during bootstrapping uno (") +
225 OUStringToOString( message
, osl_getThreadTextEncoding() );
226 PyErr_SetString( PyExc_SystemError
, buf
.getStr() );
231 static PyObject
* getComponentContext(
232 SAL_UNUSED_PARAMETER PyObject
*, SAL_UNUSED_PARAMETER PyObject
*)
237 Reference
<XComponentContext
> ctx
;
239 // getLibDir() must be called in order to set bootstrap variables correctly !
240 OUString
path( getLibDir());
241 if( Runtime::isInitialized() )
244 ctx
= runtime
.getImpl()->cargo
->xContext
;
251 PyExc_RuntimeError
, "osl_getUrlFromAddress fails, that's why I cannot find ini "
252 "file for bootstrapping python uno bridge\n" );
256 OUString iniFile
= path
+
258 "/../" LIBO_ETC_FOLDER
260 "/" SAL_CONFIGFILE( "pyuno" );
261 osl::DirectoryItem item
;
262 if( osl::DirectoryItem::get( iniFile
, item
) == osl::FileBase::E_None
)
264 // in case pyuno.ini exists, use this file for bootstrapping
265 PyThreadDetach antiguard
;
266 ctx
= cppu::defaultBootstrap_InitialComponentContext (iniFile
);
270 // defaulting to the standard bootstrapping
271 PyThreadDetach antiguard
;
272 ctx
= cppu::defaultBootstrap_InitialComponentContext ();
277 if( ! Runtime::isInitialized() )
279 Runtime::initialize( ctx
);
282 ret
= runtime
.any2PyObject( makeAny( ctx
) );
284 catch (const css::registry::InvalidRegistryException
&e
)
286 // can't use raisePyExceptionWithAny() here, because the function
287 // does any conversions, which will not work with a
288 // wrongly bootstrapped pyuno!
289 raisePySystemException( "InvalidRegistryException", e
.Message
);
291 catch(const css::lang::IllegalArgumentException
& e
)
293 raisePySystemException( "IllegalArgumentException", e
.Message
);
295 catch(const css::script::CannotConvertException
& e
)
297 raisePySystemException( "CannotConvertException", e
.Message
);
299 catch (const css::uno::RuntimeException
& e
)
301 raisePySystemException( "RuntimeException", e
.Message
);
303 catch (const css::uno::Exception
& e
)
305 raisePySystemException( "uno::Exception", e
.Message
);
307 return ret
.getAcquired();
310 // While pyuno.private_initTestEnvironment is called from individual Python tests (e.g., from
311 // UnoInProcess in unotest/source/python/org/libreoffice/unotest.py, which makes sure to call it
312 // only once), pyuno.private_deinitTestEnvironment is called centrally from
313 // unotest/source/python/org/libreoffice/unittest.py at the end of every PythonTest (to DeInitVCL
314 // exactly once near the end of the process, if InitVCL has ever been called via
315 // pyuno.private_initTestEnvironment):
317 static osl::Module
* testModule
= nullptr;
319 static PyObject
* initTestEnvironment(
320 SAL_UNUSED_PARAMETER PyObject
*, SAL_UNUSED_PARAMETER PyObject
*)
322 // this tries to bootstrap enough of the soffice from python to run
323 // unit tests, which is only possible indirectly because pyuno is URE
324 // so load "test" library and invoke a function there to do the work
325 assert(testModule
== nullptr);
328 PyObject
*const ctx(getComponentContext(nullptr, nullptr));
329 if (!ctx
) { abort(); }
330 Runtime
const runtime
;
331 Any
const a(runtime
.pyObject2Any(ctx
));
332 Reference
<XComponentContext
> xContext
;
334 if (!xContext
.is()) { abort(); }
335 using css::lang::XMultiServiceFactory
;
336 Reference
<XMultiServiceFactory
> const xMSF(
337 xContext
->getServiceManager(),
338 css::uno::UNO_QUERY_THROW
);
339 char *const testlib
= getenv("TEST_LIB");
340 if (!testlib
) { abort(); }
342 OString
const libname
= OString(testlib
, strlen(testlib
))
343 .replaceAll(OString('/'), OString('\\'));
345 OString
const libname(testlib
, strlen(testlib
));
348 osl::Module
&mod
= runtime
.getImpl()->cargo
->testModule
;
349 mod
.load(OStringToOUString(libname
, osl_getThreadTextEncoding()),
350 SAL_LOADMODULE_LAZY
| SAL_LOADMODULE_GLOBAL
);
351 if (!mod
.is()) { abort(); }
352 oslGenericFunction
const pFunc(
353 mod
.getFunctionSymbol("test_init"));
354 if (!pFunc
) { abort(); }
355 reinterpret_cast<void (SAL_CALL
*)(XMultiServiceFactory
*)>(pFunc
)(xMSF
.get());
358 catch (const css::uno::Exception
&)
365 static PyObject
* deinitTestEnvironment(
366 SAL_UNUSED_PARAMETER PyObject
*, SAL_UNUSED_PARAMETER PyObject
*)
368 if (testModule
!= nullptr)
372 oslGenericFunction
const pFunc(
373 testModule
->getFunctionSymbol("test_deinit"));
374 if (!pFunc
) { abort(); }
375 reinterpret_cast<void (SAL_CALL
*)()>(pFunc
)();
377 catch (const css::uno::Exception
&)
385 PyObject
* extractOneStringArg( PyObject
*args
, char const *funcName
)
387 if( !PyTuple_Check( args
) || PyTuple_Size( args
) != 1 )
389 OString buf
= funcName
+ OStringLiteral(": expecting one string argument");
390 PyErr_SetString( PyExc_RuntimeError
, buf
.getStr() );
393 PyObject
*obj
= PyTuple_GetItem( args
, 0 );
394 if (!PyStr_Check(obj
) && !PyUnicode_Check(obj
))
396 OString buf
= funcName
+ OStringLiteral(": expecting one string argument");
397 PyErr_SetString( PyExc_TypeError
, buf
.getStr());
403 static PyObject
*createUnoStructHelper(
404 SAL_UNUSED_PARAMETER PyObject
*, PyObject
* args
, PyObject
* keywordArgs
)
411 if( PyTuple_Size( args
) == 2 )
413 PyObject
*structName
= PyTuple_GetItem(args
, 0);
414 PyObject
*initializer
= PyTuple_GetItem(args
, 1);
416 if (PyStr_Check(structName
))
418 if( PyTuple_Check( initializer
) && PyDict_Check ( keywordArgs
) )
420 OUString
typeName( OUString::createFromAscii(PyStr_AsString(structName
)));
421 RuntimeCargo
*c
= runtime
.getImpl()->cargo
;
422 Reference
<XIdlClass
> idl_class
= c
->xCoreReflection
->forName (typeName
);
425 idl_class
->createObject (IdlStruct
);
426 PyRef
returnCandidate( PyUNOStruct_new( IdlStruct
, c
->xInvocation
) );
427 PyUNO
*me
= reinterpret_cast<PyUNO
*>( returnCandidate
.get() );
428 TypeDescription
desc( typeName
);
429 OSL_ASSERT( desc
.is() ); // could already instantiate an XInvocation2 !
431 typelib_CompoundTypeDescription
*pCompType
=
432 reinterpret_cast<typelib_CompoundTypeDescription
*>(desc
.get());
433 fillStructState state
;
434 if ( PyTuple_Size( initializer
) > 0 || PyDict_Size( keywordArgs
) > 0 )
435 fillStruct( me
->members
->xInvocation
, pCompType
, initializer
, keywordArgs
, state
, runtime
);
436 if( state
.getCntConsumed() != PyTuple_Size(initializer
) )
438 throw RuntimeException( "pyuno._createUnoStructHelper: too many "
439 "elements in the initializer list, expected " +
440 OUString::number(state
.getCntConsumed()) + ", got " +
441 OUString::number( PyTuple_Size(initializer
) ) );
443 ret
= PyRef( PyTuple_Pack(2, returnCandidate
.get(), state
.getUsed()), SAL_NO_ACQUIRE
);
448 buf
.append( "UNO struct " );
449 buf
.append( PyStr_AsString(structName
) );
450 buf
.append( " is unknown" );
451 PyErr_SetString (PyExc_RuntimeError
, buf
.getStr());
458 "pyuno._createUnoStructHelper: 2nd argument (initializer sequence) is no tuple" );
463 PyErr_SetString (PyExc_AttributeError
, "createUnoStruct: first argument wasn't a string");
468 PyErr_SetString (PyExc_AttributeError
, "pyuno._createUnoStructHelper: expects exactly two non-keyword arguments:\n\tStructure Name\n\tinitialiser tuple; may be the empty tuple");
471 catch( const css::uno::RuntimeException
& e
)
473 raisePyExceptionWithAny( makeAny( e
) );
475 catch( const css::script::CannotConvertException
& e
)
477 raisePyExceptionWithAny( makeAny( e
) );
479 catch( const css::uno::Exception
& e
)
481 raisePyExceptionWithAny( makeAny( e
) );
483 return ret
.getAcquired();
486 static PyObject
*getTypeByName(
487 SAL_UNUSED_PARAMETER PyObject
*, PyObject
*args
)
489 PyObject
* ret
= nullptr;
495 if (PyArg_ParseTuple (args
, "s", &name
))
497 OUString
typeName ( OUString::createFromAscii( name
) );
498 TypeDescription
typeDesc( typeName
);
502 ret
= PyUNO_Type_new(
503 name
, static_cast<css::uno::TypeClass
>(typeDesc
.get()->eTypeClass
), runtime
);
507 OString buf
= OStringLiteral("Type ") + name
+ " is unknown";
508 PyErr_SetString( PyExc_RuntimeError
, buf
.getStr() );
512 catch ( const RuntimeException
& e
)
514 raisePyExceptionWithAny( makeAny( e
) );
519 static PyObject
*getConstantByName(
520 SAL_UNUSED_PARAMETER PyObject
*, PyObject
*args
)
522 PyObject
*ret
= nullptr;
527 if (PyArg_ParseTuple (args
, "s", &name
))
529 OUString
typeName ( OUString::createFromAscii( name
) );
531 css::uno::Reference
< css::reflection::XConstantTypeDescription
> td
;
532 if (!(runtime
.getImpl()->cargo
->xTdMgr
->getByHierarchicalName(
536 throw RuntimeException( "pyuno.getConstantByName: " + typeName
+ "is not a constant" );
538 PyRef constant
= runtime
.any2PyObject( td
->getConstantValue() );
539 ret
= constant
.getAcquired();
542 catch( const NoSuchElementException
& e
)
544 // to the python programmer, this is a runtime exception,
545 // do not support tweakings with the type system
546 RuntimeException
runExc( e
.Message
);
547 raisePyExceptionWithAny( makeAny( runExc
) );
549 catch(const css::script::CannotConvertException
& e
)
551 raisePyExceptionWithAny( makeAny( e
) );
553 catch(const css::lang::IllegalArgumentException
& e
)
555 raisePyExceptionWithAny( makeAny( e
) );
557 catch( const RuntimeException
& e
)
559 raisePyExceptionWithAny( makeAny(e
) );
564 static PyObject
*checkType( SAL_UNUSED_PARAMETER PyObject
*, PyObject
*args
)
566 if( !PyTuple_Check( args
) || PyTuple_Size( args
) != 1 )
568 OString buf
= "pyuno.checkType : expecting one uno.Type argument";
569 PyErr_SetString( PyExc_RuntimeError
, buf
.getStr() );
572 PyObject
*obj
= PyTuple_GetItem( args
, 0 );
578 catch(const RuntimeException
& e
)
580 raisePyExceptionWithAny( makeAny( e
) );
583 Py_INCREF( Py_None
);
587 static PyObject
*checkEnum( SAL_UNUSED_PARAMETER PyObject
*, PyObject
*args
)
589 if( !PyTuple_Check( args
) || PyTuple_Size( args
) != 1 )
591 OString buf
= "pyuno.checkType : expecting one uno.Type argument";
592 PyErr_SetString( PyExc_RuntimeError
, buf
.getStr() );
595 PyObject
*obj
= PyTuple_GetItem( args
, 0 );
601 catch(const RuntimeException
& e
)
603 raisePyExceptionWithAny( makeAny( e
) );
606 Py_INCREF( Py_None
);
610 static PyObject
*getClass( SAL_UNUSED_PARAMETER PyObject
*, PyObject
*args
)
612 PyObject
*obj
= extractOneStringArg( args
, "pyuno.getClass");
619 PyRef ret
= getClass(pyString2ustring(obj
), runtime
);
620 Py_XINCREF( ret
.get() );
623 catch(const RuntimeException
& e
)
625 raisePyExceptionWithAny( makeAny(e
) );
630 static PyObject
*isInterface( SAL_UNUSED_PARAMETER PyObject
*, PyObject
*args
)
633 if( PyTuple_Check( args
) && PyTuple_Size( args
) == 1 )
635 PyObject
*obj
= PyTuple_GetItem( args
, 0 );
637 return PyLong_FromLong( isInterfaceClass( r
, obj
) );
639 return PyLong_FromLong( 0 );
642 static PyObject
* generateUuid(
643 SAL_UNUSED_PARAMETER PyObject
*, SAL_UNUSED_PARAMETER PyObject
* )
645 Sequence
< sal_Int8
> seq( 16 );
646 rtl_createUuid( reinterpret_cast<sal_uInt8
*>(seq
.getArray()) , nullptr , false );
651 ret
= runtime
.any2PyObject( makeAny( seq
) );
653 catch( const RuntimeException
& e
)
655 raisePyExceptionWithAny( makeAny(e
) );
657 return ret
.getAcquired();
660 static PyObject
*systemPathToFileUrl(
661 SAL_UNUSED_PARAMETER PyObject
*, PyObject
* args
)
663 PyObject
*obj
= extractOneStringArg( args
, "pyuno.systemPathToFileUrl" );
667 OUString sysPath
= pyString2ustring( obj
);
669 osl::FileBase::RC e
= osl::FileBase::getFileURLFromSystemPath( sysPath
, url
);
671 if( e
!= osl::FileBase::E_None
)
673 OUString buf
= "Couldn't convert " +
675 " to a file url for reason (" +
676 OUString::number( static_cast<sal_Int32
>(e
) ) +
678 raisePyExceptionWithAny(
679 makeAny( RuntimeException( buf
)));
682 return ustring2PyUnicode( url
).getAcquired();
685 static PyObject
* fileUrlToSystemPath(
686 SAL_UNUSED_PARAMETER PyObject
*, PyObject
* args
)
688 PyObject
*obj
= extractOneStringArg( args
, "pyuno.fileUrlToSystemPath" );
692 OUString url
= pyString2ustring( obj
);
694 osl::FileBase::RC e
= osl::FileBase::getSystemPathFromFileURL( url
, sysPath
);
696 if( e
!= osl::FileBase::E_None
)
698 OUString buf
= "Couldn't convert file url " +
700 " to a system path for reason (" +
701 OUString::number( static_cast<sal_Int32
>(e
) ) +
703 raisePyExceptionWithAny(
704 makeAny( RuntimeException( buf
)));
707 return ustring2PyUnicode( sysPath
).getAcquired();
710 static PyObject
* absolutize( SAL_UNUSED_PARAMETER PyObject
*, PyObject
* args
)
712 if( PyTuple_Check( args
) && PyTuple_Size( args
) == 2 )
714 OUString ouPath
= pyString2ustring( PyTuple_GetItem( args
, 0 ) );
715 OUString ouRel
= pyString2ustring( PyTuple_GetItem( args
, 1 ) );
717 oslFileError e
= osl_getAbsoluteFileURL( ouPath
.pData
, ouRel
.pData
, &(ret
.pData
) );
718 if( e
!= osl_File_E_None
)
721 "Couldn't absolutize " +
726 OUString::number(static_cast<sal_Int32
>(e
) ) +
731 OUStringToOString(buf
,osl_getThreadTextEncoding()).getStr());
734 return ustring2PyUnicode( ret
).getAcquired();
739 static PyObject
* invoke(SAL_UNUSED_PARAMETER PyObject
*, PyObject
*args
)
741 PyObject
*ret
= nullptr;
742 if(PyTuple_Check(args
) && PyTuple_Size(args
) == 3)
744 PyObject
*object
= PyTuple_GetItem(args
, 0);
745 PyObject
*item1
= PyTuple_GetItem(args
, 1);
746 if (PyStr_Check(item1
))
748 const char *name
= PyStr_AsString(item1
);
749 PyObject
*item2
= PyTuple_GetItem(args
, 2);
750 if(PyTuple_Check(item2
))
752 ret
= PyUNO_invoke(object
, name
, item2
);
757 buf
.append("uno.invoke expects a tuple as 3rd argument, got ");
758 buf
.append(PyStr_AsString(PyObject_Str(item2
)));
760 PyExc_RuntimeError
, buf
.makeStringAndClear().getStr());
766 buf
.append("uno.invoke expected a string as 2nd argument, got ");
767 buf
.append(PyStr_AsString(PyObject_Str(item1
)));
769 PyExc_RuntimeError
, buf
.makeStringAndClear().getStr());
774 OString buf
= "uno.invoke expects object, name, (arg1, arg2, ... )\n";
775 PyErr_SetString(PyExc_RuntimeError
, buf
.getStr());
780 static PyObject
*getCurrentContext(
781 SAL_UNUSED_PARAMETER PyObject
*, SAL_UNUSED_PARAMETER PyObject
* )
787 ret
= runtime
.any2PyObject(
788 makeAny( css::uno::getCurrentContext() ) );
790 catch( const css::uno::Exception
& e
)
792 raisePyExceptionWithAny( makeAny( e
) );
794 return ret
.getAcquired();
797 static PyObject
*setCurrentContext(
798 SAL_UNUSED_PARAMETER PyObject
*, SAL_UNUSED_PARAMETER PyObject
* args
)
803 if( PyTuple_Check( args
) && PyTuple_Size( args
) == 1 )
807 Any a
= runtime
.pyObject2Any( PyTuple_GetItem( args
, 0 ) );
809 Reference
< css::uno::XCurrentContext
> context
;
811 if( (a
.hasValue() && (a
>>= context
)) || ! a
.hasValue() )
813 ret
= css::uno::setCurrentContext( context
) ? Py_True
: Py_False
;
818 buf
.append( "uno.setCurrentContext expects an XComponentContext implementation, got " );
820 PyStr_AsString(PyObject_Str(PyTuple_GetItem(args
, 0))));
822 PyExc_RuntimeError
, buf
.makeStringAndClear().getStr() );
827 OString buf
= "uno.setCurrentContext expects exactly one argument (the current Context)\n";
829 PyExc_RuntimeError
, buf
.getStr() );
832 catch( const css::uno::Exception
& e
)
834 raisePyExceptionWithAny( makeAny( e
) );
836 return ret
.getAcquired();
839 static PyObject
*sal_debug(
840 SAL_UNUSED_PARAMETER PyObject
*, SAL_UNUSED_PARAMETER PyObject
* args
)
842 Py_INCREF( Py_None
);
843 if( !PyTuple_Check( args
) || PyTuple_Size( args
) != 1 )
846 OUString line
= pyString2ustring( PyTuple_GetItem( args
, 0 ) );
855 struct PyMethodDef PyUNOModule_methods
[] =
857 {"private_initTestEnvironment", initTestEnvironment
, METH_VARARGS
, nullptr},
858 {"private_deinitTestEnvironment", deinitTestEnvironment
, METH_VARARGS
, nullptr},
859 {"getComponentContext", getComponentContext
, METH_VARARGS
, nullptr},
860 {"_createUnoStructHelper", reinterpret_cast<PyCFunction
>(createUnoStructHelper
), METH_VARARGS
| METH_KEYWORDS
, nullptr},
861 {"getTypeByName", getTypeByName
, METH_VARARGS
, nullptr},
862 {"getConstantByName", getConstantByName
, METH_VARARGS
, nullptr},
863 {"getClass", getClass
, METH_VARARGS
, nullptr},
864 {"checkEnum", checkEnum
, METH_VARARGS
, nullptr},
865 {"checkType", checkType
, METH_VARARGS
, nullptr},
866 {"generateUuid", generateUuid
, METH_VARARGS
, nullptr},
867 {"systemPathToFileUrl", systemPathToFileUrl
, METH_VARARGS
, nullptr},
868 {"fileUrlToSystemPath", fileUrlToSystemPath
, METH_VARARGS
, nullptr},
869 {"absolutize", absolutize
, METH_VARARGS
| METH_KEYWORDS
, nullptr},
870 {"isInterface", isInterface
, METH_VARARGS
, nullptr},
871 {"invoke", invoke
, METH_VARARGS
| METH_KEYWORDS
, nullptr},
872 {"setCurrentContext", setCurrentContext
, METH_VARARGS
, nullptr},
873 {"getCurrentContext", getCurrentContext
, METH_VARARGS
, nullptr},
874 {"sal_debug", sal_debug
, METH_VARARGS
, nullptr},
875 {nullptr, nullptr, 0, nullptr}
881 #if PY_MAJOR_VERSION >= 3
882 PyObject
* PyInit_pyuno()
885 PyUNOStruct_initType();
886 // noop when called already, otherwise needed to allow multiple threads
887 PyEval_InitThreads();
888 static struct PyModuleDef moduledef
=
890 PyModuleDef_HEAD_INIT
,
891 "pyuno", // module name
892 nullptr, // module documentation
893 -1, // module keeps state in global variables,
894 PyUNOModule_methods
, // modules methods
895 nullptr, // m_reload (must be 0)
896 nullptr, // m_traverse
900 return PyModule_Create(&moduledef
);
906 PyUNOStruct_initType();
907 PyEval_InitThreads();
908 Py_InitModule ("pyuno", PyUNOModule_methods
);
910 #endif /* PY_MAJOR_VERSION >= 3 */
912 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */