Source

pyobjc / Xcode / Project Templates / Cocoa-Python Document-based Application / main.m

The branch 'pyobjc-ancient' does not exist.
Full commit
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
//
//  main.m
//  ÇPROJECTNAMEČ
//
//  Created by ÇFULLUSERNAMEČ on ÇDATEČ.
//  Copyright (c) ÇYEARČ ÇORGANIZATIONNAMEČ. All rights reserved.
//

#import <Cocoa/Cocoa.h>
#include <mach-o/dyld.h>
#include <sys/types.h>
#include <sys/stat.h>

//
// Constants
//
NSString *ERR_REALLYBADTITLE = @"The application could not be launched.";
NSString *ERR_TITLEFORMAT = @"%@ has encountered a fatal error, and will now terminate.";
NSString *ERR_NONAME = @"The Info.plist file must have values for the CFBundleName or CFBundleExecutable strings.";
NSString *ERR_PYRUNTIMELOCATIONS = @"The Info.plist file must have a PyRuntimeLocations array containing string values for preferred Python runtime locations.  These strings should be \"otool -L\" style mach ids; \"@executable_stub\" and \"~\" prefixes will be translated accordingly.";
NSString *ERR_NOPYTHONRUNTIME = @"A Python runtime could be located.  You may need to install a framework build of Python, or edit the PyRuntimeLocations array in this application's Info.plist file.\rThese runtime locations were attempted:\r\r";
NSString *ERR_NOPYTHONSCRIPT = @"A main script could not be located in the Resources folder.\rThese files were tried:\r\r";
NSString *ERR_LINKERRFMT = @"An internal error occurred while attempting to link with:\r\r%s\r\rSee the Console for a detailed dyld error message";
NSString *ERR_PYTHONEXCEPTION = @"An uncaught exception was raised during execution of the main script:\r\r%@: %@\r\rThis may mean that an unexpected error has occurred, or that you do not have all of the dependencies for this application.\r\rSee the Console for a detailed traceback.";
NSString *ERR_DEFAULTURLTITLE = @"Visit Website";
NSString *ERR_CONSOLEAPP = @"Console.app";
NSString *ERR_CONSOLEAPPTITLE = @"Open Console";
NSString *ERR_TERMINATE = @"Terminate";
#define PYMACAPP_NSIMAGEFLAGS (NSADDIMAGE_OPTION_RETURN_ON_ERROR | NSADDIMAGE_OPTION_WITH_SEARCHING)
#define PYMACAPP_NSLOOKUPSYMBOLINIMAGEFLAGS (NSLOOKUPSYMBOLINIMAGE_OPTION_BIND | NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR)
//
// THIS WILL NOT WORK WITH Py_TRACE_REFS / Py_DEBUG ON!!! WARNING!!
//
#define Py_XDECREF(op) if ((op) == NULL) ; else --(*op)

//
// Typedefs
//

typedef int PyObject;
typedef void (*Py_SetProgramNamePtr)(const char *);
typedef void (*Py_InitializePtr)(void);
typedef int (*PyRun_SimpleFilePtr)(FILE *, const char *);
typedef void (*Py_FinalizePtr)(void);
typedef PyObject *(*PySys_GetObjectPtr)(const char *);
typedef int *(*PySys_SetArgvPtr)(int argc, char **argv);
typedef PyObject *(*PyObject_StrPtr)(PyObject *);
typedef const char *(*PyString_AsStringPtr)(PyObject *);
typedef PyObject *(*PyObject_GetAttrStringPtr)(PyObject *, const char *);

//
// Signatures
//

int report_error(NSString *err);
int report_linkEdit_error(void);
int report_script_error(NSString *err, NSString *errClassName, NSString *errName);
NSString *pyStandardizePath(NSString *pyLocation);
BOOL doesPathExist(NSString *path);
NSString *getApplicationName(void);
NSString *getErrorTitle(NSString *applicationName);
int pyobjc_main(int argc, char * const *argv, char * const *envp);
int main(int argc, char * const *argv, char * const *envp);


//
// Implementation
//

