« It's not in a box. anymore... | Main | Bob »

PyObjC proxy fun

PyObjC is cool. Really. No Kidding. What I'm about to say, though, is making me think that my friend Patrick www.patrickkidd.com has a better idea in using pyQT for gui work...

When I'm using an external library, say perhaps one that sends information over a network, I try to be careful to keep from passing the lower-level code anything that could muck things up. Well... it turns out that it's nearly impossible to keep python's object types from being converted to pyobjc objects. This isn't too surprising, given that many ints, strings, etc. have to make their way into objc arguments, though I was making the improper assumption that python objects were converted through some kind of transparent proxy only when crossing that boundary to the objc runtime. I find out now that python objects are sometimes just converted. That being the case, I didn't expect those converted objects to act like anything but your typical python object from the point of view of python (ala duck typing... sorta). Again. WRONG. Somehow, for instance, the integer type sometimes turns into an OC_PythonInt (which has a method intValue() to get to the python int it represents, FYI) and it doesn't have the same methods as a python int. Bummer. It's kinda hard to send that over the network.

What to do?

"assert" statements, I guess.

TrackBack

TrackBack URL for this entry:
http://www.jonathansaggau.com/blog/mt-tb.cgi/44

Comments

First of all, Python ints don't have any (non-special) methods.

When numbers cross the bridge as an object, they get proxied as a subclass of int. If your code doesn't like this, then your code is broken. You should be using isinstance(foo, int) instead of type(foo) is int.

>>> objc.repythonify(1)
1
>>> type(_)

>>> x = objc.repythonify(1)
>>> type(x)

>>> type(x).__bases__
(,)

Hey thanks (again) Bob. I'll be making a correction (or ten) to my code, approving your comment to my blog, etc.

I did think of trying isinstance in some test code earlier tonight... and it came back false but now I know that I probably screwed something else up. I'm also using "somebody else's" OSC (Open Sound Control), which uses the type(foo) is int. I was hoping, stupidly, to avoid having to touch it.

You should also know that I was just about to send a note to the SIG list about this. I hope you didn't get the impression that I was consciously spreading incorrect information.

And thanks for reading my blog (I feel all famous or something, man). I feel like I've been visited by Dr. Who.

Cheers,
Jonathan Saggau

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)