xref: /aoo41x/main/pyuno/source/module/pyuno.cxx (revision fef82340)
167c7d1c1SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
367c7d1c1SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
467c7d1c1SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
567c7d1c1SAndrew Rist  * distributed with this work for additional information
667c7d1c1SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
767c7d1c1SAndrew Rist  * to you under the Apache License, Version 2.0 (the
867c7d1c1SAndrew Rist  * "License"); you may not use this file except in compliance
967c7d1c1SAndrew Rist  * with the License.  You may obtain a copy of the License at
1067c7d1c1SAndrew Rist  *
1167c7d1c1SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
1267c7d1c1SAndrew Rist  *
1367c7d1c1SAndrew Rist  * Unless required by applicable law or agreed to in writing,
1467c7d1c1SAndrew Rist  * software distributed under the License is distributed on an
1567c7d1c1SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1667c7d1c1SAndrew Rist  * KIND, either express or implied.  See the License for the
1767c7d1c1SAndrew Rist  * specific language governing permissions and limitations
1867c7d1c1SAndrew Rist  * under the License.
1967c7d1c1SAndrew Rist  *
2067c7d1c1SAndrew Rist  *************************************************************/
2167c7d1c1SAndrew Rist 
2267c7d1c1SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include "pyuno_impl.hxx"
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include <rtl/strbuf.hxx>
27cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include <osl/thread.h>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp>
32cdf0e10cSrcweir #include <com/sun/star/lang/XTypeProvider.hpp>
33cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
34cdf0e10cSrcweir #include <com/sun/star/beans/XMaterialHolder.hpp>
35cdf0e10cSrcweir 
36cdf0e10cSrcweir #define TO_ASCII(x) OUStringToOString( x , RTL_TEXTENCODING_ASCII_US).getStr()
37cdf0e10cSrcweir 
38cdf0e10cSrcweir using rtl::OStringBuffer;
39cdf0e10cSrcweir using rtl::OUStringBuffer;
40cdf0e10cSrcweir using rtl::OUStringToOString;
41cdf0e10cSrcweir using rtl::OUString;
42cdf0e10cSrcweir using com::sun::star::uno::Sequence;
43cdf0e10cSrcweir using com::sun::star::uno::Reference;
44cdf0e10cSrcweir using com::sun::star::uno::XInterface;
45cdf0e10cSrcweir using com::sun::star::uno::Any;
46cdf0e10cSrcweir using com::sun::star::uno::makeAny;
47cdf0e10cSrcweir using com::sun::star::uno::UNO_QUERY;
48cdf0e10cSrcweir using com::sun::star::uno::Type;
49cdf0e10cSrcweir using com::sun::star::uno::TypeClass;
50cdf0e10cSrcweir using com::sun::star::uno::RuntimeException;
51cdf0e10cSrcweir using com::sun::star::uno::Exception;
52cdf0e10cSrcweir using com::sun::star::uno::XComponentContext;
53cdf0e10cSrcweir using com::sun::star::lang::XSingleServiceFactory;
54cdf0e10cSrcweir using com::sun::star::lang::XServiceInfo;
55cdf0e10cSrcweir using com::sun::star::lang::XTypeProvider;
56cdf0e10cSrcweir using com::sun::star::script::XTypeConverter;
57cdf0e10cSrcweir using com::sun::star::script::XInvocation2;
58cdf0e10cSrcweir using com::sun::star::beans::XMaterialHolder;
59cdf0e10cSrcweir 
60cdf0e10cSrcweir namespace pyuno
61cdf0e10cSrcweir {
62cdf0e10cSrcweir 
63cdf0e10cSrcweir PyObject *PyUNO_str( PyObject * self );
64cdf0e10cSrcweir 
PyUNO_del(PyObject * self)65cdf0e10cSrcweir void PyUNO_del (PyObject* self)
66cdf0e10cSrcweir {
67cdf0e10cSrcweir     PyUNO* me = reinterpret_cast< PyUNO* > (self);
68cdf0e10cSrcweir     {
69cdf0e10cSrcweir         PyThreadDetach antiguard;
70cdf0e10cSrcweir         delete me->members;
71cdf0e10cSrcweir     }
72cdf0e10cSrcweir     PyObject_Del (self);
73cdf0e10cSrcweir }
74cdf0e10cSrcweir 
75cdf0e10cSrcweir 
76cdf0e10cSrcweir 
val2str(const void * pVal,typelib_TypeDescriptionReference * pTypeRef,sal_Int32 mode)77cdf0e10cSrcweir OUString val2str( const void * pVal, typelib_TypeDescriptionReference * pTypeRef , sal_Int32 mode ) SAL_THROW( () )
78cdf0e10cSrcweir {
79cdf0e10cSrcweir 	OSL_ASSERT( pVal );
80cdf0e10cSrcweir 	if (pTypeRef->eTypeClass == typelib_TypeClass_VOID)
81cdf0e10cSrcweir 		return OUString( RTL_CONSTASCII_USTRINGPARAM("void") );
82cdf0e10cSrcweir 
83cdf0e10cSrcweir 	OUStringBuffer buf( 64 );
84cdf0e10cSrcweir 	buf.append( (sal_Unicode)'(' );
85cdf0e10cSrcweir 	buf.append( pTypeRef->pTypeName );
86cdf0e10cSrcweir 	buf.append( (sal_Unicode)')' );
87cdf0e10cSrcweir 
88cdf0e10cSrcweir 	switch (pTypeRef->eTypeClass)
89cdf0e10cSrcweir 	{
90cdf0e10cSrcweir 	case typelib_TypeClass_INTERFACE:
91cdf0e10cSrcweir     {
92cdf0e10cSrcweir 		buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("0x") );
93cdf0e10cSrcweir 		buf.append( reinterpret_cast< sal_IntPtr >(*(void **)pVal), 16 );
94cdf0e10cSrcweir         if( VAL2STR_MODE_DEEP == mode )
95cdf0e10cSrcweir         {
96cdf0e10cSrcweir             buf.appendAscii( "{" );        Reference< XInterface > r = *( Reference< XInterface > * ) pVal;
97cdf0e10cSrcweir             Reference< XServiceInfo > serviceInfo( r, UNO_QUERY);
98cdf0e10cSrcweir             Reference< XTypeProvider > typeProvider(r,UNO_QUERY);
99cdf0e10cSrcweir             if( serviceInfo.is() )
100cdf0e10cSrcweir             {
101cdf0e10cSrcweir                 buf.appendAscii("implementationName=" );
102cdf0e10cSrcweir                 buf.append(serviceInfo->getImplementationName() );
103cdf0e10cSrcweir                 buf.appendAscii(", supportedServices={" );
104cdf0e10cSrcweir                 Sequence< OUString > seq = serviceInfo->getSupportedServiceNames();
105cdf0e10cSrcweir                 for( int i = 0 ; i < seq.getLength() ; i ++ )
106cdf0e10cSrcweir                 {
107cdf0e10cSrcweir                     buf.append( seq[i] );
108cdf0e10cSrcweir                     if( i +1 != seq.getLength() )
109cdf0e10cSrcweir                         buf.appendAscii( "," );
110cdf0e10cSrcweir                 }
111cdf0e10cSrcweir                 buf.appendAscii("}");
112cdf0e10cSrcweir             }
113cdf0e10cSrcweir 
114cdf0e10cSrcweir             if( typeProvider.is() )
115cdf0e10cSrcweir             {
116cdf0e10cSrcweir                 buf.appendAscii(", supportedInterfaces={" );
117cdf0e10cSrcweir                 Sequence< Type > seq (typeProvider->getTypes());
118cdf0e10cSrcweir                 for( int i = 0 ; i < seq.getLength() ; i ++ )
119cdf0e10cSrcweir                 {
120cdf0e10cSrcweir                     buf.append(seq[i].getTypeName());
121cdf0e10cSrcweir                     if( i +1 != seq.getLength() )
122cdf0e10cSrcweir                         buf.appendAscii( "," );
123cdf0e10cSrcweir                 }
124cdf0e10cSrcweir                 buf.appendAscii("}");
125cdf0e10cSrcweir             }
126cdf0e10cSrcweir             buf.appendAscii( "}" );
127cdf0e10cSrcweir         }
128cdf0e10cSrcweir 
129cdf0e10cSrcweir 		break;
130cdf0e10cSrcweir     }
131cdf0e10cSrcweir 	case typelib_TypeClass_UNION:
132cdf0e10cSrcweir 	{
133cdf0e10cSrcweir //  		typelib_TypeDescription * pTypeDescr = 0;
134cdf0e10cSrcweir //  		TYPELIB_DANGER_GET( &pTypeDescr, pTypeRef );
135cdf0e10cSrcweir //  		buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("{ ") );
136cdf0e10cSrcweir //  		buf.append( val2str( (char *)pVal + ((typelib_UnionTypeDescription *)pTypeDescr)->nValueOffset,
137cdf0e10cSrcweir //  							 union_getSetType( pVal, pTypeDescr ) ) );
138cdf0e10cSrcweir //  		buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(" }") );
139cdf0e10cSrcweir //  		TYPELIB_DANGER_RELEASE( pTypeDescr );
140cdf0e10cSrcweir 		break;
141cdf0e10cSrcweir 	}
142cdf0e10cSrcweir 	case typelib_TypeClass_STRUCT:
143cdf0e10cSrcweir 	case typelib_TypeClass_EXCEPTION:
144cdf0e10cSrcweir 	{
145cdf0e10cSrcweir 		buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("{ ") );
146cdf0e10cSrcweir 		typelib_TypeDescription * pTypeDescr = 0;
147cdf0e10cSrcweir 		TYPELIB_DANGER_GET( &pTypeDescr, pTypeRef );
148cdf0e10cSrcweir 		OSL_ASSERT( pTypeDescr );
149cdf0e10cSrcweir 
150cdf0e10cSrcweir 		typelib_CompoundTypeDescription * pCompType = (typelib_CompoundTypeDescription *)pTypeDescr;
151cdf0e10cSrcweir 		sal_Int32 nDescr = pCompType->nMembers;
152cdf0e10cSrcweir 
153cdf0e10cSrcweir 		if (pCompType->pBaseTypeDescription)
154cdf0e10cSrcweir 		{
155cdf0e10cSrcweir 			buf.append( val2str( pVal, ((typelib_TypeDescription *)pCompType->pBaseTypeDescription)->pWeakRef,mode ) );
156cdf0e10cSrcweir 			if (nDescr)
157cdf0e10cSrcweir 				buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(", ") );
158cdf0e10cSrcweir 		}
159cdf0e10cSrcweir 
160cdf0e10cSrcweir 		typelib_TypeDescriptionReference ** ppTypeRefs = pCompType->ppTypeRefs;
161cdf0e10cSrcweir 		sal_Int32 * pMemberOffsets = pCompType->pMemberOffsets;
162cdf0e10cSrcweir 		rtl_uString ** ppMemberNames = pCompType->ppMemberNames;
163cdf0e10cSrcweir 
164cdf0e10cSrcweir 		for ( sal_Int32 nPos = 0; nPos < nDescr; ++nPos )
165cdf0e10cSrcweir 		{
166cdf0e10cSrcweir 			buf.append( ppMemberNames[nPos] );
167cdf0e10cSrcweir 			buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(" = ") );
168cdf0e10cSrcweir 			typelib_TypeDescription * pMemberType = 0;
169cdf0e10cSrcweir 			TYPELIB_DANGER_GET( &pMemberType, ppTypeRefs[nPos] );
170cdf0e10cSrcweir 			buf.append( val2str( (char *)pVal + pMemberOffsets[nPos], pMemberType->pWeakRef, mode ) );
171cdf0e10cSrcweir 			TYPELIB_DANGER_RELEASE( pMemberType );
172cdf0e10cSrcweir 			if (nPos < (nDescr -1))
173cdf0e10cSrcweir 				buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(", ") );
174cdf0e10cSrcweir 		}
175cdf0e10cSrcweir 
176cdf0e10cSrcweir 		TYPELIB_DANGER_RELEASE( pTypeDescr );
177cdf0e10cSrcweir 
178cdf0e10cSrcweir 		buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(" }") );
179cdf0e10cSrcweir 		break;
180cdf0e10cSrcweir 	}
181cdf0e10cSrcweir 	case typelib_TypeClass_SEQUENCE:
182cdf0e10cSrcweir 	{
183cdf0e10cSrcweir 		typelib_TypeDescription * pTypeDescr = 0;
184cdf0e10cSrcweir 		TYPELIB_DANGER_GET( &pTypeDescr, pTypeRef );
185cdf0e10cSrcweir 
186cdf0e10cSrcweir 		uno_Sequence * pSequence = *(uno_Sequence **)pVal;
187cdf0e10cSrcweir 		typelib_TypeDescription * pElementTypeDescr = 0;
188cdf0e10cSrcweir 		TYPELIB_DANGER_GET( &pElementTypeDescr, ((typelib_IndirectTypeDescription *)pTypeDescr)->pType );
189cdf0e10cSrcweir 
190cdf0e10cSrcweir 		sal_Int32 nElementSize = pElementTypeDescr->nSize;
191cdf0e10cSrcweir 		sal_Int32 nElements	   = pSequence->nElements;
192cdf0e10cSrcweir 
193cdf0e10cSrcweir 		if (nElements)
194cdf0e10cSrcweir 		{
195cdf0e10cSrcweir 			buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("{ ") );
196cdf0e10cSrcweir 			char * pElements = pSequence->elements;
197cdf0e10cSrcweir 			for ( sal_Int32 nPos = 0; nPos < nElements; ++nPos )
198cdf0e10cSrcweir 			{
199cdf0e10cSrcweir 				buf.append( val2str( pElements + (nElementSize * nPos), pElementTypeDescr->pWeakRef, mode ) );
200cdf0e10cSrcweir 				if (nPos < (nElements -1))
201cdf0e10cSrcweir 					buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(", ") );
202cdf0e10cSrcweir 			}
203cdf0e10cSrcweir 			buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(" }") );
204cdf0e10cSrcweir 		}
205cdf0e10cSrcweir 		else
206cdf0e10cSrcweir 		{
207cdf0e10cSrcweir 			buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("{}") );
208cdf0e10cSrcweir 		}
209cdf0e10cSrcweir 		TYPELIB_DANGER_RELEASE( pElementTypeDescr );
210cdf0e10cSrcweir 		TYPELIB_DANGER_RELEASE( pTypeDescr );
211cdf0e10cSrcweir 		break;
212cdf0e10cSrcweir 	}
213cdf0e10cSrcweir 	case typelib_TypeClass_ANY:
214cdf0e10cSrcweir 		buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("{ ") );
215cdf0e10cSrcweir 		buf.append( val2str( ((uno_Any *)pVal)->pData,
216cdf0e10cSrcweir 							 ((uno_Any *)pVal)->pType ,
217cdf0e10cSrcweir                              mode) );
218cdf0e10cSrcweir 		buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(" }") );
219cdf0e10cSrcweir 		break;
220cdf0e10cSrcweir 	case typelib_TypeClass_TYPE:
221cdf0e10cSrcweir 		buf.append( (*(typelib_TypeDescriptionReference **)pVal)->pTypeName );
222cdf0e10cSrcweir 		break;
223cdf0e10cSrcweir 	case typelib_TypeClass_STRING:
224cdf0e10cSrcweir 		buf.append( (sal_Unicode)'\"' );
225cdf0e10cSrcweir 		buf.append( *(rtl_uString **)pVal );
226cdf0e10cSrcweir 		buf.append( (sal_Unicode)'\"' );
227cdf0e10cSrcweir 		break;
228cdf0e10cSrcweir 	case typelib_TypeClass_ENUM:
229cdf0e10cSrcweir 	{
230cdf0e10cSrcweir 		typelib_TypeDescription * pTypeDescr = 0;
231cdf0e10cSrcweir 		TYPELIB_DANGER_GET( &pTypeDescr, pTypeRef );
232cdf0e10cSrcweir 
233cdf0e10cSrcweir 		sal_Int32 * pValues = ((typelib_EnumTypeDescription *)pTypeDescr)->pEnumValues;
234cdf0e10cSrcweir 		sal_Int32 nPos = ((typelib_EnumTypeDescription *)pTypeDescr)->nEnumValues;
235cdf0e10cSrcweir 		while (nPos--)
236cdf0e10cSrcweir 		{
237cdf0e10cSrcweir 			if (pValues[nPos] == *(int *)pVal)
238cdf0e10cSrcweir 				break;
239cdf0e10cSrcweir 		}
240cdf0e10cSrcweir 		if (nPos >= 0)
241cdf0e10cSrcweir 			buf.append( ((typelib_EnumTypeDescription *)pTypeDescr)->ppEnumNames[nPos] );
242cdf0e10cSrcweir 		else
243cdf0e10cSrcweir 			buf.append( (sal_Unicode)'?' );
244cdf0e10cSrcweir 
245cdf0e10cSrcweir 		TYPELIB_DANGER_RELEASE( pTypeDescr );
246cdf0e10cSrcweir 		break;
247cdf0e10cSrcweir 	}
248cdf0e10cSrcweir 	case typelib_TypeClass_BOOLEAN:
249cdf0e10cSrcweir 		if (*(sal_Bool *)pVal)
250cdf0e10cSrcweir 			buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("true") );
251cdf0e10cSrcweir 		else
252cdf0e10cSrcweir 			buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("false") );
253cdf0e10cSrcweir 		break;
254cdf0e10cSrcweir 	case typelib_TypeClass_CHAR:
255cdf0e10cSrcweir 		buf.append( (sal_Unicode)'\'' );
256cdf0e10cSrcweir 		buf.append( *(sal_Unicode *)pVal );
257cdf0e10cSrcweir 		buf.append( (sal_Unicode)'\'' );
258cdf0e10cSrcweir 		break;
259cdf0e10cSrcweir 	case typelib_TypeClass_FLOAT:
260cdf0e10cSrcweir 		buf.append( *(float *)pVal );
261cdf0e10cSrcweir 		break;
262cdf0e10cSrcweir 	case typelib_TypeClass_DOUBLE:
263cdf0e10cSrcweir 		buf.append( *(double *)pVal );
264cdf0e10cSrcweir 		break;
265cdf0e10cSrcweir 	case typelib_TypeClass_BYTE:
266cdf0e10cSrcweir 		buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("0x") );
267cdf0e10cSrcweir 		buf.append( (sal_Int32)*(sal_Int8 *)pVal, 16 );
268cdf0e10cSrcweir 		break;
269cdf0e10cSrcweir 	case typelib_TypeClass_SHORT:
270cdf0e10cSrcweir 		buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("0x") );
271cdf0e10cSrcweir 		buf.append( (sal_Int32)*(sal_Int16 *)pVal, 16 );
272cdf0e10cSrcweir 		break;
273cdf0e10cSrcweir 	case typelib_TypeClass_UNSIGNED_SHORT:
274cdf0e10cSrcweir 		buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("0x") );
275cdf0e10cSrcweir 		buf.append( (sal_Int32)*(sal_uInt16 *)pVal, 16 );
276cdf0e10cSrcweir 		break;
277cdf0e10cSrcweir 	case typelib_TypeClass_LONG:
278cdf0e10cSrcweir 		buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("0x") );
279cdf0e10cSrcweir 		buf.append( *(sal_Int32 *)pVal, 16 );
280cdf0e10cSrcweir 		break;
281cdf0e10cSrcweir 	case typelib_TypeClass_UNSIGNED_LONG:
282cdf0e10cSrcweir 		buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("0x") );
283cdf0e10cSrcweir 		buf.append( (sal_Int64)*(sal_uInt32 *)pVal, 16 );
284cdf0e10cSrcweir 		break;
285cdf0e10cSrcweir 	case typelib_TypeClass_HYPER:
286cdf0e10cSrcweir 	case typelib_TypeClass_UNSIGNED_HYPER:
287cdf0e10cSrcweir 		buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("0x") );
288cdf0e10cSrcweir #if defined(GCC) && defined(SPARC)
289cdf0e10cSrcweir 		{
290cdf0e10cSrcweir 			sal_Int64 aVal;
291cdf0e10cSrcweir 			*(sal_Int32 *)&aVal = *(sal_Int32 *)pVal;
292cdf0e10cSrcweir 			*((sal_Int32 *)&aVal +1)= *((sal_Int32 *)pVal +1);
293cdf0e10cSrcweir 			buf.append( aVal, 16 );
294cdf0e10cSrcweir 		}
295cdf0e10cSrcweir #else
296cdf0e10cSrcweir 		buf.append( *(sal_Int64 *)pVal, 16 );
297cdf0e10cSrcweir #endif
298cdf0e10cSrcweir 		break;
299cdf0e10cSrcweir 
300cdf0e10cSrcweir 	case typelib_TypeClass_VOID:
301cdf0e10cSrcweir 	case typelib_TypeClass_ARRAY:
302cdf0e10cSrcweir 	case typelib_TypeClass_UNKNOWN:
303cdf0e10cSrcweir 	case typelib_TypeClass_SERVICE:
304cdf0e10cSrcweir 	case typelib_TypeClass_MODULE:
305cdf0e10cSrcweir 	default:
306cdf0e10cSrcweir 		buf.append( (sal_Unicode)'?' );
307cdf0e10cSrcweir 	}
308cdf0e10cSrcweir 
309cdf0e10cSrcweir 	return buf.makeStringAndClear();
310cdf0e10cSrcweir }
311cdf0e10cSrcweir 
312cdf0e10cSrcweir 
PyUNO_repr(PyObject * self)313cdf0e10cSrcweir PyObject *PyUNO_repr( PyObject  * self )
314cdf0e10cSrcweir {
315cdf0e10cSrcweir     PyUNO *me = (PyUNO * ) self;
316cdf0e10cSrcweir     PyObject * ret = 0;
317cdf0e10cSrcweir 
318cdf0e10cSrcweir     if( me->members->wrappedObject.getValueType().getTypeClass()
319cdf0e10cSrcweir         == com::sun::star::uno::TypeClass_EXCEPTION )
320cdf0e10cSrcweir     {
321cdf0e10cSrcweir         Reference< XMaterialHolder > rHolder(me->members->xInvocation,UNO_QUERY);
322cdf0e10cSrcweir         if( rHolder.is() )
323cdf0e10cSrcweir         {
324cdf0e10cSrcweir             Any a = rHolder->getMaterial();
325cdf0e10cSrcweir             Exception e;
326cdf0e10cSrcweir             a >>= e;
327cdf0e10cSrcweir             ret = ustring2PyUnicode(e.Message ).getAcquired();
328cdf0e10cSrcweir         }
329cdf0e10cSrcweir     }
330cdf0e10cSrcweir     else
331cdf0e10cSrcweir     {
332cdf0e10cSrcweir         ret = PyUNO_str( self );
333cdf0e10cSrcweir     }
334cdf0e10cSrcweir     return ret;
335cdf0e10cSrcweir }
336cdf0e10cSrcweir 
PyUNO_invoke(PyObject * object,const char * name,PyObject * args)337cdf0e10cSrcweir PyObject *PyUNO_invoke( PyObject *object, const char *name , PyObject *args )
338cdf0e10cSrcweir {
339cdf0e10cSrcweir     PyRef ret;
340cdf0e10cSrcweir     try
341cdf0e10cSrcweir     {
342cdf0e10cSrcweir         Runtime runtime;
343cdf0e10cSrcweir 
344cdf0e10cSrcweir         PyRef paras,callable;
34577dc4149SPedro Giffuni         if( PyObject_IsInstance( object, getPyUnoClass().get() ) )
346cdf0e10cSrcweir         {
347cdf0e10cSrcweir             PyUNO* me = (PyUNO*) object;
348cdf0e10cSrcweir             OUString attrName = OUString::createFromAscii(name);
349cdf0e10cSrcweir             if (! me->members->xInvocation->hasMethod (attrName))
350cdf0e10cSrcweir             {
351cdf0e10cSrcweir                 OUStringBuffer buf;
352cdf0e10cSrcweir                 buf.appendAscii( "Attribute " );
353cdf0e10cSrcweir                 buf.append( attrName );
354cdf0e10cSrcweir                 buf.appendAscii( " unknown" );
355cdf0e10cSrcweir                 throw RuntimeException( buf.makeStringAndClear(), Reference< XInterface > () );
356cdf0e10cSrcweir             }
357cdf0e10cSrcweir             callable = PyUNO_callable_new (
358cdf0e10cSrcweir                 me->members->xInvocation,
359cdf0e10cSrcweir                 attrName,
360cdf0e10cSrcweir                 ACCEPT_UNO_ANY);
361cdf0e10cSrcweir             paras = args;
362cdf0e10cSrcweir         }
363cdf0e10cSrcweir         else
364cdf0e10cSrcweir         {
365cdf0e10cSrcweir             // clean the tuple from uno.Any !
366cdf0e10cSrcweir             int size = PyTuple_Size( args );
367cdf0e10cSrcweir             { // for CC, keeping ref-count of tuple being 1
368cdf0e10cSrcweir             paras = PyRef(PyTuple_New( size ), SAL_NO_ACQUIRE);
369cdf0e10cSrcweir             }
370cdf0e10cSrcweir             for( int i = 0 ; i < size ;i ++ )
371cdf0e10cSrcweir             {
372cdf0e10cSrcweir                 PyObject * element = PyTuple_GetItem( args , i );
373cdf0e10cSrcweir                 if( PyObject_IsInstance( element , getAnyClass( runtime ).get() ) )
374cdf0e10cSrcweir                 {
375cdf0e10cSrcweir                     element = PyObject_GetAttrString(
376cdf0e10cSrcweir                         element, const_cast< char * >("value") );
377cdf0e10cSrcweir                 }
378cdf0e10cSrcweir                 else
379cdf0e10cSrcweir                 {
380cdf0e10cSrcweir                     Py_XINCREF( element );
381cdf0e10cSrcweir                 }
382cdf0e10cSrcweir                 PyTuple_SetItem( paras.get(), i , element );
383cdf0e10cSrcweir             }
384cdf0e10cSrcweir             callable = PyRef( PyObject_GetAttrString( object , (char*)name ), SAL_NO_ACQUIRE );
385cdf0e10cSrcweir             if( !callable.is() )
386cdf0e10cSrcweir                 return 0;
387cdf0e10cSrcweir         }
388cdf0e10cSrcweir         ret = PyRef( PyObject_CallObject( callable.get(), paras.get() ), SAL_NO_ACQUIRE );
389cdf0e10cSrcweir     }
390cdf0e10cSrcweir     catch (::com::sun::star::lang::IllegalArgumentException &e)
391cdf0e10cSrcweir     {
392cdf0e10cSrcweir         raisePyExceptionWithAny( com::sun::star::uno::makeAny( e ) );
393cdf0e10cSrcweir     }
394cdf0e10cSrcweir     catch (::com::sun::star::script::CannotConvertException &e)
395cdf0e10cSrcweir     {
396cdf0e10cSrcweir         raisePyExceptionWithAny( com::sun::star::uno::makeAny( e ) );
397cdf0e10cSrcweir     }
398cdf0e10cSrcweir     catch (::com::sun::star::uno::RuntimeException &e)
399cdf0e10cSrcweir     {
400cdf0e10cSrcweir         raisePyExceptionWithAny( com::sun::star::uno::makeAny( e ) );
401cdf0e10cSrcweir     }
402cdf0e10cSrcweir     catch (::com::sun::star::uno::Exception &e)
403cdf0e10cSrcweir     {
404cdf0e10cSrcweir         raisePyExceptionWithAny( com::sun::star::uno::makeAny( e ) );
405cdf0e10cSrcweir     }
406cdf0e10cSrcweir 
407cdf0e10cSrcweir     return ret.getAcquired();
408cdf0e10cSrcweir }
409cdf0e10cSrcweir 
PyUNO_str(PyObject * self)410cdf0e10cSrcweir PyObject *PyUNO_str( PyObject * self )
411cdf0e10cSrcweir {
412cdf0e10cSrcweir     PyUNO *me = ( PyUNO * ) self;
413cdf0e10cSrcweir 
414cdf0e10cSrcweir     OStringBuffer buf;
415cdf0e10cSrcweir 
416cdf0e10cSrcweir 
417cdf0e10cSrcweir     if( me->members->wrappedObject.getValueType().getTypeClass()
418cdf0e10cSrcweir         == com::sun::star::uno::TypeClass_STRUCT ||
419cdf0e10cSrcweir         me->members->wrappedObject.getValueType().getTypeClass()
420cdf0e10cSrcweir         == com::sun::star::uno::TypeClass_EXCEPTION)
421cdf0e10cSrcweir     {
422cdf0e10cSrcweir         Reference< XMaterialHolder > rHolder(me->members->xInvocation,UNO_QUERY);
423cdf0e10cSrcweir         if( rHolder.is() )
424cdf0e10cSrcweir         {
425cdf0e10cSrcweir             PyThreadDetach antiguard;
426cdf0e10cSrcweir             Any a = rHolder->getMaterial();
427cdf0e10cSrcweir             OUString s = val2str( (void*) a.getValue(), a.getValueType().getTypeLibType() );
428cdf0e10cSrcweir             buf.append( OUStringToOString(s,RTL_TEXTENCODING_ASCII_US) );
429cdf0e10cSrcweir         }
430cdf0e10cSrcweir     }
431cdf0e10cSrcweir     else
432cdf0e10cSrcweir     {
433cdf0e10cSrcweir         // a common UNO object
434cdf0e10cSrcweir         PyThreadDetach antiguard;
435cdf0e10cSrcweir         buf.append( "pyuno object " );
436cdf0e10cSrcweir 
437cdf0e10cSrcweir         OUString s = val2str( (void*)me->members->wrappedObject.getValue(),
438cdf0e10cSrcweir                               me->members->wrappedObject.getValueType().getTypeLibType() );
439cdf0e10cSrcweir         buf.append( OUStringToOString(s,RTL_TEXTENCODING_ASCII_US) );
440cdf0e10cSrcweir     }
441cdf0e10cSrcweir 
44277dc4149SPedro Giffuni     return PYSTR_FROMSTR( buf.getStr() );
443cdf0e10cSrcweir }
444cdf0e10cSrcweir 
44577dc4149SPedro Giffuni #if PY_MAJOR_VERSION >= 3
PyUNO_getattr(PyObject * self,PyObject * attr_name)44677dc4149SPedro Giffuni PyObject* PyUNO_getattr (PyObject* self, PyObject* attr_name)
44777dc4149SPedro Giffuni #else
448cdf0e10cSrcweir PyObject* PyUNO_getattr (PyObject* self, char* name)
44977dc4149SPedro Giffuni #endif
450cdf0e10cSrcweir {
451cdf0e10cSrcweir     PyUNO* me;
452cdf0e10cSrcweir 
45377dc4149SPedro Giffuni #if PY_VERSION_HEX >= 0x03030000
45477dc4149SPedro Giffuni     char *name = PyUnicode_AsUTF8(attr_name);
45577dc4149SPedro Giffuni #elif PY_MAJOR_VERSION >= 3
45677dc4149SPedro Giffuni     PyRef pUtf8(PyUnicode_AsUTF8String(attr_name), SAL_NO_ACQUIRE);
45777dc4149SPedro Giffuni     char *name = PyBytes_AsString(pUtf8.get());
45877dc4149SPedro Giffuni #endif
459cdf0e10cSrcweir     try
460cdf0e10cSrcweir     {
461cdf0e10cSrcweir 
462cdf0e10cSrcweir         Runtime runtime;
463cdf0e10cSrcweir 
464cdf0e10cSrcweir         me = (PyUNO*) self;
46577dc4149SPedro Giffuni #if PY_MAJOR_VERSION < 3
466cdf0e10cSrcweir         //Handle Python dir () stuff first...
467cdf0e10cSrcweir         if (strcmp (name, "__members__") == 0)
468cdf0e10cSrcweir         {
469cdf0e10cSrcweir             PyObject* member_list;
470cdf0e10cSrcweir             Sequence<OUString> oo_member_list;
471cdf0e10cSrcweir 
472cdf0e10cSrcweir             oo_member_list = me->members->xInvocation->getMemberNames ();
473cdf0e10cSrcweir             member_list = PyList_New (oo_member_list.getLength ());
474cdf0e10cSrcweir             for (int i = 0; i < oo_member_list.getLength (); i++)
475cdf0e10cSrcweir             {
476cdf0e10cSrcweir                 // setitem steals a reference
477cdf0e10cSrcweir                 PyList_SetItem (member_list, i, ustring2PyString(oo_member_list[i]).getAcquired() );
478cdf0e10cSrcweir             }
479cdf0e10cSrcweir             return member_list;
480cdf0e10cSrcweir         }
48177dc4149SPedro Giffuni #endif
48277dc4149SPedro Giffuni 
483cdf0e10cSrcweir         if (strcmp (name, "__dict__") == 0)
484cdf0e10cSrcweir         {
485cdf0e10cSrcweir             Py_INCREF (Py_None);
486cdf0e10cSrcweir             return Py_None;
487cdf0e10cSrcweir         }
48877dc4149SPedro Giffuni #if PY_MAJOR_VERSION < 3
489cdf0e10cSrcweir         if (strcmp (name, "__methods__") == 0)
490cdf0e10cSrcweir         {
491cdf0e10cSrcweir             Py_INCREF (Py_None);
492cdf0e10cSrcweir             return Py_None;
493cdf0e10cSrcweir         }
49477dc4149SPedro Giffuni #endif
495cdf0e10cSrcweir         if (strcmp (name, "__class__") == 0)
496cdf0e10cSrcweir         {
497cdf0e10cSrcweir             if( me->members->wrappedObject.getValueTypeClass() ==
498cdf0e10cSrcweir                 com::sun::star::uno::TypeClass_STRUCT ||
499cdf0e10cSrcweir                 me->members->wrappedObject.getValueTypeClass() ==
500cdf0e10cSrcweir                 com::sun::star::uno::TypeClass_EXCEPTION )
501cdf0e10cSrcweir             {
502cdf0e10cSrcweir                 return getClass(
503cdf0e10cSrcweir                     me->members->wrappedObject.getValueType().getTypeName(), runtime ).getAcquired();
504cdf0e10cSrcweir             }
505cdf0e10cSrcweir             Py_INCREF (Py_None);
506cdf0e10cSrcweir             return Py_None;
507cdf0e10cSrcweir         }
508cdf0e10cSrcweir 
509cdf0e10cSrcweir         OUString attrName( OUString::createFromAscii( name ) );
510cdf0e10cSrcweir         //We need to find out if it's a method...
511cdf0e10cSrcweir         if (me->members->xInvocation->hasMethod (attrName))
512cdf0e10cSrcweir         {
513cdf0e10cSrcweir             //Create a callable object to invoke this...
514cdf0e10cSrcweir             PyRef ret = PyUNO_callable_new (
515cdf0e10cSrcweir                 me->members->xInvocation,
516*fef82340SHerbert Dürr                 attrName);
517cdf0e10cSrcweir             Py_XINCREF( ret.get() );
518cdf0e10cSrcweir             return ret.get();
519cdf0e10cSrcweir 
520cdf0e10cSrcweir         }
521cdf0e10cSrcweir 
522cdf0e10cSrcweir         //or a property
523cdf0e10cSrcweir         if (me->members->xInvocation->hasProperty ( attrName))
524cdf0e10cSrcweir         {
525cdf0e10cSrcweir             //Return the value of the property
526cdf0e10cSrcweir             Any anyRet;
527cdf0e10cSrcweir             {
528cdf0e10cSrcweir                 PyThreadDetach antiguard;
529cdf0e10cSrcweir                 anyRet = me->members->xInvocation->getValue (attrName);
530cdf0e10cSrcweir             }
531cdf0e10cSrcweir             PyRef ret = runtime.any2PyObject(anyRet);
532cdf0e10cSrcweir             Py_XINCREF( ret.get() );
533cdf0e10cSrcweir             return ret.get();
534cdf0e10cSrcweir         }
535cdf0e10cSrcweir 
536cdf0e10cSrcweir         //or else...
537cdf0e10cSrcweir         PyErr_SetString (PyExc_AttributeError, name);
538cdf0e10cSrcweir     }
539cdf0e10cSrcweir     catch( com::sun::star::reflection::InvocationTargetException & e )
540cdf0e10cSrcweir     {
541cdf0e10cSrcweir         raisePyExceptionWithAny( makeAny(e.TargetException) );
542cdf0e10cSrcweir     }
543cdf0e10cSrcweir     catch( com::sun::star::beans::UnknownPropertyException & e )
544cdf0e10cSrcweir     {
545cdf0e10cSrcweir         raisePyExceptionWithAny( makeAny(e) );
546cdf0e10cSrcweir     }
547cdf0e10cSrcweir     catch( com::sun::star::lang::IllegalArgumentException &e )
548cdf0e10cSrcweir     {
549cdf0e10cSrcweir         raisePyExceptionWithAny( makeAny(e) );
550cdf0e10cSrcweir     }
551cdf0e10cSrcweir     catch( com::sun::star::script::CannotConvertException &e )
552cdf0e10cSrcweir     {
553cdf0e10cSrcweir         raisePyExceptionWithAny( makeAny(e) );
554cdf0e10cSrcweir     }
555cdf0e10cSrcweir     catch( RuntimeException &e )
556cdf0e10cSrcweir     {
557cdf0e10cSrcweir         raisePyExceptionWithAny( makeAny(e) );
558cdf0e10cSrcweir     }
559cdf0e10cSrcweir 
560cdf0e10cSrcweir     return NULL;
561cdf0e10cSrcweir }
562cdf0e10cSrcweir 
56377dc4149SPedro Giffuni #if PY_MAJOR_VERSION >= 3
PyUNO_setattr(PyObject * self,PyObject * attr_name,PyObject * value)56477dc4149SPedro Giffuni int PyUNO_setattr (PyObject* self, PyObject* attr_name, PyObject* value)
56577dc4149SPedro Giffuni #else
566cdf0e10cSrcweir int PyUNO_setattr (PyObject* self, char* name, PyObject* value)
56777dc4149SPedro Giffuni #endif
568cdf0e10cSrcweir {
569cdf0e10cSrcweir     PyUNO* me;
570cdf0e10cSrcweir 
57177dc4149SPedro Giffuni #if PY_VERSION_HEX >= 0x03030000
57277dc4149SPedro Giffuni     char *name = PyUnicode_AsUTF8(attr_name);
57377dc4149SPedro Giffuni #elif PY_MAJOR_VERSION >= 3
57477dc4149SPedro Giffuni     PyRef pUtf8(PyUnicode_AsUTF8String(attr_name), SAL_NO_ACQUIRE);
57577dc4149SPedro Giffuni     char *name = PyBytes_AsString(pUtf8.get());
57677dc4149SPedro Giffuni #endif
577cdf0e10cSrcweir     me = (PyUNO*) self;
578cdf0e10cSrcweir     try
579cdf0e10cSrcweir     {
580cdf0e10cSrcweir         Runtime runtime;
581cdf0e10cSrcweir         Any val= runtime.pyObject2Any(value, ACCEPT_UNO_ANY);
582cdf0e10cSrcweir 
583cdf0e10cSrcweir         OUString attrName( OUString::createFromAscii( name ) );
584cdf0e10cSrcweir         {
585cdf0e10cSrcweir             PyThreadDetach antiguard;
586cdf0e10cSrcweir             if (me->members->xInvocation->hasProperty (attrName))
587cdf0e10cSrcweir             {
588cdf0e10cSrcweir                 me->members->xInvocation->setValue (attrName, val);
589cdf0e10cSrcweir                 return 0; //Keep with Python's boolean system
590cdf0e10cSrcweir             }
591cdf0e10cSrcweir         }
592cdf0e10cSrcweir     }
593cdf0e10cSrcweir     catch( com::sun::star::reflection::InvocationTargetException & e )
594cdf0e10cSrcweir     {
595cdf0e10cSrcweir         raisePyExceptionWithAny( makeAny(e.TargetException) );
596cdf0e10cSrcweir         return 1;
597cdf0e10cSrcweir     }
598cdf0e10cSrcweir     catch( com::sun::star::beans::UnknownPropertyException & e )
599cdf0e10cSrcweir     {
600cdf0e10cSrcweir         raisePyExceptionWithAny( makeAny(e) );
601cdf0e10cSrcweir         return 1;
602cdf0e10cSrcweir     }
603cdf0e10cSrcweir     catch( com::sun::star::script::CannotConvertException &e )
604cdf0e10cSrcweir     {
605cdf0e10cSrcweir         raisePyExceptionWithAny( makeAny(e) );
606cdf0e10cSrcweir         return 1;
607cdf0e10cSrcweir     }
608cdf0e10cSrcweir     catch( RuntimeException & e )
609cdf0e10cSrcweir     {
610cdf0e10cSrcweir         raisePyExceptionWithAny( makeAny( e ) );
611cdf0e10cSrcweir         return 1;
612cdf0e10cSrcweir     }
613cdf0e10cSrcweir     PyErr_SetString (PyExc_AttributeError, name);
614cdf0e10cSrcweir     return 1; //as above.
615cdf0e10cSrcweir }
616cdf0e10cSrcweir 
61777dc4149SPedro Giffuni #if PY_MAJOR_VERSION >= 3
PyUNO_dir(PyObject * self,PyObject * that)61877dc4149SPedro Giffuni static PyObject *PyUNO_dir( PyObject *self, PyObject *that )
61977dc4149SPedro Giffuni {
62077dc4149SPedro Giffuni     PyUNO* me;
62177dc4149SPedro Giffuni     PyObject* member_list;
62277dc4149SPedro Giffuni     Sequence<OUString> oo_member_list;
62377dc4149SPedro Giffuni 
62477dc4149SPedro Giffuni     me = (PyUNO*) self;
62577dc4149SPedro Giffuni     oo_member_list = me->members->xInvocation->getMemberNames ();
62677dc4149SPedro Giffuni     member_list = PyList_New (oo_member_list.getLength ());
62777dc4149SPedro Giffuni     for (int i = 0; i < oo_member_list.getLength (); i++)
62877dc4149SPedro Giffuni     {
62977dc4149SPedro Giffuni         // setitem steals a reference
63077dc4149SPedro Giffuni         PyList_SetItem (member_list, i, ustring2PyUnicode(oo_member_list[i]).getAcquired() );
63177dc4149SPedro Giffuni     }
63277dc4149SPedro Giffuni     return member_list;
63377dc4149SPedro Giffuni }
63477dc4149SPedro Giffuni 
PyUNO_richcompare(PyObject * self,PyObject * that,int op)63577dc4149SPedro Giffuni static PyObject *PyUNO_richcompare( PyObject *self, PyObject *that, int op )
63677dc4149SPedro Giffuni {
63777dc4149SPedro Giffuni     switch (op)
63877dc4149SPedro Giffuni     {
63977dc4149SPedro Giffuni     case Py_EQ:
64077dc4149SPedro Giffuni     case Py_NE:
64177dc4149SPedro Giffuni         if( self == that )
64277dc4149SPedro Giffuni         {
64377dc4149SPedro Giffuni             if (op == Py_EQ)
64477dc4149SPedro Giffuni                 Py_RETURN_TRUE;
64577dc4149SPedro Giffuni             else
64677dc4149SPedro Giffuni                 Py_RETURN_FALSE;
64777dc4149SPedro Giffuni         }
64877dc4149SPedro Giffuni         try
64977dc4149SPedro Giffuni         {
65077dc4149SPedro Giffuni             Runtime runtime;
65177dc4149SPedro Giffuni             if( PyObject_IsInstance( that, getPyUnoClass().get() ) )
65277dc4149SPedro Giffuni             {
65377dc4149SPedro Giffuni                 PyUNO *me = reinterpret_cast< PyUNO*> ( self );
65477dc4149SPedro Giffuni                 PyUNO *other = reinterpret_cast< PyUNO *> (that );
65577dc4149SPedro Giffuni                 com::sun::star::uno::TypeClass tcMe = me->members->wrappedObject.getValueTypeClass();
65677dc4149SPedro Giffuni                 com::sun::star::uno::TypeClass tcOther = other->members->wrappedObject.getValueTypeClass();
65777dc4149SPedro Giffuni 
65877dc4149SPedro Giffuni                 if( tcMe == tcOther )
65977dc4149SPedro Giffuni                 {
66077dc4149SPedro Giffuni                     if( tcMe == com::sun::star::uno::TypeClass_STRUCT ||
66177dc4149SPedro Giffuni                         tcMe == com::sun::star::uno::TypeClass_EXCEPTION )
66277dc4149SPedro Giffuni                     {
66377dc4149SPedro Giffuni                         Reference< XMaterialHolder > xMe( me->members->xInvocation,UNO_QUERY);
66477dc4149SPedro Giffuni                         Reference< XMaterialHolder > xOther( other->members->xInvocation,UNO_QUERY );
66577dc4149SPedro Giffuni                         if( xMe->getMaterial() == xOther->getMaterial() )
66677dc4149SPedro Giffuni                         {
66777dc4149SPedro Giffuni                             if (op == Py_EQ)
66877dc4149SPedro Giffuni                                 Py_RETURN_TRUE;
66977dc4149SPedro Giffuni                             else
67077dc4149SPedro Giffuni                                 Py_RETURN_FALSE;
67177dc4149SPedro Giffuni                         }
67277dc4149SPedro Giffuni                     }
67377dc4149SPedro Giffuni                     else if( tcMe == com::sun::star::uno::TypeClass_INTERFACE )
67477dc4149SPedro Giffuni                     {
67577dc4149SPedro Giffuni                         if( me->members->wrappedObject == other->members->wrappedObject )
67677dc4149SPedro Giffuni                         {
67777dc4149SPedro Giffuni                             if (op == Py_EQ)
67877dc4149SPedro Giffuni                                 Py_RETURN_TRUE;
67977dc4149SPedro Giffuni                             else
68077dc4149SPedro Giffuni                                 Py_RETURN_FALSE;
68177dc4149SPedro Giffuni                         }
68277dc4149SPedro Giffuni                     }
68377dc4149SPedro Giffuni                 }
68477dc4149SPedro Giffuni             }
68577dc4149SPedro Giffuni             if (op == Py_EQ)
68677dc4149SPedro Giffuni                 Py_RETURN_FALSE;
68777dc4149SPedro Giffuni             else
68877dc4149SPedro Giffuni                 Py_RETURN_TRUE;
68977dc4149SPedro Giffuni         }
69077dc4149SPedro Giffuni         catch( com::sun::star::uno::RuntimeException & e)
69177dc4149SPedro Giffuni         {
69277dc4149SPedro Giffuni             raisePyExceptionWithAny( makeAny( e ) );
69377dc4149SPedro Giffuni         }
69477dc4149SPedro Giffuni         break;
69577dc4149SPedro Giffuni     default:
69677dc4149SPedro Giffuni         PyErr_SetString(Py_NotImplemented, "not implemented");
69777dc4149SPedro Giffuni         break;
69877dc4149SPedro Giffuni     }
69977dc4149SPedro Giffuni 
70077dc4149SPedro Giffuni     return NULL;
70177dc4149SPedro Giffuni }
70277dc4149SPedro Giffuni 
70377dc4149SPedro Giffuni 
70477dc4149SPedro Giffuni static struct PyMethodDef PyUNO_methods[] = {
70577dc4149SPedro Giffuni     { "__dir__", (PyCFunction)PyUNO_dir, METH_VARARGS, NULL},
70677dc4149SPedro Giffuni     { NULL, NULL }
70777dc4149SPedro Giffuni };
70877dc4149SPedro Giffuni 
70977dc4149SPedro Giffuni #else
710cdf0e10cSrcweir // ensure object identity and struct equality
PyUNO_cmp(PyObject * self,PyObject * that)711cdf0e10cSrcweir static int PyUNO_cmp( PyObject *self, PyObject *that )
712cdf0e10cSrcweir {
713cdf0e10cSrcweir     if( self == that )
714cdf0e10cSrcweir         return 0;
715cdf0e10cSrcweir     int retDefault = self > that ? 1 : -1;
716cdf0e10cSrcweir     try
717cdf0e10cSrcweir     {
718cdf0e10cSrcweir         Runtime runtime;
71977dc4149SPedro Giffuni         if( PyObject_IsInstance( that, getPyUnoClass().get() ) )
720cdf0e10cSrcweir         {
721cdf0e10cSrcweir 
722cdf0e10cSrcweir             PyUNO *me = reinterpret_cast< PyUNO*> ( self );
723cdf0e10cSrcweir             PyUNO *other = reinterpret_cast< PyUNO *> (that );
724cdf0e10cSrcweir             com::sun::star::uno::TypeClass tcMe = me->members->wrappedObject.getValueTypeClass();
725cdf0e10cSrcweir             com::sun::star::uno::TypeClass tcOther = other->members->wrappedObject.getValueTypeClass();
726cdf0e10cSrcweir 
727cdf0e10cSrcweir             if( tcMe == tcOther )
728cdf0e10cSrcweir             {
729cdf0e10cSrcweir                 if( tcMe == com::sun::star::uno::TypeClass_STRUCT ||
730cdf0e10cSrcweir                     tcMe == com::sun::star::uno::TypeClass_EXCEPTION )
731cdf0e10cSrcweir                 {
732cdf0e10cSrcweir                     Reference< XMaterialHolder > xMe( me->members->xInvocation,UNO_QUERY);
733cdf0e10cSrcweir                     Reference< XMaterialHolder > xOther( other->members->xInvocation,UNO_QUERY );
734cdf0e10cSrcweir                     if( xMe->getMaterial() == xOther->getMaterial() )
735cdf0e10cSrcweir                         return 0;
736cdf0e10cSrcweir                 }
737cdf0e10cSrcweir                 else if( tcMe == com::sun::star::uno::TypeClass_INTERFACE )
738cdf0e10cSrcweir                 {
739cdf0e10cSrcweir                     if( me->members->wrappedObject == other->members->wrappedObject )
740cdf0e10cSrcweir //                     if( me->members->xInvocation == other->members->xInvocation )
741cdf0e10cSrcweir                         return 0;
742cdf0e10cSrcweir                 }
743cdf0e10cSrcweir             }
744cdf0e10cSrcweir         }
745cdf0e10cSrcweir     }
746cdf0e10cSrcweir     catch( com::sun::star::uno::RuntimeException & e)
747cdf0e10cSrcweir     {
748cdf0e10cSrcweir         raisePyExceptionWithAny( makeAny( e ) );
749cdf0e10cSrcweir     }
750cdf0e10cSrcweir     return retDefault;
751cdf0e10cSrcweir }
75277dc4149SPedro Giffuni #endif
753cdf0e10cSrcweir 
754cdf0e10cSrcweir static PyTypeObject PyUNOType =
755cdf0e10cSrcweir {
7565c3821d8SPedro Giffuni     PyVarObject_HEAD_INIT(&PyType_Type, 0)
757cdf0e10cSrcweir     const_cast< char * >("pyuno"),
758cdf0e10cSrcweir     sizeof (PyUNO),
759cdf0e10cSrcweir     0,
760cdf0e10cSrcweir     (destructor) PyUNO_del,
761cdf0e10cSrcweir     (printfunc) 0,
76277dc4149SPedro Giffuni #if PY_MAJOR_VERSION >= 3
76377dc4149SPedro Giffuni     (getattrfunc) 0,
76477dc4149SPedro Giffuni     (setattrfunc) 0,
76577dc4149SPedro Giffuni     0,
76677dc4149SPedro Giffuni #else
76777dc4149SPedro Giffuni     (getattrfunc) PyUNO_getattr, /* tp_getattr */
76877dc4149SPedro Giffuni     (setattrfunc) PyUNO_setattr, /* tp_setattr */
76977dc4149SPedro Giffuni     (cmpfunc) PyUNO_cmp,
77077dc4149SPedro Giffuni #endif
771cdf0e10cSrcweir     (reprfunc) PyUNO_repr,
772cdf0e10cSrcweir     0,
773cdf0e10cSrcweir     0,
774cdf0e10cSrcweir     0,
775cdf0e10cSrcweir     (hashfunc) 0,
776cdf0e10cSrcweir     (ternaryfunc) 0,
777cdf0e10cSrcweir     (reprfunc) PyUNO_str,
77877dc4149SPedro Giffuni #if PY_MAJOR_VERSION >= 3
77977dc4149SPedro Giffuni     (getattrofunc)PyUNO_getattr, /* tp_getattro */
78077dc4149SPedro Giffuni     (setattrofunc)PyUNO_setattr, /* tp_setattro */
78177dc4149SPedro Giffuni #else
782cdf0e10cSrcweir     (getattrofunc)0,
783cdf0e10cSrcweir     (setattrofunc)0,
78477dc4149SPedro Giffuni #endif
785cdf0e10cSrcweir     NULL,
786cdf0e10cSrcweir     0,
787cdf0e10cSrcweir     NULL,
788cdf0e10cSrcweir     (traverseproc)0,
789cdf0e10cSrcweir     (inquiry)0,
79077dc4149SPedro Giffuni #if PY_MAJOR_VERSION >= 3
79177dc4149SPedro Giffuni     PyUNO_richcompare, /* tp_richcompare */
79277dc4149SPedro Giffuni #else
793cdf0e10cSrcweir     (richcmpfunc)0,
79477dc4149SPedro Giffuni #endif
795cdf0e10cSrcweir     0,
796cdf0e10cSrcweir     (getiterfunc)0,
797cdf0e10cSrcweir     (iternextfunc)0,
79877dc4149SPedro Giffuni #if PY_MAJOR_VERSION >= 3
79977dc4149SPedro Giffuni     PyUNO_methods, /* tp_methods */
80077dc4149SPedro Giffuni #else
801cdf0e10cSrcweir     NULL,
80277dc4149SPedro Giffuni #endif
803cdf0e10cSrcweir     NULL,
804cdf0e10cSrcweir     NULL,
805cdf0e10cSrcweir     NULL,
806cdf0e10cSrcweir     NULL,
807cdf0e10cSrcweir     (descrgetfunc)0,
808cdf0e10cSrcweir     (descrsetfunc)0,
809cdf0e10cSrcweir     0,
810cdf0e10cSrcweir     (initproc)0,
811cdf0e10cSrcweir     (allocfunc)0,
812cdf0e10cSrcweir     (newfunc)0,
813cdf0e10cSrcweir     (freefunc)0,
814cdf0e10cSrcweir     (inquiry)0,
815cdf0e10cSrcweir     NULL,
816cdf0e10cSrcweir     NULL,
817cdf0e10cSrcweir     NULL,
818cdf0e10cSrcweir     NULL,
819cdf0e10cSrcweir     NULL,
820cdf0e10cSrcweir     (destructor)0
821cdf0e10cSrcweir #if PY_VERSION_HEX >= 0x02060000
822cdf0e10cSrcweir     , 0
823cdf0e10cSrcweir #endif
824cdf0e10cSrcweir };
825cdf0e10cSrcweir 
getPyUnoClass()82677dc4149SPedro Giffuni PyRef getPyUnoClass()
827cdf0e10cSrcweir {
828cdf0e10cSrcweir     return PyRef( reinterpret_cast< PyObject * > ( &PyUNOType ) );
829cdf0e10cSrcweir }
830cdf0e10cSrcweir 
PyUNO_new(const Any & targetInterface,const Reference<XSingleServiceFactory> & ssf)831cdf0e10cSrcweir PyObject* PyUNO_new (
832cdf0e10cSrcweir     const Any & targetInterface, const Reference<XSingleServiceFactory> &ssf)
833cdf0e10cSrcweir {
834cdf0e10cSrcweir     Reference<XInterface> tmp_interface;
835cdf0e10cSrcweir 
836cdf0e10cSrcweir     targetInterface >>= tmp_interface;
837cdf0e10cSrcweir     if (!tmp_interface.is ())
838cdf0e10cSrcweir     {
839cdf0e10cSrcweir         // empty reference !
840cdf0e10cSrcweir         Py_INCREF( Py_None );
841cdf0e10cSrcweir         return Py_None;
842cdf0e10cSrcweir     }
843cdf0e10cSrcweir 
844cdf0e10cSrcweir     return PyUNO_new_UNCHECKED (targetInterface, ssf);
845cdf0e10cSrcweir }
846cdf0e10cSrcweir 
847cdf0e10cSrcweir 
PyUNO_new_UNCHECKED(const Any & targetInterface,const Reference<XSingleServiceFactory> & ssf)848cdf0e10cSrcweir PyObject* PyUNO_new_UNCHECKED (
849cdf0e10cSrcweir     const Any &targetInterface,
850cdf0e10cSrcweir     const Reference<XSingleServiceFactory> &ssf )
851cdf0e10cSrcweir {
852cdf0e10cSrcweir     PyUNO* self;
853cdf0e10cSrcweir     Sequence<Any> arguments (1);
854cdf0e10cSrcweir     Reference<XInterface> tmp_interface;
855cdf0e10cSrcweir 
856cdf0e10cSrcweir     self = PyObject_New (PyUNO, &PyUNOType);
857cdf0e10cSrcweir     if (self == NULL)
858cdf0e10cSrcweir         return NULL; //NULL == error
859cdf0e10cSrcweir     self->members = new PyUNOInternals();
860cdf0e10cSrcweir 
861cdf0e10cSrcweir     arguments[0] <<= targetInterface;
862cdf0e10cSrcweir     {
863cdf0e10cSrcweir         PyThreadDetach antiguard;
864cdf0e10cSrcweir         tmp_interface = ssf->createInstanceWithArguments (arguments);
865cdf0e10cSrcweir         Reference<XInvocation2> tmp_invocation (tmp_interface, UNO_QUERY);
866cdf0e10cSrcweir         self->members->xInvocation = tmp_invocation;
867cdf0e10cSrcweir         self->members->wrappedObject = targetInterface;
868cdf0e10cSrcweir     }
869cdf0e10cSrcweir     return (PyObject*) self;
870cdf0e10cSrcweir }
871cdf0e10cSrcweir 
872cdf0e10cSrcweir }
873