1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <sal/config.h>
#include <cassert>
#include "pyuno_impl.hxx"
#include <rtl/strbuf.hxx>
#include <rtl/ustrbuf.hxx>
#include <osl/thread.h>
#include <com/sun/star/lang/XServiceInfo.hpp>
#include <com/sun/star/lang/XTypeProvider.hpp>
#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/beans/XMaterialHolder.hpp>
using com::sun::star::uno::Sequence;
using com::sun::star::uno::Reference;
using com::sun::star::uno::XInterface;
using com::sun::star::uno::Any;
using com::sun::star::uno::makeAny;
using com::sun::star::uno::UNO_QUERY;
using com::sun::star::uno::Type;
using com::sun::star::uno::TypeClass;
using com::sun::star::uno::RuntimeException;
using com::sun::star::uno::Exception;
using com::sun::star::uno::XComponentContext;
using com::sun::star::lang::XSingleServiceFactory;
using com::sun::star::lang::XServiceInfo;
using com::sun::star::lang::XTypeProvider;
using com::sun::star::script::XTypeConverter;
using com::sun::star::script::XInvocation2;
using com::sun::star::beans::XMaterialHolder;
namespace pyuno
{
PyObject *PyUNO_str( PyObject * self );
void PyUNO_del (PyObject* self)
{
PyUNO* me = reinterpret_cast< PyUNO* > (self);
{
PyThreadDetach antiguard;
delete me->members;
}
PyObject_Del (self);
}
OUString val2str( const void * pVal, typelib_TypeDescriptionReference * pTypeRef , sal_Int32 mode )
{
assert( pVal );
if (pTypeRef->eTypeClass == typelib_TypeClass_VOID)
return OUString("void");
OUStringBuffer buf( 64 );
buf.append( '(' );
buf.append( pTypeRef->pTypeName );
buf.append( ')' );
switch (pTypeRef->eTypeClass)
{
case typelib_TypeClass_INTERFACE:
{
buf.append( "0x" );
buf.append( reinterpret_cast< sal_IntPtr >(*static_cast<void * const *>(pVal)), 16 );
if( VAL2STR_MODE_DEEP == mode )
{
buf.append( "{" ); Reference< XInterface > r = *static_cast<Reference< XInterface > const *>(pVal);
Reference< XServiceInfo > serviceInfo( r, UNO_QUERY);
Reference< XTypeProvider > typeProvider(r,UNO_QUERY);
if( serviceInfo.is() )
{
buf.append("implementationName=" );
buf.append(serviceInfo->getImplementationName() );
buf.append(", supportedServices={" );
Sequence< OUString > seq = serviceInfo->getSupportedServiceNames();
for( int i = 0 ; i < seq.getLength() ; i ++ )
{
buf.append( seq[i] );
if( i +1 != seq.getLength() )
buf.append( "," );
}
buf.append("}");
}
if( typeProvider.is() )
{
buf.append(", supportedInterfaces={" );
Sequence< Type > seq (typeProvider->getTypes());
for( int i = 0 ; i < seq.getLength() ; i ++ )
{
buf.append(seq[i].getTypeName());
if( i +1 != seq.getLength() )
buf.append( "," );
}
buf.append("}");
}
buf.append( "}" );
}
break;
}
case typelib_TypeClass_STRUCT:
case typelib_TypeClass_EXCEPTION:
{
buf.append( "{ " );
typelib_TypeDescription * pTypeDescr = 0;
TYPELIB_DANGER_GET( &pTypeDescr, pTypeRef );
assert( pTypeDescr );
typelib_CompoundTypeDescription * pCompType = reinterpret_cast<typelib_CompoundTypeDescription *>(pTypeDescr);
sal_Int32 nDescr = pCompType->nMembers;
if (pCompType->pBaseTypeDescription)
{
buf.append( val2str( pVal, pCompType->pBaseTypeDescription->aBase.pWeakRef, mode ) );
if (nDescr)
buf.append( ", " );
}
typelib_TypeDescriptionReference ** ppTypeRefs = pCompType->ppTypeRefs;
sal_Int32 * pMemberOffsets = pCompType->pMemberOffsets;
rtl_uString ** ppMemberNames = pCompType->ppMemberNames;
for ( sal_Int32 nPos = 0; nPos < nDescr; ++nPos )
{
buf.append( ppMemberNames[nPos] );
buf.append( " = " );
typelib_TypeDescription * pMemberType = 0;
TYPELIB_DANGER_GET( &pMemberType, ppTypeRefs[nPos] );
buf.append( val2str( static_cast<char const *>(pVal) + pMemberOffsets[nPos], pMemberType->pWeakRef, mode ) );
TYPELIB_DANGER_RELEASE( pMemberType );
if (nPos < (nDescr -1))
buf.append( ", " );
}
TYPELIB_DANGER_RELEASE( pTypeDescr );
buf.append( " }" );
break;
}
case typelib_TypeClass_SEQUENCE:
{
typelib_TypeDescription * pTypeDescr = 0;
TYPELIB_DANGER_GET( &pTypeDescr, pTypeRef );
uno_Sequence * pSequence = *static_cast<uno_Sequence * const *>(pVal);
typelib_TypeDescription * pElementTypeDescr = 0;
TYPELIB_DANGER_GET( &pElementTypeDescr, reinterpret_cast<typelib_IndirectTypeDescription *>(pTypeDescr)->pType );
sal_Int32 nElementSize = pElementTypeDescr->nSize;
sal_Int32 nElements = pSequence->nElements;
if (nElements)
{
buf.append( "{ " );
char * pElements = pSequence->elements;
for ( sal_Int32 nPos = 0; nPos < nElements; ++nPos )
{
buf.append( val2str( pElements + (nElementSize * nPos), pElementTypeDescr->pWeakRef, mode ) );
if (nPos < (nElements -1))
buf.append( ", " );
}
buf.append( " }" );
}
else
{
buf.append( "{}" );
}
TYPELIB_DANGER_RELEASE( pElementTypeDescr );
TYPELIB_DANGER_RELEASE( pTypeDescr );
break;
}
case typelib_TypeClass_ANY:
buf.append( "{ " );
buf.append( val2str( static_cast<uno_Any const *>(pVal)->pData,
static_cast<uno_Any const *>(pVal)->pType ,
mode) );
buf.append( " }" );
break;
case typelib_TypeClass_TYPE:
buf.append( (*static_cast<typelib_TypeDescriptionReference * const *>(pVal))->pTypeName );
break;
case typelib_TypeClass_STRING:
buf.append( '\"' );
buf.append( *static_cast<rtl_uString * const *>(pVal) );
buf.append( '\"' );
break;
case typelib_TypeClass_ENUM:
{
typelib_TypeDescription * pTypeDescr = 0;
TYPELIB_DANGER_GET( &pTypeDescr, pTypeRef );
sal_Int32 * pValues = reinterpret_cast<typelib_EnumTypeDescription *>(pTypeDescr)->pEnumValues;
sal_Int32 nPos = reinterpret_cast<typelib_EnumTypeDescription *>(pTypeDescr)->nEnumValues;
while (nPos--)
{
if (pValues[nPos] == *static_cast<int const *>(pVal))
break;
}
if (nPos >= 0)
buf.append( reinterpret_cast<typelib_EnumTypeDescription *>(pTypeDescr)->ppEnumNames[nPos] );
else
buf.append( '?' );
TYPELIB_DANGER_RELEASE( pTypeDescr );
break;
}
case typelib_TypeClass_BOOLEAN:
if (*static_cast<sal_Bool const *>(pVal))
buf.append( "true" );
else
buf.append( "false" );
break;
case typelib_TypeClass_CHAR:
buf.append( '\'' );
buf.append( *static_cast<sal_Unicode const *>(pVal) );
buf.append( '\'' );
break;
case typelib_TypeClass_FLOAT:
buf.append( *static_cast<float const *>(pVal) );
break;
case typelib_TypeClass_DOUBLE:
buf.append( *static_cast<double const *>(pVal) );
break;
case typelib_TypeClass_BYTE:
buf.append( "0x" );
buf.append( (sal_Int32)*static_cast<sal_Int8 const *>(pVal), 16 );
break;
case typelib_TypeClass_SHORT:
buf.append( "0x" );
buf.append( (sal_Int32)*static_cast<sal_Int16 const *>(pVal), 16 );
break;
case typelib_TypeClass_UNSIGNED_SHORT:
buf.append( "0x" );
buf.append( (sal_Int32)*static_cast<sal_uInt16 const *>(pVal), 16 );
break;
case typelib_TypeClass_LONG:
buf.append( "0x" );
buf.append( *static_cast<sal_Int32 const *>(pVal), 16 );
break;
case typelib_TypeClass_UNSIGNED_LONG:
buf.append( "0x" );
buf.append( (sal_Int64)*static_cast<sal_uInt32 const *>(pVal), 16 );
break;
case typelib_TypeClass_HYPER:
case typelib_TypeClass_UNSIGNED_HYPER:
buf.append( "0x" );
#if defined(__GNUC__) && defined(SPARC)
// I guess this really should check if there are strict alignment
// requirements, not just "GCC on SPARC".
{
sal_Int64 aVal;
*(sal_Int32 *)&aVal = *(sal_Int32 *)pVal;
*((sal_Int32 *)&aVal +1)= *((sal_Int32 *)pVal +1);
buf.append( aVal, 16 );
}
#else
buf.append( *static_cast<sal_Int64 const *>(pVal), 16 );
#endif
break;
case typelib_TypeClass_VOID:
case typelib_TypeClass_UNKNOWN:
case typelib_TypeClass_SERVICE:
case typelib_TypeClass_MODULE:
default:
buf.append( '?' );
}
return buf.makeStringAndClear();
}
PyObject *PyUNO_repr( PyObject * self )
{
PyUNO *me = reinterpret_cast<PyUNO *>(self);
PyObject * ret = 0;
if( me->members->wrappedObject.getValueType().getTypeClass()
== com::sun::star::uno::TypeClass_EXCEPTION )
{
Reference< XMaterialHolder > rHolder(me->members->xInvocation,UNO_QUERY);
if( rHolder.is() )
{
Any a = rHolder->getMaterial();
Exception e;
a >>= e;
ret = ustring2PyUnicode(e.Message ).getAcquired();
}
}
else
{
ret = PyUNO_str( self );
}
return ret;
}
PyObject *PyUNO_invoke( PyObject *object, const char *name , PyObject *args )
{
PyRef ret;
try
{
Runtime runtime;
PyRef paras,callable;
if( PyObject_IsInstance( object, getPyUnoClass().get() ) )
{
PyUNO* me = reinterpret_cast<PyUNO*>(object);
OUString attrName = OUString::createFromAscii(name);
if (! me->members->xInvocation->hasMethod (attrName))
{
OUStringBuffer buf;
buf.append( "Attribute " );
buf.append( attrName );
buf.append( " unknown" );
throw RuntimeException( buf.makeStringAndClear() );
}
callable = PyUNO_callable_new (
me->members->xInvocation,
attrName,
ACCEPT_UNO_ANY);
paras = args;
}
else
{
// clean the tuple from uno.Any !
int size = PyTuple_Size( args );
{ // for CC, keeping ref-count of tuple being 1
paras = PyRef(PyTuple_New( size ), SAL_NO_ACQUIRE);
}
for( int i = 0 ; i < size ;i ++ )
{
PyObject * element = PyTuple_GetItem( args , i );
if( PyObject_IsInstance( element , getAnyClass( runtime ).get() ) )
{
element = PyObject_GetAttrString(
element, "value" );
}
else
{
Py_XINCREF( element );
}
PyTuple_SetItem( paras.get(), i , element );
}
callable = PyRef( PyObject_GetAttrString( object , name ), SAL_NO_ACQUIRE );
if( !callable.is() )
return 0;
}
ret = PyRef( PyObject_CallObject( callable.get(), paras.get() ), SAL_NO_ACQUIRE );
}
catch (const ::com::sun::star::lang::IllegalArgumentException &e)
{
raisePyExceptionWithAny( com::sun::star::uno::makeAny( e ) );
}
catch (const ::com::sun::star::script::CannotConvertException &e)
{
raisePyExceptionWithAny( com::sun::star::uno::makeAny( e ) );
}
catch (const ::com::sun::star::uno::RuntimeException &e)
{
raisePyExceptionWithAny( com::sun::star::uno::makeAny( e ) );
}
catch (const ::com::sun::star::uno::Exception &e)
{
raisePyExceptionWithAny( com::sun::star::uno::makeAny( e ) );
}
return ret.getAcquired();
}
PyObject *PyUNO_str( PyObject * self )
{
PyUNO *me = reinterpret_cast<PyUNO *>(self);
OStringBuffer buf;
if( me->members->wrappedObject.getValueType().getTypeClass()
== com::sun::star::uno::TypeClass_STRUCT ||
me->members->wrappedObject.getValueType().getTypeClass()
== com::sun::star::uno::TypeClass_EXCEPTION)
{
Reference< XMaterialHolder > rHolder(me->members->xInvocation,UNO_QUERY);
if( rHolder.is() )
{
PyThreadDetach antiguard;
Any a = rHolder->getMaterial();
OUString s = val2str( a.getValue(), a.getValueType().getTypeLibType() );
buf.append( OUStringToOString(s,RTL_TEXTENCODING_ASCII_US) );
}
}
else
{
// a common UNO object
PyThreadDetach antiguard;
buf.append( "pyuno object " );
OUString s = val2str( me->members->wrappedObject.getValue(),
me->members->wrappedObject.getValueType().getTypeLibType() );
buf.append( OUStringToOString(s,RTL_TEXTENCODING_ASCII_US) );
}
return PyStr_FromString( buf.getStr());
}
PyObject* PyUNO_dir (PyObject* self)
{
PyUNO* me = reinterpret_cast<PyUNO*>(self);
PyObject* member_list = NULL;
Sequence<OUString> oo_member_list;
try
{
oo_member_list = me->members->xInvocation->getMemberNames ();
member_list = PyList_New (oo_member_list.getLength ());
for (int i = 0; i < oo_member_list.getLength (); i++)
{
// setitem steals a reference
PyList_SetItem (member_list, i, ustring2PyString(oo_member_list[i]).getAcquired() );
}
}
catch( const RuntimeException &e )
{
raisePyExceptionWithAny( makeAny(e) );
}
return member_list;
}
PyObject* PyUNO_getattr (PyObject* self, char* name)
{
PyUNO* me;
try
{
Runtime runtime;
me = reinterpret_cast<PyUNO*>(self);
if (strcmp (name, "__dict__") == 0)
{
Py_INCREF (Py_TYPE(me)->tp_dict);
return Py_TYPE(me)->tp_dict;
}
if (strcmp (name, "__class__") == 0)
{
if( me->members->wrappedObject.getValueTypeClass() ==
com::sun::star::uno::TypeClass_STRUCT ||
me->members->wrappedObject.getValueTypeClass() ==
com::sun::star::uno::TypeClass_EXCEPTION )
{
return getClass(
me->members->wrappedObject.getValueType().getTypeName(), runtime ).getAcquired();
}
Py_INCREF (Py_None);
return Py_None;
}
OUString attrName( OUString::createFromAscii( name ) );
//We need to find out if it's a method...
if (me->members->xInvocation->hasMethod (attrName))
{
//Create a callable object to invoke this...
PyRef ret = PyUNO_callable_new (
me->members->xInvocation,
attrName);
Py_XINCREF( ret.get() );
return ret.get();
}
//or a property
if (me->members->xInvocation->hasProperty ( attrName))
{
//Return the value of the property
Any anyRet;
{
PyThreadDetach antiguard;
anyRet = me->members->xInvocation->getValue (attrName);
}
PyRef ret = runtime.any2PyObject(anyRet);
Py_XINCREF( ret.get() );
return ret.get();
}
//or else...
PyErr_SetString (PyExc_AttributeError, name);
}
catch( const com::sun::star::reflection::InvocationTargetException & e )
{
raisePyExceptionWithAny( e.TargetException );
}
catch( const com::sun::star::beans::UnknownPropertyException & e )
{
raisePyExceptionWithAny( makeAny(e) );
}
catch( const com::sun::star::lang::IllegalArgumentException &e )
{
raisePyExceptionWithAny( makeAny(e) );
}
catch( const com::sun::star::script::CannotConvertException &e )
{
raisePyExceptionWithAny( makeAny(e) );
}
catch( const RuntimeException &e )
{
raisePyExceptionWithAny( makeAny(e) );
}
return NULL;
}
int PyUNO_setattr (PyObject* self, char* name, PyObject* value)
{
PyUNO* me;
me = reinterpret_cast<PyUNO*>(self);
try
{
Runtime runtime;
Any val= runtime.pyObject2Any(value, ACCEPT_UNO_ANY);
OUString attrName( OUString::createFromAscii( name ) );
{
PyThreadDetach antiguard;
if (me->members->xInvocation->hasProperty (attrName))
{
me->members->xInvocation->setValue (attrName, val);
return 0; //Keep with Python's boolean system
}
}
}
catch( const com::sun::star::reflection::InvocationTargetException & e )
{
raisePyExceptionWithAny( e.TargetException );
return 1;
}
catch( const com::sun::star::beans::UnknownPropertyException & e )
{
raisePyExceptionWithAny( makeAny(e) );
return 1;
}
catch( const com::sun::star::script::CannotConvertException &e )
{
raisePyExceptionWithAny( makeAny(e) );
return 1;
}
catch( const RuntimeException & e )
{
raisePyExceptionWithAny( makeAny( e ) );
return 1;
}
PyErr_SetString (PyExc_AttributeError, name);
return 1; //as above.
}
// ensure object identity and struct equality
static PyObject* PyUNO_cmp( PyObject *self, PyObject *that, int op )
{
PyObject *result;
if(op != Py_EQ && op != Py_NE)
{
PyErr_SetString(PyExc_TypeError, "only '==' and '!=' comparisons are defined");
return 0;
}
if( self == that )
{
result = (op == Py_EQ ? Py_True : Py_False);
Py_INCREF(result);
return result;
}
try
{
Runtime runtime;
if( PyObject_IsInstance( that, getPyUnoClass().get() ) )
{
PyUNO *me = reinterpret_cast< PyUNO*> ( self );
PyUNO *other = reinterpret_cast< PyUNO *> (that );
com::sun::star::uno::TypeClass tcMe = me->members->wrappedObject.getValueTypeClass();
com::sun::star::uno::TypeClass tcOther = other->members->wrappedObject.getValueTypeClass();
if( tcMe == tcOther )
{
if( tcMe == com::sun::star::uno::TypeClass_STRUCT ||
tcMe == com::sun::star::uno::TypeClass_EXCEPTION )
{
Reference< XMaterialHolder > xMe( me->members->xInvocation,UNO_QUERY);
Reference< XMaterialHolder > xOther( other->members->xInvocation,UNO_QUERY );
if( xMe->getMaterial() == xOther->getMaterial() )
{
result = (op == Py_EQ ? Py_True : Py_False);
Py_INCREF(result);
return result;
}
}
else if( tcMe == com::sun::star::uno::TypeClass_INTERFACE )
{
if( me->members->wrappedObject == other->members->wrappedObject )
{
result = (op == Py_EQ ? Py_True : Py_False);
Py_INCREF(result);
return result;
}
}
}
}
}
catch( const com::sun::star::uno::RuntimeException & e)
{
raisePyExceptionWithAny( makeAny( e ) );
}
result = (op == Py_EQ ? Py_False : Py_True);
Py_INCREF(result);
return result;
}
static PyMethodDef PyUNOMethods[] =
{
{"__dir__", reinterpret_cast<PyCFunction>(PyUNO_dir), METH_NOARGS, NULL},
{NULL, NULL, 0, NULL}
};
/* Python 2 has a tp_flags value for rich comparisons. Python 3 does not (on by default) */
#ifdef Py_TPFLAGS_HAVE_RICHCOMPARE
#define TP_FLAGS (Py_TPFLAGS_HAVE_RICHCOMPARE)
#else
#define TP_FLAGS 0
#endif
static PyTypeObject PyUNOType =
{
PyVarObject_HEAD_INIT( &PyType_Type, 0 )
"pyuno",
sizeof (PyUNO),
0,
PyUNO_del,
nullptr,
PyUNO_getattr,
PyUNO_setattr,
/* this type does not exist in Python 3: (cmpfunc) */ 0,
PyUNO_repr,
0,
0,
0,
nullptr,
nullptr,
PyUNO_str,
nullptr,
nullptr,
NULL,
TP_FLAGS,
NULL,
nullptr,
nullptr,
PyUNO_cmp,
0,
nullptr,
nullptr,
PyUNOMethods,
NULL,
NULL,
NULL,
NULL,
nullptr,
nullptr,
0,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
NULL,
NULL,
NULL,
NULL,
NULL,
nullptr
#if PY_VERSION_HEX >= 0x02060000
, 0
#endif
#if PY_VERSION_HEX >= 0x03040000
, 0
#endif
};
int PyUNO_initType()
{
return PyType_Ready(&PyUNOType);
}
PyRef getPyUnoClass()
{
return PyRef( reinterpret_cast< PyObject * > ( &PyUNOType ) );
}
PyObject* PyUNO_new (
const Any & targetInterface, const Reference<XSingleServiceFactory> &ssf)
{
Reference<XInterface> tmp_interface;
targetInterface >>= tmp_interface;
if (!tmp_interface.is ())
{
// empty reference !
Py_INCREF( Py_None );
return Py_None;
}
return PyUNO_new_UNCHECKED (targetInterface, ssf);
}
PyObject* PyUNO_new_UNCHECKED (
const Any &targetInterface,
const Reference<XSingleServiceFactory> &ssf )
{
Reference<XInterface> tmp_interface;
Reference<XInvocation2> tmp_invocation;
{
PyThreadDetach antiguard;
Sequence<Any> arguments(1);
arguments[0] <<= targetInterface;
tmp_interface = ssf->createInstanceWithArguments(arguments);
tmp_invocation.set(tmp_interface, UNO_QUERY);
if (!tmp_invocation.is() && tmp_interface.is()) {
throw RuntimeException("XInvocation2 not implemented, cannot interact with object");
}
}
if (!tmp_interface.is())
{
Py_INCREF( Py_None );
return Py_None;
}
PyUNO* self = PyObject_New (PyUNO, &PyUNOType);
if (self == NULL)
return NULL; // == error
self->members = new PyUNOInternals();
self->members->xInvocation = tmp_invocation;
self->members->wrappedObject = targetInterface;
return reinterpret_cast<PyObject*>(self);
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|