I have some python (v2.7) code that uses OS X's built in PyObjC bindings to get the executable's path from a bundle via NSBundle bundleWithPath:
#bundlePath is a string/path to a .app or .kext
mainBundle = Foundation.NSBundle.bundleWithPath_(bundlePath)
if mainBundle:
binaryPath = mainBundle.executablePath()
A customer reported that this was throwing an exception, and emailed me the backtrace:
...
File "utils.py", line 184, in getBinaryFromBundle
binaryPath = mainBundle.executablePath()
AttributeError: 'NoneType' object has no attribute 'executablePath'
Unfortunately, I can't replicative this on my box. NSBundle bundleWithPath: always returns either None, or a valid bundle object, which the code seems to handle as expected.
>>> print Foundation.NSBundle.bundleWithPath_(None)
None
>>> print Foundation.NSBundle.bundleWithPath_("invalid/path")
None
>>> print Foundation.NSBundle.bundleWithPath_("/Applications/Calculator.app/")
NSBundle </Applications/Calculator.app> (not yet loaded)
Sure I could wrap this code in a try/except, but my question is, why isn't the if statement preventing this exception?
update 1
Based on suggestions, I've confirmed that:
1) indentation is correct
2) the customer is using the correct (most recent) code
...still trying to figure this one out!