pyuno_type.cxx (67c7d1c1) pyuno_type.cxx (564d9007)
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

--- 152 unchanged lines hidden (view full) ---

161 return c;
162}
163
164Any PyEnum2Enum( PyObject *obj ) throw ( RuntimeException )
165{
166 Any ret;
167 PyRef typeName( PyObject_GetAttrString( obj,const_cast< char * >("typeName") ), SAL_NO_ACQUIRE);
168 PyRef value( PyObject_GetAttrString( obj, const_cast< char * >("value") ), SAL_NO_ACQUIRE);
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

--- 152 unchanged lines hidden (view full) ---

161 return c;
162}
163
164Any PyEnum2Enum( PyObject *obj ) throw ( RuntimeException )
165{
166 Any ret;
167 PyRef typeName( PyObject_GetAttrString( obj,const_cast< char * >("typeName") ), SAL_NO_ACQUIRE);
168 PyRef value( PyObject_GetAttrString( obj, const_cast< char * >("value") ), SAL_NO_ACQUIRE);
169 if( !PyString_Check( typeName.get() ) || ! PyString_Check( value.get() ) )
169 if( !PyBytes_Check( typeName.get() ) || ! PyBytes_Check( value.get() ) )
170 {
171 throw RuntimeException(
172 USTR_ASCII( "attributes typeName and/or value of uno.Enum are not strings" ),
173 Reference< XInterface > () );
174 }
175
170 {
171 throw RuntimeException(
172 USTR_ASCII( "attributes typeName and/or value of uno.Enum are not strings" ),
173 Reference< XInterface > () );
174 }
175
176 OUString strTypeName( OUString::createFromAscii( PyString_AsString( typeName.get() ) ) );
177 char *stringValue = PyString_AsString( value.get() );
176 OUString strTypeName( OUString::createFromAscii( PyBytes_AsString( typeName.get() ) ) );
177 char *stringValue = PyBytes_AsString( value.get() );
178
179 TypeDescription desc( strTypeName );
180 if( desc.is() )
181 {
182 if(desc.get()->eTypeClass != typelib_TypeClass_ENUM )
183 {
184 OUStringBuffer buf;
185 buf.appendAscii( "pyuno.checkEnum: " ).append(strTypeName).appendAscii( "is a " );

--- 13 unchanged lines hidden (view full) ---

199 {
200 break;
201 }
202 }
203 if( i == pEnumDesc->nEnumValues )
204 {
205 OUStringBuffer buf;
206 buf.appendAscii( "value " ).appendAscii( stringValue ).appendAscii( "is unknown in enum " );
178
179 TypeDescription desc( strTypeName );
180 if( desc.is() )
181 {
182 if(desc.get()->eTypeClass != typelib_TypeClass_ENUM )
183 {
184 OUStringBuffer buf;
185 buf.appendAscii( "pyuno.checkEnum: " ).append(strTypeName).appendAscii( "is a " );

--- 13 unchanged lines hidden (view full) ---

199 {
200 break;
201 }
202 }
203 if( i == pEnumDesc->nEnumValues )
204 {
205 OUStringBuffer buf;
206 buf.appendAscii( "value " ).appendAscii( stringValue ).appendAscii( "is unknown in enum " );
207 buf.appendAscii( PyString_AsString( typeName.get() ) );
207 buf.appendAscii( PyBytes_AsString( typeName.get() ) );
208 throw RuntimeException( buf.makeStringAndClear(), Reference<XInterface> () );
209 }
210 ret = Any( &pEnumDesc->pEnumValues[i], desc.get()->pWeakRef );
211 }
212 else
213 {
214 OUStringBuffer buf;
208 throw RuntimeException( buf.makeStringAndClear(), Reference<XInterface> () );
209 }
210 ret = Any( &pEnumDesc->pEnumValues[i], desc.get()->pWeakRef );
211 }
212 else
213 {
214 OUStringBuffer buf;
215 buf.appendAscii( "enum " ).appendAscii( PyString_AsString(typeName.get()) ).appendAscii( " is unknown" );
215 buf.appendAscii( "enum " ).appendAscii( PyBytes_AsString(typeName.get()) ).appendAscii( " is unknown" );
216 throw RuntimeException( buf.makeStringAndClear(), Reference< XInterface> () );
217 }
218 return ret;
219}
220
221
222Type PyType2Type( PyObject * o ) throw(RuntimeException )
223{
224 PyRef pyName( PyObject_GetAttrString( o, const_cast< char * >("typeName") ), SAL_NO_ACQUIRE);
216 throw RuntimeException( buf.makeStringAndClear(), Reference< XInterface> () );
217 }
218 return ret;
219}
220
221
222Type PyType2Type( PyObject * o ) throw(RuntimeException )
223{
224 PyRef pyName( PyObject_GetAttrString( o, const_cast< char * >("typeName") ), SAL_NO_ACQUIRE);
225 if( !PyString_Check( pyName.get() ) )
225 if( !PyBytes_Check( pyName.get() ) )
226 {
227 throw RuntimeException(
228 USTR_ASCII( "type object does not have typeName property" ),
229 Reference< XInterface > () );
230 }
231
232 PyRef pyTC( PyObject_GetAttrString( o, const_cast< char * >("typeClass") ), SAL_NO_ACQUIRE );
233 Any enumValue = PyEnum2Enum( pyTC.get() );
234
226 {
227 throw RuntimeException(
228 USTR_ASCII( "type object does not have typeName property" ),
229 Reference< XInterface > () );
230 }
231
232 PyRef pyTC( PyObject_GetAttrString( o, const_cast< char * >("typeClass") ), SAL_NO_ACQUIRE );
233 Any enumValue = PyEnum2Enum( pyTC.get() );
234
235 OUString name( OUString::createFromAscii( PyString_AsString( pyName.get() ) ) );
235 OUString name( OUString::createFromAscii( PyBytes_AsString( pyName.get() ) ) );
236 TypeDescription desc( name );
237 if( ! desc.is() )
238 {
239 OUStringBuffer buf;
240 buf.appendAscii( "type " ).append(name).appendAscii( " is unknown" );
241 throw RuntimeException(
242 buf.makeStringAndClear(), Reference< XInterface > () );
243 }

--- 29 unchanged lines hidden (view full) ---

273 if( ! typesModule.is() || ! PyModule_Check( typesModule.get() ))
274 {
275 typesModule = PyRef( PyModule_New( const_cast< char * >("unotypes") ), SAL_NO_ACQUIRE );
276 Py_INCREF( typesModule.get() );
277 PyDict_SetItemString( dict, "unotypes" , typesModule.get() );
278 }
279 PyModule_AddObject(
280 typesModule.get(),
236 TypeDescription desc( name );
237 if( ! desc.is() )
238 {
239 OUStringBuffer buf;
240 buf.appendAscii( "type " ).append(name).appendAscii( " is unknown" );
241 throw RuntimeException(
242 buf.makeStringAndClear(), Reference< XInterface > () );
243 }

--- 29 unchanged lines hidden (view full) ---

273 if( ! typesModule.is() || ! PyModule_Check( typesModule.get() ))
274 {
275 typesModule = PyRef( PyModule_New( const_cast< char * >("unotypes") ), SAL_NO_ACQUIRE );
276 Py_INCREF( typesModule.get() );
277 PyDict_SetItemString( dict, "unotypes" , typesModule.get() );
278 }
279 PyModule_AddObject(
280 typesModule.get(),
281 PyString_AsString( target ),
282 PyUNO_Type_new( PyString_AsString(str),tc,runtime ) );
281 PyBytes_AsString( target ),
282 PyUNO_Type_new( PyBytes_AsString(str),tc,runtime ) );
283
284 if( com::sun::star::uno::TypeClass_EXCEPTION == tc ||
285 com::sun::star::uno::TypeClass_STRUCT == tc )
286 {
287 PyRef exc = getClass( name, runtime );
288 PyDict_SetItem( dict, target, exc.getAcquired() );
289 }
290 else if( com::sun::star::uno::TypeClass_ENUM == tc )
291 {
292 // introduce all enums into the dictionary !
293 typelib_EnumTypeDescription *pDesc =
294 (typelib_EnumTypeDescription *) desc.get();
295 for( int i = 0 ; i < pDesc->nEnumValues; i ++ )
296 {
297 OString enumElementName(
298 OUStringToOString( pDesc->ppEnumNames[i], RTL_TEXTENCODING_ASCII_US) );
299 PyDict_SetItemString(
300 dict, (char*)enumElementName.getStr(),
283
284 if( com::sun::star::uno::TypeClass_EXCEPTION == tc ||
285 com::sun::star::uno::TypeClass_STRUCT == tc )
286 {
287 PyRef exc = getClass( name, runtime );
288 PyDict_SetItem( dict, target, exc.getAcquired() );
289 }
290 else if( com::sun::star::uno::TypeClass_ENUM == tc )
291 {
292 // introduce all enums into the dictionary !
293 typelib_EnumTypeDescription *pDesc =
294 (typelib_EnumTypeDescription *) desc.get();
295 for( int i = 0 ; i < pDesc->nEnumValues; i ++ )
296 {
297 OString enumElementName(
298 OUStringToOString( pDesc->ppEnumNames[i], RTL_TEXTENCODING_ASCII_US) );
299 PyDict_SetItemString(
300 dict, (char*)enumElementName.getStr(),
301 PyUNO_Enum_new(PyString_AsString(str) , enumElementName.getStr(), runtime ) );
301 PyUNO_Enum_new(PyBytes_AsString(str) , enumElementName.getStr(), runtime ) );
302 }
303 }
304 Py_INCREF( Py_None );
305 ret = Py_None;
306 }
307 else
308 {
309 Any a = runtime.getImpl()->cargo->xTdMgr->getByHierarchicalName(name);

--- 4 unchanged lines hidden (view full) ---

314 {
315 Py_INCREF( constant.get() );
316 PyDict_SetItem( dict, target , constant.get());
317 ret = constant.get();
318 }
319 else
320 {
321 OStringBuffer buf;
302 }
303 }
304 Py_INCREF( Py_None );
305 ret = Py_None;
306 }
307 else
308 {
309 Any a = runtime.getImpl()->cargo->xTdMgr->getByHierarchicalName(name);

--- 4 unchanged lines hidden (view full) ---

314 {
315 Py_INCREF( constant.get() );
316 PyDict_SetItem( dict, target , constant.get());
317 ret = constant.get();
318 }
319 else
320 {
321 OStringBuffer buf;
322 buf.append( "constant " ).append(PyString_AsString(str)).append( " unknown" );
322 buf.append( "constant " ).append(PyBytes_AsString(str)).append( " unknown" );
323 PyErr_SetString( PyExc_RuntimeError, buf.getStr() );
324 }
325 }
326 else
327 {
328 OUStringBuffer buf;
329 buf.appendAscii( "pyuno.imp unknown type " );
330 buf.append( name );

--- 43 unchanged lines hidden (view full) ---

374 return instance.get();
375
376}
377
378
379PyObject *PyUNO_Enum_new( const char *enumBase, const char *enumValue, const Runtime &r )
380{
381 PyRef args( PyTuple_New( 2 ), SAL_NO_ACQUIRE );
323 PyErr_SetString( PyExc_RuntimeError, buf.getStr() );
324 }
325 }
326 else
327 {
328 OUStringBuffer buf;
329 buf.appendAscii( "pyuno.imp unknown type " );
330 buf.append( name );

--- 43 unchanged lines hidden (view full) ---

374 return instance.get();
375
376}
377
378
379PyObject *PyUNO_Enum_new( const char *enumBase, const char *enumValue, const Runtime &r )
380{
381 PyRef args( PyTuple_New( 2 ), SAL_NO_ACQUIRE );
382 PyTuple_SetItem( args.get() , 0 , PyString_FromString( enumBase ) );
383 PyTuple_SetItem( args.get() , 1 , PyString_FromString( enumValue ) );
382 PyTuple_SetItem( args.get() , 0 , PyBytes_FromString( enumBase ) );
383 PyTuple_SetItem( args.get() , 1 , PyBytes_FromString( enumValue ) );
384
385 return callCtor( r, "Enum" , args );
386}
387
388
389PyObject* PyUNO_Type_new (const char *typeName , TypeClass t , const Runtime &r )
390{
391 // retrieve type object
392 PyRef args( PyTuple_New( 2 ), SAL_NO_ACQUIRE );
393
384
385 return callCtor( r, "Enum" , args );
386}
387
388
389PyObject* PyUNO_Type_new (const char *typeName , TypeClass t , const Runtime &r )
390{
391 // retrieve type object
392 PyRef args( PyTuple_New( 2 ), SAL_NO_ACQUIRE );
393
394 PyTuple_SetItem( args.get() , 0 , PyString_FromString( typeName ) );
394 PyTuple_SetItem( args.get() , 0 , PyBytes_FromString( typeName ) );
395 PyObject *typeClass = PyUNO_Enum_new( "com.sun.star.uno.TypeClass" , typeClassToString(t), r );
396 if( ! typeClass )
397 return NULL;
398 PyTuple_SetItem( args.get() , 1 , typeClass);
399
400 return callCtor( r, "Type" , args );
401}
402

--- 9 unchanged lines hidden (view full) ---

412
413 return callCtor( r, "Char" , args );
414}
415
416PyObject *PyUNO_ByteSequence_new(
417 const com::sun::star::uno::Sequence< sal_Int8 > &byteSequence, const Runtime &r )
418{
419 PyRef str(
395 PyObject *typeClass = PyUNO_Enum_new( "com.sun.star.uno.TypeClass" , typeClassToString(t), r );
396 if( ! typeClass )
397 return NULL;
398 PyTuple_SetItem( args.get() , 1 , typeClass);
399
400 return callCtor( r, "Type" , args );
401}
402

--- 9 unchanged lines hidden (view full) ---

412
413 return callCtor( r, "Char" , args );
414}
415
416PyObject *PyUNO_ByteSequence_new(
417 const com::sun::star::uno::Sequence< sal_Int8 > &byteSequence, const Runtime &r )
418{
419 PyRef str(
420 PyString_FromStringAndSize( (char*)byteSequence.getConstArray(), byteSequence.getLength()),
420 PyBytes_FromStringAndSize( (char*)byteSequence.getConstArray(), byteSequence.getLength()),
421 SAL_NO_ACQUIRE );
422 PyRef args( PyTuple_New( 1 ), SAL_NO_ACQUIRE );
423 PyTuple_SetItem( args.get() , 0 , str.getAcquired() );
424 return callCtor( r, "ByteSequence" , args );
425
426}
427}
421 SAL_NO_ACQUIRE );
422 PyRef args( PyTuple_New( 1 ), SAL_NO_ACQUIRE );
423 PyTuple_SetItem( args.get() , 0 , str.getAcquired() );
424 return callCtor( r, "ByteSequence" , args );
425
426}
427}