Skip to content

PyObjc & Seemingly Incorrect Return Value Mismatch

Categories: Software

Table of Contents

I’ve come to love developing in PyObjc. Sure, it has its quirks, isn’t the fastest, and isn’t the easiest to debug when something goes really wrong (aka imperfect integration with the xcode IDE); but it is fast to develop in. Almost every seemingly complex task that has come my way has been 50% completed by some open source python module that I can include in a commercial app without any licensing trouble.

However, the other day I came across a bug (at least, what I thought was a bug) that seemed very blaringly obvious and for a production version of a scripting bridge. When implementing the NSTableView delegate’s method tableView_toolTipForCell_rect_tableColumn_row_mouseLocation_ I was getting an error when simply returning an NSString ( TypeError: tableView:toolTipForCell:rect:tableColumn:row:mouseLocation:: Need tuple of 2 arguments as result) and for the life of me I couldn’t figure out why. Apple’s documentation states that I should return an NSString and I couldn’t find any information about a special case in pyobjc for this specific method.

I guess I did not search hard enough through the uncentralized incomplete documentation spread across the web since one of the PyObjc devs was kind enough to respond with a simple explanation:

This is not a bug, the ‘rect’ argument is a pass-by-reference argument that
can be modified, hence you have to return two values: both the return value
itself and the (possibly updated) value of rect:

return (aToolTip, aRect)