1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24 #include "pyuno_impl.hxx"
25
26 #include <rtl/strbuf.hxx>
27 #include <rtl/ustrbuf.hxx>
28
29 #include <osl/thread.h>
30
31 #include <algorithm>
32 #include <vector>
33
34 #include <com/sun/star/lang/XServiceInfo.hpp>
35 #include <com/sun/star/lang/XTypeProvider.hpp>
36 #include <com/sun/star/beans/XPropertySet.hpp>
37 #include <com/sun/star/beans/XMaterialHolder.hpp>
38
39 #define TO_ASCII(x) OUStringToOString( x , RTL_TEXTENCODING_ASCII_US).getStr()
40
41 using rtl::OStringBuffer;
42 using rtl::OUStringBuffer;
43 using rtl::OUStringToOString;
44 using rtl::OUString;
45 using com::sun::star::uno::Sequence;
46 using com::sun::star::uno::Reference;
47 using com::sun::star::uno::XInterface;
48 using com::sun::star::uno::Any;
49 using com::sun::star::uno::makeAny;
50 using com::sun::star::uno::UNO_QUERY;
51 using com::sun::star::uno::Type;
52 using com::sun::star::uno::TypeClass;
53 using com::sun::star::uno::RuntimeException;
54 using com::sun::star::uno::Exception;
55 using com::sun::star::uno::XComponentContext;
56 using com::sun::star::lang::XSingleServiceFactory;
57 using com::sun::star::lang::XServiceInfo;
58 using com::sun::star::lang::XTypeProvider;
59 using com::sun::star::script::XTypeConverter;
60 using com::sun::star::script::XInvocation;
61 using com::sun::star::script::XInvocation2;
62 using com::sun::star::reflection::XIdlClass;
63 using com::sun::star::reflection::XIdlMethod;
64 using com::sun::star::beans::XMaterialHolder;
65
66 namespace pyuno
67 {
68
69 PyObject *PyUNO_str( PyObject * self );
70
PyUNO_del(PyObject * self)71 void PyUNO_del (PyObject* self)
72 {
73 PyUNO* me = reinterpret_cast< PyUNO* > (self);
74 {
75 PyThreadDetach antiguard;
76 delete me->members;
77 }
78 PyObject_Del (self);
79 }
80
81
82
val2str(const void * pVal,typelib_TypeDescriptionReference * pTypeRef,sal_Int32 mode)83 OUString val2str( const void * pVal, typelib_TypeDescriptionReference * pTypeRef , sal_Int32 mode ) SAL_THROW( () )
84 {
85 OSL_ASSERT( pVal );
86 if (pTypeRef->eTypeClass == typelib_TypeClass_VOID)
87 return OUString( RTL_CONSTASCII_USTRINGPARAM("void") );
88
89 OUStringBuffer buf( 64 );
90 buf.append( (sal_Unicode)'(' );
91 buf.append( pTypeRef->pTypeName );
92 buf.append( (sal_Unicode)')' );
93
94 switch (pTypeRef->eTypeClass)
95 {
96 case typelib_TypeClass_INTERFACE:
97 {
98 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("0x") );
99 buf.append( reinterpret_cast< sal_IntPtr >(*(void **)pVal), 16 );
100 if( VAL2STR_MODE_DEEP == mode )
101 {
102 buf.appendAscii( "{" ); Reference< XInterface > r = *( Reference< XInterface > * ) pVal;
103 Reference< XServiceInfo > serviceInfo( r, UNO_QUERY);
104 Reference< XTypeProvider > typeProvider(r,UNO_QUERY);
105 if( serviceInfo.is() )
106 {
107 buf.appendAscii("implementationName=" );
108 buf.append(serviceInfo->getImplementationName() );
109 buf.appendAscii(", supportedServices={" );
110 Sequence< OUString > seq = serviceInfo->getSupportedServiceNames();
111 for( int i = 0 ; i < seq.getLength() ; i ++ )
112 {
113 buf.append( seq[i] );
114 if( i +1 != seq.getLength() )
115 buf.appendAscii( "," );
116 }
117 buf.appendAscii("}");
118 }
119
120 if( typeProvider.is() )
121 {
122 buf.appendAscii(", supportedInterfaces={" );
123 Sequence< Type > seq (typeProvider->getTypes());
124 for( int i = 0 ; i < seq.getLength() ; i ++ )
125 {
126 buf.append(seq[i].getTypeName());
127 if( i +1 != seq.getLength() )
128 buf.appendAscii( "," );
129 }
130 buf.appendAscii("}");
131 }
132 buf.appendAscii( "}" );
133 }
134
135 break;
136 }
137 case typelib_TypeClass_UNION:
138 {
139 // typelib_TypeDescription * pTypeDescr = 0;
140 // TYPELIB_DANGER_GET( &pTypeDescr, pTypeRef );
141 // buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("{ ") );
142 // buf.append( val2str( (char *)pVal + ((typelib_UnionTypeDescription *)pTypeDescr)->nValueOffset,
143 // union_getSetType( pVal, pTypeDescr ) ) );
144 // buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(" }") );
145 // TYPELIB_DANGER_RELEASE( pTypeDescr );
146 break;
147 }
148 case typelib_TypeClass_STRUCT:
149 case typelib_TypeClass_EXCEPTION:
150 {
151 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("{ ") );
152 typelib_TypeDescription * pTypeDescr = 0;
153 TYPELIB_DANGER_GET( &pTypeDescr, pTypeRef );
154 OSL_ASSERT( pTypeDescr );
155
156 typelib_CompoundTypeDescription * pCompType = (typelib_CompoundTypeDescription *)pTypeDescr;
157 sal_Int32 nDescr = pCompType->nMembers;
158
159 if (pCompType->pBaseTypeDescription)
160 {
161 buf.append( val2str( pVal, ((typelib_TypeDescription *)pCompType->pBaseTypeDescription)->pWeakRef,mode ) );
162 if (nDescr)
163 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(", ") );
164 }
165
166 typelib_TypeDescriptionReference ** ppTypeRefs = pCompType->ppTypeRefs;
167 sal_Int32 * pMemberOffsets = pCompType->pMemberOffsets;
168 rtl_uString ** ppMemberNames = pCompType->ppMemberNames;
169
170 for ( sal_Int32 nPos = 0; nPos < nDescr; ++nPos )
171 {
172 buf.append( ppMemberNames[nPos] );
173 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(" = ") );
174 typelib_TypeDescription * pMemberType = 0;
175 TYPELIB_DANGER_GET( &pMemberType, ppTypeRefs[nPos] );
176 buf.append( val2str( (char *)pVal + pMemberOffsets[nPos], pMemberType->pWeakRef, mode ) );
177 TYPELIB_DANGER_RELEASE( pMemberType );
178 if (nPos < (nDescr -1))
179 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(", ") );
180 }
181
182 TYPELIB_DANGER_RELEASE( pTypeDescr );
183
184 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(" }") );
185 break;
186 }
187 case typelib_TypeClass_SEQUENCE:
188 {
189 typelib_TypeDescription * pTypeDescr = 0;
190 TYPELIB_DANGER_GET( &pTypeDescr, pTypeRef );
191
192 uno_Sequence * pSequence = *(uno_Sequence **)pVal;
193 typelib_TypeDescription * pElementTypeDescr = 0;
194 TYPELIB_DANGER_GET( &pElementTypeDescr, ((typelib_IndirectTypeDescription *)pTypeDescr)->pType );
195
196 sal_Int32 nElementSize = pElementTypeDescr->nSize;
197 sal_Int32 nElements = pSequence->nElements;
198
199 if (nElements)
200 {
201 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("{ ") );
202 char * pElements = pSequence->elements;
203 for ( sal_Int32 nPos = 0; nPos < nElements; ++nPos )
204 {
205 buf.append( val2str( pElements + (nElementSize * nPos), pElementTypeDescr->pWeakRef, mode ) );
206 if (nPos < (nElements -1))
207 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(", ") );
208 }
209 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(" }") );
210 }
211 else
212 {
213 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("{}") );
214 }
215 TYPELIB_DANGER_RELEASE( pElementTypeDescr );
216 TYPELIB_DANGER_RELEASE( pTypeDescr );
217 break;
218 }
219 case typelib_TypeClass_ANY:
220 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("{ ") );
221 buf.append( val2str( ((uno_Any *)pVal)->pData,
222 ((uno_Any *)pVal)->pType ,
223 mode) );
224 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(" }") );
225 break;
226 case typelib_TypeClass_TYPE:
227 buf.append( (*(typelib_TypeDescriptionReference **)pVal)->pTypeName );
228 break;
229 case typelib_TypeClass_STRING:
230 buf.append( (sal_Unicode)'\"' );
231 buf.append( *(rtl_uString **)pVal );
232 buf.append( (sal_Unicode)'\"' );
233 break;
234 case typelib_TypeClass_ENUM:
235 {
236 typelib_TypeDescription * pTypeDescr = 0;
237 TYPELIB_DANGER_GET( &pTypeDescr, pTypeRef );
238
239 sal_Int32 * pValues = ((typelib_EnumTypeDescription *)pTypeDescr)->pEnumValues;
240 sal_Int32 nPos = ((typelib_EnumTypeDescription *)pTypeDescr)->nEnumValues;
241 while (nPos--)
242 {
243 if (pValues[nPos] == *(int *)pVal)
244 break;
245 }
246 if (nPos >= 0)
247 buf.append( ((typelib_EnumTypeDescription *)pTypeDescr)->ppEnumNames[nPos] );
248 else
249 buf.append( (sal_Unicode)'?' );
250
251 TYPELIB_DANGER_RELEASE( pTypeDescr );
252 break;
253 }
254 case typelib_TypeClass_BOOLEAN:
255 if (*(sal_Bool *)pVal)
256 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("true") );
257 else
258 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("false") );
259 break;
260 case typelib_TypeClass_CHAR:
261 buf.append( (sal_Unicode)'\'' );
262 buf.append( *(sal_Unicode *)pVal );
263 buf.append( (sal_Unicode)'\'' );
264 break;
265 case typelib_TypeClass_FLOAT:
266 buf.append( *(float *)pVal );
267 break;
268 case typelib_TypeClass_DOUBLE:
269 buf.append( *(double *)pVal );
270 break;
271 case typelib_TypeClass_BYTE:
272 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("0x") );
273 buf.append( (sal_Int32)*(sal_Int8 *)pVal, 16 );
274 break;
275 case typelib_TypeClass_SHORT:
276 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("0x") );
277 buf.append( (sal_Int32)*(sal_Int16 *)pVal, 16 );
278 break;
279 case typelib_TypeClass_UNSIGNED_SHORT:
280 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("0x") );
281 buf.append( (sal_Int32)*(sal_uInt16 *)pVal, 16 );
282 break;
283 case typelib_TypeClass_LONG:
284 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("0x") );
285 buf.append( *(sal_Int32 *)pVal, 16 );
286 break;
287 case typelib_TypeClass_UNSIGNED_LONG:
288 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("0x") );
289 buf.append( (sal_Int64)*(sal_uInt32 *)pVal, 16 );
290 break;
291 case typelib_TypeClass_HYPER:
292 case typelib_TypeClass_UNSIGNED_HYPER:
293 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("0x") );
294 #if defined(GCC) && defined(SPARC)
295 {
296 sal_Int64 aVal;
297 *(sal_Int32 *)&aVal = *(sal_Int32 *)pVal;
298 *((sal_Int32 *)&aVal +1)= *((sal_Int32 *)pVal +1);
299 buf.append( aVal, 16 );
300 }
301 #else
302 buf.append( *(sal_Int64 *)pVal, 16 );
303 #endif
304 break;
305
306 case typelib_TypeClass_VOID:
307 case typelib_TypeClass_ARRAY:
308 case typelib_TypeClass_UNKNOWN:
309 case typelib_TypeClass_SERVICE:
310 case typelib_TypeClass_MODULE:
311 default:
312 buf.append( (sal_Unicode)'?' );
313 }
314
315 return buf.makeStringAndClear();
316 }
317
318
PyUNO_repr(PyObject * self)319 PyObject *PyUNO_repr( PyObject * self )
320 {
321 PyUNO *me = (PyUNO * ) self;
322 PyObject * ret = 0;
323
324 if( me->members->wrappedObject.getValueType().getTypeClass()
325 == com::sun::star::uno::TypeClass_EXCEPTION )
326 {
327 Reference< XMaterialHolder > rHolder(me->members->xInvocation,UNO_QUERY);
328 if( rHolder.is() )
329 {
330 Any a = rHolder->getMaterial();
331 Exception e;
332 a >>= e;
333 ret = ustring2PyUnicode(e.Message ).getAcquired();
334 }
335 }
336 else
337 {
338 ret = PyUNO_str( self );
339 }
340 return ret;
341 }
342
343 // An object with its own XInvocation (no XInvocation2 from Invocation_Impl) reports only the
344 // members it chooses to, so its interface methods are found through reflection instead.
getReflectedInterfaces(PyUNO * me,const Runtime & runtime)345 static Sequence< Reference< XIdlClass > > getReflectedInterfaces( PyUNO *me, const Runtime &runtime )
346 {
347 Sequence< Reference< XIdlClass > > ret;
348 Reference< XInvocation2 > inv2( me->members->xInvocation, UNO_QUERY );
349 if( inv2.is() )
350 return ret;
351 Reference< XTypeProvider > typeProvider( me->members->wrappedObject, UNO_QUERY );
352 if( ! typeProvider.is() )
353 return ret;
354
355 Sequence< Type > types( typeProvider->getTypes() );
356 ret.realloc( types.getLength() );
357 sal_Int32 n = 0;
358 for( sal_Int32 i = 0 ; i < types.getLength() ; i ++ )
359 {
360 Reference< XIdlClass > c(
361 runtime.getImpl()->cargo->xCoreReflection->forName( types[i].getTypeName() ) );
362 if( c.is() && c->getTypeClass() == com::sun::star::uno::TypeClass_INTERFACE )
363 ret[n++] = c;
364 }
365 ret.realloc( n );
366 return ret;
367 }
368
369 // acquire(), release() and queryInterface() must never be callable from Python
isXInterfaceMember(const Reference<XIdlMethod> & m)370 static bool isXInterfaceMember( const Reference< XIdlMethod > &m )
371 {
372 Reference< XIdlClass > decl( m->getDeclaringClass() );
373 return decl.is() && decl->getName().equalsAscii( "com.sun.star.uno.XInterface" );
374 }
375
findReflectedMethod(PyUNO * me,const OUString & name,const Runtime & runtime)376 static Reference< XIdlMethod > findReflectedMethod(
377 PyUNO *me, const OUString &name, const Runtime &runtime )
378 {
379 Sequence< Reference< XIdlClass > > classes( getReflectedInterfaces( me, runtime ) );
380 for( sal_Int32 i = 0 ; i < classes.getLength() ; i ++ )
381 {
382 Reference< XIdlMethod > m( classes[i]->getMethod( name ) );
383 if( m.is() && ! isXInterfaceMember( m ) )
384 return m;
385 }
386 return Reference< XIdlMethod >();
387 }
388
PyUNO_invoke(PyObject * object,const char * name,PyObject * args)389 PyObject *PyUNO_invoke( PyObject *object, const char *name , PyObject *args )
390 {
391 PyRef ret;
392 try
393 {
394 Runtime runtime;
395
396 PyRef paras,callable;
397 if( PyObject_IsInstance( object, getPyUnoClass().get() ) )
398 {
399 PyUNO* me = (PyUNO*) object;
400 OUString attrName = OUString::createFromAscii(name);
401 Reference< XIdlMethod > xMethod;
402 bool hasMethod = false;
403 {
404 PyThreadDetach antiguard;
405 hasMethod = me->members->xInvocation->hasMethod (attrName);
406 }
407 if (! hasMethod)
408 {
409 xMethod = findReflectedMethod( me, attrName, runtime );
410 if (! xMethod.is())
411 {
412 OUStringBuffer buf;
413 buf.appendAscii( "Attribute " );
414 buf.append( attrName );
415 buf.appendAscii( " unknown" );
416 throw RuntimeException( buf.makeStringAndClear(), Reference< XInterface > () );
417 }
418 }
419 callable = PyUNO_callable_new (
420 me->members->xInvocation,
421 attrName,
422 ACCEPT_UNO_ANY,
423 xMethod,
424 me->members->wrappedObject);
425 paras = args;
426 }
427 else
428 {
429 // clean the tuple from uno.Any !
430 int size = PyTuple_Size( args );
431 { // for CC, keeping ref-count of tuple being 1
432 paras = PyRef(PyTuple_New( size ), SAL_NO_ACQUIRE);
433 }
434 for( int i = 0 ; i < size ;i ++ )
435 {
436 PyObject * element = PyTuple_GetItem( args , i );
437 if( PyObject_IsInstance( element , getAnyClass( runtime ).get() ) )
438 {
439 element = PyObject_GetAttrString(
440 element, const_cast< char * >("value") );
441 }
442 else
443 {
444 Py_XINCREF( element );
445 }
446 PyTuple_SetItem( paras.get(), i , element );
447 }
448 callable = PyRef( PyObject_GetAttrString( object , (char*)name ), SAL_NO_ACQUIRE );
449 if( !callable.is() )
450 return 0;
451 }
452 ret = PyRef( PyObject_CallObject( callable.get(), paras.get() ), SAL_NO_ACQUIRE );
453 }
454 catch (::com::sun::star::lang::IllegalArgumentException &e)
455 {
456 raisePyExceptionWithAny( com::sun::star::uno::makeAny( e ) );
457 }
458 catch (::com::sun::star::script::CannotConvertException &e)
459 {
460 raisePyExceptionWithAny( com::sun::star::uno::makeAny( e ) );
461 }
462 catch (::com::sun::star::uno::RuntimeException &e)
463 {
464 raisePyExceptionWithAny( com::sun::star::uno::makeAny( e ) );
465 }
466 catch (::com::sun::star::uno::Exception &e)
467 {
468 raisePyExceptionWithAny( com::sun::star::uno::makeAny( e ) );
469 }
470
471 return ret.getAcquired();
472 }
473
PyUNO_str(PyObject * self)474 PyObject *PyUNO_str( PyObject * self )
475 {
476 PyUNO *me = ( PyUNO * ) self;
477
478 OStringBuffer buf;
479
480
481 if( me->members->wrappedObject.getValueType().getTypeClass()
482 == com::sun::star::uno::TypeClass_STRUCT ||
483 me->members->wrappedObject.getValueType().getTypeClass()
484 == com::sun::star::uno::TypeClass_EXCEPTION)
485 {
486 Reference< XMaterialHolder > rHolder(me->members->xInvocation,UNO_QUERY);
487 if( rHolder.is() )
488 {
489 PyThreadDetach antiguard;
490 Any a = rHolder->getMaterial();
491 OUString s = val2str( (void*) a.getValue(), a.getValueType().getTypeLibType() );
492 buf.append( OUStringToOString(s,RTL_TEXTENCODING_ASCII_US) );
493 }
494 }
495 else
496 {
497 // a common UNO object
498 PyThreadDetach antiguard;
499 buf.append( "pyuno object " );
500
501 OUString s = val2str( (void*)me->members->wrappedObject.getValue(),
502 me->members->wrappedObject.getValueType().getTypeLibType() );
503 buf.append( OUStringToOString(s,RTL_TEXTENCODING_ASCII_US) );
504 }
505
506 return PYSTR_FROMSTR( buf.getStr() );
507 }
508
PyUNO_getattr(PyObject * self,PyObject * attr_name)509 PyObject* PyUNO_getattr (PyObject* self, PyObject* attr_name)
510 {
511 PyUNO* me;
512
513 const char *name = PyUnicode_AsUTF8(attr_name);
514 try
515 {
516
517 Runtime runtime;
518
519 me = (PyUNO*) self;
520
521 if (strcmp (name, "__dict__") == 0)
522 {
523 Py_INCREF (Py_None);
524 return Py_None;
525 }
526 if (strcmp (name, "__class__") == 0)
527 {
528 if( me->members->wrappedObject.getValueTypeClass() ==
529 com::sun::star::uno::TypeClass_STRUCT ||
530 me->members->wrappedObject.getValueTypeClass() ==
531 com::sun::star::uno::TypeClass_EXCEPTION )
532 {
533 return getClass(
534 me->members->wrappedObject.getValueType().getTypeName(), runtime ).getAcquired();
535 }
536 Py_INCREF (Py_None);
537 return Py_None;
538 }
539
540 OUString attrName( OUString::createFromAscii( name ) );
541 //We need to find out if it's a method...
542 bool isMethod = false;
543 bool isProperty = false;
544 {
545 PyThreadDetach antiguard;
546 isMethod = me->members->xInvocation->hasMethod (attrName);
547 if (! isMethod)
548 isProperty = me->members->xInvocation->hasProperty (attrName);
549 }
550 if (isMethod)
551 {
552 //Create a callable object to invoke this...
553 PyRef ret = PyUNO_callable_new (
554 me->members->xInvocation,
555 attrName);
556 Py_XINCREF( ret.get() );
557 return ret.get();
558
559 }
560
561 //or a property
562 if (isProperty)
563 {
564 //Return the value of the property
565 Any anyRet;
566 {
567 PyThreadDetach antiguard;
568 anyRet = me->members->xInvocation->getValue (attrName);
569 }
570 PyRef ret = runtime.any2PyObject(anyRet);
571 Py_XINCREF( ret.get() );
572 return ret.get();
573 }
574
575 //or a method of an interface that the object's own XInvocation does not report
576 Reference< XIdlMethod > xMethod( findReflectedMethod( me, attrName, runtime ) );
577 if( xMethod.is() )
578 {
579 PyRef ret = PyUNO_callable_new (
580 me->members->xInvocation,
581 attrName,
582 REJECT_UNO_ANY,
583 xMethod,
584 me->members->wrappedObject);
585 Py_XINCREF( ret.get() );
586 return ret.get();
587 }
588
589 //or else...
590 PyErr_SetString (PyExc_AttributeError, name);
591 }
592 catch( com::sun::star::reflection::InvocationTargetException & e )
593 {
594 raisePyExceptionWithAny( makeAny(e.TargetException) );
595 }
596 catch( com::sun::star::beans::UnknownPropertyException & e )
597 {
598 raisePyExceptionWithAny( makeAny(e) );
599 }
600 catch( com::sun::star::lang::IllegalArgumentException &e )
601 {
602 raisePyExceptionWithAny( makeAny(e) );
603 }
604 catch( com::sun::star::script::CannotConvertException &e )
605 {
606 raisePyExceptionWithAny( makeAny(e) );
607 }
608 catch( RuntimeException &e )
609 {
610 raisePyExceptionWithAny( makeAny(e) );
611 }
612
613 return NULL;
614 }
615
PyUNO_setattr(PyObject * self,PyObject * attr_name,PyObject * value)616 int PyUNO_setattr (PyObject* self, PyObject* attr_name, PyObject* value)
617 {
618 PyUNO* me;
619
620 const char *name = PyUnicode_AsUTF8(attr_name);
621 me = (PyUNO*) self;
622 try
623 {
624 Runtime runtime;
625 Any val= runtime.pyObject2Any(value, ACCEPT_UNO_ANY);
626
627 OUString attrName( OUString::createFromAscii( name ) );
628 {
629 PyThreadDetach antiguard;
630 if (me->members->xInvocation->hasProperty (attrName))
631 {
632 me->members->xInvocation->setValue (attrName, val);
633 return 0; //Keep with Python's boolean system
634 }
635 }
636 }
637 catch( com::sun::star::reflection::InvocationTargetException & e )
638 {
639 raisePyExceptionWithAny( makeAny(e.TargetException) );
640 return 1;
641 }
642 catch( com::sun::star::beans::UnknownPropertyException & e )
643 {
644 raisePyExceptionWithAny( makeAny(e) );
645 return 1;
646 }
647 catch( com::sun::star::script::CannotConvertException &e )
648 {
649 raisePyExceptionWithAny( makeAny(e) );
650 return 1;
651 }
652 catch( RuntimeException & e )
653 {
654 raisePyExceptionWithAny( makeAny( e ) );
655 return 1;
656 }
657 PyErr_SetString (PyExc_AttributeError, name);
658 return 1; //as above.
659 }
660
PyUNO_dir(PyObject * self,PyObject * that)661 static PyObject *PyUNO_dir( PyObject *self, PyObject *that )
662 {
663 PyUNO* me;
664 PyObject* member_list;
665 Sequence<OUString> oo_member_list;
666
667 me = (PyUNO*) self;
668 try
669 {
670 Reference< XInvocation2 > inv2( me->members->xInvocation, UNO_QUERY );
671 if( inv2.is() )
672 {
673 PyThreadDetach antiguard;
674 oo_member_list = inv2->getMemberNames ();
675 }
676 else
677 {
678 Runtime runtime;
679 std::vector< OUString > names;
680 Sequence< Reference< XIdlClass > > classes( getReflectedInterfaces( me, runtime ) );
681 for( sal_Int32 i = 0 ; i < classes.getLength() ; i ++ )
682 {
683 Sequence< Reference< XIdlMethod > > methods( classes[i]->getMethods() );
684 for( sal_Int32 j = 0 ; j < methods.getLength() ; j ++ )
685 {
686 OUString n( methods[j]->getName() );
687 if( ! isXInterfaceMember( methods[j] ) &&
688 std::find( names.begin(), names.end(), n ) == names.end() )
689 names.push_back( n );
690 }
691 }
692 oo_member_list.realloc( names.size() );
693 for( size_t k = 0 ; k < names.size() ; k ++ )
694 oo_member_list[k] = names[k];
695 }
696 }
697 catch( com::sun::star::uno::RuntimeException &e )
698 {
699 raisePyExceptionWithAny( makeAny( e ) );
700 return NULL;
701 }
702 catch( com::sun::star::uno::Exception &e )
703 {
704 raisePyExceptionWithAny( makeAny( e ) );
705 return NULL;
706 }
707 member_list = PyList_New (oo_member_list.getLength ());
708 for (int i = 0; i < oo_member_list.getLength (); i++)
709 {
710 // setitem steals a reference
711 PyList_SetItem (member_list, i, ustring2PyUnicode(oo_member_list[i]).getAcquired() );
712 }
713 return member_list;
714 }
715
PyUNO_richcompare(PyObject * self,PyObject * that,int op)716 static PyObject *PyUNO_richcompare( PyObject *self, PyObject *that, int op )
717 {
718 switch (op)
719 {
720 case Py_EQ:
721 case Py_NE:
722 if( self == that )
723 {
724 if (op == Py_EQ)
725 Py_RETURN_TRUE;
726 else
727 Py_RETURN_FALSE;
728 }
729 try
730 {
731 Runtime runtime;
732 if( PyObject_IsInstance( that, getPyUnoClass().get() ) )
733 {
734 PyUNO *me = reinterpret_cast< PyUNO*> ( self );
735 PyUNO *other = reinterpret_cast< PyUNO *> (that );
736 com::sun::star::uno::TypeClass tcMe = me->members->wrappedObject.getValueTypeClass();
737 com::sun::star::uno::TypeClass tcOther = other->members->wrappedObject.getValueTypeClass();
738
739 if( tcMe == tcOther )
740 {
741 if( tcMe == com::sun::star::uno::TypeClass_STRUCT ||
742 tcMe == com::sun::star::uno::TypeClass_EXCEPTION )
743 {
744 Reference< XMaterialHolder > xMe( me->members->xInvocation,UNO_QUERY);
745 Reference< XMaterialHolder > xOther( other->members->xInvocation,UNO_QUERY );
746 if( xMe->getMaterial() == xOther->getMaterial() )
747 {
748 if (op == Py_EQ)
749 Py_RETURN_TRUE;
750 else
751 Py_RETURN_FALSE;
752 }
753 }
754 else if( tcMe == com::sun::star::uno::TypeClass_INTERFACE )
755 {
756 if( me->members->wrappedObject == other->members->wrappedObject )
757 {
758 if (op == Py_EQ)
759 Py_RETURN_TRUE;
760 else
761 Py_RETURN_FALSE;
762 }
763 }
764 }
765 }
766 if (op == Py_EQ)
767 Py_RETURN_FALSE;
768 else
769 Py_RETURN_TRUE;
770 }
771 catch( com::sun::star::uno::RuntimeException & e)
772 {
773 raisePyExceptionWithAny( makeAny( e ) );
774 }
775 break;
776 default:
777 PyErr_SetString(Py_NotImplemented, "not implemented");
778 break;
779 }
780
781 return NULL;
782 }
783
784
785 static struct PyMethodDef PyUNO_methods[] = {
786 { "__dir__", (PyCFunction)PyUNO_dir, METH_VARARGS, NULL},
787 { NULL, NULL }
788 };
789
790 static PyTypeObject PyUNOType =
791 {
792 PyVarObject_HEAD_INIT(&PyType_Type, 0)
793 const_cast< char * >("pyuno"),
794 sizeof (PyUNO),
795 0,
796 (destructor) PyUNO_del,
797 (printfunc) 0,
798 (getattrfunc) 0,
799 (setattrfunc) 0,
800 0,
801 (reprfunc) PyUNO_repr,
802 0,
803 0,
804 0,
805 (hashfunc) 0,
806 (ternaryfunc) 0,
807 (reprfunc) PyUNO_str,
808 (getattrofunc)PyUNO_getattr, /* tp_getattro */
809 (setattrofunc)PyUNO_setattr, /* tp_setattro */
810 NULL,
811 0,
812 NULL,
813 (traverseproc)0,
814 (inquiry)0,
815 PyUNO_richcompare, /* tp_richcompare */
816 0,
817 (getiterfunc)0,
818 (iternextfunc)0,
819 PyUNO_methods, /* tp_methods */
820 NULL,
821 NULL,
822 NULL,
823 NULL,
824 (descrgetfunc)0,
825 (descrsetfunc)0,
826 0,
827 (initproc)0,
828 (allocfunc)0,
829 (newfunc)0,
830 (freefunc)0,
831 (inquiry)0,
832 NULL,
833 NULL,
834 NULL,
835 NULL,
836 NULL,
837 (destructor)0,
838 0
839 };
840
getPyUnoClass()841 PyRef getPyUnoClass()
842 {
843 return PyRef( reinterpret_cast< PyObject * > ( &PyUNOType ) );
844 }
845
PyUNO_new(const Any & targetInterface,const Reference<XSingleServiceFactory> & ssf)846 PyObject* PyUNO_new (
847 const Any & targetInterface, const Reference<XSingleServiceFactory> &ssf)
848 {
849 Reference<XInterface> tmp_interface;
850
851 targetInterface >>= tmp_interface;
852 if (!tmp_interface.is ())
853 {
854 // empty reference !
855 Py_INCREF( Py_None );
856 return Py_None;
857 }
858
859 return PyUNO_new_UNCHECKED (targetInterface, ssf);
860 }
861
862
PyUNO_new_UNCHECKED(const Any & targetInterface,const Reference<XSingleServiceFactory> & ssf)863 PyObject* PyUNO_new_UNCHECKED (
864 const Any &targetInterface,
865 const Reference<XSingleServiceFactory> &ssf )
866 {
867 PyUNO* self;
868 Sequence<Any> arguments (1);
869 Reference<XInterface> tmp_interface;
870
871 self = PyObject_New (PyUNO, &PyUNOType);
872 if (self == NULL)
873 return NULL; //NULL == error
874 self->members = new PyUNOInternals();
875
876 arguments[0] <<= targetInterface;
877 {
878 PyThreadDetach antiguard;
879 tmp_interface = ssf->createInstanceWithArguments (arguments);
880 Reference<XInvocation> tmp_invocation (tmp_interface, UNO_QUERY);
881 self->members->xInvocation = tmp_invocation;
882 self->members->wrappedObject = targetInterface;
883 }
884 if( ! self->members->xInvocation.is() )
885 {
886 Py_DECREF( (PyObject*) self );
887 throw RuntimeException(
888 OUString( RTL_CONSTASCII_USTRINGPARAM( "pyuno: no XInvocation for the wrapped object" ) ),
889 Reference< XInterface >() );
890 }
891 return (PyObject*) self;
892 }
893
894 }
895