5 // Created by Joseph Spiros on 3/5/09.
6 // Copyright iThink Software 2009. All rights reserved.
9 #import <Python/Python.h>
10 #import <Cocoa/Cocoa.h>
12 int main(int argc, char *argv[])
14 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
16 NSBundle *mainBundle = [NSBundle mainBundle];
17 NSString *resourcePath = [mainBundle resourcePath];
18 NSArray *pythonPathArray = [NSArray arrayWithObjects: resourcePath, [resourcePath stringByAppendingPathComponent:@"PyObjC"], nil];
20 setenv("PYTHONPATH", [[pythonPathArray componentsJoinedByString:@":"] UTF8String], 1);
22 NSArray *possibleMainExtensions = [NSArray arrayWithObjects: @"py", @"pyc", @"pyo", nil];
23 NSString *mainFilePath = nil;
25 for (NSString *possibleMainExtension in possibleMainExtensions) {
26 mainFilePath = [mainBundle pathForResource: @"main" ofType: possibleMainExtension];
27 if ( mainFilePath != nil ) break;
30 if ( !mainFilePath ) {
31 [NSException raise: NSInternalInconsistencyException format: @"%s:%d main() Failed to find the Main.{py,pyc,pyo} file in the application wrapper's Resources directory.", __FILE__, __LINE__];
34 Py_SetProgramName("/usr/bin/python");
36 PySys_SetArgv(argc, (char **)argv);
38 const char *mainFilePathPtr = [mainFilePath UTF8String];
39 FILE *mainFile = fopen(mainFilePathPtr, "r");
40 int result = PyRun_SimpleFile(mainFile, (char *)[[mainFilePath lastPathComponent] UTF8String]);
43 [NSException raise: NSInternalInconsistencyException
44 format: @"%s:%d main() PyRun_SimpleFile failed with file '%@'. See console for errors.", __FILE__, __LINE__, mainFilePath];