int report_script_error(NSString *err, NSString *errClassName, NSString *errName) {

	NSArray *errorScripts = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"PyErrorScripts"];
	if ( !errorScripts )
		errorScripts = [NSArray array];
	// find main python file.  __main__.py seems to be a standard, so we'll go ahead and add defaults.
	errorScripts = [errorScripts arrayByAddingObjectsFromArray:[NSArray arrayWithObjects:
		@"__error__",
		@"__error__.py",
		@"__error__.sh",
		nil]];
	NSEnumerator *errorScriptsEnumerator = [errorScripts objectEnumerator];
	NSString *path = nil;
	NSString *nextFileName;
	
	while (nextFileName = [errorScriptsEnumerator nextObject]) {
		path = [[NSBundle mainBundle] pathForResource: nextFileName ofType: nil];
		if ( path )
			break;
	}
	
	if ( !path )
		return report_error(err);
	
	NSTask *task = [[NSTask alloc] init];
	NSPipe *stdoutPipe = [NSPipe pipe];
	[task setLaunchPath: path];
	[task setArguments: [NSArray arrayWithObjects:getApplicationName(),errClassName,errName,nil]];
	[task setStandardOutput: stdoutPipe];

	NS_DURING
		[task launch];
		[task waitUntilExit];
	NS_HANDLER
		NSLog(@"Could not execute %@: %@", [path lastPathComponent], localException);
		[task release];
		return report_error(err);
	NS_ENDHANDLER

	NSData *taskData = [[stdoutPipe fileHandleForReading] readDataToEndOfFile];
	[task autorelease];
	
	if ([task terminationStatus])
		return report_error(err);
	NSString *result = [[[NSString alloc] initWithData:taskData encoding:NSUTF8StringEncoding] autorelease];
	NSArray *lines = [result componentsSeparatedByString:@"\n"];
	while ([[lines lastObject] isEqualToString: @""])
		lines = [lines subarrayWithRange:NSMakeRange(0, [lines count]-1)];
	if ( ![lines count] )
		return report_error(err);
	NSURL *buttonURL = nil;
	NSString *buttonString = nil;
		
	if ([[lines lastObject] hasPrefix:@"ERRORURL: "]) {
		NSString *lastLine = [lines lastObject];
		lines = [lines subarrayWithRange:NSMakeRange(0, [lines count]-1)];
		NSArray *buttonArr = [lastLine componentsSeparatedByString:@" "];
		buttonArr = [buttonArr subarrayWithRange:NSMakeRange(1, [buttonArr count]-1)];
		while ([[buttonArr objectAtIndex:0] isEqualToString: @""])
			buttonArr = [buttonArr subarrayWithRange:NSMakeRange(1, [buttonArr count]-1)];
		buttonURL = [NSURL URLWithString:[buttonArr objectAtIndex:0]];
		if ( buttonURL ) {
			buttonArr = [buttonArr subarrayWithRange:NSMakeRange(1, [buttonArr count]-1)];
			while ([[buttonArr objectAtIndex:0] isEqualToString: @""])
				buttonArr = [buttonArr subarrayWithRange:NSMakeRange(1, [buttonArr count]-1)];
			buttonString = [buttonArr componentsJoinedByString:@" "];
			if ( !buttonString )
				buttonString = ERR_DEFAULTURLTITLE;
		}
	}

	NSString *title = nil;
	NSString *msg = nil;
	title = [lines objectAtIndex:0];
	if ( !title )
		return report_error(err);
	
	msg = [[lines subarrayWithRange:NSMakeRange(1, [lines count]-1)] componentsJoinedByString:@"\r"];
	if ( !msg )
		msg = @"";
	
	NSLog(title);
	NSLog(msg);
	[NSApplication sharedApplication];
	if ( !buttonURL ) {
		int choice = NSRunAlertPanel(title, msg, ERR_TERMINATE, ERR_CONSOLEAPPTITLE, NULL);
		switch (choice) {
			case NSAlertAlternateReturn:
				[[NSWorkspace sharedWorkspace] launchApplication:ERR_CONSOLEAPP];
				break;
			default:
				break;
		}
	} else {
		int choice = NSRunAlertPanel(title, msg, ERR_TERMINATE, buttonString, NULL);
		switch (choice) {
			case NSAlertAlternateReturn:
				[[NSWorkspace sharedWorkspace] openURL:buttonURL];
				break;
			default:
				break;
		}
	}
	
	return -1;
}

int report_error(NSString *err) {
	int choice;
	[NSApplication sharedApplication];
	NSLog(err);
	choice = NSRunAlertPanel(getErrorTitle(getApplicationName()), err, ERR_TERMINATE, ERR_CONSOLEAPPTITLE, NULL);
	switch (choice) {
		case NSAlertAlternateReturn:
			[[NSWorkspace sharedWorkspace] launchApplication:ERR_CONSOLEAPP];
			break;
		default:
			break;
	}
	return -1;
}

