#24 isinstance() not working with cgtypes

Bug
closed-works-for-me
Matthias Baas
Modules (11)
5
2010-01-23
2010-01-20
Maxime Lemonnier
No

if I do

v = vec3()

print isinstance(v, vec3)
>> False

Discussion

  • Matthias Baas
    Matthias Baas
    2010-01-21

    • status: open --> open-works-for-me
     
  • Matthias Baas
    Matthias Baas
    2010-01-21

    See my comment to bug 2935729, the same applies here.
    In short: I can't reproduce this. Over here, isinstance() returns True.

    Does this happen inside a program or even in the shell? How did you import cgkit? Maybe another "vec3" object gets imported from somewhere else?

     
  • OS : Ubuntu
    git-master

    It does not make this in the console

    inspecting the type gives me : cgkit._core.vec3 vs cgkit.cgtypes.vec3

    Thanks

     
  • I imported it using

    from cgkit.cgtypes import *

    OR

    from cgkit import cgtypes

     
  • Matthias Baas
    Matthias Baas
    2010-01-22

    So what happens if you run the following lines:

    from cgkit.cgtypes import *
    v=vec3()
    print vec3
    print type(v)
    print isinstance(v, vec3)

    You should see this:

    <class 'cgkit.cgtypes.vec3'>
    <class 'cgkit.cgtypes.vec3'>
    True

    So are you saying you see this instead:

    <class 'cgkit.cgtypes.vec3'>
    <class 'cgkit._core.vec3'>
    False

    It's true that there are two vec3 classes, the one in _core is the wrapped C++ class whereas the one in cgtypes is a Python class that derives from _core.vec3 and just provides a more flexible constructor. If you explicitly create a cgtypes.vec3 as above, then you get just that and if in your case, Python somehow turns that into a _core.vec3, then something went horribly wrong with your Python build.
    However, if you obtained the vec3 from calling some (C++) method somewhere, then you will indeed get a _core.vec3 and a isinstance() call like above would fail.

     
  • I see the same results as you. But try this :

    m = mat3.fromEulerXYZ(0,0,0)

    print mat3
    print type(m)
    print isinstance(m, vec3)

    I get
    <class 'cgkit.cgtypes.mat3'>
    <class 'cgkit._core.mat3'>
    False

    And I suppose that, from that moment, any operation that I will do involving this matrix will return cgkit._core containers.

     
  • Matthias Baas
    Matthias Baas
    2010-01-23

    Yes, this is a demonstration of what I described in my earlier post. The fromEulerXYZ() method is a C++ method which returns a mat3, so that mat3 will be the one from the _core module because this is what Boost knows about.
    I agree that this situation is not ideal, but it's not really a bug. It's just the way it's implemented. I don't really know how I could get the same constructors that the cgtypes objects provide into the C++ wrapper.

    But note that your last example is a completely different case than your initial example.

    So if you really need to know if something is a vec3, mat3, etc., then the simplest workaround would be to check against the types in the _core module. But I would also recommend to have a look at your program and check if you really need to know what concrete type some object is. From my experience, this is rarely required and your program will even be more flexible if you just accept anything as a vector that contains 3 floats. For example, if you have a function that takes a vector v as input, you could just wrap that input by vec3(v) and then you can be sure you have a vec3 and the function can be called with either a vec3, a tuple a list, and so on.

    I'll close this issue, as it's not a bug. If you think support for isinstance() is an essential thing to have, then feel free to open a feature request so that I get reminded about this eventually. But as I said, I don't know how this can be easily fixed, so don't expect that this will be quickly implemented. Maybe it would also be worth to discuss this in the mailing list to see if other people have ideas how to implement this.

     
  • Matthias Baas
    Matthias Baas
    2010-01-23

    • status: open-works-for-me --> closed-works-for-me
     
  • Ok, sorry about that. As soon as I found out what was the source of the problem, I implemented this workaround.

    It is a quite unexpected behaviour, though.

    Thanks and sorry I didn't do my homework on this one.

    As for bug ID: 2935729, (list(vec3()) == seg fault), I will try the mailing list.

    Thanks.