Kaydet (Commit) 2387c2a4 authored tarafından Matthew J. Francis's avatar Matthew J. Francis Kaydeden (comit) Matthew Francis

Give PyUNO structs/exceptions their own separate type

Change-Id: Ie4c42c623fae1cf39c2e4c643825c9655cd28daa
Reviewed-on: https://gerrit.libreoffice.org/17410Tested-by: 's avatarJenkins <ci@libreoffice.org>
Reviewed-by: 's avatarMatthew Francis <mjay.francis@gmail.com>
üst 90225564
......@@ -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 \
......
......@@ -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() )
......
......@@ -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 );
......
......@@ -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);
}
......
......@@ -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 ();
}
else
if (!my_mh.is())
{
a = o_pi->members->wrappedObject;
throw RuntimeException(
"struct wrapper does not support XMaterialHolder" );
}
else
a = my_mh->getMaterial();
}
else if( PyObject_IsInstance( o, getCharClass( runtime ).get() ) )
{
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment