Give PyUNO structs/exceptions their own separate type

Change-Id: Ie4c42c623fae1cf39c2e4c643825c9655cd28daa
Reviewed-on: https://gerrit.libreoffice.org/17410
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Matthew Francis <mjay.francis@gmail.com>
This commit is contained in:
Matthew J. Francis 2015-07-29 15:22:54 +08:00 committed by Matthew Francis
parent 9022556453
commit 2387c2a46e
6 changed files with 438 additions and 114 deletions

View file

@ -38,6 +38,7 @@ $(eval $(call gb_Library_use_externals,pyuno,\
$(eval $(call gb_Library_add_exception_objects,pyuno,\
pyuno/source/module/pyuno_runtime \
pyuno/source/module/pyuno \
pyuno/source/module/pyuno_struct \
pyuno/source/module/pyuno_callable \
pyuno/source/module/pyuno_module \
pyuno/source/module/pyuno_type \

View file

@ -376,26 +376,7 @@ bool lcl_hasInterfaceByName( Any const &object, OUString const & interfaceName )
PyObject *PyUNO_repr( PyObject * self )
{
PyUNO *me = reinterpret_cast<PyUNO *>(self);
PyObject * ret = 0;
if( me->members->wrappedObject.getValueType().getTypeClass()
== com::sun::star::uno::TypeClass_EXCEPTION )
{
Reference< XMaterialHolder > rHolder(me->members->xInvocation,UNO_QUERY);
if( rHolder.is() )
{
Any a = rHolder->getMaterial();
Exception e;
a >>= e;
ret = ustring2PyUnicode(e.Message ).getAcquired();
}
}
else
{
ret = PyUNO_str( self );
}
return ret;
return PyUNO_str( self );
}
Py_hash_t PyUNO_hash( PyObject *self )
@ -405,20 +386,8 @@ Py_hash_t PyUNO_hash( PyObject *self )
// Py_hash_t is not necessarily the same size as a pointer, but this is not
// important for hashing - it just has to return the same value each time
if( me->members->wrappedObject.getValueType().getTypeClass()
== com::sun::star::uno::TypeClass_STRUCT ||
me->members->wrappedObject.getValueType().getTypeClass()
== com::sun::star::uno::TypeClass_EXCEPTION )
{
Reference< XMaterialHolder > xMe( me->members->xInvocation, UNO_QUERY );
return sal::static_int_cast< Py_hash_t >( reinterpret_cast< sal_IntPtr > (
*static_cast<void * const *>(xMe->getMaterial().getValue()) ) );
}
else
{
return sal::static_int_cast< Py_hash_t >( reinterpret_cast< sal_IntPtr > (
*static_cast<void * const *>(me->members->wrappedObject.getValue()) ) );
}
return sal::static_int_cast< Py_hash_t >( reinterpret_cast< sal_IntPtr > (
*static_cast<void * const *>(me->members->wrappedObject.getValue()) ) );
}
@ -501,24 +470,7 @@ PyObject *PyUNO_str( PyObject * self )
OStringBuffer buf;
if( me->members->wrappedObject.getValueType().getTypeClass()
== com::sun::star::uno::TypeClass_STRUCT ||
me->members->wrappedObject.getValueType().getTypeClass()
== com::sun::star::uno::TypeClass_EXCEPTION)
{
Reference< XMaterialHolder > rHolder(me->members->xInvocation,UNO_QUERY);
if( rHolder.is() )
{
PyThreadDetach antiguard;
Any a = rHolder->getMaterial();
OUString s = val2str( a.getValue(), a.getValueType().getTypeLibType() );
buf.append( OUStringToOString(s,RTL_TEXTENCODING_ASCII_US) );
}
}
else
{
// a common UNO object
PyThreadDetach antiguard;
buf.append( "pyuno object " );
@ -527,7 +479,7 @@ PyObject *PyUNO_str( PyObject * self )
buf.append( OUStringToOString(s,RTL_TEXTENCODING_ASCII_US) );
}
return PyStr_FromString( buf.getStr());
return PyStr_FromString( buf.getStr() );
}
PyObject* PyUNO_dir (PyObject* self)
@ -1447,18 +1399,15 @@ PyObject* PyUNO_getattr (PyObject* self, char* name)
}
if (strcmp (name, "__class__") == 0)
{
if( me->members->wrappedObject.getValueTypeClass() ==
com::sun::star::uno::TypeClass_STRUCT ||
me->members->wrappedObject.getValueTypeClass() ==
com::sun::star::uno::TypeClass_EXCEPTION )
{
return getClass(
me->members->wrappedObject.getValueType().getTypeName(), runtime ).getAcquired();
}
Py_INCREF (Py_None);
return Py_None;
}
PyObject *pRet = PyObject_GenericGetAttr( self, PyUnicode_FromString( name ) );
if( pRet )
return pRet;
PyErr_Clear();
OUString attrName( OUString::createFromAscii( name ) );
//We need to find out if it's a method...
if (me->members->xInvocation->hasMethod (attrName))
@ -1557,7 +1506,6 @@ int PyUNO_setattr (PyObject* self, char* name, PyObject* value)
return 1; //as above.
}
// ensure object identity and struct equality
static PyObject* PyUNO_cmp( PyObject *self, PyObject *that, int op )
{
PyObject *result;
@ -1586,26 +1534,11 @@ static PyObject* PyUNO_cmp( PyObject *self, PyObject *that, int op )
if( tcMe == tcOther )
{
if( tcMe == com::sun::star::uno::TypeClass_STRUCT ||
tcMe == com::sun::star::uno::TypeClass_EXCEPTION )
if( me->members->wrappedObject == other->members->wrappedObject )
{
Reference< XMaterialHolder > xMe( me->members->xInvocation,UNO_QUERY);
Reference< XMaterialHolder > xOther( other->members->xInvocation,UNO_QUERY );
if( xMe->getMaterial() == xOther->getMaterial() )
{
result = (op == Py_EQ ? Py_True : Py_False);
Py_INCREF(result);
return result;
}
}
else if( tcMe == com::sun::star::uno::TypeClass_INTERFACE )
{
if( me->members->wrappedObject == other->members->wrappedObject )
{
result = (op == Py_EQ ? Py_True : Py_False);
Py_INCREF(result);
return result;
}
result = (op == Py_EQ ? Py_True : Py_False);
Py_INCREF(result);
return result;
}
}
}
@ -1768,8 +1701,7 @@ PyRef getPyUnoClass()
PyRef PyUNO_new (
const Any &targetInterface,
const Reference<XSingleServiceFactory> &ssf,
const bool bCheckExisting )
const Reference<XSingleServiceFactory> &ssf )
{
Reference<XInvocation2> xInvocation;
@ -1780,16 +1712,13 @@ PyRef PyUNO_new (
if( !xInvocation.is() )
throw RuntimeException("XInvocation2 not implemented, cannot interact with object");
if (bCheckExisting)
Reference<XUnoTunnel> xUnoTunnel (
xInvocation->getIntrospection()->queryAdapter(cppu::UnoType<XUnoTunnel>::get()), UNO_QUERY );
if( xUnoTunnel.is() )
{
Reference<XUnoTunnel> xUnoTunnel (
xInvocation->getIntrospection()->queryAdapter(cppu::UnoType<XUnoTunnel>::get()), UNO_QUERY );
if( xUnoTunnel.is() )
{
sal_Int64 that = xUnoTunnel->getSomething( ::pyuno::Adapter::getUnoTunnelImplementationId() );
if( that )
return PyRef( reinterpret_cast<Adapter*>(that)->getWrappedObject() );
}
sal_Int64 that = xUnoTunnel->getSomething( ::pyuno::Adapter::getUnoTunnelImplementationId() );
if( that )
return PyRef( reinterpret_cast<Adapter*>(that)->getWrappedObject() );
}
}
if( !Py_IsInitialized() )

View file

@ -204,11 +204,15 @@ typedef std::unordered_map
typedef std::unordered_set< PyRef , PyRef::Hash , std::equal_to<PyRef> > ClassSet;
int PyUNO_initType();
int PyUNOStruct_initType();
PyRef PyUNO_new (
const com::sun::star::uno::Any & targetInterface,
const com::sun::star::uno::Reference<com::sun::star::lang::XSingleServiceFactory> & ssf,
const bool bCheckExisting );
const com::sun::star::uno::Reference<com::sun::star::lang::XSingleServiceFactory> & ssf );
PyRef PyUNOStruct_new (
const com::sun::star::uno::Any &targetInterface,
const com::sun::star::uno::Reference<com::sun::star::lang::XSingleServiceFactory> &ssf );
typedef struct
{
@ -283,6 +287,7 @@ PyRef getBoolClass( const Runtime &);
PyRef getCharClass( const Runtime &);
PyRef getByteSequenceClass( const Runtime & );
PyRef getPyUnoClass();
PyRef getPyUnoStructClass();
PyRef getClass( const OUString & name , const Runtime & runtime );
PyRef getAnyClass( const Runtime &);
PyObject *PyUNO_invoke( PyObject *object, const char *name , PyObject *args );

View file

@ -406,7 +406,7 @@ static PyObject *createUnoStructHelper(
if (idl_class.is ())
{
idl_class->createObject (IdlStruct);
PyRef returnCandidate( PyUNO_new( IdlStruct, c->xInvocation, false ) );
PyRef returnCandidate( PyUNOStruct_new( IdlStruct, c->xInvocation ) );
PyUNO *me = reinterpret_cast<PyUNO*>( returnCandidate.get() );
TypeDescription desc( typeName );
OSL_ASSERT( desc.is() ); // could already instantiate an XInvocation2 !
@ -862,6 +862,7 @@ extern "C"
PyObject* PyInit_pyuno()
{
PyUNO_initType();
PyUNOStruct_initType();
// noop when called already, otherwise needed to allow multiple threads
PyEval_InitThreads();
static struct PyModuleDef moduledef =
@ -882,6 +883,7 @@ PyObject* PyInit_pyuno()
void initpyuno()
{
PyUNO_initType();
PyUNOStruct_initType();
PyEval_InitThreads();
Py_InitModule ("pyuno", PyUNOModule_methods);
}

View file

@ -481,7 +481,7 @@ PyRef Runtime::any2PyObject (const Any &a ) const
case typelib_TypeClass_STRUCT:
{
PyRef excClass = getClass( a.getValueType().getTypeName(), *this );
PyRef value = PyUNO_new( a, getImpl()->cargo->xInvocation, false );
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 );
@ -556,7 +556,7 @@ PyRef Runtime::any2PyObject (const Any &a ) const
if (!tmp_interface.is ())
return Py_None;
return PyUNO_new (a, getImpl()->cargo->xInvocation, true);
return PyUNO_new( a, getImpl()->cargo->xInvocation );
}
default:
{
@ -802,27 +802,21 @@ Any Runtime::pyObject2Any ( const PyRef & source, enum ConversionMode mode ) con
}
else if( PyObject_IsInstance( o, getPyUnoClass().get() ) )
{
PyUNO* o_pi;
o_pi = reinterpret_cast<PyUNO*>(o);
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);
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 ();
if (!my_mh.is())
{
throw RuntimeException(
"struct wrapper does not support XMaterialHolder" );
}
else
{
a = o_pi->members->wrappedObject;
}
a = my_mh->getMaterial();
}
else if( PyObject_IsInstance( o, getCharClass( runtime ).get() ) )
{

View file

@ -0,0 +1,393 @@
/* -*- Mode: C++; 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 <sal/config.h>
#include <algorithm>
#include <cassert>
#include "pyuno_impl.hxx"
#include <rtl/strbuf.hxx>
#include <osl/diagnose.h>
#include <typelib/typedescription.hxx>
#include <com/sun/star/beans/XMaterialHolder.hpp>
using com::sun::star::uno::Sequence;
using com::sun::star::uno::Reference;
using com::sun::star::uno::XInterface;
using com::sun::star::uno::Any;
using com::sun::star::uno::makeAny;
using com::sun::star::uno::UNO_QUERY;
using com::sun::star::uno::Type;
using com::sun::star::uno::TypeClass;
using com::sun::star::uno::RuntimeException;
using com::sun::star::uno::Exception;
using com::sun::star::lang::XSingleServiceFactory;
using com::sun::star::lang::XUnoTunnel;
using com::sun::star::script::XInvocation2;
using com::sun::star::beans::XMaterialHolder;
namespace pyuno
{
void PyUNOStruct_del( PyObject* self )
{
PyUNO *me = reinterpret_cast<PyUNO*>( self );
{
PyThreadDetach antiguard;
delete me->members;
}
PyObject_Del( self );
}
PyObject *PyUNOStruct_str( PyObject *self )
{
PyUNO *me = reinterpret_cast<PyUNO*>( self );
OStringBuffer buf;
Reference<XMaterialHolder> rHolder( me->members->xInvocation,UNO_QUERY );
if( rHolder.is() )
{
PyThreadDetach antiguard;
Any a = rHolder->getMaterial();
OUString s = val2str( a.getValue(), a.getValueType().getTypeLibType() );
buf.append( OUStringToOString( s, RTL_TEXTENCODING_ASCII_US ) );
}
return PyStr_FromString( buf.getStr());
}
PyObject *PyUNOStruct_repr( PyObject *self )
{
PyUNO *me = reinterpret_cast<PyUNO*>( self );
PyObject *ret = 0;
if( me->members->wrappedObject.getValueType().getTypeClass()
== com::sun::star::uno::TypeClass_EXCEPTION )
{
Reference< XMaterialHolder > rHolder(me->members->xInvocation,UNO_QUERY);
if( rHolder.is() )
{
Any a = rHolder->getMaterial();
Exception e;
a >>= e;
ret = ustring2PyUnicode(e.Message ).getAcquired();
}
}
else
{
ret = PyUNOStruct_str( self );
}
return ret;
}
PyObject* PyUNOStruct_dir( PyObject *self )
{
PyUNO *me = reinterpret_cast<PyUNO*>( self );
PyObject* member_list = NULL;
try
{
member_list = PyList_New( 0 );
for( auto aMember : me->members->xInvocation->getMemberNames() )
{
// setitem steals a reference
PyList_Append( member_list, ustring2PyString( aMember ).getAcquired() );
}
}
catch( const RuntimeException &e )
{
raisePyExceptionWithAny( makeAny(e) );
}
return member_list;
}
PyObject* PyUNOStruct_getattr( PyObject* self, char* name )
{
PyUNO *me = reinterpret_cast<PyUNO*>( self );
try
{
Runtime runtime;
me = reinterpret_cast<PyUNO*>(self);
if (strcmp (name, "__dict__") == 0)
{
Py_INCREF (Py_TYPE(me)->tp_dict);
return Py_TYPE(me)->tp_dict;
}
if( strcmp( name, "__class__" ) == 0 )
{
return getClass(
me->members->wrappedObject.getValueType().getTypeName(), runtime ).getAcquired();
}
PyObject *pRet = PyObject_GenericGetAttr( self, PyUnicode_FromString( name ) );
if( pRet )
return pRet;
PyErr_Clear();
OUString attrName( OUString::createFromAscii( name ) );
if( me->members->xInvocation->hasProperty( attrName ) )
{
//Return the value of the property
Any anyRet;
{
PyThreadDetach antiguard;
anyRet = me->members->xInvocation->getValue( attrName );
}
PyRef ret = runtime.any2PyObject( anyRet );
Py_XINCREF( ret.get() );
return ret.get();
}
//or else...
PyErr_SetString (PyExc_AttributeError, name);
}
catch( const com::sun::star::reflection::InvocationTargetException & e )
{
raisePyExceptionWithAny( e.TargetException );
}
catch( const com::sun::star::beans::UnknownPropertyException & e )
{
raisePyExceptionWithAny( makeAny(e) );
}
catch( const com::sun::star::lang::IllegalArgumentException &e )
{
raisePyExceptionWithAny( makeAny(e) );
}
catch( const com::sun::star::script::CannotConvertException &e )
{
raisePyExceptionWithAny( makeAny(e) );
}
catch( const RuntimeException &e )
{
raisePyExceptionWithAny( makeAny(e) );
}
return NULL;
}
int PyUNOStruct_setattr (PyObject* self, char* name, PyObject* value)
{
PyUNO* me;
me = reinterpret_cast<PyUNO*>(self);
try
{
Runtime runtime;
Any val= runtime.pyObject2Any(value, ACCEPT_UNO_ANY);
OUString attrName( OUString::createFromAscii( name ) );
{
PyThreadDetach antiguard;
if (me->members->xInvocation->hasProperty (attrName))
{
me->members->xInvocation->setValue (attrName, val);
return 0; //Keep with Python's boolean system
}
}
}
catch( const com::sun::star::reflection::InvocationTargetException & e )
{
raisePyExceptionWithAny( e.TargetException );
return 1;
}
catch( const com::sun::star::beans::UnknownPropertyException & e )
{
raisePyExceptionWithAny( makeAny(e) );
return 1;
}
catch( const com::sun::star::script::CannotConvertException &e )
{
raisePyExceptionWithAny( makeAny(e) );
return 1;
}
catch( const RuntimeException & e )
{
raisePyExceptionWithAny( makeAny( e ) );
return 1;
}
PyErr_SetString (PyExc_AttributeError, name);
return 1; //as above.
}
static PyObject* PyUNOStruct_cmp( PyObject *self, PyObject *that, int op )
{
PyObject *result;
if(op != Py_EQ && op != Py_NE)
{
PyErr_SetString( PyExc_TypeError, "only '==' and '!=' comparisons are defined" );
return 0;
}
if( self == that )
{
result = (op == Py_EQ ? Py_True : Py_False);
Py_INCREF( result );
return result;
}
try
{
Runtime runtime;
if( PyObject_IsInstance( that, getPyUnoStructClass().get() ) )
{
PyUNO *me = reinterpret_cast< PyUNO * > ( self );
PyUNO *other = reinterpret_cast< PyUNO * > ( that );
com::sun::star::uno::TypeClass tcMe = me->members->wrappedObject.getValueTypeClass();
com::sun::star::uno::TypeClass tcOther = other->members->wrappedObject.getValueTypeClass();
if( tcMe == tcOther )
{
if( tcMe == com::sun::star::uno::TypeClass_STRUCT ||
tcMe == com::sun::star::uno::TypeClass_EXCEPTION )
{
Reference< XMaterialHolder > xMe( me->members->xInvocation,UNO_QUERY );
Reference< XMaterialHolder > xOther( other->members->xInvocation,UNO_QUERY );
if( xMe->getMaterial() == xOther->getMaterial() )
{
result = (op == Py_EQ ? Py_True : Py_False);
Py_INCREF( result );
return result;
}
}
}
}
}
catch( const com::sun::star::uno::RuntimeException & e)
{
raisePyExceptionWithAny( makeAny( e ) );
}
result = (op == Py_EQ ? Py_False : Py_True);
Py_INCREF(result);
return result;
}
static PyMethodDef PyUNOStructMethods[] =
{
{"__dir__", reinterpret_cast<PyCFunction>(PyUNOStruct_dir), METH_NOARGS, NULL},
{NULL, NULL, 0, NULL}
};
static PyTypeObject PyUNOStructType =
{
PyVarObject_HEAD_INIT( &PyType_Type, 0 )
"pyuno.struct",
sizeof (PyUNO),
0,
PyUNOStruct_del,
nullptr,
PyUNOStruct_getattr,
PyUNOStruct_setattr,
/* this type does not exist in Python 3: (cmpfunc) */ 0,
PyUNOStruct_repr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
PyUNOStruct_str,
nullptr,
nullptr,
nullptr,
Py_TPFLAGS_HAVE_RICHCOMPARE,
nullptr,
nullptr,
nullptr,
PyUNOStruct_cmp,
0,
nullptr,
nullptr,
PyUNOStructMethods,
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
, 0
#endif
};
int PyUNOStruct_initType()
{
return PyType_Ready( &PyUNOStructType );
}
PyRef getPyUnoStructClass()
{
return PyRef( reinterpret_cast< PyObject * > ( &PyUNOStructType ) );
}
PyRef PyUNOStruct_new (
const Any &targetInterface,
const Reference<XSingleServiceFactory> &ssf )
{
Reference<XInvocation2> xInvocation;
{
PyThreadDetach antiguard;
xInvocation.set(
ssf->createInstanceWithArguments( Sequence<Any>( &targetInterface, 1 ) ), UNO_QUERY );
OSL_ASSERT( xInvocation.is() );
if( !xInvocation.is() )
throw RuntimeException("XInvocation2 not implemented, cannot interact with object");
}
if( !Py_IsInitialized() )
throw RuntimeException();
PyUNO* self = PyObject_New (PyUNO, &PyUNOStructType);
if (self == NULL)
return PyRef(); // == error
self->members = new PyUNOInternals();
self->members->xInvocation = xInvocation;
self->members->wrappedObject = targetInterface;
return PyRef( reinterpret_cast<PyObject*>(self), SAL_NO_ACQUIRE );
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */