How do I get type methods?

Y

yavannadil

Hello!

If I do

import uno
localContext=uno.getComponentContext()

then localContext is of type <type 'pyuno'>
I guess it's a new type provided by PyUNO extension.
localContext.__class__ is None
Is there any way to list all methods of that new type, via Python C
API or through interpreter (other then dir(localContext) )?
What I want is to call localContext's methods like in the tutorial
example:

x=MyClass()
MyClass.f(x)

Thank you,
Dmitri
 
S

Stargaming

Hello!

If I do

import uno
localContext=uno.getComponentContext()

then localContext is of type <type 'pyuno'>
I guess it's a new type provided by PyUNO extension.
localContext.__class__ is None
Is there any way to list all methods of that new type, via Python C
API or through interpreter (other then dir(localContext) )?

What's wrong about `dir()`?
What I want is to call localContext's methods like in the tutorial
example:

x=MyClass()
MyClass.f(x)

I'd prefer::

x = MyClass()
x.f()
 
Y

yavannadil

What's wrong about `dir()`?
x = MyClass()
x.f()

I want to cashe pointers to Python functions in a non-Python app.
'dir()' requires an argument, and I want to get function pointers
before I have any variable of given type or class.
That's why 'MyClass.f(x)'
I'm not against 'dir(MyClass)'; the question is, what should I 'dir()'
to get methods of 'pyuno' type instance?

Sincerely yours,
Dmitri
 
G

Gabriel Genellina

I'm not against 'dir(MyClass)'; the question is, what should I 'dir()'
to get methods of 'pyuno' type instance?

Usually instances don't have its own methods, they get them from the
class. So you actually need dir(MyClass).
Note that dir() might not show all available methods.
 
Y

yavannadil

Usually instances don't have its own methods, they get them from the
class. So you actually need dir(MyClass).
Note that dir() might not show all available methods.

Let me retype my question: what I 'dir()' in case of 'pyuno' type
instance?
Or in case of 'dict' type instance? Or in case of any other new python
type?
 
T

Thomas Nelson

Let me retype my question: what I 'dir()' in case of 'pyuno' type
instance?
Or in case of 'dict' type instance? Or in case of any other new python
type?
.... def f(self,x):
.... print x+1
.... def g(self,x):
.... print x-1
....['__doc__', '__module__', 'f', 'g']

Is this not what you want? These are the only methods in the Foo
class.

Tom
 
M

Marc 'BlackJack' Rintsch

Thomas Nelson said:
Let me retype my question: what I 'dir()' in case of 'pyuno' type
instance?
Or in case of 'dict' type instance? Or in case of any other new python
type?
... def f(self,x):
... print x+1
... def g(self,x):
... print x-1
...['__doc__', '__module__', 'f', 'g']

Is this not what you want? These are the only methods in the Foo
class.

The OPs problem is, there is no access to the class or type because
there is no name. You can get just instances from a factory function.

Ciao,
Marc 'BlackJack' Rintsch
 
Y

yavannadil

dir(type(localContext))
Perhaps ?

It gives
['__class__', '__cmp__', '__delattr__', '__doc__', '__getattribute__',
'__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__',
'__repr__', '__setattr__', '__str__']

while

import sys
dir(type(sys.modules))

gives
['__class__', '__cmp__', '__contains__', '__delattr__', '__delitem__',
'__doc__', '__eq__', '__ge__', '__getattribute__', '__getitem__',
'__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__',
'__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__',
'__repr__', '__setattr__', '__setitem__', '__str__', 'clear', 'copy',
'fromkeys', 'get', 'has_key', 'items', 'iteritems', 'iterkeys',
'itervalues', 'keys', 'pop', 'popitem', 'setdefault', 'update',
'values']

so I guess PyUNO extension doesn't provide proper introspection :-(
 
Y

yavannadil

The OPs problem is, there is no access to the class or type because
there is no name.

Exactly :-(
You can get just instances from a factory function.

Worse, if I call

localContext.ServiceManage

I'll get something with different set of methods, but of the same type
- 'pyuno' :-(
 
C

Carsten Haese

Exactly :-(


Worse, if I call

localContext.ServiceManage

I'll get something with different set of methods, but of the same type
- 'pyuno' :-(

'pyuno' objects are proxy objects that represent UNO objects, services,
and interfaces. Since all attribute lookups are handled by the UNO
bridge, the proxy object doesn't actually know what attributes it has,
which is why it won't respond anything useful to the usual dir()
inspection.

To list the methods and properties that the UNO object behind a pyuno
proxy object has, you need to use UNO inspection capabilities. Something
like the following seems to work:

# unodir.py
def unodir(unoobj):
import uno
from com.sun.star.beans.MethodConcept import ALL as ALLMETHS
from com.sun.star.beans.PropertyConcept import ALL as ALLPROPS
ctx = uno.getComponentContext()
introspection = ctx.ServiceManager.createInstanceWithContext(
"com.sun.star.beans.Introspection", ctx)
access = introspection.inspect(unoobj)
meths = access.getMethods(ALLMETHS)
props = access.getProperties(ALLPROPS)
return [ x.getName() for x in meths ] + [ x.Name for x in props ]
[u'queryInterface', u'acquire', u'release', u'getValueByName',
u'getServiceManager', u'getElementType', u'hasElements', u'getByName',
u'getElementNames', u'hasByName', u'replaceByName', u'insertByName',
u'removeByName', u'getTypes', u'getImplementationId', u'queryAdapter',
u'dispose', u'addEventListener', u'removeEventListener',
u'ServiceManager', u'ElementType', u'ElementNames', u'Types',
u'ImplementationId']

Hope this helps,

Carsten
 
Y

yavannadil

'pyuno' objects are proxy objects that represent UNO objects, services,
and interfaces. Since all attribute lookups are handled by the UNO
bridge, the proxy object doesn't actually know what attributes it has,
which is why it won't respond anything useful to the usual dir()
inspection.

I suspected something like that, but couldn't quite believe as CORBA
proxies generated by C++/C/Java/Lisp/Python IDL compiler are normal
classes which know what methods they have...
To list the methods and properties that the UNO object behind a pyuno
proxy object has, you need to use UNO inspection capabilities. Something
like the following seems to work:

Thanks a lot! After reading your example I googled and found
unohelper.inspect, which provides all information. But I still need
instances, as 'dir(XSingleComponentFactory)' gives just ['__doc__',
'__module__', '__pyunointerface__'], and
'unohelper.inspect(XSingleComponentFactory, sys.stdout)' gives
segmentation fault.

Sincerely yours,
Dmitri
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top