xref: /AOO41X/main/pyuno/source/module/pyuno.cxx (revision 77dc4149c6400161b1d1514ba77bfa09c512b402)
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
10cdf0e10cSrcweir  *
1167c7d1c1SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
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.
19cdf0e10cSrcweir  *
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 
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 
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 
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 
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;
345*77dc4149SPedro 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                 runtime.getImpl()->cargo->xInvocation,
361cdf0e10cSrcweir                 runtime.getImpl()->cargo->xTypeConverter,
362cdf0e10cSrcweir                 ACCEPT_UNO_ANY);
363cdf0e10cSrcweir             paras = args;
364cdf0e10cSrcweir         }
365cdf0e10cSrcweir         else
366cdf0e10cSrcweir         {
367cdf0e10cSrcweir             // clean the tuple from uno.Any !
368cdf0e10cSrcweir             int size = PyTuple_Size( args );
369cdf0e10cSrcweir             { // for CC, keeping ref-count of tuple being 1
370cdf0e10cSrcweir             paras = PyRef(PyTuple_New( size ), SAL_NO_ACQUIRE);
371cdf0e10cSrcweir             }
372cdf0e10cSrcweir             for( int i = 0 ; i < size ;i ++ )
373cdf0e10cSrcweir             {
374cdf0e10cSrcweir                 PyObject * element = PyTuple_GetItem( args , i );
375cdf0e10cSrcweir                 if( PyObject_IsInstance( element , getAnyClass( runtime ).get() ) )
376cdf0e10cSrcweir                 {
377cdf0e10cSrcweir                     element = PyObject_GetAttrString(
378cdf0e10cSrcweir                         element, const_cast< char * >("value") );
379cdf0e10cSrcweir                 }
380cdf0e10cSrcweir                 else
381cdf0e10cSrcweir                 {
382cdf0e10cSrcweir                     Py_XINCREF( element );
383cdf0e10cSrcweir                 }
384cdf0e10cSrcweir                 PyTuple_SetItem( paras.get(), i , element );
385cdf0e10cSrcweir             }
386cdf0e10cSrcweir             callable = PyRef( PyObject_GetAttrString( object , (char*)name ), SAL_NO_ACQUIRE );
387cdf0e10cSrcweir             if( !callable.is() )
388cdf0e10cSrcweir                 return 0;
389cdf0e10cSrcweir         }
390cdf0e10cSrcweir         ret = PyRef( PyObject_CallObject( callable.get(), paras.get() ), SAL_NO_ACQUIRE );
391cdf0e10cSrcweir     }
392cdf0e10cSrcweir     catch (::com::sun::star::lang::IllegalArgumentException &e)
393cdf0e10cSrcweir     {
394cdf0e10cSrcweir         raisePyExceptionWithAny( com::sun::star::uno::makeAny( e ) );
395cdf0e10cSrcweir     }
396cdf0e10cSrcweir     catch (::com::sun::star::script::CannotConvertException &e)
397cdf0e10cSrcweir     {
398cdf0e10cSrcweir         raisePyExceptionWithAny( com::sun::star::uno::makeAny( e ) );
399cdf0e10cSrcweir     }
400cdf0e10cSrcweir     catch (::com::sun::star::uno::RuntimeException &e)
401cdf0e10cSrcweir     {
402cdf0e10cSrcweir         raisePyExceptionWithAny( com::sun::star::uno::makeAny( e ) );
403cdf0e10cSrcweir     }
404cdf0e10cSrcweir     catch (::com::sun::star::uno::Exception &e)
405cdf0e10cSrcweir     {
406cdf0e10cSrcweir         raisePyExceptionWithAny( com::sun::star::uno::makeAny( e ) );
407cdf0e10cSrcweir     }
408cdf0e10cSrcweir 
409cdf0e10cSrcweir     return ret.getAcquired();
410cdf0e10cSrcweir }
411cdf0e10cSrcweir 
412cdf0e10cSrcweir PyObject *PyUNO_str( PyObject * self )
413cdf0e10cSrcweir {
414cdf0e10cSrcweir     PyUNO *me = ( PyUNO * ) self;
415cdf0e10cSrcweir 
416cdf0e10cSrcweir     OStringBuffer buf;
417cdf0e10cSrcweir 
418cdf0e10cSrcweir 
419cdf0e10cSrcweir     if( me->members->wrappedObject.getValueType().getTypeClass()
420cdf0e10cSrcweir         == com::sun::star::uno::TypeClass_STRUCT ||
421cdf0e10cSrcweir         me->members->wrappedObject.getValueType().getTypeClass()
422cdf0e10cSrcweir         == com::sun::star::uno::TypeClass_EXCEPTION)
423cdf0e10cSrcweir     {
424cdf0e10cSrcweir         Reference< XMaterialHolder > rHolder(me->members->xInvocation,UNO_QUERY);
425cdf0e10cSrcweir         if( rHolder.is() )
426cdf0e10cSrcweir         {
427cdf0e10cSrcweir             PyThreadDetach antiguard;
428cdf0e10cSrcweir             Any a = rHolder->getMaterial();
429cdf0e10cSrcweir             OUString s = val2str( (void*) a.getValue(), a.getValueType().getTypeLibType() );
430cdf0e10cSrcweir             buf.append( OUStringToOString(s,RTL_TEXTENCODING_ASCII_US) );
431cdf0e10cSrcweir         }
432cdf0e10cSrcweir     }
433cdf0e10cSrcweir     else
434cdf0e10cSrcweir     {
435cdf0e10cSrcweir         // a common UNO object
436cdf0e10cSrcweir         PyThreadDetach antiguard;
437cdf0e10cSrcweir         buf.append( "pyuno object " );
438cdf0e10cSrcweir 
439cdf0e10cSrcweir         OUString s = val2str( (void*)me->members->wrappedObject.getValue(),
440cdf0e10cSrcweir                               me->members->wrappedObject.getValueType().getTypeLibType() );
441cdf0e10cSrcweir         buf.append( OUStringToOString(s,RTL_TEXTENCODING_ASCII_US) );
442cdf0e10cSrcweir     }
443cdf0e10cSrcweir 
444*77dc4149SPedro Giffuni     return PYSTR_FROMSTR( buf.getStr() );
445cdf0e10cSrcweir }
446cdf0e10cSrcweir 
447*77dc4149SPedro Giffuni #if PY_MAJOR_VERSION >= 3
448*77dc4149SPedro Giffuni PyObject* PyUNO_getattr (PyObject* self, PyObject* attr_name)
449*77dc4149SPedro Giffuni #else
450cdf0e10cSrcweir PyObject* PyUNO_getattr (PyObject* self, char* name)
451*77dc4149SPedro Giffuni #endif
452cdf0e10cSrcweir {
453cdf0e10cSrcweir     PyUNO* me;
454cdf0e10cSrcweir 
455*77dc4149SPedro Giffuni #if PY_VERSION_HEX >= 0x03030000
456*77dc4149SPedro Giffuni     char *name = PyUnicode_AsUTF8(attr_name);
457*77dc4149SPedro Giffuni #elif PY_MAJOR_VERSION >= 3
458*77dc4149SPedro Giffuni     PyRef pUtf8(PyUnicode_AsUTF8String(attr_name), SAL_NO_ACQUIRE);
459*77dc4149SPedro Giffuni     char *name = PyBytes_AsString(pUtf8.get());
460*77dc4149SPedro Giffuni #endif
461cdf0e10cSrcweir     try
462cdf0e10cSrcweir     {
463cdf0e10cSrcweir 
464cdf0e10cSrcweir         Runtime runtime;
465cdf0e10cSrcweir 
466cdf0e10cSrcweir         me = (PyUNO*) self;
467*77dc4149SPedro Giffuni #if PY_MAJOR_VERSION < 3
468cdf0e10cSrcweir         //Handle Python dir () stuff first...
469cdf0e10cSrcweir         if (strcmp (name, "__members__") == 0)
470cdf0e10cSrcweir         {
471cdf0e10cSrcweir             PyObject* member_list;
472cdf0e10cSrcweir             Sequence<OUString> oo_member_list;
473cdf0e10cSrcweir 
474cdf0e10cSrcweir             oo_member_list = me->members->xInvocation->getMemberNames ();
475cdf0e10cSrcweir             member_list = PyList_New (oo_member_list.getLength ());
476cdf0e10cSrcweir             for (int i = 0; i < oo_member_list.getLength (); i++)
477cdf0e10cSrcweir             {
478cdf0e10cSrcweir                 // setitem steals a reference
479cdf0e10cSrcweir                 PyList_SetItem (member_list, i, ustring2PyString(oo_member_list[i]).getAcquired() );
480cdf0e10cSrcweir             }
481cdf0e10cSrcweir             return member_list;
482cdf0e10cSrcweir         }
483*77dc4149SPedro Giffuni #endif
484cdf0e10cSrcweir 
485cdf0e10cSrcweir         if (strcmp (name, "__dict__") == 0)
486cdf0e10cSrcweir         {
487cdf0e10cSrcweir             Py_INCREF (Py_None);
488cdf0e10cSrcweir             return Py_None;
489cdf0e10cSrcweir         }
490*77dc4149SPedro Giffuni #if PY_MAJOR_VERSION < 3
491cdf0e10cSrcweir         if (strcmp (name, "__methods__") == 0)
492cdf0e10cSrcweir         {
493cdf0e10cSrcweir             Py_INCREF (Py_None);
494cdf0e10cSrcweir             return Py_None;
495cdf0e10cSrcweir         }
496*77dc4149SPedro Giffuni #endif
497cdf0e10cSrcweir         if (strcmp (name, "__class__") == 0)
498cdf0e10cSrcweir         {
499cdf0e10cSrcweir             if( me->members->wrappedObject.getValueTypeClass() ==
500cdf0e10cSrcweir                 com::sun::star::uno::TypeClass_STRUCT ||
501cdf0e10cSrcweir                 me->members->wrappedObject.getValueTypeClass() ==
502cdf0e10cSrcweir                 com::sun::star::uno::TypeClass_EXCEPTION )
503cdf0e10cSrcweir             {
504cdf0e10cSrcweir                 return getClass(
505cdf0e10cSrcweir                     me->members->wrappedObject.getValueType().getTypeName(), runtime ).getAcquired();
506cdf0e10cSrcweir             }
507cdf0e10cSrcweir             Py_INCREF (Py_None);
508cdf0e10cSrcweir             return Py_None;
509cdf0e10cSrcweir         }
510cdf0e10cSrcweir 
511cdf0e10cSrcweir         OUString attrName( OUString::createFromAscii( name ) );
512cdf0e10cSrcweir         //We need to find out if it's a method...
513cdf0e10cSrcweir         if (me->members->xInvocation->hasMethod (attrName))
514cdf0e10cSrcweir         {
515cdf0e10cSrcweir             //Create a callable object to invoke this...
516cdf0e10cSrcweir             PyRef ret = PyUNO_callable_new (
517cdf0e10cSrcweir                 me->members->xInvocation,
518cdf0e10cSrcweir                 attrName,
519cdf0e10cSrcweir                 runtime.getImpl()->cargo->xInvocation,
520cdf0e10cSrcweir                 runtime.getImpl()->cargo->xTypeConverter);
521cdf0e10cSrcweir             Py_XINCREF( ret.get() );
522cdf0e10cSrcweir             return ret.get();
523cdf0e10cSrcweir 
524cdf0e10cSrcweir         }
525cdf0e10cSrcweir 
526cdf0e10cSrcweir         //or a property
527cdf0e10cSrcweir         if (me->members->xInvocation->hasProperty ( attrName))
528cdf0e10cSrcweir         {
529cdf0e10cSrcweir             //Return the value of the property
530cdf0e10cSrcweir             Any anyRet;
531cdf0e10cSrcweir             {
532cdf0e10cSrcweir                 PyThreadDetach antiguard;
533cdf0e10cSrcweir                 anyRet = me->members->xInvocation->getValue (attrName);
534cdf0e10cSrcweir             }
535cdf0e10cSrcweir             PyRef ret = runtime.any2PyObject(anyRet);
536cdf0e10cSrcweir             Py_XINCREF( ret.get() );
537cdf0e10cSrcweir             return ret.get();
538cdf0e10cSrcweir         }
539cdf0e10cSrcweir 
540cdf0e10cSrcweir         //or else...
541cdf0e10cSrcweir         PyErr_SetString (PyExc_AttributeError, name);
542cdf0e10cSrcweir     }
543cdf0e10cSrcweir     catch( com::sun::star::reflection::InvocationTargetException & e )
544cdf0e10cSrcweir     {
545cdf0e10cSrcweir         raisePyExceptionWithAny( makeAny(e.TargetException) );
546cdf0e10cSrcweir     }
547cdf0e10cSrcweir     catch( com::sun::star::beans::UnknownPropertyException & e )
548cdf0e10cSrcweir     {
549cdf0e10cSrcweir         raisePyExceptionWithAny( makeAny(e) );
550cdf0e10cSrcweir     }
551cdf0e10cSrcweir     catch( com::sun::star::lang::IllegalArgumentException &e )
552cdf0e10cSrcweir     {
553cdf0e10cSrcweir         raisePyExceptionWithAny( makeAny(e) );
554cdf0e10cSrcweir     }
555cdf0e10cSrcweir     catch( com::sun::star::script::CannotConvertException &e )
556cdf0e10cSrcweir     {
557cdf0e10cSrcweir         raisePyExceptionWithAny( makeAny(e) );
558cdf0e10cSrcweir     }
559cdf0e10cSrcweir     catch( RuntimeException &e )
560cdf0e10cSrcweir     {
561cdf0e10cSrcweir         raisePyExceptionWithAny( makeAny(e) );
562cdf0e10cSrcweir     }
563cdf0e10cSrcweir 
564cdf0e10cSrcweir     return NULL;
565cdf0e10cSrcweir }
566cdf0e10cSrcweir 
567*77dc4149SPedro Giffuni #if PY_MAJOR_VERSION >= 3
568*77dc4149SPedro Giffuni int PyUNO_setattr (PyObject* self, PyObject* attr_name, PyObject* value)
569*77dc4149SPedro Giffuni #else
570cdf0e10cSrcweir int PyUNO_setattr (PyObject* self, char* name, PyObject* value)
571*77dc4149SPedro Giffuni #endif
572cdf0e10cSrcweir {
573cdf0e10cSrcweir     PyUNO* me;
574cdf0e10cSrcweir 
575*77dc4149SPedro Giffuni #if PY_VERSION_HEX >= 0x03030000
576*77dc4149SPedro Giffuni     char *name = PyUnicode_AsUTF8(attr_name);
577*77dc4149SPedro Giffuni #elif PY_MAJOR_VERSION >= 3
578*77dc4149SPedro Giffuni     PyRef pUtf8(PyUnicode_AsUTF8String(attr_name), SAL_NO_ACQUIRE);
579*77dc4149SPedro Giffuni     char *name = PyBytes_AsString(pUtf8.get());
580*77dc4149SPedro Giffuni #endif
581cdf0e10cSrcweir     me = (PyUNO*) self;
582cdf0e10cSrcweir     try
583cdf0e10cSrcweir     {
584cdf0e10cSrcweir         Runtime runtime;
585cdf0e10cSrcweir         Any val= runtime.pyObject2Any(value, ACCEPT_UNO_ANY);
586cdf0e10cSrcweir 
587cdf0e10cSrcweir         OUString attrName( OUString::createFromAscii( name ) );
588cdf0e10cSrcweir         {
589cdf0e10cSrcweir             PyThreadDetach antiguard;
590cdf0e10cSrcweir             if (me->members->xInvocation->hasProperty (attrName))
591cdf0e10cSrcweir             {
592cdf0e10cSrcweir                 me->members->xInvocation->setValue (attrName, val);
593cdf0e10cSrcweir                 return 0; //Keep with Python's boolean system
594cdf0e10cSrcweir             }
595cdf0e10cSrcweir         }
596cdf0e10cSrcweir     }
597cdf0e10cSrcweir     catch( com::sun::star::reflection::InvocationTargetException & e )
598cdf0e10cSrcweir     {
599cdf0e10cSrcweir         raisePyExceptionWithAny( makeAny(e.TargetException) );
600cdf0e10cSrcweir         return 1;
601cdf0e10cSrcweir     }
602cdf0e10cSrcweir     catch( com::sun::star::beans::UnknownPropertyException & e )
603cdf0e10cSrcweir     {
604cdf0e10cSrcweir         raisePyExceptionWithAny( makeAny(e) );
605cdf0e10cSrcweir         return 1;
606cdf0e10cSrcweir     }
607cdf0e10cSrcweir     catch( com::sun::star::script::CannotConvertException &e )
608cdf0e10cSrcweir     {
609cdf0e10cSrcweir         raisePyExceptionWithAny( makeAny(e) );
610cdf0e10cSrcweir         return 1;
611cdf0e10cSrcweir     }
612cdf0e10cSrcweir     catch( RuntimeException & e )
613cdf0e10cSrcweir     {
614cdf0e10cSrcweir         raisePyExceptionWithAny( makeAny( e ) );
615cdf0e10cSrcweir         return 1;
616cdf0e10cSrcweir     }
617cdf0e10cSrcweir     PyErr_SetString (PyExc_AttributeError, name);
618cdf0e10cSrcweir     return 1; //as above.
619cdf0e10cSrcweir }
620cdf0e10cSrcweir 
621*77dc4149SPedro Giffuni #if PY_MAJOR_VERSION >= 3
622*77dc4149SPedro Giffuni static PyObject *PyUNO_dir( PyObject *self, PyObject *that )
623*77dc4149SPedro Giffuni {
624*77dc4149SPedro Giffuni     PyUNO* me;
625*77dc4149SPedro Giffuni     PyObject* member_list;
626*77dc4149SPedro Giffuni     Sequence<OUString> oo_member_list;
627*77dc4149SPedro Giffuni 
628*77dc4149SPedro Giffuni     me = (PyUNO*) self;
629*77dc4149SPedro Giffuni     oo_member_list = me->members->xInvocation->getMemberNames ();
630*77dc4149SPedro Giffuni     member_list = PyList_New (oo_member_list.getLength ());
631*77dc4149SPedro Giffuni     for (int i = 0; i < oo_member_list.getLength (); i++)
632*77dc4149SPedro Giffuni     {
633*77dc4149SPedro Giffuni         // setitem steals a reference
634*77dc4149SPedro Giffuni         PyList_SetItem (member_list, i, ustring2PyUnicode(oo_member_list[i]).getAcquired() );
635*77dc4149SPedro Giffuni     }
636*77dc4149SPedro Giffuni     return member_list;
637*77dc4149SPedro Giffuni }
638*77dc4149SPedro Giffuni 
639*77dc4149SPedro Giffuni static PyObject *PyUNO_richcompare( PyObject *self, PyObject *that, int op )
640*77dc4149SPedro Giffuni {
641*77dc4149SPedro Giffuni     switch (op)
642*77dc4149SPedro Giffuni     {
643*77dc4149SPedro Giffuni     case Py_EQ:
644*77dc4149SPedro Giffuni     case Py_NE:
645*77dc4149SPedro Giffuni         if( self == that )
646*77dc4149SPedro Giffuni         {
647*77dc4149SPedro Giffuni             if (op == Py_EQ)
648*77dc4149SPedro Giffuni                 Py_RETURN_TRUE;
649*77dc4149SPedro Giffuni             else
650*77dc4149SPedro Giffuni                 Py_RETURN_FALSE;
651*77dc4149SPedro Giffuni         }
652*77dc4149SPedro Giffuni         try
653*77dc4149SPedro Giffuni         {
654*77dc4149SPedro Giffuni             Runtime runtime;
655*77dc4149SPedro Giffuni             if( PyObject_IsInstance( that, getPyUnoClass().get() ) )
656*77dc4149SPedro Giffuni             {
657*77dc4149SPedro Giffuni                 PyUNO *me = reinterpret_cast< PyUNO*> ( self );
658*77dc4149SPedro Giffuni                 PyUNO *other = reinterpret_cast< PyUNO *> (that );
659*77dc4149SPedro Giffuni                 com::sun::star::uno::TypeClass tcMe = me->members->wrappedObject.getValueTypeClass();
660*77dc4149SPedro Giffuni                 com::sun::star::uno::TypeClass tcOther = other->members->wrappedObject.getValueTypeClass();
661*77dc4149SPedro Giffuni 
662*77dc4149SPedro Giffuni                 if( tcMe == tcOther )
663*77dc4149SPedro Giffuni                 {
664*77dc4149SPedro Giffuni                     if( tcMe == com::sun::star::uno::TypeClass_STRUCT ||
665*77dc4149SPedro Giffuni                         tcMe == com::sun::star::uno::TypeClass_EXCEPTION )
666*77dc4149SPedro Giffuni                     {
667*77dc4149SPedro Giffuni                         Reference< XMaterialHolder > xMe( me->members->xInvocation,UNO_QUERY);
668*77dc4149SPedro Giffuni                         Reference< XMaterialHolder > xOther( other->members->xInvocation,UNO_QUERY );
669*77dc4149SPedro Giffuni                         if( xMe->getMaterial() == xOther->getMaterial() )
670*77dc4149SPedro Giffuni                         {
671*77dc4149SPedro Giffuni                             if (op == Py_EQ)
672*77dc4149SPedro Giffuni                                 Py_RETURN_TRUE;
673*77dc4149SPedro Giffuni                             else
674*77dc4149SPedro Giffuni                                 Py_RETURN_FALSE;
675*77dc4149SPedro Giffuni                         }
676*77dc4149SPedro Giffuni                     }
677*77dc4149SPedro Giffuni                     else if( tcMe == com::sun::star::uno::TypeClass_INTERFACE )
678*77dc4149SPedro Giffuni                     {
679*77dc4149SPedro Giffuni                         if( me->members->wrappedObject == other->members->wrappedObject )
680*77dc4149SPedro Giffuni                         {
681*77dc4149SPedro Giffuni                             if (op == Py_EQ)
682*77dc4149SPedro Giffuni                                 Py_RETURN_TRUE;
683*77dc4149SPedro Giffuni                             else
684*77dc4149SPedro Giffuni                                 Py_RETURN_FALSE;
685*77dc4149SPedro Giffuni                         }
686*77dc4149SPedro Giffuni                     }
687*77dc4149SPedro Giffuni                 }
688*77dc4149SPedro Giffuni             }
689*77dc4149SPedro Giffuni             if (op == Py_EQ)
690*77dc4149SPedro Giffuni                 Py_RETURN_FALSE;
691*77dc4149SPedro Giffuni             else
692*77dc4149SPedro Giffuni                 Py_RETURN_TRUE;
693*77dc4149SPedro Giffuni         }
694*77dc4149SPedro Giffuni         catch( com::sun::star::uno::RuntimeException & e)
695*77dc4149SPedro Giffuni         {
696*77dc4149SPedro Giffuni             raisePyExceptionWithAny( makeAny( e ) );
697*77dc4149SPedro Giffuni         }
698*77dc4149SPedro Giffuni         break;
699*77dc4149SPedro Giffuni     default:
700*77dc4149SPedro Giffuni         PyErr_SetString(Py_NotImplemented, "not implemented");
701*77dc4149SPedro Giffuni         break;
702*77dc4149SPedro Giffuni     }
703*77dc4149SPedro Giffuni 
704*77dc4149SPedro Giffuni     return NULL;
705*77dc4149SPedro Giffuni }
706*77dc4149SPedro Giffuni 
707*77dc4149SPedro Giffuni 
708*77dc4149SPedro Giffuni static struct PyMethodDef PyUNO_methods[] = {
709*77dc4149SPedro Giffuni     { "__dir__", (PyCFunction)PyUNO_dir, METH_VARARGS, NULL},
710*77dc4149SPedro Giffuni     { NULL, NULL }
711*77dc4149SPedro Giffuni };
712*77dc4149SPedro Giffuni 
713*77dc4149SPedro Giffuni #else
714cdf0e10cSrcweir // ensure object identity and struct equality
715cdf0e10cSrcweir static int PyUNO_cmp( PyObject *self, PyObject *that )
716cdf0e10cSrcweir {
717cdf0e10cSrcweir     if( self == that )
718cdf0e10cSrcweir         return 0;
719cdf0e10cSrcweir     int retDefault = self > that ? 1 : -1;
720cdf0e10cSrcweir     try
721cdf0e10cSrcweir     {
722cdf0e10cSrcweir         Runtime runtime;
723*77dc4149SPedro Giffuni         if( PyObject_IsInstance( that, getPyUnoClass().get() ) )
724cdf0e10cSrcweir         {
725cdf0e10cSrcweir 
726cdf0e10cSrcweir             PyUNO *me = reinterpret_cast< PyUNO*> ( self );
727cdf0e10cSrcweir             PyUNO *other = reinterpret_cast< PyUNO *> (that );
728cdf0e10cSrcweir             com::sun::star::uno::TypeClass tcMe = me->members->wrappedObject.getValueTypeClass();
729cdf0e10cSrcweir             com::sun::star::uno::TypeClass tcOther = other->members->wrappedObject.getValueTypeClass();
730cdf0e10cSrcweir 
731cdf0e10cSrcweir             if( tcMe == tcOther )
732cdf0e10cSrcweir             {
733cdf0e10cSrcweir                 if( tcMe == com::sun::star::uno::TypeClass_STRUCT ||
734cdf0e10cSrcweir                     tcMe == com::sun::star::uno::TypeClass_EXCEPTION )
735cdf0e10cSrcweir                 {
736cdf0e10cSrcweir                     Reference< XMaterialHolder > xMe( me->members->xInvocation,UNO_QUERY);
737cdf0e10cSrcweir                     Reference< XMaterialHolder > xOther( other->members->xInvocation,UNO_QUERY );
738cdf0e10cSrcweir                     if( xMe->getMaterial() == xOther->getMaterial() )
739cdf0e10cSrcweir                         return 0;
740cdf0e10cSrcweir                 }
741cdf0e10cSrcweir                 else if( tcMe == com::sun::star::uno::TypeClass_INTERFACE )
742cdf0e10cSrcweir                 {
743cdf0e10cSrcweir                     if( me->members->wrappedObject == other->members->wrappedObject )
744cdf0e10cSrcweir //                     if( me->members->xInvocation == other->members->xInvocation )
745cdf0e10cSrcweir                         return 0;
746cdf0e10cSrcweir                 }
747cdf0e10cSrcweir             }
748cdf0e10cSrcweir         }
749cdf0e10cSrcweir     }
750cdf0e10cSrcweir     catch( com::sun::star::uno::RuntimeException & e)
751cdf0e10cSrcweir     {
752cdf0e10cSrcweir         raisePyExceptionWithAny( makeAny( e ) );
753cdf0e10cSrcweir     }
754cdf0e10cSrcweir     return retDefault;
755cdf0e10cSrcweir }
756*77dc4149SPedro Giffuni #endif
757cdf0e10cSrcweir 
758cdf0e10cSrcweir static PyTypeObject PyUNOType =
759cdf0e10cSrcweir {
7605c3821d8SPedro Giffuni     PyVarObject_HEAD_INIT(&PyType_Type, 0)
761cdf0e10cSrcweir     const_cast< char * >("pyuno"),
762cdf0e10cSrcweir     sizeof (PyUNO),
763cdf0e10cSrcweir     0,
764cdf0e10cSrcweir     (destructor) PyUNO_del,
765cdf0e10cSrcweir     (printfunc) 0,
766*77dc4149SPedro Giffuni #if PY_MAJOR_VERSION >= 3
767*77dc4149SPedro Giffuni     (getattrfunc) 0,
768*77dc4149SPedro Giffuni     (setattrfunc) 0,
769*77dc4149SPedro Giffuni     0,
770*77dc4149SPedro Giffuni #else
771*77dc4149SPedro Giffuni     (getattrfunc) PyUNO_getattr, /* tp_getattr */
772*77dc4149SPedro Giffuni     (setattrfunc) PyUNO_setattr, /* tp_setattr */
773*77dc4149SPedro Giffuni     (cmpfunc) PyUNO_cmp,
774*77dc4149SPedro Giffuni #endif
775cdf0e10cSrcweir     (reprfunc) PyUNO_repr,
776cdf0e10cSrcweir     0,
777cdf0e10cSrcweir     0,
778cdf0e10cSrcweir     0,
779cdf0e10cSrcweir     (hashfunc) 0,
780cdf0e10cSrcweir     (ternaryfunc) 0,
781cdf0e10cSrcweir     (reprfunc) PyUNO_str,
782*77dc4149SPedro Giffuni #if PY_MAJOR_VERSION >= 3
783*77dc4149SPedro Giffuni     (getattrofunc)PyUNO_getattr, /* tp_getattro */
784*77dc4149SPedro Giffuni     (setattrofunc)PyUNO_setattr, /* tp_setattro */
785*77dc4149SPedro Giffuni #else
786cdf0e10cSrcweir     (getattrofunc)0,
787cdf0e10cSrcweir     (setattrofunc)0,
788*77dc4149SPedro Giffuni #endif
789cdf0e10cSrcweir     NULL,
790cdf0e10cSrcweir     0,
791cdf0e10cSrcweir     NULL,
792cdf0e10cSrcweir     (traverseproc)0,
793cdf0e10cSrcweir     (inquiry)0,
794*77dc4149SPedro Giffuni #if PY_MAJOR_VERSION >= 3
795*77dc4149SPedro Giffuni     PyUNO_richcompare, /* tp_richcompare */
796*77dc4149SPedro Giffuni #else
797cdf0e10cSrcweir     (richcmpfunc)0,
798*77dc4149SPedro Giffuni #endif
799cdf0e10cSrcweir     0,
800cdf0e10cSrcweir     (getiterfunc)0,
801cdf0e10cSrcweir     (iternextfunc)0,
802*77dc4149SPedro Giffuni #if PY_MAJOR_VERSION >= 3
803*77dc4149SPedro Giffuni     PyUNO_methods, /* tp_methods */
804*77dc4149SPedro Giffuni #else
805cdf0e10cSrcweir     NULL,
806*77dc4149SPedro Giffuni #endif
807cdf0e10cSrcweir     NULL,
808cdf0e10cSrcweir     NULL,
809cdf0e10cSrcweir     NULL,
810cdf0e10cSrcweir     NULL,
811cdf0e10cSrcweir     (descrgetfunc)0,
812cdf0e10cSrcweir     (descrsetfunc)0,
813cdf0e10cSrcweir     0,
814cdf0e10cSrcweir     (initproc)0,
815cdf0e10cSrcweir     (allocfunc)0,
816cdf0e10cSrcweir     (newfunc)0,
817cdf0e10cSrcweir     (freefunc)0,
818cdf0e10cSrcweir     (inquiry)0,
819cdf0e10cSrcweir     NULL,
820cdf0e10cSrcweir     NULL,
821cdf0e10cSrcweir     NULL,
822cdf0e10cSrcweir     NULL,
823cdf0e10cSrcweir     NULL,
824cdf0e10cSrcweir     (destructor)0
825cdf0e10cSrcweir #if PY_VERSION_HEX >= 0x02060000
826cdf0e10cSrcweir     , 0
827cdf0e10cSrcweir #endif
828cdf0e10cSrcweir };
829cdf0e10cSrcweir 
830*77dc4149SPedro Giffuni PyRef getPyUnoClass()
831cdf0e10cSrcweir {
832cdf0e10cSrcweir     return PyRef( reinterpret_cast< PyObject * > ( &PyUNOType ) );
833cdf0e10cSrcweir }
834cdf0e10cSrcweir 
835cdf0e10cSrcweir PyObject* PyUNO_new (
836cdf0e10cSrcweir     const Any & targetInterface, const Reference<XSingleServiceFactory> &ssf)
837cdf0e10cSrcweir {
838cdf0e10cSrcweir     Reference<XInterface> tmp_interface;
839cdf0e10cSrcweir 
840cdf0e10cSrcweir     targetInterface >>= tmp_interface;
841cdf0e10cSrcweir     if (!tmp_interface.is ())
842cdf0e10cSrcweir     {
843cdf0e10cSrcweir         // empty reference !
844cdf0e10cSrcweir         Py_INCREF( Py_None );
845cdf0e10cSrcweir         return Py_None;
846cdf0e10cSrcweir     }
847cdf0e10cSrcweir 
848cdf0e10cSrcweir     return PyUNO_new_UNCHECKED (targetInterface, ssf);
849cdf0e10cSrcweir }
850cdf0e10cSrcweir 
851cdf0e10cSrcweir 
852cdf0e10cSrcweir PyObject* PyUNO_new_UNCHECKED (
853cdf0e10cSrcweir     const Any &targetInterface,
854cdf0e10cSrcweir     const Reference<XSingleServiceFactory> &ssf )
855cdf0e10cSrcweir {
856cdf0e10cSrcweir     PyUNO* self;
857cdf0e10cSrcweir     Sequence<Any> arguments (1);
858cdf0e10cSrcweir     Reference<XInterface> tmp_interface;
859cdf0e10cSrcweir 
860cdf0e10cSrcweir     self = PyObject_New (PyUNO, &PyUNOType);
861cdf0e10cSrcweir     if (self == NULL)
862cdf0e10cSrcweir         return NULL; //NULL == error
863cdf0e10cSrcweir     self->members = new PyUNOInternals();
864cdf0e10cSrcweir 
865cdf0e10cSrcweir     arguments[0] <<= targetInterface;
866cdf0e10cSrcweir     {
867cdf0e10cSrcweir         PyThreadDetach antiguard;
868cdf0e10cSrcweir         tmp_interface = ssf->createInstanceWithArguments (arguments);
869cdf0e10cSrcweir         Reference<XInvocation2> tmp_invocation (tmp_interface, UNO_QUERY);
870cdf0e10cSrcweir         self->members->xInvocation = tmp_invocation;
871cdf0e10cSrcweir         self->members->wrappedObject = targetInterface;
872cdf0e10cSrcweir     }
873cdf0e10cSrcweir     return (PyObject*) self;
874cdf0e10cSrcweir }
875cdf0e10cSrcweir 
876cdf0e10cSrcweir }
877