Files
libreoffice/pyuno/source/module/pyuno_runtime.cxx

1039 lines
31 KiB
C++
Raw Normal View History

/* -*- 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 .
*/
#include <config_features.h>
#include <config_folders.h>
#include "pyuno_impl.hxx"
#include <o3tl/any.hxx>
#include <osl/diagnose.h>
#include <osl/thread.h>
#include <osl/module.h>
#include <osl/process.h>
#include <rtl/strbuf.hxx>
#include <rtl/ustrbuf.hxx>
#include <rtl/bootstrap.hxx>
Make PyUNO provide more Pythonic behaviour - Simplifies working with UNO objects by giving the behaviour of Python lists, dicts and iterators to objects which implement UNO container interfaces - Applies a custom behaviour to allow objects which implement com::sun::star::table::XCellRange to yield cells and cell ranges by subscript - When UNO container objects are addressed in the new style, eliminates the requirement to manually construct Any objects for contained elements which are typed sequences - Allows lists and iterators to be passed wherever a UNO method accepts a sequence - Relaxes the requirements for initialising UNO structs to allow some members to be skipped when all initialisers are passed by name 1. Collection interfaces ======================== Objects which implement core UNO collection interfaces are made to behave in a way that is more natural for Python code. com::sun::star::container::XIndexAccess com::sun::star::container::XIndexReplace com::sun::star::container::XIndexContainer - Objects provide Python list access semantics num = len(obj) # Number of elements val = obj[0] # Access by index val1,val2 = obj[2:4] # Access by slice val1,val2 = obj[0:3:2] # Access by extended slice if val in obj: ... # Test value presence for val in obj: ... # Implicit iterator (values) itr = iter(obj) # Named iterator (values) obj[0] = val # Replace by index obj[2:4] = val1,val2 # Replace by slice obj[0:3:2] = val1,val2 # Replace by extended slice obj[2:3] = val1,val2 # Insert/replace by slice obj[2:2] = (val,) # Insert by slice obj[2:4] = (val,) # Replace/delete by slice obj[2:3] = () # Delete by slice (implicit) del obj[0] # Delete by index del obj[2:4] # Delete by slice com::sun::star::container::XNameAccess com::sun::star::container::XNameReplace com::sun::star::container::XNameContainer - Objects provide Python dict access semantics num = len(obj) # Number of keys val = obj[key] # Access by key if key in obj: ... # Test key presence for key in obj: ... # Implicit iterator (keys) itr = iter(obj) # Named iterator (keys) obj[key] = val # Replace by key obj[key] = val # Insert by key del obj[key] # Delete by key com::sun::star::container::XEnumerationAccess - Objects provide Python iterable semantics for val in obj: ... # Implicit iterator itr = iter(obj) # Named iterator com::sun::star::container::XEnumeration - Objects provide Python iterator semantics for val in itr: ... # Iteration of named iterator if val in itr: ... # Test value presence Objects which implement both XIndex* and XName* are supported, and respond to both integer and string keys. However, iterating over such an object will return the keys (like a Python dict) rather than the values (like a Python list). 2. Cell ranges ============== A custom behaviour is applied to objects which implement com::sun::star::table::XCellRange to allow their cells and cell ranges to be addressed by subscript, in the style of a Python list or dict (read-only). This is applicable to Calc spreadsheet sheets, Writer text tables and cell ranges created upon these. cell = cellrange[0,0] # Access cell by indices rng = cellrange[0,1:2] # Access cell range by index,slice rng = cellrange[1:2,0] # Access cell range by slice,index rng = cellrange[0:1,2:3] # Access cell range by slices rng = cellrange['A1:B2'] # Access cell range by descriptor rng = cellrange['Name'] # Access cell range by name Note that the indices used are in Python/C order, and differ from the arguments to methods provided by XCellRange. - The statement cellrange[r,c], which returns the cell from row r and column c, is equivalent to calling XCellRange::getCellByPosition(c,r) - The statement cellrange[t:b,l:r], which returns a cell range covering rows t to b(non-inclusive) and columns l to r(non- inclusive), is equivalent to calling XCellRange::getCellRangeByPosition(l,t,r-1,b-1). In contrast to the handling of objects implementing XIndex*, extended slice syntax is not supported. Negative indices (from-end addresses) are supported only for objects which also implement com::sun::star::table::XColumnRowRange (currently Calc spreadsheet sheets and cell ranges created upon these). For such objects, the following syntax is also available: rng = cellrange[0] # Access cell range by row index rng = cellrange[0,:] # Access cell range by row index rng = cellrange[:,0] # Access cell range by column index 3. Elimination of explicit Any ============================== PyUNO has not previously been able to cope with certain method arguments which are typed as Any but require a sequence of specific type to be passed. This is a particular issue for container interfaces such as XIndexContainer and XNameContainer. The existing solution to dealing with such methods is to use a special method to pass an explicitly typed Any, giving code such as: index = doc.createInstance("com.sun.star.text.ContentIndex"); ... uno.invoke( index.LevelParagraphStyles , "replaceByIndex", (0, uno.Any("[]string", ('Caption',))) ) The new Pythonic container access is able to correctly infer the expected type of the sequences required by these arguments. In the new style, the above call to .replaceByIndex() can instead be written: index.LevelParagraphStyles[0] = ('Caption',) 4. List and iterator arguments ============================== Wherever a UNO API expects a sequence, a Python list or iterator can now be passed. This enables the use of list comprehensions and generator expressions for method calls and property assignments. Example: tbl = doc.createInstance('com.sun.star.text.TextTable') tbl.initialize(10,10) # ... insert table ... # Assign numbers 0..99 to the cells using a generator expression tbl.Data = ((y for y in range(10*x,10*x + 10)) for x in range(10)) 5. Tolerant struct initialisation ================================= Previously, a UNO struct could be created fully uninitialised, or by passing a combination of positional and/or named arguments to its constructor. However, if any arguments were passed, all members were required to be initialised or an exception was thrown. This requirement is relaxed such that when all arguments passed to a struct constructor are by name, some may be omitted. The existing requirement that all members must be explicitly initialised when some constructor arguments are unnamed (positional) is not affected. Example: from com.sun.star.beans import PropertyValue prop = PropertyValue(Name='foo', Value='bar') Change-Id: Id29bff10a18099b1a00af1abee1a6c1bc58b3978 Reviewed-on: https://gerrit.libreoffice.org/16272 Tested-by: Jenkins <ci@libreoffice.org> Reviewed-by: Matthew Francis <mjay.francis@gmail.com>
2015-06-01 18:34:04 +08:00
#include <list>
#include <typelib/typedescription.hxx>
#include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
#include <com/sun/star/beans/XMaterialHolder.hpp>
#include <com/sun/star/beans/theIntrospection.hpp>
#include <com/sun/star/script/Converter.hpp>
#include <com/sun/star/script/InvocationAdapterFactory.hpp>
#include <com/sun/star/reflection/theCoreReflection.hpp>
#include <comphelper/sequence.hxx>
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;
using com::sun::star::uno::Exception;
using com::sun::star::uno::RuntimeException;
using com::sun::star::uno::XComponentContext;
using com::sun::star::lang::WrappedTargetRuntimeException;
using com::sun::star::lang::XSingleServiceFactory;
using com::sun::star::lang::XUnoTunnel;
using com::sun::star::reflection::theCoreReflection;
using com::sun::star::reflection::InvocationTargetException;
using com::sun::star::script::Converter;
using com::sun::star::script::XTypeConverter;
using com::sun::star::script::XInvocation;
using com::sun::star::beans::XMaterialHolder;
using com::sun::star::beans::theIntrospection;
#include <vector>
namespace pyuno
{
static PyTypeObject RuntimeImpl_Type =
{
2011-05-07 20:35:03 +01:00
PyVarObject_HEAD_INIT (&PyType_Type, 0)
"pyuno_runtime",
sizeof (RuntimeImpl),
0,
RuntimeImpl::del,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
0,
nullptr,
nullptr,
nullptr,
nullptr,
0,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
0,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr
#if PY_VERSION_HEX >= 0x02060000
, 0
#endif
#if PY_VERSION_HEX >= 0x03040000
, nullptr
#endif
};
/*----------------------------------------------------------------------
Runtime implementation
-----------------------------------------------------------------------*/
static void getRuntimeImpl( PyRef & globalDict, PyRef &runtimeImpl )
throw ( css::uno::RuntimeException )
{
PyThreadState * state = PyThreadState_Get();
if( ! state )
{
throw RuntimeException( "python global interpreter must be held (thread must be attached)" );
}
PyObject* pModule = PyImport_AddModule("__main__");
if (!pModule)
{
throw RuntimeException("can't import __main__ module");
}
globalDict = PyRef( PyModule_GetDict(pModule));
if( ! globalDict.is() ) // FATAL !
{
throw RuntimeException("can't find __main__ module");
}
runtimeImpl = PyDict_GetItemString( globalDict.get() , "pyuno_runtime" );
}
static PyRef importUnoModule( ) throw ( RuntimeException )
{
// import the uno module
PyRef module( PyImport_ImportModule( "uno" ), SAL_NO_ACQUIRE, NOT_NULL );
if( PyErr_Occurred() )
{
PyRef excType, excValue, excTraceback;
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 );
OUStringBuffer buf;
buf.append( "python object raised an unknown exception (" );
PyRef valueRep( PyObject_Repr( excValue.get() ), SAL_NO_ACQUIRE );
buf.appendAscii( PyStr_AsString( valueRep.get())).append( ", traceback follows\n" );
buf.appendAscii( PyStr_AsString( str.get() ) );
buf.append( ")" );
throw RuntimeException( buf.makeStringAndClear() );
}
PyRef dict( PyModule_GetDict( module.get() ) );
return dict;
}
static void readLoggingConfig( sal_Int32 *pLevel, FILE **ppFile )
{
*pLevel = LogLevel::NONE;
*ppFile = nullptr;
OUString fileName;
osl_getModuleURLFromFunctionAddress(
reinterpret_cast< oslGenericFunction >(readLoggingConfig),
&fileName.pData );
fileName = fileName.copy( fileName.lastIndexOf( '/' )+1 );
#ifdef MACOSX
fileName += "../" LIBO_ETC_FOLDER "/";
#endif
fileName += SAL_CONFIGFILE("pyuno" );
rtl::Bootstrap bootstrapHandle( fileName );
OUString str;
if( bootstrapHandle.getFrom( "PYUNO_LOGLEVEL", str ) )
{
if ( str == "NONE" )
*pLevel = LogLevel::NONE;
else if ( str == "CALL" )
*pLevel = LogLevel::CALL;
else if ( str == "ARGS" )
*pLevel = LogLevel::ARGS;
else
{
fprintf( stderr, "unknown loglevel %s\n",
OUStringToOString( str, RTL_TEXTENCODING_UTF8 ).getStr() );
}
}
if( *pLevel > LogLevel::NONE )
{
*ppFile = stdout;
if( bootstrapHandle.getFrom( "PYUNO_LOGTARGET", str ) )
{
if ( str == "stdout" )
*ppFile = stdout;
else if ( str == "stderr" )
*ppFile = stderr;
else
{
oslProcessInfo data;
data.Size = sizeof( data );
osl_getProcessInfo(
nullptr , osl_Process_IDENTIFIER , &data );
osl_getSystemPathFromFileURL( str.pData, &str.pData);
OString o = OUStringToOString( str, osl_getThreadTextEncoding() );
o += ".";
o += OString::number( data.Ident );
*ppFile = fopen( o.getStr() , "w" );
if ( *ppFile )
{
// do not buffer (useful if e.g. analyzing a crash)
setvbuf( *ppFile, nullptr, _IONBF, 0 );
}
else
{
fprintf( stderr, "couldn't create file %s\n",
OUStringToOString( str, RTL_TEXTENCODING_UTF8 ).getStr() );
}
}
}
}
}
/*-------------------------------------------------------------------
RuntimeImpl implementations
*-------------------------------------------------------------------*/
PyRef stRuntimeImpl::create( const Reference< XComponentContext > &ctx )
throw( css::uno::RuntimeException, std::exception )
{
RuntimeImpl *me = PyObject_New (RuntimeImpl, &RuntimeImpl_Type);
if( ! me )
throw RuntimeException( "cannot instantiate pyuno::RuntimeImpl" );
me->cargo = nullptr;
// must use a different struct here, as the PyObject_New
// makes C++ unusable
RuntimeCargo *c = new RuntimeCargo();
readLoggingConfig( &(c->logLevel) , &(c->logFile) );
log( c, LogLevel::CALL, "Instantiating pyuno bridge" );
c->valid = true;
c->xContext = ctx;
c->xInvocation = Reference< XSingleServiceFactory > (
ctx->getServiceManager()->createInstanceWithContext(
"com.sun.star.script.Invocation",
ctx ),
UNO_QUERY );
if( ! c->xInvocation.is() )
throw RuntimeException( "pyuno: couldn't instantiate invocation service" );
c->xTypeConverter = Converter::create(ctx);
if( ! c->xTypeConverter.is() )
throw RuntimeException( "pyuno: couldn't instantiate typeconverter service" );
c->xCoreReflection = theCoreReflection::get(ctx);
c->xAdapterFactory = css::script::InvocationAdapterFactory::create(ctx);
c->xIntrospection = theIntrospection::get(ctx);
Any a = ctx->getValueByName("/singletons/com.sun.star.reflection.theTypeDescriptionManager");
a >>= c->xTdMgr;
if( ! c->xTdMgr.is() )
throw RuntimeException( "pyuno: couldn't retrieve typedescriptionmanager" );
me->cargo =c;
return PyRef( reinterpret_cast< PyObject * > ( me ), SAL_NO_ACQUIRE );
}
void stRuntimeImpl::del(PyObject* self)
{
RuntimeImpl *me = reinterpret_cast< RuntimeImpl * > ( self );
if( me->cargo->logFile )
fclose( me->cargo->logFile );
delete me->cargo;
PyObject_Del (self);
}
void Runtime::initialize( const Reference< XComponentContext > & ctx )
throw ( RuntimeException, std::exception )
{
PyRef globalDict, runtime;
getRuntimeImpl( globalDict , runtime );
RuntimeImpl *impl = reinterpret_cast< RuntimeImpl * > (runtime.get());
if( runtime.is() && impl->cargo->valid )
{
throw RuntimeException("pyuno runtime has already been initialized before" );
}
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( nullptr )
{
PyRef globalDict, runtime;
getRuntimeImpl( globalDict , runtime );
if( ! runtime.is() )
{
throw RuntimeException(
"pyuno runtime is not initialized, "
"(the pyuno.bootstrap needs to be called before using any uno classes)" );
}
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 ( css::script::CannotConvertException,
css::lang::IllegalArgumentException,
RuntimeException)
{
if( ! impl->cargo->valid )
{
throw RuntimeException("pyuno runtime must be initialized before calling any2PyObject" );
}
switch (a.getValueTypeClass ())
{
case typelib_TypeClass_VOID:
{
Py_INCREF (Py_None);
return PyRef(Py_None);
}
case typelib_TypeClass_CHAR:
{
sal_Unicode c = *o3tl::forceAccess<sal_Unicode>(a);
return PyRef( PyUNO_char_new( c , *this ), SAL_NO_ACQUIRE );
}
case typelib_TypeClass_BOOLEAN:
{
bool b;
if ((a >>= b) && b)
return Py_True;
else
return Py_False;
}
case typelib_TypeClass_BYTE:
case typelib_TypeClass_SHORT:
case typelib_TypeClass_UNSIGNED_SHORT:
case typelib_TypeClass_LONG:
{
sal_Int32 l = 0;
a >>= l;
2011-05-07 20:35:03 +01:00
return PyRef( PyLong_FromLong (l), SAL_NO_ACQUIRE );
}
case typelib_TypeClass_UNSIGNED_LONG:
{
sal_uInt32 l = 0;
a >>= l;
return PyRef( PyLong_FromUnsignedLong (l), SAL_NO_ACQUIRE );
}
case typelib_TypeClass_HYPER:
{
sal_Int64 l = 0;
a >>= l;
return PyRef( PyLong_FromLongLong (l), SAL_NO_ACQUIRE);
}
case typelib_TypeClass_UNSIGNED_HYPER:
{
sal_uInt64 l = 0;
a >>= l;
return PyRef( PyLong_FromUnsignedLongLong (l), SAL_NO_ACQUIRE);
}
case typelib_TypeClass_FLOAT:
{
float f = 0.0;
a >>= f;
return PyRef(PyFloat_FromDouble (f), SAL_NO_ACQUIRE);
}
case typelib_TypeClass_DOUBLE:
{
double d = 0.0;
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(), (css::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:
{
sal_Int32 l = *static_cast<sal_Int32 const *>(a.getValue());
TypeDescription desc( a.getValueType() );
if( desc.is() )
{
desc.makeComplete();
typelib_EnumTypeDescription *pEnumDesc =
reinterpret_cast<typelib_EnumTypeDescription *>(desc.get());
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.append( "Any carries enum " );
buf.append( a.getValueType().getTypeName());
buf.append( " with invalid value " ).append( l );
throw RuntimeException( buf.makeStringAndClear() );
}
case typelib_TypeClass_EXCEPTION:
case typelib_TypeClass_STRUCT:
{
PyRef excClass = getClass( a.getValueType().getTypeName(), *this );
PyRef value = PyUNOStruct_new( a, getImpl()->cargo->xInvocation );
PyRef argsTuple( PyTuple_New( 1 ) , SAL_NO_ACQUIRE, NOT_NULL );
PyTuple_SetItem( argsTuple.get() , 0 , value.getAcquired() );
PyRef ret( PyObject_CallObject( excClass.get() , argsTuple.get() ), SAL_NO_ACQUIRE );
if( ! ret.is() )
{
OUStringBuffer buf;
buf.append( "Couldn't instantiate python representation of structured UNO type " );
buf.append( a.getValueType().getTypeName() );
throw RuntimeException( buf.makeStringAndClear() );
}
if( auto e = o3tl::tryAccess<css::uno::Exception>(a) )
{
// add the message in a standard python way !
PyRef args( PyTuple_New( 1 ), SAL_NO_ACQUIRE, NOT_NULL );
PyRef pymsg = ustring2PyString( e->Message );
PyTuple_SetItem( args.get(), 0 , pymsg.getAcquired() );
// the exception base functions want to have an "args" tuple,
// which contains the message
PyObject_SetAttrString( ret.get(), "args", args.get() );
}
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;
tc->convertTo (a, cppu::UnoType<decltype(s)>::get()) >>= s;
PyRef tuple( PyTuple_New (s.getLength()), SAL_NO_ACQUIRE, NOT_NULL);
int i=0;
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( css::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:
{
Reference<XInterface> tmp_interface;
a >>= tmp_interface;
if (!tmp_interface.is ())
return Py_None;
return PyUNO_new( a, getImpl()->cargo->xInvocation );
}
default:
{
OUStringBuffer buf;
buf.append( "Unknown UNO type class " );
buf.append( (sal_Int32 ) a.getValueTypeClass() );
throw RuntimeException(buf.makeStringAndClear( ) );
}
}
}
static Sequence< Type > invokeGetTypes( const Runtime & r , PyObject * o )
{
Sequence< Type > ret;
PyRef method( PyObject_GetAttrString( o , "getTypes" ), SAL_NO_ACQUIRE );
raiseInvocationTargetExceptionWhenNeeded( r );
if( method.is() && PyCallable_Check( method.get() ) )
{
PyRef types( PyObject_CallObject( method.get(), nullptr ) , SAL_NO_ACQUIRE );
raiseInvocationTargetExceptionWhenNeeded( r );
if( types.is() && PyTuple_Check( types.get() ) )
{
int size = PyTuple_Size( types.get() );
// add the XUnoTunnel interface for uno object identity concept (hack)
ret.realloc( size + 1 );
for( int i = 0 ; i < size ; i ++ )
{
Any a = r.pyObject2Any(PyTuple_GetItem(types.get(),i));
a >>= ret[i];
}
ret[size] = cppu::UnoType<css::lang::XUnoTunnel>::get();
}
}
return ret;
}
static OUString
lcl_ExceptionMessage(PyObject *const o, OUString const*const pWrapped)
{
OUStringBuffer buf;
buf.append("Couldn't convert ");
PyRef reprString( PyObject_Str(o), SAL_NO_ACQUIRE );
buf.appendAscii( PyStr_AsString(reprString.get()) );
buf.append(" to a UNO type");
if (pWrapped)
{
buf.append("; caught exception: ");
buf.append(*pWrapped);
}
return buf.makeStringAndClear();
}
Make PyUNO provide more Pythonic behaviour - Simplifies working with UNO objects by giving the behaviour of Python lists, dicts and iterators to objects which implement UNO container interfaces - Applies a custom behaviour to allow objects which implement com::sun::star::table::XCellRange to yield cells and cell ranges by subscript - When UNO container objects are addressed in the new style, eliminates the requirement to manually construct Any objects for contained elements which are typed sequences - Allows lists and iterators to be passed wherever a UNO method accepts a sequence - Relaxes the requirements for initialising UNO structs to allow some members to be skipped when all initialisers are passed by name 1. Collection interfaces ======================== Objects which implement core UNO collection interfaces are made to behave in a way that is more natural for Python code. com::sun::star::container::XIndexAccess com::sun::star::container::XIndexReplace com::sun::star::container::XIndexContainer - Objects provide Python list access semantics num = len(obj) # Number of elements val = obj[0] # Access by index val1,val2 = obj[2:4] # Access by slice val1,val2 = obj[0:3:2] # Access by extended slice if val in obj: ... # Test value presence for val in obj: ... # Implicit iterator (values) itr = iter(obj) # Named iterator (values) obj[0] = val # Replace by index obj[2:4] = val1,val2 # Replace by slice obj[0:3:2] = val1,val2 # Replace by extended slice obj[2:3] = val1,val2 # Insert/replace by slice obj[2:2] = (val,) # Insert by slice obj[2:4] = (val,) # Replace/delete by slice obj[2:3] = () # Delete by slice (implicit) del obj[0] # Delete by index del obj[2:4] # Delete by slice com::sun::star::container::XNameAccess com::sun::star::container::XNameReplace com::sun::star::container::XNameContainer - Objects provide Python dict access semantics num = len(obj) # Number of keys val = obj[key] # Access by key if key in obj: ... # Test key presence for key in obj: ... # Implicit iterator (keys) itr = iter(obj) # Named iterator (keys) obj[key] = val # Replace by key obj[key] = val # Insert by key del obj[key] # Delete by key com::sun::star::container::XEnumerationAccess - Objects provide Python iterable semantics for val in obj: ... # Implicit iterator itr = iter(obj) # Named iterator com::sun::star::container::XEnumeration - Objects provide Python iterator semantics for val in itr: ... # Iteration of named iterator if val in itr: ... # Test value presence Objects which implement both XIndex* and XName* are supported, and respond to both integer and string keys. However, iterating over such an object will return the keys (like a Python dict) rather than the values (like a Python list). 2. Cell ranges ============== A custom behaviour is applied to objects which implement com::sun::star::table::XCellRange to allow their cells and cell ranges to be addressed by subscript, in the style of a Python list or dict (read-only). This is applicable to Calc spreadsheet sheets, Writer text tables and cell ranges created upon these. cell = cellrange[0,0] # Access cell by indices rng = cellrange[0,1:2] # Access cell range by index,slice rng = cellrange[1:2,0] # Access cell range by slice,index rng = cellrange[0:1,2:3] # Access cell range by slices rng = cellrange['A1:B2'] # Access cell range by descriptor rng = cellrange['Name'] # Access cell range by name Note that the indices used are in Python/C order, and differ from the arguments to methods provided by XCellRange. - The statement cellrange[r,c], which returns the cell from row r and column c, is equivalent to calling XCellRange::getCellByPosition(c,r) - The statement cellrange[t:b,l:r], which returns a cell range covering rows t to b(non-inclusive) and columns l to r(non- inclusive), is equivalent to calling XCellRange::getCellRangeByPosition(l,t,r-1,b-1). In contrast to the handling of objects implementing XIndex*, extended slice syntax is not supported. Negative indices (from-end addresses) are supported only for objects which also implement com::sun::star::table::XColumnRowRange (currently Calc spreadsheet sheets and cell ranges created upon these). For such objects, the following syntax is also available: rng = cellrange[0] # Access cell range by row index rng = cellrange[0,:] # Access cell range by row index rng = cellrange[:,0] # Access cell range by column index 3. Elimination of explicit Any ============================== PyUNO has not previously been able to cope with certain method arguments which are typed as Any but require a sequence of specific type to be passed. This is a particular issue for container interfaces such as XIndexContainer and XNameContainer. The existing solution to dealing with such methods is to use a special method to pass an explicitly typed Any, giving code such as: index = doc.createInstance("com.sun.star.text.ContentIndex"); ... uno.invoke( index.LevelParagraphStyles , "replaceByIndex", (0, uno.Any("[]string", ('Caption',))) ) The new Pythonic container access is able to correctly infer the expected type of the sequences required by these arguments. In the new style, the above call to .replaceByIndex() can instead be written: index.LevelParagraphStyles[0] = ('Caption',) 4. List and iterator arguments ============================== Wherever a UNO API expects a sequence, a Python list or iterator can now be passed. This enables the use of list comprehensions and generator expressions for method calls and property assignments. Example: tbl = doc.createInstance('com.sun.star.text.TextTable') tbl.initialize(10,10) # ... insert table ... # Assign numbers 0..99 to the cells using a generator expression tbl.Data = ((y for y in range(10*x,10*x + 10)) for x in range(10)) 5. Tolerant struct initialisation ================================= Previously, a UNO struct could be created fully uninitialised, or by passing a combination of positional and/or named arguments to its constructor. However, if any arguments were passed, all members were required to be initialised or an exception was thrown. This requirement is relaxed such that when all arguments passed to a struct constructor are by name, some may be omitted. The existing requirement that all members must be explicitly initialised when some constructor arguments are unnamed (positional) is not affected. Example: from com.sun.star.beans import PropertyValue prop = PropertyValue(Name='foo', Value='bar') Change-Id: Id29bff10a18099b1a00af1abee1a6c1bc58b3978 Reviewed-on: https://gerrit.libreoffice.org/16272 Tested-by: Jenkins <ci@libreoffice.org> Reviewed-by: Matthew Francis <mjay.francis@gmail.com>
2015-06-01 18:34:04 +08:00
// For Python 2.7 - see https://bugs.python.org/issue24161
// Fills aSeq and returns true if pObj is a valid iterator
bool Runtime::pyIterUnpack( PyObject *const pObj, Any &a ) const
{
if( !PyIter_Check( pObj ))
return false;
PyObject *pItem = PyIter_Next( pObj );
if( !pItem )
{
if( PyErr_Occurred() )
{
PyErr_Clear();
return false;
}
return true;
}
::std::list<Any> items;
do
{
PyRef rItem( pItem, SAL_NO_ACQUIRE );
items.push_back( pyObject2Any( rItem.get() ) );
}
while( (pItem = PyIter_Next( pObj )) );
a <<= comphelper::containerToSequence<Any>(items);
Make PyUNO provide more Pythonic behaviour - Simplifies working with UNO objects by giving the behaviour of Python lists, dicts and iterators to objects which implement UNO container interfaces - Applies a custom behaviour to allow objects which implement com::sun::star::table::XCellRange to yield cells and cell ranges by subscript - When UNO container objects are addressed in the new style, eliminates the requirement to manually construct Any objects for contained elements which are typed sequences - Allows lists and iterators to be passed wherever a UNO method accepts a sequence - Relaxes the requirements for initialising UNO structs to allow some members to be skipped when all initialisers are passed by name 1. Collection interfaces ======================== Objects which implement core UNO collection interfaces are made to behave in a way that is more natural for Python code. com::sun::star::container::XIndexAccess com::sun::star::container::XIndexReplace com::sun::star::container::XIndexContainer - Objects provide Python list access semantics num = len(obj) # Number of elements val = obj[0] # Access by index val1,val2 = obj[2:4] # Access by slice val1,val2 = obj[0:3:2] # Access by extended slice if val in obj: ... # Test value presence for val in obj: ... # Implicit iterator (values) itr = iter(obj) # Named iterator (values) obj[0] = val # Replace by index obj[2:4] = val1,val2 # Replace by slice obj[0:3:2] = val1,val2 # Replace by extended slice obj[2:3] = val1,val2 # Insert/replace by slice obj[2:2] = (val,) # Insert by slice obj[2:4] = (val,) # Replace/delete by slice obj[2:3] = () # Delete by slice (implicit) del obj[0] # Delete by index del obj[2:4] # Delete by slice com::sun::star::container::XNameAccess com::sun::star::container::XNameReplace com::sun::star::container::XNameContainer - Objects provide Python dict access semantics num = len(obj) # Number of keys val = obj[key] # Access by key if key in obj: ... # Test key presence for key in obj: ... # Implicit iterator (keys) itr = iter(obj) # Named iterator (keys) obj[key] = val # Replace by key obj[key] = val # Insert by key del obj[key] # Delete by key com::sun::star::container::XEnumerationAccess - Objects provide Python iterable semantics for val in obj: ... # Implicit iterator itr = iter(obj) # Named iterator com::sun::star::container::XEnumeration - Objects provide Python iterator semantics for val in itr: ... # Iteration of named iterator if val in itr: ... # Test value presence Objects which implement both XIndex* and XName* are supported, and respond to both integer and string keys. However, iterating over such an object will return the keys (like a Python dict) rather than the values (like a Python list). 2. Cell ranges ============== A custom behaviour is applied to objects which implement com::sun::star::table::XCellRange to allow their cells and cell ranges to be addressed by subscript, in the style of a Python list or dict (read-only). This is applicable to Calc spreadsheet sheets, Writer text tables and cell ranges created upon these. cell = cellrange[0,0] # Access cell by indices rng = cellrange[0,1:2] # Access cell range by index,slice rng = cellrange[1:2,0] # Access cell range by slice,index rng = cellrange[0:1,2:3] # Access cell range by slices rng = cellrange['A1:B2'] # Access cell range by descriptor rng = cellrange['Name'] # Access cell range by name Note that the indices used are in Python/C order, and differ from the arguments to methods provided by XCellRange. - The statement cellrange[r,c], which returns the cell from row r and column c, is equivalent to calling XCellRange::getCellByPosition(c,r) - The statement cellrange[t:b,l:r], which returns a cell range covering rows t to b(non-inclusive) and columns l to r(non- inclusive), is equivalent to calling XCellRange::getCellRangeByPosition(l,t,r-1,b-1). In contrast to the handling of objects implementing XIndex*, extended slice syntax is not supported. Negative indices (from-end addresses) are supported only for objects which also implement com::sun::star::table::XColumnRowRange (currently Calc spreadsheet sheets and cell ranges created upon these). For such objects, the following syntax is also available: rng = cellrange[0] # Access cell range by row index rng = cellrange[0,:] # Access cell range by row index rng = cellrange[:,0] # Access cell range by column index 3. Elimination of explicit Any ============================== PyUNO has not previously been able to cope with certain method arguments which are typed as Any but require a sequence of specific type to be passed. This is a particular issue for container interfaces such as XIndexContainer and XNameContainer. The existing solution to dealing with such methods is to use a special method to pass an explicitly typed Any, giving code such as: index = doc.createInstance("com.sun.star.text.ContentIndex"); ... uno.invoke( index.LevelParagraphStyles , "replaceByIndex", (0, uno.Any("[]string", ('Caption',))) ) The new Pythonic container access is able to correctly infer the expected type of the sequences required by these arguments. In the new style, the above call to .replaceByIndex() can instead be written: index.LevelParagraphStyles[0] = ('Caption',) 4. List and iterator arguments ============================== Wherever a UNO API expects a sequence, a Python list or iterator can now be passed. This enables the use of list comprehensions and generator expressions for method calls and property assignments. Example: tbl = doc.createInstance('com.sun.star.text.TextTable') tbl.initialize(10,10) # ... insert table ... # Assign numbers 0..99 to the cells using a generator expression tbl.Data = ((y for y in range(10*x,10*x + 10)) for x in range(10)) 5. Tolerant struct initialisation ================================= Previously, a UNO struct could be created fully uninitialised, or by passing a combination of positional and/or named arguments to its constructor. However, if any arguments were passed, all members were required to be initialised or an exception was thrown. This requirement is relaxed such that when all arguments passed to a struct constructor are by name, some may be omitted. The existing requirement that all members must be explicitly initialised when some constructor arguments are unnamed (positional) is not affected. Example: from com.sun.star.beans import PropertyValue prop = PropertyValue(Name='foo', Value='bar') Change-Id: Id29bff10a18099b1a00af1abee1a6c1bc58b3978 Reviewed-on: https://gerrit.libreoffice.org/16272 Tested-by: Jenkins <ci@libreoffice.org> Reviewed-by: Matthew Francis <mjay.francis@gmail.com>
2015-06-01 18:34:04 +08:00
return true;
}
Any Runtime::pyObject2Any ( const PyRef & source, enum ConversionMode mode ) const
throw ( css::uno::RuntimeException )
{
if( ! impl->cargo->valid )
{
throw RuntimeException("pyuno runtime must be initialized before calling any2PyObject" );
}
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
else if (PyInt_Check (o))
{
if( o == Py_True )
{
a <<= true;
}
else if ( o == Py_False )
{
a <<= false;
}
else
{
2011-05-07 20:35:03 +01:00
sal_Int32 l = (sal_Int32) PyLong_AsLong( o );
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 */
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)
{
a <<= true;
2011-05-07 20:35:03 +01:00
}
else if(o == Py_False)
{
a <<= false;
2011-05-07 20:35:03 +01:00
}
else
{
#endif /* PY_MAJOR_VERSION >= 3 */
sal_Int64 l = (sal_Int64)PyLong_AsLong (o);
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 if( l <= SAL_CONST_INT64(0x7fffffff) &&
l >= -SAL_CONST_INT64(0x80000000) )
{
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
}
else if (PyFloat_Check (o))
{
double d = PyFloat_AsDouble (o);
a <<= d;
}
else if (PyStrBytes_Check(o) || PyUnicode_Check(o))
2011-05-07 20:35:03 +01:00
{
a <<= pyString2ustring(o);
}
else if (PyTuple_Check (o))
{
Sequence<Any> s (PyTuple_Size (o));
for (Py_ssize_t i = 0; i < PyTuple_Size (o); i++)
{
s[i] = pyObject2Any (PyTuple_GetItem (o, i), mode );
}
a <<= s;
}
Make PyUNO provide more Pythonic behaviour - Simplifies working with UNO objects by giving the behaviour of Python lists, dicts and iterators to objects which implement UNO container interfaces - Applies a custom behaviour to allow objects which implement com::sun::star::table::XCellRange to yield cells and cell ranges by subscript - When UNO container objects are addressed in the new style, eliminates the requirement to manually construct Any objects for contained elements which are typed sequences - Allows lists and iterators to be passed wherever a UNO method accepts a sequence - Relaxes the requirements for initialising UNO structs to allow some members to be skipped when all initialisers are passed by name 1. Collection interfaces ======================== Objects which implement core UNO collection interfaces are made to behave in a way that is more natural for Python code. com::sun::star::container::XIndexAccess com::sun::star::container::XIndexReplace com::sun::star::container::XIndexContainer - Objects provide Python list access semantics num = len(obj) # Number of elements val = obj[0] # Access by index val1,val2 = obj[2:4] # Access by slice val1,val2 = obj[0:3:2] # Access by extended slice if val in obj: ... # Test value presence for val in obj: ... # Implicit iterator (values) itr = iter(obj) # Named iterator (values) obj[0] = val # Replace by index obj[2:4] = val1,val2 # Replace by slice obj[0:3:2] = val1,val2 # Replace by extended slice obj[2:3] = val1,val2 # Insert/replace by slice obj[2:2] = (val,) # Insert by slice obj[2:4] = (val,) # Replace/delete by slice obj[2:3] = () # Delete by slice (implicit) del obj[0] # Delete by index del obj[2:4] # Delete by slice com::sun::star::container::XNameAccess com::sun::star::container::XNameReplace com::sun::star::container::XNameContainer - Objects provide Python dict access semantics num = len(obj) # Number of keys val = obj[key] # Access by key if key in obj: ... # Test key presence for key in obj: ... # Implicit iterator (keys) itr = iter(obj) # Named iterator (keys) obj[key] = val # Replace by key obj[key] = val # Insert by key del obj[key] # Delete by key com::sun::star::container::XEnumerationAccess - Objects provide Python iterable semantics for val in obj: ... # Implicit iterator itr = iter(obj) # Named iterator com::sun::star::container::XEnumeration - Objects provide Python iterator semantics for val in itr: ... # Iteration of named iterator if val in itr: ... # Test value presence Objects which implement both XIndex* and XName* are supported, and respond to both integer and string keys. However, iterating over such an object will return the keys (like a Python dict) rather than the values (like a Python list). 2. Cell ranges ============== A custom behaviour is applied to objects which implement com::sun::star::table::XCellRange to allow their cells and cell ranges to be addressed by subscript, in the style of a Python list or dict (read-only). This is applicable to Calc spreadsheet sheets, Writer text tables and cell ranges created upon these. cell = cellrange[0,0] # Access cell by indices rng = cellrange[0,1:2] # Access cell range by index,slice rng = cellrange[1:2,0] # Access cell range by slice,index rng = cellrange[0:1,2:3] # Access cell range by slices rng = cellrange['A1:B2'] # Access cell range by descriptor rng = cellrange['Name'] # Access cell range by name Note that the indices used are in Python/C order, and differ from the arguments to methods provided by XCellRange. - The statement cellrange[r,c], which returns the cell from row r and column c, is equivalent to calling XCellRange::getCellByPosition(c,r) - The statement cellrange[t:b,l:r], which returns a cell range covering rows t to b(non-inclusive) and columns l to r(non- inclusive), is equivalent to calling XCellRange::getCellRangeByPosition(l,t,r-1,b-1). In contrast to the handling of objects implementing XIndex*, extended slice syntax is not supported. Negative indices (from-end addresses) are supported only for objects which also implement com::sun::star::table::XColumnRowRange (currently Calc spreadsheet sheets and cell ranges created upon these). For such objects, the following syntax is also available: rng = cellrange[0] # Access cell range by row index rng = cellrange[0,:] # Access cell range by row index rng = cellrange[:,0] # Access cell range by column index 3. Elimination of explicit Any ============================== PyUNO has not previously been able to cope with certain method arguments which are typed as Any but require a sequence of specific type to be passed. This is a particular issue for container interfaces such as XIndexContainer and XNameContainer. The existing solution to dealing with such methods is to use a special method to pass an explicitly typed Any, giving code such as: index = doc.createInstance("com.sun.star.text.ContentIndex"); ... uno.invoke( index.LevelParagraphStyles , "replaceByIndex", (0, uno.Any("[]string", ('Caption',))) ) The new Pythonic container access is able to correctly infer the expected type of the sequences required by these arguments. In the new style, the above call to .replaceByIndex() can instead be written: index.LevelParagraphStyles[0] = ('Caption',) 4. List and iterator arguments ============================== Wherever a UNO API expects a sequence, a Python list or iterator can now be passed. This enables the use of list comprehensions and generator expressions for method calls and property assignments. Example: tbl = doc.createInstance('com.sun.star.text.TextTable') tbl.initialize(10,10) # ... insert table ... # Assign numbers 0..99 to the cells using a generator expression tbl.Data = ((y for y in range(10*x,10*x + 10)) for x in range(10)) 5. Tolerant struct initialisation ================================= Previously, a UNO struct could be created fully uninitialised, or by passing a combination of positional and/or named arguments to its constructor. However, if any arguments were passed, all members were required to be initialised or an exception was thrown. This requirement is relaxed such that when all arguments passed to a struct constructor are by name, some may be omitted. The existing requirement that all members must be explicitly initialised when some constructor arguments are unnamed (positional) is not affected. Example: from com.sun.star.beans import PropertyValue prop = PropertyValue(Name='foo', Value='bar') Change-Id: Id29bff10a18099b1a00af1abee1a6c1bc58b3978 Reviewed-on: https://gerrit.libreoffice.org/16272 Tested-by: Jenkins <ci@libreoffice.org> Reviewed-by: Matthew Francis <mjay.francis@gmail.com>
2015-06-01 18:34:04 +08:00
else if (PyList_Check (o))
{
Py_ssize_t l = PyList_Size (o);
Sequence<Any> s (l);
for (Py_ssize_t i = 0; i < l; i++)
Make PyUNO provide more Pythonic behaviour - Simplifies working with UNO objects by giving the behaviour of Python lists, dicts and iterators to objects which implement UNO container interfaces - Applies a custom behaviour to allow objects which implement com::sun::star::table::XCellRange to yield cells and cell ranges by subscript - When UNO container objects are addressed in the new style, eliminates the requirement to manually construct Any objects for contained elements which are typed sequences - Allows lists and iterators to be passed wherever a UNO method accepts a sequence - Relaxes the requirements for initialising UNO structs to allow some members to be skipped when all initialisers are passed by name 1. Collection interfaces ======================== Objects which implement core UNO collection interfaces are made to behave in a way that is more natural for Python code. com::sun::star::container::XIndexAccess com::sun::star::container::XIndexReplace com::sun::star::container::XIndexContainer - Objects provide Python list access semantics num = len(obj) # Number of elements val = obj[0] # Access by index val1,val2 = obj[2:4] # Access by slice val1,val2 = obj[0:3:2] # Access by extended slice if val in obj: ... # Test value presence for val in obj: ... # Implicit iterator (values) itr = iter(obj) # Named iterator (values) obj[0] = val # Replace by index obj[2:4] = val1,val2 # Replace by slice obj[0:3:2] = val1,val2 # Replace by extended slice obj[2:3] = val1,val2 # Insert/replace by slice obj[2:2] = (val,) # Insert by slice obj[2:4] = (val,) # Replace/delete by slice obj[2:3] = () # Delete by slice (implicit) del obj[0] # Delete by index del obj[2:4] # Delete by slice com::sun::star::container::XNameAccess com::sun::star::container::XNameReplace com::sun::star::container::XNameContainer - Objects provide Python dict access semantics num = len(obj) # Number of keys val = obj[key] # Access by key if key in obj: ... # Test key presence for key in obj: ... # Implicit iterator (keys) itr = iter(obj) # Named iterator (keys) obj[key] = val # Replace by key obj[key] = val # Insert by key del obj[key] # Delete by key com::sun::star::container::XEnumerationAccess - Objects provide Python iterable semantics for val in obj: ... # Implicit iterator itr = iter(obj) # Named iterator com::sun::star::container::XEnumeration - Objects provide Python iterator semantics for val in itr: ... # Iteration of named iterator if val in itr: ... # Test value presence Objects which implement both XIndex* and XName* are supported, and respond to both integer and string keys. However, iterating over such an object will return the keys (like a Python dict) rather than the values (like a Python list). 2. Cell ranges ============== A custom behaviour is applied to objects which implement com::sun::star::table::XCellRange to allow their cells and cell ranges to be addressed by subscript, in the style of a Python list or dict (read-only). This is applicable to Calc spreadsheet sheets, Writer text tables and cell ranges created upon these. cell = cellrange[0,0] # Access cell by indices rng = cellrange[0,1:2] # Access cell range by index,slice rng = cellrange[1:2,0] # Access cell range by slice,index rng = cellrange[0:1,2:3] # Access cell range by slices rng = cellrange['A1:B2'] # Access cell range by descriptor rng = cellrange['Name'] # Access cell range by name Note that the indices used are in Python/C order, and differ from the arguments to methods provided by XCellRange. - The statement cellrange[r,c], which returns the cell from row r and column c, is equivalent to calling XCellRange::getCellByPosition(c,r) - The statement cellrange[t:b,l:r], which returns a cell range covering rows t to b(non-inclusive) and columns l to r(non- inclusive), is equivalent to calling XCellRange::getCellRangeByPosition(l,t,r-1,b-1). In contrast to the handling of objects implementing XIndex*, extended slice syntax is not supported. Negative indices (from-end addresses) are supported only for objects which also implement com::sun::star::table::XColumnRowRange (currently Calc spreadsheet sheets and cell ranges created upon these). For such objects, the following syntax is also available: rng = cellrange[0] # Access cell range by row index rng = cellrange[0,:] # Access cell range by row index rng = cellrange[:,0] # Access cell range by column index 3. Elimination of explicit Any ============================== PyUNO has not previously been able to cope with certain method arguments which are typed as Any but require a sequence of specific type to be passed. This is a particular issue for container interfaces such as XIndexContainer and XNameContainer. The existing solution to dealing with such methods is to use a special method to pass an explicitly typed Any, giving code such as: index = doc.createInstance("com.sun.star.text.ContentIndex"); ... uno.invoke( index.LevelParagraphStyles , "replaceByIndex", (0, uno.Any("[]string", ('Caption',))) ) The new Pythonic container access is able to correctly infer the expected type of the sequences required by these arguments. In the new style, the above call to .replaceByIndex() can instead be written: index.LevelParagraphStyles[0] = ('Caption',) 4. List and iterator arguments ============================== Wherever a UNO API expects a sequence, a Python list or iterator can now be passed. This enables the use of list comprehensions and generator expressions for method calls and property assignments. Example: tbl = doc.createInstance('com.sun.star.text.TextTable') tbl.initialize(10,10) # ... insert table ... # Assign numbers 0..99 to the cells using a generator expression tbl.Data = ((y for y in range(10*x,10*x + 10)) for x in range(10)) 5. Tolerant struct initialisation ================================= Previously, a UNO struct could be created fully uninitialised, or by passing a combination of positional and/or named arguments to its constructor. However, if any arguments were passed, all members were required to be initialised or an exception was thrown. This requirement is relaxed such that when all arguments passed to a struct constructor are by name, some may be omitted. The existing requirement that all members must be explicitly initialised when some constructor arguments are unnamed (positional) is not affected. Example: from com.sun.star.beans import PropertyValue prop = PropertyValue(Name='foo', Value='bar') Change-Id: Id29bff10a18099b1a00af1abee1a6c1bc58b3978 Reviewed-on: https://gerrit.libreoffice.org/16272 Tested-by: Jenkins <ci@libreoffice.org> Reviewed-by: Matthew Francis <mjay.francis@gmail.com>
2015-06-01 18:34:04 +08:00
{
s[i] = pyObject2Any (PyList_GetItem (o, i), mode );
}
a <<= s;
}
else if (!pyIterUnpack (o, a))
{
Runtime runtime;
// should be removed, in case ByteSequence gets derived from String
if( PyObject_IsInstance( o, getByteSequenceClass( runtime ).get() ) )
{
PyRef str(PyObject_GetAttrString( o , "value" ),SAL_NO_ACQUIRE);
Sequence< sal_Int8 > seq;
if( PyStrBytes_Check( str.get() ) )
{
seq = Sequence<sal_Int8 > (
reinterpret_cast<sal_Int8*>(PyStrBytes_AsString(str.get())), PyStrBytes_Size(str.get()));
}
a <<= seq;
}
else
if( PyObject_IsInstance( o, getTypeClass( runtime ).get() ) )
{
Type t = PyType2Type( o );
a <<= t;
}
else if( PyObject_IsInstance( o, getEnumClass( runtime ).get() ) )
{
a = PyEnum2Enum( o );
}
else if( isInstanceOfStructOrException( o ) )
{
PyRef struc(PyObject_GetAttrString( o , "value" ),SAL_NO_ACQUIRE);
PyUNO * obj = reinterpret_cast<PyUNO*>(struc.get());
Reference< XMaterialHolder > holder( obj->members->xInvocation, UNO_QUERY );
if( holder.is( ) )
a = holder->getMaterial();
else
{
throw RuntimeException(
"struct or exception wrapper does not support XMaterialHolder" );
}
}
else if( PyObject_IsInstance( o, getPyUnoClass().get() ) )
{
PyUNO* o_pi = reinterpret_cast<PyUNO*>(o);
a = o_pi->members->wrappedObject;
}
else if( PyObject_IsInstance( o, getPyUnoStructClass().get() ) )
{
PyUNO* o_pi = reinterpret_cast<PyUNO*>(o);
Reference<XMaterialHolder> my_mh (o_pi->members->xInvocation, UNO_QUERY);
if (!my_mh.is())
{
throw RuntimeException(
"struct wrapper does not support XMaterialHolder" );
}
else
a = my_mh->getMaterial();
}
else if( PyObject_IsInstance( o, getCharClass( runtime ).get() ) )
{
a <<= PyChar2Unicode( o );
}
else if( PyObject_IsInstance( o, getAnyClass( runtime ).get() ) )
{
if( ACCEPT_UNO_ANY == mode )
{
a = pyObject2Any( PyRef( PyObject_GetAttrString( o , "value" ), SAL_NO_ACQUIRE) );
Type t;
pyObject2Any( PyRef( PyObject_GetAttrString( o, "type" ), SAL_NO_ACQUIRE ) ) >>= t;
try
{
a = getImpl()->cargo->xTypeConverter->convertTo( a, t );
}
catch( const css::uno::Exception & e )
{
throw WrappedTargetRuntimeException(
e.Message, e.Context, makeAny(e));
}
}
else
{
throw RuntimeException(
"uno.Any instance not accepted during method call, "
"use uno.invoke instead" );
}
}
else
{
Reference< XInterface > mappedObject;
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;
}
if( adapterObject.is() )
{
// object got already bridged !
Reference< css::lang::XUnoTunnel > tunnel( adapterObject, UNO_QUERY );
Adapter *pAdapter = reinterpret_cast<Adapter*>(
tunnel->getSomething(
::pyuno::Adapter::getUnoTunnelImplementationId() ) );
mappedObject = impl->cargo->xAdapterFactory->createAdapter(
adapterObject, pAdapter->getWrappedTypes() );
}
else
{
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) ] =
css::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);
}
}
if( mappedObject.is() )
{
a = css::uno::makeAny( mappedObject );
}
else
{
OUString const msg(lcl_ExceptionMessage(o, nullptr));
throw RuntimeException(msg);
}
}
}
return a;
}
Any Runtime::extractUnoException( const PyRef & excType, const PyRef &excValue, const PyRef &excTraceback) const
{
OUString str;
Any ret;
if( excTraceback.is() )
{
Exception e;
PyRef unoModule;
if ( impl )
{
try
{
unoModule = impl->cargo->getUnoModule();
}
2011-12-21 21:57:16 +09:00
catch (const Exception &ei)
{
e=ei;
}
}
if( unoModule.is() )
{
PyRef extractTraceback(
PyDict_GetItemString(unoModule.get(),"_uno_extract_printable_stacktrace" ) );
if( PyCallable_Check(extractTraceback.get()) )
{
PyRef args( PyTuple_New( 1), SAL_NO_ACQUIRE, NOT_NULL );
PyTuple_SetItem( args.get(), 0, excTraceback.getAcquired() );
PyRef pyStr( PyObject_CallObject( extractTraceback.get(),args.get() ), SAL_NO_ACQUIRE);
str = OUString::createFromAscii( PyStr_AsString(pyStr.get()) );
}
else
{
str = "Couldn't find uno._uno_extract_printable_stacktrace";
}
}
else
{
str = "Could not load uno.py, no stacktrace available";
if ( !e.Message.isEmpty() )
{
str += " (Error loading uno.py: " + e.Message + ")";
}
}
}
else
{
// it may occur, that no traceback is given (e.g. only native code below)
str = "no traceback available";
}
if( isInstanceOfStructOrException( excValue.get() ) )
{
ret = pyObject2Any( excValue );
}
else
{
OUStringBuffer buf;
PyRef typeName( PyObject_Str( excType.get() ), SAL_NO_ACQUIRE );
if( typeName.is() )
{
buf.appendAscii( PyStr_AsString( typeName.get() ) );
}
else
{
buf.append( "no typename available" );
}
buf.append( ": " );
PyRef valueRep( PyObject_Str( excValue.get() ), SAL_NO_ACQUIRE );
if( valueRep.is() )
{
buf.appendAscii( PyStr_AsString( valueRep.get()));
}
else
{
buf.append( "Couldn't convert exception value to a string" );
}
buf.append( ", traceback follows\n" );
if( !str.isEmpty() )
{
buf.append( str );
buf.append( "\n" );
}
else
{
buf.append( ", no traceback available\n" );
}
RuntimeException e;
e.Message = buf.makeStringAndClear();
#if OSL_DEBUG_LEVEL > 0
fprintf( stderr, "Python exception: %s\n",
OUStringToOString(e.Message, RTL_TEXTENCODING_UTF8).getStr() );
#endif
ret = css::uno::makeAny( e );
}
return ret;
}
PyThreadAttach::PyThreadAttach( PyInterpreterState *interp)
throw ( css::uno::RuntimeException )
{
tstate = PyThreadState_New( interp );
if( !tstate )
throw RuntimeException( "Couldn't create a pythreadstate" );
PyEval_AcquireThread( tstate);
}
PyThreadAttach::~PyThreadAttach()
{
PyThreadState_Clear( tstate );
PyEval_ReleaseThread( tstate );
PyThreadState_Delete( tstate );
}
PyThreadDetach::PyThreadDetach() throw ( css::uno::RuntimeException )
{
tstate = PyThreadState_Get();
PyEval_ReleaseThread( tstate );
}
/** Acquires the global interpreter lock again
*/
PyThreadDetach::~PyThreadDetach()
{
PyEval_AcquireThread( tstate );
}
PyRef const & RuntimeCargo::getUnoModule()
{
if( ! dictUnoModule.is() )
{
dictUnoModule = importUnoModule();
}
return dictUnoModule;
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */