/* -*- Mode: C++; eval:(c-set-style "bsd"); tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* * This file is part of the LibreOffice project. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. * * This file incorporates work covered by the following license notice: * * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed * with this work for additional information regarding copyright * ownership. The ASF licenses this file to you under the Apache * License, Version 2.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
using com::sun::star::uno::Sequence; using com::sun::star::uno::Reference; using com::sun::star::uno::Any; using com::sun::star::uno::RuntimeException; using com::sun::star::uno::TypeDescription; using com::sun::star::uno::XComponentContext; using com::sun::star::container::NoSuchElementException; using com::sun::star::reflection::XIdlClass; using com::sun::star::script::XInvocation2;
usingnamespace pyuno;
namespace {
/** @ index of the next to be used member in the initializer list !
*/ // LEM TODO: export member names as keyword arguments in initialiser? // Python supports very flexible variadic functions. By marking // variables with one asterisk (e.g. *var) the given variable is // defined to be a tuple of all the extra arguments. By marking // variables with two asterisks (e.g. **var) the given variable is a // dictionary of all extra keyword arguments; the keys are strings, // which are the names that were used to identify the arguments. If // they exist, these arguments must be the last one in the list.
class fillStructState
{ // Keyword arguments used
PyObject *used; // Which structure members are initialised
std::unordered_map <OUString, bool> initialised; // How many positional arguments are consumed // This is always the so-many first ones
sal_Int32 nPosConsumed;
const sal_Int32 nMembers = pCompType->nMembers;
{ for( int i = 0 ; i < nMembers ; i ++ )
{ const OUString OUMemberName (pCompType->ppMemberNames[i]);
PyObject *pyMemberName =
PyUnicode_FromString(OUStringToOString(OUMemberName,
RTL_TEXTENCODING_UTF8).getStr()); if ( PyObject *element = PyDict_GetItem(kwinitializer, pyMemberName ) )
{
state.setInitialised(OUMemberName);
state.setUsed(pyMemberName);
Any a = runtime.pyObject2Any( element, ACCEPT_UNO_ANY );
inv->setValue( OUMemberName, a );
}
}
}
{ constint remainingPosInitialisers = PyTuple_Size(initializer) - state.getCntConsumed(); for( int i = 0 ; i < remainingPosInitialisers && i < nMembers ; i ++ )
{ constint tupleIndex = state.getCntConsumed(); const OUString aMemberName (pCompType->ppMemberNames[i]);
state.setInitialised(aMemberName, tupleIndex);
PyObject *element = PyTuple_GetItem( initializer, tupleIndex );
Any a = runtime.pyObject2Any( element, ACCEPT_UNO_ANY );
inv->setValue( aMemberName, a );
}
} if ( PyTuple_Size( initializer ) <= 0 ) return;
// Allow partial initialisation when only keyword arguments are given for ( int i = 0; i < nMembers ; ++i)
{ const OUString memberName (pCompType->ppMemberNames[i]); if ( ! state.isInitialised( memberName ) )
{
OUString buf = "pyuno._createUnoStructHelper: member '" +
memberName + "' of struct type '" +
OUString::unacquired(&pCompType->aBase.pTypeName) + "' not given a value."; throw RuntimeException(buf);
}
}
}
// workarounds the $(ORIGIN) until it is available if (Module::getUrlFromAddress(reinterpret_cast<oslGenericFunction>(getLibDir), libDir))
{
libDir = libDir.copy(0, libDir.lastIndexOf('/'));
OUString name(u"PYUNOLIBDIR"_ustr);
rtl_bootstrap_set(name.pData, libDir.pData);
} return libDir;
}
// getLibDir() must be called in order to set bootstrap variables correctly ! const OUString& path( getLibDir()); if( Runtime::isInitialized() )
{
Runtime runtime;
ctx = runtime.getImpl()->cargo->xContext;
} else
{ if( path.isEmpty() )
{
PyErr_SetString(
PyExc_RuntimeError, "osl_getUrlFromAddress fails, that's why I cannot find ini " "file for bootstrapping python uno bridge\n" ); return nullptr;
}
OUString iniFile = path + #ifdef MACOSX "/../" LIBO_ETC_FOLDER #endif "/" SAL_CONFIGFILE( "pyuno" );
osl::DirectoryItem item; if( osl::DirectoryItem::get( iniFile, item ) == osl::FileBase::E_None )
{ // in case pyuno.ini exists, use this file for bootstrapping
PyThreadDetach antiguard;
ctx = cppu::defaultBootstrap_InitialComponentContext (iniFile);
} else
{ // defaulting to the standard bootstrapping
PyThreadDetach antiguard;
ctx = cppu::defaultBootstrap_InitialComponentContext ();
}
}
if( ! Runtime::isInitialized() )
{
Runtime::initialize( ctx );
}
Runtime runtime;
ret = runtime.any2PyObject( Any( ctx ) );
} catch (const css::registry::InvalidRegistryException &e)
{ // can't use raisePyExceptionWithAny() here, because the function // does any conversions, which will not work with a // wrongly bootstrapped pyuno!
raisePySystemException( "InvalidRegistryException", e.Message );
} catch(const css::lang::IllegalArgumentException & e)
{
raisePySystemException( "IllegalArgumentException", e.Message );
} catch(const css::script::CannotConvertException & e)
{
raisePySystemException( "CannotConvertException", e.Message );
} catch (const css::uno::RuntimeException & e)
{
raisePySystemException( "RuntimeException", e.Message );
} catch (const css::uno::Exception & e)
{
raisePySystemException( "uno::Exception", e.Message );
} return ret.getAcquired();
}
// While pyuno.private_initTestEnvironment is called from individual Python tests (e.g., from // UnoInProcess in unotest/source/python/org/libreoffice/unotest.py, which makes sure to call it // only once), pyuno.private_deinitTestEnvironment is called centrally from // unotest/source/python/org/libreoffice/unittest.py at the end of every PythonTest (to DeInitVCL // exactly once near the end of the process, if InitVCL has ever been called via // pyuno.private_initTestEnvironment):
osl::Module * testModule = nullptr;
static PyObject* initTestEnvironment(
SAL_UNUSED_PARAMETER PyObject*, SAL_UNUSED_PARAMETER PyObject*)
{ // this tries to bootstrap enough of the soffice from python to run // unit tests, which is only possible indirectly because pyuno is URE // so load "test" library and invoke a function there to do the work
assert(testModule == nullptr); try
{
PyObject *const ctx(getComponentContext(nullptr, nullptr)); if (!ctx) { abort(); }
Runtime const runtime;
Any const a(runtime.pyObject2Any(ctx));
Reference<XComponentContext> xContext;
a >>= xContext; if (!xContext.is()) { abort(); } using css::lang::XMultiServiceFactory;
Reference<XMultiServiceFactory> const xMSF(
xContext->getServiceManager(),
css::uno::UNO_QUERY_THROW); char *const testlib = getenv("TEST_LIB"); if (!testlib) { abort(); } #ifdef _WIN32
OString const libname = OString(testlib, strlen(testlib))
.replaceAll(OString('/'), OString('\\')); #else
OString const libname(testlib, strlen(testlib)); #endif
static PyObject* initTestEnvironmentGPG(
SAL_UNUSED_PARAMETER PyObject*, PyObject* args)
{ // this tries to set up certificate stores for unit tests // which is only possible indirectly because pyuno is URE // so load "unotest" library and invoke a function there to do the work
Runtime const runtime;
osl::Module & rModule(runtime.getImpl()->cargo->unoTestModule);
assert(!rModule.is()); try
{ char *const testlib = getenv("UNOTEST_LIB"); if (!testlib) { abort(); } #ifdef _WIN32
OString const libname = OString(testlib, strlen(testlib))
.replaceAll(OString('/'), OString('\\')); #else
OString const libname(testlib, strlen(testlib)); #endif
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.