2011-08-20 17:14:24 +02:00
|
|
|
/* -*- Mode: C++; eval:(c-set-style "bsd"); tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-07-02 14:56:57 +01:00
|
|
|
/*
|
|
|
|
* 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 .
|
|
|
|
*/
|
2006-06-20 04:04:22 +00:00
|
|
|
|
2013-08-16 11:08:32 +03:00
|
|
|
#include <config_features.h>
|
|
|
|
#include <config_folders.h>
|
|
|
|
|
2006-06-20 04:04:22 +00:00
|
|
|
#include "pyuno_impl.hxx"
|
|
|
|
|
2014-11-14 22:52:35 +01:00
|
|
|
#include <osl/diagnose.h>
|
2003-03-23 11:12:59 +00:00
|
|
|
#include <osl/thread.h>
|
2006-03-22 09:51:22 +00:00
|
|
|
#include <osl/module.h>
|
|
|
|
#include <osl/process.h>
|
2003-03-23 11:12:59 +00:00
|
|
|
#include <rtl/strbuf.hxx>
|
|
|
|
#include <rtl/ustrbuf.hxx>
|
2006-03-22 09:51:22 +00:00
|
|
|
#include <rtl/bootstrap.hxx>
|
2003-03-23 11:12:59 +00:00
|
|
|
|
|
|
|
#include <typelib/typedescription.hxx>
|
|
|
|
|
2012-11-16 13:20:50 +01:00
|
|
|
#include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
|
2003-03-23 11:12:59 +00:00
|
|
|
#include <com/sun/star/beans/XMaterialHolder.hpp>
|
2014-03-07 10:31:07 +01:00
|
|
|
#include <com/sun/star/beans/theIntrospection.hpp>
|
2012-08-21 08:07:58 +02:00
|
|
|
#include <com/sun/star/script/Converter.hpp>
|
2013-05-14 09:52:58 +02:00
|
|
|
#include <com/sun/star/script/InvocationAdapterFactory.hpp>
|
2012-09-04 16:12:17 +02:00
|
|
|
#include <com/sun/star/reflection/theCoreReflection.hpp>
|
2003-03-23 11:12:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
using com::sun::star::uno::Reference;
|
|
|
|
using com::sun::star::uno::XInterface;
|
|
|
|
using com::sun::star::uno::Any;
|
|
|
|
using com::sun::star::uno::TypeDescription;
|
|
|
|
using com::sun::star::uno::Sequence;
|
|
|
|
using com::sun::star::uno::Type;
|
|
|
|
using com::sun::star::uno::UNO_QUERY;
|
2011-08-20 17:14:24 +02:00
|
|
|
using com::sun::star::uno::Exception;
|
2003-03-23 11:12:59 +00:00
|
|
|
using com::sun::star::uno::RuntimeException;
|
|
|
|
using com::sun::star::uno::XComponentContext;
|
2012-11-16 13:20:50 +01:00
|
|
|
using com::sun::star::lang::WrappedTargetRuntimeException;
|
2003-03-23 11:12:59 +00:00
|
|
|
using com::sun::star::lang::XSingleServiceFactory;
|
|
|
|
using com::sun::star::lang::XUnoTunnel;
|
2012-09-04 16:12:17 +02:00
|
|
|
using com::sun::star::reflection::theCoreReflection;
|
2003-03-23 11:12:59 +00:00
|
|
|
using com::sun::star::reflection::XIdlReflection;
|
2012-11-16 13:20:50 +01:00
|
|
|
using com::sun::star::reflection::InvocationTargetException;
|
2012-08-21 08:07:58 +02:00
|
|
|
using com::sun::star::script::Converter;
|
2003-03-23 11:12:59 +00:00
|
|
|
using com::sun::star::script::XTypeConverter;
|
|
|
|
using com::sun::star::script::XInvocationAdapterFactory2;
|
2004-02-02 18:30:44 +00:00
|
|
|
using com::sun::star::script::XInvocation;
|
2003-03-23 11:12:59 +00:00
|
|
|
using com::sun::star::beans::XMaterialHolder;
|
|
|
|
using com::sun::star::beans::XIntrospection;
|
2014-03-07 10:31:07 +01:00
|
|
|
using com::sun::star::beans::theIntrospection;
|
2003-03-23 11:12:59 +00:00
|
|
|
|
2011-02-03 21:35:13 -08:00
|
|
|
#include <vector>
|
|
|
|
|
2003-03-23 11:12:59 +00:00
|
|
|
namespace pyuno
|
|
|
|
{
|
|
|
|
|
|
|
|
static PyTypeObject RuntimeImpl_Type =
|
|
|
|
{
|
2011-05-07 20:35:03 +01:00
|
|
|
PyVarObject_HEAD_INIT (&PyType_Type, 0)
|
2011-08-20 17:33:37 +02:00
|
|
|
"pyuno_runtime",
|
2003-03-23 11:12:59 +00:00
|
|
|
sizeof (RuntimeImpl),
|
|
|
|
0,
|
2015-06-08 16:27:02 +02:00
|
|
|
RuntimeImpl::del,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
2011-05-07 20:35:03 +01:00
|
|
|
0,
|
2015-06-08 16:27:02 +02:00
|
|
|
nullptr,
|
2003-03-23 11:12:59 +00:00
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
2015-06-08 16:27:02 +02:00
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
2006-06-20 04:04:22 +00:00
|
|
|
NULL,
|
|
|
|
0,
|
|
|
|
NULL,
|
2015-06-08 16:27:02 +02:00
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
2006-06-20 04:04:22 +00:00
|
|
|
0,
|
2015-06-08 16:27:02 +02:00
|
|
|
nullptr,
|
|
|
|
nullptr,
|
2006-06-20 04:04:22 +00:00
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
2015-06-08 16:27:02 +02:00
|
|
|
nullptr,
|
|
|
|
nullptr,
|
2006-06-20 04:04:22 +00:00
|
|
|
0,
|
2015-06-08 16:27:02 +02:00
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
2006-06-20 04:04:22 +00:00
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
2015-06-08 16:27:02 +02:00
|
|
|
nullptr
|
2009-03-14 17:15:26 +00:00
|
|
|
#if PY_VERSION_HEX >= 0x02060000
|
|
|
|
, 0
|
|
|
|
#endif
|
2014-05-18 18:10:58 +02:00
|
|
|
#if PY_VERSION_HEX >= 0x03040000
|
|
|
|
, 0
|
|
|
|
#endif
|
2003-03-23 11:12:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*----------------------------------------------------------------------
|
|
|
|
Runtime implementation
|
|
|
|
-----------------------------------------------------------------------*/
|
|
|
|
static void getRuntimeImpl( PyRef & globalDict, PyRef &runtimeImpl )
|
|
|
|
throw ( com::sun::star::uno::RuntimeException )
|
|
|
|
{
|
|
|
|
PyThreadState * state = PyThreadState_Get();
|
|
|
|
if( ! state )
|
|
|
|
{
|
2014-05-23 12:03:21 +02:00
|
|
|
throw RuntimeException( "python global interpreter must be held (thread must be attached)" );
|
2003-03-23 11:12:59 +00:00
|
|
|
}
|
|
|
|
|
2014-01-28 09:22:26 +00:00
|
|
|
PyObject* pModule = PyImport_AddModule("__main__");
|
|
|
|
|
|
|
|
if (!pModule)
|
|
|
|
{
|
2014-05-23 12:03:21 +02:00
|
|
|
throw RuntimeException("can't import __main__ module");
|
2014-01-28 09:22:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
globalDict = PyRef( PyModule_GetDict(pModule));
|
2003-03-23 11:12:59 +00:00
|
|
|
|
|
|
|
if( ! globalDict.is() ) // FATAL !
|
|
|
|
{
|
2014-05-23 12:03:21 +02:00
|
|
|
throw RuntimeException("can't find __main__ module");
|
2003-03-23 11:12:59 +00:00
|
|
|
}
|
|
|
|
runtimeImpl = PyDict_GetItemString( globalDict.get() , "pyuno_runtime" );
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyRef importUnoModule( ) throw ( RuntimeException )
|
|
|
|
{
|
|
|
|
// import the uno module
|
2014-06-03 17:08:06 +01:00
|
|
|
PyRef module( PyImport_ImportModule( "uno" ), SAL_NO_ACQUIRE, NOT_NULL );
|
2003-03-23 11:12:59 +00:00
|
|
|
if( PyErr_Occurred() )
|
|
|
|
{
|
|
|
|
PyRef excType, excValue, excTraceback;
|
2015-01-17 19:00:24 +01:00
|
|
|
PyErr_Fetch( reinterpret_cast<PyObject **>(&excType), reinterpret_cast<PyObject**>(&excValue), reinterpret_cast<PyObject**>(&excTraceback));
|
2011-08-20 17:44:53 +02:00
|
|
|
// As of Python 2.7 this gives a rather non-useful "<traceback object at 0xADDRESS>",
|
|
|
|
// but it is the best we can do in the absence of uno._uno_extract_printable_stacktrace
|
|
|
|
// Who knows, a future Python might print something better.
|
|
|
|
PyRef str( PyObject_Str( excTraceback.get() ), SAL_NO_ACQUIRE );
|
2003-03-23 11:12:59 +00:00
|
|
|
|
|
|
|
OUStringBuffer buf;
|
|
|
|
buf.appendAscii( "python object raised an unknown exception (" );
|
|
|
|
PyRef valueRep( PyObject_Repr( excValue.get() ), SAL_NO_ACQUIRE );
|
2012-11-25 15:59:18 +01:00
|
|
|
buf.appendAscii( PyStr_AsString( valueRep.get())).appendAscii( ", traceback follows\n" );
|
|
|
|
buf.appendAscii( PyStr_AsString( str.get() ) );
|
2011-08-20 18:00:52 +02:00
|
|
|
buf.appendAscii( ")" );
|
2014-05-23 12:03:21 +02:00
|
|
|
throw RuntimeException( buf.makeStringAndClear() );
|
2003-03-23 11:12:59 +00:00
|
|
|
}
|
|
|
|
PyRef dict( PyModule_GetDict( module.get() ) );
|
|
|
|
return dict;
|
|
|
|
}
|
|
|
|
|
2006-03-22 09:51:22 +00:00
|
|
|
static void readLoggingConfig( sal_Int32 *pLevel, FILE **ppFile )
|
|
|
|
{
|
|
|
|
*pLevel = LogLevel::NONE;
|
|
|
|
*ppFile = 0;
|
|
|
|
OUString fileName;
|
2006-06-20 04:04:22 +00:00
|
|
|
osl_getModuleURLFromFunctionAddress(
|
|
|
|
reinterpret_cast< oslGenericFunction >(readLoggingConfig),
|
2015-01-17 19:00:24 +01:00
|
|
|
&fileName.pData );
|
2013-11-04 14:00:40 +02:00
|
|
|
fileName = fileName.copy( fileName.lastIndexOf( '/' )+1 );
|
Make the "Mac-like" or "canonical" app bundle structure always used on OS X
In other words, only executable files go in the MacOS folder. Dynamic
libraries and bundled frameworks (i.e., LibreOfficePython), and
nothing else, go in the Frameworks folder, and all other files go in
the Resources folder.
Especially, note that Java class files and rc (.ini) files also go in
Resources.
Such an app bundle structure is what Apple strongly suggests one
should use, and it has been hinted that future versions of code
signing and/or Gatekeeper will require such a structure.
There is still some ugliness thanks to traces of the historical
separation of URE from "the office". Like there are two separate
"unorc" files, one for URE, one for the LibreOffice application. IMHO,
this should be cleaned up, but is probably controversial.
(Eek! I now see there are actually *three* unorc files in the app
bundle. Not intentional. Need to fix that later.)
Change-Id: Idcf235038deb5b8e1d061734993e9f31869b7606
2014-09-09 10:44:56 +03:00
|
|
|
#ifdef MACOSX
|
2013-08-16 11:08:32 +03:00
|
|
|
fileName += "../" LIBO_ETC_FOLDER "/";
|
|
|
|
#endif
|
2013-11-04 14:00:40 +02:00
|
|
|
fileName += SAL_CONFIGFILE("pyuno" );
|
2006-03-22 09:51:22 +00:00
|
|
|
rtl::Bootstrap bootstrapHandle( fileName );
|
|
|
|
|
|
|
|
OUString str;
|
2013-02-01 09:33:19 +01:00
|
|
|
if( bootstrapHandle.getFrom( "PYUNO_LOGLEVEL", str ) )
|
2006-03-22 09:51:22 +00:00
|
|
|
{
|
2012-04-06 14:28:18 +02:00
|
|
|
if ( str == "NONE" )
|
2006-03-22 09:51:22 +00:00
|
|
|
*pLevel = LogLevel::NONE;
|
2012-04-06 14:28:18 +02:00
|
|
|
else if ( str == "CALL" )
|
2006-03-22 09:51:22 +00:00
|
|
|
*pLevel = LogLevel::CALL;
|
2012-04-06 14:28:18 +02:00
|
|
|
else if ( str == "ARGS" )
|
2006-03-22 09:51:22 +00:00
|
|
|
*pLevel = LogLevel::ARGS;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf( stderr, "unknown loglevel %s\n",
|
|
|
|
OUStringToOString( str, RTL_TEXTENCODING_UTF8 ).getStr() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( *pLevel > LogLevel::NONE )
|
|
|
|
{
|
|
|
|
*ppFile = stdout;
|
2013-02-01 09:33:19 +01:00
|
|
|
if( bootstrapHandle.getFrom( "PYUNO_LOGTARGET", str ) )
|
2006-03-22 09:51:22 +00:00
|
|
|
{
|
2012-04-06 14:28:18 +02:00
|
|
|
if ( str == "stdout" )
|
2006-03-22 09:51:22 +00:00
|
|
|
*ppFile = stdout;
|
2012-04-06 14:28:18 +02:00
|
|
|
else if ( str == "stderr" )
|
2006-03-22 09:51:22 +00:00
|
|
|
*ppFile = stderr;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
oslProcessInfo data;
|
|
|
|
data.Size = sizeof( data );
|
|
|
|
osl_getProcessInfo(
|
|
|
|
0 , osl_Process_IDENTIFIER , &data );
|
|
|
|
osl_getSystemPathFromFileURL( str.pData, &str.pData);
|
|
|
|
OString o = OUStringToOString( str, osl_getThreadTextEncoding() );
|
|
|
|
o += ".";
|
2013-08-21 15:07:31 +02:00
|
|
|
o += OString::number( data.Ident );
|
2006-03-22 09:51:22 +00:00
|
|
|
|
|
|
|
*ppFile = fopen( o.getStr() , "w" );
|
|
|
|
if ( *ppFile )
|
|
|
|
{
|
|
|
|
// do not buffer (useful if e.g. analyzing a crash)
|
|
|
|
setvbuf( *ppFile, 0, _IONBF, 0 );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf( stderr, "couldn't create file %s\n",
|
|
|
|
OUStringToOString( str, RTL_TEXTENCODING_UTF8 ).getStr() );
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-03-23 11:12:59 +00:00
|
|
|
/*-------------------------------------------------------------------
|
|
|
|
RuntimeImpl implementations
|
|
|
|
*-------------------------------------------------------------------*/
|
|
|
|
PyRef stRuntimeImpl::create( const Reference< XComponentContext > &ctx )
|
|
|
|
throw( com::sun::star::uno::RuntimeException )
|
|
|
|
{
|
2006-11-14 17:21:46 +00:00
|
|
|
RuntimeImpl *me = PyObject_New (RuntimeImpl, &RuntimeImpl_Type);
|
2003-03-23 11:12:59 +00:00
|
|
|
if( ! me )
|
2014-05-23 12:03:21 +02:00
|
|
|
throw RuntimeException( "cannot instantiate pyuno::RuntimeImpl" );
|
2003-03-23 11:12:59 +00:00
|
|
|
me->cargo = 0;
|
2006-11-14 17:21:46 +00:00
|
|
|
// must use a different struct here, as the PyObject_New
|
2003-03-23 11:12:59 +00:00
|
|
|
// makes C++ unusable
|
|
|
|
RuntimeCargo *c = new RuntimeCargo();
|
2006-03-22 09:51:22 +00:00
|
|
|
readLoggingConfig( &(c->logLevel) , &(c->logFile) );
|
|
|
|
log( c, LogLevel::CALL, "Instantiating pyuno bridge" );
|
|
|
|
|
2014-01-28 20:00:57 +01:00
|
|
|
c->valid = true;
|
2003-03-23 11:12:59 +00:00
|
|
|
c->xContext = ctx;
|
|
|
|
c->xInvocation = Reference< XSingleServiceFactory > (
|
|
|
|
ctx->getServiceManager()->createInstanceWithContext(
|
2012-06-01 09:15:09 -05:00
|
|
|
OUString( "com.sun.star.script.Invocation" ),
|
2003-03-23 11:12:59 +00:00
|
|
|
ctx ),
|
|
|
|
UNO_QUERY );
|
|
|
|
if( ! c->xInvocation.is() )
|
2014-05-23 12:03:21 +02:00
|
|
|
throw RuntimeException( "pyuno: couldn't instantiate invocation service" );
|
2003-03-23 11:12:59 +00:00
|
|
|
|
2012-08-21 08:07:58 +02:00
|
|
|
c->xTypeConverter = Converter::create(ctx);
|
2003-03-23 11:12:59 +00:00
|
|
|
if( ! c->xTypeConverter.is() )
|
2014-05-23 12:03:21 +02:00
|
|
|
throw RuntimeException( "pyuno: couldn't instantiate typeconverter service" );
|
2003-03-23 11:12:59 +00:00
|
|
|
|
2012-09-04 16:12:17 +02:00
|
|
|
c->xCoreReflection = theCoreReflection::get(ctx);
|
2003-03-23 11:12:59 +00:00
|
|
|
|
2013-05-14 09:52:58 +02:00
|
|
|
c->xAdapterFactory = css::script::InvocationAdapterFactory::create(ctx);
|
2003-03-23 11:12:59 +00:00
|
|
|
|
2014-03-07 10:31:07 +01:00
|
|
|
c->xIntrospection = theIntrospection::get(ctx);
|
2003-03-23 11:12:59 +00:00
|
|
|
|
2013-06-29 21:24:12 +02:00
|
|
|
Any a = ctx->getValueByName("/singletons/com.sun.star.reflection.theTypeDescriptionManager");
|
2003-03-23 11:12:59 +00:00
|
|
|
a >>= c->xTdMgr;
|
|
|
|
if( ! c->xTdMgr.is() )
|
2014-05-23 12:03:21 +02:00
|
|
|
throw RuntimeException( "pyuno: couldn't retrieve typedescriptionmanager" );
|
2003-03-23 11:12:59 +00:00
|
|
|
|
|
|
|
me->cargo =c;
|
|
|
|
return PyRef( reinterpret_cast< PyObject * > ( me ), SAL_NO_ACQUIRE );
|
|
|
|
}
|
|
|
|
|
|
|
|
void stRuntimeImpl::del(PyObject* self)
|
|
|
|
{
|
|
|
|
RuntimeImpl *me = reinterpret_cast< RuntimeImpl * > ( self );
|
2006-03-22 09:51:22 +00:00
|
|
|
if( me->cargo->logFile )
|
|
|
|
fclose( me->cargo->logFile );
|
2003-03-23 11:12:59 +00:00
|
|
|
delete me->cargo;
|
2006-11-14 17:21:46 +00:00
|
|
|
PyObject_Del (self);
|
2003-03-23 11:12:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Runtime::initialize( const Reference< XComponentContext > & ctx )
|
|
|
|
throw ( RuntimeException )
|
|
|
|
{
|
|
|
|
PyRef globalDict, runtime;
|
|
|
|
getRuntimeImpl( globalDict , runtime );
|
|
|
|
RuntimeImpl *impl = reinterpret_cast< RuntimeImpl * > (runtime.get());
|
|
|
|
|
|
|
|
if( runtime.is() && impl->cargo->valid )
|
|
|
|
{
|
2014-05-23 12:03:21 +02:00
|
|
|
throw RuntimeException("pyuno runtime has already been initialized before" );
|
2003-03-23 11:12:59 +00:00
|
|
|
}
|
|
|
|
PyRef keep( RuntimeImpl::create( ctx ) );
|
|
|
|
PyDict_SetItemString( globalDict.get(), "pyuno_runtime" , keep.get() );
|
|
|
|
Py_XINCREF( keep.get() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Runtime::isInitialized() throw ( RuntimeException )
|
|
|
|
{
|
|
|
|
PyRef globalDict, runtime;
|
|
|
|
getRuntimeImpl( globalDict , runtime );
|
|
|
|
RuntimeImpl *impl = reinterpret_cast< RuntimeImpl * > (runtime.get());
|
|
|
|
return runtime.is() && impl->cargo->valid;
|
|
|
|
}
|
|
|
|
|
|
|
|
Runtime::Runtime() throw( RuntimeException )
|
|
|
|
: impl( 0 )
|
|
|
|
{
|
|
|
|
PyRef globalDict, runtime;
|
|
|
|
getRuntimeImpl( globalDict , runtime );
|
|
|
|
if( ! runtime.is() )
|
|
|
|
{
|
|
|
|
throw RuntimeException(
|
2014-05-23 12:03:21 +02:00
|
|
|
"pyuno runtime is not initialized, "
|
|
|
|
"(the pyuno.bootstrap needs to be called before using any uno classes)" );
|
2003-03-23 11:12:59 +00:00
|
|
|
}
|
|
|
|
impl = reinterpret_cast< RuntimeImpl * > (runtime.get());
|
|
|
|
Py_XINCREF( runtime.get() );
|
|
|
|
}
|
|
|
|
|
|
|
|
Runtime::Runtime( const Runtime & r )
|
|
|
|
{
|
|
|
|
impl = r.impl;
|
|
|
|
Py_XINCREF( reinterpret_cast< PyObject * >(impl) );
|
|
|
|
}
|
|
|
|
|
|
|
|
Runtime::~Runtime()
|
|
|
|
{
|
|
|
|
Py_XDECREF( reinterpret_cast< PyObject * >(impl) );
|
|
|
|
}
|
|
|
|
|
|
|
|
Runtime & Runtime::operator = ( const Runtime & r )
|
|
|
|
{
|
|
|
|
PyRef temp( reinterpret_cast< PyObject * >(r.impl) );
|
|
|
|
Py_XINCREF( temp.get() );
|
|
|
|
Py_XDECREF( reinterpret_cast< PyObject * >(impl) );
|
|
|
|
impl = r.impl;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyRef Runtime::any2PyObject (const Any &a ) const
|
|
|
|
throw ( com::sun::star::script::CannotConvertException,
|
|
|
|
com::sun::star::lang::IllegalArgumentException,
|
|
|
|
RuntimeException)
|
|
|
|
{
|
|
|
|
if( ! impl->cargo->valid )
|
|
|
|
{
|
2014-05-23 12:03:21 +02:00
|
|
|
throw RuntimeException("pyuno runtime must be initialized before calling any2PyObject" );
|
2003-03-23 11:12:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (a.getValueTypeClass ())
|
|
|
|
{
|
|
|
|
case typelib_TypeClass_VOID:
|
|
|
|
{
|
|
|
|
Py_INCREF (Py_None);
|
|
|
|
return PyRef(Py_None);
|
|
|
|
}
|
|
|
|
case typelib_TypeClass_CHAR:
|
|
|
|
{
|
2015-03-28 19:05:27 +01:00
|
|
|
sal_Unicode c = *static_cast<sal_Unicode const *>(a.getValue());
|
2003-03-23 11:12:59 +00:00
|
|
|
return PyRef( PyUNO_char_new( c , *this ), SAL_NO_ACQUIRE );
|
|
|
|
}
|
|
|
|
case typelib_TypeClass_BOOLEAN:
|
|
|
|
{
|
2014-05-08 11:43:40 +02:00
|
|
|
bool b;
|
2006-07-19 15:42:27 +00:00
|
|
|
if ((a >>= b) && b)
|
2003-03-23 11:12:59 +00:00
|
|
|
return Py_True;
|
|
|
|
else
|
|
|
|
return Py_False;
|
|
|
|
}
|
|
|
|
case typelib_TypeClass_BYTE:
|
|
|
|
case typelib_TypeClass_SHORT:
|
|
|
|
case typelib_TypeClass_UNSIGNED_SHORT:
|
|
|
|
case typelib_TypeClass_LONG:
|
|
|
|
{
|
2006-11-21 16:39:42 +00:00
|
|
|
sal_Int32 l = 0;
|
2003-03-23 11:12:59 +00:00
|
|
|
a >>= l;
|
2011-05-07 20:35:03 +01:00
|
|
|
return PyRef( PyLong_FromLong (l), SAL_NO_ACQUIRE );
|
2003-03-23 11:12:59 +00:00
|
|
|
}
|
|
|
|
case typelib_TypeClass_UNSIGNED_LONG:
|
|
|
|
{
|
2010-01-23 12:35:22 +00:00
|
|
|
sal_uInt32 l = 0;
|
2003-03-23 11:12:59 +00:00
|
|
|
a >>= l;
|
|
|
|
return PyRef( PyLong_FromUnsignedLong (l), SAL_NO_ACQUIRE );
|
|
|
|
}
|
|
|
|
case typelib_TypeClass_HYPER:
|
|
|
|
{
|
2010-01-23 12:35:22 +00:00
|
|
|
sal_Int64 l = 0;
|
2003-03-23 11:12:59 +00:00
|
|
|
a >>= l;
|
|
|
|
return PyRef( PyLong_FromLongLong (l), SAL_NO_ACQUIRE);
|
|
|
|
}
|
|
|
|
case typelib_TypeClass_UNSIGNED_HYPER:
|
|
|
|
{
|
2010-01-23 12:35:22 +00:00
|
|
|
sal_uInt64 l = 0;
|
2003-03-23 11:12:59 +00:00
|
|
|
a >>= l;
|
|
|
|
return PyRef( PyLong_FromUnsignedLongLong (l), SAL_NO_ACQUIRE);
|
|
|
|
}
|
|
|
|
case typelib_TypeClass_FLOAT:
|
|
|
|
{
|
2010-01-23 12:35:22 +00:00
|
|
|
float f = 0.0;
|
2003-03-23 11:12:59 +00:00
|
|
|
a >>= f;
|
|
|
|
return PyRef(PyFloat_FromDouble (f), SAL_NO_ACQUIRE);
|
|
|
|
}
|
|
|
|
case typelib_TypeClass_DOUBLE:
|
|
|
|
{
|
2010-01-23 12:35:22 +00:00
|
|
|
double d = 0.0;
|
2003-03-23 11:12:59 +00:00
|
|
|
a >>= d;
|
|
|
|
return PyRef( PyFloat_FromDouble (d), SAL_NO_ACQUIRE);
|
|
|
|
}
|
|
|
|
case typelib_TypeClass_STRING:
|
|
|
|
{
|
|
|
|
OUString tmp_ostr;
|
|
|
|
a >>= tmp_ostr;
|
|
|
|
return ustring2PyUnicode( tmp_ostr );
|
|
|
|
}
|
|
|
|
case typelib_TypeClass_TYPE:
|
|
|
|
{
|
|
|
|
Type t;
|
|
|
|
a >>= t;
|
|
|
|
OString o = OUStringToOString( t.getTypeName(), RTL_TEXTENCODING_ASCII_US );
|
|
|
|
return PyRef(
|
|
|
|
PyUNO_Type_new (
|
|
|
|
o.getStr(), (com::sun::star::uno::TypeClass)t.getTypeClass(), *this),
|
|
|
|
SAL_NO_ACQUIRE);
|
|
|
|
}
|
|
|
|
case typelib_TypeClass_ANY:
|
|
|
|
{
|
|
|
|
//I don't think this can happen.
|
|
|
|
Py_INCREF (Py_None);
|
|
|
|
return Py_None;
|
|
|
|
}
|
|
|
|
case typelib_TypeClass_ENUM:
|
|
|
|
{
|
2015-03-28 19:05:27 +01:00
|
|
|
sal_Int32 l = *static_cast<sal_Int32 const *>(a.getValue());
|
2003-03-23 11:12:59 +00:00
|
|
|
TypeDescription desc( a.getValueType() );
|
|
|
|
if( desc.is() )
|
|
|
|
{
|
|
|
|
desc.makeComplete();
|
|
|
|
typelib_EnumTypeDescription *pEnumDesc =
|
2015-01-17 19:00:24 +01:00
|
|
|
reinterpret_cast<typelib_EnumTypeDescription *>(desc.get());
|
2003-03-23 11:12:59 +00:00
|
|
|
for( int i = 0 ; i < pEnumDesc->nEnumValues ; i ++ )
|
|
|
|
{
|
|
|
|
if( pEnumDesc->pEnumValues[i] == l )
|
|
|
|
{
|
|
|
|
OString v = OUStringToOString( pEnumDesc->ppEnumNames[i], RTL_TEXTENCODING_ASCII_US);
|
|
|
|
OString e = OUStringToOString( pEnumDesc->aBase.pTypeName, RTL_TEXTENCODING_ASCII_US);
|
|
|
|
return PyRef( PyUNO_Enum_new(e.getStr(),v.getStr(), *this ), SAL_NO_ACQUIRE );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
OUStringBuffer buf;
|
|
|
|
buf.appendAscii( "Any carries enum " );
|
|
|
|
buf.append( a.getValueType().getTypeName());
|
|
|
|
buf.appendAscii( " with invalid value " ).append( l );
|
2014-05-23 12:03:21 +02:00
|
|
|
throw RuntimeException( buf.makeStringAndClear() );
|
2003-03-23 11:12:59 +00:00
|
|
|
}
|
|
|
|
case typelib_TypeClass_EXCEPTION:
|
|
|
|
case typelib_TypeClass_STRUCT:
|
|
|
|
{
|
|
|
|
PyRef excClass = getClass( a.getValueType().getTypeName(), *this );
|
|
|
|
PyRef value = PyRef( PyUNO_new_UNCHECKED (a, getImpl()->cargo->xInvocation), SAL_NO_ACQUIRE);
|
2014-08-07 12:37:59 +01:00
|
|
|
PyRef argsTuple( PyTuple_New( 1 ) , SAL_NO_ACQUIRE, NOT_NULL );
|
2003-03-23 11:12:59 +00:00
|
|
|
PyTuple_SetItem( argsTuple.get() , 0 , value.getAcquired() );
|
2003-05-24 22:31:19 +00:00
|
|
|
PyRef ret( PyObject_CallObject( excClass.get() , argsTuple.get() ), SAL_NO_ACQUIRE );
|
2003-03-23 11:12:59 +00:00
|
|
|
if( ! ret.is() )
|
|
|
|
{
|
|
|
|
OUStringBuffer buf;
|
2015-06-13 09:12:34 +02:00
|
|
|
buf.appendAscii( "Couldn't instantiate python representation of structured UNO type " );
|
2003-03-23 11:12:59 +00:00
|
|
|
buf.append( a.getValueType().getTypeName() );
|
2014-05-23 12:03:21 +02:00
|
|
|
throw RuntimeException( buf.makeStringAndClear() );
|
2003-03-23 11:12:59 +00:00
|
|
|
}
|
|
|
|
|
2006-06-20 04:04:22 +00:00
|
|
|
if( com::sun::star::uno::TypeClass_EXCEPTION == a.getValueTypeClass() )
|
2003-03-23 11:12:59 +00:00
|
|
|
{
|
|
|
|
// add the message in a standard python way !
|
2014-07-10 11:56:06 +01:00
|
|
|
PyRef args( PyTuple_New( 1 ), SAL_NO_ACQUIRE, NOT_NULL );
|
2003-03-23 11:12:59 +00:00
|
|
|
|
|
|
|
// assuming that the Message is always the first member, wuuuu
|
2015-03-29 09:17:00 +02:00
|
|
|
void const *pData = a.getValue();
|
|
|
|
OUString message = *static_cast<OUString const *>(pData);
|
2003-03-23 11:12:59 +00:00
|
|
|
PyRef pymsg = ustring2PyString( message );
|
|
|
|
PyTuple_SetItem( args.get(), 0 , pymsg.getAcquired() );
|
|
|
|
// the exception base functions want to have an "args" tuple,
|
|
|
|
// which contains the message
|
2011-08-20 17:33:37 +02:00
|
|
|
PyObject_SetAttrString( ret.get(), "args", args.get() );
|
2003-03-23 11:12:59 +00:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
case typelib_TypeClass_SEQUENCE:
|
|
|
|
{
|
|
|
|
Sequence<Any> s;
|
|
|
|
|
|
|
|
Sequence< sal_Int8 > byteSequence;
|
|
|
|
if( a >>= byteSequence )
|
|
|
|
{
|
|
|
|
// byte sequence is treated in a special way because of peformance reasons
|
|
|
|
// @since 0.9.2
|
|
|
|
return PyRef( PyUNO_ByteSequence_new( byteSequence, *this ), SAL_NO_ACQUIRE );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Reference< XTypeConverter > tc = getImpl()->cargo->xTypeConverter;
|
|
|
|
Reference< XSingleServiceFactory > ssf = getImpl()->cargo->xInvocation;
|
2015-04-01 08:40:15 +02:00
|
|
|
tc->convertTo (a, cppu::UnoType<decltype(s)>::get()) >>= s;
|
2014-06-03 17:07:18 +01:00
|
|
|
PyRef tuple( PyTuple_New (s.getLength()), SAL_NO_ACQUIRE, NOT_NULL);
|
2006-09-25 12:22:58 +00:00
|
|
|
int i=0;
|
2003-03-23 11:12:59 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
for ( i = 0; i < s.getLength (); i++)
|
|
|
|
{
|
|
|
|
PyRef element;
|
|
|
|
element = any2PyObject (tc->convertTo (s[i], s[i].getValueType() ));
|
|
|
|
OSL_ASSERT( element.is() );
|
|
|
|
PyTuple_SetItem( tuple.get(), i, element.getAcquired() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch( com::sun::star::uno::Exception & )
|
|
|
|
{
|
|
|
|
for( ; i < s.getLength() ; i ++ )
|
|
|
|
{
|
|
|
|
Py_INCREF( Py_None );
|
|
|
|
PyTuple_SetItem( tuple.get(), i, Py_None );
|
|
|
|
}
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
return tuple;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case typelib_TypeClass_INTERFACE:
|
|
|
|
{
|
2014-08-28 15:11:37 +02:00
|
|
|
// fdo#46678 must unlock GIL because getSomething could acquire locks,
|
|
|
|
// and queryInterface too...
|
2003-03-23 11:12:59 +00:00
|
|
|
{
|
2014-08-28 15:11:37 +02:00
|
|
|
PyThreadDetach d;
|
|
|
|
|
|
|
|
Reference<XUnoTunnel> tunnel;
|
|
|
|
a >>= tunnel;
|
|
|
|
if (tunnel.is())
|
|
|
|
{
|
|
|
|
sal_Int64 that = tunnel->getSomething( ::pyuno::Adapter::getUnoTunnelImplementationId() );
|
|
|
|
if( that )
|
2014-09-26 13:15:08 +02:00
|
|
|
return reinterpret_cast<Adapter*>(that)->getWrappedObject();
|
2014-08-28 15:11:37 +02:00
|
|
|
}
|
2003-03-23 11:12:59 +00:00
|
|
|
}
|
|
|
|
//This is just like the struct case:
|
|
|
|
return PyRef( PyUNO_new (a, getImpl()->cargo->xInvocation), SAL_NO_ACQUIRE );
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
OUStringBuffer buf;
|
2013-11-25 22:05:13 +01:00
|
|
|
buf.appendAscii( "Unknown UNO type class " );
|
2003-03-23 11:12:59 +00:00
|
|
|
buf.append( (sal_Int32 ) a.getValueTypeClass() );
|
2014-05-23 12:03:21 +02:00
|
|
|
throw RuntimeException(buf.makeStringAndClear( ) );
|
2003-03-23 11:12:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static Sequence< Type > invokeGetTypes( const Runtime & r , PyObject * o )
|
|
|
|
{
|
|
|
|
Sequence< Type > ret;
|
|
|
|
|
2011-08-20 17:33:37 +02:00
|
|
|
PyRef method( PyObject_GetAttrString( o , "getTypes" ), SAL_NO_ACQUIRE );
|
2003-03-23 11:12:59 +00:00
|
|
|
raiseInvocationTargetExceptionWhenNeeded( r );
|
|
|
|
if( method.is() && PyCallable_Check( method.get() ) )
|
|
|
|
{
|
|
|
|
PyRef types( PyObject_CallObject( method.get(), 0 ) , SAL_NO_ACQUIRE );
|
|
|
|
raiseInvocationTargetExceptionWhenNeeded( r );
|
|
|
|
if( types.is() && PyTuple_Check( types.get() ) )
|
|
|
|
{
|
|
|
|
int size = PyTuple_Size( types.get() );
|
2004-02-02 18:30:44 +00:00
|
|
|
|
|
|
|
// add the XUnoTunnel interface for uno object identity concept (hack)
|
|
|
|
ret.realloc( size + 1 );
|
2003-03-23 11:12:59 +00:00
|
|
|
for( int i = 0 ; i < size ; i ++ )
|
|
|
|
{
|
|
|
|
Any a = r.pyObject2Any(PyTuple_GetItem(types.get(),i));
|
|
|
|
a >>= ret[i];
|
|
|
|
}
|
2014-05-23 22:11:27 +02:00
|
|
|
ret[size] = cppu::UnoType<com::sun::star::lang::XUnoTunnel>::get();
|
2003-03-23 11:12:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-11-16 13:20:50 +01:00
|
|
|
static OUString
|
|
|
|
lcl_ExceptionMessage(PyObject *const o, OUString const*const pWrapped)
|
|
|
|
{
|
|
|
|
OUStringBuffer buf;
|
|
|
|
buf.appendAscii("Couldn't convert ");
|
|
|
|
PyRef reprString( PyObject_Str(o), SAL_NO_ACQUIRE );
|
2012-11-25 15:59:18 +01:00
|
|
|
buf.appendAscii( PyStr_AsString(reprString.get()) );
|
2012-11-16 13:20:50 +01:00
|
|
|
buf.appendAscii(" to a UNO type");
|
|
|
|
if (pWrapped)
|
|
|
|
{
|
|
|
|
buf.appendAscii("; caught exception: ");
|
|
|
|
buf.append(*pWrapped);
|
|
|
|
}
|
|
|
|
return buf.makeStringAndClear();
|
|
|
|
}
|
|
|
|
|
2003-05-24 22:31:19 +00:00
|
|
|
Any Runtime::pyObject2Any ( const PyRef & source, enum ConversionMode mode ) const
|
2003-03-23 11:12:59 +00:00
|
|
|
throw ( com::sun::star::uno::RuntimeException )
|
|
|
|
{
|
|
|
|
if( ! impl->cargo->valid )
|
|
|
|
{
|
2014-05-23 12:03:21 +02:00
|
|
|
throw RuntimeException("pyuno runtime must be initialized before calling any2PyObject" );
|
2003-03-23 11:12:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Any a;
|
|
|
|
PyObject *o = source.get();
|
|
|
|
if( Py_None == o )
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
2011-05-07 20:35:03 +01:00
|
|
|
// In Python 3, there is no PyInt type.
|
|
|
|
#if PY_MAJOR_VERSION < 3
|
2003-03-23 11:12:59 +00:00
|
|
|
else if (PyInt_Check (o))
|
|
|
|
{
|
|
|
|
if( o == Py_True )
|
|
|
|
{
|
|
|
|
sal_Bool b = sal_True;
|
2015-04-01 08:40:15 +02:00
|
|
|
a = Any( &b, cppu::UnoType<bool>::get() );
|
2003-03-23 11:12:59 +00:00
|
|
|
}
|
|
|
|
else if ( o == Py_False )
|
|
|
|
{
|
|
|
|
sal_Bool b = sal_False;
|
2015-04-01 08:40:15 +02:00
|
|
|
a = Any( &b, cppu::UnoType<bool>::get() );
|
2003-03-23 11:12:59 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-05-07 20:35:03 +01:00
|
|
|
sal_Int32 l = (sal_Int32) PyLong_AsLong( o );
|
2003-03-23 11:12:59 +00:00
|
|
|
if( l < 128 && l >= -128 )
|
|
|
|
{
|
|
|
|
sal_Int8 b = (sal_Int8 ) l;
|
|
|
|
a <<= b;
|
|
|
|
}
|
|
|
|
else if( l <= 0x7fff && l >= -0x8000 )
|
|
|
|
{
|
|
|
|
sal_Int16 s = (sal_Int16) l;
|
|
|
|
a <<= s;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
a <<= l;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-05-07 20:35:03 +01:00
|
|
|
#endif /* PY_MAJOR_VERSION < 3 */
|
2003-03-23 11:12:59 +00:00
|
|
|
else if (PyLong_Check (o))
|
|
|
|
{
|
2011-05-07 20:35:03 +01:00
|
|
|
#if PY_MAJOR_VERSION >= 3
|
|
|
|
// Convert the Python 3 booleans that are actually of type PyLong.
|
|
|
|
if(o == Py_True)
|
|
|
|
{
|
|
|
|
sal_Bool b = sal_True;
|
2015-04-01 08:40:15 +02:00
|
|
|
a = Any(&b, cppu::UnoType<bool>::get());
|
2011-05-07 20:35:03 +01:00
|
|
|
}
|
|
|
|
else if(o == Py_False)
|
|
|
|
{
|
|
|
|
sal_Bool b = sal_False;
|
2015-04-01 08:40:15 +02:00
|
|
|
a = Any(&b, cppu::UnoType<bool>::get());
|
2011-05-07 20:35:03 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#endif /* PY_MAJOR_VERSION >= 3 */
|
2003-03-23 11:12:59 +00:00
|
|
|
sal_Int64 l = (sal_Int64)PyLong_AsLong (o);
|
2003-03-30 12:32:01 +00:00
|
|
|
if( l < 128 && l >= -128 )
|
|
|
|
{
|
|
|
|
sal_Int8 b = (sal_Int8 ) l;
|
|
|
|
a <<= b;
|
|
|
|
}
|
|
|
|
else if( l <= 0x7fff && l >= -0x8000 )
|
|
|
|
{
|
|
|
|
sal_Int16 s = (sal_Int16) l;
|
|
|
|
a <<= s;
|
|
|
|
}
|
2006-06-20 04:04:22 +00:00
|
|
|
else if( l <= SAL_CONST_INT64(0x7fffffff) &&
|
|
|
|
l >= -SAL_CONST_INT64(0x80000000) )
|
2003-03-30 12:32:01 +00:00
|
|
|
{
|
|
|
|
sal_Int32 l32 = (sal_Int32) l;
|
|
|
|
a <<= l32;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
a <<= l;
|
|
|
|
}
|
2011-05-07 20:35:03 +01:00
|
|
|
#if PY_MAJOR_VERSION >= 3
|
|
|
|
}
|
|
|
|
#endif
|
2003-03-23 11:12:59 +00:00
|
|
|
}
|
|
|
|
else if (PyFloat_Check (o))
|
|
|
|
{
|
|
|
|
double d = PyFloat_AsDouble (o);
|
|
|
|
a <<= d;
|
|
|
|
}
|
2012-11-25 15:59:18 +01:00
|
|
|
else if (PyStrBytes_Check(o) || PyUnicode_Check(o))
|
2011-05-07 20:35:03 +01:00
|
|
|
{
|
|
|
|
a <<= pyString2ustring(o);
|
|
|
|
}
|
2003-03-23 11:12:59 +00:00
|
|
|
else if (PyTuple_Check (o))
|
|
|
|
{
|
|
|
|
Sequence<Any> s (PyTuple_Size (o));
|
2015-05-29 14:12:20 +02:00
|
|
|
for (Py_ssize_t i = 0; i < PyTuple_Size (o); i++)
|
2003-03-23 11:12:59 +00:00
|
|
|
{
|
2003-05-24 22:31:19 +00:00
|
|
|
s[i] = pyObject2Any (PyTuple_GetItem (o, i), mode );
|
2003-03-23 11:12:59 +00:00
|
|
|
}
|
|
|
|
a <<= s;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Runtime runtime;
|
2003-03-30 12:32:01 +00:00
|
|
|
// should be removed, in case ByteSequence gets derived from String
|
2003-03-23 11:12:59 +00:00
|
|
|
if( PyObject_IsInstance( o, getByteSequenceClass( runtime ).get() ) )
|
|
|
|
{
|
2011-08-20 17:33:37 +02:00
|
|
|
PyRef str(PyObject_GetAttrString( o , "value" ),SAL_NO_ACQUIRE);
|
2003-03-23 11:12:59 +00:00
|
|
|
Sequence< sal_Int8 > seq;
|
2012-11-25 15:59:18 +01:00
|
|
|
if( PyStrBytes_Check( str.get() ) )
|
2003-03-23 11:12:59 +00:00
|
|
|
{
|
|
|
|
seq = Sequence<sal_Int8 > (
|
2015-01-17 19:00:24 +01:00
|
|
|
reinterpret_cast<sal_Int8*>(PyStrBytes_AsString(str.get())), PyStrBytes_Size(str.get()));
|
2003-03-23 11:12:59 +00:00
|
|
|
}
|
|
|
|
a <<= seq;
|
|
|
|
}
|
2003-03-30 12:32:01 +00:00
|
|
|
else
|
|
|
|
if( PyObject_IsInstance( o, getTypeClass( runtime ).get() ) )
|
2003-03-23 11:12:59 +00:00
|
|
|
{
|
2006-06-20 04:04:22 +00:00
|
|
|
Type t = PyType2Type( o );
|
2003-03-23 11:12:59 +00:00
|
|
|
a <<= t;
|
|
|
|
}
|
|
|
|
else if( PyObject_IsInstance( o, getEnumClass( runtime ).get() ) )
|
|
|
|
{
|
2006-06-20 04:04:22 +00:00
|
|
|
a = PyEnum2Enum( o );
|
2003-03-23 11:12:59 +00:00
|
|
|
}
|
2006-06-20 04:04:22 +00:00
|
|
|
else if( isInstanceOfStructOrException( o ) )
|
2003-03-23 11:12:59 +00:00
|
|
|
{
|
2011-08-20 17:33:37 +02:00
|
|
|
PyRef struc(PyObject_GetAttrString( o , "value" ),SAL_NO_ACQUIRE);
|
2015-01-17 19:00:24 +01:00
|
|
|
PyUNO * obj = reinterpret_cast<PyUNO*>(struc.get());
|
2003-03-23 11:12:59 +00:00
|
|
|
Reference< XMaterialHolder > holder( obj->members->xInvocation, UNO_QUERY );
|
|
|
|
if( holder.is( ) )
|
|
|
|
a = holder->getMaterial();
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw RuntimeException(
|
2014-05-23 12:03:21 +02:00
|
|
|
"struct or exception wrapper does not support XMaterialHolder" );
|
2003-03-23 11:12:59 +00:00
|
|
|
}
|
|
|
|
}
|
2012-01-21 15:21:16 +01:00
|
|
|
else if( PyObject_IsInstance( o, getPyUnoClass().get() ) )
|
2003-03-23 11:12:59 +00:00
|
|
|
{
|
|
|
|
PyUNO* o_pi;
|
2015-01-17 19:00:24 +01:00
|
|
|
o_pi = reinterpret_cast<PyUNO*>(o);
|
2003-03-23 11:12:59 +00:00
|
|
|
if (o_pi->members->wrappedObject.getValueTypeClass () ==
|
|
|
|
com::sun::star::uno::TypeClass_STRUCT ||
|
|
|
|
o_pi->members->wrappedObject.getValueTypeClass () ==
|
|
|
|
com::sun::star::uno::TypeClass_EXCEPTION)
|
|
|
|
{
|
|
|
|
Reference<XMaterialHolder> my_mh (o_pi->members->xInvocation, UNO_QUERY);
|
|
|
|
|
|
|
|
if (!my_mh.is ())
|
|
|
|
{
|
|
|
|
throw RuntimeException(
|
2014-05-23 12:03:21 +02:00
|
|
|
"struct wrapper does not support XMaterialHolder" );
|
2003-03-23 11:12:59 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
a = my_mh->getMaterial ();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
a = o_pi->members->wrappedObject;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( PyObject_IsInstance( o, getCharClass( runtime ).get() ) )
|
|
|
|
{
|
2006-06-20 04:04:22 +00:00
|
|
|
sal_Unicode c = PyChar2Unicode( o );
|
2015-04-01 08:40:15 +02:00
|
|
|
a.setValue( &c, cppu::UnoType<cppu::UnoCharType>::get());
|
2003-03-23 11:12:59 +00:00
|
|
|
}
|
2003-05-24 22:31:19 +00:00
|
|
|
else if( PyObject_IsInstance( o, getAnyClass( runtime ).get() ) )
|
|
|
|
{
|
|
|
|
if( ACCEPT_UNO_ANY == mode )
|
|
|
|
{
|
2011-08-20 17:33:37 +02:00
|
|
|
a = pyObject2Any( PyRef( PyObject_GetAttrString( o , "value" ), SAL_NO_ACQUIRE) );
|
2003-05-24 22:31:19 +00:00
|
|
|
Type t;
|
2011-08-20 17:33:37 +02:00
|
|
|
pyObject2Any( PyRef( PyObject_GetAttrString( o, "type" ), SAL_NO_ACQUIRE ) ) >>= t;
|
2003-05-24 22:31:19 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
a = getImpl()->cargo->xTypeConverter->convertTo( a, t );
|
|
|
|
}
|
2011-12-21 21:57:16 +09:00
|
|
|
catch( const com::sun::star::uno::Exception & e )
|
2003-05-24 22:31:19 +00:00
|
|
|
{
|
2012-11-16 13:20:50 +01:00
|
|
|
throw WrappedTargetRuntimeException(
|
|
|
|
e.Message, e.Context, makeAny(e));
|
2003-05-24 22:31:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw RuntimeException(
|
2014-05-23 12:03:21 +02:00
|
|
|
"uno.Any instance not accepted during method call, "
|
|
|
|
"use uno.invoke instead" );
|
2003-05-24 22:31:19 +00:00
|
|
|
}
|
|
|
|
}
|
2003-03-23 11:12:59 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
Reference< XInterface > mappedObject;
|
2004-02-02 18:30:44 +00:00
|
|
|
Reference< XInvocation > adapterObject;
|
|
|
|
|
|
|
|
// instance already mapped out to the world ?
|
|
|
|
PyRef2Adapter::iterator ii = impl->cargo->mappedObjects.find( PyRef( o ) );
|
|
|
|
if( ii != impl->cargo->mappedObjects.end() )
|
|
|
|
{
|
|
|
|
adapterObject = ii->second;
|
|
|
|
}
|
2003-03-23 11:12:59 +00:00
|
|
|
|
2004-02-02 18:30:44 +00:00
|
|
|
if( adapterObject.is() )
|
|
|
|
{
|
|
|
|
// object got already bridged !
|
|
|
|
Reference< com::sun::star::lang::XUnoTunnel > tunnel( adapterObject, UNO_QUERY );
|
|
|
|
|
2014-09-26 13:15:08 +02:00
|
|
|
Adapter *pAdapter = reinterpret_cast<Adapter*>(
|
2006-06-20 04:04:22 +00:00
|
|
|
tunnel->getSomething(
|
|
|
|
::pyuno::Adapter::getUnoTunnelImplementationId() ) );
|
2004-02-02 18:30:44 +00:00
|
|
|
|
|
|
|
mappedObject = impl->cargo->xAdapterFactory->createAdapter(
|
|
|
|
adapterObject, pAdapter->getWrappedTypes() );
|
|
|
|
}
|
|
|
|
else
|
2003-03-23 11:12:59 +00:00
|
|
|
{
|
2012-11-16 13:20:50 +01:00
|
|
|
try {
|
|
|
|
Sequence<Type> interfaces = invokeGetTypes(*this, o);
|
|
|
|
if (interfaces.getLength())
|
|
|
|
{
|
|
|
|
Adapter *pAdapter = new Adapter( o, interfaces );
|
|
|
|
mappedObject =
|
|
|
|
getImpl()->cargo->xAdapterFactory->createAdapter(
|
|
|
|
pAdapter, interfaces );
|
|
|
|
|
|
|
|
// keep a list of exported objects to ensure object identity !
|
|
|
|
impl->cargo->mappedObjects[ PyRef(o) ] =
|
|
|
|
com::sun::star::uno::WeakReference< XInvocation > ( pAdapter );
|
|
|
|
}
|
|
|
|
} catch (InvocationTargetException const& e) {
|
|
|
|
OUString const msg(lcl_ExceptionMessage(o, &e.Message));
|
|
|
|
throw WrappedTargetRuntimeException( // re-wrap that
|
|
|
|
msg, e.Context, e.TargetException);
|
2003-03-23 11:12:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if( mappedObject.is() )
|
|
|
|
{
|
|
|
|
a = com::sun::star::uno::makeAny( mappedObject );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-16 13:20:50 +01:00
|
|
|
OUString const msg(lcl_ExceptionMessage(o, 0));
|
2014-05-23 12:03:21 +02:00
|
|
|
throw RuntimeException(msg);
|
2003-03-23 11:12:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2003-12-17 17:46:50 +00:00
|
|
|
Any Runtime::extractUnoException( const PyRef & excType, const PyRef &excValue, const PyRef &excTraceback) const
|
|
|
|
{
|
2011-08-20 17:14:24 +02:00
|
|
|
OUString str;
|
2003-12-17 17:46:50 +00:00
|
|
|
Any ret;
|
|
|
|
if( excTraceback.is() )
|
|
|
|
{
|
2011-08-20 17:14:24 +02:00
|
|
|
Exception e;
|
|
|
|
PyRef unoModule;
|
|
|
|
if ( impl )
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
unoModule = impl->cargo->getUnoModule();
|
|
|
|
}
|
2011-12-21 21:57:16 +09:00
|
|
|
catch (const Exception &ei)
|
2011-08-20 17:14:24 +02:00
|
|
|
{
|
|
|
|
e=ei;
|
|
|
|
}
|
|
|
|
}
|
2003-12-17 17:46:50 +00:00
|
|
|
if( unoModule.is() )
|
|
|
|
{
|
|
|
|
PyRef extractTraceback(
|
|
|
|
PyDict_GetItemString(unoModule.get(),"_uno_extract_printable_stacktrace" ) );
|
|
|
|
|
2011-08-20 17:14:24 +02:00
|
|
|
if( PyCallable_Check(extractTraceback.get()) )
|
2003-12-17 17:46:50 +00:00
|
|
|
{
|
2014-05-26 15:02:11 +01:00
|
|
|
PyRef args( PyTuple_New( 1), SAL_NO_ACQUIRE, NOT_NULL );
|
2003-12-17 17:46:50 +00:00
|
|
|
PyTuple_SetItem( args.get(), 0, excTraceback.getAcquired() );
|
2011-08-20 17:14:24 +02:00
|
|
|
PyRef pyStr( PyObject_CallObject( extractTraceback.get(),args.get() ), SAL_NO_ACQUIRE);
|
2013-04-07 12:06:47 +02:00
|
|
|
str = OUString::createFromAscii( PyStr_AsString(pyStr.get()) );
|
2003-12-17 17:46:50 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-11-04 14:00:40 +02:00
|
|
|
str = "Couldn't find uno._uno_extract_printable_stacktrace";
|
2003-12-17 17:46:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-11-04 14:00:40 +02:00
|
|
|
str = "Could not load uno.py, no stacktrace available";
|
2012-01-02 15:58:10 -02:00
|
|
|
if ( !e.Message.isEmpty() )
|
2011-08-20 17:14:24 +02:00
|
|
|
{
|
2012-06-01 09:15:09 -05:00
|
|
|
str += OUString (" (Error loading uno.py: ");
|
2011-08-20 17:14:24 +02:00
|
|
|
str += e.Message;
|
2012-06-01 09:15:09 -05:00
|
|
|
str += OUString (")");
|
2011-08-20 17:14:24 +02:00
|
|
|
}
|
2003-12-17 17:46:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// it may occur, that no traceback is given (e.g. only native code below)
|
2013-11-04 14:00:40 +02:00
|
|
|
str = "no traceback available";
|
2003-12-17 17:46:50 +00:00
|
|
|
}
|
|
|
|
|
2006-06-20 04:04:22 +00:00
|
|
|
if( isInstanceOfStructOrException( excValue.get() ) )
|
2003-12-17 17:46:50 +00:00
|
|
|
{
|
|
|
|
ret = pyObject2Any( excValue );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
OUStringBuffer buf;
|
|
|
|
PyRef typeName( PyObject_Str( excType.get() ), SAL_NO_ACQUIRE );
|
|
|
|
if( typeName.is() )
|
|
|
|
{
|
2012-11-25 15:59:18 +01:00
|
|
|
buf.appendAscii( PyStr_AsString( typeName.get() ) );
|
2003-12-17 17:46:50 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
buf.appendAscii( "no typename available" );
|
|
|
|
}
|
|
|
|
buf.appendAscii( ": " );
|
|
|
|
PyRef valueRep( PyObject_Str( excValue.get() ), SAL_NO_ACQUIRE );
|
|
|
|
if( valueRep.is() )
|
|
|
|
{
|
2012-11-25 15:59:18 +01:00
|
|
|
buf.appendAscii( PyStr_AsString( valueRep.get()));
|
2003-12-17 17:46:50 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
buf.appendAscii( "Couldn't convert exception value to a string" );
|
|
|
|
}
|
|
|
|
buf.appendAscii( ", traceback follows\n" );
|
2012-01-02 15:58:10 -02:00
|
|
|
if( !str.isEmpty() )
|
2003-12-17 17:46:50 +00:00
|
|
|
{
|
2011-08-20 17:14:24 +02:00
|
|
|
buf.append( str );
|
|
|
|
buf.appendAscii( "\n" );
|
2003-12-17 17:46:50 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
buf.appendAscii( ", no traceback available\n" );
|
|
|
|
}
|
|
|
|
RuntimeException e;
|
|
|
|
e.Message = buf.makeStringAndClear();
|
2011-12-05 19:44:49 +00:00
|
|
|
#if OSL_DEBUG_LEVEL > 0
|
|
|
|
fprintf( stderr, "Python exception: %s\n",
|
2013-04-07 12:06:47 +02:00
|
|
|
OUStringToOString(e.Message, RTL_TEXTENCODING_UTF8).getStr() );
|
2011-12-05 19:44:49 +00:00
|
|
|
#endif
|
2003-12-17 17:46:50 +00:00
|
|
|
ret = com::sun::star::uno::makeAny( e );
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2003-03-23 11:12:59 +00:00
|
|
|
|
|
|
|
PyThreadAttach::PyThreadAttach( PyInterpreterState *interp)
|
|
|
|
throw ( com::sun::star::uno::RuntimeException )
|
|
|
|
{
|
|
|
|
tstate = PyThreadState_New( interp );
|
|
|
|
if( !tstate )
|
2014-05-23 12:03:21 +02:00
|
|
|
throw RuntimeException( "Couldn't create a pythreadstate" );
|
2003-03-23 11:12:59 +00:00
|
|
|
PyEval_AcquireThread( tstate);
|
|
|
|
}
|
|
|
|
|
|
|
|
PyThreadAttach::~PyThreadAttach()
|
|
|
|
{
|
|
|
|
PyThreadState_Clear( tstate );
|
|
|
|
PyEval_ReleaseThread( tstate );
|
|
|
|
PyThreadState_Delete( tstate );
|
|
|
|
}
|
|
|
|
|
|
|
|
PyThreadDetach::PyThreadDetach() throw ( com::sun::star::uno::RuntimeException )
|
|
|
|
{
|
|
|
|
tstate = PyThreadState_Get();
|
|
|
|
PyEval_ReleaseThread( tstate );
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Acquires the global interpreter lock again
|
|
|
|
|
|
|
|
*/
|
|
|
|
PyThreadDetach::~PyThreadDetach()
|
|
|
|
{
|
|
|
|
PyEval_AcquireThread( tstate );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PyRef RuntimeCargo::getUnoModule()
|
|
|
|
{
|
|
|
|
if( ! dictUnoModule.is() )
|
|
|
|
{
|
|
|
|
dictUnoModule = importUnoModule();
|
|
|
|
}
|
|
|
|
return dictUnoModule;
|
|
|
|
}
|
|
|
|
}
|
2010-10-14 08:30:07 +02:00
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|