int report_linkEdit_error() {
	NSLinkEditErrors errorClass;
	int errorNumber;
	const char *fileName;
	const char *errorString;
	NSLinkEditError(&errorClass, &errorNumber, &fileName, &errorString);
	NSLog(@"%s", errorString);
	return report_error([NSString stringWithFormat:ERR_LINKERRFMT,fileName]);
}


NSString *pyStandardizePath(NSString *pyLocation) {
		if ([pyLocation hasPrefix:@"@executable_path/"]) {
			NSMutableArray *newComponents = [[pyLocation pathComponents] mutableCopy];
			[newComponents replaceObjectAtIndex:0 withObject:[[NSBundle mainBundle] privateFrameworksPath]];
			pyLocation = [NSString pathWithComponents: newComponents];
		}
		return [pyLocation stringByStandardizingPath];
};

BOOL doesPathExist(NSString *path) {
		struct stat sb;
		return (stat([path fileSystemRepresentation], &sb) == -1) ? NO : YES;
}


NSString *getApplicationName(void) {
	NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
	NSString *applicationName = [infoDictionary objectForKey:@"CFBundleName"];
	if (!applicationName) {
		applicationName = [infoDictionary objectForKey:@"CFBundleExecutable"];
	}
	return applicationName;
}

NSString *getErrorTitle(NSString *applicationName) {
	if (!applicationName)
		return ERR_REALLYBADTITLE;
	return [NSString stringWithFormat:ERR_TITLEFORMAT,applicationName];
}



int pyobjc_main(int argc, char * const *argv, char * const *envp) {
	// little sanity check.
	if ( !getApplicationName() )
		return report_error(ERR_NONAME);
	
	
	// XXX - I'm not going to default anything here.. explicit is better than implicit.

	// get the runtime locations from the Info.plist
	NSArray *pyLocations = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"PyRuntimeLocations"];
	if ( !pyLocations )
		return report_error(ERR_PYRUNTIMELOCATIONS);
	
	// XXX - *does not* inspect DYLD environment variables for overrides, fallbacks, suffixes, etc.
	//		 I don't really consider that a very bad thing, as it makes this search extremely deterministic.
	//		 Note that I use the env variables when the image is actually linked, so what you find here 
	//		 may not be what gets linked.  If this is the case, you deserve it :)

	// find a Python runtime
	NSString *pyLocation;
	NSEnumerator *pyLocationsEnumerator = [pyLocations objectEnumerator];
	while (pyLocation = [pyLocationsEnumerator nextObject]) {
		pyLocation = pyStandardizePath(pyLocation);
		if ( doesPathExist(pyLocation) )
			break;
	}
	if ( !pyLocation ) {
	
		return report_script_error([ERR_NOPYTHONRUNTIME stringByAppendingString:[pyLocations componentsJoinedByString:@"\r\r"]], nil, nil);
	}
	
	NSString *pythonPath = [[[NSProcessInfo processInfo] environment] objectForKey: @"PYTHONPATH"];
	NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
	NSMutableArray *pythonPathArray = [NSMutableArray arrayWithObjects: resourcePath, [resourcePath stringByAppendingPathComponent:@"PyObjC"], nil];
	if (pythonPath != nil)
		[pythonPathArray addObjectsFromArray: [pythonPath componentsSeparatedByString: @":"]];

	// I *refuse* to set dirty DYLD environment variables..  
	// If you want that, you'll have to do it in your main script or fork this bootstrap ;)
	//
	setenv("PYTHONPATH", [[pythonPathArray componentsJoinedByString:@":"] UTF8String], 1);
	
	NSArray *possibleMains = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"PyMainFileNames"];
	if ( !possibleMains )
		possibleMains = [NSArray array];
	// find main python file.  __main__.py seems to be a standard, so we'll go ahead and add defaults.
	possibleMains = [possibleMains arrayByAddingObjectsFromArray:[NSArray arrayWithObjects:
		@"__main__.py",
		@"__main__.pyc",
		@"__main__.pyo",
		@"__realmain__.py",
		@"__realmain__.pyc",
		@"__realmain__.pyo",
		@"Main.py",
		@"Main.pyc",
		@"Main.pyo",
		nil]];
	NSEnumerator *possibleMainsEnumerator = [possibleMains objectEnumerator];
	NSString *mainPyPath = nil;
	NSString *nextFileName;
	
	while (nextFileName = [possibleMainsEnumerator nextObject]) {
		mainPyPath = [[NSBundle mainBundle] pathForResource: nextFileName ofType: nil];
		if ( mainPyPath )
			break;
	}

	if ( !mainPyPath )
		return report_error([ERR_NOPYTHONSCRIPT stringByAppendingString:[possibleMains componentsJoinedByString:@"\r"]]);
	
	const struct mach_header *py_dylib = NSAddImage([pyLocation fileSystemRepresentation], PYMACAPP_NSIMAGEFLAGS);
	if ( !py_dylib ) 
		return report_linkEdit_error();
	
	NSSymbol tmpSymbol;

