0

I'm currently reading the code of virt-manager/virt-clone, but did not do much python before.

Here is the important parts of the code:


https://github.com/virt-manager/virt-manager/blob/master/virt-clone#L169

conn = cli.getConnection(options.connect)
design = Cloner(conn)

https://github.com/virt-manager/virt-manager/blob/master/virtinst/cli.py#L263

from .connection import VirtualConnection
conn = VirtualConnection(uri)

https://github.com/virt-manager/virt-manager/blob/master/virtinst/connection.py#L35

class VirtualConnection(object):

https://github.com/virt-manager/virt-manager/blob/master/virtinst/cloner.py#L591

return self.conn.lookupByName(name)

The last line looks like there is a method lookupByName called for an object of class VirtualConnection. However I cannot find such a method defined in that class and it does not have a parent class to get such a method from, either.

Can you help me spot the place where lookupByName is defined?

What might be a good way to find such an answer myself? (debugger, etc?)

3 Answers 3

2

The lookupByName method of the virConnect object is defined in C as part of libvirt's Python bindings. The following code forwards the attribute accesses from it to that object which was returned by libvirt.openAuth and assigned to self._libvirtconn:

https://github.com/virt-manager/virt-manager/blob/master/virtinst/connection.py#L94

def __getattr__(self, attr):
    if attr in self.__dict__:
        return self.__dict__[attr]

    # Proxy virConnect API calls
    libvirtconn = self.__dict__.get("_libvirtconn")
    return getattr(libvirtconn, attr)

This causes conn.lookupByName to be conn._libvirtconn.lookupByName.

Sign up to request clarification or add additional context in comments.

Comments

0

I haven't investigated your case, but in Python methods can be dynamically added to a class or to an instance

class A:
    def a(self):
        print 'a'

class C(A):
    def c(self):
        print 'c'

def foo(arg):
    print 'foo',arg

a = C()
print dir(a)

C.f = foo

a.a()
a.c()
a.f()
print dir(a)

the result is:

['__doc__', '__module__', 'a', 'c']
a
c
foo <__main__.C instance at 0x024CB9B8>
['__doc__', '__module__', 'a', 'c', 'f']

As you can see the second call to dir shows new method 'f'. So in some cases it might be tricky to find the origin of that.

Comments

0

The VirtualConnection class you refer to has an implementation of __getattr__, which is a method that will be called when an attribute cannot be resolved.

This __getattr__ method looks up attributes on a "_libvirtconn" attribute of the VirtualConnection instance.

See also: https://docs.python.org/3/reference/datamodel.html#object.getattr

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.