#define LOOKUP(NAME) \
	tmpSymbol = NSLookupSymbolInImage(py_dylib, "_" #NAME, PYMACAPP_NSLOOKUPSYMBOLINIMAGEFLAGS); \
	if ( !tmpSymbol ) \
		return report_linkEdit_error(); \
	NAME ## Ptr NAME = (NAME ## Ptr)NSAddressOfSymbol(tmpSymbol)

	LOOKUP(Py_SetProgramName);
	LOOKUP(Py_Initialize);
	LOOKUP(PyRun_SimpleFile);
	LOOKUP(Py_Finalize);
	LOOKUP(PySys_GetObject);
	LOOKUP(PySys_SetArgv);
	LOOKUP(PyObject_Str);
	LOOKUP(PyString_AsString);
	LOOKUP(PyObject_GetAttrString);

#undef LOOKUP

	NSString *pythonProgramName;
	// XXX - this is NOT tested with dylib builds.. but it might work if you do things "right"
	if ([[[pyLocation stringByDeletingLastPathComponent] lastPathComponent] isEqualToString:@"lib"]) {
		// $PREFIX/lib/python.dylib -> $PREFIX
		pythonProgramName = [[pyLocation stringByDeletingLastPathComponent] stringByDeletingLastPathComponent];
	} else {
		// $PREFIX/Python -> $PREFIX
		pythonProgramName = [pyLocation stringByDeletingLastPathComponent];
	}
	
	
	// Python might not copy the strings, and some evil code may not create a new NSAutoreleasePool.
	// Who knows.  We retain things just in case...	
	NSString *pyExecutableName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"PyExecutableName"];
	if ( !pyExecutableName )
		pyExecutableName = @"python";

	pythonProgramName = [[[pythonProgramName stringByAppendingPathComponent:@"bin"] stringByAppendingPathComponent:pyExecutableName] retain];
	Py_SetProgramName([pythonProgramName fileSystemRepresentation]);
	Py_Initialize();
	PySys_SetArgv(argc, (char **)argv);
	
	NSAutoreleasePool *pythonPool = [[NSAutoreleasePool alloc] init];

	[mainPyPath retain];
	FILE *mainPy = fopen([mainPyPath fileSystemRepresentation], "r");
	int rval = PyRun_SimpleFile(mainPy, [mainPyPath fileSystemRepresentation]);
	fclose(mainPy);
	[mainPyPath release];

	while ( rval ) {
		PyObject *exc = PySys_GetObject("last_type");		
		if ( !exc ) {
			rval = report_error([NSString stringWithFormat:ERR_PYTHONEXCEPTION,"<<PyMacAppException>>","The exception went away?"]);
			break;
		}

		PyObject *exceptionClassName = PyObject_GetAttrString(exc, "__name__");
		if ( !exceptionClassName ) {
			rval = report_error([NSString stringWithFormat:ERR_PYTHONEXCEPTION,"<<PyMacAppException>>","Could not get exception class name?"]);
			break;
		}
		
		PyObject *v = PySys_GetObject("last_value");
		PyObject *exceptionName = NULL;
		if ( v )
			exceptionName = PyObject_Str(v);
				
		NSString *nsExceptionClassName = [NSString stringWithCString:PyString_AsString(exceptionClassName)];
		Py_XDECREF(exceptionClassName);exceptionClassName = NULL;
		NSString *nsExceptionName;
		if ( exceptionName ) {
			nsExceptionName = [NSString stringWithCString:PyString_AsString(exceptionName)];
			Py_XDECREF(exceptionName);exceptionName = NULL;
		} else {
			nsExceptionName = @"";
		}
		rval = report_script_error([NSString stringWithFormat:ERR_PYTHONEXCEPTION, nsExceptionClassName, nsExceptionName], nsExceptionClassName, nsExceptionName);
		break;
	}

	[pythonPool release];

	Py_Finalize();

	[pythonProgramName release];
	
	return rval;
}


int main(int argc, char * const *argv, char * const *envp)
{
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	int rval = pyobjc_main(argc, argv, envp);
	[pool release];
	return rval;
}